@tarojs/plugin-platform-harmony-ets 4.0.0-beta.2 → 4.0.0-beta.21

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 (87) hide show
  1. package/dist/apis/base/system.ts +53 -20
  2. package/dist/apis/framework/index.ts +1 -5
  3. package/dist/apis/index.ts +3 -1
  4. package/dist/apis/network/request.ts +5 -5
  5. package/dist/apis/route/index.ts +15 -0
  6. package/dist/apis/storage/index.ts +124 -60
  7. package/dist/apis/wxml/IntersectionObserver.ts +5 -3
  8. package/dist/apis/wxml/index.ts +2 -0
  9. package/dist/components-harmony-ets/button.ets +41 -78
  10. package/dist/components-harmony-ets/checkbox.ets +19 -209
  11. package/dist/components-harmony-ets/form.ets +38 -160
  12. package/dist/components-harmony-ets/icon.ets +31 -83
  13. package/dist/components-harmony-ets/image.ets +22 -78
  14. package/dist/components-harmony-ets/innerHtml.ets +11 -6
  15. package/dist/components-harmony-ets/input.ets +10 -67
  16. package/dist/components-harmony-ets/label.ets +52 -173
  17. package/dist/components-harmony-ets/movableArea.ets +89 -0
  18. package/dist/components-harmony-ets/movableView.ets +67 -0
  19. package/dist/components-harmony-ets/picker.ets +32 -147
  20. package/dist/components-harmony-ets/progress.ets +47 -0
  21. package/dist/components-harmony-ets/pseudo.ets +80 -0
  22. package/dist/components-harmony-ets/radio.ets +19 -210
  23. package/dist/components-harmony-ets/richText.ets +22 -102
  24. package/dist/components-harmony-ets/scrollView.ets +66 -169
  25. package/dist/components-harmony-ets/slider.ets +10 -72
  26. package/dist/components-harmony-ets/style.ets +244 -0
  27. package/dist/components-harmony-ets/swiper.ets +30 -87
  28. package/dist/components-harmony-ets/switch.ets +9 -71
  29. package/dist/components-harmony-ets/text.ets +38 -89
  30. package/dist/components-harmony-ets/textArea.ets +10 -67
  31. package/dist/components-harmony-ets/utils/AttributeManager.ets +1 -1
  32. package/dist/components-harmony-ets/utils/DynamicCenter.ts +1 -1
  33. package/dist/components-harmony-ets/utils/flexManager.ets +8 -7
  34. package/dist/components-harmony-ets/utils/helper.ets +5 -4
  35. package/dist/components-harmony-ets/utils/htmlParser/HarmonyHTMLParser.ts +1 -2
  36. package/dist/components-harmony-ets/utils/index.ts +55 -2
  37. package/dist/components-harmony-ets/utils/styles.ets +45 -85
  38. package/dist/components-harmony-ets/video.ets +33 -88
  39. package/dist/components-harmony-ets/view.ets +47 -160
  40. package/dist/components-harmony-ets/webView.ets +44 -99
  41. package/dist/index.d.ts +151 -0
  42. package/dist/index.js +58 -29
  43. package/dist/index.js.map +1 -1
  44. package/dist/runtime-ets/bom/window.ts +4 -2
  45. package/dist/runtime-ets/current.ts +2 -0
  46. package/dist/runtime-ets/dom/bind.ts +0 -1
  47. package/dist/runtime-ets/dom/cssNesting.ts +311 -0
  48. package/dist/runtime-ets/dom/cssStyleDeclaration.ts +15 -40
  49. package/dist/runtime-ets/dom/document.ts +21 -8
  50. package/dist/runtime-ets/dom/element/element.ts +53 -9
  51. package/dist/runtime-ets/dom/element/form.ts +11 -4
  52. package/dist/runtime-ets/dom/element/index.ts +12 -1
  53. package/dist/runtime-ets/dom/element/movableArea.ts +12 -0
  54. package/dist/runtime-ets/dom/element/movableView.ts +193 -0
  55. package/dist/runtime-ets/dom/element/normal.ts +8 -3
  56. package/dist/runtime-ets/dom/element/progress.ts +13 -0
  57. package/dist/runtime-ets/dom/element/scrollView.ts +1 -0
  58. package/dist/runtime-ets/dom/element/text.ts +1 -0
  59. package/dist/runtime-ets/dom/element/video.ts +1 -0
  60. package/dist/runtime-ets/dom/element/webView.ts +8 -0
  61. package/dist/runtime-ets/dom/event.ts +0 -1
  62. package/dist/runtime-ets/dom/eventTarget.ts +0 -3
  63. package/dist/runtime-ets/dom/node.ts +18 -17
  64. package/dist/runtime-ets/dom/stylesheet/covertWeb2Hm.ts +184 -207
  65. package/dist/runtime-ets/dom/stylesheet/index.ts +28 -308
  66. package/dist/runtime-ets/dom/stylesheet/type.ts +18 -6
  67. package/dist/runtime-ets/dom/stylesheet/util.ts +19 -15
  68. package/dist/runtime-ets/index.ts +2 -2
  69. package/dist/runtime-ets/interface/event.ts +1 -1
  70. package/dist/runtime-ets/utils/index.ts +24 -9
  71. package/dist/runtime-framework/react/app.ts +5 -1
  72. package/dist/runtime-framework/react/hooks.ts +3 -3
  73. package/dist/runtime-framework/react/native-page.ts +14 -9
  74. package/dist/runtime-framework/react/page.ts +1 -0
  75. package/dist/runtime-framework/solid/hooks.ts +3 -3
  76. package/dist/runtime-framework/solid/reconciler/use.ts +0 -1
  77. package/dist/runtime-framework/solid/utils/index.ts +0 -2
  78. package/dist/runtime-utils.d.ts +825 -0
  79. package/dist/runtime-utils.js +185 -91
  80. package/dist/runtime-utils.js.map +1 -1
  81. package/dist/runtime.d.ts +1 -0
  82. package/dist/runtime.js +185 -91
  83. package/dist/runtime.js.map +1 -1
  84. package/index.js +3 -1
  85. package/package.json +10 -10
  86. package/types/index.d.ts +4 -0
  87. package/dist/runtime-ets/utils/bind.ts +0 -24
