@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,11 +1,9 @@
1
- import type { TaroElement } from './element'
2
-
3
1
  export class ClassList {
4
- private el: TaroElement
2
+ private el: any
5
3
 
6
4
  private tokenList: string[] = []
7
5
 
8
- constructor (className: string, el: TaroElement) {
6
+ constructor (className: string, el: any) {
9
7
  this.el = el
10
8
  className.trim().split(/\s+/).forEach(token => this.tokenList.push(token))
11
9
  }
@@ -97,12 +95,12 @@ export class ClassList {
97
95
 
98
96
  if (~index) {
99
97
  this.tokenList.splice(index, 1, replacement_token)
100
- this._update()
98
+ this._update()
101
99
  }
102
100
  }
103
101
 
104
102
  toString () {
105
- return this.tokenList.filter(v => v !== '').join(' ')
103
+ return this.tokenList.filter(v => v !== '').join(' ')
106
104
  }
107
105
 
108
106
  private checkTokenIsValid (token: string) {
@@ -1,5 +1,4 @@
1
- import { NodeType } from './node'
2
- import { TaroTextNode } from './text'
1
+ import { NodeType, TaroTextNode } from './node'
3
2
 
4
3
  class TaroComment extends TaroTextNode {
5
4
  constructor(data: string) {
@@ -0,0 +1,409 @@
1
+ import { parseClasses } from '../utils'
2
+
3
+ import type { CSSProperties, ReactElement } from 'react'
4
+
5
+ // 抽出来的嵌套查询
6
+ // const __nesting_style__ = [
7
+ // {
8
+ // selectors: ['container', '>', 'hello', ' ', 'txt'],
9
+ // declaration: {
10
+ // _color: "#00F00F",
11
+ // _fontSize: convertNumber2VP(40),
12
+ // }
13
+ // },
14
+ // ]
15
+ //
16
+ // function Index() {
17
+ // return __combine_nesting_style__(jsx(TaroViewTagName, {
18
+ // className: "container aaa",
19
+ // style: calcDynamicStyle(__inner_style__(), "container", null),
20
+ // children: [
21
+ // jsx(TaroTextTagName, {
22
+ // className: "txt cc",
23
+ // style: calcDynamicStyle(__inner_style__(), "txt", null),
24
+ // children: "Hello!"
25
+ // }),
26
+ // jsx(TaroViewTagName, {
27
+ // className: "hello",
28
+ // style: calcDynamicStyle(__inner_style__(), "hello", null),
29
+ // children: [
30
+ // jsx(TaroTextTagName, {
31
+ // className: "txt",
32
+ // style: calcDynamicStyle(__inner_style__(), "txt", null),
33
+ // children: "wo2rld!"
34
+ // }),
35
+ // jsx(TaroTextTagName, {
36
+ // className: "txt2",
37
+ // style: calcDynamicStyle(__inner_style__(), "txt2", null),
38
+ // children: "Hello wo2rld!"
39
+ // })
40
+ // ]
41
+ // })
42
+ // ]
43
+ // }), __nesting_style__);
44
+ // }
45
+
46
+ type TMappingNode = {
47
+ node: ReactElement
48
+ children: TMapping
49
+ descendants: TMapping
50
+ }
51
+
52
+ type TMapping = Record<string, TMappingNode>
53
+
54
+ type TSelectorNode = {
55
+ mapping: TMapping
56
+ node: ReactElement
57
+ }
58
+
59
+ export type NestingStyle = {
60
+ node: ReactElement
61
+ selectors: string[]
62
+ declaration: Record<string, any>
63
+ }[]
64
+
65
+ // 构建映射表
66
+ function depthTraversal(root: ReactElement) {
67
+ const class_mapping: TMapping = {}
68
+ // 记录别名,防止冲突
69
+ const selector_alias: Record<string, string[]> = {}
70
+ // 统计重名次数:{ txt: 1, cc: 2 }
71
+ const selector_alias_count: Record<string, number> = {}
72
+
73
+ const traverse = (tree) => {
74
+ const result: Record<string, TMapping> = {}
75
+ if (tree && tree.props) {
76
+ // 兜底适配:如果Taro组件被原封不动的再别的地方导出使用,导致无法在编译环境添加__hmStyle
77
+ // import { View } from '~/components'
78
+ // hack:如果是taro节点,但是被赋予了__styleSheet,则走一下__styleSheet转__hmStyle
79
+ if (tree.props.__styleSheet && typeof tree.type !== 'function') {
80
+ tree.props.__hmStyle = Object.assign({}, tree.props.__hmStyle, tree.props.__styleSheet.value)
81
+ }
82
+
83
+ // 后代选择器
84
+ const descendant_map = {
85
+ node: tree,
86
+ children: {},
87
+ descendants: {}
88
+ }
89
+
90
+ if (tree.props.children) {
91
+ // 遍历叶子节点
92
+ if (tree.props.children instanceof Array) {
93
+ for (let i = 0; i < tree.props.children.length; i++) {
94
+ // 收集叶子节点所拥有的类名
95
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
96
+ processLeaf(tree.props.children[i], descendant_map)
97
+ }
98
+ } else if (typeof tree.props.children !== 'string') {
99
+ // 收集叶子节点所拥有的类名
100
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
101
+ processLeaf(tree.props.children, descendant_map)
102
+ }
103
+ }
104
+ // 拆分classnames
105
+ const classnames = parseClasses(tree.props.className || '')
106
+ for (let i = 0; i < classnames.length; i++) {
107
+ const cls = classnames[i]
108
+ let name = cls
109
+ if (selector_alias_count[name]) {
110
+ // 存在重名,需要使用别名替代
111
+ const oldName = name
112
+ name = `___${name}${selector_alias_count[oldName]}`
113
+ selector_alias_count[oldName] = selector_alias_count[oldName] + 1
114
+ selector_alias[oldName] ? selector_alias[oldName].push(name) : (selector_alias[oldName] = [name])
115
+ }
116
+ selector_alias_count[name] = 1
117
+
118
+ class_mapping[name] = descendant_map
119
+ result[name] = {
120
+ ref: descendant_map,
121
+ node: tree
122
+ }
123
+ }
124
+ }
125
+ return result
126
+ }
127
+
128
+ const processLeaf = (leaf, descendant_map: TMappingNode) => {
129
+ if (!leaf) return
130
+
131
+ // 如果是个数组
132
+ if (leaf instanceof Array) {
133
+ for (let i = 0; i < leaf.length; i++) {
134
+ processLeaf(leaf[i], descendant_map)
135
+ }
136
+ }
137
+
138
+ const leaf_map = traverse(leaf)
139
+ if (!leaf_map) return
140
+
141
+ // 直接后代
142
+ Object.assign(descendant_map.children, leaf_map)
143
+
144
+ // 子孙后代
145
+ const grandchild: (Record<string, TMapping> | TMapping)[] = [leaf_map]
146
+ Object.keys(leaf_map).forEach(key => {
147
+ const leaf_child_map = class_mapping[key]
148
+ if (leaf_child_map?.descendants) {
149
+ grandchild.push(leaf_child_map.descendants)
150
+ }
151
+ })
152
+ Object.assign(descendant_map.descendants, ...grandchild)
153
+ }
154
+
155
+ traverse(root)
156
+ return {
157
+ mapping: class_mapping,
158
+ alias: selector_alias
159
+ }
160
+ }
161
+
162
+ // 将嵌套样式与节点合并
163
+ function combineStyle(nestingStyle: NestingStyle, class_mapping: TMapping, alias: Record<string, string[]>) {
164
+ const findElement = (selector_string, combinator_type, selector_mapping, remainder_selector, declaration) => {
165
+ // 防止修改原数组
166
+ if (selector_string instanceof Array) {
167
+ selector_string = selector_string.slice()
168
+ }
169
+ let selector_list = [selector_string]
170
+ const selector_nodes: TSelectorNode[] = []
171
+ let shouldUseCombinations = false
172
+ // 判断是否存在别名
173
+ if (selector_string instanceof Array) {
174
+ // 多类选择器类名,需要进行排列组合选择了,一对多的查找 ['a', 'b'],需要找出所有a和b都指向同一个node的节点
175
+ selector_string.forEach((item, i) => {
176
+ if (alias[item]) {
177
+ selector_string[i] = [item, ...alias[item]]
178
+ }
179
+ })
180
+ shouldUseCombinations = true
181
+ } else if (alias[selector_string]) {
182
+ selector_list = selector_list.concat(alias[selector_string])
183
+ }
184
+
185
+ for (let i = 0; i < selector_list.length; i++) {
186
+ const selector = selector_list[i]
187
+ if (selector instanceof Array) {
188
+ let hitElements: any
189
+ // 如果是数组,说明他是一个多类选择器:.a.b,我们需要搜索这两个类名都指向同一个node
190
+ if (shouldUseCombinations) {
191
+ hitElements = generateCombinations(selector, (combination) => {
192
+ // combination 是组合后的选择器['parent', 'child']
193
+ const _obj = findSendNode(combination, selector_mapping)
194
+ if (_obj) return _obj
195
+ })
196
+ } else {
197
+ hitElements = findSendNode(selector, selector_mapping)
198
+ }
199
+ // 找出最后搜寻出来的node
200
+ if (hitElements) {
201
+ let objs = [hitElements]
202
+ objs = flattenArray(objs)
203
+ objs.forEach(obj => {
204
+ if (typeof obj.node.type === 'function') {
205
+ // 自定义组件,往下传递需要搜寻的内容向里面搜寻
206
+ const nestingData = {
207
+ selectors: [selector_string, combinator_type, ...remainder_selector.slice()],
208
+ declaration: declaration
209
+ }
210
+ obj.node.props.__nesting = obj.node.props.__nesting
211
+ ? [...obj.node.props.__nesting, nestingData] : [nestingData]
212
+ }
213
+ selector_nodes.push({
214
+ // @ts-ignore
215
+ mapping: (obj.ref || obj)[combinator_type === ' > ' ? 'children' : 'descendants'],
216
+ node: obj.node
217
+ })
218
+ })
219
+ }
220
+ } else {
221
+ // 查询选择器的节点
222
+ const object = selector_mapping[selector]
223
+ if (object) {
224
+ if (typeof object.node.type === 'function') {
225
+ // 自定义组件,往下传递需要搜寻的内容向里面搜寻
226
+ const nestingData = {
227
+ selectors: [selector_string, combinator_type, ...remainder_selector.slice()],
228
+ declaration: declaration
229
+ }
230
+ object.node.props.__nesting = object.node.props.__nesting
231
+ ? [...object.node.props.__nesting, nestingData] : [nestingData]
232
+ }
233
+ selector_nodes.push({
234
+ mapping: (object.ref || object)[combinator_type === ' > ' ? 'children' : 'descendants'],
235
+ node: object.node
236
+ })
237
+ }
238
+ }
239
+ }
240
+ return selector_nodes
241
+ }
242
+ const findSelector = (selectors, current_mapping, declaration): TSelectorNode[] => {
243
+ const new_selectors = selectors.slice()
244
+ const selector_string = new_selectors.shift()
245
+ const combinator_type = new_selectors.shift()
246
+ const _elements = findElement(selector_string, combinator_type, current_mapping, new_selectors, declaration)
247
+ if (_elements.length) {
248
+ if (new_selectors.length) {
249
+ let elements: TSelectorNode[] = []
250
+ _elements.forEach(element => {
251
+ elements = elements.concat(findSelector(new_selectors.slice(), element.mapping, declaration))
252
+ })
253
+ return elements
254
+ } else {
255
+ return _elements
256
+ }
257
+ } else {
258
+ return []
259
+ }
260
+ }
261
+ if (nestingStyle && nestingStyle instanceof Array) {
262
+ // 合并样式
263
+ nestingStyle.forEach(({ selectors, declaration }) => {
264
+ // 获取选中的节点列表
265
+ const selectors_elements = findSelector(selectors, class_mapping, declaration)
266
+ for (let i = 0; i < selectors_elements.length; i++) {
267
+ const ele = selectors_elements[i].node
268
+ if (ele && typeof ele !== 'function') {
269
+ // 直接注入到__hmStyle
270
+ // Hack: ReactElement会在dev模式下被冻结,所以在dev模式下,我们会将Object.freeze覆盖使其失效
271
+ if (ele.props.__hmStyle) {
272
+ ele.props.__hmStyle = Object.assign({}, ele.props.__hmStyle, declaration)
273
+ } else {
274
+ ele.props.__hmStyle = declaration
275
+ }
276
+ }
277
+ }
278
+ })
279
+ }
280
+ }
281
+
282
+ // 排列组合选择器,找出符合的组合
283
+ // let arrayA = [1, 2, 3];
284
+ // let arrayB = [4, 5, 6];
285
+ // let arrayC = 'abc'
286
+ // let result = generateCombinations([arrayA, arrayB, arrayC], (combination) => { if (combination[0] == 3 ) { return combination }})
287
+ // console.log(result) => [3, 4, "abc"]
288
+ function generateCombinations (arrays: (string[] | string)[], cbFn, currentCombination: (string[] | string)[] = []) {
289
+ if (arrays.length === 0) {
290
+ // 当所有数组都被处理完时,将当前组合添加到结果中
291
+ return cbFn(currentCombination.slice())
292
+ }
293
+ // 取出当前数组
294
+ const currentArray = arrays[0]
295
+ if (currentArray instanceof Array) {
296
+ // 遍历当前数组的每个元素
297
+ const eles: TMappingNode[] = []
298
+ for (let i = 0; i < currentArray.length; i++) {
299
+ // 将当前元素添加到当前组合中
300
+ currentCombination.push(currentArray[i])
301
+ // 递归处理剩余的数组
302
+ const shouldStop = generateCombinations(arrays.slice(1), cbFn, currentCombination)
303
+ if (shouldStop) {
304
+ eles.push(shouldStop)
305
+ }
306
+ // 回溯,移除最后一个元素,尝试其他组合
307
+ currentCombination.pop()
308
+ }
309
+ return eles
310
+ } else {
311
+ // 如果不是数组,直接将当前元素添加到当前组合中
312
+ currentCombination.push(currentArray)
313
+ // 递归处理剩余的数组
314
+ const shouldStop = generateCombinations(arrays.slice(1), cbFn, currentCombination)
315
+ if (shouldStop) {
316
+ return shouldStop
317
+ }
318
+ // 回溯,移除最后一个元素,尝试其他组合
319
+ currentCombination.pop()
320
+ }
321
+ return false
322
+ }
323
+
324
+ // 多类选择器辅助函数:.a.b => 这里的.a .b其实都是指向同一个node
325
+ // 匹配寻找所有选择器都指向同一个node
326
+ function findSendNode (selectorArr: string[], selector_mapping: TMapping) {
327
+ let obj: any = null
328
+ for (let i = 0; i < selectorArr.length; i++) {
329
+ // 查询选择器的节点
330
+ const object = selector_mapping[selectorArr[i]]
331
+ if (object) {
332
+ if (!obj) { obj = object }
333
+ if (object.node === obj.node) continue
334
+ }
335
+ obj = null
336
+ break
337
+ }
338
+ return obj
339
+ }
340
+
341
+ function insertParentStyle(data: {key: string, value: CSSProperties}, class_mapping: TMapping, alias: Record<string, string[]>) {
342
+ const { key, value } = data
343
+ key.split(' ').forEach(key => {
344
+ let classnames = [key]
345
+ if (alias[key]) {
346
+ classnames = classnames.concat(alias[key])
347
+ }
348
+ classnames.forEach(name => {
349
+ if (class_mapping[name]) {
350
+ // 插入样式
351
+ const ele = class_mapping[key].node
352
+ if (ele) {
353
+ // 直接注入到__hmStyle
354
+ // Hack: ReactElement会在dev模式下被冻结,所以在dev模式下,我们会将Object.freeze覆盖使其失效
355
+ if (ele.props.__hmStyle) {
356
+ ele.props.__hmStyle = Object.assign({}, ele.props.__hmStyle, value)
357
+ } else {
358
+ ele.props.__hmStyle = value
359
+ }
360
+ }
361
+ }
362
+ })
363
+ })
364
+ }
365
+
366
+ // 合并嵌套样式
367
+ // 1、构建映射表,生成一份扁平的样式表结构
368
+ // 2、遍历嵌套样式,根据选择器查找节点,合并样式
369
+ export function __combine_nesting_style__(react_tree: ReactElement, styles: NestingStyle | null = null) {
370
+ if (react_tree) {
371
+ let newStyle = styles
372
+ const parentProps: {
373
+ __styleSheet?: {key: string, value: CSSProperties}
374
+ __nesting?: NestingStyle
375
+ } = // @ts-ignore
376
+ react_tree._owner?.pendingProps // Hack:取出父组件的props,考虑版本问题,取的内部属性,可能会有问题
377
+
378
+ if (newStyle || (parentProps && (parentProps.__styleSheet || parentProps.__nesting))) {
379
+ // 1、循环一遍,构建出一颗JSX映射表
380
+ const { mapping, alias } = depthTraversal(react_tree)
381
+ if (parentProps) {
382
+ // 2、如果自定义节点,则增加合成__stylesheet步骤,将父节点的样式合并到当前节点
383
+ if (parentProps.__styleSheet) {
384
+ insertParentStyle(parentProps.__styleSheet, mapping, alias)
385
+ // @ts-ignore
386
+ parentProps.__styleSheet = null // 释放内存
387
+ }
388
+ // 3、如果自定节点,存在需要往下查询的嵌套样式,需要合并到子组件
389
+ if (parentProps.__nesting) {
390
+ newStyle = (newStyle || []).concat(parentProps.__nesting)
391
+ // @ts-ignore
392
+ parentProps.__nesting = null // 释放内存
393
+ }
394
+ }
395
+ // 4、合并嵌套样式样式
396
+ if (newStyle && newStyle.length) {
397
+ combineStyle(newStyle, mapping, alias)
398
+ }
399
+ }
400
+ }
401
+ return react_tree
402
+ }
403
+
404
+ // 拍平数组
405
+ function flattenArray(arr) {
406
+ return arr.reduce((acc, val) => {
407
+ return acc.concat(Array.isArray(val) ? flattenArray(val) : val)
408
+ }, [])
409
+ }
@@ -1,6 +1,7 @@
1
- import { isObject, toCamelCase, toDashed } from '@tarojs/shared'
1
+ import { isObject, isUndefined, toCamelCase, toDashed } from '@tarojs/shared'
2
2
 
3
- import { TaroElement } from './element'
3
+ import { TaroElement } from './element/element'
4
+ import convertWebStyle2HmStyle from './stylesheet/covertWeb2Hm'
4
5
 
5
6
  class CSSStyleDeclaration {
6
7
  // eslint-disable-next-line no-useless-constructor
@@ -22,9 +23,11 @@ class CSSStyleDeclaration {
22
23
  }
23
24
 
24
25
  public set cssText (value: string) {
26
+ // 复制,防止被篡改到attr上的原始内容
27
+ this._st.hmStyle = Object.assign({}, this.el._attrs?.__hmStyle) || {}
25
28
  if (value === '' || value === undefined || value === null) {
26
- // TODO:
27
- // this.el._st = {}
29
+ // 清空style,只保留class的样式
30
+ return
28
31
  }
29
32
 
30
33
  const rules = value.split(';')
@@ -39,7 +42,7 @@ class CSSStyleDeclaration {
39
42
  const [propName, ...valList] = rule.split(':')
40
43
  const val = valList.join(':')
41
44
 
42
- if (typeof val === undefined) {
45
+ if (typeof val === 'undefined') {
43
46
  continue
44
47
  }
45
48
  this.setProperty(propName.trim(), val.trim())
@@ -47,42 +50,49 @@ class CSSStyleDeclaration {
47
50
  }
48
51
 
49
52
  public setProperty (prop: string, value: any): void {
50
- const node = this.el
51
53
  prop = prop.includes('-') ? toCamelCase(prop) : prop
54
+ const node = this.el
52
55
  if ((typeof value === 'string' && value.length) || typeof value === 'number' || isObject(value)) {
53
- node._st[prop] = value
56
+ convertWebStyle2HmStyle({ [prop]: value }, node)
54
57
  } else if (!value) {
55
- // '' | undefined | null
58
+ if (value === '') {
59
+ // value设置为null或者''了
60
+ this.removeProperty(prop)
61
+ return
62
+ }
63
+ // 判断class的样式表中是否存在该属性
64
+ // 如果存在,则style删除,无需把hmStyle的值删除
65
+ // 如果不存在,则hmStyle上该样式是style设置的,需要删除
66
+ if (isUndefined(this.el._attrs?.__hmStyle?.[prop])) {
67
+ if (this.el && this.el._st) {
68
+ this.el._st.hmStyle[prop] = this.el._attrs?.__hmStyle?.[prop]
69
+ }
70
+ return
71
+ }
56
72
  this.removeProperty(prop)
57
73
  }
58
74
  }
59
75
 
60
76
  public getPropertyValue (prop: string): string | number {
61
- const node = this.el
62
77
  prop = prop.includes('-') ? toCamelCase(prop) : prop
78
+ const node = this.el
63
79
  const value = node._st[prop]
64
80
  return value === undefined ? '' : value
65
81
  }
66
82
 
67
83
  public removeProperty (prop: string): string | number {
68
- const node = this.el
69
84
  prop = prop.includes('-') ? toCamelCase(prop) : prop
70
- const value = node._st[prop]
71
- if (value === undefined) {
72
- node._st[prop] = undefined
73
- return ''
74
- } else {
75
- // delete node._st[prop]
76
- node._st[prop] = undefined
77
- // node._st = { ...node._st }
78
- return value
79
- }
85
+ const node = this.el
86
+ const value = node._st.hmStyle[prop]
87
+ delete node._st.hmStyle[prop]
88
+ return value
80
89
  }
81
90
  }
82
91
 
83
92
  type ICSSStyleDeclaration = CSSStyleDeclaration & Record<string, any>
84
93
 
85
94
  function createCSSStyleDeclaration (node: TaroElement): CSSStyleDeclaration {
95
+ // FIXME 临时使用 Proxy 代理,后续需要优化
86
96
  return new Proxy(new CSSStyleDeclaration(node), {
87
97
  set (target, prop: string, value) {
88
98
  if (prop === 'cssText') {
@@ -0,0 +1,64 @@
1
+ import { TaroEventTarget } from '../dom/eventTarget'
2
+
3
+ export default class TaroDataSourceElement extends TaroEventTarget implements IDataSource {
4
+ protected listeners: DataChangeListener[] = []
5
+
6
+ totalCount(): number {
7
+ throw new Error('Method not implemented.')
8
+ }
9
+
10
+ getData(_: number) {
11
+ throw new Error('Method not implemented.')
12
+ }
13
+
14
+ // 该方法为框架侧调用,为 LazyForEach 组件向其数据源处添加 listener 监听
15
+ registerDataChangeListener(listener: DataChangeListener): void {
16
+ if (this.listeners.indexOf(listener) < 0) {
17
+ this.listeners.push(listener)
18
+ }
19
+ }
20
+
21
+ // 该方法为框架侧调用,为对应的 LazyForEach 组件在数据源处去除 listener 监听
22
+ unregisterDataChangeListener(listener: DataChangeListener): void {
23
+ const pos = this.listeners.indexOf(listener)
24
+
25
+ if (pos >= 0) {
26
+ this.listeners.splice(pos, 1)
27
+ }
28
+ }
29
+
30
+ // 通知 LazyForEach 组件需要重载所有子组件
31
+ notifyDataReload(): void {
32
+ this.listeners.forEach(listener => {
33
+ listener.onDataReloaded()
34
+ })
35
+ }
36
+
37
+ // 通知 LazyForEach 组件需要将 from 对应索引处的子组件移动到 to 索引处
38
+ notifyDataMove(from: number, to: number): void {
39
+ this.listeners.forEach(listener => {
40
+ listener.onDataMove(from, to)
41
+ })
42
+ }
43
+
44
+ // 通知 LazyForEach 组件需要在 index 对应索引处添加子组件
45
+ notifyDataAdd(index: number): void {
46
+ this.listeners.forEach(listener => {
47
+ listener.onDataAdd(index)
48
+ })
49
+ }
50
+
51
+ // 通知 LazyForEach 组件在 index 对应索引处数据有变化,需要重建该子组件
52
+ notifyDataChange(index: number): void {
53
+ this.listeners.forEach(listener => {
54
+ listener.onDataChange(index)
55
+ })
56
+ }
57
+
58
+ // 通知 LazyForEach 组件需要在 index 对应索引处删除该子组件
59
+ notifyDataDelete(index: number): void {
60
+ this.listeners.forEach(listener => {
61
+ listener.onDataDelete(index)
62
+ })
63
+ }
64
+ }