@pairdraw/scene-graph 0.18.0 → 0.19.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.
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +6 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +14 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +1 -0
- package/dist/copy.js.map +1 -1
- package/dist/css/gradient.d.ts +23 -0
- package/dist/css/gradient.d.ts.map +1 -0
- package/dist/css/gradient.js +404 -0
- package/dist/css/gradient.js.map +1 -0
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +2 -1
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +2 -13
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/values.js +24 -1
- package/dist/css/values.js.map +1 -1
- package/dist/font-style.d.ts +2 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +31 -6
- package/dist/font-style.js.map +1 -1
- package/dist/geometry.d.ts +10 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +21 -6
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +49 -3
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +179 -17
- package/dist/graph.js.map +1 -1
- package/dist/hit-test.d.ts +3 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +38 -23
- package/dist/hit-test.js.map +1 -1
- package/dist/icons/index.d.ts +2 -2
- package/dist/icons/index.js +2 -2
- package/dist/icons/model.d.ts +8 -2
- package/dist/icons/model.d.ts.map +1 -1
- package/dist/icons/model.js +10 -1
- package/dist/icons/model.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/assignments.js +40 -6
- package/dist/instances/assignments.js.map +1 -1
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +18 -1
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +38 -2
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +134 -15
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/overrides.d.ts +10 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +28 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/placement.js +1 -1
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap-overrides.js +94 -0
- package/dist/instances/swap-overrides.js.map +1 -0
- package/dist/instances/sync.js +90 -12
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/draw-order.d.ts +11 -0
- package/dist/layout/draw-order.d.ts.map +1 -0
- package/dist/layout/draw-order.js +14 -0
- package/dist/layout/draw-order.js.map +1 -0
- package/dist/layout/index.d.ts +2 -1
- package/dist/layout/index.js +2 -1
- package/dist/node-defaults.d.ts +11 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +16 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/order-keys.d.ts +3 -1
- package/dist/order-keys.d.ts.map +1 -1
- package/dist/order-keys.js +6 -1
- package/dist/order-keys.js.map +1 -1
- package/dist/resize.d.ts +29 -9
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +95 -62
- package/dist/resize.js.map +1 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +1 -8
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/authoring.d.ts +3 -2
- package/dist/slots/authoring.d.ts.map +1 -1
- package/dist/slots/authoring.js +5 -3
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +4 -1
- package/dist/slots/content.js.map +1 -1
- package/dist/source-metadata.d.ts +4 -2
- package/dist/source-metadata.d.ts.map +1 -1
- package/dist/source-metadata.js +10 -3
- package/dist/source-metadata.js.map +1 -1
- package/dist/strokes.d.ts +39 -0
- package/dist/strokes.d.ts.map +1 -0
- package/dist/strokes.js +104 -0
- package/dist/strokes.js.map +1 -0
- package/dist/text/direction.d.ts +6 -2
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +2 -1
- package/dist/text/direction.js.map +1 -1
- package/dist/text/index.d.ts +3 -3
- package/dist/text/index.js +3 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +22 -6
- package/dist/text/overflow.d.ts.map +1 -1
- package/dist/text/overflow.js +24 -6
- package/dist/text/overflow.js.map +1 -1
- package/dist/transfer/nodes.js +9 -5
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +18 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +36 -1
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +52 -2
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +14 -6
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +42 -67
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/formula/signatures.js +1 -1
- package/dist/variables/index.d.ts +10 -4
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +58 -15
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +101 -0
- package/dist/variables/paints.d.ts.map +1 -0
- package/dist/variables/paints.js +313 -0
- package/dist/variables/paints.js.map +1 -0
- package/dist/variables/resolution.d.ts +14 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +44 -4
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +17 -0
- package/dist/variables/subtree.js.map +1 -0
- package/package.json +6 -1
- package/src/components/properties.ts +17 -2
- package/src/copy.ts +1 -0
- package/src/css/gradient.ts +438 -0
- package/src/css/index.ts +1 -0
- package/src/css/shadows.ts +2 -18
- package/src/css/values.ts +27 -1
- package/src/font-style.ts +38 -4
- package/src/geometry.ts +25 -6
- package/src/graph.ts +207 -17
- package/src/hit-test.ts +67 -24
- package/src/icons/model.ts +11 -1
- package/src/instances/assignments.ts +65 -7
- package/src/instances/fields.ts +21 -0
- package/src/instances/index.ts +237 -14
- package/src/instances/overrides.ts +36 -1
- package/src/instances/placement.ts +5 -1
- package/src/instances/swap-overrides.ts +123 -0
- package/src/instances/sync.ts +139 -12
- package/src/layout/draw-order.ts +13 -0
- package/src/layout/index.ts +1 -0
- package/src/node-defaults.ts +21 -0
- package/src/order-keys.ts +6 -0
- package/src/resize.ts +133 -69
- package/src/shared-styles.ts +1 -3
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +4 -1
- package/src/source-metadata.ts +18 -3
- package/src/strokes.ts +121 -0
- package/src/text/direction.ts +7 -5
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +30 -7
- package/src/transfer/nodes.ts +7 -5
- package/src/types.ts +17 -0
- package/src/undo.ts +79 -2
- package/src/variables/bindings.ts +52 -74
- package/src/variables/index.ts +80 -14
- package/src/variables/paints.ts +413 -0
- package/src/variables/resolution.ts +56 -5
- package/src/variables/subtree.ts +15 -0
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit/predicate'
|
|
2
|
+
|
|
3
|
+
import { copyFills, copyStrokes } from '../copy'
|
|
4
|
+
import type { SceneGraph } from '../graph'
|
|
5
|
+
import { hasInstanceOverride } from '../instances/overrides'
|
|
6
|
+
import type { Color } from '../primitives'
|
|
7
|
+
import type { Fill, SceneNode, Stroke } from '../types'
|
|
8
|
+
import { paintBindingTarget, type PaintBindingList } from './fields'
|
|
9
|
+
import { collectionsResolving, getNodeExplicitVariableModeId } from './resolution'
|
|
10
|
+
import { subtreeNodes } from './subtree'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Figma keeps a bound paint's color and opacity as a cache of its variable: it draws and reads
|
|
14
|
+
* what the paint holds, and writes the variable's color into it again only once the color the
|
|
15
|
+
* variable gives that layer changes, or the mode set for the layer changes, so an edit to the
|
|
16
|
+
* paint sticks until then. The color each binding last gave its paint, and the modes set for the
|
|
17
|
+
* layer then, live here, beside the graph and never saved, by layer and binding field. A layer
|
|
18
|
+
* without an entry, such as one just restored, counts its paint's own color as the last one
|
|
19
|
+
* applied, in the modes it has now.
|
|
20
|
+
*/
|
|
21
|
+
export interface AppliedColor {
|
|
22
|
+
readonly variableId: string
|
|
23
|
+
readonly color: Color
|
|
24
|
+
/** The mode set for the layer, or null, in each collection the variable resolves through. */
|
|
25
|
+
readonly modes: Readonly<Record<string, string | null>>
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const appliedColors = new WeakMap<SceneNode, Map<string, AppliedColor>>()
|
|
29
|
+
|
|
30
|
+
const PAINT_LISTS = ['fills', 'strokes'] as const
|
|
31
|
+
|
|
32
|
+
interface BoundPaint {
|
|
33
|
+
readonly field: string
|
|
34
|
+
readonly list: PaintBindingList
|
|
35
|
+
readonly index: number
|
|
36
|
+
readonly variableId: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The paints of `node` whose color is bound to a variable, each with a paint there to color. */
|
|
40
|
+
function boundPaints(node: SceneNode): BoundPaint[] {
|
|
41
|
+
const paints: BoundPaint[] = []
|
|
42
|
+
for (const [field, variableId] of Object.entries(node.boundVariables)) {
|
|
43
|
+
const target = paintBindingTarget(field)
|
|
44
|
+
if (target && node[target.list].at(target.index)) paints.push({ field, ...target, variableId })
|
|
45
|
+
}
|
|
46
|
+
return paints
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Whether a layer has a paint whose color is bound to a variable. */
|
|
50
|
+
export function hasBoundPaints(node: SceneNode): boolean {
|
|
51
|
+
for (const field in node.boundVariables) if (paintBindingTarget(field)) return true
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether a binding's paint holds a color this graph applied or recorded for it. A layer without
|
|
57
|
+
* an entry, such as one just imported or built with its bindings, has a stored color nothing
|
|
58
|
+
* vouches for: readers show its variable's color until a write or refresh records one.
|
|
59
|
+
*/
|
|
60
|
+
export function paintColorApplied(node: SceneNode, field: string): boolean {
|
|
61
|
+
return appliedColors.get(node)?.has(field) ?? false
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A paint's color with its opacity as alpha, as a variable's color carries it. */
|
|
65
|
+
function paintColor(paint: Fill): Color {
|
|
66
|
+
return { r: paint.color.r, g: paint.color.g, b: paint.color.b, a: paint.opacity }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Saved colors are single precision, so a color read back from a file still matches its source. */
|
|
70
|
+
const COLOR_TOLERANCE = 1e-6
|
|
71
|
+
|
|
72
|
+
function sameColor(a: Color, b: Color): boolean {
|
|
73
|
+
return (
|
|
74
|
+
Math.abs(a.r - b.r) <= COLOR_TOLERANCE &&
|
|
75
|
+
Math.abs(a.g - b.g) <= COLOR_TOLERANCE &&
|
|
76
|
+
Math.abs(a.b - b.b) <= COLOR_TOLERANCE &&
|
|
77
|
+
Math.abs(a.a - b.a) <= COLOR_TOLERANCE
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The modes set for a layer, or null, in the collections a variable resolves through. Figma
|
|
83
|
+
* refreshes a bound paint whenever one of them changes, even to a mode giving the same color.
|
|
84
|
+
*/
|
|
85
|
+
function explicitModes(
|
|
86
|
+
graph: SceneGraph,
|
|
87
|
+
node: SceneNode,
|
|
88
|
+
variableId: string
|
|
89
|
+
): Record<string, string | null> {
|
|
90
|
+
const modes: Record<string, string | null> = {}
|
|
91
|
+
for (const collectionId of collectionsResolving(graph, [variableId]))
|
|
92
|
+
modes[collectionId] = getNodeExplicitVariableModeId(graph, node.id, collectionId)
|
|
93
|
+
return modes
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Whether the color a binding last applied is stale: another color, or set in other modes. */
|
|
97
|
+
function staleApplied(
|
|
98
|
+
node: SceneNode,
|
|
99
|
+
bound: BoundPaint,
|
|
100
|
+
paint: Fill,
|
|
101
|
+
color: Color,
|
|
102
|
+
modes: Readonly<Record<string, string | null>>
|
|
103
|
+
): boolean {
|
|
104
|
+
const entry = appliedColors.get(node)?.get(bound.field)
|
|
105
|
+
if (entry?.variableId !== bound.variableId) return !sameColor(paintColor(paint), color)
|
|
106
|
+
return !sameColor(entry.color, color) || !isEqual(entry.modes, modes)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* A layer copied from another keeps the colors its bindings last applied there, so the copy's
|
|
111
|
+
* paints, edits included, stay as they are until their variables' colors for it change.
|
|
112
|
+
*/
|
|
113
|
+
export function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void {
|
|
114
|
+
const entries = appliedColors.get(source)
|
|
115
|
+
if (entries) appliedColors.set(copy, new Map(entries))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The colors copied layers' bindings last applied, by layer id, as a copy captured them. */
|
|
119
|
+
export type AppliedBoundPaints = ReadonlyMap<string, ReadonlyMap<string, AppliedColor>>
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The colors the bindings of the layers `nodeIds` names last applied, captured when they are
|
|
123
|
+
* copied, so a paste counts what the copied paints were applied from, not what the source has now.
|
|
124
|
+
*/
|
|
125
|
+
export function captureAppliedBoundPaints(
|
|
126
|
+
graph: SceneGraph,
|
|
127
|
+
nodeIds: Iterable<string>
|
|
128
|
+
): AppliedBoundPaints {
|
|
129
|
+
const captured = new Map<string, ReadonlyMap<string, AppliedColor>>()
|
|
130
|
+
for (const id of nodeIds) {
|
|
131
|
+
const node = graph.nodes.get(id)
|
|
132
|
+
const entries = node && appliedColors.get(node)
|
|
133
|
+
if (entries) captured.set(id, new Map(entries))
|
|
134
|
+
}
|
|
135
|
+
return captured
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Pasted layers keep their paints, edits included, as Figma's copies do. One copied in this
|
|
140
|
+
* document takes the colors its source's bindings had last applied when it was copied, so a paste
|
|
141
|
+
* into another mode refreshes them as a move there would; one from elsewhere counts the colors
|
|
142
|
+
* its variables give it now as applied, as a loaded document does. `copies` maps each copied
|
|
143
|
+
* layer's id to its copy's.
|
|
144
|
+
*/
|
|
145
|
+
export function keepPastedBoundPaints(
|
|
146
|
+
graph: SceneGraph,
|
|
147
|
+
copies: ReadonlyMap<string, string>,
|
|
148
|
+
applied: AppliedBoundPaints
|
|
149
|
+
): void {
|
|
150
|
+
for (const [sourceId, copyId] of copies) {
|
|
151
|
+
const copy = graph.nodes.get(copyId)
|
|
152
|
+
if (!copy || !hasBoundPaints(copy)) continue
|
|
153
|
+
const entries = applied.get(sourceId)
|
|
154
|
+
if (!entries) {
|
|
155
|
+
recordWrittenBoundPaints(graph, copy, { fills: copy.fills, strokes: copy.strokes })
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
appliedColors.set(copy, new Map(entries))
|
|
159
|
+
refresh(graph, copy, NONE_FORCED)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function recordApplied(
|
|
164
|
+
node: SceneNode,
|
|
165
|
+
bound: BoundPaint,
|
|
166
|
+
color: Color,
|
|
167
|
+
modes: Readonly<Record<string, string | null>>
|
|
168
|
+
): void {
|
|
169
|
+
let entries = appliedColors.get(node)
|
|
170
|
+
if (!entries) {
|
|
171
|
+
entries = new Map()
|
|
172
|
+
appliedColors.set(node, entries)
|
|
173
|
+
}
|
|
174
|
+
entries.set(bound.field, { variableId: bound.variableId, color: { ...color }, modes })
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */
|
|
178
|
+
function applyColor(paint: Fill, color: Color): void {
|
|
179
|
+
paint.color = { ...color, a: 1 }
|
|
180
|
+
paint.opacity = color.a
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Whether `paint` already shows `color` as a bound paint stores it. */
|
|
184
|
+
function showsColor(paint: Fill, color: Color): boolean {
|
|
185
|
+
return sameColor(paintColor(paint), color) && paint.color.a === 1
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
type PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Whether `node` is an instance, or a layer an instance copies from its component, with a paint
|
|
192
|
+
* list neither it nor an instance around it overrides: such paints follow the component, and
|
|
193
|
+
* refreshing them is no edit.
|
|
194
|
+
*/
|
|
195
|
+
export function copiesComponentPaints(
|
|
196
|
+
graph: SceneGraph,
|
|
197
|
+
node: SceneNode,
|
|
198
|
+
list: PaintBindingList
|
|
199
|
+
): boolean {
|
|
200
|
+
const walk = { copied: false }
|
|
201
|
+
const overriding = graph.closest(node.id, (current) => {
|
|
202
|
+
if (current.type !== 'INSTANCE') return false
|
|
203
|
+
walk.copied = true
|
|
204
|
+
return hasInstanceOverride(current.instanceOverrides, current.id, node.id, list)
|
|
205
|
+
})
|
|
206
|
+
return walk.copied && !overriding
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Writes refreshed paints. A copy's paints that follow its component are derived values that a
|
|
211
|
+
* save leaves to the component, not edits written over it.
|
|
212
|
+
*/
|
|
213
|
+
function writePaints(graph: SceneGraph, node: SceneNode, changes: PaintLists): void {
|
|
214
|
+
const derived = PAINT_LISTS.every(
|
|
215
|
+
(list) => !changes[list] || copiesComponentPaints(graph, node, list)
|
|
216
|
+
)
|
|
217
|
+
const write = () => graph.updateNode(node.id, changes)
|
|
218
|
+
if (derived) graph.applyComponentDerivedDuring(write)
|
|
219
|
+
else write()
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Writes into each bound paint the color its variable gives the layer now, where that differs
|
|
224
|
+
* from the color last applied, or wherever `forced` says, and records what each binding gives.
|
|
225
|
+
*/
|
|
226
|
+
function refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) => boolean): void {
|
|
227
|
+
let fills: Fill[] | undefined
|
|
228
|
+
let strokes: Stroke[] | undefined
|
|
229
|
+
for (const bound of boundPaints(node)) {
|
|
230
|
+
const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
|
|
231
|
+
const paint = node[bound.list].at(bound.index)
|
|
232
|
+
if (!color || !paint) continue
|
|
233
|
+
const modes = explicitModes(graph, node, bound.variableId)
|
|
234
|
+
const stale = staleApplied(node, bound, paint, color, modes)
|
|
235
|
+
recordApplied(node, bound, color, modes)
|
|
236
|
+
if ((!forced(bound.field) && !stale) || showsColor(paint, color)) continue
|
|
237
|
+
if (bound.list === 'fills') {
|
|
238
|
+
fills ??= copyFills(node.fills)
|
|
239
|
+
applyColor(fills[bound.index], color)
|
|
240
|
+
} else {
|
|
241
|
+
strokes ??= copyStrokes(node.strokes)
|
|
242
|
+
applyColor(strokes[bound.index], color)
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const changes: PaintLists = {}
|
|
246
|
+
if (fills) changes.fills = fills
|
|
247
|
+
if (strokes) changes.strokes = strokes
|
|
248
|
+
if (fills || strokes) writePaints(graph, node, changes)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const NONE_FORCED = (): boolean => false
|
|
252
|
+
const ALL_FORCED = (): boolean => true
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as
|
|
256
|
+
* opacity, once that color differs from the one last applied, discarding edits made since, as
|
|
257
|
+
* Figma refreshes them when a variable's value or a layer's mode changes.
|
|
258
|
+
*/
|
|
259
|
+
export function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void {
|
|
260
|
+
if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** 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)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Gives every bound paint in this subtree its variable's color where it sits. A new instance
|
|
270
|
+
* or a layer an instance sync adds is a copy, which shows its variables in its own context rather
|
|
271
|
+
* than whatever its component's paints hold.
|
|
272
|
+
*/
|
|
273
|
+
export function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void {
|
|
274
|
+
for (const node of subtreeNodes(graph, [rootId]))
|
|
275
|
+
if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** 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)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Records, for the paint lists a write sets, the colors their variables give the layer now, so a
|
|
285
|
+
* paint written with another color or opacity keeps it until that color changes.
|
|
286
|
+
*/
|
|
287
|
+
export function recordWrittenBoundPaints(
|
|
288
|
+
graph: SceneGraph,
|
|
289
|
+
node: SceneNode,
|
|
290
|
+
written: PaintLists
|
|
291
|
+
): void {
|
|
292
|
+
for (const bound of boundPaints(node)) {
|
|
293
|
+
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))
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The fills and strokes a layer shows: its stored paints, except a bound paint no applied color
|
|
301
|
+
* vouches for takes its variable's color for the layer, alpha as opacity, as Figma shows a layer
|
|
302
|
+
* whose saved cache is stale on import. Nothing is written or recorded; a copy pasted unlinked
|
|
303
|
+
* keeps the colors it showed.
|
|
304
|
+
*/
|
|
305
|
+
export function shownPaintBindings(
|
|
306
|
+
graph: SceneGraph,
|
|
307
|
+
node: SceneNode
|
|
308
|
+
): Pick<Partial<SceneNode>, 'fills' | 'strokes'> {
|
|
309
|
+
const changes: Pick<Partial<SceneNode>, 'fills' | 'strokes'> = {}
|
|
310
|
+
for (const bound of boundPaints(node)) {
|
|
311
|
+
if (paintColorApplied(node, bound.field)) continue
|
|
312
|
+
const color = graph.resolveColorVariableForNode(node.id, bound.variableId)
|
|
313
|
+
const paint = node[bound.list].at(bound.index)
|
|
314
|
+
if (!color || !paint || showsColor(paint, color)) continue
|
|
315
|
+
if (bound.list === 'fills') {
|
|
316
|
+
changes.fills ??= copyFills(node.fills)
|
|
317
|
+
applyColor(changes.fills[bound.index], color)
|
|
318
|
+
} else {
|
|
319
|
+
changes.strokes ??= copyStrokes(node.strokes)
|
|
320
|
+
applyColor(changes.strokes[bound.index], color)
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return changes
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose
|
|
328
|
+
* mode set the move changed, as Figma does when a layer enters a frame or page that sets another
|
|
329
|
+
* mode, or one where none was. A move between parents setting the same modes, or of layers that
|
|
330
|
+
* bind no paint, costs no more than comparing those modes.
|
|
331
|
+
*/
|
|
332
|
+
export function refreshMovedBoundPaints(
|
|
333
|
+
graph: SceneGraph,
|
|
334
|
+
nodeId: string,
|
|
335
|
+
previousParentId: string | null | undefined
|
|
336
|
+
): void {
|
|
337
|
+
if (graph.isApplyingImportedState || graph.variableCollections.size === 0) return
|
|
338
|
+
const parentId = graph.nodes.get(nodeId)?.parentId
|
|
339
|
+
if (!parentId) return
|
|
340
|
+
const previous = previousParentId && graph.nodes.has(previousParentId) ? previousParentId : null
|
|
341
|
+
const changed = new Set(
|
|
342
|
+
[...graph.variableCollections.keys()].filter(
|
|
343
|
+
(id) =>
|
|
344
|
+
!previous ||
|
|
345
|
+
getNodeExplicitVariableModeId(graph, previous, id) !==
|
|
346
|
+
getNodeExplicitVariableModeId(graph, parentId, id)
|
|
347
|
+
)
|
|
348
|
+
)
|
|
349
|
+
if (changed.size === 0) return
|
|
350
|
+
const reaches = new Map<string, boolean>()
|
|
351
|
+
const reachesChanged = (variableId: string) => {
|
|
352
|
+
let found = reaches.get(variableId)
|
|
353
|
+
if (found === undefined) {
|
|
354
|
+
found = [...collectionsResolving(graph, [variableId])].some((id) => changed.has(id))
|
|
355
|
+
reaches.set(variableId, found)
|
|
356
|
+
}
|
|
357
|
+
return found
|
|
358
|
+
}
|
|
359
|
+
for (const node of subtreeNodes(graph, [nodeId])) {
|
|
360
|
+
if (boundPaints(node).some((bound) => reachesChanged(bound.variableId)))
|
|
361
|
+
refresh(graph, node, NONE_FORCED)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function reboundPaint(
|
|
366
|
+
target: SceneNode,
|
|
367
|
+
bindings: Readonly<Record<string, string>>,
|
|
368
|
+
list: PaintBindingList
|
|
369
|
+
): boolean {
|
|
370
|
+
return target[list].some((_, index) => {
|
|
371
|
+
const field = `${list}/${index}/color`
|
|
372
|
+
return field in bindings && bindings[field] !== target.boundVariables[field]
|
|
373
|
+
})
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit
|
|
378
|
+
* to its paint's color or opacity does not reach them. An instance sync writing a copy's paints
|
|
379
|
+
* keeps the color and opacity of each one still bound to the same variable, and gives it the
|
|
380
|
+
* variable's color for the copy once that differs from the one last applied, or when it binds
|
|
381
|
+
* anew. `updates` holds what the sync writes, whose paints this sets in place.
|
|
382
|
+
*/
|
|
383
|
+
export function keepCopyBoundPaints(
|
|
384
|
+
graph: SceneGraph,
|
|
385
|
+
target: SceneNode,
|
|
386
|
+
updates: Partial<SceneNode>
|
|
387
|
+
): void {
|
|
388
|
+
const bindings = updates.boundVariables ?? target.boundVariables
|
|
389
|
+
for (const list of PAINT_LISTS) {
|
|
390
|
+
// A paint the component binds anew keeps its own paint otherwise, but takes the new color.
|
|
391
|
+
if (!updates[list] && reboundPaint(target, bindings, list))
|
|
392
|
+
Object.assign(updates, {
|
|
393
|
+
[list]: list === 'fills' ? copyFills(target.fills) : copyStrokes(target.strokes)
|
|
394
|
+
})
|
|
395
|
+
updates[list]?.forEach((paint, index) => {
|
|
396
|
+
const field = `${list}/${index}/color`
|
|
397
|
+
const variableId = bindings[field]
|
|
398
|
+
if (!variableId) return
|
|
399
|
+
const bound = { field, list, index, variableId }
|
|
400
|
+
const own = target[list].at(index)
|
|
401
|
+
const keeps = own !== undefined && target.boundVariables[field] === variableId
|
|
402
|
+
if (keeps) {
|
|
403
|
+
paint.color = { ...own.color }
|
|
404
|
+
paint.opacity = own.opacity
|
|
405
|
+
}
|
|
406
|
+
const color = graph.resolveColorVariableForNode(target.id, variableId)
|
|
407
|
+
if (!color) return
|
|
408
|
+
const modes = explicitModes(graph, target, variableId)
|
|
409
|
+
if (!keeps || staleApplied(target, bound, own, color, modes)) applyColor(paint, color)
|
|
410
|
+
recordApplied(target, bound, color, modes)
|
|
411
|
+
})
|
|
412
|
+
}
|
|
413
|
+
}
|
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
import type { SceneGraph } from '../graph'
|
|
2
2
|
import type { Color } from '../primitives'
|
|
3
|
-
import type { Variable, VariableLiteral, VariableValue } from '../types'
|
|
3
|
+
import type { SceneNode, Variable, VariableLiteral, VariableValue } from '../types'
|
|
4
4
|
import { MAX_FORMULA_DEPTH, VariableFormulaError } from './formula/tree'
|
|
5
|
-
import { resolveVariableValue } from './value'
|
|
5
|
+
import { resolveVariableValue, variableValueReferences } from './value'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The modes a node draws in for every collection: its ancestors' explicit modes from the outside
|
|
9
|
+
* in, then its own. A copy that re-parents a node, such as an export of one layer, keeps this on
|
|
10
|
+
* the node so it still resolves its variables as the canvas does.
|
|
11
|
+
*/
|
|
12
|
+
export function resolvedVariableModes(graph: SceneGraph, nodeId: string): Record<string, string> {
|
|
13
|
+
const chain: SceneNode[] = []
|
|
14
|
+
let node = graph.nodes.get(nodeId)
|
|
15
|
+
while (node) {
|
|
16
|
+
chain.push(node)
|
|
17
|
+
node = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
18
|
+
}
|
|
19
|
+
const modes: Record<string, string> = {}
|
|
20
|
+
for (const ancestor of chain.reverse()) Object.assign(modes, ancestor.variableModes)
|
|
21
|
+
return modes
|
|
22
|
+
}
|
|
6
23
|
|
|
7
24
|
export function getActiveModeId(graph: SceneGraph, collectionId: string): string {
|
|
8
25
|
const mode = graph.activeMode.get(collectionId)
|
|
@@ -23,15 +40,49 @@ export function getNodeVariableModeId(
|
|
|
23
40
|
collectionId: string,
|
|
24
41
|
fallback: VariableModeFallback = 'active'
|
|
25
42
|
): string {
|
|
43
|
+
const explicit = getNodeExplicitVariableModeId(graph, nodeId, collectionId)
|
|
44
|
+
if (explicit !== null) return explicit
|
|
45
|
+
if (fallback === 'default')
|
|
46
|
+
return graph.variableCollections.get(collectionId)?.defaultModeId ?? ''
|
|
47
|
+
return getActiveModeId(graph, collectionId)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The nearest mode set on a node or an ancestor for a collection, or null where none is. */
|
|
51
|
+
export function getNodeExplicitVariableModeId(
|
|
52
|
+
graph: SceneGraph,
|
|
53
|
+
nodeId: string,
|
|
54
|
+
collectionId: string
|
|
55
|
+
): string | null {
|
|
26
56
|
let node = graph.nodes.get(nodeId)
|
|
27
57
|
while (node) {
|
|
28
58
|
const modeId = node.variableModes[collectionId]
|
|
29
59
|
if (modeId) return modeId
|
|
30
60
|
node = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
31
61
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The collections whose modes decide what these variables resolve to: their own, and those of the
|
|
67
|
+
* variables they alias, at any depth and in any mode.
|
|
68
|
+
*/
|
|
69
|
+
export function collectionsResolving(
|
|
70
|
+
graph: SceneGraph,
|
|
71
|
+
variableIds: Iterable<string>
|
|
72
|
+
): Set<string> {
|
|
73
|
+
const pending = [...variableIds]
|
|
74
|
+
const seen = new Set<string>()
|
|
75
|
+
const collections = new Set<string>()
|
|
76
|
+
for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
|
|
77
|
+
if (seen.has(id)) continue
|
|
78
|
+
seen.add(id)
|
|
79
|
+
const variable = graph.variables.get(id)
|
|
80
|
+
if (!variable) continue
|
|
81
|
+
collections.add(variable.collectionId)
|
|
82
|
+
for (const value of Object.values(variable.valuesByMode))
|
|
83
|
+
pending.push(...variableValueReferences(value))
|
|
84
|
+
}
|
|
85
|
+
return collections
|
|
35
86
|
}
|
|
36
87
|
|
|
37
88
|
export function resolveVariable(
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SceneGraph } from '../graph'
|
|
2
|
+
import type { SceneNode } from '../types'
|
|
3
|
+
|
|
4
|
+
/** The layers in these subtrees, the roots included, that a binding change can reach. */
|
|
5
|
+
export function subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneNode[] {
|
|
6
|
+
const nodes: SceneNode[] = []
|
|
7
|
+
const pending = [...roots]
|
|
8
|
+
for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
|
|
9
|
+
const node = graph.getNode(id)
|
|
10
|
+
if (!node) continue
|
|
11
|
+
nodes.push(node)
|
|
12
|
+
pending.push(...node.childIds)
|
|
13
|
+
}
|
|
14
|
+
return nodes
|
|
15
|
+
}
|