@pairdraw/scene-graph 0.21.1 → 0.22.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 (100) hide show
  1. package/dist/constants.d.ts +10 -4
  2. package/dist/constants.d.ts.map +1 -1
  3. package/dist/constants.js +60 -2
  4. package/dist/constants.js.map +1 -1
  5. package/dist/copy.js +7 -0
  6. package/dist/copy.js.map +1 -1
  7. package/dist/effects.d.ts +9 -1
  8. package/dist/effects.d.ts.map +1 -1
  9. package/dist/effects.js +10 -1
  10. package/dist/effects.js.map +1 -1
  11. package/dist/fields/layout.d.ts +1 -1
  12. package/dist/fields/layout.d.ts.map +1 -1
  13. package/dist/fields/layout.js +3 -1
  14. package/dist/fields/layout.js.map +1 -1
  15. package/dist/geometry.d.ts.map +1 -1
  16. package/dist/geometry.js +3 -3
  17. package/dist/geometry.js.map +1 -1
  18. package/dist/graph.d.ts +4 -1
  19. package/dist/graph.d.ts.map +1 -1
  20. package/dist/graph.js +9 -0
  21. package/dist/graph.js.map +1 -1
  22. package/dist/hit-test.d.ts.map +1 -1
  23. package/dist/hit-test.js +1 -1
  24. package/dist/hit-test.js.map +1 -1
  25. package/dist/images.d.ts +17 -1
  26. package/dist/images.d.ts.map +1 -1
  27. package/dist/images.js +41 -1
  28. package/dist/images.js.map +1 -1
  29. package/dist/index.d.ts +2 -2
  30. package/dist/instances/fields.d.ts +5 -3
  31. package/dist/instances/fields.d.ts.map +1 -1
  32. package/dist/instances/fields.js +5 -0
  33. package/dist/instances/fields.js.map +1 -1
  34. package/dist/instances/index.d.ts +2 -2
  35. package/dist/instances/index.d.ts.map +1 -1
  36. package/dist/instances/index.js +8 -3
  37. package/dist/instances/index.js.map +1 -1
  38. package/dist/instances/placement.js +1 -1
  39. package/dist/instances/placement.js.map +1 -1
  40. package/dist/instances/sync.js +24 -5
  41. package/dist/instances/sync.js.map +1 -1
  42. package/dist/node-defaults.d.ts.map +1 -1
  43. package/dist/node-defaults.js +4 -0
  44. package/dist/node-defaults.js.map +1 -1
  45. package/dist/resize.d.ts +9 -2
  46. package/dist/resize.d.ts.map +1 -1
  47. package/dist/resize.js +20 -8
  48. package/dist/resize.js.map +1 -1
  49. package/dist/slots/content.d.ts +46 -1
  50. package/dist/slots/content.d.ts.map +1 -1
  51. package/dist/slots/content.js +146 -2
  52. package/dist/slots/content.js.map +1 -1
  53. package/dist/slots/index.d.ts +3 -2
  54. package/dist/slots/index.js +3 -2
  55. package/dist/slots/owners.d.ts +17 -0
  56. package/dist/slots/owners.d.ts.map +1 -0
  57. package/dist/slots/owners.js +39 -0
  58. package/dist/slots/owners.js.map +1 -0
  59. package/dist/types.d.ts +115 -5
  60. package/dist/types.d.ts.map +1 -1
  61. package/dist/types.js.map +1 -1
  62. package/dist/variables/bindings.d.ts +9 -3
  63. package/dist/variables/bindings.d.ts.map +1 -1
  64. package/dist/variables/bindings.js +128 -26
  65. package/dist/variables/bindings.js.map +1 -1
  66. package/dist/variables/fields.d.ts +20 -1
  67. package/dist/variables/fields.d.ts.map +1 -1
  68. package/dist/variables/fields.js +29 -3
  69. package/dist/variables/fields.js.map +1 -1
  70. package/dist/variables/index.d.ts +3 -3
  71. package/dist/variables/index.d.ts.map +1 -1
  72. package/dist/variables/index.js +25 -4
  73. package/dist/variables/index.js.map +1 -1
  74. package/dist/variant-properties.d.ts +7 -2
  75. package/dist/variant-properties.d.ts.map +1 -1
  76. package/dist/variant-properties.js +35 -3
  77. package/dist/variant-properties.js.map +1 -1
  78. package/package.json +1 -1
  79. package/src/constants.ts +32 -3
  80. package/src/copy.ts +7 -0
  81. package/src/effects.ts +21 -0
  82. package/src/fields/layout.ts +3 -1
  83. package/src/geometry.ts +5 -3
  84. package/src/graph.ts +13 -0
  85. package/src/hit-test.ts +7 -1
  86. package/src/images.ts +61 -2
  87. package/src/instances/fields.ts +5 -0
  88. package/src/instances/index.ts +10 -1
  89. package/src/instances/placement.ts +2 -1
  90. package/src/instances/sync.ts +38 -5
  91. package/src/node-defaults.ts +4 -0
  92. package/src/resize.ts +44 -6
  93. package/src/slots/content.ts +208 -1
  94. package/src/slots/index.ts +1 -0
  95. package/src/slots/owners.ts +45 -0
  96. package/src/types.ts +207 -3
  97. package/src/variables/bindings.ts +155 -33
  98. package/src/variables/fields.ts +41 -2
  99. package/src/variables/index.ts +40 -1
  100. package/src/variant-properties.ts +36 -2
