@tarojs/plugin-platform-harmony-ets 4.0.0-alpha.0 → 4.0.0-alpha.11

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
@@ -1,5 +1,8 @@
1
1
  import { convertNumber2VP } from '@tarojs/runtime'
2
- import { isNumber } from '@tarojs/shared'
2
+ import { isNumber, isUndefined } from '@tarojs/shared'
3
+
4
+ import type { RichTextProps } from '@tarojs/components/types/RichText'
5
+ import type { TaroRichTextElement } from '@tarojs/runtime'
3
6
 
4
7
  export function getSingleSelector(range, rangeKey): any[] {
5
8
  return range.map((data) => data[rangeKey])
@@ -18,66 +21,67 @@ export function getMultiSelector(ctx, range, rangeKey, value) {
18
21
  }))
19
22
  }
20
23
 
21
- export function getUnit (val) {
22
- if (/\d+(vp)$/.test(val)) {
24
+ export function getUnit (val: string | number): string {
25
+ if (isNumber(val) || /\d+px$/.test(val)) {
26
+ return convertNumber2VP(parseFloat(val as string))
27
+ } else if (/\d+(vp)$/.test(val)) {
23
28
  return val
24
- } else if (isNumber(val) || /\d+px$/.test(val)) {
25
- return convertNumber2VP(parseFloat(val))
26
29
  }
27
30
  return val
28
31
  }
29
32
 
30
- function handleNodeStyleData (dataValue: string, handler: (values: string[]) => { [key: string]: string } | void) {
31
- let res: any = {}
32
- if (dataValue) {
33
- const values = dataValue.trim().split(/\s+/)
34
- const data = handler(values)
35
-
36
- if (!data) return res
37
-
38
- res = data
33
+ export function generateText (node: TaroRichTextElement): string {
34
+ return parseHtmlNode(node._attrs.nodes || '')
35
+ }
39
36
 
40
- Object.keys(res).forEach(key => {
41
- const exec = `${res[key]}`.match(/(\d+)(px)$/)
42
- if (exec && values.length > 1) {
43
- res[key] = getUnit(+exec[1])
44
- }
45
- })
37
+ // 将nodeTree转换成harmony需要的string结构
38
+ function nodeToHtml(node: RichTextProps.Text | RichTextProps.HTMLElement): string {
39
+ if (node.type === 'text') {
40
+ return node.text
41
+ }
42
+ if (node.attrs) {
43
+ const attributes = Object.entries(node.attrs)
44
+ .map((item: [string, string]) => `${item[0]}="${item[1]}"`)
45
+ .join(' ')
46
+ const childrenHtml: string = typeof node.children === 'string' ? node.children : (node.children || []).map((child: RichTextProps.Text | RichTextProps.HTMLElement) => nodeToHtml(child)).join('')
47
+ return `<${node.name}${attributes ? ' ' + attributes : ''}>${childrenHtml}</${node.name}>`
46
48
  }
49
+ return ''
50
+ }
47
51
 
48
- return res
52
+ function parseHtmlNode (nodes: Array<RichTextProps.Text | RichTextProps.HTMLElement> | string) {
53
+ return typeof nodes === 'string' ? nodes : `<div>${nodes.map(node => nodeToHtml(node)).join('')}</div>`
49
54
  }
50
55
 
51
- export function getNodeBorderRadiusData (dataValue: string) {
52
- return handleNodeStyleData(dataValue, values => {
53
- switch (values.length) {
54
- case 1:
55
- return { topLeft: values[0], topRight: values[0], bottomRight: values[0], bottomLeft: values[0] }
56
- case 2:
57
- return { topLeft: values[0], topRight: values[1], bottomRight: values[0], bottomLeft: values[1] }
58
- case 3:
59
- return { topLeft: values[0], topRight: values[1], bottomRight: values[2], bottomLeft: values[1] }
60
- case 4:
61
- return { topLeft: values[0], topRight: values[1], bottomRight: values[2], bottomLeft: values[3] }
62
- default:
63
- break
56
+ // 背景偏移算法:https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-position
57
+ export function computeBackgroundPosition(style) {
58
+ let offsetX = style.backgroundPosition?.x || 0
59
+ let offsetY = style.backgroundPosition?.y || 0
60
+ if (style.backgroundSize && typeof style.backgroundSize !== 'number') {
61
+ if (!isUndefined(style.backgroundSize.width) && style.width) {
62
+ if (typeof style.backgroundPosition.x === 'string' && style.backgroundPosition.x.indexOf('%') > 0) {
63
+ // (container width - image width) * (position x%) = (x offset value)
64
+ const width = parseFloat(style.width)
65
+ const bgWidth = parseFloat(style.backgroundSize.width)
66
+ const bgOffsetX = parseFloat(style.backgroundPosition.x)
67
+ offsetX = Number((width - bgWidth) * (bgOffsetX) / 100) || 0
68
+ }
69
+ }
70
+ if (!isUndefined(style.backgroundSize.height) && style.height) {
71
+ if (typeof style.backgroundPosition.y === 'string' && style.backgroundPosition.y.indexOf('%') > 0) {
72
+ // (container height - image height) * (position y%) = (y offset value)
73
+ const height = parseFloat(style.height)
74
+ const bgHeight = parseFloat(style.backgroundSize.height)
75
+ const bgOffsetY = parseFloat(style.backgroundPosition.y)
76
+ offsetY = Number((height - bgHeight) * (bgOffsetY) / 100) || 0
77
+ }
64
78
  }
65
- })
79
+ }
80
+
81
+ return { offsetX, offsetY }
66
82
  }
67
83
 
68
- export function getNodeMarginOrPaddingData (dataValue: string) {
69
- return handleNodeStyleData(dataValue, values => {
70
- switch (values.length) {
71
- case 1:
72
- return { top: values[0], right: values[0], bottom: values[0], left: values[0] }
73
- case 2:
74
- return { top: values[0], right: values[1], bottom: values[0], left: values[1] }
75
- case 3:
76
- return { top: values[0], right: values[1], bottom: values[2], left: values[1] }
77
- case 4:
78
- return { top: values[0], right: values[1], bottom: values[2], left: values[3] }
79
- default:
80
- break
81
- }
82
- })
84
+ export function convertVp2Px(val: string | number) {
85
+ const vp = parseFloat(`${val}`)
86
+ return vp2px(vp)
83
87
  }
@@ -1,88 +1,203 @@
1
+ import { isUndefined } from '@tarojs/shared'
1
2
  import { ObjectAssign } from '@tarojs/runtime'
2
3
 
3
- import { TEXT_DEFAULT_STYLE } from './constant/style'
4
+ import { FlexManager } from './flexManager'
4
5
 
5
- import type { StandardProps } from '@tarojs/components/types'
6
- import type { TaroAny, TaroElement, TaroStyleType, TaroTextStyleType, HarmonyType, HarmonyStyle } from '@tarojs/runtime'
6
+ import type { StandardProps, TextProps } from '@tarojs/components/types'
7
+ import type { TaroAny, TaroElement, TaroStyleType, TaroTextStyleType, HarmonyStyle } from '@tarojs/runtime'
7
8
 
8
9
  export function getFontAttributes (node: TaroElement): TaroTextStyleType {
9
- const hmStyle = node.hmStyle
10
+ let hmStyle = getNormalAttributes(node)
11
+ const attrs: TextProps = node._attrs || {}
12
+
13
+ if (!hmStyle) return {}
14
+
15
+ let lineHeight = hmStyle.lineHeight
16
+ if (!isUndefined(hmStyle.verticalAlign)) {
17
+ lineHeight = 0
18
+ }
19
+
20
+ let WebkitLineClamp = attrs.maxLines || hmStyle.WebkitLineClamp || Infinity
21
+ if (hmStyle.textOverflow && !isUndefined(hmStyle.textOverflow.overflow)) {
22
+ switch (hmStyle.textOverflow.overflow) {
23
+ case TextOverflow.Clip:
24
+ case TextOverflow.Ellipsis:
25
+ case TextOverflow.None: WebkitLineClamp = WebkitLineClamp || 1; break
26
+ default: break
27
+ }
28
+ }
29
+
30
+ const isFlexText = hmStyle.display === 'flex'
31
+ let textAlign = hmStyle.textAlign
32
+ let verticalAlign = hmStyle.verticalAlign
33
+
34
+ // 按照 w3c 规范,一旦设置了 display: flex,textAlign 和 verticalAlign 都会直接失效
35
+ // 需要使用 justifyContent 和 alignItems
36
+ if (isFlexText) {
37
+ switch (hmStyle.justifyContent) {
38
+ case FlexAlign.Start:
39
+ textAlign = TextAlign.Start
40
+ break
41
+ case FlexAlign.Center:
42
+ textAlign = TextAlign.Center
43
+ break
44
+ case FlexAlign.End:
45
+ textAlign = TextAlign.End
46
+ break
47
+ case FlexAlign.SpaceBetween:
48
+ case FlexAlign.SpaceAround:
49
+ textAlign = TextAlign.JUSTIFY
50
+ break
51
+ default:
52
+ textAlign = TextAlign.Start
53
+ break
54
+ }
55
+ switch (hmStyle.alignItems) {
56
+ case ItemAlign.Start:
57
+ verticalAlign = Alignment.Top
58
+ break
59
+ case ItemAlign.Center:
60
+ verticalAlign = Alignment.Center
61
+ break
62
+ case ItemAlign.End:
63
+ verticalAlign = Alignment.End
64
+ break
65
+ default:
66
+ verticalAlign = Alignment.Top
67
+ break
68
+ }
69
+ }
10
70
 
11
71
  const attributes: TaroAny = {
12
- maxLines: hmStyle.maxLines || Infinity,
72
+ textAlign,
73
+ verticalAlign,
74
+ WebkitLineClamp: WebkitLineClamp,
13
75
  // 已做处理的属性
14
76
  letterSpacing: hmStyle.letterSpacing,
15
- textAlign: hmStyle.textAlign,
16
- textOverflow: hmStyle.textOverflow
77
+ textOverflow: hmStyle.textOverflow,
78
+ lineHeight: lineHeight
17
79
  }
18
80
 
19
81
  return attributes
20
82
  }
21
83
 
22
- export function getNormalAttributes (node: TaroElement): TaroStyleType {
84
+ // 模拟 div 自动撑满父元素的情况
85
+ export function isMaxWidthView (node: TaroElement) {
86
+ const parentNode: TaroElement = node.parentNode as TaroElement
87
+
88
+ return node.tagName === 'VIEW' && parentNode && parentNode.tagName === 'VIEW' && !FlexManager.isFlexNode(parentNode)
89
+ }
90
+
91
+ export function getNormalAttributes (node: TaroElement, initStyle?: HarmonyStyle): HarmonyStyle {
23
92
  const hmStyle = node.hmStyle
93
+
94
+ if (!hmStyle) return {}
24
95
 
25
96
  const _nid = node._nid
26
97
  const _attrs: StandardProps = node._attrs || {}
27
98
 
28
- const transform: TaroAny = hmStyle.transform
29
- const transformRotate: HarmonyType.Transform.Rotate = transform?.rotate?.[0]
30
- const transformTranslate: HarmonyType.Transform.Translate = transform?.translate?.[0]
31
- const transformScale: HarmonyType.Transform.Scale = transform?.scale?.[0]
32
- const transformTransform: HarmonyType.Transform.Transform = transform?.transform?.[0]
33
-
34
- let normalAttributes: HarmonyStyle = {
35
- // Flex相关
36
- flexBasis: hmStyle.flexBasis,
37
- flexGrow: hmStyle.flexGrow,
38
- flexShrink: hmStyle.flexShrink,
39
- alignSelf: hmStyle.alignSelf,
40
- // 尺寸相关
41
- width: hmStyle.width,
42
- height: hmStyle.height,
43
- constraintSize: hmStyle.constraintSize,
44
- margin: hmStyle.margin,
45
- padding: hmStyle.padding,
46
- // 边框相关
47
- borderStyle: hmStyle.borderStyle,
48
- borderWidth: hmStyle.borderWidth,
49
- borderColor: hmStyle.borderColor,
50
- borderRadius: hmStyle.borderRadius,
51
- // 背景相关
52
- backgroundColor: hmStyle.backgroundColor,
53
- backgroundImage: hmStyle.backgroundImage,
54
- backgroundImageSize: hmStyle.backgroundImageSize,
55
- backgroundRepeat: hmStyle.backgroundRepeat,
56
- backgroundImagePosition: hmStyle.backgroundImagePosition,
57
- linearGradient: hmStyle.linearGradient,
58
- // 变换相关
59
- rotate: transformRotate,
60
- translate: transformTranslate,
61
- scale: transformScale,
62
- transform: transformTransform,
63
- // 通用文本相关
64
- color: hmStyle.color,
65
- fontSize: hmStyle.fontSize,
66
- fontStyle: hmStyle.fontStyle,
67
- fontWeight: hmStyle.fontWeight,
68
- fontFamily: hmStyle.fontFamily || TEXT_DEFAULT_STYLE.FONT_FAMILY,
69
- lineHeight: hmStyle.lineHeight,
70
- decoration: hmStyle.decoration,
71
- // 其他
72
- clip: hmStyle.clip,
73
- id: _attrs.id || _nid,
74
- opacity: hmStyle.opacity,
75
- // focus: _attrs.focus || false,
99
+ let normalAttributes = hmStyle
100
+
101
+ // 覆盖属性
102
+ normalAttributes.id = _attrs.id || _nid
103
+
104
+ let pseudoStylesheet = getPseudoClass(node)
105
+ if (pseudoStylesheet) {
106
+ normalAttributes = ObjectAssign({}, normalAttributes, pseudoStylesheet)
76
107
  }
77
108
 
78
- // taro_page 等写死在运行时里的节点,没有 _nodeInfo
79
- if (node._nodeInfo) {
80
- const overwriteStyle: TaroStyleType = node._nodeInfo?.overwriteStyle
109
+ // 初始化默认的值
110
+ if (initStyle) {
111
+ normalAttributes = ObjectAssign({}, initStyle, normalAttributes)
112
+ }
113
+ return normalAttributes
114
+ }
81
115
 
82
- // 处理覆盖属性:如动画的覆盖
83
- if (overwriteStyle) {
84
- normalAttributes = ObjectAssign(normalAttributes, overwriteStyle)
116
+ // 应用伪类样式
117
+ function getPseudoClass (node: TaroElement): HarmonyStyle | null {
118
+ // 伪类
119
+ const _pseudo_class = Object.keys(node._pseudo_class)
120
+ if (_pseudo_class.length) {
121
+ for (let i = 0; i < _pseudo_class.length; i++) {
122
+ const pseudoKey = _pseudo_class[i]
123
+ const pseudoStylesheet = node._pseudo_class[pseudoKey]
124
+ if (!pseudoStylesheet) continue
125
+ switch(pseudoKey) {
126
+ case "::first-child": {
127
+ if (node.parentNode?.firstChild?._nid === node._nid) {
128
+ return pseudoStylesheet.hmStyle
129
+ }
130
+ break
131
+ }
132
+ case "::last-child": {
133
+ if (node.parentNode?.lastChild?._nid === node._nid) {
134
+ return pseudoStylesheet.hmStyle
135
+ }
136
+ break
137
+ }
138
+ case "::empty": {
139
+ if (node.children?.length === 0) {
140
+ return pseudoStylesheet.hmStyle
141
+ }
142
+ break
143
+ }
144
+ default: {
145
+ // 解析nth-child()
146
+ // 找出当前节点在父节点中的位置
147
+ // 公式
148
+ let matchs: [number, number] | null = null
149
+ if (pseudoKey === '::nth-child(odd)') {
150
+ matchs = [2, 1]
151
+ } else if (pseudoKey === '::nth-child(even)') {
152
+ matchs = [2, 0]
153
+ } else {
154
+ matchs = parseNthChild(pseudoKey)
155
+ }
156
+ if (matchs) {
157
+ let index = node.parentNode?.childNodes.findIndex((child) => child._nid === node._nid)
158
+ if (isUndefined(index)) return null
159
+ else {
160
+ index = index + 1
161
+ if (matchs[0] === 0) {
162
+ if (index === matchs[1]) {
163
+ return pseudoStylesheet.hmStyle
164
+ }
165
+ } else {
166
+ if ((index - matchs[1]) % matchs[0] === 0) {
167
+ return pseudoStylesheet.hmStyle
168
+ }
169
+ }
170
+ }
171
+ }
172
+ }
173
+ }
85
174
  }
86
175
  }
87
- return normalAttributes
176
+ return null
88
177
  }
178
+
179
+ // 正则匹配nth-child
180
+ function parseNthChild(selector: string): [number, number] | null {
181
+ const regex = /nth-child\((?:(-?\d*)n\s*)?([+-]?\s*\d+)?\)/;
182
+ const match = selector.match(regex);
183
+
184
+ if (match) {
185
+ const a = match[1] ? parseInt(match[1]) : 0;
186
+ const b = match[2] ? parseInt(match[2]) : 0;
187
+ return [a, b];
188
+ } else {
189
+ return null;
190
+ }
191
+ }
192
+
193
+
194
+ export function getStyleAttr(node: TaroElement | null, key: string): TaroAny {
195
+ if (!node) return null
196
+ const hmStyle = node.hmStyle
197
+ if (!hmStyle) return null
198
+ // 覆盖属性
199
+ if (key === 'width') {
200
+ return isMaxWidthView(node) && isUndefined(hmStyle.width) ? '100%' : hmStyle.width
201
+ }
202
+ return hmStyle[key]
203
+ }
@@ -1,9 +1,10 @@
1
1
  import { AREA_CHANGE_EVENT_NAME, eventHandler, getComponentEventCallback, VISIBLE_CHANGE_EVENT_NAME, createTaroEvent } from '@tarojs/runtime'
2
2
 
3
+ import commonStyleModify from './style'
3
4
  import { TOUCH_EVENT_MAP } from './utils/constant/event'
4
- import { getNormalAttributes, shouldBindEvent, getNodeThresholds } from './utils/helper'
5
+ import { shouldBindEvent, getNodeThresholds } from './utils/helper'
5
6
 
6
- import type { TaroStyleType, TaroAny, TaroVideoElement, TaroEvent } from '@tarojs/runtime'
7
+ import type { TaroAny, TaroVideoElement, TaroEvent } from '@tarojs/runtime'
7
8
 
8
9
  export interface VideoOptions {
9
10
  src?: string | Resource
@@ -21,37 +22,6 @@ export interface VideoUpdateEvent {
21
22
  time: number
22
23
  }
23
24
 
24
- @Extend(Video)
25
- function attrs (style: TaroStyleType) {
26
- .id(style.id)
27
- .key(style.id)
28
- .padding(style.padding)
29
- .margin(style.margin)
30
- .width(style.width)
31
- .height(style.height)
32
- .constraintSize(style.constraintSize)
33
- .flexGrow(style.flexGrow)
34
- .flexShrink(style.flexShrink)
35
- .flexBasis(style.flexBasis)
36
- .alignSelf(style.alignSelf)
37
- .backgroundColor(style.backgroundColor)
38
- .backgroundImage(style.backgroundImage, style.backgroundRepeat)
39
- .backgroundImageSize(style.backgroundImageSize)
40
- .backgroundImagePosition(style.backgroundImagePosition)
41
- .rotate(style.rotate)
42
- .scale(style.scale)
43
- .translate(style.translate)
44
- .transform(style.transform)
45
- .borderStyle(style.borderStyle)
46
- .borderWidth(style.borderWidth)
47
- .borderColor(style.borderColor)
48
- .borderRadius(style.borderRadius)
49
- .linearGradient(style.linearGradient)
50
- .zIndex(style.zIndex)
51
- .opacity(style.opacity)
52
- .clip(style.clip)
53
- }
54
-
55
25
  @Extend(Video)
56
26
  function props(attr: VideoAttrs) {
57
27
  .muted(attr.muted)
@@ -103,30 +73,43 @@ function getVideoProps (node: TaroVideoElement): VideoAttrs {
103
73
  }
104
74
 
105
75
  function handleUpdate (node: TaroVideoElement, e: VideoUpdateEvent) {
106
- node._currentTime = e.time
76
+ node._nodeInfo._currentTime = e.time
107
77
 
108
78
  emitEvent(node, 'timeUpdate', { currentTime: e.time})
109
79
  }
110
80
 
111
- @Builder
112
- export default function TaroVideo (node: TaroVideoElement) {
113
- Video(getVideoData(node))
114
- .attrs(getNormalAttributes(node))
115
- .props(getVideoProps(node))
116
- .aspectRatio(4 / 3)
117
- .onStart(shouldBindEvent(() => { emitEvent(node, 'play') }, node, ['play']))
118
- .onPause(shouldBindEvent(() => { emitEvent(node, 'pause') }, node, ['pause']))
119
- .onFinish(shouldBindEvent(() => { emitEvent(node, 'ended') }, node, ['ended']))
120
- .onError(shouldBindEvent(() => { emitEvent(node, 'error') }, node, ['error']))
121
- .onUpdate((e) => { handleUpdate(node, e) })
122
- .onPrepared(shouldBindEvent((e: TaroAny) => { emitEvent(node, 'loadedMetaData', { duration: e.duration }) }, node, ['loadedmetadata']))
123
- .onSeeking(shouldBindEvent((e: TaroAny) => { emitEvent(node, 'seeking', { duration: e.time }) }, node, ['seeking']))
124
- .onSeeked(shouldBindEvent(() => { emitEvent(node, 'seeked') }, node, ['seeked']))
125
- .onFullscreenChange(shouldBindEvent((e: TaroAny) => { emitEvent(node, 'fullScreenChange', { fullScreen: e.fullscreen}) }, node, ['fullscreenchange']))
126
- .onClick((e: ClickEvent) => { eventHandler(e, 'click', node) })
127
- .onTouch(shouldBindEvent((e: TouchEvent) => eventHandler(e, TOUCH_EVENT_MAP.get(e.type), node), node, TOUCH_EVENT_MAP.values()))
128
- .onAreaChange(getComponentEventCallback(node, AREA_CHANGE_EVENT_NAME, (res: TaroAny) => {
129
- node._nodeInfo.areaInfo = res[1]
81
+ @Component
82
+ export default struct TaroVideo {
83
+ @Builder customBuilder() {}
84
+ @BuilderParam createLazyChildren: (node: TaroVideoElement) => void = this.customBuilder
85
+ @ObjectLink node: TaroVideoElement
86
+ @State overwriteStyle: Record<string, TaroAny> = {}
87
+
88
+ aboutToAppear(): void {
89
+ if (this.node) {
90
+ this.node._instance = this
91
+ }
92
+ }
93
+
94
+ build () {
95
+ Video(getVideoData(this.node))
96
+ .attributeModifier(commonStyleModify.setNode(this.node).setAnimationStyle(this.overwriteStyle))
97
+ .props(getVideoProps(this.node))
98
+ .onStart(shouldBindEvent(() => { emitEvent(this.node, 'play') }, this.node, ['play']))
99
+ .onPause(shouldBindEvent(() => { emitEvent(this.node, 'pause') }, this.node, ['pause']))
100
+ .onFinish(shouldBindEvent(() => { emitEvent(this.node, 'ended') }, this.node, ['ended']))
101
+ .onError(shouldBindEvent(() => { emitEvent(this.node, 'error') }, this.node, ['error']))
102
+ .onUpdate((e) => { handleUpdate(this.node, e) })
103
+ .onPrepared(shouldBindEvent((e: TaroAny) => { emitEvent(this.node, 'loadedMetaData', { duration: e.duration }) }, this.node, ['loadedmetadata']))
104
+ .onSeeking(shouldBindEvent((e: TaroAny) => { emitEvent(this.node, 'seeking', { duration: e.time }) }, this.node, ['seeking']))
105
+ .onSeeked(shouldBindEvent(() => { emitEvent(this.node, 'seeked') }, this.node, ['seeked']))
106
+ .onFullscreenChange(shouldBindEvent((e: TaroAny) => { emitEvent(this.node, 'fullScreenChange', { fullScreen: e.fullscreen}) }, this.node, ['fullscreenchange']))
107
+ .onClick((e: ClickEvent) => { eventHandler(e, 'click', this.node) })
108
+ .onTouch(shouldBindEvent((e: TouchEvent) => eventHandler(e, TOUCH_EVENT_MAP.get(e.type), this.node), this.node, TOUCH_EVENT_MAP.values()))
109
+ .onAreaChange(getComponentEventCallback(this.node, AREA_CHANGE_EVENT_NAME, (res: TaroAny) => {
110
+ this.node._nodeInfo.areaInfo = res[1]
130
111
  }))
131
- .onVisibleAreaChange(getNodeThresholds(node) || [0.0, 1.0], getComponentEventCallback(node, VISIBLE_CHANGE_EVENT_NAME))
112
+ .onVisibleAreaChange(getNodeThresholds(this.node) || [0.0, 1.0], getComponentEventCallback(this.node, VISIBLE_CHANGE_EVENT_NAME))
113
+ }
114
+
132
115
  }