@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
@@ -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
+ }