pixiboardjs 0.1.0-alpha.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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +54 -0
- package/VERSIONING.md +24 -0
- package/dist/browser.d.ts +657 -0
- package/dist/browser.js +5513 -0
- package/dist/browser.js.map +1 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +3812 -0
- package/dist/index.js.map +1 -0
- package/dist/node.d.ts +2 -0
- package/dist/node.js +3812 -0
- package/dist/node.js.map +1 -0
- package/dist/types-DnR8yhxj.d.ts +702 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/etc/pixiboardjs-browser.api.md +1435 -0
- package/etc/pixiboardjs-node.api.md +761 -0
- package/etc/pixiboardjs-types.api.md +669 -0
- package/etc/pixiboardjs.api.md +761 -0
- package/package.json +82 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,3812 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// ../core/src/index.ts
|
|
8
|
+
var src_exports = {};
|
|
9
|
+
__export(src_exports, {
|
|
10
|
+
BoardAssetsController: () => BoardAssetsController,
|
|
11
|
+
BoardCore: () => BoardCore,
|
|
12
|
+
BoardDocumentController: () => BoardDocumentController,
|
|
13
|
+
BoardNodesController: () => BoardNodesController,
|
|
14
|
+
BoardViewportController: () => BoardViewportController,
|
|
15
|
+
DocumentValidationError: () => DocumentValidationError,
|
|
16
|
+
NodeNotFoundError: () => NodeNotFoundError,
|
|
17
|
+
NodeTypeNotRegisteredError: () => NodeTypeNotRegisteredError,
|
|
18
|
+
NodeTypeRegistry: () => NodeTypeRegistry,
|
|
19
|
+
NodeValidationError: () => NodeValidationError,
|
|
20
|
+
PixiBoardCoreError: () => PixiBoardCoreError,
|
|
21
|
+
RESIZE_HANDLES: () => RESIZE_HANDLES,
|
|
22
|
+
SelectionController: () => SelectionController,
|
|
23
|
+
TransactionConflictError: () => TransactionConflictError,
|
|
24
|
+
ViewportController: () => ViewportController,
|
|
25
|
+
assertWorldBounds: () => assertWorldBounds,
|
|
26
|
+
createBoardCore: () => createBoardCore,
|
|
27
|
+
isResizeHandle: () => isResizeHandle,
|
|
28
|
+
mergeBounds: () => mergeBounds,
|
|
29
|
+
nodeGeometry: () => nodeGeometry,
|
|
30
|
+
resizeHandleAxes: () => resizeHandleAxes,
|
|
31
|
+
resizeHandleCursor: () => resizeHandleCursor,
|
|
32
|
+
resolveResize: () => resolveResize,
|
|
33
|
+
resolveResizeSize: () => resolveResizeSize,
|
|
34
|
+
rotatedRectBounds: () => rotatedRectBounds
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// ../core/src/errors.ts
|
|
38
|
+
var PixiBoardCoreError = class extends Error {
|
|
39
|
+
constructor(message) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.name = new.target.name;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var NodeNotFoundError = class extends PixiBoardCoreError {
|
|
45
|
+
constructor(nodeId) {
|
|
46
|
+
super(`Node not found: ${nodeId}`);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var NodeTypeNotRegisteredError = class extends PixiBoardCoreError {
|
|
50
|
+
constructor(type) {
|
|
51
|
+
super(`Node type is not registered: ${type}`);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var NodeValidationError = class extends PixiBoardCoreError {
|
|
55
|
+
nodeId;
|
|
56
|
+
nodeType;
|
|
57
|
+
cause;
|
|
58
|
+
constructor(message, options = {}) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.nodeId = options.nodeId;
|
|
61
|
+
this.nodeType = options.nodeType;
|
|
62
|
+
this.cause = options.cause;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var DocumentValidationError = class extends PixiBoardCoreError {
|
|
66
|
+
cause;
|
|
67
|
+
constructor(message, options = {}) {
|
|
68
|
+
super(message);
|
|
69
|
+
this.cause = options.cause;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
var TransactionConflictError = class extends PixiBoardCoreError {
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// ../core/src/json.ts
|
|
76
|
+
function cloneValue(value) {
|
|
77
|
+
return structuredClone(value);
|
|
78
|
+
}
|
|
79
|
+
function immutableClone(value) {
|
|
80
|
+
return deepFreeze(cloneValue(value));
|
|
81
|
+
}
|
|
82
|
+
function deepFreeze(value) {
|
|
83
|
+
if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
84
|
+
Object.freeze(value);
|
|
85
|
+
for (const child of Object.values(value)) {
|
|
86
|
+
deepFreeze(child);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
function assertJsonValue(value, path = "value", ancestors = /* @__PURE__ */ new WeakSet()) {
|
|
92
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === "number") {
|
|
96
|
+
if (!Number.isFinite(value)) {
|
|
97
|
+
throw new DocumentValidationError(`${path} must contain only finite numbers`);
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (Array.isArray(value)) {
|
|
102
|
+
assertNotCircular(value, path, ancestors);
|
|
103
|
+
value.forEach((item, index) => assertJsonValue(item, `${path}[${index}]`, ancestors));
|
|
104
|
+
ancestors.delete(value);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (typeof value === "object") {
|
|
108
|
+
const prototype = Object.getPrototypeOf(value);
|
|
109
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
110
|
+
throw new DocumentValidationError(`${path} must be a plain JSON object`);
|
|
111
|
+
}
|
|
112
|
+
if (Reflect.ownKeys(value).some((key) => typeof key !== "string")) {
|
|
113
|
+
throw new DocumentValidationError(`${path} must not contain symbol keys`);
|
|
114
|
+
}
|
|
115
|
+
assertNotCircular(value, path, ancestors);
|
|
116
|
+
for (const [key, item] of Object.entries(value)) {
|
|
117
|
+
assertJsonValue(item, `${path}.${key}`, ancestors);
|
|
118
|
+
}
|
|
119
|
+
ancestors.delete(value);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
throw new DocumentValidationError(`${path} is not JSON-serializable`);
|
|
123
|
+
}
|
|
124
|
+
function assertNotCircular(value, path, ancestors) {
|
|
125
|
+
if (ancestors.has(value)) {
|
|
126
|
+
throw new DocumentValidationError(`${path} must not contain circular references`);
|
|
127
|
+
}
|
|
128
|
+
ancestors.add(value);
|
|
129
|
+
}
|
|
130
|
+
function jsonEqual(left, right) {
|
|
131
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ../core/src/document-validation.ts
|
|
135
|
+
function validateDocument(input, options) {
|
|
136
|
+
const value = parseInput(input);
|
|
137
|
+
if (!isRecord(value)) throw new DocumentValidationError("Document must be an object");
|
|
138
|
+
const sourceVersion = readInteger(value, "schemaVersion", "document");
|
|
139
|
+
if (sourceVersion > options.schemaVersion) {
|
|
140
|
+
throw new DocumentValidationError(
|
|
141
|
+
`Document schema ${sourceVersion} is newer than supported schema ${options.schemaVersion}`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (sourceVersion < options.schemaVersion) {
|
|
145
|
+
throw new DocumentValidationError(
|
|
146
|
+
`Document schema ${sourceVersion} is older than supported schema ${options.schemaVersion}`
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
assertJsonValue(value, "document");
|
|
150
|
+
if (!isRecord(value)) throw new DocumentValidationError("Document must be an object");
|
|
151
|
+
const schemaVersion = readInteger(value, "schemaVersion", "document");
|
|
152
|
+
const revision = readInteger(value, "revision", "document");
|
|
153
|
+
if (schemaVersion < 1) throw new DocumentValidationError("schemaVersion must be positive");
|
|
154
|
+
if (revision < 0) throw new DocumentValidationError("revision must not be negative");
|
|
155
|
+
if (!Array.isArray(value.nodes)) throw new DocumentValidationError("document.nodes must be an array");
|
|
156
|
+
if (!Array.isArray(value.assets)) throw new DocumentValidationError("document.assets must be an array");
|
|
157
|
+
const nodeIds = /* @__PURE__ */ new Set();
|
|
158
|
+
const nodes = value.nodes.map((node, index) => {
|
|
159
|
+
const validated = validateNode(node, `document.nodes[${index}]`);
|
|
160
|
+
if (nodeIds.has(validated.id)) {
|
|
161
|
+
throw new DocumentValidationError(`Duplicate node id: ${validated.id}`);
|
|
162
|
+
}
|
|
163
|
+
nodeIds.add(validated.id);
|
|
164
|
+
try {
|
|
165
|
+
return options.nodeTypes.validateNode(validated);
|
|
166
|
+
} catch (cause) {
|
|
167
|
+
if (cause instanceof NodeValidationError) throw cause;
|
|
168
|
+
throw new NodeValidationError(`Invalid node ${validated.id}`, {
|
|
169
|
+
nodeId: validated.id,
|
|
170
|
+
nodeType: validated.type,
|
|
171
|
+
cause
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
const assetIds = /* @__PURE__ */ new Set();
|
|
176
|
+
const assets = value.assets.map((asset, index) => {
|
|
177
|
+
const validated = validateAsset(asset, `document.assets[${index}]`);
|
|
178
|
+
if (assetIds.has(validated.id)) {
|
|
179
|
+
throw new DocumentValidationError(`Duplicate asset id: ${validated.id}`);
|
|
180
|
+
}
|
|
181
|
+
assetIds.add(validated.id);
|
|
182
|
+
return validated;
|
|
183
|
+
});
|
|
184
|
+
if (value.viewport !== void 0) validateViewport(value.viewport, "document.viewport");
|
|
185
|
+
if (value.metadata !== void 0 && !isRecord(value.metadata)) {
|
|
186
|
+
throw new DocumentValidationError("document.metadata must be an object");
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
...cloneValue(value),
|
|
190
|
+
schemaVersion,
|
|
191
|
+
revision,
|
|
192
|
+
nodes,
|
|
193
|
+
assets,
|
|
194
|
+
...value.viewport === void 0 ? {} : { viewport: cloneValue(value.viewport) }
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function validateNode(input, path = "node") {
|
|
198
|
+
assertJsonValue(input, path);
|
|
199
|
+
if (!isRecord(input)) throw new DocumentValidationError(`${path} must be an object`);
|
|
200
|
+
if ("assetId" in input) {
|
|
201
|
+
throw new DocumentValidationError(
|
|
202
|
+
`${path}.assetId is not supported; use ${path}.assetRefs instead`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
const id = readString(input, "id", path);
|
|
206
|
+
const type = readString(input, "type", path);
|
|
207
|
+
const typeVersion = readInteger(input, "typeVersion", path);
|
|
208
|
+
if (typeVersion < 1) throw new DocumentValidationError(`${path}.typeVersion must be positive`);
|
|
209
|
+
for (const field of ["x", "y", "width", "height", "rotation", "zIndex"]) {
|
|
210
|
+
readFiniteNumber(input, field, path);
|
|
211
|
+
}
|
|
212
|
+
if (input.width < 0 || input.height < 0) {
|
|
213
|
+
throw new DocumentValidationError(`${path} width and height must not be negative`);
|
|
214
|
+
}
|
|
215
|
+
if (input.name !== void 0 && typeof input.name !== "string") {
|
|
216
|
+
throw new DocumentValidationError(`${path}.name must be a string`);
|
|
217
|
+
}
|
|
218
|
+
if (input.locked !== void 0 && typeof input.locked !== "boolean") {
|
|
219
|
+
throw new DocumentValidationError(`${path}.locked must be a boolean`);
|
|
220
|
+
}
|
|
221
|
+
if (input.visible !== void 0 && typeof input.visible !== "boolean") {
|
|
222
|
+
throw new DocumentValidationError(`${path}.visible must be a boolean`);
|
|
223
|
+
}
|
|
224
|
+
if (!("props" in input)) throw new DocumentValidationError(`${path}.props is required`);
|
|
225
|
+
if (input.assetRefs !== void 0) validateAssetRefs(input.assetRefs, `${path}.assetRefs`);
|
|
226
|
+
return { ...cloneValue(input), id, type, typeVersion };
|
|
227
|
+
}
|
|
228
|
+
function validateAsset(input, path = "asset") {
|
|
229
|
+
assertJsonValue(input, path);
|
|
230
|
+
if (!isRecord(input)) throw new DocumentValidationError(`${path} must be an object`);
|
|
231
|
+
const id = readString(input, "id", path);
|
|
232
|
+
const kind = readString(input, "kind", path);
|
|
233
|
+
if (input.metadata !== void 0 && !isRecord(input.metadata)) {
|
|
234
|
+
throw new DocumentValidationError(`${path}.metadata must be an object`);
|
|
235
|
+
}
|
|
236
|
+
return { ...cloneValue(input), id, kind };
|
|
237
|
+
}
|
|
238
|
+
function validateViewport(input, path = "viewport") {
|
|
239
|
+
assertJsonValue(input, path);
|
|
240
|
+
if (!isRecord(input) || !isRecord(input.offset)) {
|
|
241
|
+
throw new DocumentValidationError(`${path} must include an offset object`);
|
|
242
|
+
}
|
|
243
|
+
const scale = readFiniteNumber(input, "scale", path);
|
|
244
|
+
const x = readFiniteNumber(input.offset, "x", `${path}.offset`);
|
|
245
|
+
const y = readFiniteNumber(input.offset, "y", `${path}.offset`);
|
|
246
|
+
if (scale <= 0) throw new DocumentValidationError(`${path}.scale must be greater than zero`);
|
|
247
|
+
return { scale, offset: { x, y } };
|
|
248
|
+
}
|
|
249
|
+
function validateAssetRefs(input, path) {
|
|
250
|
+
if (!isRecord(input)) throw new DocumentValidationError(`${path} must be an object`);
|
|
251
|
+
for (const [name, ref] of Object.entries(input)) {
|
|
252
|
+
if (!name || !isRecord(ref)) throw new DocumentValidationError(`${path}.${name} is invalid`);
|
|
253
|
+
readString(ref, "assetId", `${path}.${name}`);
|
|
254
|
+
if (ref.variant !== void 0 && ref.variant !== "original" && ref.variant !== "preview" && ref.variant !== "waveform") {
|
|
255
|
+
throw new DocumentValidationError(`${path}.${name}.variant is invalid`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function parseInput(input) {
|
|
260
|
+
if (typeof input !== "string") return cloneValue(input);
|
|
261
|
+
try {
|
|
262
|
+
return JSON.parse(input);
|
|
263
|
+
} catch (cause) {
|
|
264
|
+
throw new DocumentValidationError("Document JSON could not be parsed", { cause });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function isRecord(value) {
|
|
268
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
269
|
+
}
|
|
270
|
+
function readString(record, key, path) {
|
|
271
|
+
const value = record[key];
|
|
272
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
273
|
+
throw new DocumentValidationError(`${path}.${key} must be a non-empty string`);
|
|
274
|
+
}
|
|
275
|
+
return value;
|
|
276
|
+
}
|
|
277
|
+
function readInteger(record, key, path) {
|
|
278
|
+
const value = record[key];
|
|
279
|
+
if (!Number.isInteger(value)) {
|
|
280
|
+
throw new DocumentValidationError(`${path}.${key} must be an integer`);
|
|
281
|
+
}
|
|
282
|
+
return value;
|
|
283
|
+
}
|
|
284
|
+
function readFiniteNumber(record, key, path) {
|
|
285
|
+
const value = record[key];
|
|
286
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
287
|
+
throw new DocumentValidationError(`${path}.${key} must be a finite number`);
|
|
288
|
+
}
|
|
289
|
+
return value;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ../core/src/geometry.ts
|
|
293
|
+
function rotatedRectBounds(node) {
|
|
294
|
+
const cos = Math.cos(node.rotation);
|
|
295
|
+
const sin = Math.sin(node.rotation);
|
|
296
|
+
const corners = [
|
|
297
|
+
{ x: 0, y: 0 },
|
|
298
|
+
{ x: node.width, y: 0 },
|
|
299
|
+
{ x: node.width, y: node.height },
|
|
300
|
+
{ x: 0, y: node.height }
|
|
301
|
+
].map((point) => ({
|
|
302
|
+
x: node.x + point.x * cos - point.y * sin,
|
|
303
|
+
y: node.y + point.x * sin + point.y * cos
|
|
304
|
+
}));
|
|
305
|
+
return {
|
|
306
|
+
minX: Math.min(...corners.map(({ x }) => x)),
|
|
307
|
+
minY: Math.min(...corners.map(({ y }) => y)),
|
|
308
|
+
maxX: Math.max(...corners.map(({ x }) => x)),
|
|
309
|
+
maxY: Math.max(...corners.map(({ y }) => y))
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function mergeBounds(bounds) {
|
|
313
|
+
if (bounds.length === 0) return void 0;
|
|
314
|
+
return {
|
|
315
|
+
minX: Math.min(...bounds.map(({ minX }) => minX)),
|
|
316
|
+
minY: Math.min(...bounds.map(({ minY }) => minY)),
|
|
317
|
+
maxX: Math.max(...bounds.map(({ maxX }) => maxX)),
|
|
318
|
+
maxY: Math.max(...bounds.map(({ maxY }) => maxY))
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function assertWorldBounds(bounds, label = "bounds") {
|
|
322
|
+
const values = [bounds.minX, bounds.minY, bounds.maxX, bounds.maxY];
|
|
323
|
+
if (!values.every(Number.isFinite) || bounds.maxX < bounds.minX || bounds.maxY < bounds.minY) {
|
|
324
|
+
throw new RangeError(`${label} must be finite and normalized`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ../core/src/history.ts
|
|
329
|
+
var recordHistory = /* @__PURE__ */ Symbol("recordHistory");
|
|
330
|
+
var HistoryController = class {
|
|
331
|
+
constructor(host) {
|
|
332
|
+
this.host = host;
|
|
333
|
+
}
|
|
334
|
+
host;
|
|
335
|
+
undoStack = [];
|
|
336
|
+
redoStack = [];
|
|
337
|
+
listeners = /* @__PURE__ */ new Set();
|
|
338
|
+
canUndo() {
|
|
339
|
+
return this.undoStack.length > 0;
|
|
340
|
+
}
|
|
341
|
+
canRedo() {
|
|
342
|
+
return this.redoStack.length > 0;
|
|
343
|
+
}
|
|
344
|
+
undo() {
|
|
345
|
+
const entry = this.undoStack.at(-1);
|
|
346
|
+
if (!entry) return void 0;
|
|
347
|
+
const changeSet = this.host.applyHistoryEntry(entry, "undo");
|
|
348
|
+
this.undoStack.pop();
|
|
349
|
+
this.redoStack.push(entry);
|
|
350
|
+
this.emit();
|
|
351
|
+
return changeSet;
|
|
352
|
+
}
|
|
353
|
+
redo() {
|
|
354
|
+
const entry = this.redoStack.at(-1);
|
|
355
|
+
if (!entry) return void 0;
|
|
356
|
+
const changeSet = this.host.applyHistoryEntry(entry, "redo");
|
|
357
|
+
this.redoStack.pop();
|
|
358
|
+
this.undoStack.push(entry);
|
|
359
|
+
this.emit();
|
|
360
|
+
return changeSet;
|
|
361
|
+
}
|
|
362
|
+
clear() {
|
|
363
|
+
if (!this.canUndo() && !this.canRedo()) return;
|
|
364
|
+
this.undoStack = [];
|
|
365
|
+
this.redoStack = [];
|
|
366
|
+
this.emit();
|
|
367
|
+
}
|
|
368
|
+
onChange(listener) {
|
|
369
|
+
this.listeners.add(listener);
|
|
370
|
+
return () => this.listeners.delete(listener);
|
|
371
|
+
}
|
|
372
|
+
[recordHistory](entry) {
|
|
373
|
+
const previous = this.undoStack.at(-1);
|
|
374
|
+
if (entry.coalesceKey !== void 0 && previous?.coalesceKey === entry.coalesceKey) {
|
|
375
|
+
previous.forward = compactReplaces([...previous.forward, ...entry.forward]);
|
|
376
|
+
previous.inverse = compactReplaces([...entry.inverse, ...previous.inverse]);
|
|
377
|
+
this.redoStack = [];
|
|
378
|
+
this.emit();
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
this.undoStack.push(entry);
|
|
382
|
+
this.redoStack = [];
|
|
383
|
+
this.emit();
|
|
384
|
+
}
|
|
385
|
+
emit() {
|
|
386
|
+
const event = { canUndo: this.canUndo(), canRedo: this.canRedo() };
|
|
387
|
+
for (const listener of this.listeners) {
|
|
388
|
+
try {
|
|
389
|
+
listener(event);
|
|
390
|
+
} catch {
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
function compactReplaces(patches) {
|
|
396
|
+
if (patches.some((patch) => patch.op !== "node:replace" && patch.op !== "asset:replace")) {
|
|
397
|
+
return patches;
|
|
398
|
+
}
|
|
399
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
400
|
+
for (const patch of patches) {
|
|
401
|
+
const key = patch.op === "node:replace" ? `node:${patch.node.id}` : `asset:${patch.asset.id}`;
|
|
402
|
+
byKey.set(key, patch);
|
|
403
|
+
}
|
|
404
|
+
return [...byKey.values()];
|
|
405
|
+
}
|
|
406
|
+
function createHistoryController(host) {
|
|
407
|
+
const controller = new HistoryController(host);
|
|
408
|
+
return {
|
|
409
|
+
controller,
|
|
410
|
+
record: (entry) => controller[recordHistory](entry)
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// ../core/src/node-type-registry.ts
|
|
415
|
+
var NodeTypeRegistry = class {
|
|
416
|
+
definitions = /* @__PURE__ */ new Map();
|
|
417
|
+
development;
|
|
418
|
+
constructor(options = {}) {
|
|
419
|
+
this.development = options.development ?? false;
|
|
420
|
+
}
|
|
421
|
+
register(definition, options = {}) {
|
|
422
|
+
assertDefinition(definition);
|
|
423
|
+
const registered = freezeDefinition(definition);
|
|
424
|
+
const existing = this.definitions.get(registered.type);
|
|
425
|
+
if (existing && !options.replace) {
|
|
426
|
+
throw new NodeValidationError(`Node type is already registered: ${registered.type}`);
|
|
427
|
+
}
|
|
428
|
+
if (existing && options.replace && !this.development) {
|
|
429
|
+
throw new NodeValidationError(
|
|
430
|
+
`Replacing node type definitions is only allowed in development: ${registered.type}`
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
this.definitions.set(registered.type, registered);
|
|
434
|
+
return () => {
|
|
435
|
+
if (this.definitions.get(registered.type) === registered) {
|
|
436
|
+
this.definitions.delete(registered.type);
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
has(type) {
|
|
441
|
+
return this.definitions.has(type);
|
|
442
|
+
}
|
|
443
|
+
get(type) {
|
|
444
|
+
return this.definitions.get(type);
|
|
445
|
+
}
|
|
446
|
+
require(type) {
|
|
447
|
+
const definition = this.get(type);
|
|
448
|
+
if (!definition) throw new NodeTypeNotRegisteredError(type);
|
|
449
|
+
return definition;
|
|
450
|
+
}
|
|
451
|
+
list() {
|
|
452
|
+
return [...this.definitions.values()];
|
|
453
|
+
}
|
|
454
|
+
createProps(type, props) {
|
|
455
|
+
const definition = this.require(type);
|
|
456
|
+
const input = mergeDefaults(definition.defaults, props);
|
|
457
|
+
return {
|
|
458
|
+
typeVersion: definition.version,
|
|
459
|
+
props: this.runValidation(definition, input)
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
validateNode(node) {
|
|
463
|
+
const definition = this.get(node.type);
|
|
464
|
+
if (!definition) return cloneValue(node);
|
|
465
|
+
if (node.typeVersion !== definition.version) {
|
|
466
|
+
throw new NodeValidationError(
|
|
467
|
+
`Node ${node.id} uses ${node.type} typeVersion ${node.typeVersion}; registered version is ${definition.version}`,
|
|
468
|
+
{ nodeId: node.id, nodeType: node.type }
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
...cloneValue(node),
|
|
473
|
+
typeVersion: node.typeVersion,
|
|
474
|
+
props: this.runValidation(definition, node.props)
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
getBounds(node) {
|
|
478
|
+
const definition = this.require(node.type);
|
|
479
|
+
return definition.getBounds(cloneValue(node));
|
|
480
|
+
}
|
|
481
|
+
runValidation(definition, value) {
|
|
482
|
+
try {
|
|
483
|
+
const validated = definition.validate(cloneValue(value));
|
|
484
|
+
assertJsonValue(validated, `${definition.type}.props`);
|
|
485
|
+
return cloneValue(validated);
|
|
486
|
+
} catch (cause) {
|
|
487
|
+
throw new NodeValidationError(`Invalid props for node type ${definition.type}`, {
|
|
488
|
+
nodeType: definition.type,
|
|
489
|
+
cause
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
function assertDefinition(definition) {
|
|
495
|
+
if (!definition.type.trim()) {
|
|
496
|
+
throw new NodeValidationError("Node type must be a non-empty string");
|
|
497
|
+
}
|
|
498
|
+
if (!Number.isInteger(definition.version) || definition.version < 1) {
|
|
499
|
+
throw new NodeValidationError(`Node type ${definition.type} must have a positive version`);
|
|
500
|
+
}
|
|
501
|
+
if (typeof definition.validate !== "function" || typeof definition.getBounds !== "function") {
|
|
502
|
+
throw new NodeValidationError(
|
|
503
|
+
`Node type ${definition.type} must define validate() and getBounds()`
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
if (definition.defaults !== void 0) {
|
|
507
|
+
assertJsonValue(definition.defaults, `${definition.type}.defaults`);
|
|
508
|
+
}
|
|
509
|
+
assertResizePolicy(definition);
|
|
510
|
+
}
|
|
511
|
+
function assertResizePolicy(definition) {
|
|
512
|
+
const policy = definition.resize;
|
|
513
|
+
if (policy === void 0) return;
|
|
514
|
+
const modes = ["free", "aspect-ratio", "fixed", "custom"];
|
|
515
|
+
if (!modes.includes(policy.mode)) {
|
|
516
|
+
throw new NodeValidationError(
|
|
517
|
+
`Node type ${definition.type} has an unknown resize mode: ${String(policy.mode)}`
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
if (policy.mode === "custom" && typeof policy.resize !== "function") {
|
|
521
|
+
throw new NodeValidationError(
|
|
522
|
+
`Node type ${definition.type} declares resize mode "custom" without a resize() function`
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
if (policy.mode === "aspect-ratio" && policy.ratio !== void 0 && (!Number.isFinite(policy.ratio) || policy.ratio <= 0)) {
|
|
526
|
+
throw new NodeValidationError(
|
|
527
|
+
`Node type ${definition.type} declares a non-positive aspect ratio`
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
function freezeDefinition(definition) {
|
|
532
|
+
const registered = {
|
|
533
|
+
...definition,
|
|
534
|
+
...definition.defaults === void 0 ? {} : { defaults: deepFreeze(cloneValue(definition.defaults)) },
|
|
535
|
+
...definition.resize === void 0 ? {} : { resize: Object.freeze({ ...definition.resize }) }
|
|
536
|
+
};
|
|
537
|
+
return Object.freeze(registered);
|
|
538
|
+
}
|
|
539
|
+
function mergeDefaults(defaults, props) {
|
|
540
|
+
if (isPlainObject(defaults) || isPlainObject(props)) {
|
|
541
|
+
return {
|
|
542
|
+
...isPlainObject(defaults) ? cloneValue(defaults) : {},
|
|
543
|
+
...isPlainObject(props) ? cloneValue(props) : {}
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
return props ?? defaults ?? null;
|
|
547
|
+
}
|
|
548
|
+
function isPlainObject(value) {
|
|
549
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// ../core/src/patches.ts
|
|
553
|
+
function applyDataPatches(store, patches) {
|
|
554
|
+
for (const patch of patches) {
|
|
555
|
+
switch (patch.op) {
|
|
556
|
+
case "node:insert":
|
|
557
|
+
store.insertNode(patch.index, patch.node);
|
|
558
|
+
break;
|
|
559
|
+
case "node:replace":
|
|
560
|
+
store.replaceNode(patch.node);
|
|
561
|
+
break;
|
|
562
|
+
case "node:remove":
|
|
563
|
+
store.removeNode(patch.nodeId);
|
|
564
|
+
break;
|
|
565
|
+
case "asset:insert":
|
|
566
|
+
store.insertAsset(patch.index, patch.asset);
|
|
567
|
+
break;
|
|
568
|
+
case "asset:replace":
|
|
569
|
+
store.replaceAsset(patch.asset);
|
|
570
|
+
break;
|
|
571
|
+
case "asset:remove":
|
|
572
|
+
store.removeAsset(patch.assetId);
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// ../core/src/resize.ts
|
|
579
|
+
var HANDLE_AXES = {
|
|
580
|
+
nw: { horizontal: -1, vertical: -1 },
|
|
581
|
+
n: { horizontal: 0, vertical: -1 },
|
|
582
|
+
ne: { horizontal: 1, vertical: -1 },
|
|
583
|
+
e: { horizontal: 1, vertical: 0 },
|
|
584
|
+
se: { horizontal: 1, vertical: 1 },
|
|
585
|
+
s: { horizontal: 0, vertical: 1 },
|
|
586
|
+
sw: { horizontal: -1, vertical: 1 },
|
|
587
|
+
w: { horizontal: -1, vertical: 0 }
|
|
588
|
+
};
|
|
589
|
+
var RESIZE_HANDLES = Object.freeze([
|
|
590
|
+
"nw",
|
|
591
|
+
"n",
|
|
592
|
+
"ne",
|
|
593
|
+
"e",
|
|
594
|
+
"se",
|
|
595
|
+
"s",
|
|
596
|
+
"sw",
|
|
597
|
+
"w"
|
|
598
|
+
]);
|
|
599
|
+
function resizeHandleAxes(handle) {
|
|
600
|
+
const axes = HANDLE_AXES[handle];
|
|
601
|
+
if (!axes) throw new RangeError(`Unknown resize handle: ${String(handle)}`);
|
|
602
|
+
return { ...axes };
|
|
603
|
+
}
|
|
604
|
+
var DEFAULT_MIN_SIZE = 1;
|
|
605
|
+
function isResizeHandle(value) {
|
|
606
|
+
return typeof value === "string" && value in HANDLE_AXES;
|
|
607
|
+
}
|
|
608
|
+
function nodeGeometry(node) {
|
|
609
|
+
return { x: node.x, y: node.y, width: node.width, height: node.height, rotation: node.rotation };
|
|
610
|
+
}
|
|
611
|
+
function resolveResize(node, policy, request) {
|
|
612
|
+
const axes = HANDLE_AXES[request.handle];
|
|
613
|
+
if (!axes) throw new RangeError(`Unknown resize handle: ${String(request.handle)}`);
|
|
614
|
+
if (!Number.isFinite(request.deltaWorld.x) || !Number.isFinite(request.deltaWorld.y)) {
|
|
615
|
+
throw new RangeError("Resize delta must be finite");
|
|
616
|
+
}
|
|
617
|
+
const origin = request.origin ?? nodeGeometry(node);
|
|
618
|
+
const localDelta = rotatePoint(request.deltaWorld, -origin.rotation);
|
|
619
|
+
const resolved = resolveResizeSize(node, policy, {
|
|
620
|
+
handle: request.handle,
|
|
621
|
+
width: origin.width + axes.horizontal * localDelta.x,
|
|
622
|
+
height: origin.height + axes.vertical * localDelta.y,
|
|
623
|
+
origin,
|
|
624
|
+
...request.minWidth === void 0 ? {} : { minWidth: request.minWidth },
|
|
625
|
+
...request.minHeight === void 0 ? {} : { minHeight: request.minHeight }
|
|
626
|
+
});
|
|
627
|
+
if (!resolved) return {};
|
|
628
|
+
const left = anchorOffset(axes.horizontal, origin.width, resolved.width);
|
|
629
|
+
const top = anchorOffset(axes.vertical, origin.height, resolved.height);
|
|
630
|
+
const offset = rotatePoint({ x: left, y: top }, origin.rotation);
|
|
631
|
+
const patch = { ...resolved.patch, width: resolved.width, height: resolved.height };
|
|
632
|
+
if (resolved.patch.x === void 0) patch.x = origin.x + offset.x;
|
|
633
|
+
if (resolved.patch.y === void 0) patch.y = origin.y + offset.y;
|
|
634
|
+
return patch;
|
|
635
|
+
}
|
|
636
|
+
function resolveResizeSize(node, policy, request) {
|
|
637
|
+
const axes = HANDLE_AXES[request.handle];
|
|
638
|
+
if (!axes) throw new RangeError(`Unknown resize handle: ${String(request.handle)}`);
|
|
639
|
+
if (!Number.isFinite(request.width) || !Number.isFinite(request.height)) {
|
|
640
|
+
throw new RangeError("Resize size must be finite");
|
|
641
|
+
}
|
|
642
|
+
const mode = policy?.mode ?? "free";
|
|
643
|
+
if (mode === "fixed" || node.locked === true) return void 0;
|
|
644
|
+
const origin = request.origin ?? nodeGeometry(node);
|
|
645
|
+
const minWidth = Math.max(request.minWidth ?? DEFAULT_MIN_SIZE, 0);
|
|
646
|
+
const minHeight = Math.max(request.minHeight ?? DEFAULT_MIN_SIZE, 0);
|
|
647
|
+
let width = Math.max(request.width, minWidth);
|
|
648
|
+
let height = Math.max(request.height, minHeight);
|
|
649
|
+
if (mode === "aspect-ratio") {
|
|
650
|
+
const constrained = constrainToRatio(
|
|
651
|
+
{ width, height },
|
|
652
|
+
{ width: origin.width, height: origin.height },
|
|
653
|
+
axes,
|
|
654
|
+
policy?.mode === "aspect-ratio" ? policy.ratio : void 0,
|
|
655
|
+
{ minWidth, minHeight }
|
|
656
|
+
);
|
|
657
|
+
width = constrained.width;
|
|
658
|
+
height = constrained.height;
|
|
659
|
+
}
|
|
660
|
+
let patch = {};
|
|
661
|
+
if (mode === "custom" && policy?.mode === "custom") {
|
|
662
|
+
patch = policy.resize({ node, width, height });
|
|
663
|
+
if (typeof patch.width === "number") width = Math.max(patch.width, 0);
|
|
664
|
+
if (typeof patch.height === "number") height = Math.max(patch.height, 0);
|
|
665
|
+
}
|
|
666
|
+
return { width, height, patch };
|
|
667
|
+
}
|
|
668
|
+
function resizeHandleCursor(handle, rotation = 0) {
|
|
669
|
+
const base2 = { n: 0, ne: 45, e: 90, se: 135, s: 180, sw: 225, w: 270, nw: 315 };
|
|
670
|
+
const degrees = base2[handle] + rotation * 180 / Math.PI;
|
|
671
|
+
const normalized = (degrees % 180 + 180) % 180;
|
|
672
|
+
const names = ["ns", "nesw", "ew", "nwse"];
|
|
673
|
+
return `${names[Math.round(normalized / 45) % 4]}-resize`;
|
|
674
|
+
}
|
|
675
|
+
function anchorOffset(axis, originSize, size) {
|
|
676
|
+
if (axis === 1) return 0;
|
|
677
|
+
if (axis === -1) return originSize - size;
|
|
678
|
+
return (originSize - size) / 2;
|
|
679
|
+
}
|
|
680
|
+
function constrainToRatio(size, origin, axes, declaredRatio, limits) {
|
|
681
|
+
const ratio = declaredRatio !== void 0 && Number.isFinite(declaredRatio) && declaredRatio > 0 ? declaredRatio : origin.height > 0 && origin.width > 0 ? origin.width / origin.height : 1;
|
|
682
|
+
let { width, height } = size;
|
|
683
|
+
if (axes.horizontal !== 0 && axes.vertical !== 0) {
|
|
684
|
+
const widthDrives = Math.abs(width - origin.width) >= Math.abs(height - origin.height) * ratio;
|
|
685
|
+
if (widthDrives) height = width / ratio;
|
|
686
|
+
else width = height * ratio;
|
|
687
|
+
} else if (axes.horizontal !== 0) {
|
|
688
|
+
height = width / ratio;
|
|
689
|
+
} else {
|
|
690
|
+
width = height * ratio;
|
|
691
|
+
}
|
|
692
|
+
if (width < limits.minWidth) {
|
|
693
|
+
width = limits.minWidth;
|
|
694
|
+
height = width / ratio;
|
|
695
|
+
}
|
|
696
|
+
if (height < limits.minHeight) {
|
|
697
|
+
height = limits.minHeight;
|
|
698
|
+
width = height * ratio;
|
|
699
|
+
}
|
|
700
|
+
return { width, height };
|
|
701
|
+
}
|
|
702
|
+
function rotatePoint(point, angle) {
|
|
703
|
+
if (!angle) return { x: point.x, y: point.y };
|
|
704
|
+
const cos = Math.cos(angle);
|
|
705
|
+
const sin = Math.sin(angle);
|
|
706
|
+
return { x: point.x * cos - point.y * sin, y: point.x * sin + point.y * cos };
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
// ../core/src/selection.ts
|
|
710
|
+
var SelectionController = class {
|
|
711
|
+
constructor(nodeExists, beforeMutation = () => {
|
|
712
|
+
}) {
|
|
713
|
+
this.nodeExists = nodeExists;
|
|
714
|
+
this.beforeMutation = beforeMutation;
|
|
715
|
+
}
|
|
716
|
+
nodeExists;
|
|
717
|
+
beforeMutation;
|
|
718
|
+
selected = /* @__PURE__ */ new Set();
|
|
719
|
+
listeners = /* @__PURE__ */ new Set();
|
|
720
|
+
get() {
|
|
721
|
+
return [...this.selected];
|
|
722
|
+
}
|
|
723
|
+
set(nodeIds) {
|
|
724
|
+
this.beforeMutation();
|
|
725
|
+
const next = /* @__PURE__ */ new Set();
|
|
726
|
+
for (const id of nodeIds) {
|
|
727
|
+
if (this.nodeExists(id)) next.add(id);
|
|
728
|
+
}
|
|
729
|
+
this.commit(next);
|
|
730
|
+
}
|
|
731
|
+
toggle(nodeId) {
|
|
732
|
+
this.beforeMutation();
|
|
733
|
+
if (!this.nodeExists(nodeId)) return;
|
|
734
|
+
const next = new Set(this.selected);
|
|
735
|
+
if (next.has(nodeId)) next.delete(nodeId);
|
|
736
|
+
else next.add(nodeId);
|
|
737
|
+
this.commit(next);
|
|
738
|
+
}
|
|
739
|
+
clear() {
|
|
740
|
+
this.beforeMutation();
|
|
741
|
+
this.commit(/* @__PURE__ */ new Set());
|
|
742
|
+
}
|
|
743
|
+
onChange(listener) {
|
|
744
|
+
this.listeners.add(listener);
|
|
745
|
+
return () => this.listeners.delete(listener);
|
|
746
|
+
}
|
|
747
|
+
prune() {
|
|
748
|
+
const next = new Set([...this.selected].filter((id) => this.nodeExists(id)));
|
|
749
|
+
return this.commit(next);
|
|
750
|
+
}
|
|
751
|
+
commit(next) {
|
|
752
|
+
const previous = [...this.selected];
|
|
753
|
+
const current = [...next];
|
|
754
|
+
if (sameIds(previous, current)) return false;
|
|
755
|
+
this.selected = next;
|
|
756
|
+
const event = { nodeIds: [...current], previousNodeIds: [...previous] };
|
|
757
|
+
for (const listener of this.listeners) {
|
|
758
|
+
try {
|
|
759
|
+
listener(event);
|
|
760
|
+
} catch {
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
return true;
|
|
764
|
+
}
|
|
765
|
+
};
|
|
766
|
+
function sameIds(left, right) {
|
|
767
|
+
return left.length === right.length && left.every((id, index) => id === right[index]);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// ../core/src/store.ts
|
|
771
|
+
var RuntimeDocumentStore = class _RuntimeDocumentStore {
|
|
772
|
+
document;
|
|
773
|
+
nodesById = /* @__PURE__ */ new Map();
|
|
774
|
+
nodeOrder = /* @__PURE__ */ new Map();
|
|
775
|
+
assetsById = /* @__PURE__ */ new Map();
|
|
776
|
+
assetOrder = /* @__PURE__ */ new Map();
|
|
777
|
+
nodeOverrides = /* @__PURE__ */ new Map();
|
|
778
|
+
assetOverrides = /* @__PURE__ */ new Map();
|
|
779
|
+
nodeOverridesShared = false;
|
|
780
|
+
assetOverridesShared = false;
|
|
781
|
+
nodesShared = false;
|
|
782
|
+
assetsShared = false;
|
|
783
|
+
constructor(document) {
|
|
784
|
+
this.document = cloneValue(document);
|
|
785
|
+
this.rebuildIndexes();
|
|
786
|
+
}
|
|
787
|
+
clone() {
|
|
788
|
+
const clone = Object.create(_RuntimeDocumentStore.prototype);
|
|
789
|
+
clone.document = { ...this.document, nodes: this.document.nodes, assets: this.document.assets };
|
|
790
|
+
clone.nodesById = this.nodesById;
|
|
791
|
+
clone.nodeOrder = this.nodeOrder;
|
|
792
|
+
clone.assetsById = this.assetsById;
|
|
793
|
+
clone.assetOrder = this.assetOrder;
|
|
794
|
+
clone.nodeOverrides = this.nodeOverrides;
|
|
795
|
+
clone.assetOverrides = this.assetOverrides;
|
|
796
|
+
clone.nodeOverridesShared = true;
|
|
797
|
+
clone.assetOverridesShared = true;
|
|
798
|
+
clone.nodesShared = true;
|
|
799
|
+
clone.assetsShared = true;
|
|
800
|
+
return clone;
|
|
801
|
+
}
|
|
802
|
+
snapshot() {
|
|
803
|
+
return immutableClone(this.materializedDocument());
|
|
804
|
+
}
|
|
805
|
+
mutableSnapshot() {
|
|
806
|
+
return cloneValue(this.materializedDocument());
|
|
807
|
+
}
|
|
808
|
+
get revision() {
|
|
809
|
+
return this.document.revision;
|
|
810
|
+
}
|
|
811
|
+
set revision(value) {
|
|
812
|
+
this.document.revision = value;
|
|
813
|
+
}
|
|
814
|
+
getNode(id) {
|
|
815
|
+
const node = this.getNodeReference(id);
|
|
816
|
+
return node ? cloneValue(node) : void 0;
|
|
817
|
+
}
|
|
818
|
+
getNodeReference(id) {
|
|
819
|
+
return this.nodeOverrides.get(id) ?? this.nodesById.get(id);
|
|
820
|
+
}
|
|
821
|
+
requireNode(id) {
|
|
822
|
+
const node = this.getNodeReference(id);
|
|
823
|
+
if (!node) throw new NodeNotFoundError(id);
|
|
824
|
+
return cloneValue(node);
|
|
825
|
+
}
|
|
826
|
+
getNodeIndex(id) {
|
|
827
|
+
return this.nodeOrder.get(id);
|
|
828
|
+
}
|
|
829
|
+
listNodes() {
|
|
830
|
+
return cloneValue(this.materializedNodes());
|
|
831
|
+
}
|
|
832
|
+
listNodesByIds(ids) {
|
|
833
|
+
const nodes = [];
|
|
834
|
+
for (const id of ids) {
|
|
835
|
+
const node = this.getNodeReference(id);
|
|
836
|
+
if (node) nodes.push(node);
|
|
837
|
+
}
|
|
838
|
+
nodes.sort((left, right) => (this.nodeOrder.get(left.id) ?? 0) - (this.nodeOrder.get(right.id) ?? 0));
|
|
839
|
+
return cloneValue(nodes);
|
|
840
|
+
}
|
|
841
|
+
nodeCount() {
|
|
842
|
+
return this.document.nodes.length;
|
|
843
|
+
}
|
|
844
|
+
forEachNodeReference(callback) {
|
|
845
|
+
for (const node of this.document.nodes) callback(this.nodeOverrides.get(node.id) ?? node);
|
|
846
|
+
}
|
|
847
|
+
insertNode(index, node) {
|
|
848
|
+
if (this.getNodeReference(node.id)) {
|
|
849
|
+
throw new DocumentValidationError(`Duplicate node id: ${node.id}`);
|
|
850
|
+
}
|
|
851
|
+
if (!Number.isInteger(index) || index < 0 || index > this.document.nodes.length) {
|
|
852
|
+
throw new RangeError(`Invalid node insertion index: ${index}`);
|
|
853
|
+
}
|
|
854
|
+
this.materializeNodesInPlace();
|
|
855
|
+
this.document.nodes.splice(index, 0, cloneValue(node));
|
|
856
|
+
this.rebuildNodeIndex();
|
|
857
|
+
}
|
|
858
|
+
replaceNode(node) {
|
|
859
|
+
const index = this.nodeOrder.get(node.id);
|
|
860
|
+
if (index === void 0) throw new NodeNotFoundError(node.id);
|
|
861
|
+
this.ensureNodeOverridesMutable();
|
|
862
|
+
this.nodeOverrides.set(node.id, cloneValue(node));
|
|
863
|
+
}
|
|
864
|
+
removeNode(id) {
|
|
865
|
+
const index = this.getNodeIndex(id);
|
|
866
|
+
if (index === void 0) throw new NodeNotFoundError(id);
|
|
867
|
+
this.materializeNodesInPlace();
|
|
868
|
+
const [removed] = this.document.nodes.splice(index, 1);
|
|
869
|
+
this.rebuildNodeIndex();
|
|
870
|
+
return cloneValue(removed);
|
|
871
|
+
}
|
|
872
|
+
getAsset(id) {
|
|
873
|
+
const asset = this.getAssetReference(id);
|
|
874
|
+
return asset ? cloneValue(asset) : void 0;
|
|
875
|
+
}
|
|
876
|
+
getAssetReference(id) {
|
|
877
|
+
return this.assetOverrides.get(id) ?? this.assetsById.get(id);
|
|
878
|
+
}
|
|
879
|
+
getAssetIndex(id) {
|
|
880
|
+
return this.assetOrder.get(id);
|
|
881
|
+
}
|
|
882
|
+
listAssets() {
|
|
883
|
+
return cloneValue(this.materializedAssets());
|
|
884
|
+
}
|
|
885
|
+
assetCount() {
|
|
886
|
+
return this.document.assets.length;
|
|
887
|
+
}
|
|
888
|
+
insertAsset(index, asset) {
|
|
889
|
+
if (this.getAssetReference(asset.id)) {
|
|
890
|
+
throw new DocumentValidationError(`Duplicate asset id: ${asset.id}`);
|
|
891
|
+
}
|
|
892
|
+
if (!Number.isInteger(index) || index < 0 || index > this.document.assets.length) {
|
|
893
|
+
throw new RangeError(`Invalid asset insertion index: ${index}`);
|
|
894
|
+
}
|
|
895
|
+
this.materializeAssetsInPlace();
|
|
896
|
+
this.document.assets.splice(index, 0, cloneValue(asset));
|
|
897
|
+
this.rebuildAssetIndex();
|
|
898
|
+
}
|
|
899
|
+
replaceAsset(asset) {
|
|
900
|
+
const index = this.assetOrder.get(asset.id);
|
|
901
|
+
if (index === void 0) {
|
|
902
|
+
throw new DocumentValidationError(`Asset not found: ${asset.id}`);
|
|
903
|
+
}
|
|
904
|
+
this.ensureAssetOverridesMutable();
|
|
905
|
+
this.assetOverrides.set(asset.id, cloneValue(asset));
|
|
906
|
+
}
|
|
907
|
+
removeAsset(id) {
|
|
908
|
+
const index = this.getAssetIndex(id);
|
|
909
|
+
if (index === void 0) {
|
|
910
|
+
throw new DocumentValidationError(`Asset not found: ${id}`);
|
|
911
|
+
}
|
|
912
|
+
this.materializeAssetsInPlace();
|
|
913
|
+
const [removed] = this.document.assets.splice(index, 1);
|
|
914
|
+
this.rebuildAssetIndex();
|
|
915
|
+
return cloneValue(removed);
|
|
916
|
+
}
|
|
917
|
+
rebuildIndexes() {
|
|
918
|
+
this.rebuildNodeIndex();
|
|
919
|
+
this.rebuildAssetIndex();
|
|
920
|
+
}
|
|
921
|
+
rebuildNodeIndex() {
|
|
922
|
+
this.nodesById = /* @__PURE__ */ new Map();
|
|
923
|
+
this.nodeOrder = /* @__PURE__ */ new Map();
|
|
924
|
+
this.document.nodes.forEach((node, index) => {
|
|
925
|
+
if (this.nodesById.has(node.id)) {
|
|
926
|
+
throw new DocumentValidationError(`Duplicate node id: ${node.id}`);
|
|
927
|
+
}
|
|
928
|
+
this.nodesById.set(node.id, node);
|
|
929
|
+
this.nodeOrder.set(node.id, index);
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
rebuildAssetIndex() {
|
|
933
|
+
this.assetsById = /* @__PURE__ */ new Map();
|
|
934
|
+
this.assetOrder = /* @__PURE__ */ new Map();
|
|
935
|
+
this.document.assets.forEach((asset, index) => {
|
|
936
|
+
if (this.assetsById.has(asset.id)) {
|
|
937
|
+
throw new DocumentValidationError(`Duplicate asset id: ${asset.id}`);
|
|
938
|
+
}
|
|
939
|
+
this.assetsById.set(asset.id, asset);
|
|
940
|
+
this.assetOrder.set(asset.id, index);
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
ensureNodeOverridesMutable() {
|
|
944
|
+
if (!this.nodeOverridesShared) return;
|
|
945
|
+
this.nodeOverrides = new Map(this.nodeOverrides);
|
|
946
|
+
this.nodeOverridesShared = false;
|
|
947
|
+
}
|
|
948
|
+
ensureAssetOverridesMutable() {
|
|
949
|
+
if (!this.assetOverridesShared) return;
|
|
950
|
+
this.assetOverrides = new Map(this.assetOverrides);
|
|
951
|
+
this.assetOverridesShared = false;
|
|
952
|
+
}
|
|
953
|
+
materializeNodesInPlace() {
|
|
954
|
+
if (this.nodeOverrides.size === 0 && !this.nodesShared) return;
|
|
955
|
+
this.document = { ...this.document, nodes: this.materializedNodes().slice() };
|
|
956
|
+
this.nodeOverrides = /* @__PURE__ */ new Map();
|
|
957
|
+
this.nodeOverridesShared = false;
|
|
958
|
+
this.nodesShared = false;
|
|
959
|
+
this.rebuildNodeIndex();
|
|
960
|
+
}
|
|
961
|
+
materializeAssetsInPlace() {
|
|
962
|
+
if (this.assetOverrides.size === 0 && !this.assetsShared) return;
|
|
963
|
+
this.document = { ...this.document, assets: this.materializedAssets().slice() };
|
|
964
|
+
this.assetOverrides = /* @__PURE__ */ new Map();
|
|
965
|
+
this.assetOverridesShared = false;
|
|
966
|
+
this.assetsShared = false;
|
|
967
|
+
this.rebuildAssetIndex();
|
|
968
|
+
}
|
|
969
|
+
materializedNodes() {
|
|
970
|
+
if (this.nodeOverrides.size === 0) return this.document.nodes;
|
|
971
|
+
return this.document.nodes.map((node) => this.nodeOverrides.get(node.id) ?? node);
|
|
972
|
+
}
|
|
973
|
+
materializedAssets() {
|
|
974
|
+
if (this.assetOverrides.size === 0) return this.document.assets;
|
|
975
|
+
return this.document.assets.map((asset) => this.assetOverrides.get(asset.id) ?? asset);
|
|
976
|
+
}
|
|
977
|
+
materializedDocument() {
|
|
978
|
+
return {
|
|
979
|
+
...this.document,
|
|
980
|
+
nodes: this.materializedNodes(),
|
|
981
|
+
assets: this.materializedAssets()
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
};
|
|
985
|
+
|
|
986
|
+
// ../core/src/viewport.ts
|
|
987
|
+
var ViewportController = class {
|
|
988
|
+
constructor(initial = { scale: 1, offset: { x: 0, y: 0 } }, screen, beforeMutation = () => {
|
|
989
|
+
}) {
|
|
990
|
+
this.beforeMutation = beforeMutation;
|
|
991
|
+
assertViewport(initial);
|
|
992
|
+
if (screen !== void 0) assertSize(screen);
|
|
993
|
+
this.state = cloneViewport(initial);
|
|
994
|
+
this.screen = screen ? { ...screen } : { width: 1, height: 1 };
|
|
995
|
+
this.screenSizeKnown = screen !== void 0;
|
|
996
|
+
}
|
|
997
|
+
beforeMutation;
|
|
998
|
+
state;
|
|
999
|
+
screen;
|
|
1000
|
+
/**
|
|
1001
|
+
* Whether a host has told us how big the surface actually is. The
|
|
1002
|
+
* constructor default is 1x1, which is a placeholder rather than a real
|
|
1003
|
+
* measurement — reporting visible bounds from it would cull everything but a
|
|
1004
|
+
* single world pixel, so visibleWorldBounds() stays undefined until a host
|
|
1005
|
+
* calls setScreenSize().
|
|
1006
|
+
*/
|
|
1007
|
+
screenSizeKnown;
|
|
1008
|
+
listeners = /* @__PURE__ */ new Set();
|
|
1009
|
+
get() {
|
|
1010
|
+
return cloneViewport(this.state);
|
|
1011
|
+
}
|
|
1012
|
+
set(viewport) {
|
|
1013
|
+
this.beforeMutation();
|
|
1014
|
+
assertViewport(viewport);
|
|
1015
|
+
this.commit(viewport);
|
|
1016
|
+
}
|
|
1017
|
+
setScreenSize(screen) {
|
|
1018
|
+
this.beforeMutation();
|
|
1019
|
+
assertSize(screen);
|
|
1020
|
+
this.screen = { ...screen };
|
|
1021
|
+
this.screenSizeKnown = true;
|
|
1022
|
+
}
|
|
1023
|
+
getScreenSize() {
|
|
1024
|
+
return { ...this.screen };
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* The world rectangle the surface currently shows, grown by `padding` world
|
|
1028
|
+
* units on every side so a renderer can keep a margin of ready views just
|
|
1029
|
+
* outside the viewport. Returns undefined while the screen size is still
|
|
1030
|
+
* unknown, which callers must read as "everything is visible" rather than
|
|
1031
|
+
* "nothing is".
|
|
1032
|
+
*/
|
|
1033
|
+
visibleWorldBounds(padding = 0) {
|
|
1034
|
+
if (!this.screenSizeKnown) return void 0;
|
|
1035
|
+
if (!Number.isFinite(padding) || padding < 0) {
|
|
1036
|
+
throw new RangeError("visible bounds padding must be finite and non-negative");
|
|
1037
|
+
}
|
|
1038
|
+
const topLeft = this.toWorld({ x: 0, y: 0 });
|
|
1039
|
+
const bottomRight = this.toWorld({ x: this.screen.width, y: this.screen.height });
|
|
1040
|
+
return {
|
|
1041
|
+
minX: topLeft.x - padding,
|
|
1042
|
+
minY: topLeft.y - padding,
|
|
1043
|
+
maxX: bottomRight.x + padding,
|
|
1044
|
+
maxY: bottomRight.y + padding
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
panBy(deltaX, deltaY) {
|
|
1048
|
+
this.beforeMutation();
|
|
1049
|
+
assertFinite(deltaX, "deltaX");
|
|
1050
|
+
assertFinite(deltaY, "deltaY");
|
|
1051
|
+
this.commit({
|
|
1052
|
+
scale: this.state.scale,
|
|
1053
|
+
offset: {
|
|
1054
|
+
x: this.state.offset.x + deltaX,
|
|
1055
|
+
y: this.state.offset.y + deltaY
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
zoomAt(screenPoint, factor) {
|
|
1060
|
+
this.beforeMutation();
|
|
1061
|
+
assertPoint(screenPoint, "screenPoint");
|
|
1062
|
+
if (!Number.isFinite(factor) || factor <= 0) {
|
|
1063
|
+
throw new RangeError("zoom factor must be finite and greater than zero");
|
|
1064
|
+
}
|
|
1065
|
+
const worldPoint = this.toWorld(screenPoint);
|
|
1066
|
+
const scale = this.state.scale * factor;
|
|
1067
|
+
this.commit({
|
|
1068
|
+
scale,
|
|
1069
|
+
offset: {
|
|
1070
|
+
x: screenPoint.x - worldPoint.x * scale,
|
|
1071
|
+
y: screenPoint.y - worldPoint.y * scale
|
|
1072
|
+
}
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
fitBounds(bounds, options = {}) {
|
|
1076
|
+
this.beforeMutation();
|
|
1077
|
+
assertWorldBounds(bounds);
|
|
1078
|
+
const screen = options.screen ?? this.screen;
|
|
1079
|
+
assertSize(screen);
|
|
1080
|
+
const padding = options.padding ?? 72;
|
|
1081
|
+
const maxScale = options.maxScale ?? 1;
|
|
1082
|
+
if (!Number.isFinite(padding) || padding < 0) {
|
|
1083
|
+
throw new RangeError("fit padding must be finite and non-negative");
|
|
1084
|
+
}
|
|
1085
|
+
if (!Number.isFinite(maxScale) || maxScale <= 0) {
|
|
1086
|
+
throw new RangeError("fit maxScale must be finite and greater than zero");
|
|
1087
|
+
}
|
|
1088
|
+
const boundsWidth = Math.max(bounds.maxX - bounds.minX, 1);
|
|
1089
|
+
const boundsHeight = Math.max(bounds.maxY - bounds.minY, 1);
|
|
1090
|
+
const availableWidth = Math.max(screen.width - padding * 2, 1);
|
|
1091
|
+
const availableHeight = Math.max(screen.height - padding * 2, 1);
|
|
1092
|
+
const scale = Math.min(availableWidth / boundsWidth, availableHeight / boundsHeight, maxScale);
|
|
1093
|
+
const centerX = (bounds.minX + bounds.maxX) / 2;
|
|
1094
|
+
const centerY = (bounds.minY + bounds.maxY) / 2;
|
|
1095
|
+
this.commit({
|
|
1096
|
+
scale,
|
|
1097
|
+
offset: {
|
|
1098
|
+
x: screen.width / 2 - centerX * scale,
|
|
1099
|
+
y: screen.height / 2 - centerY * scale
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
toWorld(screenPoint) {
|
|
1104
|
+
assertPoint(screenPoint, "screenPoint");
|
|
1105
|
+
return {
|
|
1106
|
+
x: (screenPoint.x - this.state.offset.x) / this.state.scale,
|
|
1107
|
+
y: (screenPoint.y - this.state.offset.y) / this.state.scale
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
toScreen(worldPoint) {
|
|
1111
|
+
assertPoint(worldPoint, "worldPoint");
|
|
1112
|
+
return {
|
|
1113
|
+
x: worldPoint.x * this.state.scale + this.state.offset.x,
|
|
1114
|
+
y: worldPoint.y * this.state.scale + this.state.offset.y
|
|
1115
|
+
};
|
|
1116
|
+
}
|
|
1117
|
+
onChange(listener) {
|
|
1118
|
+
this.listeners.add(listener);
|
|
1119
|
+
return () => this.listeners.delete(listener);
|
|
1120
|
+
}
|
|
1121
|
+
commit(viewport) {
|
|
1122
|
+
assertViewport(viewport);
|
|
1123
|
+
const previousViewport = this.get();
|
|
1124
|
+
if (previousViewport.scale === viewport.scale && previousViewport.offset.x === viewport.offset.x && previousViewport.offset.y === viewport.offset.y) {
|
|
1125
|
+
return;
|
|
1126
|
+
}
|
|
1127
|
+
this.state = cloneViewport(viewport);
|
|
1128
|
+
const event = { viewport: this.get(), previousViewport };
|
|
1129
|
+
for (const listener of this.listeners) {
|
|
1130
|
+
try {
|
|
1131
|
+
listener(event);
|
|
1132
|
+
} catch {
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
};
|
|
1137
|
+
function assertViewport(viewport) {
|
|
1138
|
+
if (!Number.isFinite(viewport.scale) || viewport.scale <= 0) {
|
|
1139
|
+
throw new RangeError("viewport scale must be finite and greater than zero");
|
|
1140
|
+
}
|
|
1141
|
+
assertPoint(viewport.offset, "viewport offset");
|
|
1142
|
+
}
|
|
1143
|
+
function assertPoint(point, label) {
|
|
1144
|
+
assertFinite(point.x, `${label}.x`);
|
|
1145
|
+
assertFinite(point.y, `${label}.y`);
|
|
1146
|
+
}
|
|
1147
|
+
function assertSize(size) {
|
|
1148
|
+
if (!Number.isFinite(size.width) || size.width <= 0 || !Number.isFinite(size.height) || size.height <= 0) {
|
|
1149
|
+
throw new RangeError("screen size must be finite and greater than zero");
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
function assertFinite(value, label) {
|
|
1153
|
+
if (!Number.isFinite(value)) throw new RangeError(`${label} must be finite`);
|
|
1154
|
+
}
|
|
1155
|
+
function cloneViewport(viewport) {
|
|
1156
|
+
return { scale: viewport.scale, offset: { ...viewport.offset } };
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
// ../core/src/core.ts
|
|
1160
|
+
var coreInternals = /* @__PURE__ */ new WeakMap();
|
|
1161
|
+
var BoardCore = class {
|
|
1162
|
+
nodeTypes;
|
|
1163
|
+
nodes;
|
|
1164
|
+
assets;
|
|
1165
|
+
selection;
|
|
1166
|
+
viewport;
|
|
1167
|
+
history;
|
|
1168
|
+
document;
|
|
1169
|
+
store;
|
|
1170
|
+
schemaVersion;
|
|
1171
|
+
idFactory;
|
|
1172
|
+
now;
|
|
1173
|
+
events = new EventHub();
|
|
1174
|
+
activeTransaction;
|
|
1175
|
+
rejectedAsyncTransactionCallbacks = 0;
|
|
1176
|
+
viewportState;
|
|
1177
|
+
recordHistory;
|
|
1178
|
+
constructor(options = {}) {
|
|
1179
|
+
this.nodeTypes = options.nodeTypes ?? new NodeTypeRegistry();
|
|
1180
|
+
this.schemaVersion = options.schemaVersion ?? 1;
|
|
1181
|
+
this.idFactory = options.idFactory ?? defaultIdFactory;
|
|
1182
|
+
this.now = options.now ?? Date.now;
|
|
1183
|
+
const document = options.document ? validateDocument(options.document, {
|
|
1184
|
+
schemaVersion: this.schemaVersion,
|
|
1185
|
+
nodeTypes: this.nodeTypes
|
|
1186
|
+
}) : createEmptyDocument(this.schemaVersion);
|
|
1187
|
+
this.store = new RuntimeDocumentStore(document);
|
|
1188
|
+
this.viewportState = new ViewportController(
|
|
1189
|
+
document.viewport ?? { scale: 1, offset: { x: 0, y: 0 } },
|
|
1190
|
+
options.viewportSize,
|
|
1191
|
+
() => this.assertSessionMutationAllowed()
|
|
1192
|
+
);
|
|
1193
|
+
coreInternals.set(this, {
|
|
1194
|
+
mutate: (label, operation) => this.mutate(label, operation),
|
|
1195
|
+
currentStore: () => this.currentStore(),
|
|
1196
|
+
nextId: () => this.nextId(),
|
|
1197
|
+
validateLoadedDocument: (input, loadOptions) => this.validateLoadedDocument(input, loadOptions),
|
|
1198
|
+
loadDocument: (input, loadOptions) => this.loadDocument(input, loadOptions),
|
|
1199
|
+
snapshotDocument: () => this.snapshotDocument(),
|
|
1200
|
+
jsonDocument: () => this.jsonDocument(),
|
|
1201
|
+
recordPatch: (transaction, forward, inverse) => this.recordPatch(transaction, forward, inverse)
|
|
1202
|
+
});
|
|
1203
|
+
this.selection = new SelectionController(
|
|
1204
|
+
(id) => this.currentStore().getNode(id) !== void 0,
|
|
1205
|
+
() => this.assertSessionMutationAllowed()
|
|
1206
|
+
);
|
|
1207
|
+
this.viewport = new BoardViewportController(this, this.viewportState);
|
|
1208
|
+
const history = createHistoryController({
|
|
1209
|
+
applyHistoryEntry: (entry, direction) => this.applyHistoryEntry(entry, direction)
|
|
1210
|
+
});
|
|
1211
|
+
this.history = history.controller;
|
|
1212
|
+
this.recordHistory = history.record;
|
|
1213
|
+
this.nodes = new BoardNodesController(this);
|
|
1214
|
+
this.assets = new BoardAssetsController(this);
|
|
1215
|
+
this.document = new BoardDocumentController(this);
|
|
1216
|
+
this.selection.onChange((event) => this.events.emit("selection:change", event));
|
|
1217
|
+
this.viewportState.onChange((event) => {
|
|
1218
|
+
this.events.emit("viewport:change", event);
|
|
1219
|
+
});
|
|
1220
|
+
this.history.onChange((event) => this.events.emit("history:change", event));
|
|
1221
|
+
}
|
|
1222
|
+
on(eventName, listener) {
|
|
1223
|
+
return this.events.on(eventName, listener);
|
|
1224
|
+
}
|
|
1225
|
+
transaction(label, operation, options = {}) {
|
|
1226
|
+
if (this.activeTransaction) {
|
|
1227
|
+
throw new TransactionConflictError("Nested transactions are not supported");
|
|
1228
|
+
}
|
|
1229
|
+
if (this.rejectedAsyncTransactionCallbacks > 0) {
|
|
1230
|
+
throw new TransactionConflictError(
|
|
1231
|
+
"Writes are blocked until a rejected async transaction callback settles"
|
|
1232
|
+
);
|
|
1233
|
+
}
|
|
1234
|
+
const transaction = {
|
|
1235
|
+
id: this.idFactory(),
|
|
1236
|
+
label: label || void 0,
|
|
1237
|
+
origin: options.origin ?? "api",
|
|
1238
|
+
...options.coalesceKey === void 0 ? {} : { coalesceKey: options.coalesceKey },
|
|
1239
|
+
draft: this.store.clone(),
|
|
1240
|
+
forward: [],
|
|
1241
|
+
inverse: []
|
|
1242
|
+
};
|
|
1243
|
+
this.activeTransaction = transaction;
|
|
1244
|
+
try {
|
|
1245
|
+
const result = operation(this);
|
|
1246
|
+
if (isPromiseLike(result)) {
|
|
1247
|
+
this.rejectedAsyncTransactionCallbacks += 1;
|
|
1248
|
+
void Promise.resolve(result).catch(() => void 0).finally(() => {
|
|
1249
|
+
this.rejectedAsyncTransactionCallbacks -= 1;
|
|
1250
|
+
});
|
|
1251
|
+
throw new TransactionConflictError(
|
|
1252
|
+
"Core transaction callbacks must be synchronous"
|
|
1253
|
+
);
|
|
1254
|
+
}
|
|
1255
|
+
this.activeTransaction = void 0;
|
|
1256
|
+
this.commitTransaction(transaction, true);
|
|
1257
|
+
return result;
|
|
1258
|
+
} catch (error) {
|
|
1259
|
+
this.activeTransaction = void 0;
|
|
1260
|
+
throw error;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
applyHistoryEntry(entry, direction) {
|
|
1264
|
+
if (this.activeTransaction || this.rejectedAsyncTransactionCallbacks > 0) {
|
|
1265
|
+
throw new TransactionConflictError("History cannot run during a transaction");
|
|
1266
|
+
}
|
|
1267
|
+
const transaction = {
|
|
1268
|
+
id: this.idFactory(),
|
|
1269
|
+
label: `${direction === "undo" ? "Undo" : "Redo"}${entry.label ? `: ${entry.label}` : ""}`,
|
|
1270
|
+
origin: "history",
|
|
1271
|
+
draft: this.store.clone(),
|
|
1272
|
+
forward: direction === "undo" ? entry.inverse : entry.forward,
|
|
1273
|
+
inverse: direction === "undo" ? entry.forward : entry.inverse
|
|
1274
|
+
};
|
|
1275
|
+
applyDataPatches(transaction.draft, transaction.forward);
|
|
1276
|
+
return this.commitTransaction(transaction, false);
|
|
1277
|
+
}
|
|
1278
|
+
getBounds(nodeId) {
|
|
1279
|
+
const node = this.currentStore().requireNode(nodeId);
|
|
1280
|
+
return this.nodeTypes.has(node.type) ? this.nodeTypes.getBounds(node) : rotatedRectBounds(node);
|
|
1281
|
+
}
|
|
1282
|
+
mutate(label, operation) {
|
|
1283
|
+
if (this.rejectedAsyncTransactionCallbacks > 0) {
|
|
1284
|
+
throw new TransactionConflictError(
|
|
1285
|
+
"Writes are blocked until a rejected async transaction callback settles"
|
|
1286
|
+
);
|
|
1287
|
+
}
|
|
1288
|
+
if (this.activeTransaction) return operation(this.activeTransaction);
|
|
1289
|
+
let result;
|
|
1290
|
+
this.transaction(label, () => {
|
|
1291
|
+
result = operation(this.activeTransaction);
|
|
1292
|
+
});
|
|
1293
|
+
return result;
|
|
1294
|
+
}
|
|
1295
|
+
currentStore() {
|
|
1296
|
+
return this.activeTransaction?.draft ?? this.store;
|
|
1297
|
+
}
|
|
1298
|
+
nextId() {
|
|
1299
|
+
return this.idFactory();
|
|
1300
|
+
}
|
|
1301
|
+
validateLoadedDocument(input, options = {}) {
|
|
1302
|
+
return validateDocument(input, {
|
|
1303
|
+
schemaVersion: this.schemaVersion,
|
|
1304
|
+
nodeTypes: this.nodeTypes
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
loadDocument(input, options = {}) {
|
|
1308
|
+
if (this.activeTransaction || this.rejectedAsyncTransactionCallbacks > 0) {
|
|
1309
|
+
throw new TransactionConflictError("A document cannot be loaded during a transaction");
|
|
1310
|
+
}
|
|
1311
|
+
if (options.replaceHistory === false) {
|
|
1312
|
+
throw new DocumentValidationError(
|
|
1313
|
+
"replaceHistory:false is not supported because history patches belong to the previous document"
|
|
1314
|
+
);
|
|
1315
|
+
}
|
|
1316
|
+
const nextDocument = this.validateLoadedDocument(input, options);
|
|
1317
|
+
const before = this.store;
|
|
1318
|
+
const previousViewport = this.viewportState.get();
|
|
1319
|
+
const hadSelection = this.selection.get().length > 0;
|
|
1320
|
+
this.store = new RuntimeDocumentStore(nextDocument);
|
|
1321
|
+
this.selection.clear();
|
|
1322
|
+
this.viewportState.set(nextDocument.viewport ?? { scale: 1, offset: { x: 0, y: 0 } });
|
|
1323
|
+
this.history.clear();
|
|
1324
|
+
const viewportChanged = !jsonEqual(previousViewport, this.viewportState.get());
|
|
1325
|
+
const changeSet = createChangeSet({
|
|
1326
|
+
before: before.mutableSnapshot(),
|
|
1327
|
+
after: nextDocument,
|
|
1328
|
+
transactionId: this.idFactory(),
|
|
1329
|
+
origin: "load",
|
|
1330
|
+
revision: nextDocument.revision,
|
|
1331
|
+
timestamp: this.now(),
|
|
1332
|
+
selectionChanged: hadSelection,
|
|
1333
|
+
viewportChanged
|
|
1334
|
+
});
|
|
1335
|
+
this.emitChange(changeSet);
|
|
1336
|
+
this.emitMissingNodeTypes(nextDocument.nodes);
|
|
1337
|
+
return changeSet;
|
|
1338
|
+
}
|
|
1339
|
+
snapshotDocument() {
|
|
1340
|
+
return this.store.snapshot();
|
|
1341
|
+
}
|
|
1342
|
+
jsonDocument() {
|
|
1343
|
+
return this.store.mutableSnapshot();
|
|
1344
|
+
}
|
|
1345
|
+
recordPatch(transaction, forward, inverse) {
|
|
1346
|
+
transaction.forward.push(forward);
|
|
1347
|
+
transaction.inverse.unshift(inverse);
|
|
1348
|
+
}
|
|
1349
|
+
commitTransaction(transaction, recordHistory2) {
|
|
1350
|
+
if (transaction.forward.length === 0 && recordHistory2) return void 0;
|
|
1351
|
+
const before = this.store;
|
|
1352
|
+
const after = transaction.draft;
|
|
1353
|
+
if (!hasEffectivePatchChanges(before, after, transaction.forward)) {
|
|
1354
|
+
return void 0;
|
|
1355
|
+
}
|
|
1356
|
+
transaction.draft.revision = before.revision + 1;
|
|
1357
|
+
this.store = after;
|
|
1358
|
+
const selectionChanged = this.selection.prune();
|
|
1359
|
+
const changeSet = createTransactionChangeSet({
|
|
1360
|
+
before,
|
|
1361
|
+
after,
|
|
1362
|
+
patches: transaction.forward,
|
|
1363
|
+
transactionId: transaction.id,
|
|
1364
|
+
label: transaction.label,
|
|
1365
|
+
origin: transaction.origin,
|
|
1366
|
+
revision: after.revision,
|
|
1367
|
+
timestamp: this.now(),
|
|
1368
|
+
selectionChanged,
|
|
1369
|
+
viewportChanged: false
|
|
1370
|
+
});
|
|
1371
|
+
if (recordHistory2) {
|
|
1372
|
+
this.recordHistory({
|
|
1373
|
+
label: transaction.label,
|
|
1374
|
+
origin: transaction.origin,
|
|
1375
|
+
...transaction.coalesceKey === void 0 ? {} : { coalesceKey: transaction.coalesceKey },
|
|
1376
|
+
forward: transaction.forward,
|
|
1377
|
+
inverse: transaction.inverse
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
this.emitChange(changeSet);
|
|
1381
|
+
return changeSet;
|
|
1382
|
+
}
|
|
1383
|
+
emitChange(changeSet) {
|
|
1384
|
+
const changedNodeIds = /* @__PURE__ */ new Set([
|
|
1385
|
+
...changeSet.addedNodeIds,
|
|
1386
|
+
...changeSet.updatedNodeIds,
|
|
1387
|
+
...changeSet.assetChangedNodeIds
|
|
1388
|
+
]);
|
|
1389
|
+
const changedNodes = [];
|
|
1390
|
+
for (const nodeId of changedNodeIds) {
|
|
1391
|
+
const node = this.store.getNodeReference(nodeId);
|
|
1392
|
+
if (node) changedNodes.push(node);
|
|
1393
|
+
}
|
|
1394
|
+
this.events.emit("change", {
|
|
1395
|
+
revision: changeSet.revision,
|
|
1396
|
+
changeSet: immutableClone(changeSet),
|
|
1397
|
+
documentUpdate: immutableClone({
|
|
1398
|
+
revision: changeSet.revision,
|
|
1399
|
+
changedNodes
|
|
1400
|
+
})
|
|
1401
|
+
});
|
|
1402
|
+
}
|
|
1403
|
+
emitMissingNodeTypes(nodes) {
|
|
1404
|
+
const missing = /* @__PURE__ */ new Map();
|
|
1405
|
+
for (const node of nodes) {
|
|
1406
|
+
if (this.nodeTypes.has(node.type)) continue;
|
|
1407
|
+
const ids = missing.get(node.type) ?? [];
|
|
1408
|
+
ids.push(node.id);
|
|
1409
|
+
missing.set(node.type, ids);
|
|
1410
|
+
}
|
|
1411
|
+
for (const [type, nodeIds] of missing) {
|
|
1412
|
+
this.events.emit("node-type:missing", { type, nodeIds: [...nodeIds] });
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
assertSessionMutationAllowed() {
|
|
1416
|
+
if (this.activeTransaction || this.rejectedAsyncTransactionCallbacks > 0) {
|
|
1417
|
+
throw new TransactionConflictError(
|
|
1418
|
+
"Selection and viewport cannot change during a document transaction"
|
|
1419
|
+
);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
};
|
|
1423
|
+
var BoardNodesController = class {
|
|
1424
|
+
constructor(core) {
|
|
1425
|
+
this.core = core;
|
|
1426
|
+
}
|
|
1427
|
+
core;
|
|
1428
|
+
create(input) {
|
|
1429
|
+
const internals = getCoreInternals(this.core);
|
|
1430
|
+
return internals.mutate("Create node", (transaction) => {
|
|
1431
|
+
const { typeVersion, props } = this.core.nodeTypes.createProps(input.type, input.props);
|
|
1432
|
+
const node = validateNode({
|
|
1433
|
+
id: input.id ?? internals.nextId(),
|
|
1434
|
+
type: input.type,
|
|
1435
|
+
typeVersion: input.typeVersion ?? typeVersion,
|
|
1436
|
+
x: input.x,
|
|
1437
|
+
y: input.y,
|
|
1438
|
+
width: input.width,
|
|
1439
|
+
height: input.height,
|
|
1440
|
+
rotation: input.rotation ?? 0,
|
|
1441
|
+
zIndex: input.zIndex ?? 0,
|
|
1442
|
+
props,
|
|
1443
|
+
...input.name === void 0 ? {} : { name: input.name },
|
|
1444
|
+
...input.locked === void 0 ? {} : { locked: input.locked },
|
|
1445
|
+
...input.visible === void 0 ? {} : { visible: input.visible },
|
|
1446
|
+
...input.assetRefs === void 0 ? {} : { assetRefs: cloneValue(input.assetRefs) }
|
|
1447
|
+
});
|
|
1448
|
+
const validated = this.core.nodeTypes.validateNode(node);
|
|
1449
|
+
const index = transaction.draft.nodeCount();
|
|
1450
|
+
transaction.draft.insertNode(index, validated);
|
|
1451
|
+
internals.recordPatch(
|
|
1452
|
+
transaction,
|
|
1453
|
+
{ op: "node:insert", index, node: validated },
|
|
1454
|
+
{ op: "node:remove", nodeId: validated.id }
|
|
1455
|
+
);
|
|
1456
|
+
return immutableClone(validated);
|
|
1457
|
+
});
|
|
1458
|
+
}
|
|
1459
|
+
update(nodeId, patch) {
|
|
1460
|
+
const internals = getCoreInternals(this.core);
|
|
1461
|
+
return internals.mutate("Update node", (transaction) => {
|
|
1462
|
+
const previous = transaction.draft.requireNode(nodeId);
|
|
1463
|
+
if ("props" in patch && !this.core.nodeTypes.has(previous.type)) {
|
|
1464
|
+
throw new NodeTypeNotRegisteredError(previous.type);
|
|
1465
|
+
}
|
|
1466
|
+
const next = validateNode({ ...previous, ...cloneValue(patch), id: previous.id, type: previous.type });
|
|
1467
|
+
const validated = this.core.nodeTypes.has(next.type) ? this.core.nodeTypes.validateNode(next) : next;
|
|
1468
|
+
if (jsonEqual(previous, validated)) return immutableClone(previous);
|
|
1469
|
+
transaction.draft.replaceNode(validated);
|
|
1470
|
+
internals.recordPatch(
|
|
1471
|
+
transaction,
|
|
1472
|
+
{ op: "node:replace", node: validated },
|
|
1473
|
+
{ op: "node:replace", node: previous }
|
|
1474
|
+
);
|
|
1475
|
+
return immutableClone(validated);
|
|
1476
|
+
});
|
|
1477
|
+
}
|
|
1478
|
+
/**
|
|
1479
|
+
* Applies one resize gesture step through the node type's `ResizePolicy`.
|
|
1480
|
+
* The policy decides what the requested handle drag is allowed to become —
|
|
1481
|
+
* `fixed` refuses it outright, `aspect-ratio` locks the two axes together
|
|
1482
|
+
* and `custom` gets the last word on the resulting size — so hosts never
|
|
1483
|
+
* need to reimplement those rules on top of `update()`.
|
|
1484
|
+
*/
|
|
1485
|
+
resize(nodeId, request) {
|
|
1486
|
+
const node = getCoreInternals(this.core).currentStore().requireNode(nodeId);
|
|
1487
|
+
const policy = this.core.nodeTypes.get(node.type)?.resize;
|
|
1488
|
+
const patch = resolveResize(node, policy, request);
|
|
1489
|
+
if (Object.keys(patch).length === 0) return immutableClone(node);
|
|
1490
|
+
return this.update(nodeId, patch);
|
|
1491
|
+
}
|
|
1492
|
+
remove(nodeId) {
|
|
1493
|
+
const internals = getCoreInternals(this.core);
|
|
1494
|
+
return internals.mutate("Remove node", (transaction) => {
|
|
1495
|
+
const index = transaction.draft.getNodeIndex(nodeId);
|
|
1496
|
+
if (index === void 0) throw new NodeNotFoundError(nodeId);
|
|
1497
|
+
const removed = transaction.draft.removeNode(nodeId);
|
|
1498
|
+
internals.recordPatch(
|
|
1499
|
+
transaction,
|
|
1500
|
+
{ op: "node:remove", nodeId },
|
|
1501
|
+
{ op: "node:insert", index, node: removed }
|
|
1502
|
+
);
|
|
1503
|
+
return immutableClone(removed);
|
|
1504
|
+
});
|
|
1505
|
+
}
|
|
1506
|
+
get(nodeId) {
|
|
1507
|
+
const node = getCoreInternals(this.core).currentStore().getNode(nodeId);
|
|
1508
|
+
return node ? immutableClone(node) : void 0;
|
|
1509
|
+
}
|
|
1510
|
+
list(filter = {}) {
|
|
1511
|
+
const idFilter = filter.ids ? new Set(filter.ids) : void 0;
|
|
1512
|
+
const typeFilter = filter.types ? new Set(filter.types) : void 0;
|
|
1513
|
+
const limit = filter.limit ?? Number.POSITIVE_INFINITY;
|
|
1514
|
+
if (!Number.isInteger(limit) && limit !== Number.POSITIVE_INFINITY) {
|
|
1515
|
+
throw new RangeError("Node list limit must be an integer");
|
|
1516
|
+
}
|
|
1517
|
+
if (limit < 0) throw new RangeError("Node list limit must not be negative");
|
|
1518
|
+
const store = getCoreInternals(this.core).currentStore();
|
|
1519
|
+
const selected = filter.selected === void 0 ? void 0 : new Set(this.core.selection.get());
|
|
1520
|
+
const source = idFilter ? store.listNodesByIds(idFilter) : store.listNodes();
|
|
1521
|
+
const result = source.filter((node) => {
|
|
1522
|
+
if (idFilter && !idFilter.has(node.id)) return false;
|
|
1523
|
+
if (typeFilter && !typeFilter.has(node.type)) return false;
|
|
1524
|
+
if (filter.type && node.type !== filter.type) return false;
|
|
1525
|
+
if (filter.bounds && !boundsIntersect(this.core.getBounds(node.id), filter.bounds)) return false;
|
|
1526
|
+
if (filter.visible !== void 0 && (node.visible ?? true) !== filter.visible) return false;
|
|
1527
|
+
if (filter.selected !== void 0 && selected.has(node.id) !== filter.selected) return false;
|
|
1528
|
+
return true;
|
|
1529
|
+
});
|
|
1530
|
+
return deepFreeze(result.slice(0, limit));
|
|
1531
|
+
}
|
|
1532
|
+
};
|
|
1533
|
+
function boundsIntersect(left, right) {
|
|
1534
|
+
return left.maxX >= right.minX && left.minX <= right.maxX && left.maxY >= right.minY && left.minY <= right.maxY;
|
|
1535
|
+
}
|
|
1536
|
+
var BoardAssetsController = class {
|
|
1537
|
+
constructor(core) {
|
|
1538
|
+
this.core = core;
|
|
1539
|
+
}
|
|
1540
|
+
core;
|
|
1541
|
+
upsert(assetInput) {
|
|
1542
|
+
const internals = getCoreInternals(this.core);
|
|
1543
|
+
return internals.mutate("Upsert asset", (transaction) => {
|
|
1544
|
+
const asset = validateAsset(assetInput);
|
|
1545
|
+
const previous = transaction.draft.getAsset(asset.id);
|
|
1546
|
+
if (previous && jsonEqual(previous, asset)) return immutableClone(previous);
|
|
1547
|
+
if (previous) {
|
|
1548
|
+
transaction.draft.replaceAsset(asset);
|
|
1549
|
+
internals.recordPatch(
|
|
1550
|
+
transaction,
|
|
1551
|
+
{ op: "asset:replace", asset },
|
|
1552
|
+
{ op: "asset:replace", asset: previous }
|
|
1553
|
+
);
|
|
1554
|
+
} else {
|
|
1555
|
+
const index = transaction.draft.assetCount();
|
|
1556
|
+
transaction.draft.insertAsset(index, asset);
|
|
1557
|
+
internals.recordPatch(
|
|
1558
|
+
transaction,
|
|
1559
|
+
{ op: "asset:insert", index, asset },
|
|
1560
|
+
{ op: "asset:remove", assetId: asset.id }
|
|
1561
|
+
);
|
|
1562
|
+
}
|
|
1563
|
+
return immutableClone(asset);
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
remove(assetId) {
|
|
1567
|
+
const internals = getCoreInternals(this.core);
|
|
1568
|
+
return internals.mutate("Remove asset", (transaction) => {
|
|
1569
|
+
const index = transaction.draft.getAssetIndex(assetId);
|
|
1570
|
+
if (index === void 0) throw new DocumentValidationError(`Asset not found: ${assetId}`);
|
|
1571
|
+
const asset = transaction.draft.removeAsset(assetId);
|
|
1572
|
+
internals.recordPatch(
|
|
1573
|
+
transaction,
|
|
1574
|
+
{ op: "asset:remove", assetId },
|
|
1575
|
+
{ op: "asset:insert", index, asset }
|
|
1576
|
+
);
|
|
1577
|
+
return immutableClone(asset);
|
|
1578
|
+
});
|
|
1579
|
+
}
|
|
1580
|
+
get(assetId) {
|
|
1581
|
+
const asset = getCoreInternals(this.core).currentStore().getAsset(assetId);
|
|
1582
|
+
return asset ? immutableClone(asset) : void 0;
|
|
1583
|
+
}
|
|
1584
|
+
list() {
|
|
1585
|
+
return immutableClone(getCoreInternals(this.core).currentStore().listAssets());
|
|
1586
|
+
}
|
|
1587
|
+
};
|
|
1588
|
+
var BoardViewportController = class {
|
|
1589
|
+
constructor(core, viewport) {
|
|
1590
|
+
this.core = core;
|
|
1591
|
+
this.viewport = viewport;
|
|
1592
|
+
}
|
|
1593
|
+
core;
|
|
1594
|
+
viewport;
|
|
1595
|
+
get() {
|
|
1596
|
+
return this.viewport.get();
|
|
1597
|
+
}
|
|
1598
|
+
set(value) {
|
|
1599
|
+
this.viewport.set(value);
|
|
1600
|
+
}
|
|
1601
|
+
setScreenSize(size) {
|
|
1602
|
+
this.viewport.setScreenSize(size);
|
|
1603
|
+
}
|
|
1604
|
+
getScreenSize() {
|
|
1605
|
+
return this.viewport.getScreenSize();
|
|
1606
|
+
}
|
|
1607
|
+
visibleWorldBounds(padding) {
|
|
1608
|
+
return this.viewport.visibleWorldBounds(padding);
|
|
1609
|
+
}
|
|
1610
|
+
panBy(deltaX, deltaY) {
|
|
1611
|
+
this.viewport.panBy(deltaX, deltaY);
|
|
1612
|
+
}
|
|
1613
|
+
zoomAt(screenPoint, factor) {
|
|
1614
|
+
this.viewport.zoomAt(screenPoint, factor);
|
|
1615
|
+
}
|
|
1616
|
+
fitNodes(nodeIds, options) {
|
|
1617
|
+
const bounds = mergeBounds(nodeIds.map((nodeId) => this.core.getBounds(nodeId)));
|
|
1618
|
+
if (!bounds) return;
|
|
1619
|
+
this.viewport.fitBounds(bounds, options);
|
|
1620
|
+
}
|
|
1621
|
+
fitBounds(bounds, options) {
|
|
1622
|
+
this.viewport.fitBounds(bounds, options);
|
|
1623
|
+
}
|
|
1624
|
+
toWorld(screenPoint) {
|
|
1625
|
+
return this.viewport.toWorld(screenPoint);
|
|
1626
|
+
}
|
|
1627
|
+
toScreen(worldPoint) {
|
|
1628
|
+
return this.viewport.toScreen(worldPoint);
|
|
1629
|
+
}
|
|
1630
|
+
onChange(listener) {
|
|
1631
|
+
return this.viewport.onChange(listener);
|
|
1632
|
+
}
|
|
1633
|
+
};
|
|
1634
|
+
var BoardDocumentController = class {
|
|
1635
|
+
constructor(core) {
|
|
1636
|
+
this.core = core;
|
|
1637
|
+
}
|
|
1638
|
+
core;
|
|
1639
|
+
snapshot() {
|
|
1640
|
+
return getCoreInternals(this.core).snapshotDocument();
|
|
1641
|
+
}
|
|
1642
|
+
toJSON() {
|
|
1643
|
+
return getCoreInternals(this.core).jsonDocument();
|
|
1644
|
+
}
|
|
1645
|
+
stringify(space) {
|
|
1646
|
+
return JSON.stringify(this.toJSON(), null, space);
|
|
1647
|
+
}
|
|
1648
|
+
load(input, options) {
|
|
1649
|
+
return getCoreInternals(this.core).loadDocument(input, options);
|
|
1650
|
+
}
|
|
1651
|
+
validate(input) {
|
|
1652
|
+
return getCoreInternals(this.core).validateLoadedDocument(input);
|
|
1653
|
+
}
|
|
1654
|
+
};
|
|
1655
|
+
function createBoardCore(options) {
|
|
1656
|
+
return new BoardCore(options);
|
|
1657
|
+
}
|
|
1658
|
+
function getCoreInternals(core) {
|
|
1659
|
+
const internals = coreInternals.get(core);
|
|
1660
|
+
if (!internals) throw new Error("BoardCore internals are unavailable");
|
|
1661
|
+
return internals;
|
|
1662
|
+
}
|
|
1663
|
+
function createEmptyDocument(schemaVersion) {
|
|
1664
|
+
if (!Number.isInteger(schemaVersion) || schemaVersion < 1) {
|
|
1665
|
+
throw new DocumentValidationError("schemaVersion must be a positive integer");
|
|
1666
|
+
}
|
|
1667
|
+
return { schemaVersion, revision: 0, nodes: [], assets: [] };
|
|
1668
|
+
}
|
|
1669
|
+
function defaultIdFactory() {
|
|
1670
|
+
return globalThis.crypto.randomUUID();
|
|
1671
|
+
}
|
|
1672
|
+
function isPromiseLike(value) {
|
|
1673
|
+
return value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
1674
|
+
}
|
|
1675
|
+
function createChangeSet(input) {
|
|
1676
|
+
const beforeNodes = new Map(input.before.nodes.map((node) => [node.id, node]));
|
|
1677
|
+
const afterNodes = new Map(input.after.nodes.map((node) => [node.id, node]));
|
|
1678
|
+
const beforeNodeOrder = new Map(input.before.nodes.map((node, index) => [node.id, index]));
|
|
1679
|
+
const addedNodeIds = input.after.nodes.filter((node) => !beforeNodes.has(node.id)).map((node) => node.id);
|
|
1680
|
+
const updatedNodeIds = input.after.nodes.filter((node, index) => {
|
|
1681
|
+
const previous = beforeNodes.get(node.id);
|
|
1682
|
+
return previous !== void 0 && (!jsonEqual(previous, node) || beforeNodeOrder.get(node.id) !== index);
|
|
1683
|
+
}).map((node) => node.id);
|
|
1684
|
+
const removedNodeIds = input.before.nodes.filter((node) => !afterNodes.has(node.id)).map((node) => node.id);
|
|
1685
|
+
const beforeAssets = new Map(input.before.assets.map((asset) => [asset.id, asset]));
|
|
1686
|
+
const afterAssets = new Map(input.after.assets.map((asset) => [asset.id, asset]));
|
|
1687
|
+
const changedAssetIds = /* @__PURE__ */ new Set();
|
|
1688
|
+
for (const asset of input.before.assets) {
|
|
1689
|
+
const next = afterAssets.get(asset.id);
|
|
1690
|
+
if (!next || !jsonEqual(asset, next)) changedAssetIds.add(asset.id);
|
|
1691
|
+
}
|
|
1692
|
+
for (const asset of input.after.assets) {
|
|
1693
|
+
const previous = beforeAssets.get(asset.id);
|
|
1694
|
+
if (!previous || !jsonEqual(previous, asset)) changedAssetIds.add(asset.id);
|
|
1695
|
+
}
|
|
1696
|
+
const assetChangedNodeIds = input.after.nodes.filter((node) => {
|
|
1697
|
+
const previous = beforeNodes.get(node.id);
|
|
1698
|
+
if (previous && !jsonEqual(previous.assetRefs, node.assetRefs)) return true;
|
|
1699
|
+
return Object.values(node.assetRefs ?? {}).some((ref) => changedAssetIds.has(ref.assetId));
|
|
1700
|
+
}).map((node) => node.id);
|
|
1701
|
+
return {
|
|
1702
|
+
transactionId: input.transactionId,
|
|
1703
|
+
revision: input.revision,
|
|
1704
|
+
...input.label ? { label: input.label } : {},
|
|
1705
|
+
origin: input.origin,
|
|
1706
|
+
addedNodeIds,
|
|
1707
|
+
updatedNodeIds,
|
|
1708
|
+
removedNodeIds,
|
|
1709
|
+
assetChangedNodeIds,
|
|
1710
|
+
selectionChanged: input.selectionChanged,
|
|
1711
|
+
viewportChanged: input.viewportChanged,
|
|
1712
|
+
timestamp: input.timestamp
|
|
1713
|
+
};
|
|
1714
|
+
}
|
|
1715
|
+
function hasEffectivePatchChanges(before, after, patches) {
|
|
1716
|
+
if (patches.some((patch) => patch.op.endsWith(":insert") || patch.op.endsWith(":remove"))) {
|
|
1717
|
+
const beforeSnapshot = before.mutableSnapshot();
|
|
1718
|
+
const afterSnapshot = after.mutableSnapshot();
|
|
1719
|
+
return !jsonEqual(beforeSnapshot.nodes, afterSnapshot.nodes) || !jsonEqual(beforeSnapshot.assets, afterSnapshot.assets);
|
|
1720
|
+
}
|
|
1721
|
+
for (const patch of patches) {
|
|
1722
|
+
if (patch.op === "node:replace") {
|
|
1723
|
+
if (!jsonEqual(before.getNodeReference(patch.node.id), after.getNodeReference(patch.node.id))) {
|
|
1724
|
+
return true;
|
|
1725
|
+
}
|
|
1726
|
+
} else if (patch.op === "asset:replace") {
|
|
1727
|
+
if (!jsonEqual(before.getAssetReference(patch.asset.id), after.getAssetReference(patch.asset.id))) {
|
|
1728
|
+
return true;
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
return false;
|
|
1733
|
+
}
|
|
1734
|
+
function createTransactionChangeSet(input) {
|
|
1735
|
+
const structural = input.patches.some(
|
|
1736
|
+
(patch) => patch.op.endsWith(":insert") || patch.op.endsWith(":remove")
|
|
1737
|
+
);
|
|
1738
|
+
if (structural) {
|
|
1739
|
+
return createChangeSet({
|
|
1740
|
+
before: input.before.mutableSnapshot(),
|
|
1741
|
+
after: input.after.mutableSnapshot(),
|
|
1742
|
+
transactionId: input.transactionId,
|
|
1743
|
+
label: input.label,
|
|
1744
|
+
origin: input.origin,
|
|
1745
|
+
revision: input.revision,
|
|
1746
|
+
timestamp: input.timestamp,
|
|
1747
|
+
selectionChanged: input.selectionChanged,
|
|
1748
|
+
viewportChanged: input.viewportChanged
|
|
1749
|
+
});
|
|
1750
|
+
}
|
|
1751
|
+
const updatedIds = /* @__PURE__ */ new Set();
|
|
1752
|
+
const changedAssetIds = /* @__PURE__ */ new Set();
|
|
1753
|
+
for (const patch of input.patches) {
|
|
1754
|
+
if (patch.op === "node:replace") updatedIds.add(patch.node.id);
|
|
1755
|
+
if (patch.op === "asset:replace") changedAssetIds.add(patch.asset.id);
|
|
1756
|
+
}
|
|
1757
|
+
const updatedNodeIds = [...updatedIds].filter((id) => {
|
|
1758
|
+
const before = input.before.getNodeReference(id);
|
|
1759
|
+
const after = input.after.getNodeReference(id);
|
|
1760
|
+
return before !== void 0 && after !== void 0 && !jsonEqual(before, after);
|
|
1761
|
+
}).sort(
|
|
1762
|
+
(left, right) => (input.after.getNodeIndex(left) ?? Number.POSITIVE_INFINITY) - (input.after.getNodeIndex(right) ?? Number.POSITIVE_INFINITY)
|
|
1763
|
+
);
|
|
1764
|
+
const assetChangedNodeIds = /* @__PURE__ */ new Set();
|
|
1765
|
+
for (const id of updatedIds) {
|
|
1766
|
+
const before = input.before.getNodeReference(id);
|
|
1767
|
+
const after = input.after.getNodeReference(id);
|
|
1768
|
+
if (before && after && !jsonEqual(before.assetRefs, after.assetRefs)) {
|
|
1769
|
+
assetChangedNodeIds.add(id);
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
if (changedAssetIds.size > 0) {
|
|
1773
|
+
input.after.forEachNodeReference((node) => {
|
|
1774
|
+
if (Object.values(node.assetRefs ?? {}).some((ref) => changedAssetIds.has(ref.assetId))) {
|
|
1775
|
+
assetChangedNodeIds.add(node.id);
|
|
1776
|
+
}
|
|
1777
|
+
});
|
|
1778
|
+
}
|
|
1779
|
+
return {
|
|
1780
|
+
transactionId: input.transactionId,
|
|
1781
|
+
revision: input.revision,
|
|
1782
|
+
...input.label ? { label: input.label } : {},
|
|
1783
|
+
origin: input.origin,
|
|
1784
|
+
addedNodeIds: [],
|
|
1785
|
+
updatedNodeIds,
|
|
1786
|
+
removedNodeIds: [],
|
|
1787
|
+
assetChangedNodeIds: [...assetChangedNodeIds].sort(
|
|
1788
|
+
(left, right) => (input.after.getNodeIndex(left) ?? Number.POSITIVE_INFINITY) - (input.after.getNodeIndex(right) ?? Number.POSITIVE_INFINITY)
|
|
1789
|
+
),
|
|
1790
|
+
selectionChanged: input.selectionChanged,
|
|
1791
|
+
viewportChanged: input.viewportChanged,
|
|
1792
|
+
timestamp: input.timestamp
|
|
1793
|
+
};
|
|
1794
|
+
}
|
|
1795
|
+
var EventHub = class {
|
|
1796
|
+
listeners = /* @__PURE__ */ new Map();
|
|
1797
|
+
on(eventName, listener) {
|
|
1798
|
+
const listeners = this.listeners.get(eventName) ?? /* @__PURE__ */ new Set();
|
|
1799
|
+
listeners.add(listener);
|
|
1800
|
+
this.listeners.set(eventName, listeners);
|
|
1801
|
+
return () => listeners.delete(listener);
|
|
1802
|
+
}
|
|
1803
|
+
emit(eventName, event) {
|
|
1804
|
+
for (const listener of this.listeners.get(eventName) ?? []) {
|
|
1805
|
+
try {
|
|
1806
|
+
listener(event);
|
|
1807
|
+
} catch {
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
};
|
|
1812
|
+
|
|
1813
|
+
// ../capabilities/src/errors.ts
|
|
1814
|
+
var CAPABILITY_ERROR_BRAND = "@pixi-board/capabilities:CapabilityError";
|
|
1815
|
+
var CapabilityError = class extends Error {
|
|
1816
|
+
name = "CapabilityError";
|
|
1817
|
+
brand = CAPABILITY_ERROR_BRAND;
|
|
1818
|
+
code;
|
|
1819
|
+
details;
|
|
1820
|
+
constructor(code, message, details) {
|
|
1821
|
+
super(message);
|
|
1822
|
+
this.code = code;
|
|
1823
|
+
this.details = details;
|
|
1824
|
+
}
|
|
1825
|
+
};
|
|
1826
|
+
function isCapabilityError(value) {
|
|
1827
|
+
return value instanceof CapabilityError || value instanceof Error && value.brand === CAPABILITY_ERROR_BRAND;
|
|
1828
|
+
}
|
|
1829
|
+
var CapabilityUnavailableError = class extends CapabilityError {
|
|
1830
|
+
capability;
|
|
1831
|
+
constructor(capability) {
|
|
1832
|
+
super("CAPABILITY_UNAVAILABLE", `Capability unavailable: ${capability}`, { capability });
|
|
1833
|
+
this.capability = capability;
|
|
1834
|
+
}
|
|
1835
|
+
};
|
|
1836
|
+
var AssetUnavailableError = class extends CapabilityError {
|
|
1837
|
+
constructor(assetId, variant) {
|
|
1838
|
+
super("ASSET_UNAVAILABLE", `Asset unavailable: ${assetId}${variant ? ` (${variant})` : ""}`, { assetId, variant });
|
|
1839
|
+
}
|
|
1840
|
+
};
|
|
1841
|
+
var PermissionDeniedError = class extends CapabilityError {
|
|
1842
|
+
constructor(message = "Permission denied", details) {
|
|
1843
|
+
super("PERMISSION_DENIED", message, details);
|
|
1844
|
+
}
|
|
1845
|
+
};
|
|
1846
|
+
var BoardDestroyedError = class extends CapabilityError {
|
|
1847
|
+
constructor(message = "Board has been destroyed") {
|
|
1848
|
+
super("BOARD_DESTROYED", message);
|
|
1849
|
+
}
|
|
1850
|
+
};
|
|
1851
|
+
function mapCoreError(error) {
|
|
1852
|
+
if (isCapabilityError(error)) return error;
|
|
1853
|
+
const { NodeNotFoundError: NodeNotFoundError2, NodeTypeNotRegisteredError: NodeTypeNotRegisteredError2, NodeValidationError: NodeValidationError2, DocumentValidationError: DocumentValidationError2, TransactionConflictError: TransactionConflictError2 } = src_exports;
|
|
1854
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1855
|
+
const code = error instanceof NodeNotFoundError2 ? "NODE_NOT_FOUND" : error instanceof NodeTypeNotRegisteredError2 ? "NODE_TYPE_NOT_REGISTERED" : error instanceof NodeValidationError2 ? "NODE_VALIDATION" : error instanceof DocumentValidationError2 ? "DOCUMENT_VALIDATION" : error instanceof TransactionConflictError2 ? "TRANSACTION_CONFLICT" : error instanceof RangeError ? "INVALID_INPUT" : error instanceof AssetUnavailableError ? "ASSET_UNAVAILABLE" : error instanceof PermissionDeniedError ? "PERMISSION_DENIED" : error instanceof BoardDestroyedError ? "BOARD_DESTROYED" : error instanceof Error && error.name === "AssetUnavailableError" ? "ASSET_UNAVAILABLE" : error instanceof Error && error.name === "PermissionDeniedError" ? "PERMISSION_DENIED" : error instanceof Error && error.name === "BoardDestroyedError" ? "BOARD_DESTROYED" : "INTERNAL_ERROR";
|
|
1856
|
+
return new CapabilityError(code, message);
|
|
1857
|
+
}
|
|
1858
|
+
function aborted(signal) {
|
|
1859
|
+
if (signal?.aborted) throw new CapabilityError("ABORTED", "The capability request was aborted");
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
// ../capabilities/src/index.ts
|
|
1863
|
+
function createBoardCapabilities(core, services = {}) {
|
|
1864
|
+
const transact = (label, options, operation) => {
|
|
1865
|
+
aborted(options.signal);
|
|
1866
|
+
let changeSet;
|
|
1867
|
+
const off = core.on("change", (event) => {
|
|
1868
|
+
changeSet = event.changeSet;
|
|
1869
|
+
});
|
|
1870
|
+
try {
|
|
1871
|
+
const result = core.transaction(label, operation, { origin: options.origin ?? "api" });
|
|
1872
|
+
return { result, changed: changeSet !== void 0, changeSet };
|
|
1873
|
+
} catch (error) {
|
|
1874
|
+
throw mapCoreError(error);
|
|
1875
|
+
} finally {
|
|
1876
|
+
off();
|
|
1877
|
+
}
|
|
1878
|
+
};
|
|
1879
|
+
const page = (items, requested = 50, cursor) => {
|
|
1880
|
+
const limit = Math.min(Math.max(requested, 1), 200);
|
|
1881
|
+
const start = cursor ? Math.max(0, Number.parseInt(cursor, 10) || 0) : 0;
|
|
1882
|
+
const out = items.slice(start, start + limit);
|
|
1883
|
+
const hasMore = start + out.length < items.length;
|
|
1884
|
+
return { out, page: { hasMore, ...hasMore ? { nextCursor: String(start + out.length) } : {} } };
|
|
1885
|
+
};
|
|
1886
|
+
const writeResult = (value) => ({
|
|
1887
|
+
changed: value.changed,
|
|
1888
|
+
nodes: value.nodes ?? [],
|
|
1889
|
+
...value.assets ? { assets: value.assets } : {},
|
|
1890
|
+
...value.deletedNodeIds ? { deletedNodeIds: value.deletedNodeIds } : {},
|
|
1891
|
+
...value.deletedAssetIds ? { deletedAssetIds: value.deletedAssetIds } : {},
|
|
1892
|
+
revision: core.document.snapshot().revision,
|
|
1893
|
+
...value.changeSet ? { changeSet: value.changeSet } : {},
|
|
1894
|
+
requestId: value.requestId
|
|
1895
|
+
});
|
|
1896
|
+
const capabilities = {
|
|
1897
|
+
availability: { preview: Boolean(services.preview), capture: Boolean(services.capture) },
|
|
1898
|
+
document: {
|
|
1899
|
+
snapshot: async (options = {}) => {
|
|
1900
|
+
aborted(options.signal);
|
|
1901
|
+
return core.document.snapshot();
|
|
1902
|
+
},
|
|
1903
|
+
validate: async (input, options = {}) => {
|
|
1904
|
+
try {
|
|
1905
|
+
aborted(options.signal);
|
|
1906
|
+
return core.document.validate(input);
|
|
1907
|
+
} catch (error) {
|
|
1908
|
+
throw mapCoreError(error);
|
|
1909
|
+
}
|
|
1910
|
+
},
|
|
1911
|
+
load: async (input, options = {}) => {
|
|
1912
|
+
try {
|
|
1913
|
+
aborted(options.signal);
|
|
1914
|
+
const changeSet = core.document.load(input, options);
|
|
1915
|
+
aborted(options.signal);
|
|
1916
|
+
return { changed: true, revision: changeSet.revision, changeSet, requestId: options.requestId };
|
|
1917
|
+
} catch (error) {
|
|
1918
|
+
throw mapCoreError(error);
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
},
|
|
1922
|
+
nodes: {
|
|
1923
|
+
read: async (input = {}, options = {}) => {
|
|
1924
|
+
try {
|
|
1925
|
+
aborted(options.signal);
|
|
1926
|
+
const result = page(core.nodes.list(input.filter ?? {}), input.limit, input.cursor);
|
|
1927
|
+
return { nodes: result.out, page: result.page, revision: core.document.snapshot().revision, requestId: options.requestId };
|
|
1928
|
+
} catch (error) {
|
|
1929
|
+
throw mapCoreError(error);
|
|
1930
|
+
}
|
|
1931
|
+
},
|
|
1932
|
+
create: async (input, options = {}) => {
|
|
1933
|
+
try {
|
|
1934
|
+
if (!input.nodes.length) throw new CapabilityError("INVALID_INPUT", "nodes must not be empty");
|
|
1935
|
+
const tx = transact(options.label ?? "Create nodes", options, () => {
|
|
1936
|
+
const nodes = [];
|
|
1937
|
+
for (const raw of input.nodes) {
|
|
1938
|
+
aborted(options.signal);
|
|
1939
|
+
const { asset, ...nodeInput } = raw;
|
|
1940
|
+
const refs = asset ? { ...nodeInput.assetRefs ?? {}, primary: { assetId: asset.id, variant: "original" } } : nodeInput.assetRefs;
|
|
1941
|
+
if (asset) core.assets.upsert(asset);
|
|
1942
|
+
nodes.push(core.nodes.create({ rotation: 0, zIndex: 0, ...nodeInput, x: nodeInput.x ?? 0, y: nodeInput.y ?? 0, width: nodeInput.width ?? 100, height: nodeInput.height ?? 100, ...refs ? { assetRefs: refs } : {} }));
|
|
1943
|
+
}
|
|
1944
|
+
return nodes;
|
|
1945
|
+
});
|
|
1946
|
+
return writeResult({ ...tx, nodes: tx.result, requestId: options.requestId });
|
|
1947
|
+
} catch (error) {
|
|
1948
|
+
throw mapCoreError(error);
|
|
1949
|
+
}
|
|
1950
|
+
},
|
|
1951
|
+
update: async (input, options = {}) => {
|
|
1952
|
+
try {
|
|
1953
|
+
if (!input.nodes.length) throw new CapabilityError("INVALID_INPUT", "nodes must not be empty");
|
|
1954
|
+
const tx = transact(options.label ?? "Update nodes", options, () => {
|
|
1955
|
+
const nodes = [];
|
|
1956
|
+
for (const item of input.nodes) {
|
|
1957
|
+
aborted(options.signal);
|
|
1958
|
+
if (item.asset) core.assets.upsert(item.asset);
|
|
1959
|
+
nodes.push(core.nodes.update(item.id, item.patch));
|
|
1960
|
+
}
|
|
1961
|
+
return nodes;
|
|
1962
|
+
});
|
|
1963
|
+
return writeResult({ ...tx, nodes: tx.result, requestId: options.requestId });
|
|
1964
|
+
} catch (error) {
|
|
1965
|
+
throw mapCoreError(error);
|
|
1966
|
+
}
|
|
1967
|
+
},
|
|
1968
|
+
delete: async (input, options = {}) => {
|
|
1969
|
+
try {
|
|
1970
|
+
if (!input.nodeIds.length) throw new CapabilityError("INVALID_INPUT", "nodeIds must not be empty");
|
|
1971
|
+
const tx = transact(options.label ?? "Delete nodes", options, () => {
|
|
1972
|
+
const ids = [];
|
|
1973
|
+
for (const id of [...new Set(input.nodeIds)]) {
|
|
1974
|
+
aborted(options.signal);
|
|
1975
|
+
core.nodes.remove(id);
|
|
1976
|
+
ids.push(id);
|
|
1977
|
+
}
|
|
1978
|
+
return ids;
|
|
1979
|
+
});
|
|
1980
|
+
return writeResult({ ...tx, deletedNodeIds: tx.result, requestId: options.requestId });
|
|
1981
|
+
} catch (error) {
|
|
1982
|
+
throw mapCoreError(error);
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
},
|
|
1986
|
+
assets: {
|
|
1987
|
+
read: async (input = {}, options = {}) => {
|
|
1988
|
+
try {
|
|
1989
|
+
aborted(options.signal);
|
|
1990
|
+
const assets = core.assets.list().filter((asset) => (!input.ids || input.ids.includes(asset.id)) && (!input.kinds || input.kinds.includes(asset.kind)));
|
|
1991
|
+
const result = page(assets, input.limit, input.cursor);
|
|
1992
|
+
return { assets: result.out, page: result.page, revision: core.document.snapshot().revision, requestId: options.requestId };
|
|
1993
|
+
} catch (error) {
|
|
1994
|
+
throw mapCoreError(error);
|
|
1995
|
+
}
|
|
1996
|
+
},
|
|
1997
|
+
upsert: async (input, options = {}) => {
|
|
1998
|
+
try {
|
|
1999
|
+
if (!input.assets.length) throw new CapabilityError("INVALID_INPUT", "assets must not be empty");
|
|
2000
|
+
const tx = transact(options.label ?? "Upsert assets", options, () => input.assets.map((asset) => {
|
|
2001
|
+
aborted(options.signal);
|
|
2002
|
+
return core.assets.upsert(asset);
|
|
2003
|
+
}));
|
|
2004
|
+
return writeResult({ ...tx, assets: tx.result, requestId: options.requestId });
|
|
2005
|
+
} catch (error) {
|
|
2006
|
+
throw mapCoreError(error);
|
|
2007
|
+
}
|
|
2008
|
+
},
|
|
2009
|
+
remove: async (input, options = {}) => {
|
|
2010
|
+
try {
|
|
2011
|
+
if (!input.assetIds.length) throw new CapabilityError("INVALID_INPUT", "assetIds must not be empty");
|
|
2012
|
+
const tx = transact(options.label ?? "Remove assets", options, () => [...new Set(input.assetIds)].map((id) => {
|
|
2013
|
+
aborted(options.signal);
|
|
2014
|
+
core.assets.remove(id);
|
|
2015
|
+
return id;
|
|
2016
|
+
}));
|
|
2017
|
+
return writeResult({ ...tx, deletedAssetIds: tx.result, requestId: options.requestId });
|
|
2018
|
+
} catch (error) {
|
|
2019
|
+
throw mapCoreError(error);
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
},
|
|
2023
|
+
selection: {
|
|
2024
|
+
get: async (options = {}) => {
|
|
2025
|
+
aborted(options.signal);
|
|
2026
|
+
return core.selection.get();
|
|
2027
|
+
},
|
|
2028
|
+
set: async (nodeIds, options = {}) => {
|
|
2029
|
+
try {
|
|
2030
|
+
aborted(options.signal);
|
|
2031
|
+
core.selection.set(nodeIds);
|
|
2032
|
+
return { nodeIds: core.selection.get(), requestId: options.requestId };
|
|
2033
|
+
} catch (error) {
|
|
2034
|
+
throw mapCoreError(error);
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
},
|
|
2038
|
+
viewport: {
|
|
2039
|
+
get: async (options = {}) => {
|
|
2040
|
+
aborted(options.signal);
|
|
2041
|
+
return core.viewport.get();
|
|
2042
|
+
},
|
|
2043
|
+
set: async (viewport, options = {}) => {
|
|
2044
|
+
try {
|
|
2045
|
+
aborted(options.signal);
|
|
2046
|
+
core.viewport.set(viewport);
|
|
2047
|
+
return { viewport: core.viewport.get(), requestId: options.requestId };
|
|
2048
|
+
} catch (error) {
|
|
2049
|
+
throw mapCoreError(error);
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
},
|
|
2053
|
+
history: {
|
|
2054
|
+
canUndo: () => core.history.canUndo(),
|
|
2055
|
+
canRedo: () => core.history.canRedo(),
|
|
2056
|
+
clear: () => core.history.clear(),
|
|
2057
|
+
undo: (options = {}) => {
|
|
2058
|
+
try {
|
|
2059
|
+
aborted(options.signal);
|
|
2060
|
+
return core.history.undo();
|
|
2061
|
+
} catch (error) {
|
|
2062
|
+
throw mapCoreError(error);
|
|
2063
|
+
}
|
|
2064
|
+
},
|
|
2065
|
+
redo: (options = {}) => {
|
|
2066
|
+
try {
|
|
2067
|
+
aborted(options.signal);
|
|
2068
|
+
return core.history.redo();
|
|
2069
|
+
} catch (error) {
|
|
2070
|
+
throw mapCoreError(error);
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
},
|
|
2074
|
+
preview: { get: async (input, options = {}) => {
|
|
2075
|
+
try {
|
|
2076
|
+
aborted(options.signal);
|
|
2077
|
+
if (!core.nodes.get(input.nodeId)) throw new CapabilityError("NODE_NOT_FOUND", `Node not found: ${input.nodeId}`, { nodeId: input.nodeId });
|
|
2078
|
+
if (!services.preview) throw new CapabilityUnavailableError("preview");
|
|
2079
|
+
const value = await services.preview(input, options);
|
|
2080
|
+
aborted(options.signal);
|
|
2081
|
+
return { ...value, revision: core.document.snapshot().revision, requestId: options.requestId };
|
|
2082
|
+
} catch (error) {
|
|
2083
|
+
throw mapCoreError(error);
|
|
2084
|
+
}
|
|
2085
|
+
} },
|
|
2086
|
+
capture: { available: Boolean(services.capture), capture: async (input, options = {}) => {
|
|
2087
|
+
try {
|
|
2088
|
+
aborted(options.signal);
|
|
2089
|
+
if (!services.capture) throw new CapabilityUnavailableError("capture");
|
|
2090
|
+
const value = await services.capture(input, options);
|
|
2091
|
+
aborted(options.signal);
|
|
2092
|
+
return { ...value, revision: core.document.snapshot().revision, requestId: options.requestId };
|
|
2093
|
+
} catch (error) {
|
|
2094
|
+
throw mapCoreError(error);
|
|
2095
|
+
}
|
|
2096
|
+
} }
|
|
2097
|
+
};
|
|
2098
|
+
return capabilities;
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
// ../renderer-pixi/src/registry.ts
|
|
2102
|
+
var NodeRendererRegistry = class {
|
|
2103
|
+
renderers = /* @__PURE__ */ new Map();
|
|
2104
|
+
register(type, renderer, options = {}) {
|
|
2105
|
+
if (!type.trim()) throw new Error("Renderer type must be a non-empty string");
|
|
2106
|
+
if (this.renderers.has(type) && !options.replace) throw new Error(`Renderer already registered: ${type}`);
|
|
2107
|
+
this.renderers.set(type, renderer);
|
|
2108
|
+
return () => {
|
|
2109
|
+
if (this.renderers.get(type) === renderer) this.renderers.delete(type);
|
|
2110
|
+
};
|
|
2111
|
+
}
|
|
2112
|
+
get(type) {
|
|
2113
|
+
return this.renderers.get(type);
|
|
2114
|
+
}
|
|
2115
|
+
has(type) {
|
|
2116
|
+
return this.renderers.has(type);
|
|
2117
|
+
}
|
|
2118
|
+
resolve(node) {
|
|
2119
|
+
return this.get(node.type);
|
|
2120
|
+
}
|
|
2121
|
+
list() {
|
|
2122
|
+
return [...this.renderers.keys()];
|
|
2123
|
+
}
|
|
2124
|
+
};
|
|
2125
|
+
|
|
2126
|
+
// ../renderer-pixi/src/builtins.ts
|
|
2127
|
+
var base = (create, update) => ({ create, update, destroy(view) {
|
|
2128
|
+
view.displayObject.destroy?.({ children: true });
|
|
2129
|
+
} });
|
|
2130
|
+
function registerBuiltinRenderers(registry) {
|
|
2131
|
+
if (!registry.has("rect")) registry.register("rect", base((node, ctx) => ({ displayObject: ctx.display.createRect?.(node.width, node.height, node.props?.fill ?? 8162552) ?? ctx.display.createContainer(), state: {} }), (view, node) => applyTransform(view, node)));
|
|
2132
|
+
registerTextureNode(registry, "image", ["preview", "image", "primary", "source"]);
|
|
2133
|
+
registerTextureNode(registry, "video", ["preview", "poster", "video", "primary", "source"]);
|
|
2134
|
+
registerTextureNode(registry, "audio", ["waveform", "preview", "audio", "primary", "source"]);
|
|
2135
|
+
registerTextureNode(registry, "model", ["preview", "model", "primary", "source"]);
|
|
2136
|
+
registerTextureNode(registry, "html", ["preview", "html", "primary", "source"]);
|
|
2137
|
+
registerTextureNode(registry, "markdown", ["preview", "markdown", "primary", "source"]);
|
|
2138
|
+
registerTextureNode(registry, "text-file", ["preview", "text-file", "primary", "source"]);
|
|
2139
|
+
registerTextureNode(registry, "file", ["preview", "file", "primary", "source"]);
|
|
2140
|
+
if (!registry.has("text")) registry.register("text", {
|
|
2141
|
+
create(node, ctx) {
|
|
2142
|
+
const displayObject = ctx.display.createContainer();
|
|
2143
|
+
const text = ctx.display.createText?.(textValue(node), textStyle(node, node.width));
|
|
2144
|
+
if (text) displayObject.addChild?.(text);
|
|
2145
|
+
return { displayObject, state: { text } };
|
|
2146
|
+
},
|
|
2147
|
+
update(view, node) {
|
|
2148
|
+
applyPlacement(view, node);
|
|
2149
|
+
const text = view.state.text;
|
|
2150
|
+
if (!text) return;
|
|
2151
|
+
text.text = textValue(node);
|
|
2152
|
+
const style = textStyle(node, node.width);
|
|
2153
|
+
if (style) text.style = style;
|
|
2154
|
+
},
|
|
2155
|
+
destroy(view) {
|
|
2156
|
+
view.displayObject.destroy?.({ children: true });
|
|
2157
|
+
}
|
|
2158
|
+
});
|
|
2159
|
+
if (!registry.has("unknown-node")) registry.register("unknown-node", base((node, ctx) => ({ displayObject: ctx.display.createText?.(`${node.type}
|
|
2160
|
+
${node.id}`, { fill: 10265519 }) ?? ctx.display.createContainer(), state: {} }), (view, node) => applyTransform(view, node)));
|
|
2161
|
+
}
|
|
2162
|
+
function registerTextureNode(registry, type, refNames) {
|
|
2163
|
+
if (registry.has(type)) return;
|
|
2164
|
+
registry.register(type, {
|
|
2165
|
+
async create(node, ctx) {
|
|
2166
|
+
const ref = refNames.map((name) => node.assetRefs?.[name]).find(Boolean);
|
|
2167
|
+
const lease = ref ? await ctx.assets.acquireTexture(ref, { kind: type }) : void 0;
|
|
2168
|
+
if (ctx.signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
2169
|
+
const displayObject = await ctx.display.createImage?.(ref, node) ?? ctx.display.createContainer();
|
|
2170
|
+
if (lease?.texture !== void 0) displayObject.texture = lease.texture;
|
|
2171
|
+
displayObject.mediaKind = type;
|
|
2172
|
+
return { displayObject, state: { lease, ref } };
|
|
2173
|
+
},
|
|
2174
|
+
update(view, node) {
|
|
2175
|
+
applyTransform(view, node);
|
|
2176
|
+
},
|
|
2177
|
+
destroy(view) {
|
|
2178
|
+
view.displayObject.destroy?.({ children: true });
|
|
2179
|
+
}
|
|
2180
|
+
});
|
|
2181
|
+
}
|
|
2182
|
+
function textValue(node) {
|
|
2183
|
+
const props = node.props;
|
|
2184
|
+
const value = props.text ?? props.content ?? props.value ?? node.name ?? "";
|
|
2185
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
2186
|
+
}
|
|
2187
|
+
function textStyle(node, width) {
|
|
2188
|
+
const style = node.props.style;
|
|
2189
|
+
const resolved = style && typeof style === "object" && !Array.isArray(style) ? { ...style } : {};
|
|
2190
|
+
if (resolved.wordWrap !== false && Number.isFinite(width) && width > 0) {
|
|
2191
|
+
resolved.wordWrap = true;
|
|
2192
|
+
resolved.wordWrapWidth = width;
|
|
2193
|
+
if (resolved.breakWords === void 0) resolved.breakWords = true;
|
|
2194
|
+
}
|
|
2195
|
+
return Object.keys(resolved).length > 0 ? resolved : void 0;
|
|
2196
|
+
}
|
|
2197
|
+
function applyPlacement(view, node) {
|
|
2198
|
+
const d = view.displayObject;
|
|
2199
|
+
d.x = node.x;
|
|
2200
|
+
d.y = node.y;
|
|
2201
|
+
d.rotation = node.rotation;
|
|
2202
|
+
d.zIndex = node.zIndex;
|
|
2203
|
+
}
|
|
2204
|
+
function applyTransform(view, node) {
|
|
2205
|
+
applyPlacement(view, node);
|
|
2206
|
+
const d = view.displayObject;
|
|
2207
|
+
d.width = node.width;
|
|
2208
|
+
d.height = node.height;
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
// ../renderer-pixi/src/spatial.ts
|
|
2212
|
+
var GridSpatialIndex = class {
|
|
2213
|
+
cells = /* @__PURE__ */ new Map();
|
|
2214
|
+
items = /* @__PURE__ */ new Map();
|
|
2215
|
+
cellSize;
|
|
2216
|
+
constructor(cellSize = 256) {
|
|
2217
|
+
if (!Number.isFinite(cellSize) || cellSize <= 0) throw new RangeError("cellSize must be positive");
|
|
2218
|
+
this.cellSize = cellSize;
|
|
2219
|
+
}
|
|
2220
|
+
rebuild(items) {
|
|
2221
|
+
this.cells.clear();
|
|
2222
|
+
this.items.clear();
|
|
2223
|
+
for (const item of items) this.insert(item);
|
|
2224
|
+
}
|
|
2225
|
+
insert(item) {
|
|
2226
|
+
this.remove(item.id);
|
|
2227
|
+
const copy = { ...item };
|
|
2228
|
+
this.items.set(copy.id, copy);
|
|
2229
|
+
for (const key of this.keys(copy)) {
|
|
2230
|
+
let bucket = this.cells.get(key);
|
|
2231
|
+
if (!bucket) this.cells.set(key, bucket = /* @__PURE__ */ new Map());
|
|
2232
|
+
bucket.set(copy.id, copy);
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
update(item) {
|
|
2236
|
+
this.insert(item);
|
|
2237
|
+
}
|
|
2238
|
+
remove(id) {
|
|
2239
|
+
const previous = this.items.get(id);
|
|
2240
|
+
if (!previous) return;
|
|
2241
|
+
this.items.delete(id);
|
|
2242
|
+
for (const key of this.keys(previous)) {
|
|
2243
|
+
const bucket = this.cells.get(key);
|
|
2244
|
+
bucket?.delete(id);
|
|
2245
|
+
if (bucket?.size === 0) this.cells.delete(key);
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
query(bounds) {
|
|
2249
|
+
const result = /* @__PURE__ */ new Set();
|
|
2250
|
+
for (const key of this.keys(bounds)) {
|
|
2251
|
+
for (const item of this.cells.get(key)?.values() ?? []) if (intersects(item, bounds)) result.add(item.id);
|
|
2252
|
+
}
|
|
2253
|
+
return result;
|
|
2254
|
+
}
|
|
2255
|
+
queryPoint(point) {
|
|
2256
|
+
const key = this.key(Math.floor(point.x / this.cellSize), Math.floor(point.y / this.cellSize));
|
|
2257
|
+
const result = /* @__PURE__ */ new Set();
|
|
2258
|
+
for (const item of this.cells.get(key)?.values() ?? []) if (contains(item, point)) result.add(item.id);
|
|
2259
|
+
return result;
|
|
2260
|
+
}
|
|
2261
|
+
*keys(bounds) {
|
|
2262
|
+
const minX = Math.floor(bounds.minX / this.cellSize);
|
|
2263
|
+
const maxX = Math.floor(bounds.maxX / this.cellSize);
|
|
2264
|
+
const minY = Math.floor(bounds.minY / this.cellSize);
|
|
2265
|
+
const maxY = Math.floor(bounds.maxY / this.cellSize);
|
|
2266
|
+
for (let x = minX; x <= maxX; x++) for (let y = minY; y <= maxY; y++) yield this.key(x, y);
|
|
2267
|
+
}
|
|
2268
|
+
key(x, y) {
|
|
2269
|
+
return `${x}:${y}`;
|
|
2270
|
+
}
|
|
2271
|
+
};
|
|
2272
|
+
function intersects(a, b) {
|
|
2273
|
+
return a.minX <= b.maxX && a.maxX >= b.minX && a.minY <= b.maxY && a.maxY >= b.minY;
|
|
2274
|
+
}
|
|
2275
|
+
function contains(a, p) {
|
|
2276
|
+
return p.x >= a.minX && p.x <= a.maxX && p.y >= a.minY && p.y <= a.maxY;
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
// ../renderer-pixi/src/pixi-adapter.ts
|
|
2280
|
+
async function loadPixiRuntime() {
|
|
2281
|
+
return await import('pixi.js');
|
|
2282
|
+
}
|
|
2283
|
+
function createPixiApplicationFactory(options = {}, runtimeLoader = loadPixiRuntime) {
|
|
2284
|
+
return async () => {
|
|
2285
|
+
const pixi = await runtimeLoader();
|
|
2286
|
+
const app = new pixi.Application();
|
|
2287
|
+
Object.defineProperty(app, "initOptions", {
|
|
2288
|
+
// Pixi's TickerPlugin defaults to autoStart: true, which renders every
|
|
2289
|
+
// display frame forever regardless of whether the scene changed. The
|
|
2290
|
+
// renderer drives its own on-demand render loop (see
|
|
2291
|
+
// PixiBoardRenderer.requestFrame), so the ticker must start stopped;
|
|
2292
|
+
// callers can still opt back into continuous rendering explicitly.
|
|
2293
|
+
value: { autoStart: false, sharedTicker: false, ...options },
|
|
2294
|
+
configurable: true
|
|
2295
|
+
});
|
|
2296
|
+
return app;
|
|
2297
|
+
};
|
|
2298
|
+
}
|
|
2299
|
+
function createPixiViewFactory(pixi) {
|
|
2300
|
+
return {
|
|
2301
|
+
createContainer: () => new pixi.Container({ isRenderGroup: true, sortableChildren: true }),
|
|
2302
|
+
createRect: (width, height, fill) => {
|
|
2303
|
+
const graphics = new pixi.Graphics();
|
|
2304
|
+
graphics.rect?.(0, 0, width, height).fill?.(fill);
|
|
2305
|
+
return graphics;
|
|
2306
|
+
},
|
|
2307
|
+
createImage: (_ref) => new pixi.Sprite(),
|
|
2308
|
+
createText: (text, style) => new pixi.Text({ text, style })
|
|
2309
|
+
};
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2312
|
+
// ../renderer-pixi/src/renderer.ts
|
|
2313
|
+
var DEFAULT_LOD_THRESHOLDS = [0.25, 0.5, 1];
|
|
2314
|
+
function lodLevelFor(scale, thresholds) {
|
|
2315
|
+
if (scale === void 0) return void 0;
|
|
2316
|
+
let level = 0;
|
|
2317
|
+
for (const threshold of thresholds) {
|
|
2318
|
+
if (scale >= threshold) level++;
|
|
2319
|
+
}
|
|
2320
|
+
return level;
|
|
2321
|
+
}
|
|
2322
|
+
var PixiBoardRenderer = class {
|
|
2323
|
+
registry;
|
|
2324
|
+
diagnostics = { creates: 0, updates: 0, destroys: 0, lateUpdates: 0, activeViews: 0, pendingOperations: 0, textureLeases: 0, listeners: 0, tickers: 0 };
|
|
2325
|
+
activeViews = /* @__PURE__ */ new Map();
|
|
2326
|
+
spatialIndex;
|
|
2327
|
+
options;
|
|
2328
|
+
instanceAbort = new AbortController();
|
|
2329
|
+
entries = /* @__PURE__ */ new Map();
|
|
2330
|
+
operations = /* @__PURE__ */ new Map();
|
|
2331
|
+
app;
|
|
2332
|
+
world;
|
|
2333
|
+
revision;
|
|
2334
|
+
nodesById = /* @__PURE__ */ new Map();
|
|
2335
|
+
desiredIds = /* @__PURE__ */ new Set();
|
|
2336
|
+
visibleBounds;
|
|
2337
|
+
viewportEpoch = 0;
|
|
2338
|
+
destroyed = false;
|
|
2339
|
+
desynchronized = false;
|
|
2340
|
+
viewFactory;
|
|
2341
|
+
frameScheduled = false;
|
|
2342
|
+
lodThresholds;
|
|
2343
|
+
currentScale;
|
|
2344
|
+
currentLodLevel;
|
|
2345
|
+
constructor(options) {
|
|
2346
|
+
this.options = options;
|
|
2347
|
+
this.registry = options.registry ?? new NodeRendererRegistry();
|
|
2348
|
+
this.spatialIndex = options.spatialIndex ?? new GridSpatialIndex();
|
|
2349
|
+
this.lodThresholds = options.lod?.thresholds ?? DEFAULT_LOD_THRESHOLDS;
|
|
2350
|
+
registerBuiltinRenderers(this.registry);
|
|
2351
|
+
}
|
|
2352
|
+
async init() {
|
|
2353
|
+
if (this.destroyed) throw new Error("Renderer has been destroyed");
|
|
2354
|
+
const applicationFactory = this.options.applicationFactory ?? createPixiApplicationFactory();
|
|
2355
|
+
this.app = await applicationFactory();
|
|
2356
|
+
await this.app.init?.({ preference: "webgl", antialias: false, powerPreference: "high-performance", autoStart: false, sharedTicker: false, ...this.app.initOptions });
|
|
2357
|
+
const pixi = this.options.viewFactory ? void 0 : await loadPixiRuntime();
|
|
2358
|
+
this.viewFactory = this.options.viewFactory ?? createPixiViewFactory(pixi);
|
|
2359
|
+
this.world = this.viewFactory.createContainer();
|
|
2360
|
+
this.app.stage.addChild?.(this.world);
|
|
2361
|
+
}
|
|
2362
|
+
async rebuild(snapshot) {
|
|
2363
|
+
this.assertAlive();
|
|
2364
|
+
try {
|
|
2365
|
+
this.revision = snapshot.revision;
|
|
2366
|
+
this.nodesById.clear();
|
|
2367
|
+
for (const node of snapshot.nodes) this.nodesById.set(node.id, node);
|
|
2368
|
+
this.rebuildSpatialIndex(snapshot);
|
|
2369
|
+
this.refreshDesiredIds();
|
|
2370
|
+
for (const id of /* @__PURE__ */ new Set([...this.entries.keys(), ...this.operations.keys()])) await this.destroyView(id);
|
|
2371
|
+
await this.reconcileActiveSet(++this.viewportEpoch);
|
|
2372
|
+
this.desynchronized = false;
|
|
2373
|
+
this.requestFrame();
|
|
2374
|
+
} catch (error) {
|
|
2375
|
+
this.desynchronized = true;
|
|
2376
|
+
throw error;
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2379
|
+
async apply(update, changeSet) {
|
|
2380
|
+
this.assertAlive();
|
|
2381
|
+
if (this.desynchronized || this.revision === void 0 || changeSet.revision !== this.revision + 1 || changeSet.revision !== update.revision) {
|
|
2382
|
+
return "rebuild-required";
|
|
2383
|
+
}
|
|
2384
|
+
try {
|
|
2385
|
+
const touchedIds = /* @__PURE__ */ new Set([...changeSet.addedNodeIds, ...changeSet.updatedNodeIds, ...changeSet.assetChangedNodeIds]);
|
|
2386
|
+
const nextNodes = /* @__PURE__ */ new Map();
|
|
2387
|
+
for (const node of update.changedNodes) if (touchedIds.has(node.id)) nextNodes.set(node.id, node);
|
|
2388
|
+
for (const id of touchedIds) {
|
|
2389
|
+
if (!nextNodes.has(id)) throw new Error(`Renderer update is missing changed node: ${id}`);
|
|
2390
|
+
}
|
|
2391
|
+
const addedIds = new Set(changeSet.addedNodeIds);
|
|
2392
|
+
const updatedIds = new Set(changeSet.updatedNodeIds);
|
|
2393
|
+
const assetChangedIds = new Set(changeSet.assetChangedNodeIds);
|
|
2394
|
+
for (const id of changeSet.removedNodeIds) {
|
|
2395
|
+
this.nodesById.delete(id);
|
|
2396
|
+
this.desiredIds.delete(id);
|
|
2397
|
+
this.spatialIndex.remove(id);
|
|
2398
|
+
await this.destroyView(id);
|
|
2399
|
+
}
|
|
2400
|
+
for (const id of touchedIds) {
|
|
2401
|
+
const node = nextNodes.get(id);
|
|
2402
|
+
if (!node) continue;
|
|
2403
|
+
this.nodesById.set(id, node);
|
|
2404
|
+
const item = { ...this.getBounds(node), id };
|
|
2405
|
+
if (addedIds.has(id)) this.spatialIndex.insert(item);
|
|
2406
|
+
else if (updatedIds.has(id)) this.spatialIndex.update(item);
|
|
2407
|
+
}
|
|
2408
|
+
const customVisibleCandidates = this.visibleBounds && this.options.cullingQuery && touchedIds.size > 0 ? this.visibleCandidatesFor(touchedIds) : void 0;
|
|
2409
|
+
for (const id of touchedIds) {
|
|
2410
|
+
const node = this.nodesById.get(id);
|
|
2411
|
+
const desired = Boolean(node && this.isNodeDesired(node, customVisibleCandidates));
|
|
2412
|
+
if (desired) this.desiredIds.add(id);
|
|
2413
|
+
else this.desiredIds.delete(id);
|
|
2414
|
+
if (!node || !desired) {
|
|
2415
|
+
await this.destroyView(id);
|
|
2416
|
+
continue;
|
|
2417
|
+
}
|
|
2418
|
+
await this.ensureView(node, assetChangedIds.has(id));
|
|
2419
|
+
}
|
|
2420
|
+
this.revision = update.revision;
|
|
2421
|
+
this.requestFrame();
|
|
2422
|
+
return "applied";
|
|
2423
|
+
} catch (error) {
|
|
2424
|
+
this.desynchronized = true;
|
|
2425
|
+
throw error;
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
async setVisibleBounds(bounds, scale) {
|
|
2429
|
+
this.assertAlive();
|
|
2430
|
+
if (scale !== void 0 && (!Number.isFinite(scale) || scale <= 0)) {
|
|
2431
|
+
throw new RangeError("Viewport scale must be finite and greater than zero");
|
|
2432
|
+
}
|
|
2433
|
+
this.visibleBounds = bounds;
|
|
2434
|
+
this.currentScale = scale;
|
|
2435
|
+
const nextLevel = lodLevelFor(scale, this.lodThresholds);
|
|
2436
|
+
const levelChanged = nextLevel !== this.currentLodLevel;
|
|
2437
|
+
this.currentLodLevel = nextLevel;
|
|
2438
|
+
const retained = levelChanged ? new Set(this.entries.keys()) : void 0;
|
|
2439
|
+
this.refreshDesiredIds();
|
|
2440
|
+
const epoch = ++this.viewportEpoch;
|
|
2441
|
+
await this.reconcileActiveSet(epoch);
|
|
2442
|
+
if (retained) await this.refreshLodForRetainedViews(retained, epoch);
|
|
2443
|
+
this.requestFrame();
|
|
2444
|
+
}
|
|
2445
|
+
setCullingQuery(query) {
|
|
2446
|
+
this.options.cullingQuery = query;
|
|
2447
|
+
}
|
|
2448
|
+
/**
|
|
2449
|
+
* The ids culling currently keeps, or undefined when nothing has narrowed
|
|
2450
|
+
* the renderer yet. The distinction matters to callers that mirror this set
|
|
2451
|
+
* in the DOM: undefined has to read as "no culling information", not as
|
|
2452
|
+
* "nothing is visible", or an overlay would blank out on a renderer that
|
|
2453
|
+
* retains everything.
|
|
2454
|
+
*
|
|
2455
|
+
* Returns a copy — the internal set is mutated during reconcile, and handing
|
|
2456
|
+
* it out directly would let a caller observe a half-applied frame.
|
|
2457
|
+
*/
|
|
2458
|
+
visibleNodeIds() {
|
|
2459
|
+
return this.visibleBounds ? new Set(this.desiredIds) : void 0;
|
|
2460
|
+
}
|
|
2461
|
+
async refreshRegisteredTypes() {
|
|
2462
|
+
this.assertAlive();
|
|
2463
|
+
this.rebuildSpatialIndexFromCache();
|
|
2464
|
+
this.refreshDesiredIds();
|
|
2465
|
+
for (const id of /* @__PURE__ */ new Set([...this.entries.keys(), ...this.operations.keys()])) await this.destroyView(id);
|
|
2466
|
+
await this.reconcileActiveSet(++this.viewportEpoch);
|
|
2467
|
+
this.requestFrame();
|
|
2468
|
+
}
|
|
2469
|
+
getBounds(node) {
|
|
2470
|
+
const definition = this.options.nodeTypes?.get(node.type);
|
|
2471
|
+
return definition ? definition.getBounds(node) : rotatedRectBounds(node);
|
|
2472
|
+
}
|
|
2473
|
+
hitTest(worldPoint) {
|
|
2474
|
+
const ids = this.spatialIndex.queryPoint?.(worldPoint) ?? this.entries.keys();
|
|
2475
|
+
const order = [...ids].map((id, index) => ({ node: this.entries.get(id)?.node, index })).filter(({ node }) => node).sort((a, b) => b.node.zIndex - a.node.zIndex || b.index - a.index);
|
|
2476
|
+
for (const { node } of order) {
|
|
2477
|
+
const entry = this.entries.get(node.id);
|
|
2478
|
+
if (entry.renderer.hitTest?.(node, worldPoint) ?? contains2(this.getBounds(node), worldPoint)) return node.id;
|
|
2479
|
+
}
|
|
2480
|
+
return void 0;
|
|
2481
|
+
}
|
|
2482
|
+
async capture(request, options = {}) {
|
|
2483
|
+
this.assertAlive();
|
|
2484
|
+
const signal = combineSignals(this.instanceAbort.signal, options.signal);
|
|
2485
|
+
if (signal.aborted) throw abortError();
|
|
2486
|
+
const scale = request.scale ?? 1;
|
|
2487
|
+
if (!Number.isFinite(scale) || scale <= 0) throw new RangeError("Capture scale must be positive");
|
|
2488
|
+
let target = this.world;
|
|
2489
|
+
let frame;
|
|
2490
|
+
if (request.target === "node") {
|
|
2491
|
+
const node = this.nodesById.get(request.nodeId);
|
|
2492
|
+
if (!node) throw new Error(`Node ${request.nodeId} does not exist`);
|
|
2493
|
+
const temporary = !this.entries.has(node.id);
|
|
2494
|
+
if (temporary) await this.ensureView(node);
|
|
2495
|
+
const entry = this.entries.get(node.id);
|
|
2496
|
+
if (!entry) throw new Error(`Node ${request.nodeId} is not available for capture`);
|
|
2497
|
+
target = entry.view.displayObject;
|
|
2498
|
+
frame = { minX: 0, minY: 0, maxX: node.width, maxY: node.height };
|
|
2499
|
+
try {
|
|
2500
|
+
return await this.captureFrame(target, frame, scale, request, signal, options.requestId);
|
|
2501
|
+
} finally {
|
|
2502
|
+
if (temporary) await this.destroyView(node.id);
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
if (request.target === "bounds") frame = request.bounds;
|
|
2506
|
+
if (request.target === "viewport" && this.app?.screen) frame = { minX: 0, minY: 0, maxX: this.app.screen.width, maxY: this.app.screen.height };
|
|
2507
|
+
return this.captureFrame(target, frame, scale, request, signal, options.requestId);
|
|
2508
|
+
}
|
|
2509
|
+
async destroy() {
|
|
2510
|
+
if (this.destroyed) return;
|
|
2511
|
+
this.destroyed = true;
|
|
2512
|
+
this.instanceAbort.abort();
|
|
2513
|
+
for (const operation of this.operations.values()) operation.controller.abort();
|
|
2514
|
+
await Promise.allSettled([...this.operations.values()].map((operation) => operation.promise).filter(Boolean));
|
|
2515
|
+
for (const id of [...this.entries.keys()]) await this.destroyView(id);
|
|
2516
|
+
this.world?.destroy?.({ children: true });
|
|
2517
|
+
this.app?.destroy?.(true);
|
|
2518
|
+
this.entries.clear();
|
|
2519
|
+
this.operations.clear();
|
|
2520
|
+
this.activeViews.clear();
|
|
2521
|
+
this.desiredIds.clear();
|
|
2522
|
+
this.updateDiagnosticCounts();
|
|
2523
|
+
}
|
|
2524
|
+
async ensureView(node, forceRecreate = false) {
|
|
2525
|
+
const renderer = this.registry.get(node.type) ?? this.registry.get("unknown-node");
|
|
2526
|
+
const current = this.entries.get(node.id);
|
|
2527
|
+
if (current && !forceRecreate && current.renderer === renderer && sameAssetRefs(current.node.assetRefs, node.assetRefs)) {
|
|
2528
|
+
const operation2 = this.beginOperation(node.id);
|
|
2529
|
+
const task2 = (async () => {
|
|
2530
|
+
current.node = node;
|
|
2531
|
+
this.diagnostics.updates++;
|
|
2532
|
+
await renderer.update(current.view, node, this.context(combineSignals(current.lifetime.signal, operation2.controller.signal), current.scope));
|
|
2533
|
+
if (!this.isCurrentOperation(node.id, operation2) || this.entries.get(node.id) !== current) this.diagnostics.lateUpdates++;
|
|
2534
|
+
})();
|
|
2535
|
+
await this.trackOperation(node.id, operation2, task2);
|
|
2536
|
+
return;
|
|
2537
|
+
}
|
|
2538
|
+
if (current || forceRecreate) await this.destroyView(node.id);
|
|
2539
|
+
const operation = this.beginOperation(node.id);
|
|
2540
|
+
const lifetime = new AbortController();
|
|
2541
|
+
const scope = new ResourceScope(this);
|
|
2542
|
+
const signal = combineSignals(this.instanceAbort.signal, lifetime.signal, operation.controller.signal);
|
|
2543
|
+
const task = (async () => {
|
|
2544
|
+
let view;
|
|
2545
|
+
try {
|
|
2546
|
+
view = await renderer.create(node, this.context(signal, scope));
|
|
2547
|
+
if (signal.aborted || !this.isCurrentOperation(node.id, operation) || this.nodesById.get(node.id) !== node || !this.desiredIds.has(node.id)) {
|
|
2548
|
+
this.diagnostics.lateUpdates++;
|
|
2549
|
+
renderer.destroy(view, this.context(signal, scope));
|
|
2550
|
+
scope.dispose();
|
|
2551
|
+
return;
|
|
2552
|
+
}
|
|
2553
|
+
await renderer.update(view, node, this.context(signal, scope));
|
|
2554
|
+
if (signal.aborted || !this.isCurrentOperation(node.id, operation) || this.nodesById.get(node.id) !== node || !this.desiredIds.has(node.id)) {
|
|
2555
|
+
this.diagnostics.lateUpdates++;
|
|
2556
|
+
renderer.destroy(view, this.context(signal, scope));
|
|
2557
|
+
scope.dispose();
|
|
2558
|
+
return;
|
|
2559
|
+
}
|
|
2560
|
+
const entry = { node, view, renderer, lifetime, scope };
|
|
2561
|
+
this.entries.set(node.id, entry);
|
|
2562
|
+
this.activeViews.set(node.id, view);
|
|
2563
|
+
this.world?.addChild?.(view.displayObject);
|
|
2564
|
+
this.diagnostics.creates++;
|
|
2565
|
+
this.updateDiagnosticCounts();
|
|
2566
|
+
} catch (error) {
|
|
2567
|
+
scope.dispose();
|
|
2568
|
+
if (signal.aborted || isAbortError(error)) {
|
|
2569
|
+
this.diagnostics.lateUpdates++;
|
|
2570
|
+
return;
|
|
2571
|
+
}
|
|
2572
|
+
throw error;
|
|
2573
|
+
}
|
|
2574
|
+
})();
|
|
2575
|
+
await this.trackOperation(node.id, operation, task);
|
|
2576
|
+
}
|
|
2577
|
+
beginOperation(id) {
|
|
2578
|
+
const previous = this.operations.get(id);
|
|
2579
|
+
previous?.controller.abort();
|
|
2580
|
+
const operation = { version: (previous?.version ?? 0) + 1, controller: new AbortController() };
|
|
2581
|
+
this.operations.set(id, operation);
|
|
2582
|
+
this.updateDiagnosticCounts();
|
|
2583
|
+
return operation;
|
|
2584
|
+
}
|
|
2585
|
+
async trackOperation(id, operation, task) {
|
|
2586
|
+
operation.promise = task;
|
|
2587
|
+
try {
|
|
2588
|
+
await task;
|
|
2589
|
+
} finally {
|
|
2590
|
+
if (this.operations.get(id) === operation) this.operations.delete(id);
|
|
2591
|
+
operation.controller.abort();
|
|
2592
|
+
this.updateDiagnosticCounts();
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
isCurrentOperation(id, operation) {
|
|
2596
|
+
return this.operations.get(id) === operation;
|
|
2597
|
+
}
|
|
2598
|
+
async destroyView(id) {
|
|
2599
|
+
const operation = this.operations.get(id);
|
|
2600
|
+
operation?.controller.abort();
|
|
2601
|
+
const entry = this.entries.get(id);
|
|
2602
|
+
if (!entry) return;
|
|
2603
|
+
entry.lifetime.abort();
|
|
2604
|
+
this.entries.delete(id);
|
|
2605
|
+
this.activeViews.delete(id);
|
|
2606
|
+
this.world?.removeChild?.(entry.view.displayObject);
|
|
2607
|
+
entry.renderer.destroy(entry.view, this.context(entry.lifetime.signal, entry.scope));
|
|
2608
|
+
entry.scope.dispose();
|
|
2609
|
+
this.diagnostics.destroys++;
|
|
2610
|
+
this.updateDiagnosticCounts();
|
|
2611
|
+
}
|
|
2612
|
+
async reconcileActiveSet(epoch) {
|
|
2613
|
+
for (const id of /* @__PURE__ */ new Set([...this.entries.keys(), ...this.operations.keys()])) {
|
|
2614
|
+
if (!this.desiredIds.has(id)) await this.destroyView(id);
|
|
2615
|
+
}
|
|
2616
|
+
for (const id of this.desiredIds) {
|
|
2617
|
+
if (epoch !== this.viewportEpoch || this.destroyed) return;
|
|
2618
|
+
const node = this.nodesById.get(id);
|
|
2619
|
+
if (node && !this.entries.has(id)) await this.ensureView(node);
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
/**
|
|
2623
|
+
* Re-runs update() on views that outlived a level-of-detail change, so a
|
|
2624
|
+
* node renderer can redraw itself for the new tier. Goes through ensureView,
|
|
2625
|
+
* which already carries the operation-versioning and late-update guards that
|
|
2626
|
+
* keep a redraw from racing an in-flight create.
|
|
2627
|
+
*/
|
|
2628
|
+
async refreshLodForRetainedViews(retained, epoch) {
|
|
2629
|
+
for (const id of retained) {
|
|
2630
|
+
if (epoch !== this.viewportEpoch || this.destroyed) return;
|
|
2631
|
+
const node = this.nodesById.get(id);
|
|
2632
|
+
if (node && this.entries.has(id)) await this.ensureView(node);
|
|
2633
|
+
}
|
|
2634
|
+
}
|
|
2635
|
+
refreshDesiredIds() {
|
|
2636
|
+
const candidates = this.visibleBounds ? this.options.cullingQuery?.(this.visibleBounds) ?? this.spatialIndex.query(this.visibleBounds) : this.nodesById.keys();
|
|
2637
|
+
this.desiredIds = new Set([...candidates].filter((id) => this.nodesById.get(id)?.visible !== false));
|
|
2638
|
+
}
|
|
2639
|
+
isNodeDesired(node, customVisibleCandidates) {
|
|
2640
|
+
if (node.visible === false) return false;
|
|
2641
|
+
if (!this.visibleBounds) return true;
|
|
2642
|
+
if (customVisibleCandidates) return customVisibleCandidates.has(node.id);
|
|
2643
|
+
return intersects2(this.getBounds(node), this.visibleBounds);
|
|
2644
|
+
}
|
|
2645
|
+
visibleCandidatesFor(touchedIds) {
|
|
2646
|
+
const candidates = this.options.cullingQuery(this.visibleBounds);
|
|
2647
|
+
const has = candidates.has;
|
|
2648
|
+
if (typeof has === "function") {
|
|
2649
|
+
const visibleIds2 = /* @__PURE__ */ new Set();
|
|
2650
|
+
for (const id of touchedIds) if (has.call(candidates, id)) visibleIds2.add(id);
|
|
2651
|
+
return visibleIds2;
|
|
2652
|
+
}
|
|
2653
|
+
const pending = new Set(touchedIds);
|
|
2654
|
+
const visibleIds = /* @__PURE__ */ new Set();
|
|
2655
|
+
for (const id of candidates) {
|
|
2656
|
+
if (!pending.delete(id)) continue;
|
|
2657
|
+
visibleIds.add(id);
|
|
2658
|
+
if (pending.size === 0) break;
|
|
2659
|
+
}
|
|
2660
|
+
return visibleIds;
|
|
2661
|
+
}
|
|
2662
|
+
rebuildSpatialIndex(snapshot) {
|
|
2663
|
+
this.spatialIndex.rebuild(snapshot.nodes.map((node) => ({ ...this.getBounds(node), id: node.id })));
|
|
2664
|
+
}
|
|
2665
|
+
rebuildSpatialIndexFromCache() {
|
|
2666
|
+
const items = [...this.nodesById.values()].map((node) => ({ ...this.getBounds(node), id: node.id }));
|
|
2667
|
+
this.spatialIndex.rebuild(items);
|
|
2668
|
+
}
|
|
2669
|
+
async captureFrame(target, frame, scale, request, signal, requestId) {
|
|
2670
|
+
if (signal.aborted) throw abortError();
|
|
2671
|
+
const value = this.options.capture ? await this.options.capture({ target, frame, scale, format: "png" }, request, signal) : await this.captureWithPixi(target, frame, scale);
|
|
2672
|
+
if (signal.aborted) throw abortError();
|
|
2673
|
+
return { ...value, revision: this.revision ?? 0, ...requestId ? { requestId } : {} };
|
|
2674
|
+
}
|
|
2675
|
+
async captureWithPixi(target, frame, scale) {
|
|
2676
|
+
const extract = this.app?.renderer?.extract?.base64;
|
|
2677
|
+
if (!extract) throw new Error("Capture is unavailable without a Pixi extract adapter");
|
|
2678
|
+
const rectangle = frame ? pixiRectangle(frame) : void 0;
|
|
2679
|
+
const dataUrl = await extract.call(this.app.renderer.extract, { target, frame: rectangle, format: "png", resolution: scale });
|
|
2680
|
+
return {
|
|
2681
|
+
dataUrl,
|
|
2682
|
+
mimeType: "image/png",
|
|
2683
|
+
...rectangle ? { width: Math.round(rectangle.width * scale), height: Math.round(rectangle.height * scale) } : {}
|
|
2684
|
+
};
|
|
2685
|
+
}
|
|
2686
|
+
context(signal, scope) {
|
|
2687
|
+
if (!this.viewFactory) throw new Error("Renderer is not initialized");
|
|
2688
|
+
return {
|
|
2689
|
+
signal,
|
|
2690
|
+
assets: { acquireTexture: (ref, options) => this.acquireTexture(scope, signal, ref, options) },
|
|
2691
|
+
resources: scope.api,
|
|
2692
|
+
invalidate: () => {
|
|
2693
|
+
this.options.onInvalidate?.();
|
|
2694
|
+
this.requestFrame();
|
|
2695
|
+
},
|
|
2696
|
+
lod: { level: this.currentLodLevel, scale: this.currentScale },
|
|
2697
|
+
diagnostics: this.diagnostics,
|
|
2698
|
+
display: this.viewFactory
|
|
2699
|
+
};
|
|
2700
|
+
}
|
|
2701
|
+
/**
|
|
2702
|
+
* Renders exactly once per idle scene change instead of relying on Pixi's
|
|
2703
|
+
* default continuous ticker (which redraws every display frame forever,
|
|
2704
|
+
* regardless of whether anything changed). Calls within the same
|
|
2705
|
+
* microtask are coalesced into a single render. Skipped while a custom
|
|
2706
|
+
* node renderer's own ticker subscription is active — see
|
|
2707
|
+
* syncTickerState — because in that case Pixi's own ticker is already
|
|
2708
|
+
* driving continuous frames for that animation.
|
|
2709
|
+
*/
|
|
2710
|
+
requestFrame() {
|
|
2711
|
+
if (this.destroyed || !this.app || this.diagnostics.tickers > 0 || this.frameScheduled) return;
|
|
2712
|
+
this.frameScheduled = true;
|
|
2713
|
+
void Promise.resolve().then(() => {
|
|
2714
|
+
this.frameScheduled = false;
|
|
2715
|
+
if (this.destroyed || !this.app || this.diagnostics.tickers > 0) return;
|
|
2716
|
+
this.app.render?.();
|
|
2717
|
+
});
|
|
2718
|
+
}
|
|
2719
|
+
/**
|
|
2720
|
+
* A custom node renderer that subscribes via context.resources.addTicker
|
|
2721
|
+
* wants continuous per-frame callbacks (e.g. an animated node) — that is
|
|
2722
|
+
* the "registered animation active" case the on-demand-rendering
|
|
2723
|
+
* invariant carves out, so Pixi's own ticker must run continuously for as
|
|
2724
|
+
* long as at least one such subscription is active, and fall back to
|
|
2725
|
+
* on-demand rendering once the last one is removed.
|
|
2726
|
+
*/
|
|
2727
|
+
syncTickerState() {
|
|
2728
|
+
if (this.destroyed || !this.app) return;
|
|
2729
|
+
if (this.diagnostics.tickers > 0) this.app.ticker?.start?.();
|
|
2730
|
+
else this.app.ticker?.stop?.();
|
|
2731
|
+
}
|
|
2732
|
+
async acquireTexture(scope, signal, ref, options) {
|
|
2733
|
+
const lease = await (this.options.acquireTexture ?? (async () => ({})))(ref, {
|
|
2734
|
+
lodLevel: this.currentLodLevel,
|
|
2735
|
+
lodScale: this.currentScale,
|
|
2736
|
+
...options,
|
|
2737
|
+
signal
|
|
2738
|
+
});
|
|
2739
|
+
if (signal.aborted) {
|
|
2740
|
+
lease.release?.();
|
|
2741
|
+
throw abortError();
|
|
2742
|
+
}
|
|
2743
|
+
let active = true;
|
|
2744
|
+
this.diagnostics.textureLeases++;
|
|
2745
|
+
const release = () => {
|
|
2746
|
+
if (!active) return;
|
|
2747
|
+
active = false;
|
|
2748
|
+
lease.release?.();
|
|
2749
|
+
this.diagnostics.textureLeases--;
|
|
2750
|
+
};
|
|
2751
|
+
scope.add(release);
|
|
2752
|
+
return { ...lease, release };
|
|
2753
|
+
}
|
|
2754
|
+
updateDiagnosticCounts() {
|
|
2755
|
+
this.diagnostics.activeViews = this.activeViews.size;
|
|
2756
|
+
this.diagnostics.pendingOperations = this.operations.size;
|
|
2757
|
+
}
|
|
2758
|
+
assertAlive() {
|
|
2759
|
+
if (this.destroyed) throw new Error("Renderer has been destroyed");
|
|
2760
|
+
if (!this.app) throw new Error("Renderer is not initialized");
|
|
2761
|
+
}
|
|
2762
|
+
};
|
|
2763
|
+
var ResourceScope = class {
|
|
2764
|
+
constructor(owner) {
|
|
2765
|
+
this.owner = owner;
|
|
2766
|
+
this.api = {
|
|
2767
|
+
onCleanup: (cleanup) => this.add(cleanup),
|
|
2768
|
+
listen: (target, type, listener, options) => {
|
|
2769
|
+
target.addEventListener(type, listener, options);
|
|
2770
|
+
this.owner.diagnostics.listeners++;
|
|
2771
|
+
return this.add(() => {
|
|
2772
|
+
target.removeEventListener(type, listener, options);
|
|
2773
|
+
this.owner.diagnostics.listeners--;
|
|
2774
|
+
});
|
|
2775
|
+
},
|
|
2776
|
+
addTicker: (listener) => {
|
|
2777
|
+
const ticker = this.owner.app?.ticker;
|
|
2778
|
+
ticker?.add?.(listener);
|
|
2779
|
+
this.owner.diagnostics.tickers++;
|
|
2780
|
+
this.owner.syncTickerState();
|
|
2781
|
+
return this.add(() => {
|
|
2782
|
+
ticker?.remove?.(listener);
|
|
2783
|
+
this.owner.diagnostics.tickers--;
|
|
2784
|
+
this.owner.syncTickerState();
|
|
2785
|
+
});
|
|
2786
|
+
}
|
|
2787
|
+
};
|
|
2788
|
+
}
|
|
2789
|
+
owner;
|
|
2790
|
+
cleanups = /* @__PURE__ */ new Set();
|
|
2791
|
+
disposed = false;
|
|
2792
|
+
api;
|
|
2793
|
+
add(cleanup) {
|
|
2794
|
+
if (this.disposed) {
|
|
2795
|
+
cleanup();
|
|
2796
|
+
return () => {
|
|
2797
|
+
};
|
|
2798
|
+
}
|
|
2799
|
+
let active = true;
|
|
2800
|
+
const wrapped = () => {
|
|
2801
|
+
if (!active) return;
|
|
2802
|
+
active = false;
|
|
2803
|
+
this.cleanups.delete(wrapped);
|
|
2804
|
+
cleanup();
|
|
2805
|
+
};
|
|
2806
|
+
this.cleanups.add(wrapped);
|
|
2807
|
+
return wrapped;
|
|
2808
|
+
}
|
|
2809
|
+
dispose() {
|
|
2810
|
+
if (this.disposed) return;
|
|
2811
|
+
this.disposed = true;
|
|
2812
|
+
for (const cleanup of [...this.cleanups].reverse()) cleanup();
|
|
2813
|
+
this.cleanups.clear();
|
|
2814
|
+
}
|
|
2815
|
+
};
|
|
2816
|
+
function contains2(bounds, point) {
|
|
2817
|
+
return point.x >= bounds.minX && point.x <= bounds.maxX && point.y >= bounds.minY && point.y <= bounds.maxY;
|
|
2818
|
+
}
|
|
2819
|
+
function intersects2(left, right) {
|
|
2820
|
+
return left.minX <= right.maxX && left.maxX >= right.minX && left.minY <= right.maxY && left.maxY >= right.minY;
|
|
2821
|
+
}
|
|
2822
|
+
function sameAssetRefs(a, b) {
|
|
2823
|
+
return JSON.stringify(a ?? {}) === JSON.stringify(b ?? {});
|
|
2824
|
+
}
|
|
2825
|
+
function abortError() {
|
|
2826
|
+
return new DOMException("Aborted", "AbortError");
|
|
2827
|
+
}
|
|
2828
|
+
function isAbortError(error) {
|
|
2829
|
+
return error instanceof DOMException && error.name === "AbortError";
|
|
2830
|
+
}
|
|
2831
|
+
function pixiRectangle(frame) {
|
|
2832
|
+
const rectangle = {
|
|
2833
|
+
x: frame.minX,
|
|
2834
|
+
y: frame.minY,
|
|
2835
|
+
width: frame.maxX - frame.minX,
|
|
2836
|
+
height: frame.maxY - frame.minY,
|
|
2837
|
+
copyTo(target) {
|
|
2838
|
+
target.x = rectangle.x;
|
|
2839
|
+
target.y = rectangle.y;
|
|
2840
|
+
target.width = rectangle.width;
|
|
2841
|
+
target.height = rectangle.height;
|
|
2842
|
+
return target;
|
|
2843
|
+
}
|
|
2844
|
+
};
|
|
2845
|
+
return rectangle;
|
|
2846
|
+
}
|
|
2847
|
+
function combineSignals(...values) {
|
|
2848
|
+
const signals = values.filter(Boolean);
|
|
2849
|
+
const any = AbortSignal.any;
|
|
2850
|
+
if (any) return any(signals);
|
|
2851
|
+
const controller = new AbortController();
|
|
2852
|
+
const abort = () => controller.abort();
|
|
2853
|
+
for (const signal of signals) {
|
|
2854
|
+
if (signal.aborted) controller.abort();
|
|
2855
|
+
else signal.addEventListener("abort", abort, { once: true });
|
|
2856
|
+
}
|
|
2857
|
+
return controller.signal;
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
// src/transform.ts
|
|
2861
|
+
var DEFAULT_MIN_SIZE2 = 8;
|
|
2862
|
+
var TransformController = class {
|
|
2863
|
+
constructor(host, options = {}) {
|
|
2864
|
+
this.host = host;
|
|
2865
|
+
this.options = options;
|
|
2866
|
+
}
|
|
2867
|
+
host;
|
|
2868
|
+
options;
|
|
2869
|
+
session;
|
|
2870
|
+
bounds() {
|
|
2871
|
+
const nodes = this.selectedNodes();
|
|
2872
|
+
if (nodes.length === 0) return void 0;
|
|
2873
|
+
if (nodes.length === 1) {
|
|
2874
|
+
const [node] = nodes;
|
|
2875
|
+
return {
|
|
2876
|
+
x: node.x,
|
|
2877
|
+
y: node.y,
|
|
2878
|
+
width: node.width,
|
|
2879
|
+
height: node.height,
|
|
2880
|
+
rotation: node.rotation,
|
|
2881
|
+
nodeIds: [node.id]
|
|
2882
|
+
};
|
|
2883
|
+
}
|
|
2884
|
+
let minX = Infinity;
|
|
2885
|
+
let minY = Infinity;
|
|
2886
|
+
let maxX = -Infinity;
|
|
2887
|
+
let maxY = -Infinity;
|
|
2888
|
+
for (const node of nodes) {
|
|
2889
|
+
for (const corner of rotatedCorners(node)) {
|
|
2890
|
+
minX = Math.min(minX, corner.x);
|
|
2891
|
+
minY = Math.min(minY, corner.y);
|
|
2892
|
+
maxX = Math.max(maxX, corner.x);
|
|
2893
|
+
maxY = Math.max(maxY, corner.y);
|
|
2894
|
+
}
|
|
2895
|
+
}
|
|
2896
|
+
return {
|
|
2897
|
+
x: minX,
|
|
2898
|
+
y: minY,
|
|
2899
|
+
width: maxX - minX,
|
|
2900
|
+
height: maxY - minY,
|
|
2901
|
+
rotation: 0,
|
|
2902
|
+
nodeIds: nodes.map((node) => node.id)
|
|
2903
|
+
};
|
|
2904
|
+
}
|
|
2905
|
+
handles() {
|
|
2906
|
+
const bounds = this.bounds();
|
|
2907
|
+
if (!bounds) return [];
|
|
2908
|
+
const cos = Math.cos(bounds.rotation);
|
|
2909
|
+
const sin = Math.sin(bounds.rotation);
|
|
2910
|
+
return RESIZE_HANDLES.map((handle) => {
|
|
2911
|
+
const local = handleLocalPoint(handle, bounds.width, bounds.height);
|
|
2912
|
+
return {
|
|
2913
|
+
handle,
|
|
2914
|
+
world: {
|
|
2915
|
+
x: bounds.x + local.x * cos - local.y * sin,
|
|
2916
|
+
y: bounds.y + local.x * sin + local.y * cos
|
|
2917
|
+
},
|
|
2918
|
+
cursor: resizeHandleCursor(handle, bounds.rotation)
|
|
2919
|
+
};
|
|
2920
|
+
});
|
|
2921
|
+
}
|
|
2922
|
+
begin(handle) {
|
|
2923
|
+
this.session?.commit();
|
|
2924
|
+
const nodes = this.selectedNodes();
|
|
2925
|
+
const bounds = this.bounds();
|
|
2926
|
+
if (nodes.length === 0 || !bounds) return void 0;
|
|
2927
|
+
const session = new ActiveSession(
|
|
2928
|
+
this.host,
|
|
2929
|
+
handle,
|
|
2930
|
+
nodes,
|
|
2931
|
+
bounds,
|
|
2932
|
+
`resize:${this.host.nextId()}`,
|
|
2933
|
+
{
|
|
2934
|
+
minWidth: this.options.minWidth ?? DEFAULT_MIN_SIZE2,
|
|
2935
|
+
minHeight: this.options.minHeight ?? DEFAULT_MIN_SIZE2
|
|
2936
|
+
},
|
|
2937
|
+
() => {
|
|
2938
|
+
if (this.session === session) this.session = void 0;
|
|
2939
|
+
}
|
|
2940
|
+
);
|
|
2941
|
+
this.session = session;
|
|
2942
|
+
return session;
|
|
2943
|
+
}
|
|
2944
|
+
active() {
|
|
2945
|
+
return this.session !== void 0;
|
|
2946
|
+
}
|
|
2947
|
+
/** Ends any live gesture; called when the board is destroyed. */
|
|
2948
|
+
dispose() {
|
|
2949
|
+
this.session?.commit();
|
|
2950
|
+
}
|
|
2951
|
+
selectedNodes() {
|
|
2952
|
+
return this.host.selection().map((id) => this.host.getNode(id)).filter((node) => node !== void 0);
|
|
2953
|
+
}
|
|
2954
|
+
};
|
|
2955
|
+
var ActiveSession = class {
|
|
2956
|
+
constructor(host, handle, nodes, groupOrigin, coalesceKey, limits, onEnd) {
|
|
2957
|
+
this.host = host;
|
|
2958
|
+
this.handle = handle;
|
|
2959
|
+
this.groupOrigin = groupOrigin;
|
|
2960
|
+
this.coalesceKey = coalesceKey;
|
|
2961
|
+
this.limits = limits;
|
|
2962
|
+
this.onEnd = onEnd;
|
|
2963
|
+
this.origins = nodes.map((node) => ({ id: node.id, type: node.type, geometry: nodeGeometry(node) }));
|
|
2964
|
+
}
|
|
2965
|
+
host;
|
|
2966
|
+
handle;
|
|
2967
|
+
groupOrigin;
|
|
2968
|
+
coalesceKey;
|
|
2969
|
+
limits;
|
|
2970
|
+
onEnd;
|
|
2971
|
+
done = false;
|
|
2972
|
+
origins;
|
|
2973
|
+
update(deltaWorld) {
|
|
2974
|
+
if (this.done) return;
|
|
2975
|
+
if (!Number.isFinite(deltaWorld.x) || !Number.isFinite(deltaWorld.y)) return;
|
|
2976
|
+
this.host.transaction(
|
|
2977
|
+
"Resize selection",
|
|
2978
|
+
() => {
|
|
2979
|
+
if (this.origins.length === 1) {
|
|
2980
|
+
const [only] = this.origins;
|
|
2981
|
+
this.host.resize(only.id, {
|
|
2982
|
+
handle: this.handle,
|
|
2983
|
+
deltaWorld,
|
|
2984
|
+
origin: only.geometry,
|
|
2985
|
+
minWidth: this.limits.minWidth,
|
|
2986
|
+
minHeight: this.limits.minHeight
|
|
2987
|
+
});
|
|
2988
|
+
return;
|
|
2989
|
+
}
|
|
2990
|
+
this.scaleGroup(deltaWorld);
|
|
2991
|
+
},
|
|
2992
|
+
{ origin: "ui", coalesceKey: this.coalesceKey }
|
|
2993
|
+
);
|
|
2994
|
+
}
|
|
2995
|
+
commit() {
|
|
2996
|
+
if (this.done) return;
|
|
2997
|
+
this.done = true;
|
|
2998
|
+
this.onEnd();
|
|
2999
|
+
}
|
|
3000
|
+
cancel() {
|
|
3001
|
+
if (this.done) return;
|
|
3002
|
+
this.done = true;
|
|
3003
|
+
this.host.transaction(
|
|
3004
|
+
"Resize selection",
|
|
3005
|
+
() => {
|
|
3006
|
+
for (const { id, geometry } of this.origins) {
|
|
3007
|
+
this.host.update(id, {
|
|
3008
|
+
x: geometry.x,
|
|
3009
|
+
y: geometry.y,
|
|
3010
|
+
width: geometry.width,
|
|
3011
|
+
height: geometry.height
|
|
3012
|
+
});
|
|
3013
|
+
}
|
|
3014
|
+
},
|
|
3015
|
+
{ origin: "ui", coalesceKey: this.coalesceKey }
|
|
3016
|
+
);
|
|
3017
|
+
this.onEnd();
|
|
3018
|
+
}
|
|
3019
|
+
/**
|
|
3020
|
+
* Scales the shared group box by the handle drag, then maps every node into
|
|
3021
|
+
* the new box: its offset from the anchored corner and its size both scale
|
|
3022
|
+
* by the same factor, so the selection resizes as one rigid unit.
|
|
3023
|
+
*
|
|
3024
|
+
* Each node's size still goes through its own ResizePolicy, so a fixed node
|
|
3025
|
+
* inside a multi-selection keeps its size (and its scaled position) while
|
|
3026
|
+
* its neighbours grow. Placement is computed here rather than by
|
|
3027
|
+
* nodes.resize(), which would re-anchor each node against its own origin
|
|
3028
|
+
* and tear the group apart.
|
|
3029
|
+
*/
|
|
3030
|
+
scaleGroup(deltaWorld) {
|
|
3031
|
+
const box = this.groupOrigin;
|
|
3032
|
+
const axes = resizeHandleAxes(this.handle);
|
|
3033
|
+
const width = Math.max(box.width + axes.horizontal * deltaWorld.x, this.limits.minWidth);
|
|
3034
|
+
const height = Math.max(box.height + axes.vertical * deltaWorld.y, this.limits.minHeight);
|
|
3035
|
+
const scaleX = box.width > 0 ? width / box.width : 1;
|
|
3036
|
+
const scaleY = box.height > 0 ? height / box.height : 1;
|
|
3037
|
+
const anchorX = axes.horizontal === -1 ? box.x + box.width : box.x;
|
|
3038
|
+
const anchorY = axes.vertical === -1 ? box.y + box.height : box.y;
|
|
3039
|
+
for (const { id, type, geometry } of this.origins) {
|
|
3040
|
+
const node = this.host.getNode(id);
|
|
3041
|
+
if (!node) continue;
|
|
3042
|
+
const resolved = resolveResizeSize(node, this.host.resizePolicy(type), {
|
|
3043
|
+
handle: this.handle,
|
|
3044
|
+
width: geometry.width * scaleX,
|
|
3045
|
+
height: geometry.height * scaleY,
|
|
3046
|
+
origin: geometry,
|
|
3047
|
+
minWidth: this.limits.minWidth,
|
|
3048
|
+
minHeight: this.limits.minHeight
|
|
3049
|
+
});
|
|
3050
|
+
if (!resolved) continue;
|
|
3051
|
+
this.host.update(id, {
|
|
3052
|
+
...resolved.patch,
|
|
3053
|
+
x: anchorX + (geometry.x - anchorX) * scaleX,
|
|
3054
|
+
y: anchorY + (geometry.y - anchorY) * scaleY,
|
|
3055
|
+
width: resolved.width,
|
|
3056
|
+
height: resolved.height
|
|
3057
|
+
});
|
|
3058
|
+
}
|
|
3059
|
+
}
|
|
3060
|
+
};
|
|
3061
|
+
function handleLocalPoint(handle, width, height) {
|
|
3062
|
+
const axes = resizeHandleAxes(handle);
|
|
3063
|
+
return {
|
|
3064
|
+
x: axes.horizontal === -1 ? 0 : axes.horizontal === 1 ? width : width / 2,
|
|
3065
|
+
y: axes.vertical === -1 ? 0 : axes.vertical === 1 ? height : height / 2
|
|
3066
|
+
};
|
|
3067
|
+
}
|
|
3068
|
+
function rotatedCorners(node) {
|
|
3069
|
+
const cos = Math.cos(node.rotation);
|
|
3070
|
+
const sin = Math.sin(node.rotation);
|
|
3071
|
+
return [
|
|
3072
|
+
{ x: 0, y: 0 },
|
|
3073
|
+
{ x: node.width, y: 0 },
|
|
3074
|
+
{ x: node.width, y: node.height },
|
|
3075
|
+
{ x: 0, y: node.height }
|
|
3076
|
+
].map((point) => ({
|
|
3077
|
+
x: node.x + point.x * cos - point.y * sin,
|
|
3078
|
+
y: node.y + point.x * sin + point.y * cos
|
|
3079
|
+
}));
|
|
3080
|
+
}
|
|
3081
|
+
|
|
3082
|
+
// src/index.ts
|
|
3083
|
+
var focusedBoard;
|
|
3084
|
+
var DEFAULT_VIRTUALIZATION_PADDING = 256;
|
|
3085
|
+
var EventHub2 = class {
|
|
3086
|
+
listeners = /* @__PURE__ */ new Map();
|
|
3087
|
+
on(eventName, listener) {
|
|
3088
|
+
const listeners = this.listeners.get(eventName) ?? /* @__PURE__ */ new Set();
|
|
3089
|
+
listeners.add(listener);
|
|
3090
|
+
this.listeners.set(eventName, listeners);
|
|
3091
|
+
return () => listeners.delete(listener);
|
|
3092
|
+
}
|
|
3093
|
+
emit(eventName, event) {
|
|
3094
|
+
for (const listener of this.listeners.get(eventName) ?? []) {
|
|
3095
|
+
try {
|
|
3096
|
+
listener(event);
|
|
3097
|
+
} catch {
|
|
3098
|
+
}
|
|
3099
|
+
}
|
|
3100
|
+
}
|
|
3101
|
+
clear() {
|
|
3102
|
+
this.listeners.clear();
|
|
3103
|
+
}
|
|
3104
|
+
};
|
|
3105
|
+
var PublicNodeHandle = class {
|
|
3106
|
+
constructor(board, id) {
|
|
3107
|
+
this.board = board;
|
|
3108
|
+
this.id = id;
|
|
3109
|
+
}
|
|
3110
|
+
board;
|
|
3111
|
+
id;
|
|
3112
|
+
getAttrs() {
|
|
3113
|
+
const node = this.board.getNode(this.id);
|
|
3114
|
+
if (!node) throw new NodeNotFoundError(this.id);
|
|
3115
|
+
return node;
|
|
3116
|
+
}
|
|
3117
|
+
setAttrs(patch) {
|
|
3118
|
+
this.board.setNodeAttrs(this.id, patch);
|
|
3119
|
+
return this;
|
|
3120
|
+
}
|
|
3121
|
+
x(value) {
|
|
3122
|
+
return this.attr("x", value);
|
|
3123
|
+
}
|
|
3124
|
+
y(value) {
|
|
3125
|
+
return this.attr("y", value);
|
|
3126
|
+
}
|
|
3127
|
+
width(value) {
|
|
3128
|
+
return this.attr("width", value);
|
|
3129
|
+
}
|
|
3130
|
+
height(value) {
|
|
3131
|
+
return this.attr("height", value);
|
|
3132
|
+
}
|
|
3133
|
+
rotation(value) {
|
|
3134
|
+
return this.attr("rotation", value);
|
|
3135
|
+
}
|
|
3136
|
+
visible(value) {
|
|
3137
|
+
if (value === void 0) return this.getAttrs().visible ?? true;
|
|
3138
|
+
this.setAttrs({ visible: value });
|
|
3139
|
+
return this;
|
|
3140
|
+
}
|
|
3141
|
+
remove() {
|
|
3142
|
+
this.board.removeNode(this.id);
|
|
3143
|
+
}
|
|
3144
|
+
on(eventName, listener) {
|
|
3145
|
+
return this.board.onNode(this.id, eventName, listener);
|
|
3146
|
+
}
|
|
3147
|
+
attr(key, value) {
|
|
3148
|
+
if (value === void 0) return this.getAttrs()[key];
|
|
3149
|
+
this.setAttrs({ [key]: value });
|
|
3150
|
+
return this;
|
|
3151
|
+
}
|
|
3152
|
+
};
|
|
3153
|
+
var PixiBoardFacade = class {
|
|
3154
|
+
ready;
|
|
3155
|
+
signal;
|
|
3156
|
+
capabilities;
|
|
3157
|
+
nodes;
|
|
3158
|
+
nodeTypes;
|
|
3159
|
+
transform;
|
|
3160
|
+
selection;
|
|
3161
|
+
viewport;
|
|
3162
|
+
history;
|
|
3163
|
+
document;
|
|
3164
|
+
lifecycle = "created";
|
|
3165
|
+
abortController = new AbortController();
|
|
3166
|
+
core;
|
|
3167
|
+
events = new EventHub2();
|
|
3168
|
+
nodeEvents = new EventHub2();
|
|
3169
|
+
cleanup = /* @__PURE__ */ new Set();
|
|
3170
|
+
options;
|
|
3171
|
+
rendererRegistry;
|
|
3172
|
+
transformController;
|
|
3173
|
+
renderer;
|
|
3174
|
+
renderedRevision = 0;
|
|
3175
|
+
frameId = 0;
|
|
3176
|
+
pendingRuntimeWork = Promise.resolve();
|
|
3177
|
+
destroyPromise;
|
|
3178
|
+
transactionDepth = 0;
|
|
3179
|
+
transformSequence = 0;
|
|
3180
|
+
constructor(options) {
|
|
3181
|
+
const rendererRegistry = options.renderer?.registry ?? new NodeRendererRegistry();
|
|
3182
|
+
this.rendererRegistry = rendererRegistry;
|
|
3183
|
+
this.options = {
|
|
3184
|
+
...options,
|
|
3185
|
+
renderer: {
|
|
3186
|
+
...options.renderer,
|
|
3187
|
+
registry: rendererRegistry
|
|
3188
|
+
}
|
|
3189
|
+
};
|
|
3190
|
+
this.signal = this.abortController.signal;
|
|
3191
|
+
this.core = new BoardCore({ ...this.options.core, document: this.options.document });
|
|
3192
|
+
const capabilities = createBoardCapabilities(this.core, {
|
|
3193
|
+
preview: options.preview,
|
|
3194
|
+
capture: options.capture
|
|
3195
|
+
});
|
|
3196
|
+
this.capabilities = scopeCapabilities(capabilities, this.signal, () => this.assertAlive());
|
|
3197
|
+
this.nodes = {
|
|
3198
|
+
create: async (input) => {
|
|
3199
|
+
this.assertAlive();
|
|
3200
|
+
const node = this.core.nodes.create(input);
|
|
3201
|
+
return this.node(node.id);
|
|
3202
|
+
},
|
|
3203
|
+
update: (nodeId, patch) => {
|
|
3204
|
+
this.updateNode(nodeId, patch);
|
|
3205
|
+
return this.node(nodeId);
|
|
3206
|
+
},
|
|
3207
|
+
resize: (nodeId, request) => {
|
|
3208
|
+
this.assertAlive();
|
|
3209
|
+
this.core.nodes.resize(nodeId, request);
|
|
3210
|
+
return this.node(nodeId);
|
|
3211
|
+
},
|
|
3212
|
+
remove: (nodeId) => this.removeNode(nodeId),
|
|
3213
|
+
get: (nodeId) => this.getNode(nodeId),
|
|
3214
|
+
list: (filter = {}) => {
|
|
3215
|
+
this.assertAlive();
|
|
3216
|
+
return this.core.nodes.list(filter);
|
|
3217
|
+
}
|
|
3218
|
+
};
|
|
3219
|
+
this.nodeTypes = {
|
|
3220
|
+
register: (definition, registrationOptions = {}) => this.registerNodeType(definition, registrationOptions),
|
|
3221
|
+
has: (type) => {
|
|
3222
|
+
this.assertAlive();
|
|
3223
|
+
return this.core.nodeTypes.has(type);
|
|
3224
|
+
},
|
|
3225
|
+
get: (type) => {
|
|
3226
|
+
this.assertAlive();
|
|
3227
|
+
return this.core.nodeTypes.get(type);
|
|
3228
|
+
},
|
|
3229
|
+
list: () => {
|
|
3230
|
+
this.assertAlive();
|
|
3231
|
+
return this.core.nodeTypes.list();
|
|
3232
|
+
}
|
|
3233
|
+
};
|
|
3234
|
+
this.transformController = new TransformController({
|
|
3235
|
+
selection: () => this.core.selection.get(),
|
|
3236
|
+
getNode: (nodeId) => this.core.nodes.get(nodeId),
|
|
3237
|
+
resizePolicy: (type) => this.core.nodeTypes.get(type)?.resize,
|
|
3238
|
+
resize: (nodeId, request) => {
|
|
3239
|
+
this.core.nodes.resize(nodeId, request);
|
|
3240
|
+
},
|
|
3241
|
+
update: (nodeId, patch) => {
|
|
3242
|
+
this.core.nodes.update(nodeId, patch);
|
|
3243
|
+
},
|
|
3244
|
+
transaction: (label, operation, transactionOptions) => this.transaction(label, operation, transactionOptions),
|
|
3245
|
+
nextId: () => `${++this.transformSequence}`
|
|
3246
|
+
}, this.options.transform ?? {});
|
|
3247
|
+
this.transform = {
|
|
3248
|
+
handles: () => {
|
|
3249
|
+
this.assertAlive();
|
|
3250
|
+
return this.transformController.handles();
|
|
3251
|
+
},
|
|
3252
|
+
bounds: () => {
|
|
3253
|
+
this.assertAlive();
|
|
3254
|
+
return this.transformController.bounds();
|
|
3255
|
+
},
|
|
3256
|
+
begin: (handle) => {
|
|
3257
|
+
this.assertAlive();
|
|
3258
|
+
return this.transformController.begin(handle);
|
|
3259
|
+
},
|
|
3260
|
+
active: () => this.transformController.active()
|
|
3261
|
+
};
|
|
3262
|
+
this.selection = {
|
|
3263
|
+
get: () => {
|
|
3264
|
+
this.assertAlive();
|
|
3265
|
+
return this.core.selection.get();
|
|
3266
|
+
},
|
|
3267
|
+
set: (ids) => {
|
|
3268
|
+
this.assertAlive();
|
|
3269
|
+
this.core.selection.set(ids);
|
|
3270
|
+
},
|
|
3271
|
+
toggle: (id) => {
|
|
3272
|
+
this.assertAlive();
|
|
3273
|
+
this.core.selection.toggle(id);
|
|
3274
|
+
},
|
|
3275
|
+
clear: () => {
|
|
3276
|
+
this.assertAlive();
|
|
3277
|
+
this.core.selection.clear();
|
|
3278
|
+
},
|
|
3279
|
+
onChange: (listener) => this.on("selection:change", listener)
|
|
3280
|
+
};
|
|
3281
|
+
this.viewport = {
|
|
3282
|
+
get: () => {
|
|
3283
|
+
this.assertAlive();
|
|
3284
|
+
return this.core.viewport.get();
|
|
3285
|
+
},
|
|
3286
|
+
set: (value) => {
|
|
3287
|
+
this.assertAlive();
|
|
3288
|
+
this.core.viewport.set(value);
|
|
3289
|
+
},
|
|
3290
|
+
panBy: (x, y) => {
|
|
3291
|
+
this.assertAlive();
|
|
3292
|
+
this.core.viewport.panBy(x, y);
|
|
3293
|
+
},
|
|
3294
|
+
zoomAt: (point, factor) => {
|
|
3295
|
+
this.assertAlive();
|
|
3296
|
+
this.core.viewport.zoomAt(point, factor);
|
|
3297
|
+
},
|
|
3298
|
+
fitNodes: (ids) => {
|
|
3299
|
+
this.assertAlive();
|
|
3300
|
+
this.core.viewport.fitNodes(ids);
|
|
3301
|
+
},
|
|
3302
|
+
fitBounds: (bounds) => {
|
|
3303
|
+
this.assertAlive();
|
|
3304
|
+
this.core.viewport.fitBounds(bounds);
|
|
3305
|
+
},
|
|
3306
|
+
toWorld: (point) => {
|
|
3307
|
+
this.assertAlive();
|
|
3308
|
+
return this.core.viewport.toWorld(point);
|
|
3309
|
+
},
|
|
3310
|
+
toScreen: (point) => {
|
|
3311
|
+
this.assertAlive();
|
|
3312
|
+
return this.core.viewport.toScreen(point);
|
|
3313
|
+
}
|
|
3314
|
+
};
|
|
3315
|
+
this.history = {
|
|
3316
|
+
canUndo: () => {
|
|
3317
|
+
this.assertAlive();
|
|
3318
|
+
return this.core.history.canUndo();
|
|
3319
|
+
},
|
|
3320
|
+
canRedo: () => {
|
|
3321
|
+
this.assertAlive();
|
|
3322
|
+
return this.core.history.canRedo();
|
|
3323
|
+
},
|
|
3324
|
+
undo: () => {
|
|
3325
|
+
this.assertAlive();
|
|
3326
|
+
return this.core.history.undo();
|
|
3327
|
+
},
|
|
3328
|
+
redo: () => {
|
|
3329
|
+
this.assertAlive();
|
|
3330
|
+
return this.core.history.redo();
|
|
3331
|
+
},
|
|
3332
|
+
clear: () => {
|
|
3333
|
+
this.assertAlive();
|
|
3334
|
+
this.core.history.clear();
|
|
3335
|
+
}
|
|
3336
|
+
};
|
|
3337
|
+
this.document = {
|
|
3338
|
+
snapshot: () => {
|
|
3339
|
+
this.assertAlive();
|
|
3340
|
+
return this.core.document.snapshot();
|
|
3341
|
+
},
|
|
3342
|
+
toJSON: () => {
|
|
3343
|
+
this.assertAlive();
|
|
3344
|
+
return this.core.document.toJSON();
|
|
3345
|
+
},
|
|
3346
|
+
load: async (input, loadOptions) => {
|
|
3347
|
+
this.assertAlive();
|
|
3348
|
+
this.core.document.load(input, loadOptions);
|
|
3349
|
+
await this.pendingRuntimeWork;
|
|
3350
|
+
},
|
|
3351
|
+
validate: (input) => {
|
|
3352
|
+
this.assertAlive();
|
|
3353
|
+
return this.core.document.validate(input);
|
|
3354
|
+
}
|
|
3355
|
+
};
|
|
3356
|
+
this.bindCoreEvents();
|
|
3357
|
+
this.bindRuntimePorts();
|
|
3358
|
+
this.ready = this.mount();
|
|
3359
|
+
}
|
|
3360
|
+
get state() {
|
|
3361
|
+
return this.lifecycle;
|
|
3362
|
+
}
|
|
3363
|
+
node(nodeId) {
|
|
3364
|
+
this.assertAlive();
|
|
3365
|
+
return new PublicNodeHandle(this, nodeId);
|
|
3366
|
+
}
|
|
3367
|
+
find(filter = {}) {
|
|
3368
|
+
this.assertAlive();
|
|
3369
|
+
return this.core.nodes.list(filter);
|
|
3370
|
+
}
|
|
3371
|
+
findOne(selector) {
|
|
3372
|
+
this.assertAlive();
|
|
3373
|
+
const nodeId = selector.startsWith("#") ? selector.slice(1) : selector;
|
|
3374
|
+
if (!nodeId) return void 0;
|
|
3375
|
+
return this.core.nodes.get(nodeId);
|
|
3376
|
+
}
|
|
3377
|
+
visibleNodeIds() {
|
|
3378
|
+
this.assertAlive();
|
|
3379
|
+
return this.renderer?.visibleNodeIds?.();
|
|
3380
|
+
}
|
|
3381
|
+
transaction(label, operation, options = {}) {
|
|
3382
|
+
this.assertAlive();
|
|
3383
|
+
return this.core.transaction(label, () => {
|
|
3384
|
+
this.transactionDepth += 1;
|
|
3385
|
+
try {
|
|
3386
|
+
return operation();
|
|
3387
|
+
} finally {
|
|
3388
|
+
this.transactionDepth -= 1;
|
|
3389
|
+
}
|
|
3390
|
+
}, options);
|
|
3391
|
+
}
|
|
3392
|
+
on(eventName, listener) {
|
|
3393
|
+
this.assertAlive();
|
|
3394
|
+
return this.events.on(eventName, listener);
|
|
3395
|
+
}
|
|
3396
|
+
focus() {
|
|
3397
|
+
this.assertAlive();
|
|
3398
|
+
focusedBoard = this;
|
|
3399
|
+
const container = this.options.container;
|
|
3400
|
+
container?.focus?.();
|
|
3401
|
+
}
|
|
3402
|
+
async capture(input, options = {}) {
|
|
3403
|
+
this.assertAlive();
|
|
3404
|
+
if (!this.capabilities.capture.available) throw new CapabilityUnavailableError("capture");
|
|
3405
|
+
const signal = mergeSignals(this.signal, options.signal);
|
|
3406
|
+
return this.capabilities.capture.capture(input, { ...options, signal });
|
|
3407
|
+
}
|
|
3408
|
+
getNode(nodeId) {
|
|
3409
|
+
this.assertAlive();
|
|
3410
|
+
return this.core.nodes.get(nodeId);
|
|
3411
|
+
}
|
|
3412
|
+
updateNode(nodeId, patch) {
|
|
3413
|
+
this.assertAlive();
|
|
3414
|
+
this.core.nodes.update(nodeId, patch);
|
|
3415
|
+
}
|
|
3416
|
+
setNodeAttrs(nodeId, patch) {
|
|
3417
|
+
if (this.transactionDepth > 0) this.updateNode(nodeId, patch);
|
|
3418
|
+
else this.transaction("Update node attrs", () => this.updateNode(nodeId, patch));
|
|
3419
|
+
}
|
|
3420
|
+
removeNode(nodeId) {
|
|
3421
|
+
this.assertAlive();
|
|
3422
|
+
this.core.nodes.remove(nodeId);
|
|
3423
|
+
}
|
|
3424
|
+
async registerNodeType(definition, registrationOptions) {
|
|
3425
|
+
this.assertAlive();
|
|
3426
|
+
if (registrationOptions.replace) {
|
|
3427
|
+
throw new Error("Public node type replacement is not supported; unregister the current definition first");
|
|
3428
|
+
}
|
|
3429
|
+
const { renderer, ...dataDefinition } = definition;
|
|
3430
|
+
const unregisterData = this.core.nodeTypes.register(
|
|
3431
|
+
dataDefinition,
|
|
3432
|
+
registrationOptions
|
|
3433
|
+
);
|
|
3434
|
+
let unregisterRenderer;
|
|
3435
|
+
try {
|
|
3436
|
+
if (renderer) {
|
|
3437
|
+
unregisterRenderer = this.rendererRegistry.register(
|
|
3438
|
+
definition.type,
|
|
3439
|
+
renderer,
|
|
3440
|
+
registrationOptions
|
|
3441
|
+
);
|
|
3442
|
+
}
|
|
3443
|
+
await this.refreshRendererTypes();
|
|
3444
|
+
} catch (error) {
|
|
3445
|
+
unregisterRenderer?.();
|
|
3446
|
+
unregisterData();
|
|
3447
|
+
try {
|
|
3448
|
+
await this.refreshRendererTypes();
|
|
3449
|
+
} catch (rollbackError) {
|
|
3450
|
+
throw new AggregateError([error, rollbackError], "Node type registration and renderer rollback both failed");
|
|
3451
|
+
}
|
|
3452
|
+
throw error;
|
|
3453
|
+
}
|
|
3454
|
+
let active = true;
|
|
3455
|
+
return async () => {
|
|
3456
|
+
if (!active) return;
|
|
3457
|
+
unregisterRenderer?.();
|
|
3458
|
+
unregisterData();
|
|
3459
|
+
await this.refreshRendererTypes();
|
|
3460
|
+
active = false;
|
|
3461
|
+
};
|
|
3462
|
+
}
|
|
3463
|
+
refreshRendererTypes() {
|
|
3464
|
+
const renderer = this.renderer;
|
|
3465
|
+
if (!renderer?.refreshRegisteredTypes) return Promise.resolve();
|
|
3466
|
+
const work = this.pendingRuntimeWork.then(async () => {
|
|
3467
|
+
if (!this.signal.aborted) await renderer.refreshRegisteredTypes?.();
|
|
3468
|
+
});
|
|
3469
|
+
this.pendingRuntimeWork = work.catch(() => void 0);
|
|
3470
|
+
return work;
|
|
3471
|
+
}
|
|
3472
|
+
onNode(nodeId, eventName, listener) {
|
|
3473
|
+
this.assertAlive();
|
|
3474
|
+
return this.nodeEvents.on(`${nodeId}:${eventName}`, listener);
|
|
3475
|
+
}
|
|
3476
|
+
async destroy() {
|
|
3477
|
+
if (this.destroyPromise) return this.destroyPromise;
|
|
3478
|
+
this.destroyPromise = this.performDestroy();
|
|
3479
|
+
return this.destroyPromise;
|
|
3480
|
+
}
|
|
3481
|
+
async mount() {
|
|
3482
|
+
this.lifecycle = "mounting";
|
|
3483
|
+
const persistence = this.options.persistence;
|
|
3484
|
+
if (persistence?.load) {
|
|
3485
|
+
const loaded = await persistence.load({ signal: this.signal });
|
|
3486
|
+
if (loaded && !this.signal.aborted) this.core.document.load(loaded);
|
|
3487
|
+
}
|
|
3488
|
+
if (this.signal.aborted) return;
|
|
3489
|
+
if (!this.options.headless && this.options.container) {
|
|
3490
|
+
const factory = this.options.rendererFactory ?? ((rendererOptions) => new PixiBoardRenderer(rendererOptions));
|
|
3491
|
+
const renderer = factory({ ...this.options.renderer, nodeTypes: this.core.nodeTypes });
|
|
3492
|
+
await renderer.init();
|
|
3493
|
+
if (this.signal.aborted) {
|
|
3494
|
+
await renderer.destroy();
|
|
3495
|
+
return;
|
|
3496
|
+
}
|
|
3497
|
+
this.renderer = renderer;
|
|
3498
|
+
await this.syncVisibleBounds();
|
|
3499
|
+
const snapshot = this.core.document.snapshot();
|
|
3500
|
+
await renderer.rebuild(snapshot);
|
|
3501
|
+
this.renderedRevision = snapshot.revision;
|
|
3502
|
+
}
|
|
3503
|
+
if (!this.signal.aborted) this.lifecycle = "ready";
|
|
3504
|
+
}
|
|
3505
|
+
bindCoreEvents() {
|
|
3506
|
+
this.cleanup.add(this.core.on("change", (event) => this.queueChange(event)));
|
|
3507
|
+
this.cleanup.add(this.core.on("selection:change", (event) => {
|
|
3508
|
+
if (!this.signal.aborted) this.events.emit("selection:change", event);
|
|
3509
|
+
}));
|
|
3510
|
+
this.cleanup.add(this.core.on("viewport:change", (event) => {
|
|
3511
|
+
if (!this.signal.aborted) this.events.emit("viewport:change", event);
|
|
3512
|
+
this.queueVisibleBoundsSync();
|
|
3513
|
+
}));
|
|
3514
|
+
this.cleanup.add(this.core.on("history:change", (event) => {
|
|
3515
|
+
if (!this.signal.aborted) this.events.emit("history:change", event);
|
|
3516
|
+
}));
|
|
3517
|
+
}
|
|
3518
|
+
bindRuntimePorts() {
|
|
3519
|
+
const eventPort = this.options.ports?.events;
|
|
3520
|
+
if (eventPort && this.options.interactions?.keyboard) {
|
|
3521
|
+
const listener = (event) => {
|
|
3522
|
+
if (focusedBoard === this) this.options.ports?.onKeyboardEvent?.(event);
|
|
3523
|
+
};
|
|
3524
|
+
eventPort.addEventListener("keydown", listener);
|
|
3525
|
+
this.cleanup.add(() => eventPort.removeEventListener("keydown", listener));
|
|
3526
|
+
}
|
|
3527
|
+
if (eventPort && this.options.interactions?.clipboard) {
|
|
3528
|
+
for (const type of ["copy", "cut", "paste"]) {
|
|
3529
|
+
const listener = (event) => {
|
|
3530
|
+
if (focusedBoard === this) this.options.ports?.onClipboardEvent?.(event);
|
|
3531
|
+
};
|
|
3532
|
+
eventPort.addEventListener(type, listener);
|
|
3533
|
+
this.cleanup.add(() => eventPort.removeEventListener(type, listener));
|
|
3534
|
+
}
|
|
3535
|
+
}
|
|
3536
|
+
const pointerTarget = this.options.container;
|
|
3537
|
+
if (pointerTarget && this.options.interactions?.pointer) {
|
|
3538
|
+
const listener = () => {
|
|
3539
|
+
focusedBoard = this;
|
|
3540
|
+
};
|
|
3541
|
+
pointerTarget.addEventListener("pointerdown", listener);
|
|
3542
|
+
this.cleanup.add(() => pointerTarget.removeEventListener("pointerdown", listener));
|
|
3543
|
+
}
|
|
3544
|
+
const focusTarget = this.options.container;
|
|
3545
|
+
if (focusTarget && typeof focusTarget.addEventListener === "function" && (this.options.interactions?.keyboard || this.options.interactions?.clipboard)) {
|
|
3546
|
+
const listener = () => {
|
|
3547
|
+
focusedBoard = this;
|
|
3548
|
+
};
|
|
3549
|
+
focusTarget.addEventListener("focusin", listener);
|
|
3550
|
+
this.cleanup.add(() => focusTarget.removeEventListener("focusin", listener));
|
|
3551
|
+
if (typeof HTMLElement !== "undefined" && focusTarget instanceof HTMLElement && !focusTarget.hasAttribute("tabindex")) {
|
|
3552
|
+
focusTarget.tabIndex = -1;
|
|
3553
|
+
this.cleanup.add(() => focusTarget.removeAttribute("tabindex"));
|
|
3554
|
+
}
|
|
3555
|
+
}
|
|
3556
|
+
const container = this.options.container;
|
|
3557
|
+
const createObserver = this.options.ports?.createResizeObserver;
|
|
3558
|
+
if (container && createObserver) {
|
|
3559
|
+
const observer = createObserver((entries) => {
|
|
3560
|
+
if (this.signal.aborted) return;
|
|
3561
|
+
const rect = entries[0]?.contentRect;
|
|
3562
|
+
if (rect?.width && rect?.height) {
|
|
3563
|
+
this.core.viewport.setScreenSize({ width: rect.width, height: rect.height });
|
|
3564
|
+
this.queueVisibleBoundsSync();
|
|
3565
|
+
}
|
|
3566
|
+
});
|
|
3567
|
+
observer.observe(container);
|
|
3568
|
+
this.cleanup.add(() => observer.disconnect());
|
|
3569
|
+
}
|
|
3570
|
+
const ticker = this.options.ports?.ticker;
|
|
3571
|
+
if (ticker) {
|
|
3572
|
+
const listener = () => void 0;
|
|
3573
|
+
ticker.add(listener);
|
|
3574
|
+
this.cleanup.add(() => ticker.remove(listener));
|
|
3575
|
+
}
|
|
3576
|
+
}
|
|
3577
|
+
/**
|
|
3578
|
+
* Pushes the current visible rectangle to the renderer. Runs inside the
|
|
3579
|
+
* runtime queue so it can never interleave with an in-flight apply/rebuild,
|
|
3580
|
+
* which would leave the renderer culling against one revision while
|
|
3581
|
+
* reconciling another.
|
|
3582
|
+
*/
|
|
3583
|
+
async syncVisibleBounds() {
|
|
3584
|
+
const renderer = this.renderer;
|
|
3585
|
+
if (!renderer?.setVisibleBounds || this.signal.aborted) return;
|
|
3586
|
+
const virtualization = this.options.virtualization;
|
|
3587
|
+
if (virtualization?.enabled === false) {
|
|
3588
|
+
await renderer.setVisibleBounds(void 0);
|
|
3589
|
+
return;
|
|
3590
|
+
}
|
|
3591
|
+
const padding = virtualization?.padding ?? DEFAULT_VIRTUALIZATION_PADDING;
|
|
3592
|
+
const bounds = this.core.viewport.visibleWorldBounds(padding);
|
|
3593
|
+
await renderer.setVisibleBounds(bounds, this.core.viewport.get().scale);
|
|
3594
|
+
}
|
|
3595
|
+
queueVisibleBoundsSync() {
|
|
3596
|
+
if (this.signal.aborted || !this.renderer?.setVisibleBounds) return;
|
|
3597
|
+
this.pendingRuntimeWork = this.pendingRuntimeWork.then(async () => {
|
|
3598
|
+
await this.syncVisibleBounds();
|
|
3599
|
+
if (this.signal.aborted) return;
|
|
3600
|
+
this.events.emit("render:complete", {
|
|
3601
|
+
revision: this.renderedRevision,
|
|
3602
|
+
frameId: ++this.frameId
|
|
3603
|
+
});
|
|
3604
|
+
}).catch((error) => {
|
|
3605
|
+
if (!this.signal.aborted) queueMicrotask(() => {
|
|
3606
|
+
throw error;
|
|
3607
|
+
});
|
|
3608
|
+
});
|
|
3609
|
+
}
|
|
3610
|
+
queueChange(event) {
|
|
3611
|
+
this.pendingRuntimeWork = this.pendingRuntimeWork.then(async () => {
|
|
3612
|
+
if (this.signal.aborted) return;
|
|
3613
|
+
if (this.lifecycle === "mounting" && event.changeSet.origin === "load") return;
|
|
3614
|
+
if (this.renderer) {
|
|
3615
|
+
try {
|
|
3616
|
+
const result = await waitForAbort(this.renderer.apply(event.documentUpdate, event.changeSet), this.signal);
|
|
3617
|
+
if (result === "rebuild-required" && !this.signal.aborted) {
|
|
3618
|
+
await waitForAbort(this.renderer.rebuild(this.core.document.snapshot()), this.signal);
|
|
3619
|
+
}
|
|
3620
|
+
} catch (error) {
|
|
3621
|
+
if (!this.signal.aborted) {
|
|
3622
|
+
await waitForAbort(this.renderer.rebuild(this.core.document.snapshot()), this.signal);
|
|
3623
|
+
}
|
|
3624
|
+
throw error;
|
|
3625
|
+
}
|
|
3626
|
+
if (this.signal.aborted) return;
|
|
3627
|
+
this.renderedRevision = event.revision;
|
|
3628
|
+
this.events.emit("render:complete", {
|
|
3629
|
+
revision: event.revision,
|
|
3630
|
+
frameId: ++this.frameId
|
|
3631
|
+
});
|
|
3632
|
+
}
|
|
3633
|
+
if (this.signal.aborted) return;
|
|
3634
|
+
const persistence = this.options.persistence;
|
|
3635
|
+
if (persistence?.save) {
|
|
3636
|
+
await waitForAbort(persistence.save(this.core.document.toJSON(), { signal: this.signal }), this.signal);
|
|
3637
|
+
}
|
|
3638
|
+
if (this.signal.aborted) return;
|
|
3639
|
+
this.events.emit("change", event);
|
|
3640
|
+
if (event.changeSet.assetChangedNodeIds.length) this.events.emit("assets:change", event);
|
|
3641
|
+
for (const nodeId of [
|
|
3642
|
+
...event.changeSet.addedNodeIds,
|
|
3643
|
+
...event.changeSet.updatedNodeIds,
|
|
3644
|
+
...event.changeSet.removedNodeIds
|
|
3645
|
+
]) {
|
|
3646
|
+
this.nodeEvents.emit(`${nodeId}:change`, event);
|
|
3647
|
+
}
|
|
3648
|
+
}).catch((error) => {
|
|
3649
|
+
if (!this.signal.aborted) queueMicrotask(() => {
|
|
3650
|
+
throw error;
|
|
3651
|
+
});
|
|
3652
|
+
});
|
|
3653
|
+
}
|
|
3654
|
+
async performDestroy() {
|
|
3655
|
+
if (this.lifecycle === "destroyed") return;
|
|
3656
|
+
this.lifecycle = "destroying";
|
|
3657
|
+
this.transformController.dispose();
|
|
3658
|
+
if (focusedBoard === this) focusedBoard = void 0;
|
|
3659
|
+
this.abortController.abort(new BoardDestroyedError());
|
|
3660
|
+
for (const dispose of this.cleanup) dispose();
|
|
3661
|
+
this.cleanup.clear();
|
|
3662
|
+
await this.renderer?.destroy();
|
|
3663
|
+
await this.pendingRuntimeWork.catch(() => void 0);
|
|
3664
|
+
await this.options.persistence?.destroy?.();
|
|
3665
|
+
this.events.clear();
|
|
3666
|
+
this.nodeEvents.clear();
|
|
3667
|
+
this.lifecycle = "destroyed";
|
|
3668
|
+
}
|
|
3669
|
+
assertAlive() {
|
|
3670
|
+
if (this.lifecycle === "destroying" || this.lifecycle === "destroyed") {
|
|
3671
|
+
throw new BoardDestroyedError();
|
|
3672
|
+
}
|
|
3673
|
+
}
|
|
3674
|
+
};
|
|
3675
|
+
async function createPixiBoard(options = {}) {
|
|
3676
|
+
return new PixiBoardFacade(options);
|
|
3677
|
+
}
|
|
3678
|
+
function mergeSignals(instanceSignal, externalSignal) {
|
|
3679
|
+
if (!externalSignal) return instanceSignal;
|
|
3680
|
+
const controller = new AbortController();
|
|
3681
|
+
const abort = (signal) => controller.abort(signal.reason);
|
|
3682
|
+
if (instanceSignal.aborted) abort(instanceSignal);
|
|
3683
|
+
else instanceSignal.addEventListener("abort", () => abort(instanceSignal), { once: true });
|
|
3684
|
+
if (externalSignal.aborted) abort(externalSignal);
|
|
3685
|
+
else externalSignal.addEventListener("abort", () => abort(externalSignal), { once: true });
|
|
3686
|
+
return controller.signal;
|
|
3687
|
+
}
|
|
3688
|
+
function waitForAbort(operation, signal) {
|
|
3689
|
+
if (signal.aborted) {
|
|
3690
|
+
void operation.catch(() => void 0);
|
|
3691
|
+
return Promise.resolve(void 0);
|
|
3692
|
+
}
|
|
3693
|
+
const aborted2 = new Promise((resolve) => {
|
|
3694
|
+
signal.addEventListener("abort", () => resolve(void 0), { once: true });
|
|
3695
|
+
});
|
|
3696
|
+
void operation.catch(() => void 0);
|
|
3697
|
+
return Promise.race([operation, aborted2]);
|
|
3698
|
+
}
|
|
3699
|
+
function scopeCapabilities(capabilities, instanceSignal, assertAlive) {
|
|
3700
|
+
const options = (value) => ({
|
|
3701
|
+
...value ?? {},
|
|
3702
|
+
signal: mergeSignals(instanceSignal, value?.signal)
|
|
3703
|
+
});
|
|
3704
|
+
return {
|
|
3705
|
+
availability: capabilities.availability,
|
|
3706
|
+
document: {
|
|
3707
|
+
snapshot: (value) => {
|
|
3708
|
+
assertAlive();
|
|
3709
|
+
return capabilities.document.snapshot(options(value));
|
|
3710
|
+
},
|
|
3711
|
+
load: (input, value) => {
|
|
3712
|
+
assertAlive();
|
|
3713
|
+
return capabilities.document.load(input, options(value));
|
|
3714
|
+
},
|
|
3715
|
+
validate: (input, value) => {
|
|
3716
|
+
assertAlive();
|
|
3717
|
+
return capabilities.document.validate(input, options(value));
|
|
3718
|
+
}
|
|
3719
|
+
},
|
|
3720
|
+
nodes: {
|
|
3721
|
+
read: (input, value) => {
|
|
3722
|
+
assertAlive();
|
|
3723
|
+
return capabilities.nodes.read(input, options(value));
|
|
3724
|
+
},
|
|
3725
|
+
create: (input, value) => {
|
|
3726
|
+
assertAlive();
|
|
3727
|
+
return capabilities.nodes.create(input, options(value));
|
|
3728
|
+
},
|
|
3729
|
+
update: (input, value) => {
|
|
3730
|
+
assertAlive();
|
|
3731
|
+
return capabilities.nodes.update(input, options(value));
|
|
3732
|
+
},
|
|
3733
|
+
delete: (input, value) => {
|
|
3734
|
+
assertAlive();
|
|
3735
|
+
return capabilities.nodes.delete(input, options(value));
|
|
3736
|
+
}
|
|
3737
|
+
},
|
|
3738
|
+
assets: {
|
|
3739
|
+
read: (input, value) => {
|
|
3740
|
+
assertAlive();
|
|
3741
|
+
return capabilities.assets.read(input, options(value));
|
|
3742
|
+
},
|
|
3743
|
+
upsert: (input, value) => {
|
|
3744
|
+
assertAlive();
|
|
3745
|
+
return capabilities.assets.upsert(input, options(value));
|
|
3746
|
+
},
|
|
3747
|
+
remove: (input, value) => {
|
|
3748
|
+
assertAlive();
|
|
3749
|
+
return capabilities.assets.remove(input, options(value));
|
|
3750
|
+
}
|
|
3751
|
+
},
|
|
3752
|
+
selection: {
|
|
3753
|
+
get: (value) => {
|
|
3754
|
+
assertAlive();
|
|
3755
|
+
return capabilities.selection.get(options(value));
|
|
3756
|
+
},
|
|
3757
|
+
set: (nodeIds, value) => {
|
|
3758
|
+
assertAlive();
|
|
3759
|
+
return capabilities.selection.set(nodeIds, options(value));
|
|
3760
|
+
}
|
|
3761
|
+
},
|
|
3762
|
+
viewport: {
|
|
3763
|
+
get: (value) => {
|
|
3764
|
+
assertAlive();
|
|
3765
|
+
return capabilities.viewport.get(options(value));
|
|
3766
|
+
},
|
|
3767
|
+
set: (viewport, value) => {
|
|
3768
|
+
assertAlive();
|
|
3769
|
+
return capabilities.viewport.set(viewport, options(value));
|
|
3770
|
+
}
|
|
3771
|
+
},
|
|
3772
|
+
history: {
|
|
3773
|
+
canUndo: () => {
|
|
3774
|
+
assertAlive();
|
|
3775
|
+
return capabilities.history.canUndo();
|
|
3776
|
+
},
|
|
3777
|
+
canRedo: () => {
|
|
3778
|
+
assertAlive();
|
|
3779
|
+
return capabilities.history.canRedo();
|
|
3780
|
+
},
|
|
3781
|
+
clear: () => {
|
|
3782
|
+
assertAlive();
|
|
3783
|
+
capabilities.history.clear();
|
|
3784
|
+
},
|
|
3785
|
+
undo: (value) => {
|
|
3786
|
+
assertAlive();
|
|
3787
|
+
return capabilities.history.undo(options(value));
|
|
3788
|
+
},
|
|
3789
|
+
redo: (value) => {
|
|
3790
|
+
assertAlive();
|
|
3791
|
+
return capabilities.history.redo(options(value));
|
|
3792
|
+
}
|
|
3793
|
+
},
|
|
3794
|
+
preview: {
|
|
3795
|
+
get: (input, value) => {
|
|
3796
|
+
assertAlive();
|
|
3797
|
+
return capabilities.preview.get(input, options(value));
|
|
3798
|
+
}
|
|
3799
|
+
},
|
|
3800
|
+
capture: {
|
|
3801
|
+
available: capabilities.capture.available,
|
|
3802
|
+
capture: (input, value) => {
|
|
3803
|
+
assertAlive();
|
|
3804
|
+
return capabilities.capture.capture(input, options(value));
|
|
3805
|
+
}
|
|
3806
|
+
}
|
|
3807
|
+
};
|
|
3808
|
+
}
|
|
3809
|
+
|
|
3810
|
+
export { AssetUnavailableError, BoardDestroyedError, CapabilityError, CapabilityUnavailableError, DocumentValidationError, NodeNotFoundError, NodeTypeNotRegisteredError, NodeValidationError, PermissionDeniedError, RESIZE_HANDLES, TransactionConflictError, createPixiBoard, resizeHandleAxes, resizeHandleCursor };
|
|
3811
|
+
//# sourceMappingURL=index.js.map
|
|
3812
|
+
//# sourceMappingURL=index.js.map
|