@moondraft/import 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (323) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +169 -0
  5. package/dist/adapter/convert.d.ts +122 -0
  6. package/dist/adapter/convert.js +2302 -0
  7. package/dist/adapter/figma.d.ts +9 -0
  8. package/dist/adapter/figma.js +30 -0
  9. package/dist/adapter/file-components.d.ts +14 -0
  10. package/dist/adapter/file-components.js +262 -0
  11. package/dist/adapter/file-resources.d.ts +29 -0
  12. package/dist/adapter/file-resources.js +562 -0
  13. package/dist/adapter/index.d.ts +6 -0
  14. package/dist/adapter/index.js +9 -0
  15. package/dist/adapter/link.d.ts +45 -0
  16. package/dist/adapter/link.js +688 -0
  17. package/dist/adapter/payload.d.ts +31 -0
  18. package/dist/adapter/payload.js +78 -0
  19. package/dist/adapter/shapes.d.ts +31 -0
  20. package/dist/adapter/shapes.js +75 -0
  21. package/dist/adapter/variables.d.ts +44 -0
  22. package/dist/adapter/variables.js +103 -0
  23. package/dist/figma-file.d.ts +38 -0
  24. package/dist/figma-file.js +97 -0
  25. package/dist/figma-worker.d.ts +11 -0
  26. package/dist/figma-worker.js +12 -0
  27. package/dist/figma.d.ts +26 -0
  28. package/dist/figma.js +96 -0
  29. package/dist/index.d.ts +28 -0
  30. package/dist/index.js +49 -0
  31. package/dist/report.d.ts +15 -0
  32. package/dist/report.js +69 -0
  33. package/dist/svg.d.ts +33 -0
  34. package/dist/svg.js +44 -0
  35. package/dist/syntax.d.ts +8 -0
  36. package/dist/syntax.js +19 -0
  37. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  38. package/dist/taco/figma-import/clipboard.js +87 -0
  39. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  40. package/dist/taco/figma-import/clipboardSize.js +107 -0
  41. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  42. package/dist/taco/figma-import/container/compression.js +174 -0
  43. package/dist/taco/figma-import/container/error.d.ts +15 -0
  44. package/dist/taco/figma-import/container/error.js +19 -0
  45. package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
  46. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  47. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  48. package/dist/taco/figma-import/container/limits.js +22 -0
  49. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  50. package/dist/taco/figma-import/container/zip.js +154 -0
  51. package/dist/taco/figma-import/figFile.d.ts +34 -0
  52. package/dist/taco/figma-import/figFile.js +91 -0
  53. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  54. package/dist/taco/figma-import/figFileDocument.js +100 -0
  55. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  56. package/dist/taco/figma-import/import/blobs.js +23 -0
  57. package/dist/taco/figma-import/import/carry.d.ts +11 -0
  58. package/dist/taco/figma-import/import/carry.js +33 -0
  59. package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
  60. package/dist/taco/figma-import/import/componentProps.js +313 -0
  61. package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
  62. package/dist/taco/figma-import/import/componentSets.js +647 -0
  63. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  64. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  65. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  66. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  67. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  68. package/dist/taco/figma-import/import/converters/frame.js +55 -0
  69. package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
  70. package/dist/taco/figma-import/import/converters/gradient.js +106 -0
  71. package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
  72. package/dist/taco/figma-import/import/converters/group.js +39 -0
  73. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  74. package/dist/taco/figma-import/import/converters/image.js +83 -0
  75. package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
  76. package/dist/taco/figma-import/import/converters/layout.js +168 -0
  77. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  78. package/dist/taco/figma-import/import/converters/line.js +40 -0
  79. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  80. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  81. package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
  82. package/dist/taco/figma-import/import/converters/section.js +35 -0
  83. package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
  84. package/dist/taco/figma-import/import/converters/shared.js +362 -0
  85. package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
  86. package/dist/taco/figma-import/import/converters/text.js +576 -0
  87. package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
  88. package/dist/taco/figma-import/import/converters/vector.js +468 -0
  89. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
  90. package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
  91. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  92. package/dist/taco/figma-import/import/fields.js +29 -0
  93. package/dist/taco/figma-import/import/instances.d.ts +163 -0
  94. package/dist/taco/figma-import/import/instances.js +1323 -0
  95. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  96. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  97. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  98. package/dist/taco/figma-import/import/slots.js +96 -0
  99. package/dist/taco/figma-import/import/styles.d.ts +46 -0
  100. package/dist/taco/figma-import/import/styles.js +42 -0
  101. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  102. package/dist/taco/figma-import/import/tree.js +194 -0
  103. package/dist/taco/figma-import/import/types.d.ts +232 -0
  104. package/dist/taco/figma-import/import/types.js +33 -0
  105. package/dist/taco/figma-import/import/variables.d.ts +136 -0
  106. package/dist/taco/figma-import/import/variables.js +762 -0
  107. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  108. package/dist/taco/figma-import/import/variantTrees.js +480 -0
  109. package/dist/taco/figma-import/import/walk.d.ts +159 -0
  110. package/dist/taco/figma-import/import/walk.js +1297 -0
  111. package/dist/taco/figma-import/index.d.ts +22 -0
  112. package/dist/taco/figma-import/index.js +13 -0
  113. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  114. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  115. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  116. package/dist/taco/figma-import/kiwi/error.js +29 -0
  117. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  118. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  119. package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
  120. package/dist/taco/figma-import/kiwi/message.js +145 -0
  121. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  122. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  123. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  124. package/dist/taco/figma-import/kiwi/types.js +18 -0
  125. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  126. package/dist/taco/model/agent-hidden-fields.js +29 -0
  127. package/dist/taco/model/asset-users.d.ts +23 -0
  128. package/dist/taco/model/asset-users.js +128 -0
  129. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  130. package/dist/taco/model/auto-layout-lines.js +70 -0
  131. package/dist/taco/model/codec.d.ts +444 -0
  132. package/dist/taco/model/codec.js +5635 -0
  133. package/dist/taco/model/color.d.ts +11 -0
  134. package/dist/taco/model/color.js +61 -0
  135. package/dist/taco/model/comment-operations.d.ts +102 -0
  136. package/dist/taco/model/comment-operations.js +227 -0
  137. package/dist/taco/model/component-evaluation.d.ts +94 -0
  138. package/dist/taco/model/component-evaluation.js +548 -0
  139. package/dist/taco/model/component-expressions.d.ts +242 -0
  140. package/dist/taco/model/component-expressions.js +1082 -0
  141. package/dist/taco/model/component-properties.d.ts +111 -0
  142. package/dist/taco/model/component-properties.js +339 -0
  143. package/dist/taco/model/component-samples.d.ts +83 -0
  144. package/dist/taco/model/component-samples.js +290 -0
  145. package/dist/taco/model/component-validity.d.ts +85 -0
  146. package/dist/taco/model/component-validity.js +397 -0
  147. package/dist/taco/model/components.d.ts +155 -0
  148. package/dist/taco/model/components.js +505 -0
  149. package/dist/taco/model/connector-ends.d.ts +20 -0
  150. package/dist/taco/model/connector-ends.js +188 -0
  151. package/dist/taco/model/connector-geometry.d.ts +74 -0
  152. package/dist/taco/model/connector-geometry.js +293 -0
  153. package/dist/taco/model/corner-radii.d.ts +26 -0
  154. package/dist/taco/model/corner-radii.js +47 -0
  155. package/dist/taco/model/derivations.d.ts +81 -0
  156. package/dist/taco/model/derivations.js +151 -0
  157. package/dist/taco/model/export-settings.d.ts +53 -0
  158. package/dist/taco/model/export-settings.js +156 -0
  159. package/dist/taco/model/expressions/ast.d.ts +153 -0
  160. package/dist/taco/model/expressions/ast.js +6 -0
  161. package/dist/taco/model/expressions/cache.d.ts +23 -0
  162. package/dist/taco/model/expressions/cache.js +43 -0
  163. package/dist/taco/model/expressions/check.d.ts +72 -0
  164. package/dist/taco/model/expressions/check.js +517 -0
  165. package/dist/taco/model/expressions/completion.d.ts +61 -0
  166. package/dist/taco/model/expressions/completion.js +421 -0
  167. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  168. package/dist/taco/model/expressions/evaluate.js +519 -0
  169. package/dist/taco/model/expressions/forms.d.ts +84 -0
  170. package/dist/taco/model/expressions/forms.js +200 -0
  171. package/dist/taco/model/expressions/index.d.ts +14 -0
  172. package/dist/taco/model/expressions/index.js +17 -0
  173. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  174. package/dist/taco/model/expressions/lexer.js +213 -0
  175. package/dist/taco/model/expressions/library.d.ts +33 -0
  176. package/dist/taco/model/expressions/library.js +117 -0
  177. package/dist/taco/model/expressions/names.d.ts +43 -0
  178. package/dist/taco/model/expressions/names.js +99 -0
  179. package/dist/taco/model/expressions/parser.d.ts +15 -0
  180. package/dist/taco/model/expressions/parser.js +438 -0
  181. package/dist/taco/model/expressions/rename.d.ts +34 -0
  182. package/dist/taco/model/expressions/rename.js +46 -0
  183. package/dist/taco/model/expressions/source.d.ts +15 -0
  184. package/dist/taco/model/expressions/source.js +161 -0
  185. package/dist/taco/model/expressions/types.d.ts +119 -0
  186. package/dist/taco/model/expressions/types.js +102 -0
  187. package/dist/taco/model/expressions/values.d.ts +97 -0
  188. package/dist/taco/model/expressions/values.js +160 -0
  189. package/dist/taco/model/geometry-limits.d.ts +16 -0
  190. package/dist/taco/model/geometry-limits.js +67 -0
  191. package/dist/taco/model/geometry.d.ts +47 -0
  192. package/dist/taco/model/geometry.js +193 -0
  193. package/dist/taco/model/group-bounds.d.ts +38 -0
  194. package/dist/taco/model/group-bounds.js +144 -0
  195. package/dist/taco/model/id-table.d.ts +37 -0
  196. package/dist/taco/model/id-table.js +80 -0
  197. package/dist/taco/model/index.d.ts +25 -0
  198. package/dist/taco/model/index.js +27 -0
  199. package/dist/taco/model/instance-resolver.d.ts +372 -0
  200. package/dist/taco/model/instance-resolver.js +1748 -0
  201. package/dist/taco/model/layer-syntax.d.ts +340 -0
  202. package/dist/taco/model/layer-syntax.js +3506 -0
  203. package/dist/taco/model/layout/constraints.d.ts +84 -0
  204. package/dist/taco/model/layout/constraints.js +445 -0
  205. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  206. package/dist/taco/model/layout/frame-layout.js +618 -0
  207. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  208. package/dist/taco/model/layout/node-sizing.js +486 -0
  209. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  210. package/dist/taco/model/legacy-connector-caps.js +70 -0
  211. package/dist/taco/model/linear-call.d.ts +48 -0
  212. package/dist/taco/model/linear-call.js +58 -0
  213. package/dist/taco/model/new-nodes.d.ts +145 -0
  214. package/dist/taco/model/new-nodes.js +277 -0
  215. package/dist/taco/model/node-builder.d.ts +449 -0
  216. package/dist/taco/model/node-builder.js +2294 -0
  217. package/dist/taco/model/node-table-watch.d.ts +36 -0
  218. package/dist/taco/model/node-table-watch.js +63 -0
  219. package/dist/taco/model/node-transform.d.ts +39 -0
  220. package/dist/taco/model/node-transform.js +79 -0
  221. package/dist/taco/model/node-users-index.d.ts +10 -0
  222. package/dist/taco/model/node-users-index.js +82 -0
  223. package/dist/taco/model/operation-references.d.ts +80 -0
  224. package/dist/taco/model/operation-references.js +399 -0
  225. package/dist/taco/model/operations.d.ts +610 -0
  226. package/dist/taco/model/operations.js +2882 -0
  227. package/dist/taco/model/overlay-table.d.ts +71 -0
  228. package/dist/taco/model/overlay-table.js +171 -0
  229. package/dist/taco/model/override-remap.d.ts +69 -0
  230. package/dist/taco/model/override-remap.js +230 -0
  231. package/dist/taco/model/parent-table.d.ts +53 -0
  232. package/dist/taco/model/parent-table.js +193 -0
  233. package/dist/taco/model/problem-collector.d.ts +27 -0
  234. package/dist/taco/model/problem-collector.js +101 -0
  235. package/dist/taco/model/sha256.d.ts +10 -0
  236. package/dist/taco/model/sha256.js +74 -0
  237. package/dist/taco/model/shape-geometry.d.ts +96 -0
  238. package/dist/taco/model/shape-geometry.js +368 -0
  239. package/dist/taco/model/shared-node-table.d.ts +97 -0
  240. package/dist/taco/model/shared-node-table.js +546 -0
  241. package/dist/taco/model/slots.d.ts +113 -0
  242. package/dist/taco/model/slots.js +281 -0
  243. package/dist/taco/model/stroke-settings.d.ts +37 -0
  244. package/dist/taco/model/stroke-settings.js +90 -0
  245. package/dist/taco/model/svg-path-import.d.ts +89 -0
  246. package/dist/taco/model/svg-path-import.js +219 -0
  247. package/dist/taco/model/text-measurement.d.ts +142 -0
  248. package/dist/taco/model/text-measurement.js +846 -0
  249. package/dist/taco/model/text-reads.d.ts +66 -0
  250. package/dist/taco/model/text-reads.js +128 -0
  251. package/dist/taco/model/text-sizer.d.ts +358 -0
  252. package/dist/taco/model/text-sizer.js +996 -0
  253. package/dist/taco/model/theme-users.d.ts +59 -0
  254. package/dist/taco/model/theme-users.js +312 -0
  255. package/dist/taco/model/thumbnail.d.ts +15 -0
  256. package/dist/taco/model/thumbnail.js +34 -0
  257. package/dist/taco/model/types.d.ts +2347 -0
  258. package/dist/taco/model/types.js +366 -0
  259. package/dist/taco/model/unknown-property.d.ts +5 -0
  260. package/dist/taco/model/unknown-property.js +7 -0
  261. package/dist/taco/model/validated-documents.d.ts +22 -0
  262. package/dist/taco/model/validated-documents.js +22 -0
  263. package/dist/taco/model/variable-modes.d.ts +61 -0
  264. package/dist/taco/model/variable-modes.js +438 -0
  265. package/dist/taco/model/variable-references.d.ts +54 -0
  266. package/dist/taco/model/variable-references.js +183 -0
  267. package/dist/taco/model/variables.d.ts +24 -0
  268. package/dist/taco/model/variables.js +151 -0
  269. package/dist/taco/model/vector-network.d.ts +34 -0
  270. package/dist/taco/model/vector-network.js +411 -0
  271. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  272. package/dist/taco/svg-import/import-svg.js +729 -0
  273. package/dist/taco/svg-import/index.d.ts +3 -0
  274. package/dist/taco/svg-import/index.js +3 -0
  275. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  276. package/dist/taco/svg-import/limits/limits.js +160 -0
  277. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  278. package/dist/taco/svg-import/paint/gradients.js +429 -0
  279. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  280. package/dist/taco/svg-import/paint/paint.js +162 -0
  281. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  282. package/dist/taco/svg-import/paths/paths.js +205 -0
  283. package/dist/taco/svg-import/public-types.d.ts +279 -0
  284. package/dist/taco/svg-import/public-types.js +56 -0
  285. package/dist/taco/svg-import/report/report.d.ts +44 -0
  286. package/dist/taco/svg-import/report/report.js +79 -0
  287. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  288. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  289. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  290. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  291. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  292. package/dist/taco/svg-import/structure/effects.js +618 -0
  293. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  294. package/dist/taco/svg-import/structure/image.js +495 -0
  295. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  296. package/dist/taco/svg-import/structure/walk.js +544 -0
  297. package/dist/taco/svg-import/style/color.d.ts +40 -0
  298. package/dist/taco/svg-import/style/color.js +275 -0
  299. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  300. package/dist/taco/svg-import/style/computed.js +1534 -0
  301. package/dist/taco/svg-import/style/css.d.ts +127 -0
  302. package/dist/taco/svg-import/style/css.js +541 -0
  303. package/dist/taco/svg-import/style/media.d.ts +25 -0
  304. package/dist/taco/svg-import/style/media.js +307 -0
  305. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  306. package/dist/taco/svg-import/style/properties.js +135 -0
  307. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  308. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  309. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  310. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  311. package/dist/taco/svg-import/text/text.d.ts +29 -0
  312. package/dist/taco/svg-import/text/text.js +545 -0
  313. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  314. package/dist/taco/svg-import/transform/matrix.js +229 -0
  315. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  316. package/dist/taco/svg-import/tree/element-data.js +155 -0
  317. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  318. package/dist/taco/svg-import/tree/elements.js +121 -0
  319. package/dist/taco/svg-import/units/length.d.ts +43 -0
  320. package/dist/taco/svg-import/units/length.js +125 -0
  321. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  322. package/dist/taco/svg-import/units/viewbox.js +77 -0
  323. package/package.json +54 -4
