@open-pencil/scene-graph 0.17.0-beta.2 → 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 (244) hide show
  1. package/dist/asset-keys.d.ts +11 -1
  2. package/dist/asset-keys.d.ts.map +1 -1
  3. package/dist/asset-keys.js +16 -1
  4. package/dist/asset-keys.js.map +1 -1
  5. package/dist/checkpoint.d.ts +3 -3
  6. package/dist/checkpoint.d.ts.map +1 -1
  7. package/dist/checkpoint.js +53 -23
  8. package/dist/checkpoint.js.map +1 -1
  9. package/dist/components/index.d.ts +2 -2
  10. package/dist/components/index.js +2 -2
  11. package/dist/components/properties.d.ts +6 -1
  12. package/dist/components/properties.d.ts.map +1 -1
  13. package/dist/components/properties.js +9 -1
  14. package/dist/components/properties.js.map +1 -1
  15. package/dist/constants.d.ts +6 -1
  16. package/dist/constants.d.ts.map +1 -1
  17. package/dist/constants.js +17 -1
  18. package/dist/constants.js.map +1 -1
  19. package/dist/constraints.d.ts +25 -0
  20. package/dist/constraints.d.ts.map +1 -0
  21. package/dist/constraints.js +51 -0
  22. package/dist/constraints.js.map +1 -0
  23. package/dist/coordinate.d.ts +64 -1
  24. package/dist/coordinate.d.ts.map +1 -1
  25. package/dist/coordinate.js +217 -1
  26. package/dist/coordinate.js.map +1 -1
  27. package/dist/copy.d.ts.map +1 -1
  28. package/dist/copy.js +8 -1
  29. package/dist/copy.js.map +1 -1
  30. package/dist/css/index.d.ts +3 -2
  31. package/dist/css/index.js +3 -2
  32. package/dist/css/text.d.ts +28 -1
  33. package/dist/css/text.d.ts.map +1 -1
  34. package/dist/css/text.js +84 -17
  35. package/dist/css/text.js.map +1 -1
  36. package/dist/css/transform.d.ts +34 -0
  37. package/dist/css/transform.d.ts.map +1 -0
  38. package/dist/css/transform.js +320 -0
  39. package/dist/css/transform.js.map +1 -0
  40. package/dist/css/values.d.ts.map +1 -1
  41. package/dist/css/values.js +11 -1
  42. package/dist/css/values.js.map +1 -1
  43. package/dist/figjam/palette.d.ts +1 -1
  44. package/dist/figjam/palette.js +1 -1
  45. package/dist/figjam/palette.js.map +1 -1
  46. package/dist/geometry.d.ts +8 -1
  47. package/dist/geometry.d.ts.map +1 -1
  48. package/dist/geometry.js +19 -8
  49. package/dist/geometry.js.map +1 -1
  50. package/dist/graph.d.ts +48 -12
  51. package/dist/graph.d.ts.map +1 -1
  52. package/dist/graph.js +110 -24
  53. package/dist/graph.js.map +1 -1
  54. package/dist/group-bounds.d.ts +14 -4
  55. package/dist/group-bounds.d.ts.map +1 -1
  56. package/dist/group-bounds.js +76 -11
  57. package/dist/group-bounds.js.map +1 -1
  58. package/dist/hit-test.d.ts +5 -0
  59. package/dist/hit-test.d.ts.map +1 -1
  60. package/dist/hit-test.js +18 -12
  61. package/dist/hit-test.js.map +1 -1
  62. package/dist/images.d.ts +27 -3
  63. package/dist/images.d.ts.map +1 -1
  64. package/dist/images.js +57 -3
  65. package/dist/images.js.map +1 -1
  66. package/dist/index.d.ts +2 -2
  67. package/dist/instances/index.d.ts.map +1 -1
  68. package/dist/instances/index.js +3 -0
  69. package/dist/instances/index.js.map +1 -1
  70. package/dist/instances/placement.js +55 -12
  71. package/dist/instances/placement.js.map +1 -1
  72. package/dist/instances/swap.js +3 -0
  73. package/dist/instances/swap.js.map +1 -1
  74. package/dist/instances/sync.js +34 -13
  75. package/dist/instances/sync.js.map +1 -1
  76. package/dist/node-defaults.d.ts.map +1 -1
  77. package/dist/node-defaults.js +1 -0
  78. package/dist/node-defaults.js.map +1 -1
  79. package/dist/order-keys.d.ts +3 -1
  80. package/dist/order-keys.d.ts.map +1 -1
  81. package/dist/order-keys.js +6 -1
  82. package/dist/order-keys.js.map +1 -1
  83. package/dist/plugin-data/fields.d.ts +2 -0
  84. package/dist/plugin-data/fields.d.ts.map +1 -1
  85. package/dist/plugin-data/fields.js +2 -0
  86. package/dist/plugin-data/fields.js.map +1 -1
  87. package/dist/resize.d.ts +22 -11
  88. package/dist/resize.d.ts.map +1 -1
  89. package/dist/resize.js +227 -93
  90. package/dist/resize.js.map +1 -1
  91. package/dist/scaling/index.d.ts +3 -2
  92. package/dist/scaling/index.js +3 -2
  93. package/dist/scaling/node.d.ts.map +1 -1
  94. package/dist/scaling/node.js +5 -1
  95. package/dist/scaling/node.js.map +1 -1
  96. package/dist/scaling/snapshot.d.ts +25 -0
  97. package/dist/scaling/snapshot.d.ts.map +1 -0
  98. package/dist/scaling/snapshot.js +39 -0
  99. package/dist/scaling/snapshot.js.map +1 -0
  100. package/dist/scaling/tree.d.ts +24 -2
  101. package/dist/scaling/tree.d.ts.map +1 -1
  102. package/dist/scaling/tree.js +37 -14
  103. package/dist/scaling/tree.js.map +1 -1
  104. package/dist/shaders/model.d.ts.map +1 -1
  105. package/dist/shaders/model.js +2 -1
  106. package/dist/shaders/model.js.map +1 -1
  107. package/dist/shared-styles.d.ts +3 -1
  108. package/dist/shared-styles.d.ts.map +1 -1
  109. package/dist/shared-styles.js +7 -1
  110. package/dist/shared-styles.js.map +1 -1
  111. package/dist/slots/authoring.d.ts +3 -2
  112. package/dist/slots/authoring.d.ts.map +1 -1
  113. package/dist/slots/authoring.js +5 -3
  114. package/dist/slots/authoring.js.map +1 -1
  115. package/dist/slots/content.d.ts.map +1 -1
  116. package/dist/slots/content.js +4 -1
  117. package/dist/slots/content.js.map +1 -1
  118. package/dist/text/direction.d.ts +3 -1
  119. package/dist/text/direction.d.ts.map +1 -1
  120. package/dist/text/direction.js +5 -1
  121. package/dist/text/direction.js.map +1 -1
  122. package/dist/transfer/apply.d.ts.map +1 -1
  123. package/dist/transfer/apply.js +1 -6
  124. package/dist/transfer/apply.js.map +1 -1
  125. package/dist/transfer/copied.d.ts +35 -3
  126. package/dist/transfer/copied.d.ts.map +1 -1
  127. package/dist/transfer/copied.js +44 -13
  128. package/dist/transfer/copied.js.map +1 -1
  129. package/dist/transfer/dependencies.d.ts +38 -0
  130. package/dist/transfer/dependencies.d.ts.map +1 -0
  131. package/dist/transfer/dependencies.js +100 -0
  132. package/dist/transfer/dependencies.js.map +1 -0
  133. package/dist/transfer/history.js +0 -1
  134. package/dist/transfer/history.js.map +1 -1
  135. package/dist/transfer/index.d.ts +5 -3
  136. package/dist/transfer/index.js +5 -3
  137. package/dist/transfer/nodes.d.ts.map +1 -1
  138. package/dist/transfer/nodes.js +7 -14
  139. package/dist/transfer/nodes.js.map +1 -1
  140. package/dist/transfer/plan.d.ts +0 -1
  141. package/dist/transfer/plan.js +3 -5
  142. package/dist/transfer/plan.js.map +1 -1
  143. package/dist/transfer/remove.js +5 -2
  144. package/dist/transfer/remove.js.map +1 -1
  145. package/dist/transfer/resources.d.ts +21 -0
  146. package/dist/transfer/resources.d.ts.map +1 -0
  147. package/dist/transfer/resources.js +38 -0
  148. package/dist/transfer/resources.js.map +1 -0
  149. package/dist/transfer/variables.d.ts +8 -1
  150. package/dist/transfer/variables.d.ts.map +1 -1
  151. package/dist/transfer/variables.js +24 -1
  152. package/dist/transfer/variables.js.map +1 -1
  153. package/dist/types.d.ts +47 -2
  154. package/dist/types.d.ts.map +1 -1
  155. package/dist/types.js.map +1 -1
  156. package/dist/undo.d.ts +28 -3
  157. package/dist/undo.d.ts.map +1 -1
  158. package/dist/undo.js +24 -7
  159. package/dist/undo.js.map +1 -1
  160. package/dist/variables/bindings.d.ts +17 -14
  161. package/dist/variables/bindings.d.ts.map +1 -1
  162. package/dist/variables/bindings.js +30 -72
  163. package/dist/variables/bindings.js.map +1 -1
  164. package/dist/variables/fields.d.ts +1 -1
  165. package/dist/variables/fields.js +1 -1
  166. package/dist/variables/fields.js.map +1 -1
  167. package/dist/variables/formula/check.d.ts +1 -1
  168. package/dist/variables/formula/index.d.ts +2 -2
  169. package/dist/variables/formula/index.js +1 -1
  170. package/dist/variables/formula/index.js.map +1 -1
  171. package/dist/variables/formula/print.d.ts +1 -1
  172. package/dist/variables/formula/signatures.js +1 -1
  173. package/dist/variables/index.d.ts +6 -6
  174. package/dist/variables/index.d.ts.map +1 -1
  175. package/dist/variables/index.js +40 -24
  176. package/dist/variables/index.js.map +1 -1
  177. package/dist/variables/paints.d.ts +57 -0
  178. package/dist/variables/paints.d.ts.map +1 -0
  179. package/dist/variables/paints.js +231 -0
  180. package/dist/variables/paints.js.map +1 -0
  181. package/dist/variables/resolution.d.ts +14 -11
  182. package/dist/variables/resolution.d.ts.map +1 -1
  183. package/dist/variables/resolution.js +36 -17
  184. package/dist/variables/resolution.js.map +1 -1
  185. package/dist/variables/subtree.js +17 -0
  186. package/dist/variables/subtree.js.map +1 -0
  187. package/dist/vector-network.d.ts.map +1 -1
  188. package/dist/vector-network.js +2 -8
  189. package/dist/vector-network.js.map +1 -1
  190. package/package.json +8 -1
  191. package/src/asset-keys.ts +20 -0
  192. package/src/checkpoint.ts +87 -32
  193. package/src/components/properties.ts +9 -0
  194. package/src/constants.ts +17 -0
  195. package/src/constraints.ts +60 -0
  196. package/src/coordinate.ts +220 -0
  197. package/src/copy.ts +9 -4
  198. package/src/css/index.ts +17 -1
  199. package/src/css/text.ts +85 -17
  200. package/src/css/transform.ts +266 -0
  201. package/src/css/values.ts +8 -0
  202. package/src/figjam/palette.ts +1 -1
  203. package/src/geometry.ts +20 -9
  204. package/src/graph.ts +128 -42
  205. package/src/group-bounds.ts +84 -17
  206. package/src/hit-test.ts +30 -14
  207. package/src/images.ts +71 -3
  208. package/src/instances/index.ts +4 -0
  209. package/src/instances/placement.ts +63 -10
  210. package/src/instances/swap.ts +5 -0
  211. package/src/instances/sync.ts +48 -21
  212. package/src/node-defaults.ts +1 -0
  213. package/src/order-keys.ts +6 -0
  214. package/src/plugin-data/fields.ts +6 -0
  215. package/src/resize.ts +267 -119
  216. package/src/scaling/index.ts +9 -1
  217. package/src/scaling/node.ts +9 -1
  218. package/src/scaling/snapshot.ts +51 -0
  219. package/src/scaling/tree.ts +63 -19
  220. package/src/shaders/model.ts +2 -3
  221. package/src/shared-styles.ts +8 -0
  222. package/src/slots/authoring.ts +5 -3
  223. package/src/slots/content.ts +4 -1
  224. package/src/text/direction.ts +5 -0
  225. package/src/transfer/apply.ts +1 -13
  226. package/src/transfer/copied.ts +83 -15
  227. package/src/transfer/dependencies.ts +126 -0
  228. package/src/transfer/history.ts +0 -6
  229. package/src/transfer/index.ts +20 -2
  230. package/src/transfer/nodes.ts +10 -26
  231. package/src/transfer/plan.ts +3 -11
  232. package/src/transfer/remove.ts +5 -2
  233. package/src/transfer/resources.ts +54 -0
  234. package/src/transfer/variables.ts +34 -0
  235. package/src/types.ts +55 -1
  236. package/src/undo.ts +48 -10
  237. package/src/variables/bindings.ts +44 -91
  238. package/src/variables/fields.ts +1 -1
  239. package/src/variables/formula/index.ts +3 -1
  240. package/src/variables/index.ts +56 -38
  241. package/src/variables/paints.ts +296 -0
  242. package/src/variables/resolution.ts +39 -28
  243. package/src/variables/subtree.ts +15 -0
  244. package/src/vector-network.ts +4 -2
