@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.3

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 (390) hide show
  1. package/dist/arrow-caps.d.ts.map +1 -1
  2. package/dist/arrow-caps.js +2 -5
  3. package/dist/arrow-caps.js.map +1 -1
  4. package/dist/asset-keys.d.ts +11 -1
  5. package/dist/asset-keys.d.ts.map +1 -1
  6. package/dist/asset-keys.js +16 -1
  7. package/dist/asset-keys.js.map +1 -1
  8. package/dist/behaviours/index.d.ts +2 -2
  9. package/dist/behaviours/index.js +2 -2
  10. package/dist/behaviours/kinds.d.ts +11 -5
  11. package/dist/behaviours/kinds.d.ts.map +1 -1
  12. package/dist/behaviours/kinds.js +76 -75
  13. package/dist/behaviours/kinds.js.map +1 -1
  14. package/dist/behaviours/layers.d.ts.map +1 -1
  15. package/dist/behaviours/layers.js +3 -2
  16. package/dist/behaviours/layers.js.map +1 -1
  17. package/dist/behaviours/model.d.ts +7 -4
  18. package/dist/behaviours/model.d.ts.map +1 -1
  19. package/dist/behaviours/model.js +16 -9
  20. package/dist/behaviours/model.js.map +1 -1
  21. package/dist/behaviours/schema.d.ts +118 -4
  22. package/dist/behaviours/schema.d.ts.map +1 -1
  23. package/dist/behaviours/schema.js +18 -3
  24. package/dist/behaviours/schema.js.map +1 -1
  25. package/dist/behaviours/spec.d.ts.map +1 -1
  26. package/dist/behaviours/spec.js +3 -2
  27. package/dist/behaviours/spec.js.map +1 -1
  28. package/dist/bindings.js +9 -1
  29. package/dist/bindings.js.map +1 -1
  30. package/dist/checkpoint.d.ts +31 -2
  31. package/dist/checkpoint.d.ts.map +1 -1
  32. package/dist/checkpoint.js +211 -10
  33. package/dist/checkpoint.js.map +1 -1
  34. package/dist/components/index.d.ts +3 -3
  35. package/dist/components/index.js +3 -3
  36. package/dist/components/properties.d.ts +18 -1
  37. package/dist/components/properties.d.ts.map +1 -1
  38. package/dist/components/properties.js +66 -3
  39. package/dist/components/properties.js.map +1 -1
  40. package/dist/components/variants.d.ts +16 -10
  41. package/dist/components/variants.d.ts.map +1 -1
  42. package/dist/components/variants.js +39 -13
  43. package/dist/components/variants.js.map +1 -1
  44. package/dist/connectors/hit.d.ts.map +1 -1
  45. package/dist/connectors/hit.js +1 -7
  46. package/dist/connectors/hit.js.map +1 -1
  47. package/dist/connectors/pieces.d.ts.map +1 -1
  48. package/dist/connectors/pieces.js +2 -5
  49. package/dist/connectors/pieces.js.map +1 -1
  50. package/dist/constants.d.ts +6 -1
  51. package/dist/constants.d.ts.map +1 -1
  52. package/dist/constants.js +17 -1
  53. package/dist/constants.js.map +1 -1
  54. package/dist/constraints.d.ts +25 -0
  55. package/dist/constraints.d.ts.map +1 -0
  56. package/dist/constraints.js +51 -0
  57. package/dist/constraints.js.map +1 -0
  58. package/dist/coordinate.d.ts +64 -1
  59. package/dist/coordinate.d.ts.map +1 -1
  60. package/dist/coordinate.js +217 -1
  61. package/dist/coordinate.js.map +1 -1
  62. package/dist/copy.d.ts.map +1 -1
  63. package/dist/copy.js +17 -16
  64. package/dist/copy.js.map +1 -1
  65. package/dist/css/blend.d.ts +11 -0
  66. package/dist/css/blend.d.ts.map +1 -0
  67. package/dist/css/blend.js +30 -0
  68. package/dist/css/blend.js.map +1 -0
  69. package/dist/css/index.d.ts +4 -2
  70. package/dist/css/index.js +4 -2
  71. package/dist/css/text.d.ts +28 -1
  72. package/dist/css/text.d.ts.map +1 -1
  73. package/dist/css/text.js +84 -17
  74. package/dist/css/text.js.map +1 -1
  75. package/dist/css/transform.d.ts +34 -0
  76. package/dist/css/transform.d.ts.map +1 -0
  77. package/dist/css/transform.js +320 -0
  78. package/dist/css/transform.js.map +1 -0
  79. package/dist/css/values.d.ts.map +1 -1
  80. package/dist/css/values.js +11 -1
  81. package/dist/css/values.js.map +1 -1
  82. package/dist/cubic.d.ts +7 -0
  83. package/dist/cubic.d.ts.map +1 -0
  84. package/dist/cubic.js +17 -0
  85. package/dist/cubic.js.map +1 -0
  86. package/dist/export-format.d.ts +14 -1
  87. package/dist/export-format.d.ts.map +1 -1
  88. package/dist/export-format.js +26 -1
  89. package/dist/export-format.js.map +1 -1
  90. package/dist/fields/text.d.ts +2 -2
  91. package/dist/fields/text.d.ts.map +1 -1
  92. package/dist/fields/text.js +2 -1
  93. package/dist/fields/text.js.map +1 -1
  94. package/dist/figjam/index.d.ts +2 -2
  95. package/dist/figjam/index.js +2 -2
  96. package/dist/figjam/palette.d.ts +13 -2
  97. package/dist/figjam/palette.d.ts.map +1 -1
  98. package/dist/figjam/palette.js +30 -2
  99. package/dist/figjam/palette.js.map +1 -1
  100. package/dist/generated/index.d.ts +2 -2
  101. package/dist/generated/index.js +2 -2
  102. package/dist/generated/shape-with-text.d.ts +7 -2
  103. package/dist/generated/shape-with-text.d.ts.map +1 -1
  104. package/dist/generated/shape-with-text.js +20 -3
  105. package/dist/generated/shape-with-text.js.map +1 -1
  106. package/dist/geometry.d.ts +37 -46
  107. package/dist/geometry.d.ts.map +1 -1
  108. package/dist/geometry.js +80 -137
  109. package/dist/geometry.js.map +1 -1
  110. package/dist/graph.d.ts +49 -12
  111. package/dist/graph.d.ts.map +1 -1
  112. package/dist/graph.js +125 -27
  113. package/dist/graph.js.map +1 -1
  114. package/dist/group-bounds.d.ts +14 -4
  115. package/dist/group-bounds.d.ts.map +1 -1
  116. package/dist/group-bounds.js +76 -11
  117. package/dist/group-bounds.js.map +1 -1
  118. package/dist/guides.d.ts +3 -1
  119. package/dist/guides.d.ts.map +1 -1
  120. package/dist/guides.js +5 -1
  121. package/dist/guides.js.map +1 -1
  122. package/dist/hit-test.d.ts +5 -0
  123. package/dist/hit-test.d.ts.map +1 -1
  124. package/dist/hit-test.js +57 -38
  125. package/dist/hit-test.js.map +1 -1
  126. package/dist/images.d.ts +27 -3
  127. package/dist/images.d.ts.map +1 -1
  128. package/dist/images.js +57 -3
  129. package/dist/images.js.map +1 -1
  130. package/dist/index.d.ts +2 -2
  131. package/dist/instances/fields.d.ts +2 -2
  132. package/dist/instances/fields.d.ts.map +1 -1
  133. package/dist/instances/fields.js +8 -0
  134. package/dist/instances/fields.js.map +1 -1
  135. package/dist/instances/index.d.ts +1 -3
  136. package/dist/instances/index.d.ts.map +1 -1
  137. package/dist/instances/index.js +39 -30
  138. package/dist/instances/index.js.map +1 -1
  139. package/dist/instances/kept-overrides.js +204 -0
  140. package/dist/instances/kept-overrides.js.map +1 -0
  141. package/dist/instances/main-component.d.ts +3 -1
  142. package/dist/instances/main-component.d.ts.map +1 -1
  143. package/dist/instances/main-component.js +10 -2
  144. package/dist/instances/main-component.js.map +1 -1
  145. package/dist/instances/overrides.d.ts +4 -1
  146. package/dist/instances/overrides.d.ts.map +1 -1
  147. package/dist/instances/overrides.js +18 -2
  148. package/dist/instances/overrides.js.map +1 -1
  149. package/dist/instances/placement.js +55 -12
  150. package/dist/instances/placement.js.map +1 -1
  151. package/dist/instances/swap.js +291 -0
  152. package/dist/instances/swap.js.map +1 -0
  153. package/dist/instances/sync.js +112 -39
  154. package/dist/instances/sync.js.map +1 -1
  155. package/dist/node-defaults.d.ts.map +1 -1
  156. package/dist/node-defaults.js +2 -1
  157. package/dist/node-defaults.js.map +1 -1
  158. package/dist/order-keys.d.ts +3 -1
  159. package/dist/order-keys.d.ts.map +1 -1
  160. package/dist/order-keys.js +6 -1
  161. package/dist/order-keys.js.map +1 -1
  162. package/dist/outline/flatten.d.ts +15 -0
  163. package/dist/outline/flatten.d.ts.map +1 -0
  164. package/dist/outline/flatten.js +189 -0
  165. package/dist/outline/flatten.js.map +1 -0
  166. package/dist/outline/heads.js +34 -0
  167. package/dist/outline/heads.js.map +1 -0
  168. package/dist/outline/hit.d.ts +21 -0
  169. package/dist/outline/hit.d.ts.map +1 -0
  170. package/dist/outline/hit.js +276 -0
  171. package/dist/outline/hit.js.map +1 -0
  172. package/dist/outline/index.d.ts +5 -0
  173. package/dist/outline/index.js +5 -0
  174. package/dist/outline/point.d.ts +16 -0
  175. package/dist/outline/point.d.ts.map +1 -0
  176. package/dist/outline/point.js +42 -0
  177. package/dist/outline/point.js.map +1 -0
  178. package/dist/outline/shapes.d.ts +35 -0
  179. package/dist/outline/shapes.d.ts.map +1 -0
  180. package/dist/outline/shapes.js +181 -0
  181. package/dist/outline/shapes.js.map +1 -0
  182. package/dist/plugin-data/fields.d.ts +4 -1
  183. package/dist/plugin-data/fields.d.ts.map +1 -1
  184. package/dist/plugin-data/fields.js +21 -4
  185. package/dist/plugin-data/fields.js.map +1 -1
  186. package/dist/resize.d.ts +22 -11
  187. package/dist/resize.d.ts.map +1 -1
  188. package/dist/resize.js +227 -93
  189. package/dist/resize.js.map +1 -1
  190. package/dist/scaling/index.d.ts +3 -2
  191. package/dist/scaling/index.js +3 -2
  192. package/dist/scaling/node.d.ts.map +1 -1
  193. package/dist/scaling/node.js +5 -1
  194. package/dist/scaling/node.js.map +1 -1
  195. package/dist/scaling/snapshot.d.ts +25 -0
  196. package/dist/scaling/snapshot.d.ts.map +1 -0
  197. package/dist/scaling/snapshot.js +39 -0
  198. package/dist/scaling/snapshot.js.map +1 -0
  199. package/dist/scaling/tree.d.ts +24 -2
  200. package/dist/scaling/tree.d.ts.map +1 -1
  201. package/dist/scaling/tree.js +38 -15
  202. package/dist/scaling/tree.js.map +1 -1
  203. package/dist/shaders/model.d.ts.map +1 -1
  204. package/dist/shaders/model.js +2 -1
  205. package/dist/shaders/model.js.map +1 -1
  206. package/dist/shared-styles.d.ts +16 -1
  207. package/dist/shared-styles.d.ts.map +1 -1
  208. package/dist/shared-styles.js +69 -2
  209. package/dist/shared-styles.js.map +1 -1
  210. package/dist/slots/authoring.d.ts +3 -2
  211. package/dist/slots/authoring.d.ts.map +1 -1
  212. package/dist/slots/authoring.js +5 -3
  213. package/dist/slots/authoring.js.map +1 -1
  214. package/dist/slots/content.d.ts.map +1 -1
  215. package/dist/slots/content.js +6 -1
  216. package/dist/slots/content.js.map +1 -1
  217. package/dist/slots/frames.d.ts +2 -0
  218. package/dist/slots/frames.d.ts.map +1 -1
  219. package/dist/slots/frames.js +7 -2
  220. package/dist/slots/frames.js.map +1 -1
  221. package/dist/text/direction.d.ts +3 -1
  222. package/dist/text/direction.d.ts.map +1 -1
  223. package/dist/text/direction.js +5 -1
  224. package/dist/text/direction.js.map +1 -1
  225. package/dist/text/index.d.ts +2 -2
  226. package/dist/text/index.js +3 -3
  227. package/dist/text/lengths.d.ts +6 -1
  228. package/dist/text/lengths.d.ts.map +1 -1
  229. package/dist/text/lengths.js +10 -1
  230. package/dist/text/lengths.js.map +1 -1
  231. package/dist/text/overflow.d.ts +2 -2
  232. package/dist/text/overflow.js.map +1 -1
  233. package/dist/text/picture.d.ts +1 -2
  234. package/dist/text/picture.d.ts.map +1 -1
  235. package/dist/text/picture.js +1 -4
  236. package/dist/text/picture.js.map +1 -1
  237. package/dist/transfer/apply.d.ts.map +1 -1
  238. package/dist/transfer/apply.js +1 -6
  239. package/dist/transfer/apply.js.map +1 -1
  240. package/dist/transfer/copied.d.ts +35 -3
  241. package/dist/transfer/copied.d.ts.map +1 -1
  242. package/dist/transfer/copied.js +44 -13
  243. package/dist/transfer/copied.js.map +1 -1
  244. package/dist/transfer/dependencies.d.ts +38 -0
  245. package/dist/transfer/dependencies.d.ts.map +1 -0
  246. package/dist/transfer/dependencies.js +100 -0
  247. package/dist/transfer/dependencies.js.map +1 -0
  248. package/dist/transfer/history.js +0 -1
  249. package/dist/transfer/history.js.map +1 -1
  250. package/dist/transfer/index.d.ts +5 -3
  251. package/dist/transfer/index.js +5 -3
  252. package/dist/transfer/nodes.d.ts.map +1 -1
  253. package/dist/transfer/nodes.js +16 -19
  254. package/dist/transfer/nodes.js.map +1 -1
  255. package/dist/transfer/plan.d.ts +0 -1
  256. package/dist/transfer/plan.js +3 -5
  257. package/dist/transfer/plan.js.map +1 -1
  258. package/dist/transfer/remove.js +5 -2
  259. package/dist/transfer/remove.js.map +1 -1
  260. package/dist/transfer/resources.d.ts +21 -0
  261. package/dist/transfer/resources.d.ts.map +1 -0
  262. package/dist/transfer/resources.js +38 -0
  263. package/dist/transfer/resources.js.map +1 -0
  264. package/dist/transfer/variables.d.ts +8 -1
  265. package/dist/transfer/variables.d.ts.map +1 -1
  266. package/dist/transfer/variables.js +24 -1
  267. package/dist/transfer/variables.js.map +1 -1
  268. package/dist/types.d.ts +73 -2
  269. package/dist/types.d.ts.map +1 -1
  270. package/dist/types.js.map +1 -1
  271. package/dist/undo.d.ts +28 -3
  272. package/dist/undo.d.ts.map +1 -1
  273. package/dist/undo.js +24 -7
  274. package/dist/undo.js.map +1 -1
  275. package/dist/variables/bindings.d.ts +37 -11
  276. package/dist/variables/bindings.d.ts.map +1 -1
  277. package/dist/variables/bindings.js +122 -74
  278. package/dist/variables/bindings.js.map +1 -1
  279. package/dist/variables/fields.d.ts +19 -1
  280. package/dist/variables/fields.d.ts.map +1 -1
  281. package/dist/variables/fields.js +36 -1
  282. package/dist/variables/fields.js.map +1 -1
  283. package/dist/variables/formula/check.d.ts +1 -1
  284. package/dist/variables/formula/index.d.ts +2 -2
  285. package/dist/variables/formula/index.js +1 -1
  286. package/dist/variables/formula/index.js.map +1 -1
  287. package/dist/variables/formula/print.d.ts +1 -1
  288. package/dist/variables/formula/signatures.js +1 -1
  289. package/dist/variables/index.d.ts +7 -7
  290. package/dist/variables/index.d.ts.map +1 -1
  291. package/dist/variables/index.js +57 -26
  292. package/dist/variables/index.js.map +1 -1
  293. package/dist/variables/paints.d.ts +57 -0
  294. package/dist/variables/paints.d.ts.map +1 -0
  295. package/dist/variables/paints.js +231 -0
  296. package/dist/variables/paints.js.map +1 -0
  297. package/dist/variables/resolution.d.ts +14 -11
  298. package/dist/variables/resolution.d.ts.map +1 -1
  299. package/dist/variables/resolution.js +36 -17
  300. package/dist/variables/resolution.js.map +1 -1
  301. package/dist/variables/subtree.js +17 -0
  302. package/dist/variables/subtree.js.map +1 -0
  303. package/dist/vector-network.d.ts.map +1 -1
  304. package/dist/vector-network.js +2 -8
  305. package/dist/vector-network.js.map +1 -1
  306. package/package.json +14 -1
  307. package/src/arrow-caps.ts +2 -5
  308. package/src/asset-keys.ts +20 -0
  309. package/src/behaviours/kinds.ts +51 -34
  310. package/src/behaviours/layers.ts +4 -2
  311. package/src/behaviours/model.ts +22 -11
  312. package/src/behaviours/schema.ts +20 -4
  313. package/src/behaviours/spec.ts +8 -2
  314. package/src/bindings.ts +11 -2
  315. package/src/checkpoint.ts +319 -17
  316. package/src/components/properties.ts +101 -2
  317. package/src/components/variants.ts +51 -30
  318. package/src/connectors/hit.ts +1 -12
  319. package/src/connectors/pieces.ts +2 -5
  320. package/src/constants.ts +17 -0
  321. package/src/constraints.ts +60 -0
  322. package/src/coordinate.ts +220 -0
  323. package/src/copy.ts +18 -19
  324. package/src/css/blend.ts +27 -0
  325. package/src/css/index.ts +18 -1
  326. package/src/css/text.ts +85 -17
  327. package/src/css/transform.ts +266 -0
  328. package/src/css/values.ts +8 -0
  329. package/src/cubic.ts +14 -0
  330. package/src/export-format.ts +29 -0
  331. package/src/fields/text.ts +3 -1
  332. package/src/figjam/palette.ts +29 -1
  333. package/src/generated/shape-with-text.ts +28 -2
  334. package/src/geometry.ts +97 -206
  335. package/src/graph.ts +155 -45
  336. package/src/group-bounds.ts +84 -17
  337. package/src/guides.ts +5 -0
  338. package/src/hit-test.ts +93 -64
  339. package/src/images.ts +71 -3
  340. package/src/instances/fields.ts +8 -0
  341. package/src/instances/index.ts +62 -31
  342. package/src/instances/kept-overrides.ts +280 -0
  343. package/src/instances/main-component.ts +12 -0
  344. package/src/instances/overrides.ts +27 -1
  345. package/src/instances/placement.ts +63 -10
  346. package/src/instances/swap.ts +456 -0
  347. package/src/instances/sync.ts +176 -50
  348. package/src/node-defaults.ts +3 -1
  349. package/src/order-keys.ts +6 -0
  350. package/src/outline/flatten.ts +190 -0
  351. package/src/outline/heads.ts +52 -0
  352. package/src/outline/hit.ts +369 -0
  353. package/src/outline/index.ts +4 -0
  354. package/src/outline/point.ts +59 -0
  355. package/src/outline/shapes.ts +207 -0
  356. package/src/plugin-data/fields.ts +21 -2
  357. package/src/resize.ts +267 -119
  358. package/src/scaling/index.ts +9 -1
  359. package/src/scaling/node.ts +9 -1
  360. package/src/scaling/snapshot.ts +51 -0
  361. package/src/scaling/tree.ts +63 -19
  362. package/src/shaders/model.ts +2 -3
  363. package/src/shared-styles.ts +90 -1
  364. package/src/slots/authoring.ts +5 -3
  365. package/src/slots/content.ts +6 -1
  366. package/src/slots/frames.ts +9 -7
  367. package/src/text/direction.ts +5 -0
  368. package/src/text/lengths.ts +10 -0
  369. package/src/text/overflow.ts +2 -2
  370. package/src/text/picture.ts +0 -4
  371. package/src/transfer/apply.ts +1 -13
  372. package/src/transfer/copied.ts +83 -15
  373. package/src/transfer/dependencies.ts +126 -0
  374. package/src/transfer/history.ts +0 -6
  375. package/src/transfer/index.ts +20 -2
  376. package/src/transfer/nodes.ts +17 -31
  377. package/src/transfer/plan.ts +3 -11
  378. package/src/transfer/remove.ts +5 -2
  379. package/src/transfer/resources.ts +54 -0
  380. package/src/transfer/variables.ts +34 -0
  381. package/src/types.ts +83 -1
  382. package/src/undo.ts +48 -10
  383. package/src/variables/bindings.ts +152 -88
  384. package/src/variables/fields.ts +46 -1
  385. package/src/variables/formula/index.ts +3 -1
  386. package/src/variables/index.ts +90 -39
  387. package/src/variables/paints.ts +296 -0
  388. package/src/variables/resolution.ts +39 -28
  389. package/src/variables/subtree.ts +15 -0
  390. package/src/vector-network.ts +4 -2
