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