@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,58 @@
|
|
|
1
|
+
import type { SceneGraph, SceneNode } from '../'
|
|
2
|
+
import { TEXT_METRIC_FIELDS } from '../fields/text'
|
|
3
|
+
import { layoutSizing } from '../layout/sizing'
|
|
4
|
+
|
|
5
|
+
const METRIC_FIELDS: ReadonlySet<string> = new Set(TEXT_METRIC_FIELDS)
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The axes along which a copy sizes itself to its own content rather than to its component,
|
|
9
|
+
* judged by the state the copy is about to have.
|
|
10
|
+
*/
|
|
11
|
+
function contentSizedAxes(
|
|
12
|
+
graph: SceneGraph,
|
|
13
|
+
current: SceneNode,
|
|
14
|
+
source: SceneNode,
|
|
15
|
+
updates: Partial<SceneNode>
|
|
16
|
+
): { width: boolean; height: boolean } {
|
|
17
|
+
const copy: SceneNode = { ...current, ...updates }
|
|
18
|
+
// Text measures its own characters, set on the copy or by a property of an instance around it.
|
|
19
|
+
if (copy.type === 'TEXT') {
|
|
20
|
+
if (copy.text === source.text) return { width: false, height: false }
|
|
21
|
+
const mode = copy.textAutoResize
|
|
22
|
+
return {
|
|
23
|
+
width: mode === 'WIDTH_AND_HEIGHT',
|
|
24
|
+
height: mode === 'WIDTH_AND_HEIGHT' || mode === 'HEIGHT'
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Auto layout hugs the copy's own layers, which may show or size differently.
|
|
28
|
+
if (copy.layoutMode === 'NONE') return { width: false, height: false }
|
|
29
|
+
return {
|
|
30
|
+
width: layoutSizing(graph, copy, 'HORIZONTAL') === 'HUG',
|
|
31
|
+
height: layoutSizing(graph, copy, 'VERTICAL') === 'HUG'
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Keeps a copy's own size along the axes it sizes to its own content, as Figma sizes a badge to
|
|
37
|
+
* its count and the layers it shows rather than to the component's placeholder. Layout measures
|
|
38
|
+
* those axes; the component's size there would be stale.
|
|
39
|
+
*/
|
|
40
|
+
export function keepOwnContentSize(
|
|
41
|
+
graph: SceneGraph,
|
|
42
|
+
updates: Partial<SceneNode>,
|
|
43
|
+
copy: SceneNode,
|
|
44
|
+
source: SceneNode
|
|
45
|
+
): void {
|
|
46
|
+
const axes = contentSizedAxes(graph, copy, source, updates)
|
|
47
|
+
if (axes.width) Reflect.deleteProperty(updates, 'width')
|
|
48
|
+
if (axes.height) Reflect.deleteProperty(updates, 'height')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Drops the size a text copy kept from the file once a metric reaches it from the component, so
|
|
53
|
+
* layout measures its characters again instead of trusting the size Figma saved for the old style.
|
|
54
|
+
*/
|
|
55
|
+
export function remeasureChangedText(updates: Partial<SceneNode>, copy: SceneNode): void {
|
|
56
|
+
if (copy.type !== 'TEXT' || !copy.derivedLayout) return
|
|
57
|
+
if (Object.keys(updates).some((key) => METRIC_FIELDS.has(key))) updates.derivedLayout = null
|
|
58
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// How an instance's copies follow the component's layers when its structure changes.
|
|
2
|
+
import type { SceneGraph, SceneNode } from '../'
|
|
3
|
+
import {
|
|
4
|
+
getInstanceOverride,
|
|
5
|
+
hasInstanceOverride as hasNodeInstanceOverride,
|
|
6
|
+
type InstanceOverrideState
|
|
7
|
+
} from '../instance-overrides'
|
|
8
|
+
import { ownsSlotContent, slotPropertyId } from '../slots/frames'
|
|
9
|
+
import { DEFAULT_HISTORY_LIMIT } from '../undo'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* One instance's sync: the copies it has matched to the component's layers, the parents it
|
|
13
|
+
* synced, and, built on first need, the copies the instance had before, by the layer they copy.
|
|
14
|
+
* Copies follow their layers when the component's structure changes, as Figma moves them: a
|
|
15
|
+
* layer moved inside the component keeps its copy and the copy's overrides, and a copy whose
|
|
16
|
+
* layer left or was deleted goes.
|
|
17
|
+
*/
|
|
18
|
+
export interface InstanceSyncContext {
|
|
19
|
+
instanceId: string
|
|
20
|
+
overrides: InstanceOverrideState
|
|
21
|
+
matched: Set<string>
|
|
22
|
+
leftovers: Set<string>
|
|
23
|
+
copies?: Map<string, SceneNode[]>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createInstanceSyncContext(
|
|
27
|
+
instanceId: string,
|
|
28
|
+
overrides: InstanceOverrideState
|
|
29
|
+
): InstanceSyncContext {
|
|
30
|
+
return { instanceId, overrides, matched: new Set(), leftovers: new Set() }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The component layer a copy in the instance stands for. */
|
|
34
|
+
function copiedLayerId(node: SceneNode, context: InstanceSyncContext): string | undefined {
|
|
35
|
+
const source =
|
|
36
|
+
node.type === 'INSTANCE'
|
|
37
|
+
? getInstanceOverride(context.overrides, context.instanceId, node.id, 'sourceComponentId')
|
|
38
|
+
: undefined
|
|
39
|
+
return typeof source === 'string' ? source : (node.componentId ?? undefined)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whether sync goes inside a copy: not into slot content, a swapped instance, or an instance. */
|
|
43
|
+
function syncsInside(graph: SceneGraph, node: SceneNode, context: InstanceSyncContext): boolean {
|
|
44
|
+
if (node.type === 'INSTANCE') return false
|
|
45
|
+
if (hasNodeInstanceOverride(context.overrides, context.instanceId, node.id, 'componentId')) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
const layer = node.componentId ? graph.nodes.get(node.componentId) : undefined
|
|
49
|
+
return !layer || !ownsSlotContent(graph, node, slotPropertyId(layer))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function copiesOf(graph: SceneGraph, context: InstanceSyncContext): Map<string, SceneNode[]> {
|
|
53
|
+
if (context.copies) return context.copies
|
|
54
|
+
const copies = new Map<string, SceneNode[]>()
|
|
55
|
+
const visit = (parent: SceneNode) => {
|
|
56
|
+
for (const id of parent.childIds) {
|
|
57
|
+
const child = graph.nodes.get(id)
|
|
58
|
+
if (!child) continue
|
|
59
|
+
const layerId = copiedLayerId(child, context)
|
|
60
|
+
if (layerId) copies.set(layerId, [...(copies.get(layerId) ?? []), child])
|
|
61
|
+
if (syncsInside(graph, child, context)) visit(child)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const instance = graph.nodes.get(context.instanceId)
|
|
65
|
+
if (instance) visit(instance)
|
|
66
|
+
context.copies = copies
|
|
67
|
+
return copies
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A copy of `layerId` the sync has not placed yet, which a moved layer takes along. */
|
|
71
|
+
export function unplacedCopy(
|
|
72
|
+
graph: SceneGraph,
|
|
73
|
+
context: InstanceSyncContext,
|
|
74
|
+
layerId: string,
|
|
75
|
+
instParentId: string
|
|
76
|
+
): SceneNode | undefined {
|
|
77
|
+
return copiesOf(graph, context)
|
|
78
|
+
.get(layerId)
|
|
79
|
+
?.find(
|
|
80
|
+
(copy) =>
|
|
81
|
+
!context.matched.has(copy.id) &&
|
|
82
|
+
graph.nodes.has(copy.id) &&
|
|
83
|
+
copy.id !== instParentId &&
|
|
84
|
+
!graph.isDescendant(instParentId, copy.id)
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Whether any layer in the component subtree at `layerId` already has a copy to move. */
|
|
89
|
+
export function hasUnplacedCopiesBelow(
|
|
90
|
+
graph: SceneGraph,
|
|
91
|
+
context: InstanceSyncContext,
|
|
92
|
+
layerId: string
|
|
93
|
+
): boolean {
|
|
94
|
+
const copies = copiesOf(graph, context)
|
|
95
|
+
if (copies.size === 0) return false
|
|
96
|
+
const layer = graph.nodes.get(layerId)
|
|
97
|
+
return (layer?.childIds ?? []).some(
|
|
98
|
+
(id) =>
|
|
99
|
+
copies.get(id)?.some((copy) => !context.matched.has(copy.id)) ||
|
|
100
|
+
hasUnplacedCopiesBelow(graph, context, id)
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The copies one sync of a component's instances removed, by instance and layer. */
|
|
105
|
+
export interface CopyRemovals {
|
|
106
|
+
keys: Array<[instanceId: string, layerId: string]>
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A copy the sync removed, kept so the layer coming back, as on undo, brings it back as it was. */
|
|
110
|
+
interface RemovedCopy {
|
|
111
|
+
/** The copy and everything in it, parents first. */
|
|
112
|
+
nodes: SceneNode[]
|
|
113
|
+
overrides: Map<string, Map<string, unknown>>
|
|
114
|
+
removals: CopyRemovals
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface RemovedCopyStore {
|
|
118
|
+
/** Removed copies by instance, then by the layer they copied. */
|
|
119
|
+
byInstance: Map<string, Map<string, RemovedCopy>>
|
|
120
|
+
/** Syncs that removed copies, oldest first. */
|
|
121
|
+
removals: CopyRemovals[]
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const removedCopies = new WeakMap<SceneGraph, RemovedCopyStore>()
|
|
125
|
+
|
|
126
|
+
function removedCopyStore(graph: SceneGraph): RemovedCopyStore {
|
|
127
|
+
let store = removedCopies.get(graph)
|
|
128
|
+
if (!store) {
|
|
129
|
+
store = { byInstance: new Map(), removals: [] }
|
|
130
|
+
removedCopies.set(graph, store)
|
|
131
|
+
}
|
|
132
|
+
return store
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Keeps one edit's removed copies restorable. Only as many edits as undo keeps are kept, so
|
|
137
|
+
* copies no undo can bring back are forgotten, oldest first.
|
|
138
|
+
*/
|
|
139
|
+
function keepCopyRemovals(graph: SceneGraph, removals: CopyRemovals): void {
|
|
140
|
+
if (removals.keys.length === 0) return
|
|
141
|
+
const store = removedCopyStore(graph)
|
|
142
|
+
store.removals.push(removals)
|
|
143
|
+
while (store.removals.length > DEFAULT_HISTORY_LIMIT) {
|
|
144
|
+
const oldest = store.removals.shift()
|
|
145
|
+
for (const [instanceId, layerId] of oldest?.keys ?? []) {
|
|
146
|
+
const copies = store.byInstance.get(instanceId)
|
|
147
|
+
// A later sync may have removed the same layer's copy again; that one stays.
|
|
148
|
+
if (!copies || copies.get(layerId)?.removals !== oldest) continue
|
|
149
|
+
copies.delete(layerId)
|
|
150
|
+
if (copies.size === 0) store.byInstance.delete(instanceId)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const activeRemovals = new WeakMap<SceneGraph, CopyRemovals>()
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Runs `fn` with every copy the syncs inside it remove kept as one group: one edit's syncs, however
|
|
159
|
+
* many components they reach, take one place in the history of removals.
|
|
160
|
+
*/
|
|
161
|
+
export function groupCopyRemovals<T>(graph: SceneGraph, fn: (removals: CopyRemovals) => T): T {
|
|
162
|
+
const active = activeRemovals.get(graph)
|
|
163
|
+
if (active) return fn(active)
|
|
164
|
+
const removals: CopyRemovals = { keys: [] }
|
|
165
|
+
activeRemovals.set(graph, removals)
|
|
166
|
+
try {
|
|
167
|
+
return fn(removals)
|
|
168
|
+
} finally {
|
|
169
|
+
activeRemovals.delete(graph)
|
|
170
|
+
keepCopyRemovals(graph, removals)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Brings back the copy of `layerId` the sync removed from this instance, with the same layers
|
|
176
|
+
* and overrides, as Figma restores an instance's override when the layer's deletion is undone.
|
|
177
|
+
*/
|
|
178
|
+
export function restoreRemovedCopy(
|
|
179
|
+
graph: SceneGraph,
|
|
180
|
+
context: InstanceSyncContext,
|
|
181
|
+
layerId: string,
|
|
182
|
+
instParentId: string
|
|
183
|
+
): SceneNode | undefined {
|
|
184
|
+
const store = removedCopyStore(graph)
|
|
185
|
+
const removed = store.byInstance.get(context.instanceId)
|
|
186
|
+
const entry = removed?.get(layerId)
|
|
187
|
+
if (!removed || !entry) return undefined
|
|
188
|
+
removed.delete(layerId)
|
|
189
|
+
if (removed.size === 0) store.byInstance.delete(context.instanceId)
|
|
190
|
+
const root = entry.nodes.at(0)
|
|
191
|
+
if (!root || entry.nodes.some((node) => graph.nodes.has(node.id))) return undefined
|
|
192
|
+
for (const node of entry.nodes) {
|
|
193
|
+
const { id, type, parentId, childIds: _childIds, ...props } = structuredClone(node)
|
|
194
|
+
graph.createNodeWithId(id, type, id === root.id ? instParentId : parentId, props)
|
|
195
|
+
}
|
|
196
|
+
for (const [id, fields] of entry.overrides) context.overrides.descendants.set(id, new Map(fields))
|
|
197
|
+
return graph.nodes.get(root.id)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Deletes the copies the sync left over: ones whose layer left the component, was deleted, or
|
|
202
|
+
* now has another copy. Copies the instance owns, such as slot content, are never reached.
|
|
203
|
+
*/
|
|
204
|
+
export function removeLeftoverCopies(
|
|
205
|
+
graph: SceneGraph,
|
|
206
|
+
context: InstanceSyncContext,
|
|
207
|
+
removals: CopyRemovals
|
|
208
|
+
): void {
|
|
209
|
+
const store = removedCopyStore(graph)
|
|
210
|
+
for (const id of context.leftovers) {
|
|
211
|
+
const node = graph.nodes.get(id)
|
|
212
|
+
const layerId = node ? copiedLayerId(node, context) : undefined
|
|
213
|
+
if (!node || !layerId || context.matched.has(id)) continue
|
|
214
|
+
const nodes: SceneNode[] = []
|
|
215
|
+
const collect = (current: SceneNode) => {
|
|
216
|
+
nodes.push(structuredClone(current))
|
|
217
|
+
for (const childId of current.childIds) {
|
|
218
|
+
const child = graph.nodes.get(childId)
|
|
219
|
+
if (child) collect(child)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
collect(node)
|
|
223
|
+
const overrides = new Map<string, Map<string, unknown>>()
|
|
224
|
+
for (const removed of nodes) {
|
|
225
|
+
const fields = context.overrides.descendants.get(removed.id)
|
|
226
|
+
if (fields) overrides.set(removed.id, new Map(fields))
|
|
227
|
+
context.overrides.descendants.delete(removed.id)
|
|
228
|
+
}
|
|
229
|
+
let copies = store.byInstance.get(context.instanceId)
|
|
230
|
+
if (!copies) {
|
|
231
|
+
copies = new Map()
|
|
232
|
+
store.byInstance.set(context.instanceId, copies)
|
|
233
|
+
}
|
|
234
|
+
copies.set(layerId, { nodes, overrides, removals })
|
|
235
|
+
removals.keys.push([context.instanceId, layerId])
|
|
236
|
+
graph.deleteNode(id)
|
|
237
|
+
}
|
|
238
|
+
context.leftovers.clear()
|
|
239
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { SceneGraph } from '../'
|
|
2
|
+
import { instanceMainComponent } from './main-component'
|
|
3
|
+
|
|
4
|
+
/** Components at or above `parentId`, counting the component each enclosing instance shows. */
|
|
5
|
+
function enclosingComponents(graph: SceneGraph, parentId: string): Set<string> {
|
|
6
|
+
const ancestors = new Set<string>()
|
|
7
|
+
graph.closest(parentId, (node) => {
|
|
8
|
+
if (node.type === 'COMPONENT') ancestors.add(node.id)
|
|
9
|
+
if (node.type === 'INSTANCE') {
|
|
10
|
+
const main = instanceMainComponent(graph, node)
|
|
11
|
+
if (main) ancestors.add(main.id)
|
|
12
|
+
}
|
|
13
|
+
return false
|
|
14
|
+
})
|
|
15
|
+
return ancestors
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Whether moving `nodeId` under `parentId` would make a component contain itself: its layers,
|
|
20
|
+
* followed through instances into their components, reach a component at or above the parent.
|
|
21
|
+
* Both components and instances above the parent count: an instance's layers are its
|
|
22
|
+
* component's, so a swap inside one may not bring that component back in.
|
|
23
|
+
*/
|
|
24
|
+
export function createsComponentCycle(
|
|
25
|
+
graph: SceneGraph,
|
|
26
|
+
nodeId: string,
|
|
27
|
+
parentId: string
|
|
28
|
+
): boolean {
|
|
29
|
+
const ancestors = enclosingComponents(graph, parentId)
|
|
30
|
+
if (ancestors.size === 0) return false
|
|
31
|
+
const visited = new Set<string>()
|
|
32
|
+
function reachesAncestor(id: string): boolean {
|
|
33
|
+
if (ancestors.has(id)) return true
|
|
34
|
+
if (visited.has(id)) return false
|
|
35
|
+
visited.add(id)
|
|
36
|
+
const node = graph.getNode(id)
|
|
37
|
+
if (!node) return false
|
|
38
|
+
if (node.type === 'INSTANCE' && node.componentId && reachesAncestor(node.componentId))
|
|
39
|
+
return true
|
|
40
|
+
return node.childIds.some(reachesAncestor)
|
|
41
|
+
}
|
|
42
|
+
return reachesAncestor(nodeId)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Whether placing this definition's instance under `parentId` would make a component contain itself. */
|
|
46
|
+
export function canCreateInstance(
|
|
47
|
+
graph: SceneGraph,
|
|
48
|
+
componentId: string,
|
|
49
|
+
parentId: string
|
|
50
|
+
): boolean {
|
|
51
|
+
if (graph.getNode(componentId)?.type !== 'COMPONENT') return false
|
|
52
|
+
return !createsComponentCycle(graph, componentId, parentId)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `parentId`, or its nearest ancestor that can hold these layers or instances of these
|
|
57
|
+
* components without a component containing itself. Figma pastes an instance next to its own
|
|
58
|
+
* component, not in it.
|
|
59
|
+
*/
|
|
60
|
+
export function cycleSafeParent(
|
|
61
|
+
graph: SceneGraph,
|
|
62
|
+
parentId: string,
|
|
63
|
+
nodeIds: readonly string[]
|
|
64
|
+
): string {
|
|
65
|
+
const safe = graph.closest(
|
|
66
|
+
parentId,
|
|
67
|
+
(node) =>
|
|
68
|
+
// Climbing out of a variant passes its set, which holds only variants.
|
|
69
|
+
(node.id === parentId || node.type !== 'COMPONENT_SET') &&
|
|
70
|
+
nodeIds.every((id) => !createsComponentCycle(graph, id, node.id))
|
|
71
|
+
)
|
|
72
|
+
return safe?.id ?? parentId
|
|
73
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { SceneNode } from '../'
|
|
2
|
+
|
|
3
|
+
export const INSTANCE_SYNC_TEXT_PROPS = [
|
|
4
|
+
'name',
|
|
5
|
+
'text',
|
|
6
|
+
'fontSize',
|
|
7
|
+
'fontWeight',
|
|
8
|
+
'fontFamily',
|
|
9
|
+
'italic',
|
|
10
|
+
'textDirection',
|
|
11
|
+
'letterSpacing',
|
|
12
|
+
'lineHeight',
|
|
13
|
+
'textCase',
|
|
14
|
+
'textDecoration',
|
|
15
|
+
'textDecorationStyle',
|
|
16
|
+
'textDecorationThickness',
|
|
17
|
+
'textUnderlineOffset',
|
|
18
|
+
'textDecorationFills',
|
|
19
|
+
'textDecorationSkipInk',
|
|
20
|
+
'textAlignHorizontal',
|
|
21
|
+
'textAlignVertical',
|
|
22
|
+
'textAutoResize',
|
|
23
|
+
'textTruncation',
|
|
24
|
+
'maxLines',
|
|
25
|
+
'leadingTrim',
|
|
26
|
+
'paragraphSpacing',
|
|
27
|
+
'paragraphIndent',
|
|
28
|
+
'listSpacing',
|
|
29
|
+
'hangingList',
|
|
30
|
+
'fontVariations',
|
|
31
|
+
'fontFeatures',
|
|
32
|
+
// Styled ranges belong to the characters: an instance that overrides its text keeps them.
|
|
33
|
+
'styleRuns',
|
|
34
|
+
'textParagraphs'
|
|
35
|
+
] as const
|
|
36
|
+
|
|
37
|
+
/** Fields that belong to another one: overriding the owner keeps them too. */
|
|
38
|
+
export const INSTANCE_SYNC_FIELD_OWNERS: Readonly<Partial<Record<string, string>>> = {
|
|
39
|
+
styleRuns: 'text',
|
|
40
|
+
textParagraphs: 'text'
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const INSTANCE_SYNC_PROPS: (keyof SceneNode)[] = [
|
|
44
|
+
'width',
|
|
45
|
+
'height',
|
|
46
|
+
'minWidth',
|
|
47
|
+
'maxWidth',
|
|
48
|
+
'minHeight',
|
|
49
|
+
'maxHeight',
|
|
50
|
+
'targetAspectRatio',
|
|
51
|
+
'fills',
|
|
52
|
+
'strokes',
|
|
53
|
+
'effects',
|
|
54
|
+
'opacity',
|
|
55
|
+
'cornerRadius',
|
|
56
|
+
'topLeftRadius',
|
|
57
|
+
'topRightRadius',
|
|
58
|
+
'bottomRightRadius',
|
|
59
|
+
'bottomLeftRadius',
|
|
60
|
+
'independentCorners',
|
|
61
|
+
'layoutMode',
|
|
62
|
+
'layoutDirection',
|
|
63
|
+
'layoutWrap',
|
|
64
|
+
'primaryAxisAlign',
|
|
65
|
+
'counterAxisAlign',
|
|
66
|
+
'primaryAxisSizing',
|
|
67
|
+
'counterAxisSizing',
|
|
68
|
+
'itemSpacing',
|
|
69
|
+
'counterAxisSpacing',
|
|
70
|
+
'paddingTop',
|
|
71
|
+
'paddingRight',
|
|
72
|
+
'paddingBottom',
|
|
73
|
+
'paddingLeft',
|
|
74
|
+
'gridTemplateColumns',
|
|
75
|
+
'gridTemplateRows',
|
|
76
|
+
'gridColumnGap',
|
|
77
|
+
'gridRowGap',
|
|
78
|
+
'gridPosition',
|
|
79
|
+
'clipsContent',
|
|
80
|
+
'counterAxisAlignContent',
|
|
81
|
+
'itemReverseZIndex',
|
|
82
|
+
'strokesIncludedInLayout',
|
|
83
|
+
'layoutGrids',
|
|
84
|
+
'blendMode',
|
|
85
|
+
'cornerSmoothing',
|
|
86
|
+
'strokeWeight',
|
|
87
|
+
'strokeAlign',
|
|
88
|
+
'strokeCap',
|
|
89
|
+
'strokeJoin',
|
|
90
|
+
'strokeMiterLimit',
|
|
91
|
+
'dashPattern',
|
|
92
|
+
'independentStrokeWeights',
|
|
93
|
+
'borderTopWeight',
|
|
94
|
+
'borderRightWeight',
|
|
95
|
+
'borderBottomWeight',
|
|
96
|
+
'borderLeftWeight',
|
|
97
|
+
'boundVariables',
|
|
98
|
+
'variableModes',
|
|
99
|
+
// Applied shared styles follow the component unless an instance overrides them.
|
|
100
|
+
'fillStyleId',
|
|
101
|
+
'strokeStyleId',
|
|
102
|
+
'textStyleId',
|
|
103
|
+
'effectStyleId',
|
|
104
|
+
'gridStyleId'
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
/** How a layer inside an instance sits in its parent and whether it is locked, which its overrides may change. */
|
|
108
|
+
export const INSTANCE_SYNC_CHILD_PROPS = ['layoutGrow', 'layoutAlignSelf', 'locked'] as const
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Where a component's layer sits and how it is shaped, which Figma refuses to override in an
|
|
112
|
+
* instance ("This property cannot be overridden in an instance"), so its copies always follow.
|
|
113
|
+
* A layer in an auto layout flow takes its position from the layout instead.
|
|
114
|
+
*/
|
|
115
|
+
export const INSTANCE_FOLLOWED_FIELDS = [
|
|
116
|
+
'x',
|
|
117
|
+
'y',
|
|
118
|
+
'rotation',
|
|
119
|
+
'flipX',
|
|
120
|
+
'flipY',
|
|
121
|
+
'horizontalConstraint',
|
|
122
|
+
'verticalConstraint',
|
|
123
|
+
'layoutPositioning',
|
|
124
|
+
'isMask',
|
|
125
|
+
'maskType',
|
|
126
|
+
'arcData',
|
|
127
|
+
'pointCount',
|
|
128
|
+
'starInnerRadius'
|
|
129
|
+
] as const
|
|
130
|
+
|
|
131
|
+
/** Fields an edit to a layer inside an instance records as that instance's override. */
|
|
132
|
+
export const INSTANCE_OVERRIDE_FIELDS = [
|
|
133
|
+
...INSTANCE_SYNC_PROPS,
|
|
134
|
+
...INSTANCE_SYNC_TEXT_PROPS,
|
|
135
|
+
...INSTANCE_SYNC_CHILD_PROPS,
|
|
136
|
+
'visible'
|
|
137
|
+
] as const
|
|
138
|
+
|
|
139
|
+
/** Fields a component's layer gives the copies of it in every instance. */
|
|
140
|
+
export const INSTANCE_SYNC_FIELDS = [
|
|
141
|
+
...INSTANCE_OVERRIDE_FIELDS,
|
|
142
|
+
...INSTANCE_FOLLOWED_FIELDS
|
|
143
|
+
] as const
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SceneGraph } from '../index'
|
|
2
|
+
import type { SceneNode } from '../types'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The main component an instance shows. An instance nested in another instance points at the
|
|
6
|
+
* instance it was cloned from, so follow those links to the component.
|
|
7
|
+
*/
|
|
8
|
+
export function instanceMainComponent(
|
|
9
|
+
graph: SceneGraph,
|
|
10
|
+
instance: SceneNode
|
|
11
|
+
): SceneNode | undefined {
|
|
12
|
+
const seen = new Set<string>()
|
|
13
|
+
let current = instance.componentId ? graph.getNode(instance.componentId) : undefined
|
|
14
|
+
while (current?.type === 'INSTANCE' && current.componentId && !seen.has(current.id)) {
|
|
15
|
+
seen.add(current.id)
|
|
16
|
+
current = graph.getNode(current.componentId)
|
|
17
|
+
}
|
|
18
|
+
return current?.type === 'COMPONENT' ? current : undefined
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { SceneNode } from '../'
|
|
2
|
+
import { enforcedAspectRatio } from '../layout/aspect-ratio'
|
|
3
|
+
import { layoutConstrainedChildRect } from '../resize'
|
|
4
|
+
|
|
5
|
+
/** The fields that say where a copy sits in its parent. */
|
|
6
|
+
export const PLACEMENT_FIELDS: ReadonlySet<string> = new Set(['x', 'y', 'width', 'height'])
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The component's layer placed in the copy's parent: where its constraints put it when the
|
|
10
|
+
* instance's frame has another size than the component's, as Figma resizes an instance.
|
|
11
|
+
*/
|
|
12
|
+
export function placedByConstraints(
|
|
13
|
+
source: SceneNode,
|
|
14
|
+
compParent: SceneNode,
|
|
15
|
+
instParent: SceneNode,
|
|
16
|
+
factor: number
|
|
17
|
+
): SceneNode {
|
|
18
|
+
const before = { width: compParent.width * factor, height: compParent.height * factor }
|
|
19
|
+
if (before.width === instParent.width && before.height === instParent.height) return source
|
|
20
|
+
const rect = layoutConstrainedChildRect(
|
|
21
|
+
source,
|
|
22
|
+
before,
|
|
23
|
+
instParent,
|
|
24
|
+
source.horizontalConstraint,
|
|
25
|
+
source.verticalConstraint,
|
|
26
|
+
enforcedAspectRatio(source)
|
|
27
|
+
)
|
|
28
|
+
return { ...source, ...rect }
|
|
29
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { SceneGraph } from '../'
|
|
2
|
+
import { getInstanceOverride } from '../instance-overrides'
|
|
3
|
+
import type { SceneNode } from '../types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Walk an instance's layers beside the component layers they copy, matched the way instance
|
|
7
|
+
* sync matches them. `visit` returns whether to continue into that pair's children. With
|
|
8
|
+
* `strict`, two layers claiming one source throw instead of the later one winning.
|
|
9
|
+
*/
|
|
10
|
+
export function walkInstanceSources(
|
|
11
|
+
graph: SceneGraph,
|
|
12
|
+
instance: SceneNode,
|
|
13
|
+
visit: (source: SceneNode, target: SceneNode) => boolean,
|
|
14
|
+
strict = false
|
|
15
|
+
): void {
|
|
16
|
+
if (instance.type !== 'INSTANCE' || !instance.componentId) return
|
|
17
|
+
const component = graph.getNode(instance.componentId)
|
|
18
|
+
if (!component) return
|
|
19
|
+
const walk = (sourceParent: SceneNode, instanceParent: SceneNode): void => {
|
|
20
|
+
const bySource = new Map<string, SceneNode>()
|
|
21
|
+
for (const child of graph.getChildren(instanceParent.id)) {
|
|
22
|
+
const mapped = getInstanceOverride(
|
|
23
|
+
instance.instanceOverrides,
|
|
24
|
+
instance.id,
|
|
25
|
+
child.id,
|
|
26
|
+
'sourceComponentId'
|
|
27
|
+
)
|
|
28
|
+
const sourceId = typeof mapped === 'string' ? mapped : child.componentId
|
|
29
|
+
if (!sourceId) continue
|
|
30
|
+
if (strict && bySource.has(sourceId))
|
|
31
|
+
throw new Error(`Ambiguous component-property target ${sourceId}`)
|
|
32
|
+
bySource.set(sourceId, child)
|
|
33
|
+
}
|
|
34
|
+
for (const sourceId of sourceParent.childIds) {
|
|
35
|
+
const source = graph.getNode(sourceId)
|
|
36
|
+
const target = bySource.get(sourceId)
|
|
37
|
+
if (source && target && visit(source, target)) walk(source, target)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
walk(component, instance)
|
|
41
|
+
}
|