@@ -0,0 +1,311 @@
1
+ // 抽出来的嵌套查询
2
+ // const __nesting_style__ = [
3
+ // {
4
+ // selectors: ['container', '>', 'hello', ' ', 'txt'],
5
+ // declaration: {
6
+ // _color: "#00F00F",
7
+ // _fontSize: convertNumber2VP(40),
8
+ // }
9
+ // },
10
+ // ]
11
+ //
12
+ // function Index() {
13
+ // return __combine_nesting_style__(jsx(TaroViewTagName, {
14
+ // className: "container aaa",
15
+ // style: calcDynamicStyle(__inner_style__(), "container", null),
16
+ // children: [
17
+ // jsx(TaroTextTagName, {
18
+ // className: "txt cc",
19
+ // style: calcDynamicStyle(__inner_style__(), "txt", null),
20
+ // children: "Hello!"
21
+ // }),
22
+ // jsx(TaroViewTagName, {
23
+ // className: "hello",
24
+ // style: calcDynamicStyle(__inner_style__(), "hello", null),
25
+ // children: [
26
+ // jsx(TaroTextTagName, {
27
+ // className: "txt",
28
+ // style: calcDynamicStyle(__inner_style__(), "txt", null),
29
+ // children: "wo2rld!"
30
+ // }),
31
+ // jsx(TaroTextTagName, {
32
+ // className: "txt2",
33
+ // style: calcDynamicStyle(__inner_style__(), "txt2", null),
34
+ // children: "Hello wo2rld!"
35
+ // })
36
+ // ]
37
+ // })
38
+ // ]
39
+ // }), __nesting_style__);
40
+ // }
41
+
42
+ import type { ReactElement } from 'react'
43
+
44
+ type TMappingNode = {
45
+ node: ReactElement
46
+ children: TMapping
47
+ descendants: TMapping
48
+ }
49
+
50
+ type TMapping = Record<string, TMappingNode>
51
+
52
+ type TSelectorNode = {
53
+ mapping: TMapping
54
+ node: ReactElement
55
+ }
56
+
57
+ export type NestingStyle = {
58
+ node: ReactElement
59
+ selectors: string[]
60
+ declaration: Record<string, any>
61
+ }[]
62
+
63
+ // 构建映射表
64
+ function depthTraversal(root: ReactElement) {
65
+ const class_mapping: TMapping = {}
66
+ // 记录别名,防止冲突
67
+ const selector_alias: Record<string, string[]> = {}
68
+ // 统计重名次数:{ txt: 1, cc: 2 }
69
+ const selector_alias_count: Record<string, number> = {}
70
+
71
+ const traverse = (tree) => {
72
+ const result: Record<string, TMapping> = {}
73
+ if (tree && tree.props) {
74
+ // 后代选择器
75
+ const descendant_map = {
76
+ node: tree,
77
+ children: {},
78
+ descendants: {}
79
+ }
80
+
81
+ if (tree.props.children) {
82
+ // 遍历叶子节点
83
+ if (tree.props.children instanceof Array) {
84
+ for (let i = 0; i < tree.props.children.length; i++) {
85
+ // 收集叶子节点所拥有的类名
86
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
87
+ processLeaf(tree.props.children[i], descendant_map)
88
+ }
89
+ } else if (typeof tree.props.children !== 'string'){
90
+ // 收集叶子节点所拥有的类名
91
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
92
+ processLeaf(tree.props.children, descendant_map)
93
+ }
94
+ }
95
+ // 拆分classnames
96
+ const classnames = (tree.props.className || '').split(' ')
97
+ for (let i = 0; i < classnames.length; i++) {
98
+ const cls = classnames[i]
99
+ let name = cls
100
+ if (selector_alias_count[name]) {
101
+ // 存在重名,需要使用别名替代
102
+ const oldName = name
103
+ name = `___${name}${selector_alias_count[oldName]}`
104
+ selector_alias_count[oldName] = selector_alias_count[oldName] + 1
105
+ selector_alias[oldName] ? selector_alias[oldName].push(name) : (selector_alias[oldName] = [name])
106
+ }
107
+ selector_alias_count[name] = 1
108
+
109
+ class_mapping[name] = descendant_map
110
+ result[name] = {
111
+ ref: descendant_map,
112
+ node: tree
113
+ }
114
+ }
115
+ }
116
+ return result
117
+ }
118
+
119
+ const processLeaf = (leaf, descendant_map: TMappingNode) => {
120
+ if (!leaf) return
121
+
122
+ // 如果是个数组
123
+ if (leaf instanceof Array) {
124
+ for (let i = 0; i < leaf.length; i++) {
125
+ processLeaf(leaf[i], descendant_map)
126
+ }
127
+ }
128
+
129
+ const leaf_map = traverse(leaf)
130
+ if (!leaf_map) return
131
+ // 直接后代
132
+ Object.assign(descendant_map.children, leaf_map)
133
+ // 子孙后代
134
+ Object.assign(descendant_map.descendants, leaf_map)
135
+ const keys = Object.keys(leaf_map)
136
+ for (let i = 0; i < keys.length; i++) {
137
+ const leaf_child_map = class_mapping[keys[i]]
138
+ if (leaf_child_map) {
139
+ Object.assign(descendant_map.descendants, leaf_child_map.descendants)
140
+ }
141
+ }
142
+ }
143
+
144
+ traverse(root)
145
+ return {
146
+ mapping: class_mapping,
147
+ alias: selector_alias
148
+ }
149
+ }
150
+
151
+ // 将嵌套样式与节点合并
152
+ function combineStyle(nestingStyle: NestingStyle, class_mapping: TMapping, alias: Record<string, string[]>) {
153
+
154
+ const findElement = (selector_string, combinator_type, selector_mapping) => {
155
+ let selector_list = [selector_string]
156
+ const selector_nodes: TSelectorNode[] = []
157
+ let shouldUseCombinations = false
158
+ // 判断是否存在别名
159
+ if (selector_string instanceof Array) {
160
+ // 多类选择器类名,需要进行排列组合选择了,一对多的查找 ['a', 'b'],需要找出所有a和b都指向同一个node的节点
161
+ selector_string.forEach((item, i) => {
162
+ if (alias[item]) {
163
+ selector_string[i] = [item, ...alias[item]]
164
+ }
165
+ })
166
+ shouldUseCombinations = true
167
+ } else if (alias[selector_string]) {
168
+ selector_list = selector_list.concat(alias[selector_string])
169
+ }
170
+
171
+ for (let i = 0; i < selector_list.length; i++) {
172
+ const selector = selector_list[i]
173
+ if (selector instanceof Array) {
174
+ let obj
175
+ // 如果是数组,说明他是一个多类选择器:.a.b,我们需要搜索这两个类名都指向同一个node
176
+ if (shouldUseCombinations) {
177
+ obj = generateCombinations(selector, (combination) => {
178
+ // combination 是组合后的选择器['parent', 'child']
179
+ const _obj = findSendNode(combination, selector_mapping)
180
+ if (_obj) return _obj
181
+ })
182
+ } else {
183
+ obj = findSendNode(selector, selector_mapping)
184
+ }
185
+ // 找出最后搜寻出来的node
186
+ if (obj) {
187
+ selector_nodes.push({
188
+ mapping: (obj.ref || obj)[combinator_type === ' > ' ? 'children' : 'descendants'],
189
+ node: obj.node
190
+ })
191
+ }
192
+ } else {
193
+ // 查询选择器的节点
194
+ const object = selector_mapping[selector]
195
+ if (object) {
196
+ selector_nodes.push({
197
+ mapping: (object.ref || object)[combinator_type === ' > ' ? 'children' : 'descendants'],
198
+ node: object.node
199
+ })
200
+ }
201
+ }
202
+ }
203
+ return selector_nodes
204
+ }
205
+ const findSelector = (selectors, current_mapping): TSelectorNode[] => {
206
+ const new_selectors = selectors.slice()
207
+ const selector_string = new_selectors.shift()
208
+ const combinator_type = new_selectors.shift()
209
+ const _elements = findElement(selector_string, combinator_type, current_mapping)
210
+ if (_elements.length) {
211
+ if (new_selectors.length) {
212
+ let elements: TSelectorNode[] = []
213
+ _elements.forEach(element => {
214
+ elements = elements.concat(findSelector(new_selectors.slice(), element.mapping))
215
+ })
216
+ return elements
217
+ } else {
218
+ return _elements
219
+ }
220
+ } else {
221
+ return []
222
+ }
223
+ }
224
+ if (nestingStyle && nestingStyle instanceof Array) {
225
+ // 合并样式
226
+ nestingStyle.forEach(({ selectors, declaration }) => {
227
+ // 获取选中的节点列表
228
+ const selectors_elements = findSelector(selectors, class_mapping)
229
+ for (let i = 0; i < selectors_elements.length; i++) {
230
+ const ele = selectors_elements[i].node
231
+ if (ele) {
232
+ // 直接注入到__hmStyle
233
+ // Hack: ReactElement会在dev模式下被冻结,所以在dev模式下,我们会将Object.freeze覆盖使其失效
234
+ if (ele.props.__hmStyle) {
235
+ ele.props.__hmStyle = Object.assign({}, ele.props.__hmStyle, declaration)
236
+ } else {
237
+ ele.props.__hmStyle = declaration
238
+ }
239
+ }
240
+ }
241
+ })
242
+ }
243
+ }
244
+
245
+ // 排列组合选择器,找出符合的组合
246
+ // let arrayA = [1, 2, 3];
247
+ // let arrayB = [4, 5, 6];
248
+ // let arrayC = 'abc'
249
+ // let result = generateCombinations([arrayA, arrayB, arrayC], (combination) => { if (combination[0] == 3 ) { return combination }})
250
+ // console.log(result) => [3, 4, "abc"]
251
+ function generateCombinations (arrays: (string[] | string)[], cbFn, currentCombination: (string[] | string)[] = []) {
252
+ if (arrays.length === 0) {
253
+ // 当所有数组都被处理完时,将当前组合添加到结果中
254
+ return cbFn(currentCombination.slice())
255
+ }
256
+ // 取出当前数组
257
+ const currentArray = arrays[0]
258
+ if (currentArray instanceof Array) {
259
+ // 遍历当前数组的每个元素
260
+ for (let i = 0; i < currentArray.length; i++) {
261
+ // 将当前元素添加到当前组合中
262
+ currentCombination.push(currentArray[i])
263
+ // 递归处理剩余的数组
264
+ const shouldStop = generateCombinations(arrays.slice(1), cbFn, currentCombination)
265
+ if (shouldStop) {
266
+ return shouldStop
267
+ }
268
+ // 回溯,移除最后一个元素,尝试其他组合
269
+ currentCombination.pop()
270
+ }
271
+ } else {
272
+ // 如果不是数组,直接将当前元素添加到当前组合中
273
+ currentCombination.push(currentArray)
274
+ // 递归处理剩余的数组
275
+ const shouldStop = generateCombinations(arrays.slice(1), cbFn, currentCombination)
276
+ if (shouldStop) {
277
+ return shouldStop
278
+ }
279
+ // 回溯,移除最后一个元素,尝试其他组合
280
+ currentCombination.pop()
281
+ }
282
+ return false
283
+ }
284
+
285
+ // 多类选择器辅助函数:.a.b => 这里的.a .b其实都是指向同一个node
286
+ // 匹配寻找所有选择器都指向同一个node
287
+ function findSendNode (selectorArr: string[], selector_mapping: TMapping) {
288
+ let obj: any = null
289
+ for (let i = 0; i < selectorArr.length; i++) {
290
+ // 查询选择器的节点
291
+ const object = selector_mapping[selectorArr[i]]
292
+ if (object) {
293
+ if (!obj) { obj = object }
294
+ if (object.node === obj.node) continue
295
+ }
296
+ obj = null
297
+ break
298
+ }
299
+ return obj
300
+ }
301
+
302
+ // 合并嵌套样式
303
+ // 1、构建映射表,生成一份扁平的样式表结构
304
+ // 2、遍历嵌套样式,根据选择器查找节点,合并样式
305
+ export function __combine_nesting_style__(react_tree: ReactElement, styles: NestingStyle) {
306
+ // 循环一遍,构建出一颗JSX映射表
307
+ const { mapping, alias } = depthTraversal(react_tree)
308
+ // 合并样式
309
+ combineStyle(styles, mapping, alias)
310
+ return react_tree
311
+ }
@@ -1,7 +1,7 @@
1
1
  import { isObject, toCamelCase, toDashed } from '@tarojs/shared'