package/src/types.ts CHANGED
@@ -34,12 +34,26 @@ export type SceneGraphEventHandlers = Partial<{
34
34
  ) => void
35
35
  }>
36
36
 
37
+ /** The document's records besides its nodes, which edits report changing as they do nodes. */
38
+ export type DocumentRecordKind = 'variable' | 'collection'
39
+
37
40
  /** Hears about nodes as an edit adds and changes them, synchronously and before each change. */
38
41
  export interface NodeChangeObserver {
39
42
  /** A node is about to change and is still as it was. */
40
43
  before: (node: SceneNode) => void
41
44
  /** A node was just added. */
42
45
  created: (node: SceneNode) => void
46
+ /**
47
+ * A variable or collection is about to change, be added, or be removed, and is still as it
48
+ * was: absent, for one about to be added.
49
+ */
50
+ beforeRecord?: (kind: DocumentRecordKind, id: string) => void
51
+ /**
52
+ * The edit being observed waits on something outside it (`SceneGraph.waitOutsideEdit`): until
53
+ * `resume`, nodes change for someone else's edit, not this one.
54
+ */
55
+ pause?: () => void
56
+ resume?: () => void
43
57
  }
44
58
 
45
59
  export type DocumentColorSpace = 'srgb' | 'display-p3'
