@moondraft/import 0.0.0-stage → 0.1.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 (323) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +169 -0
  5. package/dist/adapter/convert.d.ts +122 -0
  6. package/dist/adapter/convert.js +2302 -0
  7. package/dist/adapter/figma.d.ts +9 -0
  8. package/dist/adapter/figma.js +30 -0
  9. package/dist/adapter/file-components.d.ts +14 -0
  10. package/dist/adapter/file-components.js +262 -0
  11. package/dist/adapter/file-resources.d.ts +29 -0
  12. package/dist/adapter/file-resources.js +562 -0
  13. package/dist/adapter/index.d.ts +6 -0
  14. package/dist/adapter/index.js +9 -0
  15. package/dist/adapter/link.d.ts +45 -0
  16. package/dist/adapter/link.js +688 -0
  17. package/dist/adapter/payload.d.ts +31 -0
  18. package/dist/adapter/payload.js +78 -0
  19. package/dist/adapter/shapes.d.ts +31 -0
  20. package/dist/adapter/shapes.js +75 -0
  21. package/dist/adapter/variables.d.ts +44 -0
  22. package/dist/adapter/variables.js +103 -0
  23. package/dist/figma-file.d.ts +38 -0
  24. package/dist/figma-file.js +97 -0
  25. package/dist/figma-worker.d.ts +11 -0
  26. package/dist/figma-worker.js +12 -0
  27. package/dist/figma.d.ts +26 -0
  28. package/dist/figma.js +96 -0
  29. package/dist/index.d.ts +28 -0
  30. package/dist/index.js +49 -0
  31. package/dist/report.d.ts +15 -0
  32. package/dist/report.js +69 -0
  33. package/dist/svg.d.ts +33 -0
  34. package/dist/svg.js +44 -0
  35. package/dist/syntax.d.ts +8 -0
  36. package/dist/syntax.js +19 -0
  37. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  38. package/dist/taco/figma-import/clipboard.js +87 -0
  39. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  40. package/dist/taco/figma-import/clipboardSize.js +107 -0
  41. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  42. package/dist/taco/figma-import/container/compression.js +174 -0
  43. package/dist/taco/figma-import/container/error.d.ts +15 -0
  44. package/dist/taco/figma-import/container/error.js +19 -0
  45. package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
  46. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  47. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  48. package/dist/taco/figma-import/container/limits.js +22 -0
  49. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  50. package/dist/taco/figma-import/container/zip.js +154 -0
  51. package/dist/taco/figma-import/figFile.d.ts +34 -0
  52. package/dist/taco/figma-import/figFile.js +91 -0
  53. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  54. package/dist/taco/figma-import/figFileDocument.js +100 -0
  55. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  56. package/dist/taco/figma-import/import/blobs.js +23 -0
  57. package/dist/taco/figma-import/import/carry.d.ts +11 -0
  58. package/dist/taco/figma-import/import/carry.js +33 -0
  59. package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
  60. package/dist/taco/figma-import/import/componentProps.js +313 -0
  61. package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
  62. package/dist/taco/figma-import/import/componentSets.js +647 -0
  63. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  64. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  65. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  66. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  67. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  68. package/dist/taco/figma-import/import/converters/frame.js +55 -0
  69. package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
  70. package/dist/taco/figma-import/import/converters/gradient.js +106 -0
  71. package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
  72. package/dist/taco/figma-import/import/converters/group.js +39 -0
  73. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  74. package/dist/taco/figma-import/import/converters/image.js +83 -0
  75. package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
  76. package/dist/taco/figma-import/import/converters/layout.js +168 -0
  77. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  78. package/dist/taco/figma-import/import/converters/line.js +40 -0
  79. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  80. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  81. package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
  82. package/dist/taco/figma-import/import/converters/section.js +35 -0
  83. package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
  84. package/dist/taco/figma-import/import/converters/shared.js +362 -0
  85. package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
  86. package/dist/taco/figma-import/import/converters/text.js +576 -0
  87. package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
  88. package/dist/taco/figma-import/import/converters/vector.js +468 -0
  89. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
  90. package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
  91. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  92. package/dist/taco/figma-import/import/fields.js +29 -0
  93. package/dist/taco/figma-import/import/instances.d.ts +163 -0
  94. package/dist/taco/figma-import/import/instances.js +1323 -0
  95. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  96. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  97. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  98. package/dist/taco/figma-import/import/slots.js +96 -0
  99. package/dist/taco/figma-import/import/styles.d.ts +46 -0
  100. package/dist/taco/figma-import/import/styles.js +42 -0
  101. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  102. package/dist/taco/figma-import/import/tree.js +194 -0
  103. package/dist/taco/figma-import/import/types.d.ts +232 -0
  104. package/dist/taco/figma-import/import/types.js +33 -0
  105. package/dist/taco/figma-import/import/variables.d.ts +136 -0
  106. package/dist/taco/figma-import/import/variables.js +762 -0
  107. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  108. package/dist/taco/figma-import/import/variantTrees.js +480 -0
  109. package/dist/taco/figma-import/import/walk.d.ts +159 -0
  110. package/dist/taco/figma-import/import/walk.js +1297 -0
  111. package/dist/taco/figma-import/index.d.ts +22 -0
  112. package/dist/taco/figma-import/index.js +13 -0
  113. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  114. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  115. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  116. package/dist/taco/figma-import/kiwi/error.js +29 -0
  117. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  118. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  119. package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
  120. package/dist/taco/figma-import/kiwi/message.js +145 -0
  121. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  122. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  123. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  124. package/dist/taco/figma-import/kiwi/types.js +18 -0
  125. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  126. package/dist/taco/model/agent-hidden-fields.js +29 -0
  127. package/dist/taco/model/asset-users.d.ts +23 -0
  128. package/dist/taco/model/asset-users.js +128 -0
  129. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  130. package/dist/taco/model/auto-layout-lines.js +70 -0
  131. package/dist/taco/model/codec.d.ts +444 -0
  132. package/dist/taco/model/codec.js +5635 -0
  133. package/dist/taco/model/color.d.ts +11 -0
  134. package/dist/taco/model/color.js +61 -0
  135. package/dist/taco/model/comment-operations.d.ts +102 -0
  136. package/dist/taco/model/comment-operations.js +227 -0
  137. package/dist/taco/model/component-evaluation.d.ts +94 -0
  138. package/dist/taco/model/component-evaluation.js +548 -0
  139. package/dist/taco/model/component-expressions.d.ts +242 -0
  140. package/dist/taco/model/component-expressions.js +1082 -0
  141. package/dist/taco/model/component-properties.d.ts +111 -0
  142. package/dist/taco/model/component-properties.js +339 -0
  143. package/dist/taco/model/component-samples.d.ts +83 -0
  144. package/dist/taco/model/component-samples.js +290 -0
  145. package/dist/taco/model/component-validity.d.ts +85 -0
  146. package/dist/taco/model/component-validity.js +397 -0
  147. package/dist/taco/model/components.d.ts +155 -0
  148. package/dist/taco/model/components.js +505 -0
  149. package/dist/taco/model/connector-ends.d.ts +20 -0
  150. package/dist/taco/model/connector-ends.js +188 -0
  151. package/dist/taco/model/connector-geometry.d.ts +74 -0
  152. package/dist/taco/model/connector-geometry.js +293 -0
  153. package/dist/taco/model/corner-radii.d.ts +26 -0
  154. package/dist/taco/model/corner-radii.js +47 -0
  155. package/dist/taco/model/derivations.d.ts +81 -0
  156. package/dist/taco/model/derivations.js +151 -0
  157. package/dist/taco/model/export-settings.d.ts +53 -0
  158. package/dist/taco/model/export-settings.js +156 -0
  159. package/dist/taco/model/expressions/ast.d.ts +153 -0
  160. package/dist/taco/model/expressions/ast.js +6 -0
  161. package/dist/taco/model/expressions/cache.d.ts +23 -0
  162. package/dist/taco/model/expressions/cache.js +43 -0
  163. package/dist/taco/model/expressions/check.d.ts +72 -0
  164. package/dist/taco/model/expressions/check.js +517 -0
  165. package/dist/taco/model/expressions/completion.d.ts +61 -0
  166. package/dist/taco/model/expressions/completion.js +421 -0
  167. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  168. package/dist/taco/model/expressions/evaluate.js +519 -0
  169. package/dist/taco/model/expressions/forms.d.ts +84 -0
  170. package/dist/taco/model/expressions/forms.js +200 -0
  171. package/dist/taco/model/expressions/index.d.ts +14 -0
  172. package/dist/taco/model/expressions/index.js +17 -0
  173. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  174. package/dist/taco/model/expressions/lexer.js +213 -0
  175. package/dist/taco/model/expressions/library.d.ts +33 -0
  176. package/dist/taco/model/expressions/library.js +117 -0
  177. package/dist/taco/model/expressions/names.d.ts +43 -0
  178. package/dist/taco/model/expressions/names.js +99 -0
  179. package/dist/taco/model/expressions/parser.d.ts +15 -0
  180. package/dist/taco/model/expressions/parser.js +438 -0
  181. package/dist/taco/model/expressions/rename.d.ts +34 -0
  182. package/dist/taco/model/expressions/rename.js +46 -0
  183. package/dist/taco/model/expressions/source.d.ts +15 -0
  184. package/dist/taco/model/expressions/source.js +161 -0
  185. package/dist/taco/model/expressions/types.d.ts +119 -0
  186. package/dist/taco/model/expressions/types.js +102 -0
  187. package/dist/taco/model/expressions/values.d.ts +97 -0
  188. package/dist/taco/model/expressions/values.js +160 -0
  189. package/dist/taco/model/geometry-limits.d.ts +16 -0
  190. package/dist/taco/model/geometry-limits.js +67 -0
  191. package/dist/taco/model/geometry.d.ts +47 -0
  192. package/dist/taco/model/geometry.js +193 -0
  193. package/dist/taco/model/group-bounds.d.ts +38 -0
  194. package/dist/taco/model/group-bounds.js +144 -0
  195. package/dist/taco/model/id-table.d.ts +37 -0
  196. package/dist/taco/model/id-table.js +80 -0
  197. package/dist/taco/model/index.d.ts +25 -0
  198. package/dist/taco/model/index.js +27 -0
  199. package/dist/taco/model/instance-resolver.d.ts +372 -0
  200. package/dist/taco/model/instance-resolver.js +1748 -0
  201. package/dist/taco/model/layer-syntax.d.ts +340 -0
  202. package/dist/taco/model/layer-syntax.js +3506 -0
  203. package/dist/taco/model/layout/constraints.d.ts +84 -0
  204. package/dist/taco/model/layout/constraints.js +445 -0
  205. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  206. package/dist/taco/model/layout/frame-layout.js +618 -0
  207. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  208. package/dist/taco/model/layout/node-sizing.js +486 -0
  209. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  210. package/dist/taco/model/legacy-connector-caps.js +70 -0
  211. package/dist/taco/model/linear-call.d.ts +48 -0
  212. package/dist/taco/model/linear-call.js +58 -0
  213. package/dist/taco/model/new-nodes.d.ts +145 -0
  214. package/dist/taco/model/new-nodes.js +277 -0
  215. package/dist/taco/model/node-builder.d.ts +449 -0
  216. package/dist/taco/model/node-builder.js +2294 -0
  217. package/dist/taco/model/node-table-watch.d.ts +36 -0
  218. package/dist/taco/model/node-table-watch.js +63 -0
  219. package/dist/taco/model/node-transform.d.ts +39 -0
  220. package/dist/taco/model/node-transform.js +79 -0
  221. package/dist/taco/model/node-users-index.d.ts +10 -0
  222. package/dist/taco/model/node-users-index.js +82 -0
  223. package/dist/taco/model/operation-references.d.ts +80 -0
  224. package/dist/taco/model/operation-references.js +399 -0
  225. package/dist/taco/model/operations.d.ts +610 -0
  226. package/dist/taco/model/operations.js +2882 -0
  227. package/dist/taco/model/overlay-table.d.ts +71 -0
  228. package/dist/taco/model/overlay-table.js +171 -0
  229. package/dist/taco/model/override-remap.d.ts +69 -0
  230. package/dist/taco/model/override-remap.js +230 -0
  231. package/dist/taco/model/parent-table.d.ts +53 -0
  232. package/dist/taco/model/parent-table.js +193 -0
  233. package/dist/taco/model/problem-collector.d.ts +27 -0
  234. package/dist/taco/model/problem-collector.js +101 -0
  235. package/dist/taco/model/sha256.d.ts +10 -0
  236. package/dist/taco/model/sha256.js +74 -0
  237. package/dist/taco/model/shape-geometry.d.ts +96 -0
  238. package/dist/taco/model/shape-geometry.js +368 -0
  239. package/dist/taco/model/shared-node-table.d.ts +97 -0
  240. package/dist/taco/model/shared-node-table.js +546 -0
  241. package/dist/taco/model/slots.d.ts +113 -0
  242. package/dist/taco/model/slots.js +281 -0
  243. package/dist/taco/model/stroke-settings.d.ts +37 -0
  244. package/dist/taco/model/stroke-settings.js +90 -0
  245. package/dist/taco/model/svg-path-import.d.ts +89 -0
  246. package/dist/taco/model/svg-path-import.js +219 -0
  247. package/dist/taco/model/text-measurement.d.ts +142 -0
  248. package/dist/taco/model/text-measurement.js +846 -0
  249. package/dist/taco/model/text-reads.d.ts +66 -0
  250. package/dist/taco/model/text-reads.js +128 -0
  251. package/dist/taco/model/text-sizer.d.ts +358 -0
  252. package/dist/taco/model/text-sizer.js +996 -0
  253. package/dist/taco/model/theme-users.d.ts +59 -0
  254. package/dist/taco/model/theme-users.js +312 -0
  255. package/dist/taco/model/thumbnail.d.ts +15 -0
  256. package/dist/taco/model/thumbnail.js +34 -0
  257. package/dist/taco/model/types.d.ts +2347 -0
  258. package/dist/taco/model/types.js +366 -0
  259. package/dist/taco/model/unknown-property.d.ts +5 -0
  260. package/dist/taco/model/unknown-property.js +7 -0
  261. package/dist/taco/model/validated-documents.d.ts +22 -0
  262. package/dist/taco/model/validated-documents.js +22 -0
  263. package/dist/taco/model/variable-modes.d.ts +61 -0
  264. package/dist/taco/model/variable-modes.js +438 -0
  265. package/dist/taco/model/variable-references.d.ts +54 -0
  266. package/dist/taco/model/variable-references.js +183 -0
  267. package/dist/taco/model/variables.d.ts +24 -0
  268. package/dist/taco/model/variables.js +151 -0
  269. package/dist/taco/model/vector-network.d.ts +34 -0
  270. package/dist/taco/model/vector-network.js +411 -0
  271. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  272. package/dist/taco/svg-import/import-svg.js +729 -0
  273. package/dist/taco/svg-import/index.d.ts +3 -0
  274. package/dist/taco/svg-import/index.js +3 -0
  275. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  276. package/dist/taco/svg-import/limits/limits.js +160 -0
  277. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  278. package/dist/taco/svg-import/paint/gradients.js +429 -0
  279. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  280. package/dist/taco/svg-import/paint/paint.js +162 -0
  281. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  282. package/dist/taco/svg-import/paths/paths.js +205 -0
  283. package/dist/taco/svg-import/public-types.d.ts +279 -0
  284. package/dist/taco/svg-import/public-types.js +56 -0
  285. package/dist/taco/svg-import/report/report.d.ts +44 -0
  286. package/dist/taco/svg-import/report/report.js +79 -0
  287. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  288. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  289. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  290. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  291. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  292. package/dist/taco/svg-import/structure/effects.js +618 -0
  293. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  294. package/dist/taco/svg-import/structure/image.js +495 -0
  295. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  296. package/dist/taco/svg-import/structure/walk.js +544 -0
  297. package/dist/taco/svg-import/style/color.d.ts +40 -0
  298. package/dist/taco/svg-import/style/color.js +275 -0
  299. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  300. package/dist/taco/svg-import/style/computed.js +1534 -0
  301. package/dist/taco/svg-import/style/css.d.ts +127 -0
  302. package/dist/taco/svg-import/style/css.js +541 -0
  303. package/dist/taco/svg-import/style/media.d.ts +25 -0
  304. package/dist/taco/svg-import/style/media.js +307 -0
  305. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  306. package/dist/taco/svg-import/style/properties.js +135 -0
  307. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  308. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  309. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  310. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  311. package/dist/taco/svg-import/text/text.d.ts +29 -0
  312. package/dist/taco/svg-import/text/text.js +545 -0
  313. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  314. package/dist/taco/svg-import/transform/matrix.js +229 -0
  315. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  316. package/dist/taco/svg-import/tree/element-data.js +155 -0
  317. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  318. package/dist/taco/svg-import/tree/elements.js +121 -0
  319. package/dist/taco/svg-import/units/length.d.ts +43 -0
  320. package/dist/taco/svg-import/units/length.js +125 -0
  321. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  322. package/dist/taco/svg-import/units/viewbox.js +77 -0
  323. package/package.json +54 -4
