@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.
Files changed (140) hide show
  1. package/lib/index.js +13 -8
  2. package/lib/platform/json/wx/index.js +21 -8
  3. package/lib/platform/style/wx/index.js +35 -38
  4. package/lib/platform/template/wx/component-config/canvas.js +8 -0
  5. package/lib/platform/template/wx/component-config/fix-component-name.js +15 -12
  6. package/lib/platform/template/wx/component-config/index.js +1 -1
  7. package/lib/platform/template/wx/component-config/input.js +1 -1
  8. package/lib/platform/template/wx/component-config/rich-text.js +8 -0
  9. package/lib/platform/template/wx/component-config/swiper.js +1 -1
  10. package/lib/platform/template/wx/component-config/textarea.js +1 -1
  11. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  12. package/lib/react/processStyles.js +14 -4
  13. package/lib/react/processTemplate.js +3 -0
  14. package/lib/resolver/AddModePlugin.js +8 -8
  15. package/lib/runtime/components/react/context.ts +6 -0
  16. package/lib/runtime/components/react/dist/context.js +2 -0
  17. package/lib/runtime/components/react/dist/event.config.js +24 -24
  18. package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
  19. package/lib/runtime/components/react/dist/mpx-button.jsx +77 -49
  20. package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
  21. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
  22. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
  23. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
  24. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
  25. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
  26. package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
  27. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
  28. package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
  29. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +13 -19
  30. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +29 -38
  31. package/lib/runtime/components/react/dist/mpx-form.jsx +16 -19
  32. package/lib/runtime/components/react/dist/mpx-icon.jsx +8 -16
  33. package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
  34. package/lib/runtime/components/react/dist/mpx-input.jsx +54 -27
  35. package/lib/runtime/components/react/dist/mpx-label.jsx +15 -22
  36. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +13 -16
  37. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +13 -13
  38. package/lib/runtime/components/react/dist/mpx-navigator.jsx +2 -4
  39. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
  40. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
  41. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
  42. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
  43. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
  44. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
  45. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
  46. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
  47. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
  48. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +11 -19
  49. package/lib/runtime/components/react/dist/mpx-radio.jsx +27 -42
  50. package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
  51. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
  52. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +6 -4
  53. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +47 -41
  54. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
  55. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +4 -2
  56. package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
  57. package/lib/runtime/components/react/dist/mpx-switch.jsx +20 -10
  58. package/lib/runtime/components/react/dist/mpx-text.jsx +11 -10
  59. package/lib/runtime/components/react/dist/mpx-textarea.jsx +8 -3
  60. package/lib/runtime/components/react/dist/mpx-view.jsx +65 -61
  61. package/lib/runtime/components/react/dist/mpx-web-view.jsx +112 -35
  62. package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
  63. package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
  64. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  65. package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
  66. package/lib/runtime/components/react/dist/useAnimationHooks.js +35 -9
  67. package/lib/runtime/components/react/dist/utils.jsx +70 -23
  68. package/lib/runtime/components/react/event.config.ts +25 -26
  69. package/lib/runtime/components/react/getInnerListeners.ts +237 -199
  70. package/lib/runtime/components/react/mpx-button.tsx +104 -57
  71. package/lib/runtime/components/react/mpx-canvas/Bus.ts +70 -0
  72. package/lib/runtime/components/react/mpx-canvas/CanvasGradient.ts +18 -0
  73. package/lib/runtime/components/react/mpx-canvas/CanvasRenderingContext2D.ts +87 -0
  74. package/lib/runtime/components/react/mpx-canvas/Image.ts +102 -0
  75. package/lib/runtime/components/react/mpx-canvas/ImageData.ts +23 -0
  76. package/lib/runtime/components/react/mpx-canvas/constructorsRegistry.ts +38 -0
  77. package/lib/runtime/components/react/mpx-canvas/html.ts +343 -0
  78. package/lib/runtime/components/react/mpx-canvas/index.tsx +296 -0
  79. package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
  80. package/lib/runtime/components/react/mpx-checkbox-group.tsx +28 -25
  81. package/lib/runtime/components/react/mpx-checkbox.tsx +48 -49
  82. package/lib/runtime/components/react/mpx-form.tsx +25 -28
  83. package/lib/runtime/components/react/mpx-icon.tsx +12 -17
  84. package/lib/runtime/components/react/mpx-image.tsx +436 -0
  85. package/lib/runtime/components/react/mpx-input.tsx +77 -57
  86. package/lib/runtime/components/react/mpx-label.tsx +26 -27
  87. package/lib/runtime/components/react/mpx-movable-area.tsx +18 -23
  88. package/lib/runtime/components/react/mpx-movable-view.tsx +21 -25
  89. package/lib/runtime/components/react/mpx-navigator.tsx +2 -8
  90. package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
  91. package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
  92. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
  93. package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
  94. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
  95. package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
  96. package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
  97. package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
  98. package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
  99. package/lib/runtime/components/react/mpx-radio-group.tsx +24 -27
  100. package/lib/runtime/components/react/mpx-radio.tsx +45 -54
  101. package/lib/runtime/components/react/mpx-rich-text/html.ts +40 -0
  102. package/lib/runtime/components/react/mpx-rich-text/index.tsx +121 -0
  103. package/lib/runtime/components/react/mpx-root-portal.tsx +3 -5
  104. package/lib/runtime/components/react/mpx-scroll-view.tsx +72 -71
  105. package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
  106. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +4 -2
  107. package/lib/runtime/components/react/mpx-swiper-item.tsx +3 -2
  108. package/lib/runtime/components/react/mpx-switch.tsx +29 -23
  109. package/lib/runtime/components/react/mpx-text.tsx +14 -18
  110. package/lib/runtime/components/react/mpx-textarea.tsx +11 -10
  111. package/lib/runtime/components/react/mpx-view.tsx +92 -76
  112. package/lib/runtime/components/react/mpx-web-view.tsx +116 -54
  113. package/lib/runtime/components/react/pickerFaces.ts +104 -0
  114. package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
  115. package/lib/runtime/components/react/types/common.ts +2 -0
  116. package/lib/runtime/components/react/types/global.d.ts +5 -17
  117. package/lib/runtime/components/react/useAnimationHooks.ts +36 -11
  118. package/lib/runtime/components/react/utils.tsx +99 -28
  119. package/lib/runtime/components/web/getInnerListeners.js +6 -6
  120. package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
  121. package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
  122. package/lib/runtime/components/web/mpx-picker.vue +382 -385
  123. package/lib/runtime/components/web/mpx-web-view.vue +175 -161
  124. package/lib/runtime/optionProcessor.js +7 -38
  125. package/lib/runtime/utils.js +2 -0
  126. package/lib/style-compiler/plugins/scope-id.js +30 -2
  127. package/lib/template-compiler/bind-this.js +7 -2
  128. package/lib/template-compiler/compiler.js +79 -47
  129. package/lib/template-compiler/gen-node-react.js +3 -3
  130. package/lib/utils/pre-process-json.js +9 -5
  131. package/package.json +6 -4
  132. package/LICENSE +0 -433
  133. package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
  134. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
  135. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
  136. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  137. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  138. package/lib/runtime/components/react/mpx-image/index.tsx +0 -345
  139. package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
  140. package/lib/runtime/components/web/event.js +0 -105
