@pairdraw/scene-graph 0.19.2 → 0.21.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 (285) hide show
  1. package/dist/color/index.d.ts +28 -7
  2. package/dist/color/index.d.ts.map +1 -1
  3. package/dist/color/index.js +40 -15
  4. package/dist/color/index.js.map +1 -1
  5. package/dist/color/management.d.ts +19 -6
  6. package/dist/color/management.d.ts.map +1 -1
  7. package/dist/color/management.js +56 -1
  8. package/dist/color/management.js.map +1 -1
  9. package/dist/color/profile.d.ts +32 -0
  10. package/dist/color/profile.d.ts.map +1 -0
  11. package/dist/color/profile.js +140 -0
  12. package/dist/color/profile.js.map +1 -0
  13. package/dist/comments/index.d.ts +2 -2
  14. package/dist/comments/index.js +2 -2
  15. package/dist/comments/merge.d.ts.map +1 -1
  16. package/dist/comments/merge.js +1 -0
  17. package/dist/comments/merge.js.map +1 -1
  18. package/dist/comments/schema.d.ts +3 -1
  19. package/dist/comments/schema.d.ts.map +1 -1
  20. package/dist/comments/schema.js +19 -2
  21. package/dist/comments/schema.js.map +1 -1
  22. package/dist/comments/threads.d.ts +3 -1
  23. package/dist/comments/threads.d.ts.map +1 -1
  24. package/dist/comments/threads.js +2 -1
  25. package/dist/comments/threads.js.map +1 -1
  26. package/dist/components/index.d.ts +2 -2
  27. package/dist/components/index.js +2 -2
  28. package/dist/components/properties.d.ts.map +1 -1
  29. package/dist/components/properties.js +7 -7
  30. package/dist/components/properties.js.map +1 -1
  31. package/dist/components/variants.d.ts +12 -1
  32. package/dist/components/variants.d.ts.map +1 -1
  33. package/dist/components/variants.js +30 -1
  34. package/dist/components/variants.js.map +1 -1
  35. package/dist/constants.d.ts +25 -1
  36. package/dist/constants.d.ts.map +1 -1
  37. package/dist/constants.js +94 -1
  38. package/dist/constants.js.map +1 -1
  39. package/dist/copy.d.ts.map +1 -1
  40. package/dist/copy.js +17 -1
  41. package/dist/copy.js.map +1 -1
  42. package/dist/css/index.d.ts +2 -2
  43. package/dist/css/index.js +2 -2
  44. package/dist/css/shadows.d.ts +2 -2
  45. package/dist/css/shadows.d.ts.map +1 -1
  46. package/dist/css/shadows.js +4 -4
  47. package/dist/css/shadows.js.map +1 -1
  48. package/dist/css/text.d.ts +17 -1
  49. package/dist/css/text.d.ts.map +1 -1
  50. package/dist/css/text.js +62 -1
  51. package/dist/css/text.js.map +1 -1
  52. package/dist/css/values.d.ts +7 -2
  53. package/dist/css/values.d.ts.map +1 -1
  54. package/dist/css/values.js +8 -4
  55. package/dist/css/values.js.map +1 -1
  56. package/dist/design-memory/index.d.ts +3 -0
  57. package/dist/design-memory/index.js +3 -0
  58. package/dist/design-memory/model.d.ts +33 -0
  59. package/dist/design-memory/model.d.ts.map +1 -0
  60. package/dist/design-memory/model.js +142 -0
  61. package/dist/design-memory/model.js.map +1 -0
  62. package/dist/design-memory/schema.d.ts +76 -0
  63. package/dist/design-memory/schema.d.ts.map +1 -0
  64. package/dist/design-memory/schema.js +72 -0
  65. package/dist/design-memory/schema.js.map +1 -0
  66. package/dist/effects.d.ts +7 -4
  67. package/dist/effects.d.ts.map +1 -1
  68. package/dist/effects.js +9 -4
  69. package/dist/effects.js.map +1 -1
  70. package/dist/empty-values.js +102 -0
  71. package/dist/empty-values.js.map +1 -0
  72. package/dist/fields/components.d.ts +9 -0
  73. package/dist/fields/components.d.ts.map +1 -0
  74. package/dist/fields/components.js +24 -0
  75. package/dist/fields/components.js.map +1 -0
  76. package/dist/fields/index.d.ts +3 -2
  77. package/dist/fields/index.js +3 -2
  78. package/dist/fields/layout.d.ts +3 -1
  79. package/dist/fields/layout.d.ts.map +1 -1
  80. package/dist/fields/layout.js +10 -1
  81. package/dist/fields/layout.js.map +1 -1
  82. package/dist/graph.d.ts +12 -1
  83. package/dist/graph.d.ts.map +1 -1
  84. package/dist/graph.js +39 -7
  85. package/dist/graph.js.map +1 -1
  86. package/dist/grid-values.d.ts +10 -0
  87. package/dist/grid-values.d.ts.map +1 -0
  88. package/dist/grid-values.js +53 -0
  89. package/dist/grid-values.js.map +1 -0
  90. package/dist/hit-test/drop.d.ts +20 -0
  91. package/dist/hit-test/drop.d.ts.map +1 -0
  92. package/dist/hit-test/drop.js +54 -0
  93. package/dist/hit-test/drop.js.map +1 -0
  94. package/dist/hit-test/geometry.js +86 -0
  95. package/dist/hit-test/geometry.js.map +1 -0
  96. package/dist/hit-test/masks.d.ts +14 -0
  97. package/dist/hit-test/masks.d.ts.map +1 -0
  98. package/dist/hit-test/masks.js +41 -0
  99. package/dist/hit-test/masks.js.map +1 -0
  100. package/dist/hit-test/paint.d.ts +6 -0
  101. package/dist/hit-test/paint.d.ts.map +1 -0
  102. package/dist/hit-test/paint.js +19 -0
  103. package/dist/hit-test/paint.js.map +1 -0
  104. package/dist/hit-test.d.ts +4 -15
  105. package/dist/hit-test.d.ts.map +1 -1
  106. package/dist/hit-test.js +5 -173
  107. package/dist/hit-test.js.map +1 -1
  108. package/dist/images.d.ts +3 -2
  109. package/dist/images.d.ts.map +1 -1
  110. package/dist/images.js +18 -2
  111. package/dist/images.js.map +1 -1
  112. package/dist/index.d.ts +2 -2
  113. package/dist/instances/copies.js +2 -1
  114. package/dist/instances/copies.js.map +1 -1
  115. package/dist/instances/fields.d.ts.map +1 -1
  116. package/dist/instances/fields.js +2 -0
  117. package/dist/instances/fields.js.map +1 -1
  118. package/dist/instances/index.d.ts +2 -1
  119. package/dist/instances/index.d.ts.map +1 -1
  120. package/dist/instances/index.js +13 -12
  121. package/dist/instances/index.js.map +1 -1
  122. package/dist/instances/overrides.d.ts +15 -1
  123. package/dist/instances/overrides.d.ts.map +1 -1
  124. package/dist/instances/overrides.js +37 -1
  125. package/dist/instances/overrides.js.map +1 -1
  126. package/dist/instances/source-walk.d.ts +12 -0
  127. package/dist/instances/source-walk.d.ts.map +1 -0
  128. package/dist/instances/swap-overrides.js +3 -3
  129. package/dist/instances/swap-overrides.js.map +1 -1
  130. package/dist/instances/sync.js +9 -7
  131. package/dist/instances/sync.js.map +1 -1
  132. package/dist/node-defaults.d.ts.map +1 -1
  133. package/dist/node-defaults.js +43 -38
  134. package/dist/node-defaults.js.map +1 -1
  135. package/dist/plugin-data/fields.d.ts +90 -0
  136. package/dist/plugin-data/fields.d.ts.map +1 -1
  137. package/dist/plugin-data/fields.js +33 -24
  138. package/dist/plugin-data/fields.js.map +1 -1
  139. package/dist/plugin-data/index.d.ts +2 -2
  140. package/dist/plugin-data/index.js +2 -2
  141. package/dist/plugin-data/schema.d.ts +6 -1
  142. package/dist/plugin-data/schema.d.ts.map +1 -1
  143. package/dist/plugin-data/schema.js +24 -1
  144. package/dist/plugin-data/schema.js.map +1 -1
  145. package/dist/preview.d.ts.map +1 -1
  146. package/dist/preview.js +2 -0
  147. package/dist/preview.js.map +1 -1
  148. package/dist/scaling/node.d.ts.map +1 -1
  149. package/dist/scaling/node.js +5 -1
  150. package/dist/scaling/node.js.map +1 -1
  151. package/dist/shaders/animation.d.ts +21 -0
  152. package/dist/shaders/animation.d.ts.map +1 -0
  153. package/dist/shaders/animation.js +25 -0
  154. package/dist/shaders/animation.js.map +1 -0
  155. package/dist/shaders/index.d.ts +2 -1
  156. package/dist/shaders/index.js +2 -1
  157. package/dist/slots/content.d.ts.map +1 -1
  158. package/dist/slots/content.js +4 -4
  159. package/dist/slots/content.js.map +1 -1
  160. package/dist/statuses/index.d.ts +2 -2
  161. package/dist/statuses/index.js +2 -2
  162. package/dist/statuses/workflow.d.ts +3 -1
  163. package/dist/statuses/workflow.d.ts.map +1 -1
  164. package/dist/statuses/workflow.js +6 -2
  165. package/dist/statuses/workflow.js.map +1 -1
  166. package/dist/token-sources/document.d.ts +26 -0
  167. package/dist/token-sources/document.d.ts.map +1 -0
  168. package/dist/token-sources/document.js +80 -0
  169. package/dist/token-sources/document.js.map +1 -0
  170. package/dist/token-sources/index.d.ts +3 -0
  171. package/dist/token-sources/index.js +3 -0
  172. package/dist/token-sources/schema.d.ts +43 -0
  173. package/dist/token-sources/schema.d.ts.map +1 -0
  174. package/dist/token-sources/schema.js +50 -0
  175. package/dist/token-sources/schema.js.map +1 -0
  176. package/dist/transfer/variables.d.ts.map +1 -1
  177. package/dist/transfer/variables.js +1 -1
  178. package/dist/transfer/variables.js.map +1 -1
  179. package/dist/types.d.ts +135 -15
  180. package/dist/types.d.ts.map +1 -1
  181. package/dist/types.js.map +1 -1
  182. package/dist/variables/bindings.d.ts +6 -5
  183. package/dist/variables/bindings.d.ts.map +1 -1
  184. package/dist/variables/bindings.js +24 -16
  185. package/dist/variables/bindings.js.map +1 -1
  186. package/dist/variables/formula/index.d.ts.map +1 -1
  187. package/dist/variables/formula/index.js +12 -6
  188. package/dist/variables/formula/index.js.map +1 -1
  189. package/dist/variables/index.d.ts +14 -1
  190. package/dist/variables/index.d.ts.map +1 -1
  191. package/dist/variables/index.js +38 -10
  192. package/dist/variables/index.js.map +1 -1
  193. package/dist/variables/paints.d.ts +9 -8
  194. package/dist/variables/paints.d.ts.map +1 -1
  195. package/dist/variables/paints.js +35 -26
  196. package/dist/variables/paints.js.map +1 -1
  197. package/dist/variables/resolver.d.ts +45 -0
  198. package/dist/variables/resolver.d.ts.map +1 -0
  199. package/dist/variables/resolver.js +106 -0
  200. package/dist/variables/resolver.js.map +1 -0
  201. package/dist/variations/copy.d.ts +16 -0
  202. package/dist/variations/copy.d.ts.map +1 -0
  203. package/dist/variations/copy.js +78 -0
  204. package/dist/variations/copy.js.map +1 -0
  205. package/dist/variations/index.d.ts +6 -0
  206. package/dist/variations/index.js +6 -0
  207. package/dist/variations/match.d.ts +23 -0
  208. package/dist/variations/match.d.ts.map +1 -0
  209. package/dist/variations/match.js +80 -0
  210. package/dist/variations/match.js.map +1 -0
  211. package/dist/variations/model.d.ts +39 -0
  212. package/dist/variations/model.d.ts.map +1 -0
  213. package/dist/variations/model.js +90 -0
  214. package/dist/variations/model.js.map +1 -0
  215. package/dist/variations/pick.d.ts +43 -0
  216. package/dist/variations/pick.d.ts.map +1 -0
  217. package/dist/variations/pick.js +211 -0
  218. package/dist/variations/pick.js.map +1 -0
  219. package/dist/variations/schema.d.ts +27 -0
  220. package/dist/variations/schema.d.ts.map +1 -0
  221. package/dist/variations/schema.js +27 -0
  222. package/dist/variations/schema.js.map +1 -0
  223. package/package.json +21 -1
  224. package/src/color/index.ts +43 -9
  225. package/src/color/management.ts +54 -1
  226. package/src/color/profile.ts +131 -0
  227. package/src/comments/merge.ts +3 -0
  228. package/src/comments/schema.ts +32 -2
  229. package/src/comments/threads.ts +5 -2
  230. package/src/components/properties.ts +13 -7
  231. package/src/components/variants.ts +39 -0
  232. package/src/constants.ts +75 -0
  233. package/src/copy.ts +20 -1
  234. package/src/css/index.ts +7 -1
  235. package/src/css/shadows.ts +8 -5
  236. package/src/css/text.ts +62 -0
  237. package/src/css/values.ts +12 -5
  238. package/src/design-memory/index.ts +2 -0
  239. package/src/design-memory/model.ts +143 -0
  240. package/src/design-memory/schema.ts +84 -0
  241. package/src/effects.ts +16 -7
  242. package/src/empty-values.ts +117 -0
  243. package/src/fields/components.ts +21 -0
  244. package/src/fields/index.ts +1 -0
  245. package/src/fields/layout.ts +10 -0
  246. package/src/graph.ts +50 -6
  247. package/src/grid-values.ts +47 -0
  248. package/src/hit-test/drop.ts +90 -0
  249. package/src/hit-test/geometry.ts +131 -0
  250. package/src/hit-test/masks.ts +57 -0
  251. package/src/hit-test/paint.ts +24 -0
  252. package/src/hit-test.ts +8 -277
  253. package/src/images.ts +27 -3
  254. package/src/instances/copies.ts +2 -1
  255. package/src/instances/fields.ts +2 -0
  256. package/src/instances/index.ts +20 -11
  257. package/src/instances/overrides.ts +49 -0
  258. package/src/instances/swap-overrides.ts +3 -2
  259. package/src/instances/sync.ts +11 -7
  260. package/src/node-defaults.ts +51 -41
  261. package/src/plugin-data/fields.ts +34 -32
  262. package/src/plugin-data/index.ts +1 -0
  263. package/src/plugin-data/schema.ts +30 -0
  264. package/src/preview.ts +2 -0
  265. package/src/scaling/node.ts +7 -2
  266. package/src/shaders/animation.ts +39 -0
  267. package/src/shaders/index.ts +1 -0
  268. package/src/slots/content.ts +8 -4
  269. package/src/statuses/workflow.ts +6 -4
  270. package/src/token-sources/document.ts +104 -0
  271. package/src/token-sources/index.ts +2 -0
  272. package/src/token-sources/schema.ts +59 -0
  273. package/src/transfer/variables.ts +2 -1
  274. package/src/types.ts +145 -14
  275. package/src/variables/bindings.ts +35 -18
  276. package/src/variables/formula/index.ts +19 -18
  277. package/src/variables/index.ts +49 -10
  278. package/src/variables/paints.ts +64 -29
  279. package/src/variables/resolver.ts +178 -0
  280. package/src/variations/copy.ts +89 -0
  281. package/src/variations/index.ts +5 -0
  282. package/src/variations/match.ts +100 -0
  283. package/src/variations/model.ts +132 -0
  284. package/src/variations/pick.ts +293 -0
  285. package/src/variations/schema.ts +29 -0