2
2
 
3
3
  import { TaroElement } from './element/element'
4
- import convertWebStyle2HmStyle, { WEB_STYLE_MAP } from './stylesheet/covertWeb2Hm'
4
+ import convertWebStyle2HmStyle from './stylesheet/covertWeb2Hm'
5
5
 
6
6
  class CSSStyleDeclaration {
7
7
  // eslint-disable-next-line no-useless-constructor
@@ -26,6 +26,8 @@ class CSSStyleDeclaration {
26
26
  if (value === '' || value === undefined || value === null) {
27
27
  // TODO: 清空 stylesheet 里面的 hmstyle
28
28
  // this.el._st = {}
29
+ this._st.hmStyle = {}
30
+ return
29
31
  }
30
32
 
31
33
  const rules = value.split(';')
@@ -48,59 +50,32 @@ class CSSStyleDeclaration {
48
50
  }
49
51
 
50
52
  public setProperty (prop: string, value: any): void {
51
- const node = this.el
52
-
53
53
  prop = prop.includes('-') ? toCamelCase(prop) : prop
54
-
55
- const webStyleList = WEB_STYLE_MAP[prop]
56
- const needUpdateProperty = !prop.startsWith('_') && webStyleList && webStyleList.length > 0
57
-
54
+ const node = this.el
58
55
  if ((typeof value === 'string' && value.length) || typeof value === 'number' || isObject(value)) {
59
- if (needUpdateProperty) {
60
- const newProperty = convertWebStyle2HmStyle({ [prop]: value })
61
-
62
- Object.keys(newProperty).forEach(key => {
63
- node._st[key] = newProperty[key]
64
- })
65
- } else {
66
- node._st[prop] = value
67
- }
56
+ // if (needUpdateProperty) {
57
+ const newProperty = convertWebStyle2HmStyle({ [prop]: value })
58
+ Object.keys(newProperty).forEach(key => {
59
+ node._st.hmStyle[key] = newProperty[key]
60
+ })
68
61
  } else if (!value) {
69
- // '' | undefined | null
70
- if (needUpdateProperty) {
71
- webStyleList.forEach(key => {
72
- this.removeProperty(key)
73
- })
74
- } else {
75
- this.removeProperty(prop)
76
- }
62
+ this.removeProperty(prop)
77
63
  }
78
64
  }
79
65
 
80
66
  public getPropertyValue (prop: string): string | number {
81
- const node = this.el
82
67
  prop = prop.includes('-') ? toCamelCase(prop) : prop
68
+ const node = this.el
83
69
  const value = node._st[prop]
84
70
  return value === undefined ? '' : value
85
71
  }
86
72
 
87
73
  public removeProperty (prop: string): string | number {
88
- const node = this.el
89
74
  prop = prop.includes('-') ? toCamelCase(prop) : prop
90
- const value = node._st[prop]
91
- if (prop.startsWith('_')) {
92
- prop = prop.replace('_', '')
93
- }
94
- if (value === undefined) {
95
- // node._st[prop] = undefined
96
- delete node._st.hmStyle[prop]
97
- return ''
98
- } else {
99
- delete node._st.hmStyle[prop]
100
- // node._st[prop] = undefined
101
- // node._st = { ...node._st }
102
- return value
103
- }
75
+ const node = this.el
76
+ const value = node._st.hmStyle[prop]
77
+ delete node._st.hmStyle[prop]
78
+ return value
104
79
  }
105
80
  }
106
81
 
@@ -2,11 +2,10 @@ import { eventSource } from '@tarojs/runtime/dist/runtime.esm'
2
2
  import { isUndefined } from '@tarojs/shared'
3
3
 
4
4
  import { Current } from '../current'
5
+ import { findChildNodeWithDFS, getPageScrollerOrNode } from '../utils'
5
6
  import { TaroComment } from './comment'
6
- import { createCSSStyleDeclaration } from './cssStyleDeclaration'
7
7
  import { TaroElement } from './element/element'
8
- import { NodeType, TaroNode } from './node'
9
- import { TaroTextNode } from './text'
8
+ import { NodeType, TaroNode, TaroTextNode } from './node'
10
9
 
11
10
  import type { Window } from '../bom/window'
12
11
 
@@ -45,8 +44,6 @@ class TaroDocument extends TaroNode {
45
44
  node = new TaroElement(tagName)
46
45
  }
47
46
  node._doc = this
48
- // Hack: 此 Proxy 不能放在 Element 类内定义,否则响应式更新会失效
49
- node._style = createCSSStyleDeclaration(node)
50
47
  return node
51
48
  }
