@pairdraw/scene-graph 0.19.2 → 0.21.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/color/index.d.ts +28 -7
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +40 -15
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts +19 -6
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +56 -1
- package/dist/color/management.js.map +1 -1
- package/dist/color/profile.d.ts +32 -0
- package/dist/color/profile.d.ts.map +1 -0
- package/dist/color/profile.js +140 -0
- package/dist/color/profile.js.map +1 -0
- package/dist/comments/index.d.ts +2 -2
- package/dist/comments/index.js +2 -2
- package/dist/comments/merge.d.ts.map +1 -1
- package/dist/comments/merge.js +1 -0
- package/dist/comments/merge.js.map +1 -1
- package/dist/comments/schema.d.ts +3 -1
- package/dist/comments/schema.d.ts.map +1 -1
- package/dist/comments/schema.js +19 -2
- package/dist/comments/schema.js.map +1 -1
- package/dist/comments/threads.d.ts +3 -1
- package/dist/comments/threads.d.ts.map +1 -1
- package/dist/comments/threads.js +2 -1
- package/dist/comments/threads.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.map +1 -1
- package/dist/components/properties.js +7 -7
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +12 -1
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +30 -1
- package/dist/components/variants.js.map +1 -1
- package/dist/constants.d.ts +25 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +94 -1
- package/dist/constants.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/shadows.d.ts +2 -2
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +4 -4
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/text.d.ts +17 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +62 -1
- package/dist/css/text.js.map +1 -1
- package/dist/css/values.d.ts +7 -2
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +8 -4
- package/dist/css/values.js.map +1 -1
- package/dist/design-memory/index.d.ts +3 -0
- package/dist/design-memory/index.js +3 -0
- package/dist/design-memory/model.d.ts +33 -0
- package/dist/design-memory/model.d.ts.map +1 -0
- package/dist/design-memory/model.js +142 -0
- package/dist/design-memory/model.js.map +1 -0
- package/dist/design-memory/schema.d.ts +76 -0
- package/dist/design-memory/schema.d.ts.map +1 -0
- package/dist/design-memory/schema.js +72 -0
- package/dist/design-memory/schema.js.map +1 -0
- package/dist/effects.d.ts +7 -4
- package/dist/effects.d.ts.map +1 -1
- package/dist/effects.js +9 -4
- package/dist/effects.js.map +1 -1
- package/dist/empty-values.js +102 -0
- package/dist/empty-values.js.map +1 -0
- package/dist/fields/components.d.ts +9 -0
- package/dist/fields/components.d.ts.map +1 -0
- package/dist/fields/components.js +24 -0
- package/dist/fields/components.js.map +1 -0
- package/dist/fields/index.d.ts +3 -2
- package/dist/fields/index.js +3 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +10 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/graph.d.ts +12 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +39 -7
- package/dist/graph.js.map +1 -1
- package/dist/grid-values.d.ts +10 -0
- package/dist/grid-values.d.ts.map +1 -0
- package/dist/grid-values.js +53 -0
- package/dist/grid-values.js.map +1 -0
- package/dist/hit-test/drop.d.ts +20 -0
- package/dist/hit-test/drop.d.ts.map +1 -0
- package/dist/hit-test/drop.js +54 -0
- package/dist/hit-test/drop.js.map +1 -0
- package/dist/hit-test/geometry.js +86 -0
- package/dist/hit-test/geometry.js.map +1 -0
- package/dist/hit-test/masks.d.ts +14 -0
- package/dist/hit-test/masks.d.ts.map +1 -0
- package/dist/hit-test/masks.js +41 -0
- package/dist/hit-test/masks.js.map +1 -0
- package/dist/hit-test/paint.d.ts +6 -0
- package/dist/hit-test/paint.d.ts.map +1 -0
- package/dist/hit-test/paint.js +19 -0
- package/dist/hit-test/paint.js.map +1 -0
- package/dist/hit-test.d.ts +4 -15
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +5 -173
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +3 -2
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +18 -2
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/copies.js +2 -1
- package/dist/instances/copies.js.map +1 -1
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +2 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +2 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +13 -12
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/overrides.d.ts +15 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +37 -1
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/source-walk.d.ts +12 -0
- package/dist/instances/source-walk.d.ts.map +1 -0
- package/dist/instances/swap-overrides.js +3 -3
- package/dist/instances/swap-overrides.js.map +1 -1
- package/dist/instances/sync.js +9 -7
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +43 -38
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +90 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +33 -24
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/plugin-data/index.d.ts +2 -2
- package/dist/plugin-data/index.js +2 -2
- package/dist/plugin-data/schema.d.ts +6 -1
- package/dist/plugin-data/schema.d.ts.map +1 -1
- package/dist/plugin-data/schema.js +24 -1
- package/dist/plugin-data/schema.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +2 -0
- package/dist/preview.js.map +1 -1
- 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/shaders/animation.d.ts +21 -0
- package/dist/shaders/animation.d.ts.map +1 -0
- package/dist/shaders/animation.js +25 -0
- package/dist/shaders/animation.js.map +1 -0
- package/dist/shaders/index.d.ts +2 -1
- package/dist/shaders/index.js +2 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +4 -4
- package/dist/slots/content.js.map +1 -1
- package/dist/statuses/index.d.ts +2 -2
- package/dist/statuses/index.js +2 -2
- package/dist/statuses/workflow.d.ts +3 -1
- package/dist/statuses/workflow.d.ts.map +1 -1
- package/dist/statuses/workflow.js +6 -2
- package/dist/statuses/workflow.js.map +1 -1
- package/dist/token-sources/document.d.ts +26 -0
- package/dist/token-sources/document.d.ts.map +1 -0
- package/dist/token-sources/document.js +80 -0
- package/dist/token-sources/document.js.map +1 -0
- package/dist/token-sources/index.d.ts +3 -0
- package/dist/token-sources/index.js +3 -0
- package/dist/token-sources/schema.d.ts +43 -0
- package/dist/token-sources/schema.d.ts.map +1 -0
- package/dist/token-sources/schema.js +50 -0
- package/dist/token-sources/schema.js.map +1 -0
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +1 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +135 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +6 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +24 -16
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/formula/index.d.ts.map +1 -1
- package/dist/variables/formula/index.js +12 -6
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/index.d.ts +14 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +38 -10
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +9 -8
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +35 -26
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolver.d.ts +45 -0
- package/dist/variables/resolver.d.ts.map +1 -0
- package/dist/variables/resolver.js +106 -0
- package/dist/variables/resolver.js.map +1 -0
- package/dist/variations/copy.d.ts +16 -0
- package/dist/variations/copy.d.ts.map +1 -0
- package/dist/variations/copy.js +78 -0
- package/dist/variations/copy.js.map +1 -0
- package/dist/variations/index.d.ts +6 -0
- package/dist/variations/index.js +6 -0
- package/dist/variations/match.d.ts +23 -0
- package/dist/variations/match.d.ts.map +1 -0
- package/dist/variations/match.js +80 -0
- package/dist/variations/match.js.map +1 -0
- package/dist/variations/model.d.ts +39 -0
- package/dist/variations/model.d.ts.map +1 -0
- package/dist/variations/model.js +90 -0
- package/dist/variations/model.js.map +1 -0
- package/dist/variations/pick.d.ts +43 -0
- package/dist/variations/pick.d.ts.map +1 -0
- package/dist/variations/pick.js +211 -0
- package/dist/variations/pick.js.map +1 -0
- package/dist/variations/schema.d.ts +27 -0
- package/dist/variations/schema.d.ts.map +1 -0
- package/dist/variations/schema.js +27 -0
- package/dist/variations/schema.js.map +1 -0
- package/package.json +21 -1
- package/src/color/index.ts +43 -9
- package/src/color/management.ts +54 -1
- package/src/color/profile.ts +131 -0
- package/src/comments/merge.ts +3 -0
- package/src/comments/schema.ts +32 -2
- package/src/comments/threads.ts +5 -2
- package/src/components/properties.ts +13 -7
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +7 -1
- package/src/css/shadows.ts +8 -5
- package/src/css/text.ts +62 -0
- package/src/css/values.ts +12 -5
- package/src/design-memory/index.ts +2 -0
- package/src/design-memory/model.ts +143 -0
- package/src/design-memory/schema.ts +84 -0
- package/src/effects.ts +16 -7
- package/src/empty-values.ts +117 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/graph.ts +50 -6
- package/src/grid-values.ts +47 -0
- package/src/hit-test/drop.ts +90 -0
- package/src/hit-test/geometry.ts +131 -0
- package/src/hit-test/masks.ts +57 -0
- package/src/hit-test/paint.ts +24 -0
- package/src/hit-test.ts +8 -277
- package/src/images.ts +27 -3
- package/src/instances/copies.ts +2 -1
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +20 -11
- package/src/instances/overrides.ts +49 -0
- package/src/instances/swap-overrides.ts +3 -2
- package/src/instances/sync.ts +11 -7
- package/src/node-defaults.ts +51 -41
- package/src/plugin-data/fields.ts +34 -32
- package/src/plugin-data/index.ts +1 -0
- package/src/plugin-data/schema.ts +30 -0
- package/src/preview.ts +2 -0
- package/src/scaling/node.ts +7 -2
- package/src/shaders/animation.ts +39 -0
- package/src/shaders/index.ts +1 -0
- package/src/slots/content.ts +8 -4
- package/src/statuses/workflow.ts +6 -4
- package/src/token-sources/document.ts +104 -0
- package/src/token-sources/index.ts +2 -0
- package/src/token-sources/schema.ts +59 -0
- package/src/transfer/variables.ts +2 -1
- package/src/types.ts +145 -14
- package/src/variables/bindings.ts +35 -18
- package/src/variables/formula/index.ts +19 -18
- package/src/variables/index.ts +49 -10
- package/src/variables/paints.ts +64 -29
- package/src/variables/resolver.ts +178 -0
- package/src/variations/copy.ts +89 -0
- package/src/variations/index.ts +5 -0
- package/src/variations/match.ts +100 -0
- package/src/variations/model.ts +132 -0
- package/src/variations/pick.ts +293 -0
- package/src/variations/schema.ts +29 -0
|
@@ -1 +1 @@
|
|
|
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, [(mode) => !mode.sortPosition, (mode) => mode.sortPosition ?? ''])\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;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;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,MAAM,SAAS,OAAO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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;CAG/E,IAAI,OACF,KAAK,MAAM,YAAY,WACrB,oBACE,SAAS,mBACT,SAAS,IACT,QACA,qBAAqB,KAAK,GAC1B,KAAK,eAAe,UAAU,IAChC;CACJ,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, 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 ownInstanceOverrides,\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'\nexport * from './resolver'\n\n/**\n * Variables a list shows: every one but those deleted, on their own or with their collection,\n * which only their bindings still reach.\n */\nexport function liveVariables(graph: SceneGraph): Variable[] {\n return [...graph.variables.values()].filter(\n (variable) =>\n !variable.deleted && !graph.variableCollections.get(variable.collectionId)?.deleted\n )\n}\n\n/** Collections a list shows: every one but those deleted. */\nexport function liveCollections(graph: SceneGraph): VariableCollection[] {\n return [...graph.variableCollections.values()].filter((collection) => !collection.deleted)\n}\n\n/**\n * A collection's deleted variables, which its `variableIds` leave out. A deleted collection's\n * variables are all deleted.\n */\nexport function deletedVariablesOf(graph: SceneGraph, collectionId: string): Variable[] {\n return [...graph.variables.values()].filter(\n (variable) => variable.collectionId === collectionId && variable.deleted\n )\n}\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) return\n // A collection lists its live variables, so one replaced by its deleted record leaves the list.\n if (collection.variableIds.includes(variable.id) === !variable.deleted) return\n collection.variableIds = variable.deleted\n ? collection.variableIds.filter((id) => id !== variable.id)\n : [...collection.variableIds, variable.id]\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 for (const variable of deletedVariablesOf(graph, id)) removeVariable(graph, variable.id)\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 const state = node.instanceOverrides\n const claimsMode =\n state.self.has(overrideField) ||\n [...state.descendants.values()].some((fields) => fields.has(overrideField))\n if (claimsMode) {\n const owned = ownInstanceOverrides(node)\n owned.self.delete(overrideField)\n for (const overrides of owned.descendants.values()) overrides.delete(overrideField)\n }\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, [(mode) => !mode.sortPosition, (mode) => mode.sortPosition ?? ''])\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 liveVariables(graph).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)\n setInstanceOverride(ownInstanceOverrides(nearest), nearest.id, nodeId, 'boundVariables')\n setInstanceOverride(ownInstanceOverrides(owner), 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 ownInstanceOverrides(instance),\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,cAAc,OAA+B;CAC3D,OAAO,CAAC,GAAG,MAAM,UAAU,OAAO,CAAC,CAAC,CAAC,QAClC,aACC,CAAC,SAAS,WAAW,CAAC,MAAM,oBAAoB,IAAI,SAAS,YAAY,CAAC,EAAE,OAChF;AACF;;AAGA,SAAgB,gBAAgB,OAAyC;CACvE,OAAO,CAAC,GAAG,MAAM,oBAAoB,OAAO,CAAC,CAAC,CAAC,QAAQ,eAAe,CAAC,WAAW,OAAO;AAC3F;;;;;AAMA,SAAgB,mBAAmB,OAAmB,cAAkC;CACtF,OAAO,CAAC,GAAG,MAAM,UAAU,OAAO,CAAC,CAAC,CAAC,QAClC,aAAa,SAAS,iBAAiB,gBAAgB,SAAS,OACnE;AACF;;;;;AAMA,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,SAA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BAAqB,KAAK,GAAG,MAAM,IAAI,QAAQ,gBAAgB;CAGnF,IAAI,OACF,KAAK,MAAM,YAAY,WACrB,oBACE,qBAAqB,QAAQ,GAC7B,SAAS,IACT,QACA,qBAAqB,KAAK,GAC1B,KAAK,eAAe,UAAU,IAChC;CACJ,KAAK,MAAM,YAAY,WACrB,MAAM,WAAW,SAAS,IAAI,EAAE,mBAAmB,SAAS,kBAAkB,CAAC;AACnF"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Color } from "../primitives/index.js";
|
|
2
|
+
import { VariableResolver } from "./resolver.js";
|
|
2
3
|
import { PaintBindingList } from "./fields.js";
|
|
3
4
|
import { SceneGraph } from "../graph.js";
|
|
4
5
|
import { SceneNode } from "../types.js";
|
|
@@ -58,36 +59,36 @@ declare function copiesComponentPaints(graph: SceneGraph, node: SceneNode, list:
|
|
|
58
59
|
* opacity, once that color differs from the one last applied, discarding edits made since, as
|
|
59
60
|
* Figma refreshes them when a variable's value or a layer's mode changes.
|
|
60
61
|
*/
|
|
61
|
-
declare function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void;
|
|
62
|
+
declare function refreshBoundPaints(graph: SceneGraph, node: SceneNode, resolver?: VariableResolver): void;
|
|
62
63
|
/** 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
|
+
declare function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field: string, resolver?: VariableResolver): void;
|
|
64
65
|
/**
|
|
65
66
|
* Gives every bound paint in this subtree its variable's color where it sits. A new instance
|
|
66
67
|
* or a layer an instance sync adds is a copy, which shows its variables in its own context rather
|
|
67
68
|
* than whatever its component's paints hold.
|
|
68
69
|
*/
|
|
69
|
-
declare function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void;
|
|
70
|
+
declare function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string, resolver?: VariableResolver): void;
|
|
70
71
|
/** 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
|
+
declare function refreshBoundPaintsIn(graph: SceneGraph, rootId: string, resolver?: VariableResolver): void;
|
|
72
73
|
/**
|
|
73
74
|
* Records, for the paint lists a write sets, the colors their variables give the layer now, so a
|
|
74
75
|
* paint written with another color or opacity keeps it until that color changes.
|
|
75
76
|
*/
|
|
76
|
-
declare function recordWrittenBoundPaints(graph: SceneGraph, node: SceneNode, written: PaintLists): void;
|
|
77
|
+
declare function recordWrittenBoundPaints(graph: SceneGraph, node: SceneNode, written: PaintLists, resolver?: VariableResolver): void;
|
|
77
78
|
/**
|
|
78
79
|
* The fills and strokes a layer shows: its stored paints, except a bound paint no applied color
|
|
79
80
|
* vouches for takes its variable's color for the layer, alpha as opacity, as Figma shows a layer
|
|
80
81
|
* whose saved cache is stale on import. Nothing is written or recorded; a copy pasted unlinked
|
|
81
82
|
* keeps the colors it showed.
|
|
82
83
|
*/
|
|
83
|
-
declare function shownPaintBindings(graph: SceneGraph, node: SceneNode): Pick<Partial<SceneNode>, 'fills' | 'strokes'>;
|
|
84
|
+
declare function shownPaintBindings(graph: SceneGraph, node: SceneNode, resolver?: VariableResolver): Pick<Partial<SceneNode>, 'fills' | 'strokes'>;
|
|
84
85
|
/**
|
|
85
86
|
* Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose
|
|
86
87
|
* mode set the move changed, as Figma does when a layer enters a frame or page that sets another
|
|
87
88
|
* mode, or one where none was. A move between parents setting the same modes, or of layers that
|
|
88
89
|
* bind no paint, costs no more than comparing those modes.
|
|
89
90
|
*/
|
|
90
|
-
declare function refreshMovedBoundPaints(graph: SceneGraph, nodeId: string, previousParentId: string | null | undefined): void;
|
|
91
|
+
declare function refreshMovedBoundPaints(graph: SceneGraph, nodeId: string, previousParentId: string | null | undefined, resolver?: VariableResolver): void;
|
|
91
92
|
/**
|
|
92
93
|
* A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit
|
|
93
94
|
* to its paint's color or opacity does not reach them. An instance sync writing a copy's paints
|
|
@@ -95,7 +96,7 @@ declare function refreshMovedBoundPaints(graph: SceneGraph, nodeId: string, prev
|
|
|
95
96
|
* variable's color for the copy once that differs from the one last applied, or when it binds
|
|
96
97
|
* anew. `updates` holds what the sync writes, whose paints this sets in place.
|
|
97
98
|
*/
|
|
98
|
-
declare function keepCopyBoundPaints(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode
|
|
99
|
+
declare function keepCopyBoundPaints(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>, resolver?: VariableResolver): void;
|
|
99
100
|
//#endregion
|
|
100
101
|
export { AppliedBoundPaints, AppliedColor, applyBoundPaintColor, applyBoundPaintColorsIn, captureAppliedBoundPaints, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, keepPastedBoundPaints, paintColorApplied, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints, shownPaintBindings };
|
|
101
102
|
//# sourceMappingURL=paints.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paints.d.ts","names":[],"sources":["../../src/variables/paints.ts"],"mappings":"
|
|
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;;;;;iBAuDnC,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;;;;;;iBAmEQ,mBACd,OAAO,YACP,MAAM,WACN,WAAW;;iBAMG,qBACd,OAAO,YACP,MAAM,WACN,eACA,WAAW;;;;;;iBAUG,wBACd,OAAO,YACP,gBACA,WAAW;;iBAQG,qBACd,OAAO,YACP,gBACA,WAAW;;;;;iBAUG,yBACd,OAAO,YACP,MAAM,WACN,SAAS,YACT,WAAW;;;;;;;iBAiBG,mBACd,OAAO,YACP,MAAM,WACN,WAAW,mBACV,KAAK,QAAQ;;;;;;;iBAyBA,wBACd,OAAO,YACP,gBACA,6CACA,WAAW;;;;;;;;iBA+CG,oBACd,OAAO,YACP,QAAQ,WACR,SAAS,QAAQ,YACjB,WAAW"}
|
package/dist/variables/paints.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { hasInstanceOverride } from "../instances/overrides.js";
|
|
2
2
|
import { copyFills, copyStrokes } from "../copy.js";
|
|
3
3
|
import { paintBindingTarget } from "./fields.js";
|
|
4
|
-
import {
|
|
4
|
+
import { createVariableResolver } from "./resolver.js";
|
|
5
5
|
import { subtreeNodes } from "./subtree.js";
|
|
6
6
|
import { isEqual } from "es-toolkit/predicate";
|
|
7
7
|
//#region src/variables/paints.ts
|
|
@@ -51,9 +51,10 @@ function sameColor(a, b) {
|
|
|
51
51
|
* The modes set for a layer, or null, in the collections a variable resolves through. Figma
|
|
52
52
|
* refreshes a bound paint whenever one of them changes, even to a mode giving the same color.
|
|
53
53
|
*/
|
|
54
|
-
function explicitModes(graph, node, variableId) {
|
|
54
|
+
function explicitModes(graph, node, variableId, resolver) {
|
|
55
55
|
const modes = {};
|
|
56
|
-
|
|
56
|
+
const shared = resolver ?? createVariableResolver(graph);
|
|
57
|
+
for (const collectionId of shared.dependencies([variableId])) modes[collectionId] = shared.explicitModeFor(node.id, collectionId);
|
|
57
58
|
return modes;
|
|
58
59
|
}
|
|
59
60
|
/** Whether the color a binding last applied is stale: another color, or set in other modes. */
|
|
@@ -158,14 +159,15 @@ function writePaints(graph, node, changes) {
|
|
|
158
159
|
* Writes into each bound paint the color its variable gives the layer now, where that differs
|
|
159
160
|
* from the color last applied, or wherever `forced` says, and records what each binding gives.
|
|
160
161
|
*/
|
|
161
|
-
function refresh(graph, node, forced) {
|
|
162
|
+
function refresh(graph, node, forced, resolver) {
|
|
162
163
|
let fills;
|
|
163
164
|
let strokes;
|
|
165
|
+
const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id);
|
|
164
166
|
for (const bound of boundPaints(node)) {
|
|
165
|
-
const color =
|
|
167
|
+
const color = reader.color(bound.variableId);
|
|
166
168
|
const paint = node[bound.list].at(bound.index);
|
|
167
169
|
if (!color || !paint) continue;
|
|
168
|
-
const modes = explicitModes(graph, node, bound.variableId);
|
|
170
|
+
const modes = explicitModes(graph, node, bound.variableId, resolver);
|
|
169
171
|
const stale = staleApplied(node, bound, paint, color, modes);
|
|
170
172
|
recordApplied(node, bound, color, modes);
|
|
171
173
|
if (!forced(bound.field) && !stale || showsColor(paint, color)) continue;
|
|
@@ -189,34 +191,37 @@ const ALL_FORCED = () => true;
|
|
|
189
191
|
* opacity, once that color differs from the one last applied, discarding edits made since, as
|
|
190
192
|
* Figma refreshes them when a variable's value or a layer's mode changes.
|
|
191
193
|
*/
|
|
192
|
-
function refreshBoundPaints(graph, node) {
|
|
193
|
-
if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED);
|
|
194
|
+
function refreshBoundPaints(graph, node, resolver) {
|
|
195
|
+
if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED, resolver);
|
|
194
196
|
}
|
|
195
197
|
/** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */
|
|
196
|
-
function applyBoundPaintColor(graph, node, field) {
|
|
197
|
-
if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field);
|
|
198
|
+
function applyBoundPaintColor(graph, node, field, resolver) {
|
|
199
|
+
if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field, resolver);
|
|
198
200
|
}
|
|
199
201
|
/**
|
|
200
202
|
* Gives every bound paint in this subtree its variable's color where it sits. A new instance
|
|
201
203
|
* or a layer an instance sync adds is a copy, which shows its variables in its own context rather
|
|
202
204
|
* than whatever its component's paints hold.
|
|
203
205
|
*/
|
|
204
|
-
function applyBoundPaintColorsIn(graph, rootId) {
|
|
205
|
-
|
|
206
|
+
function applyBoundPaintColorsIn(graph, rootId, resolver) {
|
|
207
|
+
const shared = resolver ?? createVariableResolver(graph);
|
|
208
|
+
for (const node of subtreeNodes(graph, [rootId])) if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED, shared);
|
|
206
209
|
}
|
|
207
210
|
/** Refreshes the bound paints in this subtree whose variables now give their layers another color. */
|
|
208
|
-
function refreshBoundPaintsIn(graph, rootId) {
|
|
209
|
-
|
|
211
|
+
function refreshBoundPaintsIn(graph, rootId, resolver) {
|
|
212
|
+
const shared = resolver ?? createVariableResolver(graph);
|
|
213
|
+
for (const node of subtreeNodes(graph, [rootId])) refreshBoundPaints(graph, node, shared);
|
|
210
214
|
}
|
|
211
215
|
/**
|
|
212
216
|
* Records, for the paint lists a write sets, the colors their variables give the layer now, so a
|
|
213
217
|
* paint written with another color or opacity keeps it until that color changes.
|
|
214
218
|
*/
|
|
215
|
-
function recordWrittenBoundPaints(graph, node, written) {
|
|
219
|
+
function recordWrittenBoundPaints(graph, node, written, resolver) {
|
|
220
|
+
const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id);
|
|
216
221
|
for (const bound of boundPaints(node)) {
|
|
217
222
|
if (!written[bound.list]) continue;
|
|
218
|
-
const color =
|
|
219
|
-
if (color) recordApplied(node, bound, color, explicitModes(graph, node, bound.variableId));
|
|
223
|
+
const color = reader.color(bound.variableId);
|
|
224
|
+
if (color) recordApplied(node, bound, color, explicitModes(graph, node, bound.variableId, resolver));
|
|
220
225
|
}
|
|
221
226
|
}
|
|
222
227
|
/**
|
|
@@ -225,11 +230,12 @@ function recordWrittenBoundPaints(graph, node, written) {
|
|
|
225
230
|
* whose saved cache is stale on import. Nothing is written or recorded; a copy pasted unlinked
|
|
226
231
|
* keeps the colors it showed.
|
|
227
232
|
*/
|
|
228
|
-
function shownPaintBindings(graph, node) {
|
|
233
|
+
function shownPaintBindings(graph, node, resolver) {
|
|
229
234
|
const changes = {};
|
|
235
|
+
const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id);
|
|
230
236
|
for (const bound of boundPaints(node)) {
|
|
231
237
|
if (paintColorApplied(node, bound.field)) continue;
|
|
232
|
-
const color =
|
|
238
|
+
const color = reader.color(bound.variableId);
|
|
233
239
|
const paint = node[bound.list].at(bound.index);
|
|
234
240
|
if (!color || !paint || showsColor(paint, color)) continue;
|
|
235
241
|
if (bound.list === "fills") {
|
|
@@ -248,23 +254,24 @@ function shownPaintBindings(graph, node) {
|
|
|
248
254
|
* mode, or one where none was. A move between parents setting the same modes, or of layers that
|
|
249
255
|
* bind no paint, costs no more than comparing those modes.
|
|
250
256
|
*/
|
|
251
|
-
function refreshMovedBoundPaints(graph, nodeId, previousParentId) {
|
|
257
|
+
function refreshMovedBoundPaints(graph, nodeId, previousParentId, resolver) {
|
|
252
258
|
if (graph.isApplyingImportedState || graph.variableCollections.size === 0) return;
|
|
253
259
|
const parentId = graph.nodes.get(nodeId)?.parentId;
|
|
254
260
|
if (!parentId) return;
|
|
255
261
|
const previous = previousParentId && graph.nodes.has(previousParentId) ? previousParentId : null;
|
|
256
|
-
const
|
|
262
|
+
const shared = resolver ?? createVariableResolver(graph);
|
|
263
|
+
const changed = new Set([...graph.variableCollections.keys()].filter((id) => !previous || shared.explicitModeFor(previous, id) !== shared.explicitModeFor(parentId, id)));
|
|
257
264
|
if (changed.size === 0) return;
|
|
258
265
|
const reaches = /* @__PURE__ */ new Map();
|
|
259
266
|
const reachesChanged = (variableId) => {
|
|
260
267
|
let found = reaches.get(variableId);
|
|
261
268
|
if (found === void 0) {
|
|
262
|
-
found = [...
|
|
269
|
+
found = [...shared.dependencies([variableId])].some((id) => changed.has(id));
|
|
263
270
|
reaches.set(variableId, found);
|
|
264
271
|
}
|
|
265
272
|
return found;
|
|
266
273
|
};
|
|
267
|
-
for (const node of subtreeNodes(graph, [nodeId])) if (boundPaints(node).some((bound) => reachesChanged(bound.variableId))) refresh(graph, node, NONE_FORCED);
|
|
274
|
+
for (const node of subtreeNodes(graph, [nodeId])) if (boundPaints(node).some((bound) => reachesChanged(bound.variableId))) refresh(graph, node, NONE_FORCED, shared);
|
|
268
275
|
}
|
|
269
276
|
function reboundPaint(target, bindings, list) {
|
|
270
277
|
return target[list].some((_, index) => {
|
|
@@ -279,8 +286,10 @@ function reboundPaint(target, bindings, list) {
|
|
|
279
286
|
* variable's color for the copy once that differs from the one last applied, or when it binds
|
|
280
287
|
* anew. `updates` holds what the sync writes, whose paints this sets in place.
|
|
281
288
|
*/
|
|
282
|
-
function keepCopyBoundPaints(graph, target, updates) {
|
|
289
|
+
function keepCopyBoundPaints(graph, target, updates, resolver) {
|
|
283
290
|
const bindings = updates.boundVariables ?? target.boundVariables;
|
|
291
|
+
const shared = resolver ?? createVariableResolver(graph);
|
|
292
|
+
const reader = shared.forNode(target.id);
|
|
284
293
|
for (const list of PAINT_LISTS) {
|
|
285
294
|
if (!updates[list] && reboundPaint(target, bindings, list)) Object.assign(updates, { [list]: list === "fills" ? copyFills(target.fills) : copyStrokes(target.strokes) });
|
|
286
295
|
updates[list]?.forEach((paint, index) => {
|
|
@@ -299,9 +308,9 @@ function keepCopyBoundPaints(graph, target, updates) {
|
|
|
299
308
|
paint.color = { ...own.color };
|
|
300
309
|
paint.opacity = own.opacity;
|
|
301
310
|
}
|
|
302
|
-
const color =
|
|
311
|
+
const color = reader.color(variableId);
|
|
303
312
|
if (!color) return;
|
|
304
|
-
const modes = explicitModes(graph, target, variableId);
|
|
313
|
+
const modes = explicitModes(graph, target, variableId, shared);
|
|
305
314
|
if (!keeps || staleApplied(target, bound, own, color, modes)) applyColor(paint, color);
|
|
306
315
|
recordApplied(target, bound, color, modes);
|
|
307
316
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paints.js","names":[],"sources":["../../src/variables/paints.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit/predicate'\n\nimport { copyFills, copyStrokes } from '../copy'\nimport type { SceneGraph } from '../graph'\nimport { hasInstanceOverride } from '../instances/overrides'\nimport type { Color } from '../primitives'\nimport type { Fill, SceneNode, Stroke } from '../types'\nimport { paintBindingTarget, type PaintBindingList } from './fields'\nimport { collectionsResolving, getNodeExplicitVariableModeId } from './resolution'\nimport { subtreeNodes } from './subtree'\n\n/**\n * Figma keeps a bound paint's color and opacity as a cache of its variable: it draws and reads\n * what the paint holds, and writes the variable's color into it again only once the color the\n * variable gives that layer changes, or the mode set for the layer changes, so an edit to the\n * paint sticks until then. The color each binding last gave its paint, and the modes set for the\n * layer then, live here, beside the graph and never saved, by layer and binding field. A layer\n * without an entry, such as one just restored, counts its paint's own color as the last one\n * applied, in the modes it has now.\n */\nexport interface AppliedColor {\n readonly variableId: string\n readonly color: Color\n /** The mode set for the layer, or null, in each collection the variable resolves through. */\n readonly modes: Readonly<Record<string, string | null>>\n}\n\nconst appliedColors = new WeakMap<SceneNode, Map<string, AppliedColor>>()\n\nconst PAINT_LISTS = ['fills', 'strokes'] as const\n\ninterface BoundPaint {\n readonly field: string\n readonly list: PaintBindingList\n readonly index: number\n readonly variableId: string\n}\n\n/** The paints of `node` whose color is bound to a variable, each with a paint there to color. */\nfunction boundPaints(node: SceneNode): BoundPaint[] {\n const paints: BoundPaint[] = []\n for (const [field, variableId] of Object.entries(node.boundVariables)) {\n const target = paintBindingTarget(field)\n if (target && node[target.list].at(target.index)) paints.push({ field, ...target, variableId })\n }\n return paints\n}\n\n/** Whether a layer has a paint whose color is bound to a variable. */\nexport function hasBoundPaints(node: SceneNode): boolean {\n for (const field in node.boundVariables) if (paintBindingTarget(field)) return true\n return false\n}\n\n/**\n * Whether a binding's paint holds a color this graph applied or recorded for it. A layer without\n * an entry, such as one just imported or built with its bindings, has a stored color nothing\n * vouches for: readers show its variable's color until a write or refresh records one.\n */\nexport function paintColorApplied(node: SceneNode, field: string): boolean {\n return appliedColors.get(node)?.has(field) ?? false\n}\n\n/** A paint's color with its opacity as alpha, as a variable's color carries it. */\nfunction paintColor(paint: Fill): Color {\n return { r: paint.color.r, g: paint.color.g, b: paint.color.b, a: paint.opacity }\n}\n\n/** Saved colors are single precision, so a color read back from a file still matches its source. */\nconst COLOR_TOLERANCE = 1e-6\n\nfunction sameColor(a: Color, b: Color): boolean {\n return (\n Math.abs(a.r - b.r) <= COLOR_TOLERANCE &&\n Math.abs(a.g - b.g) <= COLOR_TOLERANCE &&\n Math.abs(a.b - b.b) <= COLOR_TOLERANCE &&\n Math.abs(a.a - b.a) <= COLOR_TOLERANCE\n )\n}\n\n/**\n * The modes set for a layer, or null, in the collections a variable resolves through. Figma\n * refreshes a bound paint whenever one of them changes, even to a mode giving the same color.\n */\nfunction explicitModes(\n graph: SceneGraph,\n node: SceneNode,\n variableId: string\n): Record<string, string | null> {\n const modes: Record<string, string | null> = {}\n for (const collectionId of collectionsResolving(graph, [variableId]))\n modes[collectionId] = getNodeExplicitVariableModeId(graph, node.id, collectionId)\n return modes\n}\n\n/** Whether the color a binding last applied is stale: another color, or set in other modes. */\nfunction staleApplied(\n node: SceneNode,\n bound: BoundPaint,\n paint: Fill,\n color: Color,\n modes: Readonly<Record<string, string | null>>\n): boolean {\n const entry = appliedColors.get(node)?.get(bound.field)\n if (entry?.variableId !== bound.variableId) return !sameColor(paintColor(paint), color)\n return !sameColor(entry.color, color) || !isEqual(entry.modes, modes)\n}\n\n/**\n * A layer copied from another keeps the colors its bindings last applied there, so the copy's\n * paints, edits included, stay as they are until their variables' colors for it change.\n */\nexport function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void {\n const entries = appliedColors.get(source)\n if (entries) appliedColors.set(copy, new Map(entries))\n}\n\n/** The colors copied layers' bindings last applied, by layer id, as a copy captured them. */\nexport type AppliedBoundPaints = ReadonlyMap<string, ReadonlyMap<string, AppliedColor>>\n\n/**\n * The colors the bindings of the layers `nodeIds` names last applied, captured when they are\n * copied, so a paste counts what the copied paints were applied from, not what the source has now.\n */\nexport function captureAppliedBoundPaints(\n graph: SceneGraph,\n nodeIds: Iterable<string>\n): AppliedBoundPaints {\n const captured = new Map<string, ReadonlyMap<string, AppliedColor>>()\n for (const id of nodeIds) {\n const node = graph.nodes.get(id)\n const entries = node && appliedColors.get(node)\n if (entries) captured.set(id, new Map(entries))\n }\n return captured\n}\n\n/**\n * Pasted layers keep their paints, edits included, as Figma's copies do. One copied in this\n * document takes the colors its source's bindings had last applied when it was copied, so a paste\n * into another mode refreshes them as a move there would; one from elsewhere counts the colors\n * its variables give it now as applied, as a loaded document does. `copies` maps each copied\n * layer's id to its copy's.\n */\nexport function keepPastedBoundPaints(\n graph: SceneGraph,\n copies: ReadonlyMap<string, string>,\n applied: AppliedBoundPaints\n): void {\n for (const [sourceId, copyId] of copies) {\n const copy = graph.nodes.get(copyId)\n if (!copy || !hasBoundPaints(copy)) continue\n const entries = applied.get(sourceId)\n if (!entries) {\n recordWrittenBoundPaints(graph, copy, { fills: copy.fills, strokes: copy.strokes })\n continue\n }\n appliedColors.set(copy, new Map(entries))\n refresh(graph, copy, NONE_FORCED)\n }\n}\n\nfunction recordApplied(\n node: SceneNode,\n bound: BoundPaint,\n color: Color,\n modes: Readonly<Record<string, string | null>>\n): void {\n let entries = appliedColors.get(node)\n if (!entries) {\n entries = new Map()\n appliedColors.set(node, entries)\n }\n entries.set(bound.field, { variableId: bound.variableId, color: { ...color }, modes })\n}\n\n/** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */\nfunction applyColor(paint: Fill, color: Color): void {\n paint.color = { ...color, a: 1 }\n paint.opacity = color.a\n}\n\n/** Whether `paint` already shows `color` as a bound paint stores it. */\nfunction showsColor(paint: Fill, color: Color): boolean {\n return sameColor(paintColor(paint), color) && paint.color.a === 1\n}\n\ntype PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>\n\n/**\n * Whether `node` is an instance, or a layer an instance copies from its component, with a paint\n * list neither it nor an instance around it overrides: such paints follow the component, and\n * refreshing them is no edit.\n */\nexport function copiesComponentPaints(\n graph: SceneGraph,\n node: SceneNode,\n list: PaintBindingList\n): boolean {\n const walk = { copied: false }\n const overriding = graph.closest(node.id, (current) => {\n if (current.type !== 'INSTANCE') return false\n walk.copied = true\n return hasInstanceOverride(current.instanceOverrides, current.id, node.id, list)\n })\n return walk.copied && !overriding\n}\n\n/**\n * Writes refreshed paints. A copy's paints that follow its component are derived values that a\n * save leaves to the component, not edits written over it.\n */\nfunction writePaints(graph: SceneGraph, node: SceneNode, changes: PaintLists): void {\n const derived = PAINT_LISTS.every(\n (list) => !changes[list] || copiesComponentPaints(graph, node, list)\n )\n const write = () => graph.updateNode(node.id, changes)\n if (derived) graph.applyComponentDerivedDuring(write)\n else write()\n}\n\n/**\n * Writes into each bound paint the color its variable gives the layer now, where that differs\n * from the color last applied, or wherever `forced` says, and records what each binding gives.\n */\nfunction refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) => boolean): void {\n let fills: Fill[] | undefined\n let strokes: Stroke[] | undefined\n for (const bound of boundPaints(node)) {\n const color = graph.resolveColorVariableForNode(node.id, bound.variableId)\n const paint = node[bound.list].at(bound.index)\n if (!color || !paint) continue\n const modes = explicitModes(graph, node, bound.variableId)\n const stale = staleApplied(node, bound, paint, color, modes)\n recordApplied(node, bound, color, modes)\n if ((!forced(bound.field) && !stale) || showsColor(paint, color)) continue\n if (bound.list === 'fills') {\n fills ??= copyFills(node.fills)\n applyColor(fills[bound.index], color)\n } else {\n strokes ??= copyStrokes(node.strokes)\n applyColor(strokes[bound.index], color)\n }\n }\n const changes: PaintLists = {}\n if (fills) changes.fills = fills\n if (strokes) changes.strokes = strokes\n if (fills || strokes) writePaints(graph, node, changes)\n}\n\nconst NONE_FORCED = (): boolean => false\nconst ALL_FORCED = (): boolean => true\n\n/**\n * Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as\n * opacity, once that color differs from the one last applied, discarding edits made since, as\n * Figma refreshes them when a variable's value or a layer's mode changes.\n */\nexport function refreshBoundPaints(graph: SceneGraph, node: SceneNode): void {\n if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED)\n}\n\n/** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */\nexport function applyBoundPaintColor(graph: SceneGraph, node: SceneNode, field: string): void {\n if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field)\n}\n\n/**\n * Gives every bound paint in this subtree its variable's color where it sits. A new instance\n * or a layer an instance sync adds is a copy, which shows its variables in its own context rather\n * than whatever its component's paints hold.\n */\nexport function applyBoundPaintColorsIn(graph: SceneGraph, rootId: string): void {\n for (const node of subtreeNodes(graph, [rootId]))\n if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED)\n}\n\n/** Refreshes the bound paints in this subtree whose variables now give their layers another color. */\nexport function refreshBoundPaintsIn(graph: SceneGraph, rootId: string): void {\n for (const node of subtreeNodes(graph, [rootId])) refreshBoundPaints(graph, node)\n}\n\n/**\n * Records, for the paint lists a write sets, the colors their variables give the layer now, so a\n * paint written with another color or opacity keeps it until that color changes.\n */\nexport function recordWrittenBoundPaints(\n graph: SceneGraph,\n node: SceneNode,\n written: PaintLists\n): void {\n for (const bound of boundPaints(node)) {\n if (!written[bound.list]) continue\n const color = graph.resolveColorVariableForNode(node.id, bound.variableId)\n if (color) recordApplied(node, bound, color, explicitModes(graph, node, bound.variableId))\n }\n}\n\n/**\n * The fills and strokes a layer shows: its stored paints, except a bound paint no applied color\n * vouches for takes its variable's color for the layer, alpha as opacity, as Figma shows a layer\n * whose saved cache is stale on import. Nothing is written or recorded; a copy pasted unlinked\n * keeps the colors it showed.\n */\nexport function shownPaintBindings(\n graph: SceneGraph,\n node: SceneNode\n): Pick<Partial<SceneNode>, 'fills' | 'strokes'> {\n const changes: Pick<Partial<SceneNode>, 'fills' | 'strokes'> = {}\n for (const bound of boundPaints(node)) {\n if (paintColorApplied(node, bound.field)) continue\n const color = graph.resolveColorVariableForNode(node.id, bound.variableId)\n const paint = node[bound.list].at(bound.index)\n if (!color || !paint || showsColor(paint, color)) continue\n if (bound.list === 'fills') {\n changes.fills ??= copyFills(node.fills)\n applyColor(changes.fills[bound.index], color)\n } else {\n changes.strokes ??= copyStrokes(node.strokes)\n applyColor(changes.strokes[bound.index], color)\n }\n }\n return changes\n}\n\n/**\n * Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose\n * mode set the move changed, as Figma does when a layer enters a frame or page that sets another\n * mode, or one where none was. A move between parents setting the same modes, or of layers that\n * bind no paint, costs no more than comparing those modes.\n */\nexport function refreshMovedBoundPaints(\n graph: SceneGraph,\n nodeId: string,\n previousParentId: string | null | undefined\n): void {\n if (graph.isApplyingImportedState || graph.variableCollections.size === 0) return\n const parentId = graph.nodes.get(nodeId)?.parentId\n if (!parentId) return\n const previous = previousParentId && graph.nodes.has(previousParentId) ? previousParentId : null\n const changed = new Set(\n [...graph.variableCollections.keys()].filter(\n (id) =>\n !previous ||\n getNodeExplicitVariableModeId(graph, previous, id) !==\n getNodeExplicitVariableModeId(graph, parentId, id)\n )\n )\n if (changed.size === 0) return\n const reaches = new Map<string, boolean>()\n const reachesChanged = (variableId: string) => {\n let found = reaches.get(variableId)\n if (found === undefined) {\n found = [...collectionsResolving(graph, [variableId])].some((id) => changed.has(id))\n reaches.set(variableId, found)\n }\n return found\n }\n for (const node of subtreeNodes(graph, [nodeId])) {\n if (boundPaints(node).some((bound) => reachesChanged(bound.variableId)))\n refresh(graph, node, NONE_FORCED)\n }\n}\n\nfunction reboundPaint(\n target: SceneNode,\n bindings: Readonly<Record<string, string>>,\n list: PaintBindingList\n): boolean {\n return target[list].some((_, index) => {\n const field = `${list}/${index}/color`\n return field in bindings && bindings[field] !== target.boundVariables[field]\n })\n}\n\n/**\n * A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit\n * to its paint's color or opacity does not reach them. An instance sync writing a copy's paints\n * keeps the color and opacity of each one still bound to the same variable, and gives it the\n * variable's color for the copy once that differs from the one last applied, or when it binds\n * anew. `updates` holds what the sync writes, whose paints this sets in place.\n */\nexport function keepCopyBoundPaints(\n graph: SceneGraph,\n target: SceneNode,\n updates: Partial<SceneNode>\n): void {\n const bindings = updates.boundVariables ?? target.boundVariables\n for (const list of PAINT_LISTS) {\n // A paint the component binds anew keeps its own paint otherwise, but takes the new color.\n if (!updates[list] && reboundPaint(target, bindings, list))\n Object.assign(updates, {\n [list]: list === 'fills' ? copyFills(target.fills) : copyStrokes(target.strokes)\n })\n updates[list]?.forEach((paint, index) => {\n const field = `${list}/${index}/color`\n const variableId = bindings[field]\n if (!variableId) return\n const bound = { field, list, index, variableId }\n const own = target[list].at(index)\n const keeps = own !== undefined && target.boundVariables[field] === variableId\n if (keeps) {\n paint.color = { ...own.color }\n paint.opacity = own.opacity\n }\n const color = graph.resolveColorVariableForNode(target.id, variableId)\n if (!color) return\n const modes = explicitModes(graph, target, variableId)\n if (!keeps || staleApplied(target, bound, own, color, modes)) applyColor(paint, color)\n recordApplied(target, bound, color, modes)\n })\n }\n}\n"],"mappings":";;;;;;;AA2BA,MAAM,gCAAgB,IAAI,QAA8C;AAExE,MAAM,cAAc,CAAC,SAAS,SAAS;;AAUvC,SAAS,YAAY,MAA+B;CAClD,MAAM,SAAuB,CAAC;CAC9B,KAAK,MAAM,CAAC,OAAO,eAAe,OAAO,QAAQ,KAAK,cAAc,GAAG;EACrE,MAAM,SAAS,mBAAmB,KAAK;EACvC,IAAI,UAAU,KAAK,OAAO,KAAK,CAAC,GAAG,OAAO,KAAK,GAAG,OAAO,KAAK;GAAE;GAAO,GAAG;GAAQ;EAAW,CAAC;CAChG;CACA,OAAO;AACT;;AAGA,SAAgB,eAAe,MAA0B;CACvD,KAAK,MAAM,SAAS,KAAK,gBAAgB,IAAI,mBAAmB,KAAK,GAAG,OAAO;CAC/E,OAAO;AACT;;;;;;AAOA,SAAgB,kBAAkB,MAAiB,OAAwB;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CAClD,KAAK,MAAM,QAAQ,aAAa;EAE9B,IAAI,CAAC,QAAQ,SAAS,aAAa,QAAQ,UAAU,IAAI,GACvD,OAAO,OAAO,SAAS,GACpB,OAAO,SAAS,UAAU,UAAU,OAAO,KAAK,IAAI,YAAY,OAAO,OAAO,EACjF,CAAC;EACH,QAAQ,KAAK,EAAE,SAAS,OAAO,UAAU;GACvC,MAAM,QAAQ,GAAG,KAAK,GAAG,MAAM;GAC/B,MAAM,aAAa,SAAS;GAC5B,IAAI,CAAC,YAAY;GACjB,MAAM,QAAQ;IAAE;IAAO;IAAM;IAAO;GAAW;GAC/C,MAAM,MAAM,OAAO,KAAK,CAAC,GAAG,KAAK;GACjC,MAAM,QAAQ,QAAQ,KAAA,KAAa,OAAO,eAAe,WAAW;GACpE,IAAI,OAAO;IACT,MAAM,QAAQ,EAAE,GAAG,IAAI,MAAM;IAC7B,MAAM,UAAU,IAAI;GACtB;GACA,MAAM,QAAQ,MAAM,4BAA4B,OAAO,IAAI,UAAU;GACrE,IAAI,CAAC,OAAO;GACZ,MAAM,QAAQ,cAAc,OAAO,QAAQ,UAAU;GACrD,IAAI,CAAC,SAAS,aAAa,QAAQ,OAAO,KAAK,OAAO,KAAK,GAAG,WAAW,OAAO,KAAK;GACrF,cAAc,QAAQ,OAAO,OAAO,KAAK;EAC3C,CAAC;CACH;AACF"}
|
|
1
|
+
{"version":3,"file":"paints.js","names":[],"sources":["../../src/variables/paints.ts"],"sourcesContent":["import { isEqual } from 'es-toolkit/predicate'\n\nimport { copyFills, copyStrokes } from '../copy'\nimport type { SceneGraph } from '../graph'\nimport { hasInstanceOverride } from '../instances/overrides'\nimport type { Color } from '../primitives'\nimport type { Fill, SceneNode, Stroke } from '../types'\nimport { paintBindingTarget, type PaintBindingList } from './fields'\nimport { createVariableResolver, type VariableResolver } from './resolver'\nimport { subtreeNodes } from './subtree'\n\n/**\n * Figma keeps a bound paint's color and opacity as a cache of its variable: it draws and reads\n * what the paint holds, and writes the variable's color into it again only once the color the\n * variable gives that layer changes, or the mode set for the layer changes, so an edit to the\n * paint sticks until then. The color each binding last gave its paint, and the modes set for the\n * layer then, live here, beside the graph and never saved, by layer and binding field. A layer\n * without an entry, such as one just restored, counts its paint's own color as the last one\n * applied, in the modes it has now.\n */\nexport interface AppliedColor {\n readonly variableId: string\n readonly color: Color\n /** The mode set for the layer, or null, in each collection the variable resolves through. */\n readonly modes: Readonly<Record<string, string | null>>\n}\n\nconst appliedColors = new WeakMap<SceneNode, Map<string, AppliedColor>>()\n\nconst PAINT_LISTS = ['fills', 'strokes'] as const\n\ninterface BoundPaint {\n readonly field: string\n readonly list: PaintBindingList\n readonly index: number\n readonly variableId: string\n}\n\n/** The paints of `node` whose color is bound to a variable, each with a paint there to color. */\nfunction boundPaints(node: SceneNode): BoundPaint[] {\n const paints: BoundPaint[] = []\n for (const [field, variableId] of Object.entries(node.boundVariables)) {\n const target = paintBindingTarget(field)\n if (target && node[target.list].at(target.index)) paints.push({ field, ...target, variableId })\n }\n return paints\n}\n\n/** Whether a layer has a paint whose color is bound to a variable. */\nexport function hasBoundPaints(node: SceneNode): boolean {\n for (const field in node.boundVariables) if (paintBindingTarget(field)) return true\n return false\n}\n\n/**\n * Whether a binding's paint holds a color this graph applied or recorded for it. A layer without\n * an entry, such as one just imported or built with its bindings, has a stored color nothing\n * vouches for: readers show its variable's color until a write or refresh records one.\n */\nexport function paintColorApplied(node: SceneNode, field: string): boolean {\n return appliedColors.get(node)?.has(field) ?? false\n}\n\n/** A paint's color with its opacity as alpha, as a variable's color carries it. */\nfunction paintColor(paint: Fill): Color {\n return { r: paint.color.r, g: paint.color.g, b: paint.color.b, a: paint.opacity }\n}\n\n/** Saved colors are single precision, so a color read back from a file still matches its source. */\nconst COLOR_TOLERANCE = 1e-6\n\nfunction sameColor(a: Color, b: Color): boolean {\n return (\n Math.abs(a.r - b.r) <= COLOR_TOLERANCE &&\n Math.abs(a.g - b.g) <= COLOR_TOLERANCE &&\n Math.abs(a.b - b.b) <= COLOR_TOLERANCE &&\n Math.abs(a.a - b.a) <= COLOR_TOLERANCE\n )\n}\n\n/**\n * The modes set for a layer, or null, in the collections a variable resolves through. Figma\n * refreshes a bound paint whenever one of them changes, even to a mode giving the same color.\n */\nfunction explicitModes(\n graph: SceneGraph,\n node: SceneNode,\n variableId: string,\n resolver?: VariableResolver\n): Record<string, string | null> {\n const modes: Record<string, string | null> = {}\n const shared = resolver ?? createVariableResolver(graph)\n for (const collectionId of shared.dependencies([variableId]))\n modes[collectionId] = shared.explicitModeFor(node.id, collectionId)\n return modes\n}\n\n/** Whether the color a binding last applied is stale: another color, or set in other modes. */\nfunction staleApplied(\n node: SceneNode,\n bound: BoundPaint,\n paint: Fill,\n color: Color,\n modes: Readonly<Record<string, string | null>>\n): boolean {\n const entry = appliedColors.get(node)?.get(bound.field)\n if (entry?.variableId !== bound.variableId) return !sameColor(paintColor(paint), color)\n return !sameColor(entry.color, color) || !isEqual(entry.modes, modes)\n}\n\n/**\n * A layer copied from another keeps the colors its bindings last applied there, so the copy's\n * paints, edits included, stay as they are until their variables' colors for it change.\n */\nexport function copyAppliedBoundPaints(source: SceneNode, copy: SceneNode): void {\n const entries = appliedColors.get(source)\n if (entries) appliedColors.set(copy, new Map(entries))\n}\n\n/** The colors copied layers' bindings last applied, by layer id, as a copy captured them. */\nexport type AppliedBoundPaints = ReadonlyMap<string, ReadonlyMap<string, AppliedColor>>\n\n/**\n * The colors the bindings of the layers `nodeIds` names last applied, captured when they are\n * copied, so a paste counts what the copied paints were applied from, not what the source has now.\n */\nexport function captureAppliedBoundPaints(\n graph: SceneGraph,\n nodeIds: Iterable<string>\n): AppliedBoundPaints {\n const captured = new Map<string, ReadonlyMap<string, AppliedColor>>()\n for (const id of nodeIds) {\n const node = graph.nodes.get(id)\n const entries = node && appliedColors.get(node)\n if (entries) captured.set(id, new Map(entries))\n }\n return captured\n}\n\n/**\n * Pasted layers keep their paints, edits included, as Figma's copies do. One copied in this\n * document takes the colors its source's bindings had last applied when it was copied, so a paste\n * into another mode refreshes them as a move there would; one from elsewhere counts the colors\n * its variables give it now as applied, as a loaded document does. `copies` maps each copied\n * layer's id to its copy's.\n */\nexport function keepPastedBoundPaints(\n graph: SceneGraph,\n copies: ReadonlyMap<string, string>,\n applied: AppliedBoundPaints\n): void {\n for (const [sourceId, copyId] of copies) {\n const copy = graph.nodes.get(copyId)\n if (!copy || !hasBoundPaints(copy)) continue\n const entries = applied.get(sourceId)\n if (!entries) {\n recordWrittenBoundPaints(graph, copy, { fills: copy.fills, strokes: copy.strokes })\n continue\n }\n appliedColors.set(copy, new Map(entries))\n refresh(graph, copy, NONE_FORCED)\n }\n}\n\nfunction recordApplied(\n node: SceneNode,\n bound: BoundPaint,\n color: Color,\n modes: Readonly<Record<string, string | null>>\n): void {\n let entries = appliedColors.get(node)\n if (!entries) {\n entries = new Map()\n appliedColors.set(node, entries)\n }\n entries.set(bound.field, { variableId: bound.variableId, color: { ...color }, modes })\n}\n\n/** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */\nfunction applyColor(paint: Fill, color: Color): void {\n paint.color = { ...color, a: 1 }\n paint.opacity = color.a\n}\n\n/** Whether `paint` already shows `color` as a bound paint stores it. */\nfunction showsColor(paint: Fill, color: Color): boolean {\n return sameColor(paintColor(paint), color) && paint.color.a === 1\n}\n\ntype PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>\n\n/**\n * Whether `node` is an instance, or a layer an instance copies from its component, with a paint\n * list neither it nor an instance around it overrides: such paints follow the component, and\n * refreshing them is no edit.\n */\nexport function copiesComponentPaints(\n graph: SceneGraph,\n node: SceneNode,\n list: PaintBindingList\n): boolean {\n const walk = { copied: false }\n const overriding = graph.closest(node.id, (current) => {\n if (current.type !== 'INSTANCE') return false\n walk.copied = true\n return hasInstanceOverride(current.instanceOverrides, current.id, node.id, list)\n })\n return walk.copied && !overriding\n}\n\n/**\n * Writes refreshed paints. A copy's paints that follow its component are derived values that a\n * save leaves to the component, not edits written over it.\n */\nfunction writePaints(graph: SceneGraph, node: SceneNode, changes: PaintLists): void {\n const derived = PAINT_LISTS.every(\n (list) => !changes[list] || copiesComponentPaints(graph, node, list)\n )\n const write = () => graph.updateNode(node.id, changes)\n if (derived) graph.applyComponentDerivedDuring(write)\n else write()\n}\n\n/**\n * Writes into each bound paint the color its variable gives the layer now, where that differs\n * from the color last applied, or wherever `forced` says, and records what each binding gives.\n */\nfunction refresh(\n graph: SceneGraph,\n node: SceneNode,\n forced: (field: string) => boolean,\n resolver?: VariableResolver\n): void {\n let fills: Fill[] | undefined\n let strokes: Stroke[] | undefined\n const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id)\n for (const bound of boundPaints(node)) {\n const color = reader.color(bound.variableId)\n const paint = node[bound.list].at(bound.index)\n if (!color || !paint) continue\n const modes = explicitModes(graph, node, bound.variableId, resolver)\n const stale = staleApplied(node, bound, paint, color, modes)\n recordApplied(node, bound, color, modes)\n if ((!forced(bound.field) && !stale) || showsColor(paint, color)) continue\n if (bound.list === 'fills') {\n fills ??= copyFills(node.fills)\n applyColor(fills[bound.index], color)\n } else {\n strokes ??= copyStrokes(node.strokes)\n applyColor(strokes[bound.index], color)\n }\n }\n const changes: PaintLists = {}\n if (fills) changes.fills = fills\n if (strokes) changes.strokes = strokes\n if (fills || strokes) writePaints(graph, node, changes)\n}\n\nconst NONE_FORCED = (): boolean => false\nconst ALL_FORCED = (): boolean => true\n\n/**\n * Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as\n * opacity, once that color differs from the one last applied, discarding edits made since, as\n * Figma refreshes them when a variable's value or a layer's mode changes.\n */\nexport function refreshBoundPaints(\n graph: SceneGraph,\n node: SceneNode,\n resolver?: VariableResolver\n): void {\n if (hasBoundPaints(node)) refresh(graph, node, NONE_FORCED, resolver)\n}\n\n/** Gives one bound paint its variable's color for the layer, as binding it does in Figma. */\nexport function applyBoundPaintColor(\n graph: SceneGraph,\n node: SceneNode,\n field: string,\n resolver?: VariableResolver\n): void {\n if (paintBindingTarget(field)) refresh(graph, node, (bound) => bound === field, resolver)\n}\n\n/**\n * Gives every bound paint in this subtree its variable's color where it sits. A new instance\n * or a layer an instance sync adds is a copy, which shows its variables in its own context rather\n * than whatever its component's paints hold.\n */\nexport function applyBoundPaintColorsIn(\n graph: SceneGraph,\n rootId: string,\n resolver?: VariableResolver\n): void {\n const shared = resolver ?? createVariableResolver(graph)\n for (const node of subtreeNodes(graph, [rootId]))\n if (hasBoundPaints(node)) refresh(graph, node, ALL_FORCED, shared)\n}\n\n/** Refreshes the bound paints in this subtree whose variables now give their layers another color. */\nexport function refreshBoundPaintsIn(\n graph: SceneGraph,\n rootId: string,\n resolver?: VariableResolver\n): void {\n const shared = resolver ?? createVariableResolver(graph)\n for (const node of subtreeNodes(graph, [rootId])) refreshBoundPaints(graph, node, shared)\n}\n\n/**\n * Records, for the paint lists a write sets, the colors their variables give the layer now, so a\n * paint written with another color or opacity keeps it until that color changes.\n */\nexport function recordWrittenBoundPaints(\n graph: SceneGraph,\n node: SceneNode,\n written: PaintLists,\n resolver?: VariableResolver\n): void {\n const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id)\n for (const bound of boundPaints(node)) {\n if (!written[bound.list]) continue\n const color = reader.color(bound.variableId)\n if (color)\n recordApplied(node, bound, color, explicitModes(graph, node, bound.variableId, resolver))\n }\n}\n\n/**\n * The fills and strokes a layer shows: its stored paints, except a bound paint no applied color\n * vouches for takes its variable's color for the layer, alpha as opacity, as Figma shows a layer\n * whose saved cache is stale on import. Nothing is written or recorded; a copy pasted unlinked\n * keeps the colors it showed.\n */\nexport function shownPaintBindings(\n graph: SceneGraph,\n node: SceneNode,\n resolver?: VariableResolver\n): Pick<Partial<SceneNode>, 'fills' | 'strokes'> {\n const changes: Pick<Partial<SceneNode>, 'fills' | 'strokes'> = {}\n const reader = (resolver ?? createVariableResolver(graph)).forNode(node.id)\n for (const bound of boundPaints(node)) {\n if (paintColorApplied(node, bound.field)) continue\n const color = reader.color(bound.variableId)\n const paint = node[bound.list].at(bound.index)\n if (!color || !paint || showsColor(paint, color)) continue\n if (bound.list === 'fills') {\n changes.fills ??= copyFills(node.fills)\n applyColor(changes.fills[bound.index], color)\n } else {\n changes.strokes ??= copyStrokes(node.strokes)\n applyColor(changes.strokes[bound.index], color)\n }\n }\n return changes\n}\n\n/**\n * Refreshes the bound paints in a moved layer's subtree that resolve through a collection whose\n * mode set the move changed, as Figma does when a layer enters a frame or page that sets another\n * mode, or one where none was. A move between parents setting the same modes, or of layers that\n * bind no paint, costs no more than comparing those modes.\n */\nexport function refreshMovedBoundPaints(\n graph: SceneGraph,\n nodeId: string,\n previousParentId: string | null | undefined,\n resolver?: VariableResolver\n): void {\n if (graph.isApplyingImportedState || graph.variableCollections.size === 0) return\n const parentId = graph.nodes.get(nodeId)?.parentId\n if (!parentId) return\n const previous = previousParentId && graph.nodes.has(previousParentId) ? previousParentId : null\n const shared = resolver ?? createVariableResolver(graph)\n const changed = new Set(\n [...graph.variableCollections.keys()].filter(\n (id) =>\n !previous || shared.explicitModeFor(previous, id) !== shared.explicitModeFor(parentId, id)\n )\n )\n if (changed.size === 0) return\n const reaches = new Map<string, boolean>()\n const reachesChanged = (variableId: string) => {\n let found = reaches.get(variableId)\n if (found === undefined) {\n found = [...shared.dependencies([variableId])].some((id) => changed.has(id))\n reaches.set(variableId, found)\n }\n return found\n }\n for (const node of subtreeNodes(graph, [nodeId])) {\n if (boundPaints(node).some((bound) => reachesChanged(bound.variableId)))\n refresh(graph, node, NONE_FORCED, shared)\n }\n}\n\nfunction reboundPaint(\n target: SceneNode,\n bindings: Readonly<Record<string, string>>,\n list: PaintBindingList\n): boolean {\n return target[list].some((_, index) => {\n const field = `${list}/${index}/color`\n return field in bindings && bindings[field] !== target.boundVariables[field]\n })\n}\n\n/**\n * A copy's bound paints are its own cache, as Figma keeps them per layer, so a component's edit\n * to its paint's color or opacity does not reach them. An instance sync writing a copy's paints\n * keeps the color and opacity of each one still bound to the same variable, and gives it the\n * variable's color for the copy once that differs from the one last applied, or when it binds\n * anew. `updates` holds what the sync writes, whose paints this sets in place.\n */\nexport function keepCopyBoundPaints(\n graph: SceneGraph,\n target: SceneNode,\n updates: Partial<SceneNode>,\n resolver?: VariableResolver\n): void {\n const bindings = updates.boundVariables ?? target.boundVariables\n const shared = resolver ?? createVariableResolver(graph)\n const reader = shared.forNode(target.id)\n for (const list of PAINT_LISTS) {\n // A paint the component binds anew keeps its own paint otherwise, but takes the new color.\n if (!updates[list] && reboundPaint(target, bindings, list))\n Object.assign(updates, {\n [list]: list === 'fills' ? copyFills(target.fills) : copyStrokes(target.strokes)\n })\n updates[list]?.forEach((paint, index) => {\n const field = `${list}/${index}/color`\n const variableId = bindings[field]\n if (!variableId) return\n const bound = { field, list, index, variableId }\n const own = target[list].at(index)\n const keeps = own !== undefined && target.boundVariables[field] === variableId\n if (keeps) {\n paint.color = { ...own.color }\n paint.opacity = own.opacity\n }\n const color = reader.color(variableId)\n if (!color) return\n const modes = explicitModes(graph, target, variableId, shared)\n if (!keeps || staleApplied(target, bound, own, color, modes)) applyColor(paint, color)\n recordApplied(target, bound, color, modes)\n })\n }\n}\n"],"mappings":";;;;;;;AA2BA,MAAM,gCAAgB,IAAI,QAA8C;AAExE,MAAM,cAAc,CAAC,SAAS,SAAS;;AAUvC,SAAS,YAAY,MAA+B;CAClD,MAAM,SAAuB,CAAC;CAC9B,KAAK,MAAM,CAAC,OAAO,eAAe,OAAO,QAAQ,KAAK,cAAc,GAAG;EACrE,MAAM,SAAS,mBAAmB,KAAK;EACvC,IAAI,UAAU,KAAK,OAAO,KAAK,CAAC,GAAG,OAAO,KAAK,GAAG,OAAO,KAAK;GAAE;GAAO,GAAG;GAAQ;EAAW,CAAC;CAChG;CACA,OAAO;AACT;;AAGA,SAAgB,eAAe,MAA0B;CACvD,KAAK,MAAM,SAAS,KAAK,gBAAgB,IAAI,mBAAmB,KAAK,GAAG,OAAO;CAC/E,OAAO;AACT;;;;;;AAOA,SAAgB,kBAAkB,MAAi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F,cAAc,QAAQ,OAAO,OAAO,KAAK;EAC3C,CAAC;CACH;AACF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Color } from "../primitives/index.js";
|
|
2
|
+
import { VariableModeFallback } from "./resolution.js";
|
|
3
|
+
import { SceneGraph } from "../graph.js";
|
|
4
|
+
import { VariableLiteral, VariableValue } from "../types.js";
|
|
5
|
+
//#region src/variables/resolver.d.ts
|
|
6
|
+
/** One reader per mode context; completed values are shared inside the resolver. */
|
|
7
|
+
interface VariableReader {
|
|
8
|
+
/** Any variable type, following aliases and expressions. Undefined when it cannot resolve. */
|
|
9
|
+
value(variableId: string): VariableLiteral | undefined;
|
|
10
|
+
/** Strict form for formula drafts and diagnostics; throws VariableResolutionError. */
|
|
11
|
+
resolve(variableId: string): VariableLiteral;
|
|
12
|
+
/** Stored value in this context's mode, with no default fallback; aliases are followed. */
|
|
13
|
+
stored(variableId: string): VariableValue | undefined;
|
|
14
|
+
color(variableId: string): Color | undefined;
|
|
15
|
+
number(variableId: string): number | undefined;
|
|
16
|
+
string(variableId: string): string | undefined;
|
|
17
|
+
boolean(variableId: string): boolean | undefined;
|
|
18
|
+
}
|
|
19
|
+
interface VariableResolver {
|
|
20
|
+
forNode(nodeId: string, fallback?: VariableModeFallback): VariableReader;
|
|
21
|
+
active(): VariableReader;
|
|
22
|
+
/**
|
|
23
|
+
* The collection's `modeId`, with every other collection in its active mode. A mode id from
|
|
24
|
+
* another collection is not an explicit mode here, so the collection keeps its active one.
|
|
25
|
+
*/
|
|
26
|
+
forMode(collectionId: string, modeId: string): VariableReader;
|
|
27
|
+
forModes(modeFor: (collectionId: string) => string): VariableReader;
|
|
28
|
+
activeMode(collectionId: string): string;
|
|
29
|
+
modeFor(nodeId: string, collectionId: string, fallback?: VariableModeFallback): string;
|
|
30
|
+
explicitModeFor(nodeId: string, collectionId: string): string | null;
|
|
31
|
+
modesFor(nodeId: string): Record<string, string>;
|
|
32
|
+
dependencies(variableIds: Iterable<string>): Set<string>;
|
|
33
|
+
}
|
|
34
|
+
interface VariableResolverOptions {
|
|
35
|
+
/** Draft values for formula validation; stored reads still use the graph. */
|
|
36
|
+
overrides?: ReadonlyMap<string, VariableValue>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* One value resolver for canvas, editor, exports and tools. Create one per operation: the
|
|
40
|
+
* memo is only valid for the graph state it was built against.
|
|
41
|
+
*/
|
|
42
|
+
declare function createVariableResolver(graph: SceneGraph, options?: VariableResolverOptions): VariableResolver;
|
|
43
|
+
//#endregion
|
|
44
|
+
export { VariableReader, VariableResolver, VariableResolverOptions, createVariableResolver };
|
|
45
|
+
//# sourceMappingURL=resolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolver.d.ts","names":[],"sources":["../../src/variables/resolver.ts"],"mappings":";;;;;;UAciB;;EAEf,MAAM,qBAAqB;;EAE3B,QAAQ,qBAAqB;;EAE7B,OAAO,qBAAqB;EAE5B,MAAM,qBAAqB;EAC3B,OAAO;EACP,OAAO;EACP,QAAQ;;UAGO;EACf,QAAQ,gBAAgB,WAAW,uBAAuB;EAC1D,UAAU;;;;;EAKV,QAAQ,sBAAsB,iBAAiB;EAC/C,SAAS,UAAU,kCAAkC;EAErD,WAAW;EACX,QAAQ,gBAAgB,sBAAsB,WAAW;EACzD,gBAAgB,gBAAgB;EAChC,SAAS,iBAAiB;EAC1B,aAAa,aAAa,mBAAmB;;UAG9B;;EAEf,YAAY,oBAAoB;;;;;;iBAgFlB,uBACd,OAAO,YACP,UAAS,0BACR"}
|