@tarojs/plugin-platform-harmony-ets 3.7.0-alpha.27 → 4.0.0-alpha.10

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 (239) hide show
  1. package/dist/apis/apis.ts +6 -2
  2. package/dist/apis/base/crypto.ts +4 -0
  3. package/dist/apis/base/debug.ts +5 -0
  4. package/dist/apis/base/index.ts +9 -1
  5. package/dist/apis/base/performance.ts +8 -0
  6. package/dist/apis/base/system.ts +74 -21
  7. package/dist/apis/base/update.ts +5 -0
  8. package/dist/apis/base/weapp/app-event.ts +75 -0
  9. package/dist/apis/base/weapp/life-cycle.ts +21 -0
  10. package/dist/apis/canvas/index.ts +27 -0
  11. package/dist/apis/data-analysis/index.ts +6 -0
  12. package/dist/apis/device/accelerometer.ts +2 -1
  13. package/dist/apis/device/accessibility.ts +4 -0
  14. package/dist/apis/device/battery.ts +2 -2
  15. package/dist/apis/device/bluetooth-ble.ts +19 -0
  16. package/dist/apis/device/bluetooth-peripheral.ts +6 -0
  17. package/dist/apis/device/bluetooth.ts +16 -0
  18. package/dist/apis/device/calendar.ts +5 -0
  19. package/dist/apis/device/clipboard.ts +17 -9
  20. package/dist/apis/device/compass.ts +21 -0
  21. package/dist/apis/device/contact.ts +5 -0
  22. package/dist/apis/device/crypto.ts +4 -0
  23. package/dist/apis/device/gyroscope.ts +7 -0
  24. package/dist/apis/device/iBeacon.ts +10 -0
  25. package/dist/apis/device/index.ts +15 -0
  26. package/dist/apis/device/keyboard.ts +2 -1
  27. package/dist/apis/device/memory.ts +10 -3
  28. package/dist/apis/device/motion.ts +6 -0
  29. package/dist/apis/device/network.ts +26 -9
  30. package/dist/apis/device/nfc.ts +10 -0
  31. package/dist/apis/device/phone.ts +2 -1
  32. package/dist/apis/device/scan.ts +4 -0
  33. package/dist/apis/device/screen.ts +2 -1
  34. package/dist/apis/device/sms.ts +4 -0
  35. package/dist/apis/device/vibrate.ts +19 -11
  36. package/dist/apis/device/wifi.ts +15 -0
  37. package/dist/apis/ext/index.ts +5 -0
  38. package/dist/apis/files/index.ts +2 -1
  39. package/dist/apis/files/manager.ts +2 -1
  40. package/dist/apis/framework/index.ts +3 -8
  41. package/dist/apis/index.ts +27 -17
  42. package/dist/apis/location/index.ts +61 -68
  43. package/dist/apis/media/EditorContext.ts +32 -0
  44. package/dist/apis/media/audio/index.ts +18 -0
  45. package/dist/apis/media/background-audio/index.ts +16 -0
  46. package/dist/apis/media/camera.ts +16 -0
  47. package/dist/apis/media/common.ts +2 -1
  48. package/dist/apis/media/{image.ts → image/index.ts} +4 -3
  49. package/dist/apis/media/index.ts +10 -0
  50. package/dist/apis/media/live.ts +5 -0
  51. package/dist/apis/media/map.ts +4 -0
  52. package/dist/apis/media/media-recorder.ts +4 -0
  53. package/dist/apis/media/recorder.ts +6 -0
  54. package/dist/apis/media/video/VideoContext.ts +68 -0
  55. package/dist/apis/media/{video.ts → video/index.ts} +13 -2
  56. package/dist/apis/media/video-decoder.ts +4 -0
  57. package/dist/apis/media/video-processing.ts +4 -0
  58. package/dist/apis/media/voip.ts +19 -0
  59. package/dist/apis/network/index.ts +3 -0
  60. package/dist/apis/network/mdns.ts +13 -0
  61. package/dist/apis/network/request.ts +7 -6
  62. package/dist/apis/network/tcp.ts +4 -0
  63. package/dist/apis/network/udp.ts +4 -0
  64. package/dist/apis/network/webSocket.ts +2 -6
  65. package/dist/apis/open-api/account.ts +4 -0
  66. package/dist/apis/open-api/address.ts +4 -0
  67. package/dist/apis/open-api/authorize.ts +5 -0
  68. package/dist/apis/open-api/card.ts +5 -0
  69. package/dist/apis/open-api/channels-live.ts +11 -0
  70. package/dist/apis/open-api/customer-service.ts +4 -0
  71. package/dist/apis/open-api/device-voip.ts +5 -0
  72. package/dist/apis/open-api/facial.ts +7 -0
  73. package/dist/apis/open-api/favorites.ts +5 -0
  74. package/dist/apis/open-api/group.ts +4 -0
  75. package/dist/apis/open-api/index.ts +20 -6
  76. package/dist/apis/open-api/invoice.ts +5 -0
  77. package/dist/apis/open-api/license-plate.ts +4 -0
  78. package/dist/apis/open-api/login.ts +6 -0
  79. package/dist/apis/open-api/my-miniprogram.ts +4 -0
  80. package/dist/apis/open-api/privacy.ts +7 -0
  81. package/dist/apis/open-api/red-package.ts +4 -0
  82. package/dist/apis/open-api/settings.ts +5 -0
  83. package/dist/apis/open-api/soter.ts +6 -0
  84. package/dist/apis/open-api/subscribe-message.ts +6 -0
  85. package/dist/apis/open-api/{user-info/index.ts → user-info.ts} +1 -16
  86. package/dist/apis/open-api/werun.ts +5 -0
  87. package/dist/apis/payment/index.ts +6 -0
  88. package/dist/apis/route/index.ts +19 -28
  89. package/dist/apis/share/index.ts +33 -0
  90. package/dist/apis/storage/background-fetch.ts +7 -0
  91. package/dist/apis/storage/cache-manager.ts +4 -0
  92. package/dist/apis/storage/index.ts +158 -88
  93. package/dist/apis/ui/{animation.ts → animation/animation.ts} +73 -31
  94. package/dist/apis/ui/animation/index.ts +7 -0
  95. package/dist/apis/ui/background.ts +4 -19
  96. package/dist/apis/ui/custom-component.ts +8 -0
  97. package/dist/apis/ui/fonts.ts +4 -0
  98. package/dist/apis/ui/index.ts +8 -10
  99. package/dist/apis/ui/interaction/index.ts +45 -59
  100. package/dist/apis/ui/menu.ts +4 -0
  101. package/dist/apis/ui/navigation-bar/index.ts +60 -21
  102. package/dist/apis/ui/pull-down-refresh.ts +10 -4
  103. package/dist/apis/{page → ui/scroll}/index.ts +32 -27
  104. package/dist/apis/ui/sticky.ts +4 -0
  105. package/dist/apis/{tabbar/index.ts → ui/tab-bar.ts} +6 -8
  106. package/dist/apis/ui/window.ts +20 -0
  107. package/dist/apis/utils/handler.ts +4 -4
  108. package/dist/apis/utils/index.ts +1 -1
  109. package/dist/apis/worker/index.ts +4 -0
  110. package/dist/apis/wxml/IntersectionObserver.ts +24 -25
  111. package/dist/apis/wxml/index.ts +7 -2
  112. package/dist/apis/wxml/nodesRef.ts +2 -2
  113. package/dist/apis/wxml/selectorQuery.ts +29 -17
  114. package/dist/components-harmony-ets/button.ets +106 -80
  115. package/dist/components-harmony-ets/canvas.ets +51 -0
  116. package/dist/components-harmony-ets/checkbox.ets +82 -148
  117. package/dist/components-harmony-ets/form.ets +45 -132
  118. package/dist/components-harmony-ets/icon.ets +66 -69
  119. package/dist/components-harmony-ets/image.ets +26 -61
  120. package/dist/components-harmony-ets/index.ets +92 -0
  121. package/dist/components-harmony-ets/innerHtml.ets +7 -70
  122. package/dist/components-harmony-ets/input.ets +60 -146
  123. package/dist/components-harmony-ets/label.ets +86 -92
  124. package/dist/components-harmony-ets/listView.ets +26 -0
  125. package/dist/components-harmony-ets/movableArea.ets +124 -0
  126. package/dist/components-harmony-ets/movableView.ets +93 -0
  127. package/dist/components-harmony-ets/navigationBar.ets +65 -0
  128. package/dist/components-harmony-ets/pageMeta.ets +94 -0
  129. package/dist/components-harmony-ets/picker.ets +172 -198
  130. package/dist/components-harmony-ets/progress.ets +52 -0
  131. package/dist/components-harmony-ets/pseudo.ets +80 -0
  132. package/dist/components-harmony-ets/radio.ets +85 -155
  133. package/dist/components-harmony-ets/richText.ets +15 -87
  134. package/dist/components-harmony-ets/scrollList.ets +94 -0
  135. package/dist/components-harmony-ets/scrollView.ets +81 -149
  136. package/dist/components-harmony-ets/slider.ets +47 -94
  137. package/dist/components-harmony-ets/stickySection.ets +42 -0
  138. package/dist/components-harmony-ets/style.ets +396 -0
  139. package/dist/components-harmony-ets/swiper.ets +51 -151
  140. package/dist/components-harmony-ets/switch.ets +35 -76
  141. package/dist/components-harmony-ets/{index.ts → tag.ts} +6 -0
  142. package/dist/components-harmony-ets/text.ets +131 -76
  143. package/dist/components-harmony-ets/textArea.ets +64 -140
  144. package/dist/components-harmony-ets/utils/AttributeManager.ets +1 -1
  145. package/dist/components-harmony-ets/utils/DynamicCenter.ts +4 -13
  146. package/dist/components-harmony-ets/utils/constant/style.ets +9 -6
  147. package/dist/components-harmony-ets/utils/flexManager.ets +68 -85
  148. package/dist/components-harmony-ets/utils/helper.ets +18 -65
  149. package/dist/components-harmony-ets/utils/htmlParser/HarmonyHTMLParser.ts +1 -2
  150. package/dist/components-harmony-ets/utils/htmlParser/index.ts +1 -1
  151. package/dist/components-harmony-ets/utils/index.ts +55 -51
  152. package/dist/components-harmony-ets/utils/styles.ets +177 -65
  153. package/dist/components-harmony-ets/video.ets +70 -116
  154. package/dist/components-harmony-ets/view.ets +58 -64
  155. package/dist/components-harmony-ets/webView.ets +56 -0
  156. package/dist/index.d.ts +152 -0
  157. package/dist/index.js +230 -60
  158. package/dist/index.js.map +1 -1
  159. package/dist/runtime-ets/bom/document.ts +6 -4
  160. package/dist/runtime-ets/bom/getComputedStyle.ts +2 -2
  161. package/dist/runtime-ets/bom/window.ts +9 -2
  162. package/dist/runtime-ets/current.ts +3 -0
  163. package/dist/runtime-ets/dom/bind.ts +96 -0
  164. package/dist/runtime-ets/dom/class-list.ts +4 -6
  165. package/dist/runtime-ets/dom/comment.ts +1 -2
  166. package/dist/runtime-ets/dom/cssNesting.ts +409 -0
  167. package/dist/runtime-ets/dom/cssStyleDeclaration.ts +30 -20
  168. package/dist/runtime-ets/dom/dataSource.ts +64 -0
  169. package/dist/runtime-ets/dom/document.ts +36 -51
  170. package/dist/runtime-ets/dom/element/canvas.ts +136 -0
  171. package/dist/runtime-ets/dom/element/element.ts +439 -0
  172. package/dist/runtime-ets/dom/element/form.ts +403 -0
  173. package/dist/runtime-ets/dom/element/index.ts +120 -0
  174. package/dist/runtime-ets/dom/element/movableArea.ts +11 -0
  175. package/dist/runtime-ets/dom/element/movableView.ts +242 -0
  176. package/dist/runtime-ets/dom/element/normal.ts +103 -0
  177. package/dist/runtime-ets/dom/element/progress.ts +11 -0
  178. package/dist/runtime-ets/dom/element/scrollView.ts +15 -0
  179. package/dist/runtime-ets/dom/element/text.ts +10 -0
  180. package/dist/runtime-ets/dom/element/video.ts +50 -0
  181. package/dist/runtime-ets/dom/element/webView.ts +68 -0
  182. package/dist/runtime-ets/dom/event.ts +26 -5
  183. package/dist/runtime-ets/dom/eventTarget.ts +3 -3
  184. package/dist/runtime-ets/dom/node.ts +114 -49
  185. package/dist/runtime-ets/dom/stylesheet/covertWeb2Hm.ts +805 -0
  186. package/dist/runtime-ets/dom/stylesheet/index.ts +122 -429
  187. package/dist/runtime-ets/dom/stylesheet/type.ts +93 -17
  188. package/dist/runtime-ets/dom/stylesheet/util.ts +145 -17
  189. package/dist/runtime-ets/index.ts +2 -3
  190. package/dist/runtime-ets/interface/event.ts +3 -2
  191. package/dist/runtime-ets/utils/index.ts +87 -17
  192. package/dist/runtime-ets/utils/info.ts +21 -47
  193. package/dist/runtime-framework/react/app.ts +20 -28
  194. package/dist/runtime-framework/react/hooks.ts +3 -4
  195. package/dist/runtime-framework/react/index.ts +1 -2
  196. package/dist/runtime-framework/react/native-page.ts +421 -0
  197. package/dist/runtime-framework/react/page.ts +5 -17
  198. package/dist/runtime-framework/solid/app.ts +25 -45
  199. package/dist/runtime-framework/solid/connect.ts +21 -3
  200. package/dist/runtime-framework/solid/hooks.ts +17 -12
  201. package/dist/runtime-framework/solid/index.ts +6 -2
  202. package/dist/runtime-framework/solid/page.ts +84 -36
  203. package/dist/runtime-framework/solid/reconciler/props.ts +70 -25
  204. package/dist/runtime-framework/solid/reconciler/render.ts +16 -6
  205. package/dist/runtime-framework/solid/reconciler/use.ts +0 -1
  206. package/dist/runtime-framework/solid/utils/index.ts +0 -2
  207. package/dist/runtime-utils.d.ts +827 -0
  208. package/dist/runtime-utils.js +1633 -549
  209. package/dist/runtime-utils.js.map +1 -1
  210. package/dist/runtime.d.ts +1 -0
  211. package/dist/runtime.js +1633 -549
  212. package/dist/runtime.js.map +1 -1
  213. package/index.js +3 -1
  214. package/package.json +14 -15
  215. package/static/media/cancel.svg +1 -0
  216. package/static/media/circle.svg +1 -0
  217. package/static/media/clear.svg +1 -0
  218. package/static/media/download.svg +1 -0
  219. package/static/media/info.svg +1 -0
  220. package/static/media/info_circle.svg +1 -0
  221. package/static/media/search.svg +1 -0
  222. package/static/media/success.svg +1 -0
  223. package/static/media/success_no_circle.svg +1 -0
  224. package/static/media/taro_arrow_left.svg +1 -0
  225. package/static/media/taro_home.svg +1 -0
  226. package/static/media/waiting.svg +1 -0
  227. package/static/media/warn.svg +1 -0
  228. package/types/harmony.d.ts +4 -0
  229. package/types/index.d.ts +4 -0
  230. package/types/runtime.d.ts +7 -1
  231. package/LICENSE +0 -160
  232. package/dist/components-harmony-ets/base.ets +0 -63
  233. package/dist/components-harmony-ets/element.ets +0 -223
  234. package/dist/components-harmony-ets/utils/constant/icon.ts +0 -19
  235. package/dist/runtime-ets/dom/element.ts +0 -457
  236. package/dist/runtime-ets/dom/text.ts +0 -19
  237. package/dist/runtime-ets/utils/bind.ts +0 -35
  238. package/types/api.d.ts +0 -4
  239. /package/dist/runtime-framework/solid/{contant.ts → constant.ts} +0 -0
