@tarojs/plugin-platform-harmony-ets 4.0.0-beta.1 → 4.0.0-beta.100

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 (135) 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/network/request.ts +5 -5
  9. package/dist/apis/route/index.ts +15 -0
  10. package/dist/apis/storage/index.ts +146 -78
  11. package/dist/apis/ui/animation/animation.ts +71 -29
  12. package/dist/apis/ui/background.ts +2 -1
  13. package/dist/apis/ui/interaction/index.ts +42 -59
  14. package/dist/apis/ui/navigation-bar/index.ts +1 -1
  15. package/dist/apis/ui/pull-down-refresh.ts +9 -3
  16. package/dist/apis/ui/scroll/index.ts +5 -5
  17. package/dist/apis/ui/tab-bar.ts +3 -3
  18. package/dist/apis/utils/index.ts +2 -1
  19. package/dist/apis/wxml/IntersectionObserver.ts +18 -10
  20. package/dist/apis/wxml/index.ts +2 -0
  21. package/dist/apis/wxml/selectorQuery.ts +26 -13
  22. package/dist/components-harmony-ets/button.ets +81 -100
  23. package/dist/components-harmony-ets/canvas.ets +51 -0
  24. package/dist/components-harmony-ets/checkbox.ets +72 -177
  25. package/dist/components-harmony-ets/form.ets +54 -79
  26. package/dist/components-harmony-ets/icon.ets +33 -83
  27. package/dist/components-harmony-ets/image.ets +35 -79
  28. package/dist/components-harmony-ets/index.ets +92 -0
  29. package/dist/components-harmony-ets/innerHtml.ets +11 -6
  30. package/dist/components-harmony-ets/input.ets +65 -101
  31. package/dist/components-harmony-ets/label.ets +73 -87
  32. package/dist/components-harmony-ets/listView.ets +31 -0
  33. package/dist/components-harmony-ets/movableArea.ets +126 -0
  34. package/dist/components-harmony-ets/movableView.ets +93 -0
  35. package/dist/components-harmony-ets/navigationBar.ets +65 -0
  36. package/dist/components-harmony-ets/pageMeta.ets +94 -0
  37. package/dist/components-harmony-ets/picker.ets +44 -149
  38. package/dist/components-harmony-ets/progress.ets +52 -0
  39. package/dist/components-harmony-ets/pseudo.ets +80 -0
  40. package/dist/components-harmony-ets/radio.ets +77 -183
  41. package/dist/components-harmony-ets/richText.ets +20 -102
  42. package/dist/components-harmony-ets/scrollList.ets +103 -0
  43. package/dist/components-harmony-ets/scrollView.ets +67 -161
  44. package/dist/components-harmony-ets/slider.ets +22 -82
  45. package/dist/components-harmony-ets/stickySection.ets +42 -0
  46. package/dist/components-harmony-ets/style.ets +405 -0
  47. package/dist/components-harmony-ets/swiper.ets +64 -87
  48. package/dist/components-harmony-ets/switch.ets +39 -99
  49. package/dist/components-harmony-ets/{index.ts → tag.ts} +6 -0
  50. package/dist/components-harmony-ets/text.ets +111 -113
  51. package/dist/components-harmony-ets/textArea.ets +51 -95
  52. package/dist/components-harmony-ets/utils/AttributeManager.ets +2 -2
  53. package/dist/components-harmony-ets/utils/DynamicCenter.ts +2 -2
  54. package/dist/components-harmony-ets/utils/flexManager.ets +75 -7
  55. package/dist/components-harmony-ets/utils/helper.ets +19 -7
  56. package/dist/components-harmony-ets/utils/htmlParser/HarmonyHTMLParser.ts +1 -2
  57. package/dist/components-harmony-ets/utils/htmlParser/index.ts +1 -1
  58. package/dist/components-harmony-ets/utils/index.ts +54 -50
  59. package/dist/components-harmony-ets/utils/styles.ets +175 -87
  60. package/dist/components-harmony-ets/video.ets +37 -88
  61. package/dist/components-harmony-ets/view.ets +63 -162
  62. package/dist/components-harmony-ets/webView.ets +56 -0
  63. package/dist/index.d.ts +152 -0
  64. package/dist/index.js +184 -55
  65. package/dist/index.js.map +1 -1
  66. package/dist/runtime-ets/bom/document.ts +6 -4
  67. package/dist/runtime-ets/bom/getComputedStyle.ts +2 -2
  68. package/dist/runtime-ets/bom/window.ts +9 -2
  69. package/dist/runtime-ets/current.ts +5 -1
  70. package/dist/runtime-ets/dom/bind.ts +28 -12
  71. package/dist/runtime-ets/dom/class-list.ts +2 -2
  72. package/dist/runtime-ets/dom/cssNesting.ts +419 -0
  73. package/dist/runtime-ets/dom/cssStyleDeclaration.ts +29 -23
  74. package/dist/runtime-ets/dom/document.ts +22 -8
  75. package/dist/runtime-ets/dom/element/canvas.ts +136 -0
  76. package/dist/runtime-ets/dom/element/element.ts +348 -57
  77. package/dist/runtime-ets/dom/element/form.ts +31 -26
  78. package/dist/runtime-ets/dom/element/index.ts +33 -2
  79. package/dist/runtime-ets/dom/element/movableArea.ts +11 -0
  80. package/dist/runtime-ets/dom/element/movableView.ts +244 -0
  81. package/dist/runtime-ets/dom/element/normal.ts +36 -8
  82. package/dist/runtime-ets/dom/element/progress.ts +11 -0
  83. package/dist/runtime-ets/dom/element/scrollView.ts +1 -0
  84. package/dist/runtime-ets/dom/element/text.ts +1 -8
  85. package/dist/runtime-ets/dom/element/video.ts +5 -4
  86. package/dist/runtime-ets/dom/element/webView.ts +68 -0
  87. package/dist/runtime-ets/dom/event.ts +2 -4
  88. package/dist/runtime-ets/dom/eventTarget.ts +2 -3
  89. package/dist/runtime-ets/dom/node.ts +64 -32
  90. package/dist/runtime-ets/dom/stylesheet/covertWeb2Hm.ts +418 -164
  91. package/dist/runtime-ets/dom/stylesheet/index.ts +29 -311
  92. package/dist/runtime-ets/dom/stylesheet/type.ts +51 -9
  93. package/dist/runtime-ets/dom/stylesheet/util.ts +36 -28
  94. package/dist/runtime-ets/index.ts +2 -2
  95. package/dist/runtime-ets/interface/event.ts +1 -1
  96. package/dist/runtime-ets/utils/index.ts +77 -13
  97. package/dist/runtime-ets/utils/info.ts +2 -2
  98. package/dist/runtime-framework/react/app.ts +23 -28
  99. package/dist/runtime-framework/react/hooks.ts +3 -4
  100. package/dist/runtime-framework/react/index.ts +1 -2
  101. package/dist/runtime-framework/react/native-page.ts +421 -0
  102. package/dist/runtime-framework/react/page.ts +5 -10
  103. package/dist/runtime-framework/react/utils/index.ts +3 -3
  104. package/dist/runtime-framework/solid/app.ts +25 -45
  105. package/dist/runtime-framework/solid/connect.ts +21 -3
  106. package/dist/runtime-framework/solid/hooks.ts +17 -12
  107. package/dist/runtime-framework/solid/index.ts +6 -2
  108. package/dist/runtime-framework/solid/page.ts +85 -31
  109. package/dist/runtime-framework/solid/reconciler/props.ts +70 -25
  110. package/dist/runtime-framework/solid/reconciler/render.ts +16 -6
  111. package/dist/runtime-framework/solid/reconciler/use.ts +0 -1
  112. package/dist/runtime-framework/solid/utils/index.ts +3 -5
  113. package/dist/runtime-utils.d.ts +827 -0
  114. package/dist/runtime-utils.js +463 -225
  115. package/dist/runtime-utils.js.map +1 -1
  116. package/dist/runtime.d.ts +1 -0
  117. package/dist/runtime.js +463 -225
  118. package/dist/runtime.js.map +1 -1
  119. package/index.js +3 -1
  120. package/package.json +14 -15
  121. package/static/media/cancel.svg +1 -1
  122. package/static/media/circle.svg +1 -1
  123. package/static/media/clear.svg +1 -1
  124. package/static/media/download.svg +1 -1
  125. package/static/media/info.svg +1 -1
  126. package/static/media/info_circle.svg +1 -1
  127. package/static/media/search.svg +1 -1
  128. package/static/media/success.svg +1 -1
  129. package/static/media/success_no_circle.svg +1 -1
  130. package/static/media/warn.svg +1 -1
  131. package/types/harmony.d.ts +4 -0
  132. package/types/index.d.ts +4 -0
  133. package/types/runtime.d.ts +3 -1
  134. package/dist/runtime-ets/utils/bind.ts +0 -24
  135. /package/dist/runtime-framework/solid/{contant.ts → constant.ts} +0 -0
