@tarojs/plugin-platform-harmony-ets 4.0.0-alpha.2 → 4.0.0-alpha.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 (129) hide show
  1. package/dist/apis/base/system.ts +73 -20
  2. package/dist/apis/canvas/index.ts +10 -1
  3. package/dist/apis/device/clipboard.ts +16 -8
  4. package/dist/apis/framework/index.ts +1 -5
  5. package/dist/apis/index.ts +25 -17
  6. package/dist/apis/media/image/index.ts +1 -1
  7. package/dist/apis/storage/index.ts +146 -78
  8. package/dist/apis/ui/animation/animation.ts +71 -29
  9. package/dist/apis/ui/background.ts +2 -1
  10. package/dist/apis/ui/interaction/index.ts +42 -59
  11. package/dist/apis/ui/navigation-bar/index.ts +1 -1
  12. package/dist/apis/ui/pull-down-refresh.ts +9 -3
  13. package/dist/apis/ui/scroll/index.ts +5 -5
  14. package/dist/apis/ui/tab-bar.ts +3 -3
  15. package/dist/apis/utils/index.ts +2 -1
  16. package/dist/apis/wxml/IntersectionObserver.ts +18 -10
  17. package/dist/apis/wxml/selectorQuery.ts +26 -13
  18. package/dist/components-harmony-ets/button.ets +64 -34
  19. package/dist/components-harmony-ets/canvas.ets +51 -0
  20. package/dist/components-harmony-ets/checkbox.ets +72 -61
  21. package/dist/components-harmony-ets/form.ets +51 -29
  22. package/dist/components-harmony-ets/icon.ets +31 -19
  23. package/dist/components-harmony-ets/image.ets +34 -14
  24. package/dist/components-harmony-ets/index.ets +92 -0
  25. package/dist/components-harmony-ets/innerHtml.ets +11 -6
  26. package/dist/components-harmony-ets/input.ets +49 -41
  27. package/dist/components-harmony-ets/label.ets +71 -44
  28. package/dist/components-harmony-ets/listView.ets +26 -0
  29. package/dist/components-harmony-ets/movableArea.ets +126 -0
  30. package/dist/components-harmony-ets/movableView.ets +93 -0
  31. package/dist/components-harmony-ets/navigationBar.ets +65 -0
  32. package/dist/components-harmony-ets/pageMeta.ets +94 -0
  33. package/dist/components-harmony-ets/picker.ets +42 -38
  34. package/dist/components-harmony-ets/progress.ets +52 -0
  35. package/dist/components-harmony-ets/pseudo.ets +80 -0
  36. package/dist/components-harmony-ets/radio.ets +74 -64
  37. package/dist/components-harmony-ets/richText.ets +14 -30
  38. package/dist/components-harmony-ets/scrollList.ets +94 -0
  39. package/dist/components-harmony-ets/scrollView.ets +61 -57
  40. package/dist/components-harmony-ets/slider.ets +18 -14
  41. package/dist/components-harmony-ets/stickySection.ets +42 -0
  42. package/dist/components-harmony-ets/style.ets +381 -130
  43. package/dist/components-harmony-ets/swiper.ets +61 -20
  44. package/dist/components-harmony-ets/switch.ets +36 -32
  45. package/dist/components-harmony-ets/{index.ts → tag.ts} +6 -0
  46. package/dist/components-harmony-ets/text.ets +135 -49
  47. package/dist/components-harmony-ets/textArea.ets +50 -35
  48. package/dist/components-harmony-ets/utils/AttributeManager.ets +1 -1
  49. package/dist/components-harmony-ets/utils/DynamicCenter.ts +2 -2
  50. package/dist/components-harmony-ets/utils/flexManager.ets +49 -19
  51. package/dist/components-harmony-ets/utils/helper.ets +18 -5
  52. package/dist/components-harmony-ets/utils/htmlParser/HarmonyHTMLParser.ts +1 -2
  53. package/dist/components-harmony-ets/utils/htmlParser/index.ts +1 -1
  54. package/dist/components-harmony-ets/utils/index.ts +54 -50
  55. package/dist/components-harmony-ets/utils/styles.ets +170 -93
  56. package/dist/components-harmony-ets/video.ets +34 -21
  57. package/dist/components-harmony-ets/view.ets +63 -52
  58. package/dist/components-harmony-ets/webView.ets +40 -34
  59. package/dist/index.d.ts +152 -0
  60. package/dist/index.js +99 -55
  61. package/dist/index.js.map +1 -1
  62. package/dist/runtime-ets/bom/document.ts +6 -4
  63. package/dist/runtime-ets/bom/getComputedStyle.ts +2 -2
  64. package/dist/runtime-ets/bom/window.ts +7 -0
  65. package/dist/runtime-ets/current.ts +3 -0
  66. package/dist/runtime-ets/dom/bind.ts +28 -12
  67. package/dist/runtime-ets/dom/class-list.ts +2 -2
  68. package/dist/runtime-ets/dom/cssNesting.ts +419 -0
  69. package/dist/runtime-ets/dom/cssStyleDeclaration.ts +28 -42
  70. package/dist/runtime-ets/dom/document.ts +8 -11
  71. package/dist/runtime-ets/dom/element/canvas.ts +136 -0
  72. package/dist/runtime-ets/dom/element/element.ts +348 -57
  73. package/dist/runtime-ets/dom/element/form.ts +31 -26
  74. package/dist/runtime-ets/dom/element/index.ts +30 -2
  75. package/dist/runtime-ets/dom/element/movableArea.ts +11 -0
  76. package/dist/runtime-ets/dom/element/movableView.ts +244 -0
  77. package/dist/runtime-ets/dom/element/normal.ts +35 -8
  78. package/dist/runtime-ets/dom/element/progress.ts +11 -0
  79. package/dist/runtime-ets/dom/element/scrollView.ts +1 -0
  80. package/dist/runtime-ets/dom/element/text.ts +1 -8
  81. package/dist/runtime-ets/dom/element/video.ts +5 -4
  82. package/dist/runtime-ets/dom/element/webView.ts +12 -5
  83. package/dist/runtime-ets/dom/event.ts +2 -4
  84. package/dist/runtime-ets/dom/eventTarget.ts +2 -3
  85. package/dist/runtime-ets/dom/node.ts +54 -29
  86. package/dist/runtime-ets/dom/stylesheet/covertWeb2Hm.ts +416 -235
  87. package/dist/runtime-ets/dom/stylesheet/index.ts +29 -311
  88. package/dist/runtime-ets/dom/stylesheet/type.ts +51 -9
  89. package/dist/runtime-ets/dom/stylesheet/util.ts +33 -27
  90. package/dist/runtime-ets/index.ts +2 -1
  91. package/dist/runtime-ets/interface/event.ts +1 -1
  92. package/dist/runtime-ets/utils/index.ts +73 -13
  93. package/dist/runtime-ets/utils/info.ts +2 -2
  94. package/dist/runtime-framework/react/app.ts +9 -4
  95. package/dist/runtime-framework/react/hooks.ts +0 -1
  96. package/dist/runtime-framework/react/index.ts +0 -2
  97. package/dist/runtime-framework/react/native-page.ts +154 -77
  98. package/dist/runtime-framework/react/page.ts +3 -8
  99. package/dist/runtime-framework/solid/app.ts +25 -45
  100. package/dist/runtime-framework/solid/connect.ts +21 -3
  101. package/dist/runtime-framework/solid/hooks.ts +16 -11
  102. package/dist/runtime-framework/solid/index.ts +6 -2
  103. package/dist/runtime-framework/solid/page.ts +84 -30
  104. package/dist/runtime-framework/solid/reconciler/props.ts +70 -25
  105. package/dist/runtime-framework/solid/reconciler/render.ts +16 -6
  106. package/dist/runtime-framework/solid/reconciler/use.ts +0 -1
  107. package/dist/runtime-framework/solid/utils/index.ts +0 -2
  108. package/dist/runtime-utils.d.ts +827 -0
  109. package/dist/runtime-utils.js +435 -218
  110. package/dist/runtime-utils.js.map +1 -1
  111. package/dist/runtime.d.ts +1 -0
  112. package/dist/runtime.js +435 -218
  113. package/dist/runtime.js.map +1 -1
  114. package/index.js +3 -1
  115. package/package.json +14 -15
  116. package/static/media/cancel.svg +1 -1
  117. package/static/media/circle.svg +1 -1
  118. package/static/media/clear.svg +1 -1
  119. package/static/media/download.svg +1 -1
  120. package/static/media/info.svg +1 -1
  121. package/static/media/info_circle.svg +1 -1
  122. package/static/media/search.svg +1 -1
  123. package/static/media/success.svg +1 -1
  124. package/static/media/success_no_circle.svg +1 -1
  125. package/static/media/warn.svg +1 -1
  126. package/types/harmony.d.ts +4 -0
  127. package/types/index.d.ts +4 -0
  128. package/types/runtime.d.ts +1 -1
  129. /package/dist/runtime-framework/solid/{contant.ts → constant.ts} +0 -0
