@mpxjs/webpack-plugin 2.9.67 → 2.9.70-alpha.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.
Files changed (161) hide show
  1. package/README.md +1 -1
  2. package/lib/config.js +14 -0
  3. package/lib/dependencies/AddEntryDependency.js +24 -0
  4. package/lib/dependencies/ResolveDependency.js +5 -0
  5. package/lib/index.js +51 -15
  6. package/lib/json-compiler/helper.js +3 -3
  7. package/lib/loader.js +53 -0
  8. package/lib/platform/template/wx/component-config/button.js +14 -2
  9. package/lib/platform/template/wx/component-config/canvas.js +8 -0
  10. package/lib/platform/template/wx/component-config/image.js +4 -0
  11. package/lib/platform/template/wx/component-config/input.js +5 -1
  12. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  13. package/lib/platform/template/wx/component-config/scroll-view.js +4 -0
  14. package/lib/platform/template/wx/component-config/swiper.js +1 -1
  15. package/lib/platform/template/wx/component-config/switch.js +4 -0
  16. package/lib/platform/template/wx/component-config/text.js +4 -0
  17. package/lib/platform/template/wx/component-config/textarea.js +6 -1
  18. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  19. package/lib/platform/template/wx/component-config/view.js +4 -0
  20. package/lib/platform/template/wx/index.js +127 -1
  21. package/lib/react/processStyles.js +14 -4
  22. package/lib/react/processTemplate.js +3 -0
  23. package/lib/resolve-loader.js +4 -1
  24. package/lib/resolver/AddModePlugin.js +8 -8
  25. package/lib/runtime/components/react/context.ts +6 -0
  26. package/lib/runtime/components/react/dist/context.js +2 -0
  27. package/lib/runtime/components/react/dist/event.config.js +24 -24
  28. package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
  29. package/lib/runtime/components/react/dist/mpx-button.jsx +77 -49
  30. package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
  31. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
  32. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
  33. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
  34. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
  35. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
  36. package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
  37. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
  38. package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
  39. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +13 -19
  40. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +29 -38
  41. package/lib/runtime/components/react/dist/mpx-form.jsx +16 -19
  42. package/lib/runtime/components/react/dist/mpx-icon.jsx +8 -16
  43. package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
  44. package/lib/runtime/components/react/dist/mpx-input.jsx +54 -27
  45. package/lib/runtime/components/react/dist/mpx-label.jsx +15 -22
  46. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +13 -16
  47. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +13 -13
  48. package/lib/runtime/components/react/dist/mpx-navigator.jsx +2 -4
  49. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
  50. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
  51. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
  52. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
  53. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
  54. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
  55. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
  56. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
  57. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
  58. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +11 -19
  59. package/lib/runtime/components/react/dist/mpx-radio.jsx +27 -42
  60. package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
  61. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
  62. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +6 -4
  63. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +47 -41
  64. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
  65. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +4 -2
  66. package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
  67. package/lib/runtime/components/react/dist/mpx-switch.jsx +20 -10
  68. package/lib/runtime/components/react/dist/mpx-text.jsx +11 -10
  69. package/lib/runtime/components/react/dist/mpx-textarea.jsx +8 -3
  70. package/lib/runtime/components/react/dist/mpx-view.jsx +65 -61
  71. package/lib/runtime/components/react/dist/mpx-web-view.jsx +112 -35
  72. package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
  73. package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
  74. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  75. package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
  76. package/lib/runtime/components/react/dist/useAnimationHooks.js +35 -9
  77. package/lib/runtime/components/react/dist/utils.jsx +70 -23
  78. package/lib/runtime/components/react/getInnerListeners.ts +36 -43
  79. package/lib/runtime/components/react/mpx-button.tsx +95 -43
  80. package/lib/runtime/components/react/mpx-canvas/Bus.ts +70 -0
  81. package/lib/runtime/components/react/mpx-canvas/CanvasGradient.ts +18 -0
  82. package/lib/runtime/components/react/mpx-canvas/CanvasRenderingContext2D.ts +87 -0
  83. package/lib/runtime/components/react/mpx-canvas/Image.ts +102 -0
  84. package/lib/runtime/components/react/mpx-canvas/ImageData.ts +23 -0
  85. package/lib/runtime/components/react/mpx-canvas/constructorsRegistry.ts +38 -0
  86. package/lib/runtime/components/react/mpx-canvas/html.ts +343 -0
  87. package/lib/runtime/components/react/mpx-canvas/index.tsx +302 -0
  88. package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
  89. package/lib/runtime/components/react/mpx-checkbox-group.tsx +13 -12
  90. package/lib/runtime/components/react/mpx-checkbox.tsx +28 -28
  91. package/lib/runtime/components/react/mpx-form.tsx +10 -8
  92. package/lib/runtime/components/react/mpx-icon.tsx +10 -15
  93. package/lib/runtime/components/react/mpx-image.tsx +396 -0
  94. package/lib/runtime/components/react/mpx-input.tsx +61 -33
  95. package/lib/runtime/components/react/mpx-label.tsx +14 -13
  96. package/lib/runtime/components/react/mpx-movable-area.tsx +8 -7
  97. package/lib/runtime/components/react/mpx-movable-view.tsx +1 -1
  98. package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
  99. package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
  100. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
  101. package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
  102. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
  103. package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
  104. package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
  105. package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
  106. package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
  107. package/lib/runtime/components/react/mpx-radio-group.tsx +11 -12
  108. package/lib/runtime/components/react/mpx-radio.tsx +26 -29
  109. package/lib/runtime/components/react/mpx-scroll-view.tsx +32 -30
  110. package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
  111. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +4 -2
  112. package/lib/runtime/components/react/mpx-swiper-item.tsx +3 -2
  113. package/lib/runtime/components/react/mpx-switch.tsx +10 -8
  114. package/lib/runtime/components/react/mpx-text.tsx +6 -2
  115. package/lib/runtime/components/react/mpx-view.tsx +81 -59
  116. package/lib/runtime/components/react/mpx-web-view.tsx +46 -19
  117. package/lib/runtime/components/react/pickerFaces.ts +104 -0
  118. package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
  119. package/lib/runtime/components/react/types/common.ts +2 -0
  120. package/lib/runtime/components/react/types/global.d.ts +3 -16
  121. package/lib/runtime/components/react/utils.tsx +98 -27
  122. package/lib/runtime/components/tenon/getInnerListeners.js +334 -0
  123. package/lib/runtime/components/tenon/tenon-button.vue +309 -0
  124. package/lib/runtime/components/tenon/tenon-image.vue +66 -0
  125. package/lib/runtime/components/tenon/tenon-input.vue +171 -0
  126. package/lib/runtime/components/tenon/tenon-rich-text.vue +26 -0
  127. package/lib/runtime/components/tenon/tenon-scroll-view.vue +127 -0
  128. package/lib/runtime/components/tenon/tenon-switch.vue +96 -0
  129. package/lib/runtime/components/tenon/tenon-text.vue +70 -0
  130. package/lib/runtime/components/tenon/tenon-textarea.vue +86 -0
  131. package/lib/runtime/components/tenon/tenon-view.vue +93 -0
  132. package/lib/runtime/components/web/getInnerListeners.js +6 -6
  133. package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
  134. package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
  135. package/lib/runtime/components/web/mpx-picker.vue +382 -385
  136. package/lib/runtime/components/web/mpx-web-view.vue +162 -162
  137. package/lib/runtime/optionProcessor.js +7 -16
  138. package/lib/runtime/optionProcessor.tenon.js +84 -0
  139. package/lib/runtime/utils.js +2 -0
  140. package/lib/style-compiler/index.js +1 -1
  141. package/lib/style-compiler/plugins/hm.js +20 -0
  142. package/lib/template-compiler/bind-this.js +7 -2
  143. package/lib/template-compiler/compiler.js +70 -42
  144. package/lib/template-compiler/gen-node-react.js +3 -3
  145. package/lib/tenon/index.js +117 -0
  146. package/lib/tenon/processJSON.js +352 -0
  147. package/lib/tenon/processScript.js +203 -0
  148. package/lib/tenon/processStyles.js +21 -0
  149. package/lib/tenon/processTemplate.js +126 -0
  150. package/lib/tenon/script-helper.js +223 -0
  151. package/lib/utils/env.js +6 -1
  152. package/lib/utils/get-relative-path.js +25 -0
  153. package/package.json +9 -4
  154. package/LICENSE +0 -433
  155. package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
  156. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
  157. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
  158. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  159. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  160. package/lib/runtime/components/react/mpx-image/index.tsx +0 -345
  161. package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
