@moondraft/import 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 (337) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +394 -0
  5. package/dist/adapter/convert.d.ts +144 -0
  6. package/dist/adapter/convert.js +2811 -0
  7. package/dist/adapter/figma-descriptions.d.ts +7 -0
  8. package/dist/adapter/figma-descriptions.js +34 -0
  9. package/dist/adapter/figma-grids.d.ts +12 -0
  10. package/dist/adapter/figma-grids.js +175 -0
  11. package/dist/adapter/figma-handoff.d.ts +12 -0
  12. package/dist/adapter/figma-handoff.js +167 -0
  13. package/dist/adapter/figma-hyperlinks.d.ts +13 -0
  14. package/dist/adapter/figma-hyperlinks.js +77 -0
  15. package/dist/adapter/figma.d.ts +12 -0
  16. package/dist/adapter/figma.js +40 -0
  17. package/dist/adapter/file-components.d.ts +20 -0
  18. package/dist/adapter/file-components.js +352 -0
  19. package/dist/adapter/file-resources.d.ts +29 -0
  20. package/dist/adapter/file-resources.js +851 -0
  21. package/dist/adapter/image-crop.d.ts +18 -0
  22. package/dist/adapter/image-crop.js +30 -0
  23. package/dist/adapter/index.d.ts +6 -0
  24. package/dist/adapter/index.js +9 -0
  25. package/dist/adapter/link.d.ts +49 -0
  26. package/dist/adapter/link.js +892 -0
  27. package/dist/adapter/payload.d.ts +31 -0
  28. package/dist/adapter/payload.js +78 -0
  29. package/dist/adapter/shapes.d.ts +31 -0
  30. package/dist/adapter/shapes.js +75 -0
  31. package/dist/adapter/variables.d.ts +45 -0
  32. package/dist/adapter/variables.js +106 -0
  33. package/dist/figma-file.d.ts +38 -0
  34. package/dist/figma-file.js +97 -0
  35. package/dist/figma-worker.d.ts +11 -0
  36. package/dist/figma-worker.js +12 -0
  37. package/dist/figma.d.ts +26 -0
  38. package/dist/figma.js +230 -0
  39. package/dist/index.d.ts +29 -0
  40. package/dist/index.js +50 -0
  41. package/dist/report.d.ts +42 -0
  42. package/dist/report.js +142 -0
  43. package/dist/serialize.d.ts +7 -0
  44. package/dist/serialize.js +79 -0
  45. package/dist/svg.d.ts +33 -0
  46. package/dist/svg.js +44 -0
  47. package/dist/syntax.d.ts +8 -0
  48. package/dist/syntax.js +19 -0
  49. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  50. package/dist/taco/figma-import/clipboard.js +87 -0
  51. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  52. package/dist/taco/figma-import/clipboardSize.js +107 -0
  53. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  54. package/dist/taco/figma-import/container/compression.js +174 -0
  55. package/dist/taco/figma-import/container/error.d.ts +15 -0
  56. package/dist/taco/figma-import/container/error.js +19 -0
  57. package/dist/taco/figma-import/container/kiwiContainer.d.ts +41 -0
  58. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  59. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  60. package/dist/taco/figma-import/container/limits.js +22 -0
  61. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  62. package/dist/taco/figma-import/container/zip.js +154 -0
  63. package/dist/taco/figma-import/figFile.d.ts +34 -0
  64. package/dist/taco/figma-import/figFile.js +91 -0
  65. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  66. package/dist/taco/figma-import/figFileDocument.js +100 -0
  67. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  68. package/dist/taco/figma-import/import/blobs.js +23 -0
  69. package/dist/taco/figma-import/import/carry.d.ts +24 -0
  70. package/dist/taco/figma-import/import/carry.js +66 -0
  71. package/dist/taco/figma-import/import/componentProps.d.ts +90 -0
  72. package/dist/taco/figma-import/import/componentProps.js +329 -0
  73. package/dist/taco/figma-import/import/componentSets.d.ts +85 -0
  74. package/dist/taco/figma-import/import/componentSets.js +647 -0
  75. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  76. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  77. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  78. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  79. package/dist/taco/figma-import/import/converters/exportSettings.d.ts +4 -0
  80. package/dist/taco/figma-import/import/converters/exportSettings.js +49 -0
  81. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  82. package/dist/taco/figma-import/import/converters/frame.js +63 -0
  83. package/dist/taco/figma-import/import/converters/gradient.d.ts +74 -0
  84. package/dist/taco/figma-import/import/converters/gradient.js +108 -0
  85. package/dist/taco/figma-import/import/converters/group.d.ts +19 -0
  86. package/dist/taco/figma-import/import/converters/group.js +30 -0
  87. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  88. package/dist/taco/figma-import/import/converters/image.js +94 -0
  89. package/dist/taco/figma-import/import/converters/layout.d.ts +33 -0
  90. package/dist/taco/figma-import/import/converters/layout.js +187 -0
  91. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  92. package/dist/taco/figma-import/import/converters/line.js +40 -0
  93. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  94. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  95. package/dist/taco/figma-import/import/converters/section.d.ts +8 -0
  96. package/dist/taco/figma-import/import/converters/section.js +29 -0
  97. package/dist/taco/figma-import/import/converters/shared.d.ts +71 -0
  98. package/dist/taco/figma-import/import/converters/shared.js +372 -0
  99. package/dist/taco/figma-import/import/converters/text.d.ts +54 -0
  100. package/dist/taco/figma-import/import/converters/text.js +707 -0
  101. package/dist/taco/figma-import/import/converters/vector.d.ts +105 -0
  102. package/dist/taco/figma-import/import/converters/vector.js +609 -0
  103. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +114 -0
  104. package/dist/taco/figma-import/import/converters/vectorNetwork.js +503 -0
  105. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  106. package/dist/taco/figma-import/import/fields.js +29 -0
  107. package/dist/taco/figma-import/import/instances.d.ts +184 -0
  108. package/dist/taco/figma-import/import/instances.js +1530 -0
  109. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  110. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  111. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  112. package/dist/taco/figma-import/import/slots.js +96 -0
  113. package/dist/taco/figma-import/import/styles.d.ts +48 -0
  114. package/dist/taco/figma-import/import/styles.js +48 -0
  115. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  116. package/dist/taco/figma-import/import/tree.js +194 -0
  117. package/dist/taco/figma-import/import/types.d.ts +232 -0
  118. package/dist/taco/figma-import/import/types.js +33 -0
  119. package/dist/taco/figma-import/import/variables.d.ts +137 -0
  120. package/dist/taco/figma-import/import/variables.js +769 -0
  121. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  122. package/dist/taco/figma-import/import/variantTrees.js +520 -0
  123. package/dist/taco/figma-import/import/walk.d.ts +207 -0
  124. package/dist/taco/figma-import/import/walk.js +1464 -0
  125. package/dist/taco/figma-import/index.d.ts +22 -0
  126. package/dist/taco/figma-import/index.js +13 -0
  127. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  128. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  129. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  130. package/dist/taco/figma-import/kiwi/error.js +29 -0
  131. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  132. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  133. package/dist/taco/figma-import/kiwi/message.d.ts +12 -0
  134. package/dist/taco/figma-import/kiwi/message.js +147 -0
  135. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  136. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  137. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  138. package/dist/taco/figma-import/kiwi/types.js +18 -0
  139. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  140. package/dist/taco/model/agent-hidden-fields.js +29 -0
  141. package/dist/taco/model/asset-users.d.ts +23 -0
  142. package/dist/taco/model/asset-users.js +128 -0
  143. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  144. package/dist/taco/model/auto-layout-lines.js +70 -0
  145. package/dist/taco/model/codec.d.ts +444 -0
  146. package/dist/taco/model/codec.js +5665 -0
  147. package/dist/taco/model/color.d.ts +11 -0
  148. package/dist/taco/model/color.js +61 -0
  149. package/dist/taco/model/comment-operations.d.ts +102 -0
  150. package/dist/taco/model/comment-operations.js +227 -0
  151. package/dist/taco/model/component-evaluation.d.ts +96 -0
  152. package/dist/taco/model/component-evaluation.js +555 -0
  153. package/dist/taco/model/component-expressions.d.ts +242 -0
  154. package/dist/taco/model/component-expressions.js +1082 -0
  155. package/dist/taco/model/component-properties.d.ts +111 -0
  156. package/dist/taco/model/component-properties.js +339 -0
  157. package/dist/taco/model/component-samples.d.ts +83 -0
  158. package/dist/taco/model/component-samples.js +290 -0
  159. package/dist/taco/model/component-validity.d.ts +85 -0
  160. package/dist/taco/model/component-validity.js +397 -0
  161. package/dist/taco/model/components.d.ts +155 -0
  162. package/dist/taco/model/components.js +505 -0
  163. package/dist/taco/model/connector-ends.d.ts +20 -0
  164. package/dist/taco/model/connector-ends.js +188 -0
  165. package/dist/taco/model/connector-geometry.d.ts +74 -0
  166. package/dist/taco/model/connector-geometry.js +293 -0
  167. package/dist/taco/model/corner-radii.d.ts +26 -0
  168. package/dist/taco/model/corner-radii.js +47 -0
  169. package/dist/taco/model/derivations.d.ts +81 -0
  170. package/dist/taco/model/derivations.js +151 -0
  171. package/dist/taco/model/export-settings.d.ts +53 -0
  172. package/dist/taco/model/export-settings.js +156 -0
  173. package/dist/taco/model/expressions/ast.d.ts +153 -0
  174. package/dist/taco/model/expressions/ast.js +6 -0
  175. package/dist/taco/model/expressions/cache.d.ts +23 -0
  176. package/dist/taco/model/expressions/cache.js +43 -0
  177. package/dist/taco/model/expressions/check.d.ts +72 -0
  178. package/dist/taco/model/expressions/check.js +517 -0
  179. package/dist/taco/model/expressions/completion.d.ts +61 -0
  180. package/dist/taco/model/expressions/completion.js +421 -0
  181. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  182. package/dist/taco/model/expressions/evaluate.js +519 -0
  183. package/dist/taco/model/expressions/forms.d.ts +84 -0
  184. package/dist/taco/model/expressions/forms.js +200 -0
  185. package/dist/taco/model/expressions/index.d.ts +14 -0
  186. package/dist/taco/model/expressions/index.js +17 -0
  187. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  188. package/dist/taco/model/expressions/lexer.js +213 -0
  189. package/dist/taco/model/expressions/library.d.ts +33 -0
  190. package/dist/taco/model/expressions/library.js +117 -0
  191. package/dist/taco/model/expressions/names.d.ts +43 -0
  192. package/dist/taco/model/expressions/names.js +99 -0
  193. package/dist/taco/model/expressions/parser.d.ts +15 -0
  194. package/dist/taco/model/expressions/parser.js +438 -0
  195. package/dist/taco/model/expressions/rename.d.ts +34 -0
  196. package/dist/taco/model/expressions/rename.js +46 -0
  197. package/dist/taco/model/expressions/source.d.ts +15 -0
  198. package/dist/taco/model/expressions/source.js +161 -0
  199. package/dist/taco/model/expressions/types.d.ts +119 -0
  200. package/dist/taco/model/expressions/types.js +102 -0
  201. package/dist/taco/model/expressions/values.d.ts +97 -0
  202. package/dist/taco/model/expressions/values.js +160 -0
  203. package/dist/taco/model/geometry-limits.d.ts +16 -0
  204. package/dist/taco/model/geometry-limits.js +67 -0
  205. package/dist/taco/model/geometry.d.ts +47 -0
  206. package/dist/taco/model/geometry.js +193 -0
  207. package/dist/taco/model/group-bounds.d.ts +38 -0
  208. package/dist/taco/model/group-bounds.js +144 -0
  209. package/dist/taco/model/id-table.d.ts +37 -0
  210. package/dist/taco/model/id-table.js +80 -0
  211. package/dist/taco/model/index.d.ts +25 -0
  212. package/dist/taco/model/index.js +27 -0
  213. package/dist/taco/model/instance-resolver.d.ts +376 -0
  214. package/dist/taco/model/instance-resolver.js +1778 -0
  215. package/dist/taco/model/layer-syntax.d.ts +340 -0
  216. package/dist/taco/model/layer-syntax.js +3506 -0
  217. package/dist/taco/model/layout/constraints.d.ts +84 -0
  218. package/dist/taco/model/layout/constraints.js +445 -0
  219. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  220. package/dist/taco/model/layout/frame-layout.js +618 -0
  221. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  222. package/dist/taco/model/layout/node-sizing.js +486 -0
  223. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  224. package/dist/taco/model/legacy-connector-caps.js +70 -0
  225. package/dist/taco/model/linear-call.d.ts +48 -0
  226. package/dist/taco/model/linear-call.js +58 -0
  227. package/dist/taco/model/new-nodes.d.ts +145 -0
  228. package/dist/taco/model/new-nodes.js +277 -0
  229. package/dist/taco/model/node-builder.d.ts +449 -0
  230. package/dist/taco/model/node-builder.js +2294 -0
  231. package/dist/taco/model/node-table-watch.d.ts +36 -0
  232. package/dist/taco/model/node-table-watch.js +63 -0
  233. package/dist/taco/model/node-transform.d.ts +39 -0
  234. package/dist/taco/model/node-transform.js +79 -0
  235. package/dist/taco/model/node-users-index.d.ts +10 -0
  236. package/dist/taco/model/node-users-index.js +82 -0
  237. package/dist/taco/model/operation-references.d.ts +80 -0
  238. package/dist/taco/model/operation-references.js +399 -0
  239. package/dist/taco/model/operations.d.ts +610 -0
  240. package/dist/taco/model/operations.js +2882 -0
  241. package/dist/taco/model/overlay-table.d.ts +71 -0
  242. package/dist/taco/model/overlay-table.js +171 -0
  243. package/dist/taco/model/override-remap.d.ts +69 -0
  244. package/dist/taco/model/override-remap.js +230 -0
  245. package/dist/taco/model/parent-table.d.ts +53 -0
  246. package/dist/taco/model/parent-table.js +193 -0
  247. package/dist/taco/model/problem-collector.d.ts +27 -0
  248. package/dist/taco/model/problem-collector.js +101 -0
  249. package/dist/taco/model/sha256.d.ts +10 -0
  250. package/dist/taco/model/sha256.js +79 -0
  251. package/dist/taco/model/shape-geometry.d.ts +96 -0
  252. package/dist/taco/model/shape-geometry.js +368 -0
  253. package/dist/taco/model/shared-node-table.d.ts +97 -0
  254. package/dist/taco/model/shared-node-table.js +546 -0
  255. package/dist/taco/model/slots.d.ts +113 -0
  256. package/dist/taco/model/slots.js +281 -0
  257. package/dist/taco/model/stroke-settings.d.ts +37 -0
  258. package/dist/taco/model/stroke-settings.js +90 -0
  259. package/dist/taco/model/svg-path-import.d.ts +89 -0
  260. package/dist/taco/model/svg-path-import.js +219 -0
  261. package/dist/taco/model/text-measurement.d.ts +142 -0
  262. package/dist/taco/model/text-measurement.js +846 -0
  263. package/dist/taco/model/text-reads.d.ts +66 -0
  264. package/dist/taco/model/text-reads.js +128 -0
  265. package/dist/taco/model/text-sizer.d.ts +358 -0
  266. package/dist/taco/model/text-sizer.js +996 -0
  267. package/dist/taco/model/theme-users.d.ts +59 -0
  268. package/dist/taco/model/theme-users.js +312 -0
  269. package/dist/taco/model/thumbnail.d.ts +15 -0
  270. package/dist/taco/model/thumbnail.js +34 -0
  271. package/dist/taco/model/types.d.ts +2369 -0
  272. package/dist/taco/model/types.js +366 -0
  273. package/dist/taco/model/unknown-property.d.ts +5 -0
  274. package/dist/taco/model/unknown-property.js +7 -0
  275. package/dist/taco/model/validated-documents.d.ts +22 -0
  276. package/dist/taco/model/validated-documents.js +22 -0
  277. package/dist/taco/model/variable-modes.d.ts +61 -0
  278. package/dist/taco/model/variable-modes.js +438 -0
  279. package/dist/taco/model/variable-references.d.ts +54 -0
  280. package/dist/taco/model/variable-references.js +183 -0
  281. package/dist/taco/model/variables.d.ts +24 -0
  282. package/dist/taco/model/variables.js +151 -0
  283. package/dist/taco/model/vector-network.d.ts +34 -0
  284. package/dist/taco/model/vector-network.js +411 -0
  285. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  286. package/dist/taco/svg-import/import-svg.js +729 -0
  287. package/dist/taco/svg-import/index.d.ts +3 -0
  288. package/dist/taco/svg-import/index.js +3 -0
  289. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  290. package/dist/taco/svg-import/limits/limits.js +160 -0
  291. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  292. package/dist/taco/svg-import/paint/gradients.js +429 -0
  293. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  294. package/dist/taco/svg-import/paint/paint.js +162 -0
  295. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  296. package/dist/taco/svg-import/paths/paths.js +205 -0
  297. package/dist/taco/svg-import/public-types.d.ts +279 -0
  298. package/dist/taco/svg-import/public-types.js +56 -0
  299. package/dist/taco/svg-import/report/report.d.ts +44 -0
  300. package/dist/taco/svg-import/report/report.js +79 -0
  301. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  302. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  303. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  304. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  305. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  306. package/dist/taco/svg-import/structure/effects.js +618 -0
  307. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  308. package/dist/taco/svg-import/structure/image.js +495 -0
  309. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  310. package/dist/taco/svg-import/structure/walk.js +544 -0
  311. package/dist/taco/svg-import/style/color.d.ts +40 -0
  312. package/dist/taco/svg-import/style/color.js +275 -0
  313. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  314. package/dist/taco/svg-import/style/computed.js +1534 -0
  315. package/dist/taco/svg-import/style/css.d.ts +127 -0
  316. package/dist/taco/svg-import/style/css.js +541 -0
  317. package/dist/taco/svg-import/style/media.d.ts +25 -0
  318. package/dist/taco/svg-import/style/media.js +307 -0
  319. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  320. package/dist/taco/svg-import/style/properties.js +135 -0
  321. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  322. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  323. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  324. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  325. package/dist/taco/svg-import/text/text.d.ts +29 -0
  326. package/dist/taco/svg-import/text/text.js +545 -0
  327. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  328. package/dist/taco/svg-import/transform/matrix.js +229 -0
  329. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  330. package/dist/taco/svg-import/tree/element-data.js +155 -0
  331. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  332. package/dist/taco/svg-import/tree/elements.js +121 -0
  333. package/dist/taco/svg-import/units/length.d.ts +43 -0
  334. package/dist/taco/svg-import/units/length.js +125 -0
  335. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  336. package/dist/taco/svg-import/units/viewbox.js +77 -0
  337. package/package.json +55 -4
