base-screen 0.3.0 → 0.4.0

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 (210) hide show
  1. package/dist/_internal/contracts/document/schema.d.ts +7 -0
  2. package/dist/_internal/contracts/document/schema.js +1 -1
  3. package/dist/_internal/contracts/document/validate-value-ref.js +6 -1
  4. package/dist/_internal/contracts/screen-document.schema.json +15 -3
  5. package/dist/_internal/contracts/types/diagnostics.d.ts +1 -1
  6. package/dist/_internal/contracts/types/document.d.ts +3 -1
  7. package/dist/_internal/core/blueprint/runtime-simulate.d.ts +57 -0
  8. package/dist/_internal/core/blueprint/runtime-simulate.js +177 -0
  9. package/dist/_internal/core/blueprint/runtime.d.ts +1 -0
  10. package/dist/_internal/core/blueprint/runtime.js +1 -0
  11. package/dist/_internal/core/commands/apply-collection-commands.d.ts +32 -0
  12. package/dist/_internal/core/commands/apply-collection-commands.js +174 -0
  13. package/dist/_internal/core/commands/apply.js +13 -0
  14. package/dist/_internal/core/commands/parse-command.js +12 -0
  15. package/dist/_internal/core/commands/types.d.ts +29 -1
  16. package/dist/_internal/core/data/data-candidates.d.ts +38 -0
  17. package/dist/_internal/core/data/data-candidates.js +186 -0
  18. package/dist/_internal/core/data/dataset-profile.d.ts +71 -0
  19. package/dist/_internal/core/data/dataset-profile.js +170 -0
  20. package/dist/_internal/core/data/sample-values.d.ts +25 -0
  21. package/dist/_internal/core/data/sample-values.js +45 -0
  22. package/dist/_internal/core/diff/screen-diff.js +6 -0
  23. package/dist/_internal/core/index.d.ts +7 -1
  24. package/dist/_internal/core/index.js +7 -1
  25. package/dist/_internal/core/state/state-preset.d.ts +32 -0
  26. package/dist/_internal/core/state/state-preset.js +37 -0
  27. package/dist/_internal/core/validation/layout-audit.d.ts +17 -0
  28. package/dist/_internal/core/validation/layout-audit.js +228 -0
  29. package/dist/_internal/core/validation/legibility.d.ts +9 -0
  30. package/dist/_internal/core/validation/legibility.js +33 -0
  31. package/dist/_internal/engine/browser/controllers/dataset/designer-dataset-draft-controller.d.ts +6 -1
  32. package/dist/_internal/engine/browser/controllers/designer-change-controller.d.ts +27 -0
  33. package/dist/_internal/engine/browser/dom/change/designer-change-dom.d.ts +21 -5
  34. package/dist/_internal/engine/browser/dom/dataset/designer-data-section-diff.d.ts +4 -6
  35. package/dist/_internal/engine/browser/dom/value-ref/designer-value-ref-types.d.ts +2 -0
  36. package/dist/_internal/engine/browser/viewport/designer-canvas-aids-types.d.ts +3 -3
  37. package/dist/_internal/engine/browser/viewport/designer-canvas-aids.d.ts +5 -3
  38. package/dist/_internal/engine/browser/viewport/designer-canvas-measurements.d.ts +2 -14
  39. package/dist/_internal/engine/browser/viewport/designer-guide-overlay-nodes.d.ts +2 -0
  40. package/dist/_internal/engine/browser/viewport/designer-snap-tuning.d.ts +14 -0
  41. package/dist/_internal/engine/common/operations/change/designer-change-events.d.ts +2 -0
  42. package/dist/_internal/engine/common/operations/change/designer-change-proposal.d.ts +35 -2
  43. package/dist/_internal/engine/common/operations/display/designer-target-display.d.ts +2 -2
  44. package/dist/_internal/engine/common/operations/style/designer-component-states.d.ts +7 -2
  45. package/dist/_internal/engine/common/ui-text/designer-ui-text.d.ts +42 -2
  46. package/dist/_internal/engine/{designer-blueprint-workbench-panel-CJ3iZo5v.js → designer-blueprint-workbench-panel-BWSglfAb.js} +210 -290
  47. package/dist/_internal/engine/{designer-dataset-controller-CrfOg2Ys.js → designer-dataset-controller-DdbY4ILC.js} +619 -561
  48. package/dist/_internal/engine/designer-element/api/designer-element-change.d.ts +24 -0
  49. package/dist/_internal/engine/designer-element/base-screen-designer-element.d.ts +12 -0
  50. package/dist/_internal/engine/designer-element/designer-element-public-api.d.ts +2 -1
  51. package/dist/_internal/engine/designer-element/runtime/designer-element-rendering.d.ts +1 -1
  52. package/dist/_internal/engine/designer-element/runtime/designer-element-runtime.d.ts +4 -1
  53. package/dist/_internal/engine/designer-element/runtime/designer-element-sample-data.d.ts +23 -0
  54. package/dist/_internal/engine/{designer-field-primitives-KBZuWN_D.js → designer-field-primitives-Crn464Qg.js} +44 -3
  55. package/dist/_internal/engine/{designer-left-panel-groups-dom-N2OHOWjl.js → designer-left-panel-groups-dom-DFdLWxxY.js} +2 -2
  56. package/dist/_internal/engine/{host-resource-selection-xo2mLCD4.js → host-resource-selection-CWrPLVOc.js} +2 -2
  57. package/dist/_internal/engine/index.js +4742 -4360
  58. package/dist/_internal/host-kit/index.d.ts +1 -0
  59. package/dist/_internal/host-kit/index.js +1 -0
  60. package/dist/_internal/host-kit/intent/index.d.ts +16 -0
  61. package/dist/_internal/host-kit/intent/index.js +13 -0
  62. package/dist/_internal/host-kit/intent/intent-behavior.d.ts +59 -0
  63. package/dist/_internal/host-kit/intent/intent-behavior.js +812 -0
  64. package/dist/_internal/host-kit/intent/intent-block.d.ts +59 -0
  65. package/dist/_internal/host-kit/intent/intent-block.js +359 -0
  66. package/dist/_internal/host-kit/intent/intent-catalog.d.ts +105 -0
  67. package/dist/_internal/host-kit/intent/intent-catalog.js +60 -0
  68. package/dist/_internal/host-kit/intent/intent-compile-types.d.ts +222 -0
  69. package/dist/_internal/host-kit/intent/intent-compile-types.js +10 -0
  70. package/dist/_internal/host-kit/intent/intent-compile.d.ts +35 -0
  71. package/dist/_internal/host-kit/intent/intent-compile.js +465 -0
  72. package/dist/_internal/host-kit/intent/intent-data-check.d.ts +50 -0
  73. package/dist/_internal/host-kit/intent/intent-data-check.js +163 -0
  74. package/dist/_internal/host-kit/intent/intent-data-context.d.ts +53 -0
  75. package/dist/_internal/host-kit/intent/intent-data-context.js +200 -0
  76. package/dist/_internal/host-kit/intent/intent-dataset.d.ts +52 -0
  77. package/dist/_internal/host-kit/intent/intent-dataset.js +125 -0
  78. package/dist/_internal/host-kit/intent/intent-format.d.ts +33 -0
  79. package/dist/_internal/host-kit/intent/intent-format.js +94 -0
  80. package/dist/_internal/host-kit/intent/intent-kpi-bind.d.ts +29 -0
  81. package/dist/_internal/host-kit/intent/intent-kpi-bind.js +59 -0
  82. package/dist/_internal/host-kit/intent/intent-layout.d.ts +102 -0
  83. package/dist/_internal/host-kit/intent/intent-layout.js +264 -0
  84. package/dist/_internal/host-kit/intent/intent-parse.d.ts +39 -0
  85. package/dist/_internal/host-kit/intent/intent-parse.js +930 -0
  86. package/dist/_internal/host-kit/intent/intent-prepare.d.ts +52 -0
  87. package/dist/_internal/host-kit/intent/intent-prepare.js +104 -0
  88. package/dist/_internal/host-kit/intent/intent-session.d.ts +202 -0
  89. package/dist/_internal/host-kit/intent/intent-session.js +1001 -0
  90. package/dist/_internal/host-kit/intent/intent-stream.d.ts +28 -0
  91. package/dist/_internal/host-kit/intent/intent-stream.js +183 -0
  92. package/dist/_internal/host-kit/intent/intent-style.d.ts +37 -0
  93. package/dist/_internal/host-kit/intent/intent-style.js +77 -0
  94. package/dist/_internal/host-kit/intent/intent-types.d.ts +234 -0
  95. package/dist/_internal/host-kit/intent/intent-types.js +10 -0
  96. package/dist/_internal/material/chart.js +34 -12
  97. package/dist/_internal/material/table.js +52 -21
  98. package/dist/_internal/plugin-sdk/index.d.ts +6 -0
  99. package/dist/_internal/plugin-sdk/index.js +6 -0
  100. package/dist/_internal/plugin-sdk/material-isolated-csp.d.ts +38 -0
  101. package/dist/_internal/plugin-sdk/material-isolated-csp.js +67 -0
  102. package/dist/_internal/plugin-sdk/material-isolated-runtime.d.ts +22 -0
  103. package/dist/_internal/plugin-sdk/material-isolated-runtime.js +262 -0
  104. package/dist/_internal/plugin-sdk/material-isolated.d.ts +94 -0
  105. package/dist/_internal/plugin-sdk/material-isolated.js +1024 -0
  106. package/dist/_internal/plugin-sdk/material-rendered-element.d.ts +10 -2
  107. package/dist/_internal/plugin-sdk/material-rendered-element.js +71 -7
  108. package/dist/_internal/plugin-sdk/material-spec-audit.d.ts +2 -0
  109. package/dist/_internal/plugin-sdk/material-spec-audit.js +26 -6
  110. package/dist/_internal/plugin-sdk/material-spec.d.ts +20 -0
  111. package/dist/_internal/plugin-sdk/material-spec.js +78 -3
  112. package/dist/_internal/plugin-sdk/material-template-audit.d.ts +12 -0
  113. package/dist/_internal/plugin-sdk/material-template-audit.js +302 -0
  114. package/dist/_internal/plugin-sdk/material-template-expression.d.ts +98 -0
  115. package/dist/_internal/plugin-sdk/material-template-expression.js +549 -0
  116. package/dist/_internal/plugin-sdk/material-template-render.d.ts +32 -0
  117. package/dist/_internal/plugin-sdk/material-template-render.js +579 -0
  118. package/dist/_internal/plugin-sdk/material-template.d.ts +185 -0
  119. package/dist/_internal/plugin-sdk/material-template.js +879 -0
  120. package/dist/auto-register.js +10 -10
  121. package/dist/contracts.js +5 -5
  122. package/dist/contrast-DRPezUIl.js +131 -0
  123. package/dist/contrast-DRPezUIl.js.map +1 -0
  124. package/dist/core.js +156 -136
  125. package/dist/core.js.map +1 -1
  126. package/dist/dataset-profile-Dk6xdtsM.js +261 -0
  127. package/dist/dataset-profile-Dk6xdtsM.js.map +1 -0
  128. package/dist/{designer-3Tb0EIDV.js → designer-CFOvOmLn.js} +2 -2
  129. package/dist/{designer-3Tb0EIDV.js.map → designer-CFOvOmLn.js.map} +1 -1
  130. package/dist/designer-auto-register.js +5 -5
  131. package/dist/{designer-blueprint-workbench-panel-CJ3iZo5v-BebifP4x.js → designer-blueprint-workbench-panel-BWSglfAb-ktaj6Ci9.js} +452 -531
  132. package/dist/designer-blueprint-workbench-panel-BWSglfAb-ktaj6Ci9.js.map +1 -0
  133. package/dist/{designer-dataset-controller-CrfOg2Ys-DhKUEJJe.js → designer-dataset-controller-DdbY4ILC-CMdGdGbr.js} +862 -804
  134. package/dist/designer-dataset-controller-DdbY4ILC-CMdGdGbr.js.map +1 -0
  135. package/dist/{designer-field-primitives-KBZuWN_D-B3h4jpYe.js → designer-field-primitives-Crn464Qg-Bi7q8s5J.js} +45 -4
  136. package/dist/designer-field-primitives-Crn464Qg-Bi7q8s5J.js.map +1 -0
  137. package/dist/{designer-left-panel-groups-dom-N2OHOWjl-D8CzGwbF.js → designer-left-panel-groups-dom-DFdLWxxY-BarlWMLY.js} +4 -4
  138. package/dist/{designer-left-panel-groups-dom-N2OHOWjl-D8CzGwbF.js.map → designer-left-panel-groups-dom-DFdLWxxY-BarlWMLY.js.map} +1 -1
  139. package/dist/designer.js +5 -5
  140. package/dist/{dist-6GRboBs-.js → dist-B2bW2Tru.js} +4 -4
  141. package/dist/{dist-6GRboBs-.js.map → dist-B2bW2Tru.js.map} +1 -1
  142. package/dist/{dist-BjLjFUy1.js → dist-CHK9BTXX.js} +3833 -3453
  143. package/dist/dist-CHK9BTXX.js.map +1 -0
  144. package/dist/engine.js +5 -5
  145. package/dist/{fragment-Y6YnW6wy.js → fragment-B-oP0Fru.js} +3 -3
  146. package/dist/{fragment-Y6YnW6wy.js.map → fragment-B-oP0Fru.js.map} +1 -1
  147. package/dist/host-kit.js +3963 -187
  148. package/dist/host-kit.js.map +1 -1
  149. package/dist/{host-resource-selection-xo2mLCD4-BR5XCcim.js → host-resource-selection-CWrPLVOc-DPDq3jau.js} +5 -5
  150. package/dist/{host-resource-selection-xo2mLCD4-BR5XCcim.js.map → host-resource-selection-CWrPLVOc-DPDq3jau.js.map} +1 -1
  151. package/dist/index.js +10 -10
  152. package/dist/legibility-CS28ivEz.js +12 -0
  153. package/dist/legibility-CS28ivEz.js.map +1 -0
  154. package/dist/{level-scale-BzUjE31V.js → level-scale-CwHqFEVd.js} +2 -2
  155. package/dist/{level-scale-BzUjE31V.js.map → level-scale-CwHqFEVd.js.map} +1 -1
  156. package/dist/{manager-Ch0RTu_i.js → manager-CeB4Jnr9.js} +3 -3
  157. package/dist/{manager-Ch0RTu_i.js.map → manager-CeB4Jnr9.js.map} +1 -1
  158. package/dist/{material-UbSfiuZq.js → material-DeVkzSoT.js} +2 -2
  159. package/dist/{material-UbSfiuZq.js.map → material-DeVkzSoT.js.map} +1 -1
  160. package/dist/material-auto-register.js +1 -1
  161. package/dist/material.js +2 -2
  162. package/dist/{migrate-document-D3HhCVlZ.js → migrate-document-BM888PtC.js} +2 -2
  163. package/dist/{migrate-document-D3HhCVlZ.js.map → migrate-document-BM888PtC.js.map} +1 -1
  164. package/dist/{parse-AOTE9WnD.js → parse-Ji7kE9ZT.js} +9 -4
  165. package/dist/parse-Ji7kE9ZT.js.map +1 -0
  166. package/dist/{parse-B1OD1teS.js → parse-aZat9tAS.js} +3 -3
  167. package/dist/{parse-B1OD1teS.js.map → parse-aZat9tAS.js.map} +1 -1
  168. package/dist/{parse-parts-BNrpdG5o.js → parse-parts-qzl4suzk.js} +2 -2
  169. package/dist/{parse-parts-BNrpdG5o.js.map → parse-parts-qzl4suzk.js.map} +1 -1
  170. package/dist/{plugin-DHgh9uHh.js → plugin-CstHQ5ws.js} +4 -4
  171. package/dist/{plugin-DHgh9uHh.js.map → plugin-CstHQ5ws.js.map} +1 -1
  172. package/dist/plugin-core.js +2 -2
  173. package/dist/plugin-manager.js +1 -1
  174. package/dist/plugin-package-DWifuHi1.js +4206 -0
  175. package/dist/plugin-package-DWifuHi1.js.map +1 -0
  176. package/dist/{plugin-registration-rsmb4GvS.js → plugin-registration-DTwBvnHX.js} +2 -2
  177. package/dist/{plugin-registration-rsmb4GvS.js.map → plugin-registration-DTwBvnHX.js.map} +1 -1
  178. package/dist/plugin-sdk.js +6 -6
  179. package/dist/{plugins-DGRHgREK.js → plugins-QDL0yIql.js} +2 -2
  180. package/dist/{plugins-DGRHgREK.js.map → plugins-QDL0yIql.js.map} +1 -1
  181. package/dist/plugins.js +8 -8
  182. package/dist/renderer.js +1 -1
  183. package/dist/{runtime-dispatch-BfnRDtMe.js → runtime-dispatch-C0gyIrTv.js} +2 -2
  184. package/dist/{runtime-dispatch-BfnRDtMe.js.map → runtime-dispatch-C0gyIrTv.js.map} +1 -1
  185. package/dist/runtime-simulate-aSYO9yDX.js +147 -0
  186. package/dist/runtime-simulate-aSYO9yDX.js.map +1 -0
  187. package/dist/{src-Bm7u11mN-_0u6cMEw.js → src-Bm7u11mN-Bi1hDVkh.js} +39 -39
  188. package/dist/{src-Bm7u11mN-_0u6cMEw.js.map → src-Bm7u11mN-Bi1hDVkh.js.map} +1 -1
  189. package/dist/state-preset-DtpygJOH.js +22 -0
  190. package/dist/state-preset-DtpygJOH.js.map +1 -0
  191. package/dist/{text-BNksUr5J.js → text-BSTz0pFQ.js} +87 -34
  192. package/dist/text-BSTz0pFQ.js.map +1 -0
  193. package/dist/{value-ref-BG_MV9C-.js → value-ref-XkLuckGV.js} +2 -2
  194. package/dist/{value-ref-BG_MV9C-.js.map → value-ref-XkLuckGV.js.map} +1 -1
  195. package/dist/{viewer-CrJupI8I-D9dVcNw4.js → viewer-CrJupI8I-BJiz1cvW.js} +4 -4
  196. package/dist/{viewer-CrJupI8I-D9dVcNw4.js.map → viewer-CrJupI8I-BJiz1cvW.js.map} +1 -1
  197. package/dist/viewer-auto-register.js +2 -2
  198. package/dist/viewer.js +2 -2
  199. package/dist/{viewport-CiCKeAQH.js → viewport-BNIRWjiT.js} +354 -155
  200. package/dist/viewport-BNIRWjiT.js.map +1 -0
  201. package/package.json +12 -12
  202. package/dist/designer-blueprint-workbench-panel-CJ3iZo5v-BebifP4x.js.map +0 -1
  203. package/dist/designer-dataset-controller-CrfOg2Ys-DhKUEJJe.js.map +0 -1
  204. package/dist/designer-field-primitives-KBZuWN_D-B3h4jpYe.js.map +0 -1
  205. package/dist/dist-BjLjFUy1.js.map +0 -1
  206. package/dist/parse-AOTE9WnD.js.map +0 -1
  207. package/dist/plugin-package-Dr5ro6h6.js +0 -1221
  208. package/dist/plugin-package-Dr5ro6h6.js.map +0 -1
  209. package/dist/text-BNksUr5J.js.map +0 -1
  210. package/dist/viewport-CiCKeAQH.js.map +0 -1