@@ -34,7 +34,7 @@
34
34
  * ✘ bindagreeprivacyauthorization
35
35
  * ✔ bindtap
36
36
  */
37
- import { useEffect, useRef, useState, ReactNode, forwardRef, useContext, JSX } from 'react'
37
+ import { createElement, useEffect, useRef, useState, ReactNode, forwardRef, useContext, JSX } from 'react'
38
38
  import {
39
39
  View,
40
40
  StyleSheet,
@@ -45,10 +45,10 @@ import {
45
45
  NativeSyntheticEvent
46
46
  } from 'react-native'
47
47
  import { warn } from '@mpxjs/utils'
48
- import { splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren } from './utils'
48
+ import { getCurrentPage, splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren, extendObject } from './utils'
49
49
  import useInnerProps, { getCustomEvent } from './getInnerListeners'
50
50
  import useNodesRef, { HandlerRef } from './useNodesRef'
51
- import { FormContext } from './context'
51
+ import { RouteContext, FormContext } from './context'
52
52
 
53
53
  export type Type = 'default' | 'primary' | 'warn'
54
54
 
@@ -128,7 +128,8 @@ const styles = StyleSheet.create({
128
128
  }
129
129
  })
130
130
 
131
- const getOpenTypeEvent = (openType: OpenType) => {
131
+ const getOpenTypeEvent = (openType?: OpenType) => {
132
+ if (!openType) return
132
133
  if (!global.__mpx?.config?.rnConfig) {
133
134
  warn('Environment not supported')
134
135
  return
@@ -148,6 +149,12 @@ const getOpenTypeEvent = (openType: OpenType) => {
148
149
  return event
149
150
  }
150
151
 
152
+ const timer = (data: any, time = 3000) => new Promise((resolve) => {
153
+ setTimeout(() => {
154
+ resolve(data)
155
+ }, time)
156
+ })
157
+
151
158
  const Loading = ({ alone = false }: { alone: boolean }): JSX.Element => {
152
159
  const image = useRef(new Animated.Value(0)).current
153
160
 
@@ -174,11 +181,14 @@ const Loading = ({ alone = false }: { alone: boolean }): JSX.Element => {
174
181
  }
175
182
  }, [])
176
183
 
177
- const loadingStyle = {
178
- ...styles.loading,
179
- transform: [{ rotate }],
180
- marginRight: alone ? 0 : 5
181
- }
184
+ const loadingStyle = extendObject(
185
+ {},
186
+ styles.loading,
187
+ {
188
+ transform: [{ rotate }],
189
+ marginRight: alone ? 0 : 5
190
+ }
191
+ )
182
192
 
183
193
  return <Animated.Image testID="loading" style={loadingStyle} source={{ uri: LOADING_IMAGE_URI }} />
184
194
  }
@@ -211,6 +221,8 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
211
221
  bindtouchend
212
222
  } = props
