-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.tsx
99 lines (89 loc) · 2.41 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type React from 'react';
import {
findNodeHandle,
NativeModules,
Platform,
ScrollView,
TextStyle,
} from 'react-native';
const LINKING_ERROR =
`The package 'react-native-jsi-view-helpers' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo managed workflow\n';
const JsiViewHelpers = NativeModules.JsiViewHelpers
? NativeModules.JsiViewHelpers
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
JsiViewHelpers.install();
export interface MeasureParams {
text: string;
fontSize: number;
maxWidth: number;
allowFontScaling?: boolean;
usePreciseWidth?: boolean;
fontFamily?: string;
weight?: TextStyle['fontWeight'];
}
export interface MeasureTextResult {
height: number;
width: number;
lineCount: number;
lastLineWidth: number;
}
export interface MeasureViewResult {
height: number;
width: number;
x: number;
y: number;
}
export class viewHelpers {
static measureText(params: MeasureParams): MeasureTextResult {
// @ts-ignore
return global.__viewHelpers.measureText(params);
}
static measureView(ref: React.RefObject<any>): MeasureViewResult {
const viewId = findNodeHandle(ref.current);
if (!viewId) return { width: 0, height: 0, x: 0, y: 0 };
// @ts-ignore
return global.__viewHelpers.measureView(viewId);
}
static measureViewByNativeId(nativeID: string): MeasureViewResult {
// @ts-ignore
return global.__viewHelpers.measureViewByNativeId(nativeID);
}
static scrollToChild(
params:
| {
scrollNativeID: string;
childNativeID: string;
offset?: number;
scrollToEnd?: boolean;
}
| {
scrollViewRef: React.RefObject<ScrollView>;
childNativeID: string;
offset?: number;
scrollToEnd?: boolean;
}
) {
// @ts-ignore
return global.__viewHelpers.scrollToChild({
scrollNativeID:
'scrollNativeID' in params ? params.scrollNativeID : undefined,
scrollViewId:
'scrollViewRef' in params
? findNodeHandle(params.scrollViewRef.current)
: undefined,
childNativeID: params.childNativeID,
offset: params.offset ?? 0,
scrollToEnd: params.scrollToEnd ?? false,
});
}
}