designmockdown 0.0.0-stage → 0.2.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 (304) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +126 -2
  3. package/dist/app/assets/index-CmugGtLS.js +15 -0
  4. package/dist/app/assets/index-CwhgxL4e.css +1 -0
  5. package/dist/app/index.html +13 -0
  6. package/dist/lib/cli.d.ts +11 -0
  7. package/dist/lib/cli.d.ts.map +1 -0
  8. package/dist/lib/cli.js +968 -0
  9. package/dist/lib/cli.js.map +1 -0
  10. package/dist/lib/collab/client.d.ts +20 -0
  11. package/dist/lib/collab/client.d.ts.map +1 -0
  12. package/dist/lib/collab/client.js +126 -0
  13. package/dist/lib/collab/client.js.map +1 -0
  14. package/dist/lib/collab/index.d.ts +40 -0
  15. package/dist/lib/collab/index.d.ts.map +1 -0
  16. package/dist/lib/collab/index.js +154 -0
  17. package/dist/lib/collab/index.js.map +1 -0
  18. package/dist/lib/collab/room.d.ts +111 -0
  19. package/dist/lib/collab/room.d.ts.map +1 -0
  20. package/dist/lib/collab/room.js +120 -0
  21. package/dist/lib/collab/room.js.map +1 -0
  22. package/dist/lib/collab/ws.d.ts +33 -0
  23. package/dist/lib/collab/ws.d.ts.map +1 -0
  24. package/dist/lib/collab/ws.js +191 -0
  25. package/dist/lib/collab/ws.js.map +1 -0
  26. package/dist/lib/components/basic.d.ts +8 -0
  27. package/dist/lib/components/basic.d.ts.map +1 -0
  28. package/dist/lib/components/basic.js +181 -0
  29. package/dist/lib/components/basic.js.map +1 -0
  30. package/dist/lib/components/config.d.ts +26 -0
  31. package/dist/lib/components/config.d.ts.map +1 -0
  32. package/dist/lib/components/config.js +94 -0
  33. package/dist/lib/components/config.js.map +1 -0
  34. package/dist/lib/components/containers.d.ts +9 -0
  35. package/dist/lib/components/containers.d.ts.map +1 -0
  36. package/dist/lib/components/containers.js +102 -0
  37. package/dist/lib/components/containers.js.map +1 -0
  38. package/dist/lib/components/controls.d.ts +8 -0
  39. package/dist/lib/components/controls.d.ts.map +1 -0
  40. package/dist/lib/components/controls.js +376 -0
  41. package/dist/lib/components/controls.js.map +1 -0
  42. package/dist/lib/components/data.d.ts +8 -0
  43. package/dist/lib/components/data.d.ts.map +1 -0
  44. package/dist/lib/components/data.js +494 -0
  45. package/dist/lib/components/data.js.map +1 -0
  46. package/dist/lib/components/feedback.d.ts +8 -0
  47. package/dist/lib/components/feedback.d.ts.map +1 -0
  48. package/dist/lib/components/feedback.js +151 -0
  49. package/dist/lib/components/feedback.js.map +1 -0
  50. package/dist/lib/components/icons.d.ts +28 -0
  51. package/dist/lib/components/icons.d.ts.map +1 -0
  52. package/dist/lib/components/icons.js +85 -0
  53. package/dist/lib/components/icons.js.map +1 -0
  54. package/dist/lib/components/layout.d.ts +10 -0
  55. package/dist/lib/components/layout.d.ts.map +1 -0
  56. package/dist/lib/components/layout.js +82 -0
  57. package/dist/lib/components/layout.js.map +1 -0
  58. package/dist/lib/components/nav.d.ts +8 -0
  59. package/dist/lib/components/nav.d.ts.map +1 -0
  60. package/dist/lib/components/nav.js +216 -0
  61. package/dist/lib/components/nav.js.map +1 -0
  62. package/dist/lib/components/plugin.d.ts +27 -0
  63. package/dist/lib/components/plugin.d.ts.map +1 -0
  64. package/dist/lib/components/plugin.js +152 -0
  65. package/dist/lib/components/plugin.js.map +1 -0
  66. package/dist/lib/components/registry.d.ts +111 -0
  67. package/dist/lib/components/registry.d.ts.map +1 -0
  68. package/dist/lib/components/registry.js +375 -0
  69. package/dist/lib/components/registry.js.map +1 -0
  70. package/dist/lib/components/types.d.ts +111 -0
  71. package/dist/lib/components/types.d.ts.map +1 -0
  72. package/dist/lib/components/types.js +6 -0
  73. package/dist/lib/components/types.js.map +1 -0
  74. package/dist/lib/core/anim.d.ts +31 -0
  75. package/dist/lib/core/anim.d.ts.map +1 -0
  76. package/dist/lib/core/anim.js +95 -0
  77. package/dist/lib/core/anim.js.map +1 -0
  78. package/dist/lib/core/ast.d.ts +174 -0
  79. package/dist/lib/core/ast.d.ts.map +1 -0
  80. package/dist/lib/core/ast.js +42 -0
  81. package/dist/lib/core/ast.js.map +1 -0
  82. package/dist/lib/core/chart-source.d.ts +37 -0
  83. package/dist/lib/core/chart-source.d.ts.map +1 -0
  84. package/dist/lib/core/chart-source.js +125 -0
  85. package/dist/lib/core/chart-source.js.map +1 -0
  86. package/dist/lib/core/data.d.ts +32 -0
  87. package/dist/lib/core/data.d.ts.map +1 -0
  88. package/dist/lib/core/data.js +150 -0
  89. package/dist/lib/core/data.js.map +1 -0
  90. package/dist/lib/core/define.d.ts +16 -0
  91. package/dist/lib/core/define.d.ts.map +1 -0
  92. package/dist/lib/core/define.js +232 -0
  93. package/dist/lib/core/define.js.map +1 -0
  94. package/dist/lib/core/diagnostics.d.ts +64 -0
  95. package/dist/lib/core/diagnostics.d.ts.map +1 -0
  96. package/dist/lib/core/diagnostics.js +62 -0
  97. package/dist/lib/core/diagnostics.js.map +1 -0
  98. package/dist/lib/core/errors.d.ts +27 -0
  99. package/dist/lib/core/errors.d.ts.map +1 -0
  100. package/dist/lib/core/errors.js +70 -0
  101. package/dist/lib/core/errors.js.map +1 -0
  102. package/dist/lib/core/expand.d.ts +21 -0
  103. package/dist/lib/core/expand.d.ts.map +1 -0
  104. package/dist/lib/core/expand.js +226 -0
  105. package/dist/lib/core/expand.js.map +1 -0
  106. package/dist/lib/core/expr.d.ts +29 -0
  107. package/dist/lib/core/expr.d.ts.map +1 -0
  108. package/dist/lib/core/expr.js +313 -0
  109. package/dist/lib/core/expr.js.map +1 -0
  110. package/dist/lib/core/format.d.ts +30 -0
  111. package/dist/lib/core/format.d.ts.map +1 -0
  112. package/dist/lib/core/format.js +106 -0
  113. package/dist/lib/core/format.js.map +1 -0
  114. package/dist/lib/core/lexer.d.ts +54 -0
  115. package/dist/lib/core/lexer.d.ts.map +1 -0
  116. package/dist/lib/core/lexer.js +311 -0
  117. package/dist/lib/core/lexer.js.map +1 -0
  118. package/dist/lib/core/parser.d.ts +29 -0
  119. package/dist/lib/core/parser.d.ts.map +1 -0
  120. package/dist/lib/core/parser.js +674 -0
  121. package/dist/lib/core/parser.js.map +1 -0
  122. package/dist/lib/core/resolver.d.ts +37 -0
  123. package/dist/lib/core/resolver.d.ts.map +1 -0
  124. package/dist/lib/core/resolver.js +240 -0
  125. package/dist/lib/core/resolver.js.map +1 -0
  126. package/dist/lib/core/spec.d.ts +87 -0
  127. package/dist/lib/core/spec.d.ts.map +1 -0
  128. package/dist/lib/core/spec.js +207 -0
  129. package/dist/lib/core/spec.js.map +1 -0
  130. package/dist/lib/core/state.d.ts +19 -0
  131. package/dist/lib/core/state.d.ts.map +1 -0
  132. package/dist/lib/core/state.js +64 -0
  133. package/dist/lib/core/state.js.map +1 -0
  134. package/dist/lib/core/tokens.d.ts +36 -0
  135. package/dist/lib/core/tokens.d.ts.map +1 -0
  136. package/dist/lib/core/tokens.js +198 -0
  137. package/dist/lib/core/tokens.js.map +1 -0
  138. package/dist/lib/export/pages.d.ts +39 -0
  139. package/dist/lib/export/pages.d.ts.map +1 -0
  140. package/dist/lib/export/pages.js +95 -0
  141. package/dist/lib/export/pages.js.map +1 -0
  142. package/dist/lib/export/raster.d.ts +39 -0
  143. package/dist/lib/export/raster.d.ts.map +1 -0
  144. package/dist/lib/export/raster.js +152 -0
  145. package/dist/lib/export/raster.js.map +1 -0
  146. package/dist/lib/index.d.ts +33 -0
  147. package/dist/lib/index.d.ts.map +1 -0
  148. package/dist/lib/index.js +30 -0
  149. package/dist/lib/index.js.map +1 -0
  150. package/dist/lib/layout/engine.d.ts +32 -0
  151. package/dist/lib/layout/engine.d.ts.map +1 -0
  152. package/dist/lib/layout/engine.js +1063 -0
  153. package/dist/lib/layout/engine.js.map +1 -0
  154. package/dist/lib/layout/measure.d.ts +30 -0
  155. package/dist/lib/layout/measure.d.ts.map +1 -0
  156. package/dist/lib/layout/measure.js +148 -0
  157. package/dist/lib/layout/measure.js.map +1 -0
  158. package/dist/lib/layout/types.d.ts +94 -0
  159. package/dist/lib/layout/types.d.ts.map +1 -0
  160. package/dist/lib/layout/types.js +16 -0
  161. package/dist/lib/layout/types.js.map +1 -0
  162. package/dist/lib/lsp/server.d.ts +76 -0
  163. package/dist/lib/lsp/server.d.ts.map +1 -0
  164. package/dist/lib/lsp/server.js +264 -0
  165. package/dist/lib/lsp/server.js.map +1 -0
  166. package/dist/lib/output/docs.d.ts +26 -0
  167. package/dist/lib/output/docs.d.ts.map +1 -0
  168. package/dist/lib/output/docs.js +231 -0
  169. package/dist/lib/output/docs.js.map +1 -0
  170. package/dist/lib/output/ir.d.ts +105 -0
  171. package/dist/lib/output/ir.d.ts.map +1 -0
  172. package/dist/lib/output/ir.js +89 -0
  173. package/dist/lib/output/ir.js.map +1 -0
  174. package/dist/lib/output/schema.d.ts +69 -0
  175. package/dist/lib/output/schema.d.ts.map +1 -0
  176. package/dist/lib/output/schema.js +84 -0
  177. package/dist/lib/output/schema.js.map +1 -0
  178. package/dist/lib/paths.d.ts +12 -0
  179. package/dist/lib/paths.d.ts.map +1 -0
  180. package/dist/lib/paths.js +27 -0
  181. package/dist/lib/paths.js.map +1 -0
  182. package/dist/lib/playground/examples.d.ts +16 -0
  183. package/dist/lib/playground/examples.d.ts.map +1 -0
  184. package/dist/lib/playground/examples.js +515 -0
  185. package/dist/lib/playground/examples.js.map +1 -0
  186. package/dist/lib/playground/index.d.ts +51 -0
  187. package/dist/lib/playground/index.d.ts.map +1 -0
  188. package/dist/lib/playground/index.js +598 -0
  189. package/dist/lib/playground/index.js.map +1 -0
  190. package/dist/lib/render/assets.d.ts +35 -0
  191. package/dist/lib/render/assets.d.ts.map +1 -0
  192. package/dist/lib/render/assets.js +114 -0
  193. package/dist/lib/render/assets.js.map +1 -0
  194. package/dist/lib/render/backends.d.ts +31 -0
  195. package/dist/lib/render/backends.d.ts.map +1 -0
  196. package/dist/lib/render/backends.js +57 -0
  197. package/dist/lib/render/backends.js.map +1 -0
  198. package/dist/lib/render/css.d.ts +24 -0
  199. package/dist/lib/render/css.d.ts.map +1 -0
  200. package/dist/lib/render/css.js +148 -0
  201. package/dist/lib/render/css.js.map +1 -0
  202. package/dist/lib/render/document-html.d.ts +28 -0
  203. package/dist/lib/render/document-html.d.ts.map +1 -0
  204. package/dist/lib/render/document-html.js +214 -0
  205. package/dist/lib/render/document-html.js.map +1 -0
  206. package/dist/lib/render/dom.d.ts +32 -0
  207. package/dist/lib/render/dom.d.ts.map +1 -0
  208. package/dist/lib/render/dom.js +531 -0
  209. package/dist/lib/render/dom.js.map +1 -0
  210. package/dist/lib/render/primitives.d.ts +81 -0
  211. package/dist/lib/render/primitives.d.ts.map +1 -0
  212. package/dist/lib/render/primitives.js +172 -0
  213. package/dist/lib/render/primitives.js.map +1 -0
  214. package/dist/lib/render/react.d.ts +23 -0
  215. package/dist/lib/render/react.d.ts.map +1 -0
  216. package/dist/lib/render/react.js +447 -0
  217. package/dist/lib/render/react.js.map +1 -0
  218. package/dist/lib/render/svg.d.ts +45 -0
  219. package/dist/lib/render/svg.d.ts.map +1 -0
  220. package/dist/lib/render/svg.js +179 -0
  221. package/dist/lib/render/svg.js.map +1 -0
  222. package/dist/lib/runtime/proto.d.ts +19 -0
  223. package/dist/lib/runtime/proto.d.ts.map +1 -0
  224. package/dist/lib/runtime/proto.js +81 -0
  225. package/dist/lib/runtime/proto.js.map +1 -0
  226. package/dist/lib/skill/install.d.ts +28 -0
  227. package/dist/lib/skill/install.d.ts.map +1 -0
  228. package/dist/lib/skill/install.js +89 -0
  229. package/dist/lib/skill/install.js.map +1 -0
  230. package/dist/lib/workbench/app.d.ts +24 -0
  231. package/dist/lib/workbench/app.d.ts.map +1 -0
  232. package/dist/lib/workbench/app.js +45 -0
  233. package/dist/lib/workbench/app.js.map +1 -0
  234. package/dist/lib/workbench/assets.d.ts +21 -0
  235. package/dist/lib/workbench/assets.d.ts.map +1 -0
  236. package/dist/lib/workbench/assets.js +44 -0
  237. package/dist/lib/workbench/assets.js.map +1 -0
  238. package/dist/lib/workbench/client.d.ts +18 -0
  239. package/dist/lib/workbench/client.d.ts.map +1 -0
  240. package/dist/lib/workbench/client.js +736 -0
  241. package/dist/lib/workbench/client.js.map +1 -0
  242. package/dist/lib/workbench/drag.d.ts +90 -0
  243. package/dist/lib/workbench/drag.d.ts.map +1 -0
  244. package/dist/lib/workbench/drag.js +109 -0
  245. package/dist/lib/workbench/drag.js.map +1 -0
  246. package/dist/lib/workbench/geometry.d.ts +28 -0
  247. package/dist/lib/workbench/geometry.d.ts.map +1 -0
  248. package/dist/lib/workbench/geometry.js +28 -0
  249. package/dist/lib/workbench/geometry.js.map +1 -0
  250. package/dist/lib/workbench/select.d.ts +56 -0
  251. package/dist/lib/workbench/select.d.ts.map +1 -0
  252. package/dist/lib/workbench/select.js +111 -0
  253. package/dist/lib/workbench/select.js.map +1 -0
  254. package/dist/lib/workbench/server.d.ts +223 -0
  255. package/dist/lib/workbench/server.d.ts.map +1 -0
  256. package/dist/lib/workbench/server.js +576 -0
  257. package/dist/lib/workbench/server.js.map +1 -0
  258. package/dist/lib/workbench/source.d.ts +124 -0
  259. package/dist/lib/workbench/source.d.ts.map +1 -0
  260. package/dist/lib/workbench/source.js +405 -0
  261. package/dist/lib/workbench/source.js.map +1 -0
  262. package/docs/README.md +49 -0
  263. package/docs/cheatsheet.md +243 -0
  264. package/docs/cli.md +95 -0
  265. package/docs/data.md +112 -0
  266. package/docs/export.md +106 -0
  267. package/docs/getting-started.md +105 -0
  268. package/docs/language.md +136 -0
  269. package/docs/layout.md +159 -0
  270. package/docs/plugins.md +82 -0
  271. package/docs/reference.md +1156 -0
  272. package/docs/roadmap.md +54 -0
  273. package/docs/syntax.md +312 -0
  274. package/docs/workbench.md +125 -0
  275. package/examples/app.dm +76 -0
  276. package/examples/assets/avatar.png +0 -0
  277. package/examples/assets/cover.png +0 -0
  278. package/examples/assets/logo.svg +4 -0
  279. package/examples/assets.dm +49 -0
  280. package/examples/dashboard.dm +64 -0
  281. package/examples/data.dm +107 -0
  282. package/examples/layout.dm +93 -0
  283. package/examples/login.dm +43 -0
  284. package/examples/login.html +61 -0
  285. package/examples/plugins/rating/README.md +67 -0
  286. package/examples/plugins/rating/example.dm +29 -0
  287. package/examples/plugins/rating/index.ts +122 -0
  288. package/examples/plugins/rating/package.json +22 -0
  289. package/examples/recipe/assets/broccoli.jpg +0 -0
  290. package/examples/recipe/assets/cheesecake.jpg +0 -0
  291. package/examples/recipe/assets/chicken-pot.jpg +0 -0
  292. package/examples/recipe/assets/noodles.jpg +0 -0
  293. package/examples/recipe/assets/shrimp.jpg +0 -0
  294. package/examples/recipe/assets/tomato-beef.jpg +0 -0
  295. package/examples/recipe/detail.dm +105 -0
  296. package/examples/recipe/discover.dm +116 -0
  297. package/examples/recipe/favorites.dm +115 -0
  298. package/examples/recipe/index.dm +124 -0
  299. package/examples/recipe/profile.dm +134 -0
  300. package/examples/recipe/steps.dm +89 -0
  301. package/examples/settings.dm +108 -0
  302. package/package.json +84 -4
  303. package/skills/designmockdown/REFERENCE.md +373 -0
  304. package/skills/designmockdown/SKILL.md +179 -0