@@ -0,0 +1,329 @@
1
+ import { EXPRESSION_NAME_MAX_LENGTH, expressionNameProblem, formatExpressionName, normalizeExpressionName, } from "../../model/index.js";
2
+ import { field, guidText, isObject } from "./fields.js";
3
+ /**
4
+ * Figma's name kept as written, in NFC (expressions write it in backticks
5
+ * where it is not an identifier), only made unique: a name `taken` already
6
+ * has gets `_2`, `_3`, ... after it. Characters no name can hold (control,
7
+ * line break and bidirectional formatting ones) are dropped, an empty name
8
+ * becomes `property` and a name is cut to 256 characters, so the result
9
+ * depends only on the name and the names before it.
10
+ */
11
+ export function propertyNameOf(name, taken) {
12
+ let base = [...normalizeExpressionName(name)].filter((ch) => expressionNameProblem(ch) === undefined).join("");
13
+ if (base === "")
14
+ base = "property";
15
+ const cut = (text, room) => [...text].slice(0, room).join("");
16
+ base = cut(base, EXPRESSION_NAME_MAX_LENGTH);
17
+ let candidate = base;
18
+ for (let n = 2; taken.has(candidate); n++) {
19
+ const suffix = `_${String(n)}`;
20
+ candidate = cut(base, EXPRESSION_NAME_MAX_LENGTH - suffix.length) + suffix;
21
+ }
22
+ return candidate;
23
+ }
24
+ /** The defs of a node's `componentPropDefs`, keyed by id text. */
25
+ function defsById(node) {
26
+ const defs = new Map();
27
+ if (node === undefined)
28
+ return defs;
29
+ const where = `node ${node.id} componentPropDefs`;
30
+ for (const def of field(node.change, "componentPropDefs", "list", `node ${node.id}`) ?? []) {
31
+ if (isObject(def) && def["id"] !== undefined)
32
+ defs.set(guidText(def["id"], where), def);
33
+ }
34
+ return defs;
35
+ }
36
+ /** The defs of the COMPONENT_SET a variant sits in; empty for a SYMBOL that is not a variant. */
37
+ function setDefsOf(symbol, nodes) {
38
+ const owner = symbol.parentId === undefined ? undefined : nodes.get(symbol.parentId);
39
+ return owner?.change["isStateGroup"] === true ? defsById(owner) : new Map();
40
+ }
41
+ /**
42
+ * The Taco properties of a SYMBOL, from its `componentPropDefs`. A variant
43
+ * (a SYMBOL inside a COMPONENT_SET) holds only `{id, parentPropDefId}`
44
+ * entries, so the name, type and default come from the set's def. BOOL
45
+ * becomes `boolean`, TEXT `text` and INSTANCE_SWAP `instance`, whose default
46
+ * component comes from `componentOf` (`undefined` when that component is
47
+ * not in the message, which leaves the property out), and whose
48
+ * `preferredValues` become `preferred` through `componentsByKey`; a key that
49
+ * names no imported component is dropped and reported. SLOT becomes `slot`,
50
+ * with its `preferredValues` mapped the same way and its `slotPropConfig`
51
+ * settings (`slotSettingsOf`); its default content is the
52
+ * frames bound to it. VARIANT defs are not properties here: each variant
53
+ * imports as its own component. A deleted def (one with neither a type nor a
54
+ * name, as Figma keeps it after the property is removed) is skipped without a
55
+ * report. Other types are reported, and so is a name
56
+ * changed to be unique or
57
+ * to drop characters no name can hold (`propertyNameOf`). Each dropped
58
+ * preferred value is reported once, not once per variant that reads the def:
59
+ * `reported` holds the defs already reported, so share one set across the
60
+ * symbols of an import.
61
+ */
62
+ export function propertiesOf(symbol, nodes, componentOf, componentsByKey, report, reported = new WeakSet()) {
63
+ const setDefs = setDefsOf(symbol, nodes);
64
+ const where = `node ${symbol.id} componentPropDefs`;
65
+ const names = new Set();
66
+ const ids = new Set();
67
+ const properties = [];
68
+ for (const [ownId, own] of defsById(symbol)) {
69
+ const parentId = own["parentPropDefId"] === undefined ? undefined : guidText(own["parentPropDefId"], where);
70
+ // The def whose preferred values this reading takes: the variant's own when it has some, else the set's.
71
+ const source = own["preferredValues"] !== undefined ? own : (parentId === undefined ? undefined : setDefs.get(parentId)) ?? own;
72
+ const def = { ...(parentId === undefined ? {} : setDefs.get(parentId)), ...own };
73
+ const type = field(def, "type", "string", where);
74
+ if (type === "VARIANT" || (type === undefined && def["name"] === undefined))
75
+ continue;
76
+ const figmaName = field(def, "name", "string", where) ?? "";
77
+ const initial = field(def, "initialValue", "object", where) ?? {};
78
+ // A set's def id is shared by its variants, so a value keeps its property across variants; a second def pointing at it keeps its own id.
79
+ const shared = `figma-${parentId ?? ownId}`;
80
+ const base = { id: (ids.has(shared) ? `figma-${ownId}` : shared), name: propertyNameOf(figmaName, names) };
81
+ let definition;
82
+ if (type === "BOOL" || type === "BOOLEAN") {
83
+ definition = { ...base, kind: "boolean", defaultValue: field(initial, "boolValue", "boolean", where) ?? false };
84
+ }
85
+ else if (type === "TEXT") {
86
+ const text = field(initial, "textValue", "object", where);
87
+ definition = { ...base, kind: "text", defaultValue: (text === undefined ? undefined : field(text, "characters", "string", where)) ?? "" };
88
+ }
89
+ else if (type === "INSTANCE_SWAP") {
90
+ const target = initial["guidValue"] === undefined ? undefined : guidText(initial["guidValue"], where);
91
+ const component = target === undefined ? undefined : componentOf(target);
92
+ if (component === undefined) {
93
+ report("property-left-out", "an instance swap property is left out: its default component is not in the message");
94
+ continue;
95
+ }
96
+ const preferred = preferredOf(def, componentsByKey, where, onceFor(source, reported, report));
97
+ definition = { ...base, kind: "instance", defaultValue: component, ...(preferred.length === 0 ? {} : { preferred }) };
98
+ }
99
+ else if (type === "SLOT") {
100
+ // The default content is the bound frames' own children, so a slot has no default value.
101
+ const preferred = preferredOf(def, componentsByKey, where, onceFor(source, reported, report), "a slot");
102
+ definition = { ...base, kind: "slot", ...(preferred.length === 0 ? {} : { preferred }), ...slotSettingsOf(def, where, report) };
103
+ }
104
+ else {
105
+ report("property-left-out", `component property type ${String(type)} is not imported yet`);
106
+ continue;
107
+ }
108
+ if (base.name !== normalizeExpressionName(figmaName)) {
109
+ report("approximated", "a component property name is changed: a name used twice gets a number, and control, line break and bidirectional formatting characters are dropped");
110
+ }
111
+ names.add(base.name);
112
+ ids.add(base.id);
113
+ properties.push({ definition, figmaIds: parentId === undefined ? [ownId] : [ownId, parentId] });
114
+ }
115
+ return properties;
116
+ }
117
+ /** Taco's slot settings by the `slotPropConfig` field of a SLOT def that sets each. */
118
+ const SLOT_SETTINGS = { allowPreferredValuesOnly: "preferredOnly", minChildren: "minChildren", maxChildren: "maxChildren", stretchChildOnInsert: "fillCrossAxisOnInsert" };
119
+ /**
120
+ * The settings of a SLOT def's `slotPropConfig`: `allowPreferredValuesOnly`,
121
+ * `minChildren`, `maxChildren` and `stretchChildOnInsert` map to the slot
122
+ * fields of the same meaning, and `displayByDefault: false` to
123
+ * `displayEmptyByDefault`. A zero `maxChildren` sets no limit. Limits that
124
+ * cross (a minimum above the maximum) are dropped, and they and any field
125
+ * Taco has no setting for are reported.
126
+ */
127
+ function slotSettingsOf(def, where, report) {
128
+ const config = field(def, "slotPropConfig", "object", where);
129
+ const settings = {};
130
+ for (const [name, value] of Object.entries(config ?? {})) {
131
+ const taco = SLOT_SETTINGS[name];
132
+ if (name === "displayByDefault" && typeof value === "boolean") {
133
+ if (!value)
134
+ settings["displayEmptyByDefault"] = true;
135
+ }
136
+ else if (taco !== undefined && (name === "minChildren" || name === "maxChildren") && Number.isInteger(value) && value >= 0) {
137
+ if (value !== 0)
138
+ settings[taco] = value;
139
+ }
140
+ else if (taco !== undefined && typeof value === "boolean") {
141
+ if (value)
142
+ settings[taco] = true;
143
+ }
144
+ else {
145
+ report("property-left-out", `a slot setting ${name} is not imported: Taco slots have no such setting`);
146
+ }
147
+ }
148
+ if (typeof settings["minChildren"] === "number" && typeof settings["maxChildren"] === "number" && settings["minChildren"] > settings["maxChildren"]) {
149
+ report("property-left-out", "a slot's child limits are left out: its minimum is above its maximum");
150
+ delete settings["minChildren"];
151
+ delete settings["maxChildren"];
152
+ }
153
+ return settings;
154
+ }
155
+ /**
156
+ * Looks components up by the `componentKey` of a SYMBOL or a COMPONENT_SET
157
+ * (a FRAME with `isStateGroup`): a set gives its imported variants in set
158
+ * order. A key no node holds, or whose components were not imported, gives
159
+ * an empty list.
160
+ */
161
+ export function componentsByKeyOf(nodes, componentOf) {
162
+ const symbols = new Map();
163
+ const sets = new Map();
164
+ for (const node of nodes.values()) {
165
+ const key = field(node.change, "componentKey", "string", `node ${node.id}`);
166
+ if (key === undefined || key === "")
167
+ continue;
168
+ if (node.type === "SYMBOL" && !symbols.has(key))
169
+ symbols.set(key, node);
170
+ else if (node.change["isStateGroup"] === true && !sets.has(key))
171
+ sets.set(key, node);
172
+ }
173
+ return (type, key) => {
174
+ const found = (type === "COMPONENT" ? symbols : sets).get(key);
175
+ const members = found === undefined ? [] : type === "COMPONENT" ? [found] : found.children.filter((child) => child.type === "SYMBOL");
176
+ return members.flatMap((member) => componentOf(member.id) ?? []);
177
+ };
178
+ }
179
+ /** `report` for the first reading of `def` that reports anything, and a report that drops everything for later ones. */
180
+ function onceFor(def, reported, report) {
181
+ if (reported.has(def))
182
+ return () => undefined;
183
+ return (kind, reason) => { reported.add(def); report(kind, reason); };
184
+ }
185
+ /**
186
+ * The components an INSTANCE_SWAP or SLOT def's `preferredValues.instanceSwapValues`
187
+ * name, in order and each once (`what` names the property in the report). A key of a component the import does not
188
+ * hold, or of another type, is dropped and reported (`propertiesOf` reports them for one reading of the def only).
189
+ */
190
+ function preferredOf(def, componentsByKey, where, report, what = "an instance swap") {
191
+ const values = field(def, "preferredValues", "object", where);
192
+ const preferred = new Set();
193
+ for (const value of (values === undefined ? undefined : field(values, "instanceSwapValues", "list", where)) ?? []) {
194
+ const type = isObject(value) ? value["type"] : undefined;
195
+ const key = isObject(value) ? field(value, "key", "string", where) : undefined;
196
+ if (type !== "COMPONENT" && type !== "STATE_GROUP") {
197
+ report("property-left-out", `a preferred component of type ${String(type)} of ${what} property is not imported yet`);
198
+ continue;
199
+ }
200
+ const components = key === undefined ? [] : componentsByKey(type, key);
201
+ if (components.length === 0)
202
+ report("property-left-out", `a preferred component of ${what} property is dropped: its component key is not in the import`);
203
+ for (const component of components)
204
+ preferred.add(component);
205
+ }
206
+ return [...preferred];
207
+ }
208
+ /** The property a Figma def id names, by the def's own id or the set def it points at. */
209
+ export function propertyByFigmaId(properties, figmaId) {
210
+ return properties.find((property) => property.figmaIds.includes(figmaId));
211
+ }
212
+ /**
213
+ * The expressions that bind a component layer's `componentPropRefs` to the
214
+ * component's properties: VISIBLE computes `visible`, TEXT_DATA on a text
215
+ * layer `characters` and OVERRIDDEN_SYMBOL_ID on a nested instance
216
+ * `componentId`, each reading the property by name (in backticks where
217
+ * the name is not an identifier, `` `Show icon` ``). SLOT_CONTENT_ID is
218
+ * left to `slotBindingOf`. A ref on the component
219
+ * root (which cannot compute fields), to a property that was not imported,
220
+ * or of another field is reported.
221
+ */
222
+ export function propertyExpressions(layer, isRoot, properties, report) {
223
+ const where = `node ${layer.id} componentPropRefs`;
224
+ const expressions = {};
225
+ for (const ref of field(layer.change, "componentPropRefs", "list", `node ${layer.id}`) ?? []) {
226
+ if (!isObject(ref) || ref["defID"] === undefined || ref["isDeleted"] === true)
227
+ continue;
228
+ const kind = ref["componentPropNodeField"];
229
+ // A slot binding is no expression: it becomes `FrameNode.slot` (`slotBindingOf`).
230
+ if (kind === "SLOT_CONTENT_ID")
231
+ continue;
232
+ const property = propertyByFigmaId(properties, guidText(ref["defID"], where));
233
+ const target = kind === "VISIBLE" ? "visible" : kind === "TEXT_DATA" && layer.type === "TEXT" ? "characters"
234
+ : kind === "OVERRIDDEN_SYMBOL_ID" && layer.type === "INSTANCE" ? "componentId" : undefined;
235
+ if (target === undefined) {
236
+ report("property-left-out", `a component property bound to ${String(kind)} of a ${layer.type} is not imported yet`);
237
+ }
238
+ else if (isRoot) {
239
+ report("property-left-out", "a component property bound to the component's own frame is left out: a component root cannot compute fields");
240
+ }
241
+ else if (property === undefined) {
242
+ report("property-left-out", "a layer bound to a component property that was not imported keeps its own value");
243
+ }
244
+ else {
245
+ expressions[target] = formatExpressionName(property.definition.name);
246
+ }
247
+ }
248
+ return expressions;
249
+ }
250
+ /** The ids of a symbol's VARIANT defs and of its set's, whose values need no Taco property. */
251
+ export function variantDefIds(symbol, nodes) {
252
+ const ids = new Set();
253
+ for (const [id, def] of setDefsOf(symbol, nodes))
254
+ if (def["type"] === "VARIANT")
255
+ ids.add(id);
256
+ for (const [id, def] of defsById(symbol)) {
257
+ const parent = def["parentPropDefId"] === undefined ? undefined : guidText(def["parentPropDefId"], `node ${symbol.id}`);
258
+ if (def["type"] === "VARIANT" || (parent !== undefined && ids.has(parent)))
259
+ ids.add(id);
260
+ }
261
+ return ids;
262
+ }
263
+ /**
264
+ * The value a `componentPropAssignments` entry gives. Older files keep it in
265
+ * `value` (`boolValue`, `textValue.characters`, `guidValue`); newer files
266
+ * leave `value` empty and keep it in `varValue.value` (`boolValue`,
267
+ * `textDataValue.characters`, `symbolIdValue.guid`), as slot values do.
268
+ */
269
+ export function assignedValueOf(assignment, at) {
270
+ const value = field(assignment, "value", "object", at) ?? {};
271
+ const varValue = field(assignment, "varValue", "object", at);
272
+ const varAt = `${at}.varValue.value`;
273
+ const held = (varValue === undefined ? undefined : field(varValue, "value", "object", `${at}.varValue`)) ?? {};
274
+ const bool = field(value, "boolValue", "boolean", at) ?? field(held, "boolValue", "boolean", varAt);
275
+ const plain = field(value, "textValue", "object", at);
276
+ const legacyText = plain && field(plain, "characters", "string", at);
277
+ const textData = legacyText === undefined ? field(held, "textDataValue", "object", varAt) : plain;
278
+ const text = legacyText ?? (textData && field(textData, "characters", "string", varAt));
279
+ const swap = field(held, "symbolIdValue", "object", varAt);
280
+ const symbolId = value["guidValue"] !== undefined ? guidText(value["guidValue"], at)
281
+ : swap?.["guid"] !== undefined ? guidText(swap["guid"], `${varAt}.symbolIdValue`) : undefined;
282
+ return { ...(bool === undefined ? {} : { bool }), ...(text === undefined ? {} : { text, textData: textData }), ...(symbolId === undefined ? {} : { symbolId }) };
283
+ }
284
+ /**
285
+ * `componentPropAssignments` as Taco values of `properties`. A VARIANT value
286
+ * (`variantDefs`) and a slot value (which becomes a slot host) are skipped: the variant an instance shows is already its
287
+ * component. An instance swap value names a component from `componentOf`.
288
+ * A value naming no imported property, a swap to a component that is not
289
+ * in the message, or a swap `leftOutSwap` gives a reason to leave out (it
290
+ * would nest a component in itself), is reported and left out.
291
+ */
292
+ export function assignedPropertyValues(list, properties, variantDefs, componentOf, leftOutSwap, where, report, textAssignment) {
293
+ const values = {};
294
+ (list ?? []).forEach((assignment, index) => {
295
+ const at = `${where} componentPropAssignments[${String(index)}]`;
296
+ if (!isObject(assignment) || assignment["defID"] === undefined)
297
+ return;
298
+ const defId = guidText(assignment["defID"], at);
299
+ const property = propertyByFigmaId(properties, defId);
300
+ if (property === undefined) {
301
+ if (!variantDefs.has(defId))
302
+ report("property-left-out", "a component property value names a property that was not imported");
303
+ return;
304
+ }
305
+ const { definition } = property;
306
+ if (definition.kind !== "boolean" && definition.kind !== "text" && definition.kind !== "instance")
307
+ return;
308
+ const value = assignedValueOf(assignment, at);
309
+ if (definition.kind === "boolean" && value.bool !== undefined)
310
+ values[definition.id] = value.bool;
311
+ else if (definition.kind === "text" && value.text !== undefined) {
312
+ values[definition.id] = value.text;
313
+ textAssignment?.(definition.id, value.textData, value.text);
314
+ }
315
+ else if (definition.kind === "instance" && value.symbolId !== undefined) {
316
+ const component = componentOf(value.symbolId);
317
+ const leftOut = component === undefined ? undefined : leftOutSwap(value.symbolId, definition.id);
318
+ if (component === undefined)
319
+ report("property-left-out", "an instance swap value is left out: its component is not in the message");
320
+ else if (leftOut !== undefined)
321
+ report("property-left-out", `an instance swap value ${leftOut}`);
322
+ else
323
+ values[definition.id] = component;
324
+ }
325
+ else
326
+ report("property-left-out", `a ${definition.kind === "instance" ? "instance swap" : definition.kind} property value is left out: it could not be read`);
327
+ });
328
+ return values;
329
+ }
@@ -0,0 +1,85 @@
1
+ import { type ComponentDefinition, type ComponentId, type ComponentPropertyDefinition, type DocumentTheme, type FrameNode, type NodeId, type SceneNode } from "../../model/index.ts";
2
+ import type { FigmaTreeNode } from "./tree.ts";
3
+ /**
4
+ * A set keeps its variant properties and validity, with one merged layer tree.
5
+ * Fields the model cannot compute retain the stacked tree for the whole set.
6
+ */
7
+ /** One imported variant: the SYMBOL, its component and its root. */
8
+ export interface ImportedVariant {
9
+ readonly symbol: FigmaTreeNode;
10
+ readonly componentId: ComponentId;
11
+ readonly root: FrameNode;
12
+ }
13
+ /** What the merge needs of the import. */
14
+ export interface ComponentSetInput {
15
+ readonly nodes: readonly SceneNode[];
16
+ readonly theme: DocumentTheme;
17
+ readonly components: readonly ComponentDefinition[];
18
+ /** Every imported SYMBOL's component, in import order. */
19
+ readonly variants: readonly ImportedVariant[];
20
+ readonly figmaNodes: ReadonlyMap<string, FigmaTreeNode>;
21
+ /** Layers that carry source-specific metadata and must keep their identity across variants. */
22
+ readonly distinctLayers?: ReadonlySet<NodeId>;
23
+ readonly newId: () => NodeId;
24
+ /** The key of the set's component, from the set node. */
25
+ readonly keyOf: (set: FigmaTreeNode) => string;
26
+ /** The name of the set's component, from the set node. */
27
+ readonly nameOf: (set: FigmaTreeNode) => string;
28
+ readonly report: (node: FigmaTreeNode, kind: "property-left-out" | "approximated", reason: string) => void;
29
+ }
30
+ export interface ComponentSetOutput {
31
+ readonly nodes: SceneNode[];
32
+ readonly layerOf: ReadonlyMap<NodeId, NodeId>;
33
+ readonly components: ComponentDefinition[];
34
+ /** The component each merged SYMBOL and set went to, by Figma guid. */
35
+ readonly componentOf: ReadonlyMap<string, ComponentId>;
36
+ }
37
+ /** One set ready to merge. */
38
+ interface PlannedSet {
39
+ readonly set: FigmaTreeNode;
40
+ readonly variants: readonly ImportedVariant[];
41
+ /**
42
+ * Variants with the same values as an earlier one (`twin`, an index into
43
+ * `variants`). Figma keeps such a set and flags the conflict; the merge
44
+ * keeps each one's layers, never shown, and an instance of it shows its twin.
45
+ */
46
+ readonly duplicates: readonly {
47
+ readonly variant: ImportedVariant;
48
+ readonly twin: number;
49
+ }[];
50
+ readonly properties: readonly EnumProperty[];
51
+ /** The option id of each enum property, by property id, for each variant in `variants`' order. */
52
+ readonly values: readonly Readonly<Record<string, string>>[];
53
+ readonly defaultIndex: number;
54
+ /** The validity expression, when the set does not have every combination. */
55
+ readonly validity?: string;
56
+ }
57
+ type EnumProperty = Extract<ComponentPropertyDefinition, {
58
+ readonly kind: "enum";
59
+ }>;
60
+ /**
61
+ * A component's name without the parts, between `/`, that only list variant
62
+ * values: `Avatar / variant=img, type=success` gives `Avatar`, and
63
+ * `size=md` gives an empty name. A value may hold a `/`: a name that is only
64
+ * a list, such as `size=md, item=initials/icon`, gives an empty name, and
65
+ * `Avatar / item=initials/icon, size=md` gives `Avatar`. A name with no such
66
+ * part is kept as written.
67
+ */
68
+ export declare function withoutVariantParts(name: string): string;
69
+ /**
70
+ * Plans one set, or gives the reason it stays one component per variant:
71
+ * a variant without a value for each VARIANT property. A variant with the
72
+ * same values as an earlier one is one of the plan's `duplicates`.
73
+ * @param set - Figma component set.
74
+ * @param allVariants - Imported member mains in Figma order.
75
+ * @returns The ordered axes and choices, or the reason a set cannot be represented.
76
+ */
77
+ export declare function planSet(set: FigmaTreeNode, allVariants: readonly ImportedVariant[]): PlannedSet | string;
78
+ /**
79
+ * Merges each component set into one component (see the top of this file).
80
+ * A set whose variants cannot be told apart by their values, or whose merge
81
+ * would nest a component in itself, stays one component per variant and is
82
+ * reported.
83
+ */
84
+ export declare function mergeComponentSets(input: ComponentSetInput): ComponentSetOutput;
85
+ export {};