@@ -0,0 +1,136 @@
1
+ import { eventSource, TaroAny, TaroNode } from '@tarojs/runtime'
2
+
3
+ import { TaroElement } from './element'
4
+
5
+ import type { CanvasProps, CanvasTouchEvent } from '@tarojs/components/types'
6
+
7
+ export class CanvasRenderingContext2DWXAdapter extends CanvasRenderingContext2D {
8
+ // constructor(settings?: RenderingContextSetting) {
9
+ // super(settings)
10
+ // }
11
+
12
+ createCircularGradient() {
13
+ // Not supported now
14
+ }
15
+
16
+ draw(cb?: (...args: any[]) => any) {
17
+ typeof cb === 'function' && cb()
18
+ // Not supported now
19
+ }
20
+
21
+ setFillStyle(fillStyle: typeof this.fillStyle) {
22
+ this.fillStyle = fillStyle
23
+ }
24
+
25
+ setFontSize(fontSize: number) {
26
+ const font = this.font.split(' ')
27
+ font[2] = `${fontSize}`
28
+ this.font = font.join(' ')
29
+ }
30
+
31
+ setGlobalAlpha(globalAlpha: typeof this.globalAlpha) {
32
+ this.globalAlpha = globalAlpha
33
+ }
34
+
35
+ setLineCap(lineCap: typeof this.lineCap) {
36
+ this.lineCap = lineCap
37
+ }
38
+
39
+ setLineJoin(lineJoin: typeof this.lineJoin) {
40
+ this.lineJoin = lineJoin
41
+ }
42
+
43
+ setLineWidth(lineWidth: typeof this.lineWidth) {
44
+ this.lineWidth = lineWidth
45
+ }
46
+
47
+ setMiterLimit(miterLimit: typeof this.miterLimit) {
48
+ this.miterLimit = miterLimit
49
+ }
50
+
51
+ setShadow(offsetX: number, offsetY: number, blur: number, color: string) {
52
+ this.shadowOffsetX = offsetX
53
+ this.shadowOffsetY = offsetY
54
+ this.shadowBlur = blur
55
+ this.shadowColor = color
56
+ }
57
+
58
+ setStrokeStyle(strokeStyle: typeof this.strokeStyle) {
59
+ this.strokeStyle = strokeStyle
60
+ }
61
+
62
+ setTextAlign(textAlign: typeof this.textAlign) {
63
+ this.textAlign = textAlign
64
+ }
65
+
66
+ setTextBaseline(textBaseline: typeof this.textBaseline) {
67
+ this.textBaseline = textBaseline
68
+ }
69
+ }
70
+ function getContextKey(obj) {
71
+ let currentObj = obj
72
+ let res = []
73
+ while (currentObj) {
74
+ if (currentObj instanceof CanvasRenderingContext2D) {
75
+ res = [...res, ...Object.getOwnPropertyNames(currentObj)]
76
+ }
77
+ currentObj = Object.getPrototypeOf(currentObj)
78
+ }
79
+ return res
80
+ }
81
+
82
+ @Observed
83
+ export class TaroCanvasElement extends TaroElement<CanvasProps, CanvasTouchEvent> {
84
+ _drawList: {
85
+ key: string
86
+ value: TaroAny
87
+ }[] = []
88
+
89
+ settings: RenderingContextSettings
90
+ _context: CanvasRenderingContext2D
91
+ _contextProxy: CanvasRenderingContext2D
92
+
93
+ constructor() {
94
+ super('Canvas')
95
+ this.settings = new RenderingContextSettings(true)
96
+ const context = new CanvasRenderingContext2DWXAdapter(this.settings) as CanvasRenderingContext2D
97
+ this._context = context
98
+
99
+ const proxyObj = getContextKey(context).reduce((obj, key) => {
100
+ if (typeof context[key] === 'function') {
101
+ obj[key] = new Proxy(context[key], {
102
+ apply: (target, thisArg, argumentsList) => {
103
+ this._drawList.push({
104
+ key,
105
+ value: argumentsList,
106
+ })
107
+ },
108
+ })
109
+ } else {
110
+ obj[key] = context[key]
111
+ }
112
+ return obj
113
+ }, {})
114
+
115
+ this._contextProxy = new Proxy(proxyObj, {
116
+ set: (_, property, value) => {
117
+ this._drawList.push({
118
+ key: property,
119
+ value,
120
+ })
121
+ return true
122
+ },
123
+ })
124
+ }
125
+
126
+ get context() {
127
+ return this._contextProxy
128
+ }
129
+
130
+ public setAttribute(name: string, value: TaroAny): void {
131
+ if (name === 'canvasId') {
132
+ eventSource.set(`canvasId-${value}`, this as TaroNode)
133
+ }
134
+ super.setAttribute(name, value)
135
+ }
136
+ }
@@ -1,67 +1,94 @@
1
- import { eventSource } from '@tarojs/runtime/dist/runtime.esm'
1
+ import { eventCenter, 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'
5
+ import { Current } from '../../current'
4
6
  import { findChildNodeWithDFS } from '../../utils'
5
7
  import { initComponentNodeInfo, triggerAttributesCallback } from '../../utils/info'
6
8
  import { bindAnimation } from '../bind'
7
9
  import { ClassList } from '../class-list'
10
+ import { type ICSSStyleDeclaration, createCSSStyleDeclaration } from '../cssStyleDeclaration'
8
11
  import { NodeType, TaroNode } from '../node'
9
- import StyleSheet from '../stylesheet'
12
+ import StyleSheet, { HarmonyStyle, TaroStyleType } from '../stylesheet'
10
13
 
11
- import type { StandardProps } from '@tarojs/components/types'
14
+ import type { BaseTouchEvent, ITouchEvent, StandardProps } from '@tarojs/components/types'
12
15
  import type { TaroAny } from '../../utils'
13
- import type { ICSSStyleDeclaration } from '../cssStyleDeclaration'
14
16
 
15
- type NamedNodeMap = ({ name: string, value: string })[]
17
+ type NamedNodeMap = { name: string, value: string }[]
16
18
 
17
19
  export interface TaroExtraProps {
18
20
  compileMode?: string | boolean
19
21
  compileIf?: boolean
20
22
  disabled?: boolean
23
+ __hmStyle?: TaroStyleType
21
24
  }
22
25
 
23
- @Observed
24
- export class TaroElement<T extends StandardProps = StandardProps> extends TaroNode {
26
+ export class TaroElement<
27
+ T extends StandardProps<any, U> = StandardProps,
28
+ U extends BaseTouchEvent<any> = ITouchEvent
29
+ > extends TaroNode {
25
30
  public _innerHTML = ''
26
- public _nodeInfo: TaroAny = {}
31
+ public _nodeInfo: TaroAny = {
32
+ layer: 0 // 渲染层级
33
+ }
34
+
35
+ public hm_instance: TaroAny
36
+
37
+ public get _instance () {
38
+ return this.hm_instance
39
+ }
40
+
41
+ public set _instance (value) {
42
+ this.hm_instance = value
43
+ if (this._nodeInfo.aboutToAppear) {
44
+ let task
45
+ // eslint-disable-next-line no-cond-assign
46
+ while (task = this._nodeInfo.aboutToAppear.shift()) {
47
+ task()
48
+ }
49
+ }
50
+ }
51
+
27
52
  public readonly tagName: string
53
+ public dataset: Record<string, unknown> = EMPTY_OBJ
28
54
  public _attrs: T & TaroExtraProps = {} as T & TaroExtraProps
29
55
 
30
- _client?: Area
31
- _scroll?: Area
56
+ private _page: any
32
57
 
33
58
  constructor(tagName: string) {
34
59
  super(tagName.replace(new RegExp('(?<=.)([A-Z])', 'g'), '-$1').toUpperCase(), NodeType.ELEMENT_NODE)
35
60
  this.tagName = this.nodeName
36
-
61
+ this._style = createCSSStyleDeclaration(this)
37
62
  initComponentNodeInfo(this)
38
63
  bindAnimation(this)
64
+
65
+ this._page = Current.page
39
66
  }
40
67
 
41
- public set id (value: string) {
68
+ public set id(value: string) {
42
69
  this.setAttribute('id', value)
43
70
  }
44
71
 
45
- public get id (): string {
72
+ public get id(): string {
46
73
  return this.getAttribute('id') || this._nid
47
74
  }
48
75
 
49
- public set className (value: string) {
76
+ public set className(value: string) {
50
77
  this.setAttribute('class', value)
51
78
  }
52
79
 
53
- public get className (): string {
80
+ public get className(): string {
54
81
  return this.getAttribute('class') || ''
55
82
  }
56
83
 
57
- public get classList (): ClassList {
84
+ public get classList(): ClassList {
58
85
  return new ClassList(this.className, this)
59
86
  }
60
87
 
61
- public get attributes (): NamedNodeMap {
88
+ public get attributes(): NamedNodeMap {
62
89
  const list: NamedNodeMap = []
63
90
 
64
- Object.keys(this._attrs).forEach(name => {
91
+ Object.keys(this._attrs).forEach((name) => {
65
92
  const value: TaroAny = this._attrs[name]
66
93
 
67
94
  list.push({ name, value })
@@ -70,73 +97,118 @@ export class TaroElement<T extends StandardProps = StandardProps> extends TaroNo
70
97
  return list
71
98
  }
72
99
 
73
- public get children (): TaroElement[] {
74
- return this.childNodes.filter(node => node.nodeType === NodeType.ELEMENT_NODE) as TaroElement[]
100
+ public get children(): TaroElement[] {
101
+ return this.childNodes.filter((node) => node.nodeType === NodeType.ELEMENT_NODE) as TaroElement[]
75
102
  }
76
103
 
77
- public setAttribute (name: string, value: TaroAny): void {
78
- if (name === ID) {
79
- eventSource.delete(this._attrs.id)
80
- eventSource.set(value, this as TaroAny)
104
+ public setAttribute(name: string, value: TaroAny): void {
105
+ switch (name) {
106
+ case ID:
107
+ eventSource.delete(this._attrs.id)
108
+ eventSource.set(value, this as TaroAny)
109
+ break
110
+ default:
111
+ if (name.startsWith('data-')) {
112
+ if (this.dataset === EMPTY_OBJ) {
113
+ this.dataset = Object.create(null)
114
+ }
115
+ this.dataset[toCamelCase(name.replace(/^data-/, ''))] = value
116
+ }
117
+ break
81
118
  }
82
119
 
83
120
  this._attrs[name] = value
84
121
 
85
- const attributeTriggerValue: TaroAny = ATTRIBUTES_CALLBACK_TRIGGER_MAP[name]
86
- if (attributeTriggerValue) {
87
- const triggerName: TaroAny = attributeTriggerValue.triggerName
88
- const valueInspect: TaroAny = attributeTriggerValue.valueInspect
122
+ // 混合开发的组件没办法自动更新,需要把父级的结点删掉新建
123
+ // Current.nativeComponentNames会在render.ets中赋值
124
+ if (Current.nativeComponentNames?.includes(this.tagName)) {
125
+ const idxOfRef = this.parentNode?.findIndex(this)
126
+
127
+ if (idxOfRef !== undefined) {
128
+ this._nativeUpdateTrigger++
129
+ this.parentNode?.notifyDataChange(idxOfRef)
130
+ }
131
+ }
132
+
133
+ if (['PAGE-META', 'NAVIGATION-BAR'].includes(this.tagName)) {
134
+ // FIXME 等 Harmony 支持更细粒度的 @Watch 方法后移出
135
+ eventCenter.trigger('__taroComponentAttributeUpdate', {
136
+ id: this._nid,
137
+ tagName: this.tagName,
138
+ attribute: name,
139
+ value
140
+ })
141
+ } else {
142
+ const attributeTriggerValue: TaroAny = ATTRIBUTES_CALLBACK_TRIGGER_MAP[name]
143
+ if (attributeTriggerValue) {
144
+ const triggerName: TaroAny = attributeTriggerValue.triggerName
145
+ const valueInspect: TaroAny = attributeTriggerValue.valueInspect
89
146
 
90
- if (valueInspect && !valueInspect(value)) return
147
+ if (valueInspect && !valueInspect(value)) return
91
148
 
92
- triggerAttributesCallback(this, triggerName)
149
+ triggerAttributesCallback(this, triggerName)
150
+ }
93
151
  }
94
152
  }
95
153
 
96
- public getAttribute (name: string): string {
154
+ public getAttribute(name: string): string {
97
155
  return this._attrs[name]
98
156
  }
99
157
 
100
- public removeAttribute (name: string): void {
158
+ public removeAttribute(name: string): void {
101
159
  this._attrs[name] = null
102
160
  }
103
161
 
104
- public hasAttribute (name: string): boolean {
162
+ public hasAttribute(name: string): boolean {
105
163
  return !!this._attrs[name]
106
164
  }
107
165
 
108
- public hasAttributes (): boolean {
166
+ public hasAttributes(): boolean {
109
167
  return Object.keys(this._attrs).length > 0
110
168
  }
111
169
 
112
- public getElementById<T extends TaroElement = TaroElement> (id: string | undefined | null) {
113
- return findChildNodeWithDFS<T>(this as TaroAny, (el) => {
114
- return el.id === id
115
- }, false)
170
+ public getElementById<T extends TaroElement = TaroElement>(id: string | undefined | null) {
171
+ return findChildNodeWithDFS<T>(
172
+ this as TaroAny,
173
+ (el) => {
174
+ return el.id === id
175
+ },
176
+ false
177
+ )
116
178
  }
117
179
 
118
- public getElementsByTagName<T extends TaroElement = TaroElement> (tagName: string) {
119
- return findChildNodeWithDFS<T>(this as TaroAny, (el) => {
120
- return el.nodeName === tagName || (tagName === '*' && this as TaroAny !== el)
121
- }, true) || []
180
+ public getElementsByTagName<T extends TaroElement = TaroElement>(tagName: string) {
181
+ return (
182
+ findChildNodeWithDFS<T>(
183
+ this as TaroAny,
184
+ (el) => {
185
+ return el.nodeName === tagName || (tagName === '*' && (this as TaroAny) !== el)
186
+ },
187
+ true
188
+ ) || []
189
+ )
122
190
  }
123
191
 
124
- public getElementsByClassName<T extends TaroElement = TaroElement> (className: string) {
192
+ public getElementsByClassName<T extends TaroElement = TaroElement>(className: string) {
125
193
  const classNames = className.trim().split(new RegExp('\\s+'))
126
194
 
127
- return findChildNodeWithDFS<T>(this as TaroAny, (el) => {
128
- const classList = el.classList
129
- return classNames.every(c => {
130
- const bool = classList.contains(c)
131
-
132
- return bool
133
- })
134
- }, true) || []
195
+ return (
196
+ findChildNodeWithDFS<T>(
197
+ this as TaroAny,
198
+ (el) => {
199
+ const classList = el.classList
200
+ return classNames.every((c) => {
201
+ const bool = classList.contains(c)
202
+
203
+ return bool
204
+ })
205
+ },
206
+ true
207
+ ) || []
208
+ )
135
209
  }
136
210
 
137
- // TODO dataset
138
-
139
- public set innerHTML (value: string) {
211
+ public set innerHTML(value: string) {
140
212
  if (this.nodeType === NodeType.ELEMENT_NODE && this.ownerDocument) {
141
213
  const ele = this.ownerDocument.createElement('inner-html')
142
214
  ele._innerHTML = value
@@ -144,20 +216,239 @@ export class TaroElement<T extends StandardProps = StandardProps> extends TaroNo
144
216
  }
145
217
  }
146
218
 
147
- public get innerHTML (): string {
219
+ public get innerHTML(): string {
148
220
  return this._innerHTML
149
221
  }
150
222
 
223
+ // 存放的样式,获取其实跟获取style是一样的,只不过这里取的更快捷,不需要走style的get方法进到cssStyleDeclaration
151
224
  public _st = new StyleSheet()
152
225
 
153
226
  // 经转换后的鸿蒙样式
154
- public get hmStyle () {
227
+ public get hmStyle() {
155
228
  return this._st.hmStyle
156
229
  }
157
230
 
158
231
  public _style: ICSSStyleDeclaration | null = null
159
232
 
160
- public get style (): ICSSStyleDeclaration | null {
233
+ public get style(): ICSSStyleDeclaration | null {
161
234
  return this._style
162
235
  }
236
+
237
+ // 伪元素,不存在style动态设置,均已被转换为鸿蒙样式
238
+ // 可根据实际情况,迁移到具体的组件中,如View、ScrollView中,Text\Image其实是不需要的
239
+ public _pseudo_before: StyleSheet | null = null
240
+
241
+ public set_pseudo_before(value: HarmonyStyle | null) {
242
+ if (value) {
243
+ if (!this._pseudo_before) {
244
+ this._pseudo_before = new StyleSheet()
245
+ }
246
+ Object.keys(value).forEach(key => {
247
+ this._pseudo_before!.hmStyle[key] = value[key]
248
+ })
249
+ } else {
250
+ this._pseudo_before = null
251
+ }
252
+ }
253
+
254
+ public _pseudo_after: StyleSheet | null = null
255
+
256
+ public set_pseudo_after(value: HarmonyStyle | null) {
257
+ if (value) {
258
+ if (!this._pseudo_after) {
259
+ this._pseudo_after = new StyleSheet()
260
+ }
261
+ Object.keys(value).forEach(key => {
262
+ this._pseudo_after!.hmStyle[key] = value[key]
263
+ })
264
+ } else {
265
+ this._pseudo_after = null
266
+ }
267
+ }
268
+
269
+ // 伪类,在获取的时候根据dom和parent的关系,动态设置
270
+ public _pseudo_class: Record<string, StyleSheet | null> = {
271
+ // ["::first-child"]: new StyleSheet(),
272
+ }
273
+
274
+ public set_pseudo_class(name: string, value: HarmonyStyle | null) {
275
+ if (value) {
276
+ if (!this._pseudo_class[name]) {
277
+ this._pseudo_class[name] = new StyleSheet()
278
+ }
279
+ Object.keys(value).forEach(key => {
280
+ this._pseudo_class[name]!.hmStyle[key] = value[key]
281
+ })
282
+ } else {
283
+ this._pseudo_class[name] = null
284
+ }
285
+ }
286
+
287
+ get currentLayerNode () {
288
+ if (!this._page) return null
289
+ if (typeof this._page.tabBarCurrentIndex !== 'undefined') {
290
+ this._page.layerNode ||= []
291
+ this._page.layerNode[this._page.tabBarCurrentIndex] ||= Current.createHarmonyElement('VIEW')
292
+ // Tabbar
293
+ return this._page.layerNode[this._page.tabBarCurrentIndex]
294
+ } else {
295
+ this._page.layerNode ||= Current.createHarmonyElement('VIEW')
296
+ return this._page.layerNode
297
+ }
298
+ }
299
+
300
+ get currentLayerParents () {
301
+ if (!this._page) return null
302
+ if (typeof this._page.tabBarCurrentIndex !== 'undefined') {
303
+ this._page.layerParents ||= []
304
+ this._page.layerParents[this._page.tabBarCurrentIndex] ||= []
305
+ // Tabbar
306
+ return this._page.layerParents[this._page.tabBarCurrentIndex]
307
+ } else {
308
+ this._page.layerParents ||= []
309
+ return this._page.layerParents
310
+ }
311
+ }
312
+
313
+ // 设置渲染层级,0为正常层级,大于0为固定层级
314
+ // 1、appendChild的时候会判断是否需要设置层级
315
+ // 2、taro-react的setProperty,在处理属性变化的时候,会判断是否需要设置层级
316
+ // 3、removeChild的时候,会判断是否需要移除层级
317
+ public setLayer (value: number) {
318
+ if (!this.parentNode) return // 没有父节点,不需要设置层级关系
319
+ this._nodeInfo.layer = value
320
+
321
+ const currentLayerNode = this.currentLayerNode
322
+ const currentLayerParents = this.currentLayerParents
323
+ if (!currentLayerNode || !currentLayerParents) return
324
+ if (value > 0) {
325
+ // 插入到固定浮层
326
+ currentLayerNode.childNodes.push(this)
327
+ currentLayerNode.notifyDataAdd(currentLayerNode.childNodes.length - 1)
328
+ // 绑定祖先的节点id,建立关系,方便在祖先卸载(removeChild)的时候,能够找到该节点使其卸载
329
+ const _parentRecord = {}
330
+ generateLayerParentIds(_parentRecord, this)
331
+ currentLayerParents[this._nid] = _parentRecord
332
+ } else {
333
+ const idx = currentLayerNode.childNodes.findIndex(n => n._nid === this._nid)
334
+ currentLayerNode.childNodes.splice(idx, 1)
335
+ currentLayerNode.notifyDataDelete(idx)
336
+
337
+ delete currentLayerParents[this._nid]
338
+ }
339
+
340
+ if (this.parentNode) {
341
+ this.parentNode.notifyDataChange(this.parentNode.findIndex(this))
342
+ this.updateComponent()
343
+ }
344
+ }
345
+
346
+ protected toggleLayer(add: boolean) {
347
+ if (add) {
348
+ if (this._st?.hmStyle.position === 'fixed') {
349
+ this.setLayer(1)
350
+ }
351
+ } else {
352
+ const currentLayerParents = this.currentLayerParents
353
+ if (!currentLayerParents) return
354
+ // 识别Current.page.layerParents里面是否有需要移除的固定元素
355
+ if (this._nodeInfo?.layer > 0) {
356
+ delete currentLayerParents[this._nid]
357
+ this.setLayer(0)
358
+ } else {
359
+ Object.keys(currentLayerParents).forEach(fixedId => {
360
+ const parentIds = currentLayerParents[fixedId]
361
+ if (parentIds[this._nid]) {
362
+ // 需要移除fixedId
363
+ delete currentLayerParents[fixedId]
364
+ const fixedNode = eventSource.get(fixedId) as unknown as TaroElement
365
+ if (fixedNode) {
366
+ fixedNode.setLayer(0)
367
+ }
368
+ }
369
+ })
370
+ }
371
+ }
372
+ }
373
+
374
+ // 设置动画
375
+ public setAnimation (playing) {
376
+ if (!this._instance) {
377
+ if (!this._nodeInfo.aboutToAppear) {
378
+ this._nodeInfo.aboutToAppear = []
379
+ }
380
+ this._nodeInfo.aboutToAppear.push(() => {
381
+ this.setAnimation(playing)
382
+ })
383
+ return
384
+ }
385
+
386
+ const keyframes = this._st.hmStyle.animationName
387
+
388
+ // 防止动画闪烁,需要把第一帧的内容先设置上去设置初始样式
389
+ if (playing && keyframes && keyframes[0] && keyframes[0].percentage === 0) {
390
+ this._instance.overwriteStyle = keyframes[0].event
391
+ }
392
+
393
+ // 首次设置,不用实例替换
394
+ if (!this._nodeInfo.hasAnimation) {
395
+ this._nodeInfo.hasAnimation = true
396
+ // 下一帧播放,等节点样式首次设置上去在进行覆盖
397
+ setTimeout(() => {
398
+ if (playing) {
399
+ this.playAnimation()
400
+ }
401
+ }, 0)
402
+ } else if (this.parentNode) {
403
+ const idx = this.parentNode.findIndex(this)
404
+ // Note: 因为keyframeAnimateTo无法暂停,华为没有支持,只能临时先换掉实例,重新创建ark节点,使得原本的keyframeAnimateTo失效
405
+ // remove
406
+ this.parentNode.childNodes.splice(idx, 1)
407
+ this.parentNode.notifyDataDelete(idx)
408
+
409
+ // 下一帧播放,等实例被移除掉,再重新插入
410
+ setTimeout(() => {
411
+ // insert
412
+ this.parentNode?.childNodes.splice(idx, 0, this)
413
+ this.parentNode?.notifyDataAdd(idx)
414
+
415
+ // 执行动画
416
+ if (playing) {
417
+ this.playAnimation()
418
+ }
419
+ }, 0)
420
+ }
421
+ }
422
+
423
+ private playAnimation () {
424
+ const {
425
+ animationDuration = 0, animationDelay = 0, animationIterationCount = 1, animationName: keyframes,
426
+ animationTimingFunction
427
+ } = this._st.hmStyle
428
+
429
+ if (keyframes) {
430
+ let cur_percentage = 0
431
+ this._instance.getUIContext()?.keyframeAnimateTo({
432
+ delay: animationDelay,
433
+ iterations: animationIterationCount,
434
+ }, keyframes.map(item => {
435
+ const duration = (item.percentage - cur_percentage) * animationDuration
436
+ cur_percentage = item.percentage
437
+ return {
438
+ duration: duration,
439
+ curve: item.event.animationTimingFunction || animationTimingFunction,
440
+ event: () => {
441
+ this._instance.overwriteStyle = item.event
442
+ }
443
+ }
444
+ }))
445
+ }
446
+ }
447
+ }
448
+
449
+ function generateLayerParentIds(ids: Record<string, true>, node?: TaroElement) {
450
+ if (node?.parentElement) {
451
+ ids[node.parentElement._nid] = true
452
+ generateLayerParentIds(ids, node.parentElement)
453
+ }
163
454
  }