@@ -0,0 +1,15 @@
1
+ import { registerWebviewMethods } from './utils';
2
+ const METHODS = ['addColorStop'];
3
+ export default class CanvasGradient {
4
+ canvas;
5
+ constructor(canvas, noOnConstruction = false) {
6
+ this.canvas = canvas;
7
+ registerWebviewMethods(this, METHODS);
8
+ if (this.onConstruction && !noOnConstruction) {
9
+ this.onConstruction();
10
+ }
11
+ }
12
+ postMessage(message) {
13
+ return this.canvas.postMessage(message);
14
+ }
15
+ }
@@ -0,0 +1,84 @@
1
+ import { registerWebviewProperties, registerWebviewMethods, registerWebviewTarget } from './utils';
2
+ const PROPERTIES = {
3
+ direction: 'inherit',
4
+ fillStyle: '#000',
5
+ filter: 'none',
6
+ font: '10px sans-serif',
7
+ fontKerning: 'auto',
8
+ fontStretch: 'auto',
9
+ fontVariantCaps: 'normal',
10
+ globalAlpha: 1.0,
11
+ globalCompositeOperation: 'source-over',
12
+ imageSmoothingEnabled: 'true',
13
+ imageSmoothingQuality: 'low',
14
+ letterSpacing: '0px',
15
+ lineCap: 'butt',
16
+ lineDashOffset: 0.0,
17
+ lineJoin: 'miter',
18
+ lineWidth: 1.0,
19
+ miterLimit: 10.0,
20
+ shadowBlur: 0,
21
+ shadowColor: 'rgba(0,0,0,0)',
22
+ shadowOffsetX: 0,
23
+ shadowOffsetY: 0,
24
+ strokeStyle: '#000',
25
+ textAlign: 'start',
26
+ textBaseline: 'alphabetic',
27
+ textRendering: 'auto',
28
+ wordSpacing: '0px'
29
+ };
30
+ const METHODS = [
31
+ 'arc',
32
+ 'arcTo',
33
+ 'beginPath',
34
+ 'bezierCurveTo',
35
+ 'clearRect',
36
+ 'clip',
37
+ 'closePath',
38
+ 'createConicGradient',
39
+ 'createImageData',
40
+ 'createLinearGradient',
41
+ 'createPattern',
42
+ 'createRadialGradient',
43
+ 'drawFocusIfNeeded',
44
+ 'drawImage',
45
+ 'ellipse',
46
+ 'fill',
47
+ 'fillRect',
48
+ 'fillText',
49
+ 'getImageData',
50
+ 'getLineDash',
51
+ 'getTransform',
52
+ 'lineTo',
53
+ 'measureText',
54
+ 'moveTo',
55
+ 'putImageData',
56
+ 'quadraticCurveTo',
57
+ 'rect',
58
+ 'reset',
59
+ 'resetTransform',
60
+ 'restore',
61
+ 'rotate',
62
+ 'roundRect',
63
+ 'save',
64
+ 'scale',
65
+ 'setLineDash',
66
+ 'setTransform',
67
+ 'stroke',
68
+ 'strokeRect',
69
+ 'strokeText',
70
+ 'transform',
71
+ 'translate'
72
+ ];
73
+ export default class CanvasRenderingContext2D {
74
+ canvas;
75
+ constructor(canvas) {
76
+ this.canvas = canvas;
77
+ registerWebviewTarget(this, 'context2D');
78
+ registerWebviewProperties(this, PROPERTIES);
79
+ registerWebviewMethods(this, METHODS);
80
+ }
81
+ postMessage(message) {
82
+ return this.canvas.postMessage(message);
83
+ }
84
+ }
@@ -0,0 +1,87 @@
1
+ import { WEBVIEW_TARGET, registerWebviewProperties } from './utils';
2
+ const PROPERTIES = {
3
+ crossOrigin: undefined,
4
+ height: undefined,
5
+ src: undefined,
6
+ width: undefined
7
+ };
8
+ export class Image {
9
+ [WEBVIEW_TARGET];
10
+ canvas;
11
+ width;
12
+ height;
13
+ _loadListener;
14
+ _errorListener;
15
+ _onload;
16
+ _onerror;
17
+ constructor(canvas, width, height, noOnConstruction = false) {
18
+ this.canvas = canvas;
19
+ registerWebviewProperties(this, PROPERTIES);
20
+ if (width) {
21
+ this.width = width;
22
+ }
23
+ if (height) {
24
+ this.height = height;
25
+ }
26
+ if (this.onConstruction && !noOnConstruction) {
27
+ this.onConstruction();
28
+ this.postMessage({
29
+ type: 'listen',
30
+ payload: {
31
+ types: ['error', 'load'],
32
+ target: this[WEBVIEW_TARGET]
33
+ }
34
+ });
35
+ }
36
+ }
37
+ postMessage(message) {
38
+ return this.canvas.postMessage(message);
39
+ }
40
+ addEventListener(type, callbackFn) {
41
+ return this.canvas.addMessageListener((message) => {
42
+ const target = message?.payload?.target || {};
43
+ if (message &&
44
+ message.type === 'event' &&
45
+ target[WEBVIEW_TARGET] === this[WEBVIEW_TARGET] &&
46
+ message.payload.type === type) {
47
+ for (const key in target) {
48
+ const value = target[key];
49
+ if (key in this && this[key] !== value) {
50
+ this[key] = value;
51
+ }
52
+ }
53
+ callbackFn({
54
+ ...message.payload,
55
+ target: this
56
+ });
57
+ }
58
+ });
59
+ }
60
+ set onload(callback) {
61
+ this._onload = callback;
62
+ if (this._loadListener) {
63
+ this.canvas.removeMessageListener(this._loadListener);
64
+ }
65
+ if (callback) {
66
+ this._loadListener = this.addEventListener('load', callback);
67
+ }
68
+ }
69
+ get onload() {
70
+ return this._onload;
71
+ }
72
+ set onerror(callback) {
73
+ this._onerror = callback;
74
+ if (this._errorListener) {
75
+ this.canvas.removeMessageListener(this._errorListener);
76
+ }
77
+ if (callback) {
78
+ this._errorListener = this.addEventListener('error', callback);
79
+ }
80
+ }
81
+ get onerror() {
82
+ return this._onerror;
83
+ }
84
+ }
85
+ export function createImage(canvas, width, height) {
86
+ return new Image(canvas, width, height);
87
+ }
@@ -0,0 +1,15 @@
1
+ export default class ImageData {
2
+ canvas;
3
+ constructor(canvas, dataArray, width, height, noOnConstruction) {
4
+ this.canvas = canvas;
5
+ if (this.onConstruction && !noOnConstruction) {
6
+ this.onConstruction(dataArray, width, height);
7
+ }
8
+ }
9
+ postMessage = (message) => {
10
+ return this.canvas.postMessage(message);
11
+ };
12
+ }
13
+ export function createImageData(canvas, dataArray, width, height) {
14
+ return new ImageData(canvas, dataArray, width, height);
15
+ }
@@ -0,0 +1,28 @@
1
+ import { Image } from './Image';
2
+ import CanvasGradient from './CanvasGradient';
3
+ import ImageData from './ImageData';
4
+ export var ConstructorType;
5
+ (function (ConstructorType) {
6
+ ConstructorType["Image"] = "Image";
7
+ ConstructorType["CanvasGradient"] = "CanvasGradient";
8
+ ConstructorType["ImageData"] = "ImageData";
9
+ })(ConstructorType || (ConstructorType = {}));
10
+ const constructors = [
11
+ { type: ConstructorType.Image, instance: Image },
12
+ { type: ConstructorType.CanvasGradient, instance: CanvasGradient },
13
+ { type: ConstructorType.ImageData, instance: ImageData }
14
+ ];
15
+ export function useConstructorsRegistry() {
16
+ const register = (registerWebviewConstructor) => {
17
+ constructors.forEach(({ type, instance }) => {
18
+ registerWebviewConstructor(instance, type);
19
+ });
20
+ };
21
+ const getConstructor = (type) => {
22
+ return constructors.find(c => c.type === type)?.instance;
23
+ };
24
+ return {
25
+ register,
26
+ getConstructor
27
+ };
28
+ }
@@ -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>`;