@@ -6,6 +6,7 @@ import type { SceneGraph } from '../graph'
6
6
  import { findInstanceAncestor } from '../instances'
7
7
  import {
8
8
  bindingOverrideField,
9
+ ownInstanceOverrides,
9
10
  setInstanceOverride,
10
11
  variableModeOverrideField
11
12
  } from '../instances/overrides'
@@ -39,6 +40,33 @@ import { applyBoundPaintColor } from './paints'
39
40
  import { pinnedRunBindingScales } from './units'
40
41
 
41
42
  export * from './resolution'
43
+ export * from './resolver'
44
+
45
+ /**
46
+ * Variables a list shows: every one but those deleted, on their own or with their collection,
47
+ * which only their bindings still reach.
48
+ */
49
+ export function liveVariables(graph: SceneGraph): Variable[] {
50
+ return [...graph.variables.values()].filter(
51
+ (variable) =>
52
+ !variable.deleted && !graph.variableCollections.get(variable.collectionId)?.deleted
53
+ )
54
+ }
55
+
56
+ /** Collections a list shows: every one but those deleted. */
57
+ export function liveCollections(graph: SceneGraph): VariableCollection[] {
58
+ return [...graph.variableCollections.values()].filter((collection) => !collection.deleted)
59
+ }
60
+
61
+ /**
62
+ * A collection's deleted variables, which its `variableIds` leave out. A deleted collection's
63
+ * variables are all deleted.
64
+ */
65
+ export function deletedVariablesOf(graph: SceneGraph, collectionId: string): Variable[] {
66
+ return [...graph.variables.values()].filter(
67
+ (variable) => variable.collectionId === collectionId && variable.deleted
68
+ )
69
+ }
42
70
 
