@mpxjs/webpack-plugin 2.9.67 → 2.9.69-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +13 -8
- package/lib/platform/json/wx/index.js +21 -8
- package/lib/platform/style/wx/index.js +35 -38
- package/lib/platform/template/wx/component-config/canvas.js +8 -0
- package/lib/platform/template/wx/component-config/fix-component-name.js +15 -12
- package/lib/platform/template/wx/component-config/index.js +1 -1
- package/lib/platform/template/wx/component-config/input.js +1 -1
- package/lib/platform/template/wx/component-config/rich-text.js +8 -0
- package/lib/platform/template/wx/component-config/swiper.js +1 -1
- package/lib/platform/template/wx/component-config/textarea.js +1 -1
- package/lib/platform/template/wx/component-config/unsupported.js +1 -1
- package/lib/react/processStyles.js +14 -4
- package/lib/react/processTemplate.js +3 -0
- package/lib/resolver/AddModePlugin.js +8 -8
- package/lib/runtime/components/react/context.ts +6 -0
- package/lib/runtime/components/react/dist/context.js +2 -0
- package/lib/runtime/components/react/dist/event.config.js +24 -24
- package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
- package/lib/runtime/components/react/dist/mpx-button.jsx +77 -49
- package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
- package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
- package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
- package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
- package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
- package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
- package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
- package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
- package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
- package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +13 -19
- package/lib/runtime/components/react/dist/mpx-checkbox.jsx +29 -38
- package/lib/runtime/components/react/dist/mpx-form.jsx +16 -19
- package/lib/runtime/components/react/dist/mpx-icon.jsx +8 -16
- package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
- package/lib/runtime/components/react/dist/mpx-input.jsx +54 -27
- package/lib/runtime/components/react/dist/mpx-label.jsx +15 -22
- package/lib/runtime/components/react/dist/mpx-movable-area.jsx +13 -16
- package/lib/runtime/components/react/dist/mpx-movable-view.jsx +13 -13
- package/lib/runtime/components/react/dist/mpx-navigator.jsx +2 -4
- package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
- package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
- package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
- package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
- package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
- package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
- package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
- package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
- package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
- package/lib/runtime/components/react/dist/mpx-radio-group.jsx +11 -19
- package/lib/runtime/components/react/dist/mpx-radio.jsx +27 -42
- package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
- package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
- package/lib/runtime/components/react/dist/mpx-root-portal.jsx +6 -4
- package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +47 -41
- package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
- package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +4 -2
- package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
- package/lib/runtime/components/react/dist/mpx-switch.jsx +20 -10
- package/lib/runtime/components/react/dist/mpx-text.jsx +11 -10
- package/lib/runtime/components/react/dist/mpx-textarea.jsx +8 -3
- package/lib/runtime/components/react/dist/mpx-view.jsx +65 -61
- package/lib/runtime/components/react/dist/mpx-web-view.jsx +112 -35
- package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
- package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
- package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
- package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
- package/lib/runtime/components/react/dist/useAnimationHooks.js +35 -9
- package/lib/runtime/components/react/dist/utils.jsx +70 -23
- package/lib/runtime/components/react/event.config.ts +25 -26
- package/lib/runtime/components/react/getInnerListeners.ts +237 -199
- package/lib/runtime/components/react/mpx-button.tsx +104 -57
- package/lib/runtime/components/react/mpx-canvas/Bus.ts +70 -0
- package/lib/runtime/components/react/mpx-canvas/CanvasGradient.ts +18 -0
- package/lib/runtime/components/react/mpx-canvas/CanvasRenderingContext2D.ts +87 -0
- package/lib/runtime/components/react/mpx-canvas/Image.ts +102 -0
- package/lib/runtime/components/react/mpx-canvas/ImageData.ts +23 -0
- package/lib/runtime/components/react/mpx-canvas/constructorsRegistry.ts +38 -0
- package/lib/runtime/components/react/mpx-canvas/html.ts +343 -0
- package/lib/runtime/components/react/mpx-canvas/index.tsx +296 -0
- package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
- package/lib/runtime/components/react/mpx-checkbox-group.tsx +28 -25
- package/lib/runtime/components/react/mpx-checkbox.tsx +48 -49
- package/lib/runtime/components/react/mpx-form.tsx +25 -28
- package/lib/runtime/components/react/mpx-icon.tsx +12 -17
- package/lib/runtime/components/react/mpx-image.tsx +436 -0
- package/lib/runtime/components/react/mpx-input.tsx +77 -57
- package/lib/runtime/components/react/mpx-label.tsx +26 -27
- package/lib/runtime/components/react/mpx-movable-area.tsx +18 -23
- package/lib/runtime/components/react/mpx-movable-view.tsx +21 -25
- package/lib/runtime/components/react/mpx-navigator.tsx +2 -8
- package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
- package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
- package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
- package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
- package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
- package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
- package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
- package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
- package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
- package/lib/runtime/components/react/mpx-radio-group.tsx +24 -27
- package/lib/runtime/components/react/mpx-radio.tsx +45 -54
- package/lib/runtime/components/react/mpx-rich-text/html.ts +40 -0
- package/lib/runtime/components/react/mpx-rich-text/index.tsx +121 -0
- package/lib/runtime/components/react/mpx-root-portal.tsx +3 -5
- package/lib/runtime/components/react/mpx-scroll-view.tsx +72 -71
- package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
- package/lib/runtime/components/react/mpx-swiper/carouse.tsx +4 -2
- package/lib/runtime/components/react/mpx-swiper-item.tsx +3 -2
- package/lib/runtime/components/react/mpx-switch.tsx +29 -23
- package/lib/runtime/components/react/mpx-text.tsx +14 -18
- package/lib/runtime/components/react/mpx-textarea.tsx +11 -10
- package/lib/runtime/components/react/mpx-view.tsx +92 -76
- package/lib/runtime/components/react/mpx-web-view.tsx +116 -54
- package/lib/runtime/components/react/pickerFaces.ts +104 -0
- package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
- package/lib/runtime/components/react/types/common.ts +2 -0
- package/lib/runtime/components/react/types/global.d.ts +5 -17
- package/lib/runtime/components/react/useAnimationHooks.ts +36 -11
- package/lib/runtime/components/react/utils.tsx +99 -28
- package/lib/runtime/components/web/getInnerListeners.js +6 -6
- package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
- package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
- package/lib/runtime/components/web/mpx-picker.vue +382 -385
- package/lib/runtime/components/web/mpx-web-view.vue +175 -161
- package/lib/runtime/optionProcessor.js +7 -38
- package/lib/runtime/utils.js +2 -0
- package/lib/style-compiler/plugins/scope-id.js +30 -2
- package/lib/template-compiler/bind-this.js +7 -2
- package/lib/template-compiler/compiler.js +79 -47
- package/lib/template-compiler/gen-node-react.js +3 -3
- package/lib/utils/pre-process-json.js +9 -5
- package/package.json +6 -4
- package/LICENSE +0 -433
- package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
- package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
- package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
- package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
- package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
- package/lib/runtime/components/react/mpx-image/index.tsx +0 -345
- package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
- package/lib/runtime/components/web/event.js +0 -105
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
export default `<html><head>
|
|
2
|
+
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scaleable=no" name="viewport">
|
|
3
|
+
<style>
|
|
4
|
+
html {
|
|
5
|
+
-ms-content-zooming: none;
|
|
6
|
+
-ms-touch-action: pan-x pan-y;
|
|
7
|
+
}
|
|
8
|
+
body {
|
|
9
|
+
position: fixed;
|
|
10
|
+
top: 0;
|
|
11
|
+
right: 0;
|
|
12
|
+
bottom: 0;
|
|
13
|
+
left: 0;
|
|
14
|
+
margin: 0;
|
|
15
|
+
padding: 0;
|
|
16
|
+
overflow: hidden;
|
|
17
|
+
}
|
|
18
|
+
* {
|
|
19
|
+
user-select: none;
|
|
20
|
+
-ms-user-select: none;
|
|
21
|
+
-moz-user-select: none;
|
|
22
|
+
-webkit-user-select: none;
|
|
23
|
+
}
|
|
24
|
+
</style>
|
|
25
|
+
</head>
|
|
26
|
+
<body>
|
|
27
|
+
<script>
|
|
28
|
+
var scale = function (ratio) {
|
|
29
|
+
return function (item) {
|
|
30
|
+
if (typeof item === "number") {
|
|
31
|
+
return item * ratio;
|
|
32
|
+
}
|
|
33
|
+
return item;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
function autoScaleCanvas(canvas) {
|
|
37
|
+
var ctx = canvas.getContext("2d");
|
|
38
|
+
var ratio = window.devicePixelRatio || 1;
|
|
39
|
+
if (ratio !== 1) {
|
|
40
|
+
canvas.width *= ratio;
|
|
41
|
+
canvas.height *= ratio;
|
|
42
|
+
ctx.scale(ratio, ratio);
|
|
43
|
+
ctx.isPointInPath = function () {
|
|
44
|
+
var args = [];
|
|
45
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
46
|
+
args[_i] = arguments[_i];
|
|
47
|
+
}
|
|
48
|
+
return CanvasRenderingContext2D.prototype.isPointInPath.apply(ctx, args.map(scale(ratio)));
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return canvas;
|
|
52
|
+
}
|
|
53
|
+
window.autoScaleCanvas = autoScaleCanvas;
|
|
54
|
+
</script>
|
|
55
|
+
<script>
|
|
56
|
+
|
|
57
|
+
var WEBVIEW_TARGET = '@@WEBVIEW_TARGET';
|
|
58
|
+
|
|
59
|
+
var ID = function () {
|
|
60
|
+
return Math.random().toString(32).slice(2);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
var flattenObjectCopyValue = function (flatObj, srcObj, key) {
|
|
64
|
+
var value = srcObj[key];
|
|
65
|
+
if (typeof value === 'function') {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (typeof value === 'object' && value instanceof Node) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
flatObj[key] = flattenObject(value);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
var flattenObject = function (object) {
|
|
75
|
+
if (typeof object !== 'object' || object === null) {
|
|
76
|
+
return object;
|
|
77
|
+
}
|
|
78
|
+
// Handle TypedArray
|
|
79
|
+
if (object instanceof Uint8ClampedArray) {
|
|
80
|
+
return Array.from(object);
|
|
81
|
+
}
|
|
82
|
+
var flatObject = {};
|
|
83
|
+
for (var key in object) {
|
|
84
|
+
flattenObjectCopyValue(flatObject, object, key);
|
|
85
|
+
}
|
|
86
|
+
for (var key in Object.getOwnPropertyNames(object)) {
|
|
87
|
+
flattenObjectCopyValue(flatObject, object, key);
|
|
88
|
+
}
|
|
89
|
+
return flatObject;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
var AutoScaledCanvas = function (element) {
|
|
93
|
+
this.element = element;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
AutoScaledCanvas.prototype.toDataURL = function () {
|
|
97
|
+
return this.element.toDataURL.apply(this.element, arguments);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
AutoScaledCanvas.prototype.autoScale = function () {
|
|
101
|
+
if (this.savedHeight !== undefined) {
|
|
102
|
+
this.element.height = this.savedHeight;
|
|
103
|
+
}
|
|
104
|
+
if (this.savedWidth !== undefined) {
|
|
105
|
+
this.element.width = this.savedWidth;
|
|
106
|
+
}
|
|
107
|
+
window.autoScaleCanvas(this.element);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
Object.defineProperty(AutoScaledCanvas.prototype, 'width', {
|
|
111
|
+
get: function () {
|
|
112
|
+
return this.element.width;
|
|
113
|
+
},
|
|
114
|
+
set: function (value) {
|
|
115
|
+
this.savedWidth = value;
|
|
116
|
+
this.autoScale();
|
|
117
|
+
return value;
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
Object.defineProperty(AutoScaledCanvas.prototype, 'height', {
|
|
122
|
+
get: function () {
|
|
123
|
+
return this.element.height;
|
|
124
|
+
},
|
|
125
|
+
set: function (value) {
|
|
126
|
+
this.savedHeight = value;
|
|
127
|
+
this.autoScale();
|
|
128
|
+
return value;
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
var toMessage = function (result) {
|
|
132
|
+
if (result instanceof Blob) {
|
|
133
|
+
return {
|
|
134
|
+
type: 'blob',
|
|
135
|
+
payload: btoa(result),
|
|
136
|
+
meta: {},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (result instanceof Object) {
|
|
140
|
+
if (!result[WEBVIEW_TARGET]) {
|
|
141
|
+
var id = ID();
|
|
142
|
+
result[WEBVIEW_TARGET] = id;
|
|
143
|
+
targets[id] = result;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
type: 'json',
|
|
147
|
+
payload: flattenObject(result),
|
|
148
|
+
args: toArgs(flattenObject(result)),
|
|
149
|
+
meta: {
|
|
150
|
+
target: result[WEBVIEW_TARGET],
|
|
151
|
+
constructor: result.__constructorName__ || result.constructor.name,
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
type: 'json',
|
|
157
|
+
payload: typeof result === 'string' ? result : JSON.stringify(result),
|
|
158
|
+
meta: {},
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
var toArgs = function (result) {
|
|
162
|
+
var args = [];
|
|
163
|
+
for (var key in result) {
|
|
164
|
+
if (result[key] !== undefined && key !== '@@WEBVIEW_TARGET') {
|
|
165
|
+
args.push(result[key]);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return args;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
var createObjectsFromArgs = function (args) {
|
|
172
|
+
for (var index = 0; index < args.length; index += 1) {
|
|
173
|
+
var currentArg = args[index];
|
|
174
|
+
if (currentArg && currentArg.className !== undefined) {
|
|
175
|
+
var className = currentArg.className, classArgs = currentArg.classArgs;
|
|
176
|
+
// new ImageData,第一个参数需要是 Uint8ClampedArray
|
|
177
|
+
var object = new (Function.prototype.bind.apply(constructors[className], [null].concat(classArgs)))();
|
|
178
|
+
args[index] = object;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return args;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
var canvas = document.createElement('canvas');
|
|
185
|
+
canvas.style.width = '100%';
|
|
186
|
+
canvas.style.height = '100%';
|
|
187
|
+
var autoScaledCanvas = new AutoScaledCanvas(canvas);
|
|
188
|
+
|
|
189
|
+
var targets = {
|
|
190
|
+
canvas: autoScaledCanvas,
|
|
191
|
+
context2D: canvas.getContext('2d'),
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
var constructors = {
|
|
195
|
+
CanvasGradient: CanvasGradient,
|
|
196
|
+
Image: Image,
|
|
197
|
+
ImageData: ImageData,
|
|
198
|
+
Uint8ClampedArray: Uint8ClampedArray,
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
Image.bind =
|
|
202
|
+
Image.bind ||
|
|
203
|
+
function () {
|
|
204
|
+
return Image;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
ImageData.bind =
|
|
208
|
+
ImageData.bind ||
|
|
209
|
+
function () {
|
|
210
|
+
return ImageData;
|
|
211
|
+
};
|
|
212
|
+
Uint8ClampedArray.bind =
|
|
213
|
+
Uint8ClampedArray.bind ||
|
|
214
|
+
function () {
|
|
215
|
+
return Uint8ClampedArray;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
var populateRefs = function (arg) {
|
|
219
|
+
if (arg && arg.__ref__) {
|
|
220
|
+
return targets[arg.__ref__];
|
|
221
|
+
}
|
|
222
|
+
return arg;
|
|
223
|
+
};
|
|
224
|
+
document.body.appendChild(canvas);
|
|
225
|
+
|
|
226
|
+
var mergeObjects = function (target, source) {
|
|
227
|
+
for (var key in source) {
|
|
228
|
+
if (source.hasOwnProperty(key)) {
|
|
229
|
+
target[key] = source[key];
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return target;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
function handleMessage(message) {
|
|
236
|
+
var id = message.id,
|
|
237
|
+
type = message.type,
|
|
238
|
+
payload = message.payload;
|
|
239
|
+
|
|
240
|
+
switch (type) {
|
|
241
|
+
case 'exec': {
|
|
242
|
+
var target = payload.target,
|
|
243
|
+
method = payload.method,
|
|
244
|
+
args = payload.args;
|
|
245
|
+
var result = targets[target][method].apply(targets[target], args.map(populateRefs));
|
|
246
|
+
var msg = toMessage(result);
|
|
247
|
+
|
|
248
|
+
if (typeof result === 'object' && !msg.meta.constructor) {
|
|
249
|
+
for (var constructorName in constructors) {
|
|
250
|
+
if (result instanceof constructors[constructorName]) {
|
|
251
|
+
msg.meta.constructor = constructorName;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
window.ReactNativeWebView.postMessage(JSON.stringify(mergeObjects({ id: id }, msg)));
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
case 'set': {
|
|
259
|
+
var target = payload.target,
|
|
260
|
+
key = payload.key,
|
|
261
|
+
value = payload.value;
|
|
262
|
+
targets[target][key] = populateRefs(value);
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
case 'construct': {
|
|
266
|
+
var constructor = payload.constructor,
|
|
267
|
+
target = payload.id,
|
|
268
|
+
args = payload.args || [];
|
|
269
|
+
var newArgs = createObjectsFromArgs(args);
|
|
270
|
+
var object;
|
|
271
|
+
try {
|
|
272
|
+
object = new (Function.prototype.bind.apply(constructors[constructor], [null].concat(newArgs)))();
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
throw new Error('Error while constructing '.concat(constructor, ' ').concat(error.message));
|
|
276
|
+
}
|
|
277
|
+
object.__constructorName__ = constructor;
|
|
278
|
+
var msg = toMessage({});
|
|
279
|
+
targets[target] = object;
|
|
280
|
+
window.ReactNativeWebView.postMessage(JSON.stringify(mergeObjects({ id: id }, msg)));
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
case 'listen': {
|
|
284
|
+
var types = payload.types,
|
|
285
|
+
target = payload.target;
|
|
286
|
+
for (var i = 0; i < types.length; i++) {
|
|
287
|
+
var eventType = types[i];
|
|
288
|
+
targets[target].addEventListener(eventType, function (e) {
|
|
289
|
+
const message = toMessage({
|
|
290
|
+
type: 'event',
|
|
291
|
+
payload: {
|
|
292
|
+
type: e.type,
|
|
293
|
+
target: mergeObjects(flattenObject(targets[target]), {
|
|
294
|
+
[WEBVIEW_TARGET]: target,
|
|
295
|
+
}),
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
window.ReactNativeWebView.postMessage(
|
|
299
|
+
JSON.stringify(mergeObjects({ id: id }, message))
|
|
300
|
+
);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
var handleError = function (err, message) {
|
|
308
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
309
|
+
id: message.id,
|
|
310
|
+
type: 'error',
|
|
311
|
+
payload: {
|
|
312
|
+
message: err.message,
|
|
313
|
+
stack: err.stack,
|
|
314
|
+
},
|
|
315
|
+
}));
|
|
316
|
+
document.removeEventListener('message', handleIncomingMessage);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
function handleIncomingMessage(e) {
|
|
320
|
+
var data = JSON.parse(e.data);
|
|
321
|
+
if (Array.isArray(data)) {
|
|
322
|
+
for (var i = 0; i < data.length; i++) {
|
|
323
|
+
try {
|
|
324
|
+
handleMessage(data[i]);
|
|
325
|
+
} catch (err) {
|
|
326
|
+
handleError(err, data[i]);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
} else {
|
|
330
|
+
try {
|
|
331
|
+
handleMessage(data);
|
|
332
|
+
} catch (err) {
|
|
333
|
+
handleError(err, data);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
window.addEventListener('message', handleIncomingMessage);
|
|
339
|
+
document.addEventListener('message', handleIncomingMessage);
|
|
340
|
+
</script>
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
</body></html>`
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ✘ type
|
|
3
|
+
* ✘ canvas-id
|
|
4
|
+
* ✘ disable-scroll
|
|
5
|
+
* ✔ bindtouchstart
|
|
6
|
+
* ✔ bindtouchmove
|
|
7
|
+
* ✔ bindtouchend
|
|
8
|
+
* ✔ bindtouchcancel
|
|
9
|
+
* ✔ bindlongtap
|
|
10
|
+
* ✔ binderror
|
|
11
|
+
*/
|
|
12
|
+
import React, { createElement, useRef, useState, useCallback, useEffect, forwardRef, JSX, TouchEvent, MutableRefObject } from 'react'
|
|
13
|
+
import { View, Platform, StyleSheet, NativeSyntheticEvent } from 'react-native'
|
|
14
|
+
import { WebView } from 'react-native-webview'
|
|
15
|
+
import useNodesRef, { HandlerRef } from '../useNodesRef'
|
|
16
|
+
import { useLayout, useTransformStyle, extendObject } from '../utils'
|
|
17
|
+
import useInnerProps, { getCustomEvent } from '../getInnerListeners'
|
|
18
|
+
import Bus from './Bus'
|
|
19
|
+
import {
|
|
20
|
+
useWebviewBinding,
|
|
21
|
+
constructors,
|
|
22
|
+
WEBVIEW_TARGET,
|
|
23
|
+
WebviewMessage,
|
|
24
|
+
ID,
|
|
25
|
+
CanvasInstance,
|
|
26
|
+
registerWebviewConstructor
|
|
27
|
+
} from './utils'
|
|
28
|
+
import CanvasRenderingContext2D from './CanvasRenderingContext2D'
|
|
29
|
+
import html from './html'
|
|
30
|
+
import './CanvasGradient'
|
|
31
|
+
import { createImage as canvasCreateImage } from './Image'
|
|
32
|
+
import { createImageData as canvasCreateImageData } from './ImageData'
|
|
33
|
+
import { useConstructorsRegistry } from './constructorsRegistry'
|
|
34
|
+
|
|
35
|
+
const stylesheet = StyleSheet.create({
|
|
36
|
+
container: { overflow: 'hidden', flex: 0 },
|
|
37
|
+
webview: {
|
|
38
|
+
overflow: 'hidden',
|
|
39
|
+
backgroundColor: 'transparent',
|
|
40
|
+
flex: 0
|
|
41
|
+
},
|
|
42
|
+
webviewAndroid9: {
|
|
43
|
+
overflow: 'hidden',
|
|
44
|
+
backgroundColor: 'transparent',
|
|
45
|
+
flex: 0,
|
|
46
|
+
opacity: 0.99
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
interface CanvasProps {
|
|
51
|
+
style?: Record<string, any>;
|
|
52
|
+
originWhitelist?: Array<string>;
|
|
53
|
+
'enable-var'?: boolean
|
|
54
|
+
'parent-font-size'?: number
|
|
55
|
+
'parent-width'?: number
|
|
56
|
+
'parent-height'?: number
|
|
57
|
+
'external-var-context'?: Record<string, any>
|
|
58
|
+
bindtouchstart?: (event: NativeSyntheticEvent<TouchEvent>) => void;
|
|
59
|
+
bindtouchmove?: (event: NativeSyntheticEvent<TouchEvent>) => void;
|
|
60
|
+
bindtouchend?: (event: NativeSyntheticEvent<TouchEvent>) => void;
|
|
61
|
+
bindtouchcancel?: (event: NativeSyntheticEvent<TouchEvent>) => void;
|
|
62
|
+
bindlongtap?: (event: NativeSyntheticEvent<TouchEvent>) => void;
|
|
63
|
+
binderror?: (event: NativeSyntheticEvent<ErrorEvent>) => void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const _Canvas = forwardRef<HandlerRef<CanvasProps & View, CanvasProps>, CanvasProps>((props: CanvasProps = {}, ref): JSX.Element => {
|
|
67
|
+
const { style = {}, originWhitelist = ['*'], 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props
|
|
68
|
+
const [isLoaded, setIsLoaded] = useState(false)
|
|
69
|
+
const nodeRef = useRef(null)
|
|
70
|
+
|
|
71
|
+
const {
|
|
72
|
+
normalStyle,
|
|
73
|
+
hasSelfPercent,
|
|
74
|
+
setWidth,
|
|
75
|
+
setHeight
|
|
76
|
+
} = useTransformStyle(extendObject({}, style, stylesheet.container), {
|
|
77
|
+
enableVar,
|
|
78
|
+
externalVarContext,
|
|
79
|
+
parentFontSize,
|
|
80
|
+
parentWidth,
|
|
81
|
+
parentHeight
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const { width, height } = normalStyle
|
|
85
|
+
const canvasRef = useWebviewBinding({
|
|
86
|
+
targetName: 'canvas',
|
|
87
|
+
properties: { width, height },
|
|
88
|
+
methods: ['toDataURL']
|
|
89
|
+
}) as MutableRefObject<CanvasInstance>
|
|
90
|
+
|
|
91
|
+
const { register } = useConstructorsRegistry()
|
|
92
|
+
|
|
93
|
+
const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
|
|
94
|
+
const innerProps = useInnerProps(props, {
|
|
95
|
+
ref: nodeRef,
|
|
96
|
+
style: extendObject({}, normalStyle, layoutStyle, { opacity: isLoaded ? 1 : 0 }),
|
|
97
|
+
...layoutProps
|
|
98
|
+
}, [], {
|
|
99
|
+
layoutRef
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
const context2D = new CanvasRenderingContext2D(canvasRef.current) as any
|
|
103
|
+
|
|
104
|
+
register(registerWebviewConstructor)
|
|
105
|
+
// 初始化bus和context2D
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
const webviewPostMessage = (message: WebviewMessage) => {
|
|
108
|
+
if (canvasRef.current.webview) {
|
|
109
|
+
canvasRef.current.webview.postMessage(JSON.stringify(message))
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 设置bus
|
|
114
|
+
canvasRef.current.bus = new Bus(webviewPostMessage)
|
|
115
|
+
canvasRef.current.bus.pause()
|
|
116
|
+
|
|
117
|
+
// 设置 context 2D
|
|
118
|
+
canvasRef.current.context2D = context2D
|
|
119
|
+
|
|
120
|
+
// 设置 getContext 方法
|
|
121
|
+
canvasRef.current.getContext = getContext
|
|
122
|
+
|
|
123
|
+
// 设置 createImage 方法
|
|
124
|
+
canvasRef.current.createImage = createImage
|
|
125
|
+
|
|
126
|
+
// 设置 postMessage 方法
|
|
127
|
+
canvasRef.current.postMessage = postMessage
|
|
128
|
+
|
|
129
|
+
// 设置 listeners
|
|
130
|
+
canvasRef.current.listeners = []
|
|
131
|
+
|
|
132
|
+
canvasRef.current.addMessageListener = addMessageListener
|
|
133
|
+
|
|
134
|
+
canvasRef.current.removeMessageListener = removeMessageListener
|
|
135
|
+
|
|
136
|
+
canvasRef.current.createImageData = createImageData
|
|
137
|
+
return () => {
|
|
138
|
+
canvasRef.current.bus?.clearBatchingTimeout()
|
|
139
|
+
}
|
|
140
|
+
}, [])
|
|
141
|
+
|
|
142
|
+
const createImageData = (dataArray: Array<number>, width: number, height: number) => {
|
|
143
|
+
return canvasCreateImageData(canvasRef.current, dataArray, width, height)
|
|
144
|
+
}
|
|
145
|
+
const createImage = (width?: number, height?: number) => {
|
|
146
|
+
return canvasCreateImage(canvasRef.current, width, height)
|
|
147
|
+
}
|
|
148
|
+
const getContext = useCallback((contextType: string) => {
|
|
149
|
+
if (contextType === '2d') {
|
|
150
|
+
return context2D
|
|
151
|
+
}
|
|
152
|
+
return null
|
|
153
|
+
}, [])
|
|
154
|
+
|
|
155
|
+
const postMessage = useCallback(async (message: WebviewMessage) => {
|
|
156
|
+
if (!canvasRef.current?.bus) return
|
|
157
|
+
const { type, payload } = await canvasRef.current.bus.post({
|
|
158
|
+
id: ID(),
|
|
159
|
+
...message
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
switch (type) {
|
|
163
|
+
case 'error': {
|
|
164
|
+
const { binderror } = props
|
|
165
|
+
binderror &&
|
|
166
|
+
binderror(
|
|
167
|
+
getCustomEvent('error', {}, {
|
|
168
|
+
detail: {
|
|
169
|
+
errMsg: payload.message
|
|
170
|
+
},
|
|
171
|
+
layoutRef
|
|
172
|
+
}, props)
|
|
173
|
+
)
|
|
174
|
+
break
|
|
175
|
+
}
|
|
176
|
+
case 'json': {
|
|
177
|
+
return payload
|
|
178
|
+
}
|
|
179
|
+
case 'blob': {
|
|
180
|
+
return atob(payload)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}, [])
|
|
184
|
+
|
|
185
|
+
const addMessageListener = (listener: any) => {
|
|
186
|
+
canvasRef.current.listeners.push(listener)
|
|
187
|
+
return () => canvasRef.current.removeMessageListener(listener)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const removeMessageListener = (listener: any) => {
|
|
191
|
+
canvasRef.current.listeners.splice(canvasRef.current.listeners.indexOf(listener), 1)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const onMessage = useCallback((e: { nativeEvent: { data: string } }) => {
|
|
195
|
+
let data = JSON.parse(e.nativeEvent.data)
|
|
196
|
+
switch (data.type) {
|
|
197
|
+
case 'error': {
|
|
198
|
+
const { binderror } = props
|
|
199
|
+
binderror &&
|
|
200
|
+
binderror(
|
|
201
|
+
getCustomEvent('error', e, {
|
|
202
|
+
detail: {
|
|
203
|
+
errMsg: data.payload.message
|
|
204
|
+
},
|
|
205
|
+
layoutRef
|
|
206
|
+
}, props)
|
|
207
|
+
)
|
|
208
|
+
break
|
|
209
|
+
}
|
|
210
|
+
default: {
|
|
211
|
+
if (data.payload) {
|
|
212
|
+
// createLinearGradient 方法调用需要在 constructors 中需要注册 CanvasGradient
|
|
213
|
+
const constructor = constructors[data.meta.constructor]
|
|
214
|
+
if (constructor) {
|
|
215
|
+
const { args, payload } = data
|
|
216
|
+
// RN 端同步生成一个 CanvasGradient 的实例
|
|
217
|
+
const object = constructor.constructLocally(canvasRef.current, ...args)
|
|
218
|
+
Object.assign(object, payload, {
|
|
219
|
+
[WEBVIEW_TARGET]: data.meta.target
|
|
220
|
+
})
|
|
221
|
+
data = {
|
|
222
|
+
...data,
|
|
223
|
+
payload: object
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (const listener of canvasRef.current.listeners) {
|
|
227
|
+
listener(data.payload)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (canvasRef.current.bus) {
|
|
231
|
+
canvasRef.current.bus.handle(data)
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}, [])
|
|
236
|
+
|
|
237
|
+
const onLoad = useCallback(() => {
|
|
238
|
+
setIsLoaded(true)
|
|
239
|
+
if (canvasRef.current?.bus) {
|
|
240
|
+
canvasRef.current.bus.resume()
|
|
241
|
+
}
|
|
242
|
+
}, [])
|
|
243
|
+
|
|
244
|
+
useNodesRef(props, ref, nodeRef, {
|
|
245
|
+
style: normalStyle,
|
|
246
|
+
node: canvasRef.current,
|
|
247
|
+
context: context2D
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
if (Platform.OS === 'android') {
|
|
251
|
+
const isAndroid9 = Platform.Version >= 28
|
|
252
|
+
return createElement(View, innerProps, createElement(
|
|
253
|
+
WebView,
|
|
254
|
+
{
|
|
255
|
+
ref: (element) => {
|
|
256
|
+
if (canvasRef.current) {
|
|
257
|
+
canvasRef.current.webview = element
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
style: [
|
|
261
|
+
isAndroid9 ? stylesheet.webviewAndroid9 : stylesheet.webview,
|
|
262
|
+
{ height, width }
|
|
263
|
+
],
|
|
264
|
+
source: { html },
|
|
265
|
+
originWhitelist: originWhitelist,
|
|
266
|
+
onMessage: onMessage,
|
|
267
|
+
onLoad: onLoad,
|
|
268
|
+
overScrollMode: 'never',
|
|
269
|
+
mixedContentMode: 'always',
|
|
270
|
+
scalesPageToFit: false,
|
|
271
|
+
javaScriptEnabled: true,
|
|
272
|
+
domStorageEnabled: true,
|
|
273
|
+
thirdPartyCookiesEnabled: true,
|
|
274
|
+
allowUniversalAccessFromFileURLs: true
|
|
275
|
+
})
|
|
276
|
+
)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return createElement(View, innerProps, createElement(WebView, {
|
|
280
|
+
ref: (element) => {
|
|
281
|
+
if (canvasRef.current) {
|
|
282
|
+
canvasRef.current.webview = element
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
style: [stylesheet.webview, { height, width }],
|
|
286
|
+
source: { html },
|
|
287
|
+
originWhitelist: originWhitelist,
|
|
288
|
+
onMessage: onMessage,
|
|
289
|
+
onLoad: onLoad,
|
|
290
|
+
scrollEnabled: false
|
|
291
|
+
}))
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
_Canvas.displayName = 'mpxCanvas'
|
|
295
|
+
|
|
296
|
+
export default _Canvas
|