package/src/checkpoint.ts CHANGED
@@ -1,6 +1,32 @@
1
1
  import { isEqual } from 'es-toolkit'
2
2
 
3
3
  import type { SceneGraph } from './graph'
4
+ import { createSceneMutationImpact, recordSceneMutations } from './mutation-impact'
5
+ import type { SceneNode, Variable, VariableCollection } from './types'
6
+
7
+ const NODE_COUNT_ERROR = 'Atomic tools must not create or remove nodes or variables'
8
+ const NODE_IDENTITY_ERROR = 'Atomic tools must not change node hierarchy or identity'
9
+ const VARIABLE_IDENTITY_ERROR = 'Atomic tools must not change variable identity'
10
+ const DOCUMENT_ERROR = 'Atomic tools must not change collections, assets or document metadata'
11
+
12
+ /** Throws when an edit replaced a variable or changed its identity, collection, or type. */
13
+ function assertVariableIdentity(
14
+ graph: SceneGraph,
15
+ saved: ReadonlyMap<string, Variable>,
16
+ refs: ReadonlyMap<string, Variable>
17
+ ): void {
18
+ for (const [id, before] of saved) {
19
+ const after = graph.variables.get(id)
20
+ if (
21
+ !after ||
22
+ after !== refs.get(id) ||
23
+ before.id !== after.id ||
24
+ before.collectionId !== after.collectionId ||
25
+ before.type !== after.type
26
+ )
27
+ throw new Error(VARIABLE_IDENTITY_ERROR)
28
+ }
29
+ }
4
30
 