@@ -0,0 +1,188 @@
1
+ import { derivations, isFrozenNodeMap, MAX_DERIVATION_STEPS } from "./derivations.js";
2
+ import { entryOf } from "./id-table.js";
3
+ import { tableValues } from "./shared-node-table.js";
4
+ /**
5
+ * Which connectors have an end attached to each layer, kept for each frozen
6
+ * document, so that an edit that removes layers finds the connector ends it
7
+ * has to free without reading every node. What a connector is attached to
8
+ * depends on the connector alone, so the index of a document made by an
9
+ * edit is the index of the document it was made from with the nodes the
10
+ * edit wrote read again, as the index of the nodes that use each asset is
11
+ * kept (asset-users.ts).
12
+ */
13
+ /** How many layers of changes an index stacks before it is written out flat. */
14
+ const MAX_CONNECTOR_ENDS_DEPTH = 8;
15
+ /** Counts of the work done here, for tests that check what an edit costs. */
16
+ export const connectorEndStats = {
17
+ /** Indexes built by reading every node of a document. */
18
+ wholeDocumentScans: 0,
19
+ };
20
+ class ConnectorEnds {
21
+ #base;
22
+ /** The layers each node this layer lists is attached to; null for a node attached to none, or gone. */
23
+ #targetsById;
24
+ #idsByTarget;
25
+ #depth;
26
+ constructor(base, targetsById) {
27
+ this.#base = base;
28
+ this.#targetsById = targetsById;
29
+ const idsByTarget = new Map();
30
+ for (const [id, targets] of targetsById) {
31
+ for (const target of targets ?? []) {
32
+ const ids = idsByTarget.get(target);
33
+ if (ids === undefined)
34
+ idsByTarget.set(target, [id]);
35
+ else
36
+ ids.push(id);
37
+ }
38
+ }
39
+ this.#idsByTarget = idsByTarget;
40
+ this.#depth = base === undefined ? 0 : base.#depth + 1;
41
+ }
42
+ targetsOf(id) {
43
+ const own = this.#targetsById.get(id);
44
+ if (own !== undefined)
45
+ return own;
46
+ return this.#base === undefined ? null : this.#base.targetsOf(id);
47
+ }
48
+ attachedTo(target, into) {
49
+ for (const id of this.#idsByTarget.get(target) ?? [])
50
+ into.add(id);
51
+ if (this.#base === undefined)
52
+ return;
53
+ const below = new Set();
54
+ this.#base.attachedTo(target, below);
55
+ for (const id of below)
56
+ if (!this.#targetsById.has(id))
57
+ into.add(id);
58
+ }
59
+ with(changes) {
60
+ if (changes.size === 0)
61
+ return this;
62
+ if (this.#depth + 1 < MAX_CONNECTOR_ENDS_DEPTH)
63
+ return new ConnectorEnds(this, changes);
64
+ const flat = new Map();
65
+ this.#addTo(flat, new Set());
66
+ for (const [id, targets] of changes) {
67
+ if (targets === null)
68
+ flat.delete(id);
69
+ else
70
+ flat.set(id, targets);
71
+ }
72
+ return new ConnectorEnds(undefined, flat);
73
+ }
74
+ #addTo(flat, decided) {
75
+ for (const [id, targets] of this.#targetsById) {
76
+ if (decided.has(id))
77
+ continue;
78
+ decided.add(id);
79
+ if (targets !== null)
80
+ flat.set(id, targets);
81
+ }
82
+ if (this.#base !== undefined)
83
+ this.#base.#addTo(flat, decided);
84
+ }
85
+ }
86
+ const indexes = new WeakMap();
87
+ /** The layers `node` has a connector end attached to, or null when it is not an attached connector. */
88
+ export function connectorTargets(node) {
89
+ if (node?.type !== "connector")
90
+ return null;
91
+ const { start, end } = node;
92
+ if (start.nodeId === undefined)
93
+ return end.nodeId === undefined ? null : [end.nodeId];
94
+ return end.nodeId === undefined || end.nodeId === start.nodeId ? [start.nodeId] : [start.nodeId, end.nodeId];
95
+ }
96
+ function sameTargets(left, right) {
97
+ if (left === right)
98
+ return true;
99
+ if (left === null || right === null || left.length !== right.length)
100
+ return false;
101
+ return left.every((id, index) => id === right[index]);
102
+ }
103
+ /** `ends` with the nodes `ids` read again from `document`. */
104
+ function withNodes(ends, document, ids) {
105
+ const changes = new Map();
106
+ for (const id of ids) {
107
+ if (changes.has(id))
108
+ continue;
109
+ const targets = connectorTargets(entryOf(document.nodes, id));
110
+ if (!sameTargets(targets, ends.targetsOf(id)))
111
+ changes.set(id, targets);
112
+ }
113
+ return ends.with(changes);
114
+ }
115
+ function wholeIndex(document) {
116
+ connectorEndStats.wholeDocumentScans += 1;
117
+ const targetsById = new Map();
118
+ for (const node of tableValues(document.nodes)) {
119
+ const targets = connectorTargets(node);
120
+ if (targets !== null)
121
+ targetsById.set(node.id, targets);
122
+ }
123
+ return new ConnectorEnds(undefined, targetsById);
124
+ }
125
+ function canKeep(document) {
126
+ return Object.isFrozen(document) && isFrozenNodeMap(document.nodes);
127
+ }
128
+ /**
129
+ * The index of `document`: kept, or made from the index of the nearest
130
+ * document it was made from by edits (as far as `MAX_DERIVATION_STEPS`
131
+ * steps reach), or else read from every node of the earliest of them. Each
132
+ * document on the way keeps its own index. Only a frozen document keeps one.
133
+ */
134
+ function connectorEndsOf(document) {
135
+ const kept = indexes.get(document);
136
+ if (kept !== undefined)
137
+ return kept;
138
+ if (!canKeep(document))
139
+ return wholeIndex(document);
140
+ const chain = [document];
141
+ let ends;
142
+ for (let step = 0; step < MAX_DERIVATION_STEPS; step += 1) {
143
+ const base = derivations.get(chain[chain.length - 1])?.base.deref();
144
+ if (base === undefined || !canKeep(base))
145
+ break;
146
+ ends = indexes.get(base);
147
+ if (ends !== undefined)
148
+ break;
149
+ chain.push(base);
150
+ }
151
+ if (ends === undefined) {
152
+ const earliest = chain.pop();
153
+ ends = wholeIndex(earliest);
154
+ indexes.set(earliest, ends);
155
+ }
156
+ for (let index = chain.length - 1; index >= 0; index -= 1) {
157
+ const derived = chain[index];
158
+ ends = withNodes(ends, derived, derivations.get(derived).changedNodeIds);
159
+ indexes.set(derived, ends);
160
+ }
161
+ return ends;
162
+ }
163
+ /**
164
+ * The IDs of the connectors in `document` with an end attached to one of
165
+ * `targetIds`. A frozen document keeps an index for this, so after the first
166
+ * question the answer costs in proportion to the connectors found and the
167
+ * nodes each edit since wrote, not to the document.
168
+ */
169
+ export function connectorsAttachedTo(document, targetIds) {
170
+ const ends = connectorEndsOf(document);
171
+ const found = new Set();
172
+ for (const target of targetIds)
173
+ ends.attachedTo(target, found);
174
+ return found;
175
+ }
176
+ /**
177
+ * Keeps the index of `derived`, made by an edit of `base`, when `base` has
178
+ * one: so a run of edits keeps it, reading only the nodes each edit wrote.
179
+ */
180
+ export function keepConnectorEnds(base, derived) {
181
+ const kept = indexes.get(base);
182
+ if (kept === undefined || !canKeep(derived) || indexes.has(derived))
183
+ return;
184
+ const derivation = derivations.get(derived);
185
+ if (derivation === undefined || derivation.base.deref() !== base)
186
+ return;
187
+ indexes.set(derived, withNodes(kept, derived, derivation.changedNodeIds));
188
+ }
@@ -0,0 +1,74 @@
1
+ import { type ShapePoint } from "./shape-geometry.ts";
2
+ import type { ConnectorAnchor, ConnectorEndpoint, ConnectorSide, SceneNode } from "./types.ts";
3
+ /**
4
+ * Where connector ends go on a layer's outline: the key points the Connector
5
+ * tool offers as dots, the place of a pinned end, and the place of an
6
+ * automatic end. The page compiler and the editor share these so the dot a
7
+ * connector is dropped on is where the page compiler draws its end.
8
+ *
9
+ * Every position is in the layer's own coordinates, with (0, 0) at the
10
+ * top-left of its frame. A normal is the outline's outward direction at a
11
+ * point, as a unit vector (y points down).
12
+ */
13
+ /** How an end is pinned to a key point: by a side (Figma's magnet) or an anchor. */
14
+ export type ConnectorKeyPlace = {
15
+ readonly side: ConnectorSide;
16
+ readonly anchor?: undefined;
17
+ } | {
18
+ readonly anchor: ConnectorAnchor;
19
+ readonly side?: undefined;
20
+ };
21
+ /** A point on a layer's outline that a connector end can be pinned to. */
22
+ export interface ConnectorKeyPoint extends ShapePoint {
23
+ readonly place: ConnectorKeyPlace;
24
+ /** The outline's outward direction at the point. */
25
+ readonly normal: ShapePoint;
26
+ }
27
+ /** A place on a layer an end is drawn at, with the outward direction when it has one. */
28
+ export interface ConnectorOutlinePlace extends ShapePoint {
29
+ readonly normal?: ShapePoint;
30
+ }
31
+ /**
32
+ * A polygon above this many points shows its corners only: with more, the
33
+ * dots in the middle of its edges crowd the corners.
34
+ */
35
+ export declare const MAX_CONNECTOR_EDGE_KEY_POINT_COUNT = 8;
36
+ /** The outward direction of each side of a layer's box. */
37
+ export declare const CONNECTOR_SIDE_NORMALS: Readonly<Record<ConnectorSide, ShapePoint>>;
38
+ /**
39
+ * The key points of a layer, in the order the Connector tool shows them:
40
+ *
41
+ * - A rectangle, frame, text, image or any other box: the middle of each
42
+ * side, as FigJam's `TOP`, `RIGHT`, `BOTTOM` and `LEFT` magnets.
43
+ * - An ellipse: its top, right, bottom and left points (on the curve, which
44
+ * touches its box there), keeping only those an arc draws, and the two
45
+ * ends of an arc that stops short of a full turn.
46
+ * - A polygon: every corner, and the middle of every edge when it has at
47
+ * most `MAX_CONNECTOR_EDGE_KEY_POINT_COUNT` points.
48
+ * - A star: its outer tips. Its inner corners point into the star, so a
49
+ * connector there would cross it.
50
+ */
51
+ export declare function connectorKeyPoints(node: SceneNode): readonly ConnectorKeyPoint[];
52
+ /**
53
+ * The place of an anchor on a layer, or undefined when the layer has no such
54
+ * point: an index past its corners, or an anchor on a box or whole ellipse.
55
+ */
56
+ export declare function connectorAnchorPoint(node: SceneNode, anchor: ConnectorAnchor): ConnectorOutlinePlace | undefined;
57
+ /**
58
+ * Where on its outline a layer meets the line from its centre toward
59
+ * `toward`: the box's edge, the ellipse's curve (a whole ellipse, even for
60
+ * an arc) or the polygon's or star's outermost crossing. The layer's centre
61
+ * when `toward` is the centre.
62
+ */
63
+ export declare function connectorOutlinePointToward(node: SceneNode, toward: ShapePoint): ShapePoint;
64
+ /**
65
+ * The place of an attached end pinned by `side`, `point` or `anchor` on
66
+ * `node`, or undefined for an automatic end (including an anchor the layer
67
+ * does not have). A side on a box or ellipse is the middle of that side of
68
+ * the box; on a polygon or star it is where the outline meets the line from
69
+ * the centre (the average of its corners) straight toward that side. A side or anchor has the outward normal; a
70
+ * fixed point has none.
71
+ */
72
+ export declare function connectorPinnedPlace(node: SceneNode, endpoint: ConnectorEndpoint): ConnectorOutlinePlace | undefined;
73
+ /** Two places are the same pin: the same side, or the same kind and index of anchor. */
74
+ export declare function sameConnectorKeyPlace(a: ConnectorKeyPlace, b: ConnectorKeyPlace): boolean;
@@ -0,0 +1,293 @@
1
+ import { ellipseArcSweep, polygonPoints, starPoints } from "./shape-geometry.js";
2
+ /**
3
+ * A polygon above this many points shows its corners only: with more, the
4
+ * dots in the middle of its edges crowd the corners.
5
+ */
6
+ export const MAX_CONNECTOR_EDGE_KEY_POINT_COUNT = 8;
7
+ const SIDES = ["top", "right", "bottom", "left"];
8
+ /** The outward direction of each side of a layer's box. */
9
+ export const CONNECTOR_SIDE_NORMALS = {
10
+ top: { x: 0, y: -1 },
11
+ right: { x: 1, y: 0 },
12
+ bottom: { x: 0, y: 1 },
13
+ left: { x: -1, y: 0 },
14
+ };
15
+ function outlineOf(node) {
16
+ const { width, height } = node.frame;
17
+ if (node.type === "shape") {
18
+ if (node.shape === "ellipse") {
19
+ return { kind: "ellipse", width, height, ...(node.arc === undefined ? {} : { arc: node.arc }) };
20
+ }
21
+ if (node.shape === "polygon") {
22
+ return { kind: "polygon", width, height, points: polygonPoints(node.pointCount, width, height) };
23
+ }
24
+ if (node.shape === "star") {
25
+ return {
26
+ kind: "star",
27
+ width,
28
+ height,
29
+ points: starPoints(node.pointCount, node.innerRadius, width, height),
30
+ };
31
+ }
32
+ }
33
+ return { kind: "box", width, height };
34
+ }
35
+ function unit(x, y) {
36
+ const length = Math.hypot(x, y);
37
+ return length > 0 && Number.isFinite(length) ? { x: x / length, y: y / length } : undefined;
38
+ }
39
+ /** The middle of the box side, with the side's direction as its normal. */
40
+ function boxSidePoint(width, height, side) {
41
+ const normal = CONNECTOR_SIDE_NORMALS[side];
42
+ return { x: width / 2 + normal.x * width / 2, y: height / 2 + normal.y * height / 2, normal };
43
+ }
44
+ /** The point of an ellipse filling a `width` by `height` box at a parametric angle in degrees. */
45
+ function ellipsePoint(width, height, degrees) {
46
+ const radians = degrees * Math.PI / 180;
47
+ const cos = Math.cos(radians);
48
+ const sin = Math.sin(radians);
49
+ const normal = unit(cos * height, sin * width) ?? { x: cos, y: sin };
50
+ return { x: width / 2 + cos * width / 2, y: height / 2 + sin * height / 2, normal };
51
+ }
52
+ /** True when an arc's sweep passes through the parametric angle `degrees`. */
53
+ function arcCovers(arc, degrees) {
54
+ const sweep = ellipseArcSweep(arc);
55
+ if (Math.abs(sweep) >= 360)
56
+ return true;
57
+ const turn = (value) => ((value % 360) + 360) % 360;
58
+ const along = sweep >= 0 ? turn(degrees - arc.startingAngle) : turn(arc.startingAngle - degrees);
59
+ return along <= Math.abs(sweep) + 1e-9;
60
+ }
61
+ /** True when the arc stops short of a full turn, so it has two ends. */
62
+ function arcHasEnds(arc) {
63
+ return arc !== undefined && Math.abs(ellipseArcSweep(arc)) < 360;
64
+ }
65
+ /** Side of an ellipse to the parametric angle of its point there. */
66
+ const SIDE_ANGLES = { top: 270, right: 0, bottom: 90, left: 180 };
67
+ /** Twice the signed area of a closed outline; positive when it runs clockwise on screen. */
68
+ function signedArea(points) {
69
+ let area = 0;
70
+ for (let index = 0; index < points.length; index += 1) {
71
+ const point = points[index];
72
+ const next = points[(index + 1) % points.length];
73
+ area += point.x * next.y - next.x * point.y;
74
+ }
75
+ return area;
76
+ }
77
+ /** The outward normal of the edge from corner `index` to the next. */
78
+ function edgeNormal(points, index, clockwise) {
79
+ const from = points[index];
80
+ const to = points[(index + 1) % points.length];
81
+ const dx = to.x - from.x;
82
+ const dy = to.y - from.y;
83
+ return clockwise ? unit(dy, -dx) : unit(-dy, dx);
84
+ }
85
+ function centroid(points) {
86
+ let x = 0;
87
+ let y = 0;
88
+ for (const point of points) {
89
+ x += point.x;
90
+ y += point.y;
91
+ }
92
+ return { x: x / points.length, y: y / points.length };
93
+ }
94
+ /**
95
+ * The corner or edge middle of a polygon or star with its outward direction:
96
+ * an edge's normal, or at a corner the direction halfway between its two
97
+ * edges' normals. A shape squashed flat falls back to the direction from its
98
+ * centre.
99
+ */
100
+ function polygonAnchorPoint(points, anchor) {
101
+ const count = points.length;
102
+ if (!Number.isInteger(anchor.index) || anchor.index < 0 || anchor.index >= count)
103
+ return undefined;
104
+ const clockwise = signedArea(points) >= 0;
105
+ const center = centroid(points);
106
+ const point = anchor.kind === "vertex"
107
+ ? points[anchor.index]
108
+ : {
109
+ x: (points[anchor.index].x + points[(anchor.index + 1) % count].x) / 2,
110
+ y: (points[anchor.index].y + points[(anchor.index + 1) % count].y) / 2,
111
+ };
112
+ let normal;
113
+ if (anchor.kind === "edge") {
114
+ normal = edgeNormal(points, anchor.index, clockwise);
115
+ }
116
+ else {
117
+ const before = edgeNormal(points, (anchor.index - 1 + count) % count, clockwise);
118
+ const after = edgeNormal(points, anchor.index, clockwise);
119
+ if (before !== undefined && after !== undefined)
120
+ normal = unit(before.x + after.x, before.y + after.y);
121
+ normal ??= before ?? after;
122
+ }
123
+ normal ??= unit(point.x - center.x, point.y - center.y);
124
+ return { x: point.x, y: point.y, ...(normal === undefined ? {} : { normal }) };
125
+ }
126
+ /**
127
+ * Where a ray from `origin` along `direction` last leaves a closed outline,
128
+ * or undefined when it never crosses it. The last crossing is the outermost,
129
+ * so a ray through a star's notch lands on the tip beyond it, not inside.
130
+ */
131
+ function lastOutlineCrossing(points, origin, direction) {
132
+ let farthest;
133
+ for (let index = 0; index < points.length; index += 1) {
134
+ const from = points[index];
135
+ const to = points[(index + 1) % points.length];
136
+ const edgeX = to.x - from.x;
137
+ const edgeY = to.y - from.y;
138
+ const denominator = direction.x * edgeY - direction.y * edgeX;
139
+ if (Math.abs(denominator) < 1e-12)
140
+ continue;
141
+ const offsetX = from.x - origin.x;
142
+ const offsetY = from.y - origin.y;
143
+ const along = (offsetX * edgeY - offsetY * edgeX) / denominator;
144
+ const onEdge = (offsetX * direction.y - offsetY * direction.x) / denominator;
145
+ if (along >= 0 && onEdge >= -1e-9 && onEdge <= 1 + 1e-9 && (farthest === undefined || along > farthest)) {
146
+ farthest = along;
147
+ }
148
+ }
149
+ return farthest === undefined
150
+ ? undefined
151
+ : { x: origin.x + direction.x * farthest, y: origin.y + direction.y * farthest };
152
+ }
153
+ /**
154
+ * The key points of a layer, in the order the Connector tool shows them:
155
+ *
156
+ * - A rectangle, frame, text, image or any other box: the middle of each
157
+ * side, as FigJam's `TOP`, `RIGHT`, `BOTTOM` and `LEFT` magnets.
158
+ * - An ellipse: its top, right, bottom and left points (on the curve, which
159
+ * touches its box there), keeping only those an arc draws, and the two
160
+ * ends of an arc that stops short of a full turn.
161
+ * - A polygon: every corner, and the middle of every edge when it has at
162
+ * most `MAX_CONNECTOR_EDGE_KEY_POINT_COUNT` points.
163
+ * - A star: its outer tips. Its inner corners point into the star, so a
164
+ * connector there would cross it.
165
+ */
166
+ export function connectorKeyPoints(node) {
167
+ if (!("frame" in node) || node.frame === undefined)
168
+ return [];
169
+ const outline = outlineOf(node);
170
+ const { width, height } = outline;
171
+ if (outline.kind === "box") {
172
+ return SIDES.map((side) => ({ ...boxSidePoint(width, height, side), place: { side } }));
173
+ }
174
+ if (outline.kind === "ellipse") {
175
+ const points = [];
176
+ for (const side of SIDES) {
177
+ if (outline.arc !== undefined && !arcCovers(outline.arc, SIDE_ANGLES[side]))
178
+ continue;
179
+ const point = ellipsePoint(width, height, SIDE_ANGLES[side]);
180
+ points.push({ x: point.x, y: point.y, normal: point.normal, place: { side } });
181
+ }
182
+ if (arcHasEnds(outline.arc)) {
183
+ for (const index of [0, 1]) {
184
+ const point = connectorAnchorPoint(node, { kind: "vertex", index });
185
+ // An end on a top, right, bottom or left point already has its dot.
186
+ const shown = points.some((other) => Math.hypot(other.x - (point?.x ?? NaN), other.y - (point?.y ?? NaN)) < 1e-6);
187
+ if (point?.normal !== undefined && !shown) {
188
+ points.push({ x: point.x, y: point.y, normal: point.normal, place: { anchor: { kind: "vertex", index } } });
189
+ }
190
+ }
191
+ }
192
+ return points;
193
+ }
194
+ const anchors = [];
195
+ if (outline.kind === "star") {
196
+ for (let index = 0; index < outline.points.length; index += 2)
197
+ anchors.push({ kind: "vertex", index });
198
+ }
199
+ else {
200
+ const withEdges = outline.points.length <= MAX_CONNECTOR_EDGE_KEY_POINT_COUNT;
201
+ for (let index = 0; index < outline.points.length; index += 1) {
202
+ anchors.push({ kind: "vertex", index });
203
+ if (withEdges)
204
+ anchors.push({ kind: "edge", index });
205
+ }
206
+ }
207
+ const points = [];
208
+ for (const anchor of anchors) {
209
+ const point = polygonAnchorPoint(outline.points, anchor);
210
+ if (point?.normal === undefined)
211
+ continue;
212
+ points.push({ x: point.x, y: point.y, normal: point.normal, place: { anchor } });
213
+ }
214
+ return points;
215
+ }
216
+ /**
217
+ * The place of an anchor on a layer, or undefined when the layer has no such
218
+ * point: an index past its corners, or an anchor on a box or whole ellipse.
219
+ */
220
+ export function connectorAnchorPoint(node, anchor) {
221
+ if (!("frame" in node) || node.frame === undefined)
222
+ return undefined;
223
+ const outline = outlineOf(node);
224
+ if (outline.kind === "polygon" || outline.kind === "star")
225
+ return polygonAnchorPoint(outline.points, anchor);
226
+ if (outline.kind === "ellipse" && arcHasEnds(outline.arc) && anchor.kind === "vertex" &&
227
+ (anchor.index === 0 || anchor.index === 1)) {
228
+ return ellipsePoint(outline.width, outline.height, anchor.index === 0 ? outline.arc.startingAngle : outline.arc.endingAngle);
229
+ }
230
+ return undefined;
231
+ }
232
+ /**
233
+ * Where on its outline a layer meets the line from its centre toward
234
+ * `toward`: the box's edge, the ellipse's curve (a whole ellipse, even for
235
+ * an arc) or the polygon's or star's outermost crossing. The layer's centre
236
+ * when `toward` is the centre.
237
+ */
238
+ export function connectorOutlinePointToward(node, toward) {
239
+ if (!("frame" in node) || node.frame === undefined)
240
+ return toward;
241
+ const outline = outlineOf(node);
242
+ const { width, height } = outline;
243
+ const center = outline.kind === "polygon" || outline.kind === "star"
244
+ ? centroid(outline.points)
245
+ : { x: width / 2, y: height / 2 };
246
+ const dx = toward.x - center.x;
247
+ const dy = toward.y - center.y;
248
+ if (dx === 0 && dy === 0)
249
+ return center;
250
+ if (outline.kind === "polygon" || outline.kind === "star") {
251
+ return lastOutlineCrossing(outline.points, center, { x: dx, y: dy }) ?? center;
252
+ }
253
+ const halfWidth = width / 2;
254
+ const halfHeight = height / 2;
255
+ if (!(halfWidth > 0) || !(halfHeight > 0))
256
+ return center;
257
+ // Scale the ray so it just reaches the box edge, or the ellipse's curve.
258
+ const scale = outline.kind === "ellipse"
259
+ ? 1 / Math.hypot(dx / halfWidth, dy / halfHeight)
260
+ : Math.min(dx === 0 ? Infinity : halfWidth / Math.abs(dx), dy === 0 ? Infinity : halfHeight / Math.abs(dy));
261
+ return { x: center.x + dx * scale, y: center.y + dy * scale };
262
+ }
263
+ /**
264
+ * The place of an attached end pinned by `side`, `point` or `anchor` on
265
+ * `node`, or undefined for an automatic end (including an anchor the layer
266
+ * does not have). A side on a box or ellipse is the middle of that side of
267
+ * the box; on a polygon or star it is where the outline meets the line from
268
+ * the centre (the average of its corners) straight toward that side. A side or anchor has the outward normal; a
269
+ * fixed point has none.
270
+ */
271
+ export function connectorPinnedPlace(node, endpoint) {
272
+ if (!("frame" in node) || node.frame === undefined)
273
+ return undefined;
274
+ const { width, height } = node.frame;
275
+ if (endpoint.point !== undefined)
276
+ return { x: width * endpoint.point.x, y: height * endpoint.point.y };
277
+ if (endpoint.anchor !== undefined)
278
+ return connectorAnchorPoint(node, endpoint.anchor);
279
+ if (endpoint.side === undefined)
280
+ return undefined;
281
+ const outline = outlineOf(node);
282
+ const side = boxSidePoint(width, height, endpoint.side);
283
+ if (outline.kind !== "polygon" && outline.kind !== "star")
284
+ return side;
285
+ const point = lastOutlineCrossing(outline.points, centroid(outline.points), side.normal) ?? side;
286
+ return { x: point.x, y: point.y, normal: side.normal };
287
+ }
288
+ /** Two places are the same pin: the same side, or the same kind and index of anchor. */
289
+ export function sameConnectorKeyPlace(a, b) {
290
+ if (a.side !== undefined || b.side !== undefined)
291
+ return a.side === b.side;
292
+ return a.anchor.kind === b.anchor.kind && a.anchor.index === b.anchor.index;
293
+ }
@@ -0,0 +1,26 @@
1
+ import type { CornerRadii, Stroke, StrokeSides, VisualStyle } from "./types.ts";
2
+ /** The four corners in Figma's panel order: top left, top right, bottom right, bottom left. */
3
+ export declare const CORNER_KEYS: readonly ["topLeft", "topRight", "bottomRight", "bottomLeft"];
4
+ /** One corner of a rectangle or frame, as named in `CornerRadii`. */
5
+ export type CornerKey = (typeof CORNER_KEYS)[number];
6
+ /** Four equal radii. */
7
+ export declare function uniformCornerRadii(radius: number): CornerRadii;
8
+ /**
9
+ * The radii a rectangle or frame paints with: `cornerRadii` when present,
10
+ * otherwise the uniform `cornerRadius` (zero when absent) on every corner.
11
+ */
12
+ export declare function styleCornerRadii(style: Pick<VisualStyle, "cornerRadius" | "cornerRadii"> | undefined): CornerRadii;
13
+ /** True when all four corners have the same radius. */
14
+ export declare function cornerRadiiAreUniform(radii: CornerRadii): boolean;
15
+ /** True when any corner is rounded. */
16
+ export declare function cornerRadiiAreRounded(radii: CornerRadii): boolean;
17
+ /** Multiplies every radius by `factor`, as the scale tool does. */
18
+ export declare function scaleCornerRadii(radii: CornerRadii, factor: number): CornerRadii;
19
+ /** The four stroke sides in the order the wire and the panel use. */
20
+ export declare const STROKE_SIDE_KEYS: readonly ["top", "right", "bottom", "left"];
21
+ /** One side of a stroke, as named in `StrokeSides`. */
22
+ export type StrokeSideKey = (typeof STROKE_SIDE_KEYS)[number];
23
+ /** The weight of each side a stroke paints with: `sides`, or `width` on all four. */
24
+ export declare function strokeSideWeights(stroke: Pick<Stroke, "width" | "sides">): StrokeSides;
25
+ /** The largest of the four side weights. */
26
+ export declare function widestStrokeSide(sides: StrokeSides): number;
@@ -0,0 +1,47 @@
1
+ /** The four corners in Figma's panel order: top left, top right, bottom right, bottom left. */
2
+ export const CORNER_KEYS = ["topLeft", "topRight", "bottomRight", "bottomLeft"];
3
+ /** Four equal radii. */
4
+ export function uniformCornerRadii(radius) {
5
+ return { topLeft: radius, topRight: radius, bottomRight: radius, bottomLeft: radius };
6
+ }
7
+ /**
8
+ * The radii a rectangle or frame paints with: `cornerRadii` when present,
9
+ * otherwise the uniform `cornerRadius` (zero when absent) on every corner.
10
+ */
11
+ export function styleCornerRadii(style) {
12
+ return style?.cornerRadii ?? uniformCornerRadii(style?.cornerRadius ?? 0);
13
+ }
14
+ /** True when all four corners have the same radius. */
15
+ export function cornerRadiiAreUniform(radii) {
16
+ return radii.topRight === radii.topLeft &&
17
+ radii.bottomRight === radii.topLeft &&
18
+ radii.bottomLeft === radii.topLeft;
19
+ }
20
+ /** True when any corner is rounded. */
21
+ export function cornerRadiiAreRounded(radii) {
22
+ return CORNER_KEYS.some((key) => radii[key] > 0);
23
+ }
24
+ /** Multiplies every radius by `factor`, as the scale tool does. */
25
+ export function scaleCornerRadii(radii, factor) {
26
+ return {
27
+ topLeft: radii.topLeft * factor,
28
+ topRight: radii.topRight * factor,
29
+ bottomRight: radii.bottomRight * factor,
30
+ bottomLeft: radii.bottomLeft * factor,
31
+ };
32
+ }
33
+ /** The four stroke sides in the order the wire and the panel use. */
34
+ export const STROKE_SIDE_KEYS = ["top", "right", "bottom", "left"];
35
+ /** The weight of each side a stroke paints with: `sides`, or `width` on all four. */
36
+ export function strokeSideWeights(stroke) {
37
+ return stroke.sides ?? {
38
+ top: stroke.width,
39
+ right: stroke.width,
40
+ bottom: stroke.width,
41
+ left: stroke.width,
42
+ };
43
+ }
44
+ /** The largest of the four side weights. */
45
+ export function widestStrokeSide(sides) {
46
+ return Math.max(sides.top, sides.right, sides.bottom, sides.left);
47
+ }