213
223
 
224
+ const pageId = useContext(RouteContext)
225
+
214
226
  const formContext = useContext(FormContext)
215
227
 
216
228
  let submitFn: () => void | undefined
@@ -265,28 +277,28 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
265
277
  backgroundColor: plain ? 'transparent' : normalBackgroundColor
266
278
  }
267
279
 
268
- const defaultViewStyle = {
269
- ...styles.button,
270
- ...(isMiniSize && styles.buttonMini),
271
- ...viewStyle
272
- }
280
+ const defaultViewStyle = extendObject(
281
+ {},
282
+ styles.button,
283
+ isMiniSize ? styles.buttonMini : null,
284
+ viewStyle
285
+ )
273
286
 
274
- const defaultTextStyle = {
275
- ...styles.text,
276
- ...(isMiniSize && styles.textMini),
277
- color: plain ? plainTextColor : normalTextColor
278
- }
287
+ const defaultTextStyle = extendObject(
288
+ {},
289
+ styles.text,
290
+ isMiniSize ? styles.textMini : {},
291
+ { color: plain ? plainTextColor : normalTextColor }
292
+ )
279
293
 
280
- const defaultStyle = {
281
- ...defaultViewStyle,
282
- ...defaultTextStyle
283
- }
294
+ const defaultStyle = extendObject({}, defaultViewStyle, defaultTextStyle)
284
295
 
