@tarojs/plugin-platform-harmony-cpp 4.1.0 → 4.1.1-alpha.1

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 (225) hide show
  1. package/README.md +0 -7
  2. package/dist/index.js +1236 -0
  3. package/dist/runtime/apis/apis.ts +20 -0
  4. package/dist/runtime/apis/base/crypto.ts +4 -0
  5. package/dist/runtime/apis/base/debug.ts +5 -0
  6. package/dist/runtime/apis/base/index.ts +13 -0
  7. package/dist/runtime/apis/base/performance.ts +8 -0
  8. package/dist/runtime/apis/base/system.ts +181 -0
  9. package/dist/runtime/apis/base/update.ts +5 -0
  10. package/dist/runtime/apis/base/weapp/app-event.ts +75 -0
  11. package/dist/runtime/apis/base/weapp/life-cycle.ts +21 -0
  12. package/dist/runtime/apis/canvas/index.ts +27 -0
  13. package/dist/runtime/apis/data-analysis/index.ts +6 -0
  14. package/dist/runtime/apis/device/accelerometer.ts +79 -0
  15. package/dist/runtime/apis/device/accessibility.ts +4 -0
  16. package/dist/runtime/apis/device/battery.ts +24 -0
  17. package/dist/runtime/apis/device/bluetooth-ble.ts +19 -0
  18. package/dist/runtime/apis/device/bluetooth-peripheral.ts +6 -0
  19. package/dist/runtime/apis/device/bluetooth.ts +16 -0
  20. package/dist/runtime/apis/device/calendar.ts +5 -0
  21. package/dist/runtime/apis/device/clipboard.ts +85 -0
  22. package/dist/runtime/apis/device/compass.ts +21 -0
  23. package/dist/runtime/apis/device/contact.ts +5 -0
  24. package/dist/runtime/apis/device/crypto.ts +4 -0
  25. package/dist/runtime/apis/device/gyroscope.ts +77 -0
  26. package/dist/runtime/apis/device/iBeacon.ts +10 -0
  27. package/dist/runtime/apis/device/index.ts +24 -0
  28. package/dist/runtime/apis/device/keyboard.ts +63 -0
  29. package/dist/runtime/apis/device/memory.ts +11 -0
  30. package/dist/runtime/apis/device/motion.ts +6 -0
  31. package/dist/runtime/apis/device/network.ts +123 -0
  32. package/dist/runtime/apis/device/nfc.ts +10 -0
  33. package/dist/runtime/apis/device/phone.ts +44 -0
  34. package/dist/runtime/apis/device/scan.ts +4 -0
  35. package/dist/runtime/apis/device/screen.ts +80 -0
  36. package/dist/runtime/apis/device/sms.ts +4 -0
  37. package/dist/runtime/apis/device/vibrate.ts +32 -0
  38. package/dist/runtime/apis/device/wifi.ts +15 -0
  39. package/dist/runtime/apis/ext/index.ts +5 -0
  40. package/dist/runtime/apis/files/index.ts +136 -0
  41. package/dist/runtime/apis/files/manager.ts +942 -0
  42. package/dist/runtime/apis/framework/index.ts +48 -0
  43. package/dist/runtime/apis/harmony/task-pool.ts +39 -0
  44. package/dist/runtime/apis/index.ts +61 -0
  45. package/dist/runtime/apis/location/index.ts +133 -0
  46. package/dist/runtime/apis/media/EditorContext.ts +32 -0
  47. package/dist/runtime/apis/media/audio/index.ts +36 -0
  48. package/dist/runtime/apis/media/background-audio/index.ts +16 -0
  49. package/dist/runtime/apis/media/camera.ts +16 -0
  50. package/dist/runtime/apis/media/common.ts +58 -0
  51. package/dist/runtime/apis/media/image/index.ts +291 -0
  52. package/dist/runtime/apis/media/index.ts +13 -0
  53. package/dist/runtime/apis/media/live.ts +5 -0
  54. package/dist/runtime/apis/media/map.ts +4 -0
  55. package/dist/runtime/apis/media/media-recorder.ts +4 -0
  56. package/dist/runtime/apis/media/recorder.ts +6 -0
  57. package/dist/runtime/apis/media/video/VideoContext.ts +67 -0
  58. package/dist/runtime/apis/media/video/index.ts +45 -0
  59. package/dist/runtime/apis/media/video-decoder.ts +4 -0
  60. package/dist/runtime/apis/media/video-processing.ts +4 -0
  61. package/dist/runtime/apis/media/voip.ts +19 -0
  62. package/dist/runtime/apis/navigate/index.ts +8 -0
  63. package/dist/runtime/apis/network/downloadFile.ts +85 -0
  64. package/dist/runtime/apis/network/index.ts +7 -0
  65. package/dist/runtime/apis/network/mdns.ts +13 -0
  66. package/dist/runtime/apis/network/request.ts +140 -0
  67. package/dist/runtime/apis/network/tcp.ts +4 -0
  68. package/dist/runtime/apis/network/udp.ts +4 -0
  69. package/dist/runtime/apis/network/uploadFile.ts +105 -0
  70. package/dist/runtime/apis/network/webSocket.ts +126 -0
  71. package/dist/runtime/apis/open-api/account.ts +4 -0
  72. package/dist/runtime/apis/open-api/address.ts +4 -0
  73. package/dist/runtime/apis/open-api/authorize.ts +5 -0
  74. package/dist/runtime/apis/open-api/card.ts +5 -0
  75. package/dist/runtime/apis/open-api/channels-live.ts +11 -0
  76. package/dist/runtime/apis/open-api/customer-service.ts +4 -0
  77. package/dist/runtime/apis/open-api/device-voip.ts +5 -0
  78. package/dist/runtime/apis/open-api/facial.ts +7 -0
  79. package/dist/runtime/apis/open-api/favorites.ts +5 -0
  80. package/dist/runtime/apis/open-api/group.ts +4 -0
  81. package/dist/runtime/apis/open-api/index.ts +21 -0
  82. package/dist/runtime/apis/open-api/invoice.ts +5 -0
  83. package/dist/runtime/apis/open-api/license-plate.ts +4 -0
  84. package/dist/runtime/apis/open-api/login.ts +6 -0
  85. package/dist/runtime/apis/open-api/my-miniprogram.ts +4 -0
  86. package/dist/runtime/apis/open-api/privacy.ts +7 -0
  87. package/dist/runtime/apis/open-api/red-package.ts +4 -0
  88. package/dist/runtime/apis/open-api/settings.ts +5 -0
  89. package/dist/runtime/apis/open-api/soter.ts +6 -0
  90. package/dist/runtime/apis/open-api/subscribe-message.ts +6 -0
  91. package/dist/runtime/apis/open-api/user-info.ts +70 -0
  92. package/dist/runtime/apis/open-api/werun.ts +5 -0
  93. package/dist/runtime/apis/payment/index.ts +6 -0
  94. package/dist/runtime/apis/route/index.ts +88 -0
  95. package/dist/runtime/apis/share/index.ts +33 -0
  96. package/dist/runtime/apis/storage/background-fetch.ts +7 -0
  97. package/dist/runtime/apis/storage/cache-manager.ts +4 -0
  98. package/dist/runtime/apis/storage/index.ts +203 -0
  99. package/dist/runtime/apis/ui/animation/animation.ts +264 -0
  100. package/dist/runtime/apis/ui/animation/index.ts +7 -0
  101. package/dist/runtime/apis/ui/background.ts +20 -0
  102. package/dist/runtime/apis/ui/custom-component.ts +1 -0
  103. package/dist/runtime/apis/ui/fonts.ts +4 -0
  104. package/dist/runtime/apis/ui/index.ts +12 -0
  105. package/dist/runtime/apis/ui/interaction/index.ts +198 -0
  106. package/dist/runtime/apis/ui/menu.ts +4 -0
  107. package/dist/runtime/apis/ui/navigation-bar/index.ts +72 -0
  108. package/dist/runtime/apis/ui/pull-down-refresh.ts +47 -0
  109. package/dist/runtime/apis/ui/scroll/index.ts +82 -0
  110. package/dist/runtime/apis/ui/sticky.ts +4 -0
  111. package/dist/runtime/apis/ui/tab-bar.ts +144 -0
  112. package/dist/runtime/apis/ui/window.ts +20 -0
  113. package/dist/runtime/apis/utils/constant.ts +1 -0
  114. package/dist/runtime/apis/utils/handler.ts +117 -0
  115. package/dist/runtime/apis/utils/index.ts +105 -0
  116. package/dist/runtime/apis/utils/permissions.ts +6 -0
  117. package/dist/runtime/apis/utils/types.ts +12 -0
  118. package/dist/runtime/apis/utils/unit.ts +104 -0
  119. package/dist/runtime/apis/utils/validate.ts +87 -0
  120. package/dist/runtime/apis/worker/index.ts +4 -0
  121. package/dist/runtime/apis/wxml/index.ts +17 -0
  122. package/dist/runtime/apis/wxml/intersectionObserver.ts +100 -0
  123. package/dist/runtime/apis/wxml/nodesRef.ts +58 -0
  124. package/dist/runtime/apis/wxml/selectorQuery.ts +221 -0
  125. package/dist/runtime/apischunk/index.d.ts +802 -0
  126. package/dist/runtime/apischunk/index.js +5054 -0
  127. package/dist/runtime/components/deprecated.ets +43 -0
  128. package/dist/runtime/components/index.ets +49 -0
  129. package/dist/runtime/components/innerHtml.ets +16 -0
  130. package/dist/runtime/components/navigationBar.ets +65 -0
  131. package/dist/runtime/components/pageMeta.ets +94 -0
  132. package/dist/runtime/components/richText.ets +24 -0
  133. package/dist/runtime/components/slider.ets +119 -0
  134. package/dist/runtime/components/style.ets +286 -0
  135. package/dist/runtime/components/switch.ets +73 -0
  136. package/dist/runtime/components/tag.ts +58 -0
  137. package/dist/runtime/components/utils/AttributeManager.ets +252 -0
  138. package/dist/runtime/components/utils/DynamicCenter.ts +41 -0
  139. package/dist/runtime/components/utils/constant/event.ets +25 -0
  140. package/dist/runtime/components/utils/constant/style.ets +91 -0
  141. package/dist/runtime/components/utils/events.ts +26 -0
  142. package/dist/runtime/components/utils/flexManager.ets +49 -0
  143. package/dist/runtime/components/utils/helper.ets +51 -0
  144. package/dist/runtime/components/utils/htmlParser/HarmonyHTMLParser.ts +100 -0
  145. package/dist/runtime/components/utils/htmlParser/index.ts +58 -0
  146. package/dist/runtime/components/utils/index.ts +77 -0
  147. package/dist/runtime/components/utils/styles.ets +41 -0
  148. package/dist/runtime/components/video.ets +119 -0
  149. package/dist/runtime/components/webView.ets +55 -0
  150. package/dist/runtime/components/xComponent.ets +89 -0
  151. package/dist/runtime/framework/app.ts +248 -0
  152. package/dist/runtime/framework/connect.ts +24 -0
  153. package/dist/runtime/framework/constant.ts +3 -0
  154. package/dist/runtime/framework/hooks.ts +99 -0
  155. package/dist/runtime/framework/index.ts +15 -0
  156. package/dist/runtime/framework/native-page.ts +511 -0
  157. package/dist/runtime/framework/page.ts +256 -0
  158. package/dist/runtime/framework/utils/index.ts +17 -0
  159. package/dist/runtime/framework/utils/is.ts +26 -0
  160. package/dist/runtime/framework-reconciler/componentTree.ts +81 -0
  161. package/dist/runtime/framework-reconciler/constant.ts +86 -0
  162. package/dist/runtime/framework-reconciler/domInput.ts +90 -0
  163. package/dist/runtime/framework-reconciler/event.ts +108 -0
  164. package/dist/runtime/framework-reconciler/index.ts +99 -0
  165. package/dist/runtime/framework-reconciler/inputValueTracking.ts +106 -0
  166. package/dist/runtime/framework-reconciler/props.ts +132 -0
  167. package/dist/runtime/framework-reconciler/reconciler.ts +202 -0
  168. package/dist/runtime/framework-reconciler/render.ts +139 -0
  169. package/dist/runtime/framework-reconciler/workTags.ts +53 -0
  170. package/dist/runtime/runtime-cpp/bom/URL.ts +2 -0
  171. package/dist/runtime/runtime-cpp/bom/document.ts +45 -0
  172. package/dist/runtime/runtime-cpp/bom/history.ts +1 -0
  173. package/dist/runtime/runtime-cpp/bom/location.ts +1 -0
  174. package/dist/runtime/runtime-cpp/bom/navigator.ts +1 -0
  175. package/dist/runtime/runtime-cpp/bom/raf.ts +1 -0
  176. package/dist/runtime/runtime-cpp/bom/window.ts +57 -0
  177. package/dist/runtime/runtime-cpp/constant.ts +29 -0
  178. package/dist/runtime/runtime-cpp/current.ts +48 -0
  179. package/dist/runtime/runtime-cpp/dom/class-list.ts +41 -0
  180. package/dist/runtime/runtime-cpp/dom/comment.ts +9 -0
  181. package/dist/runtime/runtime-cpp/dom/dataSource.ts +87 -0
  182. package/dist/runtime/runtime-cpp/dom/document.ts +113 -0
  183. package/dist/runtime/runtime-cpp/dom/element/canvas.ts +285 -0
  184. package/dist/runtime/runtime-cpp/dom/element/element.ts +262 -0
  185. package/dist/runtime/runtime-cpp/dom/element/form.ts +371 -0
  186. package/dist/runtime/runtime-cpp/dom/element/index.ts +129 -0
  187. package/dist/runtime/runtime-cpp/dom/element/movable.ts +250 -0
  188. package/dist/runtime/runtime-cpp/dom/element/normal.ts +123 -0
  189. package/dist/runtime/runtime-cpp/dom/element/scroll_view.ts +32 -0
  190. package/dist/runtime/runtime-cpp/dom/element/text.ts +9 -0
  191. package/dist/runtime/runtime-cpp/dom/element/video.ts +66 -0
  192. package/dist/runtime/runtime-cpp/dom/element/web_view.ts +72 -0
  193. package/dist/runtime/runtime-cpp/dom/event-source.ts +1 -0
  194. package/dist/runtime/runtime-cpp/dom/event.ts +187 -0
  195. package/dist/runtime/runtime-cpp/dom/eventTarget.ts +81 -0
  196. package/dist/runtime/runtime-cpp/dom/node.ts +307 -0
  197. package/dist/runtime/runtime-cpp/dom/style.ts +44 -0
  198. package/dist/runtime/runtime-cpp/dom/stylesheet/index.ts +379 -0
  199. package/dist/runtime/runtime-cpp/dom/stylesheet/type.ts +212 -0
  200. package/dist/runtime/runtime-cpp/dom/stylesheet/util.ts +186 -0
  201. package/dist/runtime/runtime-cpp/emitter/emitter.ts +29 -0
  202. package/dist/runtime/runtime-cpp/env.ts +1 -0
  203. package/dist/runtime/runtime-cpp/harmony-library.ts +1 -0
  204. package/dist/runtime/runtime-cpp/index.ts +79 -0
  205. package/dist/runtime/runtime-cpp/interface/event.ts +10 -0
  206. package/dist/runtime/runtime-cpp/interface/index.ts +7 -0
  207. package/dist/runtime/runtime-cpp/next-tick.ts +14 -0
  208. package/dist/runtime/runtime-cpp/system.ts +213 -0
  209. package/dist/runtime/runtime-cpp/utils/index.ts +63 -0
  210. package/dist/runtime/runtime-cpp/utils/info.ts +123 -0
  211. package/dist/runtime/runtime-cpp/utils/page.ts +12 -0
  212. package/dist/runtime/runtime-cpp/utils/router.ts +1 -0
  213. package/dist/runtime/runtime-harmony/apis/helper.ets +33 -0
  214. package/dist/runtime/runtime-harmony/apis/network/common.ets +41 -0
  215. package/dist/runtime/runtime-harmony/apis/network/downloadFile.ets +327 -0
  216. package/dist/runtime/runtime-harmony/apis/network/index.ets +58 -0
  217. package/dist/runtime/runtime-harmony/apis/network/request.ets +206 -0
  218. package/dist/runtime/runtime-harmony/apis/network/uploadFile.ets +190 -0
  219. package/dist/runtime/runtime-harmony/apis/route.ets +110 -0
  220. package/dist/runtime/runtime-harmony/apis/taskpool.ets +149 -0
  221. package/dist/runtime/runtime-harmony/index.ets +41 -0
  222. package/dist/runtime/runtime-harmony/utils.ts +53 -0
  223. package/package.json +25 -23
  224. package/static/@taro-oh/library-4.1.1-alpha.0.har +0 -0
  225. package/LICENSE +0 -174