52
49
 
@@ -63,16 +60,32 @@ class TaroDocument extends TaroNode {
63
60
  return new TaroComment(data)
64
61
  }
65
62
 
66
-
67
63
  public getElementById<T extends TaroElement> (id: string | undefined | null): T | null {
68
64
  const el = eventSource.get(id)
69
65
  return isUndefined(el) ? null : el as unknown as T
70
66
  }
71
67
 
68
+ public querySelector (selectors: string): TaroElement | null {
69
+ const taro = (Current as any).taro
70
+ const page = taro.getCurrentInstance().page
71
+ const element = getPageScrollerOrNode(page.node, page)
72
+
73
+ if (element == null) return null
74
+
75
+ return findChildNodeWithDFS(element, selectors)
76
+ }
77
+
78
+ public querySelectorAll (selectors: string): TaroElement[] {
79
+ const taro = (Current as any).taro
80
+ const page = taro.getCurrentInstance().page
81
+ const element = getPageScrollerOrNode(page.node, page)
82
+
83
+ if (element == null) return []
84
+
85
+ return findChildNodeWithDFS(element, selectors, true) || []
86
+ }
72
87
  // @Todo
73
88
  // public getElementsByClassName (names: string): TaroElement[]
74
- // public querySelector (selectors: string): TaroElement | null
75
- // public querySelectorAll (selectors: string): TaroElement[]
76
89
  // public createElementNS
