@daasboard/editor 0.0.0-stage → 0.1.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/LICENSE +5 -0
- package/README.md +6 -2
- package/dist/camera/CameraInput.d.ts +106 -0
- package/dist/camera/CameraInput.js +398 -0
- package/dist/clipboard/ObjectClipboard.d.ts +40 -0
- package/dist/clipboard/ObjectClipboard.js +243 -0
- package/dist/clipboard/PasteCommitError.d.ts +4 -0
- package/dist/clipboard/PasteCommitError.js +7 -0
- package/dist/clipboard/carrier.d.ts +5 -0
- package/dist/clipboard/carrier.js +59 -0
- package/dist/clipboard/objects.d.ts +21 -0
- package/dist/clipboard/objects.js +362 -0
- package/dist/clipboard/placement.d.ts +11 -0
- package/dist/clipboard/placement.js +21 -0
- package/dist/clipboard/systemImages.d.ts +1 -0
- package/dist/clipboard/systemImages.js +24 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +22 -0
- package/dist/input/TouchInput.d.ts +159 -0
- package/dist/input/TouchInput.js +656 -0
- package/dist/input/visiblePart.d.ts +8 -0
- package/dist/input/visiblePart.js +15 -0
- package/dist/interaction/InteractionLayer.d.ts +83 -0
- package/dist/interaction/InteractionLayer.js +520 -0
- package/dist/props/PropertyController.d.ts +130 -0
- package/dist/props/PropertyController.js +1448 -0
- package/dist/props/PropertyRegistry.d.ts +38 -0
- package/dist/props/PropertyRegistry.js +21 -0
- package/dist/props/builtinProperties.d.ts +23 -0
- package/dist/props/builtinProperties.js +350 -0
- package/dist/props/defaults.d.ts +6 -0
- package/dist/props/defaults.js +29 -0
- package/dist/selection/SelectionModel.d.ts +25 -0
- package/dist/selection/SelectionModel.js +79 -0
- package/dist/text/CanvasEditorView.d.ts +99 -0
- package/dist/text/CanvasEditorView.js +308 -0
- package/dist/text/InputSink.d.ts +157 -0
- package/dist/text/InputSink.js +841 -0
- package/dist/text/TextEditSession.d.ts +173 -0
- package/dist/text/TextEditSession.js +1328 -0
- package/dist/text/clipboard.d.ts +11 -0
- package/dist/text/clipboard.js +489 -0
- package/dist/text/commands.d.ts +48 -0
- package/dist/text/commands.js +329 -0
- package/dist/text/contextMirror.d.ts +51 -0
- package/dist/text/contextMirror.js +292 -0
- package/dist/text/flat.d.ts +35 -0
- package/dist/text/flat.js +556 -0
- package/dist/text/graphemes.d.ts +16 -0
- package/dist/text/graphemes.js +219 -0
- package/dist/text/index.d.ts +13 -0
- package/dist/text/index.js +13 -0
- package/dist/text/inputReducer.d.ts +24 -0
- package/dist/text/inputReducer.js +44 -0
- package/dist/text/keymap.d.ts +17 -0
- package/dist/text/keymap.js +148 -0
- package/dist/text/schema.d.ts +12 -0
- package/dist/text/schema.js +44 -0
- package/dist/text/sessionFocus.d.ts +20 -0
- package/dist/text/sessionFocus.js +51 -0
- package/dist/text/types.d.ts +114 -0
- package/dist/text/types.js +1 -0
- package/dist/tools/CreateTool.d.ts +78 -0
- package/dist/tools/CreateTool.js +258 -0
- package/dist/tools/SelectTool.d.ts +155 -0
- package/dist/tools/SelectTool.js +808 -0
- package/dist/tools/ToolManager.d.ts +63 -0
- package/dist/tools/ToolManager.js +146 -0
- package/dist/tools/connectorEndpoints.d.ts +17 -0
- package/dist/tools/connectorEndpoints.js +53 -0
- package/dist/tools/frameRotation.d.ts +8 -0
- package/dist/tools/frameRotation.js +66 -0
- package/dist/tools/gradientHandles.d.ts +67 -0
- package/dist/tools/gradientHandles.js +110 -0
- package/dist/tools/presetPaths.d.ts +5 -0
- package/dist/tools/presetPaths.js +49 -0
- package/dist/tools/snapResize.d.ts +7 -0
- package/dist/tools/snapResize.js +65 -0
- package/dist/tools/snapping.d.ts +30 -0
- package/dist/tools/snapping.js +280 -0
- package/dist/tools/transform.d.ts +52 -0
- package/dist/tools/transform.js +277 -0
- package/package.json +36 -3
|
@@ -0,0 +1,1448 @@
|
|
|
1
|
+
import { applyTextDelta, emptyTextContent, getPath } from "@daasboard/doc";
|
|
2
|
+
import { isTransformable, mapBoxInto, transformBounds, MIN_SIZE } from "../tools/transform.js";
|
|
3
|
+
import { captureRotationIds, rotationPatches } from "../tools/frameRotation.js";
|
|
4
|
+
import { defaultStyle } from "./defaults.js";
|
|
5
|
+
import { validateObjects } from "../clipboard/objects.js";
|
|
6
|
+
import { clipboardBounds, pasteOffset } from "../clipboard/placement.js";
|
|
7
|
+
import { PasteCommitError } from "../clipboard/PasteCommitError.js";
|
|
8
|
+
import { readDescriptorState } from "./builtinProperties.js";
|
|
9
|
+
function deepEq(a, b) {
|
|
10
|
+
if (a === b || (a !== a && b !== b))
|
|
11
|
+
return true;
|
|
12
|
+
if (!a || !b || typeof a !== "object" || typeof b !== "object")
|
|
13
|
+
return false;
|
|
14
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
15
|
+
return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((v, i) => deepEq(v, b[i]));
|
|
16
|
+
}
|
|
17
|
+
const left = a, right = b;
|
|
18
|
+
const keys = Object.keys(left).filter((key) => left[key] !== undefined);
|
|
19
|
+
return keys.length === Object.keys(right).filter((key) => right[key] !== undefined).length
|
|
20
|
+
&& keys.every((key) => Object.hasOwn(right, key) && deepEq(left[key], right[key]));
|
|
21
|
+
}
|
|
22
|
+
function copyReadData(value) {
|
|
23
|
+
if (Array.isArray(value))
|
|
24
|
+
return value.map(copyReadData);
|
|
25
|
+
if (value && typeof value === "object")
|
|
26
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, copyReadData(v)]));
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
function copyReadValue(pv) {
|
|
30
|
+
const copy = { ...pv };
|
|
31
|
+
if (pv.value !== null && typeof pv.value === "object")
|
|
32
|
+
copy.value = copyReadData(pv.value);
|
|
33
|
+
if (pv.values)
|
|
34
|
+
copy.values = pv.values.map(copyReadData);
|
|
35
|
+
return copy;
|
|
36
|
+
}
|
|
37
|
+
const overlapsPath = (a, b) => a === b || a.startsWith(`${b}.`) || b.startsWith(`${a}.`);
|
|
38
|
+
function previewCopy(o) {
|
|
39
|
+
const text = Object.getOwnPropertyDescriptor(o, "text");
|
|
40
|
+
if (!text?.get || !text.enumerable)
|
|
41
|
+
return { ...o };
|
|
42
|
+
const copy = {};
|
|
43
|
+
const source = o;
|
|
44
|
+
const target = copy;
|
|
45
|
+
for (const key of Object.keys(o)) {
|
|
46
|
+
if (key === "text")
|
|
47
|
+
Object.defineProperty(copy, key, { enumerable: true, configurable: true, get: () => o.text });
|
|
48
|
+
else if (key === "__proto__")
|
|
49
|
+
Object.defineProperty(copy, key, { enumerable: true, configurable: true, writable: true, value: source[key] });
|
|
50
|
+
else
|
|
51
|
+
target[key] = source[key];
|
|
52
|
+
}
|
|
53
|
+
for (const key of Object.getOwnPropertySymbols(o))
|
|
54
|
+
if (Object.prototype.propertyIsEnumerable.call(o, key))
|
|
55
|
+
Reflect.set(copy, key, Reflect.get(o, key));
|
|
56
|
+
return copy;
|
|
57
|
+
}
|
|
58
|
+
const CONTAINER_KINDS = new Set(["frame", "group", "section"]);
|
|
59
|
+
const isMoveKey = (path) => path === "x" || path === "y";
|
|
60
|
+
let createCounter = 0;
|
|
61
|
+
function provisionalId(kind) {
|
|
62
|
+
const c = globalThis.crypto;
|
|
63
|
+
return c?.randomUUID ? `${kind}-${c.randomUUID()}` : `${kind}-${Date.now().toString(36)}-${(createCounter++).toString(36)}`;
|
|
64
|
+
}
|
|
65
|
+
export function planZOrder(sibs, selected, how) {
|
|
66
|
+
const block = sibs.filter((id) => selected.has(id));
|
|
67
|
+
const rest = sibs.filter((id) => !selected.has(id));
|
|
68
|
+
if (block.length === 0 || rest.length === 0)
|
|
69
|
+
return null;
|
|
70
|
+
let at;
|
|
71
|
+
switch (how) {
|
|
72
|
+
case "front":
|
|
73
|
+
at = rest.length;
|
|
74
|
+
break;
|
|
75
|
+
case "back":
|
|
76
|
+
at = 0;
|
|
77
|
+
break;
|
|
78
|
+
case "forward": {
|
|
79
|
+
const top = sibs.indexOf(block[block.length - 1]);
|
|
80
|
+
const above = sibs.slice(top + 1).find((id) => !selected.has(id));
|
|
81
|
+
if (above === undefined)
|
|
82
|
+
return null;
|
|
83
|
+
at = rest.indexOf(above) + 1;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case "backward": {
|
|
87
|
+
const bottom = sibs.indexOf(block[0]);
|
|
88
|
+
const below = sibs.slice(0, bottom).reverse().find((id) => !selected.has(id));
|
|
89
|
+
if (below === undefined)
|
|
90
|
+
return null;
|
|
91
|
+
at = rest.indexOf(below);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const next = [...rest.slice(0, at), ...block, ...rest.slice(at)];
|
|
96
|
+
return next.every((id, i) => id === sibs[i]) ? null : next;
|
|
97
|
+
}
|
|
98
|
+
function sameMarks(a, b) {
|
|
99
|
+
const ka = Object.keys(a).filter((k) => k !== "text");
|
|
100
|
+
const kb = Object.keys(b).filter((k) => k !== "text");
|
|
101
|
+
return ka.length === kb.length && ka.every((k) => a[k] === b[k]);
|
|
102
|
+
}
|
|
103
|
+
function withoutTextKey(t, key) {
|
|
104
|
+
return {
|
|
105
|
+
paragraphs: t.paragraphs.map((p) => {
|
|
106
|
+
const para = { ...p };
|
|
107
|
+
if ("para" in key)
|
|
108
|
+
delete para[key.para];
|
|
109
|
+
const runs = [];
|
|
110
|
+
for (const r of p.runs) {
|
|
111
|
+
const run = { ...r };
|
|
112
|
+
if ("run" in key)
|
|
113
|
+
delete run[key.run];
|
|
114
|
+
const last = runs[runs.length - 1];
|
|
115
|
+
if (last && sameMarks(last, run))
|
|
116
|
+
runs[runs.length - 1] = { ...last, text: last.text + run.text };
|
|
117
|
+
else
|
|
118
|
+
runs.push(run);
|
|
119
|
+
}
|
|
120
|
+
para.runs = runs;
|
|
121
|
+
return para;
|
|
122
|
+
}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export class PropertyController {
|
|
126
|
+
doc;
|
|
127
|
+
registry;
|
|
128
|
+
selection;
|
|
129
|
+
fixedIds;
|
|
130
|
+
listeners = new Set();
|
|
131
|
+
overlay = new Map();
|
|
132
|
+
previewLabel = "";
|
|
133
|
+
absolutePreview = false;
|
|
134
|
+
rotationIds = null;
|
|
135
|
+
rotationExcludedId = null;
|
|
136
|
+
rotationRequest = null;
|
|
137
|
+
unsubs = [];
|
|
138
|
+
textTarget_ = null;
|
|
139
|
+
textPreview = null;
|
|
140
|
+
readCache = new Map();
|
|
141
|
+
readTargets = [];
|
|
142
|
+
readVersions = [];
|
|
143
|
+
readRegistry = [];
|
|
144
|
+
readDescriptors = new Map();
|
|
145
|
+
readOverlay = new Map();
|
|
146
|
+
readPaths = new Set();
|
|
147
|
+
readAbsolute = false;
|
|
148
|
+
readInvalidation = null;
|
|
149
|
+
commands;
|
|
150
|
+
constructor(doc, registry, selection, fixedIds) {
|
|
151
|
+
this.doc = doc;
|
|
152
|
+
this.registry = registry;
|
|
153
|
+
this.selection = selection;
|
|
154
|
+
this.fixedIds = fixedIds;
|
|
155
|
+
if (!fixedIds) {
|
|
156
|
+
this.unsubs.push(selection.onChange(() => {
|
|
157
|
+
this.readCache.clear();
|
|
158
|
+
if (this.textPreview && this.textPreview.target !== this.activeTextTarget())
|
|
159
|
+
this.dropTextPreview();
|
|
160
|
+
this.emit();
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
this.unsubs.push(doc.subscribe((cs) => {
|
|
164
|
+
this.readCache.clear();
|
|
165
|
+
const ids = this.targetIds();
|
|
166
|
+
if (ids.length === 0)
|
|
167
|
+
return;
|
|
168
|
+
const touched = new Set([...cs.updated.map((o) => o.id), ...cs.removed, ...cs.added.map((o) => o.id)]);
|
|
169
|
+
if (ids.some((id) => touched.has(id)) || cs.reordered)
|
|
170
|
+
this.emit();
|
|
171
|
+
}));
|
|
172
|
+
this.commands = this.buildCommands();
|
|
173
|
+
}
|
|
174
|
+
for(ids) {
|
|
175
|
+
return new PropertyController(this.doc, this.registry, this.selection, ids);
|
|
176
|
+
}
|
|
177
|
+
targetIds() {
|
|
178
|
+
return this.fixedIds ? [...this.fixedIds] : this.selection.toArray();
|
|
179
|
+
}
|
|
180
|
+
targets() {
|
|
181
|
+
const out = [];
|
|
182
|
+
for (const id of this.targetIds()) {
|
|
183
|
+
const o = this.doc.get(id);
|
|
184
|
+
if (o)
|
|
185
|
+
out.push(o);
|
|
186
|
+
}
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
descriptors() {
|
|
190
|
+
return this.registry.forObjects(this.targets());
|
|
191
|
+
}
|
|
192
|
+
setTextTarget(t) {
|
|
193
|
+
if (t === this.textTarget_)
|
|
194
|
+
return;
|
|
195
|
+
this.dropTextPreview();
|
|
196
|
+
this.textTarget_ = t;
|
|
197
|
+
this.readCache.clear();
|
|
198
|
+
this.emit();
|
|
199
|
+
}
|
|
200
|
+
textTarget() {
|
|
201
|
+
return this.textTarget_;
|
|
202
|
+
}
|
|
203
|
+
activeTextTarget() {
|
|
204
|
+
const t = this.textTarget_;
|
|
205
|
+
if (!t)
|
|
206
|
+
return null;
|
|
207
|
+
const ids = this.targetIds();
|
|
208
|
+
return ids.length === 1 && ids[0] === t.objectId ? t : null;
|
|
209
|
+
}
|
|
210
|
+
targetsFor(key) {
|
|
211
|
+
const d = this.registry.get(key);
|
|
212
|
+
return d ? this.targets().filter((o) => d.appliesTo(o)) : [];
|
|
213
|
+
}
|
|
214
|
+
applicablePatch(o, patch) {
|
|
215
|
+
const out = {};
|
|
216
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
217
|
+
const d = this.registry.get(key);
|
|
218
|
+
if (!d || d.appliesTo(o))
|
|
219
|
+
out[key] = value;
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
read(keys) {
|
|
224
|
+
const targets = this.targets();
|
|
225
|
+
if (targets.length !== this.readTargets.length || targets.some((o, i) => o !== this.readTargets[i] || o.version !== this.readVersions[i])) {
|
|
226
|
+
this.readCache.clear();
|
|
227
|
+
this.readTargets = targets;
|
|
228
|
+
for (let i = 0; i < targets.length; i++)
|
|
229
|
+
this.readVersions[i] = targets[i].version;
|
|
230
|
+
this.readVersions.length = targets.length;
|
|
231
|
+
}
|
|
232
|
+
const all = this.registry.all();
|
|
233
|
+
if (all.some((d) => this.registry.get(d.key) !== d)) {
|
|
234
|
+
this.readCache.clear();
|
|
235
|
+
return this.readFull(keys);
|
|
236
|
+
}
|
|
237
|
+
let registryChanged = all.length !== this.readRegistry.length;
|
|
238
|
+
for (let i = 0; i < all.length; i++) {
|
|
239
|
+
const state = readDescriptorState(all[i]);
|
|
240
|
+
if (state !== this.readRegistry[i]) {
|
|
241
|
+
registryChanged = true;
|
|
242
|
+
this.readRegistry[i] = state;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
this.readRegistry.length = all.length;
|
|
246
|
+
if (registryChanged) {
|
|
247
|
+
this.readCache.clear();
|
|
248
|
+
this.readInvalidation = null;
|
|
249
|
+
this.readDescriptors = new Map(all.map((d, i) => [d, this.readRegistry[i]]));
|
|
250
|
+
}
|
|
251
|
+
let changed = registryChanged || this.absolutePreview !== this.readAbsolute || this.overlay.size !== this.readOverlay.size;
|
|
252
|
+
const paths = new Set();
|
|
253
|
+
const routed = new Set();
|
|
254
|
+
let dynamicPreview = false;
|
|
255
|
+
for (const [id, patch] of this.overlay) {
|
|
256
|
+
let previous = this.readOverlay.get(id);
|
|
257
|
+
if (!previous) {
|
|
258
|
+
changed = true;
|
|
259
|
+
previous = { keys: [], values: [] };
|
|
260
|
+
this.readOverlay.set(id, previous);
|
|
261
|
+
}
|
|
262
|
+
let at = 0;
|
|
263
|
+
for (const key in patch) {
|
|
264
|
+
if (!Object.hasOwn(patch, key))
|
|
265
|
+
continue;
|
|
266
|
+
const value = patch[key];
|
|
267
|
+
if (previous.keys[at] !== key || previous.values[at] !== value || (value !== null && typeof value === "object"))
|
|
268
|
+
changed = true;
|
|
269
|
+
previous.keys[at] = key;
|
|
270
|
+
previous.values[at++] = value;
|
|
271
|
+
if (routed.has(key))
|
|
272
|
+
continue;
|
|
273
|
+
paths.add(`key:${key}`);
|
|
274
|
+
const d = this.registry.get(key);
|
|
275
|
+
if (d?.clearsText)
|
|
276
|
+
paths.add("text");
|
|
277
|
+
if (d?.previewPatch) {
|
|
278
|
+
if (!this.readDescriptors.get(d)?.deps)
|
|
279
|
+
dynamicPreview = true;
|
|
280
|
+
else {
|
|
281
|
+
const o = this.doc.get(id);
|
|
282
|
+
if (!o)
|
|
283
|
+
continue;
|
|
284
|
+
for (const path of Object.keys(d.previewPatch(value, o)))
|
|
285
|
+
paths.add(path);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
else if (!d?.write || d.path)
|
|
289
|
+
paths.add(d?.path ?? key);
|
|
290
|
+
routed.add(key);
|
|
291
|
+
}
|
|
292
|
+
if (at !== previous.keys.length)
|
|
293
|
+
changed = true;
|
|
294
|
+
previous.keys.length = previous.values.length = at;
|
|
295
|
+
}
|
|
296
|
+
if (this.readOverlay.size !== this.overlay.size)
|
|
297
|
+
for (const id of this.readOverlay.keys())
|
|
298
|
+
if (!this.overlay.has(id))
|
|
299
|
+
this.readOverlay.delete(id);
|
|
300
|
+
if (this.overlay.size && this.textTarget_)
|
|
301
|
+
paths.add("style.textCase");
|
|
302
|
+
if (dynamicPreview)
|
|
303
|
+
this.readCache.clear();
|
|
304
|
+
if (changed) {
|
|
305
|
+
const dirty = [...new Set([...this.readPaths, ...paths])];
|
|
306
|
+
let plan = this.readInvalidation;
|
|
307
|
+
if (!plan || dirty.length !== plan.paths.length || dirty.some((path, i) => path !== plan.paths[i])) {
|
|
308
|
+
const keys = [];
|
|
309
|
+
for (const state of this.readRegistry) {
|
|
310
|
+
if (state.deps && (dirty.includes(`key:${state.key}`) || state.deps.some((path) => dirty.some((written) => overlapsPath(path, written)))))
|
|
311
|
+
keys.push(state.key);
|
|
312
|
+
}
|
|
313
|
+
this.readInvalidation = plan = { paths: dirty, keys };
|
|
314
|
+
}
|
|
315
|
+
for (const key of plan.keys)
|
|
316
|
+
this.readCache.delete(key);
|
|
317
|
+
this.readAbsolute = this.absolutePreview;
|
|
318
|
+
}
|
|
319
|
+
this.readPaths = paths;
|
|
320
|
+
const descs = keys ? keys.map((k) => this.registry.get(k)).filter((d) => !!d) : all;
|
|
321
|
+
const text = this.activeTextTarget();
|
|
322
|
+
const missing = descs.filter((d) => dynamicPreview || !this.readDescriptors.get(d)?.deps || (text && d.textMark) || !this.readCache.has(d.key));
|
|
323
|
+
const fresh = missing.length ? this.readFull(missing.map((d) => d.key), missing, targets) : {};
|
|
324
|
+
for (const d of missing) {
|
|
325
|
+
if (!dynamicPreview && this.readDescriptors.get(d)?.deps && !(text && d.textMark)) {
|
|
326
|
+
const pv = fresh[d.key];
|
|
327
|
+
this.readCache.set(d.key, pv);
|
|
328
|
+
if (pv)
|
|
329
|
+
fresh[d.key] = copyReadValue(pv);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (missing.length === descs.length)
|
|
333
|
+
return fresh;
|
|
334
|
+
const out = {};
|
|
335
|
+
for (const d of descs) {
|
|
336
|
+
if (Object.hasOwn(fresh, d.key))
|
|
337
|
+
out[d.key] = fresh[d.key];
|
|
338
|
+
else {
|
|
339
|
+
const pv = this.readCache.get(d.key);
|
|
340
|
+
if (pv)
|
|
341
|
+
out[d.key] = copyReadValue(pv);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return out;
|
|
345
|
+
}
|
|
346
|
+
readFull(keys, descriptors, preparedTargets) {
|
|
347
|
+
const targets = preparedTargets ?? this.targets();
|
|
348
|
+
const effective = this.overlay.size === 0 ? null : new Map(preparedTargets ? [] : targets.map((o) => [o.id, this.effective(o)]));
|
|
349
|
+
const view = (o) => {
|
|
350
|
+
if (!effective)
|
|
351
|
+
return o;
|
|
352
|
+
let value = effective.get(o.id);
|
|
353
|
+
if (!value)
|
|
354
|
+
effective.set(o.id, value = this.effective(o));
|
|
355
|
+
return value;
|
|
356
|
+
};
|
|
357
|
+
const boundsByApplicability = new Map();
|
|
358
|
+
const descs = descriptors ?? (keys ? keys.map((k) => this.registry.get(k)).filter((d) => !!d) : this.registry.all());
|
|
359
|
+
const out = {};
|
|
360
|
+
const text = this.activeTextTarget();
|
|
361
|
+
for (const d of descs) {
|
|
362
|
+
const applicableTargets = targets.filter((o) => d.appliesTo(o));
|
|
363
|
+
if (applicableTargets.length === 0)
|
|
364
|
+
continue;
|
|
365
|
+
if (d.multi === "bounds" && applicableTargets.length > 1 && (d.path === "x" || d.path === "y" || d.path === "w" || d.path === "h")) {
|
|
366
|
+
let bounds = boundsByApplicability.get(d.appliesTo);
|
|
367
|
+
if (bounds === undefined) {
|
|
368
|
+
bounds = transformBounds(effective ? applicableTargets.map(view) : applicableTargets);
|
|
369
|
+
boundsByApplicability.set(d.appliesTo, bounds);
|
|
370
|
+
}
|
|
371
|
+
if (bounds) {
|
|
372
|
+
out[d.key] = { value: bounds[d.path], applicable: applicableTargets.length, descriptor: d };
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
const only = applicableTargets[0];
|
|
377
|
+
const overlay = this.overlay.get(only.id);
|
|
378
|
+
if (text && d.textMark && applicableTargets.length === 1 && !(overlay && (d.key in overlay || (d.path !== undefined && d.path in overlay)))) {
|
|
379
|
+
const pending = !text.previewMarks && this.textPreview?.target === text ? this.textPreview.patch : undefined;
|
|
380
|
+
if (pending && d.key in pending) {
|
|
381
|
+
out[d.key] = { value: pending[d.key], applicable: 1, descriptor: d };
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
const r = text.readMark(d.textMark);
|
|
385
|
+
const pv = { applicable: 1, descriptor: d };
|
|
386
|
+
const meaning = (v) => (d.markRead && v !== undefined ? d.markRead(v, only) : v);
|
|
387
|
+
const values = r.mixed ? [...new Set((r.values ?? [r.value]).map(meaning))] : [meaning(r.value)];
|
|
388
|
+
if (values.length > 1 || (r.mixed && !r.values)) {
|
|
389
|
+
pv.mixed = true;
|
|
390
|
+
pv.values = values;
|
|
391
|
+
}
|
|
392
|
+
else
|
|
393
|
+
pv.value = values[0] === undefined ? d.read(only) : values[0];
|
|
394
|
+
out[d.key] = pv;
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
const values = applicableTargets.map((o) => {
|
|
398
|
+
const ov = this.overlay.get(o.id);
|
|
399
|
+
if (ov && ((d.write && !d.path && !d.previewPatch) || (text && d.textMark))) {
|
|
400
|
+
if (d.key in ov)
|
|
401
|
+
return ov[d.key];
|
|
402
|
+
if (d.path && d.path in ov)
|
|
403
|
+
return ov[d.path];
|
|
404
|
+
}
|
|
405
|
+
return d.read(view(o));
|
|
406
|
+
});
|
|
407
|
+
const first = values[0];
|
|
408
|
+
const mixed = values.some((v) => !deepEq(v, first));
|
|
409
|
+
const pv = { applicable: applicableTargets.length, descriptor: d };
|
|
410
|
+
if (mixed) {
|
|
411
|
+
pv.mixed = true;
|
|
412
|
+
pv.values = values;
|
|
413
|
+
}
|
|
414
|
+
else
|
|
415
|
+
pv.value = first;
|
|
416
|
+
out[d.key] = pv;
|
|
417
|
+
}
|
|
418
|
+
return out;
|
|
419
|
+
}
|
|
420
|
+
pathOf(key) {
|
|
421
|
+
const d = this.registry.get(key);
|
|
422
|
+
return d ? d.path : key;
|
|
423
|
+
}
|
|
424
|
+
underMovedContainer(o, movedContainers) {
|
|
425
|
+
for (let p = o.parent; p; p = this.doc.get(p)?.parent ?? null)
|
|
426
|
+
if (movedContainers.has(p))
|
|
427
|
+
return true;
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
applyPatches(tx, patches) {
|
|
431
|
+
const movedContainers = new Set();
|
|
432
|
+
for (const [o, patch] of patches) {
|
|
433
|
+
if (!CONTAINER_KINDS.has(o.kind))
|
|
434
|
+
continue;
|
|
435
|
+
for (const key of Object.keys(patch)) {
|
|
436
|
+
const d = this.registry.get(key);
|
|
437
|
+
if (isMoveKey(this.pathOf(key)) && (!d || d.appliesTo(o)))
|
|
438
|
+
movedContainers.add(o.id);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
for (const [o, patch] of patches) {
|
|
442
|
+
const follows = movedContainers.size > 0 && this.underMovedContainer(o, movedContainers);
|
|
443
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
444
|
+
if (follows && isMoveKey(this.pathOf(key)))
|
|
445
|
+
continue;
|
|
446
|
+
const d = this.registry.get(key);
|
|
447
|
+
if (d) {
|
|
448
|
+
if (!d.appliesTo(o))
|
|
449
|
+
continue;
|
|
450
|
+
if (d.write)
|
|
451
|
+
d.write(tx, o, value);
|
|
452
|
+
else if (d.path)
|
|
453
|
+
tx.set(o.id, d.path, value);
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
tx.set(o.id, key, value);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
resolveBoundsPatch(targets, patch) {
|
|
462
|
+
const boundsKeys = [];
|
|
463
|
+
for (const key of Object.keys(patch)) {
|
|
464
|
+
const d = this.registry.get(key);
|
|
465
|
+
if (d?.multi === "bounds" && d.path && (d.path === "x" || d.path === "y" || d.path === "w" || d.path === "h"))
|
|
466
|
+
boundsKeys.push(key);
|
|
467
|
+
}
|
|
468
|
+
if (boundsKeys.length === 0)
|
|
469
|
+
return null;
|
|
470
|
+
const applicable = targets.filter((o) => boundsKeys.every((k) => this.registry.get(k).appliesTo(o)));
|
|
471
|
+
if (applicable.length < 2)
|
|
472
|
+
return null;
|
|
473
|
+
const from = transformBounds(applicable);
|
|
474
|
+
if (!from)
|
|
475
|
+
return null;
|
|
476
|
+
const applicableIds = new Set(applicable.map((o) => o.id));
|
|
477
|
+
const to = { ...from };
|
|
478
|
+
const rest = {};
|
|
479
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
480
|
+
const d = this.registry.get(key);
|
|
481
|
+
if (!boundsKeys.includes(key)) {
|
|
482
|
+
rest[key] = value;
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
const n = Number(value);
|
|
486
|
+
if (!Number.isFinite(n))
|
|
487
|
+
continue;
|
|
488
|
+
const axis = d.path;
|
|
489
|
+
to[axis] = axis === "w" || axis === "h" ? Math.max(MIN_SIZE, n) : n;
|
|
490
|
+
}
|
|
491
|
+
const out = new Map();
|
|
492
|
+
for (const o of targets) {
|
|
493
|
+
if (!applicable.includes(o) || this.hasAncestorIn(o, applicableIds)) {
|
|
494
|
+
out.set(o.id, rest);
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
const box = mapBoxInto({ x: o.x, y: o.y, w: o.w, h: o.h }, from, to, o.rot);
|
|
498
|
+
out.set(o.id, { ...rest, x: box.x, y: box.y, w: box.w, h: box.h });
|
|
499
|
+
}
|
|
500
|
+
return out;
|
|
501
|
+
}
|
|
502
|
+
sanitizeGeometry(patch) {
|
|
503
|
+
let out = null;
|
|
504
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
505
|
+
const path = this.pathOf(key);
|
|
506
|
+
if (path !== "x" && path !== "y" && path !== "w" && path !== "h" && path !== "rot")
|
|
507
|
+
continue;
|
|
508
|
+
const n = typeof value === "number" ? value : Number(value);
|
|
509
|
+
out ??= { ...patch };
|
|
510
|
+
if (!Number.isFinite(n))
|
|
511
|
+
delete out[key];
|
|
512
|
+
else
|
|
513
|
+
out[key] = path === "w" || path === "h" ? Math.max(MIN_SIZE, n) : n;
|
|
514
|
+
}
|
|
515
|
+
return out ?? patch;
|
|
516
|
+
}
|
|
517
|
+
patchesFor(targets, patch) {
|
|
518
|
+
const bounds = typeof patch === "function" ? null : this.resolveBoundsPatch(targets, patch);
|
|
519
|
+
return targets.map((o) => {
|
|
520
|
+
const raw = this.sanitizeGeometry(bounds ? bounds.get(o.id) : typeof patch === "function" ? patch(o) : patch);
|
|
521
|
+
const resolved = {};
|
|
522
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
523
|
+
const d = this.registry.get(key);
|
|
524
|
+
if ((key === "rot" || d?.rotationUnit) && this.locked(o))
|
|
525
|
+
continue;
|
|
526
|
+
if (!d?.rotationUnit || !d.appliesTo(o))
|
|
527
|
+
resolved[key] = value;
|
|
528
|
+
else {
|
|
529
|
+
const angle = Number(value) * (d.rotationUnit === "degrees" ? Math.PI / 180 : 1);
|
|
530
|
+
if (Number.isFinite(angle))
|
|
531
|
+
resolved.rot = angle;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return [o, resolved];
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
rotationTargets() {
|
|
538
|
+
const descriptors = this.registry.all().filter((d) => d.rotationUnit);
|
|
539
|
+
return this.targets().filter((o) => isTransformable(o) && o.id !== this.textTarget_?.objectId && o.id !== this.rotationExcludedId && !this.locked(o) && descriptors.some((d) => d.appliesTo(o)));
|
|
540
|
+
}
|
|
541
|
+
captureRotation(roots) {
|
|
542
|
+
return captureRotationIds(roots, (id) => this.doc.get(id), () => this.doc.order());
|
|
543
|
+
}
|
|
544
|
+
sanitizeRotation(patch) {
|
|
545
|
+
const out = { ...this.sanitizeGeometry(patch) };
|
|
546
|
+
for (const key of ["x", "y", "rot"]) {
|
|
547
|
+
const value = out[key];
|
|
548
|
+
if (value !== undefined && (typeof value !== "number" || !Number.isFinite(value)))
|
|
549
|
+
delete out[key];
|
|
550
|
+
}
|
|
551
|
+
for (const key of ["conn.startAt", "conn.endAt", "conn.points"]) {
|
|
552
|
+
const value = out[key];
|
|
553
|
+
if (value === undefined)
|
|
554
|
+
continue;
|
|
555
|
+
const points = key === "conn.points" ? value : [value];
|
|
556
|
+
if (!Array.isArray(points) || !points.every((p) => Array.isArray(p) && p.length === 2 && p.every((n) => typeof n === "number" && Number.isFinite(n))))
|
|
557
|
+
delete out[key];
|
|
558
|
+
}
|
|
559
|
+
return out;
|
|
560
|
+
}
|
|
561
|
+
expandedRotation(patches, preview, centre) {
|
|
562
|
+
const roots = patches.filter(([o, p]) => typeof p.rot === "number" && Number.isFinite(p.rot) && !this.locked(o));
|
|
563
|
+
if (!roots.length)
|
|
564
|
+
return null;
|
|
565
|
+
const ids = new Set(roots.map(([o]) => o.id));
|
|
566
|
+
const top = roots.filter(([o]) => !this.hasAncestorIn(o, ids));
|
|
567
|
+
const captured = preview ? (this.rotationIds ??= this.captureRotation(roots.map(([o]) => o))) : this.captureRotation(roots.map(([o]) => o));
|
|
568
|
+
const objects = captured.map((id) => this.doc.get(id)).filter((o) => !!o);
|
|
569
|
+
const out = new Map(patches.map(([o, p]) => [o.id, [o, this.applicablePatch(o, p)]]));
|
|
570
|
+
for (const [root, patch] of top) {
|
|
571
|
+
const rootIds = new Set([root.id]);
|
|
572
|
+
const subtree = objects.filter((o) => o.id === root.id || this.hasAncestorIn(o, rootIds));
|
|
573
|
+
const translation = { x: typeof patch.x === "number" ? patch.x - root.x : 0, y: typeof patch.y === "number" ? patch.y - root.y : 0 };
|
|
574
|
+
const pivot = centre ?? { x: root.x + root.w / 2, y: root.y + root.h / 2 };
|
|
575
|
+
const turned = rotationPatches(subtree, pivot, patch.rot - root.rot, (id) => this.doc.get(id), translation);
|
|
576
|
+
for (const o of subtree) {
|
|
577
|
+
const geometry = turned.get(o.id);
|
|
578
|
+
if (geometry)
|
|
579
|
+
out.set(o.id, [o, { ...out.get(o.id)?.[1], ...this.sanitizeRotation(geometry) }]);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return [...out.values()];
|
|
583
|
+
}
|
|
584
|
+
locked(o) {
|
|
585
|
+
for (let cur = o; cur; cur = cur.parent ? this.doc.get(cur.parent) : undefined)
|
|
586
|
+
if (cur.locked)
|
|
587
|
+
return true;
|
|
588
|
+
return false;
|
|
589
|
+
}
|
|
590
|
+
applyAbsolutePatches(tx, patches) {
|
|
591
|
+
const depth = (o) => {
|
|
592
|
+
let n = 0;
|
|
593
|
+
for (let p = o.parent; p; p = this.doc.get(p)?.parent ?? null)
|
|
594
|
+
n++;
|
|
595
|
+
return n;
|
|
596
|
+
};
|
|
597
|
+
patches.sort(([a], [b]) => depth(a) - depth(b));
|
|
598
|
+
for (const [o, patch] of patches) {
|
|
599
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
600
|
+
if (key === "x" || key === "y" || key === "rot" || key === "conn.startAt" || key === "conn.endAt" || key === "conn.points")
|
|
601
|
+
tx.set(o.id, key, value);
|
|
602
|
+
else
|
|
603
|
+
this.applyPatches(tx, [[o, { [key]: value }]]);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
set(patch, opts = {}) {
|
|
608
|
+
const targets = this.targets();
|
|
609
|
+
if (targets.length === 0)
|
|
610
|
+
return;
|
|
611
|
+
const text = this.activeTextTarget();
|
|
612
|
+
if (text) {
|
|
613
|
+
const p = typeof patch === "function" ? patch(targets[0]) : patch;
|
|
614
|
+
const rest = {};
|
|
615
|
+
for (const [key, value] of Object.entries(p)) {
|
|
616
|
+
const d = this.registry.get(key);
|
|
617
|
+
if (d?.textMark)
|
|
618
|
+
text.writeMark(d.textMark, d.markWrite ? d.markWrite(value, targets[0]) : value);
|
|
619
|
+
else
|
|
620
|
+
rest[key] = value;
|
|
621
|
+
}
|
|
622
|
+
if (Object.keys(rest).length === 0)
|
|
623
|
+
return;
|
|
624
|
+
patch = rest;
|
|
625
|
+
}
|
|
626
|
+
const patches = this.patchesFor(targets, patch);
|
|
627
|
+
if (patches.every(([, p]) => Object.keys(p).length === 0))
|
|
628
|
+
return;
|
|
629
|
+
const label = opts.label ?? `Set ${Object.keys(typeof patch === "function" ? patches[0][1] : patch).join(", ")}`;
|
|
630
|
+
const rotation = this.expandedRotation(patches, false);
|
|
631
|
+
this.doc.transact(label, (tx) => rotation ? this.applyAbsolutePatches(tx, rotation) : this.applyPatches(tx, patches));
|
|
632
|
+
}
|
|
633
|
+
create(spec, opts = {}) {
|
|
634
|
+
const { style, ...rest } = spec;
|
|
635
|
+
const obj = { id: provisionalId(spec.kind), version: 0, parent: opts.parent ?? null, rot: 0, ...rest, style: { ...defaultStyle(spec.kind), ...(style ?? {}) } };
|
|
636
|
+
this.doc.transact(opts.label ?? `Create ${spec.kind}`, (tx) => {
|
|
637
|
+
tx.create(obj);
|
|
638
|
+
for (const id of opts.adopt ?? [])
|
|
639
|
+
tx.move(id, obj.id);
|
|
640
|
+
});
|
|
641
|
+
if (opts.select !== false)
|
|
642
|
+
this.selection.set([obj.id]);
|
|
643
|
+
return obj.id;
|
|
644
|
+
}
|
|
645
|
+
insertImages(images, opts) {
|
|
646
|
+
if (!images.length || ![opts.at.x, opts.at.y, opts.visible.w, opts.visible.h].every(Number.isFinite)
|
|
647
|
+
|| opts.visible.w <= 0 || opts.visible.h <= 0)
|
|
648
|
+
return [];
|
|
649
|
+
if (opts.parent && !CONTAINER_KINDS.has(this.doc.get(opts.parent)?.kind ?? ""))
|
|
650
|
+
return [];
|
|
651
|
+
const accepted = images.filter(image => /^[0-9a-f]{64}$/.test(image.hash)
|
|
652
|
+
&& Number.isFinite(image.w) && image.w > 0 && Number.isFinite(image.h) && image.h > 0);
|
|
653
|
+
if (!accepted.length)
|
|
654
|
+
return [];
|
|
655
|
+
const objects = accepted.map(image => {
|
|
656
|
+
const scale = Math.min(1, opts.visible.w / image.w, opts.visible.h / image.h);
|
|
657
|
+
const w = image.w * scale, h = image.h * scale;
|
|
658
|
+
return { id: provisionalId("image"), kind: "image", parent: opts.parent ?? null, version: 0,
|
|
659
|
+
x: opts.at.x - w / 2, y: opts.at.y - h / 2, w, h, rot: 0, style: defaultStyle("image"),
|
|
660
|
+
image: { hash: image.hash, mime: image.mime, w: image.w, h: image.h,
|
|
661
|
+
...(image.thumb === undefined ? {} : { thumb: image.thumb }),
|
|
662
|
+
...(image.crop === undefined ? {} : { crop: [...image.crop] }) } };
|
|
663
|
+
});
|
|
664
|
+
this.doc.transact(opts.label ?? "Insert images", tx => { for (const object of objects)
|
|
665
|
+
tx.create(object); });
|
|
666
|
+
const ids = objects.map(object => object.id);
|
|
667
|
+
if (opts.select !== false)
|
|
668
|
+
this.selection.set(ids);
|
|
669
|
+
return ids;
|
|
670
|
+
}
|
|
671
|
+
preview(patch, label = "preview") {
|
|
672
|
+
if (this.rotationExcludedId !== null || (this.rotationRequest && "delta" in this.rotationRequest))
|
|
673
|
+
this.rotationIds = null;
|
|
674
|
+
this.rotationExcludedId = null;
|
|
675
|
+
this.previewLabel = label;
|
|
676
|
+
const targets = this.targets();
|
|
677
|
+
const patches = this.patchesFor(targets, this.previewText(targets, patch));
|
|
678
|
+
const rotation = this.expandedRotation(patches, true);
|
|
679
|
+
if (rotation) {
|
|
680
|
+
this.rotationRequest = { patches: new Map(patches.map(([o, p]) => [o.id, p])) };
|
|
681
|
+
this.absolutePreview = true;
|
|
682
|
+
this.overlay = new Map(rotation.map(([o, p]) => [o.id, p]));
|
|
683
|
+
this.emit();
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
if (this.absolutePreview)
|
|
687
|
+
this.overlay = new Map();
|
|
688
|
+
this.absolutePreview = false;
|
|
689
|
+
this.rotationIds = null;
|
|
690
|
+
this.rotationExcludedId = null;
|
|
691
|
+
this.rotationRequest = null;
|
|
692
|
+
for (const [o, raw] of patches) {
|
|
693
|
+
const p = this.applicablePatch(o, raw);
|
|
694
|
+
if (Object.keys(p).length)
|
|
695
|
+
this.overlay.set(o.id, p);
|
|
696
|
+
else
|
|
697
|
+
this.overlay.delete(o.id);
|
|
698
|
+
}
|
|
699
|
+
this.emit();
|
|
700
|
+
}
|
|
701
|
+
previewText(targets, patch) {
|
|
702
|
+
const text = this.activeTextTarget();
|
|
703
|
+
const o = targets[0];
|
|
704
|
+
if (!text || !o) {
|
|
705
|
+
this.dropTextPreview();
|
|
706
|
+
return patch;
|
|
707
|
+
}
|
|
708
|
+
const p = typeof patch === "function" ? patch(o) : patch;
|
|
709
|
+
const keys = {};
|
|
710
|
+
const marks = {};
|
|
711
|
+
const rest = {};
|
|
712
|
+
for (const [key, value] of Object.entries(p)) {
|
|
713
|
+
const d = this.registry.get(key);
|
|
714
|
+
if (!d?.textMark)
|
|
715
|
+
rest[key] = value;
|
|
716
|
+
else {
|
|
717
|
+
keys[key] = value;
|
|
718
|
+
marks[d.textMark] = d.markWrite ? d.markWrite(value, o) : value;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
if (Object.keys(keys).length === 0)
|
|
722
|
+
this.dropTextPreview();
|
|
723
|
+
else {
|
|
724
|
+
this.textPreview = { target: text, patch: keys, marks };
|
|
725
|
+
text.previewMarks?.(marks);
|
|
726
|
+
}
|
|
727
|
+
return rest;
|
|
728
|
+
}
|
|
729
|
+
dropTextPreview() {
|
|
730
|
+
const t = this.textPreview;
|
|
731
|
+
if (!t)
|
|
732
|
+
return false;
|
|
733
|
+
this.textPreview = null;
|
|
734
|
+
t.target.previewMarks?.(null);
|
|
735
|
+
return true;
|
|
736
|
+
}
|
|
737
|
+
commitText(t, label, also) {
|
|
738
|
+
if (t.target.commitMarks) {
|
|
739
|
+
let keys = { also: undefined, text: undefined };
|
|
740
|
+
try {
|
|
741
|
+
if (also)
|
|
742
|
+
keys = this.recordWrites(also, t.target.objectId);
|
|
743
|
+
}
|
|
744
|
+
catch (err) {
|
|
745
|
+
t.target.previewMarks?.(null);
|
|
746
|
+
throw err;
|
|
747
|
+
}
|
|
748
|
+
t.target.commitMarks(t.marks, label, keys.also, keys.text);
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
t.target.previewMarks?.(null);
|
|
752
|
+
for (const [name, value] of Object.entries(t.marks))
|
|
753
|
+
t.target.writeMark(name, value);
|
|
754
|
+
if (also)
|
|
755
|
+
this.doc.transact(label, also);
|
|
756
|
+
}
|
|
757
|
+
beginRotation(excludedId) {
|
|
758
|
+
this.rotationExcludedId = excludedId ?? null;
|
|
759
|
+
this.rotationIds = this.captureRotation(this.rotationTargets());
|
|
760
|
+
this.rotationRequest = null;
|
|
761
|
+
}
|
|
762
|
+
previewRotation(delta, centre, label = "Rotate") {
|
|
763
|
+
if (typeof delta !== "number" || !Number.isFinite(delta) || (centre && (!Number.isFinite(centre.x) || !Number.isFinite(centre.y)))) {
|
|
764
|
+
this.cancelPreview();
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
this.previewLabel = label;
|
|
768
|
+
const patches = this.rotationTargets().map((o) => [o, { rot: o.rot + delta }]);
|
|
769
|
+
const rotation = this.expandedRotation(patches, true, centre);
|
|
770
|
+
this.absolutePreview = true;
|
|
771
|
+
this.rotationRequest = { delta, ...(centre ? { centre: { ...centre } } : {}) };
|
|
772
|
+
this.overlay = new Map((rotation ?? []).map(([o, p]) => [o.id, p]));
|
|
773
|
+
this.emit();
|
|
774
|
+
}
|
|
775
|
+
refreshRotationAtCommit() {
|
|
776
|
+
const request = this.rotationRequest;
|
|
777
|
+
if (!request)
|
|
778
|
+
return;
|
|
779
|
+
const patches = "delta" in request
|
|
780
|
+
? this.rotationTargets().map((o) => [o, { rot: o.rot + request.delta }])
|
|
781
|
+
: this.patchesFor(this.targets(), (o) => request.patches.get(o.id) ?? {});
|
|
782
|
+
const rotation = this.expandedRotation(patches, false, "delta" in request ? request.centre : undefined);
|
|
783
|
+
this.overlay = new Map((rotation ?? patches).filter(([, p]) => Object.keys(p).length).map(([o, p]) => [o.id, p]));
|
|
784
|
+
}
|
|
785
|
+
previewOverlay() {
|
|
786
|
+
return this.overlay;
|
|
787
|
+
}
|
|
788
|
+
hasPreview() {
|
|
789
|
+
return this.overlay.size > 0 || this.textPreview !== null;
|
|
790
|
+
}
|
|
791
|
+
commitPreview(opts = {}) {
|
|
792
|
+
this.refreshRotationAtCommit();
|
|
793
|
+
const text = this.textPreview;
|
|
794
|
+
this.textPreview = null;
|
|
795
|
+
if (this.overlay.size === 0) {
|
|
796
|
+
this.absolutePreview = false;
|
|
797
|
+
this.rotationIds = null;
|
|
798
|
+
this.rotationExcludedId = null;
|
|
799
|
+
this.rotationRequest = null;
|
|
800
|
+
if (text) {
|
|
801
|
+
this.commitText(text, this.previewLabel);
|
|
802
|
+
this.emit();
|
|
803
|
+
}
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
const absolute = this.absolutePreview;
|
|
807
|
+
const patches = new Map(this.overlay);
|
|
808
|
+
this.overlay = new Map();
|
|
809
|
+
this.absolutePreview = false;
|
|
810
|
+
this.rotationIds = null;
|
|
811
|
+
this.rotationExcludedId = null;
|
|
812
|
+
this.rotationRequest = null;
|
|
813
|
+
const targets = absolute ? [...patches.keys()].map((id) => this.doc.get(id)).filter((o) => !!o) : this.targets().filter((o) => patches.has(o.id));
|
|
814
|
+
const label = this.previewLabel;
|
|
815
|
+
const container = opts.dropTarget;
|
|
816
|
+
const reparent = container === undefined ? [] : this.reparentPlan(targets, container);
|
|
817
|
+
const write = (tx) => {
|
|
818
|
+
const writes = targets.map((o) => [o, patches.get(o.id)]);
|
|
819
|
+
if (absolute)
|
|
820
|
+
this.applyAbsolutePatches(tx, writes);
|
|
821
|
+
else
|
|
822
|
+
this.applyPatches(tx, writes);
|
|
823
|
+
for (const id of reparent)
|
|
824
|
+
tx.move(id, container);
|
|
825
|
+
};
|
|
826
|
+
if (text)
|
|
827
|
+
this.commitText(text, label, write);
|
|
828
|
+
else
|
|
829
|
+
this.doc.transact(label, write);
|
|
830
|
+
this.emit();
|
|
831
|
+
}
|
|
832
|
+
recordWrites(write, id) {
|
|
833
|
+
const calls = [];
|
|
834
|
+
let rewrite;
|
|
835
|
+
const deltas = [];
|
|
836
|
+
let created = false;
|
|
837
|
+
const refuseCreate = () => new Error("PropertyController.commitPreview: a property writer created an object in a text session's preview commit with text marks, which records the object keys' writes before its one transaction and cannot give a created object the id the document assigns. Nothing was written; commit the text marks and the creating key in separate gestures (#170 S2)");
|
|
838
|
+
write({
|
|
839
|
+
set: (...call) => {
|
|
840
|
+
if (call[0] === id && call[1] === "text")
|
|
841
|
+
rewrite = { text: (call[2] ?? { paragraphs: [] }) };
|
|
842
|
+
else
|
|
843
|
+
calls.push((tx) => tx.set(...call));
|
|
844
|
+
},
|
|
845
|
+
move: (...call) => void calls.push((tx) => tx.move(...call)),
|
|
846
|
+
create: () => {
|
|
847
|
+
created = true;
|
|
848
|
+
throw refuseCreate();
|
|
849
|
+
},
|
|
850
|
+
delete: (...call) => void calls.push((tx) => tx.delete(...call)),
|
|
851
|
+
editText: (...call) => {
|
|
852
|
+
if (call[0] === id)
|
|
853
|
+
deltas.push(call[1]);
|
|
854
|
+
else
|
|
855
|
+
calls.push((tx) => tx.editText(...call));
|
|
856
|
+
},
|
|
857
|
+
});
|
|
858
|
+
if (created)
|
|
859
|
+
throw refuseCreate();
|
|
860
|
+
const also = calls.length ? (tx) => { for (const call of calls)
|
|
861
|
+
call(tx); } : undefined;
|
|
862
|
+
if (!rewrite && deltas.length === 0)
|
|
863
|
+
return { also, text: undefined };
|
|
864
|
+
let text = rewrite?.text ?? this.doc.get(id)?.text ?? emptyTextContent();
|
|
865
|
+
for (const delta of deltas)
|
|
866
|
+
text = applyTextDelta(text, delta);
|
|
867
|
+
return { also, text };
|
|
868
|
+
}
|
|
869
|
+
reparentPlan(targets, container) {
|
|
870
|
+
const ids = new Set(targets.map((o) => o.id));
|
|
871
|
+
const out = [];
|
|
872
|
+
for (const o of targets) {
|
|
873
|
+
if (o.kind === "connector" || (o.parent ?? null) === container)
|
|
874
|
+
continue;
|
|
875
|
+
if (this.hasAncestorIn(o, ids))
|
|
876
|
+
continue;
|
|
877
|
+
if (container !== null && (container === o.id || this.hasAncestorIn(this.doc.get(container), new Set([o.id]))))
|
|
878
|
+
continue;
|
|
879
|
+
out.push(o.id);
|
|
880
|
+
}
|
|
881
|
+
if (out.length > 1) {
|
|
882
|
+
const rank = new Map(this.doc.order().map((id, i) => [id, i]));
|
|
883
|
+
out.sort((a, b) => (rank.get(a) ?? 0) - (rank.get(b) ?? 0));
|
|
884
|
+
}
|
|
885
|
+
return out;
|
|
886
|
+
}
|
|
887
|
+
hasAncestorIn(o, ids) {
|
|
888
|
+
for (let p = o?.parent ?? null; p; p = this.doc.get(p)?.parent ?? null)
|
|
889
|
+
if (ids.has(p))
|
|
890
|
+
return true;
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
893
|
+
cancelPreview() {
|
|
894
|
+
this.absolutePreview = false;
|
|
895
|
+
this.rotationIds = null;
|
|
896
|
+
this.rotationExcludedId = null;
|
|
897
|
+
this.rotationRequest = null;
|
|
898
|
+
const text = this.dropTextPreview();
|
|
899
|
+
if (this.overlay.size === 0) {
|
|
900
|
+
if (text)
|
|
901
|
+
this.emit();
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
this.overlay = new Map();
|
|
905
|
+
this.emit();
|
|
906
|
+
}
|
|
907
|
+
subtreeDelta(o) {
|
|
908
|
+
if (!CONTAINER_KINDS.has(o.kind))
|
|
909
|
+
return null;
|
|
910
|
+
const ov = this.overlay.get(o.id);
|
|
911
|
+
if (!ov)
|
|
912
|
+
return null;
|
|
913
|
+
let dx = 0;
|
|
914
|
+
let dy = 0;
|
|
915
|
+
let moved = false;
|
|
916
|
+
for (const [key, value] of Object.entries(ov)) {
|
|
917
|
+
const path = this.pathOf(key);
|
|
918
|
+
if (path === "x")
|
|
919
|
+
(dx = value - o.x), (moved = true);
|
|
920
|
+
else if (path === "y")
|
|
921
|
+
(dy = value - o.y), (moved = true);
|
|
922
|
+
}
|
|
923
|
+
return moved ? { dx, dy } : null;
|
|
924
|
+
}
|
|
925
|
+
carrierDelta(o) {
|
|
926
|
+
for (let p = o.parent; p;) {
|
|
927
|
+
const anc = this.doc.get(p);
|
|
928
|
+
if (!anc)
|
|
929
|
+
return null;
|
|
930
|
+
const d = this.subtreeDelta(anc);
|
|
931
|
+
if (d)
|
|
932
|
+
return d;
|
|
933
|
+
p = anc.parent;
|
|
934
|
+
}
|
|
935
|
+
return null;
|
|
936
|
+
}
|
|
937
|
+
effective(o) {
|
|
938
|
+
if (this.overlay.size === 0)
|
|
939
|
+
return o;
|
|
940
|
+
const carried = this.absolutePreview && this.overlay.has(o.id) ? null : this.carrierDelta(o);
|
|
941
|
+
const ov = this.overlay.get(o.id);
|
|
942
|
+
if (!ov) {
|
|
943
|
+
if (!carried || (!carried.dx && !carried.dy))
|
|
944
|
+
return o;
|
|
945
|
+
const copy = previewCopy(o);
|
|
946
|
+
copy.x += carried.dx;
|
|
947
|
+
copy.y += carried.dy;
|
|
948
|
+
return copy;
|
|
949
|
+
}
|
|
950
|
+
const copy = previewCopy(o);
|
|
951
|
+
copy.style = { ...o.style };
|
|
952
|
+
const record = copy;
|
|
953
|
+
const put = (target, key, value) => {
|
|
954
|
+
if (target === record && key === "text")
|
|
955
|
+
Object.defineProperty(target, key, { value, enumerable: true, configurable: true, writable: true });
|
|
956
|
+
else
|
|
957
|
+
target[key] = value;
|
|
958
|
+
};
|
|
959
|
+
const apply = (path, value) => {
|
|
960
|
+
if (carried && isMoveKey(path))
|
|
961
|
+
return;
|
|
962
|
+
const parts = path.split(".");
|
|
963
|
+
let cur = record;
|
|
964
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
965
|
+
const p = parts[i];
|
|
966
|
+
put(cur, p, { ...(cur[p] ?? {}) });
|
|
967
|
+
cur = cur[p];
|
|
968
|
+
}
|
|
969
|
+
const key = parts[parts.length - 1];
|
|
970
|
+
if (value === undefined)
|
|
971
|
+
delete cur[key];
|
|
972
|
+
else
|
|
973
|
+
put(cur, key, value);
|
|
974
|
+
};
|
|
975
|
+
for (const [key, value] of Object.entries(ov)) {
|
|
976
|
+
const d = this.registry.get(key);
|
|
977
|
+
if (d?.previewPatch) {
|
|
978
|
+
for (const [path, v] of Object.entries(d.previewPatch(value, o)))
|
|
979
|
+
apply(path, v);
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
if (d?.write && !d.path)
|
|
983
|
+
continue;
|
|
984
|
+
apply(d?.path ?? key, value);
|
|
985
|
+
}
|
|
986
|
+
if (d?.clearsText && copy.text)
|
|
987
|
+
apply("text", withoutTextKey(copy.text, d.clearsText));
|
|
988
|
+
}
|
|
989
|
+
if (carried) {
|
|
990
|
+
copy.x += carried.dx;
|
|
991
|
+
copy.y += carried.dy;
|
|
992
|
+
}
|
|
993
|
+
if (this.textTarget_?.objectId === o.id && copy.style?.textCase !== undefined)
|
|
994
|
+
apply("style.textCase", undefined);
|
|
995
|
+
return copy;
|
|
996
|
+
}
|
|
997
|
+
commandBlocked(o, flag) {
|
|
998
|
+
for (let cur = o; cur; cur = cur.parent ? this.doc.get(cur.parent) : undefined) {
|
|
999
|
+
if (cur[flag])
|
|
1000
|
+
return true;
|
|
1001
|
+
}
|
|
1002
|
+
return false;
|
|
1003
|
+
}
|
|
1004
|
+
selectAllIds() {
|
|
1005
|
+
const objects = this.doc.order().map((id) => this.doc.get(id)).filter((o) => !!o && o.kind !== "group" && !this.commandBlocked(o, "hidden") && !this.commandBlocked(o, "locked"));
|
|
1006
|
+
const ids = new Set(objects.map((o) => o.id));
|
|
1007
|
+
return objects.filter((o) => !this.hasAncestorIn(o, ids)).map((o) => o.id);
|
|
1008
|
+
}
|
|
1009
|
+
copyObjects() {
|
|
1010
|
+
const selected = new Set(this.targetIds());
|
|
1011
|
+
const roots = this.targets().filter((o) => !this.hasAncestorIn(o, selected));
|
|
1012
|
+
if (roots.length === 0)
|
|
1013
|
+
return [];
|
|
1014
|
+
const rootIds = new Set(roots.map((o) => o.id));
|
|
1015
|
+
const objects = this.doc.order().map((id) => this.doc.get(id)).filter((o) => !!o);
|
|
1016
|
+
const copying = new Set(objects.filter((o) => rootIds.has(o.id) || this.hasAncestorIn(o, rootIds)).map((o) => o.id));
|
|
1017
|
+
for (const o of objects) {
|
|
1018
|
+
if (o.kind === "connector" && o.conn?.start.ref && o.conn.end.ref && copying.has(o.conn.start.ref) && copying.has(o.conn.end.ref))
|
|
1019
|
+
copying.add(o.id);
|
|
1020
|
+
}
|
|
1021
|
+
return objects.filter((o) => copying.has(o.id)).map((o) => structuredClone(o));
|
|
1022
|
+
}
|
|
1023
|
+
pasteParent(id) {
|
|
1024
|
+
const candidate = typeof id === "string" ? this.doc.get(id) : undefined;
|
|
1025
|
+
if (!candidate || (candidate.kind !== "frame" && candidate.kind !== "section"))
|
|
1026
|
+
return undefined;
|
|
1027
|
+
let outermostLocked;
|
|
1028
|
+
for (let current = candidate; current; current = current.parent ? this.doc.get(current.parent) : undefined) {
|
|
1029
|
+
if (current.locked)
|
|
1030
|
+
outermostLocked = current;
|
|
1031
|
+
}
|
|
1032
|
+
if (!outermostLocked)
|
|
1033
|
+
return candidate;
|
|
1034
|
+
for (let current = outermostLocked.parent ? this.doc.get(outermostLocked.parent) : undefined; current; current = current.parent ? this.doc.get(current.parent) : undefined) {
|
|
1035
|
+
if (current.kind === "frame" || current.kind === "section")
|
|
1036
|
+
return current;
|
|
1037
|
+
}
|
|
1038
|
+
return undefined;
|
|
1039
|
+
}
|
|
1040
|
+
paste(objects, options = {}) {
|
|
1041
|
+
const sources = validateObjects(objects);
|
|
1042
|
+
if (!sources.length)
|
|
1043
|
+
return [];
|
|
1044
|
+
for (const source of sources)
|
|
1045
|
+
if (source.kind === "connector")
|
|
1046
|
+
source.parent = null;
|
|
1047
|
+
for (const point of [options.at, options.viewport]) {
|
|
1048
|
+
if (point && (!Number.isFinite(point.x) || !Number.isFinite(point.y)))
|
|
1049
|
+
return [];
|
|
1050
|
+
}
|
|
1051
|
+
if (options.viewport && (!Number.isFinite(options.viewport.w) || !Number.isFinite(options.viewport.h)))
|
|
1052
|
+
return [];
|
|
1053
|
+
const bounds = clipboardBounds(sources);
|
|
1054
|
+
if (!bounds || ![bounds.x, bounds.y, bounds.w, bounds.h].every(Number.isFinite))
|
|
1055
|
+
return [];
|
|
1056
|
+
const offset = pasteOffset(sources, options);
|
|
1057
|
+
if (!Number.isFinite(offset.x) || !Number.isFinite(offset.y))
|
|
1058
|
+
return [];
|
|
1059
|
+
for (const o of sources) {
|
|
1060
|
+
if (![o.x + offset.x, o.y + offset.y, o.x + o.w + offset.x, o.y + o.h + offset.y].every(Number.isFinite))
|
|
1061
|
+
return [];
|
|
1062
|
+
if (o.conn && [o.conn.startAt, o.conn.endAt, ...(o.conn.points ?? [])].some(([x, y]) => !Number.isFinite(x + offset.x) || !Number.isFinite(y + offset.y)))
|
|
1063
|
+
return [];
|
|
1064
|
+
}
|
|
1065
|
+
const parents = new Map();
|
|
1066
|
+
const byId = new Map(sources.map((o) => [o.id, o]));
|
|
1067
|
+
for (const o of sources) {
|
|
1068
|
+
const internal = o.parent ? byId.get(o.parent) : undefined;
|
|
1069
|
+
const destination = options.parents && Object.hasOwn(options.parents, o.id) ? options.parents[o.id] : options.parent;
|
|
1070
|
+
const parent = o.kind === "connector" ? undefined : this.pasteParent(destination);
|
|
1071
|
+
if (!internal)
|
|
1072
|
+
parents.set(o.id, parent?.id ?? null);
|
|
1073
|
+
const origin = internal ?? parent;
|
|
1074
|
+
if (origin && ![o.x + (internal ? 0 : offset.x) - origin.x, o.y + (internal ? 0 : offset.y) - origin.y].every(Number.isFinite))
|
|
1075
|
+
return [];
|
|
1076
|
+
}
|
|
1077
|
+
return this.insertCopies(sources, offset, "Paste", parents, options.select);
|
|
1078
|
+
}
|
|
1079
|
+
duplicate() {
|
|
1080
|
+
this.insertCopies(this.copyObjects(), { x: 10, y: 10 }, "Duplicate");
|
|
1081
|
+
}
|
|
1082
|
+
insertCopies(sources, offset, label, destinationParents, select = true) {
|
|
1083
|
+
if (!sources.length)
|
|
1084
|
+
return [];
|
|
1085
|
+
const sourceById = new Map(sources.map((o) => [o.id, o]));
|
|
1086
|
+
const copies = new Map();
|
|
1087
|
+
const committed = () => sources.filter((o) => !o.parent || !sourceById.has(o.parent))
|
|
1088
|
+
.map((o) => copies.get(o.id)?.id).filter((id) => !!id && !!this.doc.get(id));
|
|
1089
|
+
try {
|
|
1090
|
+
if (select)
|
|
1091
|
+
this.cancelPreview();
|
|
1092
|
+
this.doc.transact(label, (tx) => {
|
|
1093
|
+
const createCopy = (source) => {
|
|
1094
|
+
const existing = copies.get(source.id);
|
|
1095
|
+
if (existing)
|
|
1096
|
+
return existing;
|
|
1097
|
+
const parent = source.parent && sourceById.has(source.parent) ? createCopy(sourceById.get(source.parent)).id
|
|
1098
|
+
: destinationParents === undefined ? source.parent : destinationParents.get(source.id) ?? null;
|
|
1099
|
+
const copy = structuredClone(source);
|
|
1100
|
+
copy.id = provisionalId(source.kind);
|
|
1101
|
+
copy.parent = parent;
|
|
1102
|
+
copy.x += offset.x;
|
|
1103
|
+
copy.y += offset.y;
|
|
1104
|
+
copy.version = 0;
|
|
1105
|
+
delete copy.textVersion;
|
|
1106
|
+
delete copy.extId;
|
|
1107
|
+
if (copy.conn) {
|
|
1108
|
+
copy.conn.startAt = [copy.conn.startAt[0] + offset.x, copy.conn.startAt[1] + offset.y];
|
|
1109
|
+
copy.conn.endAt = [copy.conn.endAt[0] + offset.x, copy.conn.endAt[1] + offset.y];
|
|
1110
|
+
if (copy.conn.points)
|
|
1111
|
+
copy.conn.points = copy.conn.points.map(([x, y]) => [x + offset.x, y + offset.y]);
|
|
1112
|
+
}
|
|
1113
|
+
copies.set(source.id, copy);
|
|
1114
|
+
tx.create(copy);
|
|
1115
|
+
return copy;
|
|
1116
|
+
};
|
|
1117
|
+
for (const source of sources)
|
|
1118
|
+
createCopy(source);
|
|
1119
|
+
for (const source of sources) {
|
|
1120
|
+
if (!source.conn)
|
|
1121
|
+
continue;
|
|
1122
|
+
const copy = copies.get(source.id);
|
|
1123
|
+
for (const end of ["start", "end"]) {
|
|
1124
|
+
const endpoint = source.conn[end];
|
|
1125
|
+
const ref = endpoint.ref ? copies.get(endpoint.ref)?.id : undefined;
|
|
1126
|
+
if (ref)
|
|
1127
|
+
tx.set(copy.id, `conn.${end}`, { ...endpoint, ref });
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
});
|
|
1131
|
+
const created = committed();
|
|
1132
|
+
if (select && created.length)
|
|
1133
|
+
this.selection.set(created);
|
|
1134
|
+
return created;
|
|
1135
|
+
}
|
|
1136
|
+
catch (error) {
|
|
1137
|
+
if (label === "Paste")
|
|
1138
|
+
throw new PasteCommitError(error, committed());
|
|
1139
|
+
throw error;
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
layoutTargets() {
|
|
1143
|
+
const objects = this.targets().filter((o) => isTransformable(o) && !this.commandBlocked(o, "hidden"));
|
|
1144
|
+
const ids = new Set(objects.map((o) => o.id));
|
|
1145
|
+
return objects.filter((o) => !this.hasAncestorIn(o, ids));
|
|
1146
|
+
}
|
|
1147
|
+
static retainedDistributionPlans(entries, plans) {
|
|
1148
|
+
const alive = plans.map(() => true);
|
|
1149
|
+
if (!plans.length)
|
|
1150
|
+
return alive;
|
|
1151
|
+
const near = (a, b) => 8 * Number.EPSILON * Math.max(Math.abs(a), Math.abs(b));
|
|
1152
|
+
const overlaps = (a0, a1, b0, b1) => a0 < b1 - near(a0, b1) && b0 < a1 - near(b0, a1);
|
|
1153
|
+
const planOf = new Int32Array(entries.length).fill(-1);
|
|
1154
|
+
const slotOf = new Float64Array(entries.length);
|
|
1155
|
+
const ends = plans.map((p, i) => p.members.map((e, j) => {
|
|
1156
|
+
planOf[e] = i;
|
|
1157
|
+
slotOf[e] = p.slots[j];
|
|
1158
|
+
return p.slots[j] + entries[e].size;
|
|
1159
|
+
}));
|
|
1160
|
+
const moving = (e) => planOf[e] >= 0 && alive[planOf[e]];
|
|
1161
|
+
const original = [];
|
|
1162
|
+
const planned = [];
|
|
1163
|
+
entries.forEach((e, i) => {
|
|
1164
|
+
original.push({ at: e.start, entry: i }, { at: e.end, entry: i });
|
|
1165
|
+
if (planOf[i] >= 0)
|
|
1166
|
+
planned.push({ at: slotOf[i], entry: i }, { at: slotOf[i] + e.size, entry: i });
|
|
1167
|
+
});
|
|
1168
|
+
original.sort((a, b) => a.at - b.at);
|
|
1169
|
+
planned.sort((a, b) => a.at - b.at);
|
|
1170
|
+
const firstAtOrAfter = (points, x) => {
|
|
1171
|
+
let lo = 0, hi = points.length;
|
|
1172
|
+
while (lo < hi) {
|
|
1173
|
+
const mid = (lo + hi) >>> 1;
|
|
1174
|
+
if (points[mid].at < x)
|
|
1175
|
+
lo = mid + 1;
|
|
1176
|
+
else
|
|
1177
|
+
hi = mid;
|
|
1178
|
+
}
|
|
1179
|
+
return lo;
|
|
1180
|
+
};
|
|
1181
|
+
const regions = plans.map((p, i) => {
|
|
1182
|
+
const pad = near(p.start, p.stop) * 2;
|
|
1183
|
+
return { start: p.start - pad, stop: p.stop + pad, i };
|
|
1184
|
+
});
|
|
1185
|
+
const conflicts = (i) => {
|
|
1186
|
+
const { members, slots } = plans[i];
|
|
1187
|
+
const region = regions[i];
|
|
1188
|
+
const seen = new Set();
|
|
1189
|
+
const clashes = (q) => {
|
|
1190
|
+
if (planOf[q] === i || seen.has(q))
|
|
1191
|
+
return false;
|
|
1192
|
+
seen.add(q);
|
|
1193
|
+
const other = entries[q];
|
|
1194
|
+
const q0 = moving(q) ? slotOf[q] : other.start;
|
|
1195
|
+
const q1 = q0 + other.size;
|
|
1196
|
+
let lo = 0, hi = slots.length;
|
|
1197
|
+
while (lo < hi) {
|
|
1198
|
+
const mid = (lo + hi) >>> 1;
|
|
1199
|
+
if (ends[i][mid] <= q0)
|
|
1200
|
+
lo = mid + 1;
|
|
1201
|
+
else
|
|
1202
|
+
hi = mid;
|
|
1203
|
+
}
|
|
1204
|
+
for (let j = Math.max(0, lo - 1); j < slots.length && slots[j] < q1; j++) {
|
|
1205
|
+
const e = entries[members[j]];
|
|
1206
|
+
if (overlaps(slots[j], ends[i][j], q0, q1)
|
|
1207
|
+
&& !overlaps(e.start, e.end, other.start, other.end))
|
|
1208
|
+
return true;
|
|
1209
|
+
}
|
|
1210
|
+
return false;
|
|
1211
|
+
};
|
|
1212
|
+
for (let k = firstAtOrAfter(original, region.start); k < original.length && original[k].at <= region.stop; k++) {
|
|
1213
|
+
const q = original[k].entry;
|
|
1214
|
+
if (!moving(q) && clashes(q))
|
|
1215
|
+
return true;
|
|
1216
|
+
}
|
|
1217
|
+
for (let k = firstAtOrAfter(planned, region.start); k < planned.length && planned[k].at <= region.stop; k++) {
|
|
1218
|
+
const q = planned[k].entry;
|
|
1219
|
+
if (moving(q) && clashes(q))
|
|
1220
|
+
return true;
|
|
1221
|
+
}
|
|
1222
|
+
return false;
|
|
1223
|
+
};
|
|
1224
|
+
const byStart = [...regions].sort((a, b) => a.start - b.start);
|
|
1225
|
+
const reach = [];
|
|
1226
|
+
for (const r of byStart)
|
|
1227
|
+
reach.push(Math.max(reach[reach.length - 1] ?? -Infinity, r.stop));
|
|
1228
|
+
const dirty = plans.map(() => true);
|
|
1229
|
+
let next = 0;
|
|
1230
|
+
const touch = (x) => {
|
|
1231
|
+
let lo = 0, hi = byStart.length;
|
|
1232
|
+
while (lo < hi) {
|
|
1233
|
+
const mid = (lo + hi) >>> 1;
|
|
1234
|
+
if (byStart[mid].start <= x)
|
|
1235
|
+
lo = mid + 1;
|
|
1236
|
+
else
|
|
1237
|
+
hi = mid;
|
|
1238
|
+
}
|
|
1239
|
+
for (let k = lo - 1; k >= 0 && reach[k] >= x; k--) {
|
|
1240
|
+
const r = byStart[k];
|
|
1241
|
+
if (alive[r.i] && !dirty[r.i] && r.stop >= x) {
|
|
1242
|
+
dirty[r.i] = true;
|
|
1243
|
+
next = Math.min(next, r.i);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
while (next < plans.length) {
|
|
1248
|
+
const i = next++;
|
|
1249
|
+
if (!alive[i] || !dirty[i])
|
|
1250
|
+
continue;
|
|
1251
|
+
dirty[i] = false;
|
|
1252
|
+
if (!conflicts(i))
|
|
1253
|
+
continue;
|
|
1254
|
+
alive[i] = false;
|
|
1255
|
+
for (const e of plans[i].members) {
|
|
1256
|
+
touch(entries[e].start);
|
|
1257
|
+
touch(entries[e].end);
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
return alive;
|
|
1261
|
+
}
|
|
1262
|
+
layoutCommand(label, axis, align) {
|
|
1263
|
+
const minimum = align === undefined ? 3 : 2;
|
|
1264
|
+
return {
|
|
1265
|
+
label,
|
|
1266
|
+
canRun: () => {
|
|
1267
|
+
const targets = this.layoutTargets();
|
|
1268
|
+
return targets.length >= minimum && targets.some((o) => !this.commandBlocked(o, "locked"));
|
|
1269
|
+
},
|
|
1270
|
+
run: () => {
|
|
1271
|
+
const targets = this.layoutTargets();
|
|
1272
|
+
if (targets.length < minimum)
|
|
1273
|
+
return;
|
|
1274
|
+
const bounds = transformBounds(targets);
|
|
1275
|
+
const size = axis === "x" ? "w" : "h";
|
|
1276
|
+
const entries = targets.map((o) => ({ o, box: transformBounds([o]) }));
|
|
1277
|
+
const patches = [];
|
|
1278
|
+
if (align !== undefined) {
|
|
1279
|
+
for (const { o, box } of entries) {
|
|
1280
|
+
const delta = bounds[axis] + (bounds[size] - box[size]) * align - box[axis];
|
|
1281
|
+
const roundoff = 4 * Number.EPSILON * Math.max(Math.abs(bounds[axis]), bounds[size], Math.abs(o[axis]), Math.abs(box[axis]), box[size]);
|
|
1282
|
+
if (Math.abs(delta) > roundoff && !this.commandBlocked(o, "locked"))
|
|
1283
|
+
patches.push([o, { [axis]: o[axis] + delta }]);
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
else {
|
|
1287
|
+
const end = (e) => e.box[axis] + e.box[size];
|
|
1288
|
+
entries.sort((a, b) => a.box[axis] - b.box[axis] || end(b) - end(a));
|
|
1289
|
+
const furthest = entries.reduce((a, b) => end(a) >= end(b) ? a : b);
|
|
1290
|
+
const spanning = entries.find((e) => e.box[axis] === entries[0].box[axis] && end(e) === end(furthest));
|
|
1291
|
+
const first = spanning ?? entries[0];
|
|
1292
|
+
const last = spanning ?? furthest;
|
|
1293
|
+
const fixed = entries.filter((e) => e === first || e === last || this.commandBlocked(e.o, "locked"));
|
|
1294
|
+
const fixedSet = new Set(fixed);
|
|
1295
|
+
const regionAt = (position) => {
|
|
1296
|
+
let holder;
|
|
1297
|
+
for (const e of fixed) {
|
|
1298
|
+
if (e.box[axis] <= position && position < end(e)
|
|
1299
|
+
&& (!holder || e.box[axis] > holder.box[axis] || (e.box[axis] === holder.box[axis] && end(e) < end(holder))))
|
|
1300
|
+
holder = e;
|
|
1301
|
+
}
|
|
1302
|
+
let start = holder ? holder.box[axis] : first.box[axis];
|
|
1303
|
+
let stop = holder ? end(holder) : end(last);
|
|
1304
|
+
for (const e of fixed) {
|
|
1305
|
+
if (e === holder)
|
|
1306
|
+
continue;
|
|
1307
|
+
if (holder && !(e.box[axis] >= holder.box[axis] && end(e) <= end(holder)
|
|
1308
|
+
&& (e.box[axis] > holder.box[axis] || end(e) < end(holder))))
|
|
1309
|
+
continue;
|
|
1310
|
+
if (e.box[axis] <= position)
|
|
1311
|
+
start = Math.max(start, end(e));
|
|
1312
|
+
else
|
|
1313
|
+
stop = Math.min(stop, e.box[axis]);
|
|
1314
|
+
}
|
|
1315
|
+
return { start, stop, key: JSON.stringify([holder?.o.id, start, stop]) };
|
|
1316
|
+
};
|
|
1317
|
+
const runs = new Map();
|
|
1318
|
+
for (const e of entries) {
|
|
1319
|
+
if (fixedSet.has(e))
|
|
1320
|
+
continue;
|
|
1321
|
+
const region = regionAt(e.box[axis]);
|
|
1322
|
+
let run = runs.get(region.key);
|
|
1323
|
+
if (!run)
|
|
1324
|
+
runs.set(region.key, (run = { ...region, members: [] }));
|
|
1325
|
+
run.members.push(e);
|
|
1326
|
+
}
|
|
1327
|
+
const plans = new Map();
|
|
1328
|
+
for (const [key, { start, stop, members: run }] of runs) {
|
|
1329
|
+
const total = run.reduce((sum, e) => sum + e.box[size], 0);
|
|
1330
|
+
const space = stop - start - total;
|
|
1331
|
+
if (space < 0)
|
|
1332
|
+
continue;
|
|
1333
|
+
const gap = space / (run.length + 1);
|
|
1334
|
+
const moves = [];
|
|
1335
|
+
const slots = [];
|
|
1336
|
+
let fits = true;
|
|
1337
|
+
let prefix = 0;
|
|
1338
|
+
for (const [i, { o, box }] of run.entries()) {
|
|
1339
|
+
const edge = start + prefix + (i + 1) * gap;
|
|
1340
|
+
const delta = edge - box[axis];
|
|
1341
|
+
const roundoff = 4 * Number.EPSILON * Math.max(Math.abs(start), Math.abs(stop), Math.abs(o[axis]), total, box[size]);
|
|
1342
|
+
if (Math.abs(delta) > roundoff) {
|
|
1343
|
+
if (regionAt(edge).key !== key || fixed.some((f) => (box[axis] < f.box[axis]) !== (edge < f.box[axis])
|
|
1344
|
+
|| (edge < f.box[axis] && edge + box[size] > f.box[axis]))) {
|
|
1345
|
+
fits = false;
|
|
1346
|
+
break;
|
|
1347
|
+
}
|
|
1348
|
+
moves.push([o, { [axis]: o[axis] + delta }]);
|
|
1349
|
+
slots.push(edge);
|
|
1350
|
+
}
|
|
1351
|
+
else
|
|
1352
|
+
slots.push(box[axis]);
|
|
1353
|
+
prefix += box[size];
|
|
1354
|
+
}
|
|
1355
|
+
if (fits)
|
|
1356
|
+
plans.set(key, { members: run, start, stop, slots, moves });
|
|
1357
|
+
}
|
|
1358
|
+
const list = [...plans.values()];
|
|
1359
|
+
if (!list.some((p) => p.moves.length))
|
|
1360
|
+
return;
|
|
1361
|
+
const entryIndex = new Map(entries.map((e, i) => [e, i]));
|
|
1362
|
+
const retained = PropertyController.retainedDistributionPlans(entries.map((e) => ({ start: e.box[axis], end: end(e), size: e.box[size] })), list.map((p) => ({ start: p.start, stop: p.stop, slots: p.slots, members: p.members.map((e) => entryIndex.get(e)) })));
|
|
1363
|
+
list.forEach((p, i) => { if (retained[i])
|
|
1364
|
+
patches.push(...p.moves); });
|
|
1365
|
+
}
|
|
1366
|
+
if (patches.length === 0)
|
|
1367
|
+
return;
|
|
1368
|
+
this.cancelPreview();
|
|
1369
|
+
this.doc.transact(label, (tx) => this.applyPatches(tx, patches));
|
|
1370
|
+
},
|
|
1371
|
+
};
|
|
1372
|
+
}
|
|
1373
|
+
buildCommands() {
|
|
1374
|
+
const doc = this.doc;
|
|
1375
|
+
const ids = () => this.targetIds().filter((id) => doc.get(id));
|
|
1376
|
+
const nonEmpty = () => ids().length > 0;
|
|
1377
|
+
const siblingsOf = (parent) => doc.order().filter((x) => (doc.get(x)?.parent ?? null) === parent);
|
|
1378
|
+
const moveAll = (label, how) => {
|
|
1379
|
+
const selected = new Set(ids());
|
|
1380
|
+
const parents = new Set();
|
|
1381
|
+
for (const id of selected)
|
|
1382
|
+
parents.add(doc.get(id).parent ?? null);
|
|
1383
|
+
const moves = [];
|
|
1384
|
+
for (const parent of parents) {
|
|
1385
|
+
const next = planZOrder(siblingsOf(parent), selected, how);
|
|
1386
|
+
if (!next)
|
|
1387
|
+
continue;
|
|
1388
|
+
const block = next.map((id, index) => ({ id, parent, index })).filter((m) => selected.has(m.id));
|
|
1389
|
+
moves.push(...(how === "front" || how === "forward" ? block.reverse() : block));
|
|
1390
|
+
}
|
|
1391
|
+
if (moves.length === 0)
|
|
1392
|
+
return;
|
|
1393
|
+
doc.transact(label, (tx) => {
|
|
1394
|
+
for (const m of moves)
|
|
1395
|
+
tx.move(m.id, m.parent, m.index);
|
|
1396
|
+
});
|
|
1397
|
+
};
|
|
1398
|
+
return {
|
|
1399
|
+
duplicate: { label: "Duplicate", canRun: nonEmpty, run: () => this.duplicate() },
|
|
1400
|
+
selectAll: {
|
|
1401
|
+
label: "Select all",
|
|
1402
|
+
canRun: () => doc.order().some((id) => {
|
|
1403
|
+
const o = doc.get(id);
|
|
1404
|
+
return !!o && o.kind !== "group" && !this.commandBlocked(o, "hidden") && !this.commandBlocked(o, "locked");
|
|
1405
|
+
}),
|
|
1406
|
+
run: () => { this.cancelPreview(); this.selection.set(this.selectAllIds()); },
|
|
1407
|
+
},
|
|
1408
|
+
alignLeft: this.layoutCommand("Align left", "x", 0),
|
|
1409
|
+
alignCenter: this.layoutCommand("Align center", "x", 0.5),
|
|
1410
|
+
alignRight: this.layoutCommand("Align right", "x", 1),
|
|
1411
|
+
alignTop: this.layoutCommand("Align top", "y", 0),
|
|
1412
|
+
alignMiddle: this.layoutCommand("Align middle", "y", 0.5),
|
|
1413
|
+
alignBottom: this.layoutCommand("Align bottom", "y", 1),
|
|
1414
|
+
distributeHorizontal: this.layoutCommand("Distribute horizontally", "x"),
|
|
1415
|
+
distributeVertical: this.layoutCommand("Distribute vertically", "y"),
|
|
1416
|
+
delete: { label: "Delete", canRun: nonEmpty, run: () => doc.transact("Delete", (tx) => ids().forEach((id) => tx.delete(id))) },
|
|
1417
|
+
bringToFront: { label: "Bring to front", canRun: nonEmpty, run: () => moveAll("Bring to front", "front") },
|
|
1418
|
+
sendToBack: { label: "Send to back", canRun: nonEmpty, run: () => moveAll("Send to back", "back") },
|
|
1419
|
+
bringForward: { label: "Bring forward", canRun: nonEmpty, run: () => moveAll("Bring forward", "forward") },
|
|
1420
|
+
sendBackward: { label: "Send backward", canRun: nonEmpty, run: () => moveAll("Send backward", "backward") },
|
|
1421
|
+
undo: { label: "Undo", canRun: () => doc.canUndo(), run: () => void doc.undo() },
|
|
1422
|
+
redo: { label: "Redo", canRun: () => doc.canRedo(), run: () => void doc.redo() },
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
subscribe(l) {
|
|
1426
|
+
this.listeners.add(l);
|
|
1427
|
+
return () => this.listeners.delete(l);
|
|
1428
|
+
}
|
|
1429
|
+
emit() {
|
|
1430
|
+
for (const l of this.listeners)
|
|
1431
|
+
l();
|
|
1432
|
+
}
|
|
1433
|
+
dispose() {
|
|
1434
|
+
this.dropTextPreview();
|
|
1435
|
+
for (const u of this.unsubs)
|
|
1436
|
+
u();
|
|
1437
|
+
this.listeners.clear();
|
|
1438
|
+
this.readCache.clear();
|
|
1439
|
+
this.readTargets = [];
|
|
1440
|
+
this.readVersions = [];
|
|
1441
|
+
this.readRegistry = [];
|
|
1442
|
+
this.readDescriptors.clear();
|
|
1443
|
+
this.readOverlay.clear();
|
|
1444
|
+
this.readPaths.clear();
|
|
1445
|
+
this.readInvalidation = null;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
export { getPath };
|