@@ -0,0 +1,1063 @@
1
+ /**
2
+ * DesignMockdown Layout Engine.
3
+ *
4
+ * Implements a sufficient flex/grid subset:
5
+ * - column (default): children stack top to bottom, width stretches by default;
6
+ * - row (@row): children lay out at their intrinsic width, flex=1 distributes the leftover space;
7
+ * - grid (@grid): equal-width cells, row major;
8
+ * - intrinsic size: every component may declare an intrinsic width and height.
9
+ *
10
+ * Conventions:
11
+ * - a box's children are positioned relative to its own origin; the parent only translates;
12
+ * - box.children are positioned by the layout engine; box.slots are measured only and placed by the component render function.
13
+ */
14
+ import { isContainer, isHidden } from "../core/ast.js";
15
+ import { FONT_SIZE_SCALE, FONT_WEIGHT_SCALE, RADIUS_SCALE, SHADOW_SCALE, SPACE_SCALE, lookupToken, } from "../core/tokens.js";
16
+ import { measureText, wrapText } from "./measure.js";
17
+ import { PIN_POSITIONS } from "./types.js";
18
+ import { DesignMockdownError } from "../core/errors.js";
19
+ import { getComponent } from "../components/registry.js";
20
+ const ZERO_INSET = { top: 0, right: 0, bottom: 0, left: 0 };
21
+ function isBox(item) {
22
+ return item.kind !== undefined;
23
+ }
24
+ /* ------------------------------------------------------------------ value parsing */
25
+ function toNum(v) {
26
+ if (typeof v === 'number')
27
+ return Number.isFinite(v) ? v : undefined;
28
+ if (typeof v === 'string') {
29
+ if (/^-?\d+(?:\.\d+)?$/.test(v))
30
+ return Number(v);
31
+ const m = /^(-?\d+(?:\.\d+)?)px$/.exec(v);
32
+ if (m)
33
+ return Number(m[1]);
34
+ }
35
+ return undefined;
36
+ }
37
+ function resolveColor(v, theme, fallback) {
38
+ if (v === undefined || v === true || typeof v === 'number')
39
+ return fallback;
40
+ const s = String(v);
41
+ if (s === 'none' || s === 'transparent')
42
+ return undefined;
43
+ return lookupToken(theme, s) ?? s;
44
+ }
45
+ function resolveLen(v, theme, fallback) {
46
+ if (v === undefined || v === true)
47
+ return fallback;
48
+ if (typeof v === 'number')
49
+ return v;
50
+ const s = String(v);
51
+ const n = toNum(s);
52
+ if (n !== undefined)
53
+ return n;
54
+ if (s in SPACE_SCALE)
55
+ return SPACE_SCALE[s];
56
+ const token = lookupToken(theme, s);
57
+ if (token !== undefined) {
58
+ const tn = toNum(token);
59
+ if (tn !== undefined)
60
+ return tn;
61
+ }
62
+ return fallback;
63
+ }
64
+ function resolveDim(v, fallback) {
65
+ if (v === undefined || v === true)
66
+ return { kind: fallback };
67
+ if (typeof v === 'number')
68
+ return { kind: 'px', value: v };
69
+ const s = String(v);
70
+ if (s === 'full' || s === '100%')
71
+ return { kind: 'full' };
72
+ if (s === 'fit' || s === 'auto' || s === 'content')
73
+ return { kind: 'auto' };
74
+ const pct = /^(\d+(?:\.\d+)?)%$/.exec(s);
75
+ if (pct)
76
+ return { kind: 'pct', value: Number(pct[1]) };
77
+ const n = toNum(s);
78
+ if (n !== undefined)
79
+ return { kind: 'px', value: n };
80
+ return { kind: fallback };
81
+ }
82
+ function resolveInset(v, theme, fallback) {
83
+ if (v === undefined || v === true)
84
+ return { top: fallback, right: fallback, bottom: fallback, left: fallback };
85
+ if (typeof v === 'number')
86
+ return { top: v, right: v, bottom: v, left: v };
87
+ const nums = String(v)
88
+ .split(/\s+/)
89
+ .filter(Boolean)
90
+ .map((p) => resolveLen(p, theme, fallback));
91
+ if (nums.length === 1)
92
+ return { top: nums[0], right: nums[0], bottom: nums[0], left: nums[0] };
93
+ if (nums.length === 2)
94
+ return { top: nums[0], right: nums[1], bottom: nums[0], left: nums[1] };
95
+ // Three values are CSS's top / horizontal / bottom shorthand; without it p=64 24 0 falls back entirely
96
+ if (nums.length === 3)
97
+ return { top: nums[0], right: nums[1], bottom: nums[2], left: nums[1] };
98
+ if (nums.length === 4)
99
+ return { top: nums[0], right: nums[1], bottom: nums[2], left: nums[3] };
100
+ return { top: fallback, right: fallback, bottom: fallback, left: fallback };
101
+ }
102
+ /**
103
+ * Expand the shorthand (p/m) into four sides, then let single-side attributes (pt/pr/pb/pl) override.
104
+ *
105
+ * The fallback must be the value already in baseStyle: passing 0 once silently
106
+ * overwrote the section default margin-top:32 and every module gap was lost.
107
+ */
108
+ function sideInset(attrs, theme, base, shorthand) {
109
+ const inset = shorthand === undefined ? base : resolveInset(shorthand, theme, base.top);
110
+ return {
111
+ top: attrs.pt !== undefined ? resolveLen(attrs.pt, theme, inset.top) : inset.top,
112
+ right: attrs.pr !== undefined ? resolveLen(attrs.pr, theme, inset.right) : inset.right,
113
+ bottom: attrs.pb !== undefined ? resolveLen(attrs.pb, theme, inset.bottom) : inset.bottom,
114
+ left: attrs.pl !== undefined ? resolveLen(attrs.pl, theme, inset.left) : inset.left,
115
+ };
116
+ }
117
+ function sideMargin(attrs, theme, base, shorthand) {
118
+ const inset = shorthand === undefined ? base : resolveInset(shorthand, theme, base.top);
119
+ return {
120
+ top: attrs.mt !== undefined ? resolveLen(attrs.mt, theme, inset.top) : inset.top,
121
+ right: attrs.mr !== undefined ? resolveLen(attrs.mr, theme, inset.right) : inset.right,
122
+ bottom: attrs.mb !== undefined ? resolveLen(attrs.mb, theme, inset.bottom) : inset.bottom,
123
+ left: attrs.ml !== undefined ? resolveLen(attrs.ml, theme, inset.left) : inset.left,
124
+ };
125
+ }
126
+ /* ------------------------------------------------------------- style= escape hatch */
127
+ /**
128
+ * The L1 escape hatch: `style="padding:16px;background:brand;border-radius:8px"`.
129
+ *
130
+ * Only the property families the layout engine can actually consume are supported (box model,
131
+ * colors, radius, font size). Anything else - calc(), var(), transforms - is silently skipped:
132
+ * the hatch is best-effort by design, and a design draft must never fail to render because of it.
133
+ * Declarations are separated by `;`, or by `,` when the next segment looks like `prop:`.
134
+ */
135
+ const STYLE_ESCAPE_PROPS = new Set([
136
+ 'padding', 'padding-top', 'padding-right', 'padding-bottom', 'padding-left',
137
+ 'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left',
138
+ 'background', 'background-color', 'border-radius', 'font-size', 'color',
139
+ ]);
140
+ export function applyStyleEscape(style, css, theme) {
141
+ for (const decl of css.split(/[;,](?=\s*[a-zA-Z-]+\s*:)/)) {
142
+ const idx = decl.indexOf(':');
143
+ if (idx < 0)
144
+ continue;
145
+ const prop = decl.slice(0, idx).trim().toLowerCase();
146
+ const raw = decl.slice(idx + 1).trim().replace(/^["']|["']$/g, '');
147
+ if (raw === '' || !STYLE_ESCAPE_PROPS.has(prop))
148
+ continue;
149
+ switch (prop) {
150
+ case 'padding':
151
+ style.padding = resolveInset(raw, theme, 0);
152
+ break;
153
+ case 'padding-top':
154
+ style.padding = { ...style.padding, top: resolveLen(raw, theme, style.padding.top) };
155
+ break;
156
+ case 'padding-right':
157
+ style.padding = { ...style.padding, right: resolveLen(raw, theme, style.padding.right) };
158
+ break;
159
+ case 'padding-bottom':
160
+ style.padding = { ...style.padding, bottom: resolveLen(raw, theme, style.padding.bottom) };
161
+ break;
162
+ case 'padding-left':
163
+ style.padding = { ...style.padding, left: resolveLen(raw, theme, style.padding.left) };
164
+ break;
165
+ case 'margin':
166
+ style.margin = resolveInset(raw, theme, 0);
167
+ break;
168
+ case 'margin-top':
169
+ style.margin = { ...style.margin, top: resolveLen(raw, theme, style.margin.top) };
170
+ break;
171
+ case 'margin-right':
172
+ style.margin = { ...style.margin, right: resolveLen(raw, theme, style.margin.right) };
173
+ break;
174
+ case 'margin-bottom':
175
+ style.margin = { ...style.margin, bottom: resolveLen(raw, theme, style.margin.bottom) };
176
+ break;
177
+ case 'margin-left':
178
+ style.margin = { ...style.margin, left: resolveLen(raw, theme, style.margin.left) };
179
+ break;
180
+ case 'background':
181
+ case 'background-color':
182
+ // 'none' / 'transparent' resolve to undefined, which correctly clears the background
183
+ style.bg = resolveColor(raw, theme, style.bg);
184
+ break;
185
+ case 'border-radius':
186
+ style.radius = resolveLen(raw, theme, style.radius ?? 0);
187
+ break;
188
+ case 'font-size':
189
+ style.fontSize = resolveLen(raw, theme, style.fontSize);
190
+ break;
191
+ case 'color':
192
+ style.color = resolveColor(raw, theme, style.color);
193
+ break;
194
+ default:
195
+ break;
196
+ }
197
+ }
198
+ }
199
+ /* ------------------------------------------------------------- style parsing */
200
+ /**
201
+ * Every attribute the style resolver understands.
202
+ *
203
+ * Exported because the workbench's property panel needs the same list to decide what it can offer
204
+ * for editing; a second copy would drift and the panel would show keys the resolver ignores.
205
+ */
206
+ export const STYLE_KEYS = new Set([
207
+ 'dir', 'wrap', 'justify', 'align', 'align-self', 'gap',
208
+ 'p', 'pt', 'pr', 'pb', 'pl', 'm', 'mt', 'mr', 'mb', 'ml',
209
+ 'w', 'h', 'flex', 'order', 'min-w', 'max-w', 'min-h', 'max-h',
210
+ 'bg', 'rounded', 'bw', 'border', 'shadow', 'op', 'position', 'x', 'y', 'z', 'overflow',
211
+ 'size', 'weight', 'line', 'ls', 'text-align', 'color', 'hidden',
212
+ ]);
213
+ function kindOf(node) {
214
+ switch (node.type) {
215
+ case 'page':
216
+ return 'page';
217
+ case 'section':
218
+ return 'section';
219
+ case 'text':
220
+ return 'text';
221
+ case 'divider':
222
+ return 'divider';
223
+ case 'annotation':
224
+ return 'annotation';
225
+ case 'theme':
226
+ return 'theme';
227
+ case 'if':
228
+ case 'for':
229
+ // Control flow is expanded away before layout; reaching here means the resolver was skipped
230
+ return node.type;
231
+ default:
232
+ return node.name;
233
+ }
234
+ }
235
+ function attrsOf(node) {
236
+ if (node.type === 'component') {
237
+ const def = getComponent(node.name);
238
+ return { ...(def?.defaults ?? {}), ...node.attrs };
239
+ }
240
+ if (node.type === 'divider')
241
+ return node.attrs;
242
+ return {};
243
+ }
244
+ function childrenOf(node) {
245
+ // Hidden children never reach arrangement: dropping them here (rather than in the renderer)
246
+ // is what gives their space back to the siblings, and it keeps every backend in agreement.
247
+ return isContainer(node) ? node.children.filter((c) => !isHidden(c)) : [];
248
+ }
249
+ function textOf(node) {
250
+ switch (node.type) {
251
+ case 'text':
252
+ return node.content;
253
+ case 'component':
254
+ return node.content;
255
+ case 'divider':
256
+ return node.content;
257
+ case 'annotation':
258
+ return node.content;
259
+ default:
260
+ return '';
261
+ }
262
+ }
263
+ function baseStyle(kind, theme) {
264
+ const font = toNum(lookupToken(theme, 'font')) ?? 14;
265
+ const common = {
266
+ direction: 'column',
267
+ wrap: false,
268
+ justifyContent: 'start',
269
+ alignItems: 'stretch',
270
+ gap: 0,
271
+ padding: ZERO_INSET,
272
+ margin: ZERO_INSET,
273
+ widthDim: { kind: 'auto' },
274
+ heightDim: { kind: 'auto' },
275
+ flexGrow: 0,
276
+ flexShrink: 1,
277
+ borderWidth: 0,
278
+ fontSize: font,
279
+ fontWeight: 400,
280
+ lineHeight: Math.round(font * 1.6),
281
+ textAlign: 'left',
282
+ letterSpacing: 0,
283
+ opacity: 1,
284
+ position: 'static',
285
+ overflow: 'visible',
286
+ extra: {},
287
+ };
288
+ if (kind === 'page')
289
+ return { ...common, bg: lookupToken(theme, 'bg') };
290
+ if (kind === 'section') {
291
+ const fs = toNum(lookupToken(theme, 'font-lg')) ?? 16;
292
+ return {
293
+ ...common,
294
+ gap: 12,
295
+ margin: { top: 32, right: 0, bottom: 0, left: 0 },
296
+ fontSize: fs,
297
+ fontWeight: 600,
298
+ lineHeight: Math.round(fs * 1.4),
299
+ };
300
+ }
301
+ if (kind === 'text')
302
+ return { ...common, color: lookupToken(theme, 'text') };
303
+ if (kind === 'divider') {
304
+ return { ...common, heightDim: { kind: 'px', value: 1 }, margin: { top: 16, right: 0, bottom: 16, left: 0 } };
305
+ }
306
+ if (kind === 'annotation') {
307
+ return { ...common, fontSize: 12, lineHeight: 18, color: '#8A6D1F' };
308
+ }
309
+ return common;
310
+ }
311
+ export function resolveStyle(node, theme) {
312
+ const kind = kindOf(node);
313
+ const style = baseStyle(kind, theme);
314
+ const attrs = attrsOf(node);
315
+ if (attrs.dir !== undefined)
316
+ style.direction = String(attrs.dir) === 'row' ? 'row' : 'column';
317
+ if (attrs.wrap !== undefined)
318
+ style.wrap = attrs.wrap === true || String(attrs.wrap) === 'true';
319
+ if (attrs.justify !== undefined)
320
+ style.justifyContent = String(attrs.justify);
321
+ if (attrs.align !== undefined)
322
+ style.alignItems = String(attrs.align);
323
+ if (attrs['align-self'] !== undefined)
324
+ style.alignSelf = String(attrs['align-self']);
325
+ style.gap = resolveLen(attrs.gap, theme, style.gap);
326
+ style.padding = sideInset(attrs, theme, style.padding, attrs.p);
327
+ style.margin = sideMargin(attrs, theme, style.margin, attrs.m);
328
+ style.widthDim = resolveDim(attrs.w, kind === 'page' ? 'full' : 'auto');
329
+ style.heightDim = resolveDim(attrs.h, 'auto');
330
+ style.flexGrow = toNum(attrs.flex) ?? 0;
331
+ if (attrs.order !== undefined)
332
+ style.order = toNum(attrs.order);
333
+ if (attrs['min-w'] !== undefined)
334
+ style.minWidth = resolveLen(attrs['min-w'], theme, 0);
335
+ if (attrs['max-w'] !== undefined)
336
+ style.maxWidth = resolveLen(attrs['max-w'], theme, Number.MAX_SAFE_INTEGER);
337
+ if (attrs['min-h'] !== undefined)
338
+ style.minHeight = resolveLen(attrs['min-h'], theme, 0);
339
+ if (attrs['max-h'] !== undefined)
340
+ style.maxHeight = resolveLen(attrs['max-h'], theme, Number.MAX_SAFE_INTEGER);
341
+ style.bg = resolveColor(attrs.bg, theme, style.bg);
342
+ if (attrs.rounded !== undefined) {
343
+ const key = RADIUS_SCALE[String(attrs.rounded)];
344
+ style.radius = key === undefined ? resolveLen(attrs.rounded, theme, 0) : (toNum(lookupToken(theme, key)) ?? 0);
345
+ }
346
+ style.borderWidth = resolveLen(attrs.bw, theme, 0);
347
+ style.borderColor = resolveColor(attrs.border, theme, style.borderColor);
348
+ if (attrs.shadow !== undefined) {
349
+ const key = SHADOW_SCALE[String(attrs.shadow)];
350
+ style.shadow = key === undefined || key === 'none' ? undefined : lookupToken(theme, key);
351
+ }
352
+ style.opacity = toNum(attrs.op) ?? 1;
353
+ if (attrs.position !== undefined)
354
+ style.position = String(attrs.position) === 'abs' ? 'abs' : 'static';
355
+ if (attrs.x !== undefined)
356
+ style.x = resolveLen(attrs.x, theme, 0);
357
+ if (attrs.y !== undefined)
358
+ style.y = resolveLen(attrs.y, theme, 0);
359
+ if (attrs.z !== undefined)
360
+ style.z = toNum(attrs.z);
361
+ // pin implies absolute positioning: a pinned box leaves the flow, exactly like position=abs
362
+ if (attrs.pin !== undefined) {
363
+ const p = String(attrs.pin);
364
+ if (!PIN_POSITIONS.includes(p)) {
365
+ throw new DesignMockdownError(`invalid pin position "${p}"`, { line: 1, col: 1 }, `expected one of: ${PIN_POSITIONS.join(', ')}`);
366
+ }
367
+ style.pin = p;
368
+ style.position = 'abs';
369
+ }
370
+ if (attrs.offset !== undefined)
371
+ style.pinOffset = resolveLen(attrs.offset, theme, 0);
372
+ if (attrs.overflow !== undefined)
373
+ style.overflow = String(attrs.overflow) === 'hidden' ? 'hidden' : 'visible';
374
+ if (attrs.size !== undefined) {
375
+ style.fontSize = FONT_SIZE_SCALE[String(attrs.size)] ?? resolveLen(attrs.size, theme, style.fontSize);
376
+ }
377
+ if (attrs.weight !== undefined) {
378
+ style.fontWeight = FONT_WEIGHT_SCALE[String(attrs.weight)] ?? (toNum(attrs.weight) ?? style.fontWeight);
379
+ }
380
+ if (attrs.line !== undefined)
381
+ style.lineHeight = resolveLen(attrs.line, theme, style.lineHeight);
382
+ if (attrs.ls !== undefined)
383
+ style.letterSpacing = resolveLen(attrs.ls, theme, 0);
384
+ if (attrs['text-align'] !== undefined)
385
+ style.textAlign = String(attrs['text-align']);
386
+ style.color = resolveColor(attrs.color, theme, style.color);
387
+ for (const [k, v] of Object.entries(attrs)) {
388
+ if (!STYLE_KEYS.has(k))
389
+ style.extra[k] = v;
390
+ }
391
+ // The escape hatch runs last so it wins over every shorthand attribute above
392
+ if (attrs.style !== undefined)
393
+ applyStyleEscape(style, String(attrs.style), theme);
394
+ return style;
395
+ }
396
+ /* --------------------------------------------------------------- layout pipeline */
397
+ function makeBox(node, kind, style) {
398
+ // The source node is kept on the box so non-SVG backends (DOM, React) can read component attrs
399
+ // such as `src` / `ph` / `rows` that only exist on the AST, not on the resolved Style
400
+ return { id: node.id, kind, style, x: 0, y: 0, width: 0, height: 0, children: [], slots: {}, node };
401
+ }
402
+ function makeText(text, style, id) {
403
+ return { id, kind: 'text', style, x: 0, y: 0, width: 0, height: 0, children: [], slots: {}, text };
404
+ }
405
+ function intrinsicLeaf(node, style, theme) {
406
+ if (node.type === 'component') {
407
+ const def = getComponent(node.name);
408
+ if (def?.intrinsic)
409
+ return def.intrinsic(attrsOf(node), style, theme, textOf(node));
410
+ }
411
+ if (node.type === 'divider')
412
+ return { height: 1 };
413
+ return {};
414
+ }
415
+ /**
416
+ * Does this node paint its inline content as a text child?
417
+ *
418
+ * Only pure containers opt in (textAsChild). Everything else either draws box.text itself or ignores
419
+ * it, and page/section titles are never drawn at all.
420
+ */
421
+ function paintsInlineText(node) {
422
+ if (node.type !== 'component')
423
+ return false;
424
+ return getComponent(node.name)?.textAsChild === true;
425
+ }
426
+ /**
427
+ * Build the measured text child for a container's inline content (`@card 标题`).
428
+ *
429
+ * Measuring here is what makes the title visible: drawLines emits nothing for a box without lines,
430
+ * and an unmeasured box also contributes no height, so the title would be invisible AND free.
431
+ */
432
+ function makeInlineText(content, style, contentWidth, theme, id) {
433
+ const textStyle = {
434
+ ...style,
435
+ // Containers carry no text color of their own; fall back to the theme's body color so a
436
+ // card title does not turn black on a dark canvas
437
+ color: style.color ?? lookupToken(theme, 'text'),
438
+ padding: ZERO_INSET,
439
+ margin: ZERO_INSET,
440
+ widthDim: { kind: 'auto' },
441
+ heightDim: { kind: 'auto' },
442
+ };
443
+ const textBox = makeText(content, textStyle, id);
444
+ textBox.width = contentWidth;
445
+ textBox.lines = wrapText(content, contentWidth, textStyle.fontSize);
446
+ textBox.height = textBox.lines.length * textStyle.lineHeight;
447
+ return textBox;
448
+ }
449
+ /**
450
+ * A container's "max content width": the width needed when no child wraps.
451
+ * Row direction sums the children's widths; column direction takes the widest one.
452
+ *
453
+ * Why this exists: measuring a row child's intrinsic width goes through inline measurement,
454
+ * and containers such as @col/@box have no intrinsic and no inline text, so they measured 0 wide
455
+ * and their text broke per character (that is how the "Edit profile" row in settings.dm broke).
456
+ */
457
+ function maxContentWidth(item, theme) {
458
+ // Already a laid-out box, read its width directly
459
+ if (isBox(item))
460
+ return item.width + item.style.margin.left + item.style.margin.right;
461
+ const node = item;
462
+ const style = styleOf(node, theme);
463
+ const pad = style.padding.left + style.padding.right;
464
+ if (style.widthDim.kind === 'px')
465
+ return style.widthDim.value + pad;
466
+ // pct / full depend on the parent width, which is unknown here; leave it to flex or stretching
467
+ if (style.widthDim.kind === 'pct' || style.widthDim.kind === 'full')
468
+ return 0;
469
+ if (node.type === 'component') {
470
+ const def = getComponent(node.name);
471
+ if (def?.intrinsic) {
472
+ const leaf = def.intrinsic(attrsOf(node), style, theme, textOf(node));
473
+ if (leaf.width !== undefined)
474
+ return leaf.width + pad;
475
+ }
476
+ }
477
+ const text = textOf(node);
478
+ if (text !== '' && childrenOf(node).length === 0) {
479
+ return measureText(text, style.fontSize, style.fontWeight) + pad;
480
+ }
481
+ const kids = childrenOf(node);
482
+ if (kids.length === 0)
483
+ return pad;
484
+ const widths = kids.map((k) => {
485
+ const ks = styleOf(k, theme);
486
+ return maxContentWidth(k, theme) + ks.margin.left + ks.margin.right;
487
+ });
488
+ const content = style.direction === 'row'
489
+ ? widths.reduce((a, b) => a + b, 0) + style.gap * Math.max(0, kids.length - 1)
490
+ : Math.max(...widths);
491
+ return content + pad;
492
+ }
493
+ /** `rounded=full` resolves to this sentinel (see tokens.ts) instead of a real length */
494
+ const FULL_RADIUS = 9999;
495
+ /**
496
+ * The height a box will end up with, before it is applied. Callers that position children on the
497
+ * block axis (justify) need it early: explicit heights are stamped on after arranging, so measuring
498
+ * against the parent's availHeight leaves `justify=center` with no free space to distribute.
499
+ */
500
+ function resolvedHeight(s, availHeight, flow) {
501
+ let h = flow;
502
+ if (s.heightDim.kind === 'px')
503
+ h = s.heightDim.value;
504
+ else if (s.heightDim.kind === 'full' && Number.isFinite(availHeight))
505
+ h = availHeight;
506
+ else if (s.heightDim.kind === 'pct' && Number.isFinite(availHeight))
507
+ h = (availHeight * s.heightDim.value) / 100;
508
+ if (s.minHeight !== undefined)
509
+ h = Math.max(h, s.minHeight);
510
+ if (s.maxHeight !== undefined)
511
+ h = Math.min(h, s.maxHeight);
512
+ return h;
513
+ }
514
+ function applyExplicitHeight(box, availHeight) {
515
+ box.height = resolvedHeight(box.style, availHeight, box.height);
516
+ // SVG clamps a rect's rx to half the width and ry to half the height independently, so the
517
+ // sentinel renders a wide pill as an ellipse (pointed ends). Pin it to the short side here, and
518
+ // every consumer - chrome, clip paths, CSS border-radius, inspector handles - sees one number.
519
+ if (box.style.radius !== undefined && box.style.radius >= FULL_RADIUS) {
520
+ box.style.radius = Math.min(box.width, box.height) / 2;
521
+ }
522
+ }
523
+ /**
524
+ * Height to arrange children against, or undefined when the box is auto-sized. Only an explicitly
525
+ * sized box knows its final block size this early; an auto-height box must keep using the parent's
526
+ * availHeight, or justify/align would hand out space the box is never going to have.
527
+ */
528
+ function explicitBlockHeight(s, availHeight, flow) {
529
+ const sized = s.heightDim.kind !== 'auto' || s.minHeight !== undefined || s.maxHeight !== undefined;
530
+ return sized ? resolvedHeight(s, availHeight, flow) : undefined;
531
+ }
532
+ function crossOffset(child, contentSize, alignItems, alignSelf) {
533
+ const a = alignSelf ?? alignItems;
534
+ if (a === 'center')
535
+ return Math.max(0, (contentSize - child.height) / 2);
536
+ if (a === 'end')
537
+ return Math.max(0, contentSize - child.height);
538
+ return 0;
539
+ }
540
+ /** Column cross-axis (horizontal) offset: positions fixed-width children per alignItems */
541
+ function crossOffsetX(child, contentSize, alignItems, alignSelf) {
542
+ const a = alignSelf ?? alignItems;
543
+ const free = contentSize - child.width;
544
+ if (free <= 0)
545
+ return 0;
546
+ if (a === 'center')
547
+ return free / 2;
548
+ if (a === 'end')
549
+ return free;
550
+ return 0;
551
+ }
552
+ function outerHeight(b) {
553
+ return b.height + b.style.margin.top + b.style.margin.bottom;
554
+ }
555
+ function styleOf(item, theme) {
556
+ return isBox(item) ? item.style : resolveStyle(item, theme);
557
+ }
558
+ function toBox(item, contentWidth, theme, inline, override) {
559
+ return isBox(item) ? item : layoutNode(item, 0, 0, contentWidth, Infinity, inline, theme, override);
560
+ }
561
+ /**
562
+ * Stable sort by the `order` attribute. Items without `order` keep their source position,
563
+ * so adding order= to one child never reorders the rest.
564
+ */
565
+ function sortByOrder(items, theme) {
566
+ const orders = items.map((it) => styleOf(it, theme).order);
567
+ if (orders.every((o) => o === undefined))
568
+ return items;
569
+ return items
570
+ .map((it, i) => ({ it, i, o: orders[i] ?? 0 }))
571
+ .sort((a, b) => a.o - b.o || a.i - b.i)
572
+ .map((x) => x.it);
573
+ }
574
+ /**
575
+ * Intrinsic outer width (margins included) of an item.
576
+ * Row layout and absolute positioning share it so both agree on "how wide is this naturally".
577
+ */
578
+ function intrinsicOuterWidth(item, contentWidth, theme) {
579
+ const cs = styleOf(item, theme);
580
+ const m = cs.margin.left + cs.margin.right;
581
+ if (cs.widthDim.kind === 'full')
582
+ return contentWidth;
583
+ if (cs.widthDim.kind === 'pct')
584
+ return (contentWidth * cs.widthDim.value) / 100;
585
+ if (cs.widthDim.kind === 'px')
586
+ return cs.widthDim.value;
587
+ // Already a laid-out box, read its width directly
588
+ if (isBox(item))
589
+ return item.width + m;
590
+ // Components with an intrinsic size (button/input/icon...) use their own width
591
+ const leaf = intrinsicLeaf(item, cs, theme);
592
+ if (leaf.width !== undefined)
593
+ return leaf.width + m;
594
+ // Containers have no intrinsic, so use max content width or they collapse to 0
595
+ if (childrenOf(item).length > 0)
596
+ return maxContentWidth(item, theme) + m;
597
+ return toBox(item, contentWidth, theme, true).width + m;
598
+ }
599
+ /** Split items into the flow sequence and the absolutely positioned ones */
600
+ function splitAbs(items, theme) {
601
+ const flow = [];
602
+ const abs = [];
603
+ for (const it of items) {
604
+ if (styleOf(it, theme).position === 'abs')
605
+ abs.push(it);
606
+ else
607
+ flow.push(it);
608
+ }
609
+ return { flow, abs };
610
+ }
611
+ /**
612
+ * Place absolutely positioned children.
613
+ *
614
+ * They leave the flow entirely (they contribute no height) and are painted last, ordered by z so a
615
+ * higher z lands on top. They must stay inside box.children - the renderer only walks that array,
616
+ * so removing them here would make them invisible.
617
+ *
618
+ * A `pin` child gets a provisional placement here; `repositionPinned` re-places it once the parent's
619
+ * final height is known, because the arrange pass runs before the explicit height is applied.
620
+ */
621
+ function placeAbs(items, contentWidth, theme, style) {
622
+ const boxes = items.map((it) => {
623
+ const cs = styleOf(it, theme);
624
+ const w = intrinsicOuterWidth(it, contentWidth, theme);
625
+ const override = cs.widthDim.kind === 'auto' ? Math.max(0, w - cs.margin.left - cs.margin.right) : undefined;
626
+ const b = toBox(it, w, theme, false, override);
627
+ b.x = style.padding.left + (cs.x ?? 0);
628
+ b.y = style.padding.top + (cs.y ?? 0);
629
+ return b;
630
+ });
631
+ boxes.sort((a, b) => (a.style.z ?? 0) - (b.style.z ?? 0));
632
+ return boxes;
633
+ }
634
+ export function layoutNode(node, x, y, availWidth, availHeight, inline, theme, widthOverride) {
635
+ const kind = kindOf(node);
636
+ const style = resolveStyle(node, theme);
637
+ if (node.type === 'component' && node.name === 'table' && node.rows) {
638
+ style.extra.rows = node.rows;
639
+ }
640
+ const box = makeBox(node, kind, style);
641
+ // --- width ---
642
+ let width;
643
+ if (widthOverride !== undefined)
644
+ width = widthOverride;
645
+ else if (style.widthDim.kind === 'full')
646
+ width = availWidth;
647
+ else if (style.widthDim.kind === 'pct')
648
+ width = (availWidth * style.widthDim.value) / 100;
649
+ else if (style.widthDim.kind === 'px')
650
+ width = style.widthDim.value;
651
+ else if (inline) {
652
+ const leaf = intrinsicLeaf(node, style, theme);
653
+ width = leaf.width ?? measureText(textOf(node), style.fontSize, style.fontWeight);
654
+ }
655
+ else {
656
+ width = availWidth;
657
+ }
658
+ if (style.minWidth !== undefined)
659
+ width = Math.max(width, style.minWidth);
660
+ if (style.maxWidth !== undefined)
661
+ width = Math.min(width, style.maxWidth);
662
+ box.width = Math.max(0, width);
663
+ box.x = x;
664
+ box.y = y;
665
+ const contentWidth = Math.max(0, box.width - style.padding.left - style.padding.right);
666
+ const content = textOf(node);
667
+ if (content !== '') {
668
+ box.text = content;
669
+ box.lines = wrapText(content, contentWidth, style.fontSize);
670
+ }
671
+ const childNodes = childrenOf(node);
672
+ // Slot children: measured only, excluded from flow height, placed by the component render function
673
+ if (isContainer(node)) {
674
+ for (const [slotName, list] of Object.entries(node.slots)) {
675
+ // @sidebar places its own main slot (it needs a real width, not an inline measurement)
676
+ if (kind === 'sidebar' && slotName === 'main')
677
+ continue;
678
+ for (const slotNode of list) {
679
+ // A hidden slot child (an icon in `slot=append`) must not be measured either: the component
680
+ // render function places slots from these boxes, so a hidden one would still paint.
681
+ if (isHidden(slotNode))
682
+ continue;
683
+ const slotBox = layoutNode(slotNode, 0, 0, contentWidth, Infinity, true, theme);
684
+ (box.slots[slotName] ??= []).push(slotBox);
685
+ }
686
+ }
687
+ }
688
+ // A container's inline content becomes a real text child, but only for components that opt in
689
+ // via textAsChild. page/section titles are grouping markers the renderer never draws, so they must
690
+ // not take layout space either - otherwise an empty title earns a free gap.
691
+ // The box has to be measured here: an unmeasured text box has no lines, and drawLines then emits
692
+ // nothing, which is how `@card 标题` used to lose its title silently.
693
+ const inlineText = content !== '' && paintsInlineText(node) ? makeInlineText(content, style, contentWidth, theme, `${node.id}-content`) : undefined;
694
+ // A container carrying only inline text must still take the container path: the leaf path below
695
+ // would return before the text box is ever created, so `@card 标题` (no explicit children) vanished
696
+ if (childNodes.length === 0 && inlineText === undefined) {
697
+ const leaf = intrinsicLeaf(node, style, theme);
698
+ if (leaf.height !== undefined)
699
+ box.height = leaf.height;
700
+ else if (box.lines)
701
+ box.height = box.lines.length * style.lineHeight;
702
+ else
703
+ box.height = 0;
704
+ box.height += style.padding.top + style.padding.bottom;
705
+ applyExplicitHeight(box, availHeight);
706
+ return box;
707
+ }
708
+ // --- container ---
709
+ const items = [];
710
+ if (inlineText)
711
+ items.push(inlineText);
712
+ items.push(...childNodes);
713
+ if (kind === 'stack') {
714
+ const r = arrangeStack(items, contentWidth, theme, style);
715
+ box.children = r.boxes;
716
+ box.height = r.height;
717
+ }
718
+ else if (kind === 'form') {
719
+ const r = arrangeForm(items, contentWidth, theme, style);
720
+ box.children = r.boxes;
721
+ box.height = r.height;
722
+ }
723
+ else if (kind === 'sidebar') {
724
+ const main = isContainer(node) ? (node.slots.main ?? []).filter((c) => !isHidden(c)) : [];
725
+ const r = arrangeSidebar(items, main, contentWidth, theme, style);
726
+ box.children = r.boxes;
727
+ box.height = r.height;
728
+ }
729
+ else if (kind === 'grid') {
730
+ const r = arrangeGrid(items, contentWidth, theme, style);
731
+ box.children = r.boxes;
732
+ box.height = r.height;
733
+ }
734
+ else if (style.direction === 'row') {
735
+ const r = arrangeRow(items, contentWidth, theme, style, explicitBlockHeight(style, availHeight, box.height));
736
+ box.children = r.boxes;
737
+ box.height = r.height;
738
+ }
739
+ else {
740
+ const columnHeight = explicitBlockHeight(style, availHeight, box.height) ?? availHeight;
741
+ const r = arrangeColumn(items, contentWidth, columnHeight, theme, style, kind === 'page');
742
+ box.children = r.boxes;
743
+ box.height = r.height;
744
+ }
745
+ // A component with a declared intrinsic height (scroll / stack / sidebar) keeps it when it has no
746
+ // explicit children, so `@scroll 标题` does not collapse to a single line of text
747
+ if (childNodes.length === 0) {
748
+ box.height = Math.max(box.height, intrinsicLeaf(node, style, theme).height ?? 0);
749
+ }
750
+ applyExplicitHeight(box, availHeight);
751
+ // Pinned children are placed against the parent's final box, which is only known after the
752
+ // explicit height has been applied - arranging them earlier would use the flow height and put a
753
+ // bottom-pinned child above the bottom edge
754
+ repositionPinned(box);
755
+ return box;
756
+ }
757
+ /**
758
+ * Re-place `pin` children against the parent's final content box.
759
+ *
760
+ * The arrange pass runs before the parent's explicit height is known, so a bottom-pinned child
761
+ * would be measured against the flow height. This second pass runs once box.height is final.
762
+ */
763
+ function repositionPinned(box) {
764
+ const style = box.style;
765
+ const innerW = Math.max(0, box.width - style.padding.left - style.padding.right);
766
+ const innerH = Math.max(0, box.height - style.padding.top - style.padding.bottom);
767
+ for (const child of box.children) {
768
+ const cs = child.style;
769
+ if (!cs.pin)
770
+ continue;
771
+ const off = cs.pinOffset ?? 0;
772
+ // A bare "top"/"bottom"/"left"/"right" pins one axis and centres the other, which is what a
773
+ // badge or a FAB wants. "center" centres both.
774
+ const parts = cs.pin.split('-');
775
+ const v = parts.length === 2 ? parts[0] : cs.pin === 'left' || cs.pin === 'right' ? 'center' : cs.pin;
776
+ const h = parts.length === 2 ? parts[1] : cs.pin === 'top' || cs.pin === 'bottom' ? 'center' : cs.pin;
777
+ if (v === 'top')
778
+ child.y = off;
779
+ else if (v === 'bottom')
780
+ child.y = innerH - child.height - off;
781
+ else
782
+ child.y = (innerH - child.height) / 2;
783
+ if (h === 'left')
784
+ child.x = off;
785
+ else if (h === 'right')
786
+ child.x = innerW - child.width - off;
787
+ else
788
+ child.x = (innerW - child.width) / 2;
789
+ }
790
+ }
791
+ function arrangeColumn(items, contentWidth, availHeight, theme, style, isPageRoot = false) {
792
+ const gap = style.gap;
793
+ const { flow, abs } = splitAbs(items, theme);
794
+ const boxes = sortByOrder(flow, theme).map((it, i) => {
795
+ const b = toBox(it, contentWidth, theme, false);
796
+ // The first section on the page keeps no margin, or the canvas top gains a blank strip
797
+ if (isPageRoot && i === 0)
798
+ b.style.margin = { ...b.style.margin, top: 0 };
799
+ return b;
800
+ });
801
+ const total = boxes.reduce((a, b) => a + outerHeight(b), 0) + gap * Math.max(0, boxes.length - 1);
802
+ let startY = style.padding.top;
803
+ let step = gap;
804
+ if (Number.isFinite(availHeight)) {
805
+ const free = availHeight - style.padding.top - style.padding.bottom - total;
806
+ if (free > 0 && boxes.length > 0) {
807
+ if (style.justifyContent === 'center')
808
+ startY = style.padding.top + free / 2;
809
+ else if (style.justifyContent === 'end')
810
+ startY = style.padding.top + free;
811
+ else if (style.justifyContent === 'between' && boxes.length > 1)
812
+ step = gap + free / (boxes.length - 1);
813
+ else if (style.justifyContent === 'around')
814
+ step = gap + free / boxes.length;
815
+ }
816
+ }
817
+ let y = startY;
818
+ for (const b of boxes) {
819
+ const cs = b.style;
820
+ // When the parent asks for center/end, text inherits textAlign (the render layer supports it),
821
+ // while fixed-width children (avatars, icons) get their x from crossOffsetX
822
+ if (cs.textAlign === 'left') {
823
+ if (style.alignItems === 'center')
824
+ cs.textAlign = 'center';
825
+ else if (style.alignItems === 'end')
826
+ cs.textAlign = 'right';
827
+ }
828
+ b.x = style.padding.left + cs.margin.left + crossOffsetX(b, contentWidth - cs.margin.left - cs.margin.right, style.alignItems, cs.alignSelf);
829
+ b.y = y + cs.margin.top;
830
+ y += outerHeight(b) + step;
831
+ }
832
+ // Absolutely positioned children are appended last so they paint above the flow
833
+ boxes.push(...placeAbs(abs, contentWidth, theme, style));
834
+ return { boxes, height: total + style.padding.top + style.padding.bottom };
835
+ }
836
+ function arrangeRow(items, contentWidth, theme, style, blockH) {
837
+ const { flow, abs } = splitAbs(sortByOrder(items, theme), theme);
838
+ const r = style.wrap
839
+ ? arrangeRowWrapped(flow, contentWidth, theme, style)
840
+ : arrangeRowSingle(flow, contentWidth, theme, style, blockH);
841
+ return { boxes: [...r.boxes, ...placeAbs(abs, contentWidth, theme, style)], height: r.height };
842
+ }
843
+ function arrangeRowSingle(items, contentWidth, theme, style, blockH) {
844
+ const gap = style.gap;
845
+ const childStyles = items.map((it) => styleOf(it, theme));
846
+ const intrinsic = items.map((it) => intrinsicOuterWidth(it, contentWidth, theme));
847
+ const growTotal = childStyles.reduce((a, cs) => a + cs.flexGrow, 0);
848
+ const total = intrinsic.reduce((a, b) => a + b, 0) + gap * Math.max(0, items.length - 1);
849
+ const free = contentWidth - total;
850
+ const widths = intrinsic.slice();
851
+ if (free > 0 && growTotal > 0) {
852
+ for (let i = 0; i < widths.length; i++) {
853
+ const g = childStyles[i].flexGrow;
854
+ if (g > 0)
855
+ widths[i] += (free * g) / growTotal;
856
+ }
857
+ }
858
+ const boxes = items.map((it, i) => {
859
+ const cs = childStyles[i];
860
+ const override = cs.widthDim.kind === 'auto' ? Math.max(0, widths[i] - cs.margin.left - cs.margin.right) : undefined;
861
+ return toBox(it, widths[i], theme, false, override);
862
+ });
863
+ const flowHeight = Math.max(0, ...boxes.map((b) => outerHeight(b)));
864
+ // A row inside a box with an explicit height has more cross-axis room than its tallest child, so
865
+ // align=center/end measure against the box instead of the flow. The returned height stays the
866
+ // flow height: the box's own height is stamped on afterwards by applyExplicitHeight.
867
+ const innerH = blockH === undefined ? flowHeight : Math.max(flowHeight, blockH - style.padding.top - style.padding.bottom);
868
+ // Main axis (horizontal) distribution: justifyContent decides how the leftover space is used
869
+ const usedWidth = boxes.reduce((a, b) => a + b.width + b.style.margin.left + b.style.margin.right, 0) +
870
+ gap * Math.max(0, boxes.length - 1);
871
+ const freeW = Math.max(0, contentWidth - usedWidth);
872
+ let startX = style.padding.left;
873
+ let step = gap;
874
+ if (style.justifyContent === 'center')
875
+ startX = style.padding.left + freeW / 2;
876
+ else if (style.justifyContent === 'end')
877
+ startX = style.padding.left + freeW;
878
+ else if (style.justifyContent === 'between' && boxes.length > 1)
879
+ step = gap + freeW / (boxes.length - 1);
880
+ else if (style.justifyContent === 'around' && boxes.length > 0) {
881
+ startX = style.padding.left + freeW / boxes.length / 2;
882
+ step = gap + freeW / boxes.length;
883
+ }
884
+ let x = startX;
885
+ for (const b of boxes) {
886
+ b.x = x + b.style.margin.left;
887
+ b.y = style.padding.top + b.style.margin.top + crossOffset(b, innerH - b.style.margin.top - b.style.margin.bottom, style.alignItems, style.alignSelf);
888
+ x += b.width + b.style.margin.left + b.style.margin.right + step;
889
+ }
890
+ return { boxes, height: flowHeight + style.padding.top + style.padding.bottom };
891
+ }
892
+ /**
893
+ * wrap=true: break the row into lines. Each line is laid out and justified on its own,
894
+ * and the container height is the sum of the line heights - not the height of the tallest child.
895
+ */
896
+ function arrangeRowWrapped(items, contentWidth, theme, style) {
897
+ const gap = style.gap;
898
+ const childStyles = items.map((it) => styleOf(it, theme));
899
+ const widths = items.map((it) => intrinsicOuterWidth(it, contentWidth, theme));
900
+ // Greedy line breaking: an item that does not fit in what is left starts a new line
901
+ const lines = [];
902
+ let cur = [];
903
+ let curW = 0;
904
+ for (let i = 0; i < items.length; i++) {
905
+ if (cur.length > 0 && curW + gap + widths[i] > contentWidth) {
906
+ lines.push(cur);
907
+ cur = [];
908
+ curW = 0;
909
+ }
910
+ cur.push(i);
911
+ curW += (cur.length > 1 ? gap : 0) + widths[i];
912
+ }
913
+ if (cur.length > 0)
914
+ lines.push(cur);
915
+ const boxes = [];
916
+ let y = style.padding.top;
917
+ for (const line of lines) {
918
+ // flex growers share the leftover space of their own line, not of the whole container
919
+ const lineWidths = line.map((i) => widths[i]);
920
+ const used = lineWidths.reduce((a, b) => a + b, 0) + gap * Math.max(0, line.length - 1);
921
+ const free = contentWidth - used;
922
+ const growTotal = line.reduce((a, i) => a + childStyles[i].flexGrow, 0);
923
+ if (free > 0 && growTotal > 0) {
924
+ for (let k = 0; k < line.length; k++) {
925
+ const g = childStyles[line[k]].flexGrow;
926
+ if (g > 0)
927
+ lineWidths[k] += (free * g) / growTotal;
928
+ }
929
+ }
930
+ const lineBoxes = line.map((i, k) => {
931
+ const cs = childStyles[i];
932
+ const override = cs.widthDim.kind === 'auto' ? Math.max(0, lineWidths[k] - cs.margin.left - cs.margin.right) : undefined;
933
+ return toBox(items[i], lineWidths[k], theme, false, override);
934
+ });
935
+ const rowHeight = Math.max(0, ...lineBoxes.map((b) => outerHeight(b)));
936
+ const usedW = lineBoxes.reduce((a, b) => a + b.width + b.style.margin.left + b.style.margin.right, 0) +
937
+ gap * Math.max(0, lineBoxes.length - 1);
938
+ const freeW = Math.max(0, contentWidth - usedW);
939
+ let startX = style.padding.left;
940
+ let step = gap;
941
+ if (style.justifyContent === 'center')
942
+ startX = style.padding.left + freeW / 2;
943
+ else if (style.justifyContent === 'end')
944
+ startX = style.padding.left + freeW;
945
+ else if (style.justifyContent === 'between' && lineBoxes.length > 1)
946
+ step = gap + freeW / (lineBoxes.length - 1);
947
+ else if (style.justifyContent === 'around' && lineBoxes.length > 0) {
948
+ startX = style.padding.left + freeW / lineBoxes.length / 2;
949
+ step = gap + freeW / lineBoxes.length;
950
+ }
951
+ let x = startX;
952
+ for (const b of lineBoxes) {
953
+ b.x = x + b.style.margin.left;
954
+ b.y = y + b.style.margin.top + crossOffset(b, rowHeight - b.style.margin.top - b.style.margin.bottom, style.alignItems, style.alignSelf);
955
+ x += b.width + b.style.margin.left + b.style.margin.right + step;
956
+ boxes.push(b);
957
+ }
958
+ y += rowHeight + gap;
959
+ }
960
+ return { boxes, height: Math.max(0, y - gap) + style.padding.bottom };
961
+ }
962
+ function arrangeGrid(items, contentWidth, theme, style) {
963
+ const cols = Math.max(1, toNum(style.extra.cols) ?? 2);
964
+ const gap = style.gap;
965
+ const cellWidth = (contentWidth - gap * (cols - 1)) / cols;
966
+ const { flow, abs } = splitAbs(sortByOrder(items, theme), theme);
967
+ // col=span N lets a child occupy N consecutive cells
968
+ const cells = flow.map((it) => {
969
+ const cs = styleOf(it, theme);
970
+ const span = Math.max(1, Math.min(cols, Math.floor(toNum(cs.extra.span) ?? 1)));
971
+ const w = cellWidth * span + gap * (span - 1);
972
+ return { box: toBox(it, w, theme, false), span };
973
+ });
974
+ // Cursor placement: a span that does not fit in the remaining cells wraps to the next row
975
+ const rows = [[]];
976
+ let col = 0;
977
+ for (const cell of cells) {
978
+ if (col + cell.span > cols) {
979
+ rows.push([]);
980
+ col = 0;
981
+ }
982
+ rows[rows.length - 1].push(cell);
983
+ col += cell.span;
984
+ if (col >= cols) {
985
+ rows.push([]);
986
+ col = 0;
987
+ }
988
+ }
989
+ if (rows.length > 1 && rows[rows.length - 1].length === 0)
990
+ rows.pop();
991
+ let rowY = style.padding.top;
992
+ for (const row of rows) {
993
+ const rowHeight = Math.max(0, ...row.map((c) => outerHeight(c.box)));
994
+ let c2 = 0;
995
+ for (const cell of row) {
996
+ cell.box.x = style.padding.left + c2 * (cellWidth + gap) + cell.box.style.margin.left;
997
+ cell.box.y = rowY + cell.box.style.margin.top;
998
+ c2 += cell.span;
999
+ }
1000
+ rowY += rowHeight + gap;
1001
+ }
1002
+ const height = Math.max(0, rowY - gap) + style.padding.bottom;
1003
+ return { boxes: [...cells.map((c) => c.box), ...placeAbs(abs, contentWidth, theme, style)], height };
1004
+ }
1005
+ /** @stack: every child covers the same content area; later children paint on top */
1006
+ function arrangeStack(items, contentWidth, theme, style) {
1007
+ const boxes = sortByOrder(items, theme).map((it) => toBox(it, contentWidth, theme, false));
1008
+ for (const b of boxes) {
1009
+ // A child that set its own x/y keeps it: stacking everything at the origin is the default, but
1010
+ // an offset child is how a stack shows its layers instead of hiding all but the top one
1011
+ b.x = b.style.x ?? style.padding.left + b.style.margin.left;
1012
+ b.y = b.style.y ?? style.padding.top + b.style.margin.top;
1013
+ }
1014
+ const inner = Math.max(0, ...boxes.map((b) => b.height + b.style.margin.top + b.style.margin.bottom));
1015
+ return { boxes, height: inner + style.padding.top + style.padding.bottom };
1016
+ }
1017
+ /** Height reserved above a labelled @form field for its caption */
1018
+ const FORM_LABEL_H = 19;
1019
+ /** @form: a column of fields; a child carrying label= gets a caption line above it */
1020
+ function arrangeForm(items, contentWidth, theme, style) {
1021
+ const gap = style.gap;
1022
+ const boxes = [];
1023
+ let y = style.padding.top;
1024
+ for (const it of sortByOrder(items, theme)) {
1025
+ const cs = styleOf(it, theme);
1026
+ const labelH = cs.extra.label !== undefined ? FORM_LABEL_H : 0;
1027
+ const b = toBox(it, contentWidth, theme, false);
1028
+ if (labelH > 0)
1029
+ b.style.extra.labelH = labelH;
1030
+ b.x = style.padding.left + b.style.margin.left;
1031
+ b.y = y + b.style.margin.top + labelH;
1032
+ y += labelH + outerHeight(b) + gap;
1033
+ boxes.push(b);
1034
+ }
1035
+ return { boxes, height: Math.max(0, y - gap) + style.padding.bottom };
1036
+ }
1037
+ /**
1038
+ * @sidebar: a fixed-width panel on the left plus a main area.
1039
+ * Children flow inside the panel; children sent with slot=main fill the rest of the width.
1040
+ */
1041
+ function arrangeSidebar(items, mainNodes, contentWidth, theme, style) {
1042
+ // panel= is the side panel's width; w stays the box's own width like everywhere else
1043
+ const panel = Math.max(0, Math.min(contentWidth, toNum(style.extra.panel) ?? 240));
1044
+ const sideContentW = Math.max(0, panel - style.padding.left - style.padding.right);
1045
+ const mainContentW = Math.max(0, contentWidth - panel - style.padding.right);
1046
+ const r1 = arrangeColumn(items, sideContentW, Infinity, theme, style);
1047
+ // The main area starts at the panel's right edge, so its left padding must not be counted twice
1048
+ const mainStyle = { ...style, padding: { ...style.padding, left: 0 } };
1049
+ const r2 = arrangeColumn(mainNodes, mainContentW, Infinity, theme, mainStyle);
1050
+ for (const b of r2.boxes)
1051
+ b.x += panel;
1052
+ return { boxes: [...r1.boxes, ...r2.boxes], height: Math.max(r1.height, r2.height) };
1053
+ }
1054
+ /** Page entry point */
1055
+ export function layoutPage(page, theme, canvas) {
1056
+ const root = layoutNode(page, 0, 0, canvas.width, canvas.height, false, theme);
1057
+ root.x = 0;
1058
+ root.y = 0;
1059
+ root.width = canvas.width;
1060
+ root.height = canvas.height;
1061
+ return root;
1062
+ }
1063
+ //# sourceMappingURL=engine.js.map