77
90
  }
78
91
 
@@ -1,16 +1,17 @@
1
1
  import { eventSource } from '@tarojs/runtime/dist/runtime.esm'
2
+ import { EMPTY_OBJ, toCamelCase } from '@tarojs/shared'
2
3
 
3
4
  import { ATTRIBUTES_CALLBACK_TRIGGER_MAP, ID } from '../../constant'
4
5
  import { findChildNodeWithDFS } from '../../utils'
5
6
  import { initComponentNodeInfo, triggerAttributesCallback } from '../../utils/info'
6
7
  import { bindAnimation } from '../bind'
7
8
  import { ClassList } from '../class-list'
9
+ import { type ICSSStyleDeclaration, createCSSStyleDeclaration } from '../cssStyleDeclaration'
8
10
  import { NodeType, TaroNode } from '../node'
9
- import StyleSheet from '../stylesheet'
11
+ import StyleSheet, { HarmonyStyle } from '../stylesheet'
10
12
 
11
13
  import type { StandardProps } from '@tarojs/components/types'
12
14
  import type { TaroAny } from '../../utils'
13
- import type { ICSSStyleDeclaration } from '../cssStyleDeclaration'
14
15
 
15
16
  type NamedNodeMap = ({ name: string, value: string })[]
16
17
 