@@ -0,0 +1,879 @@
1
+ import { parseBaseScreenMaterialTemplateExpression, } from './material-template-expression.js';
2
+ export const BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS = {
3
+ nodes: 300,
4
+ repeat: 200,
5
+ animations: 8,
6
+ events: 4,
7
+ gradients: 8,
8
+ pathCommands: 64,
9
+ dash: 6,
10
+ minDuration: 100,
11
+ maxDuration: 20_000,
12
+ };
13
+ /** 节点种类(ADR-0116 §2.2):结构、基础、参数化高层图元。 */
14
+ export const BASE_SCREEN_MATERIAL_TEMPLATE_NODE_KINDS = [
15
+ 'g',
16
+ 'repeat',
17
+ 'when',
18
+ 'clip',
19
+ 'rect',
20
+ 'circle',
21
+ 'ellipse',
22
+ 'line',
23
+ 'polyline',
24
+ 'path',
25
+ 'text',
26
+ 'arc',
27
+ 'ticks',
28
+ 'wave',
29
+ ];
30
+ /** 各种类的数值参数;`change` 动画只能补间这里列出的参数。 */
31
+ export const BASE_SCREEN_MATERIAL_TEMPLATE_NUMBER_ATTRS = {
32
+ g: ['x', 'y', 'scale', 'rotate'],
33
+ repeat: [],
34
+ when: [],
35
+ clip: [],
36
+ rect: ['x', 'y', 'width', 'height', 'rx'],
37
+ circle: ['cx', 'cy', 'r'],
38
+ ellipse: ['cx', 'cy', 'rx', 'ry'],
39
+ line: ['x1', 'y1', 'x2', 'y2'],
40
+ polyline: [],
41
+ path: [],
42
+ text: ['x', 'y', 'fontSize'],
43
+ arc: ['cx', 'cy', 'r', 'thickness', 'start', 'end'],
44
+ ticks: ['cx', 'cy', 'r', 'length', 'count', 'start', 'end'],
45
+ wave: ['x', 'y', 'width', 'height', 'amplitude', 'wavelength', 'phase'],
46
+ };
47
+ const REQUIRED_NUMBER_ATTRS = {
48
+ rect: ['x', 'y', 'width', 'height'],
49
+ circle: ['cx', 'cy', 'r'],
50
+ ellipse: ['cx', 'cy', 'rx', 'ry'],
51
+ line: ['x1', 'y1', 'x2', 'y2'],
52
+ text: ['x', 'y', 'fontSize'],
53
+ arc: ['cx', 'cy', 'r', 'thickness', 'start', 'end'],
54
+ ticks: ['cx', 'cy', 'r', 'length', 'count', 'start', 'end'],
55
+ wave: ['x', 'y', 'width', 'height', 'amplitude', 'wavelength'],
56
+ };
57
+ const SHAPE_KINDS = new Set([
58
+ 'rect',
59
+ 'circle',
60
+ 'ellipse',
61
+ 'line',
62
+ 'polyline',
63
+ 'path',
64
+ 'text',
65
+ 'arc',
66
+ 'ticks',
67
+ 'wave',
68
+ ]);
69
+ const CLIP_SHAPE_KINDS = new Set(['rect', 'circle', 'ellipse', 'path']);
70
+ const CONTAINER_KINDS = new Set(['g', 'repeat', 'when', 'clip']);
71
+ const FILLABLE_KINDS = new Set([
72
+ 'rect',
73
+ 'circle',
74
+ 'ellipse',
75
+ 'path',
76
+ 'wave',
77
+ 'polyline',
78
+ ]);
79
+ const EXTRA_KEYS = {
80
+ g: ['children', 'opacity'],
81
+ repeat: ['source', 'children'],
82
+ when: ['test', 'children'],
83
+ clip: ['shape', 'children'],
84
+ rect: [],
85
+ circle: [],
86
+ ellipse: [],
87
+ line: [],
88
+ polyline: ['points', 'closed'],
89
+ path: ['d'],
90
+ text: ['content', 'fontWeight', 'anchor', 'baseline', 'backdrop'],
91
+ arc: ['linecap'],
92
+ ticks: [],
93
+ wave: [],
94
+ };
95
+ const SHAPE_KEYS = [
96
+ 'fill',
97
+ 'stroke',
98
+ 'strokeWidth',
99
+ 'strokeDasharray',
100
+ 'strokeLinecap',
101
+ 'opacity',
102
+ 'on',
103
+ ];
104
+ const ID_PATTERN = /^[a-z][A-Za-z0-9-]{0,39}$/;
105
+ const EVENT_PATTERN = /^[a-z][A-Za-z0-9]{0,31}$/;
106
+ const PAYLOAD_KEY_PATTERN = /^[a-z][A-Za-z0-9]{0,31}$/;
107
+ /** 字面量颜色:#rgb / #rrggbb / #rrggbbaa、rgb() / rgba() / hsl() / hsla()(括号里不能再有括号)、几个关键字。 */
108
+ export const BASE_SCREEN_MATERIAL_TEMPLATE_COLOR = /^(?:#[0-9a-fA-F]{3,4}|#[0-9a-fA-F]{6}|#[0-9a-fA-F]{8}|(?:rgb|rgba|hsl|hsla)\([0-9.,%\s/deg]*\)|transparent|none|currentColor)$/;
109
+ const PATH_COMMANDS = {
110
+ M: 2,
111
+ L: 2,
112
+ H: 1,
113
+ V: 1,
114
+ C: 6,
115
+ Q: 4,
116
+ A: 7,
117
+ Z: 0,
118
+ };
119
+ const LOOP_PROPERTIES = new Set(['translateX', 'translateY', 'rotate', 'scale', 'opacity']);
120
+ const EASINGS = new Set(['linear', 'ease', 'ease-in', 'ease-out', 'ease-in-out']);
121
+ const ANCHORS = new Set(['start', 'middle', 'end']);
122
+ const BASELINES = new Set(['auto', 'middle', 'hanging']);
123
+ const LINECAPS = new Set(['butt', 'round', 'square']);
124
+ const FONT_WEIGHTS = new Set([400, 500, 600, 700]);
125
+ const RULE_MESSAGE = (rule, message) => `${rule}:${message}`;
126
+ /**
127
+ * 解析并编译模板。诊断路径相对规格根(`['template', 'nodes', 2, 'fill']`),消息以规则号开头
128
+ * (`TPL1:…`),体检据此归到 TPL 规则。
129
+ */
130
+ export function compileBaseScreenMaterialTemplate(input, context) {
131
+ const compiler = new TemplateCompiler(context);
132
+ const compiled = compiler.compile(input);
133
+ if (compiler.diagnostics.length > 0 || compiled === undefined) {
134
+ return { ok: false, diagnostics: compiler.diagnostics };
135
+ }
136
+ return { ok: true, value: compiled, diagnostics: [] };
137
+ }
138
+ class TemplateCompiler {
139
+ context;
140
+ diagnostics = [];
141
+ #index = new Map();
142
+ #ids = new Set();
143
+ #gradientIds = new Set();
144
+ #props;
145
+ #style;
146
+ #roles;
147
+ #colorKeys;
148
+ #numberKeys;
149
+ #events;
150
+ #nodeCount = 0;
151
+ constructor(context) {
152
+ this.context = context;
153
+ this.#props = new Set();
154
+ this.#style = new Set();
155
+ this.#colorKeys = new Set();
156
+ this.#numberKeys = new Set();
157
+ for (const field of context.fields) {
158
+ (context.sectionTarget(field.section) === 'props' ? this.#props : this.#style).add(field.key);
159
+ if (field.kind === 'color')
160
+ this.#colorKeys.add(field.key);
161
+ if (field.kind === 'number')
162
+ this.#numberKeys.add(field.key);
163
+ }
164
+ // 读数格式配置组的 props(decimals / prefix / unit / thousandsSeparator)由 format() 读,不必直接引用。
165
+ this.#roles = new Set(context.data.kind === 'rows' ? context.data.roles.map((role) => role.id) : []);
166
+ this.#events = new Map((context.events ?? []).map((event) => [event.name, event]));
167
+ }
168
+ #add(rule, path, message) {
169
+ this.diagnostics.push({
170
+ severity: 'error',
171
+ code: 'PLUGIN_CORE_MANIFEST_FIELD_INVALID',
172
+ path: ['template', ...path],
173
+ message: RULE_MESSAGE(rule, message),
174
+ });
175
+ }
176
+ compile(input) {
177
+ if (!isRecord(input)) {
178
+ this.#add('TPL4', [], '模板必须是对象。');
179
+ return undefined;
180
+ }
181
+ for (const key of Object.keys(input)) {
182
+ if (!['viewBox', 'fit', 'defs', 'nodes', 'animations'].includes(key)) {
183
+ this.#add('TPL1', [key], `模板没有字段 ${key}。`);
184
+ }
185
+ }
186
+ const viewBox = input.viewBox;
187
+ const validViewBox = Array.isArray(viewBox) &&
188
+ viewBox.length === 4 &&
189
+ viewBox.every((item) => typeof item === 'number' && Number.isFinite(item)) &&
190
+ viewBox[2] > 0 &&
191
+ viewBox[3] > 0;
192
+ if (!validViewBox)
193
+ this.#add('TPL4', ['viewBox'], 'viewBox 必须是 [x, y, 宽, 高] 四个数字,宽高大于 0。');
194
+ const fit = input.fit ?? 'contain';
195
+ if (fit !== 'contain' && fit !== 'cover' && fit !== 'stretch') {
196
+ this.#add('TPL4', ['fit'], 'fit 只能是 contain / cover / stretch。');
197
+ }
198
+ const gradients = this.#gradients(input.defs);
199
+ const scope = {
200
+ props: this.#props,
201
+ style: this.#style,
202
+ roles: this.#roles,
203
+ data: this.context.data.kind,
204
+ inRepeat: false,
205
+ inRowRepeat: false,
206
+ };
207
+ if (!Array.isArray(input.nodes) || input.nodes.length === 0) {
208
+ this.#add('TPL4', ['nodes'], 'nodes 必须是非空数组。');
209
+ return undefined;
210
+ }
211
+ const nodes = this.#nodeList(input.nodes, ['nodes'], scope);
212
+ this.#checkBackdrops();
213
+ const animations = this.#animations(input.animations, scope);
214
+ if (!validViewBox)
215
+ return undefined;
216
+ return {
217
+ viewBox: viewBox,
218
+ fit: fit,
219
+ gradients,
220
+ nodes,
221
+ animations,
222
+ index: this.#index,
223
+ };
224
+ }
225
+ #gradients(input) {
226
+ if (input === undefined)
227
+ return [];
228
+ if (!Array.isArray(input)) {
229
+ this.#add('TPL4', ['defs'], 'defs 必须是数组。');
230
+ return [];
231
+ }
232
+ if (input.length > BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.gradients) {
233
+ this.#add('TPL3', ['defs'], `渐变最多 ${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.gradients} 个。`);
234
+ }
235
+ const output = [];
236
+ input.forEach((item, index) => {
237
+ const path = ['defs', index];
238
+ if (!isRecord(item) || (item.kind !== 'linearGradient' && item.kind !== 'radialGradient')) {
239
+ this.#add('TPL1', path, 'defs 里只能是 linearGradient / radialGradient。');
240
+ return;
241
+ }
242
+ const geometryKeys = item.kind === 'linearGradient' ? ['x1', 'y1', 'x2', 'y2'] : ['cx', 'cy', 'r'];
243
+ for (const key of Object.keys(item)) {
244
+ if (!['kind', 'id', 'stops', ...geometryKeys].includes(key)) {
245
+ this.#add('TPL1', [...path, key], `${item.kind} 没有字段 ${key}。`);
246
+ }
247
+ }
248
+ const id = item.id;
249
+ if (typeof id !== 'string' || !ID_PATTERN.test(id)) {
250
+ this.#add('TPL4', [...path, 'id'], 'id 为小写字母开头的字母、数字、连字符(≤ 40)。');
251
+ return;
252
+ }
253
+ if (this.#gradientIds.has(id))
254
+ this.#add('TPL4', [...path, 'id'], `渐变 id ${id} 重复。`);
255
+ this.#gradientIds.add(id);
256
+ const defaults = item.kind === 'linearGradient'
257
+ ? { x1: 0, y1: 0, x2: 0, y2: 1 }
258
+ : { cx: 0.5, cy: 0.5, r: 0.5 };
259
+ const geometry = {};
260
+ for (const key of geometryKeys) {
261
+ const value = item[key] ?? defaults[key];
262
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < -1 || value > 2) {
263
+ this.#add('TPL4', [...path, key], `${key} 是 -1 到 2 之间的数字(相对图形包围盒的比例)。`);
264
+ }
265
+ else {
266
+ geometry[key] = value;
267
+ }
268
+ }
269
+ const stops = [];
270
+ if (!Array.isArray(item.stops) || item.stops.length < 2 || item.stops.length > 8) {
271
+ this.#add('TPL4', [...path, 'stops'], 'stops 是 2–8 个色标。');
272
+ }
273
+ else {
274
+ item.stops.forEach((stop, at) => {
275
+ const stopPath = [...path, 'stops', at];
276
+ if (!isRecord(stop)) {
277
+ this.#add('TPL4', stopPath, '色标必须是对象。');
278
+ return;
279
+ }
280
+ for (const key of Object.keys(stop)) {
281
+ if (!['offset', 'color', 'opacity'].includes(key)) {
282
+ this.#add('TPL1', [...stopPath, key], `色标没有字段 ${key}。`);
283
+ }
284
+ }
285
+ const offset = stop.offset;
286
+ if (typeof offset !== 'number' || offset < 0 || offset > 1) {
287
+ this.#add('TPL4', [...stopPath, 'offset'], 'offset 是 0–1 的数字。');
288
+ }
289
+ const opacity = stop.opacity ?? 1;
290
+ if (typeof opacity !== 'number' || opacity < 0 || opacity > 1) {
291
+ this.#add('TPL4', [...stopPath, 'opacity'], 'opacity 是 0–1 的数字。');
292
+ }
293
+ const color = this.#paint(stop.color, [...stopPath, 'color'], this.#rootScope(), false);
294
+ if (color !== undefined && typeof offset === 'number' && typeof opacity === 'number') {
295
+ stops.push({ offset, color, opacity });
296
+ }
297
+ });
298
+ }
299
+ output.push({ kind: item.kind, id, geometry, stops });
300
+ });
301
+ return output;
302
+ }
303
+ #rootScope() {
304
+ return {
305
+ props: this.#props,
306
+ style: this.#style,
307
+ roles: this.#roles,
308
+ data: this.context.data.kind,
309
+ inRepeat: false,
310
+ inRowRepeat: false,
311
+ };
312
+ }
313
+ #nodeList(input, path, scope) {
314
+ const output = [];
315
+ input.forEach((item, index) => {
316
+ const node = this.#node(item, [...path, index], scope);
317
+ if (node !== undefined)
318
+ output.push(node);
319
+ });
320
+ return output;
321
+ }
322
+ #node(input, path, scope) {
323
+ this.#nodeCount += 1;
324
+ if (this.#nodeCount === BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.nodes + 1) {
325
+ this.#add('TPL3', path, `模板节点最多 ${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.nodes} 个。`);
326
+ }
327
+ if (!isRecord(input)) {
328
+ this.#add('TPL4', path, '节点必须是对象。');
329
+ return undefined;
330
+ }
331
+ const kind = input.kind;
332
+ if (typeof kind !== 'string' ||
333
+ !BASE_SCREEN_MATERIAL_TEMPLATE_NODE_KINDS.includes(kind)) {
334
+ this.#add('TPL1', [...path, 'kind'], `不支持的节点 ${String(kind)}(可用:${BASE_SCREEN_MATERIAL_TEMPLATE_NODE_KINDS.join('、')};不支持 foreignObject、image、use、script、style、filter)。`);
335
+ return undefined;
336
+ }
337
+ const nodeKind = kind;
338
+ const id = input.id;
339
+ if (typeof id !== 'string' || !ID_PATTERN.test(id)) {
340
+ this.#add('TPL4', [...path, 'id'], '每个节点都要有 id:小写字母开头的字母、数字、连字符(≤ 40)。');
341
+ return undefined;
342
+ }
343
+ if (this.#ids.has(id))
344
+ this.#add('TPL4', [...path, 'id'], `节点 id ${id} 重复。`);
345
+ this.#ids.add(id);
346
+ const allowed = new Set([
347
+ 'kind',
348
+ 'id',
349
+ ...BASE_SCREEN_MATERIAL_TEMPLATE_NUMBER_ATTRS[nodeKind],
350
+ ...EXTRA_KEYS[nodeKind],
351
+ ...(SHAPE_KINDS.has(nodeKind) ? SHAPE_KEYS : []),
352
+ ]);
353
+ for (const key of Object.keys(input)) {
354
+ if (!allowed.has(key)) {
355
+ const hint = /^on[A-Z]|^href$|^xlink|^style$|^class$|^filter$/.test(key)
356
+ ? '(不支持事件属性、外部引用、样式字符串与滤镜)'
357
+ : '';
358
+ this.#add('TPL1', [...path, key], `${nodeKind} 没有字段 ${key}${hint}。`);
359
+ }
360
+ }
361
+ const num = {};
362
+ for (const key of BASE_SCREEN_MATERIAL_TEMPLATE_NUMBER_ATTRS[nodeKind]) {
363
+ if (input[key] === undefined)
364
+ continue;
365
+ const expression = this.#number(input[key], [...path, key], scope);
366
+ if (expression !== undefined)
367
+ num[key] = expression;
368
+ }
369
+ for (const key of REQUIRED_NUMBER_ATTRS[nodeKind] ?? []) {
370
+ if (input[key] === undefined)
371
+ this.#add('TPL4', [...path, key], `${nodeKind} 缺少 ${key}。`);
372
+ }
373
+ const node = { kind: nodeKind, id, path, num };
374
+ if (SHAPE_KINDS.has(nodeKind))
375
+ this.#shapeAttributes(input, nodeKind, path, scope, node, num);
376
+ if (nodeKind === 'g' && input.opacity !== undefined) {
377
+ const opacity = this.#number(input.opacity, [...path, 'opacity'], scope);
378
+ if (opacity !== undefined)
379
+ num.opacity = opacity;
380
+ }
381
+ switch (nodeKind) {
382
+ case 'text':
383
+ this.#text(input, path, scope, node, num);
384
+ break;
385
+ case 'repeat': {
386
+ const source = this.#source(input.source, [...path, 'source'], scope);
387
+ if (source !== undefined)
388
+ node.source = source;
389
+ break;
390
+ }
391
+ case 'when': {
392
+ const test = this.#expression(input.test, [...path, 'test'], scope);
393
+ if (test !== undefined)
394
+ node.test = test;
395
+ break;
396
+ }
397
+ case 'clip': {
398
+ const shape = input.shape;
399
+ if (!isRecord(shape) ||
400
+ typeof shape.kind !== 'string' ||
401
+ !CLIP_SHAPE_KINDS.has(shape.kind)) {
402
+ this.#add('TPL4', [...path, 'shape'], 'clip.shape 必须是一个 rect / circle / ellipse / path 节点。');
403
+ }
404
+ else {
405
+ const compiled = this.#node(shape, [...path, 'shape'], scope);
406
+ if (compiled !== undefined)
407
+ node.clipShape = compiled;
408
+ }
409
+ break;
410
+ }
411
+ case 'polyline':
412
+ this.#polyline(input, path, scope, node);
413
+ break;
414
+ case 'path':
415
+ this.#path(input.d, [...path, 'd'], scope, node);
416
+ break;
417
+ case 'arc':
418
+ if (input.linecap !== undefined) {
419
+ if (typeof input.linecap !== 'string' || !LINECAPS.has(input.linecap)) {
420
+ this.#add('TPL4', [...path, 'linecap'], 'linecap 只能是 butt / round / square。');
421
+ }
422
+ else
423
+ node.linecap = input.linecap;
424
+ }
425
+ break;
426
+ default:
427
+ break;
428
+ }
429
+ if (CONTAINER_KINDS.has(nodeKind)) {
430
+ const childScope = nodeKind === 'repeat'
431
+ ? {
432
+ ...scope,
433
+ inRepeat: true,
434
+ inRowRepeat: scope.inRowRepeat || node.source?.k === 'rows',
435
+ }
436
+ : scope;
437
+ if (!Array.isArray(input.children)) {
438
+ this.#add('TPL4', [...path, 'children'], `${nodeKind} 需要 children 数组。`);
439
+ }
440
+ else {
441
+ node.children = this.#nodeList(input.children, [...path, 'children'], childScope);
442
+ }
443
+ }
444
+ const compiled = node;
445
+ this.#index.set(id, compiled);
446
+ return compiled;
447
+ }
448
+ #shapeAttributes(input, kind, path, scope, node, num) {
449
+ if (input.fill !== undefined) {
450
+ const fill = this.#paint(input.fill, [...path, 'fill'], scope, true);
451
+ if (fill !== undefined)
452
+ node.fill = fill;
453
+ }
454
+ if (input.stroke !== undefined) {
455
+ const stroke = this.#paint(input.stroke, [...path, 'stroke'], scope, true);
456
+ if (stroke !== undefined)
457
+ node.stroke = stroke;
458
+ }
459
+ for (const key of ['strokeWidth', 'opacity']) {
460
+ if (input[key] === undefined)
461
+ continue;
462
+ const value = this.#number(input[key], [...path, key], scope);
463
+ if (value !== undefined)
464
+ num[key] = value;
465
+ }
466
+ if (input.strokeDasharray !== undefined) {
467
+ const dash = input.strokeDasharray;
468
+ if (!Array.isArray(dash) ||
469
+ dash.length === 0 ||
470
+ dash.length > BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.dash) {
471
+ this.#add('TPL4', [...path, 'strokeDasharray'], `strokeDasharray 是 1–${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.dash} 个数字。`);
472
+ }
473
+ else {
474
+ const items = dash
475
+ .map((item, at) => this.#number(item, [...path, 'strokeDasharray', at], scope))
476
+ .filter((item) => item !== undefined);
477
+ node.dash = items;
478
+ }
479
+ }
480
+ if (input.strokeLinecap !== undefined) {
481
+ if (typeof input.strokeLinecap !== 'string' || !LINECAPS.has(input.strokeLinecap)) {
482
+ this.#add('TPL4', [...path, 'strokeLinecap'], 'strokeLinecap 只能是 butt / round / square。');
483
+ }
484
+ else
485
+ node.linecap = input.strokeLinecap;
486
+ }
487
+ if (input.on !== undefined) {
488
+ const name = input.on;
489
+ const event = typeof name === 'string' ? this.#events.get(name) : undefined;
490
+ if (event === undefined) {
491
+ this.#add('TPL2', [...path, 'on'], `on 必须是规格 events 里声明的事件名(当前:${[...this.#events.keys()].join('、') || '无'})。`);
492
+ }
493
+ else {
494
+ const payload = [];
495
+ for (const [key, item] of Object.entries(event.payload ?? {})) {
496
+ const expression = this.#expression(item.value, [...path, 'on', key], scope);
497
+ if (expression !== undefined)
498
+ payload.push({ key, type: item.type, value: expression });
499
+ }
500
+ node.on = { name: event.name, payload };
501
+ }
502
+ }
503
+ void kind;
504
+ }
505
+ #text(input, path, scope, node, num) {
506
+ const content = this.#expression(input.content, [...path, 'content'], scope, '文字内容是表达式:字面文字要加引号,如 "\'℃\'";读数用 format(value)。');
507
+ if (content !== undefined)
508
+ node.content = content;
509
+ const fontSize = num.fontSize;
510
+ if (fontSize !== undefined && !this.#isStaticNumber(fontSize)) {
511
+ this.#add('TPL5', [...path, 'fontSize'], '字号只能是数字,或 props / style 数字字段的四则运算(不能随数据变化),体检才能检查最小字号。');
512
+ }
513
+ if (input.fontWeight !== undefined) {
514
+ if (typeof input.fontWeight !== 'number' || !FONT_WEIGHTS.has(input.fontWeight)) {
515
+ this.#add('TPL4', [...path, 'fontWeight'], 'fontWeight 只能是 400 / 500 / 600 / 700。');
516
+ }
517
+ else
518
+ node.fontWeight = input.fontWeight;
519
+ }
520
+ for (const [key, values] of [
521
+ ['anchor', ANCHORS],
522
+ ['baseline', BASELINES],
523
+ ]) {
524
+ const value = input[key];
525
+ if (value === undefined)
526
+ continue;
527
+ if (typeof value !== 'string' || !values.has(value)) {
528
+ this.#add('TPL4', [...path, key], `${key} 只能是 ${[...values].join(' / ')}。`);
529
+ }
530
+ else
531
+ node[key] = value;
532
+ }
533
+ const backdrop = input.backdrop;
534
+ if (typeof backdrop !== 'string' || backdrop === '') {
535
+ this.#add('TPL6', [...path, 'backdrop'], '文字必须声明底板 backdrop:某个填充节点的 id,或 container(组件背景)。');
536
+ }
537
+ else
538
+ node.backdrop = backdrop;
539
+ }
540
+ #checkBackdrops() {
541
+ for (const node of this.#index.values()) {
542
+ if (node.kind !== 'text' || node.backdrop === undefined || node.backdrop === 'container')
543
+ continue;
544
+ const target = this.#index.get(node.backdrop);
545
+ if (target === undefined || !FILLABLE_KINDS.has(target.kind) || target.fill === undefined) {
546
+ this.#add('TPL6', [...node.path, 'backdrop'], `backdrop「${node.backdrop}」必须是带 fill 的填充节点,或 container。`);
547
+ }
548
+ }
549
+ }
550
+ #polyline(input, path, scope, node) {
551
+ const points = input.points;
552
+ if (!isRecord(points)) {
553
+ this.#add('TPL4', [...path, 'points'], 'polyline.points 是 { source, x, y }:对每行(或每个序号)求一个点。');
554
+ return;
555
+ }
556
+ for (const key of Object.keys(points)) {
557
+ if (!['source', 'x', 'y'].includes(key))
558
+ this.#add('TPL1', [...path, 'points', key], `points 没有字段 ${key}。`);
559
+ }
560
+ const source = this.#source(points.source, [...path, 'points', 'source'], scope);
561
+ if (source === undefined)
562
+ return;
563
+ const inner = {
564
+ ...scope,
565
+ inRepeat: true,
566
+ inRowRepeat: scope.inRowRepeat || source.k === 'rows',
567
+ };
568
+ const x = this.#number(points.x, [...path, 'points', 'x'], inner);
569
+ const y = this.#number(points.y, [...path, 'points', 'y'], inner);
570
+ if (x !== undefined && y !== undefined)
571
+ node.points = { source, x, y };
572
+ if (input.closed !== undefined) {
573
+ if (typeof input.closed !== 'boolean')
574
+ this.#add('TPL4', [...path, 'closed'], 'closed 必须是布尔值。');
575
+ else
576
+ node.closed = input.closed;
577
+ }
578
+ }
579
+ #path(input, path, scope, node) {
580
+ if (!Array.isArray(input) ||
581
+ input.length === 0 ||
582
+ input.length > BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.pathCommands) {
583
+ this.#add('TPL4', path, `path.d 是 1–${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.pathCommands} 条命令,每条形如 ["L", x, y]。`);
584
+ return;
585
+ }
586
+ const commands = [];
587
+ input.forEach((item, index) => {
588
+ const itemPath = [...path, index];
589
+ if (!Array.isArray(item) ||
590
+ typeof item[0] !== 'string' ||
591
+ PATH_COMMANDS[item[0]] === undefined) {
592
+ this.#add('TPL4', itemPath, '路径命令只能是 M / L / H / V / C / Q / A / Z(绝对坐标)。');
593
+ return;
594
+ }
595
+ const command = item[0];
596
+ const arity = PATH_COMMANDS[command];
597
+ if (item.length - 1 !== arity) {
598
+ this.#add('TPL4', itemPath, `${command} 需要 ${arity} 个参数。`);
599
+ return;
600
+ }
601
+ const args = item
602
+ .slice(1)
603
+ .map((arg, at) => this.#number(arg, [...itemPath, at + 1], scope))
604
+ .filter((arg) => arg !== undefined);
605
+ if (args.length === arity)
606
+ commands.push([command, args]);
607
+ });
608
+ node.d = commands;
609
+ }
610
+ #source(input, path, scope) {
611
+ if (input === 'rows') {
612
+ if (this.context.data.kind !== 'rows') {
613
+ this.#add('TPL2', path, '组件不是多行数据,不能遍历 rows。');
614
+ return undefined;
615
+ }
616
+ return { k: 'rows' };
617
+ }
618
+ if (isRecord(input) && Object.keys(input).length === 1 && input.range !== undefined) {
619
+ if (typeof input.range === 'number' &&
620
+ input.range > BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.repeat) {
621
+ this.#add('TPL3', [...path, 'range'], `repeat 最多 ${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.repeat} 次。`);
622
+ return undefined;
623
+ }
624
+ const n = this.#number(input.range, [...path, 'range'], scope);
625
+ return n === undefined ? undefined : { k: 'range', n };
626
+ }
627
+ this.#add('TPL4', path, 'source 只能是 "rows" 或 { "range": 次数 }。');
628
+ return undefined;
629
+ }
630
+ #animations(input, scope) {
631
+ if (input === undefined)
632
+ return [];
633
+ if (!Array.isArray(input)) {
634
+ this.#add('TPL4', ['animations'], 'animations 必须是数组。');
635
+ return [];
636
+ }
637
+ if (input.length > BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.animations) {
638
+ this.#add('TPL3', ['animations'], `动画最多 ${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.animations} 个。`);
639
+ }
640
+ const output = [];
641
+ input.forEach((item, index) => {
642
+ const path = ['animations', index];
643
+ if (!isRecord(item) || (item.kind !== 'loop' && item.kind !== 'change')) {
644
+ this.#add('TPL1', path, '动画只有 loop(循环)与 change(读数变化过渡)两种。');
645
+ return;
646
+ }
647
+ const keys = item.kind === 'loop'
648
+ ? [
649
+ 'kind',
650
+ 'target',
651
+ 'property',
652
+ 'from',
653
+ 'to',
654
+ 'duration',
655
+ 'easing',
656
+ 'alternate',
657
+ 'enabledBy',
658
+ ]
659
+ : ['kind', 'target', 'attributes', 'duration', 'easing'];
660
+ for (const key of Object.keys(item)) {
661
+ if (!keys.includes(key))
662
+ this.#add('TPL1', [...path, key], `${item.kind} 动画没有字段 ${key}。`);
663
+ }
664
+ const target = typeof item.target === 'string' ? this.#index.get(item.target) : undefined;
665
+ if (target === undefined) {
666
+ this.#add('TPL4', [...path, 'target'], 'target 必须是模板里某个节点的 id。');
667
+ return;
668
+ }
669
+ const duration = item.duration;
670
+ if (typeof duration !== 'number' ||
671
+ duration < BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.minDuration ||
672
+ duration > BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.maxDuration) {
673
+ this.#add('TPL4', [...path, 'duration'], `duration 是 ${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.minDuration}–${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.maxDuration} 毫秒。`);
674
+ return;
675
+ }
676
+ const easing = item.easing ?? (item.kind === 'loop' ? 'linear' : 'ease-out');
677
+ if (typeof easing !== 'string' || !EASINGS.has(easing)) {
678
+ this.#add('TPL4', [...path, 'easing'], `easing 只能是 ${[...EASINGS].join(' / ')}。`);
679
+ return;
680
+ }
681
+ if (item.kind === 'loop') {
682
+ if (typeof item.property !== 'string' || !LOOP_PROPERTIES.has(item.property)) {
683
+ this.#add('TPL4', [...path, 'property'], `循环动画只能改 ${[...LOOP_PROPERTIES].join(' / ')}(只动组件内部,不动外框)。`);
684
+ return;
685
+ }
686
+ const from = this.#number(item.from, [...path, 'from'], scope);
687
+ const to = this.#number(item.to, [...path, 'to'], scope);
688
+ if (item.alternate !== undefined && typeof item.alternate !== 'boolean') {
689
+ this.#add('TPL4', [...path, 'alternate'], 'alternate 必须是布尔值。');
690
+ }
691
+ const enabledBy = item.enabledBy === undefined
692
+ ? undefined
693
+ : this.#expression(item.enabledBy, [...path, 'enabledBy'], scope);
694
+ if (from === undefined || to === undefined)
695
+ return;
696
+ output.push({
697
+ kind: 'loop',
698
+ target: target.id,
699
+ property: item.property,
700
+ from,
701
+ to,
702
+ duration,
703
+ easing,
704
+ alternate: item.alternate === true,
705
+ ...(enabledBy === undefined ? {} : { enabledBy }),
706
+ });
707
+ return;
708
+ }
709
+ const numeric = BASE_SCREEN_MATERIAL_TEMPLATE_NUMBER_ATTRS[target.kind];
710
+ if (!Array.isArray(item.attributes) ||
711
+ item.attributes.length === 0 ||
712
+ item.attributes.some((attr) => typeof attr !== 'string' || !numeric.includes(attr))) {
713
+ this.#add('TPL4', [...path, 'attributes'], `${target.kind} 能补间的参数:${numeric.join('、') || '无'}。`);
714
+ return;
715
+ }
716
+ output.push({
717
+ kind: 'change',
718
+ target: target.id,
719
+ attributes: item.attributes,
720
+ duration,
721
+ easing,
722
+ });
723
+ });
724
+ return output;
725
+ }
726
+ #expression(input, path, scope, hint) {
727
+ if (typeof input === 'boolean')
728
+ return { k: 'lit', v: input };
729
+ if (typeof input === 'number' && Number.isFinite(input))
730
+ return { k: 'lit', v: input };
731
+ if (typeof input !== 'string') {
732
+ this.#add('TPL4', path, `需要一个表达式字符串${hint === undefined ? '' : `:${hint}`}`);
733
+ return undefined;
734
+ }
735
+ const parsed = parseBaseScreenMaterialTemplateExpression(input, scope);
736
+ if (!parsed.ok) {
737
+ this.#add('TPL2', path, `${parsed.message}${hint === undefined ? '' : ` ${hint}`}`);
738
+ return undefined;
739
+ }
740
+ return parsed.value;
741
+ }
742
+ #number(input, path, scope) {
743
+ if (typeof input === 'number') {
744
+ if (!Number.isFinite(input)) {
745
+ this.#add('TPL4', path, '数值必须是有限数字。');
746
+ return undefined;
747
+ }
748
+ return { k: 'lit', v: input };
749
+ }
750
+ if (typeof input === 'string')
751
+ return this.#expression(input, path, scope);
752
+ this.#add('TPL4', path, '数值参数写数字或表达式字符串。');
753
+ return undefined;
754
+ }
755
+ #paint(input, path, scope, allowGradient) {
756
+ if (isRecord(input) && Object.keys(input).length === 1 && typeof input.gradient === 'string') {
757
+ if (!allowGradient || !this.#gradientIds.has(input.gradient)) {
758
+ this.#add('TPL4', path, `渐变「${input.gradient}」没有在 defs 里声明。`);
759
+ return undefined;
760
+ }
761
+ return { k: 'gradient', id: input.gradient };
762
+ }
763
+ if (typeof input !== 'string') {
764
+ this.#add('TPL5', path, '颜色写字面量(#rrggbb、rgba(...)、none)、style.<颜色字段>,或 { "gradient": "渐变 id" }。');
765
+ return undefined;
766
+ }
767
+ if (BASE_SCREEN_MATERIAL_TEMPLATE_COLOR.test(input.trim()))
768
+ return { k: 'color', v: input.trim() };
769
+ if (/url\s*\(/i.test(input)) {
770
+ this.#add('TPL1', path, '颜色里不能写 url(...):渐变用 { "gradient": "id" } 引用。');
771
+ return undefined;
772
+ }
773
+ const expression = this.#expression(input, path, scope);
774
+ if (expression === undefined)
775
+ return undefined;
776
+ if (!this.#isAuditablePaint(expression)) {
777
+ this.#add('TPL5', path, '颜色表达式只能在字面量颜色与 style.<颜色字段> 之间选择(pick / 三元),不能由数据算出颜色;语义色请声明成样式字段,值可写 theme:danger。');
778
+ return undefined;
779
+ }
780
+ return { k: 'expr', e: expression };
781
+ }
782
+ /** 颜色表达式能否静态枚举出全部候选(字面量颜色或颜色样式字段)。 */
783
+ #isAuditablePaint(expression) {
784
+ switch (expression.k) {
785
+ case 'lit':
786
+ return (typeof expression.v === 'string' && BASE_SCREEN_MATERIAL_TEMPLATE_COLOR.test(expression.v));
787
+ case 'ref':
788
+ return (expression.root === 'style' &&
789
+ expression.key !== undefined &&
790
+ this.#colorKeys.has(expression.key));
791
+ case 'cond':
792
+ return this.#isAuditablePaint(expression.a) && this.#isAuditablePaint(expression.b);
793
+ case 'call':
794
+ if (expression.fn !== 'pick')
795
+ return false;
796
+ return expression.args.every((arg, index) => index === 0 ||
797
+ (index % 2 === 1 && index !== expression.args.length - 1) ||
798
+ this.#isAuditablePaint(arg));
799
+ default:
800
+ return false;
801
+ }
802
+ }
803
+ /** 字号:字面量或 props / style 数字字段的运算,不随数据变化。 */
804
+ #isStaticNumber(expression) {
805
+ switch (expression.k) {
806
+ case 'lit':
807
+ return typeof expression.v === 'number';
808
+ case 'ref':
809
+ return ((expression.root === 'props' || expression.root === 'style') &&
810
+ expression.key !== undefined &&
811
+ this.#numberKeys.has(expression.key));
812
+ case 'un':
813
+ return expression.op === '-' && this.#isStaticNumber(expression.a);
814
+ case 'bin':
815
+ return (['+', '-', '*', '/'].includes(expression.op) &&
816
+ this.#isStaticNumber(expression.a) &&
817
+ this.#isStaticNumber(expression.b));
818
+ case 'call':
819
+ return (['min', 'max', 'clamp', 'round'].includes(expression.fn) &&
820
+ expression.args.every((arg) => this.#isStaticNumber(arg)));
821
+ default:
822
+ return false;
823
+ }
824
+ }
825
+ }
826
+ function isRecord(value) {
827
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
828
+ }
829
+ /** 规格事件的形状校验(ADR-0116 §2.6),路径相对规格根。 */
830
+ export function parseBaseScreenMaterialSpecEvents(input, add) {
831
+ if (input === undefined)
832
+ return;
833
+ if (!Array.isArray(input) || input.length > BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.events) {
834
+ add(['events'], `events 是最多 ${BASE_SCREEN_MATERIAL_TEMPLATE_LIMITS.events} 个事件的数组。`);
835
+ return;
836
+ }
837
+ const names = new Set();
838
+ input.forEach((event, index) => {
839
+ const path = ['events', index];
840
+ if (!isRecord(event)) {
841
+ add(path, '事件必须是对象。');
842
+ return;
843
+ }
844
+ for (const key of Object.keys(event)) {
845
+ if (!['name', 'label', 'payload'].includes(key))
846
+ add([...path, key], `未知字段 ${key}。`);
847
+ }
848
+ if (typeof event.name !== 'string' || !EVENT_PATTERN.test(event.name)) {
849
+ add([...path, 'name'], '事件名为小驼峰 ASCII(如 valueClick)。');
850
+ }
851
+ else if (names.has(event.name)) {
852
+ add([...path, 'name'], `事件 ${event.name} 重复。`);
853
+ }
854
+ else
855
+ names.add(event.name);
856
+ if (typeof event.label !== 'string' ||
857
+ event.label.trim() === '' ||
858
+ [...event.label].length > 16) {
859
+ add([...path, 'label'], '事件标签为 1–16 个字符。');
860
+ }
861
+ if (event.payload !== undefined) {
862
+ if (!isRecord(event.payload) || Object.keys(event.payload).length > 6) {
863
+ add([...path, 'payload'], 'payload 是最多 6 项的对象:{ 键: { type, value } }。');
864
+ return;
865
+ }
866
+ for (const [key, item] of Object.entries(event.payload)) {
867
+ if (!PAYLOAD_KEY_PATTERN.test(key))
868
+ add([...path, 'payload', key], '负载键为小驼峰 ASCII。');
869
+ if (!isRecord(item) ||
870
+ (item.type !== 'number' && item.type !== 'string' && item.type !== 'boolean') ||
871
+ typeof item.value !== 'string' ||
872
+ Object.keys(item).some((name) => name !== 'type' && name !== 'value')) {
873
+ add([...path, 'payload', key], '负载项写 { "type": "number" | "string" | "boolean", "value": "表达式" }。');
874
+ }
875
+ }
876
+ }
877
+ });
878
+ }
879
+ //# sourceMappingURL=material-template.js.map