@@ -1,6 +1,6 @@
1
1
  import { isFunction } from '@tarojs/shared'
2
2
 
3
- import type { TaroElement } from '../dom/element'
3
+ import type { TaroElement } from '../dom/element/element'
4
4
 
5
5
  export const AREA_CHANGE_EVENT_NAME = 'areaChange'
6
6
  export const VISIBLE_CHANGE_EVENT_NAME = 'visibleChange'
@@ -20,34 +20,23 @@ export function createEventTapName (eventName: string) {
20
20
  export const disconnectEvent = (node: TaroElement, eventName: string) => {
21
21
  if (!node) return null
22
22
 
23
- const id = node._nid
24
- const component = node._instance
25
-
26
23
  try {
27
- component.nodeInfoMap[id].eventMap[createEventTapName(eventName)] = false
24
+ node._nodeInfo.eventMap[createEventTapName(eventName)] = false
28
25
  } catch (e) {
29
26
  console.warn(`disconnectEvent ${eventName} error: `, e)
30
27
  }
31
28
  }
32
29
 
33
- export function getComponentEventCallback (node: TaroElement, eventName: string, callback?: (data: {component: any, eventResult: any}) => void) {
34
- if (!node || !node?._instance) return null
35
-
36
- const id = node._nid
37
- const component = node?._instance
30
+ export function getComponentEventCallback (node: TaroElement, eventName: string, callback?: (data: any) => void) {
31
+ if (!node) return null
38
32
 
39
- if (component?.nodeInfoMap?.[id]?.eventMap?.[createEventTapName(eventName)]) {
33
+ if (node._nodeInfo?.eventMap?.[createEventTapName(eventName)]) {
40
34
  return (...eventResult: any[]) => {
41
- if (!component?.nodeInfoMap?.[id]?.eventMap?.[createEventTapName(eventName)]) return
42
-
43
- const res = {
44
- component,
45
- eventResult
46
- }
35
+ if (!node._nodeInfo?.eventMap?.[createEventTapName(eventName)]) return
47
36
 
48
- callback && callback.call(component, res)
37
+ callback && callback(eventResult)
49
38
 
50
- node?.[createEventOnName(eventName)]?.(res)
39
+ node?.[createEventOnName(eventName)]?.(eventResult)
51
40
  }
52
41
  }
53
42
 
@@ -56,14 +45,11 @@ export function getComponentEventCallback (node: TaroElement, eventName: string,
56
45
 
57
46
  // eslint-disable-next-line @typescript-eslint/ban-types
58
47
  function tapCallbackToNodeAndUpdate (node: TaroElement, eventName: string, callback: Function) {
59
- const id = node._nid
60
-
61
- node._instance.nodeInfoMap[id].eventMap[createEventTapName(eventName)] = true
62
- node[createEventOnName(eventName)] = ({ eventResult }) => {
48
+ node._nodeInfo.eventMap[createEventTapName(eventName)] = true
49
+ node[createEventOnName(eventName)] = (eventResult) => {
63
50
  callback && callback(...eventResult)
64
51
  }
65
52
 
66
- // 是半编译模式的节点但没有自主更新权,需要父节点触发更新
67
53
  if (!node._isDynamicNode && node._isCompileMode) {
68
54
  node.updateComponent()
69
55
  }
@@ -71,18 +57,15 @@ function tapCallbackToNodeAndUpdate (node: TaroElement, eventName: string, callb
71
57
 
72
58
  // eslint-disable-next-line @typescript-eslint/ban-types
73
59
  export async function setNodeEventCallbackAndTriggerComponentUpdate (node: TaroElement, eventName: string, callback: Function, isAsync = false) {
74
- const id = node._nid
75
- const instance = node._instance
76
-
77
60
  if (isAsync) {
78
61
  // 阻塞函数执行,等待监听节点绑定上的回调函数
79
- if (!instance.nodeInfoMap[id].eventMap[createEventTapName(eventName)]) {
62
+ if (!node._nodeInfo.eventMap[createEventTapName(eventName)]) {
80
63
  let onEventPromiseResolve
81
64
  const eventPromise = new Promise(resolve => {
82
65
  onEventPromiseResolve = resolve
83
66
  })
84
67
 
85
- instance.nodeInfoMap[id].promiseMap[eventName] = eventPromise
68
+ node._nodeInfo.promiseMap[eventName] = eventPromise
86
69
  tapCallbackToNodeAndUpdate(node, eventName, (...eventResult) => {
87
70
  callback && callback(...eventResult)
88
71
 
@@ -90,39 +73,30 @@ export async function setNodeEventCallbackAndTriggerComponentUpdate (node: TaroE
90
73
  })
91
74
  }
92
75
 
93
- await instance.nodeInfoMap[id].promiseMap[eventName]
76
+ await node._nodeInfo.promiseMap[eventName]
94
77
  } else {
95
78
  tapCallbackToNodeAndUpdate(node, eventName, callback)
96
79
  }
97
80
  }
98
81
 
99
82
  // eslint-disable-next-line @typescript-eslint/ban-types
100
- export function bindAttributesCallback (node: TaroElement, attributeName: string, callback: Function) {
83
+ export function bindAttributesCallback (node: TaroElement, _: string, callback: Function) {
101
84
  if (!node) return
102
85
 
103
- const id = node._nid
104
- const component = node._instance
105
-
106
- component.nodeInfoMap[id].attributeCallback[attributeName] = callback
86
+ node._nodeInfo = callback
107
87
  }
108
88
 
109
89
  export function triggerAttributesCallback (node, attributeName) {
110
90
  if (!node) return
111
91
 
112
- const id = node._nid
113
92
  const value = node._attrs[attributeName]
93
+ const cb = node._nodeInfo.attributeCallback[attributeName]
114
94
 
115
- return node.awaitAppear.then(() => {
116
- const component = node._instance
117
- const cb = component.nodeInfoMap[id].attributeCallback[attributeName]
118
-
119
- isFunction(cb) && cb(value)
120
- })
95
+ isFunction(cb) && cb(value)
121
96
  }
122
97
 
123
- export function initComponentNodeInfo (component: any, node: TaroElement) {
124
- component.nodeInfoMap[node._nid] = {}
125
- component.nodeInfoMap[node._nid].eventMap = {}
126
- component.nodeInfoMap[node._nid].promiseMap = {}
127
- component.nodeInfoMap[node._nid].attributeCallback = {}
98
+ export function initComponentNodeInfo (node: TaroElement) {
99
+ node._nodeInfo.eventMap = {}
100
+ node._nodeInfo.promiseMap = {}
101
+ node._nodeInfo.attributeCallback = {}
128
102
  }
@@ -1,12 +1,11 @@
1
- // eslint-disable-next-line import/no-duplicates
2
- import { Current, document } from '@tarojs/runtime'
3
- // eslint-disable-next-line import/no-duplicates
4
- import { eventCenter } from '@tarojs/runtime/dist/runtime.esm'
1
+ import { Current, document } from '@tarojs/runtime' // eslint-disable-line import/no-duplicates
2
+ import { eventCenter } from '@tarojs/runtime/dist/runtime.esm' // eslint-disable-line import/no-duplicates
5
3
 
6
4
  import { setReconciler } from './connect'
7
5
  import { injectPageInstance } from './page'
8
6
  import { EMPTY_OBJ, incrementId, isClassComponent } from './utils'
9
7
 
8
+ import type { AppInstance } from '@tarojs/runtime'
10
9
  import type React from 'react'
11
10
 
12
11
  let h: typeof React.createElement
@@ -28,8 +27,11 @@ const pageKeyId = incrementId()
28
27
 
29
28
  export function connectReactPage (
30
29
  R: typeof React,
31
- id: string
30
+ id: string,
31
+ getCtx: () => any
32
32
  ) {
33
+ const ctx = getCtx?.()
34
+
33
35
  return (Page): React.ComponentClass<any> => {
34
36
  // eslint-disable-next-line dot-notation
35
37
  const isReactComponent = isClassComponent(R, Page)
@@ -65,11 +67,10 @@ export function connectReactPage (
65
67
  const children = this.state.hasError
66
68
  ? []
67
69
  : h(ReactMeta.PageContext.Provider, { value: id }, h(Page, {
68
- ...this.props,
70
+ ...Object.assign({}, ctx?.props, this.props),
69
71
  ...refs
70
72
  }))
71
73
 
72
- // TODO root
73
74
  return h(
74
75
  'view',
75
76
  { id, className: 'taro_page' },
@@ -129,8 +130,8 @@ export function createReactApp (
129
130
  appWrapperResolver(this)
130
131
  }
131
132
 
132
- public mount (pageComponent: any, id: string, cb: () => void) {
133
- const pageWrapper = connectReactPage(react, id)(pageComponent)
133
+ public mount (pageComponent: any, id: string, getCtx: () => any, cb: () => void) {
134
+ const pageWrapper = connectReactPage(react, id, getCtx)(pageComponent)
134
135
  const key = id + pageKeyId()
135
136
  const page = () => h(pageWrapper, { key, tid: id })
136
137
  this.pages.push(page)
@@ -172,14 +173,13 @@ export function createReactApp (
172
173
  render (cb: () => void) {
173
174
  appWrapper.forceUpdate(cb)
174
175
  },
175
- mount (component: any, id: string, cb: () => void) {
176
+ mount (component: any, id: string, getCtx: () => any, cb: () => void) {
176
177
  if (appWrapper) {
177
- appWrapper.mount(component, id, cb)
178
+ appWrapper.mount(component, id, getCtx, cb)
178
179
  } else {
179
- appWrapperPromise.then(appWrapper => appWrapper.mount(component, id, cb))
180
+ appWrapperPromise.then(appWrapper => appWrapper.mount(component, id, getCtx, cb))
180
181
  }
181
182
  },
182
-
183
183
  unmount (id: string, cb: () => void) {
184
184
  appWrapper?.unmount(id, cb)
185
185
  },
@@ -212,7 +212,7 @@ export function createReactApp (
212
212
 
213
213
  app.onCreate?.()
214
214
  }
215
-
215
+
216
216
  eventCenter.trigger('__taroRouterLaunch', launchParam)
217
217
  })
218
218
  },
@@ -236,21 +236,13 @@ export function createReactApp (
236
236
  app?.componentDidHide?.()
237
237
  })
238
238
  }
239
- }
239
+ } as unknown as AppInstance
240
240
 
241
- // TODO: function componennt hook
242
- // function triggerAppHook (lifecycle: string, ...option) {
243
- // const instance = getPageInstance('taro-app')
244
- // if (instance) {
245
- // const app = getAppInstance()
246
- // const func = hooks.call('getLifecycle', instance, lifecycle)
247
- // if (Array.isArray(func)) {
248
- // func.forEach(cb => cb.apply(app, option))
249
- // }
250
- // }
251
- // }
252
-
253
- Current.app = app
241
+ if (Current.app) {
242
+ Current.app = Object.assign(app, Current.app)
243
+ } else {
244
+ Current.app = app
245
+ }
254
246
 
255
247
  return app
256
248
  }
@@ -7,14 +7,14 @@ import { HOOKS_APP_ID } from './utils'
7
7
 
8
8
  import type {
9
9
  AppInstance,
10
- Func,
11
10
  Instance,
12
11
  PageLifeCycle,
13
- PageProps
12
+ PageProps,
13
+ TFunc
14
14
  } from '@tarojs/runtime/dist/runtime.esm'
15
15
 
16
16
  const createTaroHook = (lifecycle: keyof PageLifeCycle | keyof AppInstance) => {
17
- return (fn: Func) => {
17
+ return (fn: TFunc) => {
18
18
  const { R: React, PageContext } = ReactMeta
19
19
  const id = React.useContext(PageContext) || HOOKS_APP_ID
20
20
  const instRef = React.useRef<Instance<PageProps>>()
@@ -24,7 +24,6 @@ const createTaroHook = (lifecycle: keyof PageLifeCycle | keyof AppInstance) => {
24
24
  if (fnRef.current !== fn) fnRef.current = fn
25
25
 
26
26
  React.useLayoutEffect(() => {
27
-
28
27
  let inst = instRef.current = getPageInstance(id)
29
28
  let first = false
30
29
  if (!inst) {
@@ -2,8 +2,6 @@ import { hooks } from '@tarojs/shared'
2
2
 
3
3
  import * as taroHooks from './hooks'
4
4
 
5
- // declare const __TARO_FRAMEWORK__: string;
6
-
7
5
  hooks.tap('initNativeApi', function (taro) {
8
6
  for (const hook in taroHooks) {
9
7
  taro[hook] = taroHooks[hook]
@@ -13,4 +11,5 @@ hooks.tap('initNativeApi', function (taro) {
13
11
  export * from './app'
14
12
  export * from './connect'
15
13
  export * from './hooks'
14
+ export * from './native-page'
16
15
  export * from './page'
@@ -0,0 +1,421 @@
1
+ import { Current, document, requestAnimationFrame, TaroElement, window } from '@tarojs/runtime' // eslint-disable-line import/no-duplicates
2
+ import { addLeadingSlash, CONTEXT_ACTIONS, env, eventCenter, TFunc } from '@tarojs/runtime/dist/runtime.esm' // eslint-disable-line import/no-duplicates
3
+ import { ensure, hooks, isUndefined } from '@tarojs/shared'
4
+
5
+ import { ReactMeta as reactMeta } from './app'
6
+ import { setReconciler } from './connect'
7
+ import { ON_HIDE, ON_READY, ON_SHOW } from './constant'
8
+ import {
9
+ getOnHideEventKey,
10
+ getOnReadyEventKey,
11
+ getOnShowEventKey,
12
+ getPath,
13
+ injectPageInstance,
14
+ removePageInstance,
15
+ safeExecute,
16
+ } from './page'
17
+ import { EMPTY_OBJ, incrementId, isClassComponent } from './utils'
18
+
19
+ import type { AppInstance } from '@tarojs/taro'
20
+ import type * as React from 'react'
21
+
22
+ const getNativeCompId = incrementId()
23
+ let h: typeof React.createElement
24
+ let ReactDOM
25
+ let nativeComponentApp: AppInstance
26
+ interface InitNativeComponentEntryParams {
27
+ R: typeof React
28
+ ReactDOM: typeof ReactDOM
29
+ cb?: TFunc
30
+ // 是否使用默认的 DOM 入口 - app;默认为true,false的时候,会创建一个新的dom并且把它挂载在 app 下面
31
+ isDefaultEntryDom?: boolean
32
+ }
33
+
34
+ function initNativeComponentEntry (params: InitNativeComponentEntryParams) {
35
+ const { R, ReactDOM, cb, isDefaultEntryDom = true } = params
36
+ interface IEntryState {
37
+ components: {
38
+ compId: string
39
+ element: React.ReactElement
40
+ }[]
41
+ }
42
+
43
+ interface IWrapperProps {
44
+ compId: string
45
+ getCtx: () => any
46
+ renderComponent: (ctx: any) => React.ReactElement
47
+ }
48
+
49
+ class NativeComponentWrapper extends R.Component<IWrapperProps, Record<any, any>> {
50
+ root = R.createRef<TaroElement>()
51
+ ctx = this.props.getCtx()
52
+
53
+ componentDidMount () {
54
+ this.ctx.component = this
55
+ if (this.root.current) {
56
+ this.root.current = this.ctx
57
+ }
58
+ }
59
+
60
+ // React 16 uncaught error 会导致整个应用 crash,
61
+ // 目前把错误缩小到页面
62
+ componentDidCatch (error, info: React.ErrorInfo) {
63
+ console.warn(error)
64
+ console.error(info.componentStack)
65
+ }
66
+
67
+ render () {
68
+ return h(
69
+ 'view',
70
+ {
71
+ ref: this.root,
72
+ id: this.props.compId,
73
+ },
74
+ this.props.renderComponent(this.ctx)
75
+ )
76
+ }
77
+ }
78
+
79
+ class Entry extends R.Component<Record<any, any>, IEntryState> {
80
+ state: IEntryState = {
81
+ components: [],
82
+ }
83
+
84
+ componentDidMount () {
85
+ if (isDefaultEntryDom) {
86
+ if (Current.app) {
87
+ Current.app = Object.assign(this, Current.app)
88
+ } else {
89
+ Current.app = this
90
+ }
91
+ } else {
92
+ nativeComponentApp = this
93
+ }
94
+ cb && cb()
95
+ }
96
+
97
+ // React 16 uncaught error 会导致整个应用 crash,
98
+ // 目前把错误缩小到页面
99
+ componentDidCatch (error, info: React.ErrorInfo) {
100
+ console.warn(error)
101
+ console.error(info.componentStack)
102
+ }
103
+
104
+ mount (Component, compId, getCtx, cb?) {
105
+ const isReactComponent = isClassComponent(R, Component)
106
+ const inject = (node?: any) => node && injectPageInstance(node, compId)
107
+ const refs = isReactComponent
108
+ ? { ref: inject }
109
+ : {
110
+ forwardedRef: inject,
111
+ reactReduxForwardedRef: inject,
112
+ }
113
+ if (reactMeta.PageContext === EMPTY_OBJ) {
114
+ reactMeta.PageContext = R.createContext('')
115
+ }
116
+ const item = {
117
+ compId,
118
+ element: h(NativeComponentWrapper, {
119
+ key: compId,
120
+ compId,
121
+ getCtx,
122
+ renderComponent (ctx) {
123
+ return h(
124
+ reactMeta.PageContext.Provider,
125
+ { value: compId },
126
+ h(Component, {
127
+ // TODO: 传递 Props
128
+ ...(ctx.props || {}),
129
+ ...refs,
130
+ $scope: ctx,
131
+ })
132
+ )
133
+ },
134
+ }),
135
+ }
136
+ this.setState(
137
+ {
138
+ components: [...this.state.components, item],
139
+ },
140
+ () => cb && cb()
141
+ )
142
+ }
143
+
144
+ unmount (compId, cb?) {
145
+ const components = this.state.components
146
+ const index = components.findIndex((item) => item.compId === compId)
147
+ const next = [...components.slice(0, index), ...components.slice(index + 1)]
148
+ this.setState(
149
+ {
150
+ components: next,
151
+ },
152
+ () => {
153
+ removePageInstance(compId)
154
+ cb && cb()
155
+ }
156
+ )
157
+ }
158
+
159
+ render () {
160
+ const components = this.state.components
161
+
162
+ return components.map(({ element }) => element)
163
+ }
164
+ }
165
+
166
+ setReconciler(ReactDOM)
167
+
168
+ let app = document.getElementById('app')
169
+ if (!isDefaultEntryDom && !nativeComponentApp) {
170
+ // create
171
+ const nativeApp = document.createElement('nativeComponent')
172
+ // insert
173
+ app?.appendChild(nativeApp)
174
+ app = nativeApp
175
+ }
176
+ // eslint-disable-next-line react/no-deprecated
177
+ ReactDOM.render(h(Entry, {}), app)
178
+ }
179
+
180
+ export function createNativePageConfig (
181
+ Component,
182
+ pageName: string,
183
+ react: typeof React,
184
+ reactDOM: typeof ReactDOM,
185
+ pageConfig
186
+ ) {
187
+ reactMeta.R = react
188
+ h = react.createElement
189
+ ReactDOM = reactDOM
190
+ setReconciler(ReactDOM)
191
+ const [ONLOAD, ONUNLOAD, ONREADY, ONSHOW, ONHIDE, LIFECYCLES, SIDE_EFFECT_LIFECYCLES] =
192
+ hooks.call('getMiniLifecycleImpl')!.page
193
+ let unmounting = false
194
+ let prepareMountList: (() => void)[] = []
195
+ let pageElement: TaroElement | null = null
196
+ let loadResolver: (...args: unknown[]) => void
197
+ let hasLoaded: Promise<void>
198
+ const id = pageName ?? `taro_page_${getNativeCompId()}`
199
+ function setCurrentRouter (page) {
200
+ const router = page.route || page.__route__ || page.$taroPath
201
+ Current.router = {
202
+ params: page.$taroParams!,
203
+ path: addLeadingSlash(router),
204
+ $taroPath: page.$taroPath,
205
+ onReady: getOnReadyEventKey(id),
206
+ onShow: getOnShowEventKey(id),
207
+ onHide: getOnHideEventKey(id),
208
+ }
209
+ if (!isUndefined(page.exitState)) {
210
+ Current.router.exitState = page.exitState
211
+ }
212
+ }
213
+
214
+ const pageObj: Record<string, any> = {
215
+ options: pageConfig,
216
+ [ONLOAD] (options: Readonly<Record<string, unknown>> = {}, cb?: TFunc) {
217
+ hasLoaded = new Promise((resolve) => {
218
+ loadResolver = resolve
219
+ })
220
+ Current.page = this as any
221
+ this.config = pageConfig || {}
222
+ // this.$taroPath 是页面唯一标识
223
+ const uniqueOptions = Object.assign({}, options, { $taroTimestamp: Date.now() })
224
+ const $taroPath = (this.$taroPath = getPath(id, uniqueOptions))
225
+
226
+ // this.$taroParams 作为暴露给开发者的页面参数对象,可以被随意修改
227
+ if (this.$taroParams == null) {
228
+ this.$taroParams = uniqueOptions
229
+ }
230
+
231
+ setCurrentRouter(this)
232
+ window.trigger(CONTEXT_ACTIONS.INIT, $taroPath)
233
+
234
+ const mountCallback = () => {
235
+ pageElement = document.getElementById($taroPath)
236
+
237
+ ensure(pageElement !== null, '没有找到页面实例。')
238
+
239
+ safeExecute($taroPath, ONLOAD, this.$taroParams)
240
+ loadResolver()
241
+ cb && cb(pageElement)
242
+ pageElement.ctx = this
243
+ }
244
+
245
+ const mount = () => {
246
+ if (!Current.app) {
247
+ initNativeComponentEntry({
248
+ R: react,
249
+ ReactDOM,
250
+ cb: () => {
251
+ Current.app!.mount!(Component, $taroPath, () => this, mountCallback)
252
+ },
253
+ })
254
+ } else {
255
+ Current.app!.mount!(Component, $taroPath, () => this, mountCallback)
256
+ }
257
+ }
258
+
259
+ if (unmounting) {
260
+ prepareMountList.push(mount)
261
+ } else {
262
+ mount()
263
+ }
264
+ },
265
+ [ONUNLOAD] () {
266
+ const $taroPath = this.$taroPath
267
+ // 销毁当前页面的上下文信息
268
+ window.trigger(CONTEXT_ACTIONS.DESTORY, $taroPath)
269
+ // 触发onUnload生命周期
270
+ safeExecute($taroPath, ONUNLOAD)
271
+ resetCurrent.call(this)
272
+ unmounting = true
273
+ Current.app!.unmount!($taroPath, () => {
274
+ unmounting = false
275
+ removePageInstance($taroPath)
276
+ if (pageElement) {
277
+ pageElement.ctx = null
278
+ pageElement = null
279
+ }
280
+ if (prepareMountList.length) {
281
+ prepareMountList.forEach((fn) => fn())
282
+ prepareMountList = []
283
+ }
284
+ })
285
+ },
286
+ [ONREADY] () {
287
+ hasLoaded.then(() => {
288
+ // 触发生命周期
289
+ safeExecute(this.$taroPath, ON_READY)
290
+ // 通过事件触发子组件的生命周期
291
+ requestAnimationFrame(() => eventCenter.trigger(getOnReadyEventKey(id)))
292
+ this.onReady = {}
293
+ this.onReady.called = true
294
+ })
295
+ },
296
+ [ONSHOW] (options = {}) {
297
+ hasLoaded.then(() => {
298
+ // 设置 Current 的 page 和 router
299
+ Current.page = this as any
300
+ setCurrentRouter(this)
301
+ // 恢复上下文信息
302
+ window.trigger(CONTEXT_ACTIONS.RECOVER, this.$taroPath)
303
+ // 触发生命周期
304
+ safeExecute(this.$taroPath, ON_SHOW, options)
305
+ // 通过事件触发子组件的生命周期
306
+ requestAnimationFrame(() => eventCenter.trigger(getOnShowEventKey(id)))
307
+ })
308
+ },
309
+ [ONHIDE] () {
310
+ // 缓存当前页面上下文信息
311
+ window.trigger(CONTEXT_ACTIONS.RESTORE, this.$taroPath)
312
+ // 设置 Current 的 page 和 router
313
+ if (Current.page === this) {
314
+ Current.page = null
315
+ Current.router = null
316
+ }
317
+ // 触发生命周期
318
+ safeExecute(this.$taroPath, ON_HIDE)
319
+ // 通过事件触发子组件的生命周期
320
+ eventCenter.trigger(getOnHideEventKey(id))
321
+ },
322
+ }
323
+
324
+ function resetCurrent () {
325
+ if (Current.page === this) {
326
+ // 小程序插件页面卸载之后返回到宿主页面时,需重置Current页面和路由。否则引发插件组件二次加载异常 fix:#11991
327
+ Current.page = null
328
+ Current.router = null
329
+ }
330
+ }
331
+
332
+ LIFECYCLES.forEach((lifecycle) => {
333
+ pageObj[lifecycle] = function () {
334
+ return safeExecute(this.$taroPath, lifecycle, ...arguments)
335
+ }
336
+ })
337
+
338
+ // onShareAppMessage 和 onShareTimeline 一样,会影响小程序右上方按钮的选项,因此不能默认注册。
339
+ SIDE_EFFECT_LIFECYCLES.forEach((lifecycle) => {
340
+ if (Component[lifecycle] || Component.prototype?.[lifecycle] || Component[lifecycle.replace(/^on/, 'enable')]) {
341
+ pageObj[lifecycle] = function (...args) {
342
+ const target = args[0]?.target
343
+ if (target?.id) {
344
+ const id = target.id
345
+ const element = env.document.getElementById(id)
346
+ if (element) {
347
+ target.dataset = element.dataset
348
+ }
349
+ }
350
+ return safeExecute(this.$taroPath, lifecycle, ...args)
351
+ }
352
+ }
353
+ })
354
+
355
+ hooks.call('modifyPageObject', pageObj)
356
+
357
+ return pageObj
358
+ }
359
+
360
+ export function createNativeComponentConfig (
361
+ Component,
362
+ react: typeof React,
363
+ reactdom,
364
+ componentConfig
365
+ ) {
366
+ reactMeta.R = react
367
+ h = react.createElement
368
+ ReactDOM = reactdom
369
+ setReconciler(ReactDOM)
370
+ const { isNewBlended } = componentConfig
371
+
372
+ const componentObj: Record<string, any> = {
373
+ options: componentConfig,
374
+ onLoad (
375
+ options: Readonly<Record<string, unknown>> = {}, // eslint-disable-line @typescript-eslint/no-unused-vars
376
+ cb?: TFunc
377
+ ) {
378
+ const app = isNewBlended ? nativeComponentApp : Current.app
379
+
380
+ const mountComponent = () => {
381
+ const app = isNewBlended ? nativeComponentApp : Current.app
382
+ const compId = (this.compId = getNativeCompId())
383
+
384
+ this.config = componentConfig
385
+ app!.mount!(
386
+ Component,
387
+ compId,
388
+ () => this,
389
+ () => {
390
+ const el = document.getElementById(compId)
391
+
392
+ if (!el) {
393
+ throw new Error(`没有找到组件实例。`)
394
+ } else {
395
+ el.ctx = this
396
+ cb && cb(el)
397
+ }
398
+ }
399
+ )
400
+ }
401
+
402
+ if (!app) {
403
+ initNativeComponentEntry({
404
+ R: react,
405
+ ReactDOM,
406
+ isDefaultEntryDom: !isNewBlended,
407
+ cb: mountComponent,
408
+ })
409
+ } else {
410
+ mountComponent()
411
+ }
412
+ },
413
+
414
+ onUnload () {
415
+ const app = isNewBlended ? nativeComponentApp : Current.app
416
+ app!.unmount!(this.compId)
417
+ },
418
+ }
419
+
420
+ return componentObj
421
+ }