@open-pencil/scene-graph 0.14.0 → 0.15.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/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/index.ts
ADDED
|
@@ -0,0 +1,686 @@
|
|
|
1
|
+
/* eslint-disable max-lines -- SceneGraph exposes a stable facade over domain modules */
|
|
2
|
+
export * from './mutation-impact'
|
|
3
|
+
export * from './instance-overrides'
|
|
4
|
+
export * from './images'
|
|
5
|
+
export * from './components/properties'
|
|
6
|
+
export * from './copy'
|
|
7
|
+
export {
|
|
8
|
+
copyInstanceComponentProps,
|
|
9
|
+
hasInstanceOverride,
|
|
10
|
+
INSTANCE_SYNC_FIELDS,
|
|
11
|
+
INSTANCE_SYNC_PROPS,
|
|
12
|
+
INSTANCE_SYNC_TEXT_PROPS,
|
|
13
|
+
recordInstanceOverride
|
|
14
|
+
} from './instances'
|
|
15
|
+
export {
|
|
16
|
+
clearInstanceOverrides,
|
|
17
|
+
cloneInstanceOverrideState,
|
|
18
|
+
forEachInstanceOverride,
|
|
19
|
+
getInstanceOverride,
|
|
20
|
+
setInstanceOverride,
|
|
21
|
+
type InstanceOverrideState
|
|
22
|
+
} from './instance-overrides'
|
|
23
|
+
export * from './snap'
|
|
24
|
+
export * from './export-scale'
|
|
25
|
+
export * from './coordinate'
|
|
26
|
+
export * from './constants'
|
|
27
|
+
export * from './geometry'
|
|
28
|
+
export * from './guides'
|
|
29
|
+
export * from './layout-guides'
|
|
30
|
+
export * from './font-style'
|
|
31
|
+
export * from './shared-styles'
|
|
32
|
+
export { default as TransformMatrix } from './matrix'
|
|
33
|
+
export type { Mat3 } from './matrix'
|
|
34
|
+
export { UndoManager, type UndoEntry, type UndoManagerOptions } from './undo'
|
|
35
|
+
|
|
36
|
+
import { createNanoEvents } from 'nanoevents'
|
|
37
|
+
|
|
38
|
+
import { removeStaleBindings } from './bindings'
|
|
39
|
+
import { cloneNodeProps } from './copy'
|
|
40
|
+
import { bindNodeEvents } from './events'
|
|
41
|
+
import * as HitTest from './hit-test'
|
|
42
|
+
import * as Instances from './instances'
|
|
43
|
+
import { CONTAINER_TYPES, createDefaultNode } from './node-defaults'
|
|
44
|
+
import { updateNodePreview, type NodePreviewObserver } from './preview'
|
|
45
|
+
import { styleDetachmentChanges } from './shared-styles'
|
|
46
|
+
import { markSourceFieldsEdited } from './source-metadata'
|
|
47
|
+
import { GLYPH_AFFECTING_KEYS, invalidateTextCaches, TEXT_PICTURE_KEYS } from './text-picture'
|
|
48
|
+
import * as Variables from './variables'
|
|
49
|
+
import { normalizeVectorNetwork } from './vector-network'
|
|
50
|
+
|
|
51
|
+
export type { GUID, Color, Size, Vector } from './primitives'
|
|
52
|
+
export * from './types'
|
|
53
|
+
|
|
54
|
+
import type { Emitter } from 'nanoevents'
|
|
55
|
+
|
|
56
|
+
import { getAbsolutePosition } from './coordinate'
|
|
57
|
+
import type { Color, Rect, Vector } from './primitives'
|
|
58
|
+
import type {
|
|
59
|
+
DocumentColorSpace,
|
|
60
|
+
EnabledLibraryBinding,
|
|
61
|
+
NodeType,
|
|
62
|
+
SceneGraphEventHandlers,
|
|
63
|
+
SceneGraphEvents,
|
|
64
|
+
SceneNode,
|
|
65
|
+
SourceMetadata,
|
|
66
|
+
Variable,
|
|
67
|
+
VariableCollection,
|
|
68
|
+
VariableType,
|
|
69
|
+
VariableValue
|
|
70
|
+
} from './types'
|
|
71
|
+
|
|
72
|
+
export {
|
|
73
|
+
cloneVectorNetwork,
|
|
74
|
+
mergeVectorNetworks,
|
|
75
|
+
normalizeVectorNetwork,
|
|
76
|
+
transformVectorNetwork,
|
|
77
|
+
validateVectorNetwork,
|
|
78
|
+
vectorNetworksEqual
|
|
79
|
+
} from './vector-network'
|
|
80
|
+
|
|
81
|
+
let nextLocalID = 1
|
|
82
|
+
|
|
83
|
+
export function generateId(): string {
|
|
84
|
+
return `0:${nextLocalID++}`
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function stripUndefinedProps<T extends object>(obj: T): T {
|
|
88
|
+
const result = {} as T
|
|
89
|
+
for (const key of Object.keys(obj) as (keyof T)[]) {
|
|
90
|
+
const val = obj[key]
|
|
91
|
+
if (val !== undefined) {
|
|
92
|
+
result[key] = val
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return result
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export { captureGraphCheckpoint } from './checkpoint'
|
|
99
|
+
|
|
100
|
+
export class SceneGraph {
|
|
101
|
+
nodes = new Map<string, SceneNode>()
|
|
102
|
+
images = new Map<string, Uint8Array>()
|
|
103
|
+
variables = new Map<string, Variable>()
|
|
104
|
+
variableCollections = new Map<string, VariableCollection>()
|
|
105
|
+
activeMode = new Map<string, string>()
|
|
106
|
+
rootId: string
|
|
107
|
+
figKiwiVersion: number | null = null
|
|
108
|
+
/** Deflated kiwi schema bytes from the original .fig file, preserved for roundtrip fidelity. */
|
|
109
|
+
figSchemaDeflated: Uint8Array | null = null
|
|
110
|
+
documentColorSpace: DocumentColorSpace = 'srgb'
|
|
111
|
+
enabledLibraries = new Map<string, EnabledLibraryBinding>()
|
|
112
|
+
readonly emitter: Emitter<SceneGraphEvents> = createNanoEvents()
|
|
113
|
+
private absPosCache = new Map<string, Vector>()
|
|
114
|
+
private previewMutationDepth = 0
|
|
115
|
+
private previewObservers: NodePreviewObserver[] = []
|
|
116
|
+
private sourceMetadataPreservationDepth = 0
|
|
117
|
+
private layoutMutationDepth = 0
|
|
118
|
+
positionPreviewVersion = 0
|
|
119
|
+
instanceIndex = new Map<string, Set<string>>()
|
|
120
|
+
|
|
121
|
+
constructor() {
|
|
122
|
+
const root = createDefaultNode(generateId, 'FRAME', {
|
|
123
|
+
name: 'Document',
|
|
124
|
+
width: 0,
|
|
125
|
+
height: 0
|
|
126
|
+
})
|
|
127
|
+
this.rootId = root.id
|
|
128
|
+
this.nodes.set(root.id, root)
|
|
129
|
+
|
|
130
|
+
this.addPage('Page 1')
|
|
131
|
+
}
|
|
132
|
+
addPage(name: string): SceneNode {
|
|
133
|
+
return this.createNode('CANVAS', this.rootId, { name, width: 0, height: 0 })
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
getPages(includeInternal = false): SceneNode[] {
|
|
137
|
+
return this.getChildren(this.rootId).filter(
|
|
138
|
+
(n) => n.type === 'CANVAS' && (includeInternal || !n.internalOnly)
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
getAllNodes(): Iterable<SceneNode> {
|
|
142
|
+
return this.nodes.values()
|
|
143
|
+
}
|
|
144
|
+
getNode(id: string): SceneNode | undefined {
|
|
145
|
+
return this.nodes.get(id)
|
|
146
|
+
}
|
|
147
|
+
onNodeEvents(handlers: SceneGraphEventHandlers): () => void {
|
|
148
|
+
return bindNodeEvents(this.emitter, handlers)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
countDescendants(nodeId: string): number {
|
|
152
|
+
const node = this.nodes.get(nodeId)
|
|
153
|
+
if (!node) return 0
|
|
154
|
+
let count = 0
|
|
155
|
+
const stack = [...node.childIds]
|
|
156
|
+
while (stack.length > 0) {
|
|
157
|
+
const id = stack.pop()
|
|
158
|
+
if (id === undefined) break
|
|
159
|
+
count++
|
|
160
|
+
const child = this.nodes.get(id)
|
|
161
|
+
if (child) {
|
|
162
|
+
for (const childId of child.childIds) {
|
|
163
|
+
stack.push(childId)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return count
|
|
168
|
+
}
|
|
169
|
+
addVariable(variable: Variable): void {
|
|
170
|
+
Variables.addVariable(this, variable)
|
|
171
|
+
}
|
|
172
|
+
removeVariable(id: string): void {
|
|
173
|
+
Variables.removeVariable(this, id)
|
|
174
|
+
}
|
|
175
|
+
addCollection(collection: VariableCollection): void {
|
|
176
|
+
Variables.addCollection(this, collection)
|
|
177
|
+
}
|
|
178
|
+
createVariable(
|
|
179
|
+
name: string,
|
|
180
|
+
type: VariableType,
|
|
181
|
+
collectionId: string,
|
|
182
|
+
value?: VariableValue
|
|
183
|
+
): Variable {
|
|
184
|
+
return Variables.createVariable(this, generateId, name, type, collectionId, value)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
createCollection(name: string): VariableCollection {
|
|
188
|
+
return Variables.createCollection(this, generateId, name)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
removeCollection(id: string): void {
|
|
192
|
+
Variables.removeCollection(this, id)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
getActiveModeId(collectionId: string): string {
|
|
196
|
+
return Variables.getActiveModeId(this, collectionId)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
getNodeVariableModeId(nodeId: string, collectionId: string): string {
|
|
200
|
+
return Variables.getNodeVariableModeId(this, nodeId, collectionId)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
setActiveMode(collectionId: string, modeId: string): void {
|
|
204
|
+
Variables.setActiveMode(this, collectionId, modeId)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
addMode(collectionId: string, modeId: string, name: string, sourceMode?: string): void {
|
|
208
|
+
Variables.addMode(this, collectionId, modeId, name, sourceMode)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
removeMode(collectionId: string, modeId: string): void {
|
|
212
|
+
Variables.removeMode(this, collectionId, modeId)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
renameMode(collectionId: string, modeId: string, name: string): void {
|
|
216
|
+
Variables.renameMode(this, collectionId, modeId, name)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
setDefaultMode(collectionId: string, modeId: string): void {
|
|
220
|
+
Variables.setDefaultMode(this, collectionId, modeId)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
resolveVariable(
|
|
224
|
+
variableId: string,
|
|
225
|
+
modeId?: string,
|
|
226
|
+
visited?: Set<string>
|
|
227
|
+
): VariableValue | undefined {
|
|
228
|
+
return Variables.resolveVariable(this, variableId, modeId, visited)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
resolveColorVariable(variableId: string): Color | undefined {
|
|
232
|
+
return Variables.resolveColorVariable(this, variableId)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
resolveNumberVariable(variableId: string): number | undefined {
|
|
236
|
+
return Variables.resolveNumberVariable(this, variableId)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
resolveColorVariableForNode(nodeId: string, variableId: string): Color | undefined {
|
|
240
|
+
return Variables.resolveColorVariableForNode(this, nodeId, variableId)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
resolveNumberVariableForNode(nodeId: string, variableId: string): number | undefined {
|
|
244
|
+
return Variables.resolveNumberVariableForNode(this, nodeId, variableId)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
getVariablesForCollection(collectionId: string): Variable[] {
|
|
248
|
+
return Variables.getVariablesForCollection(this, collectionId)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
getVariablesByType(type: VariableType): Variable[] {
|
|
252
|
+
return Variables.getVariablesByType(this, type)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
bindVariable(nodeId: string, field: string, variableId: string): void {
|
|
256
|
+
Variables.bindVariable(this, nodeId, field, variableId)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
unbindVariable(nodeId: string, field: string): void {
|
|
260
|
+
Variables.unbindVariable(this, nodeId, field)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
getChildren(id: string): SceneNode[] {
|
|
264
|
+
const node = this.nodes.get(id)
|
|
265
|
+
if (!node) return []
|
|
266
|
+
return node.childIds
|
|
267
|
+
.map((cid) => this.nodes.get(cid))
|
|
268
|
+
.filter((n): n is SceneNode => n !== undefined)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
isContainer(id: string): boolean {
|
|
272
|
+
const node = this.nodes.get(id)
|
|
273
|
+
return node ? CONTAINER_TYPES.has(node.type) : false
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
isDescendant(childId: string, ancestorId: string): boolean {
|
|
277
|
+
let current = this.nodes.get(childId)
|
|
278
|
+
while (current) {
|
|
279
|
+
if (current.id === ancestorId) return true
|
|
280
|
+
current = current.parentId ? this.nodes.get(current.parentId) : undefined
|
|
281
|
+
}
|
|
282
|
+
return false
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
clearAbsPosCache(): void {
|
|
286
|
+
this.absPosCache.clear()
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
getAbsolutePosition(id: string): Vector {
|
|
290
|
+
const cached = this.absPosCache.get(id)
|
|
291
|
+
if (cached) return cached
|
|
292
|
+
|
|
293
|
+
const node = this.getNode(id)
|
|
294
|
+
if (!node) return { x: 0, y: 0 }
|
|
295
|
+
|
|
296
|
+
const result = getAbsolutePosition(node, this)
|
|
297
|
+
this.absPosCache.set(id, result)
|
|
298
|
+
return result
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
getAbsoluteBounds(id: string): Rect {
|
|
302
|
+
const pos = this.getAbsolutePosition(id)
|
|
303
|
+
const node = this.nodes.get(id)
|
|
304
|
+
return {
|
|
305
|
+
x: pos.x,
|
|
306
|
+
y: pos.y,
|
|
307
|
+
width: node?.width ?? 0,
|
|
308
|
+
height: node?.height ?? 0
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
private generateNodeId(): string {
|
|
312
|
+
let id = generateId()
|
|
313
|
+
while (this.nodes.has(id)) id = generateId()
|
|
314
|
+
return id
|
|
315
|
+
}
|
|
316
|
+
private registerNode(node: SceneNode, parentId: string | null): SceneNode {
|
|
317
|
+
node.parentId = parentId
|
|
318
|
+
this.nodes.set(node.id, node)
|
|
319
|
+
if (node.type === 'INSTANCE' && node.componentId) {
|
|
320
|
+
let set = this.instanceIndex.get(node.componentId)
|
|
321
|
+
if (!set) {
|
|
322
|
+
set = new Set()
|
|
323
|
+
this.instanceIndex.set(node.componentId, set)
|
|
324
|
+
}
|
|
325
|
+
set.add(node.id)
|
|
326
|
+
}
|
|
327
|
+
this.emitter.emit('node:created', node)
|
|
328
|
+
return node
|
|
329
|
+
}
|
|
330
|
+
createNode(type: NodeType, parentId: string, overrides: Partial<SceneNode> = {}): SceneNode {
|
|
331
|
+
const node = createDefaultNode(() => this.generateNodeId(), type, overrides)
|
|
332
|
+
this.nodes.get(parentId)?.childIds.push(node.id)
|
|
333
|
+
return this.registerNode(node, parentId)
|
|
334
|
+
}
|
|
335
|
+
createNodeWithId(
|
|
336
|
+
id: string,
|
|
337
|
+
type: NodeType,
|
|
338
|
+
parentId: string | null,
|
|
339
|
+
overrides: Partial<SceneNode> = {}
|
|
340
|
+
): SceneNode {
|
|
341
|
+
const node = createDefaultNode(() => id, type, overrides)
|
|
342
|
+
node.id = id
|
|
343
|
+
const parent = parentId ? this.nodes.get(parentId) : undefined
|
|
344
|
+
if (parent && !parent.childIds.includes(id)) parent.childIds.push(id)
|
|
345
|
+
return this.registerNode(node, parentId)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
static TEXT_PICTURE_KEYS: ReadonlySet<string> = TEXT_PICTURE_KEYS
|
|
349
|
+
static GLYPH_AFFECTING_KEYS: ReadonlySet<string> = GLYPH_AFFECTING_KEYS
|
|
350
|
+
|
|
351
|
+
static LAYOUT_AFFECTING_KEYS: ReadonlySet<string> = new Set([
|
|
352
|
+
'x',
|
|
353
|
+
'y',
|
|
354
|
+
'width',
|
|
355
|
+
'height',
|
|
356
|
+
'rotation',
|
|
357
|
+
'flipX',
|
|
358
|
+
'flipY',
|
|
359
|
+
'layoutMode',
|
|
360
|
+
'layoutDirection',
|
|
361
|
+
'itemSpacing',
|
|
362
|
+
'counterAxisSpacing',
|
|
363
|
+
'paddingLeft',
|
|
364
|
+
'paddingRight',
|
|
365
|
+
'paddingTop',
|
|
366
|
+
'paddingBottom',
|
|
367
|
+
'primaryAxisAlign',
|
|
368
|
+
'counterAxisAlign',
|
|
369
|
+
'counterAxisAlignContent',
|
|
370
|
+
'layoutWrap',
|
|
371
|
+
'primaryAxisSizing',
|
|
372
|
+
'counterAxisSizing',
|
|
373
|
+
'layoutPositioning',
|
|
374
|
+
'layoutGrow',
|
|
375
|
+
'layoutAlignSelf',
|
|
376
|
+
'strokesIncludedInLayout',
|
|
377
|
+
'horizontalConstraint',
|
|
378
|
+
'verticalConstraint',
|
|
379
|
+
'gridTemplateColumns',
|
|
380
|
+
'gridTemplateRows',
|
|
381
|
+
'gridColumnGap',
|
|
382
|
+
'gridRowGap',
|
|
383
|
+
'gridPosition',
|
|
384
|
+
'minWidth',
|
|
385
|
+
'maxWidth',
|
|
386
|
+
'minHeight',
|
|
387
|
+
'maxHeight'
|
|
388
|
+
])
|
|
389
|
+
|
|
390
|
+
runPreviewUpdates(fn: () => void, beforeUpdate?: NodePreviewObserver): void {
|
|
391
|
+
this.previewMutationDepth++
|
|
392
|
+
if (beforeUpdate) this.previewObservers.push(beforeUpdate)
|
|
393
|
+
try {
|
|
394
|
+
fn()
|
|
395
|
+
} finally {
|
|
396
|
+
if (beforeUpdate) this.previewObservers.pop()
|
|
397
|
+
this.previewMutationDepth--
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
preserveSourceMetadataDuring(fn: () => void): void {
|
|
401
|
+
this.sourceMetadataPreservationDepth++
|
|
402
|
+
try {
|
|
403
|
+
fn()
|
|
404
|
+
} finally {
|
|
405
|
+
this.sourceMetadataPreservationDepth--
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
withLayoutMutations(fn: () => void): void {
|
|
409
|
+
this.layoutMutationDepth++
|
|
410
|
+
try {
|
|
411
|
+
fn()
|
|
412
|
+
} finally {
|
|
413
|
+
this.layoutMutationDepth--
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
get isApplyingLayout(): boolean {
|
|
417
|
+
return this.layoutMutationDepth > 0
|
|
418
|
+
}
|
|
419
|
+
updateNodePositionPreview(id: string, x: number, y: number): void {
|
|
420
|
+
this.updateNodePreview(id, { x, y })
|
|
421
|
+
}
|
|
422
|
+
updateNodePreview(id: string, changes: Partial<SceneNode>): void {
|
|
423
|
+
const appliedChanges = updateNodePreview(this, id, changes, (node, applied) => {
|
|
424
|
+
for (const observe of this.previewObservers) observe(node, applied)
|
|
425
|
+
})
|
|
426
|
+
if (appliedChanges) this.emitter.emit('node:previewUpdated', id, appliedChanges)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
updateNode(id: string, changes: Partial<SceneNode>): void {
|
|
430
|
+
if (this.previewMutationDepth > 0) {
|
|
431
|
+
this.updateNodePreview(id, changes)
|
|
432
|
+
return
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const node = this.nodes.get(id)
|
|
436
|
+
if (!node) return
|
|
437
|
+
changes = stripUndefinedProps(styleDetachmentChanges(node, stripUndefinedProps(changes)))
|
|
438
|
+
this.applyNodeChanges(node, changes)
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** Replay captured properties without dropping explicit undefined values or absent keys. */
|
|
442
|
+
restoreNodeProperties(
|
|
443
|
+
id: string,
|
|
444
|
+
changes: Partial<SceneNode>,
|
|
445
|
+
absent: readonly (keyof SceneNode)[]
|
|
446
|
+
): void {
|
|
447
|
+
const node = this.nodes.get(id)
|
|
448
|
+
if (node) this.applyNodeChanges(node, changes, absent)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
private applyNodeChanges(
|
|
452
|
+
node: SceneNode,
|
|
453
|
+
changes: Partial<SceneNode>,
|
|
454
|
+
absent: readonly (keyof SceneNode)[] = []
|
|
455
|
+
): void {
|
|
456
|
+
const { id } = node
|
|
457
|
+
// Include removed keys in cache invalidation and update notifications.
|
|
458
|
+
if (absent.length) {
|
|
459
|
+
changes = { ...changes }
|
|
460
|
+
for (const key of absent) Reflect.set(changes, key, undefined)
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Only clear absPosCache when layout-affecting properties change.
|
|
464
|
+
// Fills, strokes, effects, plugin data changes do NOT affect absolute position.
|
|
465
|
+
const affectsLayout = Object.keys(changes).some((k) => SceneGraph.LAYOUT_AFFECTING_KEYS.has(k))
|
|
466
|
+
if (affectsLayout) this.absPosCache.clear()
|
|
467
|
+
if (
|
|
468
|
+
node.type === 'INSTANCE' &&
|
|
469
|
+
'componentId' in changes &&
|
|
470
|
+
changes.componentId !== node.componentId
|
|
471
|
+
) {
|
|
472
|
+
if (node.componentId) this.instanceIndex.get(node.componentId)?.delete(id)
|
|
473
|
+
if (changes.componentId) {
|
|
474
|
+
let set = this.instanceIndex.get(changes.componentId)
|
|
475
|
+
if (!set) {
|
|
476
|
+
set = new Set()
|
|
477
|
+
this.instanceIndex.set(changes.componentId, set)
|
|
478
|
+
}
|
|
479
|
+
set.add(id)
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (node.type === 'TEXT') invalidateTextCaches(node, changes)
|
|
483
|
+
if (this.sourceMetadataPreservationDepth === 0) {
|
|
484
|
+
markSourceFieldsEdited(node, Object.keys(changes))
|
|
485
|
+
}
|
|
486
|
+
if (changes.vectorNetwork) {
|
|
487
|
+
changes = { ...changes, vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork) }
|
|
488
|
+
}
|
|
489
|
+
Object.assign(node, changes)
|
|
490
|
+
if (changes.fills) removeStaleBindings(node, 'fills', changes)
|
|
491
|
+
if (changes.strokes) removeStaleBindings(node, 'strokes', changes)
|
|
492
|
+
for (const key of absent) Reflect.deleteProperty(node, key)
|
|
493
|
+
this.emitter.emit('node:updated', id, changes)
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
reparentNode(nodeId: string, newParentId: string): void {
|
|
497
|
+
const node = this.nodes.get(nodeId)
|
|
498
|
+
if (!node || nodeId === this.rootId) return
|
|
499
|
+
if (this.isDescendant(newParentId, nodeId)) return
|
|
500
|
+
|
|
501
|
+
const oldParent = node.parentId ? this.nodes.get(node.parentId) : undefined
|
|
502
|
+
const newParent = this.nodes.get(newParentId)
|
|
503
|
+
if (!newParent) return
|
|
504
|
+
if (node.parentId === newParentId) return
|
|
505
|
+
|
|
506
|
+
const oldParentId = node.parentId
|
|
507
|
+
this.absPosCache.clear()
|
|
508
|
+
|
|
509
|
+
const absPos = this.getAbsolutePosition(nodeId)
|
|
510
|
+
const newParentNode = this.nodes.get(newParentId)
|
|
511
|
+
const newParentAbs =
|
|
512
|
+
newParentId === this.rootId || newParentNode?.type === 'CANVAS'
|
|
513
|
+
? { x: 0, y: 0 }
|
|
514
|
+
: this.getAbsolutePosition(newParentId)
|
|
515
|
+
|
|
516
|
+
if (oldParent) {
|
|
517
|
+
oldParent.childIds = oldParent.childIds.filter((cid) => cid !== nodeId)
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
node.parentId = newParentId
|
|
521
|
+
newParent.childIds.push(nodeId)
|
|
522
|
+
|
|
523
|
+
node.x = absPos.x - newParentAbs.x
|
|
524
|
+
node.y = absPos.y - newParentAbs.y
|
|
525
|
+
|
|
526
|
+
this.emitter.emit('node:reparented', nodeId, oldParentId, newParentId)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
reorderChild(nodeId: string, parentId: string, insertIndex: number): void {
|
|
530
|
+
const node = this.nodes.get(nodeId)
|
|
531
|
+
if (!node) return
|
|
532
|
+
|
|
533
|
+
const previousParentId = node.parentId
|
|
534
|
+
const oldParent = previousParentId ? this.nodes.get(previousParentId) : undefined
|
|
535
|
+
const newParent = this.nodes.get(parentId)
|
|
536
|
+
if (!newParent || this.isDescendant(parentId, nodeId)) return
|
|
537
|
+
|
|
538
|
+
// Remove from old parent
|
|
539
|
+
if (oldParent) {
|
|
540
|
+
oldParent.childIds = oldParent.childIds.filter((cid) => cid !== nodeId)
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// If same parent, adjust index since we removed the item
|
|
544
|
+
let idx = insertIndex
|
|
545
|
+
if (
|
|
546
|
+
oldParent === newParent &&
|
|
547
|
+
idx > (!oldParent.childIds.includes(nodeId) ? idx : oldParent.childIds.length)
|
|
548
|
+
) {
|
|
549
|
+
// Already removed above, no adjustment needed
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
node.parentId = parentId
|
|
553
|
+
this.absPosCache.clear()
|
|
554
|
+
idx = Math.min(idx, newParent.childIds.length)
|
|
555
|
+
newParent.childIds.splice(idx, 0, nodeId)
|
|
556
|
+
|
|
557
|
+
this.emitter.emit('node:reordered', nodeId, parentId, idx, previousParentId)
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
insertChildAt(childId: string, parentId: string, index: number): void {
|
|
561
|
+
const node = this.getNode(childId)
|
|
562
|
+
const newParent = this.getNode(parentId)
|
|
563
|
+
if (!node || !newParent || childId === parentId || this.isDescendant(parentId, childId)) return
|
|
564
|
+
const previousParentId = node.parentId
|
|
565
|
+
const oldParent = previousParentId ? this.getNode(previousParentId) : undefined
|
|
566
|
+
if (oldParent) {
|
|
567
|
+
oldParent.childIds = oldParent.childIds.filter((id) => id !== childId)
|
|
568
|
+
}
|
|
569
|
+
newParent.childIds = newParent.childIds.filter((id) => id !== childId)
|
|
570
|
+
newParent.childIds.splice(index, 0, childId)
|
|
571
|
+
node.parentId = parentId
|
|
572
|
+
this.clearAbsPosCache()
|
|
573
|
+
this.emitter.emit('node:reordered', childId, parentId, index, previousParentId)
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
deleteNode(id: string): void {
|
|
577
|
+
const node = this.nodes.get(id)
|
|
578
|
+
if (!node || id === this.rootId) return
|
|
579
|
+
|
|
580
|
+
if (node.parentId) {
|
|
581
|
+
const parent = this.nodes.get(node.parentId)
|
|
582
|
+
if (parent) {
|
|
583
|
+
parent.childIds = parent.childIds.filter((cid) => cid !== id)
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
for (const childId of Array.from(node.childIds)) {
|
|
588
|
+
this.deleteNode(childId)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
if (node.type === 'INSTANCE' && node.componentId) {
|
|
592
|
+
this.instanceIndex.get(node.componentId)?.delete(id)
|
|
593
|
+
}
|
|
594
|
+
this.nodes.delete(id)
|
|
595
|
+
this.emitter.emit('node:deleted', id, node.parentId)
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
hitTest(px: number, py: number, scopeId?: string): SceneNode | null {
|
|
599
|
+
return HitTest.hitTest(this, px, py, scopeId)
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
hitTestDeep(px: number, py: number, scopeId?: string): SceneNode | null {
|
|
603
|
+
return HitTest.hitTestDeep(this, px, py, scopeId)
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
hitTestFrame(
|
|
607
|
+
px: number,
|
|
608
|
+
py: number,
|
|
609
|
+
excludeIds: Set<string>,
|
|
610
|
+
scopeId?: string
|
|
611
|
+
): SceneNode | null {
|
|
612
|
+
return HitTest.hitTestFrame(this, px, py, excludeIds, scopeId)
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
cloneTree(
|
|
616
|
+
sourceId: string,
|
|
617
|
+
parentId: string,
|
|
618
|
+
overrides: Partial<SceneNode> = {}
|
|
619
|
+
): SceneNode | null {
|
|
620
|
+
const src = this.nodes.get(sourceId)
|
|
621
|
+
if (!src) return null
|
|
622
|
+
|
|
623
|
+
const props = cloneNodeProps(src, null)
|
|
624
|
+
// Null out Figma source identifiers so the clone is treated as local.
|
|
625
|
+
// `as SourceMetadata` required: cloneNodeProps returns Partial<SceneNode>,
|
|
626
|
+
// so props.source is SourceMetadata | undefined, but we know it's always set.
|
|
627
|
+
props.source = { ...(props.source as SourceMetadata), id: null, orderKey: null }
|
|
628
|
+
const clone = this.createNode(src.type, parentId, { ...props, ...overrides })
|
|
629
|
+
|
|
630
|
+
for (const childId of src.childIds) {
|
|
631
|
+
this.cloneTree(childId, clone.id)
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
return clone
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
createInstance(
|
|
638
|
+
componentId: string,
|
|
639
|
+
parentId: string,
|
|
640
|
+
overrides: Partial<SceneNode> = {}
|
|
641
|
+
): SceneNode | null {
|
|
642
|
+
return Instances.createInstance(this, componentId, parentId, overrides)
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
populateInstanceChildren(
|
|
646
|
+
instanceId: string,
|
|
647
|
+
componentId: string,
|
|
648
|
+
mode: Instances.NodeCloneMode = 'deep'
|
|
649
|
+
): void {
|
|
650
|
+
Instances.populateInstanceChildren(this, instanceId, componentId, mode)
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
swapInstanceComponent(instanceId: string, componentId: string): void {
|
|
654
|
+
Instances.swapInstanceComponent(this, instanceId, componentId)
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
syncInstances(componentId: string): void {
|
|
658
|
+
Instances.syncInstances(this, componentId)
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
detachInstance(instanceId: string): void {
|
|
662
|
+
Instances.detachInstance(this, instanceId)
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
getMainComponent(instanceId: string): SceneNode | undefined {
|
|
666
|
+
return Instances.getMainComponent(this, instanceId)
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
getInstances(componentId: string): SceneNode[] {
|
|
670
|
+
return Instances.getInstances(this, componentId)
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
flattenTree(parentId?: string, depth = 0): Array<{ node: SceneNode; depth: number }> {
|
|
674
|
+
const id = parentId ?? this.rootId
|
|
675
|
+
const parent = this.nodes.get(id)
|
|
676
|
+
if (!parent) return []
|
|
677
|
+
const result: Array<{ node: SceneNode; depth: number }> = []
|
|
678
|
+
for (const childId of parent.childIds) {
|
|
679
|
+
const child = this.nodes.get(childId)
|
|
680
|
+
if (!child) continue
|
|
681
|
+
result.push({ node: child, depth })
|
|
682
|
+
if (child.childIds.length > 0) result.push(...this.flattenTree(childId, depth + 1))
|
|
683
|
+
}
|
|
684
|
+
return result
|
|
685
|
+
}
|
|
686
|
+
}
|