@open-pencil/scene-graph 0.15.0 → 0.16.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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/chunks/behaviours/kinds.js +273 -0
- package/dist/chunks/behaviours/kinds.js.map +1 -0
- package/dist/chunks/behaviours/layers.js +596 -0
- package/dist/chunks/behaviours/layers.js.map +1 -0
- package/dist/chunks/behaviours/spec.js +181 -0
- package/dist/chunks/behaviours/spec.js.map +1 -0
- package/dist/chunks/buffered-events.js +57 -0
- package/dist/chunks/buffered-events.js.map +1 -0
- package/dist/chunks/color/contrast.js +57 -0
- package/dist/chunks/color/contrast.js.map +1 -0
- package/dist/chunks/{components/properties.js → color/index.js} +573 -521
- package/dist/chunks/color/index.js.map +1 -0
- package/dist/chunks/comments/document.js +32 -0
- package/dist/chunks/comments/document.js.map +1 -0
- package/dist/chunks/comments/list.js +182 -0
- package/dist/chunks/comments/list.js.map +1 -0
- package/dist/chunks/comments/merge.js +58 -0
- package/dist/chunks/comments/merge.js.map +1 -0
- package/dist/chunks/components/exposure.js +2021 -0
- package/dist/chunks/components/exposure.js.map +1 -0
- package/dist/chunks/components/variants.js +32 -0
- package/dist/chunks/components/variants.js.map +1 -0
- package/dist/chunks/coordinate.js +93 -55
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/css/grid.js +94 -0
- package/dist/chunks/css/grid.js.map +1 -0
- package/dist/chunks/css/index.js +160 -0
- package/dist/chunks/css/index.js.map +1 -0
- package/dist/chunks/fields/geometry.js +14 -0
- package/dist/chunks/fields/geometry.js.map +1 -0
- package/dist/chunks/fields/text.js +36 -0
- package/dist/chunks/fields/text.js.map +1 -0
- package/dist/chunks/group-bounds.js +104 -0
- package/dist/chunks/group-bounds.js.map +1 -0
- package/dist/chunks/icons/glyph.js +147 -0
- package/dist/chunks/icons/glyph.js.map +1 -0
- package/dist/chunks/index.js +1712 -662
- package/dist/chunks/index.js.map +1 -1
- package/dist/color.d.ts +72 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +3 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.js +2 -2
- package/dist/constants2.d.ts +3 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +2 -448
- package/dist/coordinate.js +2 -2
- package/dist/copy.d.ts +1 -1
- package/dist/copy.js +1 -1
- package/dist/css.d.ts +39 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +3 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/geometry.js +1 -1
- package/dist/gradient.d.ts +51 -0
- package/dist/gradient.d.ts.map +1 -0
- package/dist/gradient.js +132 -0
- package/dist/gradient.js.map +1 -0
- package/dist/guides.d.ts +2 -2
- package/dist/guides.js +39 -0
- package/dist/guides.js.map +1 -0
- package/dist/hit-test.d.ts +2 -9
- package/dist/hit-test.js +154 -18
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +4 -8
- package/dist/index.js +20 -5
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout.d.ts +2 -0
- package/dist/layout.js +4 -0
- package/dist/node-defaults.d.ts +2 -8
- package/dist/node-defaults.js +2 -2
- package/dist/order-keys.d.ts +39 -0
- package/dist/order-keys.d.ts.map +1 -0
- package/dist/order-keys.js +136 -0
- package/dist/order-keys.js.map +1 -0
- package/dist/parse-path.d.ts +7 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +6 -2
- package/dist/parse-path.js.map +1 -1
- package/dist/polygon.d.ts +53 -0
- package/dist/polygon.d.ts.map +1 -0
- package/dist/polygon.js +205 -0
- package/dist/polygon.js.map +1 -0
- package/dist/preview.d.ts +1 -1
- package/dist/random.d.ts +7 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +2 -0
- package/dist/resize.d.ts +11 -2
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +2 -184
- package/dist/snap.d.ts +1 -1
- package/dist/text-direction.d.ts +13 -0
- package/dist/text-direction.d.ts.map +1 -0
- package/dist/text-direction.js +31 -0
- package/dist/text-direction.js.map +1 -0
- package/dist/text-picture.d.ts +1 -1
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text.d.ts +2 -0
- package/dist/text.js +3 -0
- package/dist/types.d.ts +2063 -13
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -1
- package/dist/types2.d.ts +2 -2
- package/dist/undo.d.ts +2 -2
- package/dist/undo.js +2 -2
- package/dist/undo2.d.ts +29 -1
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +2 -29
- package/dist/variables.js +2 -2
- package/dist/variant-properties.d.ts +9 -1
- package/dist/variant-properties.d.ts.map +1 -1
- package/dist/variant-properties.js +36 -1
- package/dist/variant-properties.js.map +1 -1
- package/dist/vector-network.d.ts +1 -1
- package/dist/vector-network.js +1 -1
- package/package.json +60 -9
- package/src/behaviours/kinds.ts +185 -0
- package/src/behaviours/layers.ts +51 -0
- package/src/behaviours/model.ts +192 -0
- package/src/behaviours/schema.ts +58 -0
- package/src/behaviours/spec.ts +223 -0
- package/src/buffered-events.ts +64 -0
- package/src/color/contrast.ts +31 -0
- package/src/color/index.ts +88 -0
- package/src/color/management.ts +210 -0
- package/src/color/normalize.ts +7 -0
- package/src/color/okhcl.ts +201 -0
- package/src/comments/document.ts +51 -0
- package/src/comments/list.ts +52 -0
- package/src/comments/merge.ts +63 -0
- package/src/comments/threads.ts +156 -0
- package/src/components/exposure.ts +61 -0
- package/src/components/properties.ts +18 -15
- package/src/components/variants.ts +53 -0
- package/src/constants.ts +2 -0
- package/src/coordinate.ts +100 -43
- package/src/copy.ts +13 -5
- package/src/css/grid.ts +70 -0
- package/src/css/index.ts +9 -0
- package/src/css/layout.ts +53 -0
- package/src/css/shadows.ts +68 -0
- package/src/css/values.ts +42 -0
- package/src/export-format.ts +12 -0
- package/src/fields/geometry.ts +11 -0
- package/src/fields/text.ts +35 -0
- package/src/gradient.ts +166 -0
- package/src/group-bounds.ts +131 -0
- package/src/guides.ts +39 -0
- package/src/hit-test.ts +219 -25
- package/src/icons/glyph.ts +128 -0
- package/src/icons/model.ts +54 -0
- package/src/icons/schema.ts +18 -0
- package/src/index.ts +344 -56
- package/src/instance-overrides.ts +27 -0
- package/src/instances/copies.ts +239 -0
- package/src/instances/cycles.ts +73 -0
- package/src/instances/fields.ts +143 -0
- package/src/instances/main-component.ts +19 -0
- package/src/instances/placement.ts +29 -0
- package/src/instances/source-walk.ts +41 -0
- package/src/instances/sync.ts +599 -0
- package/src/instances.ts +122 -330
- package/src/layout/aspect-ratio.ts +51 -0
- package/src/{layout-guides.ts → layout/guides.ts} +1 -1
- package/src/layout/index.ts +4 -0
- package/src/layout/keywords.ts +73 -0
- package/src/layout/sizing.ts +196 -0
- package/src/mutation-impact.ts +25 -10
- package/src/node-defaults.ts +50 -2
- package/src/order-keys.ts +162 -0
- package/src/parse-path.ts +10 -2
- package/src/plugin-data/field.ts +131 -0
- package/src/plugin-data/fields.ts +246 -0
- package/src/polygon.ts +211 -0
- package/src/preview.ts +1 -1
- package/src/random.ts +28 -0
- package/src/resize.ts +99 -12
- package/src/scaling/index.ts +2 -0
- package/src/scaling/node.ts +164 -0
- package/src/scaling/tree.ts +50 -0
- package/src/shaders/model.ts +98 -0
- package/src/shaders/schema.ts +48 -0
- package/src/slots/authoring.ts +101 -0
- package/src/slots/content.ts +124 -0
- package/src/slots/frames.ts +160 -0
- package/src/slots/limits.ts +49 -0
- package/src/text/direction.ts +55 -0
- package/src/text/index.ts +2 -0
- package/src/text/lists.ts +138 -0
- package/src/text/paragraphs.ts +241 -0
- package/src/{text-picture.ts → text/picture.ts} +21 -29
- package/src/transfer/apply.ts +84 -0
- package/src/transfer/history.ts +49 -0
- package/src/transfer/identity.ts +16 -0
- package/src/transfer/index.ts +6 -0
- package/src/transfer/nodes.ts +108 -0
- package/src/transfer/plan.ts +107 -0
- package/src/transfer/references.ts +9 -0
- package/src/transfer/remove.ts +39 -0
- package/src/transfer/validation.ts +54 -0
- package/src/transfer/variables.ts +82 -0
- package/src/types.ts +221 -11
- package/src/undo.ts +54 -2
- package/src/variables/bindings.ts +186 -0
- package/src/variables/fields.ts +43 -0
- package/src/variables/token.ts +14 -0
- package/src/variables/units.ts +15 -0
- package/src/variables.ts +118 -69
- package/src/variant-properties.ts +50 -0
- package/dist/chunks/components/properties.js.map +0 -1
- package/dist/chunks/export-scale.js +0 -19
- package/dist/chunks/export-scale.js.map +0 -1
- package/dist/coordinate.d.ts.map +0 -1
- package/dist/coordinate2.d.ts +0 -2
- package/dist/guides2.d.ts +0 -11
- package/dist/guides2.d.ts.map +0 -1
- package/dist/hit-test.d.ts.map +0 -1
- package/dist/layout-guides.d.ts +0 -2
- package/dist/layout-guides.js +0 -2
- package/dist/layout-guides2.d.ts +0 -16
- package/dist/layout-guides2.d.ts.map +0 -1
- package/dist/node-defaults.d.ts.map +0 -1
- package/dist/preview2.d.ts +0 -12
- package/dist/preview2.d.ts.map +0 -1
- package/dist/resize.js.map +0 -1
- package/dist/variables.d.ts.map +0 -1
- package/dist/vector-network2.d.ts +0 -32
- package/dist/vector-network2.d.ts.map +0 -1
- package/src/instance-overrides.test.ts +0 -67
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { SceneGraph } from '../index'
|
|
2
|
+
import type { SceneNode } from '../types'
|
|
3
|
+
import { behaviourOwner, readBehaviour } from './model'
|
|
4
|
+
|
|
5
|
+
/** Whether an instance's main component, or its set, has a behaviour. */
|
|
6
|
+
export function hasBehaviour(graph: SceneGraph, node: SceneNode): boolean {
|
|
7
|
+
if (node.type !== 'INSTANCE' || !node.componentId) return false
|
|
8
|
+
const component = graph.getNode(node.componentId)
|
|
9
|
+
const owner = component && behaviourOwner(graph, component)
|
|
10
|
+
return !!owner && !!readBehaviour(owner)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A name as a path segment: `/` separates segments and `#` the twin index, so both are escaped. */
|
|
14
|
+
const pathName = (name: string) => name.replace(/[%/#]/g, (char) => encodeURIComponent(char))
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A layer's path below a root: the names of the layers down to it, with the position among
|
|
18
|
+
* same-named siblings when names repeat. It stays the same when a variant switch rebuilds an
|
|
19
|
+
* instance's layers, so it identifies controls and keeps their DOM in place.
|
|
20
|
+
*/
|
|
21
|
+
export function layerPath(graph: SceneGraph, rootId: string, nodeId: string): string {
|
|
22
|
+
const segments: string[] = []
|
|
23
|
+
// `closest` stops on a parent cycle in bad data, where a hand-written walk would not.
|
|
24
|
+
graph.closest(nodeId, (node) => {
|
|
25
|
+
if (node.id === rootId) return true
|
|
26
|
+
const twins = node.parentId
|
|
27
|
+
? graph.getChildren(node.parentId).filter((child) => child.name === node.name)
|
|
28
|
+
: []
|
|
29
|
+
const index = twins.findIndex((child) => child.id === node.id)
|
|
30
|
+
segments.unshift(twins.length > 1 ? `${pathName(node.name)}#${index}` : pathName(node.name))
|
|
31
|
+
return false
|
|
32
|
+
})
|
|
33
|
+
return segments.join('/')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The layer at `path` below a root, the inverse of `layerPath`. */
|
|
37
|
+
export function findLayerByPath(
|
|
38
|
+
graph: SceneGraph,
|
|
39
|
+
rootId: string,
|
|
40
|
+
path: string
|
|
41
|
+
): SceneNode | undefined {
|
|
42
|
+
let current = graph.getNode(rootId)
|
|
43
|
+
for (const segment of path ? path.split('/') : []) {
|
|
44
|
+
if (!current) return undefined
|
|
45
|
+
const hash = segment.lastIndexOf('#')
|
|
46
|
+
const name = hash === -1 ? segment : segment.slice(0, hash)
|
|
47
|
+
const twins = graph.getChildren(current.id).filter((child) => pathName(child.name) === name)
|
|
48
|
+
current = twins.at(hash === -1 ? 0 : Number(segment.slice(hash + 1)))
|
|
49
|
+
}
|
|
50
|
+
return current
|
|
51
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { SceneGraph } from '../index'
|
|
2
|
+
import { readPluginData, withPluginData } from '../plugin-data/field'
|
|
3
|
+
import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'
|
|
4
|
+
import type { ComponentPropertyDefinition, SceneNode } from '../types'
|
|
5
|
+
import {
|
|
6
|
+
behaviourContract,
|
|
7
|
+
INTERACTION_STATES,
|
|
8
|
+
type BehaviourKind,
|
|
9
|
+
type InteractionState
|
|
10
|
+
} from './kinds'
|
|
11
|
+
import type {
|
|
12
|
+
Behaviour,
|
|
13
|
+
BehaviourBooleanBinding,
|
|
14
|
+
BehaviourInteractionStates,
|
|
15
|
+
BehaviourNumberSettings
|
|
16
|
+
} from './schema'
|
|
17
|
+
|
|
18
|
+
export type {
|
|
19
|
+
Behaviour,
|
|
20
|
+
BehaviourBooleanBinding,
|
|
21
|
+
BehaviourInteractionStates,
|
|
22
|
+
BehaviourNumberSettings
|
|
23
|
+
} from './schema'
|
|
24
|
+
export { isNumberRange } from './schema'
|
|
25
|
+
|
|
26
|
+
export const DEFAULT_NUMBER_SETTINGS: BehaviourNumberSettings = {
|
|
27
|
+
min: 0,
|
|
28
|
+
max: 100,
|
|
29
|
+
step: 1,
|
|
30
|
+
default: 50
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A boolean value's binding, if the behaviour has one. */
|
|
34
|
+
export function booleanBinding(
|
|
35
|
+
behaviour: Behaviour,
|
|
36
|
+
valueId: string
|
|
37
|
+
): BehaviourBooleanBinding | undefined {
|
|
38
|
+
return Object.hasOwn(behaviour.booleans, valueId) ? behaviour.booleans[valueId] : undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The slot property a part is bound to, if any. */
|
|
42
|
+
export function partBinding(behaviour: Behaviour, partId: string): string | undefined {
|
|
43
|
+
return Object.hasOwn(behaviour.parts, partId) ? behaviour.parts[partId] : undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The text property a text value is bound to, if any. */
|
|
47
|
+
export function textBinding(behaviour: Behaviour, valueId: string): string | undefined {
|
|
48
|
+
return Object.hasOwn(behaviour.texts, valueId) ? behaviour.texts[valueId].propertyId : undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A number value's range, if the behaviour has one. */
|
|
52
|
+
export function numberSettings(
|
|
53
|
+
behaviour: Behaviour,
|
|
54
|
+
valueId: string
|
|
55
|
+
): BehaviourNumberSettings | undefined {
|
|
56
|
+
return Object.hasOwn(behaviour.numbers, valueId) ? behaviour.numbers[valueId] : undefined
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A new behaviour of a kind, its number values at their defaults and nothing bound. */
|
|
60
|
+
export function emptyBehaviour(kind: BehaviourKind): Behaviour {
|
|
61
|
+
const contract = behaviourContract(kind)
|
|
62
|
+
const numbers = Object.fromEntries(
|
|
63
|
+
contract.values
|
|
64
|
+
.filter((value) => value.type === 'number')
|
|
65
|
+
.map((value) => [value.id, { ...DEFAULT_NUMBER_SETTINGS }])
|
|
66
|
+
)
|
|
67
|
+
return { kind, booleans: {}, texts: {}, numbers, parts: {} }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The node that keeps the behaviour: a component set for its variants, otherwise the main
|
|
72
|
+
* component itself.
|
|
73
|
+
*/
|
|
74
|
+
export function behaviourOwner(graph: SceneGraph, node: SceneNode): SceneNode | undefined {
|
|
75
|
+
if (node.type === 'COMPONENT_SET') return node
|
|
76
|
+
if (node.type !== 'COMPONENT') return undefined
|
|
77
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : undefined
|
|
78
|
+
return parent?.type === 'COMPONENT_SET' ? parent : node
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The behaviour a component or component set keeps, or null when it has none or it is unreadable. */
|
|
82
|
+
export function readBehaviour(owner: SceneNode): Behaviour | null {
|
|
83
|
+
return readPluginData(owner.pluginData, OPEN_PENCIL_PLUGIN_DATA.behaviour) ?? null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The owner's plugin data with the behaviour set, or removed when null. */
|
|
87
|
+
export function withBehaviour(
|
|
88
|
+
owner: SceneNode,
|
|
89
|
+
behaviour: Behaviour | null
|
|
90
|
+
): SceneNode['pluginData'] {
|
|
91
|
+
return withPluginData(owner.pluginData, OPEN_PENCIL_PLUGIN_DATA.behaviour, behaviour ?? undefined)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Component properties the behaviour can bind to: the owner's, and a set's variants' own. */
|
|
95
|
+
export function behaviourProperties(
|
|
96
|
+
graph: SceneGraph,
|
|
97
|
+
owner: SceneNode
|
|
98
|
+
): ComponentPropertyDefinition[] {
|
|
99
|
+
const variants =
|
|
100
|
+
owner.type === 'COMPONENT_SET'
|
|
101
|
+
? owner.childIds.flatMap((id) => graph.getNode(id)?.componentPropertyDefinitions ?? [])
|
|
102
|
+
: []
|
|
103
|
+
const seen = new Set<string>()
|
|
104
|
+
return [...owner.componentPropertyDefinitions, ...variants].filter((definition) => {
|
|
105
|
+
if (seen.has(definition.id)) return false
|
|
106
|
+
seen.add(definition.id)
|
|
107
|
+
return true
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Required values and parts that are unbound, or bound to a property the component no longer
|
|
113
|
+
* has, by value or part id; `states` when the interaction states lost their variant property.
|
|
114
|
+
*/
|
|
115
|
+
export function missingBindings(
|
|
116
|
+
graph: SceneGraph,
|
|
117
|
+
owner: SceneNode,
|
|
118
|
+
behaviour: Behaviour
|
|
119
|
+
): string[] {
|
|
120
|
+
const contract = behaviourContract(behaviour.kind)
|
|
121
|
+
const properties = new Map(
|
|
122
|
+
behaviourProperties(graph, owner).map((definition) => [definition.id, definition])
|
|
123
|
+
)
|
|
124
|
+
const bound = (propertyId: string | undefined, type: ComponentPropertyDefinition['type'][]) => {
|
|
125
|
+
const definition = propertyId ? properties.get(propertyId) : undefined
|
|
126
|
+
return !!definition && type.includes(definition.type)
|
|
127
|
+
}
|
|
128
|
+
const values = contract.values
|
|
129
|
+
.filter((value) => value.required)
|
|
130
|
+
.filter((value) =>
|
|
131
|
+
value.type === 'text'
|
|
132
|
+
? !bound(textBinding(behaviour, value.id), ['TEXT'])
|
|
133
|
+
: value.type === 'boolean' &&
|
|
134
|
+
!bound(booleanBinding(behaviour, value.id)?.propertyId, ['VARIANT', 'BOOLEAN'])
|
|
135
|
+
)
|
|
136
|
+
.map((value) => value.id)
|
|
137
|
+
const parts = contract.parts
|
|
138
|
+
.filter((part) => part.required && !bound(partBinding(behaviour, part.id), ['SLOT']))
|
|
139
|
+
.map((part) => part.id)
|
|
140
|
+
const states =
|
|
141
|
+
behaviour.states && !bound(behaviour.states.propertyId, ['VARIANT']) ? ['states'] : []
|
|
142
|
+
return [...values, ...parts, ...states]
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Variant value names that usually mean each interaction state, compared case-insensitively. */
|
|
146
|
+
const STATE_NAMES: Record<InteractionState, readonly string[]> = {
|
|
147
|
+
rest: ['default', 'rest', 'idle', 'normal', 'enabled'],
|
|
148
|
+
hover: ['hover', 'hovered', 'hovering'],
|
|
149
|
+
pressed: ['pressed', 'active', 'down', 'pressing'],
|
|
150
|
+
focus: ['focus', 'focused', 'focus visible', 'focus-visible'],
|
|
151
|
+
disabled: ['disabled', 'inactive']
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Interaction states for a variant property, each mapped to the value whose name means it, such
|
|
156
|
+
* as `Hover` or `Pressed`; states without such a value are left unset.
|
|
157
|
+
*/
|
|
158
|
+
export function guessInteractionStates(
|
|
159
|
+
propertyId: string,
|
|
160
|
+
values: readonly string[]
|
|
161
|
+
): BehaviourInteractionStates {
|
|
162
|
+
const states: BehaviourInteractionStates = { propertyId }
|
|
163
|
+
for (const state of INTERACTION_STATES) {
|
|
164
|
+
const match = values.find((value) => STATE_NAMES[state].includes(value.trim().toLowerCase()))
|
|
165
|
+
if (match) states[state] = match
|
|
166
|
+
}
|
|
167
|
+
return states
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* The behaviour with every component property id it binds renamed by `rename`, for formats that
|
|
172
|
+
* give properties new ids on write, such as `.fig` with its GUIDs.
|
|
173
|
+
*/
|
|
174
|
+
export function renameBehaviourProperties(
|
|
175
|
+
behaviour: Behaviour,
|
|
176
|
+
rename: (propertyId: string) => string
|
|
177
|
+
): Behaviour {
|
|
178
|
+
const mapValues = <T>(record: Record<string, T>, change: (value: T) => T) =>
|
|
179
|
+
Object.fromEntries(Object.entries(record).map(([key, value]) => [key, change(value)]))
|
|
180
|
+
return {
|
|
181
|
+
...behaviour,
|
|
182
|
+
booleans: mapValues(behaviour.booleans, (binding) => ({
|
|
183
|
+
...binding,
|
|
184
|
+
propertyId: rename(binding.propertyId)
|
|
185
|
+
})),
|
|
186
|
+
texts: mapValues(behaviour.texts, (binding) => ({ propertyId: rename(binding.propertyId) })),
|
|
187
|
+
parts: mapValues(behaviour.parts, rename),
|
|
188
|
+
...(behaviour.states
|
|
189
|
+
? { states: { ...behaviour.states, propertyId: rename(behaviour.states.propertyId) } }
|
|
190
|
+
: {})
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import { BEHAVIOUR_KINDS, type InteractionState } from './kinds'
|
|
4
|
+
|
|
5
|
+
const BooleanBinding = v.object({
|
|
6
|
+
propertyId: v.string(),
|
|
7
|
+
/** For a variant property: the values that mean on and off. */
|
|
8
|
+
on: v.optional(v.string()),
|
|
9
|
+
off: v.optional(v.string())
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
/** Whether a number value's range can be stepped through: `max` above `min`, a positive step. */
|
|
13
|
+
export function isNumberRange(settings: { min: number; max: number; step: number }): boolean {
|
|
14
|
+
return settings.max > settings.min && settings.step > 0
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const NumberSettings = v.pipe(
|
|
18
|
+
v.object({
|
|
19
|
+
min: v.pipe(v.number(), v.finite()),
|
|
20
|
+
max: v.pipe(v.number(), v.finite()),
|
|
21
|
+
step: v.pipe(v.number(), v.finite()),
|
|
22
|
+
default: v.pipe(v.number(), v.finite())
|
|
23
|
+
}),
|
|
24
|
+
v.check(
|
|
25
|
+
(settings) => isNumberRange(settings),
|
|
26
|
+
'A number value needs max above min and a positive step'
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
/** Each interaction state's variant value, by state; a state left out shows the rest value. */
|
|
31
|
+
export const interactionStateValues = {
|
|
32
|
+
rest: v.optional(v.string()),
|
|
33
|
+
hover: v.optional(v.string()),
|
|
34
|
+
pressed: v.optional(v.string()),
|
|
35
|
+
focus: v.optional(v.string()),
|
|
36
|
+
disabled: v.optional(v.string())
|
|
37
|
+
} satisfies Record<InteractionState, unknown>
|
|
38
|
+
|
|
39
|
+
const InteractionStates = v.object({ propertyId: v.string(), ...interactionStateValues })
|
|
40
|
+
|
|
41
|
+
export const behaviourSchema = v.object({
|
|
42
|
+
kind: v.picklist(BEHAVIOUR_KINDS),
|
|
43
|
+
/** Boolean values, by value id, bound to component properties. */
|
|
44
|
+
booleans: v.record(v.string(), BooleanBinding),
|
|
45
|
+
/** Text values, by value id, bound to text properties. */
|
|
46
|
+
texts: v.optional(v.record(v.string(), v.object({ propertyId: v.string() })), {}),
|
|
47
|
+
/** Number values, by value id: the range the behaviour keeps itself. */
|
|
48
|
+
numbers: v.record(v.string(), NumberSettings),
|
|
49
|
+
/** Parts, by part id, bound to slot properties. */
|
|
50
|
+
parts: v.record(v.string(), v.string()),
|
|
51
|
+
states: v.optional(InteractionStates)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
/** How a main component behaves as a control, as kept in its plugin data. */
|
|
55
|
+
export type Behaviour = v.InferOutput<typeof behaviourSchema>
|
|
56
|
+
export type BehaviourBooleanBinding = v.InferOutput<typeof BooleanBinding>
|
|
57
|
+
export type BehaviourNumberSettings = v.InferOutput<typeof NumberSettings>
|
|
58
|
+
export type BehaviourInteractionStates = v.InferOutput<typeof InteractionStates>
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { omitBy } from 'es-toolkit/object'
|
|
2
|
+
import * as v from 'valibot'
|
|
3
|
+
|
|
4
|
+
import type { SceneGraph } from '../index'
|
|
5
|
+
import type { ComponentPropertyDefinition, SceneNode } from '../types'
|
|
6
|
+
import { BEHAVIOUR_KINDS, behaviourContract, type BehaviourKind } from './kinds'
|
|
7
|
+
import {
|
|
8
|
+
behaviourProperties,
|
|
9
|
+
emptyBehaviour,
|
|
10
|
+
guessInteractionStates,
|
|
11
|
+
textBinding,
|
|
12
|
+
type Behaviour,
|
|
13
|
+
type BehaviourNumberSettings
|
|
14
|
+
} from './model'
|
|
15
|
+
import { interactionStateValues, isNumberRange } from './schema'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A behaviour as scripts, tools, and JSX write it: component properties and slots by name, not
|
|
19
|
+
* by id. Values are boolean or text values by their contract id; numbers are a slider's or
|
|
20
|
+
* number field's own range.
|
|
21
|
+
*/
|
|
22
|
+
export const behaviourSpecSchema = v.object({
|
|
23
|
+
kind: v.picklist(BEHAVIOUR_KINDS),
|
|
24
|
+
values: v.optional(
|
|
25
|
+
v.record(
|
|
26
|
+
v.string(),
|
|
27
|
+
v.union([
|
|
28
|
+
v.string(),
|
|
29
|
+
v.object({ property: v.string(), on: v.optional(v.string()), off: v.optional(v.string()) })
|
|
30
|
+
])
|
|
31
|
+
)
|
|
32
|
+
),
|
|
33
|
+
numbers: v.optional(
|
|
34
|
+
v.record(
|
|
35
|
+
v.string(),
|
|
36
|
+
v.object({
|
|
37
|
+
min: v.optional(v.number()),
|
|
38
|
+
max: v.optional(v.number()),
|
|
39
|
+
step: v.optional(v.number()),
|
|
40
|
+
default: v.optional(v.number())
|
|
41
|
+
})
|
|
42
|
+
)
|
|
43
|
+
),
|
|
44
|
+
parts: v.optional(v.record(v.string(), v.string())),
|
|
45
|
+
states: v.optional(
|
|
46
|
+
v.union([v.string(), v.object({ property: v.string(), ...interactionStateValues })])
|
|
47
|
+
)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
export type BehaviourSpec = v.InferOutput<typeof behaviourSpecSchema>
|
|
51
|
+
|
|
52
|
+
/** Variant values that usually mean on, compared case-insensitively. */
|
|
53
|
+
const ON_NAMES = ['on', 'true', 'yes', 'checked', 'pressed', 'open', 'selected', 'active']
|
|
54
|
+
const OFF_NAMES = ['off', 'false', 'no', 'unchecked', 'closed', 'default', 'inactive']
|
|
55
|
+
|
|
56
|
+
function named(names: readonly string[], options: readonly string[]): string | undefined {
|
|
57
|
+
return options.find((option) => names.includes(option.trim().toLowerCase()))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The variant values that likely mean on and off: names such as On, True, or Checked first,
|
|
62
|
+
* then the values in order.
|
|
63
|
+
*/
|
|
64
|
+
export function guessOnOff(
|
|
65
|
+
options: readonly string[],
|
|
66
|
+
explicit: { on?: string; off?: string } = {}
|
|
67
|
+
): { on?: string; off?: string } {
|
|
68
|
+
const on = explicit.on ?? named(ON_NAMES, options) ?? options[0]
|
|
69
|
+
const off = explicit.off ?? named(OFF_NAMES, options) ?? options.find((option) => option !== on)
|
|
70
|
+
return { on, off }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function findProperty(
|
|
74
|
+
properties: readonly ComponentPropertyDefinition[],
|
|
75
|
+
name: string,
|
|
76
|
+
types: readonly ComponentPropertyDefinition['type'][],
|
|
77
|
+
what: string
|
|
78
|
+
): ComponentPropertyDefinition {
|
|
79
|
+
const found = properties.find((item) => item.name === name && types.includes(item.type))
|
|
80
|
+
if (found) return found
|
|
81
|
+
const available = properties.filter((item) => types.includes(item.type)).map((item) => item.name)
|
|
82
|
+
throw new Error(
|
|
83
|
+
`${what} needs a ${types.join(' or ')} property named "${name}"; the component has ${
|
|
84
|
+
available.length ? available.map((item) => `"${item}"`).join(', ') : 'none'
|
|
85
|
+
}`
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type ValueBinding = NonNullable<BehaviourSpec['values']>[string]
|
|
90
|
+
type StatesSpec = NonNullable<BehaviourSpec['states']>
|
|
91
|
+
|
|
92
|
+
function listed(items: readonly { id: string }[]): string {
|
|
93
|
+
return items.map((item) => `"${item.id}"`).join(', ')
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Bind a boolean or text value to the property named in its binding. */
|
|
97
|
+
function resolveValue(
|
|
98
|
+
behaviour: Behaviour,
|
|
99
|
+
properties: readonly ComponentPropertyDefinition[],
|
|
100
|
+
valueId: string,
|
|
101
|
+
binding: ValueBinding
|
|
102
|
+
): void {
|
|
103
|
+
const contract = behaviourContract(behaviour.kind)
|
|
104
|
+
const bindable = contract.values.filter((item) => item.type === 'boolean' || item.type === 'text')
|
|
105
|
+
const value = bindable.find((item) => item.id === valueId)
|
|
106
|
+
if (!value)
|
|
107
|
+
throw new Error(
|
|
108
|
+
`A ${behaviour.kind} has no boolean or text value "${valueId}"; it has ${listed(bindable)}`
|
|
109
|
+
)
|
|
110
|
+
const name = typeof binding === 'string' ? binding : binding.property
|
|
111
|
+
if (value.type === 'text') {
|
|
112
|
+
behaviour.texts[valueId] = { propertyId: findProperty(properties, name, ['TEXT'], valueId).id }
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
const definition = findProperty(properties, name, ['VARIANT', 'BOOLEAN'], valueId)
|
|
116
|
+
if (definition.type === 'BOOLEAN') {
|
|
117
|
+
behaviour.booleans[valueId] = { propertyId: definition.id }
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
const options = definition.variantOptions ?? []
|
|
121
|
+
const explicit: { on?: string; off?: string } = typeof binding === 'string' ? {} : binding
|
|
122
|
+
const { on, off } = guessOnOff(options, explicit)
|
|
123
|
+
for (const choice of [on, off])
|
|
124
|
+
if (choice !== undefined && !options.includes(choice))
|
|
125
|
+
throw new Error(`"${definition.name}" has no value "${choice}"; it has ${options.join(', ')}`)
|
|
126
|
+
behaviour.booleans[valueId] = { propertyId: definition.id, on, off }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function resolveStates(
|
|
130
|
+
behaviour: Behaviour,
|
|
131
|
+
properties: readonly ComponentPropertyDefinition[],
|
|
132
|
+
states: StatesSpec
|
|
133
|
+
): void {
|
|
134
|
+
const name = typeof states === 'string' ? states : states.property
|
|
135
|
+
const definition = findProperty(properties, name, ['VARIANT'], 'states')
|
|
136
|
+
const guessed = guessInteractionStates(definition.id, definition.variantOptions ?? [])
|
|
137
|
+
if (typeof states === 'string') {
|
|
138
|
+
behaviour.states = guessed
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
const { property: _property, ...values } = states
|
|
142
|
+
behaviour.states = { ...guessed, ...stripUndefined(values) }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The stored behaviour for a spec, with names resolved to the owner's property and slot ids. A
|
|
147
|
+
* name the component lacks, a value the kind does not have, or a variant value the property
|
|
148
|
+
* does not offer fails with a message naming what exists.
|
|
149
|
+
*/
|
|
150
|
+
export function behaviourFromSpec(
|
|
151
|
+
graph: SceneGraph,
|
|
152
|
+
owner: SceneNode,
|
|
153
|
+
spec: BehaviourSpec
|
|
154
|
+
): Behaviour {
|
|
155
|
+
const contract = behaviourContract(spec.kind)
|
|
156
|
+
const properties = behaviourProperties(graph, owner)
|
|
157
|
+
const behaviour = emptyBehaviour(spec.kind)
|
|
158
|
+
for (const [valueId, binding] of Object.entries(spec.values ?? {}))
|
|
159
|
+
resolveValue(behaviour, properties, valueId, binding)
|
|
160
|
+
const numbers = contract.values.filter((item) => item.type === 'number')
|
|
161
|
+
for (const [valueId, settings] of Object.entries(spec.numbers ?? {})) {
|
|
162
|
+
if (!numbers.some((item) => item.id === valueId))
|
|
163
|
+
throw new Error(`A ${spec.kind} has no number value "${valueId}"; it has ${listed(numbers)}`)
|
|
164
|
+
const range = { ...behaviour.numbers[valueId], ...stripUndefined(settings) }
|
|
165
|
+
if (!isNumberRange(range))
|
|
166
|
+
throw new Error(`"${valueId}" needs max above min and a positive step`)
|
|
167
|
+
behaviour.numbers[valueId] = range
|
|
168
|
+
}
|
|
169
|
+
for (const [partId, slotName] of Object.entries(spec.parts ?? {})) {
|
|
170
|
+
if (!contract.parts.some((item) => item.id === partId))
|
|
171
|
+
throw new Error(`A ${spec.kind} has no part "${partId}"; it has ${listed(contract.parts)}`)
|
|
172
|
+
const id = findProperty(properties, slotName, ['SLOT'], partId).id
|
|
173
|
+
const taken = Object.entries(behaviour.parts).find(([, slot]) => slot === id)?.[0]
|
|
174
|
+
if (taken) throw new Error(`"${slotName}" is already the ${taken}; a slot draws one part`)
|
|
175
|
+
behaviour.parts[partId] = id
|
|
176
|
+
}
|
|
177
|
+
if (spec.states !== undefined) resolveStates(behaviour, properties, spec.states)
|
|
178
|
+
return behaviour
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function stripUndefined<T extends Record<string, unknown>>(record: T): Partial<T> {
|
|
182
|
+
return omitBy(record, (value) => value === undefined)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The spec a stored behaviour reads as, with ids back to names; unresolved bindings are left out. */
|
|
186
|
+
export function behaviourToSpec(
|
|
187
|
+
graph: SceneGraph,
|
|
188
|
+
owner: SceneNode,
|
|
189
|
+
behaviour: Behaviour
|
|
190
|
+
): BehaviourSpec {
|
|
191
|
+
const properties = behaviourProperties(graph, owner)
|
|
192
|
+
const nameOf = (id: string | undefined) => properties.find((item) => item.id === id)?.name
|
|
193
|
+
const values: NonNullable<BehaviourSpec['values']> = {}
|
|
194
|
+
for (const [valueId, binding] of Object.entries(behaviour.booleans)) {
|
|
195
|
+
const name = nameOf(binding.propertyId)
|
|
196
|
+
if (name)
|
|
197
|
+
values[valueId] =
|
|
198
|
+
binding.on || binding.off ? { property: name, on: binding.on, off: binding.off } : name
|
|
199
|
+
}
|
|
200
|
+
for (const valueId of Object.keys(behaviour.texts)) {
|
|
201
|
+
const name = nameOf(textBinding(behaviour, valueId))
|
|
202
|
+
if (name) values[valueId] = name
|
|
203
|
+
}
|
|
204
|
+
const parts = Object.fromEntries(
|
|
205
|
+
Object.entries(behaviour.parts).flatMap(([partId, id]) => {
|
|
206
|
+
const name = nameOf(id)
|
|
207
|
+
return name ? [[partId, name]] : []
|
|
208
|
+
})
|
|
209
|
+
)
|
|
210
|
+
const statesName = nameOf(behaviour.states?.propertyId)
|
|
211
|
+
const spec: BehaviourSpec = { kind: behaviour.kind }
|
|
212
|
+
if (Object.keys(values).length) spec.values = values
|
|
213
|
+
if (Object.keys(behaviour.numbers).length) spec.numbers = structuredClone(behaviour.numbers)
|
|
214
|
+
if (Object.keys(parts).length) spec.parts = parts
|
|
215
|
+
if (behaviour.states && statesName)
|
|
216
|
+
spec.states = {
|
|
217
|
+
...stripUndefined({ ...behaviour.states, propertyId: undefined }),
|
|
218
|
+
property: statesName
|
|
219
|
+
}
|
|
220
|
+
return spec
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export type { BehaviourKind, BehaviourNumberSettings }
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createNanoEvents, type Emitter } from 'nanoevents'
|
|
2
|
+
|
|
3
|
+
import type { SceneGraphEvents } from './types'
|
|
4
|
+
|
|
5
|
+
export class CommittedGraphEventError extends AggregateError {
|
|
6
|
+
readonly committed = true
|
|
7
|
+
|
|
8
|
+
constructor(errors: readonly unknown[]) {
|
|
9
|
+
super(errors, 'Committed graph event delivery failed')
|
|
10
|
+
this.name = 'CommittedGraphEventError'
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Stable emitter identity with synchronous, nestable event buffering. */
|
|
15
|
+
export class BufferedSceneEmitter implements Emitter<SceneGraphEvents> {
|
|
16
|
+
private readonly delegate = createNanoEvents<SceneGraphEvents>()
|
|
17
|
+
private readonly buffers: Array<Array<() => void>> = []
|
|
18
|
+
|
|
19
|
+
get events(): Emitter<SceneGraphEvents>['events'] {
|
|
20
|
+
return this.delegate.events
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
set events(events: Emitter<SceneGraphEvents>['events']) {
|
|
24
|
+
this.delegate.events = events
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
on<K extends keyof SceneGraphEvents>(event: K, listener: SceneGraphEvents[K]) {
|
|
28
|
+
return this.delegate.on(event, listener)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
emit<K extends keyof SceneGraphEvents>(event: K, ...args: Parameters<SceneGraphEvents[K]>): void {
|
|
32
|
+
const buffer = this.buffers.at(-1)
|
|
33
|
+
if (buffer) buffer.push(() => this.delegate.emit(event, ...args))
|
|
34
|
+
else this.delegate.emit(event, ...args)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Buffers events, not mutations. Callers own rollback if the action throws. */
|
|
38
|
+
batch<T>(action: () => T): T {
|
|
39
|
+
const pending: Array<() => void> = []
|
|
40
|
+
this.buffers.push(pending)
|
|
41
|
+
let result: T
|
|
42
|
+
try {
|
|
43
|
+
result = action()
|
|
44
|
+
} catch (error) {
|
|
45
|
+
this.buffers.pop()
|
|
46
|
+
throw error
|
|
47
|
+
}
|
|
48
|
+
this.buffers.pop()
|
|
49
|
+
const outer = this.buffers.at(-1)
|
|
50
|
+
if (outer) outer.push(...pending)
|
|
51
|
+
else {
|
|
52
|
+
const errors: unknown[] = []
|
|
53
|
+
for (const publish of pending) {
|
|
54
|
+
try {
|
|
55
|
+
publish()
|
|
56
|
+
} catch (error) {
|
|
57
|
+
errors.push(error)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (errors.length) throw new CommittedGraphEventError(errors)
|
|
61
|
+
}
|
|
62
|
+
return result
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { wcagContrast } from 'culori'
|
|
2
|
+
|
|
3
|
+
import { BLACK, WHITE } from '../constants'
|
|
4
|
+
import type { Color } from '../primitives'
|
|
5
|
+
|
|
6
|
+
/** Opaque color seen when `foreground` at `alpha` is drawn over an opaque `background`. */
|
|
7
|
+
export function compositeOver(foreground: Color, background: Color, alpha = foreground.a): Color {
|
|
8
|
+
const mix = (top: number, bottom: number) => top * alpha + bottom * (1 - alpha)
|
|
9
|
+
return {
|
|
10
|
+
r: mix(foreground.r, background.r),
|
|
11
|
+
g: mix(foreground.g, background.g),
|
|
12
|
+
b: mix(foreground.b, background.b),
|
|
13
|
+
a: 1
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** WCAG 2 contrast ratio of two colors, from 1 to 21; alpha is ignored, so composite first. */
|
|
18
|
+
export function contrastRatio(
|
|
19
|
+
a: Pick<Color, 'r' | 'g' | 'b'>,
|
|
20
|
+
b: Pick<Color, 'r' | 'g' | 'b'>
|
|
21
|
+
): number {
|
|
22
|
+
return wcagContrast(
|
|
23
|
+
{ mode: 'rgb', r: a.r, g: a.g, b: a.b },
|
|
24
|
+
{ mode: 'rgb', r: b.r, g: b.g, b: b.b }
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Black or white, whichever reads better on an opaque `background`. */
|
|
29
|
+
export function readableForeground(background: Pick<Color, 'r' | 'g' | 'b'>): Color {
|
|
30
|
+
return contrastRatio(background, BLACK) >= contrastRatio(background, WHITE) ? BLACK : WHITE
|
|
31
|
+
}
|