@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,87 @@
1
+ import { CanvasInstance, WebviewMessage, registerWebviewProperties, registerWebviewMethods, registerWebviewTarget } from './utils'
2
+
3
+ const PROPERTIES = {
4
+ direction: 'inherit',
5
+ fillStyle: '#000',
6
+ filter: 'none',
7
+ font: '10px sans-serif',
8
+ fontKerning: 'auto',
9
+ fontStretch: 'auto',
10
+ fontVariantCaps: 'normal',
11
+ globalAlpha: 1.0,
12
+ globalCompositeOperation: 'source-over',
13
+ imageSmoothingEnabled: 'true',
14
+ imageSmoothingQuality: 'low',
15
+ letterSpacing: '0px',
16
+ lineCap: 'butt',
17
+ lineDashOffset: 0.0,
18
+ lineJoin: 'miter',
19
+ lineWidth: 1.0,
20
+ miterLimit: 10.0,
21
+ shadowBlur: 0,
22
+ shadowColor: 'rgba(0,0,0,0)',
23
+ shadowOffsetX: 0,
24
+ shadowOffsetY: 0,
25
+ strokeStyle: '#000',
26
+ textAlign: 'start',
27
+ textBaseline: 'alphabetic',
28
+ textRendering: 'auto',
29
+ wordSpacing: '0px'
30
+ }
31
+
32
+ const METHODS = [
33
+ 'arc',
34
+ 'arcTo',
35
+ 'beginPath',
36
+ 'bezierCurveTo',
37
+ 'clearRect',
38
+ 'clip',
39
+ 'closePath',
40
+ 'createConicGradient',
41
+ 'createImageData',
42
+ 'createLinearGradient',
43
+ 'createPattern',
44
+ 'createRadialGradient',
45
+ 'drawFocusIfNeeded',
46
+ 'drawImage',
47
+ 'ellipse',
48
+ 'fill',
49
+ 'fillRect',
50
+ 'fillText',
51
+ 'getImageData',
52
+ 'getLineDash',
53
+ 'getTransform',
54
+ 'lineTo',
55
+ 'measureText',
56
+ 'moveTo',
57
+ 'putImageData',
58
+ 'quadraticCurveTo',
59
+ 'rect',
60
+ 'reset',
61
+ 'resetTransform',
62
+ 'restore',
63
+ 'rotate',
64
+ 'roundRect',
65
+ 'save',
66
+ 'scale',
67
+ 'setLineDash',
68
+ 'setTransform',
69
+ 'stroke',
70
+ 'strokeRect',
71
+ 'strokeText',
72
+ 'transform',
73
+ 'translate'
74
+ ]
75
+ export default class CanvasRenderingContext2D {
76
+ canvas: CanvasInstance
77
+ constructor (canvas: CanvasInstance) {
78
+ this.canvas = canvas
79
+ registerWebviewTarget(this, 'context2D')
80
+ registerWebviewProperties(this, PROPERTIES)
81
+ registerWebviewMethods(this, METHODS)
82
+ }
83
+
84
+ postMessage (message: WebviewMessage) {
85
+ return this.canvas.postMessage(message)
86
+ }
87
+ }
@@ -0,0 +1,102 @@
1
+ import { WebviewMessage, WEBVIEW_TARGET, registerWebviewProperties, CanvasInstance } from './utils'
2
+
3
+ const PROPERTIES = {
4
+ crossOrigin: undefined,
5
+ height: undefined,
6
+ src: undefined,
7
+ width: undefined
8
+ }
9
+
10
+ export class Image {
11
+ [WEBVIEW_TARGET]: string;
12
+ canvas: any;
13
+ width: number;
14
+ height: number;
15
+ private _loadListener: any;
16
+ private _errorListener: any;
17
+ private _onload: ((...args: any[]) => void);
18
+ private _onerror: ((...args: any[]) => void);
19
+ [key: string]: any;
20
+
21
+ constructor (canvas: CanvasInstance, width?: number, height?: number, noOnConstruction = false) {
22
+ this.canvas = canvas
23
+ registerWebviewProperties(this, PROPERTIES)
24
+
25
+ if (width) {
26
+ this.width = width
27
+ }
28
+ if (height) {
29
+ this.height = height
30
+ }
31
+
32
+ if (this.onConstruction && !noOnConstruction) {
33
+ this.onConstruction()
34
+ this.postMessage({
35
+ type: 'listen',
36
+ payload: {
37
+ types: ['error', 'load'],
38
+ target: this[WEBVIEW_TARGET]
39
+ }
40
+ })
41
+ }
42
+ }
43
+
44
+ postMessage (message: WebviewMessage) {
45
+ return this.canvas.postMessage(message)
46
+ }
47
+
48
+ addEventListener (type: 'load' | 'error', callbackFn: Function) {
49
+ return this.canvas.addMessageListener((message: WebviewMessage) => {
50
+ const target = message?.payload?.target as { [key: string]: any } || {}
51
+ if (
52
+ message &&
53
+ message.type === 'event' &&
54
+ target[WEBVIEW_TARGET] === this[WEBVIEW_TARGET] &&
55
+ message.payload.type === type
56
+ ) {
57
+ for (const key in target) {
58
+ const value = target[key]
59
+ if (key in this && this[key] !== value) {
60
+ this[key] = value
61
+ }
62
+ }
63
+ callbackFn({
64
+ ...message.payload,
65
+ target: this
66
+ })
67
+ }
68
+ })
69
+ }
70
+
71
+ set onload (callback: ((...args: any[]) => void)) {
72
+ this._onload = callback
73
+ if (this._loadListener) {
74
+ this.canvas.removeMessageListener(this._loadListener)
75
+ }
76
+ if (callback) {
77
+ this._loadListener = this.addEventListener('load', callback)
78
+ }
79
+ }
80
+
81
+ get onload (): ((...args: any[]) => void) {
82
+ return this._onload
83
+ }
84
+
85
+ set onerror (callback: ((...args: any[]) => void)) {
86
+ this._onerror = callback
87
+ if (this._errorListener) {
88
+ this.canvas.removeMessageListener(this._errorListener)
89
+ }
90
+ if (callback) {
91
+ this._errorListener = this.addEventListener('error', callback)
92
+ }
93
+ }
94
+
95
+ get onerror () : ((...args: any[]) => void) {
96
+ return this._onerror
97
+ }
98
+ }
99
+
100
+ export function createImage (canvas: CanvasInstance, width?: number, height?: number) {
101
+ return new Image(canvas, width, height)
102
+ }
@@ -0,0 +1,23 @@
1
+ import {
2
+ WebviewMessage,
3
+ CanvasInstance
4
+ } from './utils'
5
+
6
+ export default class ImageData {
7
+ canvas: CanvasInstance;
8
+ [key: string]: any;
9
+ constructor (canvas: CanvasInstance, dataArray: number[], width: number, height: number, noOnConstruction?: boolean) {
10
+ this.canvas = canvas
11
+ if (this.onConstruction && !noOnConstruction) {
12
+ this.onConstruction(dataArray, width, height)
13
+ }
14
+ }
15
+
16
+ postMessage = (message: WebviewMessage) => {
17
+ return this.canvas.postMessage(message)
18
+ };
19
+ }
20
+
21
+ export function createImageData (canvas: CanvasInstance, dataArray: number[], width: number, height: number) {
22
+ return new ImageData(canvas, dataArray, width, height)
23
+ }
@@ -0,0 +1,38 @@
1
+ import { Image } from './Image'
2
+ import CanvasGradient from './CanvasGradient'
3
+ import ImageData from './ImageData'
4
+ import { WebviewConstructor } from './utils'
5
+
6
+ export enum ConstructorType {
7
+ Image = 'Image',
8
+ CanvasGradient = 'CanvasGradient',
9
+ ImageData = 'ImageData'
10
+ }
11
+
12
+ interface Constructor {
13
+ type: ConstructorType
14
+ instance: WebviewConstructor
15
+ }
16
+
17
+ const constructors: Constructor[] = [
18
+ { type: ConstructorType.Image, instance: Image },
19
+ { type: ConstructorType.CanvasGradient, instance: CanvasGradient },
20
+ { type: ConstructorType.ImageData, instance: ImageData }
21
+ ]
22
+
23
+ export function useConstructorsRegistry () {
24
+ const register = (registerWebviewConstructor: Function): void => {
25
+ constructors.forEach(({ type, instance }) => {
26
+ registerWebviewConstructor(instance, type)
27
+ })
28
+ }
29
+
30
+ const getConstructor = (type: ConstructorType): WebviewConstructor | undefined => {
31
+ return constructors.find(c => c.type === type)?.instance
32
+ }
33
+
34
+ return {
35
+ register,
36
+ getConstructor
37
+ }
38
+ }
@@ -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>`