@@ -20,20 +21,17 @@ export interface TaroExtraProps {
20
21
  disabled?: boolean
21
22
  }
22
23
 
23
- @Observed
24
24
  export class TaroElement<T extends StandardProps = StandardProps> extends TaroNode {
25
25
  public _innerHTML = ''
26
26
  public _nodeInfo: TaroAny = {}
27
27
  public readonly tagName: string
28
+ public dataset: Record<string, unknown> = EMPTY_OBJ
28
29
  public _attrs: T & TaroExtraProps = {} as T & TaroExtraProps
29
30
 
30
- _client?: Area
31
- _scroll?: Area
32
-
33
31
  constructor(tagName: string) {
34
32
  super(tagName.replace(new RegExp('(?<=.)([A-Z])', 'g'), '-$1').toUpperCase(), NodeType.ELEMENT_NODE)
35
33
  this.tagName = this.nodeName
36
-
34
+ this._style = createCSSStyleDeclaration(this)
37
35
  initComponentNodeInfo(this)
38
36
  bindAnimation(this)
39
37
  }
@@ -75,6 +73,21 @@ export class TaroElement<T extends StandardProps = StandardProps> extends TaroNo
75
73
  }
76
74
 
77
75
  public setAttribute (name: string, value: TaroAny): void {
76
+ switch (name) {
77
+ case ID:
78
+ eventSource.delete(this._attrs.id)
79
+ eventSource.set(value, this as TaroAny)
80
+ break
81
+ default:
82
+ if (name.startsWith('data-')) {
83
+ if (this.dataset === EMPTY_OBJ) {
84
+ this.dataset = Object.create(null)
85
+ }
86
+ this.dataset[toCamelCase(name.replace(/^data-/, ''))] = value
87
+ }
88
+ break
89
+ }
90
+
78
91
  if (name === ID) {
79
92
  eventSource.delete(this._attrs.id)
80
93
  eventSource.set(value, this as TaroAny)
@@ -134,8 +147,6 @@ export class TaroElement<T extends StandardProps = StandardProps> extends TaroNo
134
147
  }, true) || []