5
31
  /** Capture rollback state without changing graph or surviving object identities. */
6
32
  export function captureGraphCheckpoint(graph: SceneGraph) {
@@ -11,7 +37,6 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
11
37
  nodes: graph.nodes,
12
38
  variables: graph.variables,
13
39
  variableCollections: graph.variableCollections,
14
- activeMode: graph.activeMode,
15
40
  instanceIndex: graph.instanceIndex,
16
41
  enabledLibraries: graph.enabledLibraries,
17
42
  rootId: graph.rootId,
@@ -27,7 +52,7 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
27
52
  snapshot.nodes.size !== graph.nodes.size ||
28
53
  snapshot.variables.size !== graph.variables.size
29
54
  )
30
- throw new Error('Atomic tools must not create or remove nodes or variables')
55
+ throw new Error(NODE_COUNT_ERROR)
31
56
  for (const [id, before] of snapshot.nodes) {
32
57
  const after = graph.nodes.get(id)
33
58
  if (
@@ -39,27 +64,16 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
39
64
  before.componentId !== after.componentId ||
40
65
  !isEqual(before.childIds, after.childIds)
41
66
  ) {
42
- throw new Error('Atomic tools must not change node hierarchy or identity')
67
+ throw new Error(NODE_IDENTITY_ERROR)
43
68
  }
44
69
  }
