@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
package/dist/variables/index.js
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { BLACK } from "../constants.js";
|
|
2
|
-
import { bindingOverrideField, setInstanceOverride } from "../instances/overrides.js";
|
|
2
|
+
import { bindingOverrideField, setInstanceOverride, variableModeOverrideField } from "../instances/overrides.js";
|
|
3
3
|
import { BOOLEAN_BINDING_FIELDS, ICON_BINDING_FIELDS, NUMERIC_FIELDS, STRING_BINDING_FIELDS, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, isNumericVariableBindingField, paintBindingTarget, uniformBindingTarget } from "./fields.js";
|
|
4
|
-
import { SCALED_RUN_BINDING_FIELDS, pinnedRunBindingScales, runBindingScale, scaleVariableBindingUnits, withBoundBindingScales } from "./units.js";
|
|
5
|
-
import { findInstanceAncestor } from "../instances/index.js";
|
|
6
|
-
import { isShaderSourcePaint, shaderOfPaint, withShaderPaints } from "../shaders/model.js";
|
|
7
|
-
import { variableFontFamily, variableText, variableTruthy } from "./text.js";
|
|
8
4
|
import { MAX_FORMULA_DEPTH, VariableFormulaError, foldFormula, isFormulaOperation, isFormulaReference } from "./formula/tree.js";
|
|
5
|
+
import { variableFontFamily, variableText, variableTruthy } from "./text.js";
|
|
9
6
|
import { formulaArity, formulaSymbol } from "./formula/signatures.js";
|
|
10
7
|
import { formulaValueType, inferFormulaType } from "./formula/check.js";
|
|
11
8
|
import { evaluateVariableFormula } from "./formula/evaluate.js";
|
|
12
9
|
import { variableAliasSchema, variableCollectionSchema, variableColorExpressionSchema, variableSchema } from "./schema.js";
|
|
13
10
|
import { isUnsupportedVariableValue, mapVariableValueReferences, resolveVariableValue, variableValueReferences } from "./value.js";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
11
|
+
import { VariableResolutionError, collectionsResolving, createBatchVariableResolver, getActiveModeId, getNodeExplicitVariableModeId, getNodeVariableModeId, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, resolvedVariableModes, variableValueInMode } from "./resolution.js";
|
|
12
|
+
import { applyBoundPaintColor, applyBoundPaintColorsIn, captureAppliedBoundPaints, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, keepPastedBoundPaints, paintColorApplied, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints, shownPaintBindings } from "./paints.js";
|
|
13
|
+
import { SCALED_RUN_BINDING_FIELDS, pinnedRunBindingScales, runBindingScale, scaleVariableBindingUnits, withBoundBindingScales } from "./units.js";
|
|
14
|
+
import { assignVariableBindingUnits, boundNumberValue, collectionsBoundIn, layersUsingVariables, listBindingsAfterEdit, reconcileVariableBindings, resolvedIconBinding, resolvedNumericBindings, resolvedRunBindings, resolvedValueBindings, variableBindingOwner, variablesResolvingThrough } from "./bindings.js";
|
|
15
|
+
import { findInstanceAncestor } from "../instances/index.js";
|
|
16
|
+
import { siblingOrderKeys } from "../order-keys.js";
|
|
17
|
+
import { isShaderSourcePaint, shaderOfPaint, withShaderPaints } from "../shaders/model.js";
|
|
16
18
|
import { independentCornerChanges } from "./corners.js";
|
|
17
19
|
import { formulaTokenName, formulaTokens, readFormulaToken } from "./formula/tokens.js";
|
|
18
20
|
import { printFormula } from "./formula/print.js";
|
|
@@ -20,7 +22,7 @@ import { formatVariableFormula, parseVariableFormula, variableFormulaReference }
|
|
|
20
22
|
import { variableFormulaSchema } from "./formula/schema.js";
|
|
21
23
|
import { ROOT_FONT_SIZE_PX, tokenExpressionForValue, tokenNumberFromUnit, tokenNumberInUnit, updateTokenExpression } from "./token.js";
|
|
22
24
|
import { omit, omitBy } from "es-toolkit/object";
|
|
23
|
-
import { partition } from "es-toolkit/array";
|
|
25
|
+
import { compact, partition, sortBy, uniq } from "es-toolkit/array";
|
|
24
26
|
//#region src/variables/index.ts
|
|
25
27
|
/**
|
|
26
28
|
* Gives the runs that bind `fields` the scale they resolve with as their own, before the layer's
|
|
@@ -109,6 +111,13 @@ function removeCollection(graph, id) {
|
|
|
109
111
|
if (collection) for (const varId of Array.from(collection.variableIds)) removeVariable(graph, varId);
|
|
110
112
|
graph.variableCollections.delete(id);
|
|
111
113
|
graph.activeMode.delete(id);
|
|
114
|
+
for (const node of graph.nodes.values()) {
|
|
115
|
+
if (!(id in node.variableModes)) continue;
|
|
116
|
+
const variableModes = omit(node.variableModes, [id]);
|
|
117
|
+
graph.willChangeNode(node);
|
|
118
|
+
node.variableModes = variableModes;
|
|
119
|
+
graph.emitter.emit("node:updated", node.id, { variableModes });
|
|
120
|
+
}
|
|
112
121
|
}
|
|
113
122
|
function setActiveMode(graph, collectionId, modeId) {
|
|
114
123
|
graph.activeMode.set(collectionId, modeId);
|
|
@@ -138,11 +147,21 @@ function removeMode(graph, collectionId, modeId) {
|
|
|
138
147
|
const collection = graph.variableCollections.get(collectionId);
|
|
139
148
|
if (!collection || collection.modes.length <= 1) return;
|
|
140
149
|
collection.modes = collection.modes.filter((m) => m.modeId !== modeId);
|
|
141
|
-
if (collection.defaultModeId === modeId) collection.defaultModeId = collection
|
|
150
|
+
if (collection.defaultModeId === modeId) collection.defaultModeId = modesInListedOrder(collection)[0].modeId;
|
|
142
151
|
for (const varId of collection.variableIds) {
|
|
143
152
|
const variable = graph.variables.get(varId);
|
|
144
153
|
if (variable) variable.valuesByMode = omit(variable.valuesByMode, [modeId]);
|
|
145
154
|
}
|
|
155
|
+
const overrideField = variableModeOverrideField(collectionId);
|
|
156
|
+
for (const node of graph.nodes.values()) {
|
|
157
|
+
if (node.variableModes[collectionId] !== modeId) continue;
|
|
158
|
+
const variableModes = omit(node.variableModes, [collectionId]);
|
|
159
|
+
graph.willChangeNode(node);
|
|
160
|
+
node.variableModes = variableModes;
|
|
161
|
+
node.instanceOverrides.self.delete(overrideField);
|
|
162
|
+
for (const overrides of node.instanceOverrides.descendants.values()) overrides.delete(overrideField);
|
|
163
|
+
graph.emitter.emit("node:updated", node.id, { variableModes });
|
|
164
|
+
}
|
|
146
165
|
if (graph.activeMode.get(collectionId) === modeId) graph.activeMode.set(collectionId, collection.defaultModeId);
|
|
147
166
|
}
|
|
148
167
|
function renameMode(graph, collectionId, modeId, name) {
|
|
@@ -156,13 +175,30 @@ function setDefaultMode(graph, collectionId, modeId) {
|
|
|
156
175
|
if (!collection) return;
|
|
157
176
|
if (!collection.modes.some((m) => m.modeId === modeId)) return;
|
|
158
177
|
collection.defaultModeId = modeId;
|
|
159
|
-
collection.modes
|
|
178
|
+
if (collection.modes.every((mode) => !mode.sortPosition)) {
|
|
179
|
+
collection.modes = modesDefaultFirst(collection);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const listed = modesDefaultFirst(collection);
|
|
183
|
+
const positions = siblingOrderKeys(listed.map((mode) => mode.sortPosition));
|
|
184
|
+
const byId = new Map(listed.map((mode, index) => [mode.modeId, positions[index]]));
|
|
185
|
+
collection.modes = collection.modes.map((mode) => ({
|
|
186
|
+
...mode,
|
|
187
|
+
sortPosition: byId.get(mode.modeId) ?? mode.sortPosition
|
|
188
|
+
}));
|
|
160
189
|
}
|
|
161
|
-
/** Figma has no default-mode field: the first mode is the default. */
|
|
190
|
+
/** Figma has no default-mode field: the first listed mode is the default. */
|
|
162
191
|
function modesDefaultFirst(collection) {
|
|
163
|
-
const [defaults, rest] = partition(collection
|
|
192
|
+
const [defaults, rest] = partition(modesInListedOrder(collection), (m) => m.modeId === collection.defaultModeId);
|
|
164
193
|
return [...defaults, ...rest];
|
|
165
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* The modes as Figma lists them: by `sortPosition`, modes without one following in saved order.
|
|
197
|
+
* The collection's own array keeps the file's saved order, which an export writes back as it was.
|
|
198
|
+
*/
|
|
199
|
+
function modesInListedOrder(collection) {
|
|
200
|
+
return sortBy(collection.modes, [(mode) => !mode.sortPosition, (mode) => mode.sortPosition ?? ""]);
|
|
201
|
+
}
|
|
166
202
|
function getVariablesForCollection(graph, collectionId) {
|
|
167
203
|
const collection = graph.variableCollections.get(collectionId);
|
|
168
204
|
if (!collection) return [];
|
|
@@ -207,7 +243,10 @@ function bindVariable(graph, nodeId, field, variableId) {
|
|
|
207
243
|
if (variable.type !== "COLOR") throw new Error(`Cannot bind ${variable.type} variable to color field "${field}"`);
|
|
208
244
|
const currentLength = node[paint.list].length;
|
|
209
245
|
if (paint.index >= currentLength) throw new Error(`Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`);
|
|
210
|
-
if (paint.list in node.boundVariables)
|
|
246
|
+
if (paint.list in node.boundVariables) {
|
|
247
|
+
graph.willChangeNode(node);
|
|
248
|
+
node.boundVariables = omit(node.boundVariables, [paint.list]);
|
|
249
|
+
}
|
|
211
250
|
}
|
|
212
251
|
checkBindingFieldType(field, variable.type);
|
|
213
252
|
const uniform = uniformBindingTarget(field);
|
|
@@ -244,6 +283,7 @@ function bindVariable(graph, nodeId, field, variableId) {
|
|
|
244
283
|
variableBindingScales: { ...node.variableBindingScales },
|
|
245
284
|
...runs
|
|
246
285
|
});
|
|
286
|
+
if (paint) applyBoundPaintColor(graph, node, field);
|
|
247
287
|
}
|
|
248
288
|
function unbindVariable(graph, nodeId, field) {
|
|
249
289
|
const node = graph.nodes.get(nodeId);
|
|
@@ -266,11 +306,14 @@ function markBoundVariablesOverrideOnInstance(graph, nodeId, field) {
|
|
|
266
306
|
const owner = variableBindingOwner(graph, node);
|
|
267
307
|
if (owner.type !== "INSTANCE") return;
|
|
268
308
|
const nearest = findInstanceAncestor(graph, nodeId);
|
|
309
|
+
const instances = uniq(compact([nearest, owner])).filter((x) => x.id !== nodeId);
|
|
310
|
+
for (const instance of instances) graph.willChangeNode(instance);
|
|
269
311
|
if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, "boundVariables");
|
|
270
312
|
setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, "boundVariables");
|
|
271
|
-
if (field) setInstanceOverride(
|
|
313
|
+
if (field) for (const instance of instances) setInstanceOverride(instance.instanceOverrides, instance.id, nodeId, bindingOverrideField(field), node.boundVariables[field] ?? null);
|
|
314
|
+
for (const instance of instances) graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides });
|
|
272
315
|
}
|
|
273
316
|
//#endregion
|
|
274
|
-
export { ICON_BINDING_FIELDS, MAX_FORMULA_DEPTH, ROOT_FONT_SIZE_PX, SCALED_RUN_BINDING_FIELDS, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, VariableFormulaError, VariableResolutionError, addCollection, addMode, addVariable, assignVariableBindingUnits, bindVariable, boundNumberValue, collectionsBoundIn, createBatchVariableResolver, createCollection, createMode, createVariable, evaluateVariableFormula, foldFormula, formatVariableFormula, formulaArity, formulaSymbol, formulaTokenName, formulaTokens, formulaValueType, getActiveModeId, getNodeVariableModeId, getVariablesByType, getVariablesForCollection, independentCornerChanges, inferFormulaType, isFormulaOperation, isFormulaReference, isNumericVariableBindingField, isUnsupportedVariableValue, layersUsingVariables, mapVariableValueReferences, modesDefaultFirst, paintBindingTarget, parseVariableFormula, pinnedRunBindingScales, printFormula, readFormulaToken, reconcileVariableBindings, removeCollection, removeMode, removeVariable, renameMode, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, resolveVariableValue, resolvedIconBinding, resolvedNumericBindings,
|
|
317
|
+
export { ICON_BINDING_FIELDS, MAX_FORMULA_DEPTH, ROOT_FONT_SIZE_PX, SCALED_RUN_BINDING_FIELDS, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, VariableFormulaError, VariableResolutionError, addCollection, addMode, addVariable, applyBoundPaintColor, applyBoundPaintColorsIn, assignVariableBindingUnits, bindVariable, boundNumberValue, captureAppliedBoundPaints, collectionsBoundIn, collectionsResolving, copiesComponentPaints, copyAppliedBoundPaints, createBatchVariableResolver, createCollection, createMode, createVariable, evaluateVariableFormula, foldFormula, formatVariableFormula, formulaArity, formulaSymbol, formulaTokenName, formulaTokens, formulaValueType, getActiveModeId, getNodeExplicitVariableModeId, getNodeVariableModeId, getVariablesByType, getVariablesForCollection, hasBoundPaints, independentCornerChanges, inferFormulaType, isFormulaOperation, isFormulaReference, isNumericVariableBindingField, isUnsupportedVariableValue, keepCopyBoundPaints, keepPastedBoundPaints, layersUsingVariables, listBindingsAfterEdit, mapVariableValueReferences, modesDefaultFirst, modesInListedOrder, paintBindingTarget, paintColorApplied, parseVariableFormula, pinnedRunBindingScales, printFormula, readFormulaToken, reconcileVariableBindings, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints, removeCollection, removeMode, removeVariable, renameMode, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, resolveVariableValue, resolvedIconBinding, resolvedNumericBindings, resolvedRunBindings, resolvedValueBindings, resolvedVariableModes, runBindingScale, scaleVariableBindingUnits, setActiveMode, setDefaultMode, shownPaintBindings, tokenExpressionForValue, tokenNumberFromUnit, tokenNumberInUnit, unbindVariable, updateTokenExpression, variableAliasSchema, variableBindingOwner, variableCollectionSchema, variableColorExpressionSchema, variableFontFamily, variableFormulaReference, variableFormulaSchema, variableSchema, variableText, variableTruthy, variableValueInMode, variableValueReferences, variablesResolvingThrough, withBoundBindingScales };
|
|
275
318
|
|
|
276
319
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/variables/index.ts"],"sourcesContent":["import { partition } from 'es-toolkit/array'\nimport { omit, omitBy } from 'es-toolkit/object'\n\nimport { BLACK } from '../constants'\nimport type { SceneGraph } from '../graph'\nimport { findInstanceAncestor } from '../instances'\nimport { bindingOverrideField, setInstanceOverride } from '../instances/overrides'\nimport { isShaderSourcePaint, shaderOfPaint, withShaderPaints } from '../shaders/model'\nimport type {\n SceneNode,\n Variable,\n VariableCollection,\n VariableCollectionMode,\n VariableType,\n VariableValue\n} from '../types'\nimport {\n isNumericVariableBindingField,\n variableBindingOwner,\n assignVariableBindingUnits\n} from './bindings'\nimport {\n BOOLEAN_BINDING_FIELDS,\n ICON_BINDING_FIELDS,\n NUMERIC_FIELDS,\n paintBindingTarget,\n STRING_BINDING_FIELDS,\n TEXT_CONTENT_VARIABLE_TYPES,\n uniformBindingTarget,\n VISIBILITY_VARIABLE_TYPES,\n type PaintBindingList\n} from './fields'\nimport { pinnedRunBindingScales } from './units'\n\nexport * from './resolution'\n\n/**\n * Gives the runs that bind `fields` the scale they resolve with as their own, before the layer's\n * scale for those fields changes or goes, and the change to announce.\n */\nfunction pinRunBindingScales(\n node: SceneNode,\n fields: readonly string[]\n): Pick<Partial<SceneNode>, 'styleRuns'> {\n const styleRuns = pinnedRunBindingScales(node, fields)\n if (!styleRuns) return {}\n node.styleRuns = styleRuns\n return { styleRuns }\n}\n\nexport function addVariable(graph: SceneGraph, variable: Variable): void {\n graph.variables.set(variable.id, variable)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection && !collection.variableIds.includes(variable.id)) {\n collection.variableIds.push(variable.id)\n }\n}\n\nexport function removeVariable(graph: SceneGraph, id: string): void {\n const variable = graph.variables.get(id)\n if (!variable) return\n graph.variables.delete(id)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection) {\n collection.variableIds = collection.variableIds.filter((vid) => vid !== id)\n }\n for (const node of graph.nodes.values()) {\n const hadBinding = Object.values(node.boundVariables).includes(id)\n if (!hadBinding) continue\n graph.willChangeNode(node)\n node.boundVariables = omitBy(node.boundVariables, (varId) => varId === id) as Record<\n string,\n string\n >\n const pruned = Object.keys(node.variableBindingScales).filter(\n (field) => !(field in node.boundVariables)\n )\n const runs = pinRunBindingScales(node, pruned)\n node.variableBindingScales = omit(node.variableBindingScales, pruned)\n graph.emitter.emit('node:updated', node.id, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales },\n ...runs\n })\n markBoundVariablesOverrideOnInstance(graph, node.id)\n }\n}\n\nexport function addCollection(graph: SceneGraph, collection: VariableCollection): void {\n graph.variableCollections.set(collection.id, collection)\n if (!graph.activeMode.has(collection.id)) {\n graph.activeMode.set(collection.id, collection.defaultModeId)\n }\n}\n\nfunction defaultVariableValue(type: VariableType, value?: VariableValue): VariableValue {\n if (value !== undefined) return value\n if (type === 'COLOR') return { ...BLACK }\n if (type === 'FLOAT') return 0\n if (type === 'BOOLEAN') return false\n return ''\n}\n\nexport function createVariable(\n graph: SceneGraph,\n generateId: () => string,\n name: string,\n type: VariableType,\n collectionId: string,\n value?: VariableValue\n): Variable {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) throw new Error(`Collection \"${collectionId}\" not found`)\n const id = generateId()\n const defaultValue = defaultVariableValue(type, value)\n const valuesByMode: Record<string, VariableValue> = {}\n for (const mode of collection.modes) {\n valuesByMode[mode.modeId] = structuredClone(defaultValue)\n }\n const variable: Variable = {\n id,\n name,\n type,\n collectionId,\n valuesByMode,\n description: '',\n hiddenFromPublishing: false\n }\n addVariable(graph, variable)\n return variable\n}\n\nexport function createCollection(\n graph: SceneGraph,\n generateId: () => string,\n name: string\n): VariableCollection {\n const id = generateId()\n const modeId = generateId()\n const collection: VariableCollection = {\n id,\n name,\n modes: [{ modeId, name: 'Mode 1' }],\n defaultModeId: modeId,\n variableIds: []\n }\n addCollection(graph, collection)\n return collection\n}\n\nexport function removeCollection(graph: SceneGraph, id: string): void {\n const collection = graph.variableCollections.get(id)\n if (collection) {\n for (const varId of Array.from(collection.variableIds)) {\n removeVariable(graph, varId)\n }\n }\n graph.variableCollections.delete(id)\n graph.activeMode.delete(id)\n}\n\nexport function setActiveMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n graph.activeMode.set(collectionId, modeId)\n}\n\nexport function addMode(\n graph: SceneGraph,\n collectionId: string,\n modeId: string,\n name: string,\n sourceMode?: string\n): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n collection.modes.push({ modeId, name })\n const sourceModeId = sourceMode ?? collection.defaultModeId\n for (const varId of collection.variableIds) {\n const variable = graph.variables.get(varId)\n if (!variable) continue\n variable.valuesByMode[modeId] = structuredClone(\n variable.valuesByMode[sourceModeId] ?? Object.values(variable.valuesByMode)[0]\n )\n }\n}\n\n/** Add a mode with a new ID from `generateId`; undo and redo replay it with `addMode`. */\nexport function createMode(\n graph: SceneGraph,\n generateId: () => string,\n collectionId: string,\n name: string,\n sourceMode?: string\n): string | undefined {\n if (!graph.variableCollections.has(collectionId)) return undefined\n const modeId = generateId()\n addMode(graph, collectionId, modeId, name, sourceMode)\n return modeId\n}\n\nexport function removeMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection || collection.modes.length <= 1) return\n collection.modes = collection.modes.filter((m) => m.modeId !== modeId)\n if (collection.defaultModeId === modeId) {\n collection.defaultModeId = collection.modes[0].modeId\n }\n for (const varId of collection.variableIds) {\n const variable = graph.variables.get(varId)\n if (variable) variable.valuesByMode = omit(variable.valuesByMode, [modeId])\n }\n if (graph.activeMode.get(collectionId) === modeId) {\n graph.activeMode.set(collectionId, collection.defaultModeId)\n }\n}\n\nexport function renameMode(\n graph: SceneGraph,\n collectionId: string,\n modeId: string,\n name: string\n): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n const mode = collection.modes.find((m) => m.modeId === modeId)\n if (mode) mode.name = name\n}\n\nexport function setDefaultMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n if (!collection.modes.some((m) => m.modeId === modeId)) return\n collection.defaultModeId = modeId\n collection.modes = modesDefaultFirst(collection)\n}\n\n/** Figma has no default-mode field: the first mode is the default. */\nexport function modesDefaultFirst(collection: VariableCollection): VariableCollectionMode[] {\n const [defaults, rest] = partition(collection.modes, (m) => m.modeId === collection.defaultModeId)\n return [...defaults, ...rest]\n}\n\nexport function getVariablesForCollection(graph: SceneGraph, collectionId: string): Variable[] {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return []\n return collection.variableIds\n .map((id) => graph.variables.get(id))\n .filter((v): v is Variable => v !== undefined)\n}\n\nexport function getVariablesByType(graph: SceneGraph, type: VariableType): Variable[] {\n return [...graph.variables.values()].filter((v) => v.type === type)\n}\n\nconst PAINT_BINDING_LISTS = ['fills', 'strokes'] as const satisfies readonly PaintBindingList[]\n\n/**\n * Every field a binding takes, from the same sets `checkBindingFieldType` reads, so a refusal for\n * an unknown one names what the caller can send instead of leaving them to guess.\n */\nconst ACCEPTED_BINDING_FIELDS: readonly string[] = [\n ...NUMERIC_FIELDS,\n ...STRING_BINDING_FIELDS,\n ...BOOLEAN_BINDING_FIELDS,\n ...ICON_BINDING_FIELDS,\n ...PAINT_BINDING_LISTS.flatMap((list) => [`${list}/<n>/color`, `${list}/<n>/uniforms/<name>`])\n]\n\n/**\n * Throws when a variable of `type` cannot bind to `field`, or for a field no binding takes. A\n * paint target is checked by its caller, which also validates its index.\n */\nfunction checkBindingFieldType(field: string, type: VariableType): void {\n if (isNumericVariableBindingField(field) && type !== 'FLOAT') {\n throw new Error(`Cannot bind ${type} variable to scalar field \"${field}\"`)\n }\n\n const textTypes: readonly VariableType[] =\n field === 'text' ? TEXT_CONTENT_VARIABLE_TYPES : ['STRING']\n if (STRING_BINDING_FIELDS.has(field) && !textTypes.includes(type)) {\n throw new Error(`Cannot bind ${type} variable to string field \"${field}\"`)\n }\n\n if (\n BOOLEAN_BINDING_FIELDS.has(field) &&\n !VISIBILITY_VARIABLE_TYPES.some((kind) => kind === type)\n ) {\n throw new Error(`Cannot bind ${type} variable to boolean field \"${field}\"`)\n }\n\n if (ICON_BINDING_FIELDS.has(field) && type !== 'STRING') {\n throw new Error(`Cannot bind ${type} variable to icon field \"${field}\"`)\n }\n\n // A shader uniform takes a number, a colour, or a boolean; its shader declares which.\n const uniform = uniformBindingTarget(field)\n if (uniform && type !== 'FLOAT' && type !== 'COLOR' && type !== 'BOOLEAN') {\n throw new Error(`Cannot bind ${type} variable to shader input \"${field}\"`)\n }\n\n const isKnownField =\n isNumericVariableBindingField(field) ||\n STRING_BINDING_FIELDS.has(field) ||\n BOOLEAN_BINDING_FIELDS.has(field) ||\n ICON_BINDING_FIELDS.has(field) ||\n paintBindingTarget(field) !== null ||\n uniform !== null\n\n if (!isKnownField) {\n throw new Error(\n `Unknown binding field \"${field}\". Accepted: ${ACCEPTED_BINDING_FIELDS.join(', ')}`\n )\n }\n}\nexport function bindVariable(\n graph: SceneGraph,\n nodeId: string,\n field: string,\n variableId: string\n): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n\n // Validate variable exists\n const variable = graph.variables.get(variableId)\n if (!variable) {\n throw new Error(`Variable \"${variableId}\" not found`)\n }\n\n // Color fields require COLOR variable type\n const paint = paintBindingTarget(field)\n if (paint) {\n if (variable.type !== 'COLOR') {\n throw new Error(`Cannot bind ${variable.type} variable to color field \"${field}\"`)\n }\n // Validate index is within current array bounds\n const currentLength = node[paint.list].length\n if (paint.index >= currentLength) {\n throw new Error(\n `Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`\n )\n }\n // Auto-remove top-level dead binding (e.g. 'fills') when setting indexed binding\n if (paint.list in node.boundVariables) {\n node.boundVariables = omit(node.boundVariables, [paint.list])\n }\n }\n\n checkBindingFieldType(field, variable.type)\n\n // A shader input belongs to one paint, so its index must exist on the layer.\n const uniform = uniformBindingTarget(field)\n if (uniform && uniform.index >= node[uniform.list].length) {\n throw new Error(\n `Index ${uniform.index} out of range for ${uniform.list} (length ${node[uniform.list].length})`\n )\n }\n if (uniform) {\n // The binding lives with the shader entry, next to the value it resolves, so any renderer\n // that draws the paint resolves it the same way.\n const paints = node[uniform.list]\n const paint = paints.at(uniform.index)\n const shader = paint ? shaderOfPaint(node, paint) : null\n if (paint && shader && isShaderSourcePaint(shader)) {\n node.pluginData = withShaderPaints(node, paints, [\n {\n ...shader,\n uniformBindings: {\n ...shader.uniformBindings,\n [uniform.name]: variableId\n }\n }\n ])\n }\n graph.willChangeNode(node)\n graph.emitter.emit('node:updated', nodeId, {\n pluginData: node.pluginData,\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n return\n }\n graph.willChangeNode(node)\n node.boundVariables = { ...node.boundVariables, [field]: variableId }\n const runs = pinRunBindingScales(node, [field])\n assignVariableBindingUnits(graph, node, field)\n markBoundVariablesOverrideOnInstance(graph, nodeId, field)\n graph.emitter.emit('node:updated', nodeId, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales },\n ...runs\n })\n}\n\nexport function unbindVariable(graph: SceneGraph, nodeId: string, field: string): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n if (!(field in node.boundVariables)) return\n graph.willChangeNode(node)\n node.boundVariables = omit(node.boundVariables, [field])\n const runs = pinRunBindingScales(node, [field])\n node.variableBindingScales = omit(node.variableBindingScales, [field])\n markBoundVariablesOverrideOnInstance(graph, nodeId, field)\n graph.emitter.emit('node:updated', nodeId, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales },\n ...runs\n })\n}\n\nfunction markBoundVariablesOverrideOnInstance(\n graph: SceneGraph,\n nodeId: string,\n field?: string\n): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n\n const owner = variableBindingOwner(graph, node)\n if (owner.type !== 'INSTANCE') return\n const nearest = findInstanceAncestor(graph, nodeId)\n if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, 'boundVariables')\n setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, 'boundVariables')\n if (field)\n setInstanceOverride(\n owner.instanceOverrides,\n owner.id,\n nodeId,\n bindingOverrideField(field),\n node.boundVariables[field] ?? null\n )\n}\n\nexport * from './bindings'\nexport * from './corners'\nexport * from './formula'\nexport * from './formula/schema'\nexport * from './schema'\nexport * from './text'\nexport * from './token'\nexport * from './units'\nexport * from './value'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,SAAS,oBACP,MACA,QACuC;CACvC,MAAM,YAAY,uBAAuB,MAAM,MAAM;CACrD,IAAI,CAAC,WAAW,OAAO,CAAC;CACxB,KAAK,YAAY;CACjB,OAAO,EAAE,UAAU;AACrB;AAEA,SAAgB,YAAY,OAAmB,UAA0B;CACvE,MAAM,UAAU,IAAI,SAAS,IAAI,QAAQ;CACzC,MAAM,aAAa,MAAM,oBAAoB,IAAI,SAAS,YAAY;CACtE,IAAI,cAAc,CAAC,WAAW,YAAY,SAAS,SAAS,EAAE,GAC5D,WAAW,YAAY,KAAK,SAAS,EAAE;AAE3C;AAE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,CAAC;CACrE,qCAAqC,OAAO,QAAQ,KAAK;CACzD,MAAM,QAAQ,KAAK,gBAAgB,QAAQ;EACzC,gBAAgB,EAAE,GAAG,KAAK,eAAe;EACzC,uBAAuB,EAAE,GAAG,KAAK,sBAAsB;EACvD,GAAG;CACL,CAAC;AACH;AAEA,SAAS,qCACP,OACA,QACA,OACM;CACN,MAAM,OAAO,MAAM,MAAM,IAAI,MAAM;CACnC,IAAI,CAAC,MAAM;CAEX,MAAM,QAAQ,qBAAqB,OAAO,IAAI;CAC9C,IAAI,MAAM,SAAS,YAAY;CAC/B,MAAM,UAAU,qBAAqB,OAAO,MAAM;CAClD,IAAI,SAAS,oBAAoB,QAAQ,mBAAmB,QAAQ,IAAI,QAAQ,gBAAgB;CAChG,oBAAoB,MAAM,mBAAmB,MAAM,IAAI,QAAQ,gBAAgB;CAC/E,IAAI,OACF,oBACE,MAAM,mBACN,MAAM,IACN,QACA,qBAAqB,KAAK,GAC1B,KAAK,eAAe,UAAU,IAChC;AACJ"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/variables/index.ts"],"sourcesContent":["import { compact, partition, sortBy, uniq } from 'es-toolkit/array'\nimport { omit, omitBy } from 'es-toolkit/object'\n\nimport { BLACK } from '../constants'\nimport type { SceneGraph } from '../graph'\nimport { findInstanceAncestor } from '../instances'\nimport {\n bindingOverrideField,\n setInstanceOverride,\n variableModeOverrideField\n} from '../instances/overrides'\nimport { siblingOrderKeys } from '../order-keys'\nimport { isShaderSourcePaint, shaderOfPaint, withShaderPaints } from '../shaders/model'\nimport type {\n SceneNode,\n Variable,\n VariableCollection,\n VariableCollectionMode,\n VariableType,\n VariableValue\n} from '../types'\nimport {\n isNumericVariableBindingField,\n variableBindingOwner,\n assignVariableBindingUnits\n} from './bindings'\nimport {\n BOOLEAN_BINDING_FIELDS,\n ICON_BINDING_FIELDS,\n NUMERIC_FIELDS,\n paintBindingTarget,\n STRING_BINDING_FIELDS,\n TEXT_CONTENT_VARIABLE_TYPES,\n uniformBindingTarget,\n VISIBILITY_VARIABLE_TYPES,\n type PaintBindingList\n} from './fields'\nimport { applyBoundPaintColor } from './paints'\nimport { pinnedRunBindingScales } from './units'\n\nexport * from './resolution'\n\n/**\n * Gives the runs that bind `fields` the scale they resolve with as their own, before the layer's\n * scale for those fields changes or goes, and the change to announce.\n */\nfunction pinRunBindingScales(\n node: SceneNode,\n fields: readonly string[]\n): Pick<Partial<SceneNode>, 'styleRuns'> {\n const styleRuns = pinnedRunBindingScales(node, fields)\n if (!styleRuns) return {}\n node.styleRuns = styleRuns\n return { styleRuns }\n}\n\nexport function addVariable(graph: SceneGraph, variable: Variable): void {\n graph.variables.set(variable.id, variable)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection && !collection.variableIds.includes(variable.id)) {\n collection.variableIds.push(variable.id)\n }\n}\n\nexport function removeVariable(graph: SceneGraph, id: string): void {\n const variable = graph.variables.get(id)\n if (!variable) return\n graph.variables.delete(id)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection) {\n collection.variableIds = collection.variableIds.filter((vid) => vid !== id)\n }\n for (const node of graph.nodes.values()) {\n const hadBinding = Object.values(node.boundVariables).includes(id)\n if (!hadBinding) continue\n graph.willChangeNode(node)\n node.boundVariables = omitBy(node.boundVariables, (varId) => varId === id) as Record<\n string,\n string\n >\n const pruned = Object.keys(node.variableBindingScales).filter(\n (field) => !(field in node.boundVariables)\n )\n const runs = pinRunBindingScales(node, pruned)\n node.variableBindingScales = omit(node.variableBindingScales, pruned)\n graph.emitter.emit('node:updated', node.id, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales },\n ...runs\n })\n markBoundVariablesOverrideOnInstance(graph, node.id)\n }\n}\n\nexport function addCollection(graph: SceneGraph, collection: VariableCollection): void {\n graph.variableCollections.set(collection.id, collection)\n if (!graph.activeMode.has(collection.id)) {\n graph.activeMode.set(collection.id, collection.defaultModeId)\n }\n}\n\nfunction defaultVariableValue(type: VariableType, value?: VariableValue): VariableValue {\n if (value !== undefined) return value\n if (type === 'COLOR') return { ...BLACK }\n if (type === 'FLOAT') return 0\n if (type === 'BOOLEAN') return false\n return ''\n}\n\nexport function createVariable(\n graph: SceneGraph,\n generateId: () => string,\n name: string,\n type: VariableType,\n collectionId: string,\n value?: VariableValue\n): Variable {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) throw new Error(`Collection \"${collectionId}\" not found`)\n const id = generateId()\n const defaultValue = defaultVariableValue(type, value)\n const valuesByMode: Record<string, VariableValue> = {}\n for (const mode of collection.modes) {\n valuesByMode[mode.modeId] = structuredClone(defaultValue)\n }\n const variable: Variable = {\n id,\n name,\n type,\n collectionId,\n valuesByMode,\n description: '',\n hiddenFromPublishing: false\n }\n addVariable(graph, variable)\n return variable\n}\n\nexport function createCollection(\n graph: SceneGraph,\n generateId: () => string,\n name: string\n): VariableCollection {\n const id = generateId()\n const modeId = generateId()\n const collection: VariableCollection = {\n id,\n name,\n modes: [{ modeId, name: 'Mode 1' }],\n defaultModeId: modeId,\n variableIds: []\n }\n addCollection(graph, collection)\n return collection\n}\n\nexport function removeCollection(graph: SceneGraph, id: string): void {\n const collection = graph.variableCollections.get(id)\n if (collection) {\n for (const varId of Array.from(collection.variableIds)) {\n removeVariable(graph, varId)\n }\n }\n graph.variableCollections.delete(id)\n graph.activeMode.delete(id)\n // A layer that chose one of its modes has nothing left to choose from.\n for (const node of graph.nodes.values()) {\n if (!(id in node.variableModes)) continue\n const variableModes = omit(node.variableModes, [id])\n graph.willChangeNode(node)\n node.variableModes = variableModes\n graph.emitter.emit('node:updated', node.id, { variableModes })\n }\n}\n\nexport function setActiveMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n graph.activeMode.set(collectionId, modeId)\n}\n\nexport function addMode(\n graph: SceneGraph,\n collectionId: string,\n modeId: string,\n name: string,\n sourceMode?: string\n): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n collection.modes.push({ modeId, name })\n const sourceModeId = sourceMode ?? collection.defaultModeId\n for (const varId of collection.variableIds) {\n const variable = graph.variables.get(varId)\n if (!variable) continue\n variable.valuesByMode[modeId] = structuredClone(\n variable.valuesByMode[sourceModeId] ?? Object.values(variable.valuesByMode)[0]\n )\n }\n}\n\n/** Add a mode with a new ID from `generateId`; undo and redo replay it with `addMode`. */\nexport function createMode(\n graph: SceneGraph,\n generateId: () => string,\n collectionId: string,\n name: string,\n sourceMode?: string\n): string | undefined {\n if (!graph.variableCollections.has(collectionId)) return undefined\n const modeId = generateId()\n addMode(graph, collectionId, modeId, name, sourceMode)\n return modeId\n}\n\nexport function removeMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection || collection.modes.length <= 1) return\n collection.modes = collection.modes.filter((m) => m.modeId !== modeId)\n if (collection.defaultModeId === modeId) {\n // The next default is the next listed mode, as Figma picks it, not the next saved entry.\n collection.defaultModeId = modesInListedOrder(collection)[0].modeId\n }\n for (const varId of collection.variableIds) {\n const variable = graph.variables.get(varId)\n if (variable) variable.valuesByMode = omit(variable.valuesByMode, [modeId])\n }\n // A layer that chose the mode goes back to the collection's default, and an instance keeps no\n // override naming a mode that is gone, which would otherwise come back on a later sync.\n const overrideField = variableModeOverrideField(collectionId)\n for (const node of graph.nodes.values()) {\n if (node.variableModes[collectionId] !== modeId) continue\n const variableModes = omit(node.variableModes, [collectionId])\n graph.willChangeNode(node)\n node.variableModes = variableModes\n node.instanceOverrides.self.delete(overrideField)\n for (const overrides of node.instanceOverrides.descendants.values())\n overrides.delete(overrideField)\n graph.emitter.emit('node:updated', node.id, { variableModes })\n }\n if (graph.activeMode.get(collectionId) === modeId) {\n graph.activeMode.set(collectionId, collection.defaultModeId)\n }\n}\n\nexport function renameMode(\n graph: SceneGraph,\n collectionId: string,\n modeId: string,\n name: string\n): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n const mode = collection.modes.find((m) => m.modeId === modeId)\n if (mode) mode.name = name\n}\n\nexport function setDefaultMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n if (!collection.modes.some((m) => m.modeId === modeId)) return\n collection.defaultModeId = modeId\n if (collection.modes.every((mode) => !mode.sortPosition)) {\n // No sort positions carry an order of their own: the array is the order, so the new\n // default moves first in it, as a collection created here has always done.\n collection.modes = modesDefaultFirst(collection)\n return\n }\n // The saved array order stays the file's own; the new default sorts first by its sort\n // position, so the listed order shows it first and a re-import reads it as the default.\n const listed = modesDefaultFirst(collection)\n const positions = siblingOrderKeys(listed.map((mode) => mode.sortPosition))\n const byId = new Map(listed.map((mode, index) => [mode.modeId, positions[index]]))\n collection.modes = collection.modes.map((mode) => ({\n ...mode,\n sortPosition: byId.get(mode.modeId) ?? mode.sortPosition\n }))\n}\n\n/** Figma has no default-mode field: the first listed mode is the default. */\nexport function modesDefaultFirst(collection: VariableCollection): VariableCollectionMode[] {\n const [defaults, rest] = partition(\n modesInListedOrder(collection),\n (m) => m.modeId === collection.defaultModeId\n )\n return [...defaults, ...rest]\n}\n\n/**\n * The modes as Figma lists them: by `sortPosition`, modes without one following in saved order.\n * The collection's own array keeps the file's saved order, which an export writes back as it was.\n */\nexport function modesInListedOrder(collection: VariableCollection): VariableCollectionMode[] {\n return sortBy(collection.modes, [\n (mode) => !mode.sortPosition,\n (mode) => mode.sortPosition ?? ''\n ])\n}\n\nexport function getVariablesForCollection(graph: SceneGraph, collectionId: string): Variable[] {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return []\n return collection.variableIds\n .map((id) => graph.variables.get(id))\n .filter((v): v is Variable => v !== undefined)\n}\n\nexport function getVariablesByType(graph: SceneGraph, type: VariableType): Variable[] {\n return [...graph.variables.values()].filter((v) => v.type === type)\n}\n\nconst PAINT_BINDING_LISTS = ['fills', 'strokes'] as const satisfies readonly PaintBindingList[]\n\n/**\n * Every field a binding takes, from the same sets `checkBindingFieldType` reads, so a refusal for\n * an unknown one names what the caller can send instead of leaving them to guess.\n */\nconst ACCEPTED_BINDING_FIELDS: readonly string[] = [\n ...NUMERIC_FIELDS,\n ...STRING_BINDING_FIELDS,\n ...BOOLEAN_BINDING_FIELDS,\n ...ICON_BINDING_FIELDS,\n ...PAINT_BINDING_LISTS.flatMap((list) => [`${list}/<n>/color`, `${list}/<n>/uniforms/<name>`])\n]\n\n/**\n * Throws when a variable of `type` cannot bind to `field`, or for a field no binding takes. A\n * paint target is checked by its caller, which also validates its index.\n */\nfunction checkBindingFieldType(field: string, type: VariableType): void {\n if (isNumericVariableBindingField(field) && type !== 'FLOAT') {\n throw new Error(`Cannot bind ${type} variable to scalar field \"${field}\"`)\n }\n\n const textTypes: readonly VariableType[] =\n field === 'text' ? TEXT_CONTENT_VARIABLE_TYPES : ['STRING']\n if (STRING_BINDING_FIELDS.has(field) && !textTypes.includes(type)) {\n throw new Error(`Cannot bind ${type} variable to string field \"${field}\"`)\n }\n\n if (\n BOOLEAN_BINDING_FIELDS.has(field) &&\n !VISIBILITY_VARIABLE_TYPES.some((kind) => kind === type)\n ) {\n throw new Error(`Cannot bind ${type} variable to boolean field \"${field}\"`)\n }\n\n if (ICON_BINDING_FIELDS.has(field) && type !== 'STRING') {\n throw new Error(`Cannot bind ${type} variable to icon field \"${field}\"`)\n }\n\n // A shader uniform takes a number, a colour, or a boolean; its shader declares which.\n const uniform = uniformBindingTarget(field)\n if (uniform && type !== 'FLOAT' && type !== 'COLOR' && type !== 'BOOLEAN') {\n throw new Error(`Cannot bind ${type} variable to shader input \"${field}\"`)\n }\n\n const isKnownField =\n isNumericVariableBindingField(field) ||\n STRING_BINDING_FIELDS.has(field) ||\n BOOLEAN_BINDING_FIELDS.has(field) ||\n ICON_BINDING_FIELDS.has(field) ||\n paintBindingTarget(field) !== null ||\n uniform !== null\n\n if (!isKnownField) {\n throw new Error(\n `Unknown binding field \"${field}\". Accepted: ${ACCEPTED_BINDING_FIELDS.join(', ')}`\n )\n }\n}\nexport function bindVariable(\n graph: SceneGraph,\n nodeId: string,\n field: string,\n variableId: string\n): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n\n // Validate variable exists\n const variable = graph.variables.get(variableId)\n if (!variable) {\n throw new Error(`Variable \"${variableId}\" not found`)\n }\n\n const paint = paintBindingTarget(field)\n if (paint) {\n if (variable.type !== 'COLOR') {\n throw new Error(`Cannot bind ${variable.type} variable to color field \"${field}\"`)\n }\n // Validate index is within current array bounds\n const currentLength = node[paint.list].length\n if (paint.index >= currentLength) {\n throw new Error(\n `Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`\n )\n }\n // Auto-remove top-level dead binding (e.g. 'fills') when setting indexed binding\n if (paint.list in node.boundVariables) {\n graph.willChangeNode(node)\n node.boundVariables = omit(node.boundVariables, [paint.list])\n }\n }\n\n checkBindingFieldType(field, variable.type)\n\n // A shader input belongs to one paint, so its index must exist on the layer.\n const uniform = uniformBindingTarget(field)\n if (uniform && uniform.index >= node[uniform.list].length) {\n throw new Error(\n `Index ${uniform.index} out of range for ${uniform.list} (length ${node[uniform.list].length})`\n )\n }\n if (uniform) {\n // The binding lives with the shader entry, next to the value it resolves, so any renderer\n // that draws the paint resolves it the same way.\n const paints = node[uniform.list]\n const paint = paints.at(uniform.index)\n const shader = paint ? shaderOfPaint(node, paint) : null\n if (paint && shader && isShaderSourcePaint(shader)) {\n node.pluginData = withShaderPaints(node, paints, [\n {\n ...shader,\n uniformBindings: {\n ...shader.uniformBindings,\n [uniform.name]: variableId\n }\n }\n ])\n }\n graph.willChangeNode(node)\n graph.emitter.emit('node:updated', nodeId, {\n pluginData: node.pluginData,\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n return\n }\n graph.willChangeNode(node)\n node.boundVariables = { ...node.boundVariables, [field]: variableId }\n const runs = pinRunBindingScales(node, [field])\n assignVariableBindingUnits(graph, node, field)\n markBoundVariablesOverrideOnInstance(graph, nodeId, field)\n graph.emitter.emit('node:updated', nodeId, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales },\n ...runs\n })\n // A paint takes its variable's color, alpha as opacity, when it binds, as Figma's does.\n if (paint) applyBoundPaintColor(graph, node, field)\n}\n\nexport function unbindVariable(graph: SceneGraph, nodeId: string, field: string): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n if (!(field in node.boundVariables)) return\n graph.willChangeNode(node)\n node.boundVariables = omit(node.boundVariables, [field])\n const runs = pinRunBindingScales(node, [field])\n node.variableBindingScales = omit(node.variableBindingScales, [field])\n markBoundVariablesOverrideOnInstance(graph, nodeId, field)\n graph.emitter.emit('node:updated', nodeId, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales },\n ...runs\n })\n}\n\nfunction markBoundVariablesOverrideOnInstance(\n graph: SceneGraph,\n nodeId: string,\n field?: string\n): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n\n const owner = variableBindingOwner(graph, node)\n if (owner.type !== 'INSTANCE') return\n const nearest = findInstanceAncestor(graph, nodeId)\n const instances = uniq(compact([nearest, owner])).filter((x) => x.id !== nodeId)\n for (const instance of instances) graph.willChangeNode(instance)\n if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, 'boundVariables')\n setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, 'boundVariables')\n // Each instance claims the binding it changed alone, so the layer's other bindings keep\n // following their components, as Figma claims only the paint or effect a script edits.\n if (field)\n for (const instance of instances)\n setInstanceOverride(\n instance.instanceOverrides,\n instance.id,\n nodeId,\n bindingOverrideField(field),\n node.boundVariables[field] ?? null\n )\n for (const instance of instances)\n graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })\n}\n\nexport * from './bindings'\nexport * from './corners'\nexport * from './formula'\nexport * from './formula/schema'\nexport * from './paints'\nexport * from './schema'\nexport * from './text'\nexport * from './token'\nexport * from './units'\nexport * from './value'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,SAAS,oBACP,MACA,QACuC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YAAY,WACrB,MAAM,WAAW,SAAS,IAAI,EAAE,mBAAmB,SAAS,kBAAkB,CAAC;AACnF"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Color } from "../primitives/index.js";
|
|
2
|
+
import { PaintBindingList } from "./fields.js";
|
|
3
|
+
import { SceneGraph } from "../graph.js";
|
|
4
|
+
import { SceneNode } from "../types.js";
|
|
5
|
+
//#region src/variables/paints.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Figma keeps a bound paint's color and opacity as a cache of its variable: it draws and reads
|
|
8
|
+
* what the paint holds, and writes the variable's color into it again only once the color the
|
|
9
|
+
* variable gives that layer changes, or the mode set for the layer changes, so an edit to the
|
|
10
|
+
* paint sticks until then. The color each binding last gave its paint, and the modes set for the
|
|
11
|
+
* layer then, live here, beside the graph and never saved, by layer and binding field. A layer
|
|
12
|
+
* without an entry, such as one just restored, counts its paint's own color as the last one
|
|
13
|
+
* applied, in the modes it has now.
|
|
14
|
+
*/
|
|
15
|
+
interface AppliedColor {
|
|
16
|
+
readonly variableId: string;
|
|
17
|
+
readonly color: Color;
|
|
18
|
+
/** The mode set for the layer, or null, in each collection the variable resolves through. */
|
|
19
|
+
readonly modes: Readonly<Record<string, string | null>>;
|
|
20
|
+
}
|
|
21
|
+
/** Whether a layer has a paint whose color is bound to a variable. */
|
|
22
|
+
declare function hasBoundPaints(node: SceneNode): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Whether a binding's paint holds a color this graph applied or recorded for it. A layer without
|
|
25
|
+
* an entry, such as one just imported or built with its bindings, has a stored color nothing
|
|
26
|
+
* vouches for: readers show its variable's color until a write or refresh records one.
|
|
27
|
+
*/
|
|
28
|
+
declare function paintColorApplied(node: SceneNode, field: string): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* A layer copied from another keeps the colors its bindings last applied there, so the copy's
|
|
31
|
+
* paints, edits included, stay as they are until their variables' colors for it change.
|
|
32
|
+
*/
|
|
33
|
+
declare function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void;
|
|
34
|
+
/** The colors copied layers' bindings last applied, by layer id, as a copy captured them. */
|
|
35
|
+
type AppliedBoundPaints = ReadonlyMap<string, ReadonlyMap<string, AppliedColor>>;
|
|
36
|
+
/**
|
|
37
|
+
* The colors the bindings of the layers `nodeIds` names last applied, captured when they are
|
|
38
|
+
* copied, so a paste counts what the copied paints were applied from, not what the source has now.
|
|
39
|
+
*/
|
|
40
|
+
declare function captureAppliedBoundPaints(graph: SceneGraph, nodeIds: Iterable<string>): AppliedBoundPaints;
|
|
41
|
+
/**
|
|
42
|
+
* Pasted layers keep their paints, edits included, as Figma's copies do. One copied in this
|
|
43
|
+
* document takes the colors its source's bindings had last applied when it was copied, so a paste
|
|
44
|
+
* into another mode refreshes them as a move there would; one from elsewhere counts the colors
|
|
45
|
+
* its variables give it now as applied, as a loaded document does. `copies` maps each copied
|
|
46
|
+
* layer's id to its copy's.
|
|
47
|
+
*/
|
|
48
|
+
declare function keepPastedBoundPaints(graph: SceneGraph, copies: ReadonlyMap<string, string>, applied: AppliedBoundPaints): void;
|
|
49
|
+
type PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>;
|
|
50
|
+
/**
|
|
51
|
+
* Whether `node` is an instance, or a layer an instance copies from its component, with a paint
|
|
52
|
+
* list neither it nor an instance around it overrides: such paints follow the component, and
|
|
53
|
+
* refreshing them is no edit.
|
|
54
|
+
*/
|
|
55
|
+
declare function copiesComponentPaints(graph: SceneGraph, node: SceneNode, list: PaintBindingList): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as
|
|
58
|
+
* opacity, once that color differs from the one last applied, discarding edits made since, as
|
|
59
|
+
* Figma refreshes them when a variable's value or a layer's mode changes.
|
|
60
|
+
*/
|
|
61
|
+
declare function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void;
|
|
62
|
+
/** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */
|
|
63
|
+
declare function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field: string): void;
|
|
64
|
+
/**
|
|
65
|
+
* Gives every bound paint in this subtree its variable's color where it sits. A new instance
|
|
66
|
+
* or a layer an instance sync adds is a copy, which shows its variables in its own context rather
|
|
67
|
+
* than whatever its component's paints hold.
|
|
68
|
+
*/
|
|
69
|
+
declare function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void;
|
|
70
|
+
/** Refreshes the bound paints in this subtree whose variables now give their layers another color. */
|
|
71
|
+
declare function refreshBoundPaintsIn(graph: SceneGraph, rootId: string): void;
|
|
72
|
+
/**
|
|
73
|
+
* Records, for the paint lists a write sets, the colors their variables give the layer now, so a
|
|
74
|
+
* paint written with another color or opacity keeps it until that color changes.
|
|
75
|
+
*/
|
|
76
|
+
declare function recordWrittenBoundPaints(graph: SceneGraph, node: SceneNode, written: PaintLists): void;
|
|
77
|
+
/**
|
|
78
|
+
* The fills and strokes a layer shows: its stored paints, except a bound paint no applied color
|
|
79
|
+
* vouches for takes its variable's color for the layer, alpha as opacity, as Figma shows a layer
|
|
80
|
+
* whose saved cache is stale on import. Nothing is written or recorded; a copy pasted unlinked
|
|
81
|
+
* keeps the colors it showed.
|
|
82
|
+
*/
|
|
83
|
+
declare function shownPaintBindings(graph: SceneGraph, node: SceneNode): Pick<Partial<SceneNode>, 'fills' | 'strokes'>;
|
|
84
|
+
/**
|
|
85
|
+
* Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose
|
|
86
|
+
* mode set the move changed, as Figma does when a layer enters a frame or page that sets another
|
|
87
|
+
* mode, or one where none was. A move between parents setting the same modes, or of layers that
|
|
88
|
+
* bind no paint, costs no more than comparing those modes.
|
|
89
|
+
*/
|
|
90
|
+
declare function refreshMovedBoundPaints(graph: SceneGraph, nodeId: string, previousParentId: string | null | undefined): void;
|
|
91
|
+
/**
|
|
92
|
+
* A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit
|
|
93
|
+
* to its paint's color or opacity does not reach them. An instance sync writing a copy's paints
|
|
94
|
+
* keeps the color and opacity of each one still bound to the same variable, and gives it the
|
|
95
|
+
* variable's color for the copy once that differs from the one last applied, or when it binds
|
|
96
|
+
* anew. `updates` holds what the sync writes, whose paints this sets in place.
|
|
97
|
+
*/
|
|
98
|
+
declare function keepCopyBoundPaints(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void;
|
|
99
|
+
//#endregion
|
|
100
|
+
export { AppliedBoundPaints, AppliedColor, applyBoundPaintColor, applyBoundPaintColorsIn, captureAppliedBoundPaints, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, keepPastedBoundPaints, paintColorApplied, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints, shownPaintBindings };
|
|
101
|
+
//# sourceMappingURL=paints.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paints.d.ts","names":[],"sources":["../../src/variables/paints.ts"],"mappings":";;;;;;;;;;;;;;UAoBiB;WACN;WACA,OAAO;;WAEP,OAAO,SAAS;;;iBAyBX,eAAe,MAAM;;;;;;iBAUrB,kBAAkB,MAAM,WAAW;;;;;iBAqDnC,uBAAuB,QAAQ,WAAW,MAAM;;KAMpD,qBAAqB,oBAAoB,oBAAoB;;;;;iBAMzD,0BACd,OAAO,YACP,SAAS,mBACR;;;;;;;;iBAiBa,sBACd,OAAO,YACP,QAAQ,6BACR,SAAS;KAwCN,aAAa,KAAK,QAAQ;;;;;;iBAOf,sBACd,OAAO,YACP,MAAM,WACN,MAAM;;;;;;iBA6DQ,mBAAmB,OAAO,YAAY,MAAM;;iBAK5C,qBAAqB,OAAO,YAAY,MAAM,WAAW;;;;;;iBASzD,wBAAwB,OAAO,YAAY;;iBAM3C,qBAAqB,OAAO,YAAY;;;;;iBAQxC,yBACd,OAAO,YACP,MAAM,WACN,SAAS;;;;;;;iBAeK,mBACd,OAAO,YACP,MAAM,YACL,KAAK,QAAQ;;;;;;;iBAwBA,wBACd,OAAO,YACP,gBACA;;;;;;;;iBAgDc,oBACd,OAAO,YACP,QAAQ,WACR,SAAS,QAAQ"}
|