@pairdraw/scene-graph 0.20.0 → 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 (145) 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/properties.d.ts.map +1 -1
  27. package/dist/components/properties.js +7 -7
  28. package/dist/components/properties.js.map +1 -1
  29. package/dist/css/shadows.d.ts +2 -2
  30. package/dist/css/shadows.d.ts.map +1 -1
  31. package/dist/css/shadows.js +4 -4
  32. package/dist/css/shadows.js.map +1 -1
  33. package/dist/css/values.d.ts +7 -2
  34. package/dist/css/values.d.ts.map +1 -1
  35. package/dist/css/values.js +8 -4
  36. package/dist/css/values.js.map +1 -1
  37. package/dist/empty-values.js +9 -0
  38. package/dist/empty-values.js.map +1 -1
  39. package/dist/graph.d.ts +4 -0
  40. package/dist/graph.d.ts.map +1 -1
  41. package/dist/graph.js +11 -2
  42. package/dist/graph.js.map +1 -1
  43. package/dist/images.d.ts +3 -2
  44. package/dist/images.d.ts.map +1 -1
  45. package/dist/images.js +18 -2
  46. package/dist/images.js.map +1 -1
  47. package/dist/index.d.ts +2 -2
  48. package/dist/instances/copies.js +2 -1
  49. package/dist/instances/copies.js.map +1 -1
  50. package/dist/instances/index.d.ts.map +1 -1
  51. package/dist/instances/index.js +12 -11
  52. package/dist/instances/index.js.map +1 -1
  53. package/dist/instances/overrides.d.ts +15 -1
  54. package/dist/instances/overrides.d.ts.map +1 -1
  55. package/dist/instances/overrides.js +37 -1
  56. package/dist/instances/overrides.js.map +1 -1
  57. package/dist/instances/swap-overrides.js +3 -3
  58. package/dist/instances/swap-overrides.js.map +1 -1
  59. package/dist/instances/sync.js +9 -7
  60. package/dist/instances/sync.js.map +1 -1
  61. package/dist/node-defaults.js +2 -2
  62. package/dist/node-defaults.js.map +1 -1
  63. package/dist/plugin-data/fields.d.ts +15 -0
  64. package/dist/plugin-data/fields.d.ts.map +1 -1
  65. package/dist/plugin-data/fields.js +15 -23
  66. package/dist/plugin-data/fields.js.map +1 -1
  67. package/dist/plugin-data/index.d.ts +2 -2
  68. package/dist/plugin-data/index.js +2 -2
  69. package/dist/plugin-data/schema.d.ts +6 -1
  70. package/dist/plugin-data/schema.d.ts.map +1 -1
  71. package/dist/plugin-data/schema.js +24 -1
  72. package/dist/plugin-data/schema.js.map +1 -1
  73. package/dist/shaders/animation.d.ts +21 -0
  74. package/dist/shaders/animation.d.ts.map +1 -0
  75. package/dist/shaders/animation.js +25 -0
  76. package/dist/shaders/animation.js.map +1 -0
  77. package/dist/shaders/index.d.ts +2 -1
  78. package/dist/shaders/index.js +2 -1
  79. package/dist/slots/content.d.ts.map +1 -1
  80. package/dist/slots/content.js +4 -4
  81. package/dist/slots/content.js.map +1 -1
  82. package/dist/statuses/index.d.ts +2 -2
  83. package/dist/statuses/index.js +2 -2
  84. package/dist/statuses/workflow.d.ts +3 -1
  85. package/dist/statuses/workflow.d.ts.map +1 -1
  86. package/dist/statuses/workflow.js +6 -2
  87. package/dist/statuses/workflow.js.map +1 -1
  88. package/dist/transfer/variables.d.ts.map +1 -1
  89. package/dist/transfer/variables.js +1 -1
  90. package/dist/transfer/variables.js.map +1 -1
  91. package/dist/types.d.ts +34 -2
  92. package/dist/types.d.ts.map +1 -1
  93. package/dist/types.js.map +1 -1
  94. package/dist/variables/bindings.d.ts +6 -5
  95. package/dist/variables/bindings.d.ts.map +1 -1
  96. package/dist/variables/bindings.js +24 -16
  97. package/dist/variables/bindings.js.map +1 -1
  98. package/dist/variables/formula/index.d.ts.map +1 -1
  99. package/dist/variables/formula/index.js +12 -6
  100. package/dist/variables/formula/index.js.map +1 -1
  101. package/dist/variables/index.d.ts +14 -1
  102. package/dist/variables/index.d.ts.map +1 -1
  103. package/dist/variables/index.js +38 -10
  104. package/dist/variables/index.js.map +1 -1
  105. package/dist/variables/paints.d.ts +9 -8
  106. package/dist/variables/paints.d.ts.map +1 -1
  107. package/dist/variables/paints.js +35 -26
  108. package/dist/variables/paints.js.map +1 -1
  109. package/dist/variables/resolver.d.ts +45 -0
  110. package/dist/variables/resolver.d.ts.map +1 -0
  111. package/dist/variables/resolver.js +106 -0
  112. package/dist/variables/resolver.js.map +1 -0
  113. package/package.json +1 -1
  114. package/src/color/index.ts +43 -9
  115. package/src/color/management.ts +54 -1
  116. package/src/color/profile.ts +131 -0
  117. package/src/comments/merge.ts +3 -0
  118. package/src/comments/schema.ts +32 -2
  119. package/src/comments/threads.ts +5 -2
  120. package/src/components/properties.ts +13 -7
  121. package/src/css/shadows.ts +8 -5
  122. package/src/css/values.ts +12 -5
  123. package/src/empty-values.ts +16 -0
  124. package/src/graph.ts +13 -1
  125. package/src/images.ts +27 -3
  126. package/src/instances/copies.ts +2 -1
  127. package/src/instances/index.ts +19 -11
  128. package/src/instances/overrides.ts +49 -0
  129. package/src/instances/swap-overrides.ts +3 -2
  130. package/src/instances/sync.ts +11 -7
  131. package/src/node-defaults.ts +2 -2
  132. package/src/plugin-data/fields.ts +16 -31
  133. package/src/plugin-data/index.ts +1 -0
  134. package/src/plugin-data/schema.ts +30 -0
  135. package/src/shaders/animation.ts +39 -0
  136. package/src/shaders/index.ts +1 -0
  137. package/src/slots/content.ts +8 -4
  138. package/src/statuses/workflow.ts +6 -4
  139. package/src/transfer/variables.ts +2 -1
  140. package/src/types.ts +30 -1
  141. package/src/variables/bindings.ts +35 -18
  142. package/src/variables/formula/index.ts +19 -18
  143. package/src/variables/index.ts +49 -10
  144. package/src/variables/paints.ts +64 -29
  145. package/src/variables/resolver.ts +178 -0