45
- for (const [id, before] of snapshot.variables) {
46
- const after = graph.variables.get(id)
47
- if (
48
- !after ||
49
- after !== variableRefs.get(id) ||
50
- before.id !== after.id ||
51
- before.collectionId !== after.collectionId ||
52
- before.type !== after.type
53
- )
54
- throw new Error('Atomic tools must not change variable identity')
55
- }
70
+ assertVariableIdentity(graph, snapshot.variables, variableRefs)
56
71
  assertDocumentUnchanged()
57
72
  }
58
73
 
59
74
  function assertDocumentUnchanged() {
60
75
  if (
61
76
  !isEqual(snapshot.variableCollections, graph.variableCollections) ||
62
- !isEqual(snapshot.activeMode, graph.activeMode) ||
63
77
  !isEqual(snapshot.instanceIndex, graph.instanceIndex) ||
64
78
  !isEqual(snapshot.enabledLibraries, graph.enabledLibraries) ||
65
79
  images.size !== graph.images.size ||
@@ -69,7 +83,7 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
69
83
  graph.figKiwiVersion !== snapshot.figKiwiVersion ||
70
84
  !isEqual(graph.figSchemaDeflated, snapshot.figSchemaDeflated)
71
85
  ) {
72
- throw new Error('Atomic tools must not change collections, assets or document metadata')
86
+ throw new Error(DOCUMENT_ERROR)
73
87
  }
74
88
  }
75
89
 
@@ -78,10 +92,12 @@ export function captureGraphCheckpoint(graph: SceneGraph) {
78
92
  restoreObjects(graph.nodes, snapshot.nodes, nodeRefs)
79
93
  restoreObjects(graph.variables, snapshot.variables, variableRefs)
80
94
  restoreObjects(graph.variableCollections, snapshot.variableCollections, collectionRefs)
81
- restoreMap(graph.activeMode, structuredClone(snapshot.activeMode))
82
95
  restoreMap(graph.instanceIndex, structuredClone(snapshot.instanceIndex))
83
96
  restoreMap(graph.enabledLibraries, structuredClone(snapshot.enabledLibraries))
84
97
  restoreMap(graph.images, images)
98
+ graph.connectorIds.clear()
99
+ for (const node of graph.nodes.values())
100
+ if (node.type === 'CONNECTOR') graph.connectorIds.add(node.id)
85
101
  graph.rootId = snapshot.rootId
86
102
  graph.figKiwiVersion = snapshot.figKiwiVersion
87
103
  graph.figSchemaDeflated = structuredClone(snapshot.figSchemaDeflated)
@@ -117,7 +133,293 @@ function restoreObjects<T extends object>(
117
133
  }
118
134
  }