@@ -0,0 +1,618 @@
1
+ import { fillsAxis, nodeResizing, stackLineBreaks, } from "../auto-layout-lines.js";
2
+ import { getNodeBounds } from "../geometry.js";
3
+ import { constrainChildren } from "./constraints.js";
4
+ import { entryOf, putEntry } from "../id-table.js";
5
+ /** The gap, in pixels, a new auto layout starts with. */
6
+ export const DEFAULT_AUTO_LAYOUT_GAP = 24;
7
+ const ZERO_PADDING = { top: 0, right: 0, bottom: 0, left: 0 };
8
+ /**
9
+ * Fill sharing compares sums of lengths; this tolerance keeps floating-point
10
+ * noise from freezing a child that fits exactly.
11
+ */
12
+ const LENGTH_TOLERANCE = 1e-6;
13
+ /**
14
+ * A new layout of the given kind. Auto layouts start with a fixed width, a
15
+ * hugged height, no padding and `gap` between children (a grid uses it for
16
+ * both its column and row gaps).
17
+ */
18
+ export function createFrameLayout(kind, gap = DEFAULT_AUTO_LAYOUT_GAP) {
19
+ if (kind === "absolute")
20
+ return { kind };
21
+ const constraints = {
22
+ widthSizing: "fixed",
23
+ heightSizing: "hug",
24
+ };
25
+ if (kind === "grid") {
26
+ return {
27
+ kind,
28
+ ...constraints,
29
+ columns: 2,
30
+ columnGap: gap,
31
+ rowGap: gap,
32
+ padding: ZERO_PADDING,
33
+ };
34
+ }
35
+ return {
36
+ kind,
37
+ ...constraints,
38
+ gap,
39
+ padding: ZERO_PADDING,
40
+ align: "start",
41
+ justify: "start",
42
+ };
43
+ }
44
+ /**
45
+ * The gap a layout keeps between children: a stack's gap or a grid's column
46
+ * gap. Without an auto layout it is `DEFAULT_AUTO_LAYOUT_GAP`.
47
+ */
48
+ export function frameLayoutGap(layout) {
49
+ if (layout.kind === "absolute")
50
+ return DEFAULT_AUTO_LAYOUT_GAP;
51
+ return layout.kind === "grid" ? layout.columnGap : layout.gap;
52
+ }
53
+ function limits(node, axis) {
54
+ const resizing = nodeResizing(node);
55
+ return axis === "width"
56
+ ? { min: resizing?.minWidth ?? 0, max: resizing?.maxWidth ?? Infinity }
57
+ : { min: resizing?.minHeight ?? 0, max: resizing?.maxHeight ?? Infinity };
58
+ }
59
+ function clamp(value, range) {
60
+ return Math.min(range.max, Math.max(range.min, value));
61
+ }
62
+ /**
63
+ * The `ContentLength` of the nodes in `nodes`: an auto layout row or column
64
+ * needs its padding and its flow children, laid end to end with its gaps along
65
+ * its main axis, or the longest across it. A child that fills the same axis
66
+ * counts with its own content length. A wrapping stack, a grid and every
67
+ * other node have none, and neither has a stack with such a filling child.
68
+ */
69
+ function stackContentLength(nodes) {
70
+ const measure = (node, axis, depth) => {
71
+ if (node.type !== "frame" || depth > 32)
72
+ return undefined;
73
+ const layout = node.layout;
74
+ if ((layout.kind !== "horizontal" && layout.kind !== "vertical") || layout.wrap === true)
75
+ return undefined;
76
+ const along = (layout.kind === "vertical") === (axis === "height");
77
+ const lengths = [];
78
+ for (const childId of node.children) {
79
+ const child = entryOf(nodes, childId);
80
+ if (child === undefined || !inStackFlow(child))
81
+ continue;
82
+ const box = getNodeBounds(child);
83
+ const length = fillsAxis(child, axis) ? measure(child, axis, depth + 1) : axis === "width" ? box.width : box.height;
84
+ if (length === undefined)
85
+ return undefined;
86
+ lengths.push(length);
87
+ }
88
+ const padding = axis === "width" ? layout.padding.left + layout.padding.right : layout.padding.top + layout.padding.bottom;
89
+ const autoGap = layout.justify === "space-between" || layout.justify === "space-around" || layout.justify === "space-evenly";
90
+ const content = along
91
+ ? lengths.reduce((total, length) => total + length, 0) + Math.max(0, lengths.length - 1) * (autoGap ? 0 : layout.gap)
92
+ : Math.max(0, ...lengths);
93
+ return clamp(content + padding, limits(node, axis));
94
+ };
95
+ return (node, axis) => measure(node, axis, 0);
96
+ }
97
+ function placeNode(node, x, y, size) {
98
+ if (node.type === "shape" && node.shape === "path") {
99
+ const bounds = getNodeBounds(node);
100
+ const deltaX = x - bounds.x;
101
+ const deltaY = y - bounds.y;
102
+ if (deltaX === 0 && deltaY === 0)
103
+ return node;
104
+ return {
105
+ ...node,
106
+ transform: [
107
+ node.transform[0],
108
+ node.transform[1],
109
+ node.transform[2],
110
+ node.transform[3],
111
+ node.transform[4] + deltaX,
112
+ node.transform[5] + deltaY,
113
+ ],
114
+ };
115
+ }
116
+ const resized = size === undefined
117
+ ? node
118
+ : {
119
+ ...node,
120
+ frame: {
121
+ ...node.frame,
122
+ width: size.width,
123
+ height: size.height,
124
+ },
125
+ };
126
+ const bounds = getNodeBounds(resized);
127
+ const deltaX = x - bounds.x;
128
+ const deltaY = y - bounds.y;
129
+ const width = resized.frame.width;
130
+ const height = resized.frame.height;
131
+ const frameX = resized.frame.x + deltaX;
132
+ const frameY = resized.frame.y + deltaY;
133
+ if (node.frame.x === frameX
134
+ && node.frame.y === frameY
135
+ && node.frame.width === width
136
+ && node.frame.height === height)
137
+ return node;
138
+ return {
139
+ ...resized,
140
+ frame: {
141
+ ...resized.frame,
142
+ x: frameX,
143
+ y: frameY,
144
+ width,
145
+ height,
146
+ },
147
+ };
148
+ }
149
+ /**
150
+ * The frame's own size on an axis, bounded by its limits. As in Figma, a
151
+ * fixed or filled size keeps its stored value down to 0 (a collapsed body of
152
+ * 0.0001 adds only 0.0001 to a hugging parent), and a Hug size measured from
153
+ * at least one flow child follows that content down to 0. Only a Hug frame
154
+ * with nothing to hug stays at least 1, so a person can still see and grab
155
+ * it.
156
+ */
157
+ function frameAxisSize(frame, axis, value, hugsNothing) {
158
+ return Math.max(hugsNothing ? 1 : 0, clamp(value, limits(frame, axis)));
159
+ }
160
+ /**
161
+ * Whether the frame hugs its content on `axis`. On the main axis a child that
162
+ * fills it turns Hug into Fixed, as in Figma, since the two would depend on
163
+ * each other. On the cross axis Figma keeps Hug: the frame hugs its children,
164
+ * a child that fills that axis counting with the size its own content needs
165
+ * (`measured`, see `ContentLength`), and the filling children then stretch to
166
+ * the result. It stays Fixed when every child fills the cross axis, or when
167
+ * the content of a filling child cannot be measured (a wrapping stack, a
168
+ * grid, text or a frame without auto layout).
169
+ */
170
+ function hugsAxis(layout, axis, children, measured) {
171
+ const sizing = axis === "width" ? layout.widthSizing : layout.heightSizing;
172
+ if (sizing !== "hug")
173
+ return false;
174
+ const filling = children.filter((child) => fillsAxis(child, axis));
175
+ if (filling.length === 0)
176
+ return true;
177
+ // Cross axis: hug only when some child does not fill and every filling child's content was measured.
178
+ return measured !== undefined && filling.length < children.length && filling.every((child) => measured.has(child.id));
179
+ }
180
+ /**
181
+ * Shares `free` space between Fill children the way CSS flex does for
182
+ * `flex: 1 0 0`: equal shares, then any child that a limit stops is frozen at
183
+ * that limit and the rest is shared again among the others.
184
+ */
185
+ function shareFillSpace(free, ranges) {
186
+ const sizes = ranges.map(() => 0);
187
+ const frozen = ranges.map(() => false);
188
+ for (;;) {
189
+ const open = ranges.flatMap((_, index) => (frozen[index] ? [] : [index]));
190
+ if (open.length === 0)
191
+ return sizes;
192
+ const frozenTotal = sizes.reduce((total, size, index) => (frozen[index] ? total + size : total), 0);
193
+ const share = (free - frozenTotal) / open.length;
194
+ let violation = 0;
195
+ open.forEach((index) => {
196
+ const range = ranges[index];
197
+ sizes[index] = clamp(share, range);
198
+ violation += sizes[index] - share;
199
+ });
200
+ if (Math.abs(violation) <= LENGTH_TOLERANCE)
201
+ return sizes;
202
+ let frozeAny = false;
203
+ open.forEach((index) => {
204
+ const range = ranges[index];
205
+ const size = sizes[index];
206
+ if ((violation > 0 && size === range.min && share < range.min)
207
+ || (violation < 0 && size === range.max && share > range.max)) {
208
+ frozen[index] = true;
209
+ frozeAny = true;
210
+ }
211
+ });
212
+ if (!frozeAny)
213
+ return sizes;
214
+ }
215
+ }
216
+ /**
217
+ * Where a line's children start on the main axis and the gap between them.
218
+ * The automatic gaps are never negative; when children overflow, they pack
219
+ * from the start.
220
+ */
221
+ function mainAxisSpacing(justify, gap, available, sizes) {
222
+ const count = sizes.length;
223
+ const itemTotal = sizes.reduce((total, size) => total + size, 0);
224
+ const extra = available - itemTotal;
225
+ switch (justify) {
226
+ case "space-between":
227
+ return count > 1
228
+ ? { offset: 0, gap: Math.max(0, extra / (count - 1)) }
229
+ : { offset: 0, gap: 0 };
230
+ case "space-around": {
231
+ const autoGap = Math.max(0, extra) / Math.max(1, count);
232
+ return { offset: autoGap / 2, gap: autoGap };
233
+ }
234
+ case "space-evenly": {
235
+ const autoGap = Math.max(0, extra) / (count + 1);
236
+ return { offset: autoGap, gap: autoGap };
237
+ }
238
+ default: {
239
+ const used = itemTotal + Math.max(0, count - 1) * gap;
240
+ const offset = justify === "center"
241
+ ? (available - used) / 2
242
+ : justify === "end"
243
+ ? available - used
244
+ : 0;
245
+ return { offset, gap };
246
+ }
247
+ }
248
+ }
249
+ function alignOffset(align, available, size) {
250
+ if (align === "center")
251
+ return (available - size) / 2;
252
+ if (align === "end")
253
+ return available - size;
254
+ return 0;
255
+ }
256
+ /**
257
+ * The main length of a child in a single-line stack. As in Figma, a child with
258
+ * a locked aspect ratio that fills the cross axis but not the main axis keeps
259
+ * its proportions: its main length follows the cross length it stretches to
260
+ * (`lineCross`, bounded by its limits), at its stored `aspectRatio`. The
261
+ * ratio is stored rather than read from the child's box, since a box can
262
+ * already be out of proportion (a nested instance's width from its component
263
+ * and height from the instance, or a length a limit stopped). A stored length
264
+ * within a pixel of the result stays. Every other child keeps its stored main
265
+ * length.
266
+ */
267
+ function ratioMain(node, mainAxis, storedMain, lineCross) {
268
+ const crossAxis = mainAxis === "width" ? "height" : "width";
269
+ const ratio = node.aspectRatio;
270
+ if (node.aspectRatioLocked !== true
271
+ || ratio === undefined
272
+ || lineCross === undefined
273
+ || !fillsAxis(node, crossAxis)
274
+ || fillsAxis(node, mainAxis))
275
+ return storedMain;
276
+ // `aspectRatio` is width over height.
277
+ const mainPerCross = mainAxis === "width" ? ratio : 1 / ratio;
278
+ const length = clamp(clamp(lineCross, limits(node, crossAxis)) * mainPerCross, limits(node, mainAxis));
279
+ // The stored ratio can be a little off the stored size (an image's own pixel ratio, 2502:724, on a 1104 x 319 layer),
280
+ // and Figma keeps that size; a length less than a pixel off counts as in proportion, so layers do not move by fractions.
281
+ return Math.abs(length - storedMain) < 1 ? storedMain : length;
282
+ }
283
+ /**
284
+ * Lays out a row or column, following Figma: optional wrapping into lines,
285
+ * Fill children sharing each line's free space, automatic gaps, and Hug sizes
286
+ * bounded by the frame's limits. Lengths are not rounded.
287
+ */
288
+ function stackLayout(frame, children, layout, contentLength) {
289
+ const vertical = layout.kind === "vertical";
290
+ const mainAxis = vertical ? "height" : "width";
291
+ const crossAxis = vertical ? "width" : "height";
292
+ const mainPaddingStart = vertical ? layout.padding.top : layout.padding.left;
293
+ const mainPaddingEnd = vertical ? layout.padding.bottom : layout.padding.right;
294
+ const crossPaddingStart = vertical ? layout.padding.left : layout.padding.top;
295
+ const crossPaddingEnd = vertical ? layout.padding.right : layout.padding.bottom;
296
+ const autoGap = layout.justify === "space-between"
297
+ || layout.justify === "space-around"
298
+ || layout.justify === "space-evenly";
299
+ // An automatic gap counts as zero when measuring Hug and breaking lines.
300
+ const measureGap = autoGap ? 0 : layout.gap;
301
+ const wrap = layout.wrap === true;
302
+ const spreadLines = wrap && layout.alignContent === "space-between";
303
+ const lineGap = layout.wrapGap ?? layout.gap;
304
+ const measureLineGap = spreadLines ? 0 : lineGap;
305
+ // The content length of each child that fills the cross axis, where it can be measured (none in a wrapping stack).
306
+ const crossContent = new Map();
307
+ if (!wrap && contentLength !== undefined) {
308
+ for (const child of children) {
309
+ if (!fillsAxis(child, crossAxis))
310
+ continue;
311
+ const length = contentLength(child, crossAxis);
312
+ if (length !== undefined)
313
+ crossContent.set(child.id, length);
314
+ }
315
+ }
316
+ // A single line spans the frame's cross axis, so a child with a locked aspect ratio that stretches across it can take
317
+ // its main length from that span before the main size is measured (`ratioMain`).
318
+ const singleLineCross = wrap ? undefined : (() => {
319
+ const crossOf = (node) => {
320
+ const box = getNodeBounds(node);
321
+ return fillsAxis(node, crossAxis)
322
+ ? Math.max(limits(node, crossAxis).min, crossContent.get(node.id) ?? 0)
323
+ : vertical ? box.width : box.height;
324
+ };
325
+ const size = hugsAxis(layout, crossAxis, children, crossContent)
326
+ ? Math.max(0, ...children.map(crossOf)) + crossPaddingStart + crossPaddingEnd
327
+ : vertical ? frame.frame.width : frame.frame.height;
328
+ return Math.max(0, frameAxisSize(frame, crossAxis, size, children.length === 0) - crossPaddingStart - crossPaddingEnd);
329
+ })();
330
+ const items = children.map((node, index) => {
331
+ const box = getNodeBounds(node);
332
+ const fillMain = fillsAxis(node, mainAxis);
333
+ const fillCross = fillsAxis(node, crossAxis);
334
+ const storedMain = vertical ? box.height : box.width;
335
+ const boxCross = vertical ? box.width : box.height;
336
+ const boxMain = ratioMain(node, mainAxis, storedMain, singleLineCross);
337
+ return {
338
+ node,
339
+ index,
340
+ fillMain,
341
+ fillCross,
342
+ baseMain: fillMain ? limits(node, mainAxis).min : boxMain,
343
+ // A single line hugs a child that fills across it by its content (`hugsAxis`).
344
+ baseCross: fillCross ? Math.max(limits(node, crossAxis).min, crossContent.get(node.id) ?? 0) : boxCross,
345
+ boxMain,
346
+ boxCross,
347
+ };
348
+ });
349
+ // 1. The main size: Hug measures every child on one line.
350
+ const singleLineMain = items.reduce((total, item) => total + item.baseMain, 0)
351
+ + Math.max(0, items.length - 1) * measureGap;
352
+ const hugMain = hugsAxis(layout, mainAxis, children);
353
+ const mainSize = frameAxisSize(frame, mainAxis, hugMain
354
+ ? singleLineMain + mainPaddingStart + mainPaddingEnd
355
+ : vertical ? frame.frame.height : frame.frame.width, hugMain && children.length === 0);
356
+ const availableMain = mainSize - mainPaddingStart - mainPaddingEnd;
357
+ // 2. Break lines greedily.
358
+ const lineItems = stackLineBreaks(layout, availableMain, items.map((item) => item.baseMain))
359
+ .map((indexes) => indexes.map((index) => items[index]));
360
+ // 4. Each line is as thick as its thickest child.
361
+ const lines = lineItems.map((lineMembers) => ({
362
+ items: lineMembers,
363
+ crossSize: Math.max(0, ...lineMembers.map((item) => item.baseCross)),
364
+ }));
365
+ // 5. The cross size and the lines' cross positions.
366
+ const lineTotal = lines.reduce((total, line) => total + line.crossSize, 0);
367
+ const hugCross = hugsAxis(layout, crossAxis, children, crossContent);
368
+ const crossSize = frameAxisSize(frame, crossAxis, hugCross
369
+ ? lineTotal
370
+ + Math.max(0, lines.length - 1) * measureLineGap
371
+ + crossPaddingStart
372
+ + crossPaddingEnd
373
+ : vertical ? frame.frame.width : frame.frame.height, hugCross && children.length === 0);
374
+ const availableCross = Math.max(0, crossSize - crossPaddingStart - crossPaddingEnd);
375
+ const lineStarts = [];
376
+ const lineSizes = [];
377
+ if (!wrap) {
378
+ // A single line always spans the frame's cross axis.
379
+ lineStarts.push(crossPaddingStart);
380
+ lineSizes.push(availableCross);
381
+ }
382
+ else if (spreadLines) {
383
+ const spacing = lines.length > 1
384
+ ? Math.max(0, (availableCross - lineTotal) / (lines.length - 1))
385
+ : 0;
386
+ let position = crossPaddingStart;
387
+ lines.forEach((line) => {
388
+ lineStarts.push(position);
389
+ lineSizes.push(line.crossSize);
390
+ position += line.crossSize + spacing;
391
+ });
392
+ }
393
+ else {
394
+ const free = availableCross - lineTotal - Math.max(0, lines.length - 1) * lineGap;
395
+ const everyChildFillsCross = items.length > 0 && items.every((item) => item.fillCross);
396
+ const share = everyChildFillsCross && free > 0 ? free / lines.length : 0;
397
+ const blockSize = lineTotal + share * lines.length + Math.max(0, lines.length - 1) * lineGap;
398
+ let position = crossPaddingStart + (share > 0 ? 0 : alignOffset(layout.align, availableCross, blockSize));
399
+ lines.forEach((line) => {
400
+ lineStarts.push(position);
401
+ lineSizes.push(line.crossSize + share);
402
+ position += line.crossSize + share + lineGap;
403
+ });
404
+ }
405
+ const placedChildren = new Map();
406
+ lines.forEach((line, lineIndex) => {
407
+ // 3. Fill children share the line's free main space.
408
+ const fixedTotal = line.items.reduce((total, item) => (item.fillMain ? total : total + item.boxMain), 0);
409
+ const fillItems = line.items.filter((item) => item.fillMain);
410
+ const free = availableMain - fixedTotal - Math.max(0, line.items.length - 1) * measureGap;
411
+ const fillSizes = shareFillSpace(free, fillItems.map((item) => limits(item.node, mainAxis)));
412
+ const mainSizes = line.items.map((item) => (item.fillMain ? fillSizes[fillItems.indexOf(item)] ?? 0 : item.boxMain));
413
+ // 6. Spread the line on the main axis.
414
+ const spacing = mainAxisSpacing(layout.justify, layout.gap, availableMain, mainSizes);
415
+ let mainPosition = mainPaddingStart + spacing.offset;
416
+ const lineStart = lineStarts[lineIndex] ?? crossPaddingStart;
417
+ const lineSize = lineSizes[lineIndex] ?? 0;
418
+ line.items.forEach((item, itemIndex) => {
419
+ const mainLength = mainSizes[itemIndex] ?? item.boxMain;
420
+ // 7. Place the child across its line.
421
+ const crossLength = item.fillCross
422
+ ? clamp(lineSize, limits(item.node, crossAxis))
423
+ : item.boxCross;
424
+ const cross = lineStart + alignOffset(layout.align, lineSize, crossLength);
425
+ const resized = item.fillMain || item.fillCross;
426
+ placedChildren.set(item.node.id, placeNode(item.node, vertical ? cross : mainPosition, vertical ? mainPosition : cross, resized
427
+ ? {
428
+ width: vertical ? crossLength : mainLength,
429
+ height: vertical ? mainLength : crossLength,
430
+ }
431
+ : undefined));
432
+ mainPosition += mainLength + spacing.gap;
433
+ });
434
+ });
435
+ const width = vertical ? crossSize : mainSize;
436
+ const height = vertical ? mainSize : crossSize;
437
+ return {
438
+ frame: { ...frame, layout, frame: { ...frame.frame, width, height } },
439
+ children: placedChildren,
440
+ };
441
+ }
442
+ /**
443
+ * Lays out a grid: equal columns sharing the width, rows as tall as their
444
+ * tallest child. A Fill child takes its cell's width or height.
445
+ */
446
+ function gridLayout(frame, children, layout) {
447
+ const columns = Math.max(1, Math.floor(layout.columns));
448
+ const rows = Math.ceil(children.length / columns);
449
+ const boxes = children.map((child) => getNodeBounds(child));
450
+ const baseWidths = children.map((child, index) => (fillsAxis(child, "width") ? limits(child, "width").min : boxes[index].width));
451
+ const baseHeights = children.map((child, index) => (fillsAxis(child, "height") ? limits(child, "height").min : boxes[index].height));
452
+ const widestChild = Math.max(0, ...baseWidths);
453
+ const rowHeights = Array.from({ length: rows }, (_, row) => (Math.max(0, ...baseHeights.slice(row * columns, (row + 1) * columns))));
454
+ const naturalWidth = layout.padding.left
455
+ + layout.padding.right
456
+ + widestChild * columns
457
+ + Math.max(0, columns - 1) * layout.columnGap;
458
+ const naturalHeight = layout.padding.top
459
+ + layout.padding.bottom
460
+ + rowHeights.reduce((total, height) => total + height, 0)
461
+ + Math.max(0, rows - 1) * layout.rowGap;
462
+ const width = frameAxisSize(frame, "width", layout.widthSizing === "hug" ? naturalWidth : frame.frame.width, layout.widthSizing === "hug" && children.length === 0);
463
+ const height = frameAxisSize(frame, "height", layout.heightSizing === "hug" ? naturalHeight : frame.frame.height, layout.heightSizing === "hug" && children.length === 0);
464
+ const availableWidth = Math.max(0, width
465
+ - layout.padding.left
466
+ - layout.padding.right
467
+ - Math.max(0, columns - 1) * layout.columnGap);
468
+ const columnWidth = availableWidth / columns;
469
+ const rowOffsets = [];
470
+ let nextRowOffset = layout.padding.top;
471
+ rowHeights.forEach((rowHeight) => {
472
+ rowOffsets.push(nextRowOffset);
473
+ nextRowOffset += rowHeight + layout.rowGap;
474
+ });
475
+ const placedChildren = new Map();
476
+ children.forEach((child, index) => {
477
+ const column = index % columns;
478
+ const row = Math.floor(index / columns);
479
+ const fillWidth = fillsAxis(child, "width");
480
+ const fillHeight = fillsAxis(child, "height");
481
+ const box = boxes[index];
482
+ placedChildren.set(child.id, placeNode(child, layout.padding.left + column * (columnWidth + layout.columnGap), rowOffsets[row] ?? layout.padding.top, fillWidth || fillHeight
483
+ ? {
484
+ width: fillWidth ? clamp(columnWidth, limits(child, "width")) : box.width,
485
+ height: fillHeight ? clamp(rowHeights[row] ?? 0, limits(child, "height")) : box.height,
486
+ }
487
+ : undefined));
488
+ });
489
+ return {
490
+ frame: { ...frame, layout, frame: { ...frame.frame, width, height } },
491
+ children: placedChildren,
492
+ };
493
+ }
494
+ /**
495
+ * Whether a row or column lays out `child`: not when it ignores auto layout
496
+ * (`layoutPositioning: "absolute"`), and not while it is hidden. As in Figma,
497
+ * a hidden child takes no slot and no gap, so its neighbours close up, and it
498
+ * keeps its own frame until it is shown again, when it gets its slot back.
499
+ * @since 0.32.0
500
+ */
501
+ export function inStackFlow(child) {
502
+ return child.layoutPositioning !== "absolute" && child.visible;
503
+ }
504
+ /**
505
+ * Lays out one frame's direct children with `layout` and returns the frame
506
+ * with its new size and the moved or resized children. Child frames are not
507
+ * laid out again here; `layoutFrameTree` does that. `contentLength` gives
508
+ * the length a child that fills the cross axis needs for its own content, so
509
+ * a frame that hugs that axis can hug it; without it such a child counts with
510
+ * its minimum.
511
+ */
512
+ export function applyFrameLayout(frame, childNodes, layout, contentLength) {
513
+ if (layout.kind === "absolute") {
514
+ return {
515
+ frame: { ...frame, layout },
516
+ children: new Map(childNodes.map((child) => [child.id, child])),
517
+ };
518
+ }
519
+ // Absolute children stay out of the flow: the layout neither places them
520
+ // nor hugs them. `layoutFrameTree` moves them by their constraints. A stack
521
+ // also leaves out its hidden children, as Figma does (`inStackFlow`).
522
+ const flow = layout.kind === "grid"
523
+ ? childNodes.filter((child) => child.layoutPositioning !== "absolute")
524
+ : childNodes.filter(inStackFlow);
525
+ const applied = layout.kind === "grid"
526
+ ? gridLayout(frame, flow, layout)
527
+ : stackLayout(frame, flow, layout, contentLength);
528
+ if (flow.length === childNodes.length)
529
+ return applied;
530
+ return {
531
+ frame: applied.frame,
532
+ children: new Map(childNodes.map((child) => [child.id, applied.children.get(child.id) ?? child])),
533
+ };
534
+ }
535
+ function isAutoLayoutFrame(node) {
536
+ return node?.type === "frame" && node.layout.kind !== "absolute";
537
+ }
538
+ function sameSize(left, right) {
539
+ const leftBox = getNodeBounds(left);
540
+ const rightBox = getNodeBounds(right);
541
+ return leftBox.width === rightBox.width && leftBox.height === rightBox.height;
542
+ }
543
+ /**
544
+ * The most times `layoutFrameTree` lays out one frame per call. A second pass
545
+ * lets a frame react once to a child frame whose Hug size changed after the
546
+ * frame gave it a Fill size (for example a wrapping child that became
547
+ * taller); stopping there keeps a frame and its children from resizing each
548
+ * other forever.
549
+ */
550
+ const MAX_LAYOUT_PASSES = 2;
551
+ /**
552
+ * Lays out the auto layout frame `frameId` in `nodes` (changed in place),
553
+ * then lays out again each child frame whose size it changed, so Fill sizes
554
+ * reach nested auto layouts, and each auto layout frame that constraints
555
+ * resized. Returns the IDs of every node it changed. The
556
+ * caller lays out hugging descendants first, deepest first, so their sizes
557
+ * are known.
558
+ */
559
+ export function layoutFrameTree(nodes, frameId, changed = new Set()) {
560
+ for (let pass = 0; pass < MAX_LAYOUT_PASSES; pass += 1) {
561
+ const frame = entryOf(nodes, frameId);
562
+ if (!isAutoLayoutFrame(frame))
563
+ return changed;
564
+ const children = frame.children.flatMap((childId) => {
565
+ const child = entryOf(nodes, childId);
566
+ return child === undefined ? [] : [child];
567
+ });
568
+ const applied = applyFrameLayout(frame, children, frame.layout, stackContentLength(nodes));
569
+ if (applied.frame.frame.x !== frame.frame.x
570
+ || applied.frame.frame.y !== frame.frame.y
571
+ || applied.frame.frame.width !== frame.frame.width
572
+ || applied.frame.frame.height !== frame.frame.height) {
573
+ putEntry(nodes, frameId, applied.frame);
574
+ changed.add(frameId);
575
+ }
576
+ const resizedFrames = [];
577
+ // Auto layout frames that constraints resized: their own flow children
578
+ // follow once they are laid out again. They are absolute children or
579
+ // inside a frame without auto layout, so their size does not feed back
580
+ // into this frame's layout.
581
+ const constrainedFrames = [];
582
+ const writeConstrained = (node) => {
583
+ const previous = entryOf(nodes, node.id);
584
+ putEntry(nodes, node.id, node);
585
+ changed.add(node.id);
586
+ if (isAutoLayoutFrame(node) && previous !== undefined && !sameSize(previous, node)) {
587
+ constrainedFrames.push(node.id);
588
+ }
589
+ };
590
+ children.forEach((previous) => {
591
+ const next = applied.children.get(previous.id);
592
+ if (next === undefined || next === previous)
593
+ return;
594
+ putEntry(nodes, previous.id, next);
595
+ changed.add(previous.id);
596
+ // A frame the layout resized (a Fill child) moves its own children by
597
+ // their constraints before it is laid out again.
598
+ if (previous.type === "frame" && next.type === "frame") {
599
+ constrainChildren(nodes, next.id, previous.frame, next.frame).forEach(writeConstrained);
600
+ }
601
+ if (isAutoLayoutFrame(next) && !sameSize(previous, next))
602
+ resizedFrames.push(next.id);
603
+ });
604
+ // Absolute children follow the frame's own size change by their constraints.
605
+ constrainChildren(nodes, frameId, frame.frame, applied.frame.frame).forEach(writeConstrained);
606
+ constrainedFrames.forEach((childId) => layoutFrameTree(nodes, childId, changed));
607
+ let childChangedItself = false;
608
+ resizedFrames.forEach((childId) => {
609
+ const given = entryOf(nodes, childId);
610
+ layoutFrameTree(nodes, childId, changed);
611
+ if (!sameSize(given, entryOf(nodes, childId)))
612
+ childChangedItself = true;
613
+ });
614
+ if (!childChangedItself)
615
+ return changed;
616
+ }
617
+ return changed;
618
+ }