@@ -0,0 +1,45 @@
1
+ import type { SceneGraph } from '../graph'
2
+
3
+ /** A slot-content assignment the reader dropped because another instance owns the frame. */
4
+ export interface DuplicateSlotContentAssignment {
5
+ instanceId: string
6
+ propertyId: string
7
+ frameId: string
8
+ }
9
+
10
+ /**
11
+ * Re-establishes one-owner-per-slot-content-frame on a graph read from a file, where nothing
12
+ * prevented two instances naming the same frame. Keeps the first owner in document order and
13
+ * clears the later assignment, without marking the cleared instance as edited by the user.
14
+ */
15
+ export function dropDuplicateSlotContentAssignments(
16
+ graph: SceneGraph
17
+ ): DuplicateSlotContentAssignment[] {
18
+ const owners = new Map<string, string>()
19
+ const dropped: DuplicateSlotContentAssignment[] = []
20
+ for (const instance of graph.nodes.values()) {
21
+ if (instance.type !== 'INSTANCE') continue
22
+ const assignments = instance.componentPropertyAssignments
23
+ const droppedHere = new Set<string>()
24
+ for (const [propertyId, frameId] of Object.entries(assignments)) {
25
+ if (frameId === '' || graph.getNode(frameId)?.type !== 'FRAME') continue
26
+ const owner = owners.get(frameId)
27
+ if (owner === undefined) {
28
+ owners.set(frameId, instance.id)
29
+ continue
30
+ }
31
+ if (owner === instance.id) continue
32
+ droppedHere.add(propertyId)
33
+ dropped.push({ instanceId: instance.id, propertyId, frameId })
34
+ }
35
+ if (droppedHere.size > 0) {
36
+ const kept = Object.fromEntries(
37
+ Object.entries(assignments).filter(([propertyId]) => !droppedHere.has(propertyId))
38
+ )
39
+ graph.applyImportedStateDuring(() =>
40
+ graph.updateNode(instance.id, { componentPropertyAssignments: kept })
41
+ )
42
+ }
43
+ }
44
+ return dropped
45
+ }
package/src/types.ts CHANGED
@@ -157,6 +157,7 @@ export type NodeType =
157
157
  | 'CONNECTOR'
158
158
  | 'SHAPE_WITH_TEXT'
159
159
  | 'STICKY'
160
+ | 'SLICE'
160
161
  | 'SCRIPT'
161
162
 
162
163
  export type FillType =
@@ -544,6 +545,9 @@ export type AxisSizingMode = 'FIXED' | 'HUG'
544
545
 
545
546
  export type GridTrackSizing = 'FIXED' | 'FR' | 'AUTO'
546
547
 