43
71
  /**
44
72
  * Gives the runs that bind `fields` the scale they resolve with as their own, before the layer's
@@ -57,9 +85,12 @@ function pinRunBindingScales(
57
85
  export function addVariable(graph: SceneGraph, variable: Variable): void {
58
86
  graph.variables.set(variable.id, variable)
59
87
  const collection = graph.variableCollections.get(variable.collectionId)
60
- if (collection && !collection.variableIds.includes(variable.id)) {
61
- collection.variableIds.push(variable.id)
62
- }
88
+ if (!collection) return
89
+ // A collection lists its live variables, so one replaced by its deleted record leaves the list.
90
+ if (collection.variableIds.includes(variable.id) === !variable.deleted) return
91
+ collection.variableIds = variable.deleted
92
+ ? collection.variableIds.filter((id) => id !== variable.id)
93
+ : [...collection.variableIds, variable.id]
63
94
  }
64
95
 
65
96
  export function removeVariable(graph: SceneGraph, id: string): void {
@@ -160,6 +191,7 @@ export function removeCollection(graph: SceneGraph, id: string): void {
160
191
  for (const varId of Array.from(collection.variableIds)) {
161
192
  removeVariable(graph, varId)
162
193
  }
194
+ for (const variable of deletedVariablesOf(graph, id)) removeVariable(graph, variable.id)
163
195
  }
164
196
  graph.variableCollections.delete(id)
165
197
  graph.activeMode.delete(id)
@@ -231,9 +263,15 @@ export function removeMode(graph: SceneGraph, collectionId: string, modeId: stri
231
263
  const variableModes = omit(node.variableModes, [collectionId])
232
264
  graph.willChangeNode(node)
233
265
  node.variableModes = variableModes
234
- node.instanceOverrides.self.delete(overrideField)
235
- for (const overrides of node.instanceOverrides.descendants.values())
236
- overrides.delete(overrideField)
266
+ const state = node.instanceOverrides
267
+ const claimsMode =
268
+ state.self.has(overrideField) ||
269
+ [...state.descendants.values()].some((fields) => fields.has(overrideField))
270
+ if (claimsMode) {
271
+ const owned = ownInstanceOverrides(node)
272
+ owned.self.delete(overrideField)
273
+ for (const overrides of owned.descendants.values()) overrides.delete(overrideField)
274
+ }
237
275
  graph.emitter.emit('node:updated', node.id, { variableModes })
238
276
  }
239
277
  if (graph.activeMode.get(collectionId) === modeId) {
@@ -301,7 +339,7 @@ export function getVariablesForCollection(graph: SceneGraph, collectionId: strin
301
339
  }
302
340
 
303
341
  export function getVariablesByType(graph: SceneGraph, type: VariableType): Variable[] {
304
- return [...graph.variables.values()].filter((v) => v.type === type)
342
+ return liveVariables(graph).filter((v) => v.type === type)
305
343
  }
306
344
 
307
345
  const PAINT_BINDING_LISTS = ['fills', 'strokes'] as const satisfies readonly PaintBindingList[]
@@ -475,14 +513,15 @@ function markBoundVariablesOverrideOnInstance(
475
513
  const nearest = findInstanceAncestor(graph, nodeId)
476
514
  const instances = uniq(compact([nearest, owner])).filter((x) => x.id !== nodeId)
477
515
  for (const instance of instances) graph.willChangeNode(instance)
478
- if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, 'boundVariables')
479
- setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, 'boundVariables')
516
+ if (nearest)
517
+ setInstanceOverride(ownInstanceOverrides(nearest), nearest.id, nodeId, 'boundVariables')
518
+ setInstanceOverride(ownInstanceOverrides(owner), owner.id, nodeId, 'boundVariables')
480
519
  // Each instance claims the binding it changed alone, so the layer's other bindings keep
481
520
  // following their components, as Figma claims only the paint or effect a script edits.
482
521
  if (field)
483
522
  for (const instance of instances)
484
523
  setInstanceOverride(
485
- instance.instanceOverrides,
524
+ ownInstanceOverrides(instance),
486
525
  instance.id,
487
526
  nodeId,
488
527
  bindingOverrideField(field),
@@ -6,7 +6,7 @@ import { hasInstanceOverride } from '../instances/overrides'
6
6
  import type { Color } from '../primitives'
7
7
  import type { Fill, SceneNode, Stroke } from '../types'
8
8
  import { paintBindingTarget, type PaintBindingList } from './fields'
9
- import { collectionsResolving, getNodeExplicitVariableModeId } from './resolution'
9
+ import { createVariableResolver, type VariableResolver } from './resolver'
10
10
  import { subtreeNodes } from './subtree'
11
11
 
12
12
  /**
@@ -85,11 +85,13 @@ function sameColor(a: Color, b: Color): boolean {
85
85
  function explicitModes(
86
86
  graph: SceneGraph,
87
87
  node: SceneNode,
88
- variableId: string
88
+ variableId: string,
89
+ resolver?: VariableResolver
89
90
  ): Record<string, string | null> {
90
91
  const modes: Record<string, string | null> = {}
91
- for (const collectionId of collectionsResolving(graph, [variableId]))
92
- modes[collectionId] = getNodeExplicitVariableModeId(graph, node.id, collectionId)
92
+ const shared = resolver ?? createVariableResolver(graph)
93
+ for (const collectionId of shared.dependencies([variableId]))
94
+ modes[collectionId] = shared.explicitModeFor(node.id, collectionId)
93
95
  return modes
94
96
  }
95
97
 
@@ -223,14 +225,20 @@ function writePaints(graph: SceneGraph, node: SceneNode, changes: PaintLists): v
223
225
  * Writes into each bound paint the color its variable gives the layer now, where that differs
224
226
  * from the color last applied, or wherever `forced` says, and records what each binding gives.
225
227
  */
