@pairdraw/scene-graph 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +6 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +14 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +1 -0
- package/dist/copy.js.map +1 -1
- package/dist/css/gradient.d.ts +23 -0
- package/dist/css/gradient.d.ts.map +1 -0
- package/dist/css/gradient.js +404 -0
- package/dist/css/gradient.js.map +1 -0
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +2 -1
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +2 -13
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/values.js +24 -1
- package/dist/css/values.js.map +1 -1
- package/dist/font-style.d.ts +2 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +31 -6
- package/dist/font-style.js.map +1 -1
- package/dist/geometry.d.ts +10 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +21 -6
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +49 -3
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +179 -17
- package/dist/graph.js.map +1 -1
- package/dist/hit-test.d.ts +3 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +38 -23
- package/dist/hit-test.js.map +1 -1
- package/dist/icons/index.d.ts +2 -2
- package/dist/icons/index.js +2 -2
- package/dist/icons/model.d.ts +8 -2
- package/dist/icons/model.d.ts.map +1 -1
- package/dist/icons/model.js +10 -1
- package/dist/icons/model.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/assignments.js +40 -6
- package/dist/instances/assignments.js.map +1 -1
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +18 -1
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +38 -2
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +134 -15
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/overrides.d.ts +10 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +28 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/placement.js +1 -1
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap-overrides.js +94 -0
- package/dist/instances/swap-overrides.js.map +1 -0
- package/dist/instances/sync.js +90 -12
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/draw-order.d.ts +11 -0
- package/dist/layout/draw-order.d.ts.map +1 -0
- package/dist/layout/draw-order.js +14 -0
- package/dist/layout/draw-order.js.map +1 -0
- package/dist/layout/index.d.ts +2 -1
- package/dist/layout/index.js +2 -1
- package/dist/node-defaults.d.ts +11 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +16 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/order-keys.d.ts +3 -1
- package/dist/order-keys.d.ts.map +1 -1
- package/dist/order-keys.js +6 -1
- package/dist/order-keys.js.map +1 -1
- package/dist/resize.d.ts +29 -9
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +95 -62
- package/dist/resize.js.map +1 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +1 -8
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/authoring.d.ts +3 -2
- package/dist/slots/authoring.d.ts.map +1 -1
- package/dist/slots/authoring.js +5 -3
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +4 -1
- package/dist/slots/content.js.map +1 -1
- package/dist/source-metadata.d.ts +4 -2
- package/dist/source-metadata.d.ts.map +1 -1
- package/dist/source-metadata.js +10 -3
- package/dist/source-metadata.js.map +1 -1
- package/dist/strokes.d.ts +39 -0
- package/dist/strokes.d.ts.map +1 -0
- package/dist/strokes.js +104 -0
- package/dist/strokes.js.map +1 -0
- package/dist/text/direction.d.ts +6 -2
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +2 -1
- package/dist/text/direction.js.map +1 -1
- package/dist/text/index.d.ts +3 -3
- package/dist/text/index.js +3 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +22 -6
- package/dist/text/overflow.d.ts.map +1 -1
- package/dist/text/overflow.js +24 -6
- package/dist/text/overflow.js.map +1 -1
- package/dist/transfer/nodes.js +9 -5
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +18 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +36 -1
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +52 -2
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +14 -6
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +42 -67
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/formula/signatures.js +1 -1
- package/dist/variables/index.d.ts +10 -4
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +58 -15
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +101 -0
- package/dist/variables/paints.d.ts.map +1 -0
- package/dist/variables/paints.js +313 -0
- package/dist/variables/paints.js.map +1 -0
- package/dist/variables/resolution.d.ts +14 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +44 -4
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +17 -0
- package/dist/variables/subtree.js.map +1 -0
- package/package.json +6 -1
- package/src/components/properties.ts +17 -2
- package/src/copy.ts +1 -0
- package/src/css/gradient.ts +438 -0
- package/src/css/index.ts +1 -0
- package/src/css/shadows.ts +2 -18
- package/src/css/values.ts +27 -1
- package/src/font-style.ts +38 -4
- package/src/geometry.ts +25 -6
- package/src/graph.ts +207 -17
- package/src/hit-test.ts +67 -24
- package/src/icons/model.ts +11 -1
- package/src/instances/assignments.ts +65 -7
- package/src/instances/fields.ts +21 -0
- package/src/instances/index.ts +237 -14
- package/src/instances/overrides.ts +36 -1
- package/src/instances/placement.ts +5 -1
- package/src/instances/swap-overrides.ts +123 -0
- package/src/instances/sync.ts +139 -12
- package/src/layout/draw-order.ts +13 -0
- package/src/layout/index.ts +1 -0
- package/src/node-defaults.ts +21 -0
- package/src/order-keys.ts +6 -0
- package/src/resize.ts +133 -69
- package/src/shared-styles.ts +1 -3
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +4 -1
- package/src/source-metadata.ts +18 -3
- package/src/strokes.ts +121 -0
- package/src/text/direction.ts +7 -5
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +30 -7
- package/src/transfer/nodes.ts +7 -5
- package/src/types.ts +17 -0
- package/src/undo.ts +79 -2
- package/src/variables/bindings.ts +52 -74
- package/src/variables/index.ts +80 -14
- package/src/variables/paints.ts +413 -0
- package/src/variables/resolution.ts +56 -5
- package/src/variables/subtree.ts +15 -0
package/src/graph.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable max-lines -- SceneGraph is the facade over the domain modules */
|
|
2
|
-
import { omitBy } from 'es-toolkit/object'
|
|
3
|
-
import { isUndefined } from 'es-toolkit/predicate'
|
|
2
|
+
import { omitBy, pick } from 'es-toolkit/object'
|
|
3
|
+
import { isEmptyObject, isUndefined } from 'es-toolkit/predicate'
|
|
4
4
|
|
|
5
5
|
import { removeStaleBindings } from './bindings'
|
|
6
6
|
import { BufferedSceneEmitter } from './buffered-events'
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
import { cloneNodeProps, type NodeCloneMode } from './copy'
|
|
15
15
|
import { normalizeEffects } from './effects'
|
|
16
16
|
import { bindNodeEvents } from './events'
|
|
17
|
+
import { TRANSFORM_FIELDS } from './fields/geometry'
|
|
17
18
|
import { LAYOUT_AFFECTING_KEYS } from './fields/layout'
|
|
18
19
|
import { withGeneratedFrameSize } from './generated'
|
|
19
20
|
import * as HitTest from './hit-test'
|
|
@@ -24,7 +25,10 @@ import Matrix from './matrix'
|
|
|
24
25
|
import { CONTAINER_TYPES, createDefaultNode } from './node-defaults'
|
|
25
26
|
import { updateNodePreview, type NodePreviewObserver } from './preview'
|
|
26
27
|
import type { Color, Rect, Vector } from './primitives'
|
|
28
|
+
/* eslint-disable max-lines -- SceneGraph is the facade over the domain modules */
|
|
29
|
+
import { slotPropertyId } from './slots/frames'
|
|
27
30
|
import { markSourceFieldsEdited } from './source-metadata'
|
|
31
|
+
import { strokeLineChanges } from './strokes'
|
|
28
32
|
import { invalidateTextCaches } from './text/picture'
|
|
29
33
|
import type {
|
|
30
34
|
DocumentColorSpace,
|
|
@@ -45,7 +49,13 @@ import type { VariableModeFallback } from './variables'
|
|
|
45
49
|
import { normalizeVectorNetwork } from './vector-network'
|
|
46
50
|
|
|
47
51
|
/** Scopes that graph methods check while a caller's callback runs; scopes nest. */
|
|
48
|
-
type GraphScope =
|
|
52
|
+
type GraphScope =
|
|
53
|
+
| 'preview'
|
|
54
|
+
| 'sourceMetadata'
|
|
55
|
+
| 'importedState'
|
|
56
|
+
| 'layout'
|
|
57
|
+
| 'derivedLayout'
|
|
58
|
+
| 'componentDerived'
|
|
49
59
|
|
|
50
60
|
export class SceneGraph {
|
|
51
61
|
nodes = new Map<string, SceneNode>()
|
|
@@ -67,10 +77,18 @@ export class SceneGraph {
|
|
|
67
77
|
sourceMetadata: 0,
|
|
68
78
|
importedState: 0,
|
|
69
79
|
layout: 0,
|
|
70
|
-
derivedLayout: 0
|
|
80
|
+
derivedLayout: 0,
|
|
81
|
+
componentDerived: 0
|
|
71
82
|
}
|
|
72
83
|
private previewObservers: NodePreviewObserver[] = []
|
|
84
|
+
/** Edited-field marks added since the last undo step, which that step takes back when undone. */
|
|
85
|
+
private pendingSourceMarks: Array<[string, string]> = []
|
|
86
|
+
private readonly observeSourceMark = (id: string, field: string) => {
|
|
87
|
+
this.pendingSourceMarks.push([id, field])
|
|
88
|
+
}
|
|
73
89
|
private readonly changeObservers = new Set<NodeChangeObserver>()
|
|
90
|
+
/** How many waits outside the edit each recorder is paused for. */
|
|
91
|
+
private readonly outsideWaits = new Map<NodeChangeObserver, number>()
|
|
74
92
|
private importedIdGenerator: (() => string) | undefined
|
|
75
93
|
private layoutWrites: LayoutWrites | null = null
|
|
76
94
|
positionPreviewVersion = 0
|
|
@@ -119,6 +137,51 @@ export class SceneGraph {
|
|
|
119
137
|
return () => this.changeObservers.delete(observer)
|
|
120
138
|
}
|
|
121
139
|
|
|
140
|
+
/**
|
|
141
|
+
/**
|
|
142
|
+
* Runs `write`, a change someone else makes, such as a page the reader worker loaded or a
|
|
143
|
+
* collaborator's arriving, outside every edit in progress: the recorders open now do not see
|
|
144
|
+
* it, so none records it as its own. A recorder opened while the write runs is not paused.
|
|
145
|
+
*/
|
|
146
|
+
runOutsideEdits<T>(write: () => T): T {
|
|
147
|
+
const paused = [...this.changeObservers]
|
|
148
|
+
for (const observer of paused) this.changeObservers.delete(observer)
|
|
149
|
+
try {
|
|
150
|
+
return write()
|
|
151
|
+
} finally {
|
|
152
|
+
for (const observer of paused) this.changeObservers.add(observer)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Waits for `promise`, such as a font or an image download an edit needs, outside the edit in
|
|
158
|
+
* progress: what changes meanwhile, such as the user's or a collaborator's edit, is not recorded
|
|
159
|
+
* as the waiting edit's. Only for code that writes nothing itself until the wait ends.
|
|
160
|
+
*/
|
|
161
|
+
async waitOutsideEdit<T>(promise: Promise<T>): Promise<T> {
|
|
162
|
+
// A wait pauses the recorders open when it starts, and only those: a recorder opened by a
|
|
163
|
+
// later edit carries its own waits.
|
|
164
|
+
const waiting = [...this.changeObservers]
|
|
165
|
+
for (const observer of waiting) {
|
|
166
|
+
const waits = this.outsideWaits.get(observer) ?? 0
|
|
167
|
+
if (waits === 0) observer.pause?.()
|
|
168
|
+
this.outsideWaits.set(observer, waits + 1)
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
return await promise
|
|
172
|
+
} finally {
|
|
173
|
+
for (const observer of waiting) {
|
|
174
|
+
const waits = (this.outsideWaits.get(observer) ?? 1) - 1
|
|
175
|
+
if (waits > 0) {
|
|
176
|
+
this.outsideWaits.set(observer, waits)
|
|
177
|
+
continue
|
|
178
|
+
}
|
|
179
|
+
this.outsideWaits.delete(observer)
|
|
180
|
+
if (this.changeObservers.has(observer)) observer.resume?.()
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
122
185
|
/** Tells `observeNodeChanges` observers that `node` is about to change. */
|
|
123
186
|
willChangeNode(node: SceneNode | undefined): void {
|
|
124
187
|
if (!node) return
|
|
@@ -429,6 +492,7 @@ export class SceneGraph {
|
|
|
429
492
|
const parent = this.nodes.get(parentId)
|
|
430
493
|
if (!parent) throw new Error(`Parent "${parentId}" does not exist`)
|
|
431
494
|
const node = createDefaultNode(() => this.generateEntityId(), type, overrides)
|
|
495
|
+
Object.assign(node, strokeLineChanges(node, overrides))
|
|
432
496
|
this.willChangeNode(parent)
|
|
433
497
|
parent.childIds.push(node.id)
|
|
434
498
|
return this.registerNode(node, parentId)
|
|
@@ -479,6 +543,28 @@ export class SceneGraph {
|
|
|
479
543
|
return this.scopeDepths.sourceMetadata > 0
|
|
480
544
|
}
|
|
481
545
|
|
|
546
|
+
/** The edited-field marks added since the last call, which that undo step takes back. */
|
|
547
|
+
takeSourceMarks(): Array<[string, string]> {
|
|
548
|
+
const marks = this.pendingSourceMarks
|
|
549
|
+
this.pendingSourceMarks = []
|
|
550
|
+
return marks
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** Marks fields as edited again, for redo of a step that marked them. */
|
|
554
|
+
addSourceMarks(id: string, fields: readonly string[]): void {
|
|
555
|
+
const node = this.nodes.get(id)
|
|
556
|
+
if (node) markSourceFieldsEdited(node, [...fields])
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** Takes back the marks a step added, for undo of that step. */
|
|
560
|
+
removeSourceMarks(id: string, fields: readonly string[]): void {
|
|
561
|
+
const node = this.nodes.get(id)
|
|
562
|
+
if (!node) return
|
|
563
|
+
const removed = new Set(fields)
|
|
564
|
+
const kept = node.source.editedFields.filter((field) => !removed.has(field))
|
|
565
|
+
if (kept.length !== node.source.editedFields.length) node.source.editedFields = kept
|
|
566
|
+
}
|
|
567
|
+
|
|
482
568
|
applyImportedStateDuring<T>(fn: () => T, idGenerator?: () => string): T {
|
|
483
569
|
const previousGenerator = this.importedIdGenerator
|
|
484
570
|
this.importedIdGenerator = idGenerator ?? previousGenerator
|
|
@@ -501,6 +587,19 @@ export class SceneGraph {
|
|
|
501
587
|
this.withScope('derivedLayout', fn)
|
|
502
588
|
}
|
|
503
589
|
|
|
590
|
+
/**
|
|
591
|
+
* Runs `fn` as values a copy takes from its component, such as the bound paints of a layer an
|
|
592
|
+
* instance copies, refreshed for it. Source metadata is preserved, and a save leaves them to the
|
|
593
|
+
* component rather than writing them as edits of the copy.
|
|
594
|
+
*/
|
|
595
|
+
applyComponentDerivedDuring(fn: () => void): void {
|
|
596
|
+
this.withScope('componentDerived', () => this.preserveSourceMetadataDuring(fn))
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
get isApplyingComponentDerived(): boolean {
|
|
600
|
+
return this.scopeDepths.componentDerived > 0
|
|
601
|
+
}
|
|
602
|
+
|
|
504
603
|
get isApplyingDerivedLayout(): boolean {
|
|
505
604
|
return this.scopeDepths.derivedLayout > 0
|
|
506
605
|
}
|
|
@@ -530,7 +629,7 @@ export class SceneGraph {
|
|
|
530
629
|
this.layoutWrites = null
|
|
531
630
|
if (!writes) return
|
|
532
631
|
for (const [node, changes, edited] of writes.updates((nodeId) => this.nodes.get(nodeId))) {
|
|
533
|
-
markSourceFieldsEdited(node, edited)
|
|
632
|
+
markSourceFieldsEdited(node, edited, this.observeSourceMark)
|
|
534
633
|
this.emitter.emit('node:updated', node.id, changes)
|
|
535
634
|
}
|
|
536
635
|
}
|
|
@@ -542,7 +641,7 @@ export class SceneGraph {
|
|
|
542
641
|
}
|
|
543
642
|
updateNodePreview(id: string, changes: Partial<SceneNode>): void {
|
|
544
643
|
const node = this.nodes.get(id)
|
|
545
|
-
if (node) changes = this.
|
|
644
|
+
if (node) changes = this.impliedChanges(node, changes)
|
|
546
645
|
const appliedChanges = updateNodePreview(this, id, changes, (node, applied) => {
|
|
547
646
|
for (const observe of this.previewObservers) observe(node, applied)
|
|
548
647
|
})
|
|
@@ -561,12 +660,17 @@ export class SceneGraph {
|
|
|
561
660
|
|
|
562
661
|
const node = this.nodes.get(id)
|
|
563
662
|
if (!node) return
|
|
564
|
-
this.applyNodeChanges(node, this.
|
|
663
|
+
this.applyNodeChanges(node, this.impliedChanges(node, omitBy(changes, isUndefined)))
|
|
565
664
|
}
|
|
566
665
|
|
|
567
|
-
/**
|
|
568
|
-
|
|
569
|
-
|
|
666
|
+
/**
|
|
667
|
+
* `changes` with what they also set: a size from outside layout on a generated frame, and
|
|
668
|
+
* strokes and the layer's line fields kept in agreement (`strokeLineChanges`).
|
|
669
|
+
*/
|
|
670
|
+
private impliedChanges(node: SceneNode, changes: Partial<SceneNode>): Partial<SceneNode> {
|
|
671
|
+
const sized = this.isApplyingLayout ? changes : withGeneratedFrameSize(node, changes)
|
|
672
|
+
const lines = strokeLineChanges(node, sized)
|
|
673
|
+
return isEmptyObject(lines) ? sized : { ...sized, ...lines }
|
|
570
674
|
}
|
|
571
675
|
|
|
572
676
|
/** Replay captured properties without dropping explicit undefined values or absent keys. */
|
|
@@ -599,7 +703,7 @@ export class SceneGraph {
|
|
|
599
703
|
this.reindexInstance(node, changes)
|
|
600
704
|
if (node.type === 'TEXT') invalidateTextCaches(node, changes)
|
|
601
705
|
if (this.scopeDepths.sourceMetadata === 0 && !this.isApplyingLayout) {
|
|
602
|
-
markSourceFieldsEdited(node, Object.keys(changes))
|
|
706
|
+
markSourceFieldsEdited(node, Object.keys(changes), this.observeSourceMark)
|
|
603
707
|
}
|
|
604
708
|
if (changes.vectorNetwork) {
|
|
605
709
|
changes = { ...changes, vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork) }
|
|
@@ -615,18 +719,57 @@ export class SceneGraph {
|
|
|
615
719
|
Object.assign(node, changes)
|
|
616
720
|
if (changes.fills) removeStaleBindings(node, 'fills', changes)
|
|
617
721
|
if (changes.strokes) removeStaleBindings(node, 'strokes', changes)
|
|
722
|
+
// A bound paint written keeps what it was given until its variable's color changes.
|
|
723
|
+
if ((changes.fills || changes.strokes) && !this.isApplyingImportedState)
|
|
724
|
+
Variables.recordWrittenBoundPaints(this, node, changes)
|
|
618
725
|
for (const key of absent) Reflect.deleteProperty(node, key)
|
|
619
726
|
this.publishNodeUpdate(id, changes)
|
|
620
727
|
}
|
|
621
728
|
|
|
729
|
+
/** Marks fields an edit changed, unless metadata is being restored or layout is writing them. */
|
|
730
|
+
private markEdited(node: SceneNode, keys: string[]): void {
|
|
731
|
+
if (this.scopeDepths.sourceMetadata === 0 && !this.isApplyingLayout) {
|
|
732
|
+
markSourceFieldsEdited(node, keys, this.observeSourceMark)
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
622
736
|
/** Publishes an update now, or leaves it to the running layout, which publishes once it ends. */
|
|
623
737
|
private publishNodeUpdate(id: string, changes: Partial<SceneNode>): void {
|
|
624
738
|
if (!this.layoutWrites) this.emitter.emit('node:updated', id, changes)
|
|
625
739
|
}
|
|
626
740
|
|
|
741
|
+
/** Whether moving `node` under `parentId` would put an instance inside the component it draws. */
|
|
742
|
+
private movesIntoInstanceOutsideSlot(parentId: string): boolean {
|
|
743
|
+
let current = this.nodes.get(parentId)
|
|
744
|
+
while (current) {
|
|
745
|
+
if (slotPropertyId(current)) return false
|
|
746
|
+
if (current.type === 'INSTANCE') return true
|
|
747
|
+
current = current.parentId ? this.nodes.get(current.parentId) : undefined
|
|
748
|
+
}
|
|
749
|
+
return false
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
private movesInstanceIntoItsComponent(node: SceneNode, parentId: string): boolean {
|
|
753
|
+
const stack = [node]
|
|
754
|
+
while (stack.length > 0) {
|
|
755
|
+
const current = stack.pop()
|
|
756
|
+
if (!current) continue
|
|
757
|
+
const componentId = current.componentId
|
|
758
|
+
if (componentId && this.closest(parentId, (candidate) => candidate.id === componentId))
|
|
759
|
+
return true
|
|
760
|
+
for (const childId of current.childIds) {
|
|
761
|
+
const child = this.nodes.get(childId)
|
|
762
|
+
if (child) stack.push(child)
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
return false
|
|
766
|
+
}
|
|
767
|
+
|
|
627
768
|
reparentNode(nodeId: string, newParentId: string): void {
|
|
628
769
|
const node = this.nodes.get(nodeId)
|
|
629
770
|
if (!node || nodeId === this.rootId) return
|
|
771
|
+
// A page is never a layer: it holds the document's layers and cannot move inside one.
|
|
772
|
+
if (node.type === 'CANVAS') return
|
|
630
773
|
if (this.isDescendant(newParentId, nodeId)) return
|
|
631
774
|
|
|
632
775
|
const oldParent = node.parentId ? this.nodes.get(node.parentId) : undefined
|
|
@@ -634,6 +777,13 @@ export class SceneGraph {
|
|
|
634
777
|
if (!newParent) return
|
|
635
778
|
if (node.parentId === newParentId) return
|
|
636
779
|
|
|
780
|
+
// An instance inside the component it draws would sync itself forever, so Figma keeps it
|
|
781
|
+
// out: the move is refused, as a move into the layer's own subtree is.
|
|
782
|
+
if (this.movesInstanceIntoItsComponent(node, newParentId)) return
|
|
783
|
+
// An instance's contents are its component's copies; only a slot takes the document's own
|
|
784
|
+
// layers, so a layer moved anywhere else inside one would have nothing to correspond to.
|
|
785
|
+
if (this.movesIntoInstanceOutsideSlot(newParentId)) return
|
|
786
|
+
|
|
637
787
|
const oldParentId = node.parentId
|
|
638
788
|
this.willChangeNode(node)
|
|
639
789
|
this.willChangeNode(oldParent)
|
|
@@ -650,6 +800,7 @@ export class SceneGraph {
|
|
|
650
800
|
node.parentId = newParentId
|
|
651
801
|
newParent.childIds.push(nodeId)
|
|
652
802
|
|
|
803
|
+
const before = pick(node, TRANSFORM_FIELDS)
|
|
653
804
|
if (isTranslationOnly(oldParentWorld) && isTranslationOnly(newParentWorld)) {
|
|
654
805
|
node.x += oldParentWorld[2] - newParentWorld[2]
|
|
655
806
|
node.y += oldParentWorld[5] - newParentWorld[5]
|
|
@@ -658,8 +809,15 @@ export class SceneGraph {
|
|
|
658
809
|
const local = localTransformFromWorld(node, world, newParentWorld)
|
|
659
810
|
if (local) Object.assign(node, local)
|
|
660
811
|
}
|
|
812
|
+
// The node keeps its place on the canvas by new coordinates, which a save must write.
|
|
813
|
+
this.markEdited(
|
|
814
|
+
node,
|
|
815
|
+
TRANSFORM_FIELDS.filter((key) => node[key] !== before[key])
|
|
816
|
+
)
|
|
661
817
|
|
|
662
818
|
this.emitter.emit('node:reparented', nodeId, oldParentId, newParentId)
|
|
819
|
+
// Bound paints show their variables in the modes the layer now sits in.
|
|
820
|
+
Variables.refreshMovedBoundPaints(this, nodeId, oldParentId)
|
|
663
821
|
}
|
|
664
822
|
|
|
665
823
|
/**
|
|
@@ -685,6 +843,8 @@ export class SceneGraph {
|
|
|
685
843
|
node.parentId = parentId
|
|
686
844
|
this.absPosCache.clear()
|
|
687
845
|
this.emitter.emit('node:reordered', childId, parentId, at, previousParentId)
|
|
846
|
+
if (previousParentId !== parentId)
|
|
847
|
+
Variables.refreshMovedBoundPaints(this, childId, previousParentId)
|
|
688
848
|
}
|
|
689
849
|
|
|
690
850
|
deleteNode(id: string): void {
|
|
@@ -705,6 +865,12 @@ export class SceneGraph {
|
|
|
705
865
|
}
|
|
706
866
|
|
|
707
867
|
if (node.type === 'INSTANCE' && node.componentId) this.unindexInstance(node.componentId, id)
|
|
868
|
+
// An override names a layer of its instance: one left naming a deleted layer would be written
|
|
869
|
+
// as a claim for nothing, and reopening would restore a layer the document no longer has.
|
|
870
|
+
for (let owner = node.parentId ? this.nodes.get(node.parentId) : undefined; owner;) {
|
|
871
|
+
if (owner.type === 'INSTANCE') owner.instanceOverrides.descendants.delete(id)
|
|
872
|
+
owner = owner.parentId ? this.nodes.get(owner.parentId) : undefined
|
|
873
|
+
}
|
|
708
874
|
this.connectorIds.delete(id)
|
|
709
875
|
this.nodes.delete(id)
|
|
710
876
|
this.emitter.emit('node:deleted', id, node.parentId)
|
|
@@ -719,14 +885,18 @@ export class SceneGraph {
|
|
|
719
885
|
return HitTest.hitTestDeep(this, px, py, scopeId, pad)
|
|
720
886
|
}
|
|
721
887
|
|
|
888
|
+
/**
|
|
889
|
+
* The layer a press selects at the point; `locked` hits locked layers too, as a right-click does.
|
|
890
|
+
*/
|
|
722
891
|
hitTestSelectable(
|
|
723
892
|
px: number,
|
|
724
893
|
py: number,
|
|
725
894
|
scopeId: string,
|
|
726
895
|
selectedIds: ReadonlySet<string>,
|
|
727
|
-
pad = 0
|
|
896
|
+
pad = 0,
|
|
897
|
+
locked = false
|
|
728
898
|
): SceneNode | null {
|
|
729
|
-
return HitTest.hitTestSelectable(this, px, py, scopeId, selectedIds, pad)
|
|
899
|
+
return HitTest.hitTestSelectable(this, px, py, scopeId, selectedIds, pad, locked)
|
|
730
900
|
}
|
|
731
901
|
|
|
732
902
|
hitTestOpenContainer(px: number, py: number, scopeId: string): SceneNode | null {
|
|
@@ -760,6 +930,8 @@ export class SceneGraph {
|
|
|
760
930
|
const clone = this.cloneSubtree(sourceId, parentId, overrides, copies)
|
|
761
931
|
// A copied instance records the overrides of its own layers, which are the cloned ones.
|
|
762
932
|
Instances.retargetCopiedOverrides(this, copies)
|
|
933
|
+
// A copy placed in other modes than its source shows its bound paints there, as a move does.
|
|
934
|
+
if (clone) Variables.refreshMovedBoundPaints(this, clone.id, this.nodes.get(sourceId)?.parentId)
|
|
763
935
|
return clone
|
|
764
936
|
}
|
|
765
937
|
|
|
@@ -778,6 +950,7 @@ export class SceneGraph {
|
|
|
778
950
|
// A copy is a new design nobody has reviewed: Figma's duplicate and clone() drop the status.
|
|
779
951
|
props.status = null
|
|
780
952
|
const clone = this.createNode(src.type, parentId, { ...props, ...overrides })
|
|
953
|
+
Variables.copyAppliedBoundPaints(src, clone)
|
|
781
954
|
copies.set(src.id, clone.id)
|
|
782
955
|
if (src.status) markSourceFieldsEdited(clone, ['status'])
|
|
783
956
|
|
|
@@ -836,16 +1009,33 @@ export class SceneGraph {
|
|
|
836
1009
|
return Instances.getInstances(this, componentId)
|
|
837
1010
|
}
|
|
838
1011
|
|
|
1012
|
+
/**
|
|
1013
|
+
* The descendants of a layer in depth-first order with their depths. Each layer with children is
|
|
1014
|
+
* walked once, so a cycle or repeated links in bad data cannot hang it or repeat its subtree.
|
|
1015
|
+
*/
|
|
839
1016
|
flattenTree(parentId?: string, depth = 0): Array<{ node: SceneNode; depth: number }> {
|
|
840
1017
|
const id = parentId ?? this.rootId
|
|
841
1018
|
const parent = this.nodes.get(id)
|
|
842
1019
|
if (!parent) return []
|
|
843
1020
|
const result: Array<{ node: SceneNode; depth: number }> = []
|
|
844
|
-
|
|
845
|
-
|
|
1021
|
+
const pending = [{ node: parent, depth, index: 0 }]
|
|
1022
|
+
const walked = new Set([parent])
|
|
1023
|
+
while (pending.length > 0) {
|
|
1024
|
+
const frame = pending.at(-1)
|
|
1025
|
+
if (!frame) break
|
|
1026
|
+
if (frame.index >= frame.node.childIds.length) {
|
|
1027
|
+
pending.pop()
|
|
1028
|
+
continue
|
|
1029
|
+
}
|
|
1030
|
+
const child = this.nodes.get(frame.node.childIds[frame.index++])
|
|
846
1031
|
if (!child) continue
|
|
847
|
-
|
|
848
|
-
if (
|
|
1032
|
+
const container = child.childIds.length > 0
|
|
1033
|
+
if (container && walked.has(child)) continue
|
|
1034
|
+
result.push({ node: child, depth: frame.depth })
|
|
1035
|
+
if (container) {
|
|
1036
|
+
walked.add(child)
|
|
1037
|
+
pending.push({ node: child, depth: frame.depth + 1, index: 0 })
|
|
1038
|
+
}
|
|
849
1039
|
}
|
|
850
1040
|
return result
|
|
851
1041
|
}
|
package/src/hit-test.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { hitsConnectorLine } from './connectors/hit'
|
|
|
2
2
|
import { getAxisAlignedBoundsAt, getNodeLocalMatrix, getWorldMatrix } from './coordinate'
|
|
3
3
|
import { geometryBlobPolylines } from './geometry'
|
|
4
4
|
import type { SceneGraph } from './graph'
|
|
5
|
+
import { drawnChildIds } from './layout/draw-order'
|
|
5
6
|
import Matrix, { type Mat3 } from './matrix'
|
|
6
7
|
import type { Rect, Vector } from './primitives'
|
|
7
8
|
import type { SceneNode, NodeType } from './types'
|
|
@@ -195,14 +196,23 @@ function hitTestOpaqueContainer(
|
|
|
195
196
|
childId: string,
|
|
196
197
|
deep: boolean,
|
|
197
198
|
transformCache: Map<string, boolean>,
|
|
198
|
-
pad: number
|
|
199
|
+
pad: number,
|
|
200
|
+
locked = false
|
|
199
201
|
): SceneNode | null {
|
|
200
202
|
if (!containsPoint(px, py, child, graph, transformCache)) return null
|
|
201
|
-
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad)
|
|
203
|
+
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad, locked)
|
|
202
204
|
if (childHit) return child
|
|
203
205
|
if (hasVisibleFillOrStroke(child)) return child
|
|
204
206
|
return null
|
|
205
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* What a hit inside a container reports: the container itself when it is locked (a locked layer,
|
|
210
|
+
* where one is hit at all, stands in for everything inside it) or the walk stops at containers.
|
|
211
|
+
*/
|
|
212
|
+
function standIn(container: SceneNode, hit: SceneNode, deep: boolean): SceneNode {
|
|
213
|
+
return container.locked || !deep ? container : hit
|
|
214
|
+
}
|
|
215
|
+
|
|
206
216
|
function hitTestTransparentContainer(
|
|
207
217
|
graph: SceneGraph,
|
|
208
218
|
px: number,
|
|
@@ -211,21 +221,19 @@ function hitTestTransparentContainer(
|
|
|
211
221
|
childId: string,
|
|
212
222
|
deep: boolean,
|
|
213
223
|
transformCache: Map<string, boolean>,
|
|
214
|
-
pad: number
|
|
224
|
+
pad: number,
|
|
225
|
+
locked = false
|
|
215
226
|
): SceneNode | null {
|
|
216
227
|
if (child.type === 'GROUP') {
|
|
217
228
|
// A group fits its layers, so a padded click on one at its edge falls just outside it.
|
|
218
229
|
if (!containsPoint(px, py, child, graph, transformCache, pad)) return null
|
|
219
|
-
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad)
|
|
220
|
-
if (childHit) return
|
|
230
|
+
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad, locked)
|
|
231
|
+
if (childHit) return standIn(child, childHit, deep)
|
|
221
232
|
return containsPoint(px, py, child, graph, transformCache) ? child : null
|
|
222
233
|
}
|
|
223
234
|
|
|
224
|
-
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad)
|
|
225
|
-
if (childHit)
|
|
226
|
-
if (child.locked) return child
|
|
227
|
-
return childHit
|
|
228
|
-
}
|
|
235
|
+
const childHit = hitTestChildren(graph, px, py, childId, deep, transformCache, pad, locked)
|
|
236
|
+
if (childHit) return standIn(child, childHit, true)
|
|
229
237
|
|
|
230
238
|
if (
|
|
231
239
|
containsPoint(px, py, child, graph, transformCache) &&
|
|
@@ -254,6 +262,24 @@ function isMaskedOut(
|
|
|
254
262
|
* The topmost layer under the point among a container's children. `pad`, in canvas units, grows
|
|
255
263
|
* each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.
|
|
256
264
|
*/
|
|
265
|
+
/**
|
|
266
|
+
* Whether a layer under the point can take the click. A locked layer and everything in it are
|
|
267
|
+
* invisible to a press and to hover, as in Figma: a click falls through to what lies under it, and
|
|
268
|
+
* a drag from it draws a marquee.
|
|
269
|
+
*/
|
|
270
|
+
function takesClick(
|
|
271
|
+
graph: SceneGraph,
|
|
272
|
+
px: number,
|
|
273
|
+
py: number,
|
|
274
|
+
child: SceneNode | undefined,
|
|
275
|
+
mask: readonly SceneNode[] | undefined,
|
|
276
|
+
locked: boolean,
|
|
277
|
+
transformCache: Map<string, boolean>
|
|
278
|
+
): child is SceneNode {
|
|
279
|
+
if (!child || child.internalOnly || !child.visible || (child.locked && !locked)) return false
|
|
280
|
+
return !isMaskedOut(px, py, mask, graph, transformCache)
|
|
281
|
+
}
|
|
282
|
+
|
|
257
283
|
function hitTestChildren(
|
|
258
284
|
graph: SceneGraph,
|
|
259
285
|
px: number,
|
|
@@ -261,7 +287,8 @@ function hitTestChildren(
|
|
|
261
287
|
parentId: string,
|
|
262
288
|
deep = false,
|
|
263
289
|
transformCache = new Map<string, boolean>(),
|
|
264
|
-
pad = 0
|
|
290
|
+
pad = 0,
|
|
291
|
+
locked = false
|
|
265
292
|
): SceneNode | null {
|
|
266
293
|
const parent = graph.nodes.get(parentId)
|
|
267
294
|
if (!parent) return null
|
|
@@ -270,15 +297,26 @@ function hitTestChildren(
|
|
|
270
297
|
if (!containsPoint(px, py, parent, graph, transformCache)) return null
|
|
271
298
|
}
|
|
272
299
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
300
|
+
// Front to back, as the layers are drawn: a reversed auto layout puts the first child on top.
|
|
301
|
+
const drawn = drawnChildIds(parent)
|
|
302
|
+
const masks = clippingMasks(graph, drawn)
|
|
303
|
+
for (let i = drawn.length - 1; i >= 0; i--) {
|
|
304
|
+
const childId = drawn[i]
|
|
276
305
|
const child = graph.nodes.get(childId)
|
|
277
|
-
if (!
|
|
278
|
-
if (isMaskedOut(px, py, masks[i], graph, transformCache)) continue
|
|
306
|
+
if (!takesClick(graph, px, py, child, masks[i], locked, transformCache)) continue
|
|
279
307
|
if (CONTAINER_TYPES.has(child.type)) {
|
|
280
308
|
if (OPAQUE_CONTAINER_TYPES.has(child.type) && !deep) {
|
|
281
|
-
const hit = hitTestOpaqueContainer(
|
|
309
|
+
const hit = hitTestOpaqueContainer(
|
|
310
|
+
graph,
|
|
311
|
+
px,
|
|
312
|
+
py,
|
|
313
|
+
child,
|
|
314
|
+
childId,
|
|
315
|
+
deep,
|
|
316
|
+
transformCache,
|
|
317
|
+
pad,
|
|
318
|
+
locked
|
|
319
|
+
)
|
|
282
320
|
if (hit) return hit
|
|
283
321
|
continue
|
|
284
322
|
}
|
|
@@ -291,7 +329,8 @@ function hitTestChildren(
|
|
|
291
329
|
childId,
|
|
292
330
|
deep,
|
|
293
331
|
transformCache,
|
|
294
|
-
pad
|
|
332
|
+
pad,
|
|
333
|
+
locked
|
|
295
334
|
)
|
|
296
335
|
if (hit) return hit
|
|
297
336
|
continue
|
|
@@ -348,12 +387,14 @@ function frameChildAt(
|
|
|
348
387
|
graph: SceneGraph,
|
|
349
388
|
container: SceneNode,
|
|
350
389
|
px: number,
|
|
351
|
-
py: number
|
|
390
|
+
py: number,
|
|
391
|
+
locked = false
|
|
352
392
|
): SceneNode | null {
|
|
353
393
|
const transformCache = new Map<string, boolean>()
|
|
354
394
|
for (let i = container.childIds.length - 1; i >= 0; i--) {
|
|
355
395
|
const child = graph.nodes.get(container.childIds[i])
|
|
356
396
|
if (!child || child.internalOnly || !child.visible || child.type !== 'FRAME') continue
|
|
397
|
+
if (child.locked && !locked) continue
|
|
357
398
|
// A board, such as a top-level frame in a section, keeps its empty area as background.
|
|
358
399
|
if (opensByItself(graph, child) && !ownsItsEmptyArea(graph, child)) continue
|
|
359
400
|
if (containsPoint(px, py, child, graph, transformCache)) return child
|
|
@@ -404,12 +445,14 @@ export function hitTestSelectable(
|
|
|
404
445
|
py: number,
|
|
405
446
|
scopeId: string,
|
|
406
447
|
selectedIds: ReadonlySet<string>,
|
|
407
|
-
pad = 0
|
|
448
|
+
pad = 0,
|
|
449
|
+
/** Whether locked layers are hit, as a right-click hits them; see `hitTestAtPoint`. */
|
|
450
|
+
locked = false
|
|
408
451
|
): SceneNode | null {
|
|
409
|
-
const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), pad)
|
|
452
|
+
const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), pad, locked)
|
|
410
453
|
if (!deepest) {
|
|
411
454
|
const scope = graph.nodes.get(scopeId)
|
|
412
|
-
return scope ? frameChildAt(graph, scope, px, py) : null
|
|
455
|
+
return scope ? frameChildAt(graph, scope, px, py, locked) : null
|
|
413
456
|
}
|
|
414
457
|
|
|
415
458
|
const chain: SceneNode[] = []
|
|
@@ -421,13 +464,13 @@ export function hitTestSelectable(
|
|
|
421
464
|
const opened = openedBySelection(graph, selectedIds, scopeId)
|
|
422
465
|
|
|
423
466
|
for (const node of chain) {
|
|
424
|
-
// A locked layer stands in for everything inside it.
|
|
467
|
+
// A locked layer, hit only with `locked`, stands in for everything inside it.
|
|
425
468
|
if (node.locked) return node
|
|
426
469
|
if (!opened.has(node.id) && !opensByItself(graph, node)) return node
|
|
427
470
|
}
|
|
428
471
|
const last = chain.at(-1)
|
|
429
472
|
if (!last) return null
|
|
430
|
-
const frame = frameChildAt(graph, last, px, py)
|
|
473
|
+
const frame = frameChildAt(graph, last, px, py, locked)
|
|
431
474
|
if (frame) return frame
|
|
432
475
|
if (ownsItsEmptyArea(graph, last)) return last
|
|
433
476
|
return opened.has(last.id) && !opensByItself(graph, last) ? last : null
|
package/src/icons/model.ts
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
|
+
import { CONTAINER_TYPES } from '../node-defaults'
|
|
1
2
|
import { readPluginData, withPluginData } from '../plugin-data/field'
|
|
2
3
|
import { PAIRDRAW_PLUGIN_DATA } from '../plugin-data/fields'
|
|
3
4
|
import type { SceneNode } from '../types'
|
|
4
5
|
import type { Icon, IconPaint } from './schema'
|
|
5
6
|
|
|
6
7
|
/** The icon a frame draws, or null when it is not an icon or its data is unreadable. */
|
|
7
|
-
export function readIcon(node: SceneNode): Icon | null {
|
|
8
|
+
export function readIcon(node: Pick<SceneNode, 'pluginData'>): Icon | null {
|
|
8
9
|
return readPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.icon) ?? null
|
|
9
10
|
}
|
|
10
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Whether a layer holds other layers. An icon frame does not: it draws one glyph at the
|
|
14
|
+
* proportions it was placed with, so a layer put inside would cover it, and a top-level icon
|
|
15
|
+
* would stand where a slide does.
|
|
16
|
+
*/
|
|
17
|
+
export function acceptsContent(node: Pick<SceneNode, 'type' | 'pluginData'>): boolean {
|
|
18
|
+
return node.type !== 'CANVAS' && CONTAINER_TYPES.has(node.type) && readIcon(node) === null
|
|
19
|
+
}
|
|
20
|
+
|
|
11
21
|
/** The node's plugin data with the icon set, or removed when null. */
|
|
12
22
|
export function withIcon(node: SceneNode, icon: Icon | null): SceneNode['pluginData'] {
|
|
13
23
|
return withPluginData(node.pluginData, PAIRDRAW_PLUGIN_DATA.icon, icon ?? undefined)
|