@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,14 @@
1
+ export interface ZipLimits {
2
+ readonly maxEntries: number;
3
+ readonly maxEntryBytes: number;
4
+ readonly maxTotalBytes: number;
5
+ }
6
+ /**
7
+ * Unzips in two steps. The first reads the central directory (APPNOTE 4.3.12
8
+ * to 4.3.16, with the ZIP64 records) and checks the entry count and the
9
+ * declared sizes against the limits, before anything is inflated. The
10
+ * second inflates each entry in small steps and counts the bytes it really
11
+ * produces, so an entry whose data inflates to more than its declared size
12
+ * is refused instead of being inflated in full.
13
+ */
14
+ export declare function unzipWithLimits(bytes: Uint8Array, limits: ZipLimits): Map<string, Uint8Array>;
@@ -0,0 +1,154 @@
1
+ import { Inflate } from "fflate";
2
+ import { FigmaReadError } from "./error.js";
3
+ const END_OF_DIRECTORY = 0x06054b50;
4
+ const ZIP64_LOCATOR = 0x07064b50;
5
+ const ZIP64_END_OF_DIRECTORY = 0x06064b50;
6
+ const DIRECTORY_HEADER = 0x02014b50;
7
+ const LOCAL_HEADER = 0x04034b50;
8
+ const STORED = 0;
9
+ const DEFLATED = 8;
10
+ /**
11
+ * Compressed bytes given to the inflater at a time. DEFLATE expands at most
12
+ * about 1032 times, so one step yields at most about 8 MiB beyond the
13
+ * declared size before the count stops it.
14
+ */
15
+ const INFLATE_STEP = 8 * 1024;
16
+ /**
17
+ * Unzips in two steps. The first reads the central directory (APPNOTE 4.3.12
18
+ * to 4.3.16, with the ZIP64 records) and checks the entry count and the
19
+ * declared sizes against the limits, before anything is inflated. The
20
+ * second inflates each entry in small steps and counts the bytes it really
21
+ * produces, so an entry whose data inflates to more than its declared size
22
+ * is refused instead of being inflated in full.
23
+ */
24
+ export function unzipWithLimits(bytes, limits) {
25
+ const entries = readDirectory(bytes, limits);
26
+ const files = new Map();
27
+ for (const entry of entries) {
28
+ files.set(entry.name, readEntry(bytes, entry));
29
+ }
30
+ return files;
31
+ }
32
+ function readDirectory(bytes, limits) {
33
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
34
+ const u16 = (at) => view.getUint16(at, true);
35
+ const u32 = (at) => view.getUint32(at, true);
36
+ const u64 = (at) => u32(at) + u32(at + 4) * 2 ** 32;
37
+ const fail = (reason) => {
38
+ throw new FigmaReadError("bad-zip", `the ZIP directory is unreadable: ${reason}`);
39
+ };
40
+ const inside = (at, length) => {
41
+ if (at < 0 || at + length > bytes.length)
42
+ fail("a record points past the end of the file");
43
+ };
44
+ // The end record is 22 bytes followed by a comment of at most 65535 bytes.
45
+ let end = bytes.length - 22;
46
+ const lowest = Math.max(0, end - 0xffff);
47
+ while (end >= lowest && !(end >= 0 && u32(end) === END_OF_DIRECTORY))
48
+ end -= 1;
49
+ if (end < lowest || end < 0)
50
+ fail("no end of central directory record");
51
+ let count = u16(end + 10);
52
+ let offset = u32(end + 16);
53
+ const zip64 = end >= 20 && u32(end - 20) === ZIP64_LOCATOR;
54
+ if (zip64) {
55
+ const record = u64(end - 12);
56
+ inside(record, 56);
57
+ if (u32(record) !== ZIP64_END_OF_DIRECTORY)
58
+ fail("the ZIP64 end record is missing");
59
+ count = u64(record + 32);
60
+ offset = u64(record + 48);
61
+ }
62
+ if (count > limits.maxEntries) {
63
+ throw new FigmaReadError("too-many-entries", `the ZIP has more than maxFileEntries (${limits.maxEntries}) entries`);
64
+ }
65
+ const entries = [];
66
+ let total = 0;
67
+ for (let index = 0; index < count; index += 1) {
68
+ inside(offset, 46);
69
+ if (u32(offset) !== DIRECTORY_HEADER)
70
+ fail(`entry ${index} has no central directory header`);
71
+ const flags = u16(offset + 8);
72
+ const method = u16(offset + 10);
73
+ let compressedSize = u32(offset + 20);
74
+ let size = u32(offset + 24);
75
+ const nameLength = u16(offset + 28);
76
+ const extraLength = u16(offset + 30);
77
+ const commentLength = u16(offset + 32);
78
+ let localHeader = u32(offset + 42);
79
+ const nameStart = offset + 46;
80
+ inside(nameStart, nameLength + extraLength);
81
+ const nameBytes = bytes.subarray(nameStart, nameStart + nameLength);
82
+ // Bit 11 marks a UTF-8 name; other names are read byte by byte, as fflate does.
83
+ const name = flags & 0x800 ? new TextDecoder().decode(nameBytes) : String.fromCharCode(...nameBytes);
84
+ // APPNOTE 4.5.3: the ZIP64 extra field holds only the fields whose
85
+ // 32-bit value is 0xFFFFFFFF, in this order.
86
+ if (zip64 && (size === 0xffffffff || compressedSize === 0xffffffff || localHeader === 0xffffffff)) {
87
+ const extraEnd = nameStart + nameLength + extraLength;
88
+ let field = nameStart + nameLength;
89
+ while (field + 4 <= extraEnd && u16(field) !== 1)
90
+ field += 4 + u16(field + 2);
91
+ if (field + 4 > extraEnd)
92
+ fail(`entry ${JSON.stringify(name)} has no ZIP64 sizes`);
93
+ let at = field + 4;
94
+ if (size === 0xffffffff)
95
+ (size = u64(at)), (at += 8);
96
+ if (compressedSize === 0xffffffff)
97
+ (compressedSize = u64(at)), (at += 8);
98
+ if (localHeader === 0xffffffff)
99
+ localHeader = u64(at);
100
+ }
101
+ const largest = Math.max(size, compressedSize);
102
+ if (largest > limits.maxEntryBytes) {
103
+ throw new FigmaReadError("entry-too-large", `the ZIP entry ${JSON.stringify(name)} is ${largest} bytes, more than maxFileEntryBytes (${limits.maxEntryBytes})`);
104
+ }
105
+ total += largest;
106
+ if (total > limits.maxTotalBytes) {
107
+ throw new FigmaReadError("file-too-large", `the ZIP entries add up to more than maxFileTotalBytes (${limits.maxTotalBytes}) bytes`);
108
+ }
109
+ entries.push({ name, method, compressedSize, size, localHeader });
110
+ offset = nameStart + nameLength + extraLength + commentLength;
111
+ }
112
+ return entries;
113
+ }
114
+ function readEntry(bytes, entry) {
115
+ const corrupt = (reason) => {
116
+ throw new FigmaReadError("corrupt-entry", `the ZIP entry ${JSON.stringify(entry.name)} is corrupt: ${reason}`);
117
+ };
118
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
119
+ const header = entry.localHeader;
120
+ if (header + 30 > bytes.length || view.getUint32(header, true) !== LOCAL_HEADER)
121
+ corrupt("no local header");
122
+ const start = header + 30 + view.getUint16(header + 26, true) + view.getUint16(header + 28, true);
123
+ if (start + entry.compressedSize > bytes.length)
124
+ corrupt("its data runs past the end of the file");
125
+ const data = bytes.subarray(start, start + entry.compressedSize);
126
+ if (entry.method === STORED) {
127
+ if (entry.compressedSize !== entry.size)
128
+ corrupt("a stored entry's two sizes differ");
129
+ return data.slice();
130
+ }
131
+ if (entry.method !== DEFLATED)
132
+ corrupt(`compression method ${entry.method} is not supported`);
133
+ const out = new Uint8Array(entry.size);
134
+ let written = 0;
135
+ const inflater = new Inflate((chunk) => {
136
+ if (written + chunk.length > entry.size)
137
+ corrupt(`it inflates to more than its declared ${entry.size} bytes`);
138
+ out.set(chunk, written);
139
+ written += chunk.length;
140
+ });
141
+ try {
142
+ for (let at = 0; at < data.length || at === 0; at += INFLATE_STEP) {
143
+ inflater.push(data.subarray(at, at + INFLATE_STEP), at + INFLATE_STEP >= data.length);
144
+ }
145
+ }
146
+ catch (error) {
147
+ if (error instanceof FigmaReadError)
148
+ throw error;
149
+ corrupt(error instanceof Error ? error.message : String(error));
150
+ }
151
+ if (written !== entry.size)
152
+ corrupt(`it inflates to ${written} bytes, not its declared ${entry.size}`);
153
+ return out;
154
+ }
@@ -0,0 +1,34 @@
1
+ import { type KiwiContainerHeader, type KiwiContainerOptions } from "./container/kiwiContainer.ts";
2
+ import type { KiwiObject, KiwiSchema } from "./kiwi/types.ts";
3
+ export interface FigFileOptions extends KiwiContainerOptions {
4
+ /** Defaults to `FIGMA_READ_LIMITS.maxFileEntries`. */
5
+ readonly maxFileEntries?: number;
6
+ /** Defaults to `FIGMA_READ_LIMITS.maxFileEntryBytes`. */
7
+ readonly maxFileEntryBytes?: number;
8
+ /** Defaults to `FIGMA_READ_LIMITS.maxFileTotalBytes`. */
9
+ readonly maxFileTotalBytes?: number;
10
+ }
11
+ export interface FigFile {
12
+ /** The parsed `meta.json`, or `null` when the file has none (older files are a bare container). */
13
+ readonly meta: Readonly<Record<string, unknown>> | null;
14
+ readonly header: KiwiContainerHeader;
15
+ readonly schema: KiwiSchema;
16
+ readonly message: KiwiObject;
17
+ /** Image bytes by the name under `images/`, which is the hex SHA-1 a paint's `image.hash` holds. */
18
+ readonly images: ReadonlyMap<string, Uint8Array>;
19
+ /**
20
+ * The `thumbnail.png` bytes, when the file has one. A bare container's
21
+ * third chunk is used instead when it holds a PNG.
22
+ */
23
+ readonly thumbnail?: Uint8Array;
24
+ }
25
+ /**
26
+ * Reads a saved Figma design file. A current .fig file is a ZIP holding
27
+ * `canvas.fig` (a Kiwi container, the same form the clipboard uses),
28
+ * `meta.json`, `thumbnail.png` and `images/<sha1 hex>`; an older one is a
29
+ * bare Kiwi container. ZIP entries are stored or deflated. Every entry's
30
+ * declared size is checked against the limits before anything is inflated,
31
+ * and an entry that inflates past its declared size is refused. FigJam
32
+ * boards and Slides decks are refused with `not-design-file`.
33
+ */
34
+ export declare function readFigFile(bytes: Uint8Array, options?: FigFileOptions): FigFile;
@@ -0,0 +1,91 @@
1
+ import { FigmaReadError } from "./container/error.js";
2
+ import { KIWI_CONTAINER_MAGICS, readKiwiContainer, } from "./container/kiwiContainer.js";
3
+ import { FIGMA_READ_LIMITS } from "./container/limits.js";
4
+ import { unzipWithLimits } from "./container/zip.js";
5
+ const CANVAS_ENTRY = "canvas.fig";
6
+ const META_ENTRY = "meta.json";
7
+ const THUMBNAIL_ENTRY = "thumbnail.png";
8
+ const IMAGES_FOLDER = "images/";
9
+ const ZIP_LOCAL_HEADER = [0x50, 0x4b, 0x03, 0x04];
10
+ const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
11
+ const DESIGN_MAGIC = "fig-kiwi";
12
+ /**
13
+ * Reads a saved Figma design file. A current .fig file is a ZIP holding
14
+ * `canvas.fig` (a Kiwi container, the same form the clipboard uses),
15
+ * `meta.json`, `thumbnail.png` and `images/<sha1 hex>`; an older one is a
16
+ * bare Kiwi container. ZIP entries are stored or deflated. Every entry's
17
+ * declared size is checked against the limits before anything is inflated,
18
+ * and an entry that inflates past its declared size is refused. FigJam
19
+ * boards and Slides decks are refused with `not-design-file`.
20
+ */
21
+ export function readFigFile(bytes, options = {}) {
22
+ if (!(bytes instanceof Uint8Array)) {
23
+ throw new TypeError("readFigFile expects a Uint8Array");
24
+ }
25
+ if (!startsWith(bytes, ZIP_LOCAL_HEADER)) {
26
+ if (!isKiwiMagic(magicOf(bytes))) {
27
+ throw new FigmaReadError("not-figma", "the file is neither a ZIP nor a Figma container", 0);
28
+ }
29
+ const { preview, ...canvas } = readCanvas(bytes, "the file", options);
30
+ return { ...canvas, meta: null, images: new Map(), ...(preview === undefined ? {} : { thumbnail: preview }) };
31
+ }
32
+ const entries = unzipWithLimits(bytes, {
33
+ maxEntries: options.maxFileEntries ?? FIGMA_READ_LIMITS.maxFileEntries,
34
+ maxEntryBytes: options.maxFileEntryBytes ?? FIGMA_READ_LIMITS.maxFileEntryBytes,
35
+ maxTotalBytes: options.maxFileTotalBytes ?? FIGMA_READ_LIMITS.maxFileTotalBytes,
36
+ });
37
+ const canvas = entries.get(CANVAS_ENTRY);
38
+ if (canvas === undefined) {
39
+ throw new FigmaReadError("not-figma", `the ZIP has no ${CANVAS_ENTRY} entry, so it is not a Figma file`);
40
+ }
41
+ const images = new Map();
42
+ for (const [name, data] of entries) {
43
+ if (name.startsWith(IMAGES_FOLDER) && name.length > IMAGES_FOLDER.length && !name.endsWith("/")) {
44
+ images.set(name.slice(IMAGES_FOLDER.length), data);
45
+ }
46
+ }
47
+ const metaBytes = entries.get(META_ENTRY);
48
+ const thumbnail = entries.get(THUMBNAIL_ENTRY);
49
+ const { header, schema, message } = readCanvas(canvas, CANVAS_ENTRY, options);
50
+ return {
51
+ header,
52
+ schema,
53
+ message,
54
+ meta: metaBytes === undefined ? null : readMeta(metaBytes),
55
+ images,
56
+ ...(thumbnail === undefined ? {} : { thumbnail }),
57
+ };
58
+ }
59
+ function readCanvas(bytes, where, options) {
60
+ const magic = magicOf(bytes);
61
+ if (magic !== DESIGN_MAGIC && isKiwiMagic(magic)) {
62
+ const kind = magic === "fig-jam." ? "a FigJam board" : "a Slides deck";
63
+ throw new FigmaReadError("not-design-file", `${where} is ${kind}; only Figma design files are read`, 0);
64
+ }
65
+ const { header, schema, message, chunks } = readKiwiContainer(bytes, options);
66
+ const third = chunks[2];
67
+ // Figma sometimes adds a preview image as the third chunk; only a PNG is taken as one.
68
+ const preview = third !== undefined && startsWith(third, PNG_SIGNATURE) ? third : undefined;
69
+ return { header, schema, message, preview };
70
+ }
71
+ function readMeta(bytes) {
72
+ let value;
73
+ try {
74
+ value = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
75
+ }
76
+ catch (error) {
77
+ throw new FigmaReadError("bad-meta", `${META_ENTRY} is not UTF-8 JSON: ${messageOf(error)}`);
78
+ }
79
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
80
+ throw new FigmaReadError("bad-meta", `${META_ENTRY} is not a JSON object`);
81
+ }
82
+ return value;
83
+ }
84
+ const magicOf = (bytes) => String.fromCharCode(...bytes.subarray(0, 8));
85
+ const isKiwiMagic = (magic) => KIWI_CONTAINER_MAGICS.includes(magic);
86
+ function startsWith(bytes, prefix) {
87
+ return prefix.every((byte, index) => bytes[index] === byte);
88
+ }
89
+ function messageOf(error) {
90
+ return error instanceof Error ? error.message : String(error);
91
+ }
@@ -0,0 +1,25 @@
1
+ import { type DesignDocument, type DocumentId } from "../model/index.ts";
2
+ import type { FigmaImportResult } from "./import/types.ts";
3
+ export interface FigmaFileDocumentOptions {
4
+ readonly id: DocumentId;
5
+ readonly title: string;
6
+ /** Gives each page and its root frame an id; called once a page. */
7
+ readonly idSource: () => string;
8
+ /**
9
+ * The document the pages go into, such as a host's blank document: its
10
+ * theme, assets and other fields are kept, its pages and layers are
11
+ * replaced.
12
+ */
13
+ readonly base: DesignDocument;
14
+ }
15
+ /**
16
+ * The document a whole .fig file makes: one page for each imported page
17
+ * (`importFigmaMessage` with `roots: "pages"`), in the file's order and with
18
+ * its name and chosen variable modes, each page's layers under a root frame
19
+ * of its own, every image the layers use as an image asset, every component
20
+ * made from a SYMBOL added to the base's components (their layers are among
21
+ * `nodes`, on no page), and the file's variable collections added to the base
22
+ * theme's. The result is not checked here;
23
+ * `deserializeDocument` (or a store made from it) checks it.
24
+ */
25
+ export declare function figmaFileDocument(result: Pick<FigmaImportResult, "pages" | "nodes" | "assets" | "components" | "variableCollections"> & Partial<Pick<FigmaImportResult, "textStyles">>, options: FigmaFileDocumentOptions): DesignDocument;
@@ -0,0 +1,100 @@
1
+ import { entryOf, nodeId, pageId, splitCssFontFamilies, } from "../model/index.js";
2
+ /** The size of a page's root frame, as the editor gives a new page; the canvas neither draws nor clips it. */
3
+ const PAGE_ROOT_SIZE = { width: 1920, height: 1080 };
4
+ /**
5
+ * The document a whole .fig file makes: one page for each imported page
6
+ * (`importFigmaMessage` with `roots: "pages"`), in the file's order and with
7
+ * its name and chosen variable modes, each page's layers under a root frame
8
+ * of its own, every image the layers use as an image asset, every component
9
+ * made from a SYMBOL added to the base's components (their layers are among
10
+ * `nodes`, on no page), and the file's variable collections added to the base
11
+ * theme's. The result is not checked here;
12
+ * `deserializeDocument` (or a store made from it) checks it.
13
+ */
14
+ export function figmaFileDocument(result, options) {
15
+ const { base } = options;
16
+ const nodes = {};
17
+ for (const node of result.nodes)
18
+ nodes[node.id] = node;
19
+ const pageOrder = [];
20
+ const pages = {};
21
+ result.pages.forEach((page, index) => {
22
+ const suffix = options.idSource();
23
+ const id = pageId(`page-${suffix}`);
24
+ const rootId = nodeId(`page-${suffix}-root`);
25
+ const root = {
26
+ id: rootId,
27
+ name: "Page",
28
+ visible: true,
29
+ locked: false,
30
+ opacity: 1,
31
+ type: "frame",
32
+ frame: { x: 0, y: 0, ...PAGE_ROOT_SIZE },
33
+ rotation: 0,
34
+ children: [...page.roots],
35
+ layout: { kind: "absolute" },
36
+ clipContent: false,
37
+ };
38
+ nodes[rootId] = root;
39
+ pages[id] = {
40
+ id,
41
+ name: page.name.trim() === "" ? `Page ${String(index + 1)}` : page.name,
42
+ rootId,
43
+ ...(page.variableModes === undefined ? {} : { variableModes: page.variableModes }),
44
+ };
45
+ pageOrder.push(id);
46
+ });
47
+ const assets = { ...base.assets };
48
+ for (const image of Object.values(result.assets)) {
49
+ const asset = {
50
+ id: image.id,
51
+ kind: "image",
52
+ mimeType: image.mimeType,
53
+ source: { kind: "data", data: bytesAsBase64(image.bytes) },
54
+ ...(image.width === undefined ? {} : { width: image.width }),
55
+ ...(image.height === undefined ? {} : { height: image.height }),
56
+ };
57
+ assets[image.id] = asset;
58
+ }
59
+ const components = { ...base.components };
60
+ for (const component of result.components)
61
+ components[component.id] = component;
62
+ const variableTheme = Object.keys(result.variableCollections).length === 0
63
+ ? base.theme
64
+ : { ...base.theme, variableCollections: { ...base.theme.variableCollections, ...result.variableCollections } };
65
+ // An imported text style names no font asset. It takes the document's asset of its own family, weight and slant when
66
+ // there is one; otherwise it has none, and the host binds its runs to a face when the document opens. The default
67
+ // style's fallbacks are kept, as runs without a style have them.
68
+ const fallback = entryOf(base.theme.textStyles, base.theme.defaultTextStyleId);
69
+ const fontAssets = Object.values(assets).filter((asset) => asset.kind === "font");
70
+ const withFonts = (style) => {
71
+ const asset = matchingFontAsset(fontAssets, style);
72
+ const fallbacks = fallback?.fontFallbackAssetIds?.filter((id) => id !== asset?.id);
73
+ return {
74
+ ...(asset === undefined ? {} : { fontAssetId: asset.id }),
75
+ ...(fallbacks === undefined || fallbacks.length === 0 ? {} : { fontFallbackAssetIds: fallbacks }),
76
+ ...style,
77
+ };
78
+ };
79
+ const textStyles = Object.entries(result.textStyles ?? {});
80
+ const theme = textStyles.length === 0
81
+ ? variableTheme
82
+ : { ...variableTheme, textStyles: { ...variableTheme.textStyles, ...Object.fromEntries(textStyles.map(([id, style]) => [id, withFonts(style)])) } };
83
+ return { ...base, id: options.id, title: options.title, pageOrder, pages, nodes, assets, components, theme };
84
+ }
85
+ /** The font asset of exactly `style`'s face: its first family (without case), its weight and its slant. */
86
+ function matchingFontAsset(assets, style) {
87
+ const family = (splitCssFontFamilies(style.fontFamily)[0] ?? style.fontFamily).replace(/^(["'])(.*)\1$/, "$2").toLowerCase();
88
+ const italic = style.italic === true;
89
+ return assets.find((asset) => asset.family.toLowerCase() === family && asset.weight === style.fontWeight
90
+ && (asset.style !== "normal") === italic);
91
+ }
92
+ /** Bytes as base64, for an image asset's `data` source. */
93
+ function bytesAsBase64(bytes) {
94
+ let binary = "";
95
+ const chunk = 0x8000;
96
+ for (let start = 0; start < bytes.length; start += chunk) {
97
+ binary += String.fromCharCode(...bytes.subarray(start, start + chunk));
98
+ }
99
+ return btoa(binary);
100
+ }
@@ -0,0 +1,10 @@
1
+ import { type FigmaObject } from "./fields.ts";
2
+ /**
3
+ * Returns a reader for the message's `blobs` list. A blob reference, such as
4
+ * a `Path.commandsBlob`, is the index into that list plus the message's
5
+ * `blobBaseIndex` (0 when absent, as on clipboard messages), as grida reads
6
+ * it (packages/grida-canvas-io-figma/fig-kiwi/index.ts, `getBlobBytes`). The
7
+ * reader gives `undefined` for a reference outside the list, and refuses the
8
+ * message when a blob in it has no `bytes`.
9
+ */
10
+ export declare function blobReader(message: FigmaObject): (reference: number) => Uint8Array | undefined;
@@ -0,0 +1,23 @@
1
+ import { field, isObject } from "./fields.js";
2
+ import { FigmaImportError } from "./types.js";
3
+ /**
4
+ * Returns a reader for the message's `blobs` list. A blob reference, such as
5
+ * a `Path.commandsBlob`, is the index into that list plus the message's
6
+ * `blobBaseIndex` (0 when absent, as on clipboard messages), as grida reads
7
+ * it (packages/grida-canvas-io-figma/fig-kiwi/index.ts, `getBlobBytes`). The
8
+ * reader gives `undefined` for a reference outside the list, and refuses the
9
+ * message when a blob in it has no `bytes`.
10
+ */
11
+ export function blobReader(message) {
12
+ return (reference) => {
13
+ const blobs = field(message, "blobs", "list", "message") ?? [];
14
+ const index = reference - (field(message, "blobBaseIndex", "number", "message") ?? 0);
15
+ if (!Number.isInteger(index) || index < 0 || index >= blobs.length)
16
+ return undefined;
17
+ const blob = blobs[index];
18
+ const bytes = isObject(blob) ? blob["bytes"] : undefined;
19
+ if (!(bytes instanceof Uint8Array))
20
+ throw new FigmaImportError("not-node-changes", `message blobs[${String(index)}] has no bytes`);
21
+ return bytes;
22
+ };
23
+ }
@@ -0,0 +1,24 @@
1
+ import type { SceneNode, TransformMatrix } from "../../model/index.ts";
2
+ export interface FigmaImageCrop {
3
+ readonly matrix: TransformMatrix;
4
+ readonly aspect?: number;
5
+ }
6
+ /** Marks a Taco shadow as one Figma shows behind its node. */
7
+ export declare function markShadowBehind(shadow: object): void;
8
+ /** Marks a Taco image paint as one Figma tiles at `scale`. */
9
+ export declare function markTile(paint: object, scale: number): void;
10
+ /** Carries a Figma matrix from layer UVs to image UVs to the adapter. */
11
+ export declare function markImageCrop(paint: object, crop: FigmaImageCrop): void;
12
+ export declare function imageCropOf(paint: unknown): FigmaImageCrop | undefined;
13
+ /** Reads tiling directly from a region paint, which has no layer metadata index. */
14
+ export declare function imageTileOf(paint: object): number | undefined;
15
+ /** Includes the crop when comparing paints for instance and variant overrides. */
16
+ export declare function withImageCrop(_key: string, value: unknown): unknown;
17
+ /** Moves a mark to a copy of the marked object. */
18
+ export declare function moveMark(from: object, to: object): void;
19
+ /** Adds `data` to the layer's `metadata.figma`. */
20
+ export declare function carry(node: SceneNode, data: NonNullable<SceneNode["metadata"]>): void;
21
+ /** Clears a layer's constraints where Figma does not apply them. */
22
+ export declare function clearConstraints(node: SceneNode): void;
23
+ /** Carries the marks on a converted layer's shadows and fills into its metadata. */
24
+ export declare function carryMarks(node: SceneNode): void;
@@ -0,0 +1,66 @@
1
+ import { isObject } from "./fields.js";
2
+ const marks = new WeakMap();
3
+ /** Marks a Taco shadow as one Figma shows behind its node. */
4
+ export function markShadowBehind(shadow) {
5
+ marks.set(shadow, { behind: true });
6
+ }
7
+ /** Marks a Taco image paint as one Figma tiles at `scale`. */
8
+ export function markTile(paint, scale) {
9
+ marks.set(paint, { tile: scale });
10
+ }
11
+ /** Carries a Figma matrix from layer UVs to image UVs to the adapter. */
12
+ export function markImageCrop(paint, crop) {
13
+ marks.set(paint, { crop });
14
+ }
15
+ export function imageCropOf(paint) {
16
+ return paint !== null && typeof paint === "object" ? marks.get(paint)?.crop : undefined;
17
+ }
18
+ /** Reads tiling directly from a region paint, which has no layer metadata index. */
19
+ export function imageTileOf(paint) {
20
+ return marks.get(paint)?.tile;
21
+ }
22
+ /** Includes the crop when comparing paints for instance and variant overrides. */
23
+ export function withImageCrop(_key, value) {
24
+ const crop = imageCropOf(value);
25
+ return crop ? { ...value, crop } : value;
26
+ }
27
+ /** Moves a mark to a copy of the marked object. */
28
+ export function moveMark(from, to) {
29
+ const mark = marks.get(from);
30
+ if (mark !== undefined)
31
+ marks.set(to, mark);
32
+ }
33
+ /** Adds `data` to the layer's `metadata.figma`. */
34
+ export function carry(node, data) {
35
+ const metadata = node.metadata ?? {}, figma = metadata["figma"];
36
+ const had = typeof figma === "object" && figma !== null && !Array.isArray(figma) ? figma : {};
37
+ Object.assign(node, { metadata: { ...metadata, figma: { ...had, ...data } } });
38
+ }
39
+ /** Clears a layer's constraints where Figma does not apply them. */
40
+ export function clearConstraints(node) {
41
+ if ("constraints" in node)
42
+ delete node.constraints;
43
+ if (node.type !== "group" || node.metadata === undefined)
44
+ return;
45
+ const { figma, ...rest } = node.metadata;
46
+ if (!isObject(figma) || !Object.hasOwn(figma, "groupConstraints"))
47
+ return;
48
+ const { groupConstraints: _constraints, ...remaining } = figma;
49
+ const metadata = Object.keys(remaining).length > 0 ? { ...rest, figma: remaining } : rest;
50
+ if (Object.keys(metadata).length > 0)
51
+ Object.assign(node, { metadata });
52
+ else
53
+ delete node.metadata;
54
+ }
55
+ /** Carries the marks on a converted layer's shadows and fills into its metadata. */
56
+ export function carryMarks(node) {
57
+ const style = node.style;
58
+ const behind = (style?.shadows ?? []).flatMap((shadow, index) => marks.get(shadow)?.behind === true ? [index] : []);
59
+ const tiles = (style?.fills ?? []).flatMap((paint, index) => {
60
+ const scale = imageTileOf(paint);
61
+ return scale === undefined ? [] : [[index, scale]];
62
+ });
63
+ if (behind.length === 0 && tiles.length === 0)
64
+ return;
65
+ carry(node, { ...(behind.length > 0 ? { shadowBehind: behind } : {}), ...(tiles.length > 0 ? { tiles } : {}) });
66
+ }
@@ -0,0 +1,90 @@
1
+ import { type ComponentId, type ComponentPropertyDefinition, type ComponentPropertyId, type ComponentPropertyValue, type NodeExpressions } from "../../model/index.ts";
2
+ import { type FigmaObject } from "./fields.ts";
3
+ import type { FigmaTreeNode } from "./tree.ts";
4
+ /** One Taco property made from a Figma `componentPropDefs` entry, with the Figma ids that name it. */
5
+ export interface ImportedProperty {
6
+ readonly definition: ComponentPropertyDefinition;
7
+ /** The def's own id and, for a variant, the id of the set's def it points at (`parentPropDefId`). */
8
+ readonly figmaIds: readonly string[];
9
+ }
10
+ /** Where component property import sends what it could not express; the walk counts each reason once per kind. */
11
+ export type PropertyReport = (kind: "property-left-out" | "approximated", reason: string) => void;
12
+ /**
13
+ * Figma's name kept as written, in NFC (expressions write it in backticks
14
+ * where it is not an identifier), only made unique: a name `taken` already
15
+ * has gets `_2`, `_3`, ... after it. Characters no name can hold (control,
16
+ * line break and bidirectional formatting ones) are dropped, an empty name
17
+ * becomes `property` and a name is cut to 256 characters, so the result
18
+ * depends only on the name and the names before it.
19
+ */
20
+ export declare function propertyNameOf(name: string, taken: ReadonlySet<string>): string;
21
+ /**
22
+ * The Taco properties of a SYMBOL, from its `componentPropDefs`. A variant
23
+ * (a SYMBOL inside a COMPONENT_SET) holds only `{id, parentPropDefId}`
24
+ * entries, so the name, type and default come from the set's def. BOOL
25
+ * becomes `boolean`, TEXT `text` and INSTANCE_SWAP `instance`, whose default
26
+ * component comes from `componentOf` (`undefined` when that component is
27
+ * not in the message, which leaves the property out), and whose
28
+ * `preferredValues` become `preferred` through `componentsByKey`; a key that
29
+ * names no imported component is dropped and reported. SLOT becomes `slot`,
30
+ * with its `preferredValues` mapped the same way and its `slotPropConfig`
31
+ * settings (`slotSettingsOf`); its default content is the
32
+ * frames bound to it. VARIANT defs are not properties here: each variant
33
+ * imports as its own component. A deleted def (one with neither a type nor a
34
+ * name, as Figma keeps it after the property is removed) is skipped without a
35
+ * report. Other types are reported, and so is a name
36
+ * changed to be unique or
37
+ * to drop characters no name can hold (`propertyNameOf`). Each dropped
38
+ * preferred value is reported once, not once per variant that reads the def:
39
+ * `reported` holds the defs already reported, so share one set across the
40
+ * symbols of an import.
41
+ */
42
+ export declare function propertiesOf(symbol: FigmaTreeNode, nodes: ReadonlyMap<string, FigmaTreeNode>, componentOf: (symbolId: string) => ComponentId | undefined, componentsByKey: ComponentsByKey, report: PropertyReport, reported?: WeakSet<FigmaObject>): ImportedProperty[];
43
+ /** The Taco components a Figma component key names: a COMPONENT key its SYMBOL, a STATE_GROUP key its set's variants. */
44
+ export type ComponentsByKey = (type: "COMPONENT" | "STATE_GROUP", key: string) => readonly ComponentId[];
45
+ /**
46
+ * Looks components up by the `componentKey` of a SYMBOL or a COMPONENT_SET
47
+ * (a FRAME with `isStateGroup`): a set gives its imported variants in set
48
+ * order. A key no node holds, or whose components were not imported, gives
49
+ * an empty list.
50
+ */
51
+ export declare function componentsByKeyOf(nodes: ReadonlyMap<string, FigmaTreeNode>, componentOf: (symbolId: string) => ComponentId | undefined): ComponentsByKey;
52
+ /** The property a Figma def id names, by the def's own id or the set def it points at. */
53
+ export declare function propertyByFigmaId(properties: readonly ImportedProperty[], figmaId: string): ImportedProperty | undefined;
54
+ /**
55
+ * The expressions that bind a component layer's `componentPropRefs` to the
56
+ * component's properties: VISIBLE computes `visible`, TEXT_DATA on a text
57
+ * layer `characters` and OVERRIDDEN_SYMBOL_ID on a nested instance
58
+ * `componentId`, each reading the property by name (in backticks where
59
+ * the name is not an identifier, `` `Show icon` ``). SLOT_CONTENT_ID is
60
+ * left to `slotBindingOf`. A ref on the component
61
+ * root (which cannot compute fields), to a property that was not imported,
62
+ * or of another field is reported.
63
+ */
64
+ export declare function propertyExpressions(layer: FigmaTreeNode, isRoot: boolean, properties: readonly ImportedProperty[], report: PropertyReport): NodeExpressions;
65
+ /** The ids of a symbol's VARIANT defs and of its set's, whose values need no Taco property. */
66
+ export declare function variantDefIds(symbol: FigmaTreeNode, nodes: ReadonlyMap<string, FigmaTreeNode>): Set<string>;
67
+ /** The value a `componentPropAssignments` entry gives, by kind; a field is `undefined` when the entry does not hold that kind. */
68
+ export interface AssignedValue {
69
+ readonly bool?: boolean;
70
+ readonly text?: string;
71
+ readonly textData?: FigmaObject;
72
+ /** The SYMBOL an instance swap value names. */
73
+ readonly symbolId?: string;
74
+ }
75
+ /**
76
+ * The value a `componentPropAssignments` entry gives. Older files keep it in
77
+ * `value` (`boolValue`, `textValue.characters`, `guidValue`); newer files
78
+ * leave `value` empty and keep it in `varValue.value` (`boolValue`,
79
+ * `textDataValue.characters`, `symbolIdValue.guid`), as slot values do.
80
+ */
81
+ export declare function assignedValueOf(assignment: FigmaObject, at: string): AssignedValue;
82
+ /**
83
+ * `componentPropAssignments` as Taco values of `properties`. A VARIANT value
84
+ * (`variantDefs`) and a slot value (which becomes a slot host) are skipped: the variant an instance shows is already its
85
+ * component. An instance swap value names a component from `componentOf`.
86
+ * A value naming no imported property, a swap to a component that is not
87
+ * in the message, or a swap `leftOutSwap` gives a reason to leave out (it
88
+ * would nest a component in itself), is reported and left out.
89
+ */
90
+ export declare function assignedPropertyValues(list: readonly unknown[] | undefined, properties: readonly ImportedProperty[], variantDefs: ReadonlySet<string>, componentOf: (symbolId: string) => ComponentId | undefined, leftOutSwap: (symbolId: string, property: ComponentPropertyId) => string | undefined, where: string, report: PropertyReport, textAssignment?: (id: ComponentPropertyId, data: FigmaObject, characters: string) => void): Record<string, ComponentPropertyValue>;