@open-pencil/scene-graph 0.17.0 → 0.18.0-beta.1
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/behaviours/index.d.ts +3 -2
- package/dist/behaviours/index.js +2 -1
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +15 -8
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/model.d.ts +2 -2
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +136 -1
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +17 -2
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +4 -0
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/behaviours/transition.d.ts +23 -0
- package/dist/behaviours/transition.d.ts.map +1 -0
- package/dist/behaviours/transition.js +36 -0
- package/dist/behaviours/transition.js.map +1 -0
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +2 -0
- package/dist/checkpoint.js.map +1 -1
- package/dist/color/index.d.ts +12 -9
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +15 -25
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts +51 -23
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +154 -149
- package/dist/color/management.js.map +1 -1
- package/dist/color/okhcl.d.ts +28 -10
- package/dist/color/okhcl.d.ts.map +1 -1
- package/dist/color/okhcl.js +56 -40
- package/dist/color/okhcl.js.map +1 -1
- package/dist/color/schema.d.ts +6 -1
- package/dist/color/schema.d.ts.map +1 -1
- package/dist/color/schema.js +10 -1
- package/dist/color/schema.js.map +1 -1
- package/dist/constraints.d.ts.map +1 -1
- package/dist/constraints.js +2 -1
- package/dist/constraints.js.map +1 -1
- package/dist/copy.d.ts +7 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +22 -1
- package/dist/copy.js.map +1 -1
- package/dist/corners.d.ts +3 -1
- package/dist/corners.d.ts.map +1 -1
- package/dist/corners.js +3 -1
- package/dist/corners.js.map +1 -1
- package/dist/css/grid.js +1 -1
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +3 -2
- package/dist/css/motion.d.ts +12 -0
- package/dist/css/motion.d.ts.map +1 -0
- package/dist/css/motion.js +60 -0
- package/dist/css/motion.js.map +1 -0
- package/dist/css/shadows.d.ts +4 -3
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +7 -6
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/values.d.ts +3 -2
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +4 -4
- package/dist/css/values.js.map +1 -1
- package/dist/fields/index.d.ts +2 -2
- package/dist/fields/index.js +2 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +9 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/figjam/palette.js +1 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +7 -3
- package/dist/font-style.js.map +1 -1
- package/dist/generated/index.d.ts +6 -2
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +14 -4
- package/dist/generated/index.js.map +1 -1
- package/dist/geometry.d.ts +6 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +10 -2
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +29 -4
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +59 -9
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +5 -2
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +38 -8
- package/dist/group-bounds.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +19 -8
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/instances/content-size.js +1 -1
- package/dist/instances/fields.d.ts +2 -2
- 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.map +1 -1
- package/dist/instances/index.js +50 -11
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/sync.js +67 -7
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/flow.d.ts +16 -0
- package/dist/layout/flow.d.ts.map +1 -0
- package/dist/layout/flow.js +34 -0
- package/dist/layout/flow.js.map +1 -0
- package/dist/layout/index.d.ts +4 -3
- package/dist/layout/index.js +4 -3
- package/dist/layout/keywords.d.ts.map +1 -1
- package/dist/layout/keywords.js +7 -1
- package/dist/layout/keywords.js.map +1 -1
- package/dist/layout/sizing.d.ts +9 -1
- package/dist/layout/sizing.d.ts.map +1 -1
- package/dist/layout/sizing.js +17 -1
- package/dist/layout/sizing.js.map +1 -1
- package/dist/layout/spacing.d.ts +7 -1
- package/dist/layout/spacing.d.ts.map +1 -1
- package/dist/layout/spacing.js +10 -1
- package/dist/layout/spacing.js.map +1 -1
- package/dist/mesh/geometry.d.ts +83 -0
- package/dist/mesh/geometry.d.ts.map +1 -0
- package/dist/mesh/geometry.js +441 -0
- package/dist/mesh/geometry.js.map +1 -0
- package/dist/mesh/index.d.ts +4 -0
- package/dist/mesh/index.js +4 -0
- package/dist/mesh/model.d.ts +28 -0
- package/dist/mesh/model.d.ts.map +1 -0
- package/dist/mesh/model.js +40 -0
- package/dist/mesh/model.js.map +1 -0
- package/dist/mesh/schema.d.ts +191 -0
- package/dist/mesh/schema.d.ts.map +1 -0
- package/dist/mesh/schema.js +56 -0
- package/dist/mesh/schema.js.map +1 -0
- package/dist/mutation-impact.d.ts +2 -0
- package/dist/mutation-impact.d.ts.map +1 -1
- package/dist/mutation-impact.js +18 -7
- package/dist/mutation-impact.js.map +1 -1
- package/dist/node-defaults.d.ts +3 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +22 -6
- package/dist/node-defaults.js.map +1 -1
- package/dist/paints/index.d.ts +5 -3
- package/dist/paints/index.js +5 -3
- package/dist/paints/pattern.d.ts +5 -1
- package/dist/paints/pattern.d.ts.map +1 -1
- package/dist/paints/pattern.js +33 -1
- package/dist/paints/pattern.js.map +1 -1
- package/dist/paints/shows.d.ts +4 -1
- package/dist/paints/shows.d.ts.map +1 -1
- package/dist/paints/shows.js +8 -1
- package/dist/paints/shows.js.map +1 -1
- package/dist/paints/solid.d.ts +11 -0
- package/dist/paints/solid.d.ts.map +1 -0
- package/dist/paints/solid.js +18 -0
- package/dist/paints/solid.js.map +1 -0
- package/dist/paints/stills.d.ts +74 -0
- package/dist/paints/stills.d.ts.map +1 -0
- package/dist/paints/stills.js +113 -0
- package/dist/paints/stills.js.map +1 -0
- package/dist/plugin-data/field.d.ts.map +1 -1
- package/dist/plugin-data/field.js +2 -0
- package/dist/plugin-data/field.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +67 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +12 -7
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/resize.d.ts +10 -3
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +34 -3
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +2 -2
- package/dist/scaling/index.js +2 -2
- package/dist/scaling/node.d.ts +2 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +27 -7
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/index.d.ts +2 -3
- package/dist/shaders/index.js +2 -3
- package/dist/shaders/model.d.ts +3 -11
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +13 -36
- package/dist/shaders/model.js.map +1 -1
- package/dist/shaders/schema.d.ts +1 -1
- package/dist/shaders/schema.js +1 -1
- package/dist/shaders/schema.js.map +1 -1
- package/dist/shape-with-text/document.js +276 -0
- package/dist/shape-with-text/document.js.map +1 -0
- package/dist/shape-with-text/geometry.d.ts +13 -1
- package/dist/shape-with-text/geometry.d.ts.map +1 -1
- package/dist/shape-with-text/geometry.js +103 -41
- package/dist/shape-with-text/geometry.js.map +1 -1
- package/dist/shape-with-text/index.d.ts +2 -2
- package/dist/shape-with-text/index.js +2 -2
- package/dist/shared-style-index.d.ts +21 -0
- package/dist/shared-style-index.d.ts.map +1 -0
- package/dist/shared-style-index.js +72 -0
- package/dist/shared-style-index.js.map +1 -0
- package/dist/shared-styles.d.ts +3 -3
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +16 -7
- package/dist/shared-styles.js.map +1 -1
- package/dist/slides/drop.d.ts +32 -0
- package/dist/slides/drop.d.ts.map +1 -0
- package/dist/slides/drop.js +81 -0
- package/dist/slides/drop.js.map +1 -0
- package/dist/slides/index.d.ts +56 -0
- package/dist/slides/index.d.ts.map +1 -0
- package/dist/slides/index.js +134 -0
- package/dist/slides/index.js.map +1 -0
- package/dist/slides/motion.d.ts +55 -0
- package/dist/slides/motion.d.ts.map +1 -0
- package/dist/slides/motion.js +142 -0
- package/dist/slides/motion.js.map +1 -0
- package/dist/slides/playback.d.ts +59 -0
- package/dist/slides/playback.d.ts.map +1 -0
- package/dist/slides/playback.js +188 -0
- package/dist/slides/playback.js.map +1 -0
- package/dist/slides/types.d.ts +62 -0
- package/dist/slides/types.d.ts.map +1 -0
- package/dist/slides/types.js +39 -0
- package/dist/slides/types.js.map +1 -0
- package/dist/statuses/workflow.js +1 -1
- package/dist/text/direction.d.ts +6 -3
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +12 -12
- package/dist/text/direction.js.map +1 -1
- package/dist/text/index.d.ts +2 -1
- package/dist/text/index.js +3 -2
- package/dist/text/name.d.ts +22 -0
- package/dist/text/name.d.ts.map +1 -0
- package/dist/text/name.js +38 -0
- package/dist/text/name.js.map +1 -0
- package/dist/text/references.d.ts.map +1 -1
- package/dist/text/references.js +1 -2
- package/dist/text/references.js.map +1 -1
- package/dist/thumbnail.d.ts +23 -0
- package/dist/thumbnail.d.ts.map +1 -0
- package/dist/thumbnail.js +47 -0
- package/dist/thumbnail.js.map +1 -0
- package/dist/transfer/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +14 -3
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +6 -1
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +2 -2
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +4 -1
- package/dist/transfer/dependencies.d.ts.map +1 -1
- package/dist/transfer/dependencies.js +10 -1
- package/dist/transfer/dependencies.js.map +1 -1
- package/dist/transfer/history.js +2 -0
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/identity.js +18 -3
- package/dist/transfer/identity.js.map +1 -1
- package/dist/transfer/index.d.ts +2 -1
- package/dist/transfer/index.js +4 -3
- package/dist/transfer/library.d.ts +45 -0
- package/dist/transfer/library.d.ts.map +1 -0
- package/dist/transfer/library.js +102 -0
- package/dist/transfer/library.js.map +1 -0
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +9 -0
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +9 -0
- package/dist/transfer/plan.d.ts.map +1 -1
- package/dist/transfer/plan.js +51 -6
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +7 -1
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/validation.js +8 -4
- package/dist/transfer/validation.js.map +1 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +2 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +90 -12
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +33 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +11 -6
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +162 -38
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/easing.d.ts +48 -0
- package/dist/variables/easing.d.ts.map +1 -0
- package/dist/variables/easing.js +321 -0
- package/dist/variables/easing.js.map +1 -0
- package/dist/variables/formula/index.d.ts +4 -2
- package/dist/variables/formula/index.d.ts.map +1 -1
- package/dist/variables/formula/index.js +26 -9
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/parse.js +1 -1
- package/dist/variables/formula/schema.js +1 -1
- package/dist/variables/formula/signatures.d.ts +2 -2
- package/dist/variables/formula/signatures.d.ts.map +1 -1
- package/dist/variables/formula/signatures.js.map +1 -1
- package/dist/variables/formula/tree.d.ts +2 -2
- package/dist/variables/formula/tree.d.ts.map +1 -1
- package/dist/variables/formula/tree.js.map +1 -1
- package/dist/variables/index.d.ts +22 -5
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +67 -17
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/lookup.js +1 -1
- package/dist/variables/lookup.js.map +1 -1
- package/dist/variables/motion.d.ts +46 -0
- package/dist/variables/motion.d.ts.map +1 -0
- package/dist/variables/motion.js +107 -0
- package/dist/variables/motion.js.map +1 -0
- package/dist/variables/paints.d.ts +10 -2
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +24 -10
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +10 -9
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/schema.d.ts +59 -1
- package/dist/variables/schema.d.ts.map +1 -1
- package/dist/variables/schema.js +36 -1
- package/dist/variables/schema.js.map +1 -1
- package/dist/variables/style-bindings.d.ts +23 -0
- package/dist/variables/style-bindings.d.ts.map +1 -0
- package/dist/variables/style-bindings.js +83 -0
- package/dist/variables/style-bindings.js.map +1 -0
- package/package.json +21 -2
- package/src/behaviours/index.ts +1 -0
- package/src/behaviours/kinds.ts +5 -1
- package/src/behaviours/model.ts +2 -2
- package/src/behaviours/schema.ts +19 -2
- package/src/behaviours/spec.ts +5 -0
- package/src/behaviours/transition.ts +46 -0
- package/src/checkpoint.ts +2 -0
- package/src/color/index.ts +15 -24
- package/src/color/management.ts +144 -168
- package/src/color/okhcl.ts +62 -43
- package/src/color/schema.ts +13 -0
- package/src/constraints.ts +2 -1
- package/src/copy.ts +29 -0
- package/src/corners.ts +3 -0
- package/src/css/index.ts +1 -0
- package/src/css/motion.ts +51 -0
- package/src/css/shadows.ts +7 -6
- package/src/css/values.ts +7 -3
- package/src/fields/layout.ts +9 -0
- package/src/font-style.ts +8 -3
- package/src/generated/index.ts +15 -3
- package/src/geometry.ts +13 -1
- package/src/graph.ts +72 -9
- package/src/group-bounds.ts +54 -13
- package/src/hit-test.ts +13 -8
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +71 -12
- package/src/instances/sync.ts +93 -11
- package/src/layout/flow.ts +34 -0
- package/src/layout/index.ts +1 -0
- package/src/layout/keywords.ts +7 -1
- package/src/layout/sizing.ts +30 -0
- package/src/layout/spacing.ts +12 -0
- package/src/mesh/geometry.ts +433 -0
- package/src/mesh/index.ts +3 -0
- package/src/mesh/model.ts +50 -0
- package/src/mesh/schema.ts +81 -0
- package/src/mutation-impact.ts +23 -7
- package/src/node-defaults.ts +22 -5
- package/src/paints/index.ts +2 -0
- package/src/paints/pattern.ts +55 -0
- package/src/paints/shows.ts +9 -0
- package/src/paints/solid.ts +10 -0
- package/src/paints/stills.ts +182 -0
- package/src/plugin-data/field.ts +3 -0
- package/src/plugin-data/fields.ts +15 -2
- package/src/resize.ts +32 -20
- package/src/scaling/index.ts +1 -1
- package/src/scaling/node.ts +43 -6
- package/src/shaders/index.ts +0 -1
- package/src/shaders/model.ts +19 -49
- package/src/shaders/schema.ts +1 -1
- package/src/shape-with-text/document.ts +175 -0
- package/src/shape-with-text/geometry.ts +99 -27
- package/src/shared-style-index.ts +106 -0
- package/src/shared-styles.ts +21 -14
- package/src/slides/drop.ts +94 -0
- package/src/slides/index.ts +125 -0
- package/src/slides/motion.ts +169 -0
- package/src/slides/playback.ts +200 -0
- package/src/slides/types.ts +94 -0
- package/src/text/direction.ts +13 -23
- package/src/text/index.ts +1 -0
- package/src/text/name.ts +39 -0
- package/src/text/references.ts +3 -3
- package/src/thumbnail.ts +52 -0
- package/src/transfer/apply.ts +16 -3
- package/src/transfer/copied.ts +7 -2
- package/src/transfer/dependencies.ts +10 -1
- package/src/transfer/history.ts +2 -0
- package/src/transfer/identity.ts +34 -2
- package/src/transfer/index.ts +6 -0
- package/src/transfer/library.ts +153 -0
- package/src/transfer/nodes.ts +20 -0
- package/src/transfer/plan.ts +96 -10
- package/src/transfer/remove.ts +6 -2
- package/src/transfer/validation.ts +15 -7
- package/src/transfer/variables.ts +8 -2
- package/src/types.ts +114 -10
- package/src/variables/bindings.ts +260 -36
- package/src/variables/easing.ts +282 -0
- package/src/variables/formula/index.ts +33 -14
- package/src/variables/formula/signatures.ts +6 -2
- package/src/variables/formula/tree.ts +3 -3
- package/src/variables/index.ts +90 -37
- package/src/variables/lookup.ts +1 -1
- package/src/variables/motion.ts +151 -0
- package/src/variables/paints.ts +29 -7
- package/src/variables/resolution.ts +11 -5
- package/src/variables/schema.ts +41 -0
- package/src/variables/style-bindings.ts +137 -0
- package/dist/shaders/still.d.ts +0 -18
- package/dist/shaders/still.d.ts.map +0 -1
- package/dist/shaders/still.js +0 -36
- package/dist/shaders/still.js.map +0 -1
- package/src/shaders/still.ts +0 -41
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/variables/index.ts"],"sourcesContent":["import { compact, partition, uniq } from 'es-toolkit/array'\nimport { omit, omitBy } from 'es-toolkit/object'\n\nimport { BLACK } from '../constants'\nimport type { SceneGraph } from '../graph'\nimport { findInstanceAncestor } from '../instances'\nimport { bindingOverrideField, setInstanceOverride } from '../instances/overrides'\nimport { siblingOrderKeys } from '../order-keys'\nimport type {\n Effect,\n SceneNode,\n Variable,\n VariableCollection,\n VariableCollectionMode,\n VariableType,\n VariableValue\n} from '../types'\nimport {\n isNumericVariableBindingField,\n variableBindingOwner,\n assignVariableBindingUnits\n} from './bindings'\nimport {\n BOOLEAN_BINDING_FIELDS,\n effectBindableProperties,\n effectBindingTarget,\n effectBindingVariableType,\n type EffectBindingProperty,\n paintBindingTarget,\n STRING_BINDING_FIELDS,\n TEXT_CONTENT_VARIABLE_TYPES,\n VISIBILITY_VARIABLE_TYPES\n} from './fields'\nimport { applyBoundPaintColor } from './paints'\n\nexport * from './resolution'\n\nexport function addVariable(graph: SceneGraph, variable: Variable): void {\n graph.willChangeVariable(variable.id)\n graph.variables.set(variable.id, variable)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection && !collection.variableIds.includes(variable.id)) {\n graph.willChangeCollection(collection.id)\n collection.variableIds.push(variable.id)\n }\n}\n\nexport function removeVariable(graph: SceneGraph, id: string): void {\n const variable = graph.variables.get(id)\n if (!variable) return\n graph.willChangeVariable(id)\n graph.variables.delete(id)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection) {\n graph.willChangeCollection(collection.id)\n collection.variableIds = collection.variableIds.filter((vid) => vid !== id)\n }\n for (const node of graph.nodes.values()) {\n const hadBinding = Object.values(node.boundVariables).includes(id)\n if (!hadBinding) continue\n graph.willChangeNode(node)\n node.boundVariables = omitBy(node.boundVariables, (varId) => varId === id) as Record<\n string,\n string\n >\n node.variableBindingScales = omitBy(\n node.variableBindingScales,\n (_, field) => !(field in node.boundVariables)\n )\n graph.emitter.emit('node:updated', node.id, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n markBoundVariablesOverrideOnInstance(graph, node.id)\n }\n}\n\nexport function addCollection(graph: SceneGraph, collection: VariableCollection): void {\n graph.willChangeCollection(collection.id)\n graph.variableCollections.set(collection.id, collection)\n}\n\nfunction defaultVariableValue(type: VariableType, value?: VariableValue): VariableValue {\n if (value !== undefined) return value\n if (type === 'COLOR') return { ...BLACK }\n if (type === 'FLOAT') return 0\n if (type === 'BOOLEAN') return false\n return ''\n}\n\nexport function createVariable(\n graph: SceneGraph,\n generateId: () => string,\n name: string,\n type: VariableType,\n collectionId: string,\n value?: VariableValue\n): Variable {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) throw new Error(`Collection \"${collectionId}\" not found`)\n const id = generateId()\n const defaultValue = defaultVariableValue(type, value)\n const valuesByMode: Record<string, VariableValue> = {}\n for (const mode of collection.modes) {\n valuesByMode[mode.modeId] = structuredClone(defaultValue)\n }\n const variable: Variable = {\n id,\n name,\n type,\n collectionId,\n valuesByMode,\n description: '',\n hiddenFromPublishing: false\n }\n addVariable(graph, variable)\n return variable\n}\n\nexport function createCollection(\n graph: SceneGraph,\n generateId: () => string,\n name: string\n): VariableCollection {\n const id = generateId()\n const modeId = generateId()\n const collection: VariableCollection = {\n id,\n name,\n modes: [{ modeId, name: 'Mode 1' }],\n defaultModeId: modeId,\n variableIds: []\n }\n addCollection(graph, collection)\n return collection\n}\n\nexport function removeCollection(graph: SceneGraph, id: string): void {\n const collection = graph.variableCollections.get(id)\n if (collection) {\n for (const varId of Array.from(collection.variableIds)) {\n removeVariable(graph, varId)\n }\n }\n graph.willChangeCollection(id)\n graph.variableCollections.delete(id)\n}\n\nexport function addMode(\n graph: SceneGraph,\n collectionId: string,\n modeId: string,\n name: string,\n sourceMode?: string\n): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n graph.willChangeCollection(collectionId)\n collection.modes.push({ modeId, name })\n const sourceModeId = sourceMode ?? collection.defaultModeId\n for (const varId of collection.variableIds) {\n const variable = graph.variables.get(varId)\n if (!variable) continue\n graph.willChangeVariable(varId)\n variable.valuesByMode[modeId] = structuredClone(\n variable.valuesByMode[sourceModeId] ?? Object.values(variable.valuesByMode)[0]\n )\n }\n}\n\n/** Add a mode with a new ID from `generateId`; undo and redo replay it with `addMode`. */\nexport function createMode(\n graph: SceneGraph,\n generateId: () => string,\n collectionId: string,\n name: string,\n sourceMode?: string\n): string | undefined {\n if (!graph.variableCollections.has(collectionId)) return undefined\n const modeId = generateId()\n addMode(graph, collectionId, modeId, name, sourceMode)\n return modeId\n}\n\nexport function removeMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection || collection.modes.length <= 1) return\n graph.willChangeCollection(collectionId)\n collection.modes = collection.modes.filter((m) => m.modeId !== modeId)\n if (collection.defaultModeId === modeId) {\n collection.defaultModeId = collection.modes[0].modeId\n }\n for (const varId of collection.variableIds) {\n const variable = graph.variables.get(varId)\n if (!variable) continue\n graph.willChangeVariable(varId)\n variable.valuesByMode = omit(variable.valuesByMode, [modeId])\n }\n}\n\nexport function renameMode(\n graph: SceneGraph,\n collectionId: string,\n modeId: string,\n name: string\n): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n const mode = collection.modes.find((m) => m.modeId === modeId)\n if (!mode) return\n graph.willChangeCollection(collectionId)\n mode.name = name\n}\n\nexport function setDefaultMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n if (!collection.modes.some((m) => m.modeId === modeId)) return\n graph.willChangeCollection(collectionId)\n collection.defaultModeId = modeId\n collection.modes = withSortPositionsInOrder(modesDefaultFirst(collection))\n}\n\nfunction withSortPositionsInOrder(modes: VariableCollectionMode[]): VariableCollectionMode[] {\n if (modes.every((mode) => !mode.sortPosition)) {\n return modes\n }\n const positions = siblingOrderKeys(modes.map((mode) => mode.sortPosition))\n return modes.map((mode, index) => ({ ...mode, sortPosition: positions[index] }))\n}\n\n/** Figma has no default-mode field: the first mode is the default. */\nexport function modesDefaultFirst(collection: VariableCollection): VariableCollectionMode[] {\n const [defaults, rest] = partition(collection.modes, (m) => m.modeId === collection.defaultModeId)\n return [...defaults, ...rest]\n}\n\nexport function getVariablesForCollection(graph: SceneGraph, collectionId: string): Variable[] {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return []\n return collection.variableIds\n .map((id) => graph.variables.get(id))\n .filter((v): v is Variable => v !== undefined)\n}\n\nexport function getVariablesByType(graph: SceneGraph, type: VariableType): Variable[] {\n return [...graph.variables.values()].filter((v) => v.type === type)\n}\n\n/** Checks that an effect binding names an effect the node has, a property it binds, and its type. */\nfunction assertEffectBinding(\n effects: readonly Effect[],\n target: { index: number; property: EffectBindingProperty },\n type: VariableType,\n field: string\n): void {\n const effect = effects.at(target.index)\n if (!effect) {\n throw new Error(`Index ${target.index} out of range for effects (length ${effects.length})`)\n }\n if (!effectBindableProperties(effect.type).includes(target.property)) {\n throw new Error(`A ${effect.type} effect has no bindable ${target.property} (\"${field}\")`)\n }\n if (type !== effectBindingVariableType(target.property)) {\n throw new Error(`Cannot bind ${type} variable to effect field \"${field}\"`)\n }\n}\n\n/** Checks a paint binding and drops the list's dead top-level binding it replaces. */\nfunction preparePaintBinding(\n graph: SceneGraph,\n node: SceneNode,\n paint: { list: 'fills' | 'strokes'; index: number },\n variable: Variable,\n field: string\n): void {\n // Color fields require COLOR variable type\n if (variable.type !== 'COLOR') {\n throw new Error(`Cannot bind ${variable.type} variable to color field \"${field}\"`)\n }\n // Validate index is within current array bounds\n const currentLength = node[paint.list].length\n if (paint.index >= currentLength) {\n throw new Error(`Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`)\n }\n // Auto-remove top-level dead binding (e.g. 'fills') when setting indexed binding\n if (paint.list in node.boundVariables) {\n graph.willChangeNode(node)\n node.boundVariables = omit(node.boundVariables, [paint.list])\n }\n}\n\nexport function bindVariable(\n graph: SceneGraph,\n nodeId: string,\n field: string,\n variableId: string\n): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n\n // Validate variable exists\n const variable = graph.variables.get(variableId)\n if (!variable) {\n throw new Error(`Variable \"${variableId}\" not found`)\n }\n\n const paint = paintBindingTarget(field)\n if (paint) preparePaintBinding(graph, node, paint, variable, field)\n\n const effect = effectBindingTarget(field)\n if (effect) assertEffectBinding(node.effects, effect, variable.type, field)\n\n if (isNumericVariableBindingField(field) && variable.type !== 'FLOAT') {\n throw new Error(`Cannot bind ${variable.type} variable to scalar field \"${field}\"`)\n }\n\n const textTypes: readonly VariableType[] =\n field === 'text' ? TEXT_CONTENT_VARIABLE_TYPES : ['STRING']\n if (STRING_BINDING_FIELDS.has(field) && !textTypes.includes(variable.type)) {\n throw new Error(`Cannot bind ${variable.type} variable to string field \"${field}\"`)\n }\n\n if (\n BOOLEAN_BINDING_FIELDS.has(field) &&\n !VISIBILITY_VARIABLE_TYPES.some((type) => type === variable.type)\n ) {\n throw new Error(`Cannot bind ${variable.type} variable to boolean field \"${field}\"`)\n }\n\n const isKnownField =\n isNumericVariableBindingField(field) ||\n STRING_BINDING_FIELDS.has(field) ||\n BOOLEAN_BINDING_FIELDS.has(field) ||\n paint !== null ||\n effect !== null\n\n if (!isKnownField) {\n throw new Error(`Unknown binding field \"${field}\"`)\n }\n\n graph.willChangeNode(node)\n node.boundVariables = { ...node.boundVariables, [field]: variableId }\n assignVariableBindingUnits(graph, node, field)\n markBoundVariablesOverrideOnInstance(graph, nodeId, field)\n graph.emitter.emit('node:updated', nodeId, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n // A paint takes its variable's color, alpha as opacity, when it binds, as Figma's does.\n if (paint) applyBoundPaintColor(graph, node, field)\n}\n\nexport function unbindVariable(graph: SceneGraph, nodeId: string, field: string): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n if (!(field in node.boundVariables)) return\n graph.willChangeNode(node)\n node.boundVariables = omit(node.boundVariables, [field])\n node.variableBindingScales = omit(node.variableBindingScales, [field])\n markBoundVariablesOverrideOnInstance(graph, nodeId, field)\n graph.emitter.emit('node:updated', nodeId, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n}\n\nfunction markBoundVariablesOverrideOnInstance(\n graph: SceneGraph,\n nodeId: string,\n field?: string\n): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n\n const owner = variableBindingOwner(graph, node)\n if (owner.type !== 'INSTANCE') return\n const nearest = findInstanceAncestor(graph, nodeId)\n const instances = uniq(compact([nearest, owner])).filter((x) => x.id !== nodeId)\n for (const instance of instances) graph.willChangeNode(instance)\n if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, 'boundVariables')\n setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, 'boundVariables')\n // Each instance claims the binding it changed alone, so the layer's other bindings keep\n // following their components, as Figma claims only the paint or effect a script edits.\n if (field)\n for (const instance of uniq(compact([nearest, owner])))\n setInstanceOverride(\n instance.instanceOverrides,\n instance.id,\n nodeId,\n bindingOverrideField(field),\n node.boundVariables[field] ?? null\n )\n for (const instance of instances)\n graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })\n}\n\nexport * from './bindings'\nexport * from './corners'\nexport * from './formula'\nexport * from './formula/schema'\nexport * from './lookup'\nexport * from './paints'\nexport * from './schema'\nexport * from './text'\nexport * from './token'\nexport * from './value'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,YAAY,OAAmB,UAA0B;CACvE,MAAM,mBAAmB,SAAS,EAAE;CACpC,MAAM,UAAU,IAAI,SAAS,IAAI,QAAQ;CACzC,MAAM,aAAa,MAAM,oBAAoB,IAAI,SAAS,YAAY;CACtE,IAAI,cAAc,CAAC,WAAW,YAAY,SAAS,SAAS,EAAE,GAAG;EAC/D,MAAM,qBAAqB,WAAW,EAAE;EACxC,WAAW,YAAY,KAAK,SAAS,EAAE;CACzC;AACF;AAEA,SAAgB,eAAe,OAAmB,IAAkB;CAClE,MAAM,WAAW,MAAM,UAAU,IAAI,EAAE;CACvC,IAAI,CAAC,UAAU;CACf,MAAM,mBAAmB,EAAE;CAC3B,MAAM,UAAU,OAAO,EAAE;CACzB,MAAM,aAAa,MAAM,oBAAoB,IAAI,SAAS,YAAY;CACtE,IAAI,YAAY;EACd,MAAM,qBAAqB,WAAW,E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,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, uniq } from 'es-toolkit/array'\nimport { omit, omitBy } from 'es-toolkit/object'\n\nimport { BLACK } from '../constants'\nimport type { SceneGraph } from '../graph'\nimport { findInstanceAncestor } from '../instances'\nimport { bindingOverrideField, setInstanceOverride } from '../instances/overrides'\nimport { siblingOrderKeys } from '../order-keys'\nimport {\n TEXT_BINDABLE_FIELDS,\n type Effect,\n type SceneNode,\n type TextBindableField,\n type Variable,\n type VariableCollection,\n type VariableCollectionMode,\n type VariableType,\n type VariableValue\n} from '../types'\nimport {\n isNumericVariableBindingField,\n variableBindingOwner,\n assignVariableBindingUnits\n} from './bindings'\nimport {\n BOOLEAN_BINDING_FIELDS,\n effectBindableProperties,\n effectBindingTarget,\n effectBindingVariableType,\n type EffectBindingProperty,\n paintBindingTarget,\n STRING_BINDING_FIELDS,\n TEXT_CONTENT_VARIABLE_TYPES,\n VISIBILITY_VARIABLE_TYPES\n} from './fields'\nimport { applyBoundPaintColor } from './paints'\n\nexport * from './resolution'\n\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\nexport function addVariable(graph: SceneGraph, variable: Variable): void {\n graph.willChangeVariable(variable.id)\n graph.variables.set(variable.id, variable)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (!collection) 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 graph.willChangeCollection(collection.id)\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.willChangeVariable(id)\n graph.variables.delete(id)\n const collection = graph.variableCollections.get(variable.collectionId)\n if (collection) {\n graph.willChangeCollection(collection.id)\n collection.variableIds = collection.variableIds.filter((vid) => vid !== id)\n }\n for (const node of graph.nodes.values()) {\n const hadBinding = Object.values(node.boundVariables).includes(id)\n if (!hadBinding) continue\n graph.willChangeNode(node)\n node.boundVariables = omitBy(node.boundVariables, (varId) => varId === id) as Record<\n string,\n string\n >\n node.variableBindingScales = omitBy(\n node.variableBindingScales,\n (_, field) => !(field in node.boundVariables)\n )\n graph.emitter.emit('node:updated', node.id, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n markBoundVariablesOverrideOnInstance(graph, node.id)\n }\n}\n\nexport function addCollection(graph: SceneGraph, collection: VariableCollection): void {\n graph.willChangeCollection(collection.id)\n graph.variableCollections.set(collection.id, collection)\n}\n\n/** The value a new variable of this type starts with, unless it is given one. */\nexport function defaultVariableValue(type: VariableType, value?: VariableValue): VariableValue {\n if (value !== undefined) return value\n if (type === 'COLOR') return { ...BLACK }\n if (type === 'FLOAT' || type === 'TIMING') return 0\n if (type === 'BOOLEAN') return false\n // Figma starts an easing as IN_CUBIC with no numbers, which it shows as Ease in.\n if (type === 'EASING') return { easing: 'IN_CUBIC' }\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.willChangeCollection(id)\n graph.variableCollections.delete(id)\n}\n\nexport function addMode(\n graph: SceneGraph,\n collectionId: string,\n modeId: string,\n name: string,\n sourceMode?: string\n): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n graph.willChangeCollection(collectionId)\n collection.modes.push({ modeId, name })\n const sourceModeId = sourceMode ?? collection.defaultModeId\n for (const varId of collection.variableIds) {\n const variable = graph.variables.get(varId)\n if (!variable) continue\n graph.willChangeVariable(varId)\n variable.valuesByMode[modeId] = structuredClone(\n variable.valuesByMode[sourceModeId] ?? Object.values(variable.valuesByMode)[0]\n )\n }\n}\n\n/** Add a mode with a new ID from `generateId`; undo and redo replay it with `addMode`. */\nexport function createMode(\n graph: SceneGraph,\n generateId: () => string,\n collectionId: string,\n name: string,\n sourceMode?: string\n): string | undefined {\n if (!graph.variableCollections.has(collectionId)) return undefined\n const modeId = generateId()\n addMode(graph, collectionId, modeId, name, sourceMode)\n return modeId\n}\n\nexport function removeMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection || collection.modes.length <= 1) return\n graph.willChangeCollection(collectionId)\n collection.modes = collection.modes.filter((m) => m.modeId !== modeId)\n if (collection.defaultModeId === modeId) {\n collection.defaultModeId = collection.modes[0].modeId\n }\n for (const varId of collection.variableIds) {\n const variable = graph.variables.get(varId)\n if (!variable) continue\n graph.willChangeVariable(varId)\n variable.valuesByMode = omit(variable.valuesByMode, [modeId])\n }\n}\n\nexport function renameMode(\n graph: SceneGraph,\n collectionId: string,\n modeId: string,\n name: string\n): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n const mode = collection.modes.find((m) => m.modeId === modeId)\n if (!mode) return\n graph.willChangeCollection(collectionId)\n mode.name = name\n}\n\nexport function setDefaultMode(graph: SceneGraph, collectionId: string, modeId: string): void {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return\n if (!collection.modes.some((m) => m.modeId === modeId)) return\n graph.willChangeCollection(collectionId)\n collection.defaultModeId = modeId\n collection.modes = withSortPositionsInOrder(modesDefaultFirst(collection))\n}\n\nfunction withSortPositionsInOrder(modes: VariableCollectionMode[]): VariableCollectionMode[] {\n if (modes.every((mode) => !mode.sortPosition)) {\n return modes\n }\n const positions = siblingOrderKeys(modes.map((mode) => mode.sortPosition))\n return modes.map((mode, index) => ({ ...mode, sortPosition: positions[index] }))\n}\n\n/** Figma has no default-mode field: the first mode is the default. */\nexport function modesDefaultFirst(collection: VariableCollection): VariableCollectionMode[] {\n const [defaults, rest] = partition(collection.modes, (m) => m.modeId === collection.defaultModeId)\n return [...defaults, ...rest]\n}\n\nexport function getVariablesForCollection(graph: SceneGraph, collectionId: string): Variable[] {\n const collection = graph.variableCollections.get(collectionId)\n if (!collection) return []\n return collection.variableIds\n .map((id) => graph.variables.get(id))\n .filter((v): v is Variable => v !== undefined)\n}\n\nexport function getVariablesByType(graph: SceneGraph, type: VariableType): Variable[] {\n return liveVariables(graph).filter((v) => v.type === type)\n}\n\n/** Checks that an effect binding names an effect the node has, a property it binds, and its type. */\nfunction assertEffectBinding(\n effects: readonly Effect[],\n target: { index: number; property: EffectBindingProperty },\n type: VariableType,\n field: string\n): void {\n const effect = effects.at(target.index)\n if (!effect) {\n throw new Error(`Index ${target.index} out of range for effects (length ${effects.length})`)\n }\n if (!effectBindableProperties(effect.type).includes(target.property)) {\n throw new Error(`A ${effect.type} effect has no bindable ${target.property} (\"${field}\")`)\n }\n if (type !== effectBindingVariableType(target.property)) {\n throw new Error(`Cannot bind ${type} variable to effect field \"${field}\"`)\n }\n}\n\n/** Checks a paint binding and drops the list's dead top-level binding it replaces. */\nfunction preparePaintBinding(\n graph: SceneGraph,\n node: SceneNode,\n paint: { list: 'fills' | 'strokes'; index: number },\n variable: Variable,\n field: string\n): void {\n // Color fields require COLOR variable type\n if (variable.type !== 'COLOR') {\n throw new Error(`Cannot bind ${variable.type} variable to color field \"${field}\"`)\n }\n // Validate index is within current array bounds\n const currentLength = node[paint.list].length\n if (paint.index >= currentLength) {\n throw new Error(`Index ${paint.index} out of range for ${paint.list} (length ${currentLength})`)\n }\n // Auto-remove top-level dead binding (e.g. 'fills') when setting indexed binding\n if (paint.list in node.boundVariables) {\n graph.willChangeNode(node)\n node.boundVariables = omit(node.boundVariables, [paint.list])\n }\n}\n\nconst TEXT_BINDABLE: ReadonlySet<string> = new Set(TEXT_BINDABLE_FIELDS)\n\nfunction isTextBindableField(field: string): field is TextBindableField {\n return TEXT_BINDABLE.has(field)\n}\n\n/**\n * Checks the variable's type against a scalar, text, boolean, or text style field, and says\n * whether the field is one of those. A text style binds every field a range of characters can,\n * its face by style or by weight.\n */\nfunction assertFieldVariableType(node: SceneNode, field: string, variable: Variable): boolean {\n if (isNumericVariableBindingField(field)) {\n if (variable.type !== 'FLOAT')\n throw new Error(`Cannot bind ${variable.type} variable to scalar field \"${field}\"`)\n return true\n }\n if (STRING_BINDING_FIELDS.has(field)) {\n const types: readonly VariableType[] =\n field === 'text' ? TEXT_CONTENT_VARIABLE_TYPES : ['STRING']\n if (!types.includes(variable.type))\n throw new Error(`Cannot bind ${variable.type} variable to string field \"${field}\"`)\n return true\n }\n if (BOOLEAN_BINDING_FIELDS.has(field)) {\n if (!VISIBILITY_VARIABLE_TYPES.some((type) => type === variable.type))\n throw new Error(`Cannot bind ${variable.type} variable to boolean field \"${field}\"`)\n return true\n }\n if (node.sharedStyleType !== 'TEXT' || !isTextBindableField(field)) return false\n const type: VariableType = field === 'fontStyle' ? 'STRING' : 'FLOAT'\n if (variable.type !== type)\n throw new Error(`Cannot bind ${variable.type} variable to text style field \"${field}\"`)\n return true\n}\n\nexport function bindVariable(\n graph: SceneGraph,\n nodeId: string,\n field: string,\n variableId: string\n): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n\n // Validate variable exists\n const variable = graph.variables.get(variableId)\n if (!variable) {\n throw new Error(`Variable \"${variableId}\" not found`)\n }\n\n const paint = paintBindingTarget(field)\n if (paint) preparePaintBinding(graph, node, paint, variable, field)\n\n const effect = effectBindingTarget(field)\n if (effect) assertEffectBinding(node.effects, effect, variable.type, field)\n\n const isKnownField =\n assertFieldVariableType(node, field, variable) || paint !== null || effect !== null\n\n if (!isKnownField) {\n throw new Error(`Unknown binding field \"${field}\"`)\n }\n\n graph.willChangeNode(node)\n node.boundVariables = { ...node.boundVariables, [field]: variableId }\n assignVariableBindingUnits(graph, node, field)\n markBoundVariablesOverrideOnInstance(graph, nodeId, field)\n graph.emitter.emit('node:updated', nodeId, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n // A paint takes its variable's color, alpha as opacity, when it binds, as Figma's does.\n if (paint) applyBoundPaintColor(graph, node, field)\n}\n\nexport function unbindVariable(graph: SceneGraph, nodeId: string, field: string): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n if (!(field in node.boundVariables)) return\n graph.willChangeNode(node)\n node.boundVariables = omit(node.boundVariables, [field])\n node.variableBindingScales = omit(node.variableBindingScales, [field])\n markBoundVariablesOverrideOnInstance(graph, nodeId, field)\n graph.emitter.emit('node:updated', nodeId, {\n boundVariables: { ...node.boundVariables },\n variableBindingScales: { ...node.variableBindingScales }\n })\n}\n\nfunction markBoundVariablesOverrideOnInstance(\n graph: SceneGraph,\n nodeId: string,\n field?: string\n): void {\n const node = graph.nodes.get(nodeId)\n if (!node) return\n\n const owner = variableBindingOwner(graph, node)\n if (owner.type !== 'INSTANCE') return\n const nearest = findInstanceAncestor(graph, nodeId)\n const instances = uniq(compact([nearest, owner])).filter((x) => x.id !== nodeId)\n for (const instance of instances) graph.willChangeNode(instance)\n if (nearest) setInstanceOverride(nearest.instanceOverrides, nearest.id, nodeId, 'boundVariables')\n setInstanceOverride(owner.instanceOverrides, owner.id, nodeId, 'boundVariables')\n // Each instance claims the binding it changed alone, so the layer's other bindings keep\n // following their components, as Figma claims only the paint or effect a script edits.\n if (field)\n for (const instance of uniq(compact([nearest, owner])))\n setInstanceOverride(\n instance.instanceOverrides,\n instance.id,\n nodeId,\n bindingOverrideField(field),\n node.boundVariables[field] ?? null\n )\n for (const instance of instances)\n graph.updateNode(instance.id, { instanceOverrides: instance.instanceOverrides })\n}\n\nexport * from './bindings'\nexport * from './style-bindings'\nexport * from './corners'\nexport * from './easing'\nexport * from './formula'\nexport * from './formula/schema'\nexport * from './lookup'\nexport * from './motion'\nexport * from './paints'\nexport * from './schema'\nexport * from './text'\nexport * from './token'\nexport * from './value'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,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;A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;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,KAAK,QAAQ,CAAC,SAAS,KAAK,CAAC,CAAC,GACnD,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"}
|
package/dist/variables/lookup.js
CHANGED
|
@@ -17,7 +17,7 @@ function lookupCollection(graph, ref) {
|
|
|
17
17
|
kind: "found",
|
|
18
18
|
value: byId
|
|
19
19
|
};
|
|
20
|
-
return byName(graph.
|
|
20
|
+
return byName(graph.getLiveCollections(), ref);
|
|
21
21
|
}
|
|
22
22
|
/** The mode of `collection` with this ID or, failing that, the one with this exact name. */
|
|
23
23
|
function lookupMode(collection, ref) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lookup.js","names":[],"sources":["../../src/variables/lookup.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { VariableCollection, VariableCollectionMode } from '../types'\n\n/** What a reference by ID or name finds: one match, none, or several that share the name. */\nexport type NamedLookup<T> =\n | { kind: 'found'; value: T }\n | { kind: 'missing' }\n | { kind: 'ambiguous'; matches: T[] }\n\nfunction byName<T extends { name: string }>(items: Iterable<T>, name: string): NamedLookup<T> {\n const matches = [...items].filter((item) => item.name === name)\n if (matches.length === 1) return { kind: 'found', value: matches[0] }\n return matches.length === 0 ? { kind: 'missing' } : { kind: 'ambiguous', matches }\n}\n\n/** The collection with this ID or, failing that, the one with this exact name. */\nexport function lookupCollection(graph: SceneGraph, ref: string): NamedLookup<VariableCollection> {\n const byId = graph.variableCollections.get(ref)\n if (byId) return { kind: 'found', value: byId }\n return byName(graph.
|
|
1
|
+
{"version":3,"file":"lookup.js","names":[],"sources":["../../src/variables/lookup.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { VariableCollection, VariableCollectionMode } from '../types'\n\n/** What a reference by ID or name finds: one match, none, or several that share the name. */\nexport type NamedLookup<T> =\n | { kind: 'found'; value: T }\n | { kind: 'missing' }\n | { kind: 'ambiguous'; matches: T[] }\n\nfunction byName<T extends { name: string }>(items: Iterable<T>, name: string): NamedLookup<T> {\n const matches = [...items].filter((item) => item.name === name)\n if (matches.length === 1) return { kind: 'found', value: matches[0] }\n return matches.length === 0 ? { kind: 'missing' } : { kind: 'ambiguous', matches }\n}\n\n/** The collection with this ID or, failing that, the one with this exact name. */\nexport function lookupCollection(graph: SceneGraph, ref: string): NamedLookup<VariableCollection> {\n const byId = graph.variableCollections.get(ref)\n if (byId) return { kind: 'found', value: byId }\n return byName(graph.getLiveCollections(), ref)\n}\n\n/** The mode of `collection` with this ID or, failing that, the one with this exact name. */\nexport function lookupMode(\n collection: VariableCollection,\n ref: string\n): NamedLookup<VariableCollectionMode> {\n const byId = collection.modes.find((mode) => mode.modeId === ref)\n if (byId) return { kind: 'found', value: byId }\n return byName(collection.modes, ref)\n}\n"],"mappings":";AASA,SAAS,OAAmC,OAAoB,MAA8B;CAC5F,MAAM,UAAU,CAAC,GAAG,KAAK,CAAC,CAAC,QAAQ,SAAS,KAAK,SAAS,IAAI;CAC9D,IAAI,QAAQ,WAAW,GAAG,OAAO;EAAE,MAAM;EAAS,OAAO,QAAQ;CAAG;CACpE,OAAO,QAAQ,WAAW,IAAI,EAAE,MAAM,UAAU,IAAI;EAAE,MAAM;EAAa;CAAQ;AACnF;;AAGA,SAAgB,iBAAiB,OAAmB,KAA8C;CAChG,MAAM,OAAO,MAAM,oBAAoB,IAAI,GAAG;CAC9C,IAAI,MAAM,OAAO;EAAE,MAAM;EAAS,OAAO;CAAK;CAC9C,OAAO,OAAO,MAAM,mBAAmB,GAAG,GAAG;AAC/C;;AAGA,SAAgB,WACd,YACA,KACqC;CACrC,MAAM,OAAO,WAAW,MAAM,MAAM,SAAS,KAAK,WAAW,GAAG;CAChE,IAAI,MAAM,OAAO;EAAE,MAAM;EAAS,OAAO;CAAK;CAC9C,OAAO,OAAO,WAAW,OAAO,GAAG;AACrC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { Easing, Variable } from "../types.js";
|
|
3
|
+
import { MotionSpec } from "./schema.js";
|
|
4
|
+
//#region src/variables/motion.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A duration in seconds and an easing, each its own value or, while the variable exists, a TIMING
|
|
7
|
+
* or EASING variable's, as Figma keeps a transition's duration and easing beside the variables
|
|
8
|
+
* they follow.
|
|
9
|
+
*/
|
|
10
|
+
interface Motion {
|
|
11
|
+
duration: number;
|
|
12
|
+
easing: Easing;
|
|
13
|
+
durationVariableId?: string;
|
|
14
|
+
easingVariableId?: string;
|
|
15
|
+
}
|
|
16
|
+
/** What a motion plays: its duration and easing, with variables read. */
|
|
17
|
+
interface ResolvedMotion {
|
|
18
|
+
duration: number;
|
|
19
|
+
easing: Easing;
|
|
20
|
+
}
|
|
21
|
+
/** A live variable of `type` by its ID, or undefined when it is gone or holds something else. */
|
|
22
|
+
declare function liveVariable(graph: SceneGraph, id: string | undefined, type: Variable['type']): Variable | undefined;
|
|
23
|
+
/** A TIMING variable's value for `nodeId`, or `fallback` when there is none to read. */
|
|
24
|
+
declare function resolveTiming(graph: SceneGraph, variableId: string | undefined, nodeId: string, fallback: number): number;
|
|
25
|
+
/**
|
|
26
|
+
* The motion `nodeId` plays: each variable read in the modes the node is in, and the motion's own
|
|
27
|
+
* value where the variable is gone or gives nothing.
|
|
28
|
+
*/
|
|
29
|
+
declare function resolveMotion(graph: SceneGraph, motion: Motion, nodeId: string): ResolvedMotion;
|
|
30
|
+
/** The variables a motion follows. */
|
|
31
|
+
declare function motionVariableIds(motion: Motion): string[];
|
|
32
|
+
/**
|
|
33
|
+
* The motion following other variables, as a copy into another document or a file's own IDs name
|
|
34
|
+
* them; a variable `map` gives nothing for is let go, keeping the value.
|
|
35
|
+
*/
|
|
36
|
+
declare function mapMotionVariables<T extends Motion>(motion: T, map: (variableId: string) => string | undefined): T;
|
|
37
|
+
/**
|
|
38
|
+
* `base` with what a spec sets: values as given, and variables, which keep their value in their
|
|
39
|
+
* collection's default mode beside them, for when they are gone.
|
|
40
|
+
*/
|
|
41
|
+
declare function motionFromSpec<T extends Motion>(graph: SceneGraph, spec: MotionSpec, base: T): T;
|
|
42
|
+
/** A motion as a spec reads it: variables by name, while they exist, else values. */
|
|
43
|
+
declare function motionToSpec(graph: SceneGraph, motion: Motion): Required<MotionSpec>;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { Motion, ResolvedMotion, liveVariable, mapMotionVariables, motionFromSpec, motionToSpec, motionVariableIds, resolveMotion, resolveTiming };
|
|
46
|
+
//# sourceMappingURL=motion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"motion.d.ts","names":[],"sources":["../../src/variables/motion.ts"],"mappings":";;;;;;;;;UAaiB;EACf;EACA,QAAQ;EACR;EACA;;;UAIe;EACf;EACA,QAAQ;;;iBAIM,aACd,OAAO,YACP,wBACA,MAAM,mBACL;;iBAOa,cACd,OAAO,YACP,gCACA,gBACA;;;;;iBAWc,cAAc,OAAO,YAAY,QAAQ,QAAQ,iBAAiB;;iBAUlE,kBAAkB,QAAQ;;;;;iBAQ1B,mBAAmB,UAAU,QAC3C,QAAQ,GACR,MAAM,4CACL;;;;;iBAoCa,eAAe,UAAU,QAAQ,OAAO,YAAY,MAAM,YAAY,MAAM,IAAI;;iBAiChF,aAAa,OAAO,YAAY,QAAQ,SAAS,SAAS"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { EASING_CURVES } from "../types.js";
|
|
2
|
+
import { easingPreset, isEasing } from "./easing.js";
|
|
3
|
+
import { parseCSSEasing } from "../css/motion.js";
|
|
4
|
+
import { compact } from "es-toolkit/array";
|
|
5
|
+
//#region src/variables/motion.ts
|
|
6
|
+
/** A live variable of `type` by its ID, or undefined when it is gone or holds something else. */
|
|
7
|
+
function liveVariable(graph, id, type) {
|
|
8
|
+
const variable = id ? graph.variables.get(id) : void 0;
|
|
9
|
+
return variable?.type === type ? variable : void 0;
|
|
10
|
+
}
|
|
11
|
+
/** A TIMING variable's value for `nodeId`, or `fallback` when there is none to read. */
|
|
12
|
+
function resolveTiming(graph, variableId, nodeId, fallback) {
|
|
13
|
+
const timing = liveVariable(graph, variableId, "TIMING");
|
|
14
|
+
const value = timing && graph.resolveVariableForNode(nodeId, timing.id);
|
|
15
|
+
return typeof value === "number" && value >= 0 ? value : fallback;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The motion `nodeId` plays: each variable read in the modes the node is in, and the motion's own
|
|
19
|
+
* value where the variable is gone or gives nothing.
|
|
20
|
+
*/
|
|
21
|
+
function resolveMotion(graph, motion, nodeId) {
|
|
22
|
+
const easing = liveVariable(graph, motion.easingVariableId, "EASING");
|
|
23
|
+
const curve = easing && graph.resolveVariableForNode(nodeId, easing.id);
|
|
24
|
+
return {
|
|
25
|
+
duration: resolveTiming(graph, motion.durationVariableId, nodeId, motion.duration),
|
|
26
|
+
easing: isEasing(curve) ? curve : motion.easing
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** The variables a motion follows. */
|
|
30
|
+
function motionVariableIds(motion) {
|
|
31
|
+
return compact([motion.durationVariableId, motion.easingVariableId]);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The motion following other variables, as a copy into another document or a file's own IDs name
|
|
35
|
+
* them; a variable `map` gives nothing for is let go, keeping the value.
|
|
36
|
+
*/
|
|
37
|
+
function mapMotionVariables(motion, map) {
|
|
38
|
+
const { durationVariableId, easingVariableId, ...values } = motion;
|
|
39
|
+
const mapped = structuredClone(values);
|
|
40
|
+
const duration = durationVariableId === void 0 ? void 0 : map(durationVariableId);
|
|
41
|
+
const easing = easingVariableId === void 0 ? void 0 : map(easingVariableId);
|
|
42
|
+
if (duration !== void 0) mapped.durationVariableId = duration;
|
|
43
|
+
if (easing !== void 0) mapped.easingVariableId = easing;
|
|
44
|
+
return mapped;
|
|
45
|
+
}
|
|
46
|
+
/** A live variable of `type`, by its ID or its name; a spec names one either way. */
|
|
47
|
+
function namedVariable(graph, name, type) {
|
|
48
|
+
const variable = graph.variables.get(name) ?? graph.getLiveVariables().find((item) => item.name === name);
|
|
49
|
+
if (!variable || variable.deleted) throw new Error(`No variable named "${name}"`);
|
|
50
|
+
if (variable.type !== type) throw new Error(`"${variable.name}" holds ${variable.type}; this needs a ${type} variable`);
|
|
51
|
+
return variable;
|
|
52
|
+
}
|
|
53
|
+
function isEasingCurve(name) {
|
|
54
|
+
return EASING_CURVES.includes(name);
|
|
55
|
+
}
|
|
56
|
+
function easingOfSpec(easing) {
|
|
57
|
+
if (isEasingCurve(easing)) return easingPreset(easing);
|
|
58
|
+
const parsed = parseCSSEasing(easing);
|
|
59
|
+
if (!parsed) throw new Error(`"${easing}" is not an easing: use a CSS easing or a name such as OUT_CUBIC`);
|
|
60
|
+
return parsed;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* `base` with what a spec sets: values as given, and variables, which keep their value in their
|
|
64
|
+
* collection's default mode beside them, for when they are gone.
|
|
65
|
+
*/
|
|
66
|
+
function motionFromSpec(graph, spec, base) {
|
|
67
|
+
const motion = structuredClone(base);
|
|
68
|
+
if (typeof spec.duration === "number") {
|
|
69
|
+
motion.duration = spec.duration;
|
|
70
|
+
delete motion.durationVariableId;
|
|
71
|
+
} else if (spec.duration) {
|
|
72
|
+
const variable = namedVariable(graph, spec.duration.variable, "TIMING");
|
|
73
|
+
const value = graph.resolveVariable(variable.id);
|
|
74
|
+
motion.durationVariableId = variable.id;
|
|
75
|
+
if (typeof value === "number") motion.duration = value;
|
|
76
|
+
}
|
|
77
|
+
if (typeof spec.easing === "string") {
|
|
78
|
+
motion.easing = easingOfSpec(spec.easing);
|
|
79
|
+
delete motion.easingVariableId;
|
|
80
|
+
} else if (spec.easing && "variable" in spec.easing) {
|
|
81
|
+
const variable = namedVariable(graph, spec.easing.variable, "EASING");
|
|
82
|
+
const value = graph.resolveVariable(variable.id);
|
|
83
|
+
motion.easingVariableId = variable.id;
|
|
84
|
+
if (isEasing(value)) motion.easing = value;
|
|
85
|
+
} else if (spec.easing) {
|
|
86
|
+
motion.easing = structuredClone(spec.easing);
|
|
87
|
+
delete motion.easingVariableId;
|
|
88
|
+
}
|
|
89
|
+
return motion;
|
|
90
|
+
}
|
|
91
|
+
/** How a spec names a variable: by its name, or by its ID where another live variable shares it. */
|
|
92
|
+
function specReference(graph, variable) {
|
|
93
|
+
return { variable: graph.getLiveVariables().filter((item) => item.name === variable.name).length === 1 ? variable.name : variable.id };
|
|
94
|
+
}
|
|
95
|
+
/** A motion as a spec reads it: variables by name, while they exist, else values. */
|
|
96
|
+
function motionToSpec(graph, motion) {
|
|
97
|
+
const timing = liveVariable(graph, motion.durationVariableId, "TIMING");
|
|
98
|
+
const easing = liveVariable(graph, motion.easingVariableId, "EASING");
|
|
99
|
+
return {
|
|
100
|
+
duration: timing ? specReference(graph, timing) : motion.duration,
|
|
101
|
+
easing: easing ? specReference(graph, easing) : structuredClone(motion.easing)
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
export { liveVariable, mapMotionVariables, motionFromSpec, motionToSpec, motionVariableIds, resolveMotion, resolveTiming };
|
|
106
|
+
|
|
107
|
+
//# sourceMappingURL=motion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"motion.js","names":[],"sources":["../../src/variables/motion.ts"],"sourcesContent":["import { compact } from 'es-toolkit/array'\n\nimport { parseCSSEasing } from '../css/motion'\nimport type { SceneGraph } from '../graph'\nimport { EASING_CURVES, type Easing, type EasingCurve, type Variable } from '../types'\nimport { easingPreset, isEasing } from './easing'\nimport type { MotionSpec } from './schema'\n\n/**\n * A duration in seconds and an easing, each its own value or, while the variable exists, a TIMING\n * or EASING variable's, as Figma keeps a transition's duration and easing beside the variables\n * they follow.\n */\nexport interface Motion {\n duration: number\n easing: Easing\n durationVariableId?: string\n easingVariableId?: string\n}\n\n/** What a motion plays: its duration and easing, with variables read. */\nexport interface ResolvedMotion {\n duration: number\n easing: Easing\n}\n\n/** A live variable of `type` by its ID, or undefined when it is gone or holds something else. */\nexport function liveVariable(\n graph: SceneGraph,\n id: string | undefined,\n type: Variable['type']\n): Variable | undefined {\n const variable = id ? graph.variables.get(id) : undefined\n // A deleted variable the document keeps still resolves for the bindings it had.\n return variable?.type === type ? variable : undefined\n}\n\n/** A TIMING variable's value for `nodeId`, or `fallback` when there is none to read. */\nexport function resolveTiming(\n graph: SceneGraph,\n variableId: string | undefined,\n nodeId: string,\n fallback: number\n): number {\n const timing = liveVariable(graph, variableId, 'TIMING')\n const value = timing && graph.resolveVariableForNode(nodeId, timing.id)\n return typeof value === 'number' && value >= 0 ? value : fallback\n}\n\n/**\n * The motion `nodeId` plays: each variable read in the modes the node is in, and the motion's own\n * value where the variable is gone or gives nothing.\n */\nexport function resolveMotion(graph: SceneGraph, motion: Motion, nodeId: string): ResolvedMotion {\n const easing = liveVariable(graph, motion.easingVariableId, 'EASING')\n const curve = easing && graph.resolveVariableForNode(nodeId, easing.id)\n return {\n duration: resolveTiming(graph, motion.durationVariableId, nodeId, motion.duration),\n easing: isEasing(curve) ? curve : motion.easing\n }\n}\n\n/** The variables a motion follows. */\nexport function motionVariableIds(motion: Motion): string[] {\n return compact([motion.durationVariableId, motion.easingVariableId])\n}\n\n/**\n * The motion following other variables, as a copy into another document or a file's own IDs name\n * them; a variable `map` gives nothing for is let go, keeping the value.\n */\nexport function mapMotionVariables<T extends Motion>(\n motion: T,\n map: (variableId: string) => string | undefined\n): T {\n const { durationVariableId, easingVariableId, ...values } = motion\n const mapped = structuredClone(values) as T\n const duration = durationVariableId === undefined ? undefined : map(durationVariableId)\n const easing = easingVariableId === undefined ? undefined : map(easingVariableId)\n if (duration !== undefined) mapped.durationVariableId = duration\n if (easing !== undefined) mapped.easingVariableId = easing\n return mapped\n}\n\n/** A live variable of `type`, by its ID or its name; a spec names one either way. */\nfunction namedVariable(graph: SceneGraph, name: string, type: Variable['type']): Variable {\n const variable =\n graph.variables.get(name) ?? graph.getLiveVariables().find((item) => item.name === name)\n if (!variable || variable.deleted) throw new Error(`No variable named \"${name}\"`)\n if (variable.type !== type)\n throw new Error(`\"${variable.name}\" holds ${variable.type}; this needs a ${type} variable`)\n return variable\n}\n\nfunction isEasingCurve(name: string): name is EasingCurve {\n return (EASING_CURVES as readonly string[]).includes(name)\n}\n\nfunction easingOfSpec(easing: string): Easing {\n if (isEasingCurve(easing)) return easingPreset(easing)\n const parsed = parseCSSEasing(easing)\n if (!parsed)\n throw new Error(`\"${easing}\" is not an easing: use a CSS easing or a name such as OUT_CUBIC`)\n return parsed\n}\n\n/**\n * `base` with what a spec sets: values as given, and variables, which keep their value in their\n * collection's default mode beside them, for when they are gone.\n */\nexport function motionFromSpec<T extends Motion>(graph: SceneGraph, spec: MotionSpec, base: T): T {\n const motion = structuredClone(base)\n if (typeof spec.duration === 'number') {\n motion.duration = spec.duration\n delete motion.durationVariableId\n } else if (spec.duration) {\n const variable = namedVariable(graph, spec.duration.variable, 'TIMING')\n const value = graph.resolveVariable(variable.id)\n motion.durationVariableId = variable.id\n if (typeof value === 'number') motion.duration = value\n }\n if (typeof spec.easing === 'string') {\n motion.easing = easingOfSpec(spec.easing)\n delete motion.easingVariableId\n } else if (spec.easing && 'variable' in spec.easing) {\n const variable = namedVariable(graph, spec.easing.variable, 'EASING')\n const value = graph.resolveVariable(variable.id)\n motion.easingVariableId = variable.id\n if (isEasing(value)) motion.easing = value\n } else if (spec.easing) {\n motion.easing = structuredClone(spec.easing)\n delete motion.easingVariableId\n }\n return motion\n}\n\n/** How a spec names a variable: by its name, or by its ID where another live variable shares it. */\nfunction specReference(graph: SceneGraph, variable: Variable): { variable: string } {\n const named = graph.getLiveVariables().filter((item) => item.name === variable.name)\n return { variable: named.length === 1 ? variable.name : variable.id }\n}\n\n/** A motion as a spec reads it: variables by name, while they exist, else values. */\nexport function motionToSpec(graph: SceneGraph, motion: Motion): Required<MotionSpec> {\n const timing = liveVariable(graph, motion.durationVariableId, 'TIMING')\n const easing = liveVariable(graph, motion.easingVariableId, 'EASING')\n return {\n duration: timing ? specReference(graph, timing) : motion.duration,\n easing: easing ? specReference(graph, easing) : structuredClone(motion.easing)\n }\n}\n"],"mappings":";;;;;;AA2BA,SAAgB,aACd,OACA,IACA,MACsB;CACtB,MAAM,WAAW,KAAK,MAAM,UAAU,IAAI,EAAE,IAAI,KAAA;CAEhD,OAAO,UAAU,SAAS,OAAO,WAAW,KAAA;AAC9C;;AAGA,SAAgB,cACd,OACA,YACA,QACA,UACQ;CACR,MAAM,SAAS,aAAa,OAAO,YAAY,QAAQ;CACvD,MAAM,QAAQ,UAAU,MAAM,uBAAuB,QAAQ,OAAO,EAAE;CACtE,OAAO,OAAO,UAAU,YAAY,SAAS,IAAI,QAAQ;AAC3D;;;;;AAMA,SAAgB,cAAc,OAAmB,QAAgB,QAAgC;CAC/F,MAAM,SAAS,aAAa,OAAO,OAAO,kBAAkB,QAAQ;CACpE,MAAM,QAAQ,UAAU,MAAM,uBAAuB,QAAQ,OAAO,EAAE;CACtE,OAAO;EACL,UAAU,cAAc,OAAO,OAAO,oBAAoB,QAAQ,OAAO,QAAQ;EACjF,QAAQ,SAAS,KAAK,IAAI,QAAQ,OAAO;CAC3C;AACF;;AAGA,SAAgB,kBAAkB,QAA0B;CAC1D,OAAO,QAAQ,CAAC,OAAO,oBAAoB,OAAO,gBAAgB,CAAC;AACrE;;;;;AAMA,SAAgB,mBACd,QACA,KACG;CACH,MAAM,EAAE,oBAAoB,kBAAkB,GAAG,WAAW;CAC5D,MAAM,SAAS,gBAA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U;EACnC,OAAO,SAAS,aAAa,KAAK,MAAM;EACxC,OAAO,OAAO;CAChB,OAAO,IAAI,KAAK,UAAU,cAAc,KAAK,QAAQ;EACnD,MAAM,WAAW,cAAc,OAAO,KAAK,OAAO,UAAU,QAAQ;EACpE,MAAM,QAAQ,MAAM,gBAAgB,SAAS,EAAE;EAC/C,OAAO,mBAAmB,SAAS;EACnC,IAAI,SAAS,KAAK,GAAG,OAAO,SAAS;CACvC,OAAO,IAAI,KAAK,QAAQ;EACtB,OAAO,SAAS,gBAAgB,KAAK,MAAM;EAC3C,OAAO,OAAO;CAChB;CACA,OAAO;AACT;;AAGA,SAAS,cAAc,OAAmB,UAA0C;CAElF,OAAO,EAAE,UADK,MAAM,iBAAiB,CAAC,CAAC,QAAQ,SAAS,KAAK,SAAS,SAAS,IACxD,CAAC,CAAC,WAAW,IAAI,SAAS,OAAO,SAAS,GAAG;AACtE;;AAGA,SAAgB,aAAa,OAAmB,QAAsC;CACpF,MAAM,SAAS,aAAa,OAAO,OAAO,oBAAoB,QAAQ;CACtE,MAAM,SAAS,aAAa,OAAO,OAAO,kBAAkB,QAAQ;CACpE,OAAO;EACL,UAAU,SAAS,cAAc,OAAO,MAAM,IAAI,OAAO;EACzD,QAAQ,SAAS,cAAc,OAAO,MAAM,IAAI,gBAAgB,OAAO,MAAM;CAC/E;AACF"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Color } from "../primitives/index.js";
|
|
2
2
|
import { SceneGraph } from "../graph.js";
|
|
3
|
-
import { SceneNode } from "../types.js";
|
|
3
|
+
import { CharacterStyleOverride, Fill, SceneNode } from "../types.js";
|
|
4
4
|
import { PaintBindingList } from "./fields.js";
|
|
5
5
|
//#region src/variables/paints.d.ts
|
|
6
6
|
/**
|
|
@@ -40,6 +40,8 @@ declare function captureAppliedBoundPaints(graph: SceneGraph, nodeIds: Iterable<
|
|
|
40
40
|
* layer's id to its copy's.
|
|
41
41
|
*/
|
|
42
42
|
declare function keepPastedBoundPaints(graph: SceneGraph, copies: ReadonlyMap<string, string>, applied: AppliedBoundPaints): void;
|
|
43
|
+
/** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */
|
|
44
|
+
declare function applyVariableColor(paint: Fill, color: Color): void;
|
|
43
45
|
type PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>;
|
|
44
46
|
/**
|
|
45
47
|
* Whether `node` is an instance, or a layer an instance copies from its component, with a paint
|
|
@@ -47,6 +49,12 @@ type PaintLists = Pick<Partial<SceneNode>, 'fills' | 'strokes'>;
|
|
|
47
49
|
* refreshing them is no edit.
|
|
48
50
|
*/
|
|
49
51
|
declare function copiesComponentPaints(graph: SceneGraph, node: SceneNode, list: PaintBindingList): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* A range's own fills with each bound one showing the colour its variable gives the text now, or
|
|
54
|
+
* null when every one does already. A range keeps no record of the colours it was last given, so
|
|
55
|
+
* an edit to a bound range paint lasts until the next refresh of the text's bindings.
|
|
56
|
+
*/
|
|
57
|
+
declare function refreshedRunFills(graph: SceneGraph, nodeId: string, style: CharacterStyleOverride): Fill[] | null;
|
|
50
58
|
/**
|
|
51
59
|
* Refreshes a layer's bound paints: each takes its variable's color for the layer, alpha as
|
|
52
60
|
* opacity, once that color differs from the one last applied, discarding edits made since, as
|
|
@@ -84,5 +92,5 @@ declare function refreshMovedBoundPaints(graph: SceneGraph, nodeId: string, prev
|
|
|
84
92
|
*/
|
|
85
93
|
declare function keepCopyBoundPaints(graph: SceneGraph, target: SceneNode, updates: Partial<SceneNode>): void;
|
|
86
94
|
//#endregion
|
|
87
|
-
export { AppliedBoundPaints, AppliedColor, applyBoundPaintColor, applyBoundPaintColorsIn, captureAppliedBoundPaints, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, keepPastedBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints };
|
|
95
|
+
export { AppliedBoundPaints, AppliedColor, applyBoundPaintColor, applyBoundPaintColorsIn, applyVariableColor, captureAppliedBoundPaints, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, keepPastedBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints, refreshedRunFills };
|
|
88
96
|
//# 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":";;;;;;;;;;;;;;UAqBiB;WACN;WACA,OAAO;;WAEP,OAAO,SAAS;;;iBAyBX,eAAe,MAAM;;;;;iBAsDrB,uBAAuB,QAAQ,WAAW,MAAM;;KAMpD,qBAAqB,oBAAoB,oBAAoB;;;;;iBAMzD,0BACd,OAAO,YACP,SAAS,mBACR;;;;;;;;iBAiBa,sBACd,OAAO,YACP,QAAQ,6BACR,SAAS;;iBA8BK,mBAAmB,OAAO,MAAM,OAAO;KASlD,aAAa,KAAK,QAAQ;;;;;;iBAOf,sBACd,OAAO,YACP,MAAM,WACN,MAAM;;;;;;iBA0DQ,kBACd,OAAO,YACP,gBACA,OAAO,yBACN;;;;;;iBAqBa,mBAAmB,OAAO,YAAY,MAAM;;iBAK5C,qBAAqB,OAAO,YAAY,MAAM,WAAW;;;;;;iBASzD,wBAAwB,OAAO,YAAY;;iBAM3C,qBAAqB,OAAO,YAAY;;;;;iBAQxC,yBACd,OAAO,YACP,MAAM,WACN,SAAS;;;;;;;iBAeK,wBACd,OAAO,YACP,gBACA;;;;;;;;iBAgDc,oBACd,OAAO,YACP,QAAQ,WACR,SAAS,QAAQ"}
|
package/dist/variables/paints.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { paintBindingTarget } from "./fields.js";
|
|
2
2
|
import { hasInstanceOverride } from "../instances/overrides.js";
|
|
3
3
|
import { copyFills, copyStrokes } from "../copy.js";
|
|
4
|
+
import { solidPaintColor } from "../paints/solid.js";
|
|
4
5
|
import { collectionsResolving, getNodeExplicitVariableModeId } from "./resolution.js";
|
|
5
6
|
import { subtreeNodes } from "./subtree.js";
|
|
6
7
|
import { isEqual } from "es-toolkit/predicate";
|
|
@@ -111,12 +112,8 @@ function recordApplied(node, bound, color, modes) {
|
|
|
111
112
|
});
|
|
112
113
|
}
|
|
113
114
|
/** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */
|
|
114
|
-
function
|
|
115
|
-
paint
|
|
116
|
-
...color,
|
|
117
|
-
a: 1
|
|
118
|
-
};
|
|
119
|
-
paint.opacity = color.a;
|
|
115
|
+
function applyVariableColor(paint, color) {
|
|
116
|
+
Object.assign(paint, solidPaintColor(color));
|
|
120
117
|
}
|
|
121
118
|
/** Whether `paint` already shows `color` as a bound paint stores it. */
|
|
122
119
|
function showsColor(paint, color) {
|
|
@@ -163,10 +160,10 @@ function refresh(graph, node, forced) {
|
|
|
163
160
|
if (!forced(bound.field) && !stale || showsColor(paint, color)) continue;
|
|
164
161
|
if (bound.list === "fills") {
|
|
165
162
|
fills ??= copyFills(node.fills);
|
|
166
|
-
|
|
163
|
+
applyVariableColor(fills[bound.index], color);
|
|
167
164
|
} else {
|
|
168
165
|
strokes ??= copyStrokes(node.strokes);
|
|
169
|
-
|
|
166
|
+
applyVariableColor(strokes[bound.index], color);
|
|
170
167
|
}
|
|
171
168
|
}
|
|
172
169
|
const changes = {};
|
|
@@ -174,6 +171,23 @@ function refresh(graph, node, forced) {
|
|
|
174
171
|
if (strokes) changes.strokes = strokes;
|
|
175
172
|
if (fills || strokes) writePaints(graph, node, changes);
|
|
176
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* A range's own fills with each bound one showing the colour its variable gives the text now, or
|
|
176
|
+
* null when every one does already. A range keeps no record of the colours it was last given, so
|
|
177
|
+
* an edit to a bound range paint lasts until the next refresh of the text's bindings.
|
|
178
|
+
*/
|
|
179
|
+
function refreshedRunFills(graph, nodeId, style) {
|
|
180
|
+
let fills;
|
|
181
|
+
for (const [field, variableId] of Object.entries(style.boundVariables ?? {})) {
|
|
182
|
+
const target = paintBindingTarget(field);
|
|
183
|
+
const paint = target?.list === "fills" ? style.fills?.at(target.index) : void 0;
|
|
184
|
+
const color = paint && variableId && graph.resolveColorVariableForNode(nodeId, variableId);
|
|
185
|
+
if (!target || !paint || !color || showsColor(paint, color)) continue;
|
|
186
|
+
fills ??= copyFills(style.fills ?? []);
|
|
187
|
+
applyVariableColor(fills[target.index], color);
|
|
188
|
+
}
|
|
189
|
+
return fills ?? null;
|
|
190
|
+
}
|
|
177
191
|
const NONE_FORCED = () => false;
|
|
178
192
|
const ALL_FORCED = () => true;
|
|
179
193
|
/**
|
|
@@ -271,12 +285,12 @@ function keepCopyBoundPaints(graph, target, updates) {
|
|
|
271
285
|
const color = graph.resolveColorVariableForNode(target.id, variableId);
|
|
272
286
|
if (!color) return;
|
|
273
287
|
const modes = explicitModes(graph, target, variableId);
|
|
274
|
-
if (!keeps || staleApplied(target, bound, own, color, modes))
|
|
288
|
+
if (!keeps || staleApplied(target, bound, own, color, modes)) applyVariableColor(paint, color);
|
|
275
289
|
recordApplied(target, bound, color, modes);
|
|
276
290
|
});
|
|
277
291
|
}
|
|
278
292
|
}
|
|
279
293
|
//#endregion
|
|
280
|
-
export { applyBoundPaintColor, applyBoundPaintColorsIn, captureAppliedBoundPaints, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, keepPastedBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints };
|
|
294
|
+
export { applyBoundPaintColor, applyBoundPaintColorsIn, applyVariableColor, captureAppliedBoundPaints, copiesComponentPaints, copyAppliedBoundPaints, hasBoundPaints, keepCopyBoundPaints, keepPastedBoundPaints, recordWrittenBoundPaints, refreshBoundPaints, refreshBoundPaintsIn, refreshMovedBoundPaints, refreshedRunFills };
|
|
281
295
|
|
|
282
296
|
//# sourceMappingURL=paints.js.map
|
|
@@ -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/** 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 * 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(pa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,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 { solidPaintColor } from '../paints/solid'\nimport type { Color } from '../primitives'\nimport type { CharacterStyleOverride, 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/** 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. */\nexport function applyVariableColor(paint: Fill, color: Color): void {\n Object.assign(paint, solidPaintColor(color))\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 applyVariableColor(fills[bound.index], color)\n } else {\n strokes ??= copyStrokes(node.strokes)\n applyVariableColor(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\n/**\n * A range's own fills with each bound one showing the colour its variable gives the text now, or\n * null when every one does already. A range keeps no record of the colours it was last given, so\n * an edit to a bound range paint lasts until the next refresh of the text's bindings.\n */\nexport function refreshedRunFills(\n graph: SceneGraph,\n nodeId: string,\n style: CharacterStyleOverride\n): Fill[] | null {\n let fills: Fill[] | undefined\n for (const [field, variableId] of Object.entries(style.boundVariables ?? {})) {\n const target = paintBindingTarget(field)\n const paint = target?.list === 'fills' ? style.fills?.at(target.index) : undefined\n const color = paint && variableId && graph.resolveColorVariableForNode(nodeId, variableId)\n if (!target || !paint || !color || showsColor(paint, color)) continue\n fills ??= copyFills(style.fills ?? [])\n applyVariableColor(fills[target.index], color)\n }\n return fills ?? null\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 * 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)) applyVariableColor(paint, color)\n recordApplied(target, bound, color, modes)\n })\n }\n}\n"],"mappings":";;;;;;;;AA4BA,MAAM,gCAAgB,IAAI,QAA8C;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BgB,sBACd,OAAO,YACP,gBACA;;iBAQc,8BACd,OAAO,YACP,gBACA;;;;;iBAec,qBACd,OAAO,YACP,aAAa,mBACZ;iBAgBa,gBACd,OAAO,YACP,oBACA,iBACA,UAAU,cACT;cAaU,gCAAgC;WAGhC;WACA;EAHF;EAEE,YAAA,wFACA,oBACT;;;;;;
|
|
1
|
+
{"version":3,"file":"resolution.d.ts","names":[],"sources":["../../src/variables/resolution.ts"],"mappings":";;;;;;;;;iBAgBgB,sBACd,OAAO,YACP,gBACA;;iBAQc,8BACd,OAAO,YACP,gBACA;;;;;iBAec,qBACd,OAAO,YACP,aAAa,mBACZ;iBAgBa,gBACd,OAAO,YACP,oBACA,iBACA,UAAU,cACT;cAaU,gCAAgC;WAGhC;WACA;EAHF;EAEE,YAAA,wFACA,oBACT;;;;;;iBAmBY,uBACd,OAAO,YACP,oBACA,UAAU,iCACV,WAAQ,aACR,OAAO,YAAY,kBACnB,YAAY,oBAAoB,iBAC/B;;iBA0Ha,oBACd,OAAO,YACP,UAAU,UACV,iBACC;KAQS,wBACV,gBACA,uBACG;;iBAGW,4BAA4B,OAAO,aAAa;iBAqBhD,qBAAqB,OAAO,YAAY,qBAAqB;iBAM7D,sBAAsB,OAAO,YAAY;iBAKzC,4BACd,OAAO,YACP,gBACA,qBACC;iBAMa,6BACd,OAAO,YACP,gBACA;;iBAOc,uBACd,OAAO,YACP,gBACA,qBACC;iBAMa,6BACd,OAAO,YACP,gBACA"}
|
|
@@ -54,12 +54,14 @@ var VariableResolutionError = class extends VariableFormulaError {
|
|
|
54
54
|
this.variableId = variableId;
|
|
55
55
|
}
|
|
56
56
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
/** Whether a resolved value is one a variable of this type gives. */
|
|
58
|
+
function fitsType(value, type) {
|
|
59
|
+
if (type === "COLOR") return typeof value === "object" && "r" in value;
|
|
60
|
+
if (type === "EASING") return typeof value === "object" && "easing" in value;
|
|
61
|
+
if (type === "BOOLEAN") return typeof value === "boolean";
|
|
62
|
+
if (type === "STRING") return typeof value === "string";
|
|
63
|
+
return typeof value === "number";
|
|
64
|
+
}
|
|
63
65
|
/**
|
|
64
66
|
* Resolve a draft or saved definition with identical mode, dependency and error semantics. A
|
|
65
67
|
* memo shares values between lookups in one mode context; without one, nothing is cached.
|
|
@@ -81,7 +83,7 @@ function evaluateDefinition(graph, variable, value, modeFor, visiting, memo, ove
|
|
|
81
83
|
visiting.add(variableId);
|
|
82
84
|
try {
|
|
83
85
|
const result = resolveVariableValue(value, (id) => resolveVariableInModes(graph, id, modeFor, visiting, memo, overrides));
|
|
84
|
-
if (result === void 0 ||
|
|
86
|
+
if (result === void 0 || !fitsType(result, variable.type)) throw new VariableResolutionError("type", variableId, `Wrong result type: ${variable.name}`);
|
|
85
87
|
if (typeof result === "number" && !Number.isFinite(result)) throw new VariableResolutionError("non-finite", variableId, `Non-finite value: ${variable.name}`);
|
|
86
88
|
memo?.set(variableId, result);
|
|
87
89
|
return result;
|
|
@@ -121,8 +123,7 @@ function resolveExpression(graph, variable, value, modeFor, visiting, memo) {
|
|
|
121
123
|
}
|
|
122
124
|
/** A resolved value the canvas can draw for a variable of this type, or nothing. */
|
|
123
125
|
function drawable(value, type) {
|
|
124
|
-
if (value === void 0 ||
|
|
125
|
-
if (typeof value === "object" && !("r" in value)) return void 0;
|
|
126
|
+
if (value === void 0 || !fitsType(value, type)) return void 0;
|
|
126
127
|
return typeof value === "number" && !Number.isFinite(value) ? void 0 : value;
|
|
127
128
|
}
|
|
128
129
|
/** Shared mode fallback for canvas resolution and formula draft validation. */
|