226
- function refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) => boolean): void {
228
+ function refresh(
229
+ graph: SceneGraph,
230
+ node: SceneNode,
231
+ forced: (field: string) => boolean,
232
+ resolver?: VariableResolver
233
+ ): void {
227
234
  let fills: Fill[] | undefined
228
235
  let strokes: Stroke[] | undefined
236
+ const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id)
229
237
  for (const bound of boundPaints(node)) {
230
- const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
238
+ const color = reader.color(bound.variableId)
231
239
  const paint = node[bound.list].at(bound.index)
232
240
  if (!color || !paint) continue
233
- const modes = explicitModes(graph, node, bound.variableId)
241
+ const modes = explicitModes(graph, node, bound.variableId, resolver)
234
242
  const stale = staleApplied(node, bound, paint, color, modes)
235
243
  recordApplied(node, bound, color, modes)
236
244
  if ((!forced(bound.field) && !stale) || showsColor(paint, color)) continue
@@ -256,13 +264,22 @@ const ALL_FORCED = (): boolean => true
256
264
  * opacity, once that color differs from the one last applied, discarding edits made since, as
257
265
  * Figma refreshes them when a variable's value or a layer's mode changes.
258
266
  */
259
- export function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void {
260
- if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED)
267
+ export function refreshBoundPaints(
268
+ graph: SceneGraph,
269
+ node: SceneNode,
270
+ resolver?: VariableResolver
271
+ ): void {
272
+ if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED, resolver)
261
273
  }
262
274
 
263
275
  /** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */
264
- export function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field: string): void {
265
- if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field)
276
+ export function applyBoundPaintColor(
277
+ graph: SceneGraph,
278
+ node: SceneNode,
279
+ field: string,
280
+ resolver?: VariableResolver
281
+ ): void {
282
+ if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field, resolver)
266
283
  }
