@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,599 @@
|
|
|
1
|
+
// Child cloning and property synchronization shared by instance creation, swap, and sync.
|
|
2
|
+
import { isEqual } from 'es-toolkit/predicate'
|
|
3
|
+
|
|
4
|
+
import type { ComponentPropertyReferenceField, SceneGraph, SceneNode } from '../'
|
|
5
|
+
import { cloneNodeProps, copyEffects, copyFills, copyStrokes, copyStyleRuns } from '../copy'
|
|
6
|
+
import type { NodeCloneMode } from '../copy'
|
|
7
|
+
import {
|
|
8
|
+
getInstanceOverride,
|
|
9
|
+
hasInstanceOverride as hasNodeInstanceOverride,
|
|
10
|
+
setInstanceOverride,
|
|
11
|
+
type InstanceOverrideState
|
|
12
|
+
} from '../instance-overrides'
|
|
13
|
+
import { scaleNodeChanges } from '../scaling/node'
|
|
14
|
+
import { ownsSlotContent, slotPropertyId } from '../slots/frames'
|
|
15
|
+
import { scaleVariableBindingUnits } from '../variables/units'
|
|
16
|
+
import {
|
|
17
|
+
createInstanceSyncContext,
|
|
18
|
+
hasUnplacedCopiesBelow,
|
|
19
|
+
restoreRemovedCopy,
|
|
20
|
+
unplacedCopy,
|
|
21
|
+
type InstanceSyncContext
|
|
22
|
+
} from './copies'
|
|
23
|
+
import { INSTANCE_SYNC_FIELD_OWNERS, INSTANCE_SYNC_FIELDS } from './fields'
|
|
24
|
+
import { PLACEMENT_FIELDS, placedByConstraints } from './placement'
|
|
25
|
+
|
|
26
|
+
function setSceneProp<K extends keyof SceneNode>(
|
|
27
|
+
target: Partial<SceneNode>,
|
|
28
|
+
key: K,
|
|
29
|
+
value: SceneNode[K]
|
|
30
|
+
): void {
|
|
31
|
+
target[key] = value
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function copyProp(
|
|
35
|
+
target: Partial<SceneNode> | SceneNode,
|
|
36
|
+
source: SceneNode,
|
|
37
|
+
key: keyof SceneNode
|
|
38
|
+
): void {
|
|
39
|
+
if (key === 'fills') {
|
|
40
|
+
setSceneProp(target, key, copyFills(source.fills))
|
|
41
|
+
} else if (key === 'strokes') {
|
|
42
|
+
setSceneProp(target, key, copyStrokes(source.strokes))
|
|
43
|
+
} else if (key === 'effects') {
|
|
44
|
+
setSceneProp(target, key, copyEffects(source.effects))
|
|
45
|
+
} else if (key === 'styleRuns') {
|
|
46
|
+
setSceneProp(target, key, copyStyleRuns(source.styleRuns))
|
|
47
|
+
} else if (key === 'boundVariables') {
|
|
48
|
+
// Shallow copy the binding map — values are variable IDs (strings), not objects
|
|
49
|
+
setSceneProp(target, key, { ...source.boundVariables })
|
|
50
|
+
setSceneProp(target, 'variableBindingScales', { ...source.variableBindingScales })
|
|
51
|
+
} else if (key === 'variableModes') {
|
|
52
|
+
setSceneProp(target, key, { ...source.variableModes })
|
|
53
|
+
} else if (key === 'gridPosition') {
|
|
54
|
+
// Shallow copy the grid position object — all fields are primitives
|
|
55
|
+
setSceneProp(target, key, source.gridPosition ? { ...source.gridPosition } : null)
|
|
56
|
+
} else if (key === 'targetAspectRatio') {
|
|
57
|
+
setSceneProp(target, key, source.targetAspectRatio ? { ...source.targetAspectRatio } : null)
|
|
58
|
+
} else {
|
|
59
|
+
const value = source[key]
|
|
60
|
+
setSceneProp(target, key, Array.isArray(value) ? structuredClone(value) : value)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function bindingProtection(scopes: ReadonlySet<string>[]): (field: string) => boolean {
|
|
65
|
+
const fields = new Set<string>()
|
|
66
|
+
for (const scope of scopes) {
|
|
67
|
+
const perField = [...scope].some((field) => field.startsWith('boundVariables/'))
|
|
68
|
+
for (const field of scope) {
|
|
69
|
+
if (field !== 'boundVariables' || !perField) fields.add(field)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return (field) => fields.has(field)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function syncBindingFields(
|
|
76
|
+
target: SceneNode,
|
|
77
|
+
source: SceneNode,
|
|
78
|
+
updates: Partial<SceneNode>,
|
|
79
|
+
protectedField: (field: string) => boolean
|
|
80
|
+
): void {
|
|
81
|
+
if (protectedField('boundVariables')) return
|
|
82
|
+
const bindings = { ...source.boundVariables }
|
|
83
|
+
const scales = { ...source.variableBindingScales }
|
|
84
|
+
for (const field of new Set([
|
|
85
|
+
...Object.keys(source.boundVariables),
|
|
86
|
+
...Object.keys(target.boundVariables),
|
|
87
|
+
...Object.keys(target.variableBindingScales)
|
|
88
|
+
])) {
|
|
89
|
+
if (!protectedField(`boundVariables/${field}`)) continue
|
|
90
|
+
if (!Object.hasOwn(target.boundVariables, field)) Reflect.deleteProperty(bindings, field)
|
|
91
|
+
else bindings[field] = target.boundVariables[field]
|
|
92
|
+
if (target.variableBindingScales[field] === undefined) Reflect.deleteProperty(scales, field)
|
|
93
|
+
else scales[field] = target.variableBindingScales[field]
|
|
94
|
+
}
|
|
95
|
+
updates.boundVariables = bindings
|
|
96
|
+
updates.variableBindingScales = scales
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function isProtectedSyncField(
|
|
100
|
+
node: SceneNode,
|
|
101
|
+
key: string,
|
|
102
|
+
protectedField: (field: string) => boolean
|
|
103
|
+
): boolean {
|
|
104
|
+
return (
|
|
105
|
+
protectedField(key) ||
|
|
106
|
+
(key in node.boundVariables &&
|
|
107
|
+
(protectedField('boundVariables') || protectedField(`boundVariables/${key}`)))
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function sourceInTargetCoordinates(source: SceneNode, targetScale: number): SceneNode {
|
|
112
|
+
const factor = targetScale / source.componentScale
|
|
113
|
+
if (!Number.isFinite(factor) || factor <= 0) throw new Error('Invalid component coordinate scale')
|
|
114
|
+
if (factor === 1) return source
|
|
115
|
+
return {
|
|
116
|
+
...source,
|
|
117
|
+
...scaleNodeChanges(source, factor, true),
|
|
118
|
+
...scaleVariableBindingUnits(source, factor)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface SyncedFieldsInput {
|
|
123
|
+
compParent: SceneNode
|
|
124
|
+
instParent: SceneNode
|
|
125
|
+
/** The component's coordinates in the copy's. */
|
|
126
|
+
factor: number
|
|
127
|
+
/** Fields the copy keeps: overridden, bound, or driven by a property. */
|
|
128
|
+
skip: (key: string) => boolean
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The fields a copy takes from its component's layer, placed in the copy's parent. */
|
|
132
|
+
function copySyncedFields(
|
|
133
|
+
updates: Partial<SceneNode>,
|
|
134
|
+
copy: SceneNode,
|
|
135
|
+
source: SceneNode,
|
|
136
|
+
{ compParent, instParent, factor, skip }: SyncedFieldsInput
|
|
137
|
+
): void {
|
|
138
|
+
// In an auto layout flow the layout places the copy, as it placed the component's layer.
|
|
139
|
+
const placedByLayout = instParent.layoutMode !== 'NONE' && source.layoutPositioning !== 'ABSOLUTE'
|
|
140
|
+
const placed = placedByLayout
|
|
141
|
+
? source
|
|
142
|
+
: placedByConstraints(source, compParent, instParent, factor)
|
|
143
|
+
for (const key of INSTANCE_SYNC_FIELDS) {
|
|
144
|
+
if (key === 'boundVariables') continue
|
|
145
|
+
if (placedByLayout && (key === 'x' || key === 'y')) continue
|
|
146
|
+
if (skip(key)) continue
|
|
147
|
+
const owner = INSTANCE_SYNC_FIELD_OWNERS[key]
|
|
148
|
+
if (owner && skip(owner)) continue
|
|
149
|
+
const from = PLACEMENT_FIELDS.has(key) ? placed : source
|
|
150
|
+
// Most fields already match: comparing first spares cloning them for nothing.
|
|
151
|
+
if (isEqual(copy[key], from[key])) continue
|
|
152
|
+
copyProp(updates, from, key)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function updateSyncedProps(
|
|
157
|
+
graph: SceneGraph,
|
|
158
|
+
target: SceneNode,
|
|
159
|
+
updates: Partial<SceneNode>
|
|
160
|
+
): void {
|
|
161
|
+
const changed = Object.fromEntries(
|
|
162
|
+
Object.entries(updates).filter(
|
|
163
|
+
([key, value]) => !isEqual(target[key as keyof SceneNode], value)
|
|
164
|
+
)
|
|
165
|
+
) as Partial<SceneNode>
|
|
166
|
+
if (Object.keys(changed).length) graph.updateNode(target.id, changed)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function cloneChildInCoordinates(
|
|
170
|
+
graph: SceneGraph,
|
|
171
|
+
src: SceneNode,
|
|
172
|
+
sourceParent: SceneNode,
|
|
173
|
+
targetParent: SceneNode,
|
|
174
|
+
mode: NodeCloneMode = 'deep'
|
|
175
|
+
): SceneNode {
|
|
176
|
+
const componentScale =
|
|
177
|
+
(src.componentScale * targetParent.componentScale) / sourceParent.componentScale
|
|
178
|
+
return graph.createNode(src.type, targetParent.id, {
|
|
179
|
+
...cloneNodeProps(sourceInTargetCoordinates(src, componentScale), src.id, mode),
|
|
180
|
+
componentScale
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function cloneChildrenWithMapping(
|
|
185
|
+
graph: SceneGraph,
|
|
186
|
+
sourceParentId: string,
|
|
187
|
+
destParentId: string,
|
|
188
|
+
mode: NodeCloneMode = 'deep'
|
|
189
|
+
): void {
|
|
190
|
+
// Guard against cloning a subtree into itself or its own descendant. Without this,
|
|
191
|
+
// a self-referential or cyclic component (e.g. an INSTANCE whose componentId points
|
|
192
|
+
// to an ancestor) causes unbounded recursion and eventual stack overflow / OOM.
|
|
193
|
+
if (sourceParentId === destParentId || graph.isDescendant(destParentId, sourceParentId)) return
|
|
194
|
+
|
|
195
|
+
const sourceParent = graph.nodes.get(sourceParentId)
|
|
196
|
+
const targetParent = graph.nodes.get(destParentId)
|
|
197
|
+
if (!sourceParent || !targetParent) return
|
|
198
|
+
|
|
199
|
+
for (const childId of sourceParent.childIds) {
|
|
200
|
+
const src = graph.nodes.get(childId)
|
|
201
|
+
if (!src) continue
|
|
202
|
+
|
|
203
|
+
const clone = cloneChildInCoordinates(graph, src, sourceParent, targetParent, mode)
|
|
204
|
+
|
|
205
|
+
if (src.childIds.length > 0) {
|
|
206
|
+
cloneChildrenWithMapping(graph, childId, clone.id, mode)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function enclosingInstanceOverrideFields(graph: SceneGraph, node: SceneNode): Set<string>[] {
|
|
212
|
+
const fields: Set<string>[] = []
|
|
213
|
+
let parent = node.parentId ? graph.nodes.get(node.parentId) : undefined
|
|
214
|
+
while (parent) {
|
|
215
|
+
if (parent.type === 'INSTANCE') {
|
|
216
|
+
fields.push(new Set(parent.instanceOverrides.descendants.get(node.id)?.keys()))
|
|
217
|
+
}
|
|
218
|
+
parent = parent.parentId ? graph.nodes.get(parent.parentId) : undefined
|
|
219
|
+
}
|
|
220
|
+
return fields
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** SLOT_CONTENT is absent because it drives children, which `ownsSlotContent` keeps instead. */
|
|
224
|
+
const PROPERTY_REFERENCE_FIELDS: Partial<Record<ComponentPropertyReferenceField, string>> = {
|
|
225
|
+
VISIBLE: 'visible',
|
|
226
|
+
TEXT: 'text',
|
|
227
|
+
INSTANCE_SWAP: 'componentId'
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Instance links are expected to be shallow; the cap only stops a cycle from hanging sync. */
|
|
231
|
+
const INSTANCE_CHAIN_LIMIT = 16
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Whether this instance's component, or the set it is a variant of, is where `propertyId` is
|
|
235
|
+
* defined. Only a set counts: a component nested in an ordinary component defines its own
|
|
236
|
+
* properties, not the outer one's.
|
|
237
|
+
*/
|
|
238
|
+
function definesProperty(graph: SceneGraph, instance: SceneNode, propertyId: string): boolean {
|
|
239
|
+
// An instance of an instance links through to the component, so follow the chain to it.
|
|
240
|
+
let component = instance.componentId ? graph.nodes.get(instance.componentId) : undefined
|
|
241
|
+
for (let hops = 0; component?.type === 'INSTANCE' && hops < INSTANCE_CHAIN_LIMIT; hops++) {
|
|
242
|
+
component = component.componentId ? graph.nodes.get(component.componentId) : undefined
|
|
243
|
+
}
|
|
244
|
+
const parent = component?.parentId ? graph.nodes.get(component.parentId) : undefined
|
|
245
|
+
const set = parent?.type === 'COMPONENT_SET' ? parent : undefined
|
|
246
|
+
return [component, set]
|
|
247
|
+
.flatMap((node) => node?.componentPropertyDefinitions ?? [])
|
|
248
|
+
.some((definition) => definition.id === propertyId)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* What the nearest enclosing instance assigns `propertyId`, if any does. A property id belongs
|
|
253
|
+
* to the component that defines it, so the walk stops at an instance of that component even
|
|
254
|
+
* when it assigns nothing; otherwise an outer instance's unrelated property of the same id wins.
|
|
255
|
+
*/
|
|
256
|
+
function enclosingAssignment(
|
|
257
|
+
graph: SceneGraph,
|
|
258
|
+
node: SceneNode,
|
|
259
|
+
propertyId: string
|
|
260
|
+
): string | undefined {
|
|
261
|
+
let current: SceneNode | undefined = node
|
|
262
|
+
while (current) {
|
|
263
|
+
if (current.type === 'INSTANCE') {
|
|
264
|
+
if (Object.hasOwn(current.componentPropertyAssignments, propertyId))
|
|
265
|
+
return current.componentPropertyAssignments[propertyId]
|
|
266
|
+
if (definesProperty(graph, current, propertyId)) return undefined
|
|
267
|
+
}
|
|
268
|
+
current = current.parentId ? graph.nodes.get(current.parentId) : undefined
|
|
269
|
+
}
|
|
270
|
+
return undefined
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function hasEnclosingAssignment(graph: SceneGraph, node: SceneNode, propertyId: string): boolean {
|
|
274
|
+
return enclosingAssignment(graph, node, propertyId) !== undefined
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* A component can gain a property-driven layer after an instance of it exists. Pass 4 leaves a
|
|
279
|
+
* driven field alone, so the fresh clone has to take the enclosing instance's assignment here or
|
|
280
|
+
* it keeps the component's default while every other instance layer shows the assigned value.
|
|
281
|
+
*/
|
|
282
|
+
function applyEnclosingAssignments(graph: SceneGraph, clone: SceneNode): void {
|
|
283
|
+
for (const reference of clone.componentPropertyReferences) {
|
|
284
|
+
const field = PROPERTY_REFERENCE_FIELDS[reference.field]
|
|
285
|
+
if (!field) continue
|
|
286
|
+
const value = enclosingAssignment(graph, clone, reference.propertyId)
|
|
287
|
+
if (value === undefined) continue
|
|
288
|
+
if (field === 'visible') graph.updateNode(clone.id, { visible: value === 'true' })
|
|
289
|
+
else if (field === 'text') graph.updateNode(clone.id, { text: value })
|
|
290
|
+
else if (field === 'componentId' && clone.type === 'INSTANCE' && graph.nodes.has(value)) {
|
|
291
|
+
graph.swapInstanceComponent(clone.id, value)
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
for (const child of graph.getChildren(clone.id)) applyEnclosingAssignments(graph, child)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Fields a component property drives on this child. The component states the default, but
|
|
299
|
+
* an enclosing instance's assignment decides the value, so synchronising must not copy the
|
|
300
|
+
* default over it — a page loaded later would otherwise reset the instance to the default.
|
|
301
|
+
*/
|
|
302
|
+
function propertyDrivenFields(
|
|
303
|
+
graph: SceneGraph,
|
|
304
|
+
instChild: SceneNode,
|
|
305
|
+
compChild: SceneNode
|
|
306
|
+
): Set<string> {
|
|
307
|
+
const driven = new Set<string>()
|
|
308
|
+
for (const reference of compChild.componentPropertyReferences) {
|
|
309
|
+
const field = PROPERTY_REFERENCE_FIELDS[reference.field]
|
|
310
|
+
if (field && hasEnclosingAssignment(graph, instChild, reference.propertyId)) driven.add(field)
|
|
311
|
+
}
|
|
312
|
+
return driven
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The text and visibility enclosing assignments give an existing child whose layer a property
|
|
317
|
+
* drives. A layer linked to a property after the instance was made would otherwise keep what it
|
|
318
|
+
* showed before, since synchronising leaves driven fields alone.
|
|
319
|
+
*/
|
|
320
|
+
function assignedFieldValues(
|
|
321
|
+
graph: SceneGraph,
|
|
322
|
+
instChild: SceneNode,
|
|
323
|
+
compChild: SceneNode
|
|
324
|
+
): Partial<SceneNode> {
|
|
325
|
+
const values: Partial<SceneNode> = {}
|
|
326
|
+
for (const reference of compChild.componentPropertyReferences) {
|
|
327
|
+
const value = enclosingAssignment(graph, instChild, reference.propertyId)
|
|
328
|
+
if (value === undefined) continue
|
|
329
|
+
if (reference.field === 'TEXT' && instChild.type === 'TEXT') values.text = value
|
|
330
|
+
else if (reference.field === 'VISIBLE') values.visible = value === 'true'
|
|
331
|
+
}
|
|
332
|
+
return values
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function childBindingProtection(
|
|
336
|
+
graph: SceneGraph,
|
|
337
|
+
child: SceneNode,
|
|
338
|
+
overrides: InstanceOverrideState
|
|
339
|
+
) {
|
|
340
|
+
const fields = enclosingInstanceOverrideFields(graph, child)
|
|
341
|
+
fields.push(new Set(overrides.descendants.get(child.id)?.keys()))
|
|
342
|
+
return bindingProtection(fields)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function linkMatchedChild(
|
|
346
|
+
overrides: InstanceOverrideState,
|
|
347
|
+
instParentId: string,
|
|
348
|
+
instChild: SceneNode,
|
|
349
|
+
compChildId: string
|
|
350
|
+
): void {
|
|
351
|
+
if (instChild.type === 'INSTANCE') {
|
|
352
|
+
setInstanceOverride(overrides, instParentId, instChild.id, 'sourceComponentId', compChildId)
|
|
353
|
+
} else {
|
|
354
|
+
instChild.componentId = compChildId
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function matchFallbackChildren(
|
|
359
|
+
graph: SceneGraph,
|
|
360
|
+
compParent: SceneNode,
|
|
361
|
+
instParent: SceneNode,
|
|
362
|
+
instParentId: string,
|
|
363
|
+
overrides: InstanceOverrideState,
|
|
364
|
+
instChildMap: Map<string, SceneNode>,
|
|
365
|
+
usedInstChildIds: Set<string>
|
|
366
|
+
): void {
|
|
367
|
+
const fallbackByType = new Map<SceneNode['type'], Map<string, SceneNode[]>>()
|
|
368
|
+
const remainingCandidateCount = new Map<SceneNode['type'], number>()
|
|
369
|
+
const unmatchedComponentCount = new Map<SceneNode['type'], number>()
|
|
370
|
+
for (const compChildId of compParent.childIds) {
|
|
371
|
+
if (instChildMap.has(compChildId)) continue
|
|
372
|
+
const child = graph.nodes.get(compChildId)
|
|
373
|
+
if (child) {
|
|
374
|
+
unmatchedComponentCount.set(child.type, (unmatchedComponentCount.get(child.type) ?? 0) + 1)
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
for (const childId of instParent.childIds) {
|
|
378
|
+
const child = graph.nodes.get(childId)
|
|
379
|
+
if (!child || usedInstChildIds.has(child.id)) continue
|
|
380
|
+
remainingCandidateCount.set(child.type, (remainingCandidateCount.get(child.type) ?? 0) + 1)
|
|
381
|
+
let byName = fallbackByType.get(child.type)
|
|
382
|
+
if (!byName) {
|
|
383
|
+
byName = new Map()
|
|
384
|
+
fallbackByType.set(child.type, byName)
|
|
385
|
+
}
|
|
386
|
+
const queue = byName.get(child.name)
|
|
387
|
+
if (queue) queue.push(child)
|
|
388
|
+
else byName.set(child.name, [child])
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Match by name and type with FIFO queues to preserve sibling order in linear time.
|
|
392
|
+
for (const compChildId of compParent.childIds) {
|
|
393
|
+
if (instChildMap.has(compChildId)) continue
|
|
394
|
+
const compChild = graph.nodes.get(compChildId)
|
|
395
|
+
if (!compChild) continue
|
|
396
|
+
const candidatesByName = fallbackByType.get(compChild.type)
|
|
397
|
+
const candidateCount = remainingCandidateCount.get(compChild.type) ?? 0
|
|
398
|
+
if (candidateCount > (unmatchedComponentCount.get(compChild.type) ?? 0)) continue
|
|
399
|
+
const match = candidatesByName?.get(compChild.name)?.shift()
|
|
400
|
+
if (!match) continue
|
|
401
|
+
remainingCandidateCount.set(compChild.type, candidateCount - 1)
|
|
402
|
+
instChildMap.set(compChildId, match)
|
|
403
|
+
usedInstChildIds.add(match.id)
|
|
404
|
+
linkMatchedChild(overrides, instParentId, match, compChildId)
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function sortInstanceChildren(
|
|
409
|
+
graph: SceneGraph,
|
|
410
|
+
instParent: SceneNode,
|
|
411
|
+
instParentId: string,
|
|
412
|
+
compChildOrder: string[],
|
|
413
|
+
overrides: InstanceOverrideState
|
|
414
|
+
): void {
|
|
415
|
+
const orderMap = new Map<string, number>()
|
|
416
|
+
for (let i = 0; i < compChildOrder.length; i++) orderMap.set(compChildOrder[i], i)
|
|
417
|
+
|
|
418
|
+
const ranks = new Map<string, number>()
|
|
419
|
+
for (let index = 0; index < instParent.childIds.length; index++) {
|
|
420
|
+
const childId = instParent.childIds[index]
|
|
421
|
+
const node = graph.nodes.get(childId)
|
|
422
|
+
const source = node
|
|
423
|
+
? getInstanceOverride(overrides, instParentId, node.id, 'sourceComponentId')
|
|
424
|
+
: undefined
|
|
425
|
+
const mapped = typeof source === 'string' ? source : node?.componentId
|
|
426
|
+
const componentIndex = mapped ? orderMap.get(mapped) : undefined
|
|
427
|
+
ranks.set(childId, componentIndex ?? compChildOrder.length + index)
|
|
428
|
+
}
|
|
429
|
+
const sorted = instParent.childIds.toSorted(
|
|
430
|
+
(left, right) => (ranks.get(left) ?? 0) - (ranks.get(right) ?? 0)
|
|
431
|
+
)
|
|
432
|
+
// Move through the graph so the reorder is reported, as collaboration syncs it.
|
|
433
|
+
sorted.forEach((childId, index) => {
|
|
434
|
+
if (instParent.childIds[index] !== childId) graph.insertChildAt(childId, instParentId, index)
|
|
435
|
+
})
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** True when syncing `compParentId` into `instParentId` would form a cycle. */
|
|
439
|
+
function isCyclicSync(graph: SceneGraph, compParentId: string, instParentId: string): boolean {
|
|
440
|
+
return compParentId === instParentId || graph.isDescendant(instParentId, compParentId)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** Children already linked to a component child by sourceComponentId or componentId. */
|
|
444
|
+
function matchLinkedChildren(
|
|
445
|
+
graph: SceneGraph,
|
|
446
|
+
instParent: SceneNode,
|
|
447
|
+
instParentId: string,
|
|
448
|
+
overrides: InstanceOverrideState,
|
|
449
|
+
compChildIdSet: ReadonlySet<string>,
|
|
450
|
+
instChildMap: Map<string, SceneNode>,
|
|
451
|
+
usedInstChildIds: Set<string>
|
|
452
|
+
): void {
|
|
453
|
+
for (const childId of instParent.childIds) {
|
|
454
|
+
const child = graph.nodes.get(childId)
|
|
455
|
+
if (!child) continue
|
|
456
|
+
const sourceComponentId = getInstanceOverride(
|
|
457
|
+
overrides,
|
|
458
|
+
instParentId,
|
|
459
|
+
child.id,
|
|
460
|
+
'sourceComponentId'
|
|
461
|
+
)
|
|
462
|
+
const mappedComponentId =
|
|
463
|
+
typeof sourceComponentId === 'string' ? sourceComponentId : child.componentId
|
|
464
|
+
if (mappedComponentId && compChildIdSet.has(mappedComponentId)) {
|
|
465
|
+
instChildMap.set(mappedComponentId, child)
|
|
466
|
+
usedInstChildIds.add(child.id)
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Pass 3 of a sync: bring along the copy of a layer moved here inside the component, bring back
|
|
473
|
+
* one the sync removed when its layer returns, or clone a layer the instance has no copy of. A
|
|
474
|
+
* clone takes its own subtree unless copies the instance has belong in it, which the recursion
|
|
475
|
+
* moves in instead. Children left unmatched are the sync's to remove once it finishes.
|
|
476
|
+
*/
|
|
477
|
+
function placeMissingChildren(
|
|
478
|
+
graph: SceneGraph,
|
|
479
|
+
compParent: SceneNode,
|
|
480
|
+
instParent: SceneNode,
|
|
481
|
+
instChildMap: Map<string, SceneNode>,
|
|
482
|
+
usedInstChildIds: Set<string>,
|
|
483
|
+
context: InstanceSyncContext
|
|
484
|
+
): void {
|
|
485
|
+
for (const child of instChildMap.values()) context.matched.add(child.id)
|
|
486
|
+
|
|
487
|
+
for (const compChildId of compParent.childIds) {
|
|
488
|
+
if (instChildMap.has(compChildId)) continue
|
|
489
|
+
const moved =
|
|
490
|
+
unplacedCopy(graph, context, compChildId, instParent.id) ??
|
|
491
|
+
restoreRemovedCopy(graph, context, compChildId, instParent.id)
|
|
492
|
+
if (moved) {
|
|
493
|
+
graph.insertChildAt(moved.id, instParent.id, instParent.childIds.length)
|
|
494
|
+
instChildMap.set(compChildId, moved)
|
|
495
|
+
usedInstChildIds.add(moved.id)
|
|
496
|
+
context.matched.add(moved.id)
|
|
497
|
+
continue
|
|
498
|
+
}
|
|
499
|
+
const src = graph.nodes.get(compChildId)
|
|
500
|
+
if (!src) continue
|
|
501
|
+
const clone = cloneChildInCoordinates(graph, src, compParent, instParent)
|
|
502
|
+
if (src.childIds.length > 0 && !hasUnplacedCopiesBelow(graph, context, compChildId)) {
|
|
503
|
+
cloneChildrenWithMapping(graph, compChildId, clone.id)
|
|
504
|
+
}
|
|
505
|
+
applyEnclosingAssignments(graph, clone)
|
|
506
|
+
instChildMap.set(compChildId, clone)
|
|
507
|
+
usedInstChildIds.add(clone.id)
|
|
508
|
+
context.matched.add(clone.id)
|
|
509
|
+
}
|
|
510
|
+
for (const childId of instParent.childIds) {
|
|
511
|
+
if (!usedInstChildIds.has(childId)) context.leftovers.add(childId)
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
export function syncChildren(
|
|
516
|
+
graph: SceneGraph,
|
|
517
|
+
compParentId: string,
|
|
518
|
+
instParentId: string,
|
|
519
|
+
overrides: InstanceOverrideState,
|
|
520
|
+
context: InstanceSyncContext = createInstanceSyncContext(instParentId, overrides)
|
|
521
|
+
): void {
|
|
522
|
+
// Guard against cyclic sync: if the instance parent is inside the component's own
|
|
523
|
+
// subtree, syncing would clone the component into itself — a self-referential cycle
|
|
524
|
+
// that causes unbounded recursion and OOM.
|
|
525
|
+
if (isCyclicSync(graph, compParentId, instParentId)) return
|
|
526
|
+
|
|
527
|
+
const compParent = graph.nodes.get(compParentId)
|
|
528
|
+
const instParent = graph.nodes.get(instParentId)
|
|
529
|
+
if (!compParent || !instParent) return
|
|
530
|
+
|
|
531
|
+
const instChildMap = new Map<string, SceneNode>()
|
|
532
|
+
const usedInstChildIds = new Set<string>()
|
|
533
|
+
const compChildIdSet = new Set(compParent.childIds)
|
|
534
|
+
|
|
535
|
+
// Pass 1: Direct matching via sourceComponentId or componentId
|
|
536
|
+
matchLinkedChildren(
|
|
537
|
+
graph,
|
|
538
|
+
instParent,
|
|
539
|
+
instParentId,
|
|
540
|
+
overrides,
|
|
541
|
+
compChildIdSet,
|
|
542
|
+
instChildMap,
|
|
543
|
+
usedInstChildIds
|
|
544
|
+
)
|
|
545
|
+
|
|
546
|
+
// Pass 2: Fallback matching for unmatched children (e.g. from Figma imports)
|
|
547
|
+
matchFallbackChildren(
|
|
548
|
+
graph,
|
|
549
|
+
compParent,
|
|
550
|
+
instParent,
|
|
551
|
+
instParentId,
|
|
552
|
+
overrides,
|
|
553
|
+
instChildMap,
|
|
554
|
+
usedInstChildIds
|
|
555
|
+
)
|
|
556
|
+
|
|
557
|
+
placeMissingChildren(graph, compParent, instParent, instChildMap, usedInstChildIds, context)
|
|
558
|
+
|
|
559
|
+
// Pass 4: Synchronize properties and recurse
|
|
560
|
+
for (const compChildId of compParent.childIds) {
|
|
561
|
+
const compChild = graph.nodes.get(compChildId)
|
|
562
|
+
const instChild = instChildMap.get(compChildId)
|
|
563
|
+
if (!compChild || !instChild) continue
|
|
564
|
+
|
|
565
|
+
const protectedField = childBindingProtection(graph, instChild, overrides)
|
|
566
|
+
const driven = propertyDrivenFields(graph, instChild, compChild)
|
|
567
|
+
const componentScale =
|
|
568
|
+
(compChild.componentScale * instParent.componentScale) / compParent.componentScale
|
|
569
|
+
const source = sourceInTargetCoordinates(compChild, componentScale)
|
|
570
|
+
// Which properties a layer serves is the component's to say; a slot or exposed layer
|
|
571
|
+
// created on the component becomes one in every instance.
|
|
572
|
+
const updates: Partial<SceneNode> = {
|
|
573
|
+
componentScale,
|
|
574
|
+
componentPropertyReferences: structuredClone(compChild.componentPropertyReferences)
|
|
575
|
+
}
|
|
576
|
+
syncBindingFields(instChild, source, updates, protectedField)
|
|
577
|
+
copySyncedFields(updates, instChild, source, {
|
|
578
|
+
compParent,
|
|
579
|
+
instParent,
|
|
580
|
+
factor: componentScale / compChild.componentScale,
|
|
581
|
+
skip: (key) => driven.has(key) || isProtectedSyncField(instChild, key, protectedField)
|
|
582
|
+
})
|
|
583
|
+
Object.assign(updates, assignedFieldValues(graph, instChild, compChild))
|
|
584
|
+
updateSyncedProps(graph, instChild, updates)
|
|
585
|
+
|
|
586
|
+
if (
|
|
587
|
+
compChild.childIds.length > 0 &&
|
|
588
|
+
!hasNodeInstanceOverride(overrides, instParentId, instChild.id, 'componentId') &&
|
|
589
|
+
// The component's frame is the authority on which slot this is; instance copies of
|
|
590
|
+
// its bindings are not synced.
|
|
591
|
+
!ownsSlotContent(graph, instChild, slotPropertyId(compChild))
|
|
592
|
+
) {
|
|
593
|
+
syncChildren(graph, compChildId, instChild.id, overrides, context)
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// Pass 5: Sort instance children to match component child order
|
|
598
|
+
sortInstanceChildren(graph, instParent, instParentId, compParent.childIds, overrides)
|
|
599
|
+
}
|