@@ -1,5 +1,7 @@
1
1
  import * as v from 'valibot'
2
2
 
3
+ import type { EnabledLibraryBinding, SourceLibraryPublication } from '../types'
4
+
3
5
  /** A quote kept on a sticky. A longer passage is cut before it is stored. */
4
6
  export const CITATION_QUOTE_LIMIT = 2_000
5
7
 
@@ -33,3 +35,31 @@ export const webCitationSchema = v.object({
33
35
  })
34
36
 
35
37
  export type WebCitation = v.InferOutput<typeof webCitationSchema>
38
+
39
+ const enabledLibrarySchema = v.object({
40
+ libraryId: v.string(),
41
+ revisionId: v.string(),
42
+ enabled: v.optional(v.unknown())
43
+ })
44
+
45
+ /** One library a document switched on; invalid entries are skipped one by one. */
46
+ export const enabledLibrariesSchema: v.GenericSchema<unknown, EnabledLibraryBinding[]> = v.pipe(
47
+ v.array(v.unknown()),
48
+ v.transform((items) =>
49
+ items.flatMap((item) => {
50
+ const parsed = v.safeParse(enabledLibrarySchema, item)
51
+ if (!parsed.success) return []
52
+ const { libraryId, revisionId, enabled } = parsed.output
53
+ return [{ libraryId, revisionId, enabled: enabled === true }]
54
+ })
55
+ )
56
+ )
57
+
58
+ /** The library a document publishes as: its identity, its revision, and where it was published. */
59
+ export const sourceLibraryPublicationSchema: v.GenericSchema<unknown, SourceLibraryPublication> =
60
+ v.object({
61
+ libraryId: v.string(),
62
+ revisionId: v.string(),
63
+ name: v.string(),
64
+ catalogSource: v.optional(v.string())
65
+ })
@@ -0,0 +1,39 @@
1
+ import type { ShaderComponent, ShaderPreset } from './schema'
2
+
3
+ /**
4
+ * What the library says of an effect's motion: whether it is driven by time, and whether a
5
+ * `speed` prop of its own decides how fast it moves.
6
+ */
7
+ export interface ShaderEffectMotion {
8
+ animates: boolean
9
+ speed: boolean
10
+ }
11
+
12
+ /** What the caller knows of an effect's motion, or undefined for an effect it does not know yet. */
13
+ export type ShaderMotion = (type: string) => ShaderEffectMotion | undefined
14
+
15
+ /**
16
+ * Whether `component` draws a picture that changes with time. An effect missing from the
17
+ * library's metadata counts as animated, so a newer effect plays rather than freezing; `speed: 0`
18
+ * and an `animated` switch turned off stop it.
19
+ */
20
+ function componentAnimates(component: ShaderComponent, motion: ShaderMotion): boolean {
21
+ const props = component.props ?? {}
22
+ if (props.animated === true) return true
23
+ const known = motion(component.type)
24
+ const moves =
25
+ typeof props.speed === 'number'
26
+ ? props.speed !== 0
27
+ : known === undefined || known.animates || known.speed
28
+ if (moves) return true
29
+ return (component.children ?? []).some((child) => componentAnimates(child, motion))
30
+ }
31
+
32
+ /**
33
+ * Whether `preset` changes between frames: any of its effects animates. A preset of still
34
+ * effects, or one whose animated effects are all at `speed: 0`, draws one picture forever and
35
+ * does not need a player.
36
+ */
37
+ export function presetAnimates(preset: ShaderPreset, motion: ShaderMotion): boolean {
38
+ return preset.components.some((component) => componentAnimates(component, motion))
39
+ }
@@ -1,3 +1,4 @@
1
+ export * from './animation'
1
2
  export * from './directives'