135
148
  }
136
149
 
137
- // TODO dataset
138
-
139
150
  public set innerHTML (value: string) {
140
151
  if (this.nodeType === NodeType.ELEMENT_NODE && this.ownerDocument) {
141
152
  const ele = this.ownerDocument.createElement('inner-html')
@@ -148,6 +159,7 @@ export class TaroElement<T extends StandardProps = StandardProps> extends TaroNo
148
159
  return this._innerHTML
149
160
  }
150
161
 
162
+ // 存放的样式,获取其实跟获取style是一样的,只不过这里取的更快捷,不需要走style的get方法进到cssStyleDeclaration
151
163
  public _st = new StyleSheet()
152
164
 
153
165
  // 经转换后的鸿蒙样式
@@ -160,4 +172,36 @@ export class TaroElement<T extends StandardProps = StandardProps> extends TaroNo
160
172
  public get style (): ICSSStyleDeclaration | null {
161
173
  return this._style
162
174
  }
175
+
176
+ // 伪类,不存在style动态设置,均已被转换为鸿蒙样式
177
+ // 可根据实际情况,迁移到具体的组件中,如View、ScrollView中,Text\Image其实是不需要的
178
+ public _pseudo_before: StyleSheet | null = null
179
+
180
+ public set_pseudo_before (value: HarmonyStyle | null) {
181
+ if (value) {
182
+ if (!this._pseudo_before) {
183
+ this._pseudo_before = new StyleSheet()
184
+ }
185
+ Object.keys(value).forEach(key => {
186
+ this._pseudo_before!.hmStyle[key] = value[key]
187
+ })
188
+ } else {
189
+ this._pseudo_before = null
190
+ }
191
+ }
192
+
193
+ public _pseudo_after: StyleSheet | null = null
194
+
195
+ public set_pseudo_after (value: HarmonyStyle | null) {
196
+ if (value) {
197
+ if (!this._pseudo_after) {
198
+ this._pseudo_after = new StyleSheet()
199
+ }
200
+ Object.keys(value).forEach(key => {
201
+ this._pseudo_after!.hmStyle[key] = value[key]
202
+ })
203
+ } else {
204
+ this._pseudo_after = null
205
+ }
206
+ }
163
207
  }
