@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/arrow-caps.d.ts.map +1 -1
- package/dist/arrow-caps.js +2 -5
- package/dist/arrow-caps.js.map +1 -1
- package/dist/asset-keys.d.ts +11 -1
- package/dist/asset-keys.d.ts.map +1 -1
- package/dist/asset-keys.js +16 -1
- package/dist/asset-keys.js.map +1 -1
- package/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/kinds.d.ts +11 -5
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +76 -75
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/layers.d.ts.map +1 -1
- package/dist/behaviours/layers.js +3 -2
- package/dist/behaviours/layers.js.map +1 -1
- package/dist/behaviours/model.d.ts +7 -4
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js +16 -9
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +118 -4
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +18 -3
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +3 -2
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/bindings.js +9 -1
- package/dist/bindings.js.map +1 -1
- package/dist/checkpoint.d.ts +31 -2
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +211 -10
- package/dist/checkpoint.js.map +1 -1
- package/dist/components/index.d.ts +3 -3
- package/dist/components/index.js +3 -3
- package/dist/components/properties.d.ts +18 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +66 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +16 -10
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +39 -13
- package/dist/components/variants.js.map +1 -1
- package/dist/connectors/hit.d.ts.map +1 -1
- package/dist/connectors/hit.js +1 -7
- package/dist/connectors/hit.js.map +1 -1
- package/dist/connectors/pieces.d.ts.map +1 -1
- package/dist/connectors/pieces.js +2 -5
- package/dist/connectors/pieces.js.map +1 -1
- package/dist/constants.d.ts +6 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +17 -1
- package/dist/constants.js.map +1 -1
- package/dist/constraints.d.ts +25 -0
- package/dist/constraints.d.ts.map +1 -0
- package/dist/constraints.js +51 -0
- package/dist/constraints.js.map +1 -0
- package/dist/coordinate.d.ts +64 -1
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +217 -1
- package/dist/coordinate.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -16
- package/dist/copy.js.map +1 -1
- package/dist/css/blend.d.ts +11 -0
- package/dist/css/blend.d.ts.map +1 -0
- package/dist/css/blend.js +30 -0
- package/dist/css/blend.js.map +1 -0
- package/dist/css/index.d.ts +4 -2
- package/dist/css/index.js +4 -2
- package/dist/css/text.d.ts +28 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +84 -17
- package/dist/css/text.js.map +1 -1
- package/dist/css/transform.d.ts +34 -0
- package/dist/css/transform.d.ts.map +1 -0
- package/dist/css/transform.js +320 -0
- package/dist/css/transform.js.map +1 -0
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +11 -1
- package/dist/css/values.js.map +1 -1
- package/dist/cubic.d.ts +7 -0
- package/dist/cubic.d.ts.map +1 -0
- package/dist/cubic.js +17 -0
- package/dist/cubic.js.map +1 -0
- package/dist/export-format.d.ts +14 -1
- package/dist/export-format.d.ts.map +1 -1
- package/dist/export-format.js +26 -1
- package/dist/export-format.js.map +1 -1
- package/dist/fields/text.d.ts +2 -2
- package/dist/fields/text.d.ts.map +1 -1
- package/dist/fields/text.js +2 -1
- package/dist/fields/text.js.map +1 -1
- package/dist/figjam/index.d.ts +2 -2
- package/dist/figjam/index.js +2 -2
- package/dist/figjam/palette.d.ts +13 -2
- package/dist/figjam/palette.d.ts.map +1 -1
- package/dist/figjam/palette.js +30 -2
- package/dist/figjam/palette.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/geometry.d.ts +37 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +80 -137
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +49 -12
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +125 -27
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +14 -4
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +76 -11
- package/dist/group-bounds.js.map +1 -1
- package/dist/guides.d.ts +3 -1
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +5 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts +5 -0
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +57 -38
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +27 -3
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +57 -3
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +8 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +1 -3
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +39 -30
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +204 -0
- package/dist/instances/kept-overrides.js.map +1 -0
- package/dist/instances/main-component.d.ts +3 -1
- package/dist/instances/main-component.d.ts.map +1 -1
- package/dist/instances/main-component.js +10 -2
- package/dist/instances/main-component.js.map +1 -1
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +18 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/placement.js +55 -12
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap.js +291 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +112 -39
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +2 -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/outline/flatten.d.ts +15 -0
- package/dist/outline/flatten.d.ts.map +1 -0
- package/dist/outline/flatten.js +189 -0
- package/dist/outline/flatten.js.map +1 -0
- package/dist/outline/heads.js +34 -0
- package/dist/outline/heads.js.map +1 -0
- package/dist/outline/hit.d.ts +21 -0
- package/dist/outline/hit.d.ts.map +1 -0
- package/dist/outline/hit.js +276 -0
- package/dist/outline/hit.js.map +1 -0
- package/dist/outline/index.d.ts +5 -0
- package/dist/outline/index.js +5 -0
- package/dist/outline/point.d.ts +16 -0
- package/dist/outline/point.d.ts.map +1 -0
- package/dist/outline/point.js +42 -0
- package/dist/outline/point.js.map +1 -0
- package/dist/outline/shapes.d.ts +35 -0
- package/dist/outline/shapes.d.ts.map +1 -0
- package/dist/outline/shapes.js +181 -0
- package/dist/outline/shapes.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +4 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +21 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/resize.d.ts +22 -11
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +227 -93
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +3 -2
- package/dist/scaling/index.js +3 -2
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/scaling/snapshot.d.ts +25 -0
- package/dist/scaling/snapshot.d.ts.map +1 -0
- package/dist/scaling/snapshot.js +39 -0
- package/dist/scaling/snapshot.js.map +1 -0
- package/dist/scaling/tree.d.ts +24 -2
- package/dist/scaling/tree.d.ts.map +1 -1
- package/dist/scaling/tree.js +38 -15
- package/dist/scaling/tree.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +2 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts +16 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +69 -2
- 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 +6 -1
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.d.ts +2 -0
- package/dist/slots/frames.d.ts.map +1 -1
- package/dist/slots/frames.js +7 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/text/direction.d.ts +3 -1
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +5 -1
- package/dist/text/direction.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- 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 +2 -2
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/picture.d.ts +1 -2
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +1 -4
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +1 -6
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +35 -3
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +44 -13
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +38 -0
- package/dist/transfer/dependencies.d.ts.map +1 -0
- package/dist/transfer/dependencies.js +100 -0
- package/dist/transfer/dependencies.js.map +1 -0
- package/dist/transfer/history.js +0 -1
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/index.d.ts +5 -3
- package/dist/transfer/index.js +5 -3
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +16 -19
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +0 -1
- package/dist/transfer/plan.js +3 -5
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +5 -2
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/resources.d.ts +21 -0
- package/dist/transfer/resources.d.ts.map +1 -0
- package/dist/transfer/resources.js +38 -0
- package/dist/transfer/resources.js.map +1 -0
- package/dist/transfer/variables.d.ts +8 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +24 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +73 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +28 -3
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +24 -7
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +37 -11
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +122 -74
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +19 -1
- package/dist/variables/fields.d.ts.map +1 -1
- package/dist/variables/fields.js +36 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/formula/check.d.ts +1 -1
- package/dist/variables/formula/index.d.ts +2 -2
- package/dist/variables/formula/index.js +1 -1
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/print.d.ts +1 -1
- package/dist/variables/formula/signatures.js +1 -1
- package/dist/variables/index.d.ts +7 -7
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +57 -26
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +57 -0
- package/dist/variables/paints.d.ts.map +1 -0
- package/dist/variables/paints.js +231 -0
- package/dist/variables/paints.js.map +1 -0
- package/dist/variables/resolution.d.ts +14 -11
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +36 -17
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +17 -0
- package/dist/variables/subtree.js.map +1 -0
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +2 -8
- package/dist/vector-network.js.map +1 -1
- package/package.json +14 -1
- package/src/arrow-caps.ts +2 -5
- package/src/asset-keys.ts +20 -0
- package/src/behaviours/kinds.ts +51 -34
- package/src/behaviours/layers.ts +4 -2
- package/src/behaviours/model.ts +22 -11
- package/src/behaviours/schema.ts +20 -4
- package/src/behaviours/spec.ts +8 -2
- package/src/bindings.ts +11 -2
- package/src/checkpoint.ts +319 -17
- package/src/components/properties.ts +101 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/constants.ts +17 -0
- package/src/constraints.ts +60 -0
- package/src/coordinate.ts +220 -0
- package/src/copy.ts +18 -19
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +18 -1
- package/src/css/text.ts +85 -17
- package/src/css/transform.ts +266 -0
- package/src/css/values.ts +8 -0
- package/src/cubic.ts +14 -0
- package/src/export-format.ts +29 -0
- package/src/fields/text.ts +3 -1
- package/src/figjam/palette.ts +29 -1
- package/src/generated/shape-with-text.ts +28 -2
- package/src/geometry.ts +97 -206
- package/src/graph.ts +155 -45
- package/src/group-bounds.ts +84 -17
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +93 -64
- package/src/images.ts +71 -3
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +62 -31
- package/src/instances/kept-overrides.ts +280 -0
- package/src/instances/main-component.ts +12 -0
- package/src/instances/overrides.ts +27 -1
- package/src/instances/placement.ts +63 -10
- package/src/instances/swap.ts +456 -0
- package/src/instances/sync.ts +176 -50
- package/src/node-defaults.ts +3 -1
- package/src/order-keys.ts +6 -0
- package/src/outline/flatten.ts +190 -0
- package/src/outline/heads.ts +52 -0
- package/src/outline/hit.ts +369 -0
- package/src/outline/index.ts +4 -0
- package/src/outline/point.ts +59 -0
- package/src/outline/shapes.ts +207 -0
- package/src/plugin-data/fields.ts +21 -2
- package/src/resize.ts +267 -119
- package/src/scaling/index.ts +9 -1
- package/src/scaling/node.ts +9 -1
- package/src/scaling/snapshot.ts +51 -0
- package/src/scaling/tree.ts +63 -19
- package/src/shaders/model.ts +2 -3
- package/src/shared-styles.ts +90 -1
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +6 -1
- package/src/slots/frames.ts +9 -7
- package/src/text/direction.ts +5 -0
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/apply.ts +1 -13
- package/src/transfer/copied.ts +83 -15
- package/src/transfer/dependencies.ts +126 -0
- package/src/transfer/history.ts +0 -6
- package/src/transfer/index.ts +20 -2
- package/src/transfer/nodes.ts +17 -31
- package/src/transfer/plan.ts +3 -11
- package/src/transfer/remove.ts +5 -2
- package/src/transfer/resources.ts +54 -0
- package/src/transfer/variables.ts +34 -0
- package/src/types.ts +83 -1
- package/src/undo.ts +48 -10
- package/src/variables/bindings.ts +152 -88
- package/src/variables/fields.ts +46 -1
- package/src/variables/formula/index.ts +3 -1
- package/src/variables/index.ts +90 -39
- package/src/variables/paints.ts +296 -0
- package/src/variables/resolution.ts +39 -28
- package/src/variables/subtree.ts +15 -0
- package/src/vector-network.ts +4 -2
package/dist/undo.js
CHANGED
|
@@ -10,9 +10,13 @@ var UndoManager = class {
|
|
|
10
10
|
settlers = /* @__PURE__ */ new Set();
|
|
11
11
|
limit;
|
|
12
12
|
onChange;
|
|
13
|
+
onDiscard;
|
|
14
|
+
runReplay;
|
|
13
15
|
constructor(options = {}) {
|
|
14
16
|
this.limit = options.limit ?? 200;
|
|
15
17
|
this.onChange = options.onChange;
|
|
18
|
+
this.onDiscard = options.onDiscard;
|
|
19
|
+
this.runReplay = options.runReplay ?? ((replay) => replay());
|
|
16
20
|
}
|
|
17
21
|
apply(entry) {
|
|
18
22
|
this.execute(entry);
|
|
@@ -58,7 +62,7 @@ var UndoManager = class {
|
|
|
58
62
|
if (!entry) return null;
|
|
59
63
|
this.replay(() => entry.inverse());
|
|
60
64
|
this.redoStack.push(entry);
|
|
61
|
-
this.onChange?.();
|
|
65
|
+
this.onChange?.("undo");
|
|
62
66
|
return entry.label;
|
|
63
67
|
}
|
|
64
68
|
redo() {
|
|
@@ -67,7 +71,7 @@ var UndoManager = class {
|
|
|
67
71
|
if (!entry) return null;
|
|
68
72
|
this.replay(() => entry.forward());
|
|
69
73
|
this.undoStack.push(entry);
|
|
70
|
-
this.onChange?.();
|
|
74
|
+
this.onChange?.("redo");
|
|
71
75
|
return entry.label;
|
|
72
76
|
}
|
|
73
77
|
beginBatch(label, coalesceKey) {
|
|
@@ -97,9 +101,21 @@ var UndoManager = class {
|
|
|
97
101
|
}
|
|
98
102
|
}
|
|
99
103
|
rollbackBatch() {
|
|
100
|
-
const batch = this.batches.
|
|
104
|
+
const batch = this.batches.at(-1);
|
|
101
105
|
if (!batch) return;
|
|
102
|
-
|
|
106
|
+
try {
|
|
107
|
+
for (const entry of batch.entries.toReversed()) entry.inverse();
|
|
108
|
+
} finally {
|
|
109
|
+
this.batches.pop();
|
|
110
|
+
}
|
|
111
|
+
this.onDiscard?.();
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Reports an edit that ended without a step, rolled back or changing nothing, as a rolled-back
|
|
115
|
+
* batch does. Called once the edit has closed, so `isRecording` tells whether another is open.
|
|
116
|
+
*/
|
|
117
|
+
discardEdit() {
|
|
118
|
+
this.onDiscard?.();
|
|
103
119
|
}
|
|
104
120
|
/** Abandon provisional history without replaying it or changing committed undo/redo entries. */
|
|
105
121
|
discardBatches() {
|
|
@@ -109,7 +125,7 @@ var UndoManager = class {
|
|
|
109
125
|
this.undoStack = [];
|
|
110
126
|
this.redoStack = [];
|
|
111
127
|
this.discardBatches();
|
|
112
|
-
this.onChange?.();
|
|
128
|
+
this.onChange?.("clear");
|
|
113
129
|
}
|
|
114
130
|
/** Whether undo or redo is replaying a step, whose changes are history rather than new edits. */
|
|
115
131
|
get isReplaying() {
|
|
@@ -135,12 +151,13 @@ var UndoManager = class {
|
|
|
135
151
|
entry.inverse();
|
|
136
152
|
inverse();
|
|
137
153
|
};
|
|
154
|
+
this.onChange?.("join", last);
|
|
138
155
|
return true;
|
|
139
156
|
}
|
|
140
157
|
replay(step) {
|
|
141
158
|
this.replaying++;
|
|
142
159
|
try {
|
|
143
|
-
step
|
|
160
|
+
this.runReplay(step);
|
|
144
161
|
} finally {
|
|
145
162
|
this.replaying--;
|
|
146
163
|
}
|
|
@@ -222,7 +239,7 @@ var UndoManager = class {
|
|
|
222
239
|
else this.undoStack.push(entry);
|
|
223
240
|
this.redoStack = [];
|
|
224
241
|
this.trimUndoStack();
|
|
225
|
-
this.onChange?.();
|
|
242
|
+
this.onChange?.("record", this.undoStack.at(-1));
|
|
226
243
|
}
|
|
227
244
|
trimUndoStack() {
|
|
228
245
|
if (!Number.isFinite(this.limit) || this.limit <= 0) return;
|
package/dist/undo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"undo.js","names":[],"sources":["../src/undo.ts"],"sourcesContent":["export interface UndoEntry {\n label: string\n forward: () => void\n inverse: () => void\n coalesceKey?: string\n}\n\nexport interface UndoManagerOptions {\n limit?: number\n /** Called after recording, undoing, redoing, or clearing committed history. */\n onChange?: () => void\n}\n\ninterface UndoBatch {\n label: string\n entries: UndoEntry[]\n coalesceKey?: string\n}\n\n/** How many edits undo keeps by default. */\nexport const DEFAULT_HISTORY_LIMIT = 200\n\nexport class UndoManager {\n private undoStack: UndoEntry[] = []\n private redoStack: UndoEntry[] = []\n private batches: UndoBatch[] = []\n private replaying = 0\n private openEdits = 0\n private readonly settlers = new Set<() => void>()\n private readonly limit: number\n private readonly onChange: (() => void) | undefined\n\n constructor(options: UndoManagerOptions = {}) {\n this.limit = options.limit ?? DEFAULT_HISTORY_LIMIT\n this.onChange = options.onChange\n }\n\n apply(entry: UndoEntry): void {\n this.execute(entry)\n }\n\n execute(entry: UndoEntry): void {\n entry.forward()\n this.record(entry)\n }\n\n push(entry: UndoEntry): void {\n this.record(entry)\n }\n\n record(entry: UndoEntry): void {\n const batch = this.currentBatch\n if (batch) {\n batch.entries.push(entry)\n return\n }\n this.pushUndoEntry(entry)\n }\n\n /**\n * Records an entry as a step of its own, outside the batches being built, such as an agent's\n * edit made while a colour picker is open. Batches settle first, so the step follows the\n * changes they hold so far.\n */\n pushStep(entry: UndoEntry): void {\n this.settle()\n this.pushUndoEntry(entry)\n }\n\n /**\n * Registers a hook that commits an edit still being coalesced, such as a run of picker changes.\n * Undo and redo run every hook first, so they act on the newest change.\n */\n onBeforeHistory(settle: () => void): () => void {\n this.settlers.add(settle)\n return () => this.settlers.delete(settle)\n }\n\n private settle() {\n for (const settle of this.settlers) settle()\n }\n\n undo(): string | null {\n this.settle()\n const entry = this.undoStack.pop()\n if (!entry) return null\n this.replay(() => entry.inverse())\n this.redoStack.push(entry)\n this.onChange?.()\n return entry.label\n }\n\n redo(): string | null {\n this.settle()\n const entry = this.redoStack.pop()\n if (!entry) return null\n this.replay(() => entry.forward())\n this.undoStack.push(entry)\n this.onChange?.()\n return entry.label\n }\n\n beginBatch(label: string, coalesceKey?: string): void {\n this.batches.push({ label, entries: [], coalesceKey })\n }\n\n commitBatch(): void {\n const batch = this.batches.pop()\n if (!batch || batch.entries.length === 0) return\n\n const entry = this.createBatchEntry(batch)\n const parentBatch = this.currentBatch\n if (parentBatch) parentBatch.entries.push(entry)\n else this.pushUndoEntry(entry)\n }\n\n runBatch<T>(label: string, fn: () => T, coalesceKey?: string): T {\n this.beginBatch(label, coalesceKey)\n try {\n const result = fn()\n this.commitBatch()\n return result\n } catch (error) {\n this.rollbackBatch()\n throw error\n }\n }\n\n rollbackBatch(): void {\n const batch = this.batches.pop()\n if (!batch) return\n for (const entry of batch.entries.toReversed()) entry.inverse()\n }\n\n /** Abandon provisional history without replaying it or changing committed undo/redo entries. */\n discardBatches(): void {\n this.batches = []\n }\n\n clear(): void {\n this.undoStack = []\n this.redoStack = []\n this.discardBatches()\n this.onChange?.()\n }\n\n /** Whether undo or redo is replaying a step, whose changes are history rather than new edits. */\n get isReplaying(): boolean {\n return this.replaying > 0\n }\n\n /**\n * Adds a change that the newest step caused, as one an editor makes in response to it, so undo\n * and redo apply them together. Inside a batch it joins the batch. False with no step to join.\n */\n extendLastStep(entry: UndoEntry): boolean {\n if (this.currentBatch) {\n this.currentBatch.entries.push(entry)\n return true\n }\n const last = this.undoStack.at(-1)\n if (!last) return false\n // The step keeps its identity, so callers that hold it still find it in the history.\n const { forward, inverse } = last\n last.forward = () => {\n forward()\n entry.forward()\n }\n last.inverse = () => {\n entry.inverse()\n inverse()\n }\n return true\n }\n\n private replay(step: () => void) {\n this.replaying++\n try {\n step()\n } finally {\n this.replaying--\n }\n }\n\n get isBatching(): boolean {\n return this.batches.length > 0\n }\n\n /**\n * Marks an edit whose step is recorded when it ends, such as an agent's call that awaits fonts\n * or a layout before it finishes. Returns the function that ends it; ending it twice is a no-op.\n */\n openEdit(): () => void {\n this.openEdits++\n let open = true\n return () => {\n if (!open) return\n open = false\n this.openEdits--\n }\n }\n\n /**\n * Whether a batch or an edit opened with `openEdit` will record a step for the changes made now,\n * so they are not edits outside the history.\n */\n get isRecording(): boolean {\n return this.isBatching || this.openEdits > 0\n }\n\n /** Committed history, or changes in an open batch that a settling hook commits first. */\n get canUndo(): boolean {\n if (this.undoStack.length > 0) return true\n return this.settlers.size > 0 && this.batches.some((batch) => batch.entries.length > 0)\n }\n\n get canRedo(): boolean {\n return this.redoStack.length > 0\n }\n\n /**\n * The entry `depth` steps below the top of the undo stack, so a caller that pushed entries\n * can tell whether they are still the most recent ones.\n */\n peekUndo(depth = 0): UndoEntry | undefined {\n return this.undoStack.at(-1 - depth)\n }\n\n /** The entry `depth` steps below the top of the redo stack, the next one Redo applies at 0. */\n peekRedo(depth = 0): UndoEntry | undefined {\n return this.redoStack.at(-1 - depth)\n }\n\n /**\n * Whether `entry` is applied (on the undo stack) or undone (on the redo stack); null once it\n * left the history, trimmed or dropped when an edit cleared Redo.\n */\n stateOf(entry: UndoEntry): 'applied' | 'undone' | null {\n if (this.undoStack.includes(entry)) return 'applied'\n if (this.redoStack.includes(entry)) return 'undone'\n return null\n }\n\n get undoLabel(): string | null {\n return this.undoStack.at(-1)?.label ?? null\n }\n\n get redoLabel(): string | null {\n return this.redoStack.at(-1)?.label ?? null\n }\n\n private get currentBatch(): UndoBatch | null {\n return this.batches.at(-1) ?? null\n }\n\n private createBatchEntry(batch: UndoBatch): UndoEntry {\n return {\n label: batch.label,\n forward: () => batch.entries.forEach((entry) => entry.forward()),\n inverse: () => batch.entries.toReversed().forEach((entry) => entry.inverse()),\n coalesceKey: batch.coalesceKey\n }\n }\n\n private pushUndoEntry(entry: UndoEntry): void {\n const previous = this.undoStack.at(-1)\n if (entry.coalesceKey && previous?.coalesceKey === entry.coalesceKey) {\n this.undoStack[this.undoStack.length - 1] = {\n ...entry,\n inverse: previous.inverse\n }\n } else {\n this.undoStack.push(entry)\n }\n this.redoStack = []\n this.trimUndoStack()\n this.onChange?.()\n }\n\n private trimUndoStack(): void {\n if (!Number.isFinite(this.limit) || this.limit <= 0) return\n const overflow = this.undoStack.length - this.limit\n if (overflow > 0) this.undoStack.splice(0, overflow)\n }\n}\n"],"mappings":";;AAoBA,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AEA,cAAsB,OAAwB;EAC5C,MAAM,WAAW,KAAK,UAAU,GAAG,EAAE;EACrC,IAAI,MAAM,eAAe,UAAU,gBAAgB,MAAM,aACvD,KAAK,UAAU,KAAK,UAAU,SAAS,KAAK;GAC1C,GAAG;GACH,SAAS,SAAS;EACpB;OAEA,KAAK,UAAU,KAAK,KAAK;EAE3B,KAAK,YAAY,CAAC;EAClB,KAAK,cAAc;EACnB,KAAK,WAAW;CAClB;CAEA,gBAA8B;EAC5B,IAAI,CAAC,OAAO,SAAS,KAAK,KAAK,KAAK,KAAK,SAAS,GAAG;EACrD,MAAM,WAAW,KAAK,UAAU,SAAS,KAAK;EAC9C,IAAI,WAAW,GAAG,KAAK,UAAU,OAAO,GAAG,QAAQ;CACrD;AACF"}
|
|
1
|
+
{"version":3,"file":"undo.js","names":[],"sources":["../src/undo.ts"],"sourcesContent":["export interface UndoEntry {\n label: string\n forward: () => void\n inverse: () => void\n coalesceKey?: string\n}\n\n/**\n * Why committed history changed: a new step, a change joining the newest step, undo, redo, or\n * clearing it.\n */\nexport type HistoryChange = 'record' | 'join' | 'undo' | 'redo' | 'clear'\n\nexport interface UndoManagerOptions {\n limit?: number\n /**\n * Called after recording, joining, undoing, redoing, or clearing committed history, with which,\n * and with the step recorded or joined for `'record'` and `'join'`.\n */\n onChange?: (change: HistoryChange, step?: UndoEntry) => void\n /**\n * Called when an edit ends without a step, its changes undone or amounting to nothing: a batch\n * rolled back, or an edit reported with `discardEdit`. History is unchanged, so `onChange` is not.\n */\n onDiscard?: () => void\n /**\n * Runs an undo or a redo, which the editor runs outside edits in progress\n * (`SceneGraph.runOutsideEdits`), so an agent's edit open meanwhile does not record it.\n */\n runReplay?: (replay: () => void) => void\n}\n\ninterface UndoBatch {\n label: string\n entries: UndoEntry[]\n coalesceKey?: string\n}\n\n/** How many edits undo keeps by default. */\nexport const DEFAULT_HISTORY_LIMIT = 200\n\nexport class UndoManager {\n private undoStack: UndoEntry[] = []\n private redoStack: UndoEntry[] = []\n private batches: UndoBatch[] = []\n private replaying = 0\n private openEdits = 0\n private readonly settlers = new Set<() => void>()\n private readonly limit: number\n private readonly onChange: ((change: HistoryChange, step?: UndoEntry) => void) | undefined\n private readonly onDiscard: (() => void) | undefined\n private readonly runReplay: (replay: () => void) => void\n\n constructor(options: UndoManagerOptions = {}) {\n this.limit = options.limit ?? DEFAULT_HISTORY_LIMIT\n this.onChange = options.onChange\n this.onDiscard = options.onDiscard\n this.runReplay = options.runReplay ?? ((replay) => replay())\n }\n\n apply(entry: UndoEntry): void {\n this.execute(entry)\n }\n\n execute(entry: UndoEntry): void {\n entry.forward()\n this.record(entry)\n }\n\n push(entry: UndoEntry): void {\n this.record(entry)\n }\n\n record(entry: UndoEntry): void {\n const batch = this.currentBatch\n if (batch) {\n batch.entries.push(entry)\n return\n }\n this.pushUndoEntry(entry)\n }\n\n /**\n * Records an entry as a step of its own, outside the batches being built, such as an agent's\n * edit made while a colour picker is open. Batches settle first, so the step follows the\n * changes they hold so far.\n */\n pushStep(entry: UndoEntry): void {\n this.settle()\n this.pushUndoEntry(entry)\n }\n\n /**\n * Registers a hook that commits an edit still being coalesced, such as a run of picker changes.\n * Undo and redo run every hook first, so they act on the newest change.\n */\n onBeforeHistory(settle: () => void): () => void {\n this.settlers.add(settle)\n return () => this.settlers.delete(settle)\n }\n\n private settle() {\n for (const settle of this.settlers) settle()\n }\n\n undo(): string | null {\n this.settle()\n const entry = this.undoStack.pop()\n if (!entry) return null\n this.replay(() => entry.inverse())\n this.redoStack.push(entry)\n this.onChange?.('undo')\n return entry.label\n }\n\n redo(): string | null {\n this.settle()\n const entry = this.redoStack.pop()\n if (!entry) return null\n this.replay(() => entry.forward())\n this.undoStack.push(entry)\n this.onChange?.('redo')\n return entry.label\n }\n\n beginBatch(label: string, coalesceKey?: string): void {\n this.batches.push({ label, entries: [], coalesceKey })\n }\n\n commitBatch(): void {\n const batch = this.batches.pop()\n if (!batch || batch.entries.length === 0) return\n\n const entry = this.createBatchEntry(batch)\n const parentBatch = this.currentBatch\n if (parentBatch) parentBatch.entries.push(entry)\n else this.pushUndoEntry(entry)\n }\n\n runBatch<T>(label: string, fn: () => T, coalesceKey?: string): T {\n this.beginBatch(label, coalesceKey)\n try {\n const result = fn()\n this.commitBatch()\n return result\n } catch (error) {\n this.rollbackBatch()\n throw error\n }\n }\n\n rollbackBatch(): void {\n const batch = this.batches.at(-1)\n if (!batch) return\n // Still open while it rolls back, so what the rollback changes belongs to the batch.\n try {\n for (const entry of batch.entries.toReversed()) entry.inverse()\n } finally {\n this.batches.pop()\n }\n this.onDiscard?.()\n }\n\n /**\n * Reports an edit that ended without a step, rolled back or changing nothing, as a rolled-back\n * batch does. Called once the edit has closed, so `isRecording` tells whether another is open.\n */\n discardEdit(): void {\n this.onDiscard?.()\n }\n\n /** Abandon provisional history without replaying it or changing committed undo/redo entries. */\n discardBatches(): void {\n this.batches = []\n }\n\n clear(): void {\n this.undoStack = []\n this.redoStack = []\n this.discardBatches()\n this.onChange?.('clear')\n }\n\n /** Whether undo or redo is replaying a step, whose changes are history rather than new edits. */\n get isReplaying(): boolean {\n return this.replaying > 0\n }\n\n /**\n * Adds a change that the newest step caused, as one an editor makes in response to it, so undo\n * and redo apply them together. Inside a batch it joins the batch. False with no step to join.\n */\n extendLastStep(entry: UndoEntry): boolean {\n if (this.currentBatch) {\n this.currentBatch.entries.push(entry)\n return true\n }\n const last = this.undoStack.at(-1)\n if (!last) return false\n // The step keeps its identity, so callers that hold it still find it in the history.\n const { forward, inverse } = last\n last.forward = () => {\n forward()\n entry.forward()\n }\n last.inverse = () => {\n entry.inverse()\n inverse()\n }\n this.onChange?.('join', last)\n return true\n }\n\n private replay(step: () => void) {\n this.replaying++\n try {\n this.runReplay(step)\n } finally {\n this.replaying--\n }\n }\n\n get isBatching(): boolean {\n return this.batches.length > 0\n }\n\n /**\n * Marks an edit whose step is recorded when it ends, such as an agent's call that awaits fonts\n * or a layout before it finishes. Returns the function that ends it; ending it twice is a no-op.\n */\n openEdit(): () => void {\n this.openEdits++\n let open = true\n return () => {\n if (!open) return\n open = false\n this.openEdits--\n }\n }\n\n /**\n * Whether a batch or an edit opened with `openEdit` will record a step for the changes made now,\n * so they are not edits outside the history.\n */\n get isRecording(): boolean {\n return this.isBatching || this.openEdits > 0\n }\n\n /** Committed history, or changes in an open batch that a settling hook commits first. */\n get canUndo(): boolean {\n if (this.undoStack.length > 0) return true\n return this.settlers.size > 0 && this.batches.some((batch) => batch.entries.length > 0)\n }\n\n get canRedo(): boolean {\n return this.redoStack.length > 0\n }\n\n /**\n * The entry `depth` steps below the top of the undo stack, so a caller that pushed entries\n * can tell whether they are still the most recent ones.\n */\n peekUndo(depth = 0): UndoEntry | undefined {\n return this.undoStack.at(-1 - depth)\n }\n\n /** The entry `depth` steps below the top of the redo stack, the next one Redo applies at 0. */\n peekRedo(depth = 0): UndoEntry | undefined {\n return this.redoStack.at(-1 - depth)\n }\n\n /**\n * Whether `entry` is applied (on the undo stack) or undone (on the redo stack); null once it\n * left the history, trimmed or dropped when an edit cleared Redo.\n */\n stateOf(entry: UndoEntry): 'applied' | 'undone' | null {\n if (this.undoStack.includes(entry)) return 'applied'\n if (this.redoStack.includes(entry)) return 'undone'\n return null\n }\n\n get undoLabel(): string | null {\n return this.undoStack.at(-1)?.label ?? null\n }\n\n get redoLabel(): string | null {\n return this.redoStack.at(-1)?.label ?? null\n }\n\n private get currentBatch(): UndoBatch | null {\n return this.batches.at(-1) ?? null\n }\n\n private createBatchEntry(batch: UndoBatch): UndoEntry {\n return {\n label: batch.label,\n forward: () => batch.entries.forEach((entry) => entry.forward()),\n inverse: () => batch.entries.toReversed().forEach((entry) => entry.inverse()),\n coalesceKey: batch.coalesceKey\n }\n }\n\n private pushUndoEntry(entry: UndoEntry): void {\n const previous = this.undoStack.at(-1)\n if (entry.coalesceKey && previous?.coalesceKey === entry.coalesceKey) {\n this.undoStack[this.undoStack.length - 1] = {\n ...entry,\n inverse: previous.inverse\n }\n } else {\n this.undoStack.push(entry)\n }\n this.redoStack = []\n this.trimUndoStack()\n this.onChange?.('record', this.undoStack.at(-1))\n }\n\n private trimUndoStack(): void {\n if (!Number.isFinite(this.limit) || this.limit <= 0) return\n const overflow = this.undoStack.length - this.limit\n if (overflow > 0) this.undoStack.splice(0, overflow)\n }\n}\n"],"mappings":";;AAuCA,MAAa,wBAAwB;AAErC,IAAa,cAAb,MAAyB;CACvB,YAAiC,CAAC;CAClC,YAAiC,CAAC;CAClC,UAA+B,CAAC;CAChC,YAAoB;CACpB,YAAoB;CACpB,2BAA4B,IAAI,IAAgB;CAChD;CACA;CACA;CACA;CAEA,YAAY,UAA8B,CAAC,GAAG;EAC5C,KAAK,QAAQ,QAAQ,SAAA;EACrB,KAAK,WAAW,QAAQ;EACxB,KAAK,YAAY,QAAQ;EACzB,KAAK,YAAY,QAAQ,eAAe,WAAW,OAAO;CAC5D;CAEA,MAAM,OAAwB;EAC5B,KAAK,QAAQ,KAAK;CACpB;CAEA,QAAQ,OAAwB;EAC9B,MAAM,QAAQ;EACd,KAAK,OAAO,KAAK;CACnB;CAEA,KAAK,OAAwB;EAC3B,KAAK,OAAO,KAAK;CACnB;CAEA,OAAO,OAAwB;EAC7B,MAAM,QAAQ,KAAK;EACnB,IAAI,OAAO;GACT,MAAM,QAAQ,KAAK,KAAK;GACxB;EACF;EACA,KAAK,cAAc,KAAK;CAC1B;;;;;;CAOA,SAAS,OAAwB;EAC/B,KAAK,OAAO;EACZ,KAAK,cAAc,KAAK;CAC1B;;;;;CAMA,gBAAgB,QAAgC;EAC9C,KAAK,SAAS,IAAI,MAAM;EACxB,aAAa,KAAK,SAAS,OAAO,MAAM;CAC1C;CAEA,SAAiB;EACf,KAAK,MAAM,UAAU,KAAK,UAAU,OAAO;CAC7C;CAEA,OAAsB;EACpB,KAAK,OAAO;EACZ,MAAM,QAAQ,KAAK,UAAU,IAAI;EACjC,IAAI,CAAC,OAAO,OAAO;EACnB,KAAK,aAAa,MAAM,QAAQ,CAAC;EACjC,K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AO;EAC3C,OAAO;CACT;CAEA,IAAI,YAA2B;EAC7B,OAAO,KAAK,UAAU,GAAG,EAAE,CAAC,EAAE,SAAS;CACzC;CAEA,IAAI,YAA2B;EAC7B,OAAO,KAAK,UAAU,GAAG,EAAE,CAAC,EAAE,SAAS;CACzC;CAEA,IAAY,eAAiC;EAC3C,OAAO,KAAK,QAAQ,GAAG,EAAE,KAAK;CAChC;CAEA,iBAAyB,OAA6B;EACpD,OAAO;GACL,OAAO,MAAM;GACb,eAAe,MAAM,QAAQ,SAAS,UAAU,MAAM,QAAQ,CAAC;GAC/D,eAAe,MAAM,QAAQ,WAAW,CAAC,CAAC,SAAS,UAAU,MAAM,QAAQ,CAAC;GAC5E,aAAa,MAAM;EACrB;CACF;CAEA,cAAsB,OAAwB;EAC5C,MAAM,WAAW,KAAK,UAAU,GAAG,EAAE;EACrC,IAAI,MAAM,eAAe,UAAU,gBAAgB,MAAM,aACvD,KAAK,UAAU,KAAK,UAAU,SAAS,KAAK;GAC1C,GAAG;GACH,SAAS,SAAS;EACpB;OAEA,KAAK,UAAU,KAAK,KAAK;EAE3B,KAAK,YAAY,CAAC;EAClB,KAAK,cAAc;EACnB,KAAK,WAAW,UAAU,KAAK,UAAU,GAAG,EAAE,CAAC;CACjD;CAEA,gBAA8B;EAC5B,IAAI,CAAC,OAAO,SAAS,KAAK,KAAK,KAAK,KAAK,SAAS,GAAG;EACrD,MAAM,WAAW,KAAK,UAAU,SAAS,KAAK;EAC9C,IAAI,WAAW,GAAG,KAAK,UAAU,OAAO,GAAG,QAAQ;CACrD;AACF"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { VariableModeFallback } from "./resolution.js";
|
|
2
|
-
import { PaintBindingList, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, isNumericVariableBindingField, paintBindingTarget } from "./fields.js";
|
|
3
1
|
import { SceneGraph } from "../graph.js";
|
|
4
2
|
import { SceneNode, TextLength } from "../types.js";
|
|
3
|
+
import { EFFECT_BINDING_PROPERTIES, EffectBindingProperty, PaintBindingList, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, effectBindableProperties, effectBindingField, effectBindingTarget, effectBindingVariableType, isNumericVariableBindingField, paintBindingTarget } from "./fields.js";
|
|
5
4
|
//#region src/variables/bindings.d.ts
|
|
6
5
|
/** New declarations belong to the outermost occurrence, bounded by a component definition. */
|
|
7
6
|
declare function variableBindingOwner(graph: SceneGraph, node: SceneNode): SceneNode;
|
|
@@ -12,7 +11,7 @@ declare function boundNumberValue(field: string, value: number): number | TextLe
|
|
|
12
11
|
* The values a node's numeric bindings resolve to in its mode, in scene units, for the fields
|
|
13
12
|
* whose stored value differs. A width or height an instance overrides keeps its own value.
|
|
14
13
|
*/
|
|
15
|
-
declare function resolvedNumericBindings(graph: SceneGraph, node: SceneNode
|
|
14
|
+
declare function resolvedNumericBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode>;
|
|
16
15
|
/**
|
|
17
16
|
* The text and visibility a node's string and boolean bindings resolve to in its mode, for the
|
|
18
17
|
* fields whose stored value differs. Text or visibility an instance overrides, which typing in a
|
|
@@ -20,14 +19,20 @@ declare function resolvedNumericBindings(graph: SceneGraph, node: SceneNode, fal
|
|
|
20
19
|
* A bound font family is left as stored: changing it needs the font loaded, which resolving a
|
|
21
20
|
* binding cannot do.
|
|
22
21
|
*/
|
|
23
|
-
declare function resolvedValueBindings(graph: SceneGraph, node: SceneNode
|
|
22
|
+
declare function resolvedValueBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode>;
|
|
24
23
|
/**
|
|
25
24
|
* The runs a text's character bindings resolve to in its mode, when a bound number differs from
|
|
26
25
|
* what its characters hold. Bound font families and styles are left as stored, as the text's own.
|
|
27
26
|
*/
|
|
28
|
-
declare function resolvedRunBindings(graph: SceneGraph, node: SceneNode
|
|
29
|
-
/**
|
|
30
|
-
|
|
27
|
+
declare function resolvedRunBindings(graph: SceneGraph, node: SceneNode): Pick<Partial<SceneNode>, 'styleRuns'>;
|
|
28
|
+
/**
|
|
29
|
+
* The effects a node's effect bindings resolve to in its mode, when one differs from what the node
|
|
30
|
+
* holds. Lengths stay as the variables give them, negative ones included, as Figma keeps them;
|
|
31
|
+
* drawing treats a negative blur as none. In an instance they take its scale against its
|
|
32
|
+
* component, whenever they were bound, as Figma scales them; a layer scaled outside an instance
|
|
33
|
+
* goes back to the variable's own value.
|
|
34
|
+
*/
|
|
35
|
+
declare function resolvedEffectBindings(graph: SceneGraph, node: SceneNode): Pick<Partial<SceneNode>, 'effects'>;
|
|
31
36
|
/**
|
|
32
37
|
* The layers a reconcile looks at: those bound to these variables, directly or through a variable
|
|
33
38
|
* aliasing them, or the layers in these subtrees. Without a scope, every layer.
|
|
@@ -50,11 +55,32 @@ declare function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>):
|
|
|
50
55
|
*/
|
|
51
56
|
declare function layersUsingVariables(graph: SceneGraph, ids: Iterable<string>): SceneNode[];
|
|
52
57
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
|
|
58
|
+
* The layers in these subtrees that resolve a variable of these collections, binding it or a
|
|
59
|
+
* variable that aliases it, so a mode change for the collections can change what they show.
|
|
60
|
+
*/
|
|
61
|
+
declare function layersResolvingCollections(graph: SceneGraph, roots: Iterable<string>, collectionIds: ReadonlySet<string>): SceneNode[];
|
|
62
|
+
/**
|
|
63
|
+
* Resolve live numeric, text, visibility, and effect bindings, numbers in scene units, and refresh
|
|
64
|
+
* bound paints whose variable's color for the layer changed (`refreshBoundPaints`), without
|
|
65
|
+
* authoring instance overrides. A scope limits it to the layers a change can reach, so layers whose
|
|
66
|
+
* saved values differ from their bindings elsewhere in the document are left as saved. Returns the
|
|
67
|
+
* layers whose change needs a layout pass; effects and paints are not a layout input, so a change
|
|
68
|
+
* to them alone is written but not returned.
|
|
56
69
|
*/
|
|
57
70
|
declare function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[];
|
|
71
|
+
/**
|
|
72
|
+
* As `reconcileVariableBindings`, with the layout fields each relaid layer received. Effects and
|
|
73
|
+
* paints are written too, but draw without layout, so a layer whose only change is one is left out.
|
|
74
|
+
*/
|
|
75
|
+
declare function reconcileVariableBindingUpdates(graph: SceneGraph, scope?: BindingScope): Map<string, Partial<SceneNode>>;
|
|
76
|
+
/** A layer list whose items carry their own bindings, keyed by position such as `fills/1/color`. */
|
|
77
|
+
type BoundLayerList = 'fills' | 'strokes' | 'effects';
|
|
78
|
+
/**
|
|
79
|
+
* `boundVariables` once the `list` items are rebuilt from `sources`, where `sources[i]` is the
|
|
80
|
+
* position the new item `i` had before, or null for an added item. A binding moves with its item
|
|
81
|
+
* and goes with a removed one, as Figma keeps a binding on its paint or effect.
|
|
82
|
+
*/
|
|
83
|
+
declare function listBindingsAfterEdit(boundVariables: Readonly<Record<string, string>>, list: BoundLayerList, sources: readonly (number | null)[]): Record<string, string>;
|
|
58
84
|
//#endregion
|
|
59
|
-
export { BindingScope, type PaintBindingList, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, assignVariableBindingUnits, boundNumberValue, collectionsBoundIn, isNumericVariableBindingField, layersUsingVariables, paintBindingTarget, reconcileVariableBindings,
|
|
85
|
+
export { BindingScope, BoundLayerList, EFFECT_BINDING_PROPERTIES, type EffectBindingProperty, type PaintBindingList, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, assignVariableBindingUnits, boundNumberValue, collectionsBoundIn, effectBindableProperties, effectBindingField, effectBindingTarget, effectBindingVariableType, isNumericVariableBindingField, layersResolvingCollections, layersUsingVariables, listBindingsAfterEdit, paintBindingTarget, reconcileVariableBindingUpdates, reconcileVariableBindings, resolvedEffectBindings, resolvedNumericBindings, resolvedRunBindings, resolvedValueBindings, variableBindingOwner, variablesResolvingThrough };
|
|
60
86
|
//# sourceMappingURL=bindings.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindings.d.ts","names":[],"sources":["../../src/variables/bindings.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"bindings.d.ts","names":[],"sources":["../../src/variables/bindings.ts"],"mappings":";;;;;iBAkCgB,qBAAqB,OAAO,YAAY,MAAM,YAAY;iBAW1D,2BACd,OAAO,YACP,MAAM,WACN;;iBAoBc,iBAAiB,eAAe,yBAAyB;;;;;iBAQzD,wBAAwB,OAAO,YAAY,MAAM,YAAY,QAAQ;;;;;;;;iBA2BrE,sBAAsB,OAAO,YAAY,MAAM,YAAY,QAAQ;;;;;iBAsBnE,oBACd,OAAO,YACP,MAAM,YACL,KAAK,QAAQ;;;;;;;;iBAqDA,uBACd,OAAO,YACP,MAAM,YACL,KAAK,QAAQ;;;;;KAyBJ;EAAiB,WAAW;;EAAuB,UAAU;;;iBAGzD,0BAA0B,OAAO,YAAY,KAAK,mBAAmB;;;;;iBAuBrE,mBAAmB,OAAO,YAAY,OAAO,mBAAmB;;;;;iBAQhE,qBAAqB,OAAO,YAAY,KAAK,mBAAmB;;;;;iBAWhE,2BACd,OAAO,YACP,OAAO,kBACP,eAAe,sBACd;;;;;;;;;iBA2Ba,0BAA0B,OAAO,YAAY,QAAQ;;;;;iBAQrD,gCACd,OAAO,YACP,QAAQ,eACP,YAAY,QAAQ;;KAmBX;;;;;;iBAOI,sBACd,gBAAgB,SAAS,yBACzB,MAAM,gBACN,sCACC"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { EFFECT_BINDING_PROPERTIES, NUMERIC_FIELDS, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, effectBindableProperties, effectBindingField, effectBindingTarget, effectBindingVariableType, isNumericVariableBindingField, paintBindingTarget } from "./fields.js";
|
|
2
|
+
import { hasInstanceOverride } from "../instances/overrides.js";
|
|
1
3
|
import { pixelLength } from "../text/lengths.js";
|
|
2
|
-
import { copyFills, copyStrokes } from "../copy.js";
|
|
3
|
-
import { NUMERIC_FIELDS, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, isNumericVariableBindingField, paintBindingTarget } from "./fields.js";
|
|
4
|
-
import { hasInstanceOverride } from "../instances/index.js";
|
|
5
4
|
import { variableText, variableTruthy } from "./text.js";
|
|
6
5
|
import { variableValueReferences } from "./value.js";
|
|
7
|
-
import {
|
|
6
|
+
import { collectionsResolving } from "./resolution.js";
|
|
7
|
+
import { subtreeNodes } from "./subtree.js";
|
|
8
|
+
import { refreshBoundPaints } from "./paints.js";
|
|
9
|
+
import { isEmptyObject, isEqual } from "es-toolkit/predicate";
|
|
8
10
|
//#region src/variables/bindings.ts
|
|
9
11
|
/** New declarations belong to the outermost occurrence, bounded by a component definition. */
|
|
10
12
|
function variableBindingOwner(graph, node) {
|
|
@@ -27,6 +29,11 @@ function assignVariableBindingUnits(graph, node, field) {
|
|
|
27
29
|
}
|
|
28
30
|
/** Fields a bound number sets as a length in pixels, as Figma's do, whatever unit they held. */
|
|
29
31
|
const TEXT_LENGTH_FIELDS = /* @__PURE__ */ new Set(["lineHeight", "letterSpacing"]);
|
|
32
|
+
/** Whether the instance a layer sits in, or the instance itself, overrides one of its fields. */
|
|
33
|
+
function overriddenInInstance(graph, nodeId, field) {
|
|
34
|
+
const instance = graph.closest(nodeId, (node) => node.type === "INSTANCE");
|
|
35
|
+
return !!instance && hasInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field);
|
|
36
|
+
}
|
|
30
37
|
/** A bound number as the field stores it. */
|
|
31
38
|
function boundNumberValue(field, value) {
|
|
32
39
|
return TEXT_LENGTH_FIELDS.has(field) ? pixelLength(value) : value;
|
|
@@ -35,12 +42,12 @@ function boundNumberValue(field, value) {
|
|
|
35
42
|
* The values a node's numeric bindings resolve to in its mode, in scene units, for the fields
|
|
36
43
|
* whose stored value differs. A width or height an instance overrides keeps its own value.
|
|
37
44
|
*/
|
|
38
|
-
function resolvedNumericBindings(graph, node
|
|
45
|
+
function resolvedNumericBindings(graph, node) {
|
|
39
46
|
const updates = {};
|
|
40
47
|
for (const [field, variableId] of Object.entries(node.boundVariables)) {
|
|
41
48
|
if (!NUMERIC_FIELDS.has(field)) continue;
|
|
42
|
-
if ((field === "width" || field === "height") &&
|
|
43
|
-
const value = graph.resolveNumberVariableForNode(node.id, variableId
|
|
49
|
+
if ((field === "width" || field === "height") && overriddenInInstance(graph, node.id, field)) continue;
|
|
50
|
+
const value = graph.resolveNumberVariableForNode(node.id, variableId);
|
|
44
51
|
if (value === void 0 || !Number.isFinite(value)) continue;
|
|
45
52
|
const effective = value * (node.variableBindingScales[field] ?? 1);
|
|
46
53
|
if (!Number.isFinite(effective)) continue;
|
|
@@ -56,12 +63,12 @@ function resolvedNumericBindings(graph, node, fallback = "active") {
|
|
|
56
63
|
* A bound font family is left as stored: changing it needs the font loaded, which resolving a
|
|
57
64
|
* binding cannot do.
|
|
58
65
|
*/
|
|
59
|
-
function resolvedValueBindings(graph, node
|
|
66
|
+
function resolvedValueBindings(graph, node) {
|
|
60
67
|
const updates = {};
|
|
61
68
|
const resolve = (field) => {
|
|
62
69
|
const variableId = node.boundVariables[field];
|
|
63
|
-
if (!variableId ||
|
|
64
|
-
return graph.resolveVariableForNode(node.id, variableId
|
|
70
|
+
if (!variableId || overriddenInInstance(graph, node.id, field)) return void 0;
|
|
71
|
+
return graph.resolveVariableForNode(node.id, variableId);
|
|
65
72
|
};
|
|
66
73
|
const text = node.type === "TEXT" ? variableText(resolve("text")) : void 0;
|
|
67
74
|
if (text !== void 0 && text !== node.text) updates.text = text;
|
|
@@ -80,7 +87,7 @@ const NUMERIC_RUN_FIELDS = [
|
|
|
80
87
|
* The runs a text's character bindings resolve to in its mode, when a bound number differs from
|
|
81
88
|
* what its characters hold. Bound font families and styles are left as stored, as the text's own.
|
|
82
89
|
*/
|
|
83
|
-
function resolvedRunBindings(graph, node
|
|
90
|
+
function resolvedRunBindings(graph, node) {
|
|
84
91
|
const styleRuns = node.styleRuns.map((run) => {
|
|
85
92
|
const bound = run.style.boundVariables;
|
|
86
93
|
if (!bound) return run;
|
|
@@ -88,7 +95,7 @@ function resolvedRunBindings(graph, node, fallback = "active") {
|
|
|
88
95
|
for (const field of NUMERIC_RUN_FIELDS) {
|
|
89
96
|
const variableId = bound[field];
|
|
90
97
|
if (!variableId) continue;
|
|
91
|
-
const value = graph.resolveNumberVariableForNode(node.id, variableId
|
|
98
|
+
const value = graph.resolveNumberVariableForNode(node.id, variableId);
|
|
92
99
|
if (value === void 0 || !Number.isFinite(value)) continue;
|
|
93
100
|
const next = boundNumberValue(field, value);
|
|
94
101
|
if (isEqual(style[field], next)) continue;
|
|
@@ -108,38 +115,58 @@ function resolvedRunBindings(graph, node, fallback = "active") {
|
|
|
108
115
|
function boundVariableIds(node) {
|
|
109
116
|
return [...Object.values(node.boundVariables), ...node.styleRuns.flatMap((run) => Object.values(run.style.boundVariables ?? {}))].filter((id) => typeof id === "string");
|
|
110
117
|
}
|
|
111
|
-
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
/** `effect` with one bound property set to its variable's value; a colour keeps its alpha. */
|
|
119
|
+
function withBoundEffectValue(effect, property, value) {
|
|
120
|
+
if (typeof value !== "number") return property === "color" ? {
|
|
121
|
+
...effect,
|
|
122
|
+
color: value
|
|
123
|
+
} : effect;
|
|
124
|
+
if (property === "offsetX") return {
|
|
125
|
+
...effect,
|
|
126
|
+
offset: {
|
|
127
|
+
...effect.offset,
|
|
128
|
+
x: value
|
|
129
|
+
}
|
|
119
130
|
};
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
for (const field in node.boundVariables) {
|
|
126
|
-
if (!field.endsWith("/color")) continue;
|
|
127
|
-
const paint = paintBindingTarget(field);
|
|
128
|
-
if (!paint) continue;
|
|
129
|
-
const color = graph.resolveColorVariableForNode(node.id, node.boundVariables[field], fallback);
|
|
130
|
-
const { index } = paint;
|
|
131
|
-
if (!color) continue;
|
|
132
|
-
if (paint.list === "fills") {
|
|
133
|
-
if (!node.fills[index]) continue;
|
|
134
|
-
changes.fills ??= copyFills(node.fills);
|
|
135
|
-
applyBoundColor(changes.fills[index], color);
|
|
136
|
-
} else {
|
|
137
|
-
if (!node.strokes[index]) continue;
|
|
138
|
-
changes.strokes ??= copyStrokes(node.strokes);
|
|
139
|
-
applyBoundColor(changes.strokes[index], color);
|
|
131
|
+
if (property === "offsetY") return {
|
|
132
|
+
...effect,
|
|
133
|
+
offset: {
|
|
134
|
+
...effect.offset,
|
|
135
|
+
y: value
|
|
140
136
|
}
|
|
137
|
+
};
|
|
138
|
+
if (property === "radius" || property === "spread") return {
|
|
139
|
+
...effect,
|
|
140
|
+
[property]: value
|
|
141
|
+
};
|
|
142
|
+
return effect;
|
|
143
|
+
}
|
|
144
|
+
/** A bound effect length in an occurrence's coordinates, which are 1:1 outside instances. */
|
|
145
|
+
function scaledLength(length, node) {
|
|
146
|
+
return length === void 0 ? void 0 : length * node.componentScale;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The effects a node's effect bindings resolve to in its mode, when one differs from what the node
|
|
150
|
+
* holds. Lengths stay as the variables give them, negative ones included, as Figma keeps them;
|
|
151
|
+
* drawing treats a negative blur as none. In an instance they take its scale against its
|
|
152
|
+
* component, whenever they were bound, as Figma scales them; a layer scaled outside an instance
|
|
153
|
+
* goes back to the variable's own value.
|
|
154
|
+
*/
|
|
155
|
+
function resolvedEffectBindings(graph, node) {
|
|
156
|
+
let effects;
|
|
157
|
+
for (const [field, variableId] of Object.entries(node.boundVariables)) {
|
|
158
|
+
const target = effectBindingTarget(field);
|
|
159
|
+
if (!target) continue;
|
|
160
|
+
const effect = (effects ?? node.effects).at(target.index);
|
|
161
|
+
if (!effect || !effectBindableProperties(effect.type).includes(target.property)) continue;
|
|
162
|
+
const value = target.property === "color" ? graph.resolveColorVariableForNode(node.id, variableId) : scaledLength(graph.resolveNumberVariableForNode(node.id, variableId), node);
|
|
163
|
+
if (value == null || typeof value === "number" && !Number.isFinite(value)) continue;
|
|
164
|
+
const next = withBoundEffectValue(effect, target.property, value);
|
|
165
|
+
if (isEqual(next, effect)) continue;
|
|
166
|
+
effects ??= [...node.effects];
|
|
167
|
+
effects[target.index] = next;
|
|
141
168
|
}
|
|
142
|
-
return
|
|
169
|
+
return effects ? { effects } : {};
|
|
143
170
|
}
|
|
144
171
|
/** These variables and every variable that aliases one of them, at any depth and in any mode. */
|
|
145
172
|
function variablesResolvingThrough(graph, ids) {
|
|
@@ -157,34 +184,12 @@ function variablesResolvingThrough(graph, ids) {
|
|
|
157
184
|
}
|
|
158
185
|
return found;
|
|
159
186
|
}
|
|
160
|
-
function subtreeNodes(graph, roots) {
|
|
161
|
-
const nodes = [];
|
|
162
|
-
const pending = [...roots];
|
|
163
|
-
for (let id = pending.pop(); id !== void 0; id = pending.pop()) {
|
|
164
|
-
const node = graph.getNode(id);
|
|
165
|
-
if (!node) continue;
|
|
166
|
-
nodes.push(node);
|
|
167
|
-
pending.push(...node.childIds);
|
|
168
|
-
}
|
|
169
|
-
return nodes;
|
|
170
|
-
}
|
|
171
187
|
/**
|
|
172
188
|
* The collections whose variables the layers in these subtrees resolve: those they bind, and
|
|
173
189
|
* those the bound variables alias, so a mode set on the subtrees can change what they show.
|
|
174
190
|
*/
|
|
175
191
|
function collectionsBoundIn(graph, roots) {
|
|
176
|
-
|
|
177
|
-
const seen = /* @__PURE__ */ new Set();
|
|
178
|
-
const collections = /* @__PURE__ */ new Set();
|
|
179
|
-
for (let id = pending.pop(); id !== void 0; id = pending.pop()) {
|
|
180
|
-
if (seen.has(id)) continue;
|
|
181
|
-
seen.add(id);
|
|
182
|
-
const variable = graph.variables.get(id);
|
|
183
|
-
if (!variable) continue;
|
|
184
|
-
collections.add(variable.collectionId);
|
|
185
|
-
for (const value of Object.values(variable.valuesByMode)) pending.push(...variableValueReferences(value));
|
|
186
|
-
}
|
|
187
|
-
return collections;
|
|
192
|
+
return collectionsResolving(graph, subtreeNodes(graph, roots).flatMap(boundVariableIds));
|
|
188
193
|
}
|
|
189
194
|
/**
|
|
190
195
|
* The layers a change to these variables reaches: those binding one of them, or a variable that
|
|
@@ -194,31 +199,74 @@ function layersUsingVariables(graph, ids) {
|
|
|
194
199
|
const variables = variablesResolvingThrough(graph, ids);
|
|
195
200
|
return [...graph.getAllNodes()].filter((node) => boundVariableIds(node).some((id) => variables.has(id)));
|
|
196
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* The layers in these subtrees that resolve a variable of these collections, binding it or a
|
|
204
|
+
* variable that aliases it, so a mode change for the collections can change what they show.
|
|
205
|
+
*/
|
|
206
|
+
function layersResolvingCollections(graph, roots, collectionIds) {
|
|
207
|
+
const variables = variablesResolvingThrough(graph, [...graph.variables.values()].filter((variable) => collectionIds.has(variable.collectionId)).map((variable) => variable.id));
|
|
208
|
+
return subtreeNodes(graph, roots).filter((node) => boundVariableIds(node).some((id) => variables.has(id)));
|
|
209
|
+
}
|
|
197
210
|
function scopedNodes(graph, scope) {
|
|
198
211
|
if (!scope) return graph.getAllNodes();
|
|
199
212
|
if ("subtrees" in scope) return subtreeNodes(graph, scope.subtrees);
|
|
200
213
|
return layersUsingVariables(graph, scope.variables);
|
|
201
214
|
}
|
|
202
215
|
/**
|
|
203
|
-
* Resolve live numeric, text, and
|
|
204
|
-
*
|
|
205
|
-
*
|
|
216
|
+
* Resolve live numeric, text, visibility, and effect bindings, numbers in scene units, and refresh
|
|
217
|
+
* bound paints whose variable's color for the layer changed (`refreshBoundPaints`), without
|
|
218
|
+
* authoring instance overrides. A scope limits it to the layers a change can reach, so layers whose
|
|
219
|
+
* saved values differ from their bindings elsewhere in the document are left as saved. Returns the
|
|
220
|
+
* layers whose change needs a layout pass; effects and paints are not a layout input, so a change
|
|
221
|
+
* to them alone is written but not returned.
|
|
206
222
|
*/
|
|
207
223
|
function reconcileVariableBindings(graph, scope) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
224
|
+
return [...reconcileVariableBindingUpdates(graph, scope).keys()];
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* As `reconcileVariableBindings`, with the layout fields each relaid layer received. Effects and
|
|
228
|
+
* paints are written too, but draw without layout, so a layer whose only change is one is left out.
|
|
229
|
+
*/
|
|
230
|
+
function reconcileVariableBindingUpdates(graph, scope) {
|
|
231
|
+
const relaid = /* @__PURE__ */ new Map();
|
|
232
|
+
const nodes = [...scopedNodes(graph, scope)];
|
|
233
|
+
for (const node of nodes) refreshBoundPaints(graph, node);
|
|
234
|
+
for (const node of nodes) {
|
|
235
|
+
const layoutUpdates = {
|
|
211
236
|
...resolvedNumericBindings(graph, node),
|
|
212
237
|
...resolvedValueBindings(graph, node),
|
|
213
238
|
...resolvedRunBindings(graph, node)
|
|
214
239
|
};
|
|
215
|
-
|
|
240
|
+
const updates = {
|
|
241
|
+
...layoutUpdates,
|
|
242
|
+
...resolvedEffectBindings(graph, node)
|
|
243
|
+
};
|
|
244
|
+
if (isEmptyObject(updates)) continue;
|
|
216
245
|
graph.updateNode(node.id, updates);
|
|
217
|
-
|
|
246
|
+
if (!isEmptyObject(layoutUpdates)) relaid.set(node.id, layoutUpdates);
|
|
247
|
+
}
|
|
248
|
+
return relaid;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* `boundVariables` once the `list` items are rebuilt from `sources`, where `sources[i]` is the
|
|
252
|
+
* position the new item `i` had before, or null for an added item. A binding moves with its item
|
|
253
|
+
* and goes with a removed one, as Figma keeps a binding on its paint or effect.
|
|
254
|
+
*/
|
|
255
|
+
function listBindingsAfterEdit(boundVariables, list, sources) {
|
|
256
|
+
const prefix = `${list}/`;
|
|
257
|
+
const moved = {};
|
|
258
|
+
for (const [field, variableId] of Object.entries(boundVariables)) {
|
|
259
|
+
if (!field.startsWith(prefix)) {
|
|
260
|
+
moved[field] = variableId;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const [index, ...property] = field.slice(prefix.length).split("/");
|
|
264
|
+
const next = sources.indexOf(Number(index));
|
|
265
|
+
if (next !== -1 && property.length > 0) moved[`${prefix}${next}/${property.join("/")}`] = variableId;
|
|
218
266
|
}
|
|
219
|
-
return
|
|
267
|
+
return moved;
|
|
220
268
|
}
|
|
221
269
|
//#endregion
|
|
222
|
-
export { TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, assignVariableBindingUnits, boundNumberValue, collectionsBoundIn, isNumericVariableBindingField, layersUsingVariables, paintBindingTarget, reconcileVariableBindings,
|
|
270
|
+
export { EFFECT_BINDING_PROPERTIES, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, assignVariableBindingUnits, boundNumberValue, collectionsBoundIn, effectBindableProperties, effectBindingField, effectBindingTarget, effectBindingVariableType, isNumericVariableBindingField, layersResolvingCollections, layersUsingVariables, listBindingsAfterEdit, paintBindingTarget, reconcileVariableBindingUpdates, reconcileVariableBindings, resolvedEffectBindings, resolvedNumericBindings, resolvedRunBindings, resolvedValueBindings, variableBindingOwner, variablesResolvingThrough };
|
|
223
271
|
|
|
224
272
|
//# sourceMappingURL=bindings.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bindings.js","names":[],"sources":["../../src/variables/bindings.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit/predicate'\n\nimport type { VariableModeFallback } from '.'\nimport { copyFills, copyStrokes } from '../copy'\nimport type { SceneGraph } from '../graph'\nimport { hasInstanceOverride } from '../instances'\nimport type { Color } from '../primitives'\nimport { pixelLength } from '../text/lengths'\nimport type { Fill, SceneNode, Stroke, StyleRun, TextLength } from '../types'\nimport { NUMERIC_FIELDS, isNumericVariableBindingField, paintBindingTarget } from './fields'\nimport { variableText, variableTruthy } from './text'\nimport { variableValueReferences } from './value'\nexport {\n isNumericVariableBindingField,\n paintBindingTarget,\n TEXT_CONTENT_VARIABLE_TYPES,\n VISIBILITY_VARIABLE_TYPES,\n type PaintBindingList\n} from './fields'\n\n/** New declarations belong to the outermost occurrence, bounded by a component definition. */\nexport function variableBindingOwner(graph: SceneGraph, node: SceneNode): SceneNode {\n let owner = node\n let current: SceneNode | undefined = node\n while (current) {\n if (current.type === 'COMPONENT' || current.type === 'COMPONENT_SET') break\n if (current.type === 'INSTANCE') owner = current\n current = current.parentId ? graph.getNode(current.parentId) : undefined\n }\n return owner\n}\n\nexport function assignVariableBindingUnits(\n graph: SceneGraph,\n node: SceneNode,\n field: string\n): void {\n if (!isNumericVariableBindingField(field)) return\n const owner = variableBindingOwner(graph, node)\n node.variableBindingScales = {\n ...node.variableBindingScales,\n [field]: owner.variableAssignmentScales[field] ?? 1\n }\n}\n\n/** Fields a bound number sets as a length in pixels, as Figma's do, whatever unit they held. */\nconst TEXT_LENGTH_FIELDS: ReadonlySet<string> = new Set(['lineHeight', 'letterSpacing'])\n\n/** A bound number as the field stores it. */\nexport function boundNumberValue(field: string, value: number): number | TextLength {\n return TEXT_LENGTH_FIELDS.has(field) ? pixelLength(value) : value\n}\n\n/**\n * The values a node's numeric bindings resolve to in its mode, in scene units, for the fields\n * whose stored value differs. A width or height an instance overrides keeps its own value.\n */\nexport function resolvedNumericBindings(\n graph: SceneGraph,\n node: SceneNode,\n fallback: VariableModeFallback = 'active'\n): Partial<SceneNode> {\n const updates: Partial<SceneNode> = {}\n for (const [field, variableId] of Object.entries(node.boundVariables)) {\n if (!NUMERIC_FIELDS.has(field)) continue\n if ((field === 'width' || field === 'height') && hasInstanceOverride(graph, node.id, field))\n continue\n const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)\n if (value === undefined || !Number.isFinite(value)) continue\n const scale = node.variableBindingScales[field] ?? 1\n const effective = value * scale\n if (!Number.isFinite(effective)) continue\n const next = boundNumberValue(\n field,\n field === 'opacity' ? Math.max(0, Math.min(1, effective)) : effective\n )\n if (!isEqual(node[field as keyof SceneNode], next)) Object.assign(updates, { [field]: next })\n }\n return updates\n}\n\n/**\n * The text and visibility a node's string and boolean bindings resolve to in its mode, for the\n * fields whose stored value differs. Text or visibility an instance overrides, which typing in a\n * layer or toggling its eye records, keeps its own value, as an overridden width or height does.\n * A bound font family is left as stored: changing it needs the font loaded, which resolving a\n * binding cannot do.\n */\nexport function resolvedValueBindings(\n graph: SceneGraph,\n node: SceneNode,\n fallback: VariableModeFallback = 'active'\n): Partial<SceneNode> {\n const updates: Partial<SceneNode> = {}\n const resolve = (field: 'text' | 'visible') => {\n const variableId = node.boundVariables[field]\n if (!variableId || hasInstanceOverride(graph, node.id, field)) return undefined\n return graph.resolveVariableForNode(node.id, variableId, fallback)\n }\n const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined\n if (text !== undefined && text !== node.text) updates.text = text\n // Figma binds visibility to text too, and shows the layer when the text reads as true.\n const visible = variableTruthy(resolve('visible'))\n if (visible !== undefined && visible !== node.visible) updates.visible = visible\n return updates\n}\n\n/** Fields of a range of characters a bound number gives its value; the rest need fonts loaded. */\nconst NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeight'] as const\n\n/**\n * The runs a text's character bindings resolve to in its mode, when a bound number differs from\n * what its characters hold. Bound font families and styles are left as stored, as the text's own.\n */\nexport function resolvedRunBindings(\n graph: SceneGraph,\n node: SceneNode,\n fallback: VariableModeFallback = 'active'\n): Pick<Partial<SceneNode>, 'styleRuns'> {\n const styleRuns = node.styleRuns.map((run): StyleRun => {\n const bound = run.style.boundVariables\n if (!bound) return run\n let style = run.style\n for (const field of NUMERIC_RUN_FIELDS) {\n const variableId = bound[field]\n if (!variableId) continue\n const value = graph.resolveNumberVariableForNode(node.id, variableId, fallback)\n if (value === undefined || !Number.isFinite(value)) continue\n const next = boundNumberValue(field, value)\n if (isEqual(style[field], next)) continue\n style = { ...style, [field]: next }\n }\n return style === run.style ? run : { ...run, style }\n })\n const changed = styleRuns.some((run, index) => run !== node.styleRuns[index])\n return changed ? { styleRuns } : {}\n}\n\n/** The variables a node binds, its own and its characters'. */\nfunction boundVariableIds(node: SceneNode): string[] {\n return [\n ...Object.values(node.boundVariables),\n ...node.styleRuns.flatMap((run) => Object.values(run.style.boundVariables ?? {}))\n ].filter((id): id is string => typeof id === 'string')\n}\n\n/**\n * A bound paint takes its whole color from the variable, alpha included, the way Figma stores\n * it: the variable's RGB at full alpha, and its alpha as the paint's opacity.\n */\nfunction applyBoundColor(paint: Fill | Stroke, color: Color): void {\n paint.color = { ...color, a: 1 }\n paint.opacity = color.a\n}\n\n/** The fills and strokes a node's color bindings resolve to in its mode, when it has any. */\nexport function resolvedPaintBindings(\n graph: SceneGraph,\n node: SceneNode,\n fallback: VariableModeFallback = 'active'\n): Pick<Partial<SceneNode>, 'fills' | 'strokes'> {\n // Most nodes bind nothing, so no copy is made until a paint binding is actually found.\n const changes: Pick<Partial<SceneNode>, 'fills' | 'strokes'> = {}\n for (const field in node.boundVariables) {\n if (!field.endsWith('/color')) continue\n const paint = paintBindingTarget(field)\n if (!paint) continue\n const color = graph.resolveColorVariableForNode(node.id, node.boundVariables[field], fallback)\n const { index } = paint\n if (!color) continue\n if (paint.list === 'fills') {\n if (!node.fills[index]) continue\n changes.fills ??= copyFills(node.fills)\n applyBoundColor(changes.fills[index], color)\n } else {\n if (!node.strokes[index]) continue\n changes.strokes ??= copyStrokes(node.strokes)\n applyBoundColor(changes.strokes[index], color)\n }\n }\n return changes\n}\n\n/**\n * The layers a reconcile looks at: those bound to these variables, directly or through a variable\n * aliasing them, or the layers in these subtrees. Without a scope, every layer.\n */\nexport type BindingScope = { variables: Iterable<string> } | { subtrees: Iterable<string> }\n\n/** These variables and every variable that aliases one of them, at any depth and in any mode. */\nexport function variablesResolvingThrough(graph: SceneGraph, ids: Iterable<string>): Set<string> {\n const found = new Set(ids)\n let grew = true\n while (grew) {\n grew = false\n for (const variable of graph.variables.values()) {\n if (found.has(variable.id)) continue\n const aliases = Object.values(variable.valuesByMode).some((value) =>\n variableValueReferences(value).some((id) => found.has(id))\n )\n if (aliases) {\n found.add(variable.id)\n grew = true\n }\n }\n }\n return found\n}\n\nfunction subtreeNodes(graph: SceneGraph, roots: Iterable<string>): SceneNode[] {\n const nodes: SceneNode[] = []\n const pending = [...roots]\n for (let id = pending.pop(); id !== undefined; id = pending.pop()) {\n const node = graph.getNode(id)\n if (!node) continue\n nodes.push(node)\n pending.push(...node.childIds)\n }\n return nodes\n}\n\n/**\n * The collections whose variables the layers in these subtrees resolve: those they bind, and\n * those the bound variables alias, so a mode set on the subtrees can change what they show.\n */\nexport function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {\n const pending = subtreeNodes(graph, roots).flatMap(boundVariableIds)\n const seen = new Set<string>()\n const collections = new Set<string>()\n for (let id = pending.pop(); id !== undefined; id = pending.pop()) {\n if (seen.has(id)) continue\n seen.add(id)\n const variable = graph.variables.get(id)\n if (!variable) continue\n collections.add(variable.collectionId)\n for (const value of Object.values(variable.valuesByMode))\n pending.push(...variableValueReferences(value))\n }\n return collections\n}\n\n/**\n * The layers a change to these variables reaches: those binding one of them, or a variable that\n * aliases one, on the layer itself or on its characters.\n */\nexport function layersUsingVariables(graph: SceneGraph, ids: Iterable<string>): SceneNode[] {\n const variables = variablesResolvingThrough(graph, ids)\n return [...graph.getAllNodes()].filter((node) =>\n boundVariableIds(node).some((id) => variables.has(id))\n )\n}\n\nfunction scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterable<SceneNode> {\n if (!scope) return graph.getAllNodes()\n if ('subtrees' in scope) return subtreeNodes(graph, scope.subtrees)\n return layersUsingVariables(graph, scope.variables)\n}\n\n/**\n * Resolve live numeric, text, and visibility bindings, numbers in scene units, without authoring\n * instance overrides. A scope limits it to the layers a change can reach, so layers whose saved\n * values differ from their bindings elsewhere in the document are left as saved.\n */\nexport function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[] {\n const changed: string[] = []\n for (const node of scopedNodes(graph, scope)) {\n const updates = {\n ...resolvedNumericBindings(graph, node),\n ...resolvedValueBindings(graph, node),\n ...resolvedRunBindings(graph, node)\n }\n if (Object.keys(updates).length === 0) continue\n graph.updateNode(node.id, updates)\n changed.push(node.id)\n }\n return changed\n}\n"],"mappings":";;;;;;;;;AAqBA,SAAgB,qBAAqB,OAAmB,MAA4B;CAClF,IAAI,QAAQ;CACZ,IAAI,UAAiC;CACrC,OAAO,SAAS;EACd,IAAI,QAAQ,SAAS,eAAe,QAAQ,SAAS,iBAAiB;EACtE,IAAI,QAAQ,SAAS,YAAY,QAAQ;EACzC,UAAU,QAAQ,WAAW,MAAM,QAAQ,QAAQ,QAAQ,IAAI,KAAA;CACjE;CACA,OAAO;AACT;AAEA,SAAgB,2BACd,OACA,MACA,OACM;CACN,IAAI,CAAC,8BAA8B,KAAK,GAAG;CAC3C,MAAM,QAAQ,qBAAqB,OAAO,IAAI;CAC9C,KAAK,wBAAwB;EAC3B,GAAG,KAAK;GACP,QAAQ,MAAM,yBAAyB,UAAU;CACpD;AACF;;AAGA,MAAM,qCAA0C,IAAI,IAAI,CAAC,cAAc,eAAe,CAAC;;AAGvF,SAAgB,iBAAiB,OAAe,OAAoC;CAClF,OAAO,mBAAmB,IAAI,KAAK,IAAI,YAAY,KAAK,IAAI;AAC9D;;;;;AAMA,SAAgB,wBACd,OACA,MACA,WAAiC,UACb;CACpB,MAAM,UAA8B,CAAC;CACrC,KAAK,MAAM,CAAC,OAAO,eAAe,OAAO,QAAQ,KAAK,cAAc,GAAG;EACrE,IAAI,CAAC,eAAe,IAAI,KAAK,GAAG;EAChC,KAAK,UAAU,WAAW,UAAU,aAAa,oBAAoB,OAAO,KAAK,IAAI,KAAK,GACxF;EACF,MAAM,QAAQ,MAAM,6BAA6B,KAAK,IAAI,YAAY,QAAQ;EAC9E,IAAI,UAAU,KAAA,KAAa,CAAC,OAAO,SAAS,KAAK,GAAG;EAEpD,MAAM,YAAY,SADJ,KAAK,sBAAsB,UAAU;EAEnD,IAAI,CAAC,OAAO,SAAS,SAAS,GAAG;EACjC,MAAM,OA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ACA,OAAO;AACT;;;;;AAMA,SAAgB,qBAAqB,OAAmB,KAAoC;CAC1F,MAAM,YAAY,0BAA0B,OAAO,GAAG;CACtD,OAAO,CAAC,GAAG,MAAM,YAAY,CAAC,CAAC,CAAC,QAAQ,SACtC,iBAAiB,IAAI,CAAC,CAAC,MAAM,OAAO,UAAU,IAAI,EAAE,CAAC,CACvD;AACF;AAEA,SAAS,YAAY,OAAmB,OAAsD;CAC5F,IAAI,CAAC,OAAO,OAAO,MAAM,YAAY;CACrC,IAAI,cAAc,OAAO,OAAO,aAAa,OAAO,MAAM,QAAQ;CAClE,OAAO,qBAAqB,OAAO,MAAM,SAAS;AACpD;;;;;;AAOA,SAAgB,0BAA0B,OAAmB,OAAgC;CAC3F,MAAM,UAAoB,CAAC;CAC3B,KAAK,MAAM,QAAQ,YAAY,OAAO,KAAK,GAAG;EAC5C,MAAM,UAAU;GACd,GAAG,wBAAwB,OAAO,IAAI;GACtC,GAAG,sBAAsB,OAAO,IAAI;GACpC,GAAG,oBAAoB,OAAO,IAAI;EACpC;EACA,IAAI,OAAO,KAAK,OAAO,CAAC,CAAC,WAAW,GAAG;EACvC,MAAM,WAAW,KAAK,IAAI,OAAO;EACjC,QAAQ,KAAK,KAAK,EAAE;CACtB;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"bindings.js","names":[],"sources":["../../src/variables/bindings.ts"],"sourcesContent":["import { isEmptyObject, isEqual } from 'es-toolkit/predicate'\n\nimport type { SceneGraph } from '../graph'\nimport { hasInstanceOverride } from '../instances/overrides'\nimport type { Color } from '../primitives'\nimport { pixelLength } from '../text/lengths'\nimport type { Effect, SceneNode, StyleRun, TextLength } from '../types'\nimport {\n effectBindableProperties,\n effectBindingTarget,\n NUMERIC_FIELDS,\n isNumericVariableBindingField,\n type EffectBindingProperty\n} from './fields'\nimport { refreshBoundPaints } from './paints'\nimport { collectionsResolving } from './resolution'\nimport { subtreeNodes } from './subtree'\nimport { variableText, variableTruthy } from './text'\nimport { variableValueReferences } from './value'\nexport {\n EFFECT_BINDING_PROPERTIES,\n effectBindableProperties,\n effectBindingField,\n effectBindingTarget,\n effectBindingVariableType,\n isNumericVariableBindingField,\n paintBindingTarget,\n type EffectBindingProperty,\n TEXT_CONTENT_VARIABLE_TYPES,\n VISIBILITY_VARIABLE_TYPES,\n type PaintBindingList\n} from './fields'\n\n/** New declarations belong to the outermost occurrence, bounded by a component definition. */\nexport function variableBindingOwner(graph: SceneGraph, node: SceneNode): SceneNode {\n let owner = node\n let current: SceneNode | undefined = node\n while (current) {\n if (current.type === 'COMPONENT' || current.type === 'COMPONENT_SET') break\n if (current.type === 'INSTANCE') owner = current\n current = current.parentId ? graph.getNode(current.parentId) : undefined\n }\n return owner\n}\n\nexport function assignVariableBindingUnits(\n graph: SceneGraph,\n node: SceneNode,\n field: string\n): void {\n if (!isNumericVariableBindingField(field)) return\n const owner = variableBindingOwner(graph, node)\n node.variableBindingScales = {\n ...node.variableBindingScales,\n [field]: owner.variableAssignmentScales[field] ?? 1\n }\n}\n\n/** Fields a bound number sets as a length in pixels, as Figma's do, whatever unit they held. */\nconst TEXT_LENGTH_FIELDS: ReadonlySet<string> = new Set(['lineHeight', 'letterSpacing'])\n\n/** Whether the instance a layer sits in, or the instance itself, overrides one of its fields. */\nfunction overriddenInInstance(graph: SceneGraph, nodeId: string, field: string): boolean {\n const instance = graph.closest(nodeId, (node) => node.type === 'INSTANCE')\n return !!instance && hasInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)\n}\n\n/** A bound number as the field stores it. */\nexport function boundNumberValue(field: string, value: number): number | TextLength {\n return TEXT_LENGTH_FIELDS.has(field) ? pixelLength(value) : value\n}\n\n/**\n * The values a node's numeric bindings resolve to in its mode, in scene units, for the fields\n * whose stored value differs. A width or height an instance overrides keeps its own value.\n */\nexport function resolvedNumericBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode> {\n const updates: Partial<SceneNode> = {}\n for (const [field, variableId] of Object.entries(node.boundVariables)) {\n if (!NUMERIC_FIELDS.has(field)) continue\n if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))\n continue\n const value = graph.resolveNumberVariableForNode(node.id, variableId)\n if (value === undefined || !Number.isFinite(value)) continue\n const scale = node.variableBindingScales[field] ?? 1\n const effective = value * scale\n if (!Number.isFinite(effective)) continue\n const next = boundNumberValue(\n field,\n field === 'opacity' ? Math.max(0, Math.min(1, effective)) : effective\n )\n if (!isEqual(node[field as keyof SceneNode], next)) Object.assign(updates, { [field]: next })\n }\n return updates\n}\n\n/**\n * The text and visibility a node's string and boolean bindings resolve to in its mode, for the\n * fields whose stored value differs. Text or visibility an instance overrides, which typing in a\n * layer or toggling its eye records, keeps its own value, as an overridden width or height does.\n * A bound font family is left as stored: changing it needs the font loaded, which resolving a\n * binding cannot do.\n */\nexport function resolvedValueBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode> {\n const updates: Partial<SceneNode> = {}\n const resolve = (field: 'text' | 'visible') => {\n const variableId = node.boundVariables[field]\n if (!variableId || overriddenInInstance(graph, node.id, field)) return undefined\n return graph.resolveVariableForNode(node.id, variableId)\n }\n const text = node.type === 'TEXT' ? variableText(resolve('text')) : undefined\n if (text !== undefined && text !== node.text) updates.text = text\n // Figma binds visibility to text too, and shows the layer when the text reads as true.\n const visible = variableTruthy(resolve('visible'))\n if (visible !== undefined && visible !== node.visible) updates.visible = visible\n return updates\n}\n\n/** Fields of a range of characters a bound number gives its value; the rest need fonts loaded. */\nconst NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeight'] as const\n\n/**\n * The runs a text's character bindings resolve to in its mode, when a bound number differs from\n * what its characters hold. Bound font families and styles are left as stored, as the text's own.\n */\nexport function resolvedRunBindings(\n graph: SceneGraph,\n node: SceneNode\n): Pick<Partial<SceneNode>, 'styleRuns'> {\n const styleRuns = node.styleRuns.map((run): StyleRun => {\n const bound = run.style.boundVariables\n if (!bound) return run\n let style = run.style\n for (const field of NUMERIC_RUN_FIELDS) {\n const variableId = bound[field]\n if (!variableId) continue\n const value = graph.resolveNumberVariableForNode(node.id, variableId)\n if (value === undefined || !Number.isFinite(value)) continue\n const next = boundNumberValue(field, value)\n if (isEqual(style[field], next)) continue\n style = { ...style, [field]: next }\n }\n return style === run.style ? run : { ...run, style }\n })\n const changed = styleRuns.some((run, index) => run !== node.styleRuns[index])\n return changed ? { styleRuns } : {}\n}\n\n/** The variables a node binds, its own and its characters'. */\nfunction boundVariableIds(node: SceneNode): string[] {\n return [\n ...Object.values(node.boundVariables),\n ...node.styleRuns.flatMap((run) => Object.values(run.style.boundVariables ?? {}))\n ].filter((id): id is string => typeof id === 'string')\n}\n\n/** `effect` with one bound property set to its variable's value; a colour keeps its alpha. */\nfunction withBoundEffectValue(\n effect: Effect,\n property: EffectBindingProperty,\n value: number | Color\n): Effect {\n if (typeof value !== 'number') return property === 'color' ? { ...effect, color: value } : effect\n if (property === 'offsetX') return { ...effect, offset: { ...effect.offset, x: value } }\n if (property === 'offsetY') return { ...effect, offset: { ...effect.offset, y: value } }\n if (property === 'radius' || property === 'spread') return { ...effect, [property]: value }\n return effect\n}\n\n/** A bound effect length in an occurrence's coordinates, which are 1:1 outside instances. */\nfunction scaledLength(length: number | undefined, node: SceneNode): number | undefined {\n return length === undefined ? undefined : length * node.componentScale\n}\n\n/**\n * The effects a node's effect bindings resolve to in its mode, when one differs from what the node\n * holds. Lengths stay as the variables give them, negative ones included, as Figma keeps them;\n * drawing treats a negative blur as none. In an instance they take its scale against its\n * component, whenever they were bound, as Figma scales them; a layer scaled outside an instance\n * goes back to the variable's own value.\n */\nexport function resolvedEffectBindings(\n graph: SceneGraph,\n node: SceneNode\n): Pick<Partial<SceneNode>, 'effects'> {\n // Most nodes bind nothing, so no copy is made until a value actually changes.\n let effects: Effect[] | undefined\n for (const [field, variableId] of Object.entries(node.boundVariables)) {\n const target = effectBindingTarget(field)\n if (!target) continue\n const effect = (effects ?? node.effects).at(target.index)\n if (!effect || !effectBindableProperties(effect.type).includes(target.property)) continue\n const value =\n target.property === 'color'\n ? graph.resolveColorVariableForNode(node.id, variableId)\n : scaledLength(graph.resolveNumberVariableForNode(node.id, variableId), node)\n if (value == null || (typeof value === 'number' && !Number.isFinite(value))) continue\n const next = withBoundEffectValue(effect, target.property, value)\n if (isEqual(next, effect)) continue\n effects ??= [...node.effects]\n effects[target.index] = next\n }\n return effects ? { effects } : {}\n}\n\n/**\n * The layers a reconcile looks at: those bound to these variables, directly or through a variable\n * aliasing them, or the layers in these subtrees. Without a scope, every layer.\n */\nexport type BindingScope = { variables: Iterable<string> } | { subtrees: Iterable<string> }\n\n/** These variables and every variable that aliases one of them, at any depth and in any mode. */\nexport function variablesResolvingThrough(graph: SceneGraph, ids: Iterable<string>): Set<string> {\n const found = new Set(ids)\n let grew = true\n while (grew) {\n grew = false\n for (const variable of graph.variables.values()) {\n if (found.has(variable.id)) continue\n const aliases = Object.values(variable.valuesByMode).some((value) =>\n variableValueReferences(value).some((id) => found.has(id))\n )\n if (aliases) {\n found.add(variable.id)\n grew = true\n }\n }\n }\n return found\n}\n\n/**\n * The collections whose variables the layers in these subtrees resolve: those they bind, and\n * those the bound variables alias, so a mode set on the subtrees can change what they show.\n */\nexport function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {\n return collectionsResolving(graph, subtreeNodes(graph, roots).flatMap(boundVariableIds))\n}\n\n/**\n * The layers a change to these variables reaches: those binding one of them, or a variable that\n * aliases one, on the layer itself or on its characters.\n */\nexport function layersUsingVariables(graph: SceneGraph, ids: Iterable<string>): SceneNode[] {\n const variables = variablesResolvingThrough(graph, ids)\n return [...graph.getAllNodes()].filter((node) =>\n boundVariableIds(node).some((id) => variables.has(id))\n )\n}\n\n/**\n * The layers in these subtrees that resolve a variable of these collections, binding it or a\n * variable that aliases it, so a mode change for the collections can change what they show.\n */\nexport function layersResolvingCollections(\n graph: SceneGraph,\n roots: Iterable<string>,\n collectionIds: ReadonlySet<string>\n): SceneNode[] {\n const inCollections = [...graph.variables.values()].filter((variable) =>\n collectionIds.has(variable.collectionId)\n )\n const variables = variablesResolvingThrough(\n graph,\n inCollections.map((variable) => variable.id)\n )\n return subtreeNodes(graph, roots).filter((node) =>\n boundVariableIds(node).some((id) => variables.has(id))\n )\n}\n\nfunction scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterable<SceneNode> {\n if (!scope) return graph.getAllNodes()\n if ('subtrees' in scope) return subtreeNodes(graph, scope.subtrees)\n return layersUsingVariables(graph, scope.variables)\n}\n\n/**\n * Resolve live numeric, text, visibility, and effect bindings, numbers in scene units, and refresh\n * bound paints whose variable's color for the layer changed (`refreshBoundPaints`), without\n * authoring instance overrides. A scope limits it to the layers a change can reach, so layers whose\n * saved values differ from their bindings elsewhere in the document are left as saved. Returns the\n * layers whose change needs a layout pass; effects and paints are not a layout input, so a change\n * to them alone is written but not returned.\n */\nexport function reconcileVariableBindings(graph: SceneGraph, scope?: BindingScope): string[] {\n return [...reconcileVariableBindingUpdates(graph, scope).keys()]\n}\n\n/**\n * As `reconcileVariableBindings`, with the layout fields each relaid layer received. Effects and\n * paints are written too, but draw without layout, so a layer whose only change is one is left out.\n */\nexport function reconcileVariableBindingUpdates(\n graph: SceneGraph,\n scope?: BindingScope\n): Map<string, Partial<SceneNode>> {\n const relaid = new Map<string, Partial<SceneNode>>()\n const nodes = [...scopedNodes(graph, scope)]\n for (const node of nodes) refreshBoundPaints(graph, node)\n for (const node of nodes) {\n const layoutUpdates = {\n ...resolvedNumericBindings(graph, node),\n ...resolvedValueBindings(graph, node),\n ...resolvedRunBindings(graph, node)\n }\n const updates = { ...layoutUpdates, ...resolvedEffectBindings(graph, node) }\n if (isEmptyObject(updates)) continue\n graph.updateNode(node.id, updates)\n if (!isEmptyObject(layoutUpdates)) relaid.set(node.id, layoutUpdates)\n }\n return relaid\n}\n\n/** A layer list whose items carry their own bindings, keyed by position such as `fills/1/color`. */\nexport type BoundLayerList = 'fills' | 'strokes' | 'effects'\n\n/**\n * `boundVariables` once the `list` items are rebuilt from `sources`, where `sources[i]` is the\n * position the new item `i` had before, or null for an added item. A binding moves with its item\n * and goes with a removed one, as Figma keeps a binding on its paint or effect.\n */\nexport function listBindingsAfterEdit(\n boundVariables: Readonly<Record<string, string>>,\n list: BoundLayerList,\n sources: readonly (number | null)[]\n): Record<string, string> {\n const prefix = `${list}/`\n const moved: Record<string, string> = {}\n for (const [field, variableId] of Object.entries(boundVariables)) {\n if (!field.startsWith(prefix)) {\n moved[field] = variableId\n continue\n }\n const [index, ...property] = field.slice(prefix.length).split('/')\n const next = sources.indexOf(Number(index))\n if (next !== -1 && property.length > 0)\n moved[`${prefix}${next}/${property.join('/')}`] = variableId\n }\n return moved\n}\n"],"mappings":";;;;;;;;;;;AAkCA,SAAgB,qBAAqB,OAAmB,MAA4B;CAClF,IAAI,QAAQ;CACZ,IAAI,UAAiC;CACrC,OAAO,SAAS;EACd,IAAI,QAAQ,SAAS,eAAe,QAAQ,SAAS,iBAAiB;EACtE,IAAI,QAAQ,SAAS,YAAY,QAAQ;EACzC,UAAU,QAAQ,WAAW,MAAM,QAAQ,QAAQ,QAAQ,IAAI,KAAA;CACjE;CACA,OAAO;AACT;AAEA,SAAgB,2BACd,OACA,MACA,OACM;CACN,IAAI,CAAC,8BAA8B,KAAK,GAAG;CAC3C,MAAM,QAAQ,qBAAqB,OAAO,IAAI;CAC9C,KAAK,wBAAwB;EAC3B,GAAG,KAAK;GACP,QAAQ,MAAM,yBAAyB,UAAU;CACpD;AACF;;AAGA,MAAM,qCAA0C,IAAI,IAAI,CAAC,cAAc,eAAe,CAAC;;AAGvF,SAAS,qBAAqB,OAAmB,QAAgB,OAAwB;CACvF,MAAM,WAAW,MAAM,QAAQ,SAAS,SAAS,KAAK,SAAS,UAAU;CACzE,OAAO,CAAC,CAAC,YAAY,oBAAoB,SAAS,mBAAmB,SAAS,IAAI,QAAQ,KAAK;AACjG;;AAGA,SAAgB,iBAAiB,OAAe,OAAoC;CAClF,OAAO,mBA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