267
284
 
268
285
  /**
@@ -270,14 +287,24 @@ export function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field:
270
287
  * or a layer an instance sync adds is a copy, which shows its variables in its own context rather
271
288
  * than whatever its component's paints hold.
272
289
  */
273
- export function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void {
290
+ export function applyBoundPaintColorsIn(
291
+ graph: SceneGraph,
292
+ rootId: string,
293
+ resolver?: VariableResolver
294
+ ): void {
295
+ const shared = resolver ?? createVariableResolver(graph)
274
296
  for (const node of subtreeNodes(graph, [rootId]))
275
- if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED)
297
+ if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED, shared)
276
298
  }
277
299
 
278
300
  /** Refreshes the bound paints in this subtree whose variables now give their layers another color. */
279
- export function refreshBoundPaintsIn(graph: SceneGraph, rootId: string): void {
280
- for (const node of subtreeNodes(graph, [rootId])) refreshBoundPaints(graph, node)
301
+ export function refreshBoundPaintsIn(
302
+ graph: SceneGraph,
303
+ rootId: string,
304
+ resolver?: VariableResolver
305
+ ): void {
306
+ const shared = resolver ?? createVariableResolver(graph)
307
+ for (const node of subtreeNodes(graph, [rootId])) refreshBoundPaints(graph, node, shared)
281
308
  }
282
309
 
283
310
  /**
@@ -287,12 +314,15 @@ export function refreshBoundPaintsIn(graph: SceneGraph, rootId: string): void {
287
314
  export function recordWrittenBoundPaints(
288
315
  graph: SceneGraph,
289
316
  node: SceneNode,
290
- written: PaintLists
317
+ written: PaintLists,
318
+ resolver?: VariableResolver
291
319
  ): void {
320
+ const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id)
292
321
  for (const bound of boundPaints(node)) {
293
322
  if (!written[bound.list]) continue
294
- const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
295
- if (color) recordApplied(node, bound, color, explicitModes(graph, node, bound.variableId))
323
+ const color = reader.color(bound.variableId)
324
+ if (color)
325
+ recordApplied(node, bound, color, explicitModes(graph, node, bound.variableId, resolver))
296
326
  }
297
327
  }
298
328
 
@@ -304,12 +334,14 @@ export function recordWrittenBoundPaints(
304
334
  */
305
335
  export function shownPaintBindings(
306
336
  graph: SceneGraph,
307
- node: SceneNode
337
+ node: SceneNode,
338
+ resolver?: VariableResolver
308
339
  ): Pick<Partial<SceneNode>, 'fills' | 'strokes'> {
309
340
  const changes: Pick<Partial<SceneNode>, 'fills' | 'strokes'> = {}
341
+ const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id)
310
342
  for (const bound of boundPaints(node)) {
311
343
  if (paintColorApplied(node, bound.field)) continue
312
- const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
344
+ const color = reader.color(bound.variableId)
313
345
  const paint = node[bound.list].at(bound.index)
314
346
  if (!color || !paint || showsColor(paint, color)) continue
315
347
  if (bound.list === 'fills') {
@@ -332,18 +364,18 @@ export function shownPaintBindings(
332
364
  export function refreshMovedBoundPaints(
333
365
  graph: SceneGraph,
334
366
  nodeId: string,
335
- previousParentId: string | null | undefined
367
+ previousParentId: string | null | undefined,
368
+ resolver?: VariableResolver
336
369
  ): void {
337
370
  if (graph.isApplyingImportedState || graph.variableCollections.size === 0) return
338
371
  const parentId = graph.nodes.get(nodeId)?.parentId
339
372
  if (!parentId) return
340
373
  const previous = previousParentId && graph.nodes.has(previousParentId) ? previousParentId : null
374
+ const shared = resolver ?? createVariableResolver(graph)
341
375
  const changed = new Set(
342
376
  [...graph.variableCollections.keys()].filter(
343
377
  (id) =>
344
- !previous ||
345
- getNodeExplicitVariableModeId(graph, previous, id) !==
346
- getNodeExplicitVariableModeId(graph, parentId, id)
378
+ !previous || shared.explicitModeFor(previous, id) !== shared.explicitModeFor(parentId, id)
347
379
  )
348
380
  )
349
381
  if (changed.size === 0) return
@@ -351,14 +383,14 @@ export function refreshMovedBoundPaints(
351
383
  const reachesChanged = (variableId: string) => {
352
384
  let found = reaches.get(variableId)
353
385
  if (found === undefined) {
354
- found = [...collectionsResolving(graph, [variableId])].some((id) => changed.has(id))
386
+ found = [...shared.dependencies([variableId])].some((id) => changed.has(id))
355
387
  reaches.set(variableId, found)
356
388
  }
357
389
  return found
358
390
  }
359
391
  for (const node of subtreeNodes(graph, [nodeId])) {
360
392
  if (boundPaints(node).some((bound) => reachesChanged(bound.variableId)))
361
- refresh(graph, node, NONE_FORCED)
393
+ refresh(graph, node, NONE_FORCED, shared)
362
394
  }
363
395
  }
364
396
 
@@ -383,9 +415,12 @@ function reboundPaint(
383
415
  export function keepCopyBoundPaints(
384
416
  graph: SceneGraph,
385
417
  target: SceneNode,
386
- updates: Partial<SceneNode>
418
+ updates: Partial<SceneNode>,
419
+ resolver?: VariableResolver
387
420
  ): void {
388
421
  const bindings = updates.boundVariables ?? target.boundVariables
422
+ const shared = resolver ?? createVariableResolver(graph)
423
+ const reader = shared.forNode(target.id)
389
424
  for (const list of PAINT_LISTS) {
390
425
  // A paint the component binds anew keeps its own paint otherwise, but takes the new color.
391
426
  if (!updates[list] && reboundPaint(target, bindings, list))
@@ -403,9 +438,9 @@ export function keepCopyBoundPaints(
403
438
  paint.color = { ...own.color }
404
439
  paint.opacity = own.opacity
405
440
  }
406
- const color = graph.resolveColorVariableForNode(target.id, variableId)
441
+ const color = reader.color(variableId)
407
442
  if (!color) return
408
- const modes = explicitModes(graph, target, variableId)
443
+ const modes = explicitModes(graph, target, variableId, shared)
409
444
  if (!keeps || staleApplied(target, bound, own, color, modes)) applyColor(paint, color)
410
445
  recordApplied(target, bound, color, modes)
411
446
  })
@@ -0,0 +1,178 @@
1
+ import type { SceneGraph } from '../graph'
2
+ import type { Color } from '../primitives'
3
+ import type { VariableLiteral, VariableValue } from '../types'
4
+ import {
5
+ collectionsResolving,
6
+ getActiveModeId,
7
+ getNodeExplicitVariableModeId,
8
+ getNodeVariableModeId,
9
+ resolveVariableInModes,
10
+ resolvedVariableModes,
11
+ type VariableModeFallback
12
+ } from './resolution'
13
+
14
+ /** One reader per mode context; completed values are shared inside the resolver. */
15
+ export interface VariableReader {
16
+ /** Any variable type, following aliases and expressions. Undefined when it cannot resolve. */
17
+ value(variableId: string): VariableLiteral | undefined
18
+ /** Strict form for formula drafts and diagnostics; throws VariableResolutionError. */
19
+ resolve(variableId: string): VariableLiteral
20
+ /** Stored value in this context's mode, with no default fallback; aliases are followed. */
21
+ stored(variableId: string): VariableValue | undefined
22
+
23
+ color(variableId: string): Color | undefined
24
+ number(variableId: string): number | undefined
25
+ string(variableId: string): string | undefined
26
+ boolean(variableId: string): boolean | undefined
27
+ }
28
+
29
+ export interface VariableResolver {
30
+ forNode(nodeId: string, fallback?: VariableModeFallback): VariableReader
31
+ active(): VariableReader
32
+ /**
33
+ * The collection's `modeId`, with every other collection in its active mode. A mode id from
34
+ * another collection is not an explicit mode here, so the collection keeps its active one.
35
+ */
36
+ forMode(collectionId: string, modeId: string): VariableReader
37
+ forModes(modeFor: (collectionId: string) => string): VariableReader
38
+
39
+ activeMode(collectionId: string): string
40
+ modeFor(nodeId: string, collectionId: string, fallback?: VariableModeFallback): string
41
+ explicitModeFor(nodeId: string, collectionId: string): string | null
42
+ modesFor(nodeId: string): Record<string, string>
43
+ dependencies(variableIds: Iterable<string>): Set<string>
44
+ }
45
+
46
+ export interface VariableResolverOptions {
47
+ /** Draft values for formula validation; stored reads still use the graph. */
48
+ overrides?: ReadonlyMap<string, VariableValue>
49
+ }
50
+
51
+ function isColor(value: VariableLiteral): value is Color {
52
+ return typeof value === 'object' && 'r' in value
53
+ }
54
+
55
+ /** Whether `modeId` names one of the collection's own modes. */
56
+ function collectionHasMode(graph: SceneGraph, collectionId: string, modeId: string): boolean {
57
+ const collection = graph.variableCollections.get(collectionId)
58
+ return collection?.modes.some((mode) => mode.modeId === modeId) ?? false
59
+ }
60
+
61
+ function reader(
62
+ graph: SceneGraph,
63
+ modeFor: (collectionId: string) => string,
64
+ overrides?: ReadonlyMap<string, VariableValue>
65
+ ): VariableReader {
66
+ const memo = new Map<string, VariableLiteral>()
67
+ const storedValues = new Map<string, VariableValue | undefined>()
68
+
69
+ const strict = (variableId: string) =>
70
+ resolveVariableInModes(graph, variableId, modeFor, new Set(), memo, overrides)
71
+
72
+ const value = (variableId: string): VariableLiteral | undefined => {
73
+ try {
74
+ return strict(variableId)
75
+ } catch {
76
+ return undefined
77
+ }
78
+ }
79
+
80
+ const stored = (variableId: string): VariableValue | undefined => {
81
+ if (storedValues.has(variableId)) return storedValues.get(variableId)
82
+ const visiting = new Set<string>()
83
+ let id = variableId
84
+ for (;;) {
85
+ if (visiting.has(id)) return undefined
86
+ visiting.add(id)
87
+ const variable = graph.variables.get(id)
88
+ if (!variable || !graph.variableCollections.has(variable.collectionId)) return undefined
89
+ const mode = modeFor(variable.collectionId)
90
+ const valueInMode = variable.valuesByMode[mode]
91
+ if (!Object.hasOwn(variable.valuesByMode, mode)) return undefined
92
+ if (valueInMode && typeof valueInMode === 'object' && 'aliasId' in valueInMode) {
93
+ id = valueInMode.aliasId
94
+ continue
95
+ }
96
+ storedValues.set(variableId, valueInMode)
97
+ return valueInMode
98
+ }
99
+ }
100
+
101
+ return {
102
+ value,
103
+ resolve: strict,
104
+ stored,
105
+ color: (variableId) => {
106
+ const resolved = value(variableId)
107
+ return resolved !== undefined && isColor(resolved) ? resolved : undefined
108
+ },
109
+ number: (variableId) => {
110
+ const resolved = value(variableId)
111
+ return typeof resolved === 'number' ? resolved : undefined
112
+ },
113
+ string: (variableId) => {
114
+ const resolved = value(variableId)
115
+ return typeof resolved === 'string' ? resolved : undefined
116
+ },
117
+ boolean: (variableId) => {
118
+ const resolved = value(variableId)
119
+ return typeof resolved === 'boolean' ? resolved : undefined
120
+ }
121
+ }
122
+ }
123
+
124
+ /**
125
+ * One value resolver for canvas, editor, exports and tools. Create one per operation: the
126
+ * memo is only valid for the graph state it was built against.
127
+ */
128
+ export function createVariableResolver(
129
+ graph: SceneGraph,
130
+ options: VariableResolverOptions = {}
131
+ ): VariableResolver {
132
+ const readers = new Map<string, VariableReader>()
133
+ const overrides = options.overrides
134
+
135
+ const cached = (key: string, create: () => VariableReader): VariableReader => {
136
+ let found = readers.get(key)
137
+ if (!found) {
138
+ found = create()
139
+ readers.set(key, found)
140
+ }
141
+ return found
142
+ }
143
+
144
+ return {
145
+ forNode(nodeId, fallback = 'active') {
146
+ return cached(`node:${fallback}:${nodeId}`, () =>
147
+ reader(
148
+ graph,
149
+ (collectionId) => getNodeVariableModeId(graph, nodeId, collectionId, fallback),
150
+ overrides
151
+ )
152
+ )
153
+ },
154
+ active() {
155
+ return cached('active', () => reader(graph, (id) => getActiveModeId(graph, id), overrides))
156
+ },
157
+ forMode(collectionId, modeId) {
158
+ // A mode id is scoped to its collection, so one from another collection selects nothing:
159
+ // it means "no explicit mode" for this collection, not that collection's default mode.
160
+ const mode = collectionHasMode(graph, collectionId, modeId)
161
+ ? modeId
162
+ : getActiveModeId(graph, collectionId)
163
+ return cached(JSON.stringify(['mode', collectionId, mode]), () =>
164
+ reader(graph, (id) => (id === collectionId ? mode : getActiveModeId(graph, id)), overrides)
165
+ )
166
+ },
167
+ forModes(modeFor) {
168
+ return reader(graph, modeFor, overrides)
169
+ },
170
+ activeMode: (collectionId) => getActiveModeId(graph, collectionId),
171
+ modeFor: (nodeId, collectionId, fallback = 'active') =>
172
+ getNodeVariableModeId(graph, nodeId, collectionId, fallback),
173
+ explicitModeFor: (nodeId, collectionId) =>
174
+ getNodeExplicitVariableModeId(graph, nodeId, collectionId),
175
+ modesFor: (nodeId) => resolvedVariableModes(graph, nodeId),
176
+ dependencies: (variableIds) => collectionsResolving(graph, variableIds)
177
+ }
178
+ }
@@ -0,0 +1,89 @@
1
+ import { pick } from 'es-toolkit'
2
+
3
+ import { MAIN_COMPONENT_FIELDS } from '../fields'
4
+ import type { SceneGraph } from '../graph'
5
+ import { createDefaultNode } from '../node-defaults'
6
+ import { readPluginData, withPluginData } from '../plugin-data/field'
7
+ import { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'
8
+ import type { SceneNode } from '../types'
9
+
10
+ /** `node`'s plugin data marking it as the stand-in for `mainId`, or unmarked for null. */
11
+ export function withStandIn(node: Pick<SceneNode, 'pluginData'>, mainId: string | null) {
12
+ return withPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn, mainId ?? undefined)
13
+ }
14
+
15
+ /** What a copied main component or component set clears to become a plain frame. */
16
+ function plainFrameFields(): Partial<SceneNode> {
17
+ const frame = createDefaultNode(() => '', 'FRAME')
18
+ return { type: 'FRAME', ...pick(frame, MAIN_COMPONENT_FIELDS) }
19
+ }
20
+
21
+ /**
22
+ * A copied main component inside the copy shows the component it copied, as an instance in its
23
+ * place, so copies never add main components; one that cannot become an instance, such as one
24
+ * that would hold itself, stays a plain frame.
25
+ */
26
+ function instanceInPlace(graph: SceneGraph, component: SceneNode, copy: SceneNode, index: number) {
27
+ const parentId = copy.parentId
28
+ const instance = parentId
29
+ ? graph.createInstance(component.id, parentId, { name: copy.name, x: copy.x, y: copy.y })
30
+ : null
31
+ if (!instance || !parentId) {
32
+ graph.updateNode(copy.id, plainFrameFields())
33
+ return
34
+ }
35
+ graph.updateNode(instance.id, { pluginData: withStandIn(instance, component.id) })
36
+ graph.insertChildAt(instance.id, parentId, index)
37
+ graph.deleteNode(copy.id)
38
+ }
39
+
40
+ /**
41
+ * Copies `source` under `parentId` as a version: its root is never a main component or a
42
+ * component set (it becomes a plain frame), and the main components inside it are instances of
43
+ * the components they copy. Instances stay instances. Each layer standing in for a main
44
+ * component or set is marked with its id (`variationStandIn`), which is how a pick knows it. Right after a copy the two trees have the
45
+ * same shape, so each copied layer is found at its source's place.
46
+ */
47
+ export function copyVersion(
48
+ graph: SceneGraph,
49
+ sourceId: string,
50
+ parentId: string,
51
+ overrides: Partial<SceneNode>
52
+ ): SceneNode | null {
53
+ const copy = graph.cloneTree(sourceId, parentId, overrides)
54
+ if (!copy) return null
55
+ if (copy.type === 'COMPONENT' || copy.type === 'COMPONENT_SET') {
56
+ graph.updateNode(copy.id, plainFrameFields())
57
+ }
58
+ const pending: Array<[string, string]> = [[sourceId, copy.id]]
59
+ // Pairs are appended while the list is walked, parents before their children.
60
+ for (const [fromId, toId] of pending) {
61
+ const from = graph.getNode(fromId)
62
+ const to = graph.getNode(toId)
63
+ // An instance's layers are its component's copies, kept by sync.
64
+ if (!from || !to || from.type === 'INSTANCE') continue
65
+ const copied = [...to.childIds]
66
+ from.childIds.forEach((childId, index) => {
67
+ const child = graph.getNode(childId)
68
+ const childCopy = graph.getNode(copied[index] ?? '')
69
+ if (!child || !childCopy) return
70
+ // Copying a version copies its stand-ins as stand-ins, which a plain copy drops.
71
+ const mark = readPluginData(child.pluginData, PAIRDRAW_PLUGIN_DATA.variationStandIn)
72
+ if (mark !== undefined) {
73
+ graph.updateNode(childCopy.id, { pluginData: withStandIn(childCopy, mark) })
74
+ }
75
+ if (child.type === 'COMPONENT') {
76
+ instanceInPlace(graph, child, childCopy, index)
77
+ return
78
+ }
79
+ if (child.type === 'COMPONENT_SET') {
80
+ graph.updateNode(childCopy.id, {
81
+ ...plainFrameFields(),
82
+ pluginData: withStandIn(childCopy, child.id)
83
+ })
84
+ }
85
+ pending.push([childId, childCopy.id])
86
+ })
87
+ }
88
+ return graph.getNode(copy.id) ?? null
89
+ }
@@ -0,0 +1,5 @@
1
+ export * from './copy'
2
+ export * from './match'
3
+ export * from './model'
4
+ export * from './pick'
5
+ export * from './schema'