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