@open-pencil/scene-graph 0.13.2 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/arrow-caps.d.ts +44 -0
- package/dist/arrow-caps.d.ts.map +1 -0
- package/dist/arrow-caps.js +131 -0
- package/dist/arrow-caps.js.map +1 -0
- package/dist/chunks/checkpoint.js +78 -0
- package/dist/chunks/checkpoint.js.map +1 -0
- package/dist/chunks/components/properties.js +1571 -0
- package/dist/chunks/components/properties.js.map +1 -0
- package/dist/chunks/coordinate.js +87 -33
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/font-style.js +2 -1
- package/dist/chunks/font-style.js.map +1 -1
- package/dist/chunks/index.js +1283 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/constants.js +1 -18
- package/dist/constants2.d.ts +0 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +107 -11
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +2 -2
- package/dist/coordinate2.d.ts +2 -2
- package/dist/copy.d.ts +2 -2
- package/dist/copy.js +2 -2
- package/dist/geometry.d.ts +2 -2
- package/dist/geometry.js +2 -431
- package/dist/guides.d.ts +2 -0
- package/dist/guides.js +0 -0
- package/dist/guides2.d.ts +11 -0
- package/dist/guides2.d.ts.map +1 -0
- package/dist/hit-test.d.ts +1 -2
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +26 -14
- package/dist/hit-test.js.map +1 -1
- package/dist/images.js +0 -1
- package/dist/images.js.map +1 -1
- package/dist/images2.d.ts.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -6
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout-guides.d.ts +2 -0
- package/dist/layout-guides.js +2 -0
- package/dist/layout-guides2.d.ts +16 -0
- package/dist/layout-guides2.d.ts.map +1 -0
- package/dist/matrix2.d.ts +0 -1
- package/dist/matrix2.d.ts.map +1 -1
- package/dist/node-defaults.d.ts +1 -2
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/parse-path.d.ts +1 -2
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/preview.d.ts +2 -12
- package/dist/preview.js +1 -1
- package/dist/preview2.d.ts +12 -0
- package/dist/preview2.d.ts.map +1 -0
- package/dist/primitives2.d.ts.map +1 -1
- package/dist/resize.d.ts +18 -4
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +70 -22
- package/dist/resize.js.map +1 -1
- package/dist/snap.js +1 -1
- package/dist/source-metadata.d.ts +1 -2
- package/dist/source-metadata.d.ts.map +1 -1
- package/dist/source-metadata.js +1 -1
- package/dist/text-picture.d.ts +13 -2
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text-picture.js +2 -2
- package/dist/types.d.ts +499 -2
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1296
- package/dist/types2.d.ts +2 -430
- package/dist/undo.js +1 -1
- package/dist/undo2.d.ts +5 -0
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +1 -2
- package/dist/variables.d.ts.map +1 -1
- package/dist/variables.js +1 -1
- package/dist/variant-name.d.ts.map +1 -1
- package/dist/variant-properties.d.ts +10 -0
- package/dist/variant-properties.d.ts.map +1 -0
- package/dist/variant-properties.js +39 -0
- package/dist/variant-properties.js.map +1 -0
- package/dist/vector-network.js +1 -1
- package/dist/vector-network2.d.ts +1 -2
- package/dist/vector-network2.d.ts.map +1 -1
- package/package.json +40 -15
- package/src/arrow-caps.ts +146 -0
- package/src/bindings.ts +20 -0
- package/src/checkpoint.ts +123 -0
- package/src/components/properties.ts +238 -0
- package/src/constants.ts +6 -0
- package/src/coordinate.ts +226 -0
- package/src/copy.ts +294 -0
- package/src/events.ts +21 -0
- package/src/export-scale.ts +16 -0
- package/src/font-style.ts +77 -0
- package/src/geometry.ts +530 -0
- package/src/guides.ts +8 -0
- package/src/hit-test.ts +205 -0
- package/src/images.ts +34 -0
- package/src/index.ts +686 -0
- package/src/instance-overrides.test.ts +67 -0
- package/src/instance-overrides.ts +152 -0
- package/src/instances.ts +480 -0
- package/src/layout-guides.ts +103 -0
- package/src/matrix.ts +116 -0
- package/src/mutation-impact.ts +70 -0
- package/src/node-defaults.ts +186 -0
- package/src/parse-path.ts +191 -0
- package/src/preview.ts +80 -0
- package/src/primitives.ts +42 -0
- package/src/resize.ts +302 -0
- package/src/shared-styles.ts +73 -0
- package/src/snap.ts +129 -0
- package/src/source-metadata.ts +8 -0
- package/src/text-picture.ts +65 -0
- package/src/types.ts +635 -0
- package/src/undo.ts +171 -0
- package/src/variables.ts +399 -0
- package/src/variant-name.ts +15 -0
- package/src/variant-properties.ts +48 -0
- package/src/vector-network.ts +293 -0
- package/dist/chunks/copy.js +0 -576
- package/dist/chunks/copy.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/geometry.js.map +0 -1
- package/dist/preview.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/types2.d.ts.map +0 -1
package/src/undo.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
export interface UndoEntry {
|
|
2
|
+
label: string
|
|
3
|
+
forward: () => void
|
|
4
|
+
inverse: () => void
|
|
5
|
+
coalesceKey?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface UndoManagerOptions {
|
|
9
|
+
limit?: number
|
|
10
|
+
/** Called after recording, undoing, redoing, or clearing committed history. */
|
|
11
|
+
onChange?: () => void
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface UndoBatch {
|
|
15
|
+
label: string
|
|
16
|
+
entries: UndoEntry[]
|
|
17
|
+
coalesceKey?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const DEFAULT_HISTORY_LIMIT = 200
|
|
21
|
+
|
|
22
|
+
export class UndoManager {
|
|
23
|
+
private undoStack: UndoEntry[] = []
|
|
24
|
+
private redoStack: UndoEntry[] = []
|
|
25
|
+
private batches: UndoBatch[] = []
|
|
26
|
+
private readonly limit: number
|
|
27
|
+
private readonly onChange: (() => void) | undefined
|
|
28
|
+
|
|
29
|
+
constructor(options: UndoManagerOptions = {}) {
|
|
30
|
+
this.limit = options.limit ?? DEFAULT_HISTORY_LIMIT
|
|
31
|
+
this.onChange = options.onChange
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
apply(entry: UndoEntry): void {
|
|
35
|
+
this.execute(entry)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
execute(entry: UndoEntry): void {
|
|
39
|
+
entry.forward()
|
|
40
|
+
this.record(entry)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
push(entry: UndoEntry): void {
|
|
44
|
+
this.record(entry)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
record(entry: UndoEntry): void {
|
|
48
|
+
const batch = this.currentBatch
|
|
49
|
+
if (batch) {
|
|
50
|
+
batch.entries.push(entry)
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
this.pushUndoEntry(entry)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
undo(): string | null {
|
|
57
|
+
const entry = this.undoStack.pop()
|
|
58
|
+
if (!entry) return null
|
|
59
|
+
entry.inverse()
|
|
60
|
+
this.redoStack.push(entry)
|
|
61
|
+
this.onChange?.()
|
|
62
|
+
return entry.label
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
redo(): string | null {
|
|
66
|
+
const entry = this.redoStack.pop()
|
|
67
|
+
if (!entry) return null
|
|
68
|
+
entry.forward()
|
|
69
|
+
this.undoStack.push(entry)
|
|
70
|
+
this.onChange?.()
|
|
71
|
+
return entry.label
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
beginBatch(label: string, coalesceKey?: string): void {
|
|
75
|
+
this.batches.push({ label, entries: [], coalesceKey })
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
commitBatch(): void {
|
|
79
|
+
const batch = this.batches.pop()
|
|
80
|
+
if (!batch || batch.entries.length === 0) return
|
|
81
|
+
|
|
82
|
+
const entry = this.createBatchEntry(batch)
|
|
83
|
+
const parentBatch = this.currentBatch
|
|
84
|
+
if (parentBatch) parentBatch.entries.push(entry)
|
|
85
|
+
else this.pushUndoEntry(entry)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
runBatch<T>(label: string, fn: () => T, coalesceKey?: string): T {
|
|
89
|
+
this.beginBatch(label, coalesceKey)
|
|
90
|
+
try {
|
|
91
|
+
const result = fn()
|
|
92
|
+
this.commitBatch()
|
|
93
|
+
return result
|
|
94
|
+
} catch (error) {
|
|
95
|
+
this.rollbackBatch()
|
|
96
|
+
throw error
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
rollbackBatch(): void {
|
|
101
|
+
const batch = this.batches.pop()
|
|
102
|
+
if (!batch) return
|
|
103
|
+
for (const entry of batch.entries.toReversed()) entry.inverse()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Abandon provisional history without replaying it or changing committed undo/redo entries. */
|
|
107
|
+
discardBatches(): void {
|
|
108
|
+
this.batches = []
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
clear(): void {
|
|
112
|
+
this.undoStack = []
|
|
113
|
+
this.redoStack = []
|
|
114
|
+
this.discardBatches()
|
|
115
|
+
this.onChange?.()
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get isBatching(): boolean {
|
|
119
|
+
return this.batches.length > 0
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get canUndo(): boolean {
|
|
123
|
+
return this.undoStack.length > 0
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get canRedo(): boolean {
|
|
127
|
+
return this.redoStack.length > 0
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get undoLabel(): string | null {
|
|
131
|
+
return this.undoStack.at(-1)?.label ?? null
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
get redoLabel(): string | null {
|
|
135
|
+
return this.redoStack.at(-1)?.label ?? null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private get currentBatch(): UndoBatch | null {
|
|
139
|
+
return this.batches.at(-1) ?? null
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private createBatchEntry(batch: UndoBatch): UndoEntry {
|
|
143
|
+
return {
|
|
144
|
+
label: batch.label,
|
|
145
|
+
forward: () => batch.entries.forEach((entry) => entry.forward()),
|
|
146
|
+
inverse: () => batch.entries.toReversed().forEach((entry) => entry.inverse()),
|
|
147
|
+
coalesceKey: batch.coalesceKey
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private pushUndoEntry(entry: UndoEntry): void {
|
|
152
|
+
const previous = this.undoStack.at(-1)
|
|
153
|
+
if (entry.coalesceKey && previous?.coalesceKey === entry.coalesceKey) {
|
|
154
|
+
this.undoStack[this.undoStack.length - 1] = {
|
|
155
|
+
...entry,
|
|
156
|
+
inverse: previous.inverse
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
this.undoStack.push(entry)
|
|
160
|
+
}
|
|
161
|
+
this.redoStack = []
|
|
162
|
+
this.trimUndoStack()
|
|
163
|
+
this.onChange?.()
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private trimUndoStack(): void {
|
|
167
|
+
if (!Number.isFinite(this.limit) || this.limit <= 0) return
|
|
168
|
+
const overflow = this.undoStack.length - this.limit
|
|
169
|
+
if (overflow > 0) this.undoStack.splice(0, overflow)
|
|
170
|
+
}
|
|
171
|
+
}
|
package/src/variables.ts
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { omit, omitBy } from 'es-toolkit/object'
|
|
2
|
+
|
|
3
|
+
import { BLACK } from './constants'
|
|
4
|
+
import type { SceneGraph } from './index'
|
|
5
|
+
import { setInstanceOverride } from './instance-overrides'
|
|
6
|
+
import type { Color } from './primitives'
|
|
7
|
+
import type { Variable, VariableCollection, VariableType, VariableValue } from './types'
|
|
8
|
+
|
|
9
|
+
export function addVariable(graph: SceneGraph, variable: Variable): void {
|
|
10
|
+
graph.variables.set(variable.id, variable)
|
|
11
|
+
const collection = graph.variableCollections.get(variable.collectionId)
|
|
12
|
+
if (collection && !collection.variableIds.includes(variable.id)) {
|
|
13
|
+
collection.variableIds.push(variable.id)
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function removeVariable(graph: SceneGraph, id: string): void {
|
|
18
|
+
const variable = graph.variables.get(id)
|
|
19
|
+
if (!variable) return
|
|
20
|
+
graph.variables.delete(id)
|
|
21
|
+
const collection = graph.variableCollections.get(variable.collectionId)
|
|
22
|
+
if (collection) {
|
|
23
|
+
collection.variableIds = collection.variableIds.filter((vid) => vid !== id)
|
|
24
|
+
}
|
|
25
|
+
for (const node of graph.nodes.values()) {
|
|
26
|
+
const hadBinding = Object.values(node.boundVariables).includes(id)
|
|
27
|
+
if (!hadBinding) continue
|
|
28
|
+
node.boundVariables = omitBy(node.boundVariables, (varId) => varId === id) as Record<
|
|
29
|
+
string,
|
|
30
|
+
string
|
|
31
|
+
>
|
|
32
|
+
graph.emitter.emit('node:updated', node.id, { boundVariables: { ...node.boundVariables } })
|
|
33
|
+
markBoundVariablesOverrideOnInstance(graph, node.id)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function addCollection(graph: SceneGraph, collection: VariableCollection): void {
|
|
38
|
+
graph.variableCollections.set(collection.id, collection)
|
|
39
|
+
if (!graph.activeMode.has(collection.id)) {
|
|
40
|
+
graph.activeMode.set(collection.id, collection.defaultModeId)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function defaultVariableValue(type: VariableType, value?: VariableValue): VariableValue {
|
|
45
|
+
if (value !== undefined) return value
|
|
46
|
+
if (type === 'COLOR') return { ...BLACK }
|
|
47
|
+
if (type === 'FLOAT') return 0
|
|
48
|
+
if (type === 'BOOLEAN') return false
|
|
49
|
+
return ''
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createVariable(
|
|
53
|
+
graph: SceneGraph,
|
|
54
|
+
generateId: () => string,
|
|
55
|
+
name: string,
|
|
56
|
+
type: VariableType,
|
|
57
|
+
collectionId: string,
|
|
58
|
+
value?: VariableValue
|
|
59
|
+
): Variable {
|
|
60
|
+
const collection = graph.variableCollections.get(collectionId)
|
|
61
|
+
if (!collection) throw new Error(`Collection "${collectionId}" not found`)
|
|
62
|
+
const id = generateId()
|
|
63
|
+
const defaultValue = defaultVariableValue(type, value)
|
|
64
|
+
const valuesByMode: Record<string, VariableValue> = {}
|
|
65
|
+
for (const mode of collection.modes) {
|
|
66
|
+
valuesByMode[mode.modeId] = structuredClone(defaultValue)
|
|
67
|
+
}
|
|
68
|
+
const variable: Variable = {
|
|
69
|
+
id,
|
|
70
|
+
name,
|
|
71
|
+
type,
|
|
72
|
+
collectionId,
|
|
73
|
+
valuesByMode,
|
|
74
|
+
description: '',
|
|
75
|
+
hiddenFromPublishing: false
|
|
76
|
+
}
|
|
77
|
+
addVariable(graph, variable)
|
|
78
|
+
return variable
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function createCollection(
|
|
82
|
+
graph: SceneGraph,
|
|
83
|
+
generateId: () => string,
|
|
84
|
+
name: string
|
|
85
|
+
): VariableCollection {
|
|
86
|
+
const id = generateId()
|
|
87
|
+
const modeId = generateId()
|
|
88
|
+
const collection: VariableCollection = {
|
|
89
|
+
id,
|
|
90
|
+
name,
|
|
91
|
+
modes: [{ modeId, name: 'Mode 1' }],
|
|
92
|
+
defaultModeId: modeId,
|
|
93
|
+
variableIds: []
|
|
94
|
+
}
|
|
95
|
+
addCollection(graph, collection)
|
|
96
|
+
return collection
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function removeCollection(graph: SceneGraph, id: string): void {
|
|
100
|
+
const collection = graph.variableCollections.get(id)
|
|
101
|
+
if (collection) {
|
|
102
|
+
for (const varId of Array.from(collection.variableIds)) {
|
|
103
|
+
removeVariable(graph, varId)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
graph.variableCollections.delete(id)
|
|
107
|
+
graph.activeMode.delete(id)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function getActiveModeId(graph: SceneGraph, collectionId: string): string {
|
|
111
|
+
const mode = graph.activeMode.get(collectionId)
|
|
112
|
+
if (mode) return mode
|
|
113
|
+
const collection = graph.variableCollections.get(collectionId)
|
|
114
|
+
return collection?.defaultModeId ?? ''
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function getNodeVariableModeId(
|
|
118
|
+
graph: SceneGraph,
|
|
119
|
+
nodeId: string,
|
|
120
|
+
collectionId: string
|
|
121
|
+
): string {
|
|
122
|
+
let node = graph.nodes.get(nodeId)
|
|
123
|
+
while (node) {
|
|
124
|
+
const modeId = node.variableModes[collectionId]
|
|
125
|
+
if (modeId) return modeId
|
|
126
|
+
node = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
127
|
+
}
|
|
128
|
+
return getActiveModeId(graph, collectionId)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function setActiveMode(graph: SceneGraph, collectionId: string, modeId: string): void {
|
|
132
|
+
graph.activeMode.set(collectionId, modeId)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function addMode(
|
|
136
|
+
graph: SceneGraph,
|
|
137
|
+
collectionId: string,
|
|
138
|
+
modeId: string,
|
|
139
|
+
name: string,
|
|
140
|
+
sourceMode?: string
|
|
141
|
+
): void {
|
|
142
|
+
const collection = graph.variableCollections.get(collectionId)
|
|
143
|
+
if (!collection) return
|
|
144
|
+
collection.modes.push({ modeId, name })
|
|
145
|
+
const sourceModeId = sourceMode ?? collection.defaultModeId
|
|
146
|
+
for (const varId of collection.variableIds) {
|
|
147
|
+
const variable = graph.variables.get(varId)
|
|
148
|
+
if (!variable) continue
|
|
149
|
+
variable.valuesByMode[modeId] = structuredClone(
|
|
150
|
+
variable.valuesByMode[sourceModeId] ?? Object.values(variable.valuesByMode)[0]
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function removeMode(graph: SceneGraph, collectionId: string, modeId: string): void {
|
|
156
|
+
const collection = graph.variableCollections.get(collectionId)
|
|
157
|
+
if (!collection || collection.modes.length <= 1) return
|
|
158
|
+
collection.modes = collection.modes.filter((m) => m.modeId !== modeId)
|
|
159
|
+
if (collection.defaultModeId === modeId) {
|
|
160
|
+
collection.defaultModeId = collection.modes[0].modeId
|
|
161
|
+
}
|
|
162
|
+
for (const varId of collection.variableIds) {
|
|
163
|
+
const variable = graph.variables.get(varId)
|
|
164
|
+
if (variable) variable.valuesByMode = omit(variable.valuesByMode, [modeId])
|
|
165
|
+
}
|
|
166
|
+
if (graph.activeMode.get(collectionId) === modeId) {
|
|
167
|
+
graph.activeMode.set(collectionId, collection.defaultModeId)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function renameMode(
|
|
172
|
+
graph: SceneGraph,
|
|
173
|
+
collectionId: string,
|
|
174
|
+
modeId: string,
|
|
175
|
+
name: string
|
|
176
|
+
): void {
|
|
177
|
+
const collection = graph.variableCollections.get(collectionId)
|
|
178
|
+
if (!collection) return
|
|
179
|
+
const mode = collection.modes.find((m) => m.modeId === modeId)
|
|
180
|
+
if (mode) mode.name = name
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function setDefaultMode(graph: SceneGraph, collectionId: string, modeId: string): void {
|
|
184
|
+
const collection = graph.variableCollections.get(collectionId)
|
|
185
|
+
if (!collection) return
|
|
186
|
+
if (!collection.modes.some((m) => m.modeId === modeId)) return
|
|
187
|
+
collection.defaultModeId = modeId
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function resolveVariable(
|
|
191
|
+
graph: SceneGraph,
|
|
192
|
+
variableId: string,
|
|
193
|
+
modeId?: string,
|
|
194
|
+
visited?: Set<string>
|
|
195
|
+
): VariableValue | undefined {
|
|
196
|
+
if (visited?.has(variableId)) return undefined
|
|
197
|
+
const variable = graph.variables.get(variableId)
|
|
198
|
+
if (!variable) return undefined
|
|
199
|
+
const collection = graph.variableCollections.get(variable.collectionId)
|
|
200
|
+
const preferredModeId = modeId ?? getActiveModeId(graph, variable.collectionId)
|
|
201
|
+
const fallbackModeId = collection?.defaultModeId
|
|
202
|
+
let value = Object.hasOwn(variable.valuesByMode, preferredModeId)
|
|
203
|
+
? variable.valuesByMode[preferredModeId]
|
|
204
|
+
: undefined
|
|
205
|
+
if (
|
|
206
|
+
value === undefined &&
|
|
207
|
+
fallbackModeId &&
|
|
208
|
+
Object.hasOwn(variable.valuesByMode, fallbackModeId)
|
|
209
|
+
) {
|
|
210
|
+
value = variable.valuesByMode[fallbackModeId]
|
|
211
|
+
}
|
|
212
|
+
value ??= Object.values(variable.valuesByMode)[0]
|
|
213
|
+
if (value && typeof value === 'object' && 'aliasId' in value) {
|
|
214
|
+
const seen = visited ?? new Set<string>()
|
|
215
|
+
seen.add(variableId)
|
|
216
|
+
return resolveVariable(graph, value.aliasId, preferredModeId, seen)
|
|
217
|
+
}
|
|
218
|
+
return value
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function resolveColorVariable(graph: SceneGraph, variableId: string): Color | undefined {
|
|
222
|
+
const value = resolveVariable(graph, variableId)
|
|
223
|
+
if (value && typeof value === 'object' && 'r' in value) return value
|
|
224
|
+
return undefined
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function resolveNumberVariable(graph: SceneGraph, variableId: string): number | undefined {
|
|
228
|
+
const value = resolveVariable(graph, variableId)
|
|
229
|
+
return typeof value === 'number' ? value : undefined
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function resolveColorVariableForNode(
|
|
233
|
+
graph: SceneGraph,
|
|
234
|
+
nodeId: string,
|
|
235
|
+
variableId: string
|
|
236
|
+
): Color | undefined {
|
|
237
|
+
const variable = graph.variables.get(variableId)
|
|
238
|
+
if (!variable) return undefined
|
|
239
|
+
const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId)
|
|
240
|
+
const value = resolveVariable(graph, variableId, modeId)
|
|
241
|
+
if (value && typeof value === 'object' && 'r' in value) return value
|
|
242
|
+
return undefined
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function resolveNumberVariableForNode(
|
|
246
|
+
graph: SceneGraph,
|
|
247
|
+
nodeId: string,
|
|
248
|
+
variableId: string
|
|
249
|
+
): number | undefined {
|
|
250
|
+
const variable = graph.variables.get(variableId)
|
|
251
|
+
if (!variable) return undefined
|
|
252
|
+
const modeId = getNodeVariableModeId(graph, nodeId, variable.collectionId)
|
|
253
|
+
const value = resolveVariable(graph, variableId, modeId)
|
|
254
|
+
return typeof value === 'number' ? value : undefined
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function getVariablesForCollection(graph: SceneGraph, collectionId: string): Variable[] {
|
|
258
|
+
const collection = graph.variableCollections.get(collectionId)
|
|
259
|
+
if (!collection) return []
|
|
260
|
+
return collection.variableIds
|
|
261
|
+
.map((id) => graph.variables.get(id))
|
|
262
|
+
.filter((v): v is Variable => v !== undefined)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function getVariablesByType(graph: SceneGraph, type: VariableType): Variable[] {
|
|
266
|
+
return [...graph.variables.values()].filter((v) => v.type === type)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const SCALAR_BINDING_FIELDS: ReadonlySet<string> = new Set([
|
|
270
|
+
'opacity',
|
|
271
|
+
'width',
|
|
272
|
+
'height',
|
|
273
|
+
'cornerRadius',
|
|
274
|
+
'fontSize',
|
|
275
|
+
'letterSpacing',
|
|
276
|
+
'lineHeight',
|
|
277
|
+
'itemSpacing',
|
|
278
|
+
'strokeWeight',
|
|
279
|
+
'paddingLeft',
|
|
280
|
+
'paddingRight',
|
|
281
|
+
'paddingTop',
|
|
282
|
+
'paddingBottom',
|
|
283
|
+
'counterAxisSpacing',
|
|
284
|
+
'topLeftRadius',
|
|
285
|
+
'topRightRadius',
|
|
286
|
+
'bottomLeftRadius',
|
|
287
|
+
'bottomRightRadius',
|
|
288
|
+
'rotation',
|
|
289
|
+
'x',
|
|
290
|
+
'y',
|
|
291
|
+
'minWidth',
|
|
292
|
+
'maxWidth',
|
|
293
|
+
'minHeight',
|
|
294
|
+
'maxHeight',
|
|
295
|
+
'borderTopWeight',
|
|
296
|
+
'borderBottomWeight',
|
|
297
|
+
'borderLeftWeight',
|
|
298
|
+
'borderRightWeight',
|
|
299
|
+
'gridRowGap',
|
|
300
|
+
'gridColumnGap'
|
|
301
|
+
])
|
|
302
|
+
|
|
303
|
+
const STRING_BINDING_FIELDS: ReadonlySet<string> = new Set(['fontFamily'])
|
|
304
|
+
|
|
305
|
+
const BOOLEAN_BINDING_FIELDS: ReadonlySet<string> = new Set(['visible'])
|
|
306
|
+
|
|
307
|
+
export function bindVariable(
|
|
308
|
+
graph: SceneGraph,
|
|
309
|
+
nodeId: string,
|
|
310
|
+
field: string,
|
|
311
|
+
variableId: string
|
|
312
|
+
): void {
|
|
313
|
+
const node = graph.nodes.get(nodeId)
|
|
314
|
+
if (!node) return
|
|
315
|
+
|
|
316
|
+
// Validate variable exists
|
|
317
|
+
const variable = graph.variables.get(variableId)
|
|
318
|
+
if (!variable) {
|
|
319
|
+
throw new Error(`Variable "${variableId}" not found`)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Color fields require COLOR variable type
|
|
323
|
+
const colorFieldMatch = field.match(/^(fills|strokes)\/(\d+)\/color$/)
|
|
324
|
+
if (colorFieldMatch) {
|
|
325
|
+
if (variable.type !== 'COLOR') {
|
|
326
|
+
throw new Error(`Cannot bind ${variable.type} variable to color field "${field}"`)
|
|
327
|
+
}
|
|
328
|
+
// Validate index is within current array bounds
|
|
329
|
+
const arrayKey = colorFieldMatch[1] as 'fills' | 'strokes'
|
|
330
|
+
const index = Number.parseInt(colorFieldMatch[2], 10)
|
|
331
|
+
const currentLength = (node[arrayKey] as unknown[] | undefined)?.length ?? 0
|
|
332
|
+
if (index >= currentLength) {
|
|
333
|
+
throw new Error(`Index ${index} out of range for ${arrayKey} (length ${currentLength})`)
|
|
334
|
+
}
|
|
335
|
+
// Auto-remove top-level dead binding (e.g. 'fills') when setting indexed binding
|
|
336
|
+
const topLevelKey = colorFieldMatch[1]
|
|
337
|
+
if (topLevelKey in node.boundVariables) {
|
|
338
|
+
node.boundVariables = omit(node.boundVariables, [topLevelKey])
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (SCALAR_BINDING_FIELDS.has(field) && variable.type !== 'FLOAT') {
|
|
343
|
+
throw new Error(`Cannot bind ${variable.type} variable to scalar field "${field}"`)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (STRING_BINDING_FIELDS.has(field) && variable.type !== 'STRING') {
|
|
347
|
+
throw new Error(`Cannot bind ${variable.type} variable to string field "${field}"`)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (BOOLEAN_BINDING_FIELDS.has(field) && variable.type !== 'BOOLEAN') {
|
|
351
|
+
throw new Error(`Cannot bind ${variable.type} variable to boolean field "${field}"`)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const isKnownField =
|
|
355
|
+
SCALAR_BINDING_FIELDS.has(field) ||
|
|
356
|
+
STRING_BINDING_FIELDS.has(field) ||
|
|
357
|
+
BOOLEAN_BINDING_FIELDS.has(field) ||
|
|
358
|
+
colorFieldMatch
|
|
359
|
+
|
|
360
|
+
if (!isKnownField) {
|
|
361
|
+
throw new Error(`Unknown binding field "${field}"`)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
node.boundVariables = { ...node.boundVariables, [field]: variableId }
|
|
365
|
+
graph.emitter.emit('node:updated', nodeId, { boundVariables: { ...node.boundVariables } })
|
|
366
|
+
markBoundVariablesOverrideOnInstance(graph, nodeId)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export function unbindVariable(graph: SceneGraph, nodeId: string, field: string): void {
|
|
370
|
+
const node = graph.nodes.get(nodeId)
|
|
371
|
+
if (!node) return
|
|
372
|
+
if (!(field in node.boundVariables)) return
|
|
373
|
+
node.boundVariables = omit(node.boundVariables, [field])
|
|
374
|
+
graph.emitter.emit('node:updated', nodeId, { boundVariables: { ...node.boundVariables } })
|
|
375
|
+
markBoundVariablesOverrideOnInstance(graph, nodeId)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function markBoundVariablesOverrideOnInstance(graph: SceneGraph, nodeId: string): void {
|
|
379
|
+
const node = graph.nodes.get(nodeId)
|
|
380
|
+
if (!node) return
|
|
381
|
+
|
|
382
|
+
// Mark the field on the canonical structured state.
|
|
383
|
+
if (node.type === 'INSTANCE') {
|
|
384
|
+
setInstanceOverride(node.instanceOverrides, node.id, node.id, 'boundVariables')
|
|
385
|
+
return
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Walk up to the owning instance for descendant fields.
|
|
389
|
+
let current = node
|
|
390
|
+
while (current.parentId) {
|
|
391
|
+
const parent = graph.nodes.get(current.parentId)
|
|
392
|
+
if (!parent) break
|
|
393
|
+
if (parent.type === 'INSTANCE') {
|
|
394
|
+
setInstanceOverride(parent.instanceOverrides, parent.id, nodeId, 'boundVariables')
|
|
395
|
+
break
|
|
396
|
+
}
|
|
397
|
+
current = parent
|
|
398
|
+
}
|
|
399
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function parseVariantName(name: string): Record<string, string> {
|
|
2
|
+
const values: Record<string, string> = {}
|
|
3
|
+
for (const part of name.split(',').map((s) => s.trim())) {
|
|
4
|
+
const eqIdx = part.indexOf('=')
|
|
5
|
+
if (eqIdx === -1) continue
|
|
6
|
+
values[part.slice(0, eqIdx).trim()] = part.slice(eqIdx + 1).trim()
|
|
7
|
+
}
|
|
8
|
+
return values
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function buildVariantName(values: Record<string, string>): string {
|
|
12
|
+
return Object.entries(values)
|
|
13
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
14
|
+
.join(', ')
|
|
15
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { ComponentPropertyDefinition, SceneNode } from './types'
|
|
2
|
+
|
|
3
|
+
export interface DerivedVariantProperties {
|
|
4
|
+
definitions: ComponentPropertyDefinition[]
|
|
5
|
+
variants: Map<string, Pick<SceneNode, 'componentPropertyValues' | 'name'>>
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function deriveSlashVariantProperties(
|
|
9
|
+
components: ReadonlyArray<Pick<SceneNode, 'id' | 'name'>>,
|
|
10
|
+
createPropertyId: () => string
|
|
11
|
+
): DerivedVariantProperties | null {
|
|
12
|
+
const slashCounts = components.map((component) => (component.name.match(/\//g) ?? []).length)
|
|
13
|
+
const slashCount = slashCounts[0] ?? 0
|
|
14
|
+
if (slashCount === 0 || !slashCounts.every((count) => count === slashCount)) return null
|
|
15
|
+
|
|
16
|
+
const definitions: ComponentPropertyDefinition[] = Array.from(
|
|
17
|
+
{ length: slashCount },
|
|
18
|
+
(_, index) => ({
|
|
19
|
+
id: createPropertyId(),
|
|
20
|
+
name: index === 0 ? 'Variant' : `Property ${index + 1}`,
|
|
21
|
+
type: 'VARIANT',
|
|
22
|
+
defaultValue: ''
|
|
23
|
+
})
|
|
24
|
+
)
|
|
25
|
+
const options = new Map(definitions.map((definition) => [definition.name, new Set<string>()]))
|
|
26
|
+
const variants = new Map<string, Pick<SceneNode, 'componentPropertyValues' | 'name'>>()
|
|
27
|
+
|
|
28
|
+
for (const component of components) {
|
|
29
|
+
const parts = component.name.split('/').slice(1)
|
|
30
|
+
const componentPropertyValues: Record<string, string> = {}
|
|
31
|
+
for (const [index, definition] of definitions.entries()) {
|
|
32
|
+
const value = parts[index]?.trim() ?? ''
|
|
33
|
+
componentPropertyValues[definition.name] = value
|
|
34
|
+
options.get(definition.name)?.add(value)
|
|
35
|
+
}
|
|
36
|
+
variants.set(component.id, {
|
|
37
|
+
componentPropertyValues,
|
|
38
|
+
name: Object.values(componentPropertyValues).join(', ')
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const definition of definitions) {
|
|
43
|
+
definition.variantOptions = [...(options.get(definition.name) ?? [])]
|
|
44
|
+
definition.defaultValue = definition.variantOptions[0] ?? ''
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { definitions, variants }
|
|
48
|
+
}
|