119
135
 
136
+ /** Copies `id`'s record the first time it is reported: `null` for one about to be added. */
137
+ function copyRecord<T>(copies: Map<string, T | null>, live: ReadonlyMap<string, T>, id: string) {
138
+ if (!copies.has(id)) copies.set(id, structuredClone(live.get(id) ?? null))
139
+ }
140
+
141
+ /** Whether `target` holds exactly the objects `refs` held, in any order. */
142
+ function sameEntries<T>(target: ReadonlyMap<string, T>, refs: ReadonlyMap<string, T>): boolean {
143
+ if (target.size !== refs.size) return false
144
+ for (const [id, ref] of refs) if (target.get(id) !== ref) return false
145
+ return true
146
+ }
147
+
148
+ /**
149
+ * Puts back the records `refs` held, in their order, with the copied ones rewritten in place to
150
+ * their copies, as views may hold them.
151
+ */
152
+ function restoreCopied<T extends object>(
153
+ target: Map<string, T>,
154
+ copies: ReadonlyMap<string, T | null>,
155
+ refs: ReadonlyMap<string, T>
156
+ ) {
157
+ target.clear()
158
+ for (const [id, original] of refs) {
159
+ const saved = copies.get(id)
160
+ if (saved) {
161
+ for (const key of Object.keys(original) as (keyof T)[]) {
162
+ if (!(key in saved)) Reflect.deleteProperty(original, key)
163
+ }
164
+ Object.assign(original, structuredClone(saved))
165
+ }
166
+ target.set(id, original)
167
+ }
168
+ }
169
+
120
170
  function restoreMap<K, V>(target: Map<K, V>, saved: Map<K, V>) {
121
171
  target.clear()
122
172
  for (const [key, value] of saved) target.set(key, value)
123
173
  }