548
+ /** Where a grid child sits inside its cell, as Figma's `GridChildAlign` names it. */
549
+ export type GridChildAlign = 'AUTO' | 'MIN' | 'CENTER' | 'MAX'
550
+
547
551
  export interface GridTrack {
548
552
  sizing: GridTrackSizing
549
553
  value: number
@@ -764,7 +768,51 @@ export type PrototypeTriggerType =
764
768
  | 'AFTER_TIMEOUT'
765
769
 
766
770
  /** What a reaction does, as `.fig` keeps `ConnectionType` on `PrototypeAction`. */
767
- export type PrototypeActionType = 'NODE' | 'BACK' | 'CLOSE'
771
+ export type PrototypeActionType =
772
+ | 'NODE'
773
+ | 'BACK'
774
+ | 'CLOSE'
775
+ | 'SET_VARIABLE'
776
+ | 'SET_VARIABLE_MODE'
777
+ | 'CONDITIONAL'
778
+
779
+ /** The operations a prototype expression combines values with, Figma's `ExpressionFunction`. */
780
+ export type PrototypeExpressionFunction =
781
+ | 'ADDITION'
782
+ | 'SUBTRACTION'
783
+ | 'MULTIPLY'
784
+ | 'DIVIDE'
785
+ | 'EQUALS'
786
+ | 'NOT_EQUAL'
787
+ | 'LESS_THAN'
788
+ | 'LESS_THAN_OR_EQUAL'
789
+ | 'GREATER_THAN'
790
+ | 'GREATER_THAN_OR_EQUAL'
791
+ | 'AND'
792
+ | 'OR'
793
+ | 'NOT'
794
+ | 'STRINGIFY'
795
+ | 'TERNARY'
796
+ | 'NEGATE'
797
+ | 'IS_TRUTHY'
798
+ | 'RESOLVE_VARIANT'
799
+ | 'VAR_MODE_LOOKUP'
800
+ | 'KEYFRAME'
801
+ | 'COMPOSE_COLOR'
802
+
803
+ /** A value a Set variable action writes or a condition compares, as Figma's `VariableData` keeps it. */
804
+ export type PrototypeVariableValue =
805
+ | { kind: 'boolean'; value: boolean }
806
+ | { kind: 'number'; value: number }
807
+ | { kind: 'string'; value: string }
808
+ | { kind: 'color'; value: Color }
809
+ | { kind: 'alias'; variableId: string }
810
+ | {
811
+ kind: 'expression'
812
+ /** An unknown function keeps its raw name so the writer preserves it and the evaluator refuses. */
813
+ fn: string
814
+ args: PrototypeVariableValue[]
815
+ }
768
816
 
769
817
  /**
770
818
  * What a node action does with its node, as `NavigationType`: move to a frame, open it over the
@@ -789,7 +837,37 @@ export type ScrollBehavior = 'SCROLLS' | 'FIXED' | 'STICKY_SCROLLS'
789
837
  export type PrototypeTransitionType = 'INSTANT' | 'DISSOLVE' | 'SMART_ANIMATE'
790
838
 
791
839
  /** How a transition eases, by the names Figma's plugin API gives its presets. */
792
- export type PrototypeEasingType = 'LINEAR' | 'EASE_IN' | 'EASE_OUT' | 'EASE_IN_AND_OUT'
840
+ export type PrototypeEasingType =
841
+ | 'LINEAR'
842
+ | 'EASE_IN'
843
+ | 'EASE_OUT'
844
+ | 'EASE_IN_AND_OUT'
845
+ | 'EASE_IN_BACK'
846
+ | 'EASE_OUT_BACK'
847
+ | 'EASE_IN_AND_OUT_BACK'
848
+ | 'CUSTOM_CUBIC_BEZIER'
849
+ | 'GENTLE'
850
+ | 'QUICK'
851
+ | 'BOUNCY'
852
+ | 'SLOW'
853
+ | 'CUSTOM_SPRING'
854
+
855
+ /** A cubic-bezier easing's handles, from (0,0) to (1,1). */
856
+ export interface PrototypeCubicBezier {
857
+ x1: number
858
+ y1: number
859
+ x2: number
860
+ y2: number
861
+ }
862
+
863
+ /** A spring's physical parameters, as Figma's plugin API names them. */
864
+ export interface PrototypeSpring {
865
+ mass: number
866
+ stiffness: number
867
+ damping: number
868
+ /** The starting velocity, in the same units as the animated value. */
869
+ initialVelocity: number
870
+ }
793
871
 
794
872
  /** Where an overlay sits over the screen, as `OverlayPositionType`. */
795
873
  export type PrototypeOverlayPositionType =
@@ -807,6 +885,10 @@ export interface PrototypeTransition {
807
885
  duration: number
808
886
  /** Absent eases as Figma's default does, `DEFAULT_PROTOTYPE_EASING`. */
809
887
  easing?: PrototypeEasingType
888
+ /** The handles a `CUSTOM_CUBIC_BEZIER` transition plays with. */
889
+ easingCurve?: PrototypeCubicBezier
890
+ /** The parameters a `CUSTOM_SPRING` transition plays with. */
891
+ easingSpring?: PrototypeSpring
810
892
  }
811
893
 
812
894
  /** A reaction's move to another frame, the overlay it opens, the variant or layer it shows. */
@@ -833,7 +915,39 @@ export interface PrototypeCloseAction {
833
915
  type: 'CLOSE'
834
916
  }
835
917
 
836
- export type PrototypeAction = PrototypeNodeAction | PrototypeBackAction | PrototypeCloseAction
918
+ /** Writes a variable for the rest of the play session. */
919
+ export interface PrototypeSetVariableAction {
920
+ type: 'SET_VARIABLE'
921
+ variableId: string | null
922
+ value: PrototypeVariableValue
923
+ }
924
+
925
+ /** Switches a collection's mode on the frame the reaction runs from, for the play session. */
926
+ export interface PrototypeSetVariableModeAction {
927
+ type: 'SET_VARIABLE_MODE'
928
+ collectionId: string | null
929
+ modeId: string | null
930
+ }
931
+
932
+ /** One branch of a Conditional action; the last branch has no condition and is the else. */
933
+ export interface PrototypeConditionalBlock {
934
+ condition?: PrototypeVariableValue
935
+ actions: PrototypeAction[]
936
+ }
937
+
938
+ /** Runs the first branch whose condition is true. */
939
+ export interface PrototypeConditionalAction {
940
+ type: 'CONDITIONAL'
941
+ blocks: PrototypeConditionalBlock[]
942
+ }
943
+
944
+ export type PrototypeAction =
945
+ | PrototypeNodeAction
946
+ | PrototypeBackAction
947
+ | PrototypeCloseAction
948
+ | PrototypeSetVariableAction
949
+ | PrototypeSetVariableModeAction
950
+ | PrototypeConditionalAction
837
951
 
838
952
  /** What starts a reaction, and after how long an AFTER_TIMEOUT or mouse enter or leave does. */
839
953
  export interface PrototypeTrigger {
@@ -852,6 +966,88 @@ export interface PrototypeReaction {
852
966
  actions: PrototypeAction[]
853
967
  }
854
968
 
969
+ /** A property an annotation pins to the layer, in Figma's own names. */
970
+ export type AnnotationPropertyName =
971
+ | 'fills'
972
+ | 'strokes'
973
+ | 'effects'
974
+ | 'width'
975
+ | 'height'
976
+ | 'minWidth'
977
+ | 'minHeight'
978
+ | 'maxWidth'
979
+ | 'maxHeight'
980
+ | 'strokeWeight'
981
+ | 'cornerRadius'
982
+ | 'textStyleId'
983
+ | 'textAlignHorizontal'
984
+ | 'fontFamily'
985
+ | 'fontStyle'
986
+ | 'fontSize'
987
+ | 'fontWeight'
988
+ | 'lineHeight'
989
+ | 'letterSpacing'
990
+ | 'itemSpacing'
991
+ | 'padding'
992
+ | 'layoutMode'
993
+ | 'alignItems'
994
+ | 'opacity'
995
+ | 'mainComponent'
996
+ | 'gridRowGap'
997
+ | 'gridColumnGap'
998
+ | 'gridRowCount'
999
+ | 'gridColumnCount'
1000
+ | 'gridRowAnchorIndex'
1001
+ | 'gridColumnAnchorIndex'
1002
+ | 'gridRowSpan'
1003
+ | 'gridColumnSpan'
1004
+
1005
+ /** The edge of a layer a measurement starts from. */
1006
+ export type AnnotationMeasurementSide = 'TOP' | 'BOTTOM' | 'LEFT' | 'RIGHT'
1007
+
1008
+ /** A Dev Mode note pinned to a layer; `labelMarkdown` is the rich form, `label` its text. */
1009
+ export interface Annotation {
1010
+ label: string
1011
+ labelMarkdown: string
1012
+ properties: AnnotationPropertyName[]
1013
+ categoryId: string | null
1014
+ }
1015
+
1016
+ /** A Dev Mode distance between two layers, from `fromNodeId`'s `fromNodeSide`. */
1017
+ export interface AnnotationMeasurement {
1018
+ id: string
1019
+ fromNodeId: string
1020
+ toNodeId: string
1021
+ fromNodeSide: AnnotationMeasurementSide
1022
+ /** Whether the end is drawn on the matching side of `toNodeId`. */
1023
+ toSameSide: boolean
1024
+ /** INNER offset, relative to the gap (0–1). */
1025
+ innerOffsetRelative: number
1026
+ /** OUTER offset, a fixed distance in pixels. */
1027
+ outerOffsetFixed: number
1028
+ freeText: string
1029
+ }
1030
+
1031
+ /** Figma's annotation category colours, as the plugin API names them. */
1032
+ export type AnnotationCategoryColor =
1033
+ | 'yellow'
1034
+ | 'orange'
1035
+ | 'red'
1036
+ | 'pink'
1037
+ | 'violet'
1038
+ | 'blue'
1039
+ | 'teal'
1040
+ | 'green'
1041
+
1042
+ /** A document-level annotation category, preset (by Figma) or custom. */
1043
+ export interface AnnotationCategory {
1044
+ id: string
1045
+ label: string
1046
+ color: AnnotationCategoryColor
1047
+ isPreset: boolean
1048
+ customColor: Color | null
1049
+ }
1050
+
855
1051
  export interface SceneNode {
856
1052
  id: string
857
1053
  type: NodeType
@@ -1036,6 +1232,10 @@ export interface SceneNode {
1036
1232
  gridColumnGap: number
1037
1233
  gridRowGap: number
1038
1234
  gridPosition: GridPosition | null
1235
+ /** Horizontal alignment inside the cell; `AUTO` leaves it to the layer's size. */
1236
+ gridChildHorizontalAlign: GridChildAlign
1237
+ /** Vertical alignment inside the cell; `AUTO` leaves it to the layer's size. */
1238
+ gridChildVerticalAlign: GridChildAlign
1039
1239
 
1040
1240
  counterAxisAlignContent: 'AUTO' | 'SPACE_BETWEEN'
1041
1241
  itemReverseZIndex: boolean
@@ -1079,6 +1279,10 @@ export interface SceneNode {
1079
1279
  isExposedInstance: boolean
1080
1280
  symbolDescription: string
1081
1281
  symbolLinks: SymbolLink[]
1282
+ /** Dev Mode notes on the layer. */
1283
+ annotations: Annotation[]
1284
+ /** Dev Mode distances that start at this layer. */
1285
+ measurements: AnnotationMeasurement[]
1082
1286
  variantPropSpecs: VariantPropSpec[]
1083
1287
 
1084
1288
  boundVariables: Record<string, string>
@@ -1,13 +1,21 @@
1
1
  import { isEqual } from 'es-toolkit/predicate'
2
2
 
3
3
  import type { VariableModeFallback } from '.'
4
+ import { copyEffects, copyLayoutGrids } from '../copy'
5
+ import { isShadowEffect } from '../effects'
4
6
  import type { SceneGraph } from '../graph'
5
7
  import { readIcon } from '../icons/model'
6
8
  import { ICON_NAME } from '../icons/schema'
7
9
  import { hasInstanceOverride } from '../instances/overrides'
8
10
  import { pixelLength } from '../text/lengths'
9
- import type { SceneNode, StyleRun, TextLength } from '../types'
10
- import { NUMERIC_FIELDS, isNumericVariableBindingField } from './fields'
11
+ import type { Effect, LayoutGrid, SceneNode, ShadowEffect, StyleRun, TextLength } from '../types'
12
+ import {
13
+ effectBindingTarget,
14
+ effectColorBindingTarget,
15
+ isNumericVariableBindingField,
16
+ layoutGridBindingTarget,
17
+ NUMERIC_FIELDS
18
+ } from './fields'
11
19
  import { refreshBoundPaints } from './paints'
12
20
  import { createVariableResolver, type VariableResolver } from './resolver'
13
21
  import { subtreeNodes } from './subtree'
@@ -67,6 +75,19 @@ function resolverFor(graph: SceneGraph, resolver?: VariableResolver): VariableRe
67
75
  return resolver ?? createVariableResolver(graph)
68
76
  }
69
77
 
78
+ function resolveBindings(
79
+ graph: SceneGraph,
80
+ node: SceneNode,
81
+ fallback: VariableModeFallback,
82
+ resolver: VariableResolver | undefined,
83
+ resolve: (reader: ReturnType<VariableResolver['forNode']>, updates: Partial<SceneNode>) => void
84
+ ): Partial<SceneNode> {
85
+ const updates: Partial<SceneNode> = {}
86
+ const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
87
+ resolve(reader, updates)
88
+ return updates
89
+ }
90
+
70
91
  /**
71
92
  * The values a node's numeric bindings resolve to in its mode, in scene units, for the fields
72
93
  * whose stored value differs. A width or height an instance overrides keeps its own value.
@@ -77,24 +98,123 @@ export function resolvedNumericBindings(
77
98
  fallback: VariableModeFallback = 'active',
78
99
  resolver?: VariableResolver
79
100
  ): Partial<SceneNode> {
80
- const updates: Partial<SceneNode> = {}
81
- const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
101
+ return resolveBindings(graph, node, fallback, resolver, (reader, updates) => {
102
+ for (const [field, variableId] of Object.entries(node.boundVariables)) {
103
+ if (!NUMERIC_FIELDS.has(field)) continue
104
+ if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))
105
+ continue
106
+ const value = reader.number(variableId)
107
+ if (value === undefined || !Number.isFinite(value)) continue
108
+ const scale = node.variableBindingScales[field] ?? 1
109
+ const effective = value * scale
110
+ if (!Number.isFinite(effective)) continue
111
+ const next = boundNumberValue(
112
+ field,
113
+ field === 'opacity' ? Math.max(0, Math.min(1, effective)) : effective
114
+ )
115
+ if (!isEqual(node[field as keyof SceneNode], next)) Object.assign(updates, { [field]: next })
116
+ }
117
+ const effects = nextEffectsFromBindings(node, reader)
118
+ if (effects) updates.effects = effects
119
+ const layoutGrids = nextLayoutGridsFromBindings(node, reader)
120
+ if (layoutGrids) updates.layoutGrids = layoutGrids
121
+ })
122
+ }
123
+
124
+ /**
125
+ * A shadow's colour after a colour variable bound to it, or null when none changes. Figma's
126
+ * shadow colour includes its opacity (`Effect.color`, developer docs), so the variable's own alpha
127
+ * is the shadow's alpha: there is no separate shadow opacity for it to lose to.
128
+ */
129
+ export function resolvedEffectColorBindings(
130
+ graph: SceneGraph,
131
+ node: SceneNode,
132
+ resolver?: VariableResolver
133
+ ): Effect[] | null {
134
+ const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id)
135
+ let next: Effect[] | null = null
82
136
  for (const [field, variableId] of Object.entries(node.boundVariables)) {
83
- if (!NUMERIC_FIELDS.has(field)) continue
84
- if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))
85
- continue
137
+ const target = effectColorBindingTarget(field)
138
+ if (!target) continue
139
+ const color = reader.color(variableId)
140
+ if (!color) continue
141
+ const effect = (next ?? node.effects).at(target.index)
142
+ if (!effect || !('color' in effect) || isEqual(effect.color, color)) continue
143
+ next ??= copyEffects(node.effects)
144
+ next[target.index] = { ...effect, color } as Effect
145
+ }
146
+ return next
147
+ }
148
+
149
+ /** A shadow's numeric list values after the bindings on them, or null when none changes. */
150
+ function nextEffectsFromBindings(
151
+ node: SceneNode,
152
+ reader: ReturnType<VariableResolver['forNode']>
153
+ ): Effect[] | null {
154
+ let next: Effect[] | null = null
155
+ for (const [field, variableId] of Object.entries(node.boundVariables)) {
156
+ const target = effectBindingTarget(field)
157
+ if (!target) continue
158
+ const current = (next ?? node.effects).at(target.index)
159
+ if (!current || !isShadowEffect(current)) continue
86
160
  const value = reader.number(variableId)
87
161
  if (value === undefined || !Number.isFinite(value)) continue
88
- const scale = node.variableBindingScales[field] ?? 1
89
- const effective = value * scale
90
- if (!Number.isFinite(effective)) continue
91
- const next = boundNumberValue(
92
- field,
93
- field === 'opacity' ? Math.max(0, Math.min(1, effective)) : effective
94
- )
95
- if (!isEqual(node[field as keyof SceneNode], next)) Object.assign(updates, { [field]: next })
162
+ const shadow = current
163
+ const changed: ShadowEffect = { ...shadow }
164
+ switch (target.field) {
165
+ case 'radius':
166
+ changed.radius = Math.max(0, value)
167
+ break
168
+ case 'spread':
169
+ changed.spread = value
170
+ break
171
+ case 'offsetX':
172
+ changed.offset = { ...shadow.offset, x: value }
173
+ break
174
+ case 'offsetY':
175
+ changed.offset = { ...shadow.offset, y: value }
176
+ break
177
+ }
178
+ if (isEqual(shadow, changed)) continue
179
+ next ??= copyEffects(node.effects)
180
+ next[target.index] = changed
96
181
  }
97
- return updates
182
+ return next
183
+ }
184
+
185
+ /** A layout grid's numeric values after the bindings on them, or null when none changes. */
186
+ function nextLayoutGridsFromBindings(
187
+ node: SceneNode,
188
+ reader: ReturnType<VariableResolver['forNode']>
189
+ ): LayoutGrid[] | null {
190
+ let next: LayoutGrid[] | null = null
191
+ for (const [field, variableId] of Object.entries(node.boundVariables)) {
192
+ const target = layoutGridBindingTarget(field)
193
+ if (!target) continue
194
+ const current = (next ?? node.layoutGrids).at(target.index)
195
+ if (!current) continue
196
+ const value = reader.number(variableId)
197
+ if (value === undefined || !Number.isFinite(value)) continue
198
+ const changed: LayoutGrid = { ...current }
199
+ switch (target.field) {
200
+ case 'count':
201
+ changed.count = Math.max(1, Math.round(value))
202
+ break
203
+ case 'gutterSize':
204
+ changed.gutterSize = Math.max(0, value)
205
+ break
206
+ case 'offset':
207
+ changed.offset = Math.max(0, value)
208
+ break
209
+ case 'sectionSize':
210
+ changed.sectionSize = Math.max(0, value)
211
+ break
212
+ }
213
+ if (isEqual(current, changed)) continue
214
+ next ??= copyLayoutGrids(node.layoutGrids)
215
+ next[target.index] = changed
216
+ }
217
+ return next
98
218
  }
99
219
 
100
220
  /**
@@ -110,21 +230,20 @@ export function resolvedValueBindings(
110
230
  fallback: VariableModeFallback = 'active',
111
231
  resolver?: VariableResolver
112
232
  ): Partial<SceneNode> {
113
- const updates: Partial<SceneNode> = {}
114
- const reader = resolverFor(graph, resolver).forNode(node.id, fallback)
115
- const resolve = (field: 'text' | 'fontFamily' | 'visible') => {
116
- const variableId = node.boundVariables[field]
117
- if (!variableId || overriddenInInstance(graph, node.id, field)) return undefined
118
- return reader.value(variableId)
119
- }
120
- const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
121
- if (text !== undefined && text !== node.text) updates.text = text
122
- const fontFamily = node.type === 'TEXT' ? variableFontFamily(resolve('fontFamily')) : undefined
123
- if (fontFamily !== undefined && fontFamily !== node.fontFamily) updates.fontFamily = fontFamily
124
- // Figma binds visibility to text too, and shows the layer when the text reads as true.
125
- const visible = variableTruthy(resolve('visible'))
126
- if (visible !== undefined && visible !== node.visible) updates.visible = visible
127
- return updates
233
+ return resolveBindings(graph, node, fallback, resolver, (reader, updates) => {
234
+ const resolve = (field: 'text' | 'fontFamily' | 'visible') => {
235
+ const variableId = node.boundVariables[field]
236
+ if (!variableId || overriddenInInstance(graph, node.id, field)) return undefined
237
+ return reader.value(variableId)
238
+ }
239
+ const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined
240
+ if (text !== undefined && text !== node.text) updates.text = text
241
+ const fontFamily = node.type === 'TEXT' ? variableFontFamily(resolve('fontFamily')) : undefined
242
+ if (fontFamily !== undefined && fontFamily !== node.fontFamily) updates.fontFamily = fontFamily
243
+ // Figma binds visibility to text too, and shows the layer when the text reads as true.
244
+ const visible = variableTruthy(resolve('visible'))
245
+ if (visible !== undefined && visible !== node.visible) updates.visible = visible
246
+ })
128
247
  }
129
248
 
130
249
  /**
@@ -268,11 +387,14 @@ export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScop
268
387
  const resolver = createVariableResolver(graph)
269
388
  for (const node of nodes) refreshBoundPaints(graph, node, resolver)
270
389
  for (const node of nodes) {
390
+ // A colour binding refreshes the effect list the numeric resolver then adds to.
391
+ const effects = resolvedEffectColorBindings(graph, node, resolver)
271
392
  const updates = {
272
- ...resolvedNumericBindings(graph, node, 'active', resolver),
393
+ ...resolvedNumericBindings(graph, effects ? { ...node, effects } : node, 'active', resolver),
273
394
  ...resolvedValueBindings(graph, node, 'active', resolver),
274
395
  ...resolvedRunBindings(graph, node, 'active', resolver)
275
396
  }
397
+ if (!updates.effects && effects) updates.effects = effects
276
398
  if (Object.keys(updates).length === 0) continue
277
399
  graph.updateNode(node.id, updates)
278
400
  changed.push(node.id)
@@ -281,7 +403,7 @@ export function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScop
281
403
  }
282
404
 
283
405
  /** A layer list whose items carry their own bindings, keyed by position such as `fills/1/color`. */
284
- export type BoundLayerList = 'fills' | 'strokes' | 'effects'
406
+ export type BoundLayerList = 'fills' | 'strokes' | 'effects' | 'layoutGrids'
285
407
 
286
408
  /**
287
409
  * `boundVariables` once the `list` items are rebuilt from `sources`, where `sources[i]` is the
@@ -32,11 +32,50 @@ export const NUMERIC_FIELDS = new Set([
32
32
  'itemSpacing',
33
33
  'counterAxisSpacing',
34
34
  'gridRowGap',
35
- 'gridColumnGap'
35
+ 'gridColumnGap',
36
+ 'paragraphSpacing',
37
+ 'paragraphIndent'
36
38
  ])
37
39
 
40
+ /** The effect a colour binding path names, or null for a field that binds anything else. */
41
+ export function effectColorBindingTarget(field: string): { index: number } | null {
42
+ const match = /^effects\/(\d+)\/color$/.exec(field)
43
+ return match ? { index: Number(match[1]) } : null
44
+ }
45
+
46
+ /** The part of a shadow the field of `effects/<n>/<field>` addresses. */
47
+ export type EffectBindingField = 'radius' | 'spread' | 'offsetX' | 'offsetY'
48
+
49
+ const EFFECT_BINDING_FIELD = /^effects\/(\d+)\/(radius|spread|offsetX|offsetY)$/
50
+
51
+ /** The effect and shadow part a binding path names, or null for a field that binds anything else. */
52
+ export function effectBindingTarget(
53
+ field: string
54
+ ): { index: number; field: EffectBindingField } | null {
55
+ const match = EFFECT_BINDING_FIELD.exec(field)
56
+ return match ? { index: Number(match[1]), field: match[2] as EffectBindingField } : null
57
+ }
58
+
59
+ /** The part of a layout grid the field of `layoutGrids/<n>/<field>` addresses. */
60
+ export type LayoutGridBindingField = 'count' | 'gutterSize' | 'offset' | 'sectionSize'
61
+
62
+ const LAYOUT_GRID_BINDING_FIELD = /^layoutGrids\/(\d+)\/(count|gutterSize|offset|sectionSize)$/
63
+
64
+ /** The grid and field a binding path names, or null for a field that binds anything else. */
65
+ export function layoutGridBindingTarget(
66
+ field: string
67
+ ): { index: number; field: LayoutGridBindingField } | null {
68
+ const match = LAYOUT_GRID_BINDING_FIELD.exec(field)
69
+ return match ? { index: Number(match[1]), field: match[2] as LayoutGridBindingField } : null
70
+ }
71
+
72
+ /** A scalar field or an effect/grid list field a number variable can bind. */
38
73
  export function isNumericVariableBindingField(field: string): boolean {
39
- return NUMERIC_FIELDS.has(field)
74
+ return (
75
+ NUMERIC_FIELDS.has(field) ||
76
+ effectBindingTarget(field) !== null ||
77
+ layoutGridBindingTarget(field) !== null
78
+ )
40
79
  }
41
80
 
42
81
  /** Text content and font family are the string-valued bindings a node accepts. */