-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
81 lines (75 loc) · 2.47 KB
/
index.tsx
File metadata and controls
81 lines (75 loc) · 2.47 KB
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
import { registerRootComponent } from 'expo'
import WebView from 'react-native-webview'
import React from 'react'
import * as RNFS from 'react-native-fs'
import { Platform } from 'react-native'
import { launchImageLibrary } from 'react-native-image-picker'
import { main } from './src/main'
var webViewRef: any
const onclicks: any = {}
const onMessage = (json: any) => {
const event = JSON.parse(json.nativeEvent.data)
if (event.id === "ready") main()
if (event.id === "onclick") {
onclicks[event.value]()
}
}
const document = {
getElementById(id: string): any {
return new Proxy({}, {
set(_, prop: string, value) {
if (prop == "onclick") {
onclicks[id] = value
webViewRef.injectJavaScript(`
document.getElementById("${id}").onclick = () => {
window.ReactNativeWebView.postMessage(JSON.stringify({ id: "onclick", value: "${id}" }));
}; true
`)
} else
webViewRef.injectJavaScript(`document.getElementById("${id}").${prop} = ${parseValue(value)}; true`)
return true
},
get(_, prop: string) {
if (prop === "style") return new Proxy({}, {
set(_, styleProp: string, value) {
webViewRef.injectJavaScript(`document.getElementById("${id}").style.${styleProp} = ${parseValue(value)}; true`)
return true
}
})
if (prop === "insertAdjacentHTML") return (position: string, html: string) => {
webViewRef.injectJavaScript(`document.getElementById("${id}").insertAdjacentHTML('${position}', \`${html}\`); true`)
return true
}
return undefined
}
}
)
}
}
function parseValue(value: any) {
if (value !== true && value !== false) return `"${value}"`
return value
}
(globalThis as any).document = document;
registerRootComponent(() =>
<WebView
ref={ref => { webViewRef = ref }}
onMessage={onMessage}
source={require("./index.html")}
scrollEnabled={false}
overScrollMode={'content'}
/>
)
export async function loadAsset(path: string): Promise<string> {
if (Platform.OS === 'ios') path = RNFS.MainBundlePath + "/" + path
var readFile = Platform.OS === 'ios' ? RNFS.readFile : RNFS.readFileRes
return await readFile(path, 'base64')
}
export async function pickImage(): Promise<string | null> {
var response = await launchImageLibrary({
mediaType: 'photo',
selectionLimit: 1,
includeBase64: true
})
return response?.assets?.[0].base64!
}