@open-pencil/scene-graph 0.13.2 → 0.15.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/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
|
@@ -0,0 +1,1283 @@
|
|
|
1
|
+
import { BLACK, CONTAINER_TYPES, cloneNodeProps, createDefaultNode, createInstance, detachInstance, getInstances, getMainComponent, normalizeVectorNetwork, populateInstanceChildren, rotatedBBox, setInstanceOverride, swapInstanceComponent, syncInstances } from "./components/properties.js";
|
|
2
|
+
import { getAbsolutePosition } from "./coordinate.js";
|
|
3
|
+
import { removeStaleBindings } from "./bindings.js";
|
|
4
|
+
import { bindNodeEvents } from "./events.js";
|
|
5
|
+
import { hitTest, hitTestDeep, hitTestFrame } from "../hit-test.js";
|
|
6
|
+
import { createNanoEvents } from "nanoevents";
|
|
7
|
+
import { omit, omitBy } from "es-toolkit/object";
|
|
8
|
+
//#region src/mutation-impact.ts
|
|
9
|
+
function createSceneMutationImpact() {
|
|
10
|
+
return {
|
|
11
|
+
changedNodeIds: /* @__PURE__ */ new Set(),
|
|
12
|
+
previousParentIds: /* @__PURE__ */ new Set(),
|
|
13
|
+
currentParentIds: /* @__PURE__ */ new Set(),
|
|
14
|
+
createdNodeIds: /* @__PURE__ */ new Set(),
|
|
15
|
+
deletedNodeIds: /* @__PURE__ */ new Set()
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** Collects the actual graph nodes and parent containers touched by an operation. */
|
|
19
|
+
async function collectSceneMutation(graph, operation) {
|
|
20
|
+
const impact = createSceneMutationImpact();
|
|
21
|
+
const unbind = graph.onNodeEvents({
|
|
22
|
+
created: (node) => {
|
|
23
|
+
impact.createdNodeIds.add(node.id);
|
|
24
|
+
impact.changedNodeIds.add(node.id);
|
|
25
|
+
if (node.parentId) impact.currentParentIds.add(node.parentId);
|
|
26
|
+
},
|
|
27
|
+
updated: (id) => impact.changedNodeIds.add(id),
|
|
28
|
+
deleted: (id, parentId) => {
|
|
29
|
+
if (parentId) impact.previousParentIds.add(parentId);
|
|
30
|
+
impact.deletedNodeIds.add(id);
|
|
31
|
+
impact.changedNodeIds.add(id);
|
|
32
|
+
},
|
|
33
|
+
reparented: (nodeId, oldParentId, newParentId) => {
|
|
34
|
+
impact.changedNodeIds.add(nodeId);
|
|
35
|
+
if (oldParentId) impact.previousParentIds.add(oldParentId);
|
|
36
|
+
impact.currentParentIds.add(newParentId);
|
|
37
|
+
},
|
|
38
|
+
reordered: (nodeId, parentId, _index, previousParentId) => {
|
|
39
|
+
impact.changedNodeIds.add(nodeId);
|
|
40
|
+
if (previousParentId && previousParentId !== parentId) impact.previousParentIds.add(previousParentId);
|
|
41
|
+
impact.currentParentIds.add(parentId);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
try {
|
|
45
|
+
return {
|
|
46
|
+
result: await operation(),
|
|
47
|
+
impact
|
|
48
|
+
};
|
|
49
|
+
} finally {
|
|
50
|
+
unbind();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function mutationLayoutScopeIds(impact) {
|
|
54
|
+
return [.../* @__PURE__ */ new Set([
|
|
55
|
+
...impact.changedNodeIds,
|
|
56
|
+
...impact.previousParentIds,
|
|
57
|
+
...impact.currentParentIds
|
|
58
|
+
])];
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/snap.ts
|
|
62
|
+
const DEFAULT_SNAP_THRESHOLD = 5;
|
|
63
|
+
function getEdges(node) {
|
|
64
|
+
return rotatedBBox(node.x, node.y, node.width, node.height, node.rotation);
|
|
65
|
+
}
|
|
66
|
+
function computeSnap(movingIds, movingBounds, allNodes, threshold = DEFAULT_SNAP_THRESHOLD) {
|
|
67
|
+
const targets = allNodes.filter((n) => !movingIds.has(n.id));
|
|
68
|
+
if (targets.length === 0) return {
|
|
69
|
+
dx: 0,
|
|
70
|
+
dy: 0,
|
|
71
|
+
guides: []
|
|
72
|
+
};
|
|
73
|
+
const m = {
|
|
74
|
+
left: movingBounds.x,
|
|
75
|
+
right: movingBounds.x + movingBounds.width,
|
|
76
|
+
centerX: movingBounds.x + movingBounds.width / 2,
|
|
77
|
+
top: movingBounds.y,
|
|
78
|
+
bottom: movingBounds.y + movingBounds.height,
|
|
79
|
+
centerY: movingBounds.y + movingBounds.height / 2
|
|
80
|
+
};
|
|
81
|
+
let bestDx = Infinity;
|
|
82
|
+
let bestDy = Infinity;
|
|
83
|
+
const guides = [];
|
|
84
|
+
for (const target of targets) {
|
|
85
|
+
const t = getEdges(target);
|
|
86
|
+
const xPairs = [
|
|
87
|
+
[m.left, t.left],
|
|
88
|
+
[m.left, t.right],
|
|
89
|
+
[m.right, t.left],
|
|
90
|
+
[m.right, t.right],
|
|
91
|
+
[m.centerX, t.centerX]
|
|
92
|
+
];
|
|
93
|
+
for (const [mVal, tVal] of xPairs) {
|
|
94
|
+
const d = tVal - mVal;
|
|
95
|
+
if (Math.abs(d) < threshold && Math.abs(d) <= Math.abs(bestDx)) {
|
|
96
|
+
if (Math.abs(d) < Math.abs(bestDx)) {
|
|
97
|
+
bestDx = d;
|
|
98
|
+
guides.length = guides.filter((g) => g.axis === "y").length ? guides.length : guides.length;
|
|
99
|
+
for (let i = guides.length - 1; i >= 0; i--) if (guides[i].axis === "x") guides.splice(i, 1);
|
|
100
|
+
}
|
|
101
|
+
if (Math.abs(d) === Math.abs(bestDx)) {
|
|
102
|
+
const minY = Math.min(m.top, t.top);
|
|
103
|
+
const maxY = Math.max(m.bottom, t.bottom);
|
|
104
|
+
guides.push({
|
|
105
|
+
axis: "x",
|
|
106
|
+
position: tVal,
|
|
107
|
+
from: minY,
|
|
108
|
+
to: maxY
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const yPairs = [
|
|
114
|
+
[m.top, t.top],
|
|
115
|
+
[m.top, t.bottom],
|
|
116
|
+
[m.bottom, t.top],
|
|
117
|
+
[m.bottom, t.bottom],
|
|
118
|
+
[m.centerY, t.centerY]
|
|
119
|
+
];
|
|
120
|
+
for (const [mVal, tVal] of yPairs) {
|
|
121
|
+
const d = tVal - mVal;
|
|
122
|
+
if (Math.abs(d) < threshold && Math.abs(d) <= Math.abs(bestDy)) {
|
|
123
|
+
if (Math.abs(d) < Math.abs(bestDy)) {
|
|
124
|
+
for (let i = guides.length - 1; i >= 0; i--) if (guides[i].axis === "y") guides.splice(i, 1);
|
|
125
|
+
bestDy = d;
|
|
126
|
+
}
|
|
127
|
+
if (Math.abs(d) === Math.abs(bestDy)) {
|
|
128
|
+
const minX = Math.min(m.left, t.left);
|
|
129
|
+
const maxX = Math.max(m.right, t.right);
|
|
130
|
+
guides.push({
|
|
131
|
+
axis: "y",
|
|
132
|
+
position: tVal,
|
|
133
|
+
from: minX,
|
|
134
|
+
to: maxX
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
dx: Math.abs(bestDx) <= threshold ? bestDx : 0,
|
|
142
|
+
dy: Math.abs(bestDy) <= threshold ? bestDy : 0,
|
|
143
|
+
guides
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function computeSelectionBounds(nodes) {
|
|
147
|
+
if (nodes.length === 0) return null;
|
|
148
|
+
let minX = Infinity;
|
|
149
|
+
let minY = Infinity;
|
|
150
|
+
let maxX = -Infinity;
|
|
151
|
+
let maxY = -Infinity;
|
|
152
|
+
for (const n of nodes) {
|
|
153
|
+
const edges = getEdges(n);
|
|
154
|
+
minX = Math.min(minX, edges.left);
|
|
155
|
+
minY = Math.min(minY, edges.top);
|
|
156
|
+
maxX = Math.max(maxX, edges.right);
|
|
157
|
+
maxY = Math.max(maxY, edges.bottom);
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
x: minX,
|
|
161
|
+
y: minY,
|
|
162
|
+
width: maxX - minX,
|
|
163
|
+
height: maxY - minY
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/layout-guides.ts
|
|
168
|
+
const MAX_LAYOUT_GUIDE_LINES = 1e4;
|
|
169
|
+
function normalizedCount(grid) {
|
|
170
|
+
const value = grid.count ?? grid.numSections ?? 1;
|
|
171
|
+
if (!Number.isFinite(value)) return 0;
|
|
172
|
+
return Math.min(MAX_LAYOUT_GUIDE_LINES, Math.max(0, Math.floor(value)));
|
|
173
|
+
}
|
|
174
|
+
function pattern(grid) {
|
|
175
|
+
if (grid.pattern === "GRID" || grid.pattern === "ROWS") return grid.pattern;
|
|
176
|
+
if (grid.axis === "Y") return "ROWS";
|
|
177
|
+
return "COLUMNS";
|
|
178
|
+
}
|
|
179
|
+
function alignment(grid) {
|
|
180
|
+
const value = grid.alignment ?? grid.type;
|
|
181
|
+
if (value === "CENTER" || value === "MAX" || value === "STRETCH") return value;
|
|
182
|
+
return "MIN";
|
|
183
|
+
}
|
|
184
|
+
function sectionSize(nodeSize, grid) {
|
|
185
|
+
const fixed = grid.sectionSize ?? 0;
|
|
186
|
+
if (alignment(grid) !== "STRETCH") return fixed;
|
|
187
|
+
const count = normalizedCount(grid);
|
|
188
|
+
const gutter = grid.gutterSize ?? 0;
|
|
189
|
+
return (nodeSize - (grid.offset ?? 0) * 2 - Math.max(0, count - 1) * gutter) / count;
|
|
190
|
+
}
|
|
191
|
+
function start(nodeSize, grid, size) {
|
|
192
|
+
const count = normalizedCount(grid);
|
|
193
|
+
const gutter = grid.gutterSize ?? 0;
|
|
194
|
+
const offset = grid.offset ?? 0;
|
|
195
|
+
const span = count * size + Math.max(0, count - 1) * gutter;
|
|
196
|
+
if (alignment(grid) === "CENTER") return (nodeSize - span) / 2 + offset;
|
|
197
|
+
if (alignment(grid) === "MAX") return nodeSize - span - offset;
|
|
198
|
+
return offset;
|
|
199
|
+
}
|
|
200
|
+
function layoutGuideSections(node, grid) {
|
|
201
|
+
if (grid.visible === false || pattern(grid) === "GRID") return [];
|
|
202
|
+
const axis = pattern(grid) === "ROWS" ? "y" : "x";
|
|
203
|
+
const nodeSize = axis === "x" ? node.width : node.height;
|
|
204
|
+
const size = sectionSize(nodeSize, grid);
|
|
205
|
+
const count = normalizedCount(grid);
|
|
206
|
+
if (count <= 0 || size <= 0) return [];
|
|
207
|
+
const first = start(nodeSize, grid, size);
|
|
208
|
+
const step = size + (grid.gutterSize ?? 0);
|
|
209
|
+
return Array.from({ length: count }, (_, index) => {
|
|
210
|
+
const sectionStart = first + index * step;
|
|
211
|
+
return {
|
|
212
|
+
axis,
|
|
213
|
+
start: sectionStart,
|
|
214
|
+
end: sectionStart + size
|
|
215
|
+
};
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
function layoutGuideLines(node, grid) {
|
|
219
|
+
if (grid.visible === false) return [];
|
|
220
|
+
const gridPattern = pattern(grid);
|
|
221
|
+
const lines = [];
|
|
222
|
+
if (gridPattern === "GRID") {
|
|
223
|
+
const size = grid.sectionSize ?? 0;
|
|
224
|
+
if (size <= 0) return lines;
|
|
225
|
+
for (let index = 0, x = grid.offset ?? 0; x <= node.width && index < MAX_LAYOUT_GUIDE_LINES; index++, x += size) lines.push({
|
|
226
|
+
axis: "x",
|
|
227
|
+
position: x
|
|
228
|
+
});
|
|
229
|
+
for (let index = 0, y = grid.offset ?? 0; y <= node.height && index < MAX_LAYOUT_GUIDE_LINES; index++, y += size) lines.push({
|
|
230
|
+
axis: "y",
|
|
231
|
+
position: y
|
|
232
|
+
});
|
|
233
|
+
return lines;
|
|
234
|
+
}
|
|
235
|
+
const sections = layoutGuideSections(node, grid);
|
|
236
|
+
for (const section of sections) lines.push({
|
|
237
|
+
axis: section.axis,
|
|
238
|
+
position: section.start
|
|
239
|
+
}, {
|
|
240
|
+
axis: section.axis,
|
|
241
|
+
position: section.end
|
|
242
|
+
});
|
|
243
|
+
return lines;
|
|
244
|
+
}
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/shared-styles.ts
|
|
247
|
+
const STYLE_REF_KEYS = {
|
|
248
|
+
fill: "fillStyleId",
|
|
249
|
+
stroke: "strokeStyleId",
|
|
250
|
+
text: "textStyleId",
|
|
251
|
+
effect: "effectStyleId",
|
|
252
|
+
grid: "gridStyleId"
|
|
253
|
+
};
|
|
254
|
+
const STYLE_TYPES = {
|
|
255
|
+
fill: "FILL",
|
|
256
|
+
stroke: "FILL",
|
|
257
|
+
text: "TEXT",
|
|
258
|
+
effect: "EFFECT",
|
|
259
|
+
grid: "GRID"
|
|
260
|
+
};
|
|
261
|
+
const TEXT_STYLE_KEYS = /* @__PURE__ */ new Set([
|
|
262
|
+
"fontFamily",
|
|
263
|
+
"fontWeight",
|
|
264
|
+
"italic",
|
|
265
|
+
"fontSize",
|
|
266
|
+
"lineHeight",
|
|
267
|
+
"letterSpacing",
|
|
268
|
+
"textDecoration",
|
|
269
|
+
"textCase",
|
|
270
|
+
"fontFeatures"
|
|
271
|
+
]);
|
|
272
|
+
function sharedStyleRefKey(kind) {
|
|
273
|
+
return STYLE_REF_KEYS[kind];
|
|
274
|
+
}
|
|
275
|
+
function sharedStyleTypeForKind(kind) {
|
|
276
|
+
return STYLE_TYPES[kind];
|
|
277
|
+
}
|
|
278
|
+
function getSharedStyles(graph, kind) {
|
|
279
|
+
const type = sharedStyleTypeForKind(kind);
|
|
280
|
+
const styles = [];
|
|
281
|
+
for (const node of graph.getAllNodes()) {
|
|
282
|
+
if (node.sharedStyleType !== type || !node.source.id) continue;
|
|
283
|
+
styles.push({
|
|
284
|
+
id: node.source.id,
|
|
285
|
+
nodeId: node.id,
|
|
286
|
+
name: node.name,
|
|
287
|
+
type
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
return styles.sort((left, right) => left.name.localeCompare(right.name));
|
|
291
|
+
}
|
|
292
|
+
function styleDetachmentChanges(node, changes) {
|
|
293
|
+
const next = { ...changes };
|
|
294
|
+
if ("fills" in changes && !("fillStyleId" in changes) && node.fillStyleId) next.fillStyleId = null;
|
|
295
|
+
if ("strokes" in changes && !("strokeStyleId" in changes) && node.strokeStyleId) next.strokeStyleId = null;
|
|
296
|
+
if ("effects" in changes && !("effectStyleId" in changes) && node.effectStyleId) next.effectStyleId = null;
|
|
297
|
+
if ("layoutGrids" in changes && !("gridStyleId" in changes) && node.gridStyleId) next.gridStyleId = null;
|
|
298
|
+
if (Object.keys(changes).some((key) => TEXT_STYLE_KEYS.has(key)) && !("textStyleId" in changes) && node.textStyleId) next.textStyleId = null;
|
|
299
|
+
return next;
|
|
300
|
+
}
|
|
301
|
+
//#endregion
|
|
302
|
+
//#region src/undo.ts
|
|
303
|
+
const DEFAULT_HISTORY_LIMIT = 200;
|
|
304
|
+
var UndoManager = class {
|
|
305
|
+
undoStack = [];
|
|
306
|
+
redoStack = [];
|
|
307
|
+
batches = [];
|
|
308
|
+
limit;
|
|
309
|
+
onChange;
|
|
310
|
+
constructor(options = {}) {
|
|
311
|
+
this.limit = options.limit ?? DEFAULT_HISTORY_LIMIT;
|
|
312
|
+
this.onChange = options.onChange;
|
|
313
|
+
}
|
|
314
|
+
apply(entry) {
|
|
315
|
+
this.execute(entry);
|
|
316
|
+
}
|
|
317
|
+
execute(entry) {
|
|
318
|
+
entry.forward();
|
|
319
|
+
this.record(entry);
|
|
320
|
+
}
|
|
321
|
+
push(entry) {
|
|
322
|
+
this.record(entry);
|
|
323
|
+
}
|
|
324
|
+
record(entry) {
|
|
325
|
+
const batch = this.currentBatch;
|
|
326
|
+
if (batch) {
|
|
327
|
+
batch.entries.push(entry);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
this.pushUndoEntry(entry);
|
|
331
|
+
}
|
|
332
|
+
undo() {
|
|
333
|
+
const entry = this.undoStack.pop();
|
|
334
|
+
if (!entry) return null;
|
|
335
|
+
entry.inverse();
|
|
336
|
+
this.redoStack.push(entry);
|
|
337
|
+
this.onChange?.();
|
|
338
|
+
return entry.label;
|
|
339
|
+
}
|
|
340
|
+
redo() {
|
|
341
|
+
const entry = this.redoStack.pop();
|
|
342
|
+
if (!entry) return null;
|
|
343
|
+
entry.forward();
|
|
344
|
+
this.undoStack.push(entry);
|
|
345
|
+
this.onChange?.();
|
|
346
|
+
return entry.label;
|
|
347
|
+
}
|
|
348
|
+
beginBatch(label, coalesceKey) {
|
|
349
|
+
this.batches.push({
|
|
350
|
+
label,
|
|
351
|
+
entries: [],
|
|
352
|
+
coalesceKey
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
commitBatch() {
|
|
356
|
+
const batch = this.batches.pop();
|
|
357
|
+
if (!batch || batch.entries.length === 0) return;
|
|
358
|
+
const entry = this.createBatchEntry(batch);
|
|
359
|
+
const parentBatch = this.currentBatch;
|
|
360
|
+
if (parentBatch) parentBatch.entries.push(entry);
|
|
361
|
+
else this.pushUndoEntry(entry);
|
|
362
|
+
}
|
|
363
|
+
runBatch(label, fn, coalesceKey) {
|
|
364
|
+
this.beginBatch(label, coalesceKey);
|
|
365
|
+
try {
|
|
366
|
+
const result = fn();
|
|
367
|
+
this.commitBatch();
|
|
368
|
+
return result;
|
|
369
|
+
} catch (error) {
|
|
370
|
+
this.rollbackBatch();
|
|
371
|
+
throw error;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
rollbackBatch() {
|
|
375
|
+
const batch = this.batches.pop();
|
|
376
|
+
if (!batch) return;
|
|
377
|
+
for (const entry of batch.entries.toReversed()) entry.inverse();
|
|
378
|
+
}
|
|
379
|
+
/** Abandon provisional history without replaying it or changing committed undo/redo entries. */
|
|
380
|
+
discardBatches() {
|
|
381
|
+
this.batches = [];
|
|
382
|
+
}
|
|
383
|
+
clear() {
|
|
384
|
+
this.undoStack = [];
|
|
385
|
+
this.redoStack = [];
|
|
386
|
+
this.discardBatches();
|
|
387
|
+
this.onChange?.();
|
|
388
|
+
}
|
|
389
|
+
get isBatching() {
|
|
390
|
+
return this.batches.length > 0;
|
|
391
|
+
}
|
|
392
|
+
get canUndo() {
|
|
393
|
+
return this.undoStack.length > 0;
|
|
394
|
+
}
|
|
395
|
+
get canRedo() {
|
|
396
|
+
return this.redoStack.length > 0;
|
|
397
|
+
}
|
|
398
|
+
get undoLabel() {
|
|
399
|
+
return this.undoStack.at(-1)?.label ?? null;
|
|
400
|
+
}
|
|
401
|
+
get redoLabel() {
|
|
402
|
+
return this.redoStack.at(-1)?.label ?? null;
|
|
403
|
+
}
|
|
404
|
+
get currentBatch() {
|
|
405
|
+
return this.batches.at(-1) ?? null;
|
|
406
|
+
}
|
|
407
|
+
createBatchEntry(batch) {
|
|
408
|
+
return {
|
|
409
|
+
label: batch.label,
|
|
410
|
+
forward: () => batch.entries.forEach((entry) => entry.forward()),
|
|
411
|
+
inverse: () => batch.entries.toReversed().forEach((entry) => entry.inverse()),
|
|
412
|
+
coalesceKey: batch.coalesceKey
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
pushUndoEntry(entry) {
|
|
416
|
+
const previous = this.undoStack.at(-1);
|
|
417
|
+
if (entry.coalesceKey && previous?.coalesceKey === entry.coalesceKey) this.undoStack[this.undoStack.length - 1] = {
|
|
418
|
+
...entry,
|
|
419
|
+
inverse: previous.inverse
|
|
420
|
+
};
|
|
421
|
+
else this.undoStack.push(entry);
|
|
422
|
+
this.redoStack = [];
|
|
423
|
+
this.trimUndoStack();
|
|
424
|
+
this.onChange?.();
|
|
425
|
+
}
|
|
426
|
+
trimUndoStack() {
|
|
427
|
+
if (!Number.isFinite(this.limit) || this.limit <= 0) return;
|
|
428
|
+
const overflow = this.undoStack.length - this.limit;
|
|
429
|
+
if (overflow > 0) this.undoStack.splice(0, overflow);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region src/text-picture.ts
|
|
434
|
+
/**
|
|
435
|
+
* Invalidate cached Skia textPicture (Paragraph snapshot). Includes width/height
|
|
436
|
+
* because wrapping/layout depends on the box.
|
|
437
|
+
*/
|
|
438
|
+
const TEXT_PICTURE_KEYS = /* @__PURE__ */ new Set([
|
|
439
|
+
"text",
|
|
440
|
+
"fontSize",
|
|
441
|
+
"fontFamily",
|
|
442
|
+
"fontWeight",
|
|
443
|
+
"italic",
|
|
444
|
+
"textAlignHorizontal",
|
|
445
|
+
"textDirection",
|
|
446
|
+
"textAlignVertical",
|
|
447
|
+
"lineHeight",
|
|
448
|
+
"letterSpacing",
|
|
449
|
+
"textDecoration",
|
|
450
|
+
"textCase",
|
|
451
|
+
"styleRuns",
|
|
452
|
+
"fills",
|
|
453
|
+
"width",
|
|
454
|
+
"height"
|
|
455
|
+
]);
|
|
456
|
+
const GLYPH_AFFECTING_KEYS = /* @__PURE__ */ new Set([
|
|
457
|
+
"text",
|
|
458
|
+
"fontSize",
|
|
459
|
+
"fontFamily",
|
|
460
|
+
"fontWeight",
|
|
461
|
+
"italic",
|
|
462
|
+
"textDirection",
|
|
463
|
+
"lineHeight",
|
|
464
|
+
"letterSpacing",
|
|
465
|
+
"textCase",
|
|
466
|
+
"styleRuns"
|
|
467
|
+
]);
|
|
468
|
+
/**
|
|
469
|
+
* Shared by SceneGraph.updateNode and updateNodePreview (drag hot path) so the
|
|
470
|
+
* two invalidation rules cannot drift. Glyphs are kept when the caller
|
|
471
|
+
* replaces them in the same update (resize supplies scaled copies).
|
|
472
|
+
*/
|
|
473
|
+
function textCacheInvalidationChanges(node, changes) {
|
|
474
|
+
const invalidated = {};
|
|
475
|
+
const keys = Object.keys(changes);
|
|
476
|
+
if (node.textPicture && keys.some((key) => TEXT_PICTURE_KEYS.has(key))) invalidated.textPicture = null;
|
|
477
|
+
const glyphsInvalidated = keys.some((key) => GLYPH_AFFECTING_KEYS.has(key));
|
|
478
|
+
if (node.derivedTextGlyphs && glyphsInvalidated && !changes.derivedTextGlyphs) {
|
|
479
|
+
invalidated.derivedTextGlyphs = null;
|
|
480
|
+
invalidated.textPathData = null;
|
|
481
|
+
}
|
|
482
|
+
return invalidated;
|
|
483
|
+
}
|
|
484
|
+
function invalidateTextCaches(node, changes) {
|
|
485
|
+
Object.assign(node, textCacheInvalidationChanges(node, changes));
|
|
486
|
+
}
|
|
487
|
+
//#endregion
|
|
488
|
+
//#region src/preview.ts
|
|
489
|
+
const LAYOUT_AFFECTING_KEYS = /* @__PURE__ */ new Set([
|
|
490
|
+
"x",
|
|
491
|
+
"y",
|
|
492
|
+
"width",
|
|
493
|
+
"height",
|
|
494
|
+
"rotation",
|
|
495
|
+
"flipX",
|
|
496
|
+
"flipY",
|
|
497
|
+
"parentId",
|
|
498
|
+
"childIds",
|
|
499
|
+
"layoutMode",
|
|
500
|
+
"layoutDirection",
|
|
501
|
+
"layoutWrap",
|
|
502
|
+
"primaryAxisSizing",
|
|
503
|
+
"counterAxisSizing",
|
|
504
|
+
"itemSpacing",
|
|
505
|
+
"counterAxisSpacing",
|
|
506
|
+
"paddingTop",
|
|
507
|
+
"paddingRight",
|
|
508
|
+
"paddingBottom",
|
|
509
|
+
"paddingLeft",
|
|
510
|
+
"layoutGrow",
|
|
511
|
+
"layoutAlignSelf",
|
|
512
|
+
"layoutPositioning",
|
|
513
|
+
"minWidth",
|
|
514
|
+
"maxWidth",
|
|
515
|
+
"minHeight",
|
|
516
|
+
"maxHeight",
|
|
517
|
+
"visible",
|
|
518
|
+
"text",
|
|
519
|
+
"fontSize",
|
|
520
|
+
"lineHeight",
|
|
521
|
+
"letterSpacing",
|
|
522
|
+
"styleRuns",
|
|
523
|
+
"textAutoResize"
|
|
524
|
+
]);
|
|
525
|
+
function updateNodePreview(graph, id, changes, beforeUpdate) {
|
|
526
|
+
const node = graph.nodes.get(id);
|
|
527
|
+
if (!node) return null;
|
|
528
|
+
changes = Object.fromEntries(Object.entries(changes).filter(([, value]) => value !== void 0));
|
|
529
|
+
if (Object.keys(changes).every((key) => node[key] === changes[key])) return null;
|
|
530
|
+
if (Object.keys(changes).some((key) => LAYOUT_AFFECTING_KEYS.has(key))) graph.clearAbsPosCache();
|
|
531
|
+
let normalizedChanges = changes;
|
|
532
|
+
if (node.type === "TEXT") normalizedChanges = {
|
|
533
|
+
...textCacheInvalidationChanges(node, changes),
|
|
534
|
+
...changes
|
|
535
|
+
};
|
|
536
|
+
if (changes.vectorNetwork) normalizedChanges = {
|
|
537
|
+
...normalizedChanges,
|
|
538
|
+
vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork)
|
|
539
|
+
};
|
|
540
|
+
beforeUpdate?.(node, normalizedChanges);
|
|
541
|
+
graph.positionPreviewVersion++;
|
|
542
|
+
Object.assign(node, normalizedChanges);
|
|
543
|
+
return normalizedChanges;
|
|
544
|
+
}
|
|
545
|
+
//#endregion
|
|
546
|
+
//#region src/source-metadata.ts
|
|
547
|
+
function markSourceFieldsEdited(node, changeKeys) {
|
|
548
|
+
if (changeKeys.length === 0) return;
|
|
549
|
+
const editedFields = new Set(node.source.editedFields);
|
|
550
|
+
for (const key of changeKeys) editedFields.add(key);
|
|
551
|
+
node.source.editedFields = [...editedFields];
|
|
552
|
+
}
|
|
553
|
+
//#endregion
|
|
554
|
+
//#region src/variables.ts
|
|
555
|
+
function addVariable(graph, variable) {
|
|
556
|
+
graph.variables.set(variable.id, variable);
|
|
557
|
+
const collection = graph.variableCollections.get(variable.collectionId);
|
|
558
|
+
if (collection && !collection.variableIds.includes(variable.id)) collection.variableIds.push(variable.id);
|
|
559
|
+
}
|
|
560
|
+
function removeVariable(graph, id) {
|
|
561
|
+
const variable = graph.variables.get(id);
|
|
562
|
+
if (!variable) return;
|
|
563
|
+
graph.variables.delete(id);
|
|
564
|
+
const collection = graph.variableCollections.get(variable.collectionId);
|
|
565
|
+
if (collection) collection.variableIds = collection.variableIds.filter((vid) => vid !== id);
|
|
566
|
+
for (const node of graph.nodes.values()) {
|
|
567
|
+
if (!Object.values(node.boundVariables).includes(id)) continue;
|
|
568
|
+
node.boundVariables = omitBy(node.boundVariables, (varId) => varId === id);
|
|
569
|
+
graph.emitter.emit("node:updated", node.id, { boundVariables: { ...node.boundVariables } });
|
|
570
|
+
markBoundVariablesOverrideOnInstance(graph, node.id);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
function addCollection(graph, collection) {
|
|
574
|
+
graph.variableCollections.set(collection.id, collection);
|
|
575
|
+
if (!graph.activeMode.has(collection.id)) graph.activeMode.set(collection.id, collection.defaultModeId);
|
|
576
|
+
}
|
|
577
|
+
function defaultVariableValue(type, value) {
|
|
578
|
+
if (value !== void 0) return value;
|
|
579
|
+
if (type === "COLOR") return { ...BLACK };
|
|
580
|
+
if (type === "FLOAT") return 0;
|
|
581
|
+
if (type === "BOOLEAN") return false;
|
|
582
|
+
return "";
|
|
583
|
+
}
|
|
584
|
+
function createVariable(graph, generateId, name, type, collectionId, value) {
|
|
585
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
586
|
+
if (!collection) throw new Error(`Collection "${collectionId}" not found`);
|
|
587
|
+
const id = generateId();
|
|
588
|
+
const defaultValue = defaultVariableValue(type, value);
|
|
589
|
+
const valuesByMode = {};
|
|
590
|
+
for (const mode of collection.modes) valuesByMode[mode.modeId] = structuredClone(defaultValue);
|
|
591
|
+
const variable = {
|
|
592
|
+
id,
|
|
593
|
+
name,
|
|
594
|
+
type,
|
|
595
|
+
collectionId,
|
|
596
|
+
valuesByMode,
|
|
597
|
+
description: "",
|
|
598
|
+
hiddenFromPublishing: false
|
|
599
|
+
};
|
|
600
|
+
addVariable(graph, variable);
|
|
601
|
+
return variable;
|
|
602
|
+
}
|
|
603
|
+
function createCollection(graph, generateId, name) {
|
|
604
|
+
const id = generateId();
|
|
605
|
+
const modeId = generateId();
|
|
606
|
+
const collection = {
|
|
607
|
+
id,
|
|
608
|
+
name,
|
|
609
|
+
modes: [{
|
|
610
|
+
modeId,
|
|
611
|
+
name: "Mode 1"
|
|
612
|
+
}],
|
|
613
|
+
defaultModeId: modeId,
|
|
614
|
+
variableIds: []
|
|
615
|
+
};
|
|
616
|
+
addCollection(graph, collection);
|
|
617
|
+
return collection;
|
|
618
|
+
}
|
|
619
|
+
function removeCollection(graph, id) {
|
|
620
|
+
const collection = graph.variableCollections.get(id);
|
|
621
|
+
if (collection) for (const varId of Array.from(collection.variableIds)) removeVariable(graph, varId);
|
|
622
|
+
graph.variableCollections.delete(id);
|
|
623
|
+
graph.activeMode.delete(id);
|
|
624
|
+
}
|
|
625
|
+
function getActiveModeId(graph, collectionId) {
|
|
626
|
+
const mode = graph.activeMode.get(collectionId);
|
|
627
|
+
if (mode) return mode;
|
|
628
|
+
return graph.variableCollections.get(collectionId)?.defaultModeId ?? "";
|
|
629
|
+
}
|
|
630
|
+
function getNodeVariableModeId(graph, nodeId, collectionId) {
|
|
631
|
+
let node = graph.nodes.get(nodeId);
|
|
632
|
+
while (node) {
|
|
633
|
+
const modeId = node.variableModes[collectionId];
|
|
634
|
+
if (modeId) return modeId;
|
|
635
|
+
node = node.parentId ? graph.nodes.get(node.parentId) : void 0;
|
|
636
|
+
}
|
|
637
|
+
return getActiveModeId(graph, collectionId);
|
|
638
|
+
}
|
|
639
|
+
function setActiveMode(graph, collectionId, modeId) {
|
|
640
|
+
graph.activeMode.set(collectionId, modeId);
|
|
641
|
+
}
|
|
642
|
+
function addMode(graph, collectionId, modeId, name, sourceMode) {
|
|
643
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
644
|
+
if (!collection) return;
|
|
645
|
+
collection.modes.push({
|
|
646
|
+
modeId,
|
|
647
|
+
name
|
|
648
|
+
});
|
|
649
|
+
const sourceModeId = sourceMode ?? collection.defaultModeId;
|
|
650
|
+
for (const varId of collection.variableIds) {
|
|
651
|
+
const variable = graph.variables.get(varId);
|
|
652
|
+
if (!variable) continue;
|
|
653
|
+
variable.valuesByMode[modeId] = structuredClone(variable.valuesByMode[sourceModeId] ?? Object.values(variable.valuesByMode)[0]);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function removeMode(graph, collectionId, modeId) {
|
|
657
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
658
|
+
if (!collection || collection.modes.length <= 1) return;
|
|
659
|
+
collection.modes = collection.modes.filter((m) => m.modeId !== modeId);
|
|
660
|
+
if (collection.defaultModeId === modeId) collection.defaultModeId = collection.modes[0].modeId;
|
|
661
|
+
for (const varId of collection.variableIds) {
|
|
662
|
+
const variable = graph.variables.get(varId);
|
|
663
|
+
if (variable) variable.valuesByMode = omit(variable.valuesByMode, [modeId]);
|
|
664
|
+
}
|
|
665
|
+
if (graph.activeMode.get(collectionId) === modeId) graph.activeMode.set(collectionId, collection.defaultModeId);
|
|
666
|
+
}
|
|
667
|
+
function renameMode(graph, collectionId, modeId, name) {
|
|
668
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
669
|
+
if (!collection) return;
|
|
670
|
+
const mode = collection.modes.find((m) => m.modeId === modeId);
|
|
671
|
+
if (mode) mode.name = name;
|
|
672
|
+
}
|
|
673
|
+
function setDefaultMode(graph, collectionId, modeId) {
|
|
674
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
675
|
+
if (!collection) return;
|
|
676
|
+
if (!collection.modes.some((m) => m.modeId === modeId)) return;
|
|
677
|
+
collection.defaultModeId = modeId;
|
|
678
|
+
}
|
|
679
|
+
function resolveVariable(graph, variableId, modeId, visited) {
|
|
680
|
+
if (visited?.has(variableId)) return void 0;
|
|
681
|
+
const variable = graph.variables.get(variableId);
|
|
682
|
+
if (!variable) return void 0;
|
|
683
|
+
const collection = graph.variableCollections.get(variable.collectionId);
|
|
684
|
+
const preferredModeId = modeId ?? getActiveModeId(graph, variable.collectionId);
|
|
685
|
+
const fallbackModeId = collection?.defaultModeId;
|
|
686
|
+
let value = Object.hasOwn(variable.valuesByMode, preferredModeId) ? variable.valuesByMode[preferredModeId] : void 0;
|
|
687
|
+
if (value === void 0 && fallbackModeId && Object.hasOwn(variable.valuesByMode, fallbackModeId)) value = variable.valuesByMode[fallbackModeId];
|
|
688
|
+
value ??= Object.values(variable.valuesByMode)[0];
|
|
689
|
+
if (value && typeof value === "object" && "aliasId" in value) {
|
|
690
|
+
const seen = visited ?? /* @__PURE__ */ new Set();
|
|
691
|
+
seen.add(variableId);
|
|
692
|
+
return resolveVariable(graph, value.aliasId, preferredModeId, seen);
|
|
693
|
+
}
|
|
694
|
+
return value;
|
|
695
|
+
}
|
|
696
|
+
function resolveColorVariable(graph, variableId) {
|
|
697
|
+
const value = resolveVariable(graph, variableId);
|
|
698
|
+
if (value && typeof value === "object" && "r" in value) return value;
|
|
699
|
+
}
|
|
700
|
+
function resolveNumberVariable(graph, variableId) {
|
|
701
|
+
const value = resolveVariable(graph, variableId);
|
|
702
|
+
return typeof value === "number" ? value : void 0;
|
|
703
|
+
}
|
|
704
|
+
function resolveColorVariableForNode(graph, nodeId, variableId) {
|
|
705
|
+
const variable = graph.variables.get(variableId);
|
|
706
|
+
if (!variable) return void 0;
|
|
707
|
+
const value = resolveVariable(graph, variableId, getNodeVariableModeId(graph, nodeId, variable.collectionId));
|
|
708
|
+
if (value && typeof value === "object" && "r" in value) return value;
|
|
709
|
+
}
|
|
710
|
+
function resolveNumberVariableForNode(graph, nodeId, variableId) {
|
|
711
|
+
const variable = graph.variables.get(variableId);
|
|
712
|
+
if (!variable) return void 0;
|
|
713
|
+
const value = resolveVariable(graph, variableId, getNodeVariableModeId(graph, nodeId, variable.collectionId));
|
|
714
|
+
return typeof value === "number" ? value : void 0;
|
|
715
|
+
}
|
|
716
|
+
function getVariablesForCollection(graph, collectionId) {
|
|
717
|
+
const collection = graph.variableCollections.get(collectionId);
|
|
718
|
+
if (!collection) return [];
|
|
719
|
+
return collection.variableIds.map((id) => graph.variables.get(id)).filter((v) => v !== void 0);
|
|
720
|
+
}
|
|
721
|
+
function getVariablesByType(graph, type) {
|
|
722
|
+
return [...graph.variables.values()].filter((v) => v.type === type);
|
|
723
|
+
}
|
|
724
|
+
const SCALAR_BINDING_FIELDS = /* @__PURE__ */ new Set([
|
|
725
|
+
"opacity",
|
|
726
|
+
"width",
|
|
727
|
+
"height",
|
|
728
|
+
"cornerRadius",
|
|
729
|
+
"fontSize",
|
|
730
|
+
"letterSpacing",
|
|
731
|
+
"lineHeight",
|
|
732
|
+
"itemSpacing",
|
|
733
|
+
"strokeWeight",
|
|
734
|
+
"paddingLeft",
|
|
735
|
+
"paddingRight",
|
|
736
|
+
"paddingTop",
|
|
737
|
+
"paddingBottom",
|
|
738
|
+
"counterAxisSpacing",
|
|
739
|
+
"topLeftRadius",
|
|
740
|
+
"topRightRadius",
|
|
741
|
+
"bottomLeftRadius",
|
|
742
|
+
"bottomRightRadius",
|
|
743
|
+
"rotation",
|
|
744
|
+
"x",
|
|
745
|
+
"y",
|
|
746
|
+
"minWidth",
|
|
747
|
+
"maxWidth",
|
|
748
|
+
"minHeight",
|
|
749
|
+
"maxHeight",
|
|
750
|
+
"borderTopWeight",
|
|
751
|
+
"borderBottomWeight",
|
|
752
|
+
"borderLeftWeight",
|
|
753
|
+
"borderRightWeight",
|
|
754
|
+
"gridRowGap",
|
|
755
|
+
"gridColumnGap"
|
|
756
|
+
]);
|
|
757
|
+
const STRING_BINDING_FIELDS = /* @__PURE__ */ new Set(["fontFamily"]);
|
|
758
|
+
const BOOLEAN_BINDING_FIELDS = /* @__PURE__ */ new Set(["visible"]);
|
|
759
|
+
function bindVariable(graph, nodeId, field, variableId) {
|
|
760
|
+
const node = graph.nodes.get(nodeId);
|
|
761
|
+
if (!node) return;
|
|
762
|
+
const variable = graph.variables.get(variableId);
|
|
763
|
+
if (!variable) throw new Error(`Variable "${variableId}" not found`);
|
|
764
|
+
const colorFieldMatch = field.match(/^(fills|strokes)\/(\d+)\/color$/);
|
|
765
|
+
if (colorFieldMatch) {
|
|
766
|
+
if (variable.type !== "COLOR") throw new Error(`Cannot bind ${variable.type} variable to color field "${field}"`);
|
|
767
|
+
const arrayKey = colorFieldMatch[1];
|
|
768
|
+
const index = Number.parseInt(colorFieldMatch[2], 10);
|
|
769
|
+
const currentLength = node[arrayKey]?.length ?? 0;
|
|
770
|
+
if (index >= currentLength) throw new Error(`Index ${index} out of range for ${arrayKey} (length ${currentLength})`);
|
|
771
|
+
const topLevelKey = colorFieldMatch[1];
|
|
772
|
+
if (topLevelKey in node.boundVariables) node.boundVariables = omit(node.boundVariables, [topLevelKey]);
|
|
773
|
+
}
|
|
774
|
+
if (SCALAR_BINDING_FIELDS.has(field) && variable.type !== "FLOAT") throw new Error(`Cannot bind ${variable.type} variable to scalar field "${field}"`);
|
|
775
|
+
if (STRING_BINDING_FIELDS.has(field) && variable.type !== "STRING") throw new Error(`Cannot bind ${variable.type} variable to string field "${field}"`);
|
|
776
|
+
if (BOOLEAN_BINDING_FIELDS.has(field) && variable.type !== "BOOLEAN") throw new Error(`Cannot bind ${variable.type} variable to boolean field "${field}"`);
|
|
777
|
+
if (!(SCALAR_BINDING_FIELDS.has(field) || STRING_BINDING_FIELDS.has(field) || BOOLEAN_BINDING_FIELDS.has(field) || colorFieldMatch)) throw new Error(`Unknown binding field "${field}"`);
|
|
778
|
+
node.boundVariables = {
|
|
779
|
+
...node.boundVariables,
|
|
780
|
+
[field]: variableId
|
|
781
|
+
};
|
|
782
|
+
graph.emitter.emit("node:updated", nodeId, { boundVariables: { ...node.boundVariables } });
|
|
783
|
+
markBoundVariablesOverrideOnInstance(graph, nodeId);
|
|
784
|
+
}
|
|
785
|
+
function unbindVariable(graph, nodeId, field) {
|
|
786
|
+
const node = graph.nodes.get(nodeId);
|
|
787
|
+
if (!node) return;
|
|
788
|
+
if (!(field in node.boundVariables)) return;
|
|
789
|
+
node.boundVariables = omit(node.boundVariables, [field]);
|
|
790
|
+
graph.emitter.emit("node:updated", nodeId, { boundVariables: { ...node.boundVariables } });
|
|
791
|
+
markBoundVariablesOverrideOnInstance(graph, nodeId);
|
|
792
|
+
}
|
|
793
|
+
function markBoundVariablesOverrideOnInstance(graph, nodeId) {
|
|
794
|
+
const node = graph.nodes.get(nodeId);
|
|
795
|
+
if (!node) return;
|
|
796
|
+
if (node.type === "INSTANCE") {
|
|
797
|
+
setInstanceOverride(node.instanceOverrides, node.id, node.id, "boundVariables");
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
let current = node;
|
|
801
|
+
while (current.parentId) {
|
|
802
|
+
const parent = graph.nodes.get(current.parentId);
|
|
803
|
+
if (!parent) break;
|
|
804
|
+
if (parent.type === "INSTANCE") {
|
|
805
|
+
setInstanceOverride(parent.instanceOverrides, parent.id, nodeId, "boundVariables");
|
|
806
|
+
break;
|
|
807
|
+
}
|
|
808
|
+
current = parent;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
//#endregion
|
|
812
|
+
//#region src/index.ts
|
|
813
|
+
let nextLocalID = 1;
|
|
814
|
+
function generateId() {
|
|
815
|
+
return `0:${nextLocalID++}`;
|
|
816
|
+
}
|
|
817
|
+
function stripUndefinedProps(obj) {
|
|
818
|
+
const result = {};
|
|
819
|
+
for (const key of Object.keys(obj)) {
|
|
820
|
+
const val = obj[key];
|
|
821
|
+
if (val !== void 0) result[key] = val;
|
|
822
|
+
}
|
|
823
|
+
return result;
|
|
824
|
+
}
|
|
825
|
+
var SceneGraph = class SceneGraph {
|
|
826
|
+
nodes = /* @__PURE__ */ new Map();
|
|
827
|
+
images = /* @__PURE__ */ new Map();
|
|
828
|
+
variables = /* @__PURE__ */ new Map();
|
|
829
|
+
variableCollections = /* @__PURE__ */ new Map();
|
|
830
|
+
activeMode = /* @__PURE__ */ new Map();
|
|
831
|
+
rootId;
|
|
832
|
+
figKiwiVersion = null;
|
|
833
|
+
/** Deflated kiwi schema bytes from the original .fig file, preserved for roundtrip fidelity. */
|
|
834
|
+
figSchemaDeflated = null;
|
|
835
|
+
documentColorSpace = "display-p3";
|
|
836
|
+
enabledLibraries = /* @__PURE__ */ new Map();
|
|
837
|
+
emitter = createNanoEvents();
|
|
838
|
+
absPosCache = /* @__PURE__ */ new Map();
|
|
839
|
+
previewMutationDepth = 0;
|
|
840
|
+
previewObservers = [];
|
|
841
|
+
sourceMetadataPreservationDepth = 0;
|
|
842
|
+
layoutMutationDepth = 0;
|
|
843
|
+
positionPreviewVersion = 0;
|
|
844
|
+
instanceIndex = /* @__PURE__ */ new Map();
|
|
845
|
+
constructor() {
|
|
846
|
+
const root = createDefaultNode(generateId, "FRAME", {
|
|
847
|
+
name: "Document",
|
|
848
|
+
width: 0,
|
|
849
|
+
height: 0
|
|
850
|
+
});
|
|
851
|
+
this.rootId = root.id;
|
|
852
|
+
this.nodes.set(root.id, root);
|
|
853
|
+
this.addPage("Page 1");
|
|
854
|
+
}
|
|
855
|
+
addPage(name) {
|
|
856
|
+
return this.createNode("CANVAS", this.rootId, {
|
|
857
|
+
name,
|
|
858
|
+
width: 0,
|
|
859
|
+
height: 0
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
getPages(includeInternal = false) {
|
|
863
|
+
return this.getChildren(this.rootId).filter((n) => n.type === "CANVAS" && (includeInternal || !n.internalOnly));
|
|
864
|
+
}
|
|
865
|
+
getAllNodes() {
|
|
866
|
+
return this.nodes.values();
|
|
867
|
+
}
|
|
868
|
+
getNode(id) {
|
|
869
|
+
return this.nodes.get(id);
|
|
870
|
+
}
|
|
871
|
+
onNodeEvents(handlers) {
|
|
872
|
+
return bindNodeEvents(this.emitter, handlers);
|
|
873
|
+
}
|
|
874
|
+
countDescendants(nodeId) {
|
|
875
|
+
const node = this.nodes.get(nodeId);
|
|
876
|
+
if (!node) return 0;
|
|
877
|
+
let count = 0;
|
|
878
|
+
const stack = [...node.childIds];
|
|
879
|
+
while (stack.length > 0) {
|
|
880
|
+
const id = stack.pop();
|
|
881
|
+
if (id === void 0) break;
|
|
882
|
+
count++;
|
|
883
|
+
const child = this.nodes.get(id);
|
|
884
|
+
if (child) for (const childId of child.childIds) stack.push(childId);
|
|
885
|
+
}
|
|
886
|
+
return count;
|
|
887
|
+
}
|
|
888
|
+
addVariable(variable) {
|
|
889
|
+
addVariable(this, variable);
|
|
890
|
+
}
|
|
891
|
+
removeVariable(id) {
|
|
892
|
+
removeVariable(this, id);
|
|
893
|
+
}
|
|
894
|
+
addCollection(collection) {
|
|
895
|
+
addCollection(this, collection);
|
|
896
|
+
}
|
|
897
|
+
createVariable(name, type, collectionId, value) {
|
|
898
|
+
return createVariable(this, generateId, name, type, collectionId, value);
|
|
899
|
+
}
|
|
900
|
+
createCollection(name) {
|
|
901
|
+
return createCollection(this, generateId, name);
|
|
902
|
+
}
|
|
903
|
+
removeCollection(id) {
|
|
904
|
+
removeCollection(this, id);
|
|
905
|
+
}
|
|
906
|
+
getActiveModeId(collectionId) {
|
|
907
|
+
return getActiveModeId(this, collectionId);
|
|
908
|
+
}
|
|
909
|
+
getNodeVariableModeId(nodeId, collectionId) {
|
|
910
|
+
return getNodeVariableModeId(this, nodeId, collectionId);
|
|
911
|
+
}
|
|
912
|
+
setActiveMode(collectionId, modeId) {
|
|
913
|
+
setActiveMode(this, collectionId, modeId);
|
|
914
|
+
}
|
|
915
|
+
addMode(collectionId, modeId, name, sourceMode) {
|
|
916
|
+
addMode(this, collectionId, modeId, name, sourceMode);
|
|
917
|
+
}
|
|
918
|
+
removeMode(collectionId, modeId) {
|
|
919
|
+
removeMode(this, collectionId, modeId);
|
|
920
|
+
}
|
|
921
|
+
renameMode(collectionId, modeId, name) {
|
|
922
|
+
renameMode(this, collectionId, modeId, name);
|
|
923
|
+
}
|
|
924
|
+
setDefaultMode(collectionId, modeId) {
|
|
925
|
+
setDefaultMode(this, collectionId, modeId);
|
|
926
|
+
}
|
|
927
|
+
resolveVariable(variableId, modeId, visited) {
|
|
928
|
+
return resolveVariable(this, variableId, modeId, visited);
|
|
929
|
+
}
|
|
930
|
+
resolveColorVariable(variableId) {
|
|
931
|
+
return resolveColorVariable(this, variableId);
|
|
932
|
+
}
|
|
933
|
+
resolveNumberVariable(variableId) {
|
|
934
|
+
return resolveNumberVariable(this, variableId);
|
|
935
|
+
}
|
|
936
|
+
resolveColorVariableForNode(nodeId, variableId) {
|
|
937
|
+
return resolveColorVariableForNode(this, nodeId, variableId);
|
|
938
|
+
}
|
|
939
|
+
resolveNumberVariableForNode(nodeId, variableId) {
|
|
940
|
+
return resolveNumberVariableForNode(this, nodeId, variableId);
|
|
941
|
+
}
|
|
942
|
+
getVariablesForCollection(collectionId) {
|
|
943
|
+
return getVariablesForCollection(this, collectionId);
|
|
944
|
+
}
|
|
945
|
+
getVariablesByType(type) {
|
|
946
|
+
return getVariablesByType(this, type);
|
|
947
|
+
}
|
|
948
|
+
bindVariable(nodeId, field, variableId) {
|
|
949
|
+
bindVariable(this, nodeId, field, variableId);
|
|
950
|
+
}
|
|
951
|
+
unbindVariable(nodeId, field) {
|
|
952
|
+
unbindVariable(this, nodeId, field);
|
|
953
|
+
}
|
|
954
|
+
getChildren(id) {
|
|
955
|
+
const node = this.nodes.get(id);
|
|
956
|
+
if (!node) return [];
|
|
957
|
+
return node.childIds.map((cid) => this.nodes.get(cid)).filter((n) => n !== void 0);
|
|
958
|
+
}
|
|
959
|
+
isContainer(id) {
|
|
960
|
+
const node = this.nodes.get(id);
|
|
961
|
+
return node ? CONTAINER_TYPES.has(node.type) : false;
|
|
962
|
+
}
|
|
963
|
+
isDescendant(childId, ancestorId) {
|
|
964
|
+
let current = this.nodes.get(childId);
|
|
965
|
+
while (current) {
|
|
966
|
+
if (current.id === ancestorId) return true;
|
|
967
|
+
current = current.parentId ? this.nodes.get(current.parentId) : void 0;
|
|
968
|
+
}
|
|
969
|
+
return false;
|
|
970
|
+
}
|
|
971
|
+
clearAbsPosCache() {
|
|
972
|
+
this.absPosCache.clear();
|
|
973
|
+
}
|
|
974
|
+
getAbsolutePosition(id) {
|
|
975
|
+
const cached = this.absPosCache.get(id);
|
|
976
|
+
if (cached) return cached;
|
|
977
|
+
const node = this.getNode(id);
|
|
978
|
+
if (!node) return {
|
|
979
|
+
x: 0,
|
|
980
|
+
y: 0
|
|
981
|
+
};
|
|
982
|
+
const result = getAbsolutePosition(node, this);
|
|
983
|
+
this.absPosCache.set(id, result);
|
|
984
|
+
return result;
|
|
985
|
+
}
|
|
986
|
+
getAbsoluteBounds(id) {
|
|
987
|
+
const pos = this.getAbsolutePosition(id);
|
|
988
|
+
const node = this.nodes.get(id);
|
|
989
|
+
return {
|
|
990
|
+
x: pos.x,
|
|
991
|
+
y: pos.y,
|
|
992
|
+
width: node?.width ?? 0,
|
|
993
|
+
height: node?.height ?? 0
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
generateNodeId() {
|
|
997
|
+
let id = generateId();
|
|
998
|
+
while (this.nodes.has(id)) id = generateId();
|
|
999
|
+
return id;
|
|
1000
|
+
}
|
|
1001
|
+
registerNode(node, parentId) {
|
|
1002
|
+
node.parentId = parentId;
|
|
1003
|
+
this.nodes.set(node.id, node);
|
|
1004
|
+
if (node.type === "INSTANCE" && node.componentId) {
|
|
1005
|
+
let set = this.instanceIndex.get(node.componentId);
|
|
1006
|
+
if (!set) {
|
|
1007
|
+
set = /* @__PURE__ */ new Set();
|
|
1008
|
+
this.instanceIndex.set(node.componentId, set);
|
|
1009
|
+
}
|
|
1010
|
+
set.add(node.id);
|
|
1011
|
+
}
|
|
1012
|
+
this.emitter.emit("node:created", node);
|
|
1013
|
+
return node;
|
|
1014
|
+
}
|
|
1015
|
+
createNode(type, parentId, overrides = {}) {
|
|
1016
|
+
const node = createDefaultNode(() => this.generateNodeId(), type, overrides);
|
|
1017
|
+
this.nodes.get(parentId)?.childIds.push(node.id);
|
|
1018
|
+
return this.registerNode(node, parentId);
|
|
1019
|
+
}
|
|
1020
|
+
createNodeWithId(id, type, parentId, overrides = {}) {
|
|
1021
|
+
const node = createDefaultNode(() => id, type, overrides);
|
|
1022
|
+
node.id = id;
|
|
1023
|
+
const parent = parentId ? this.nodes.get(parentId) : void 0;
|
|
1024
|
+
if (parent && !parent.childIds.includes(id)) parent.childIds.push(id);
|
|
1025
|
+
return this.registerNode(node, parentId);
|
|
1026
|
+
}
|
|
1027
|
+
static TEXT_PICTURE_KEYS = TEXT_PICTURE_KEYS;
|
|
1028
|
+
static GLYPH_AFFECTING_KEYS = GLYPH_AFFECTING_KEYS;
|
|
1029
|
+
static LAYOUT_AFFECTING_KEYS = /* @__PURE__ */ new Set([
|
|
1030
|
+
"x",
|
|
1031
|
+
"y",
|
|
1032
|
+
"width",
|
|
1033
|
+
"height",
|
|
1034
|
+
"rotation",
|
|
1035
|
+
"flipX",
|
|
1036
|
+
"flipY",
|
|
1037
|
+
"layoutMode",
|
|
1038
|
+
"layoutDirection",
|
|
1039
|
+
"itemSpacing",
|
|
1040
|
+
"counterAxisSpacing",
|
|
1041
|
+
"paddingLeft",
|
|
1042
|
+
"paddingRight",
|
|
1043
|
+
"paddingTop",
|
|
1044
|
+
"paddingBottom",
|
|
1045
|
+
"primaryAxisAlign",
|
|
1046
|
+
"counterAxisAlign",
|
|
1047
|
+
"counterAxisAlignContent",
|
|
1048
|
+
"layoutWrap",
|
|
1049
|
+
"primaryAxisSizing",
|
|
1050
|
+
"counterAxisSizing",
|
|
1051
|
+
"layoutPositioning",
|
|
1052
|
+
"layoutGrow",
|
|
1053
|
+
"layoutAlignSelf",
|
|
1054
|
+
"strokesIncludedInLayout",
|
|
1055
|
+
"horizontalConstraint",
|
|
1056
|
+
"verticalConstraint",
|
|
1057
|
+
"gridTemplateColumns",
|
|
1058
|
+
"gridTemplateRows",
|
|
1059
|
+
"gridColumnGap",
|
|
1060
|
+
"gridRowGap",
|
|
1061
|
+
"gridPosition",
|
|
1062
|
+
"minWidth",
|
|
1063
|
+
"maxWidth",
|
|
1064
|
+
"minHeight",
|
|
1065
|
+
"maxHeight"
|
|
1066
|
+
]);
|
|
1067
|
+
runPreviewUpdates(fn, beforeUpdate) {
|
|
1068
|
+
this.previewMutationDepth++;
|
|
1069
|
+
if (beforeUpdate) this.previewObservers.push(beforeUpdate);
|
|
1070
|
+
try {
|
|
1071
|
+
fn();
|
|
1072
|
+
} finally {
|
|
1073
|
+
if (beforeUpdate) this.previewObservers.pop();
|
|
1074
|
+
this.previewMutationDepth--;
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
preserveSourceMetadataDuring(fn) {
|
|
1078
|
+
this.sourceMetadataPreservationDepth++;
|
|
1079
|
+
try {
|
|
1080
|
+
fn();
|
|
1081
|
+
} finally {
|
|
1082
|
+
this.sourceMetadataPreservationDepth--;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
withLayoutMutations(fn) {
|
|
1086
|
+
this.layoutMutationDepth++;
|
|
1087
|
+
try {
|
|
1088
|
+
fn();
|
|
1089
|
+
} finally {
|
|
1090
|
+
this.layoutMutationDepth--;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
get isApplyingLayout() {
|
|
1094
|
+
return this.layoutMutationDepth > 0;
|
|
1095
|
+
}
|
|
1096
|
+
updateNodePositionPreview(id, x, y) {
|
|
1097
|
+
this.updateNodePreview(id, {
|
|
1098
|
+
x,
|
|
1099
|
+
y
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
updateNodePreview(id, changes) {
|
|
1103
|
+
const appliedChanges = updateNodePreview(this, id, changes, (node, applied) => {
|
|
1104
|
+
for (const observe of this.previewObservers) observe(node, applied);
|
|
1105
|
+
});
|
|
1106
|
+
if (appliedChanges) this.emitter.emit("node:previewUpdated", id, appliedChanges);
|
|
1107
|
+
}
|
|
1108
|
+
updateNode(id, changes) {
|
|
1109
|
+
if (this.previewMutationDepth > 0) {
|
|
1110
|
+
this.updateNodePreview(id, changes);
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
const node = this.nodes.get(id);
|
|
1114
|
+
if (!node) return;
|
|
1115
|
+
changes = stripUndefinedProps(styleDetachmentChanges(node, stripUndefinedProps(changes)));
|
|
1116
|
+
this.applyNodeChanges(node, changes);
|
|
1117
|
+
}
|
|
1118
|
+
/** Replay captured properties without dropping explicit undefined values or absent keys. */
|
|
1119
|
+
restoreNodeProperties(id, changes, absent) {
|
|
1120
|
+
const node = this.nodes.get(id);
|
|
1121
|
+
if (node) this.applyNodeChanges(node, changes, absent);
|
|
1122
|
+
}
|
|
1123
|
+
applyNodeChanges(node, changes, absent = []) {
|
|
1124
|
+
const { id } = node;
|
|
1125
|
+
if (absent.length) {
|
|
1126
|
+
changes = { ...changes };
|
|
1127
|
+
for (const key of absent) Reflect.set(changes, key, void 0);
|
|
1128
|
+
}
|
|
1129
|
+
if (Object.keys(changes).some((k) => SceneGraph.LAYOUT_AFFECTING_KEYS.has(k))) this.absPosCache.clear();
|
|
1130
|
+
if (node.type === "INSTANCE" && "componentId" in changes && changes.componentId !== node.componentId) {
|
|
1131
|
+
if (node.componentId) this.instanceIndex.get(node.componentId)?.delete(id);
|
|
1132
|
+
if (changes.componentId) {
|
|
1133
|
+
let set = this.instanceIndex.get(changes.componentId);
|
|
1134
|
+
if (!set) {
|
|
1135
|
+
set = /* @__PURE__ */ new Set();
|
|
1136
|
+
this.instanceIndex.set(changes.componentId, set);
|
|
1137
|
+
}
|
|
1138
|
+
set.add(id);
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
if (node.type === "TEXT") invalidateTextCaches(node, changes);
|
|
1142
|
+
if (this.sourceMetadataPreservationDepth === 0) markSourceFieldsEdited(node, Object.keys(changes));
|
|
1143
|
+
if (changes.vectorNetwork) changes = {
|
|
1144
|
+
...changes,
|
|
1145
|
+
vectorNetwork: normalizeVectorNetwork(changes.vectorNetwork)
|
|
1146
|
+
};
|
|
1147
|
+
Object.assign(node, changes);
|
|
1148
|
+
if (changes.fills) removeStaleBindings(node, "fills", changes);
|
|
1149
|
+
if (changes.strokes) removeStaleBindings(node, "strokes", changes);
|
|
1150
|
+
for (const key of absent) Reflect.deleteProperty(node, key);
|
|
1151
|
+
this.emitter.emit("node:updated", id, changes);
|
|
1152
|
+
}
|
|
1153
|
+
reparentNode(nodeId, newParentId) {
|
|
1154
|
+
const node = this.nodes.get(nodeId);
|
|
1155
|
+
if (!node || nodeId === this.rootId) return;
|
|
1156
|
+
if (this.isDescendant(newParentId, nodeId)) return;
|
|
1157
|
+
const oldParent = node.parentId ? this.nodes.get(node.parentId) : void 0;
|
|
1158
|
+
const newParent = this.nodes.get(newParentId);
|
|
1159
|
+
if (!newParent) return;
|
|
1160
|
+
if (node.parentId === newParentId) return;
|
|
1161
|
+
const oldParentId = node.parentId;
|
|
1162
|
+
this.absPosCache.clear();
|
|
1163
|
+
const absPos = this.getAbsolutePosition(nodeId);
|
|
1164
|
+
const newParentNode = this.nodes.get(newParentId);
|
|
1165
|
+
const newParentAbs = newParentId === this.rootId || newParentNode?.type === "CANVAS" ? {
|
|
1166
|
+
x: 0,
|
|
1167
|
+
y: 0
|
|
1168
|
+
} : this.getAbsolutePosition(newParentId);
|
|
1169
|
+
if (oldParent) oldParent.childIds = oldParent.childIds.filter((cid) => cid !== nodeId);
|
|
1170
|
+
node.parentId = newParentId;
|
|
1171
|
+
newParent.childIds.push(nodeId);
|
|
1172
|
+
node.x = absPos.x - newParentAbs.x;
|
|
1173
|
+
node.y = absPos.y - newParentAbs.y;
|
|
1174
|
+
this.emitter.emit("node:reparented", nodeId, oldParentId, newParentId);
|
|
1175
|
+
}
|
|
1176
|
+
reorderChild(nodeId, parentId, insertIndex) {
|
|
1177
|
+
const node = this.nodes.get(nodeId);
|
|
1178
|
+
if (!node) return;
|
|
1179
|
+
const previousParentId = node.parentId;
|
|
1180
|
+
const oldParent = previousParentId ? this.nodes.get(previousParentId) : void 0;
|
|
1181
|
+
const newParent = this.nodes.get(parentId);
|
|
1182
|
+
if (!newParent || this.isDescendant(parentId, nodeId)) return;
|
|
1183
|
+
if (oldParent) oldParent.childIds = oldParent.childIds.filter((cid) => cid !== nodeId);
|
|
1184
|
+
let idx = insertIndex;
|
|
1185
|
+
if (oldParent === newParent && idx > (!oldParent.childIds.includes(nodeId) ? idx : oldParent.childIds.length)) {}
|
|
1186
|
+
node.parentId = parentId;
|
|
1187
|
+
this.absPosCache.clear();
|
|
1188
|
+
idx = Math.min(idx, newParent.childIds.length);
|
|
1189
|
+
newParent.childIds.splice(idx, 0, nodeId);
|
|
1190
|
+
this.emitter.emit("node:reordered", nodeId, parentId, idx, previousParentId);
|
|
1191
|
+
}
|
|
1192
|
+
insertChildAt(childId, parentId, index) {
|
|
1193
|
+
const node = this.getNode(childId);
|
|
1194
|
+
const newParent = this.getNode(parentId);
|
|
1195
|
+
if (!node || !newParent || childId === parentId || this.isDescendant(parentId, childId)) return;
|
|
1196
|
+
const previousParentId = node.parentId;
|
|
1197
|
+
const oldParent = previousParentId ? this.getNode(previousParentId) : void 0;
|
|
1198
|
+
if (oldParent) oldParent.childIds = oldParent.childIds.filter((id) => id !== childId);
|
|
1199
|
+
newParent.childIds = newParent.childIds.filter((id) => id !== childId);
|
|
1200
|
+
newParent.childIds.splice(index, 0, childId);
|
|
1201
|
+
node.parentId = parentId;
|
|
1202
|
+
this.clearAbsPosCache();
|
|
1203
|
+
this.emitter.emit("node:reordered", childId, parentId, index, previousParentId);
|
|
1204
|
+
}
|
|
1205
|
+
deleteNode(id) {
|
|
1206
|
+
const node = this.nodes.get(id);
|
|
1207
|
+
if (!node || id === this.rootId) return;
|
|
1208
|
+
if (node.parentId) {
|
|
1209
|
+
const parent = this.nodes.get(node.parentId);
|
|
1210
|
+
if (parent) parent.childIds = parent.childIds.filter((cid) => cid !== id);
|
|
1211
|
+
}
|
|
1212
|
+
for (const childId of Array.from(node.childIds)) this.deleteNode(childId);
|
|
1213
|
+
if (node.type === "INSTANCE" && node.componentId) this.instanceIndex.get(node.componentId)?.delete(id);
|
|
1214
|
+
this.nodes.delete(id);
|
|
1215
|
+
this.emitter.emit("node:deleted", id, node.parentId);
|
|
1216
|
+
}
|
|
1217
|
+
hitTest(px, py, scopeId) {
|
|
1218
|
+
return hitTest(this, px, py, scopeId);
|
|
1219
|
+
}
|
|
1220
|
+
hitTestDeep(px, py, scopeId) {
|
|
1221
|
+
return hitTestDeep(this, px, py, scopeId);
|
|
1222
|
+
}
|
|
1223
|
+
hitTestFrame(px, py, excludeIds, scopeId) {
|
|
1224
|
+
return hitTestFrame(this, px, py, excludeIds, scopeId);
|
|
1225
|
+
}
|
|
1226
|
+
cloneTree(sourceId, parentId, overrides = {}) {
|
|
1227
|
+
const src = this.nodes.get(sourceId);
|
|
1228
|
+
if (!src) return null;
|
|
1229
|
+
const props = cloneNodeProps(src, null);
|
|
1230
|
+
props.source = {
|
|
1231
|
+
...props.source,
|
|
1232
|
+
id: null,
|
|
1233
|
+
orderKey: null
|
|
1234
|
+
};
|
|
1235
|
+
const clone = this.createNode(src.type, parentId, {
|
|
1236
|
+
...props,
|
|
1237
|
+
...overrides
|
|
1238
|
+
});
|
|
1239
|
+
for (const childId of src.childIds) this.cloneTree(childId, clone.id);
|
|
1240
|
+
return clone;
|
|
1241
|
+
}
|
|
1242
|
+
createInstance(componentId, parentId, overrides = {}) {
|
|
1243
|
+
return createInstance(this, componentId, parentId, overrides);
|
|
1244
|
+
}
|
|
1245
|
+
populateInstanceChildren(instanceId, componentId, mode = "deep") {
|
|
1246
|
+
populateInstanceChildren(this, instanceId, componentId, mode);
|
|
1247
|
+
}
|
|
1248
|
+
swapInstanceComponent(instanceId, componentId) {
|
|
1249
|
+
swapInstanceComponent(this, instanceId, componentId);
|
|
1250
|
+
}
|
|
1251
|
+
syncInstances(componentId) {
|
|
1252
|
+
syncInstances(this, componentId);
|
|
1253
|
+
}
|
|
1254
|
+
detachInstance(instanceId) {
|
|
1255
|
+
detachInstance(this, instanceId);
|
|
1256
|
+
}
|
|
1257
|
+
getMainComponent(instanceId) {
|
|
1258
|
+
return getMainComponent(this, instanceId);
|
|
1259
|
+
}
|
|
1260
|
+
getInstances(componentId) {
|
|
1261
|
+
return getInstances(this, componentId);
|
|
1262
|
+
}
|
|
1263
|
+
flattenTree(parentId, depth = 0) {
|
|
1264
|
+
const id = parentId ?? this.rootId;
|
|
1265
|
+
const parent = this.nodes.get(id);
|
|
1266
|
+
if (!parent) return [];
|
|
1267
|
+
const result = [];
|
|
1268
|
+
for (const childId of parent.childIds) {
|
|
1269
|
+
const child = this.nodes.get(childId);
|
|
1270
|
+
if (!child) continue;
|
|
1271
|
+
result.push({
|
|
1272
|
+
node: child,
|
|
1273
|
+
depth
|
|
1274
|
+
});
|
|
1275
|
+
if (child.childIds.length > 0) result.push(...this.flattenTree(childId, depth + 1));
|
|
1276
|
+
}
|
|
1277
|
+
return result;
|
|
1278
|
+
}
|
|
1279
|
+
};
|
|
1280
|
+
//#endregion
|
|
1281
|
+
export { GLYPH_AFFECTING_KEYS, SceneGraph, TEXT_PICTURE_KEYS, UndoManager, addCollection, addMode, addVariable, bindVariable, collectSceneMutation, computeSelectionBounds, computeSnap, createCollection, createSceneMutationImpact, createVariable, generateId, getActiveModeId, getNodeVariableModeId, getSharedStyles, getVariablesByType, getVariablesForCollection, invalidateTextCaches, layoutGuideLines, layoutGuideSections, markSourceFieldsEdited, mutationLayoutScopeIds, removeCollection, removeMode, removeVariable, renameMode, resolveColorVariable, resolveColorVariableForNode, resolveNumberVariable, resolveNumberVariableForNode, resolveVariable, setActiveMode, setDefaultMode, sharedStyleRefKey, sharedStyleTypeForKind, styleDetachmentChanges, textCacheInvalidationChanges, unbindVariable, updateNodePreview };
|
|
1282
|
+
|
|
1283
|
+
//# sourceMappingURL=index.js.map
|