174
+
175
+ /**
176
+ * Rollback state for an edit that may only change properties, which costs what the edit touches
177
+ * rather than a copy of the document: a node is copied the first time the graph reports it about
178
+ * to change (`SceneGraph.observeNodeChanges`), together with its ancestors below the page, which
179
+ * keep instance override maps the graph writes into in place.
180
+ */
181
+ export interface TouchedCheckpoint {
182
+ /** Copies of the reported nodes and their ancestors below the page, as they were before. */
183
+ readonly nodes: ReadonlyMap<string, SceneNode>
184
+ /** Nodes the graph reported changing or creating. */
185
+ readonly reported: ReadonlySet<string>
186
+ /** Copies of the variables reported changing (`SceneGraph.willChangeVariable`), as they were. */
187
+ readonly variables: ReadonlyMap<string, Variable | null>
188
+ readonly variableCollections: ReadonlyMap<string, VariableCollection | null>
189
+ /** Throws unless the edit only changed properties of existing nodes and variables. */
190
+ assertPropertiesOnly(): void
191
+ /** Stops observing and puts back everything the edit changed, keeping object identities. */
192
+ restore(): void
193
+ /** Stops observing the graph. */
194
+ stop(): void
195
+ }
196
+
197
+ /**
198
+ * Starts observing `graph` for an edit that may only change properties. Node identities are
199
+ * kept as pointers, so replacing, adding, or removing a node anywhere is found; a node's fields
200
+ * are compared and restored only where the graph reported it, so code that writes into a node
201
+ * directly calls `SceneGraph.willChangeNode` first, and a node event without such a report fails
202
+ * the edit.
203
+ */
204
+ export function captureTouchedCheckpoint(graph: SceneGraph): TouchedCheckpoint {
205
+ const nodeRefs = new Map(graph.nodes)
206
+ const variableRefs = new Map(graph.variables)
207
+ const collectionRefs = new Map(graph.variableCollections)
208
+ const saved = structuredClone({
209
+ enabledLibraries: graph.enabledLibraries
210
+ })
211
+ // Instances follow their nodes' components, which a property edit cannot change, so the sets
212
+ // are kept rather than their ids copied deeply.
213
+ const instanceIndex = new Map(
214
+ [...graph.instanceIndex].map(([id, instances]) => [id, new Set(instances)])
215
+ )
216
+ const variables = new Map<string, Variable | null>()
217
+ const variableCollections = new Map<string, VariableCollection | null>()
218
+ // Property edits cannot edit assets. Retain existing buffers, not copies of every bitmap.
219
+ const images = new Map(graph.images)
220
+ const { rootId, figKiwiVersion, figSchemaDeflated, documentColorSpace } = graph
221
+ const nodes = new Map<string, SceneNode>()
222
+ const reported = new Set<string>()
223
+ const created = new Set<string>()
224
+ const impact = createSceneMutationImpact()
225
+
226
+ /** Copies a node as the edit found it: the original object, should the edit have replaced it. */
227
+ function copy(node: SceneNode) {
228
+ nodes.set(node.id, structuredClone(nodeRefs.get(node.id) ?? node))
229
+ }
230
+
231
+ const skipped = (node: SceneNode) => nodes.has(node.id) || created.has(node.id)
232
+
233
+ /**
234
+ * Copies `node` and each ancestor not copied yet, up to but excluding its page. An ancestor
235
+ * copied already had its own ancestors copied with it, so the walk stops there.
236
+ */
237
+ function copyLineage(node: SceneNode) {
238
+ if (skipped(node)) return
239
+ copy(node)
240
+ if (!node.parentId) return
241
+ graph.closest(node.parentId, (ancestor) => {
242
+ if (ancestor.type === 'CANVAS' || ancestor.id === rootId || skipped(ancestor)) return true
243
+ copy(ancestor)
244
+ return false
245
+ })
246
+ }
247
+
248
+ const unobserve = graph.observeNodeChanges({
249
+ created: (node) => {
250
+ created.add(node.id)
251
+ reported.add(node.id)
252
+ },
253
+ before: (node) => {
254
+ reported.add(node.id)
255
+ copyLineage(node)
256
+ },
257
+ beforeRecord: (kind, id) => {
258
+ if (kind === 'variable') copyRecord(variables, graph.variables, id)
259
+ else copyRecord(variableCollections, graph.variableCollections, id)
260
+ }
261
+ })
262
+ const unbindEvents = recordSceneMutations(graph, impact)
263
+ let observing = true
264
+
265
+ function stop() {
266
+ if (!observing) return
267
+ observing = false
268
+ unobserve()
269
+ unbindEvents()
270
+ }
271
+
272
+ function assertNodes() {
273
+ if (
274
+ created.size > 0 ||
275
+ impact.deletedNodeIds.size > 0 ||
276
+ graph.nodes.size !== nodeRefs.size ||
277
+ graph.variables.size !== variableRefs.size
278
+ )
279
+ throw new Error(NODE_COUNT_ERROR)
280
+ for (const [id, node] of graph.nodes) {
281
+ const original = nodeRefs.get(id)
282
+ if (!original) throw new Error(NODE_COUNT_ERROR)
283
+ if (original !== node) throw new Error(NODE_IDENTITY_ERROR)
284
+ }
285
+ for (const [id, before] of nodes) {
286
+ const after = graph.nodes.get(id)
287
+ if (
288
+ !after ||
289
+ before.id !== after.id ||
290
+ before.type !== after.type ||
291
+ before.parentId !== after.parentId ||
292
+ before.componentId !== after.componentId ||
293
+ !isEqual(before.childIds, after.childIds)
294
+ )
295
+ throw new Error(NODE_IDENTITY_ERROR)
296
+ }
297
+ for (const id of [
298
+ ...impact.changedNodeIds,
299
+ ...impact.previousParentIds,
300
+ ...impact.currentParentIds
301
+ ]) {
302
+ if (!nodes.has(id) && !created.has(id)) {
303
+ throw new Error(
304
+ `Atomic tools must report layer changes to the scene graph (SceneGraph.willChangeNode): ${id}`
305
+ )
306
+ }
307
+ }
308
+ }
309
+
310
+ function assertPropertiesOnly() {
311
+ assertNodes()
312
+ if (!sameEntries(graph.variables, variableRefs)) throw new Error(VARIABLE_IDENTITY_ERROR)
313
+ for (const [id, before] of variables) {
314
+ const after = graph.variables.get(id)
315
+ if (
316
+ !before ||
317
+ !after ||
318
+ before.id !== after.id ||
319
+ before.collectionId !== after.collectionId ||
320
+ before.type !== after.type
321
+ )
322
+ throw new Error(VARIABLE_IDENTITY_ERROR)
323
+ }
324
+ if (
325
+ !sameEntries(graph.variableCollections, collectionRefs) ||
326
+ [...variableCollections].some(
327
+ ([id, before]) => !isEqual(before, graph.variableCollections.get(id) ?? null)
328
+ ) ||
329
+ !isEqual(instanceIndex, graph.instanceIndex) ||
330
+ !isEqual(saved.enabledLibraries, graph.enabledLibraries) ||
331
+ images.size !== graph.images.size ||
332
+ [...images].some(([id, bytes]) => graph.images.get(id) !== bytes) ||
333
+ graph.rootId !== rootId ||
334
+ graph.documentColorSpace !== documentColorSpace ||
335
+ graph.figKiwiVersion !== figKiwiVersion ||
336
+ // Every writer assigns a new schema rather than writing into it.
337
+ graph.figSchemaDeflated !== figSchemaDeflated
338
+ )
339
+ throw new Error(DOCUMENT_ERROR)
340
+ }
341
+
342
+ /** Puts the node map back as it was, in its order, if the edit changed its entries. */
343
+ function restoreNodeMap(): { removed: Map<string, SceneNode>; readded: Set<string> } {
344
+ const removed = new Map<string, SceneNode>()
345
+ const readded = new Set<string>()
346
+ let changed = graph.nodes.size !== nodeRefs.size
347
+ if (!changed) {
348
+ // In lockstep, so a node removed and set again, which moves to the end, is put back too.
349
+ const live = graph.nodes.entries()
350
+ for (const [id, node] of nodeRefs) {
351
+ const next = live.next()
352
+ if (next.done || next.value[0] !== id || next.value[1] !== node) {
353
+ changed = true
354
+ break
355
+ }
356
+ }
357
+ }
358
+ if (!changed) return { removed, readded }
359
+ const failed = new Map(graph.nodes)
360
+ graph.nodes.clear()
361
+ for (const [id, node] of nodeRefs) {
362
+ graph.nodes.set(id, node)
363
+ if (!failed.has(id)) readded.add(id)
364
+ }
365
+ for (const [id, node] of failed) if (!nodeRefs.has(id)) removed.set(id, node)
366
+ return { removed, readded }
367
+ }
368
+
369
+ function restore() {
370
+ stop()
371
+ const { removed, readded } = restoreNodeMap()
372
+ for (const [id, copy] of nodes) {
373
+ const original = nodeRefs.get(id)
374
+ if (!original) continue
375
+ for (const key of Object.keys(original)) {
376
+ if (!Object.hasOwn(copy, key)) Reflect.deleteProperty(original, key)
377
+ }
378
+ Object.assign(original, structuredClone(copy))
379
+ }
380
+ restoreCopied(graph.variables, variables, variableRefs)
381
+ restoreCopied(graph.variableCollections, variableCollections, collectionRefs)
382
+ restoreMap(
383
+ graph.instanceIndex,
384
+ new Map([...instanceIndex].map(([id, instances]) => [id, new Set(instances)]))
385
+ )
386
+ restoreMap(graph.enabledLibraries, structuredClone(saved.enabledLibraries))
387
+ restoreMap(graph.images, images)
388
+ graph.rootId = rootId
389
+ graph.figKiwiVersion = figKiwiVersion
390
+ graph.figSchemaDeflated = figSchemaDeflated
391
+ graph.documentColorSpace = documentColorSpace
392
+ graph.clearAbsPosCache()
393
+ const touched = new Set([
394
+ ...nodes.keys(),
395
+ ...created,
396
+ ...readded,
397
+ ...removed.keys(),
398
+ ...impact.changedNodeIds,
399
+ ...impact.previousParentIds,
400
+ ...impact.currentParentIds
401
+ ])
402
+ for (const id of touched) {
403
+ if (graph.nodes.get(id)?.type === 'CONNECTOR') graph.connectorIds.add(id)
404
+ else graph.connectorIds.delete(id)
405
+ }
406
+ // Notify consumers only after the complete hierarchy and indexes have been restored.
407
+ for (const [id, node] of removed) graph.emitter.emit('node:deleted', id, node.parentId)
408
+ for (const id of touched) {
409
+ const node = graph.nodes.get(id)
410
+ if (!node) continue
411
+ if (readded.has(id)) graph.emitter.emit('node:created', node)
412
+ else graph.emitter.emit('node:updated', id, node)
413
+ }
414
+ }
415
+
416
+ return {
417
+ nodes,
418
+ reported,
419
+ variables,
420
+ variableCollections,
421
+ assertPropertiesOnly,
422
+ restore,
423
+ stop
424
+ }
425
+ }
@@ -1,6 +1,5 @@
1
1
  import type { SceneGraph } from '../graph'