285
- const styleObj = {
286
- ...defaultStyle,
287
- ...style,
288
- ...(applyHoverEffect && hoverStyle)
289
- }
296
+ const styleObj = extendObject(
297
+ {},
298
+ defaultStyle,
299
+ style,
300
+ applyHoverEffect ? hoverStyle : {}
301
+ )
290
302
 
291
303
  const {
292
304
  hasSelfPercent,
@@ -299,28 +311,52 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
299
311
 
300
312
  const nodeRef = useRef(null)
301
313
 
302
- useNodesRef(props, ref, nodeRef, { defaultStyle })
314
+ useNodesRef(props, ref, nodeRef, { style: normalStyle })
303
315
 
304
316
  const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef })
305
317
 
306
- const { textStyle, backgroundStyle, innerStyle } = splitStyle(normalStyle)
318
+ const { textStyle, backgroundStyle, innerStyle = {} } = splitStyle(normalStyle)
307
319
 
308
320
  if (backgroundStyle) {
309
321
  warn('Button does not support background image-related styles!')
310
322
  }
311
323
 
312
324
  const handleOpenTypeEvent = (evt: NativeSyntheticEvent<TouchEvent>) => {
313
- if (!openType) return
314
325
  const handleEvent = getOpenTypeEvent(openType)
326
+ if (!handleEvent) return
315
327
 
316
328
  if (openType === 'share') {
317
- handleEvent && handleEvent({
329
+ const currentPage = getCurrentPage(pageId)
330
+ const event = {
318
331
  from: 'button',
319
- target: getCustomEvent('tap', evt, { layoutRef }, props).target
320
- })
332
+ target: getCustomEvent('tap', evt, { layoutRef }, props).target,
333
+ webViewUrl: currentPage?.__webViewUrl
334
+ }
335
+ if (currentPage) {
336
+ const defaultMessage = {
337
+ title: global.__mpx.config.rnConfig.projectName || 'AwesomeProject',
338
+ path: currentPage.route || ''
339
+ }
340
+ if (currentPage.onShareAppMessage) {
341
+ const { promise, ...message } = currentPage.onShareAppMessage(event) || {}
342
+ if (promise) {
343
+ Promise.race([Promise.resolve(promise), timer(message)])
344
+ .then((msg) => {
345
+ handleEvent(Object.assign({}, defaultMessage, msg))
346
+ })
347
+ } else {
348
+ handleEvent(Object.assign({}, defaultMessage, message))
349
+ }
350
+ } else {
351
+ handleEvent(defaultMessage)
352
+ }
353
+ } else {
354
+ warn('Current page not found')
355
+ // Todo handleEvent(event)
356
+ }
321
357
  }
322
358
 
323
- if (openType === 'getUserInfo' && handleEvent && bindgetuserinfo) {
359
+ if (openType === 'getUserInfo' && bindgetuserinfo) {
324
360
  Promise.resolve(handleEvent)
325
361
  .then((userInfo) => {
326
362
  if (typeof userInfo === 'object') {
@@ -375,36 +411,47 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
375
411
 
376
412
  const innerProps = useInnerProps(
377
413
  props,
378
- {
379
- ref: nodeRef,
380
- style: { ...innerStyle, ...layoutStyle },
381
- ...layoutProps,
382
- bindtouchstart: onTouchStart,
383
- bindtouchend: onTouchEnd,
384
- bindtap: onTap
385
- },
386
- [],
414
+ extendObject(
415
+ {
416
+ ref: nodeRef,
417
+ style: extendObject({}, innerStyle, layoutStyle)
418
+ },
419
+ layoutProps,
420
+ {
421
+ bindtouchstart: (bindtouchstart || !disabled) && onTouchStart,
422
+ bindtouchend: (bindtouchend || !disabled) && onTouchEnd,
423
+ bindtap: !disabled && onTap
424
+ }
425
+ ),
426
+ [
427
+ 'disabled',
428
+ 'size',
429
+ 'type',
430
+ 'plain',
431
+ 'loading',
432
+ 'hover-class',
433
+ 'hover-style',
434
+ 'hover-start-time',
435
+ 'hover-stay-time',
436
+ 'open-type',
437
+ 'form-type'
438
+ ],
387
439
  {
388
440
  layoutRef,
389
441
  disableTap: disabled
390
442
  }
391
443
  )
392
444
 
393
- return (
394
- <View {...innerProps}>
395
- {loading && <Loading alone={!children} />}
445
+ return createElement(View, innerProps, loading && createElement(Loading, { alone: !children }),
446
+ wrapChildren(
447
+ props,
396
448
  {
397
- wrapChildren(
398
- props,
399
- {
400
- hasVarDec,
401
- varContext: varContextRef.current,
402
- textStyle,
403
- textProps
404
- }
405
- )
449
+ hasVarDec,
450
+ varContext: varContextRef.current,
451
+ textStyle,
452
+ textProps
406
453
  }
407
- </View>
454
+ )
408
455
  )
409
456
  })
410
457
 
@@ -0,0 +1,70 @@
1
+ import { warn } from '@mpxjs/utils'
2
+ interface Message {
3
+ id?: string
4
+ type: string
5
+ payload?: any
6
+ }
7
+ export default class Bus {
8
+ _paused: Boolean = false;
9
+ _messageListeners: { [key: string]: (message: Message) => void } = {}
10
+ _queue: Message[] = []
11
+ _send: (message: Message | Message[]) => void
12
+ _timeoutId: NodeJS.Timeout | null = null
13
+ constructor (send: (message: Message | Message[]) => void) {
14
+ this._send = send
15
+ }
16
+
17
+ post (message: Message): Promise<any> {
18
+ return new Promise((resolve) => {
19
+ if (message.type !== 'set' && message.id) {
20
+ this._messageListeners[message.id] = resolve
21
+ }
22
+
23
+ if (!this._paused) {
24
+ this._queue.push(message)
25
+ this.startBatching()
26
+ } else {
27
+ this._queue.push(message)
28
+ }
29
+ })
30
+ }
31
+
32
+ handle (message: Message): void {
33
+ if (!message.id) return
34
+ const handler = this._messageListeners[message.id]
35
+ delete this._messageListeners[message.id]
36
+
37
+ if (handler) {
38
+ handler(message)
39
+ } else {
40
+ warn(`Received unexpected message: ${message}`)
41
+ }
42
+ }
43
+
44
+ pause (): void {
45
+ this._paused = true
46
+ }
47
+
48
+ resume (): void {
49
+ this._paused = false
50
+ this._send(this._queue)
51
+ this._queue = []
52
+ }
53
+
54
+ startBatching (): void {
55
+ if (this._timeoutId) return
56
+
57
+ this._timeoutId = setTimeout(() => {
58
+ this._send(this._queue)
59
+ this._queue = []
60
+ this._timeoutId = null
61
+ }, 16)
62
+ }
63
+
64
+ clearBatchingTimeout (): void {
65
+ if (this._timeoutId) {
66
+ clearTimeout(this._timeoutId)
67
+ this._timeoutId = null
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,18 @@
1
+ import { WebviewMessage, CanvasInstance, registerWebviewMethods } from './utils'
2
+
3
+ const METHODS = ['addColorStop']
4
+ export default class CanvasGradient {
5
+ private canvas: CanvasInstance;
6
+ [key: string]: any;
7
+ constructor (canvas: CanvasInstance, noOnConstruction = false) {
8
+ this.canvas = canvas
9
+ registerWebviewMethods(this, METHODS)
10
+ if (this.onConstruction && !noOnConstruction) {
11
+ this.onConstruction()
12
+ }
13
+ }
14
+
15
+ postMessage (message: WebviewMessage) {
16
+ return this.canvas.postMessage(message)
17
+ }
18
+ }
@@ -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
+ }