@@ -243,8 +257,39 @@ export interface Stroke extends Fill {
243
257
  dashPattern?: number[]
244
258
  }
245
259
 
260
+ /**
261
+ * A Glass effect's lens, in Figma's plugin terms: how far in from the edge it bends what is
262
+ * behind the layer (`depth`, 1 to 100 px) and how strongly (`refraction`, 0 to 1), the light it
263
+ * catches (`lightAngle` in degrees, 0 lighting the top edge, and `lightIntensity`, 0 to 1), and how
264
+ * far the colours split (`dispersion`, 0 to 1). The effect's `radius` frosts the glass.
265
+ */
266
+ export interface GlassEffectParams {
267
+ refraction: number
268
+ depth: number
269
+ lightAngle: number
270
+ lightIntensity: number
271
+ dispersion: number
272
+ }
273
+
274
+ /**
275
+ * A progressive blur's ramp, in Figma's plugin terms: the blur grows from `startRadius` at
276
+ * `startOffset` to the effect's `radius` at `endOffset`, both points fractions of the layer's
277
+ * size, and holds each end's radius beyond it.
278
+ */
279
+ export interface ProgressiveBlur {
280
+ startRadius: number
281
+ startOffset: Vector
282
+ endOffset: Vector
283
+ }
284
+
246
285
  export interface Effect {
247
- type: 'DROP_SHADOW' | 'INNER_SHADOW' | 'LAYER_BLUR' | 'BACKGROUND_BLUR' | 'FOREGROUND_BLUR'
286
+ type:
287
+ | 'DROP_SHADOW'
288
+ | 'INNER_SHADOW'
289
+ | 'LAYER_BLUR'
290
+ | 'BACKGROUND_BLUR'
291
+ | 'FOREGROUND_BLUR'
292
+ | 'GLASS'
248
293
  color: Color
249
294
  offset: Vector
250
295
  radius: number
@@ -252,6 +297,10 @@ export interface Effect {
252
297
  visible: boolean
253
298
  blendMode?: BlendMode
254
299
  showShadowBehindNode?: boolean
300
+ /** Set on a `GLASS` effect, and only there. */
301
+ glass?: GlassEffectParams
302
+ /** Set on a layer or background blur that is progressive, and only there. */
303
+ progressive?: ProgressiveBlur
255
304
  }
256
305
 
257
306
  export type ConstraintType = 'MIN' | 'CENTER' | 'MAX' | 'STRETCH' | 'SCALE'
@@ -850,6 +899,11 @@ export interface SceneNode {
850
899
  sharedSymbolVersion: string | null
851
900
  publishedVersion: string | null
852
901
  librarySource: LibraryAssetSource | null
902
+ /**
903
+ * The layer of a library's source document that a library definition's layer copies, which pairs
904
+ * it with the same layer in the library's next revision whatever that layer is called by then.
905
+ */
906
+ libraryLayerId: string | null
853
907
  isPublishable: boolean
854
908
  isSymbolPublishable: boolean
855
909
  /**
package/src/undo.ts CHANGED
@@ -5,10 +5,29 @@ export interface UndoEntry {
5
5
  coalesceKey?: string
6
6
  }
7
7
 
8
+ /**
9
+ * Why committed history changed: a new step, a change joining the newest step, undo, redo, or
10
+ * clearing it.
11
+ */
12
+ export type HistoryChange = 'record' | 'join' | 'undo' | 'redo' | 'clear'
13
+
8
14
  export interface UndoManagerOptions {
9
15
  limit?: number
10
- /** Called after recording, undoing, redoing, or clearing committed history. */
11
- onChange?: () => void
16
+ /**
17
+ * Called after recording, joining, undoing, redoing, or clearing committed history, with which,
18
+ * and with the step recorded or joined for `'record'` and `'join'`.
19
+ */
20
+ onChange?: (change: HistoryChange, step?: UndoEntry) => void
21
+ /**
22
+ * Called when an edit ends without a step, its changes undone or amounting to nothing: a batch
23
+ * rolled back, or an edit reported with `discardEdit`. History is unchanged, so `onChange` is not.
24
+ */
25
+ onDiscard?: () => void
26
+ /**
27
+ * Runs an undo or a redo, which the editor runs outside edits in progress
28
+ * (`SceneGraph.runOutsideEdits`), so an agent's edit open meanwhile does not record it.
29
+ */
30
+ runReplay?: (replay: () => void) => void
12
31
  }
13
32
 
14
33
  interface UndoBatch {
@@ -28,11 +47,15 @@ export class UndoManager {
28
47
  private openEdits = 0
29
48
  private readonly settlers = new Set<() => void>()
30
49
  private readonly limit: number
31
- private readonly onChange: (() => void) | undefined
50
+ private readonly onChange: ((change: HistoryChange, step?: UndoEntry) => void) | undefined
51
+ private readonly onDiscard: (() => void) | undefined
52
+ private readonly runReplay: (replay: () => void) => void
32
53
 
33
54
  constructor(options: UndoManagerOptions = {}) {
34
55
  this.limit = options.limit ?? DEFAULT_HISTORY_LIMIT
35
56
  this.onChange = options.onChange
57
+ this.onDiscard = options.onDiscard
58
+ this.runReplay = options.runReplay ?? ((replay) => replay())
36
59
  }
37
60
 
38
61
  apply(entry: UndoEntry): void {
@@ -86,7 +109,7 @@ export class UndoManager {
86
109
  if (!entry) return null
87
110
  this.replay(() => entry.inverse())
88
111
  this.redoStack.push(entry)
89
- this.onChange?.()
112
+ this.onChange?.('undo')
90
113
  return entry.label
91
114
  }
92
115
 
@@ -96,7 +119,7 @@ export class UndoManager {
96
119
  if (!entry) return null
97
120
  this.replay(() => entry.forward())
98
121
  this.undoStack.push(entry)
99
- this.onChange?.()
122
+ this.onChange?.('redo')
100
123
  return entry.label
101
124
  }
102
125
 
@@ -127,9 +150,23 @@ export class UndoManager {
127
150
  }
128
151
 
129
152
  rollbackBatch(): void {
130
- const batch = this.batches.pop()
153
+ const batch = this.batches.at(-1)
131
154
  if (!batch) return
132
- for (const entry of batch.entries.toReversed()) entry.inverse()
155
+ // Still open while it rolls back, so what the rollback changes belongs to the batch.
156
+ try {
157
+ for (const entry of batch.entries.toReversed()) entry.inverse()
158
+ } finally {
159
+ this.batches.pop()
160
+ }
161
+ this.onDiscard?.()
162
+ }
163
+
164
+ /**
165
+ * Reports an edit that ended without a step, rolled back or changing nothing, as a rolled-back
166
+ * batch does. Called once the edit has closed, so `isRecording` tells whether another is open.
167
+ */
168
+ discardEdit(): void {
169
+ this.onDiscard?.()
133
170
  }
134
171
 
135
172
  /** Abandon provisional history without replaying it or changing committed undo/redo entries. */
@@ -141,7 +178,7 @@ export class UndoManager {
141
178
  this.undoStack = []
142
179
  this.redoStack = []
143
180
  this.discardBatches()
144
- this.onChange?.()
181
+ this.onChange?.('clear')
145
182
  }
146
183
 
147
184
  /** Whether undo or redo is replaying a step, whose changes are history rather than new edits. */
@@ -170,13 +207,14 @@ export class UndoManager {
170
207
  entry.inverse()
171
208
  inverse()
172
209
  }
210
+ this.onChange?.('join', last)
173
211
  return true
174
212
  }
175
213
 
176
214
  private replay(step: () => void) {
177
215
  this.replaying++
178
216
  try {
179
- step()
217
+ this.runReplay(step)
180
218
  } finally {
181
219
  this.replaying--
182
220
  }
@@ -274,7 +312,7 @@ export class UndoManager {
274
312
  }
275
313
  this.redoStack = []
276
314
  this.trimUndoStack()
277
- this.onChange?.()
315
+ this.onChange?.('record', this.undoStack.at(-1))
278
316
  }
279
317
 
280
318
  private trimUndoStack(): void {
@@ -1,20 +1,20 @@
1
1
  import { isEmptyObject, isEqual } from 'es-toolkit/predicate'
2
2
 
3
- import type { VariableModeFallback } from '.'
4
- import { copyFills, copyStrokes } from '../copy'
5
3
  import type { SceneGraph } from '../graph'
6
4
  import { hasInstanceOverride } from '../instances/overrides'
7
5
  import type { Color } from '../primitives'
8
6
  import { pixelLength } from '../text/lengths'
9
- import type { Effect, Fill, SceneNode, Stroke, StyleRun, TextLength } from '../types'
7
+ import type { Effect, SceneNode, StyleRun, TextLength } from '../types'
10
8
  import {
11
9
  effectBindableProperties,
12
10
  effectBindingTarget,
13
11
  NUMERIC_FIELDS,
14
12
  isNumericVariableBindingField,
15
- paintBindingTarget,
16
13
  type EffectBindingProperty
17
14
  } from './fields'
15
+ import { refreshBoundPaints } from './paints'
16
+ import { collectionsResolving } from './resolution'
17
+ import { subtreeNodes } from './subtree'
18
18
  import { variableText, variableTruthy } from './text'
19
19
  import { variableValueReferences } from './value'
20
20
  export {
@@ -74,17 +74,13 @@ export function boundNumberValue(field: string, value: number): number | TextLen
74
74
  * The values a node's numeric bindings resolve to in its mode, in scene units, for the fields
75
75
  * whose stored value differs. A width or height an instance overrides keeps its own value.
76
76
  */
77
- export function resolvedNumericBindings(
78
- graph: SceneGraph,
79
- node: SceneNode,
80
- fallback: VariableModeFallback = 'active'
81
- ): Partial<SceneNode> {
77
+ export function resolvedNumericBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode> {
82
78
  const updates: Partial<SceneNode> = {}
83
79
  for (const [field, variableId] of Object.entries(node.boundVariables)) {
84
80
  if (!NUMERIC_FIELDS.has(field)) continue
85
81
  if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))
86
82
  continue
87
- const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
83
+ const value = graph.resolveNumberVariableForNode(node.id, variableId)
88
84
  if (value === undefined || !Number.isFinite(value)) continue
89
85
  const scale = node.variableBindingScales[field] ?? 1
90
86
  const effective = value * scale
@@ -105,16 +101,12 @@ export function resolvedNumericBindings(
105
101
  * A bound font family is left as stored: changing it needs the font loaded, which resolving a
106
102
  * binding cannot do.
107
103
  */
108
- export function resolvedValueBindings(
109
- graph: SceneGraph,
110
- node: SceneNode,
111
- fallback: VariableModeFallback = 'active'
112
- ): Partial<SceneNode> {
104
+ export function resolvedValueBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode> {
113
105
  const updates: Partial<SceneNode> = {}
114
106
  const resolve = (field: 'text' | 'visible') => {
115
107
  const variableId = node.boundVariables[field]
116
108
  if (!variableId || overriddenInInstance(graph, node.id, field)) return undefined
117
- return graph.resolveVariableForNode(node.id, variableId, fallback)
109
+ return graph.resolveVariableForNode(node.id, variableId)
118
110
  }
119
111
  const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
120
112
  if (text !== undefined && text !== node.text) updates.text = text
@@ -133,8 +125,7 @@ const NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeig
133
125
  */
134
126
  export function resolvedRunBindings(
135
127
  graph: SceneGraph,
136
- node: SceneNode,
137
- fallback: VariableModeFallback = 'active'
128
+ node: SceneNode
138
129
  ): Pick<Partial<SceneNode>, 'styleRuns'> {
139
130
  const styleRuns = node.styleRuns.map((run): StyleRun => {
140
131
  const bound = run.style.boundVariables
@@ -143,7 +134,7 @@ export function resolvedRunBindings(
143
134
  for (const field of NUMERIC_RUN_FIELDS) {
144
135
  const variableId = bound[field]
145
136
  if (!variableId) continue
146
- const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
137
+ const value = graph.resolveNumberVariableForNode(node.id, variableId)
147
138
  if (value === undefined || !Number.isFinite(value)) continue
148
139
  const next = boundNumberValue(field, value)
149
140
  if (isEqual(style[field], next)) continue
@@ -163,43 +154,6 @@ function boundVariableIds(node: SceneNode): string[] {
163
154
  ].filter((id): id is string => typeof id === 'string')
164
155
  }
165
156
 
166
- /**
167
- * A bound paint takes its whole color from the variable, alpha included, the way Figma stores
168
- * it: the variable's RGB at full alpha, and its alpha as the paint's opacity.
169
- */
170
- function applyBoundColor(paint: Fill | Stroke, color: Color): void {
171
- paint.color = { ...color, a: 1 }
172
- paint.opacity = color.a
173
- }
174
-
175
- /** The fills and strokes a node's color bindings resolve to in its mode, when it has any. */
176
- export function resolvedPaintBindings(
177
- graph: SceneGraph,
178
- node: SceneNode,
179
- fallback: VariableModeFallback = 'active'
180
- ): Pick<Partial<SceneNode>, 'fills' | 'strokes'> {
181
- // Most nodes bind nothing, so no copy is made until a paint binding is actually found.
182
- const changes: Pick<Partial<SceneNode>, 'fills' | 'strokes'> = {}
183
- for (const field in node.boundVariables) {
184
- if (!field.endsWith('/color')) continue
185
- const paint = paintBindingTarget(field)
186
- if (!paint) continue
187
- const color = graph.resolveColorVariableForNode(node.id, node.boundVariables[field], fallback)
188
- const { index } = paint
189
- if (!color) continue
190
- if (paint.list === 'fills') {
191
- if (!node.fills[index]) continue
192
- changes.fills ??= copyFills(node.fills)
193
- applyBoundColor(changes.fills[index], color)
194
- } else {
195
- if (!node.strokes[index]) continue
196
- changes.strokes ??= copyStrokes(node.strokes)
197
- applyBoundColor(changes.strokes[index], color)
198
- }
199
- }
200
- return changes
201
- }
202
-
203
157
  /** `effect` with one bound property set to its variable's value; a colour keeps its alpha. */
204
158
  function withBoundEffectValue(
205
159
  effect: Effect,
@@ -227,8 +181,7 @@ function scaledLength(length: number | undefined, node: SceneNode): number | und
227
181
  */
228
182
  export function resolvedEffectBindings(
229
183
  graph: SceneGraph,
230
- node: SceneNode,
231
- fallback: VariableModeFallback = 'active'
184
+ node: SceneNode
232
185
  ): Pick<Partial<SceneNode>, 'effects'> {
233
186
  // Most nodes bind nothing, so no copy is made until a value actually changes.
234
187
  let effects: Effect[] | undefined
@@ -239,8 +192,8 @@ export function resolvedEffectBindings(
239
192
  if (!effect || !effectBindableProperties(effect.type).includes(target.property)) continue
240
193
  const value =
241
194
  target.property === 'color'
242
- ? graph.resolveColorVariableForNode(node.id, variableId, fallback)
243
- : scaledLength(graph.resolveNumberVariableForNode(node.id, variableId, fallback), node)
195
+ ? graph.resolveColorVariableForNode(node.id, variableId)
196
+ : scaledLength(graph.resolveNumberVariableForNode(node.id, variableId), node)
244
197
  if (value == null || (typeof value === 'number' && !Number.isFinite(value))) continue
245
198
  const next = withBoundEffectValue(effect, target.property, value)
246
199
  if (isEqual(next, effect)) continue
@@ -276,36 +229,12 @@ export function variablesResolvingThrough(graph: SceneGraph, ids: Iterable<strin
276
229
  return found
277
230
  }
278
231
 
279
- function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneNode[] {
280
- const nodes: SceneNode[] = []
281
- const pending = [...roots]
282
- for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
283
- const node = graph.getNode(id)
284
- if (!node) continue
285
- nodes.push(node)
286
- pending.push(...node.childIds)
287
- }
288
- return nodes
289
- }
290
-
291
232
  /**
292
233
  * The collections whose variables the layers in these subtrees resolve: those they bind, and
293
234
  * those the bound variables alias, so a mode set on the subtrees can change what they show.
294
235
  */
295
236
  export function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {
296
- const pending = subtreeNodes(graph, roots).flatMap(boundVariableIds)
297
- const seen = new Set<string>()
298
- const collections = new Set<string>()
299
- for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
300
- if (seen.has(id)) continue
301
- seen.add(id)
302
- const variable = graph.variables.get(id)
303
- if (!variable) continue
304
- collections.add(variable.collectionId)
305
- for (const value of Object.values(variable.valuesByMode))
306
- pending.push(...variableValueReferences(value))
307
- }
308
- return collections
237
+ return collectionsResolving(graph, subtreeNodes(graph, roots).flatMap(boundVariableIds))
309
238
  }
310
239
 
311
240
  /**
@@ -319,6 +248,27 @@ export function layersUsingVariables(graph: SceneGraph, ids: Iterable<string>):
319
248
  )
320
249
  }
321
250
 
251
+ /**
252
+ * The layers in these subtrees that resolve a variable of these collections, binding it or a
253
+ * variable that aliases it, so a mode change for the collections can change what they show.
254
+ */
255
+ export function layersResolvingCollections(
256
+ graph: SceneGraph,
257
+ roots: Iterable<string>,
258
+ collectionIds: ReadonlySet<string>
259
+ ): SceneNode[] {
260
+ const inCollections = [...graph.variables.values()].filter((variable) =>
261
+ collectionIds.has(variable.collectionId)
262
+ )
263
+ const variables = variablesResolvingThrough(
264
+ graph,
265
+ inCollections.map((variable) => variable.id)
266
+ )
267
+ return subtreeNodes(graph, roots).filter((node) =>
268
+ boundVariableIds(node).some((id) => variables.has(id))
269
+ )
270
+ }
271
+
322
272
  function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterable<SceneNode> {
323
273
  if (!scope) return graph.getAllNodes()
324
274
  if ('subtrees' in scope) return subtreeNodes(graph, scope.subtrees)
@@ -326,26 +276,29 @@ function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterab
326
276
  }
327
277
 
328
278
  /**
329
- * Resolve live numeric, text, visibility, and effect bindings, numbers in scene units, without
279
+ * Resolve live numeric, text, visibility, and effect bindings, numbers in scene units, and refresh
280
+ * bound paints whose variable's color for the layer changed (`refreshBoundPaints`), without
330
281
  * authoring instance overrides. A scope limits it to the layers a change can reach, so layers whose
331
282
  * saved values differ from their bindings elsewhere in the document are left as saved. Returns the
332
- * layers whose change needs a layout pass; effects are not a layout input, so an effect-only change
333
- * is written but not returned.
283
+ * layers whose change needs a layout pass; effects and paints are not a layout input, so a change
284
+ * to them alone is written but not returned.
334
285
  */
335
286
  export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[] {
336
287
  return [...reconcileVariableBindingUpdates(graph, scope).keys()]
337
288
  }
338
289
 
339
290
  /**
340
- * As `reconcileVariableBindings`, with the layout fields each relaid layer received. Effects
341
- * are written too, but draw without layout, so a layer whose only change is an effect is left out.
291
+ * As `reconcileVariableBindings`, with the layout fields each relaid layer received. Effects and
292
+ * paints are written too, but draw without layout, so a layer whose only change is one is left out.
342
293
  */
343
294
  export function reconcileVariableBindingUpdates(
344
295
  graph: SceneGraph,
345
296
  scope?: BindingScope
346
297
  ): Map<string, Partial<SceneNode>> {
347
298
  const relaid = new Map<string, Partial<SceneNode>>()
348
- for (const node of scopedNodes(graph, scope)) {
299
+ const nodes = [...scopedNodes(graph, scope)]
300
+ for (const node of nodes) refreshBoundPaints(graph, node)
301
+ for (const node of nodes) {
349
302
  const layoutUpdates = {
350
303
  ...resolvedNumericBindings(graph, node),
351
304
  ...resolvedValueBindings(graph, node),
@@ -113,7 +113,7 @@ export function effectBindingTarget(
113
113
  return { index: Number(match[1]), property: match[2] as EffectBindingProperty }
114
114
  }
115
115
 
116
- /** The properties of an effect that bind: every shadow property, and only a blur's radius. */
116
+ /** The properties of an effect that bind: every shadow property, else only the radius. */
117
117
  export function effectBindableProperties(type: Effect['type']): readonly EffectBindingProperty[] {
118
118
  return type === 'DROP_SHADOW' || type === 'INNER_SHADOW'
119
119
  ? EFFECT_BINDING_PROPERTIES
@@ -120,7 +120,9 @@ export function parseVariableFormula(
120
120
  graph,
121
121
  variableId,
122
122
  (collectionId) =>
123
- collectionId === variable.collectionId ? modeId : graph.getActiveModeId(collectionId),
123
+ collectionId === variable.collectionId
124
+ ? modeId
125
+ : (graph.variableCollections.get(collectionId)?.defaultModeId ?? ''),
124
126
  new Set(),
125
127
  new Map(),
126
128
  new Map([[variableId, value]])