-
Notifications
You must be signed in to change notification settings - Fork 1
/
pintura.html
261 lines (222 loc) · 9.68 KB
/
pintura.html
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pintura Editor</title>
<!-- Pintura editor core styles -->
<style>
/*__PINTURA_CSS__*/
/*__PINTURA_VIDEO_CSS__*/
</style>
<!-- Styles for the base page -->
<style>
html {
font-family: sans-serif;
font-size: 16px;
}
html,
body {
padding: 0;
margin: 0;
height: 100%;
}
#app {
width: 100%;
height: 100%;
}
</style>
<!-- This will contain custom editor styles set through `styleRules` property on proxy component -->
<style id="editor-styles"></style>
</head>
<body>
<div id="app"></div>
<script>
// This replaces undefined values in outgoing messages so they're not lost
const stringifyMessage = (obj) =>
JSON.stringify(obj, (k, v) => (v === undefined ? '__UNDEFINED__' : v));
// This deep replaces object values with a replacer function
const deepReplaceValues = (obj, replacer) => {
let out = Array.isArray(obj) ? [...obj] : { ...obj };
Object.entries(out).forEach(([key, value]) => {
if (Array.isArray(value) || typeof value === 'object') {
out[key] = deepReplaceValues(value, replacer);
} else {
out[key] = replacer(key, value);
}
});
return out;
};
// this restores undefined and function values
const replaceValues = (key, value) => {
// only replacing special strings, ignore reset
if (typeof value !== 'string') return value;
// should be set to undefined
if (value === '__UNDEFINED__') return undefined;
// is a function __FUNCTION__foo,bar,baz____body
if (value.startsWith('__FUNCTION__')) {
const parts = value.substring(12).split('____');
return new Function(parts[0], parts[1]);
}
return value;
};
const parseMessage = (str) => deepReplaceValues(JSON.parse(str), replaceValues);
// This enables using console.log for debugging
console.log = (...args) => {
if (!('ReactNativeWebView' in window)) return;
window.ReactNativeWebView.postMessage(
stringifyMessage({ type: 'log', detail: args })
);
};
// Converts a Blob to a dataURL, with React Native we cannot send Blobs through window.postMessage
const toDateURI = (file) =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(file);
});
// Converts an event object entry to a property (basically encodes any Blob values)
const toMessageProp = ([key, value]) =>
new Promise((resolve) => {
if (value instanceof Blob) {
/*__WEBM_DURATION_FIX_APPLY__*/
toDateURI(value).then((str) => resolve([key, str]));
return;
}
resolve([key, value]);
});
// All editor events are routed here so we can send them to the React Native layer
const handleEditorEvent = (type, detail) => {
// filter out data uri's from events so messages are faster
if (type === 'load') {
// remove original image source
delete detail.src;
// remove resulting file object as used by editor
delete detail.dest;
} else if (type === 'loadpreview') {
// remove preview data
detail = {};
} else if (type === 'process') {
// remove source image
delete detail.src;
}
// is plain string
if (typeof detail === 'string' || typeof detail === 'number') {
window.ReactNativeWebView.postMessage(stringifyMessage({ type, detail }));
return;
}
// create message for arrays and object
Promise.all(Object.entries(detail || {}).map(toMessageProp)).then((entries) => {
const detail = entries.reduce((prev, [key, value]) => {
prev[key] = value;
return prev;
}, {});
let message;
try {
message = stringifyMessage({ type, detail });
} catch (err) {
alert(err);
return;
}
window.ReactNativeWebView.postMessage(message);
});
};
// Go!
const initEditorProxy = () => {
// Default editor
const {
setPlugins,
appendDefaultEditor,
createDefaultImageReader,
createDefaultImageWriter,
createDefaultMediaWriter,
createMarkupEditorShapeStyleControls,
imageStateToCanvas,
} = pintura;
/*__PINTURA_VIDEO_INIT__*/
// Set properties that can't be set from the native layer in the object below
const editorCustomOptions = {
//
// Your properties here
//
};
// Create the editor
const editor = appendDefaultEditor(window.app, {
// merge custom options
...editorCustomOptions,
/*__PINTURA_VIDEO_OPTIONS__*/
// Route editor events to the React Native layer
handleEvent: handleEditorEvent,
});
// Allow comms with editor from React Native layer
const handleMessage = (event) => {
// parse options
let msg;
try {
msg = parseMessage(event.data);
} catch (err) {
alert(err);
return;
}
// is a `style` object, let's update the styles
if (msg.editorStyleRules) {
const styles = document.getElementById('editor-styles');
styles.textContent = msg.editorStyleRules;
}
// is an `options` object? lets assign to editor
if (msg.editorOptions) {
if (msg.editorOptions.markupEditorShapeStyleControls) {
try {
const controls = createMarkupEditorShapeStyleControls(
msg.editorOptions.markupEditorShapeStyleControls
);
msg.editorOptions.markupEditorShapeStyleControls = controls;
} catch (err) {
console.log(err.message);
}
}
if (msg.editorOptions.imageReader) {
msg.editorOptions.imageReader = createDefaultImageReader(
msg.editorOptions.imageReader
);
}
if (msg.editorOptions.imageWriter) {
msg.editorOptions.imageWriter = createDefaultImageWriter(
msg.editorOptions.imageWriter
);
}
/*__PINTURA_VIDEO_WRITER__*/
return Object.assign(editor, msg.editorOptions);
}
// is a function perhaps, lets run it
if (msg.editorFunction) {
const [name, ...args] = msg.editorFunction;
const method = name.split('.').reduce((prev, curr) => {
return prev[curr];
}, editor);
if (!method) return;
method(...args);
}
};
// for Android
document.addEventListener('message', handleMessage);
// for iOS
window.addEventListener('message', handleMessage);
// now ready to receive messages
window.ReactNativeWebView.postMessage(
stringifyMessage({
type: 'webviewloaded',
})
);
};
// Wait for page to load, then set up proxy
document.addEventListener('DOMContentLoaded', initEditorProxy);
</script>
<script>
/*__PINTURA_JS__*/
/*__PINTURA_VIDEO_JS__*/
/*__WEBM_DURATION_FIX_JS__*/
</script>
</body>
</html>