@open-pencil/scene-graph 0.17.0-beta.2 → 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/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/checkpoint.d.ts +3 -3
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +53 -23
- package/dist/checkpoint.js.map +1 -1
- 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 +9 -1
- package/dist/components/properties.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 +8 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +3 -2
- package/dist/css/index.js +3 -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/figjam/palette.d.ts +1 -1
- package/dist/figjam/palette.js +1 -1
- package/dist/figjam/palette.js.map +1 -1
- package/dist/geometry.d.ts +8 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +19 -8
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +48 -12
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +110 -24
- 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/hit-test.d.ts +5 -0
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +18 -12
- 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/index.d.ts.map +1 -1
- package/dist/instances/index.js +3 -0
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/placement.js +55 -12
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap.js +3 -0
- package/dist/instances/swap.js.map +1 -1
- package/dist/instances/sync.js +34 -13
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -0
- 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/plugin-data/fields.d.ts +2 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +2 -0
- 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 +37 -14
- 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 +3 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +7 -1
- 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/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/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 +7 -14
- 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 +47 -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 +17 -14
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +30 -72
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +1 -1
- package/dist/variables/fields.js +1 -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 +6 -6
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +40 -24
- 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 +8 -1
- package/src/asset-keys.ts +20 -0
- package/src/checkpoint.ts +87 -32
- package/src/components/properties.ts +9 -0
- package/src/constants.ts +17 -0
- package/src/constraints.ts +60 -0
- package/src/coordinate.ts +220 -0
- package/src/copy.ts +9 -4
- package/src/css/index.ts +17 -1
- package/src/css/text.ts +85 -17
- package/src/css/transform.ts +266 -0
- package/src/css/values.ts +8 -0
- package/src/figjam/palette.ts +1 -1
- package/src/geometry.ts +20 -9
- package/src/graph.ts +128 -42
- package/src/group-bounds.ts +84 -17
- package/src/hit-test.ts +30 -14
- package/src/images.ts +71 -3
- package/src/instances/index.ts +4 -0
- package/src/instances/placement.ts +63 -10
- package/src/instances/swap.ts +5 -0
- package/src/instances/sync.ts +48 -21
- package/src/node-defaults.ts +1 -0
- package/src/order-keys.ts +6 -0
- package/src/plugin-data/fields.ts +6 -0
- 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 +8 -0
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +4 -1
- package/src/text/direction.ts +5 -0
- 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 +10 -26
- 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 +55 -1
- package/src/undo.ts +48 -10
- package/src/variables/bindings.ts +44 -91
- package/src/variables/fields.ts +1 -1
- package/src/variables/formula/index.ts +3 -1
- package/src/variables/index.ts +56 -38
- 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
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import { SceneGraph } from "../../graph.js";
|
|
2
|
+
import { VariableLiteral, VariableValue } from "../../types.js";
|
|
1
3
|
import { FormulaFold, MAX_FORMULA_DEPTH, VariableFormulaError, foldFormula, isFormulaOperation, isFormulaReference } from "./tree.js";
|
|
2
4
|
import { FormulaValueType, formulaArity, formulaSymbol } from "./signatures.js";
|
|
3
5
|
import { PrintFormulaOptions, printFormula } from "./print.js";
|
|
4
6
|
import { FormulaToken, FormulaTokenKind, formulaTokenName, formulaTokens, readFormulaToken } from "./tokens.js";
|
|
5
7
|
import { evaluateVariableFormula } from "./evaluate.js";
|
|
6
8
|
import { formulaValueType, inferFormulaType } from "./check.js";
|
|
7
|
-
import { SceneGraph } from "../../graph.js";
|
|
8
|
-
import { VariableLiteral, VariableValue } from "../../types.js";
|
|
9
9
|
//#region src/variables/formula/index.d.ts
|
|
10
10
|
/** Qualified names disambiguate references; the stored formula uses stable IDs. */
|
|
11
11
|
declare function variableFormulaReference(graph: SceneGraph, id: string): string;
|
|
@@ -68,7 +68,7 @@ function parseVariableFormula(graph, variableId, modeId, source) {
|
|
|
68
68
|
} : node;
|
|
69
69
|
return {
|
|
70
70
|
value,
|
|
71
|
-
resolved: resolveVariableInModes(graph, variableId, (collectionId) => collectionId === variable.collectionId ? modeId : graph.
|
|
71
|
+
resolved: resolveVariableInModes(graph, variableId, (collectionId) => collectionId === variable.collectionId ? modeId : graph.variableCollections.get(collectionId)?.defaultModeId ?? "", /* @__PURE__ */ new Set(), /* @__PURE__ */ new Map(), /* @__PURE__ */ new Map([[variableId, value]]))
|
|
72
72
|
};
|
|
73
73
|
}
|
|
74
74
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../src/variables/formula/index.ts"],"sourcesContent":["import type { SceneGraph } from '#scene-graph/graph'\nimport type { VariableLiteral, VariableValue } from '#scene-graph/types'\n\nimport { resolveVariableInModes } from '../resolution'\nimport { parseFormulaSyntax, type FormulaReference } from './parse'\nimport { printFormula, type PrintFormulaOptions } from './print'\nimport type { FormulaValueType } from './signatures'\nimport { isFormulaOperation, VariableFormulaError } from './tree'\n\nexport { printFormula, type PrintFormulaOptions } from './print'\nexport {\n formulaTokenName,\n formulaTokens,\n readFormulaToken,\n type FormulaToken,\n type FormulaTokenKind\n} from './tokens'\n\nexport { evaluateVariableFormula } from './evaluate'\nexport { formulaValueType, inferFormulaType } from './check'\nexport { formulaArity, formulaSymbol, type FormulaValueType } from './signatures'\nexport {\n foldFormula,\n isFormulaOperation,\n isFormulaReference,\n MAX_FORMULA_DEPTH,\n VariableFormulaError,\n type FormulaFold\n} from './tree'\n\nfunction qualifiedName(graph: SceneGraph, id: string) {\n const variable = graph.variables.get(id)\n if (!variable) return undefined\n const collection = graph.variableCollections.get(variable.collectionId)\n return `${collection?.name ?? variable.collectionId}::${variable.name}`\n}\n\n/** Qualified names disambiguate references; the stored formula uses stable IDs. */\nexport function variableFormulaReference(graph: SceneGraph, id: string): string {\n const variable = graph.variables.get(id)\n if (!variable) return `{#${id}}`\n const unique = (name: string, of: (id: string) => string | undefined) =>\n [...graph.variables.keys()].filter((other) => of(other) === name).length === 1\n const plain = variable.name\n if (\n !/[{}]/.test(plain) &&\n plain.trim() === plain &&\n unique(plain, (other) => graph.variables.get(other)?.name)\n )\n return `{${plain}}`\n const qualified = qualifiedName(graph, id) ?? ''\n return /[{}]/.test(qualified) || !unique(qualified, (other) => qualifiedName(graph, other))\n ? `{#${id}}`\n : `{${qualified}}`\n}\n\n/** The formula's source text, with `{name}` for each reference, as the clipboard and scripts write it; text is quoted. */\nexport function formatVariableFormula(\n graph: SceneGraph,\n value: VariableValue,\n options?: PrintFormulaOptions\n): string {\n const reference = (id: string) => variableFormulaReference(graph, id)\n const typed = { type: (id: string) => graph.variables.get(id)?.type, ...options }\n if (typeof value !== 'object' || !('expression' in value))\n return printFormula(value, reference, typed)\n return value.expression === 'FORMULA' ? printFormula(value.value, reference, typed) : ''\n}\n\n/**\n * Find the variable a reference names: `#id`, `Collection::name`, or a name, preferring the\n * edited variable's own collection when a name appears in several.\n */\nfunction resolveReference(graph: SceneGraph, collectionId: string, name: string): FormulaReference {\n const candidates = [...graph.variables.values()].filter(\n (candidate) =>\n name === `#${candidate.id}` ||\n name === qualifiedName(graph, candidate.id) ||\n name === candidate.name\n )\n const local = candidates.filter((candidate) => candidate.collectionId === collectionId)\n const matches = local.length === 1 ? local : candidates\n const match = matches.at(0)\n if (!match) throw new VariableFormulaError(`No variable named ${name}`)\n if (matches.length !== 1)\n throw new VariableFormulaError(`More than one variable is named ${name}`)\n return { id: match.id, type: match.type }\n}\n\nconst TYPE_NAMES: Record<FormulaValueType, string> = {\n FLOAT: 'a number',\n BOOLEAN: 'true or false',\n STRING: 'text',\n COLOR: 'a color'\n}\n\n/** Validate a draft without changing the document. Formulas operate on stored units. */\nexport function parseVariableFormula(\n graph: SceneGraph,\n variableId: string,\n modeId: string,\n source: string\n): { value: VariableValue; resolved: VariableLiteral } {\n const variable = graph.variables.get(variableId)\n if (!variable) throw new VariableFormulaError('Select a variable')\n const collection = graph.variableCollections.get(variable.collectionId)\n if (!collection?.modes.some((mode) => mode.modeId === modeId))\n throw new VariableFormulaError('Unknown variable mode')\n const { node, type } = parseFormulaSyntax(source, (name) =>\n resolveReference(graph, variable.collectionId, name)\n )\n if (type !== variable.type)\n throw new VariableFormulaError(\n `This gives ${TYPE_NAMES[type]}, but ${variable.name} holds ${TYPE_NAMES[variable.type]}`\n )\n const value: VariableValue = isFormulaOperation(node)\n ? { expression: 'FORMULA', type: variable.type, value: node }\n : node\n const resolved = resolveVariableInModes(\n graph,\n variableId,\n (collectionId) =>\n collectionId === variable.collectionId
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/variables/formula/index.ts"],"sourcesContent":["import type { SceneGraph } from '#scene-graph/graph'\nimport type { VariableLiteral, VariableValue } from '#scene-graph/types'\n\nimport { resolveVariableInModes } from '../resolution'\nimport { parseFormulaSyntax, type FormulaReference } from './parse'\nimport { printFormula, type PrintFormulaOptions } from './print'\nimport type { FormulaValueType } from './signatures'\nimport { isFormulaOperation, VariableFormulaError } from './tree'\n\nexport { printFormula, type PrintFormulaOptions } from './print'\nexport {\n formulaTokenName,\n formulaTokens,\n readFormulaToken,\n type FormulaToken,\n type FormulaTokenKind\n} from './tokens'\n\nexport { evaluateVariableFormula } from './evaluate'\nexport { formulaValueType, inferFormulaType } from './check'\nexport { formulaArity, formulaSymbol, type FormulaValueType } from './signatures'\nexport {\n foldFormula,\n isFormulaOperation,\n isFormulaReference,\n MAX_FORMULA_DEPTH,\n VariableFormulaError,\n type FormulaFold\n} from './tree'\n\nfunction qualifiedName(graph: SceneGraph, id: string) {\n const variable = graph.variables.get(id)\n if (!variable) return undefined\n const collection = graph.variableCollections.get(variable.collectionId)\n return `${collection?.name ?? variable.collectionId}::${variable.name}`\n}\n\n/** Qualified names disambiguate references; the stored formula uses stable IDs. */\nexport function variableFormulaReference(graph: SceneGraph, id: string): string {\n const variable = graph.variables.get(id)\n if (!variable) return `{#${id}}`\n const unique = (name: string, of: (id: string) => string | undefined) =>\n [...graph.variables.keys()].filter((other) => of(other) === name).length === 1\n const plain = variable.name\n if (\n !/[{}]/.test(plain) &&\n plain.trim() === plain &&\n unique(plain, (other) => graph.variables.get(other)?.name)\n )\n return `{${plain}}`\n const qualified = qualifiedName(graph, id) ?? ''\n return /[{}]/.test(qualified) || !unique(qualified, (other) => qualifiedName(graph, other))\n ? `{#${id}}`\n : `{${qualified}}`\n}\n\n/** The formula's source text, with `{name}` for each reference, as the clipboard and scripts write it; text is quoted. */\nexport function formatVariableFormula(\n graph: SceneGraph,\n value: VariableValue,\n options?: PrintFormulaOptions\n): string {\n const reference = (id: string) => variableFormulaReference(graph, id)\n const typed = { type: (id: string) => graph.variables.get(id)?.type, ...options }\n if (typeof value !== 'object' || !('expression' in value))\n return printFormula(value, reference, typed)\n return value.expression === 'FORMULA' ? printFormula(value.value, reference, typed) : ''\n}\n\n/**\n * Find the variable a reference names: `#id`, `Collection::name`, or a name, preferring the\n * edited variable's own collection when a name appears in several.\n */\nfunction resolveReference(graph: SceneGraph, collectionId: string, name: string): FormulaReference {\n const candidates = [...graph.variables.values()].filter(\n (candidate) =>\n name === `#${candidate.id}` ||\n name === qualifiedName(graph, candidate.id) ||\n name === candidate.name\n )\n const local = candidates.filter((candidate) => candidate.collectionId === collectionId)\n const matches = local.length === 1 ? local : candidates\n const match = matches.at(0)\n if (!match) throw new VariableFormulaError(`No variable named ${name}`)\n if (matches.length !== 1)\n throw new VariableFormulaError(`More than one variable is named ${name}`)\n return { id: match.id, type: match.type }\n}\n\nconst TYPE_NAMES: Record<FormulaValueType, string> = {\n FLOAT: 'a number',\n BOOLEAN: 'true or false',\n STRING: 'text',\n COLOR: 'a color'\n}\n\n/** Validate a draft without changing the document. Formulas operate on stored units. */\nexport function parseVariableFormula(\n graph: SceneGraph,\n variableId: string,\n modeId: string,\n source: string\n): { value: VariableValue; resolved: VariableLiteral } {\n const variable = graph.variables.get(variableId)\n if (!variable) throw new VariableFormulaError('Select a variable')\n const collection = graph.variableCollections.get(variable.collectionId)\n if (!collection?.modes.some((mode) => mode.modeId === modeId))\n throw new VariableFormulaError('Unknown variable mode')\n const { node, type } = parseFormulaSyntax(source, (name) =>\n resolveReference(graph, variable.collectionId, name)\n )\n if (type !== variable.type)\n throw new VariableFormulaError(\n `This gives ${TYPE_NAMES[type]}, but ${variable.name} holds ${TYPE_NAMES[variable.type]}`\n )\n const value: VariableValue = isFormulaOperation(node)\n ? { expression: 'FORMULA', type: variable.type, value: node }\n : node\n const resolved = resolveVariableInModes(\n graph,\n variableId,\n (collectionId) =>\n collectionId === variable.collectionId\n ? modeId\n : (graph.variableCollections.get(collectionId)?.defaultModeId ?? ''),\n new Set(),\n new Map(),\n new Map([[variableId, value]])\n )\n return { value, resolved }\n}\n"],"mappings":";;;;;;;;;AA8BA,SAAS,cAAc,OAAmB,IAAY;CACpD,MAAM,WAAW,MAAM,UAAU,IAAI,E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AE;AAC3B"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { Variable, VariableCollection, VariableCollectionMode, VariableType, VariableValue } from "../types.js";
|
|
1
3
|
import { FormulaFold, MAX_FORMULA_DEPTH, VariableFormulaError, foldFormula, isFormulaOperation, isFormulaReference } from "./formula/tree.js";
|
|
2
|
-
import { NodeVariableResolver,
|
|
4
|
+
import { NodeVariableResolver, VariableResolutionError, collectionsResolving, createBatchVariableResolver, getNodeVariableModeId, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, variableValueInMode } from "./resolution.js";
|
|
3
5
|
import { EFFECT_BINDING_PROPERTIES, EffectBindingProperty, PaintBindingList, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, effectBindableProperties, effectBindingField, effectBindingTarget, effectBindingVariableType, isNumericVariableBindingField, paintBindingTarget } from "./fields.js";
|
|
4
|
-
import { BindingScope, BoundLayerList, assignVariableBindingUnits, boundNumberValue, collectionsBoundIn, layersUsingVariables, listBindingsAfterEdit, reconcileVariableBindingUpdates, reconcileVariableBindings, resolvedEffectBindings, resolvedNumericBindings,
|
|
6
|
+
import { BindingScope, BoundLayerList, assignVariableBindingUnits, boundNumberValue, collectionsBoundIn, layersResolvingCollections, layersUsingVariables, listBindingsAfterEdit, reconcileVariableBindingUpdates, reconcileVariableBindings, resolvedEffectBindings, resolvedNumericBindings, resolvedRunBindings, resolvedValueBindings, variableBindingOwner, variablesResolvingThrough } from "./bindings.js";
|
|
5
7
|
import { independentCornerChanges } from "./corners.js";
|
|
6
8
|
import { FormulaValueType, formulaArity, formulaSymbol } from "./formula/signatures.js";
|
|
7
9
|
import { PrintFormulaOptions, printFormula } from "./formula/print.js";
|
|
@@ -10,12 +12,11 @@ import { evaluateVariableFormula } from "./formula/evaluate.js";
|
|
|
10
12
|
import { formulaValueType, inferFormulaType } from "./formula/check.js";
|
|
11
13
|
import { formatVariableFormula, parseVariableFormula, variableFormulaReference } from "./formula/index.js";
|
|
12
14
|
import { variableFormulaSchema } from "./formula/schema.js";
|
|
15
|
+
import { applyBoundPaintColor, applyBoundPaintColorsIn, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints } from "./paints.js";
|
|
13
16
|
import { variableAliasSchema, variableCollectionSchema, variableColorExpressionSchema, variableSchema } from "./schema.js";
|
|
14
17
|
import { variableText, variableTruthy } from "./text.js";
|
|
15
18
|
import { ROOT_FONT_SIZE_PX, tokenExpressionForValue, tokenNumberFromUnit, tokenNumberInUnit, updateTokenExpression } from "./token.js";
|
|
16
19
|
import { isUnsupportedVariableValue, mapVariableValueReferences, resolveVariableValue, variableValueReferences } from "./value.js";
|
|
17
|
-
import { SceneGraph } from "../graph.js";
|
|
18
|
-
import { Variable, VariableCollection, VariableCollectionMode, VariableType, VariableValue } from "../types.js";
|
|
19
20
|
//#region src/variables/index.d.ts
|
|
20
21
|
declare function addVariable(graph: SceneGraph, variable: Variable): void;
|
|
21
22
|
declare function removeVariable(graph: SceneGraph, id: string): void;
|
|
@@ -23,7 +24,6 @@ declare function addCollection(graph: SceneGraph, collection: VariableCollection
|
|
|
23
24
|
declare function createVariable(graph: SceneGraph, generateId: () => string, name: string, type: VariableType, collectionId: string, value?: VariableValue): Variable;
|
|
24
25
|
declare function createCollection(graph: SceneGraph, generateId: () => string, name: string): VariableCollection;
|
|
25
26
|
declare function removeCollection(graph: SceneGraph, id: string): void;
|
|
26
|
-
declare function setActiveMode(graph: SceneGraph, collectionId: string, modeId: string): void;
|
|
27
27
|
declare function addMode(graph: SceneGraph, collectionId: string, modeId: string, name: string, sourceMode?: string): void;
|
|
28
28
|
/** Add a mode with a new ID from `generateId`; undo and redo replay it with `addMode`. */
|
|
29
29
|
declare function createMode(graph: SceneGraph, generateId: () => string, collectionId: string, name: string, sourceMode?: string): string | undefined;
|
|
@@ -37,5 +37,5 @@ declare function getVariablesByType(graph: SceneGraph, type: VariableType): Vari
|
|
|
37
37
|
declare function bindVariable(graph: SceneGraph, nodeId: string, field: string, variableId: string): void;
|
|
38
38
|
declare function unbindVariable(graph: SceneGraph, nodeId: string, field: string): void;
|
|
39
39
|
//#endregion
|
|
40
|
-
export { BindingScope, BoundLayerList, EFFECT_BINDING_PROPERTIES, type EffectBindingProperty, type FormulaFold, type FormulaToken, type FormulaTokenKind, type FormulaValueType, MAX_FORMULA_DEPTH, NodeVariableResolver, type PaintBindingList, type PrintFormulaOptions, ROOT_FONT_SIZE_PX, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, VariableFormulaError,
|
|
40
|
+
export { BindingScope, BoundLayerList, EFFECT_BINDING_PROPERTIES, type EffectBindingProperty, type FormulaFold, type FormulaToken, type FormulaTokenKind, type FormulaValueType, MAX_FORMULA_DEPTH, NodeVariableResolver, type PaintBindingList, type PrintFormulaOptions, ROOT_FONT_SIZE_PX, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, VariableFormulaError, VariableResolutionError, addCollection, addMode, addVariable, applyBoundPaintColor, applyBoundPaintColorsIn, assignVariableBindingUnits, bindVariable, boundNumberValue, collectionsBoundIn, collectionsResolving, copiesComponentPaints, copyAppliedBoundPaints, createBatchVariableResolver, createCollection, createMode, createVariable, effectBindableProperties, effectBindingField, effectBindingTarget, effectBindingVariableType, evaluateVariableFormula, foldFormula, formatVariableFormula, formulaArity, formulaSymbol, formulaTokenName, formulaTokens, formulaValueType, getNodeVariableModeId, getVariablesByType, getVariablesForCollection, hasBoundPaints, independentCornerChanges, inferFormulaType, isFormulaOperation, isFormulaReference, isNumericVariableBindingField, isUnsupportedVariableValue, keepCopyBoundPaints, layersResolvingCollections, layersUsingVariables, listBindingsAfterEdit, mapVariableValueReferences, modesDefaultFirst, paintBindingTarget, parseVariableFormula, printFormula, readFormulaToken, reconcileVariableBindingUpdates, reconcileVariableBindings, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints, removeCollection, removeMode, removeVariable, renameMode, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, resolveVariableValue, resolvedEffectBindings, resolvedNumericBindings, resolvedRunBindings, resolvedValueBindings, setDefaultMode, tokenExpressionForValue, tokenNumberFromUnit, tokenNumberInUnit, unbindVariable, updateTokenExpression, variableAliasSchema, variableBindingOwner, variableCollectionSchema, variableColorExpressionSchema, variableFormulaReference, variableFormulaSchema, variableSchema, variableText, variableTruthy, variableValueInMode, variableValueReferences, variablesResolvingThrough };
|
|
41
41
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/variables/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/variables/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;iBAqCgB,YAAY,OAAO,YAAY,UAAU;iBAUzC,eAAe,OAAO,YAAY;iBA8BlC,cAAc,OAAO,YAAY,YAAY;iBAa7C,eACd,OAAO,YACP,0BACA,cACA,MAAM,cACN,sBACA,QAAQ,gBACP;iBAsBa,iBACd,OAAO,YACP,0BACA,eACC;iBAca,iBAAiB,OAAO,YAAY;iBAWpC,QACd,OAAO,YACP,sBACA,gBACA,cACA;;iBAkBc,WACd,OAAO,YACP,0BACA,sBACA,cACA;iBAQc,WAAW,OAAO,YAAY,sBAAsB;iBAgBpD,WACd,OAAO,YACP,sBACA,gBACA;iBAUc,eAAe,OAAO,YAAY,sBAAsB;;iBAkBxD,kBAAkB,YAAY,qBAAqB;iBAKnD,0BAA0B,OAAO,YAAY,uBAAuB;iBAQpE,mBAAmB,OAAO,YAAY,MAAM,eAAe;iBA+C3D,aACd,OAAO,YACP,gBACA,eACA;iBAyDc,eAAe,OAAO,YAAY,gBAAgB"}
|
package/dist/variables/index.js
CHANGED
|
@@ -2,16 +2,17 @@ import { BOOLEAN_BINDING_FIELDS, EFFECT_BINDING_PROPERTIES, STRING_BINDING_FIELD
|
|
|
2
2
|
import { BLACK } from "../constants.js";
|
|
3
3
|
import { bindingOverrideField, setInstanceOverride } from "../instances/overrides.js";
|
|
4
4
|
import { findInstanceAncestor } from "../instances/main-component.js";
|
|
5
|
-
import { variableText, variableTruthy } from "./text.js";
|
|
6
5
|
import { MAX_FORMULA_DEPTH, VariableFormulaError, foldFormula, isFormulaOperation, isFormulaReference } from "./formula/tree.js";
|
|
6
|
+
import { variableText, variableTruthy } from "./text.js";
|
|
7
7
|
import { formulaArity, formulaSymbol } from "./formula/signatures.js";
|
|
8
8
|
import { formulaValueType, inferFormulaType } from "./formula/check.js";
|
|
9
9
|
import { evaluateVariableFormula } from "./formula/evaluate.js";
|
|
10
10
|
import { variableAliasSchema, variableCollectionSchema, variableColorExpressionSchema, variableSchema } from "./schema.js";
|
|
11
11
|
import { isUnsupportedVariableValue, mapVariableValueReferences, resolveVariableValue, variableValueReferences } from "./value.js";
|
|
12
|
-
import {
|
|
12
|
+
import { VariableResolutionError, collectionsResolving, createBatchVariableResolver, getNodeVariableModeId, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, variableValueInMode } from "./resolution.js";
|
|
13
|
+
import { applyBoundPaintColor, applyBoundPaintColorsIn, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints } from "./paints.js";
|
|
14
|
+
import { assignVariableBindingUnits, boundNumberValue, collectionsBoundIn, layersResolvingCollections, layersUsingVariables, listBindingsAfterEdit, reconcileVariableBindingUpdates, reconcileVariableBindings, resolvedEffectBindings, resolvedNumericBindings, resolvedRunBindings, resolvedValueBindings, variableBindingOwner, variablesResolvingThrough } from "./bindings.js";
|
|
13
15
|
import { siblingOrderKeys } from "../order-keys.js";
|
|
14
|
-
import { VariableResolutionError, createBatchVariableResolver, getActiveModeId, getNodeVariableModeId, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, variableValueInMode } from "./resolution.js";
|
|
15
16
|
import { independentCornerChanges } from "./corners.js";
|
|
16
17
|
import { formulaTokenName, formulaTokens, readFormulaToken } from "./formula/tokens.js";
|
|
17
18
|
import { printFormula } from "./formula/print.js";
|
|
@@ -22,16 +23,24 @@ import { omit, omitBy } from "es-toolkit/object";
|
|
|
22
23
|
import { compact, partition, uniq } from "es-toolkit/array";
|
|
23
24
|
//#region src/variables/index.ts
|
|
24
25
|
function addVariable(graph, variable) {
|
|
26
|
+
graph.willChangeVariable(variable.id);
|
|
25
27
|
graph.variables.set(variable.id, variable);
|
|
26
28
|
const collection = graph.variableCollections.get(variable.collectionId);
|
|
27
|
-
if (collection && !collection.variableIds.includes(variable.id))
|
|
29
|
+
if (collection && !collection.variableIds.includes(variable.id)) {
|
|
30
|
+
graph.willChangeCollection(collection.id);
|
|
31
|
+
collection.variableIds.push(variable.id);
|
|
32
|
+
}
|
|
28
33
|
}
|
|
29
34
|
function removeVariable(graph, id) {
|
|
30
35
|
const variable = graph.variables.get(id);
|
|
31
36
|
if (!variable) return;
|
|
37
|
+
graph.willChangeVariable(id);
|
|
32
38
|
graph.variables.delete(id);
|
|
33
39
|
const collection = graph.variableCollections.get(variable.collectionId);
|
|
34
|
-
if (collection)
|
|
40
|
+
if (collection) {
|
|
41
|
+
graph.willChangeCollection(collection.id);
|
|
42
|
+
collection.variableIds = collection.variableIds.filter((vid) => vid !== id);
|
|
43
|
+
}
|
|
35
44
|
for (const node of graph.nodes.values()) {
|
|
36
45
|
if (!Object.values(node.boundVariables).includes(id)) continue;
|
|
37
46
|
graph.willChangeNode(node);
|
|
@@ -45,8 +54,8 @@ function removeVariable(graph, id) {
|
|
|
45
54
|
}
|
|
46
55
|
}
|
|
47
56
|
function addCollection(graph, collection) {
|
|
57
|
+
graph.willChangeCollection(collection.id);
|
|
48
58
|
graph.variableCollections.set(collection.id, collection);
|
|
49
|
-
if (!graph.activeMode.has(collection.id)) graph.activeMode.set(collection.id, collection.defaultModeId);
|
|
50
59
|
}
|
|
51
60
|
function defaultVariableValue(type, value) {
|
|
52
61
|
if (value !== void 0) return value;
|
|
@@ -93,15 +102,13 @@ function createCollection(graph, generateId, name) {
|
|
|
93
102
|
function removeCollection(graph, id) {
|
|
94
103
|
const collection = graph.variableCollections.get(id);
|
|
95
104
|
if (collection) for (const varId of Array.from(collection.variableIds)) removeVariable(graph, varId);
|
|
105
|
+
graph.willChangeCollection(id);
|
|
96
106
|
graph.variableCollections.delete(id);
|
|
97
|
-
graph.activeMode.delete(id);
|
|
98
|
-
}
|
|
99
|
-
function setActiveMode(graph, collectionId, modeId) {
|
|
100
|
-
graph.activeMode.set(collectionId, modeId);
|
|
101
107
|
}
|
|
102
108
|
function addMode(graph, collectionId, modeId, name, sourceMode) {
|
|
103
109
|
const collection = graph.variableCollections.get(collectionId);
|
|
104
110
|
if (!collection) return;
|
|
111
|
+
graph.willChangeCollection(collectionId);
|
|
105
112
|
collection.modes.push({
|
|
106
113
|
modeId,
|
|
107
114
|
name
|
|
@@ -110,6 +117,7 @@ function addMode(graph, collectionId, modeId, name, sourceMode) {
|
|
|
110
117
|
for (const varId of collection.variableIds) {
|
|
111
118
|
const variable = graph.variables.get(varId);
|
|
112
119
|
if (!variable) continue;
|
|
120
|
+
graph.willChangeVariable(varId);
|
|
113
121
|
variable.valuesByMode[modeId] = structuredClone(variable.valuesByMode[sourceModeId] ?? Object.values(variable.valuesByMode)[0]);
|
|
114
122
|
}
|
|
115
123
|
}
|
|
@@ -123,24 +131,29 @@ function createMode(graph, generateId, collectionId, name, sourceMode) {
|
|
|
123
131
|
function removeMode(graph, collectionId, modeId) {
|
|
124
132
|
const collection = graph.variableCollections.get(collectionId);
|
|
125
133
|
if (!collection || collection.modes.length <= 1) return;
|
|
134
|
+
graph.willChangeCollection(collectionId);
|
|
126
135
|
collection.modes = collection.modes.filter((m) => m.modeId !== modeId);
|
|
127
136
|
if (collection.defaultModeId === modeId) collection.defaultModeId = collection.modes[0].modeId;
|
|
128
137
|
for (const varId of collection.variableIds) {
|
|
129
138
|
const variable = graph.variables.get(varId);
|
|
130
|
-
if (variable)
|
|
139
|
+
if (!variable) continue;
|
|
140
|
+
graph.willChangeVariable(varId);
|
|
141
|
+
variable.valuesByMode = omit(variable.valuesByMode, [modeId]);
|
|
131
142
|
}
|
|
132
|
-
if (graph.activeMode.get(collectionId) === modeId) graph.activeMode.set(collectionId, collection.defaultModeId);
|
|
133
143
|
}
|
|
134
144
|
function renameMode(graph, collectionId, modeId, name) {
|
|
135
145
|
const collection = graph.variableCollections.get(collectionId);
|
|
136
146
|
if (!collection) return;
|
|
137
147
|
const mode = collection.modes.find((m) => m.modeId === modeId);
|
|
138
|
-
if (mode)
|
|
148
|
+
if (!mode) return;
|
|
149
|
+
graph.willChangeCollection(collectionId);
|
|
150
|
+
mode.name = name;
|
|
139
151
|
}
|
|
140
152
|
function setDefaultMode(graph, collectionId, modeId) {
|
|
141
153
|
const collection = graph.variableCollections.get(collectionId);
|
|
142
154
|
if (!collection) return;
|
|
143
155
|
if (!collection.modes.some((m) => m.modeId === modeId)) return;
|
|
156
|
+
graph.willChangeCollection(collectionId);
|
|
144
157
|
collection.defaultModeId = modeId;
|
|
145
158
|
collection.modes = withSortPositionsInOrder(modesDefaultFirst(collection));
|
|
146
159
|
}
|
|
@@ -172,21 +185,23 @@ function assertEffectBinding(effects, target, type, field) {
|
|
|
172
185
|
if (!effectBindableProperties(effect.type).includes(target.property)) throw new Error(`A ${effect.type} effect has no bindable ${target.property} ("${field}")`);
|
|
173
186
|
if (type !== effectBindingVariableType(target.property)) throw new Error(`Cannot bind ${type} variable to effect field "${field}"`);
|
|
174
187
|
}
|
|
188
|
+
/** Checks a paint binding and drops the list's dead top-level binding it replaces. */
|
|
189
|
+
function preparePaintBinding(graph, node, paint, variable, field) {
|
|
190
|
+
if (variable.type !== "COLOR") throw new Error(`Cannot bind ${variable.type} variable to color field "${field}"`);
|
|
191
|
+
const currentLength = node[paint.list].length;
|
|
192
|
+
if (paint.index >= currentLength) throw new Error(`Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`);
|
|
193
|
+
if (paint.list in node.boundVariables) {
|
|
194
|
+
graph.willChangeNode(node);
|
|
195
|
+
node.boundVariables = omit(node.boundVariables, [paint.list]);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
175
198
|
function bindVariable(graph, nodeId, field, variableId) {
|
|
176
199
|
const node = graph.nodes.get(nodeId);
|
|
177
200
|
if (!node) return;
|
|
178
201
|
const variable = graph.variables.get(variableId);
|
|
179
202
|
if (!variable) throw new Error(`Variable "${variableId}" not found`);
|
|
180
203
|
const paint = paintBindingTarget(field);
|
|
181
|
-
if (paint)
|
|
182
|
-
if (variable.type !== "COLOR") throw new Error(`Cannot bind ${variable.type} variable to color field "${field}"`);
|
|
183
|
-
const currentLength = node[paint.list].length;
|
|
184
|
-
if (paint.index >= currentLength) throw new Error(`Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`);
|
|
185
|
-
if (paint.list in node.boundVariables) {
|
|
186
|
-
graph.willChangeNode(node);
|
|
187
|
-
node.boundVariables = omit(node.boundVariables, [paint.list]);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
204
|
+
if (paint) preparePaintBinding(graph, node, paint, variable, field);
|
|
190
205
|
const effect = effectBindingTarget(field);
|
|
191
206
|
if (effect) assertEffectBinding(node.effects, effect, variable.type, field);
|
|
192
207
|
if (isNumericVariableBindingField(field) && variable.type !== "FLOAT") throw new Error(`Cannot bind ${variable.type} variable to scalar field "${field}"`);
|
|
@@ -205,6 +220,7 @@ function bindVariable(graph, nodeId, field, variableId) {
|
|
|
205
220
|
boundVariables: { ...node.boundVariables },
|
|
206
221
|
variableBindingScales: { ...node.variableBindingScales }
|
|
207
222
|
});
|
|
223
|
+
if (paint) applyBoundPaintColor(graph, node, field);
|
|
208
224
|
}
|
|
209
225
|
function unbindVariable(graph, nodeId, field) {
|
|
210
226
|
const node = graph.nodes.get(nodeId);
|
|
@@ -229,10 +245,10 @@ function markBoundVariablesOverrideOnInstance(graph, nodeId, field) {
|
|
|
229
245
|
for (const instance of instances) graph.willChangeNode(instance);
|
|
230
246
|
if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, "boundVariables");
|
|
231
247
|
setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, "boundVariables");
|
|
232
|
-
if (field) setInstanceOverride(
|
|
248
|
+
if (field) for (const instance of uniq(compact([nearest, owner]))) setInstanceOverride(instance.instanceOverrides, instance.id, nodeId, bindingOverrideField(field), node.boundVariables[field] ?? null);
|
|
233
249
|
for (const instance of instances) graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides });
|
|
234
250
|
}
|
|
235
251
|
//#endregion
|
|
236
|
-
export { EFFECT_BINDING_PROPERTIES, MAX_FORMULA_DEPTH, ROOT_FONT_SIZE_PX, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, VariableFormulaError, VariableResolutionError, addCollection, addMode, addVariable, assignVariableBindingUnits, bindVariable, boundNumberValue, collectionsBoundIn, createBatchVariableResolver, createCollection, createMode, createVariable, effectBindableProperties, effectBindingField, effectBindingTarget, effectBindingVariableType, evaluateVariableFormula, foldFormula, formatVariableFormula, formulaArity, formulaSymbol, formulaTokenName, formulaTokens, formulaValueType,
|
|
252
|
+
export { EFFECT_BINDING_PROPERTIES, MAX_FORMULA_DEPTH, ROOT_FONT_SIZE_PX, TEXT_CONTENT_VARIABLE_TYPES, VISIBILITY_VARIABLE_TYPES, VariableFormulaError, VariableResolutionError, addCollection, addMode, addVariable, applyBoundPaintColor, applyBoundPaintColorsIn, assignVariableBindingUnits, bindVariable, boundNumberValue, collectionsBoundIn, collectionsResolving, copiesComponentPaints, copyAppliedBoundPaints, createBatchVariableResolver, createCollection, createMode, createVariable, effectBindableProperties, effectBindingField, effectBindingTarget, effectBindingVariableType, evaluateVariableFormula, foldFormula, formatVariableFormula, formulaArity, formulaSymbol, formulaTokenName, formulaTokens, formulaValueType, getNodeVariableModeId, getVariablesByType, getVariablesForCollection, hasBoundPaints, independentCornerChanges, inferFormulaType, isFormulaOperation, isFormulaReference, isNumericVariableBindingField, isUnsupportedVariableValue, keepCopyBoundPaints, layersResolvingCollections, layersUsingVariables, listBindingsAfterEdit, mapVariableValueReferences, modesDefaultFirst, paintBindingTarget, parseVariableFormula, printFormula, readFormulaToken, reconcileVariableBindingUpdates, reconcileVariableBindings, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints, removeCollection, removeMode, removeVariable, renameMode, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveStringVariableForNode, resolveVariable, resolveVariableForNode, resolveVariableInModes, resolveVariableValue, resolvedEffectBindings, resolvedNumericBindings, resolvedRunBindings, resolvedValueBindings, setDefaultMode, tokenExpressionForValue, tokenNumberFromUnit, tokenNumberInUnit, unbindVariable, updateTokenExpression, variableAliasSchema, variableBindingOwner, variableCollectionSchema, variableColorExpressionSchema, variableFormulaReference, variableFormulaSchema, variableSchema, variableText, variableTruthy, variableValueInMode, variableValueReferences, variablesResolvingThrough };
|
|
237
253
|
|
|
238
254
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/variables/index.ts"],"sourcesContent":["import { compact, partition, 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 { bindingOverrideField, setInstanceOverride } from '../instances/overrides'\nimport { siblingOrderKeys } from '../order-keys'\nimport type {\n Effect,\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 effectBindableProperties,\n effectBindingTarget,\n effectBindingVariableType,\n type EffectBindingProperty,\n paintBindingTarget,\n STRING_BINDING_FIELDS,\n TEXT_CONTENT_VARIABLE_TYPES,\n VISIBILITY_VARIABLE_TYPES\n} from './fields'\n\nexport * from './resolution'\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 node.variableBindingScales = omitBy(\n node.variableBindingScales,\n (_, field) => !(field in node.boundVariables)\n )\n graph.emitter.emit('node:updated', node.id, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\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 = withSortPositionsInOrder(modesDefaultFirst(collection))\n}\n\nfunction withSortPositionsInOrder(modes: VariableCollectionMode[]): VariableCollectionMode[] {\n if (modes.every((mode) => !mode.sortPosition)) {\n return modes\n }\n const positions = siblingOrderKeys(modes.map((mode) => mode.sortPosition))\n return modes.map((mode, index) => ({ ...mode, sortPosition: positions[index] }))\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\n/** Checks that an effect binding names an effect the node has, a property it binds, and its type. */\nfunction assertEffectBinding(\n effects: readonly Effect[],\n target: { index: number; property: EffectBindingProperty },\n type: VariableType,\n field: string\n): void {\n const effect = effects.at(target.index)\n if (!effect) {\n throw new Error(`Index ${target.index} out of range for effects (length ${effects.length})`)\n }\n if (!effectBindableProperties(effect.type).includes(target.property)) {\n throw new Error(`A ${effect.type} effect has no bindable ${target.property} (\"${field}\")`)\n }\n if (type !== effectBindingVariableType(target.property)) {\n throw new Error(`Cannot bind ${type} variable to effect field \"${field}\"`)\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 graph.willChangeNode(node)\n node.boundVariables = omit(node.boundVariables, [paint.list])\n }\n }\n\n const effect = effectBindingTarget(field)\n if (effect) assertEffectBinding(node.effects, effect, variable.type, field)\n\n if (isNumericVariableBindingField(field) && variable.type !== 'FLOAT') {\n throw new Error(`Cannot bind ${variable.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(variable.type)) {\n throw new Error(`Cannot bind ${variable.type} variable to string field \"${field}\"`)\n }\n\n if (\n BOOLEAN_BINDING_FIELDS.has(field) &&\n !VISIBILITY_VARIABLE_TYPES.some((type) => type === variable.type)\n ) {\n throw new Error(`Cannot bind ${variable.type} variable to boolean field \"${field}\"`)\n }\n\n const isKnownField =\n isNumericVariableBindingField(field) ||\n STRING_BINDING_FIELDS.has(field) ||\n BOOLEAN_BINDING_FIELDS.has(field) ||\n paint !== null ||\n effect !== null\n\n if (!isKnownField) {\n throw new Error(`Unknown binding field \"${field}\"`)\n }\n\n graph.willChangeNode(node)\n node.boundVariables = { ...node.boundVariables, [field]: variableId }\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 })\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 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 })\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 if (field)\n setInstanceOverride(\n owner.instanceOverrides,\n owner.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 './schema'\nexport * from './text'\nexport * from './token'\nexport * from './value'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAmCA,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;AAEA,SAAgB,eAAe,OAAmB,IAAkB;CAClE,MAAM,WAAW,MAAM,UAAU,IAAI,EAAE;CACvC,IAAI,CAAC,UAAU;CACf,MAAM,UAAU,OAAO,EAAE;CACzB,MAAM,aAAa,MAAM,oBAAoB,IAAI,SAAS,YAAY;CACtE,IAAI,YACF,WAAW,cAAc,WAAW,YAAY,QAAQ,QAAQ,QAAQ,EAAE;CAE5E,KAAK,MAAM,QAAQ,MAAM,MAAM,OAAO,GAAG;EAEvC,IAAI,CADe,OAAO,OAAO,KAAK,cAAc,CAAC,CAAC,SAAS,EACjD,GAAG;EACjB,MAAM,eAAe,IAAI;EACzB,KAAK,iBAAiB,OAAO,KAAK,iBAAiB,UAAU,UAAU,EAAE;EAIzE,KAAK,wBAAwB,OAC3B,KAAK,wBACJ,GAAG,UAAU,EAAE,SAAS,KAAK,eAChC;EACA,MAAM,QAAQ,KAAK,gBAAgB,KAAK,IAAI;GAC1C,gBAAgB,EAAE,GAAG,KAAK,eAAe;G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AM,QAAQ,KAAK,gBAAgB,QAAQ;EACzC,gBAAgB,EAAE,GAAG,KAAK,eAAe;EACzC,uBAAuB,EAAE,GAAG,KAAK,sBAAsB;CACzD,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,MAAM,YAAY,KAAK,QAAQ,CAAC,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,MAAM,EAAE,OAAO,MAAM;CAC/E,KAAK,MAAM,YAAY,WAAW,MAAM,eAAe,QAAQ;CAC/D,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;CACF,KAAK,MAAM,YAAY,WACrB,MAAM,WAAW,SAAS,IAAI,EAAE,mBAAmB,SAAS,kBAAkB,CAAC;AACnF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/variables/index.ts"],"sourcesContent":["import { compact, partition, 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 { bindingOverrideField, setInstanceOverride } from '../instances/overrides'\nimport { siblingOrderKeys } from '../order-keys'\nimport type {\n Effect,\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 effectBindableProperties,\n effectBindingTarget,\n effectBindingVariableType,\n type EffectBindingProperty,\n paintBindingTarget,\n STRING_BINDING_FIELDS,\n TEXT_CONTENT_VARIABLE_TYPES,\n VISIBILITY_VARIABLE_TYPES\n} from './fields'\nimport { applyBoundPaintColor } from './paints'\n\nexport * from './resolution'\n\nexport function addVariable(graph: SceneGraph, variable: Variable): void {\n graph.willChangeVariable(variable.id)\n graph.variables.set(variable.id, variable)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection && !collection.variableIds.includes(variable.id)) {\n graph.willChangeCollection(collection.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.willChangeVariable(id)\n graph.variables.delete(id)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection) {\n graph.willChangeCollection(collection.id)\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 node.variableBindingScales = omitBy(\n node.variableBindingScales,\n (_, field) => !(field in node.boundVariables)\n )\n graph.emitter.emit('node:updated', node.id, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n markBoundVariablesOverrideOnInstance(graph, node.id)\n }\n}\n\nexport function addCollection(graph: SceneGraph, collection: VariableCollection): void {\n graph.willChangeCollection(collection.id)\n graph.variableCollections.set(collection.id, collection)\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.willChangeCollection(id)\n graph.variableCollections.delete(id)\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 graph.willChangeCollection(collectionId)\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 graph.willChangeVariable(varId)\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 graph.willChangeCollection(collectionId)\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) continue\n graph.willChangeVariable(varId)\n variable.valuesByMode = omit(variable.valuesByMode, [modeId])\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) return\n graph.willChangeCollection(collectionId)\n 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 graph.willChangeCollection(collectionId)\n collection.defaultModeId = modeId\n collection.modes = withSortPositionsInOrder(modesDefaultFirst(collection))\n}\n\nfunction withSortPositionsInOrder(modes: VariableCollectionMode[]): VariableCollectionMode[] {\n if (modes.every((mode) => !mode.sortPosition)) {\n return modes\n }\n const positions = siblingOrderKeys(modes.map((mode) => mode.sortPosition))\n return modes.map((mode, index) => ({ ...mode, sortPosition: positions[index] }))\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\n/** Checks that an effect binding names an effect the node has, a property it binds, and its type. */\nfunction assertEffectBinding(\n effects: readonly Effect[],\n target: { index: number; property: EffectBindingProperty },\n type: VariableType,\n field: string\n): void {\n const effect = effects.at(target.index)\n if (!effect) {\n throw new Error(`Index ${target.index} out of range for effects (length ${effects.length})`)\n }\n if (!effectBindableProperties(effect.type).includes(target.property)) {\n throw new Error(`A ${effect.type} effect has no bindable ${target.property} (\"${field}\")`)\n }\n if (type !== effectBindingVariableType(target.property)) {\n throw new Error(`Cannot bind ${type} variable to effect field \"${field}\"`)\n }\n}\n\n/** Checks a paint binding and drops the list's dead top-level binding it replaces. */\nfunction preparePaintBinding(\n graph: SceneGraph,\n node: SceneNode,\n paint: { list: 'fills' | 'strokes'; index: number },\n variable: Variable,\n field: string\n): void {\n // Color fields require COLOR variable type\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(`Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`)\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\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) preparePaintBinding(graph, node, paint, variable, field)\n\n const effect = effectBindingTarget(field)\n if (effect) assertEffectBinding(node.effects, effect, variable.type, field)\n\n if (isNumericVariableBindingField(field) && variable.type !== 'FLOAT') {\n throw new Error(`Cannot bind ${variable.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(variable.type)) {\n throw new Error(`Cannot bind ${variable.type} variable to string field \"${field}\"`)\n }\n\n if (\n BOOLEAN_BINDING_FIELDS.has(field) &&\n !VISIBILITY_VARIABLE_TYPES.some((type) => type === variable.type)\n ) {\n throw new Error(`Cannot bind ${variable.type} variable to boolean field \"${field}\"`)\n }\n\n const isKnownField =\n isNumericVariableBindingField(field) ||\n STRING_BINDING_FIELDS.has(field) ||\n BOOLEAN_BINDING_FIELDS.has(field) ||\n paint !== null ||\n effect !== null\n\n if (!isKnownField) {\n throw new Error(`Unknown binding field \"${field}\"`)\n }\n\n graph.willChangeNode(node)\n node.boundVariables = { ...node.boundVariables, [field]: variableId }\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 })\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 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 })\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 uniq(compact([nearest, owner])))\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 './value'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,YAAY,OAAmB,UAA0B;CACvE,MAAM,mBAAmB,SAAS,EAAE;CACpC,MAAM,UAAU,IAAI,SAAS,IAAI,QAAQ;CACzC,MAAM,aAAa,MAAM,oBAAoB,IAAI,SAAS,YAAY;CACtE,IAAI,cAAc,CAAC,WAAW,YAAY,SAAS,SAAS,EAAE,GAAG;EAC/D,MAAM,qBAAqB,WAAW,EAAE;EACxC,WAAW,YAAY,KAAK,SAAS,EAAE;CACzC;AACF;AAEA,SAAgB,eAAe,OAAmB,IAAkB;CAClE,MAAM,WAAW,MAAM,UAAU,IAAI,EAAE;CACvC,IAAI,CAAC,UAAU;CACf,MAAM,mBAAmB,EAAE;CAC3B,MAAM,UAAU,OAAO,EAAE;CACzB,MAAM,aAAa,MAAM,oBAAoB,IAAI,SAAS,YAAY;CACtE,IAAI,YAAY;EACd,MAAM,qBAAqB,WAAW,EAAE;EACxC,WAAW,cAAc,WAAW,YAAY,QAAQ,QAAQ,QAAQ,EAAE;CAC5E;CACA,KAAK,MAAM,QAAQ,MAAM,MAAM,OAAO,GAAG;EAEvC,IAAI,CADe,OA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BAAkB,CAAC;AACnF"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { SceneNode } from "../types.js";
|
|
3
|
+
import { PaintBindingList } from "./fields.js";
|
|
4
|
+
//#region src/variables/paints.d.ts
|
|
5
|
+
/** Whether a layer has a paint whose color is bound to a variable. */
|
|
6
|
+
declare function hasBoundPaints(node: SceneNode): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* A layer copied from another keeps the colors its bindings last applied there, so the copy's
|
|
9
|
+
* paints, edits included, stay as they are until their variables' colors for it change.
|
|
10
|
+
*/
|
|
11
|
+
declare function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void;
|
|
12
|
+
type PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>;
|
|
13
|
+
/**
|
|
14
|
+
* Whether `node` is an instance, or a layer an instance copies from its component, with a paint
|
|
15
|
+
* list neither it nor an instance around it overrides: such paints follow the component, and
|
|
16
|
+
* refreshing them is no edit.
|
|
17
|
+
*/
|
|
18
|
+
declare function copiesComponentPaints(graph: SceneGraph, node: SceneNode, list: PaintBindingList): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as
|
|
21
|
+
* opacity, once that color differs from the one last applied, discarding edits made since, as
|
|
22
|
+
* Figma refreshes them when a variable's value or a layer's mode changes.
|
|
23
|
+
*/
|
|
24
|
+
declare function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void;
|
|
25
|
+
/** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */
|
|
26
|
+
declare function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field: string): void;
|
|
27
|
+
/**
|
|
28
|
+
* Gives every bound paint in this subtree its variable's color where it sits. A new instance
|
|
29
|
+
* or a layer an instance sync adds is a copy, which shows its variables in its own context rather
|
|
30
|
+
* than whatever its component's paints hold.
|
|
31
|
+
*/
|
|
32
|
+
declare function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void;
|
|
33
|
+
/** Refreshes the bound paints in this subtree whose variables now give their layers another color. */
|
|
34
|
+
declare function refreshBoundPaintsIn(graph: SceneGraph, rootId: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* Records, for the paint lists a write sets, the colors their variables give the layer now, so a
|
|
37
|
+
* paint written with another color or opacity keeps it until that color changes.
|
|
38
|
+
*/
|
|
39
|
+
declare function recordWrittenBoundPaints(graph: SceneGraph, node: SceneNode, written: PaintLists): void;
|
|
40
|
+
/**
|
|
41
|
+
* Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose
|
|
42
|
+
* mode the move changed, as Figma does when a layer enters a frame or page in another mode. A
|
|
43
|
+
* move between parents in the same modes, or of layers that bind no paint, costs no more than
|
|
44
|
+
* comparing those modes.
|
|
45
|
+
*/
|
|
46
|
+
declare function refreshMovedBoundPaints(graph: SceneGraph, nodeId: string, previousParentId: string | null | undefined): void;
|
|
47
|
+
/**
|
|
48
|
+
* A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit
|
|
49
|
+
* to its paint's color or opacity does not reach them. An instance sync writing a copy's paints
|
|
50
|
+
* keeps the color and opacity of each one still bound to the same variable, and gives it the
|
|
51
|
+
* variable's color for the copy once that differs from the one last applied, or when it binds
|
|
52
|
+
* anew. `updates` holds what the sync writes, whose paints this sets in place.
|
|
53
|
+
*/
|
|
54
|
+
declare function keepCopyBoundPaints(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void;
|
|
55
|
+
//#endregion
|
|
56
|
+
export { applyBoundPaintColor, applyBoundPaintColorsIn, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints };
|
|
57
|
+
//# sourceMappingURL=paints.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paints.d.ts","names":[],"sources":["../../src/variables/paints.ts"],"mappings":";;;;;iBA4CgB,eAAe,MAAM;;;;;iBA+BrB,uBAAuB,QAAQ,WAAW,MAAM;KAyB3D,aAAa,KAAK,QAAQ;;;;;;iBAOf,sBACd,OAAO,YACP,MAAM,WACN,MAAM;;;;;;iBA4DQ,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,wBACd,OAAO,YACP,gBACA;;;;;;;;iBA+Cc,oBACd,OAAO,YACP,QAAQ,WACR,SAAS,QAAQ"}
|