@open-pencil/scene-graph 0.14.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/arrow-caps.d.ts +44 -0
- package/dist/arrow-caps.d.ts.map +1 -0
- package/dist/arrow-caps.js +131 -0
- package/dist/arrow-caps.js.map +1 -0
- package/dist/chunks/checkpoint.js +78 -0
- package/dist/chunks/checkpoint.js.map +1 -0
- package/dist/chunks/components/properties.js +1571 -0
- package/dist/chunks/components/properties.js.map +1 -0
- package/dist/chunks/coordinate.js +87 -33
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/font-style.js +2 -1
- package/dist/chunks/font-style.js.map +1 -1
- package/dist/chunks/index.js +1283 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/constants.js +1 -18
- package/dist/constants2.d.ts +0 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +107 -11
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +2 -2
- package/dist/coordinate2.d.ts +2 -2
- package/dist/copy.d.ts +2 -2
- package/dist/copy.js +2 -2
- package/dist/geometry.d.ts +2 -2
- package/dist/geometry.js +2 -431
- package/dist/guides.d.ts +2 -0
- package/dist/guides.js +0 -0
- package/dist/guides2.d.ts +11 -0
- package/dist/guides2.d.ts.map +1 -0
- package/dist/hit-test.d.ts +1 -2
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +26 -14
- package/dist/hit-test.js.map +1 -1
- package/dist/images.js +0 -1
- package/dist/images.js.map +1 -1
- package/dist/images2.d.ts.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -6
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout-guides.d.ts +2 -0
- package/dist/layout-guides.js +2 -0
- package/dist/layout-guides2.d.ts +16 -0
- package/dist/layout-guides2.d.ts.map +1 -0
- package/dist/matrix2.d.ts +0 -1
- package/dist/matrix2.d.ts.map +1 -1
- package/dist/node-defaults.d.ts +1 -2
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/parse-path.d.ts +1 -2
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js.map +1 -1
- package/dist/preview.d.ts +2 -12
- package/dist/preview.js +1 -1
- package/dist/preview2.d.ts +12 -0
- package/dist/preview2.d.ts.map +1 -0
- package/dist/primitives2.d.ts.map +1 -1
- package/dist/resize.d.ts +18 -4
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +70 -22
- package/dist/resize.js.map +1 -1
- package/dist/snap.js +1 -1
- package/dist/source-metadata.d.ts +1 -2
- package/dist/source-metadata.d.ts.map +1 -1
- package/dist/source-metadata.js +1 -1
- package/dist/text-picture.d.ts +13 -2
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text-picture.js +2 -2
- package/dist/types.d.ts +499 -2
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1296
- package/dist/types2.d.ts +2 -430
- package/dist/undo.js +1 -1
- package/dist/undo2.d.ts +5 -0
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +1 -2
- package/dist/variables.d.ts.map +1 -1
- package/dist/variables.js +1 -1
- package/dist/variant-name.d.ts.map +1 -1
- package/dist/variant-properties.d.ts +10 -0
- package/dist/variant-properties.d.ts.map +1 -0
- package/dist/variant-properties.js +39 -0
- package/dist/variant-properties.js.map +1 -0
- package/dist/vector-network.js +1 -1
- package/dist/vector-network2.d.ts +1 -2
- package/dist/vector-network2.d.ts.map +1 -1
- package/package.json +40 -15
- package/src/arrow-caps.ts +146 -0
- package/src/bindings.ts +20 -0
- package/src/checkpoint.ts +123 -0
- package/src/components/properties.ts +238 -0
- package/src/constants.ts +6 -0
- package/src/coordinate.ts +226 -0
- package/src/copy.ts +294 -0
- package/src/events.ts +21 -0
- package/src/export-scale.ts +16 -0
- package/src/font-style.ts +77 -0
- package/src/geometry.ts +530 -0
- package/src/guides.ts +8 -0
- package/src/hit-test.ts +205 -0
- package/src/images.ts +34 -0
- package/src/index.ts +686 -0
- package/src/instance-overrides.test.ts +67 -0
- package/src/instance-overrides.ts +152 -0
- package/src/instances.ts +480 -0
- package/src/layout-guides.ts +103 -0
- package/src/matrix.ts +116 -0
- package/src/mutation-impact.ts +70 -0
- package/src/node-defaults.ts +186 -0
- package/src/parse-path.ts +191 -0
- package/src/preview.ts +80 -0
- package/src/primitives.ts +42 -0
- package/src/resize.ts +302 -0
- package/src/shared-styles.ts +73 -0
- package/src/snap.ts +129 -0
- package/src/source-metadata.ts +8 -0
- package/src/text-picture.ts +65 -0
- package/src/types.ts +635 -0
- package/src/undo.ts +171 -0
- package/src/variables.ts +399 -0
- package/src/variant-name.ts +15 -0
- package/src/variant-properties.ts +48 -0
- package/src/vector-network.ts +293 -0
- package/dist/chunks/copy.js +0 -576
- package/dist/chunks/copy.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/geometry.js.map +0 -1
- package/dist/preview.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/types2.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-pencil/scene-graph",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "0.15.1",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist",
|
|
6
|
+
"src",
|
|
7
|
+
"README.md"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"es-toolkit": "^1.51.0",
|
|
13
|
+
"nanoevents": "^9.1.0",
|
|
14
|
+
"svgpath": "^2.6.0"
|
|
15
|
+
},
|
|
6
16
|
"exports": {
|
|
7
17
|
".": {
|
|
8
18
|
"types": "./dist/index.d.ts",
|
|
@@ -10,6 +20,12 @@
|
|
|
10
20
|
"import": "./dist/index.js",
|
|
11
21
|
"default": "./dist/index.js"
|
|
12
22
|
},
|
|
23
|
+
"./arrow-caps": {
|
|
24
|
+
"types": "./dist/arrow-caps.d.ts",
|
|
25
|
+
"bun": "./src/arrow-caps.ts",
|
|
26
|
+
"import": "./dist/arrow-caps.js",
|
|
27
|
+
"default": "./dist/arrow-caps.js"
|
|
28
|
+
},
|
|
13
29
|
"./copy": {
|
|
14
30
|
"types": "./dist/copy.d.ts",
|
|
15
31
|
"bun": "./src/copy.ts",
|
|
@@ -52,12 +68,30 @@
|
|
|
52
68
|
"import": "./dist/variant-name.js",
|
|
53
69
|
"default": "./dist/variant-name.js"
|
|
54
70
|
},
|
|
71
|
+
"./variant-properties": {
|
|
72
|
+
"types": "./dist/variant-properties.d.ts",
|
|
73
|
+
"bun": "./src/variant-properties.ts",
|
|
74
|
+
"import": "./dist/variant-properties.js",
|
|
75
|
+
"default": "./dist/variant-properties.js"
|
|
76
|
+
},
|
|
55
77
|
"./vector-network": {
|
|
56
78
|
"types": "./dist/vector-network.d.ts",
|
|
57
79
|
"bun": "./src/vector-network.ts",
|
|
58
80
|
"import": "./dist/vector-network.js",
|
|
59
81
|
"default": "./dist/vector-network.js"
|
|
60
82
|
},
|
|
83
|
+
"./guides": {
|
|
84
|
+
"types": "./dist/guides.d.ts",
|
|
85
|
+
"bun": "./src/guides.ts",
|
|
86
|
+
"import": "./dist/guides.js",
|
|
87
|
+
"default": "./dist/guides.js"
|
|
88
|
+
},
|
|
89
|
+
"./layout-guides": {
|
|
90
|
+
"types": "./dist/layout-guides.d.ts",
|
|
91
|
+
"bun": "./src/layout-guides.ts",
|
|
92
|
+
"import": "./dist/layout-guides.js",
|
|
93
|
+
"default": "./dist/layout-guides.js"
|
|
94
|
+
},
|
|
61
95
|
"./types": {
|
|
62
96
|
"types": "./dist/types.d.ts",
|
|
63
97
|
"bun": "./src/types.ts",
|
|
@@ -137,21 +171,12 @@
|
|
|
137
171
|
"default": "./dist/parse-path.js"
|
|
138
172
|
}
|
|
139
173
|
},
|
|
140
|
-
"
|
|
141
|
-
"types": "./dist/index.d.ts",
|
|
142
|
-
"sideEffects": false,
|
|
143
|
-
"files": [
|
|
144
|
-
"dist",
|
|
145
|
-
"README.md"
|
|
146
|
-
],
|
|
174
|
+
"license": "MIT",
|
|
147
175
|
"repository": {
|
|
148
176
|
"type": "git",
|
|
149
177
|
"url": "git+https://github.com/open-pencil/open-pencil.git",
|
|
150
178
|
"directory": "packages/scene-graph"
|
|
151
179
|
},
|
|
152
|
-
"
|
|
153
|
-
|
|
154
|
-
"nanoevents": "^9.1.0",
|
|
155
|
-
"svgpath": "^2.6.0"
|
|
156
|
-
}
|
|
180
|
+
"type": "module",
|
|
181
|
+
"sideEffects": false
|
|
157
182
|
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { Vector } from './primitives'
|
|
2
|
+
import type { Stroke, StrokeCap, VectorNetwork } from './types'
|
|
3
|
+
|
|
4
|
+
export type ArrowCapKind = 'ARROW_LINES' | 'ARROW_EQUILATERAL'
|
|
5
|
+
|
|
6
|
+
export interface ArrowEndpoint {
|
|
7
|
+
x: number
|
|
8
|
+
y: number
|
|
9
|
+
/** Direction the arrow points, in radians, away from the path. */
|
|
10
|
+
angle: number
|
|
11
|
+
cap: ArrowCapKind
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ArrowWing {
|
|
15
|
+
from: Vector
|
|
16
|
+
to: Vector
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Arrow head side length relative to stroke weight, tuned to match Figma. */
|
|
20
|
+
const ARROW_SIDE_PER_WEIGHT = 4
|
|
21
|
+
const WING_SWEEP = Math.PI / 6
|
|
22
|
+
|
|
23
|
+
export function isArrowCap(cap: string | undefined): cap is ArrowCapKind {
|
|
24
|
+
return cap === 'ARROW_LINES' || cap === 'ARROW_EQUILATERAL'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Worst-case reach of an arrow head past the path: a head's base corners sit
|
|
29
|
+
* one full head side from the tip, and line-arrow wings are stroked at the
|
|
30
|
+
* shaft weight on top of that. Used to widen visual bounds and render margins
|
|
31
|
+
* so heads are not cropped by caches, exports, or culling. Vertex-level caps
|
|
32
|
+
* in the network count even when no stroke or node cap is an arrow.
|
|
33
|
+
*/
|
|
34
|
+
export function arrowCapOverflow(
|
|
35
|
+
strokes?: Stroke[],
|
|
36
|
+
fallbackCap?: StrokeCap,
|
|
37
|
+
network?: VectorNetwork | null
|
|
38
|
+
): number {
|
|
39
|
+
const networkHasArrow = network?.vertices.some((vertex) => isArrowCap(vertex.strokeCap)) ?? false
|
|
40
|
+
let overflow = 0
|
|
41
|
+
for (const stroke of strokes ?? []) {
|
|
42
|
+
if (!stroke.visible) continue
|
|
43
|
+
if (networkHasArrow || isArrowCap(stroke.cap ?? fallbackCap)) {
|
|
44
|
+
overflow = Math.max(overflow, (ARROW_SIDE_PER_WEIGHT + 0.5) * stroke.weight)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return overflow
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Finds the open path ends that should carry an arrow head: vertices used by
|
|
52
|
+
* exactly one segment, with a per-vertex cap override falling back to the
|
|
53
|
+
* stroke cap. The returned angle points away from the path, following the
|
|
54
|
+
* terminal bezier tangent when the segment is curved.
|
|
55
|
+
*/
|
|
56
|
+
export function collectArrowEndpoints(
|
|
57
|
+
network: VectorNetwork,
|
|
58
|
+
fallbackCap: StrokeCap
|
|
59
|
+
): ArrowEndpoint[] {
|
|
60
|
+
const degrees = Array.from({ length: network.vertices.length }, () => 0)
|
|
61
|
+
for (const segment of network.segments) {
|
|
62
|
+
degrees[segment.start]++
|
|
63
|
+
degrees[segment.end]++
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const endpoints: ArrowEndpoint[] = []
|
|
67
|
+
for (const segment of network.segments) {
|
|
68
|
+
for (const side of ['start', 'end'] as const) {
|
|
69
|
+
const vertexIndex = segment[side]
|
|
70
|
+
if (degrees[vertexIndex] !== 1) continue
|
|
71
|
+
const vertex = network.vertices[vertexIndex]
|
|
72
|
+
const cap = (vertex.strokeCap as StrokeCap | undefined) ?? fallbackCap
|
|
73
|
+
if (!isArrowCap(cap)) continue
|
|
74
|
+
|
|
75
|
+
const tangent = side === 'start' ? segment.tangentStart : segment.tangentEnd
|
|
76
|
+
let controlX: number
|
|
77
|
+
let controlY: number
|
|
78
|
+
if (Math.abs(tangent.x) > 0.001 || Math.abs(tangent.y) > 0.001) {
|
|
79
|
+
controlX = vertex.x + tangent.x
|
|
80
|
+
controlY = vertex.y + tangent.y
|
|
81
|
+
} else {
|
|
82
|
+
const other = network.vertices[side === 'start' ? segment.end : segment.start]
|
|
83
|
+
controlX = other.x
|
|
84
|
+
controlY = other.y
|
|
85
|
+
}
|
|
86
|
+
const dx = vertex.x - controlX
|
|
87
|
+
const dy = vertex.y - controlY
|
|
88
|
+
if (dx === 0 && dy === 0) continue
|
|
89
|
+
endpoints.push({ x: vertex.x, y: vertex.y, angle: Math.atan2(dy, dx), cap })
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return endpoints
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Points of a filled equilateral arrow head: tip first, then the two base
|
|
97
|
+
* corners. The tip sits exactly on the endpoint and the head sweeps back
|
|
98
|
+
* along the path.
|
|
99
|
+
*/
|
|
100
|
+
export function equilateralArrowPoints(
|
|
101
|
+
x: number,
|
|
102
|
+
y: number,
|
|
103
|
+
angle: number,
|
|
104
|
+
weight: number
|
|
105
|
+
): [Vector, Vector, Vector] {
|
|
106
|
+
const halfBase = (ARROW_SIDE_PER_WEIGHT / 2) * weight
|
|
107
|
+
const depth = halfBase * Math.sqrt(3)
|
|
108
|
+
const dirX = Math.cos(angle)
|
|
109
|
+
const dirY = Math.sin(angle)
|
|
110
|
+
const perpX = -dirY
|
|
111
|
+
const perpY = dirX
|
|
112
|
+
const baseX = x - depth * dirX
|
|
113
|
+
const baseY = y - depth * dirY
|
|
114
|
+
return [
|
|
115
|
+
{ x, y },
|
|
116
|
+
{ x: baseX - halfBase * perpX, y: baseY - halfBase * perpY },
|
|
117
|
+
{ x: baseX + halfBase * perpX, y: baseY + halfBase * perpY }
|
|
118
|
+
]
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The two stroked wings of an open (lines-style) arrow head. */
|
|
122
|
+
export function arrowLinesSegments(
|
|
123
|
+
x: number,
|
|
124
|
+
y: number,
|
|
125
|
+
angle: number,
|
|
126
|
+
weight: number
|
|
127
|
+
): [ArrowWing, ArrowWing] {
|
|
128
|
+
const wingLength = ARROW_SIDE_PER_WEIGHT * weight
|
|
129
|
+
const tip = { x, y }
|
|
130
|
+
const makeWing = (wingAngle: number): ArrowWing => ({
|
|
131
|
+
from: tip,
|
|
132
|
+
to: { x: x + wingLength * Math.cos(wingAngle), y: y + wingLength * Math.sin(wingAngle) }
|
|
133
|
+
})
|
|
134
|
+
return [makeWing(angle + Math.PI - WING_SWEEP), makeWing(angle + Math.PI + WING_SWEEP)]
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Arrow endpoints for a LINE node, whose local geometry runs (0,0) → (width,height). */
|
|
138
|
+
export function lineArrowEndpoints(width: number, height: number, cap: StrokeCap): ArrowEndpoint[] {
|
|
139
|
+
if (!isArrowCap(cap)) return []
|
|
140
|
+
if (width === 0 && height === 0) return []
|
|
141
|
+
const angle = Math.atan2(height, width)
|
|
142
|
+
return [
|
|
143
|
+
{ x: 0, y: 0, angle: Math.atan2(-height, -width), cap },
|
|
144
|
+
{ x: width, y: height, angle, cap }
|
|
145
|
+
]
|
|
146
|
+
}
|
package/src/bindings.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { omit } from 'es-toolkit/object'
|
|
2
|
+
|
|
3
|
+
import type { SceneNode } from './types'
|
|
4
|
+
|
|
5
|
+
export function removeStaleBindings(
|
|
6
|
+
node: SceneNode,
|
|
7
|
+
field: 'fills' | 'strokes',
|
|
8
|
+
changes: Partial<SceneNode>
|
|
9
|
+
): void {
|
|
10
|
+
const length = node[field].length
|
|
11
|
+
const stale = Object.keys(node.boundVariables).filter((key) => {
|
|
12
|
+
if (key === field) return true
|
|
13
|
+
if (!key.startsWith(`${field}/`)) return false
|
|
14
|
+
const index = Number.parseInt(key.split('/')[1] ?? '', 10)
|
|
15
|
+
return Number.isNaN(index) || index < 0 || index >= length
|
|
16
|
+
})
|
|
17
|
+
if (stale.length === 0) return
|
|
18
|
+
node.boundVariables = omit(node.boundVariables, stale)
|
|
19
|
+
changes.boundVariables = { ...node.boundVariables }
|
|
20
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit'
|
|
2
|
+
|
|
3
|
+
import type { SceneGraph } from './index'
|
|
4
|
+
|
|
5
|
+
/** Capture rollback state without changing graph or surviving object identities. */
|
|
6
|
+
export function captureGraphCheckpoint(graph: SceneGraph) {
|
|
7
|
+
const nodeRefs = new Map(graph.nodes)
|
|
8
|
+
const variableRefs = new Map(graph.variables)
|
|
9
|
+
const collectionRefs = new Map(graph.variableCollections)
|
|
10
|
+
const snapshot = structuredClone({
|
|
11
|
+
nodes: graph.nodes,
|
|
12
|
+
variables: graph.variables,
|
|
13
|
+
variableCollections: graph.variableCollections,
|
|
14
|
+
activeMode: graph.activeMode,
|
|
15
|
+
instanceIndex: graph.instanceIndex,
|
|
16
|
+
enabledLibraries: graph.enabledLibraries,
|
|
17
|
+
rootId: graph.rootId,
|
|
18
|
+
figKiwiVersion: graph.figKiwiVersion,
|
|
19
|
+
figSchemaDeflated: graph.figSchemaDeflated,
|
|
20
|
+
documentColorSpace: graph.documentColorSpace
|
|
21
|
+
})
|
|
22
|
+
// Atomic property tools cannot edit assets. Retain existing buffers, not copies of every bitmap.
|
|
23
|
+
const images = new Map(graph.images)
|
|
24
|
+
|
|
25
|
+
function assertPropertiesOnly() {
|
|
26
|
+
if (
|
|
27
|
+
snapshot.nodes.size !== graph.nodes.size ||
|
|
28
|
+
snapshot.variables.size !== graph.variables.size
|
|
29
|
+
)
|
|
30
|
+
throw new Error('Atomic tools must not create or remove nodes or variables')
|
|
31
|
+
for (const [id, before] of snapshot.nodes) {
|
|
32
|
+
const after = graph.nodes.get(id)
|
|
33
|
+
if (
|
|
34
|
+
!after ||
|
|
35
|
+
after !== nodeRefs.get(id) ||
|
|
36
|
+
before.id !== after.id ||
|
|
37
|
+
before.type !== after.type ||
|
|
38
|
+
before.parentId !== after.parentId ||
|
|
39
|
+
before.componentId !== after.componentId ||
|
|
40
|
+
!isEqual(before.childIds, after.childIds)
|
|
41
|
+
) {
|
|
42
|
+
throw new Error('Atomic tools must not change node hierarchy or identity')
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
for (const [id, before] of snapshot.variables) {
|
|
46
|
+
const after = graph.variables.get(id)
|
|
47
|
+
if (
|
|
48
|
+
!after ||
|
|
49
|
+
after !== variableRefs.get(id) ||
|
|
50
|
+
before.id !== after.id ||
|
|
51
|
+
before.collectionId !== after.collectionId ||
|
|
52
|
+
before.type !== after.type
|
|
53
|
+
)
|
|
54
|
+
throw new Error('Atomic tools must not change variable identity')
|
|
55
|
+
}
|
|
56
|
+
assertDocumentUnchanged()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function assertDocumentUnchanged() {
|
|
60
|
+
if (
|
|
61
|
+
!isEqual(snapshot.variableCollections, graph.variableCollections) ||
|
|
62
|
+
!isEqual(snapshot.activeMode, graph.activeMode) ||
|
|
63
|
+
!isEqual(snapshot.instanceIndex, graph.instanceIndex) ||
|
|
64
|
+
!isEqual(snapshot.enabledLibraries, graph.enabledLibraries) ||
|
|
65
|
+
images.size !== graph.images.size ||
|
|
66
|
+
[...images].some(([id, bytes]) => graph.images.get(id) !== bytes) ||
|
|
67
|
+
graph.rootId !== snapshot.rootId ||
|
|
68
|
+
graph.documentColorSpace !== snapshot.documentColorSpace ||
|
|
69
|
+
graph.figKiwiVersion !== snapshot.figKiwiVersion ||
|
|
70
|
+
!isEqual(graph.figSchemaDeflated, snapshot.figSchemaDeflated)
|
|
71
|
+
) {
|
|
72
|
+
throw new Error('Atomic tools must not change collections, assets or document metadata')
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function restore() {
|
|
77
|
+
const failedNodes = new Map(graph.nodes)
|
|
78
|
+
restoreObjects(graph.nodes, snapshot.nodes, nodeRefs)
|
|
79
|
+
restoreObjects(graph.variables, snapshot.variables, variableRefs)
|
|
80
|
+
restoreObjects(graph.variableCollections, snapshot.variableCollections, collectionRefs)
|
|
81
|
+
restoreMap(graph.activeMode, structuredClone(snapshot.activeMode))
|
|
82
|
+
restoreMap(graph.instanceIndex, structuredClone(snapshot.instanceIndex))
|
|
83
|
+
restoreMap(graph.enabledLibraries, structuredClone(snapshot.enabledLibraries))
|
|
84
|
+
restoreMap(graph.images, images)
|
|
85
|
+
graph.rootId = snapshot.rootId
|
|
86
|
+
graph.figKiwiVersion = snapshot.figKiwiVersion
|
|
87
|
+
graph.figSchemaDeflated = structuredClone(snapshot.figSchemaDeflated)
|
|
88
|
+
graph.documentColorSpace = snapshot.documentColorSpace
|
|
89
|
+
graph.clearAbsPosCache()
|
|
90
|
+
// Notify consumers only after the complete hierarchy and indexes have been restored.
|
|
91
|
+
for (const [id, node] of failedNodes) {
|
|
92
|
+
if (!graph.nodes.has(id)) graph.emitter.emit('node:deleted', id, node.parentId)
|
|
93
|
+
}
|
|
94
|
+
for (const [id, node] of graph.nodes) {
|
|
95
|
+
if (failedNodes.has(id)) graph.emitter.emit('node:updated', id, node)
|
|
96
|
+
else graph.emitter.emit('node:created', node)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return { nodes: snapshot.nodes, variables: snapshot.variables, assertPropertiesOnly, restore }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function restoreObjects<T extends object>(
|
|
104
|
+
target: Map<string, T>,
|
|
105
|
+
snapshot: Map<string, T>,
|
|
106
|
+
refs: Map<string, T>
|
|
107
|
+
) {
|
|
108
|
+
target.clear()
|
|
109
|
+
for (const [id, saved] of snapshot) {
|
|
110
|
+
const original = refs.get(id)
|
|
111
|
+
if (!original) throw new Error(`Missing checkpoint reference: ${id}`)
|
|
112
|
+
for (const key of Object.keys(original) as (keyof T)[]) {
|
|
113
|
+
if (!(key in saved)) Reflect.deleteProperty(original, key)
|
|
114
|
+
}
|
|
115
|
+
Object.assign(original, structuredClone(saved))
|
|
116
|
+
target.set(id, original)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function restoreMap<K, V>(target: Map<K, V>, saved: Map<K, V>) {
|
|
121
|
+
target.clear()
|
|
122
|
+
for (const [key, value] of saved) target.set(key, value)
|
|
123
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import type { SceneGraph } from '../index'
|
|
2
|
+
import { setInstanceOverride } from '../instance-overrides'
|
|
3
|
+
import { findInstanceAncestor } from '../instances'
|
|
4
|
+
import type {
|
|
5
|
+
ComponentPropertyDefinition,
|
|
6
|
+
ComponentPropertyReferenceField,
|
|
7
|
+
SceneNode
|
|
8
|
+
} from '../types'
|
|
9
|
+
|
|
10
|
+
export interface ComponentPropertyTarget {
|
|
11
|
+
node: SceneNode
|
|
12
|
+
field: ComponentPropertyReferenceField
|
|
13
|
+
source: SceneNode
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function componentPropertyOwners(graph: SceneGraph, instance: SceneNode): SceneNode[] {
|
|
17
|
+
if (instance.type !== 'INSTANCE' || !instance.componentId) return []
|
|
18
|
+
const component = graph.getNode(instance.componentId)
|
|
19
|
+
if (!component) return []
|
|
20
|
+
const parent = component.parentId ? graph.getNode(component.parentId) : null
|
|
21
|
+
return parent?.type === 'COMPONENT_SET' ? [parent, component] : [component]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function componentPropertyDefinitions(
|
|
25
|
+
graph: SceneGraph,
|
|
26
|
+
instance: SceneNode
|
|
27
|
+
): ComponentPropertyDefinition[] {
|
|
28
|
+
const definitions = new Map<string, ComponentPropertyDefinition>()
|
|
29
|
+
for (const owner of componentPropertyOwners(graph, instance)) {
|
|
30
|
+
for (const definition of owner.componentPropertyDefinitions) {
|
|
31
|
+
if (!definitions.has(definition.id)) definitions.set(definition.id, definition)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return [...definitions.values()]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolveComponentPropertyValue(graph: SceneGraph, value: string): SceneNode | null {
|
|
38
|
+
const direct = graph.getNode(value)
|
|
39
|
+
if (direct?.type === 'COMPONENT') return direct
|
|
40
|
+
if (direct?.type === 'COMPONENT_SET') {
|
|
41
|
+
const componentId = direct.childIds.find((id) => graph.getNode(id)?.type === 'COMPONENT')
|
|
42
|
+
return componentId ? (graph.getNode(componentId) ?? null) : null
|
|
43
|
+
}
|
|
44
|
+
for (const node of graph.getAllNodes()) {
|
|
45
|
+
if (
|
|
46
|
+
node.type === 'COMPONENT' &&
|
|
47
|
+
(node.componentKey === value || node.sourceLibraryKey === value || node.source.id === value)
|
|
48
|
+
) {
|
|
49
|
+
return node
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function findComponentPropertyTarget(
|
|
56
|
+
graph: SceneGraph,
|
|
57
|
+
instance: SceneNode,
|
|
58
|
+
propertyId: string
|
|
59
|
+
): ComponentPropertyTarget | null {
|
|
60
|
+
return findComponentPropertyTargets(graph, instance, propertyId)[0] ?? null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function findComponentPropertyTargets(
|
|
64
|
+
graph: SceneGraph,
|
|
65
|
+
instance: SceneNode,
|
|
66
|
+
propertyId: string
|
|
67
|
+
): ComponentPropertyTarget[] {
|
|
68
|
+
if (instance.type !== 'INSTANCE' || !instance.componentId) return []
|
|
69
|
+
const component = graph.getNode(instance.componentId)
|
|
70
|
+
if (!component) return []
|
|
71
|
+
const targets: ComponentPropertyTarget[] = []
|
|
72
|
+
const visit = (sourceParent: SceneNode, instanceParent: SceneNode): void => {
|
|
73
|
+
for (const [index, childId] of sourceParent.childIds.entries()) {
|
|
74
|
+
const source = graph.getNode(childId)
|
|
75
|
+
const targetId = instanceParent.childIds[index]
|
|
76
|
+
const target = targetId ? graph.getNode(targetId) : undefined
|
|
77
|
+
if (!source || !target) continue
|
|
78
|
+
const reference = source.componentPropertyReferences.find(
|
|
79
|
+
(candidate) => candidate.propertyId === propertyId
|
|
80
|
+
)
|
|
81
|
+
if (reference) targets.push({ node: target, field: reference.field, source })
|
|
82
|
+
visit(source, target)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
visit(component, instance)
|
|
86
|
+
return targets
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function applyTextProperty(
|
|
90
|
+
graph: SceneGraph,
|
|
91
|
+
owner: SceneNode,
|
|
92
|
+
targets: ComponentPropertyTarget[],
|
|
93
|
+
value: string
|
|
94
|
+
): void {
|
|
95
|
+
for (const item of targets) {
|
|
96
|
+
if (item.field !== 'TEXT' || item.node.type !== 'TEXT') continue
|
|
97
|
+
graph.updateNode(item.node.id, { text: value })
|
|
98
|
+
if (findInstanceAncestor(graph, item.node.id)) {
|
|
99
|
+
setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'text', value)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function applyBooleanProperty(
|
|
105
|
+
graph: SceneGraph,
|
|
106
|
+
owner: SceneNode,
|
|
107
|
+
targets: ComponentPropertyTarget[],
|
|
108
|
+
value: string
|
|
109
|
+
): void {
|
|
110
|
+
for (const item of targets) {
|
|
111
|
+
if (item.field !== 'VISIBLE') continue
|
|
112
|
+
const instance = findInstanceAncestor(graph, item.node.id)
|
|
113
|
+
graph.updateNode(item.node.id, { visible: value === 'true' })
|
|
114
|
+
if (instance) {
|
|
115
|
+
setInstanceOverride(
|
|
116
|
+
owner.instanceOverrides,
|
|
117
|
+
owner.id,
|
|
118
|
+
item.node.id,
|
|
119
|
+
'visible',
|
|
120
|
+
value === 'true'
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function applyInstanceSwapProperty(
|
|
127
|
+
graph: SceneGraph,
|
|
128
|
+
owner: SceneNode,
|
|
129
|
+
targets: ComponentPropertyTarget[],
|
|
130
|
+
target: SceneNode
|
|
131
|
+
): void {
|
|
132
|
+
for (const item of targets) {
|
|
133
|
+
if (item.field !== 'INSTANCE_SWAP' || item.node.type !== 'INSTANCE') continue
|
|
134
|
+
graph.swapInstanceComponent(item.node.id, target.id)
|
|
135
|
+
setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'name', target.name)
|
|
136
|
+
setInstanceOverride(owner.instanceOverrides, owner.id, item.node.id, 'componentId', target.id)
|
|
137
|
+
setInstanceOverride(
|
|
138
|
+
owner.instanceOverrides,
|
|
139
|
+
owner.id,
|
|
140
|
+
item.node.id,
|
|
141
|
+
'sourceComponentId',
|
|
142
|
+
item.source.id
|
|
143
|
+
)
|
|
144
|
+
graph.updateNode(owner.id, { instanceOverrides: owner.instanceOverrides })
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function applyComponentPropertyValue(
|
|
149
|
+
graph: SceneGraph,
|
|
150
|
+
instanceId: string,
|
|
151
|
+
definition: ComponentPropertyDefinition,
|
|
152
|
+
value: string
|
|
153
|
+
): SceneNode | null {
|
|
154
|
+
const instance = graph.getNode(instanceId)
|
|
155
|
+
if (instance?.type !== 'INSTANCE') return null
|
|
156
|
+
if (definition.type === 'VARIANT') return null
|
|
157
|
+
const targets = findComponentPropertyTargets(graph, instance, definition.id)
|
|
158
|
+
const target =
|
|
159
|
+
definition.type === 'INSTANCE_SWAP' ? resolveComponentPropertyValue(graph, value) : null
|
|
160
|
+
if (definition.type === 'INSTANCE_SWAP' && !target) return null
|
|
161
|
+
if (definition.type === 'TEXT') {
|
|
162
|
+
applyTextProperty(graph, instance, targets, value)
|
|
163
|
+
} else if (definition.type === 'BOOLEAN') {
|
|
164
|
+
applyBooleanProperty(graph, instance, targets, value)
|
|
165
|
+
} else if (target) {
|
|
166
|
+
applyInstanceSwapProperty(graph, instance, targets, target)
|
|
167
|
+
}
|
|
168
|
+
graph.updateNode(instance.id, {
|
|
169
|
+
componentPropertyAssignments: {
|
|
170
|
+
...instance.componentPropertyAssignments,
|
|
171
|
+
[definition.id]: definition.type === 'INSTANCE_SWAP' && target ? target.id : value
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
return definition.type === 'INSTANCE_SWAP' ? target : instance
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function componentSubtree(graph: SceneGraph, componentId: string): SceneNode[] {
|
|
178
|
+
const component = graph.getNode(componentId)
|
|
179
|
+
if (!component) return []
|
|
180
|
+
const nodes = [component]
|
|
181
|
+
const visit = (node: SceneNode): void => {
|
|
182
|
+
for (const child of graph.getChildren(node.id)) {
|
|
183
|
+
nodes.push(child)
|
|
184
|
+
visit(child)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
visit(component)
|
|
188
|
+
return nodes
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function componentInstances(graph: SceneGraph, componentId: string): SceneNode[] {
|
|
192
|
+
return [...graph.getAllNodes()].filter(
|
|
193
|
+
(node) => node.type === 'INSTANCE' && node.componentId === componentId
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function removePropertyFromNode(graph: SceneGraph, node: SceneNode, propertyId: string): void {
|
|
198
|
+
if (node.componentPropertyReferences.some((reference) => reference.propertyId === propertyId)) {
|
|
199
|
+
graph.updateNode(node.id, {
|
|
200
|
+
componentPropertyReferences: node.componentPropertyReferences.filter(
|
|
201
|
+
(reference) => reference.propertyId !== propertyId
|
|
202
|
+
)
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
if (node.type === 'INSTANCE' && propertyId in node.componentPropertyAssignments) {
|
|
206
|
+
graph.updateNode(node.id, {
|
|
207
|
+
componentPropertyAssignments: Object.fromEntries(
|
|
208
|
+
Object.entries(node.componentPropertyAssignments).filter(([id]) => id !== propertyId)
|
|
209
|
+
)
|
|
210
|
+
})
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function removeComponentProperty(
|
|
215
|
+
graph: SceneGraph,
|
|
216
|
+
ownerId: string,
|
|
217
|
+
propertyId: string
|
|
218
|
+
): boolean {
|
|
219
|
+
const owner = graph.getNode(ownerId)
|
|
220
|
+
if (!owner || (owner.type !== 'COMPONENT' && owner.type !== 'COMPONENT_SET')) return false
|
|
221
|
+
graph.updateNode(owner.id, {
|
|
222
|
+
componentPropertyDefinitions: owner.componentPropertyDefinitions.filter(
|
|
223
|
+
(definition) => definition.id !== propertyId
|
|
224
|
+
)
|
|
225
|
+
})
|
|
226
|
+
const nodes = [
|
|
227
|
+
...componentSubtree(graph, owner.id),
|
|
228
|
+
...componentInstances(graph, owner.id),
|
|
229
|
+
...(owner.type === 'COMPONENT_SET'
|
|
230
|
+
? owner.childIds.flatMap((id) => [
|
|
231
|
+
...componentSubtree(graph, id),
|
|
232
|
+
...componentInstances(graph, id)
|
|
233
|
+
])
|
|
234
|
+
: [])
|
|
235
|
+
]
|
|
236
|
+
for (const node of nodes) removePropertyFromNode(graph, node, propertyId)
|
|
237
|
+
return true
|
|
238
|
+
}
|
package/src/constants.ts
ADDED