@@ -1,21 +1,89 @@
1
- import type { TaroAny, TaroElement } from '@tarojs/runtime'
1
+ import type { TaroElement, HarmonyStyle } from '@tarojs/runtime'
2
+ import { isUndefined } from '@tarojs/shared'
3
+ import { getNormalAttributes } from './styles'
4
+
5
+ interface IFlexOptions {
6
+ direction: FlexDirection,
7
+ justifyContent: FlexAlign,
8
+ alignItems: ItemAlign,
9
+ wrap?: FlexWrap,
10
+ alignContent?: FlexAlign
11
+ }
2
12
 
3
13
  class FlexManager {
4
- static flexOptions (node: TaroElement): FlexOptions {
5
- const hmStyle = node.hmStyle
14
+ static isFlexNode (node: TaroElement): boolean {
15
+ return !!node.hmStyle?.display?.includes('flex')
16
+ }
17
+
18
+ static convertFlexAlignItemsToColumnOrRow (direction: FlexDirection, alignItems: ItemAlign | undefined): VerticalAlign | HorizontalAlign {
19
+ if (direction === FlexDirection.Column) {
20
+ switch (alignItems) {
21
+ case ItemAlign.Center:
22
+ return HorizontalAlign.Center
23
+ case ItemAlign.End:
24
+ return HorizontalAlign.End
25
+ default:
26
+ return HorizontalAlign.Start
27
+ }
28
+ } else {
29
+ switch (alignItems) {
30
+ case ItemAlign.Center:
31
+ return VerticalAlign.Center
32
+ case ItemAlign.End:
33
+ return VerticalAlign.Bottom
34
+ default:
35
+ return VerticalAlign.Top
36
+ }
37
+ }
38
+ }
6
39
 
40
+ static useFlexLayout (node: TaroElement): boolean {
41
+ const hmStyle: HarmonyStyle = getNormalAttributes(node) || {}
42
+ const isReverse = hmStyle.flexDirection && [FlexDirection.RowReverse, FlexDirection.ColumnReverse].indexOf(hmStyle.flexDirection) !== -1;
43
+ const isUnknownAlign = [ItemAlign.Stretch, ItemAlign.Baseline].indexOf(hmStyle.alignItems!) !== -1;
44
+ return !isUndefined(hmStyle.flexWrap) || isReverse || isUnknownAlign;
45
+ }
46
+
47
+ static flexOptions (node: TaroElement): IFlexOptions {
48
+ const hmStyle: HarmonyStyle = getNormalAttributes(node) || {}
49
+ const isFlex = FlexManager.isFlexNode(node)
7
50
  let flexDirection = hmStyle.flexDirection
8
51
  if (!flexDirection && flexDirection !== 0) {
9
52
  flexDirection = hmStyle.display === 'flex' ? FlexDirection.Row : FlexDirection.Column
10
53
  }
11
54
 
12
55
  return {
56
+ alignItems: isFlex ? (isUndefined(hmStyle.alignItems) ? ItemAlign.Start : hmStyle.alignItems) : ItemAlign.Start,
57
+ justifyContent: isFlex ? (isUndefined(hmStyle.justifyContent) ? FlexAlign.Start : hmStyle.justifyContent) : FlexAlign.Start,
13
58
  direction: flexDirection,
14
- justifyContent: hmStyle.justifyContent,
15
- alignItems: hmStyle.alignItems,
16
- wrap: hmStyle.flexWrap,
17
- alignContent: hmStyle.alignContent,
59
+ wrap: isFlex ? hmStyle.flexWrap: FlexWrap.NoWrap,
60
+ alignContent: isFlex ? hmStyle.alignContent: FlexAlign.Start
61
+ }
62
+ }
63
+
64
+ static direction (node: TaroElement): FlexDirection {
65
+ const hmStyle: HarmonyStyle = getNormalAttributes(node)
66
+ let flexDirection = hmStyle.flexDirection
67
+ if (!flexDirection && flexDirection !== 0) {
68
+ flexDirection = hmStyle.display === 'flex' ? FlexDirection.Row : FlexDirection.Column
18
69
  }
70
+ return flexDirection
71
+ }
72
+
73
+ static alignItems<T> (node: TaroElement): T {
74
+ const hmStyle: HarmonyStyle = getNormalAttributes(node)
75
+ const isFlex = FlexManager.isFlexNode(node)
76
+ let flexDirection = hmStyle.flexDirection
77
+ if (!flexDirection && flexDirection !== 0) {
78
+ flexDirection = hmStyle.display === 'flex' ? FlexDirection.Row : FlexDirection.Column
79
+ }
80
+ return (isFlex ? FlexManager.convertFlexAlignItemsToColumnOrRow(flexDirection, hmStyle.alignItems) : HorizontalAlign.Start) as T
81
+ }
82
+
83
+ static justifyContent (node: TaroElement): FlexAlign {
84
+ const hmStyle: HarmonyStyle = getNormalAttributes(node)
85
+ const isFlex = FlexManager.isFlexNode(node)
86
+ return isFlex ? (isUndefined(hmStyle.justifyContent) ? FlexAlign.Start : hmStyle.justifyContent) : FlexAlign.Start
19
87
  }
20
88
  }
21
89
 
@@ -1,7 +1,8 @@
1
- import { getNormalAttributes, getFontAttributes } from './styles'
1
+ import { VIEW } from '@tarojs/runtime/dist/runtime.esm'
2
2
 
3
- import type { Func } from '@tarojs/runtime/dist/runtime.esm'
4
- import type { TaroElement } from '@tarojs/runtime'
3
+ import { getNormalAttributes, getFontAttributes, getStyleAttr } from './styles'
4
+
5
+ import type { TaroElement, TaroNode, TaroAny } from '@tarojs/runtime'
5
6
 
6
7
  export const parseStyles = (styles = ''): Record<string, string> => {
7
8
  const styleObj: Record<string, string> = {}
@@ -20,7 +21,7 @@ export const parseStyles = (styles = ''): Record<string, string> => {
20
21
  return styleObj
21
22
  }
22
23
 
23
- export function shouldBindEvent (cb: Func, node: TaroElement, eventNames: string[], disabled = false) {
24
+ export function shouldBindEvent (cb: TaroAny, node: TaroElement | null, eventNames: string[], disabled = false): TaroAny {
24
25
  if (!node || node._attrs?.disabled || disabled) return null
25
26
  if (!node.__listeners) {
26
27
  return null
@@ -31,9 +32,20 @@ export function shouldBindEvent (cb: Func, node: TaroElement, eventNames: string
31
32
  }
32
33
 
33
34
  export function getNodeThresholds (node: TaroElement): number[] | null {
34
- if (!node) return null
35
+ return node?._nodeInfo?.thresholds || null
36
+ }
35
37
 
36
- return node?._nodeInfo.thresholds || null
38
+ export function isTagFirstChild (node: TaroElement, tagName = VIEW, level = 0): boolean {
39
+ const parent: TaroElement | null = node.parentElement
40
+ const list: TaroNode[] = node.parentNode?.childNodes || []
41
+ if (list.length < 1 || level < 0) return false
42
+ else if (!parent) return true
43
+
44
+ if (parent.nodeName === tagName.toUpperCase()) {
45
+ return list[0] === node
46
+ } else {
47
+ return (list[0] === node) && isTagFirstChild(parent, tagName, --level)
48
+ }
37
49
  }
38
50
 
39
- export { getNormalAttributes, getFontAttributes }
51
+ export { getNormalAttributes, getFontAttributes, getStyleAttr }
@@ -99,5 +99,4 @@ function parseTag(htmlString, startIndex) {
99
99
  return tag
100
100
  }
101
101
 
102
-
103
- export default parseHTML
102
+ export default parseHTML
@@ -15,7 +15,7 @@ function buildDomTree(dom: TTreeNode) {
15
15
  case 'span': tagName = 'text'; break
16
16
  }
17
17
  if (!tagName) return null
18
-
18
+
19
19
  const ele = document.createElement(tagName)
20
20
  attributes && Object.keys(attributes).forEach(key => {
21
21
  if (key === 'style') {
@@ -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,115 +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
6
  import type { StandardProps, TextProps } from '@tarojs/components/types'
6
- import type { TaroAny, TaroElement, TaroStyleType, TaroTextStyleType, HarmonyType, HarmonyStyle } from '@tarojs/runtime'
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)
10
11
  const attrs: TextProps = node._attrs || {}
11
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
+ }
70
+
12
71
  const attributes: TaroAny = {
13
- WebkitLineClamp: attrs.maxLines || hmStyle.WebkitLineClamp || Infinity,
72
+ textAlign,
73
+ verticalAlign,
74
+ WebkitLineClamp: WebkitLineClamp,
14
75
  // 已做处理的属性
15
76
  letterSpacing: hmStyle.letterSpacing,
16
- textAlign: hmStyle.textAlign,
17
- textOverflow: hmStyle.textOverflow
77
+ textOverflow: hmStyle.textOverflow,
78
+ lineHeight: lineHeight
18
79
  }
19
80
 
20
81
  return attributes
21
82
  }
22
83
 
23
- 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 {
24
92
  const hmStyle = node.hmStyle
93
+
94
+ if (!hmStyle) return {}
25
95
 
26
96
  const _nid = node._nid
27
97
  const _attrs: StandardProps = node._attrs || {}
28
98
 
29
- let linearGradient: HarmonyType.LinearGradient | undefined = undefined
30
- // 渐变
31
- if (hmStyle.backgroundImage?.colors) {
32
- linearGradient = hmStyle.backgroundImage
33
- }
99
+ let normalAttributes = hmStyle
34
100
 
35
- let normalAttributes: HarmonyStyle = {
36
- // Flex相关
37
- flexBasis: hmStyle.flexBasis,
38
- flexGrow: hmStyle.flexGrow,
39
- flexShrink: hmStyle.flexShrink,
40
- alignSelf: hmStyle.alignSelf,
41
- // 尺寸相关
42
- width: hmStyle.width,
43
- height: hmStyle.height,
44
- minHeight: hmStyle.minHeight,
45
- maxHeight: hmStyle.maxHeight,
46
- minWidth: hmStyle.minWidth,
47
- maxWidth: hmStyle.maxWidth,
48
- marginTop: hmStyle.marginTop,
49
- marginRight: hmStyle.marginRight,
50
- marginBottom: hmStyle.marginBottom,
51
- marginLeft: hmStyle.marginLeft,
52
- paddingTop: hmStyle.paddingTop,
53
- paddingRight: hmStyle.paddingRight,
54
- paddingBottom: hmStyle.paddingBottom,
55
- paddingLeft: hmStyle.paddingLeft,
56
- // 边框相关
57
- borderStyle: hmStyle.borderStyle,
58
- borderTopStyle: hmStyle.borderTopStyle,
59
- borderRightStyle: hmStyle.borderRightStyle,
60
- borderBottomStyle: hmStyle.borderBottomStyle,
61
- borderLeftStyle: hmStyle.borderLeftStyle,
62
- borderWidth: hmStyle.borderWidth,
63
- borderTopWidth: hmStyle.borderTopWidth,
64
- borderRightWidth: hmStyle.borderRightWidth,
65
- borderBottomWidth: hmStyle.borderBottomWidth,
66
- borderLeftWidth: hmStyle.borderLeftWidth,
67
- borderColor: hmStyle.borderColor,
68
- borderTopColor: hmStyle.borderTopColor,
69
- borderRightColor: hmStyle.borderRightColor,
70
- borderBottomColor: hmStyle.borderBottomColor,
71
- borderLeftColor: hmStyle.borderLeftColor,
72
- borderRadius: hmStyle.borderRadius,
73
- borderTopLeftRadius: hmStyle.borderTopLeftRadius,
74
- borderTopRightRadius: hmStyle.borderTopRightRadius,
75
- borderBottomRightRadius: hmStyle.borderBottomRightRadius,
76
- borderBottomLeftRadius: hmStyle.borderBottomLeftRadius,
77
- // 背景相关
78
- backgroundColor: hmStyle.backgroundColor,
79
- backgroundImage: hmStyle.backgroundImage,
80
- backgroundSize: hmStyle.backgroundSize,
81
- backgroundRepeat: hmStyle.backgroundRepeat,
82
- backgroundPosition: hmStyle.backgroundPosition,
83
- linearGradient,
84
- // 变换相关
85
- transform: hmStyle.transform,
86
- transformOrigin: {
87
- x: hmStyle.transformOrigin?.x || 0,
88
- y: hmStyle.transformOrigin?.y || 0
89
- },
90
- // 通用文本相关
91
- color: hmStyle.color,
92
- fontSize: hmStyle.fontSize,
93
- fontStyle: hmStyle.fontStyle,
94
- fontWeight: hmStyle.fontWeight,
95
- fontFamily: hmStyle.fontFamily || TEXT_DEFAULT_STYLE.FONT_FAMILY,
96
- lineHeight: hmStyle.lineHeight,
97
- textDecoration: hmStyle.textDecoration,
98
- // 其他
99
- overflow: hmStyle.overflow,
100
- id: _attrs.id || _nid,
101
- opacity: hmStyle.opacity,
102
- // focus: _attrs.focus || false,
101
+ // 覆盖属性
102
+ normalAttributes.id = _attrs.id || _nid.toString()
103
+
104
+ let pseudoStylesheet = getPseudoClass(node)
105
+ if (pseudoStylesheet) {
106
+ normalAttributes = ObjectAssign({}, normalAttributes, pseudoStylesheet)
103
107
  }
104
108
 
105
- // taro_page 等写死在运行时里的节点,没有 _nodeInfo
106
- if (node._nodeInfo) {
107
- const overwriteStyle: TaroStyleType = node._nodeInfo?.overwriteStyle
109
+ // 初始化默认的值
110
+ if (initStyle) {
111
+ normalAttributes = ObjectAssign({}, initStyle, normalAttributes)
112
+ }
113
+ return normalAttributes
114
+ }
108
115
 
109
- // 处理覆盖属性:如动画的覆盖
110
- if (overwriteStyle) {
111
- 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
+ }
112
174
  }
113
175
  }
114
- return normalAttributes
176
+ return null
115
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
+ }