@@ -0,0 +1,256 @@
1
+ import { addLeadingSlash, CONTEXT_ACTIONS, Current, document, env, eventCenter, requestAnimationFrame, window } from '@tarojs/runtime'
2
+ import { hooks } from '@tarojs/shared'
3
+
4
+ import { ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, ON_UNLOAD } from './constant'
5
+ import { incrementId, isArray, isFunction, isUndefined } from './utils'
6
+
7
+ import type { PageConfig } from '@tarojs/taro'
8
+
9
+ const instances = new Map<string, any>()
10
+ const pageId = incrementId(1)
11
+
12
+ export function injectPageInstance (inst: any, id: string) {
13
+ hooks.call('mergePageInstance', instances.get(id), inst)
14
+ instances.set(id, inst)
15
+ }
16
+
17
+ export function getPageInstance (id: string): any {
18
+ return instances.get(id)
19
+ }
20
+
21
+ export function removePageInstance (id: string) {
22
+ instances.delete(id)
23
+ }
24
+
25
+ export function safeExecute (path: string, lifecycle: string, ...args: unknown[]) {
26
+ const instance = instances.get(path)
27
+
28
+ if (instance == null) {
29
+ return
30
+ }
31
+
32
+ lifecycle = lifecycle.replace(/^on(Show|Hide)$/, 'componentDid$1')
33
+ const func = instance[lifecycle]
34
+
35
+ if (isArray(func)) {
36
+ const res = func.map(fn => fn.apply(instance, args))
37
+ return res[0]
38
+ }
39
+
40
+ if (!isFunction(func)) {
41
+ return
42
+ }
43
+
44
+ return func.apply(instance, args)
45
+ }
46
+
47
+ export function stringify (obj?: Record<string, unknown>) {
48
+ if (obj == null) {
49
+ return ''
50
+ }
51
+ const path = Object.keys(obj).map((key) => {
52
+ return key + '=' + obj[key]
53
+ }).join('&')
54
+ return path === '' ? path : '?' + path
55
+ }
56
+
57
+ export function getPath (id: string, options?: Record<string, unknown>): string {
58
+ const idx = id.indexOf('?')
59
+
60
+ return `${idx > -1 ? id.substring(0, idx) : id}${stringify(options)}`
61
+ }
62
+
63
+ export function getOnReadyEventKey (path: string) {
64
+ return path + '.' + ON_READY
65
+ }
66
+
67
+ export function getOnShowEventKey (path: string) {
68
+ return path + '.' + ON_SHOW
69
+ }
70
+
71
+ export function getOnHideEventKey (path: string) {
72
+ return path + '.' + ON_HIDE
73
+ }
74
+
75
+ export function createPageConfig (component: any, pageName?: string, pageConfig?: PageConfig) {
76
+ // 小程序 Page 构造器是一个傲娇小公主,不能把复杂的对象挂载到参数上
77
+ const id = pageName ?? `taro_page_${pageId()}`
78
+ const [
79
+ ONLOAD,
80
+ ONUNLOAD,
81
+ ONREADY,
82
+ ONSHOW,
83
+ ONHIDE,
84
+ LIFECYCLES,
85
+ SIDE_EFFECT_LIFECYCLES,
86
+ ] = hooks.call('getMiniLifecycleImpl')!.page
87
+
88
+ let pageElement: any = null
89
+ let unmounting = false
90
+ let prepareMountList: (() => void)[] = []
91
+
92
+ function setCurrentRouter (page) {
93
+ const router = page.route || page.__route__ || page.$taroPath
94
+
95
+ Current.router = {
96
+ params: page.$taroParams!,
97
+ path: addLeadingSlash(router),
98
+ $taroPath: page.$taroPath,
99
+ onReady: getOnReadyEventKey(page.$taroPath),
100
+ onShow: getOnShowEventKey(page.$taroPath),
101
+ onHide: getOnHideEventKey(page.$taroPath),
102
+ getEventName (event: string) {
103
+ return `${page.$taroPath}.${event}`
104
+ },
105
+ }
106
+
107
+ if (!isUndefined(page.exitState)) {
108
+ Current.router.exitState = page.exitState
109
+ }
110
+ }
111
+
112
+ let loadResolver: (...args: any[]) => void
113
+ let hasLoaded: Promise<void>
114
+ const page = {
115
+ [ONLOAD] (options: Readonly<Record<string, unknown>> = {}, cb?: (...args: any[]) => any) {
116
+ hasLoaded = new Promise(resolve => { loadResolver = resolve })
117
+
118
+ Current.page = this as any
119
+
120
+ // this.$taroPath 是页面唯一标识
121
+ const uniqueOptions = Object.assign({}, options, { $taroTimestamp: Date.now() })
122
+ const $taroPath = this.$taroPath = getPath(id, uniqueOptions)
123
+
124
+ // this.$taroParams 作为暴露给开发者的页面参数对象,可以被随意修改
125
+ if (this.$taroParams == null) {
126
+ this.$taroParams = uniqueOptions
127
+ }
128
+
129
+ setCurrentRouter(this)
130
+
131
+ window.trigger(CONTEXT_ACTIONS.INIT, $taroPath)
132
+
133
+ const mount = () => {
134
+ Current.app!.mount!(component, $taroPath, null as any, () => {
135
+ pageElement = document.getElementById($taroPath)
136
+
137
+ if (!pageElement) {
138
+ throw new Error(`没有找到页面实例。`)
139
+ }
140
+
141
+ safeExecute($taroPath, ON_LOAD, this.$taroParams)
142
+ loadResolver()
143
+ cb && cb(pageElement)
144
+ pageElement.ctx = this
145
+ })
146
+ }
147
+
148
+ if (unmounting) {
149
+ prepareMountList.push(mount)
150
+ } else {
151
+ mount()
152
+ }
153
+ },
154
+ [ONUNLOAD] () {
155
+ const $taroPath = this.$taroPath
156
+ // 销毁当前页面的上下文信息
157
+ window.trigger(CONTEXT_ACTIONS.DESTORY, $taroPath)
158
+
159
+ // 触发onUnload生命周期
160
+ safeExecute($taroPath, ON_UNLOAD)
161
+ unmounting = true
162
+ Current.app!.unmount!($taroPath, () => {
163
+ unmounting = false
164
+ instances.delete($taroPath)
165
+ if (pageElement) {
166
+ pageElement.ctx = null
167
+ pageElement = null
168
+ }
169
+ if (prepareMountList.length) {
170
+ prepareMountList.forEach(fn => fn())
171
+ prepareMountList = []
172
+ }
173
+ })
174
+ },
175
+ [ONREADY] () {
176
+ hasLoaded.then(() => {
177
+ // 触发生命周期
178
+ safeExecute(this.$taroPath, ON_READY)
179
+ // 通过事件触发子组件的生命周期
180
+ requestAnimationFrame(() => eventCenter.trigger(getOnReadyEventKey(this.$taroPath)))
181
+ this.onReady = {}
182
+ this.onReady.called = true
183
+ })
184
+ },
185
+ [ONSHOW] (options = {}) {
186
+ hasLoaded.then(() => {
187
+ // 设置 Current 的 page 和 router
188
+ Current.page = this as any
189
+ setCurrentRouter(this)
190
+ // 恢复上下文信息
191
+ window.trigger(CONTEXT_ACTIONS.RECOVER, this.$taroPath)
192
+ // 触发生命周期
193
+ safeExecute(this.$taroPath, ON_SHOW, options)
194
+ // 通过事件触发子组件的生命周期
195
+ requestAnimationFrame(() => eventCenter.trigger(getOnShowEventKey(this.$taroPath)))
196
+ })
197
+ },
198
+ [ONHIDE] () {
199
+ // 缓存当前页面上下文信息
200
+ window.trigger(CONTEXT_ACTIONS.RESTORE, this.$taroPath)
201
+
202
+ // 设置 Current 的 page 和 router
203
+ if (Current.page === this) {
204
+ Current.page = null
205
+ Current.router = null
206
+ }
207
+ // 触发生命周期
208
+ safeExecute(this.$taroPath, ON_HIDE)
209
+ // TODO 通过事件触发子组件的生命周期
210
+ eventCenter.trigger(getOnHideEventKey(this.$taroPath))
211
+ },
212
+ }
213
+
214
+ LIFECYCLES.forEach((lifecycle) => {
215
+ let isDefer = false
216
+ lifecycle = lifecycle.replace(/^defer:/, () => {
217
+ isDefer = true
218
+ return ''
219
+ })
220
+ page[lifecycle] = function () {
221
+ const exec = () => {
222
+ eventCenter.trigger(`${this.$taroPath}.${lifecycle}`, ...arguments)
223
+ return safeExecute(this.$taroPath, lifecycle, ...arguments)
224
+ }
225
+ if (isDefer) {
226
+ hasLoaded.then(exec)
227
+ } else {
228
+ return exec()
229
+ }
230
+ }
231
+ })
232
+
233
+ // onShareAppMessage 和 onShareTimeline 一样,会影响小程序右上方按钮的选项,因此不能默认注册。
234
+ SIDE_EFFECT_LIFECYCLES.forEach(lifecycle => {
235
+ if (component[lifecycle] ||
236
+ component.prototype?.[lifecycle] ||
237
+ component[lifecycle.replace(/^on/, 'enable')] ||
238
+ pageConfig?.[lifecycle.replace(/^on/, 'enable')]
239
+ ) {
240
+ page[lifecycle] = function (...args) {
241
+ const target = args[0]?.target as any
242
+ if (target?.id) {
243
+ const id = target.id
244
+ const element = (env.document as any).container.getElementById(id)
245
+ if (element) {
246
+ target.dataset = element.dataset
247
+ }
248
+ }
249
+ eventCenter.trigger(`${this.$taroPath}.${lifecycle}`, ...arguments)
250
+ return safeExecute(this.$taroPath, lifecycle, ...args)
251
+ }
252
+ }
253
+ })
254
+
255
+ return page
256
+ }
@@ -0,0 +1,17 @@
1
+ import { isArray } from './is'
2
+
3
+ export * from './is'
4
+ export { capitalize, noop } from '@tarojs/shared'
5
+
6
+ export const incrementId = (init = 0) => {
7
+ let n = init
8
+ return () => n++
9
+ }
10
+
11
+ export function ensureIsArray<T> (item: T | T[]): T[] {
12
+ if (isArray(item)) {
13
+ return item
14
+ } else {
15
+ return item ? [item] : []
16
+ }
17
+ }
@@ -0,0 +1,26 @@
1
+ import { isFunction } from '@tarojs/shared'
2
+
3
+ export function isClassComponent (R, component) {
4
+ const prototype = component.prototype
5
+
6
+ // For React Redux
7
+ if (component.displayName?.includes('Connect')) return false
8
+
9
+ return (
10
+ isFunction(component.render) ||
11
+ !!prototype?.isReactComponent ||
12
+ prototype instanceof R.Component // compat for some others react-like library
13
+ )
14
+ }
15
+
16
+ export {
17
+ isArray,
18
+ isBoolean,
19
+ isBooleanStringLiteral,
20
+ isFunction,
21
+ isNull,
22
+ isNumber,
23
+ isObject,
24
+ isString,
25
+ isUndefined,
26
+ } from '@tarojs/shared'
@@ -0,0 +1,81 @@
1
+ /**
2
+ * 给 TaroElement 绑定 react fiber、react props 等属性
3
+ * 提供 fiber -> element、element -> fiber、element -> props 的方法
4
+ */
5
+ import { internalContainerInstanceKey, internalEventHandlerListenersKey, internalEventHandlersKey, internalEventHandlesSetKey, internalInstanceKey, internalPropsKey } from './constant'
6
+ import { HostComponent, HostRoot, HostText, SuspenseComponent } from './workTags'
7
+
8
+ import type { TaroElement, TaroText } from '@tarojs/runtime'
9
+ import type { Fiber } from 'react-reconciler'
10
+ import type { Props } from './props'
11
+
12
+ export function precacheFiberNode (hostInst: Fiber, node: TaroElement | TaroText): void {
13
+ node[internalInstanceKey] = hostInst
14
+ }
15
+
16
+ export function markContainerAsRoot (hostRoot: Fiber, node: TaroElement | TaroText): void {
17
+ node[internalContainerInstanceKey] = hostRoot
18
+ }
19
+
20
+ export function unmarkContainerAsRoot (node: TaroElement | TaroText): void {
21
+ node[internalContainerInstanceKey] = null
22
+ }
23
+
24
+ export function isContainerMarkedAsRoot (node: TaroElement | TaroText): boolean {
25
+ return !!node[internalContainerInstanceKey]
26
+ }
27
+
28
+ /**
29
+ * Given a DOM node, return the ReactDOMComponent or ReactDOMTextComponent
30
+ * instance, or null if the node was not rendered by this React.
31
+ */
32
+ export function getInstanceFromNode (node: TaroElement | TaroText): Fiber | null {
33
+ const inst = node[internalInstanceKey] || node[internalContainerInstanceKey]
34
+
35
+ if (inst) {
36
+ if (
37
+ inst.tag === HostComponent ||
38
+ inst.tag === HostText ||
39
+ inst.tag === SuspenseComponent ||
40
+ inst.tag === HostRoot
41
+ ) {
42
+ return inst
43
+ } else {
44
+ return null
45
+ }
46
+ }
47
+ return null
48
+ }
49
+
50
+ /**
51
+ * Given a ReactDOMComponent or ReactDOMTextComponent, return the corresponding
52
+ * DOM node.
53
+ */
54
+ export function getNodeFromInstance (inst: Fiber) {
55
+ if (inst.tag === HostComponent || inst.tag === HostText) {
56
+ // In Fiber this, is just the state node right now. We assume it will be
57
+ // a host component or host text.
58
+ return inst.stateNode
59
+ }
60
+ }
61
+
62
+ export function getFiberCurrentPropsFromNode (node: TaroElement | TaroText): Props {
63
+ return node[internalPropsKey] || null
64
+ }
65
+
66
+ export function updateFiberProps (
67
+ node: TaroElement | TaroText,
68
+ props: Props,
69
+ ): void {
70
+ node[internalPropsKey] = props
71
+ }
72
+
73
+ export function detachDeletedInstance (node: TaroElement | TaroText) {
74
+ (node as any).dispose?.()
75
+
76
+ delete node[internalInstanceKey]
77
+ delete node[internalPropsKey]
78
+ delete node[internalEventHandlersKey]
79
+ delete node[internalEventHandlerListenersKey]
80
+ delete node[internalEventHandlesSetKey]
81
+ }
@@ -0,0 +1,86 @@
1
+ export const supportedInputTypes: { [key: string]: boolean } = {
2
+ color: true,
3
+ date: true,
4
+ datetime: true,
5
+ 'datetime-local': true,
6
+ email: true,
7
+ month: true,
8
+ number: true,
9
+ password: true,
10
+ range: true,
11
+ search: true,
12
+ tel: true,
13
+ text: true,
14
+ time: true,
15
+ url: true,
16
+ week: true,
17
+ }
18
+
19
+ const SyncLane = 1
20
+ const InputContinuousLane = 4
21
+ const DefaultLane = 16
22
+ const DiscreteEventPriority = SyncLane
23
+ const ContinuousEventPriority = InputContinuousLane
24
+ const DefaultEventPriority = DefaultLane
25
+
26
+ export function getEventPriority (domEventName) {
27
+ switch (domEventName) {
28
+ case 'cancel':
29
+ case 'click':
30
+ case 'close':
31
+ case 'contextmenu':
32
+ case 'copy':
33
+ case 'cut':
34
+ case 'dragend':
35
+ case 'dragstart':
36
+ case 'drop':
37
+ case 'input':
38
+ case 'paste':
39
+ case 'pause':
40
+ case 'play':
41
+ case 'pointercancel':
42
+ case 'pointerdown':
43
+ case 'pointerup':
44
+ case 'reset':
45
+ case 'resize':
46
+ case 'submit':
47
+ case 'touchcancel':
48
+ case 'touchend':
49
+ case 'touchstart':
50
+ case 'change':
51
+ case 'blur':
52
+ case 'focus':
53
+ case 'select':
54
+ case 'selectstart':
55
+ return DiscreteEventPriority
56
+ case 'drag':
57
+ case 'dragenter':
58
+ case 'dragexit':
59
+ case 'dragleave':
60
+ case 'dragover':
61
+ case 'pointermove':
62
+ case 'pointerout':
63
+ case 'pointerover':
64
+ case 'scroll':
65
+ case 'toggle':
66
+ case 'touchmove':
67
+ case 'pointerenter':
68
+ case 'pointerleave':
69
+ return ContinuousEventPriority
70
+ default:
71
+ return DefaultEventPriority
72
+ }
73
+ }
74
+
75
+ const randomKey = Math.random()
76
+ .toString(36)
77
+ .slice(2)
78
+
79
+ export const internalPropsKey = '__reactProps$' + randomKey
80
+ export const internalInstanceKey = '__reactFiber$' + randomKey
81
+ export const internalContainerInstanceKey = '__reactContainer$' + randomKey
82
+ export const internalEventHandlersKey = '__reactEvents$' + randomKey
83
+ export const internalEventHandlerListenersKey = '__reactListeners$' + randomKey
84
+ export const internalEventHandlesSetKey = '__reactHandles$' + randomKey
85
+ export const internalRootNodeResourcesKey = '__reactResources$' + randomKey
86
+ export const internalHoistableMarker = '__reactMarker$' + randomKey
@@ -0,0 +1,90 @@
1
+ import { supportedInputTypes } from './constant'
2
+ import { Props } from './props'
3
+
4
+ import type { FormElement, TaroElement } from '@tarojs/runtime'
5
+ import type { RestoreType } from './event'
6
+
7
+ // 从 props 中,更新 input 组件的 value 值
8
+ function updateInputWrapper (element: TaroElement, oldValue: RestoreType, props: Props) {
9
+ const node = element
10
+ const checked = props.checked
11
+
12
+ if (checked != null) {
13
+ console.warn('updateCheck 未实现', node)
14
+ return
15
+ }
16
+
17
+ updateWrapper(element, oldValue, props)
18
+ updateNamedCousins(element, props)
19
+ }
20
+
21
+ // react 中原本处理 type=radio 的逻辑,这里留个空,暂时不处理
22
+ function updateNamedCousins (rootNode, props) {
23
+ const name = props.name
24
+
25
+ if (props.type === 'radio' && name != null) {
26
+ console.warn('radio updateNamedCousins 未实现', rootNode, props)
27
+ }
28
+ }
29
+
30
+ export function getToStringValue (value: any) {
31
+ const isEmptyType = typeof value === 'function' || typeof value === 'symbol'
32
+
33
+ return isEmptyType ? '' : value
34
+ }
35
+
36
+ export function toString (value): string {
37
+ return '' + value
38
+ }
39
+
40
+ export function updateWrapper (element: TaroElement, oldValue: RestoreType, props: Props) {
41
+ const node = element as FormElement
42
+ const value = getToStringValue(props.value)
43
+ const type = props.type as string
44
+
45
+ setNodeValue(node, oldValue, value, type)
46
+ }
47
+
48
+ // oldValue 为 event.detail.value,value 为 fiber.props.value
49
+ // 如果 oldValue 和 value 不相等,代表受控组件需要更新
50
+ // 更新的原则为,fiber.props.value 永远为用户所需要的值,因此 node.value = toString(value)
51
+ export function setNodeValue (node, oldValue: RestoreType, value, type = 'string') {
52
+ if (value != null) {
53
+ if (type === 'number') {
54
+ if (
55
+ (value === 0 && node.value === '')
56
+ // We explicitly want to coerce to number here if possible.
57
+ // eslint-disable-next-line
58
+ || oldValue != value
59
+ ) {
60
+ node.value = toString(value)
61
+ }
62
+ } else if (oldValue !== toString(value)) {
63
+ node.value = toString(value)
64
+ }
65
+ } else if (type === 'submit' || type === 'reset') {
66
+ // Submit/reset inputs need the attribute removed completely to avoid
67
+ // blank-text buttons.
68
+ node.removeAttribute('value')
69
+ }
70
+ }
71
+
72
+ // 判断当前 TaroElement 是否为 supportedInputTypes input 或 textarea
73
+ export function isTextInputElement (elem: TaroElement): boolean {
74
+ const nodeName = elem && elem.nodeName && elem.nodeName.toLowerCase()
75
+
76
+ if (nodeName === 'input') {
77
+ const type = (elem as FormElement).type
78
+
79
+ return !type || !!supportedInputTypes[type]
80
+ }
81
+
82
+ if (nodeName === 'textarea') {
83
+ return true
84
+ }
85
+
86
+ return false
87
+ }
88
+
89
+ export const ReactDOMTextareaRestoreControlledState = updateWrapper
90
+ export const ReactDOMInputRestoreControlledState = updateInputWrapper
@@ -0,0 +1,108 @@
1
+ import { getFiberCurrentPropsFromNode, getInstanceFromNode, getNodeFromInstance } from './componentTree'
2
+ import { isTextInputElement, ReactDOMInputRestoreControlledState, ReactDOMTextareaRestoreControlledState, toString } from './domInput'
3
+ import { updateValueIfChanged } from './inputValueTracking'
4
+ import { TaroReconciler } from './reconciler'
5
+
6
+ import type { TaroElement, TaroEvent } from '@tarojs/runtime'
7
+ import type { Fiber } from 'react-reconciler'
8
+ import type { Props } from './props'
9
+
10
+ export type RestoreType = string | number | boolean | any[]
11
+
12
+ interface RestoreItem {
13
+ target: TaroElement
14
+ value: RestoreType
15
+ }
16
+
17
+ let restoreQueue: RestoreItem[] | null = null
18
+
19
+ // 对比 TaroElement tracker 下的 value 和事件下的 value,判断 element 的值是否存在更改
20
+ export function getTargetInstForInputOrChangeEvent (e: TaroEvent, node: TaroElement) {
21
+ const targetInst = getInstanceFromNode(node)
22
+ const domEventName = e.type
23
+
24
+ if (!targetInst || !isTextInputElement(node)) return
25
+
26
+ if (domEventName === 'input' || domEventName === 'change') {
27
+ const nextValue = toString(e.mpEvent?.detail?.value)
28
+
29
+ return getInstIfValueChanged(targetInst, nextValue)
30
+ }
31
+ }
32
+
33
+ function getInstIfValueChanged (targetInst: Fiber, nextValue: string) {
34
+ const targetNode = getNodeFromInstance(targetInst)
35
+
36
+ if (!targetNode) return false
37
+
38
+ if (updateValueIfChanged(targetNode, nextValue)) {
39
+ return targetInst
40
+ }
41
+ }
42
+
43
+ // 把 target 塞入更新队列中
44
+ export function enqueueStateRestore (target: RestoreItem): void {
45
+ if (restoreQueue) {
46
+ restoreQueue.push(target)
47
+ } else {
48
+ restoreQueue = [target]
49
+ }
50
+ }
51
+
52
+ // 判断是否需要恢复 target(input、textarea) 的状态
53
+ export function needsStateRestore (): boolean {
54
+ return restoreQueue !== null
55
+ }
56
+
57
+ export function finishEventHandler () {
58
+ const controlledComponentsHavePendingUpdates = needsStateRestore()
59
+
60
+ if (controlledComponentsHavePendingUpdates) {
61
+ TaroReconciler.flushSync()
62
+ restoreStateIfNeeded()
63
+ }
64
+ }
65
+
66
+ // 遍历 restoreQueue、restoreTarget,恢复其状态
67
+ export function restoreStateIfNeeded () {
68
+ if (!restoreQueue) {
69
+ return
70
+ }
71
+
72
+ const queuedTargets = restoreQueue
73
+ restoreQueue = null
74
+
75
+ for (let i = 0; i < queuedTargets.length; i++) {
76
+ restoreStateOfTarget(queuedTargets[i])
77
+ }
78
+ }
79
+
80
+ function restoreImpl (
81
+ domElement: TaroElement,
82
+ tag: string,
83
+ oldValue: string | number | boolean | any[],
84
+ props: Props,
85
+ ): void {
86
+ switch (tag) {
87
+ case 'input':
88
+ ReactDOMInputRestoreControlledState(domElement, oldValue, props)
89
+ break
90
+ case 'textarea':
91
+ ReactDOMTextareaRestoreControlledState(domElement, oldValue, props)
92
+ break
93
+ }
94
+ }
95
+
96
+ function restoreStateOfTarget (item: RestoreItem) {
97
+ const internalInstance = getInstanceFromNode(item.target)
98
+
99
+ if (!internalInstance) return
100
+
101
+ const { stateNode, type } = internalInstance
102
+
103
+ if (stateNode) {
104
+ const props = getFiberCurrentPropsFromNode(stateNode)
105
+
106
+ restoreImpl(stateNode, type, item.value, props)
107
+ }
108
+ }