2
- import { findInstanceAncestor } from '../instances'
3
- import { instanceMainComponent } from '../instances/main-component'
2
+ import { findInstanceAncestor, instanceMainComponent } from '../instances/main-component'
4
3
  import { setInstanceOverride } from '../instances/overrides'
5
4
  import { walkInstanceSources } from '../instances/source-walk'
6
5
  import { randomHex } from '../random'
@@ -16,6 +15,15 @@ export interface ComponentPropertyTarget {
16
15
  source: SceneNode
17
16
  }
18
17
 
18
+ /**
19
+ * The component that defines a main component's properties: its set when it is a variant,
20
+ * since variants own no properties of their own (desktop 126), else the component itself.
21
+ */
22
+ export function propertyOwner(graph: SceneGraph, component: SceneNode): SceneNode {
23
+ const parent = component.parentId ? graph.getNode(component.parentId) : undefined
24
+ return component.type === 'COMPONENT' && parent?.type === 'COMPONENT_SET' ? parent : component
25
+ }
26
+
19
27
  export function componentPropertyOwners(graph: SceneGraph, instance: SceneNode): SceneNode[] {
20
28
  if (instance.type !== 'INSTANCE') return []
21
29
  const component = instanceMainComponent(graph, instance)
@@ -172,6 +180,60 @@ export function applyComponentPropertyValue(
172
180
  return definition.type === 'INSTANCE_SWAP' ? target : instance
173
181
  }
174
182
 
183
+ export function reapplyComponentPropertyAssignments(
184
+ graph: SceneGraph,
185
+ instanceId: string
186
+ ): ComponentPropertyDefinition[] {
187
+ const instance = graph.getNode(instanceId)
188
+ if (instance?.type !== 'INSTANCE') {
189
+ return []
190
+ }
191
+ const definitions = new Map(
192
+ componentPropertyDefinitions(graph, instance).map((definition) => [definition.id, definition])
193
+ )
194
+ const applied: ComponentPropertyDefinition[] = []
195
+ for (const [propertyId, value] of Object.entries(instance.componentPropertyAssignments)) {
196
+ const definition = definitions.get(propertyId)
197
+ if (definition && definition.type !== 'VARIANT') {
198
+ applyComponentPropertyValue(graph, instanceId, definition, value)
199
+ applied.push(definition)
200
+ }
201
+ }
202
+ return applied
203
+ }
204
+
205
+ const ASSIGNMENT_OVERRIDE_PREFIX = 'componentPropertyAssignments/'
206
+
207
+ export function assignmentOverrideField(propertyId: string): string {
208
+ return `${ASSIGNMENT_OVERRIDE_PREFIX}${propertyId}`
209
+ }
210
+
211
+ export function overriddenAssignmentId(field: string): string | undefined {
212
+ return field.startsWith(ASSIGNMENT_OVERRIDE_PREFIX)
213
+ ? field.slice(ASSIGNMENT_OVERRIDE_PREFIX.length)
214
+ : undefined
215
+ }
216
+
217
+ export function recordAssignmentOverride(
218
+ graph: SceneGraph,
219
+ instanceId: string,
220
+ propertyId: string
221
+ ): void {
222
+ const instance = graph.getNode(instanceId)
223
+ const owner = instance?.parentId ? findInstanceAncestor(graph, instance.parentId) : undefined
224
+ if (!instance || !owner) {
225
+ return
226
+ }
227
+ setInstanceOverride(
228
+ owner.instanceOverrides,
229
+ owner.id,
230
+ instance.id,
231
+ assignmentOverrideField(propertyId),
232
+ instance.componentPropertyAssignments[propertyId]
233
+ )
234
+ graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })
235
+ }
236
+
175
237
  function componentSubtree(graph: SceneGraph, componentId: string): SceneNode[] {
176
238
  const component = graph.getNode(componentId)
177
239
  if (!component) return []
@@ -235,6 +297,43 @@ export function removeComponentProperty(
235
297
  return true
236
298
  }
237
299
 
300
+ /**
301
+ * Moves the boolean, text, instance swap, and slot properties a set's variants define themselves
302
+ * onto the set, under their IDs, so the layers and instances naming them still do: a variant's
303
+ * properties are its set's, as Figma keeps them, and combining components moves theirs to the
304
+ * set (desktop 126). A property the set already has under that ID is not added again. Returns
305
+ * the IDs of the set and the variants it changed.
306
+ */
307
+ export function moveVariantPropertiesToSet(graph: SceneGraph, setId: string): string[] {
308
+ const set = graph.getNode(setId)
309
+ if (set?.type !== 'COMPONENT_SET') return []
310
+ const changed: string[] = []
311
+ const known = new Set(set.componentPropertyDefinitions.map((definition) => definition.id))
312
+ const moved: ComponentPropertyDefinition[] = []
313
+ for (const variant of graph.getChildren(setId)) {
314
+ const own = variant.componentPropertyDefinitions.filter(
315
+ (definition) => definition.type !== 'VARIANT'
316
+ )
317
+ if (variant.type !== 'COMPONENT' || own.length === 0) continue
318
+ for (const definition of own) {
319
+ if (known.has(definition.id)) continue
320
+ known.add(definition.id)
321
+ moved.push(definition)
322
+ }
323
+ graph.updateNode(variant.id, {
324
+ componentPropertyDefinitions: variant.componentPropertyDefinitions.filter(
325
+ (definition) => definition.type === 'VARIANT'
326
+ )
327
+ })
328
+ changed.push(variant.id)
329
+ }
330
+ if (moved.length === 0) return changed
331
+ graph.updateNode(setId, {
332
+ componentPropertyDefinitions: [...moved, ...set.componentPropertyDefinitions]
333
+ })
334
+ return [setId, ...changed]
335
+ }
336
+
238
337
  /** A new component property ID, in the `prop:` form the editor, plugin API, and design JSX share. */
239
338
  export function createComponentPropertyId(): string {
240
339
  return `prop:${randomHex(8)}`
@@ -1,5 +1,7 @@
1
+ import { compact, uniq } from 'es-toolkit/array'
2
+
1
3
  import type { SceneGraph } from '../graph'
2
- import type { SceneNode } from '../types'
4
+ import type { ComponentPropertyDefinition, SceneNode } from '../types'
3
5
 
4
6
  /** Variants in reading order on the canvas: top to bottom, then left to right, then by name. */
5
7
  export function compareCanvasPosition(a: SceneNode, b: SceneNode): number {
@@ -7,47 +9,66 @@ export function compareCanvasPosition(a: SceneNode, b: SceneNode): number {
7
9
  }
8
10
 
9
11
  /**
10
- * A component set's default variant, the one an instance starts as: the variant in its
11
- * top-left corner, as Figma defines it.
12
+ * Whether `a` comes before `b` reading the set as Figma does: variants whose rows overlap, edges
13
+ * touching included, read left to right, and others top to bottom.
14
+ */
15
+ function readsBefore(a: SceneNode, b: SceneNode): boolean {
16
+ const sameRow = a.y <= b.y + b.height && b.y <= a.y + a.height
17
+ return sameRow ? a.x < b.x || (a.x === b.x && a.y < b.y) : a.y < b.y
18
+ }
19
+
20
+ /**
21
+ * A component set's default variant, the one an instance starts as: the first in reading order,
22
+ * and the first in layer order of those at the same place (measured in Figma desktop 126).
12
23
  */
13
24
  export function defaultVariant(graph: SceneGraph, set: SceneNode): SceneNode | undefined {
14
- return graph
15
- .getChildren(set.id)
16
- .filter((child) => child.type === 'COMPONENT')
17
- .sort(compareCanvasPosition)[0]
25
+ let first: SceneNode | undefined
26
+ for (const child of graph.getChildren(set.id)) {
27
+ if (child.type !== 'COMPONENT') continue
28
+ if (!first || readsBefore(child, first)) first = child
29
+ }
30
+ return first
18
31
  }
19
32
 
20
33
  /**
21
- * The value a variant property starts at: its recorded default when that is one of its options,
22
- * or else the default variant's, since a document may leave the recorded default empty.
34
+ * The value a variant property starts at: the default variant's, as Figma reads it whatever
35
+ * default the document records, and follows when another variant moves to the top left. The
36
+ * recorded default stands only while the default variant has no value for the property.
23
37
  */
24
38
  export function variantDefaultValue(
25
39
  graph: SceneGraph,
26
40
  set: SceneNode,
27
- property: { name: string; defaultValue: string; variantOptions?: string[] }
41
+ property: { name: string; defaultValue: string }
28
42
  ): string {
29
- if (property.variantOptions?.includes(property.defaultValue)) return property.defaultValue
30
- return defaultVariant(graph, set)?.componentPropertyValues[property.name] ?? property.defaultValue
43
+ const value = defaultVariant(graph, set)?.componentPropertyValues[property.name]
44
+ return value || property.defaultValue
31
45
  }
32
46
 
33
47
  /**
34
- * The variant with every property at its default value: the recorded defaults when they name a
35
- * variant, else the default variant, as a document that leaves them out draws it.
48
+ * A variant property's values: the ones the document records, then any its variants use that it
49
+ * does not, since older documents record none and a set's page may have loaded only some
50
+ * variants.
36
51
  */
37
- export function restingVariant(graph: SceneGraph, set: SceneNode): SceneNode | undefined {
38
- const properties = set.componentPropertyDefinitions.filter(
39
- (definition) => definition.type === 'VARIANT'
40
- )
41
- const values = properties.map(
42
- (property) => [property.name, variantDefaultValue(graph, set, property)] as const
43
- )
44
- return (
45
- graph
46
- .getChildren(set.id)
47
- .find(
48
- (child) =>
49
- child.type === 'COMPONENT' &&
50
- values.every(([name, value]) => child.componentPropertyValues[name] === value)
51
- ) ?? defaultVariant(graph, set)
52
- )
52
+ export function variantOptions(
53
+ graph: SceneGraph,
54
+ set: SceneNode,
55
+ property: { name: string; variantOptions?: string[] }
56
+ ): string[] {
57
+ const used = graph
58
+ .getChildren(set.id)
59
+ .map((child) =>
60
+ child.type === 'COMPONENT' ? child.componentPropertyValues[property.name] : ''
61
+ )
62
+ return compact(uniq([...(property.variantOptions ?? []), ...used]))
63
+ }
64
+
65
+ /** A set's definition with a variant property's values and default filled in from its variants. */
66
+ export function resolvedVariantDefinition(
67
+ graph: SceneGraph,
68
+ set: SceneNode,
69
+ definition: ComponentPropertyDefinition
70
+ ): ComponentPropertyDefinition {
71
+ if (definition.type !== 'VARIANT') return definition
72
+ const options = { ...definition, variantOptions: variantOptions(graph, set, definition) }
73
+ return { ...options, defaultValue: variantDefaultValue(graph, set, options) }
53
74
  }
@@ -1,7 +1,7 @@
1
1
  import { getWorldMatrix } from '../coordinate'
2
2
  import type { SceneGraph } from '../graph'
3
3
  import Matrix from '../matrix'
4
- import type { Vector } from '../primitives'
4
+ import { distanceToSegment } from '../outline/point'
5
5
  import type { SceneNode } from '../types'
6
6
  import { isStraight, pointAt, type ConnectorPiece } from './pieces'
7
7
  import { connectorSublayers } from './sublayers'
@@ -11,17 +11,6 @@ const CONNECTOR_HIT_SLOP = 4
11
11
  /** Points sampled along a curved segment to measure distance to it. */
12
12
  const CURVE_SAMPLES = 24
13
13
 
14
- function distanceToSegment(p: Vector, a: Vector, b: Vector): number {
15
- const dx = b.x - a.x
16
- const dy = b.y - a.y
17
- const lengthSquared = dx * dx + dy * dy
18
- const t =
19
- lengthSquared === 0
20
- ? 0
21
- : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / lengthSquared))
22
- return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy))
23
- }
24
-
25
14
  /**
26
15
  * Whether a canvas point lands on a connector's line: within half its stroke and a few units of
27
16
  * slop, plus `pad`. A connector is picked by its line, not by the box around its route.
@@ -1,3 +1,4 @@
1
+ import { cubicPoint } from '../cubic'
1
2
  import { computeBounds } from '../geometry'
2
3
  import type { Rect, Vector } from '../primitives'
3
4
  import type { StrokeCap, VectorNetwork } from '../types'
@@ -47,11 +48,7 @@ export function pointAt(piece: ConnectorPiece, t: number): Vector {
47
48
  const { from: a, to: b } = piece
48
49
  if (isStraight(piece)) return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t }
49
50
  const [c1, c2] = controls(piece)
50
- const u = 1 - t
51
- return {
52
- x: u * u * u * a.x + 3 * u * u * t * c1.x + 3 * u * t * t * c2.x + t * t * t * b.x,
53
- y: u * u * u * a.y + 3 * u * u * t * c1.y + 3 * u * t * t * c2.y + t * t * t * b.y
54
- }
51
+ return cubicPoint(a, c1, c2, b, t)
55
52
  }
56
53
 
57
54
  function derivativeAt(piece: ConnectorPiece, t: number): Vector {