@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,6 +1,7 @@
|
|
|
1
1
|
import type { SceneGraph } from '#scene-graph/graph'
|
|
2
|
-
import type { VariableLiteral, VariableValue } from '#scene-graph/types'
|
|
2
|
+
import type { Variable, VariableLiteral, VariableType, VariableValue } from '#scene-graph/types'
|
|
3
3
|
|
|
4
|
+
import { isEasing } from '../easing'
|
|
4
5
|
import { resolveVariableInModes } from '../resolution'
|
|
5
6
|
import { parseFormulaSyntax, type FormulaReference } from './parse'
|
|
6
7
|
import { printFormula, type PrintFormulaOptions } from './print'
|
|
@@ -38,12 +39,15 @@ function qualifiedName(graph: SceneGraph, id: string) {
|
|
|
38
39
|
/** Qualified names disambiguate references; the stored formula uses stable IDs. */
|
|
39
40
|
export function variableFormulaReference(graph: SceneGraph, id: string): string {
|
|
40
41
|
const variable = graph.variables.get(id)
|
|
41
|
-
|
|
42
|
+
// A deleted variable's name no longer names it, so only its ID does.
|
|
43
|
+
if (!variable || variable.deleted) return `{#${id}}`
|
|
42
44
|
const unique = (name: string, of: (id: string) => string | undefined) =>
|
|
43
|
-
|
|
45
|
+
graph.getLiveVariables().filter((other) => of(other.id) === name).length === 1
|
|
44
46
|
const plain = variable.name
|
|
45
47
|
if (
|
|
46
48
|
!/[{}]/.test(plain) &&
|
|
49
|
+
// A name that starts like an ID would be read as one.
|
|
50
|
+
!plain.startsWith('#') &&
|
|
47
51
|
plain.trim() === plain &&
|
|
48
52
|
unique(plain, (other) => graph.variables.get(other)?.name)
|
|
49
53
|
)
|
|
@@ -61,7 +65,9 @@ export function formatVariableFormula(
|
|
|
61
65
|
options?: PrintFormulaOptions
|
|
62
66
|
): string {
|
|
63
67
|
const reference = (id: string) => variableFormulaReference(graph, id)
|
|
64
|
-
const typed = { type: (id: string) => graph.variables.get(id)?.type, ...options }
|
|
68
|
+
const typed = { type: (id: string) => formulaTypeOf(graph.variables.get(id)?.type), ...options }
|
|
69
|
+
// An easing is no formula's value and has no source text.
|
|
70
|
+
if (isEasing(value)) return ''
|
|
65
71
|
if (typeof value !== 'object' || !('expression' in value))
|
|
66
72
|
return printFormula(value, reference, typed)
|
|
67
73
|
return value.expression === 'FORMULA' ? printFormula(value.value, reference, typed) : ''
|
|
@@ -72,19 +78,31 @@ export function formatVariableFormula(
|
|
|
72
78
|
* edited variable's own collection when a name appears in several.
|
|
73
79
|
*/
|
|
74
80
|
function resolveReference(graph: SceneGraph, collectionId: string, name: string): FormulaReference {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
// An ID names its variable before any name does; a deleted variable is reached only by it, as
|
|
82
|
+
// a formula saved before the deletion names it.
|
|
83
|
+
const byId = name.startsWith('#') ? graph.variables.get(name.slice(1)) : undefined
|
|
84
|
+
if (byId) return { id: byId.id, type: formulaType(byId) }
|
|
85
|
+
const candidates = graph
|
|
86
|
+
.getLiveVariables()
|
|
87
|
+
.filter((candidate) => name === qualifiedName(graph, candidate.id) || name === candidate.name)
|
|
81
88
|
const local = candidates.filter((candidate) => candidate.collectionId === collectionId)
|
|
82
89
|
const matches = local.length === 1 ? local : candidates
|
|
83
90
|
const match = matches.at(0)
|
|
84
91
|
if (!match) throw new VariableFormulaError(`No variable named ${name}`)
|
|
85
92
|
if (matches.length !== 1)
|
|
86
93
|
throw new VariableFormulaError(`More than one variable is named ${name}`)
|
|
87
|
-
return { id: match.id, type: match
|
|
94
|
+
return { id: match.id, type: formulaType(match) }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The type a formula computes a variable's value as; easings and timings have none. */
|
|
98
|
+
export function formulaTypeOf(type: VariableType | undefined): FormulaValueType | undefined {
|
|
99
|
+
return type === 'EASING' || type === 'TIMING' ? undefined : type
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function formulaType(variable: Variable): FormulaValueType {
|
|
103
|
+
const type = formulaTypeOf(variable.type)
|
|
104
|
+
if (!type) throw new VariableFormulaError(`Formulas cannot use ${variable.name}`)
|
|
105
|
+
return type
|
|
88
106
|
}
|
|
89
107
|
|
|
90
108
|
const TYPE_NAMES: Record<FormulaValueType, string> = {
|
|
@@ -106,15 +124,16 @@ export function parseVariableFormula(
|
|
|
106
124
|
const collection = graph.variableCollections.get(variable.collectionId)
|
|
107
125
|
if (!collection?.modes.some((mode) => mode.modeId === modeId))
|
|
108
126
|
throw new VariableFormulaError('Unknown variable mode')
|
|
127
|
+
const holds = formulaType(variable)
|
|
109
128
|
const { node, type } = parseFormulaSyntax(source, (name) =>
|
|
110
129
|
resolveReference(graph, variable.collectionId, name)
|
|
111
130
|
)
|
|
112
|
-
if (type !==
|
|
131
|
+
if (type !== holds)
|
|
113
132
|
throw new VariableFormulaError(
|
|
114
|
-
`This gives ${TYPE_NAMES[type]}, but ${variable.name} holds ${TYPE_NAMES[
|
|
133
|
+
`This gives ${TYPE_NAMES[type]}, but ${variable.name} holds ${TYPE_NAMES[holds]}`
|
|
115
134
|
)
|
|
116
135
|
const value: VariableValue = isFormulaOperation(node)
|
|
117
|
-
? { expression: 'FORMULA', type:
|
|
136
|
+
? { expression: 'FORMULA', type: holds, value: node }
|
|
118
137
|
: node
|
|
119
138
|
const resolved = resolveVariableInModes(
|
|
120
139
|
graph,
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
VariableFormulaOperator,
|
|
3
|
+
VariableFormulaType,
|
|
4
|
+
VariableLiteral
|
|
5
|
+
} from '#scene-graph/types'
|
|
2
6
|
|
|
3
7
|
import { variableText, variableTruthy } from '../text'
|
|
4
8
|
import { VariableFormulaError } from './tree'
|
|
5
9
|
|
|
6
|
-
export type FormulaValueType =
|
|
10
|
+
export type FormulaValueType = VariableFormulaType
|
|
7
11
|
|
|
8
12
|
/** One set of operand types an operator takes, and the type it then gives. */
|
|
9
13
|
export interface FormulaOverload {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
VariableAlias,
|
|
3
3
|
VariableFormulaNode,
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
VariableFormulaLiteral,
|
|
5
|
+
VariableFormulaOperation
|
|
6
6
|
} from '#scene-graph/types'
|
|
7
7
|
|
|
8
8
|
export const MAX_FORMULA_DEPTH = 64
|
|
@@ -22,7 +22,7 @@ export function isFormulaReference(node: VariableFormulaNode): node is VariableA
|
|
|
22
22
|
|
|
23
23
|
/** What a fold makes of each kind of node; an operation gets its operands' results. */
|
|
24
24
|
export interface FormulaFold<T> {
|
|
25
|
-
literal: (value:
|
|
25
|
+
literal: (value: VariableFormulaLiteral) => T
|
|
26
26
|
reference: (id: string) => T
|
|
27
27
|
operation: (node: VariableFormulaOperation, operands: T[]) => T
|
|
28
28
|
}
|
package/src/variables/index.ts
CHANGED
|
@@ -6,14 +6,16 @@ import type { SceneGraph } from '../graph'
|
|
|
6
6
|
import { findInstanceAncestor } from '../instances'
|
|
7
7
|
import { bindingOverrideField, setInstanceOverride } from '../instances/overrides'
|
|
8
8
|
import { siblingOrderKeys } from '../order-keys'
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
import {
|
|
10
|
+
TEXT_BINDABLE_FIELDS,
|
|
11
|
+
type Effect,
|
|
12
|
+
type SceneNode,
|
|
13
|
+
type TextBindableField,
|
|
14
|
+
type Variable,
|
|
15
|
+
type VariableCollection,
|
|
16
|
+
type VariableCollectionMode,
|
|
17
|
+
type VariableType,
|
|
18
|
+
type VariableValue
|
|
17
19
|
} from '../types'
|
|
18
20
|
import {
|
|
19
21
|
isNumericVariableBindingField,
|
|
@@ -35,14 +37,43 @@ import { applyBoundPaintColor } from './paints'
|
|
|
35
37
|
|
|
36
38
|
export * from './resolution'
|
|
37
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Variables a list shows: every one but those deleted, on their own or with their collection,
|
|
42
|
+
* which only their bindings still reach.
|
|
43
|
+
*/
|
|
44
|
+
export function liveVariables(graph: SceneGraph): Variable[] {
|
|
45
|
+
return [...graph.variables.values()].filter(
|
|
46
|
+
(variable) =>
|
|
47
|
+
!variable.deleted && !graph.variableCollections.get(variable.collectionId)?.deleted
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Collections a list shows: every one but those deleted. */
|
|
52
|
+
export function liveCollections(graph: SceneGraph): VariableCollection[] {
|
|
53
|
+
return [...graph.variableCollections.values()].filter((collection) => !collection.deleted)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A collection's deleted variables, which its `variableIds` leave out. A deleted collection's
|
|
58
|
+
* variables are all deleted.
|
|
59
|
+
*/
|
|
60
|
+
export function deletedVariablesOf(graph: SceneGraph, collectionId: string): Variable[] {
|
|
61
|
+
return [...graph.variables.values()].filter(
|
|
62
|
+
(variable) => variable.collectionId === collectionId && variable.deleted
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
38
66
|
export function addVariable(graph: SceneGraph, variable: Variable): void {
|
|
39
67
|
graph.willChangeVariable(variable.id)
|
|
40
68
|
graph.variables.set(variable.id, variable)
|
|
41
69
|
const collection = graph.variableCollections.get(variable.collectionId)
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
70
|
+
if (!collection) return
|
|
71
|
+
// A collection lists its live variables, so one replaced by its deleted record leaves the list.
|
|
72
|
+
if (collection.variableIds.includes(variable.id) === !variable.deleted) return
|
|
73
|
+
graph.willChangeCollection(collection.id)
|
|
74
|
+
collection.variableIds = variable.deleted
|
|
75
|
+
? collection.variableIds.filter((id) => id !== variable.id)
|
|
76
|
+
: [...collection.variableIds, variable.id]
|
|
46
77
|
}
|
|
47
78
|
|
|
48
79
|
export function removeVariable(graph: SceneGraph, id: string): void {
|
|
@@ -80,11 +111,14 @@ export function addCollection(graph: SceneGraph, collection: VariableCollection)
|
|
|
80
111
|
graph.variableCollections.set(collection.id, collection)
|
|
81
112
|
}
|
|
82
113
|
|
|
83
|
-
|
|
114
|
+
/** The value a new variable of this type starts with, unless it is given one. */
|
|
115
|
+
export function defaultVariableValue(type: VariableType, value?: VariableValue): VariableValue {
|
|
84
116
|
if (value !== undefined) return value
|
|
85
117
|
if (type === 'COLOR') return { ...BLACK }
|
|
86
|
-
if (type === 'FLOAT') return 0
|
|
118
|
+
if (type === 'FLOAT' || type === 'TIMING') return 0
|
|
87
119
|
if (type === 'BOOLEAN') return false
|
|
120
|
+
// Figma starts an easing as IN_CUBIC with no numbers, which it shows as Ease in.
|
|
121
|
+
if (type === 'EASING') return { easing: 'IN_CUBIC' }
|
|
88
122
|
return ''
|
|
89
123
|
}
|
|
90
124
|
|
|
@@ -141,6 +175,7 @@ export function removeCollection(graph: SceneGraph, id: string): void {
|
|
|
141
175
|
for (const varId of Array.from(collection.variableIds)) {
|
|
142
176
|
removeVariable(graph, varId)
|
|
143
177
|
}
|
|
178
|
+
for (const variable of deletedVariablesOf(graph, id)) removeVariable(graph, variable.id)
|
|
144
179
|
}
|
|
145
180
|
graph.willChangeCollection(id)
|
|
146
181
|
graph.variableCollections.delete(id)
|
|
@@ -244,7 +279,7 @@ export function getVariablesForCollection(graph: SceneGraph, collectionId: strin
|
|
|
244
279
|
}
|
|
245
280
|
|
|
246
281
|
export function getVariablesByType(graph: SceneGraph, type: VariableType): Variable[] {
|
|
247
|
-
return
|
|
282
|
+
return liveVariables(graph).filter((v) => v.type === type)
|
|
248
283
|
}
|
|
249
284
|
|
|
250
285
|
/** Checks that an effect binding names an effect the node has, a property it binds, and its type. */
|
|
@@ -290,6 +325,42 @@ function preparePaintBinding(
|
|
|
290
325
|
}
|
|
291
326
|
}
|
|
292
327
|
|
|
328
|
+
const TEXT_BINDABLE: ReadonlySet<string> = new Set(TEXT_BINDABLE_FIELDS)
|
|
329
|
+
|
|
330
|
+
function isTextBindableField(field: string): field is TextBindableField {
|
|
331
|
+
return TEXT_BINDABLE.has(field)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Checks the variable's type against a scalar, text, boolean, or text style field, and says
|
|
336
|
+
* whether the field is one of those. A text style binds every field a range of characters can,
|
|
337
|
+
* its face by style or by weight.
|
|
338
|
+
*/
|
|
339
|
+
function assertFieldVariableType(node: SceneNode, field: string, variable: Variable): boolean {
|
|
340
|
+
if (isNumericVariableBindingField(field)) {
|
|
341
|
+
if (variable.type !== 'FLOAT')
|
|
342
|
+
throw new Error(`Cannot bind ${variable.type} variable to scalar field "${field}"`)
|
|
343
|
+
return true
|
|
344
|
+
}
|
|
345
|
+
if (STRING_BINDING_FIELDS.has(field)) {
|
|
346
|
+
const types: readonly VariableType[] =
|
|
347
|
+
field === 'text' ? TEXT_CONTENT_VARIABLE_TYPES : ['STRING']
|
|
348
|
+
if (!types.includes(variable.type))
|
|
349
|
+
throw new Error(`Cannot bind ${variable.type} variable to string field "${field}"`)
|
|
350
|
+
return true
|
|
351
|
+
}
|
|
352
|
+
if (BOOLEAN_BINDING_FIELDS.has(field)) {
|
|
353
|
+
if (!VISIBILITY_VARIABLE_TYPES.some((type) => type === variable.type))
|
|
354
|
+
throw new Error(`Cannot bind ${variable.type} variable to boolean field "${field}"`)
|
|
355
|
+
return true
|
|
356
|
+
}
|
|
357
|
+
if (node.sharedStyleType !== 'TEXT' || !isTextBindableField(field)) return false
|
|
358
|
+
const type: VariableType = field === 'fontStyle' ? 'STRING' : 'FLOAT'
|
|
359
|
+
if (variable.type !== type)
|
|
360
|
+
throw new Error(`Cannot bind ${variable.type} variable to text style field "${field}"`)
|
|
361
|
+
return true
|
|
362
|
+
}
|
|
363
|
+
|
|
293
364
|
export function bindVariable(
|
|
294
365
|
graph: SceneGraph,
|
|
295
366
|
nodeId: string,
|
|
@@ -311,29 +382,8 @@ export function bindVariable(
|
|
|
311
382
|
const effect = effectBindingTarget(field)
|
|
312
383
|
if (effect) assertEffectBinding(node.effects, effect, variable.type, field)
|
|
313
384
|
|
|
314
|
-
if (isNumericVariableBindingField(field) && variable.type !== 'FLOAT') {
|
|
315
|
-
throw new Error(`Cannot bind ${variable.type} variable to scalar field "${field}"`)
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
const textTypes: readonly VariableType[] =
|
|
319
|
-
field === 'text' ? TEXT_CONTENT_VARIABLE_TYPES : ['STRING']
|
|
320
|
-
if (STRING_BINDING_FIELDS.has(field) && !textTypes.includes(variable.type)) {
|
|
321
|
-
throw new Error(`Cannot bind ${variable.type} variable to string field "${field}"`)
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
if (
|
|
325
|
-
BOOLEAN_BINDING_FIELDS.has(field) &&
|
|
326
|
-
!VISIBILITY_VARIABLE_TYPES.some((type) => type === variable.type)
|
|
327
|
-
) {
|
|
328
|
-
throw new Error(`Cannot bind ${variable.type} variable to boolean field "${field}"`)
|
|
329
|
-
}
|
|
330
|
-
|
|
331
385
|
const isKnownField =
|
|
332
|
-
|
|
333
|
-
STRING_BINDING_FIELDS.has(field) ||
|
|
334
|
-
BOOLEAN_BINDING_FIELDS.has(field) ||
|
|
335
|
-
paint !== null ||
|
|
336
|
-
effect !== null
|
|
386
|
+
assertFieldVariableType(node, field, variable) || paint !== null || effect !== null
|
|
337
387
|
|
|
338
388
|
if (!isKnownField) {
|
|
339
389
|
throw new Error(`Unknown binding field "${field}"`)
|
|
@@ -396,10 +446,13 @@ function markBoundVariablesOverrideOnInstance(
|
|
|
396
446
|
}
|
|
397
447
|
|
|
398
448
|
export * from './bindings'
|
|
449
|
+
export * from './style-bindings'
|
|
399
450
|
export * from './corners'
|
|
451
|
+
export * from './easing'
|
|
400
452
|
export * from './formula'
|
|
401
453
|
export * from './formula/schema'
|
|
402
454
|
export * from './lookup'
|
|
455
|
+
export * from './motion'
|
|
403
456
|
export * from './paints'
|
|
404
457
|
export * from './schema'
|
|
405
458
|
export * from './text'
|
package/src/variables/lookup.ts
CHANGED
|
@@ -17,7 +17,7 @@ function byName<T extends { name: string }>(items: Iterable<T>, name: string): N
|
|
|
17
17
|
export function lookupCollection(graph: SceneGraph, ref: string): NamedLookup<VariableCollection> {
|
|
18
18
|
const byId = graph.variableCollections.get(ref)
|
|
19
19
|
if (byId) return { kind: 'found', value: byId }
|
|
20
|
-
return byName(graph.
|
|
20
|
+
return byName(graph.getLiveCollections(), ref)
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/** The mode of `collection` with this ID or, failing that, the one with this exact name. */
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { compact } from 'es-toolkit/array'
|
|
2
|
+
|
|
3
|
+
import { parseCSSEasing } from '../css/motion'
|
|
4
|
+
import type { SceneGraph } from '../graph'
|
|
5
|
+
import { EASING_CURVES, type Easing, type EasingCurve, type Variable } from '../types'
|
|
6
|
+
import { easingPreset, isEasing } from './easing'
|
|
7
|
+
import type { MotionSpec } from './schema'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A duration in seconds and an easing, each its own value or, while the variable exists, a TIMING
|
|
11
|
+
* or EASING variable's, as Figma keeps a transition's duration and easing beside the variables
|
|
12
|
+
* they follow.
|
|
13
|
+
*/
|
|
14
|
+
export interface Motion {
|
|
15
|
+
duration: number
|
|
16
|
+
easing: Easing
|
|
17
|
+
durationVariableId?: string
|
|
18
|
+
easingVariableId?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** What a motion plays: its duration and easing, with variables read. */
|
|
22
|
+
export interface ResolvedMotion {
|
|
23
|
+
duration: number
|
|
24
|
+
easing: Easing
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A live variable of `type` by its ID, or undefined when it is gone or holds something else. */
|
|
28
|
+
export function liveVariable(
|
|
29
|
+
graph: SceneGraph,
|
|
30
|
+
id: string | undefined,
|
|
31
|
+
type: Variable['type']
|
|
32
|
+
): Variable | undefined {
|
|
33
|
+
const variable = id ? graph.variables.get(id) : undefined
|
|
34
|
+
// A deleted variable the document keeps still resolves for the bindings it had.
|
|
35
|
+
return variable?.type === type ? variable : undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A TIMING variable's value for `nodeId`, or `fallback` when there is none to read. */
|
|
39
|
+
export function resolveTiming(
|
|
40
|
+
graph: SceneGraph,
|
|
41
|
+
variableId: string | undefined,
|
|
42
|
+
nodeId: string,
|
|
43
|
+
fallback: number
|
|
44
|
+
): number {
|
|
45
|
+
const timing = liveVariable(graph, variableId, 'TIMING')
|
|
46
|
+
const value = timing && graph.resolveVariableForNode(nodeId, timing.id)
|
|
47
|
+
return typeof value === 'number' && value >= 0 ? value : fallback
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The motion `nodeId` plays: each variable read in the modes the node is in, and the motion's own
|
|
52
|
+
* value where the variable is gone or gives nothing.
|
|
53
|
+
*/
|
|
54
|
+
export function resolveMotion(graph: SceneGraph, motion: Motion, nodeId: string): ResolvedMotion {
|
|
55
|
+
const easing = liveVariable(graph, motion.easingVariableId, 'EASING')
|
|
56
|
+
const curve = easing && graph.resolveVariableForNode(nodeId, easing.id)
|
|
57
|
+
return {
|
|
58
|
+
duration: resolveTiming(graph, motion.durationVariableId, nodeId, motion.duration),
|
|
59
|
+
easing: isEasing(curve) ? curve : motion.easing
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The variables a motion follows. */
|
|
64
|
+
export function motionVariableIds(motion: Motion): string[] {
|
|
65
|
+
return compact([motion.durationVariableId, motion.easingVariableId])
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The motion following other variables, as a copy into another document or a file's own IDs name
|
|
70
|
+
* them; a variable `map` gives nothing for is let go, keeping the value.
|
|
71
|
+
*/
|
|
72
|
+
export function mapMotionVariables<T extends Motion>(
|
|
73
|
+
motion: T,
|
|
74
|
+
map: (variableId: string) => string | undefined
|
|
75
|
+
): T {
|
|
76
|
+
const { durationVariableId, easingVariableId, ...values } = motion
|
|
77
|
+
const mapped = structuredClone(values) as T
|
|
78
|
+
const duration = durationVariableId === undefined ? undefined : map(durationVariableId)
|
|
79
|
+
const easing = easingVariableId === undefined ? undefined : map(easingVariableId)
|
|
80
|
+
if (duration !== undefined) mapped.durationVariableId = duration
|
|
81
|
+
if (easing !== undefined) mapped.easingVariableId = easing
|
|
82
|
+
return mapped
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A live variable of `type`, by its ID or its name; a spec names one either way. */
|
|
86
|
+
function namedVariable(graph: SceneGraph, name: string, type: Variable['type']): Variable {
|
|
87
|
+
const variable =
|
|
88
|
+
graph.variables.get(name) ?? graph.getLiveVariables().find((item) => item.name === name)
|
|
89
|
+
if (!variable || variable.deleted) throw new Error(`No variable named "${name}"`)
|
|
90
|
+
if (variable.type !== type)
|
|
91
|
+
throw new Error(`"${variable.name}" holds ${variable.type}; this needs a ${type} variable`)
|
|
92
|
+
return variable
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isEasingCurve(name: string): name is EasingCurve {
|
|
96
|
+
return (EASING_CURVES as readonly string[]).includes(name)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function easingOfSpec(easing: string): Easing {
|
|
100
|
+
if (isEasingCurve(easing)) return easingPreset(easing)
|
|
101
|
+
const parsed = parseCSSEasing(easing)
|
|
102
|
+
if (!parsed)
|
|
103
|
+
throw new Error(`"${easing}" is not an easing: use a CSS easing or a name such as OUT_CUBIC`)
|
|
104
|
+
return parsed
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* `base` with what a spec sets: values as given, and variables, which keep their value in their
|
|
109
|
+
* collection's default mode beside them, for when they are gone.
|
|
110
|
+
*/
|
|
111
|
+
export function motionFromSpec<T extends Motion>(graph: SceneGraph, spec: MotionSpec, base: T): T {
|
|
112
|
+
const motion = structuredClone(base)
|
|
113
|
+
if (typeof spec.duration === 'number') {
|
|
114
|
+
motion.duration = spec.duration
|
|
115
|
+
delete motion.durationVariableId
|
|
116
|
+
} else if (spec.duration) {
|
|
117
|
+
const variable = namedVariable(graph, spec.duration.variable, 'TIMING')
|
|
118
|
+
const value = graph.resolveVariable(variable.id)
|
|
119
|
+
motion.durationVariableId = variable.id
|
|
120
|
+
if (typeof value === 'number') motion.duration = value
|
|
121
|
+
}
|
|
122
|
+
if (typeof spec.easing === 'string') {
|
|
123
|
+
motion.easing = easingOfSpec(spec.easing)
|
|
124
|
+
delete motion.easingVariableId
|
|
125
|
+
} else if (spec.easing && 'variable' in spec.easing) {
|
|
126
|
+
const variable = namedVariable(graph, spec.easing.variable, 'EASING')
|
|
127
|
+
const value = graph.resolveVariable(variable.id)
|
|
128
|
+
motion.easingVariableId = variable.id
|
|
129
|
+
if (isEasing(value)) motion.easing = value
|
|
130
|
+
} else if (spec.easing) {
|
|
131
|
+
motion.easing = structuredClone(spec.easing)
|
|
132
|
+
delete motion.easingVariableId
|
|
133
|
+
}
|
|
134
|
+
return motion
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** How a spec names a variable: by its name, or by its ID where another live variable shares it. */
|
|
138
|
+
function specReference(graph: SceneGraph, variable: Variable): { variable: string } {
|
|
139
|
+
const named = graph.getLiveVariables().filter((item) => item.name === variable.name)
|
|
140
|
+
return { variable: named.length === 1 ? variable.name : variable.id }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** A motion as a spec reads it: variables by name, while they exist, else values. */
|
|
144
|
+
export function motionToSpec(graph: SceneGraph, motion: Motion): Required<MotionSpec> {
|
|
145
|
+
const timing = liveVariable(graph, motion.durationVariableId, 'TIMING')
|
|
146
|
+
const easing = liveVariable(graph, motion.easingVariableId, 'EASING')
|
|
147
|
+
return {
|
|
148
|
+
duration: timing ? specReference(graph, timing) : motion.duration,
|
|
149
|
+
easing: easing ? specReference(graph, easing) : structuredClone(motion.easing)
|
|
150
|
+
}
|
|
151
|
+
}
|
package/src/variables/paints.ts
CHANGED
|
@@ -3,8 +3,9 @@ import { isEqual } from 'es-toolkit/predicate'
|
|
|
3
3
|
import { copyFills, copyStrokes } from '../copy'
|
|
4
4
|
import type { SceneGraph } from '../graph'
|
|
5
5
|
import { hasInstanceOverride } from '../instances/overrides'
|
|
6
|
+
import { solidPaintColor } from '../paints/solid'
|
|
6
7
|
import type { Color } from '../primitives'
|
|
7
|
-
import type { Fill, SceneNode, Stroke } from '../types'
|
|
8
|
+
import type { CharacterStyleOverride, Fill, SceneNode, Stroke } from '../types'
|
|
8
9
|
import { paintBindingTarget, type PaintBindingList } from './fields'
|
|
9
10
|
import { collectionsResolving, getNodeExplicitVariableModeId } from './resolution'
|
|
10
11
|
import { subtreeNodes } from './subtree'
|
|
@@ -166,9 +167,8 @@ function recordApplied(
|
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
/** The variable's RGB at full alpha in the paint, and its alpha as the paint's opacity. */
|
|
169
|
-
function
|
|
170
|
-
paint
|
|
171
|
-
paint.opacity = color.a
|
|
170
|
+
export function applyVariableColor(paint: Fill, color: Color): void {
|
|
171
|
+
Object.assign(paint, solidPaintColor(color))
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
/** Whether `paint` already shows `color` as a bound paint stores it. */
|
|
@@ -227,10 +227,10 @@ function refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) =>
|
|
|
227
227
|
if ((!forced(bound.field) && !stale) || showsColor(paint, color)) continue
|
|
228
228
|
if (bound.list === 'fills') {
|
|
229
229
|
fills ??= copyFills(node.fills)
|
|
230
|
-
|
|
230
|
+
applyVariableColor(fills[bound.index], color)
|
|
231
231
|
} else {
|
|
232
232
|
strokes ??= copyStrokes(node.strokes)
|
|
233
|
-
|
|
233
|
+
applyVariableColor(strokes[bound.index], color)
|
|
234
234
|
}
|
|
235
235
|
}
|
|
236
236
|
const changes: PaintLists = {}
|
|
@@ -239,6 +239,28 @@ function refresh(graph: SceneGraph, node: SceneNode, forced: (field: string) =>
|
|
|
239
239
|
if (fills || strokes) writePaints(graph, node, changes)
|
|
240
240
|
}
|
|
241
241
|
|
|
242
|
+
/**
|
|
243
|
+
* A range's own fills with each bound one showing the colour its variable gives the text now, or
|
|
244
|
+
* null when every one does already. A range keeps no record of the colours it was last given, so
|
|
245
|
+
* an edit to a bound range paint lasts until the next refresh of the text's bindings.
|
|
246
|
+
*/
|
|
247
|
+
export function refreshedRunFills(
|
|
248
|
+
graph: SceneGraph,
|
|
249
|
+
nodeId: string,
|
|
250
|
+
style: CharacterStyleOverride
|
|
251
|
+
): Fill[] | null {
|
|
252
|
+
let fills: Fill[] | undefined
|
|
253
|
+
for (const [field, variableId] of Object.entries(style.boundVariables ?? {})) {
|
|
254
|
+
const target = paintBindingTarget(field)
|
|
255
|
+
const paint = target?.list === 'fills' ? style.fills?.at(target.index) : undefined
|
|
256
|
+
const color = paint && variableId && graph.resolveColorVariableForNode(nodeId, variableId)
|
|
257
|
+
if (!target || !paint || !color || showsColor(paint, color)) continue
|
|
258
|
+
fills ??= copyFills(style.fills ?? [])
|
|
259
|
+
applyVariableColor(fills[target.index], color)
|
|
260
|
+
}
|
|
261
|
+
return fills ?? null
|
|
262
|
+
}
|
|
263
|
+
|
|
242
264
|
const NONE_FORCED = (): boolean => false
|
|
243
265
|
const ALL_FORCED = (): boolean => true
|
|
244
266
|
|
|
@@ -370,7 +392,7 @@ export function keepCopyBoundPaints(
|
|
|
370
392
|
const color = graph.resolveColorVariableForNode(target.id, variableId)
|
|
371
393
|
if (!color) return
|
|
372
394
|
const modes = explicitModes(graph, target, variableId)
|
|
373
|
-
if (!keeps || staleApplied(target, bound, own, color, modes))
|
|
395
|
+
if (!keeps || staleApplied(target, bound, own, color, modes)) applyVariableColor(paint, color)
|
|
374
396
|
recordApplied(target, bound, color, modes)
|
|
375
397
|
})
|
|
376
398
|
}
|
|
@@ -2,7 +2,7 @@ import { appendAll } from '@open-pencil/std/arrays'
|
|
|
2
2
|
|
|
3
3
|
import type { SceneGraph } from '../graph'
|
|
4
4
|
import type { Color } from '../primitives'
|
|
5
|
-
import type { Variable, VariableLiteral, VariableValue } from '../types'
|
|
5
|
+
import type { Variable, VariableLiteral, VariableType, VariableValue } from '../types'
|
|
6
6
|
import { MAX_FORMULA_DEPTH, VariableFormulaError } from './formula/tree'
|
|
7
7
|
import { resolveVariableValue, variableValueReferences } from './value'
|
|
8
8
|
|
|
@@ -91,7 +91,14 @@ export class VariableResolutionError extends VariableFormulaError {
|
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
/** Whether a resolved value is one a variable of this type gives. */
|
|
95
|
+
function fitsType(value: VariableValue, type: VariableType): value is VariableLiteral {
|
|
96
|
+
if (type === 'COLOR') return typeof value === 'object' && 'r' in value
|
|
97
|
+
if (type === 'EASING') return typeof value === 'object' && 'easing' in value
|
|
98
|
+
if (type === 'BOOLEAN') return typeof value === 'boolean'
|
|
99
|
+
if (type === 'STRING') return typeof value === 'string'
|
|
100
|
+
return typeof value === 'number'
|
|
101
|
+
}
|
|
95
102
|
|
|
96
103
|
/**
|
|
97
104
|
* Resolve a draft or saved definition with identical mode, dependency and error semantics. A
|
|
@@ -146,7 +153,7 @@ function evaluateDefinition(
|
|
|
146
153
|
const result = resolveVariableValue(value, (id) =>
|
|
147
154
|
resolveVariableInModes(graph, id, modeFor, visiting, memo, overrides)
|
|
148
155
|
)
|
|
149
|
-
if (result === undefined ||
|
|
156
|
+
if (result === undefined || !fitsType(result, variable.type))
|
|
150
157
|
throw new VariableResolutionError('type', variableId, `Wrong result type: ${variable.name}`)
|
|
151
158
|
if (typeof result === 'number' && !Number.isFinite(result))
|
|
152
159
|
throw new VariableResolutionError(
|
|
@@ -221,8 +228,7 @@ function resolveExpression(
|
|
|
221
228
|
|
|
222
229
|
/** A resolved value the canvas can draw for a variable of this type, or nothing. */
|
|
223
230
|
function drawable(value: VariableValue | undefined, type: Variable['type']) {
|
|
224
|
-
if (value === undefined ||
|
|
225
|
-
if (typeof value === 'object' && !('r' in value)) return undefined
|
|
231
|
+
if (value === undefined || !fitsType(value, type)) return undefined
|
|
226
232
|
return typeof value === 'number' && !Number.isFinite(value) ? undefined : value
|
|
227
233
|
}
|
|
228
234
|
|
package/src/variables/schema.ts
CHANGED
|
@@ -2,6 +2,7 @@ import * as v from 'valibot'
|
|
|
2
2
|
|
|
3
3
|
import { colorSchema } from '../color/schema'
|
|
4
4
|
import { finiteNumberSchema } from '../primitives/schema'
|
|
5
|
+
import { EASING_CURVES } from '../types'
|
|
5
6
|
|
|
6
7
|
export const variableAliasSchema = v.object({ aliasId: v.string() })
|
|
7
8
|
export const variableColorExpressionSchema = v.object({
|
|
@@ -29,3 +30,43 @@ export const variableCollectionSchema = v.looseObject({
|
|
|
29
30
|
modes: v.array(v.unknown()),
|
|
30
31
|
variableIds: v.array(v.unknown())
|
|
31
32
|
})
|
|
33
|
+
|
|
34
|
+
export const cubicBezierSchema = v.object({
|
|
35
|
+
x1: finiteNumberSchema,
|
|
36
|
+
y1: finiteNumberSchema,
|
|
37
|
+
x2: finiteNumberSchema,
|
|
38
|
+
y2: finiteNumberSchema
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const positiveSchema = v.pipe(finiteNumberSchema, v.minValue(0))
|
|
42
|
+
|
|
43
|
+
export const springSchema = v.object({
|
|
44
|
+
stiffness: positiveSchema,
|
|
45
|
+
damping: positiveSchema,
|
|
46
|
+
// A spring without mass has no motion to compute.
|
|
47
|
+
mass: v.pipe(finiteNumberSchema, v.gtValue(0)),
|
|
48
|
+
type: v.optional(v.picklist(['NORMALIZED', 'PHYSICAL'])),
|
|
49
|
+
platform: v.optional(v.picklist(['IOS_WEB', 'ANDROID']))
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
export const easingSchema = v.object({
|
|
53
|
+
easing: v.picklist(EASING_CURVES),
|
|
54
|
+
bezier: v.optional(cubicBezierSchema),
|
|
55
|
+
spring: v.optional(springSchema)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
/** A duration or easing as a spec writes it: a value, or a variable by name or ID. */
|
|
59
|
+
const variableReferenceSchema = v.object({ variable: v.string() })
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A duration and easing as scripts, tools, and JSX write them: seconds or a TIMING variable; a CSS
|
|
63
|
+
* easing such as `ease-out` or `cubic-bezier(…)`, an easing name such as `GENTLE_SPRING`, an
|
|
64
|
+
* easing as stored, or an EASING variable.
|
|
65
|
+
*/
|
|
66
|
+
export const motionSpecSchema = v.object({
|
|
67
|
+
duration: v.optional(
|
|
68
|
+
v.union([v.pipe(v.number(), v.finite(), v.minValue(0)), variableReferenceSchema])
|
|
69
|
+
),
|
|
70
|
+
easing: v.optional(v.union([v.string(), variableReferenceSchema, easingSchema]))
|
|
71
|
+
})
|
|
72
|
+
export type MotionSpec = v.InferOutput<typeof motionSpecSchema>
|