2
3
  export * from './model'
3
4
  export * from './schema'
@@ -1,6 +1,10 @@
1
1
  import { isGeneratedFrame } from '../generated'
2
2
  import type { SceneGraph } from '../graph'
3
- import { getInstanceOverride, setInstanceOverride } from '../instances/overrides'
3
+ import {
4
+ getInstanceOverride,
5
+ ownInstanceOverrides,
6
+ setInstanceOverride
7
+ } from '../instances/overrides'
4
8
  import { cloneChildrenWithMapping } from '../instances/sync'
5
9
  import type { SceneNode } from '../types'
6
10
  import { applyBoundPaintColorsIn } from '../variables/paints'
@@ -63,7 +67,7 @@ export function claimSlotContent(
63
67
  ): void {
64
68
  const { frame, instance, propertyId } = scope
65
69
  if (ownsSlotContent(graph, frame, propertyId)) return
66
- const overrides = instance.instanceOverrides
70
+ const overrides = ownInstanceOverrides(instance)
67
71
  for (const node of descendants(graph, frame, false)) {
68
72
  overrides.descendants.delete(node.id)
69
73
  if (node.type !== 'INSTANCE') {
@@ -76,10 +80,10 @@ export function claimSlotContent(
76
80
  if (!fields) continue
77
81
  for (const [field, value] of fields)
78
82
  if (field !== 'sourceComponentId')
79
- setInstanceOverride(node.instanceOverrides, node.id, inner.id, field, value)
83
+ setInstanceOverride(ownInstanceOverrides(node), node.id, inner.id, field, value)
80
84
  overrides.descendants.delete(inner.id)
81
85
  }
82
- graph.updateNode(node.id, { instanceOverrides: node.instanceOverrides })
86
+ graph.updateNode(node.id, { instanceOverrides: ownInstanceOverrides(node) })
83
87
  }
84
88
  graph.updateNode(instance.id, {
85
89
  instanceOverrides: overrides,
@@ -63,10 +63,12 @@ export type StatusWorkflowPreset = keyof typeof STATUS_WORKFLOW_PRESETS
63
63
  * Ready for dev and Completed read as statuses without setup.
64
64
  */
65
65
  export function readStatusWorkflow(graph: SceneGraph): StatusWorkflow {
66
- return (
67
- readPluginData(graph.getNode(graph.rootId)?.pluginData, statuses) ??
68
- STATUS_WORKFLOW_PRESETS.handoff
69
- )
66
+ return readStoredStatusWorkflow(graph) ?? STATUS_WORKFLOW_PRESETS.handoff
67
+ }
68
+
69
+ /** The workflow the document stored, or `undefined` when it never chose one. */
70
+ export function readStoredStatusWorkflow(graph: SceneGraph): StatusWorkflow | undefined {
71
+ return readPluginData(graph.getNode(graph.rootId)?.pluginData, statuses)
70
72
  }
71
73
 
72
74
  /** Stores the workflow on the document node, as an undoable document change. */
@@ -32,7 +32,8 @@ function assertResourceOwnership(
32
32
  }
33
33
  for (const variable of variables) {
34
34
  const collection = sets.get(variable.collectionId)
35
- if (!collection?.variableIds.includes(variable.id))
35
+ // A collection lists its live variables: a deleted one is kept out of the list (addVariable).
36
+ if (!collection || (!variable.deleted && !collection.variableIds.includes(variable.id)))
36
37
  throw new Error(`Missing collection membership for ${variable.id}`)
37
38
  const modes = new Set(collection.modes.map((mode) => mode.modeId))
38
39
  for (const mode of Object.keys(variable.valuesByMode)) {
package/src/types.ts CHANGED
@@ -47,7 +47,14 @@ export interface NodeChangeObserver {
47
47
  resume?: () => void
48
48
  }
49
49
 
50
- export type DocumentColorSpace = 'srgb' | 'display-p3'
50
+ /**
51
+ * A color profile: the one a document's colors are stored in, or the one they are shown or
52
+ * exported in. Figma files are sRGB or Display P3.
53
+ */
54
+ export type ColorProfile = 'srgb' | 'display-p3'
55
+
56
+ /** @deprecated use `ColorProfile` */
57
+ export type DocumentColorSpace = ColorProfile
51
58
 
52
59
  /** What a document is for, as Figma keeps design files and FigJam boards apart. */
53
60
  export type DocumentKind = 'design' | 'board'
@@ -700,6 +707,16 @@ export interface CommentReply {
700
707
  deleted?: boolean
701
708
  }
702
709
 
710
+ /** A stroke drawn over the commented area in Comment mode, kept with its thread. */
711
+ export interface CommentRegion {
712
+ /** Stroke points, in canvas units relative to the thread's pin anchor. */
713
+ points: [number, number][]
714
+ /** Stroke width in canvas units. */
715
+ width: number
716
+ /** Bounds of the stroke including its width, relative to the same anchor. */
717
+ bounds: { minX: number; minY: number; maxX: number; maxY: number }
718
+ }
719
+
703
720
  /** A canvas comment thread, kept in the document's root plugin data. */
704
721
  export interface CommentThread {
705
722
  id: string
@@ -727,6 +744,8 @@ export interface CommentThread {
727
744
  resolvedAt?: string | null
728
745
  /** Deleted threads stay as tombstones so a stale copy cannot bring them back. */
729
746
  deleted?: boolean
747
+ /** The area the person circled, when the thread was drawn rather than pinned. */
748
+ region?: CommentRegion | null
730
749
  replies: CommentReply[]
731
750
  }
732
751
 
@@ -1276,6 +1295,11 @@ export interface Variable {
1276
1295
  key?: string
1277
1296
  /** Published library version (from NodeChange.version). Used for assetRef resolution in colorVar. */
1278
1297
  version?: string
1298
+ /**
1299
+ * Deleted as Figma deletes a variable: gone from its collection's `variableIds` and from every
1300
+ * list of variables, while layers bound to it still resolve it and the file keeps it.
1301
+ */
1302
+ deleted?: true
1279
1303
  }
1280
1304
 
1281
1305
  /** Scalar numeric node fields accepted by numeric property controls. */
@@ -1315,6 +1339,11 @@ export interface VariableCollection {
1315
1339
  key?: string
1316
1340
  /** Published library version, which references by key carry with it. */
1317
1341
  version?: string
1342
+ /**
1343
+ * Deleted as Figma deletes a collection: gone from every list of collections, with its variables
1344
+ * deleted too, while layers bound to them still resolve them and the file keeps them.
1345
+ */
1346
+ deleted?: true
1318
1347
  }
1319
1348
 
1320
1349
  /** Whether a collection is a library's copy, which takes no variables or modes of the file's. */
@@ -9,7 +9,7 @@ import { pixelLength } from '../text/lengths'
9
9
  import type { SceneNode, StyleRun, TextLength } from '../types'
10
10
  import { NUMERIC_FIELDS, isNumericVariableBindingField } from './fields'
11
11
  import { refreshBoundPaints } from './paints'
12
- import { collectionsResolving } from './resolution'
12
+ import { createVariableResolver, type VariableResolver } from './resolver'
13
13
  import { subtreeNodes } from './subtree'
14
14
  import { variableFontFamily, variableText, variableTruthy } from './text'
15
15
  import { runBindingScale } from './units'
@@ -62,6 +62,11 @@ export function boundNumberValue(field: string, value: number): number | TextLen
62
62
  return TEXT_LENGTH_FIELDS.has(field) ? pixelLength(value) : value
63
63
  }
64
64
 
65
+ /** One resolver per field lookup, unless a caller reconciling many nodes shares its own. */
66
+ function resolverFor(graph: SceneGraph, resolver?: VariableResolver): VariableResolver {
67
+ return resolver ?? createVariableResolver(graph)
68
+ }
69
+
65
70
  /**
66
71
  * The values a node's numeric bindings resolve to in its mode, in scene units, for the fields
67
72
  * whose stored value differs. A width or height an instance overrides keeps its own value.
@@ -69,14 +74,16 @@ export function boundNumberValue(field: string, value: number): number | TextLen
69
74
  export function resolvedNumericBindings(
70
75
  graph: SceneGraph,
71
76
  node: SceneNode,
72
- fallback: VariableModeFallback = 'active'
77
+ fallback: VariableModeFallback = 'active',
78
+ resolver?: VariableResolver
73
79
  ): Partial<SceneNode> {
74
80
  const updates: Partial<SceneNode> = {}
81
+ const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
75
82
  for (const [field, variableId] of Object.entries(node.boundVariables)) {
76
83
  if (!NUMERIC_FIELDS.has(field)) continue
77
84
  if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))
78
85
  continue
79
- const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
86
+ const value = reader.number(variableId)
80
87
  if (value === undefined || !Number.isFinite(value)) continue
81
88
  const scale = node.variableBindingScales[field] ?? 1
82
89
  const effective = value * scale
@@ -100,13 +107,15 @@ export function resolvedNumericBindings(
100
107
  export function resolvedValueBindings(
101
108
  graph: SceneGraph,
102
109
  node: SceneNode,
103
- fallback: VariableModeFallback = 'active'
110
+ fallback: VariableModeFallback = 'active',
111
+ resolver?: VariableResolver
104
112
  ): Partial<SceneNode> {
105
113
  const updates: Partial<SceneNode> = {}
114
+ const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
106
115
  const resolve = (field: 'text' | 'fontFamily' | 'visible') => {
107
116
  const variableId = node.boundVariables[field]
108
117
  if (!variableId || overriddenInInstance(graph, node.id, field)) return undefined
109
- return graph.resolveVariableForNode(node.id, variableId, fallback)
118
+ return reader.value(variableId)
110
119
  }
111
120
  const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
112
121
  if (text !== undefined && text !== node.text) updates.text = text
@@ -126,14 +135,15 @@ export function resolvedValueBindings(
126
135
  export function resolvedIconBinding(
127
136
  graph: SceneGraph,
128
137
  node: SceneNode,
129
- fallback: VariableModeFallback = 'active'
138
+ fallback: VariableModeFallback = 'active',
139
+ resolver?: VariableResolver
130
140
  ): string | undefined {
131
141
  if (node.type !== 'FRAME') return undefined
132
142
  const icon = readIcon(node)
133
143
  if (!icon) return undefined
134
144
  const variableId = node.boundVariables['iconName']
135
145
  if (!variableId || overriddenInInstance(graph, node.id, 'iconName')) return undefined
136
- const resolved = graph.resolveVariableForNode(node.id, variableId, fallback)
146
+ const resolved = resolverFor(graph, resolver).forNode(node.id, fallback).value(variableId)
137
147
  if (typeof resolved !== 'string') return undefined
138
148
  const name = resolved.trim()
139
149
  if (name === '' || name === icon.name || !ICON_NAME.test(name)) return undefined
@@ -151,8 +161,10 @@ const NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeig
151
161
  export function resolvedRunBindings(
152
162
  graph: SceneGraph,
153
163
  node: SceneNode,
154
- fallback: VariableModeFallback = 'active'
164
+ fallback: VariableModeFallback = 'active',
165
+ resolver?: VariableResolver
155
166
  ): Pick<Partial<SceneNode>, 'styleRuns'> {
167
+ const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
156
168
  const styleRuns = node.styleRuns.map((run): StyleRun => {
157
169
  const bound = run.style.boundVariables
158
170
  if (!bound) return run
@@ -160,7 +172,7 @@ export function resolvedRunBindings(
160
172
  for (const field of NUMERIC_RUN_FIELDS) {
161
173
  const variableId = bound[field]
162
174
  if (!variableId) continue
163
- const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)
175
+ const value = reader.number(variableId)
164
176
  if (value === undefined || !Number.isFinite(value)) continue
165
177
  const effective = value * runBindingScale(node, style, field)
166
178
  if (!Number.isFinite(effective)) continue
@@ -169,9 +181,7 @@ export function resolvedRunBindings(
169
181
  style = { ...style, [field]: next }
170
182
  }
171
183
  const familyId = bound.fontFamily
172
- const family = familyId
173
- ? variableFontFamily(graph.resolveVariableForNode(node.id, familyId, fallback))
174
- : undefined
184
+ const family = familyId ? variableFontFamily(reader.value(familyId)) : undefined
175
185
  if (family !== undefined && family !== style.fontFamily)
176
186
  style = { ...style, fontFamily: family }
177
187
  return style === run.style ? run : { ...run, style }
@@ -218,8 +228,14 @@ export function variablesResolvingThrough(graph: SceneGraph, ids: Iterable<strin
218
228
  * The collections whose variables the layers in these subtrees resolve: those they bind, and
219
229
  * those the bound variables alias, so a mode set on the subtrees can change what they show.
220
230
  */
221
- export function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {
222
- return collectionsResolving(graph, subtreeNodes(graph, roots).flatMap(boundVariableIds))
231
+ export function collectionsBoundIn(
232
+ graph: SceneGraph,
233
+ roots: Iterable<string>,
234
+ resolver?: VariableResolver
235
+ ): Set<string> {
236
+ return resolverFor(graph, resolver).dependencies(
237
+ subtreeNodes(graph, roots).flatMap(boundVariableIds)
238
+ )
223
239
  }
224
240
 
225
241
  /**
@@ -249,12 +265,13 @@ function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterab
249
265
  export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[] {
250
266
  const changed: string[] = []
251
267
  const nodes = [...scopedNodes(graph, scope)]
252
- for (const node of nodes) refreshBoundPaints(graph, node)
268
+ const resolver = createVariableResolver(graph)
269
+ for (const node of nodes) refreshBoundPaints(graph, node, resolver)
253
270
  for (const node of nodes) {
254
271
  const updates = {
255
- ...resolvedNumericBindings(graph, node),
256
- ...resolvedValueBindings(graph, node),
257
- ...resolvedRunBindings(graph, node)
272
+ ...resolvedNumericBindings(graph, node, 'active', resolver),
273
+ ...resolvedValueBindings(graph, node, 'active', resolver),
274
+ ...resolvedRunBindings(graph, node, 'active', resolver)
258
275
  }
259
276
  if (Object.keys(updates).length === 0) continue
260
277
  graph.updateNode(node.id, updates)
@@ -1,7 +1,7 @@
1
1
  import type { SceneGraph } from '#scene-graph/graph'
2
2
  import type { VariableLiteral, VariableValue } from '#scene-graph/types'
3
3
 
4
- import { resolveVariableInModes } from '../resolution'
4
+ import { createVariableResolver } from '../resolver'
5
5
  import { parseFormulaSyntax, type FormulaReference } from './parse'
6
6
  import { printFormula, type PrintFormulaOptions } from './print'
7
7
  import type { FormulaValueType } from './signatures'
@@ -38,12 +38,15 @@ function qualifiedName(graph: SceneGraph, id: string) {
38
38
  /** Qualified names disambiguate references; the stored formula uses stable IDs. */
39
39
  export function variableFormulaReference(graph: SceneGraph, id: string): string {
40
40
  const variable = graph.variables.get(id)
41
- if (!variable) return `{#${id}}`
41
+ // A deleted variable's name no longer names it, so only its ID does.
42
+ if (!variable || variable.deleted) return `{#${id}}`
42
43
  const unique = (name: string, of: (id: string) => string | undefined) =>
43
- [...graph.variables.keys()].filter((other) => of(other) === name).length === 1
44
+ graph.getLiveVariables().filter((other) => of(other.id) === name).length === 1
44
45
  const plain = variable.name
45
46
  if (
46
47
  !/[{}]/.test(plain) &&
48
+ // A name that starts like an ID would be read as one.
49
+ !plain.startsWith('#') &&
47
50
  plain.trim() === plain &&
48
51
  unique(plain, (other) => graph.variables.get(other)?.name)
49
52
  )
@@ -72,12 +75,13 @@ export function formatVariableFormula(
72
75
  * edited variable's own collection when a name appears in several.
73
76
  */
74
77
  function resolveReference(graph: SceneGraph, collectionId: string, name: string): FormulaReference {
75
- const candidates = [...graph.variables.values()].filter(
76
- (candidate) =>
77
- name === `#${candidate.id}` ||
78
- name === qualifiedName(graph, candidate.id) ||
79
- name === candidate.name
80
- )
78
+ // An ID names its variable before any name does; a deleted variable is reached only by it, as
79
+ // a formula saved before the deletion names it.
80
+ const byId = name.startsWith('#') ? graph.variables.get(name.slice(1)) : undefined
81
+ if (byId) return { id: byId.id, type: byId.type }
82
+ const candidates = graph
83
+ .getLiveVariables()
84
+ .filter((candidate) => name === qualifiedName(graph, candidate.id) || name === candidate.name)
81
85
  const local = candidates.filter((candidate) => candidate.collectionId === collectionId)
82
86
  const matches = local.length === 1 ? local : candidates
83
87
  const match = matches.at(0)
@@ -116,14 +120,11 @@ export function parseVariableFormula(
116
120
  const value: VariableValue = isFormulaOperation(node)
117
121
  ? { expression: 'FORMULA', type: variable.type, value: node }
118
122
  : node
119
- const resolved = resolveVariableInModes(
120
- graph,
121
- variableId,
122
- (collectionId) =>
123
- collectionId === variable.collectionId ? modeId : graph.getActiveModeId(collectionId),
124
- new Set(),
125
- new Map(),
126
- new Map([[variableId, value]])
127
- )
123
+ const resolver = createVariableResolver(graph, { overrides: new Map([[variableId, value]]) })
124
+ const resolved = resolver
125
+ .forModes((collectionId) =>
126
+ collectionId === variable.collectionId ? modeId : resolver.activeMode(collectionId)
127
+ )
128
+ .resolve(variableId)
128
129
  return { value, resolved }
129
130
  }
@@ -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),