@@ -134,6 +134,7 @@ class TaroCheckedElement<T extends StandardProps & { checked?: boolean } = Stand
134
134
  }
135
135
  }
136
136
 
137
+ @Observed
137
138
  class TaroInputElement<T extends FormWidgetProps = InputProps> extends TaroFormWidgetElement<T> {
138
139
  _height = 0
139
140
 
@@ -193,7 +194,7 @@ class TaroInputElement<T extends FormWidgetProps = InputProps> extends TaroFormW
193
194
  }
194
195
  }
195
196
 
196
-
197
+ @Observed
197
198
  class TaroTextAreaElement extends TaroInputElement<TextareaProps>{
198
199
  controller: TextAreaController = new TextAreaController()
199
200
 
@@ -201,13 +202,14 @@ class TaroTextAreaElement extends TaroInputElement<TextareaProps>{
201
202
  super('TextArea')
202
203
  }
203
204
  }
204
-
205
+ @Observed
205
206
  class TaroCheckboxElement extends TaroCheckedElement<CheckboxProps>{
206
207
  constructor() {
207
208
  super('Checkbox')
208
209
  }
209
210
  }
210
211
 
212
+ @Observed
211
213
  class TaroRadioElement extends TaroCheckedElement<RadioProps>{
212
214
  public group?: string
213
215
 
@@ -215,6 +217,8 @@ class TaroRadioElement extends TaroCheckedElement<RadioProps>{
215
217
  super('Radio')
216
218
  }
217
219
  }
220
+
221
+ @Observed
218
222
  class TaroSliderElement extends TaroFormWidgetElement<SliderProps> {
219
223
  _value = 0
220
224
 
@@ -224,7 +228,7 @@ class TaroSliderElement extends TaroFormWidgetElement<SliderProps> {
224
228
  this._value = Number(this._attrs.value || 0)
225
229
  }
226
230
  }
227
-
231
+ @Observed
228
232
  class TaroPickerElement extends TaroFormWidgetElement<PickerSelectorProps | PickerTimeProps | PickerDateProps | PickerMultiSelectorProps> {
229
233
  constructor() {
230
234
  super('Picker')
@@ -273,12 +277,13 @@ class TaroPickerElement extends TaroFormWidgetElement<PickerSelectorProps | Pick
273
277
  }
274
278
  }
275
279
 
280
+ @Observed
276
281
  class TaroSwitchElement extends TaroCheckedElement<SwitchProps> {
277
282
  constructor() {
278
283
  super('Switch')
279
284
  }
280
285
  }
281
-
286
+ @Observed
282
287
  class TaroCheckboxGroupElement extends TaroFormWidgetElement<CheckboxGroupProps> {
283
288
  constructor() {
284
289
  super('CheckboxGroup')
@@ -304,6 +309,7 @@ class TaroCheckboxGroupElement extends TaroFormWidgetElement<CheckboxGroupProps>
304
309
  }
305
310
  }
306
311
 
312
+ @Observed
307
313
  class TaroRadioGroupElement extends TaroFormWidgetElement<RadioGroupProps> {
308
314
  constructor() {
309
315
  super('RadioGroup')
@@ -329,6 +335,7 @@ class TaroRadioGroupElement extends TaroFormWidgetElement<RadioGroupProps> {
329
335
  }
330
336
  }
331
337
 
338
+ @Observed
332
339
  class TaroFormElement extends TaroFormWidgetElement {
333
340
  constructor() {
334
341
  super('Form')