@widgetic/canvas 0.5.41 → 0.5.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.js +17 -18
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.js +1286 -1536
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/browserServices.js +65 -116
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/pageEvents.js +4 -4
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.js +3 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +18 -18
- package/dist/canvas/ui/floatingLayer.js +5 -6
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +34 -40
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.js +696 -795
- package/dist/engine/codec/canvasDocument.js +183 -147
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +76 -89
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +67 -94
- package/dist/engine/core/defaults.js +90 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.js +33 -50
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.js +129 -187
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +31 -64
- package/dist/engine/core/transform.js +83 -94
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +467 -474
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +262 -308
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +27 -30
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.js +4 -15
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.js +17 -68
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/settings/workspaceToolSettings.js +32 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +105 -141
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/types.js +1 -1
- package/package.json +1 -1
|
@@ -10,74 +10,6 @@
|
|
|
10
10
|
* end, so one gesture is one undo step. The pointer controller drives them; everything else (the menus, the
|
|
11
11
|
* keyboard, the host's own calls) uses the commands below, each of which is one step.
|
|
12
12
|
*/
|
|
13
|
-
var __assign = (this && this.__assign) || function () {
|
|
14
|
-
__assign = Object.assign || function(t) {
|
|
15
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
16
|
-
s = arguments[i];
|
|
17
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
18
|
-
t[p] = s[p];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
|
-
return __assign.apply(this, arguments);
|
|
23
|
-
};
|
|
24
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
25
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
26
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
27
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
28
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
29
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
30
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
34
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
35
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
36
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
37
|
-
function step(op) {
|
|
38
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
39
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
40
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
41
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
42
|
-
switch (op[0]) {
|
|
43
|
-
case 0: case 1: t = op; break;
|
|
44
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
45
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
46
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
47
|
-
default:
|
|
48
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
49
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
50
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
51
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
52
|
-
if (t[2]) _.ops.pop();
|
|
53
|
-
_.trys.pop(); continue;
|
|
54
|
-
}
|
|
55
|
-
op = body.call(thisArg, _);
|
|
56
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
57
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
61
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
62
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
63
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
64
|
-
};
|
|
65
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
66
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
67
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
68
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
69
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
70
|
-
};
|
|
71
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
72
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
73
|
-
if (ar || !(i in from)) {
|
|
74
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
75
|
-
ar[i] = from[i];
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
79
|
-
};
|
|
80
|
-
var _EngineCore_instances, _EngineCore_state, _EngineCore_rotationResetCancel, _EngineCore_listeners, _EngineCore_history, _EngineCore_clipboard, _EngineCore_pasteSteps, _EngineCore_nudgeMergeUntil, _EngineCore_textSession, _EngineCore_preferredTextFontFamily, _EngineCore_workspaceTextDefaults, _EngineCore_workspacePaintDefaults, _EngineCore_scribble, _EngineCore_indexes, _EngineCore_selectedShapesMemo, _EngineCore_set, _EngineCore_callHook, _EngineCore_sceneForSave, _EngineCore_afterHistoryMove, _EngineCore_setSelection, _EngineCore_dropScribble, _EngineCore_viewCenter, _EngineCore_fittedViewport, _EngineCore_fitText, _EngineCore_fitTexts, _EngineCore_arrange, _EngineCore_insertCopies, _EngineCore_flattenWidgetCard;
|
|
81
13
|
import { parseCanvasDocument, serializeCanvasDocument } from './codec/canvasDocument.js';
|
|
82
14
|
import { rotationAfterResetProgress, rotationResetProgress } from './core/rotationReset.js';
|
|
83
15
|
import { alignObjects, distributeObjects } from './core/arrange.js';
|
|
@@ -93,17 +25,17 @@ import { INITIAL_VIEWPORT, clampZoom, fitViewportToRect, nextZoomStep, panViewpo
|
|
|
93
25
|
import { DEFAULT_WIDGET_RUNTIME, EMPTY_OVERLAY, createInitialState } from './state.js';
|
|
94
26
|
import { createFixedAdvanceMeasure, fitTextObject } from './text/layoutText.js';
|
|
95
27
|
/** How many undo steps the board keeps (as the canvas it replaces did). */
|
|
96
|
-
export
|
|
28
|
+
export const HISTORY_LIMIT = 50;
|
|
97
29
|
/** How far a duplicate or a repeated paste lands from the original, in world units. */
|
|
98
|
-
export
|
|
30
|
+
export const PASTE_OFFSET = 20;
|
|
99
31
|
/** Arrow-key nudges within this time of each other are one undo step. */
|
|
100
|
-
export
|
|
32
|
+
export const NUDGE_MERGE_MS = 700;
|
|
101
33
|
/** Room left around the board when it is fitted to the view, in screen pixels. */
|
|
102
|
-
export
|
|
34
|
+
export const VIEW_FIT_PADDING = 48;
|
|
103
35
|
/** The longest a text may be, in letters. */
|
|
104
|
-
export
|
|
36
|
+
export const MAX_TEXT_LENGTH = 100_000;
|
|
105
37
|
/** A shape turned by less than this (degrees) stands upright: there is nothing for a reset to do. */
|
|
106
|
-
export
|
|
38
|
+
export const UPRIGHT_TOLERANCE_DEGREES = 0.01;
|
|
107
39
|
/** The cursor a tool shows over the board when nothing is under the pointer. */
|
|
108
40
|
export function cursorForTool(tool) {
|
|
109
41
|
switch (tool) {
|
|
@@ -117,174 +49,221 @@ export function cursorForTool(tool) {
|
|
|
117
49
|
return 'crosshair';
|
|
118
50
|
}
|
|
119
51
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
_EngineCore_scribble.set(this, null);
|
|
146
|
-
_EngineCore_indexes.set(this, new WeakMap());
|
|
147
|
-
_EngineCore_selectedShapesMemo.set(this, null);
|
|
52
|
+
export class EngineCore {
|
|
53
|
+
/** What the host shell listens to; the shell sets its own functions here. */
|
|
54
|
+
hooks = {};
|
|
55
|
+
/** Whether a new shape is plain white with a grey outline (true) or gets a random colour. */
|
|
56
|
+
wireframeMode = true;
|
|
57
|
+
/** The last place the pointer was over the board, in world units: where a paste lands. */
|
|
58
|
+
lastPointerWorld = null;
|
|
59
|
+
services;
|
|
60
|
+
#state = createInitialState();
|
|
61
|
+
#rotationResetCancel = null;
|
|
62
|
+
#listeners = new Set();
|
|
63
|
+
#history;
|
|
64
|
+
#clipboard = null;
|
|
65
|
+
#pasteSteps = 0;
|
|
66
|
+
#nudgeMergeUntil = 0;
|
|
67
|
+
#textSession = null;
|
|
68
|
+
/** The last font family chosen in the panel; new text objects start with this instead of the catalog default. */
|
|
69
|
+
#preferredTextFontFamily = null;
|
|
70
|
+
#workspaceTextDefaults = null;
|
|
71
|
+
#workspacePaintDefaults = null;
|
|
72
|
+
// The eraser's trail while it lives; what the views draw of it is `state.scribble`.
|
|
73
|
+
#scribble = null;
|
|
74
|
+
#indexes = new WeakMap();
|
|
75
|
+
#selectedShapesMemo = null;
|
|
76
|
+
constructor(services = {}) {
|
|
148
77
|
this.services = {
|
|
149
|
-
createId:
|
|
150
|
-
measureText:
|
|
151
|
-
now:
|
|
152
|
-
random:
|
|
153
|
-
requestAnimationFrame:
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
78
|
+
createId: services.createId ?? ((prefix) => createObjectId(prefix)),
|
|
79
|
+
measureText: services.measureText ?? createFixedAdvanceMeasure(),
|
|
80
|
+
now: services.now ?? (() => Date.now()),
|
|
81
|
+
random: services.random ?? Math.random,
|
|
82
|
+
requestAnimationFrame: services.requestAnimationFrame ??
|
|
83
|
+
((callback) => {
|
|
84
|
+
if (typeof globalThis.requestAnimationFrame === 'function') {
|
|
85
|
+
const id = globalThis.requestAnimationFrame(callback);
|
|
86
|
+
return () => globalThis.cancelAnimationFrame?.(id);
|
|
87
|
+
}
|
|
88
|
+
const id = globalThis.setTimeout(() => callback(this.services.now()), 16);
|
|
89
|
+
return () => globalThis.clearTimeout(id);
|
|
90
|
+
})
|
|
161
91
|
};
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
},
|
|
169
|
-
enumerable: false,
|
|
170
|
-
configurable: true
|
|
171
|
-
});
|
|
92
|
+
this.#history = new HistoryStack(this.#state.scene, { limit: HISTORY_LIMIT });
|
|
93
|
+
}
|
|
94
|
+
// ──────────────────────────────────────────────────────── state and subscriptions
|
|
95
|
+
get state() {
|
|
96
|
+
return this.#state;
|
|
97
|
+
}
|
|
172
98
|
/** Calls the listener after every change of the state. Returns the function that stops it. */
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
__classPrivateFieldGet(_this, _EngineCore_listeners, "f").delete(listener);
|
|
99
|
+
subscribe(listener) {
|
|
100
|
+
this.#listeners.add(listener);
|
|
101
|
+
return () => {
|
|
102
|
+
this.#listeners.delete(listener);
|
|
178
103
|
};
|
|
179
|
-
}
|
|
104
|
+
}
|
|
180
105
|
/** Lets go of every listener and hook (the board was removed from the page). */
|
|
181
|
-
|
|
106
|
+
dispose() {
|
|
182
107
|
this.cancelRotationReset();
|
|
183
|
-
|
|
108
|
+
this.#listeners.clear();
|
|
184
109
|
this.hooks = {};
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
110
|
+
this.#textSession = null;
|
|
111
|
+
this.#scribble = null;
|
|
112
|
+
}
|
|
188
113
|
/** Stops an upright animation in progress and records the angle on screen as one undo step. */
|
|
189
|
-
|
|
190
|
-
if (!
|
|
114
|
+
cancelRotationReset() {
|
|
115
|
+
if (!this.#rotationResetCancel)
|
|
191
116
|
return;
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
117
|
+
this.#rotationResetCancel();
|
|
118
|
+
this.#rotationResetCancel = null;
|
|
119
|
+
}
|
|
120
|
+
#set(patch) {
|
|
121
|
+
let changed = false;
|
|
122
|
+
for (const key of Object.keys(patch)) {
|
|
123
|
+
if (!Object.is(patch[key], this.#state[key])) {
|
|
124
|
+
changed = true;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (!changed)
|
|
129
|
+
return;
|
|
130
|
+
const before = this.#state.selectedIds;
|
|
131
|
+
this.#state = { ...this.#state, ...patch };
|
|
132
|
+
for (const listener of [...this.#listeners])
|
|
133
|
+
listener(this.#state);
|
|
134
|
+
if (this.#state.selectedIds !== before && !sameIds(this.#state.selectedIds, before)) {
|
|
135
|
+
this.#callHook(() => this.hooks.onSelectionChange?.(this.#state.selectedIds), 'selection hook');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
#callHook(run, name) {
|
|
139
|
+
try {
|
|
140
|
+
run();
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
this.hooks.onError?.(`The ${name} threw`, error);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
195
146
|
/** The objects of the scene by id. Built once per scene and kept while the scene is the same array. */
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
var index = __classPrivateFieldGet(this, _EngineCore_indexes, "f").get(scene);
|
|
147
|
+
sceneIndex(scene = this.#state.scene) {
|
|
148
|
+
let index = this.#indexes.get(scene);
|
|
199
149
|
if (!index) {
|
|
200
150
|
index = indexScene(scene);
|
|
201
|
-
|
|
151
|
+
this.#indexes.set(scene, index);
|
|
202
152
|
}
|
|
203
153
|
return index;
|
|
204
|
-
}
|
|
205
|
-
|
|
154
|
+
}
|
|
155
|
+
getObject(id) {
|
|
206
156
|
return this.sceneIndex().get(id);
|
|
207
|
-
}
|
|
157
|
+
}
|
|
208
158
|
/** The objects that are selected (a group counts once). */
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
for (
|
|
213
|
-
|
|
214
|
-
var object = index.get(id);
|
|
159
|
+
selectedObjects() {
|
|
160
|
+
const index = this.sceneIndex();
|
|
161
|
+
const objects = [];
|
|
162
|
+
for (const id of this.#state.selectedIds) {
|
|
163
|
+
const object = index.get(id);
|
|
215
164
|
if (object)
|
|
216
165
|
objects.push(object);
|
|
217
166
|
}
|
|
218
167
|
return objects;
|
|
219
|
-
}
|
|
168
|
+
}
|
|
220
169
|
/**
|
|
221
170
|
* The shapes the selection draws: its members when a group is selected. Found once for a scene and a selection and kept
|
|
222
171
|
* while they are the same ones: the frame, the handles and the panel each ask at every step of a drag.
|
|
223
172
|
*/
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
173
|
+
selectedShapes() {
|
|
174
|
+
const { scene, selectedIds } = this.#state;
|
|
175
|
+
const memo = this.#selectedShapesMemo;
|
|
227
176
|
if (memo && memo.scene === scene && memo.selectedIds === selectedIds)
|
|
228
177
|
return memo.shapes;
|
|
229
|
-
|
|
230
|
-
|
|
178
|
+
const shapes = leafShapesOf(scene, selectedIds);
|
|
179
|
+
this.#selectedShapesMemo = { scene, selectedIds, shapes };
|
|
231
180
|
return shapes;
|
|
232
|
-
}
|
|
181
|
+
}
|
|
233
182
|
/** The box the selection handles surround, or null when nothing is selected. */
|
|
234
|
-
|
|
183
|
+
selectionFrame() {
|
|
235
184
|
return selectionFrameOf(this.selectedShapes());
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return
|
|
239
|
-
}
|
|
185
|
+
}
|
|
186
|
+
isEmpty() {
|
|
187
|
+
return this.#state.scene.length === 0;
|
|
188
|
+
}
|
|
240
189
|
// ──────────────────────────────────────────────────────── loading and saving
|
|
241
190
|
/** Shows a canvas that was read from saved data. History starts again from it, and it is not "changed". */
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
__classPrivateFieldSet(this, _EngineCore_textSession, null, "f");
|
|
191
|
+
load(parsed, options = {}) {
|
|
192
|
+
this.#textSession = null;
|
|
245
193
|
// The texts are laid out first: an arrow tied to one rests on the box its letters make.
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
194
|
+
const scene = reconcileArrowBindings(this.#fitTexts(parsed.scene));
|
|
195
|
+
this.#history.reset(scene);
|
|
196
|
+
const viewport = options.fitView ? this.#fittedViewport(scene) : parsed.viewport;
|
|
197
|
+
this.#set({
|
|
198
|
+
...this.#dropScribble(),
|
|
199
|
+
scene,
|
|
200
|
+
viewport,
|
|
201
|
+
selectedIds: [],
|
|
202
|
+
enteredGroupId: null,
|
|
203
|
+
hoverId: null,
|
|
204
|
+
editingTextId: null,
|
|
205
|
+
overlay: EMPTY_OVERLAY,
|
|
206
|
+
pending: Object.freeze({}),
|
|
207
|
+
hoverWidgetId: null,
|
|
208
|
+
canUndo: false,
|
|
209
|
+
canRedo: false,
|
|
210
|
+
revision: this.#state.revision + 1
|
|
211
|
+
});
|
|
212
|
+
}
|
|
251
213
|
/** Reads saved data (text or an object) and shows it. Never throws: a bad document loads as an empty board. */
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
var parsed = parseCanvasDocument(input);
|
|
214
|
+
loadDocument(input, options = {}) {
|
|
215
|
+
const parsed = parseCanvasDocument(input);
|
|
255
216
|
this.load(parsed, options);
|
|
256
217
|
return parsed;
|
|
257
|
-
}
|
|
218
|
+
}
|
|
258
219
|
/** The board as text to store. A card still waiting for its widget, and a text nobody typed in, are left out. */
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
220
|
+
serialize(options = {}) {
|
|
221
|
+
return serializeCanvasDocument(this.#sceneForSave(), this.#state.viewport, {
|
|
222
|
+
now: new Date(this.services.now()),
|
|
223
|
+
...options
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
#sceneForSave() {
|
|
227
|
+
const { scene, pending, editingTextId } = this.#state;
|
|
228
|
+
const unwanted = new Set();
|
|
229
|
+
for (const object of scene) {
|
|
230
|
+
if (pending[object.id])
|
|
231
|
+
unwanted.add(object.id);
|
|
232
|
+
if (object.id === editingTextId && object.type === 'text' && object.text.trim() === '')
|
|
233
|
+
unwanted.add(object.id);
|
|
234
|
+
}
|
|
235
|
+
if (unwanted.size === 0)
|
|
236
|
+
return scene;
|
|
237
|
+
return normalizeScene(scene.filter((object) => !unwanted.has(object.id)));
|
|
238
|
+
}
|
|
263
239
|
/** Forgets what can be undone (after the host replaced the board). */
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
240
|
+
resetHistory() {
|
|
241
|
+
this.#history.reset(this.#state.scene);
|
|
242
|
+
this.#set({ canUndo: false, canRedo: false });
|
|
243
|
+
}
|
|
244
|
+
setLoading(loading) {
|
|
245
|
+
this.#set({ loading });
|
|
246
|
+
}
|
|
271
247
|
/**
|
|
272
248
|
* Tells the engine how big the board element is. With `keepCentre` the view stays on the same point of the world: when
|
|
273
249
|
* the board grows or shrinks, the pan moves by half of the change, so what was in the middle stays in the middle (a board
|
|
274
250
|
* that its page resizes by dragging its edges). The first measure (from 0 by 0) never moves the pan, whatever `keepCentre` says.
|
|
275
251
|
*/
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
var current = __classPrivateFieldGet(this, _EngineCore_state, "f").viewSize;
|
|
252
|
+
setViewSize(size, keepCentre = false) {
|
|
253
|
+
const current = this.#state.viewSize;
|
|
279
254
|
if (current.width === size.width && current.height === size.height)
|
|
280
255
|
return;
|
|
281
|
-
|
|
256
|
+
const patch = { viewSize: { width: size.width, height: size.height } };
|
|
282
257
|
if (keepCentre && current.width > 0 && current.height > 0 && size.width > 0 && size.height > 0) {
|
|
283
|
-
|
|
284
|
-
patch.viewport =
|
|
258
|
+
const viewport = this.#state.viewport;
|
|
259
|
+
patch.viewport = {
|
|
260
|
+
...viewport,
|
|
261
|
+
x: viewport.x + (size.width - current.width) / 2,
|
|
262
|
+
y: viewport.y + (size.height - current.height) / 2
|
|
263
|
+
};
|
|
285
264
|
}
|
|
286
|
-
|
|
287
|
-
}
|
|
265
|
+
this.#set(patch);
|
|
266
|
+
}
|
|
288
267
|
// ──────────────────────────────────────────────────────── scene changes
|
|
289
268
|
/**
|
|
290
269
|
* Puts a new scene on the board. `transient` is for the frames of a gesture (nothing is recorded), `push`
|
|
@@ -293,502 +272,525 @@ var EngineCore = /** @class */ (function () {
|
|
|
293
272
|
* Whatever the change was (a drag, a stretch, a word typed, an undo), the arrows that are tied to shapes are drawn again from
|
|
294
273
|
* where their shapes are now, in the same scene and the same step: nothing else has to know about the ties.
|
|
295
274
|
*/
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
var previous = __classPrivateFieldGet(this, _EngineCore_state, "f").scene;
|
|
300
|
-
var next = reconcileArrowBindings(requested);
|
|
275
|
+
applyScene(requested, mode = 'push') {
|
|
276
|
+
const previous = this.#state.scene;
|
|
277
|
+
const next = reconcileArrowBindings(requested);
|
|
301
278
|
if (next === previous)
|
|
302
279
|
return mode === 'push' ? this.commit() : false;
|
|
303
|
-
|
|
280
|
+
const patch = { scene: next, revision: this.#state.revision + 1 };
|
|
304
281
|
if (mode !== 'transient' || next.length !== previous.length) {
|
|
305
282
|
// Keep everything that points at an object pointing at one that is still there.
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
if (selection.length !==
|
|
283
|
+
const index = this.sceneIndex(next);
|
|
284
|
+
const selection = this.#state.selectedIds.filter((id) => index.has(id));
|
|
285
|
+
if (selection.length !== this.#state.selectedIds.length)
|
|
309
286
|
patch.selectedIds = selection;
|
|
310
|
-
if (
|
|
287
|
+
if (this.#state.hoverId && !index.has(this.#state.hoverId))
|
|
311
288
|
patch.hoverId = null;
|
|
312
|
-
if (
|
|
289
|
+
if (this.#state.enteredGroupId && !index.has(this.#state.enteredGroupId))
|
|
313
290
|
patch.enteredGroupId = null;
|
|
314
|
-
if (
|
|
291
|
+
if (this.#state.editingTextId && !index.has(this.#state.editingTextId))
|
|
315
292
|
patch.editingTextId = null;
|
|
316
|
-
|
|
317
|
-
if (pendingIds.some(
|
|
318
|
-
patch.pending = Object.freeze(Object.fromEntries(Object.entries(
|
|
319
|
-
var id = _a[0];
|
|
320
|
-
return index_1.has(id);
|
|
321
|
-
})));
|
|
293
|
+
const pendingIds = Object.keys(this.#state.pending);
|
|
294
|
+
if (pendingIds.some((id) => !index.has(id))) {
|
|
295
|
+
patch.pending = Object.freeze(Object.fromEntries(Object.entries(this.#state.pending).filter(([id]) => index.has(id))));
|
|
322
296
|
}
|
|
323
297
|
}
|
|
324
298
|
if (mode === 'replace')
|
|
325
|
-
|
|
326
|
-
|
|
299
|
+
this.#history.replaceCurrent(next);
|
|
300
|
+
this.#set(patch);
|
|
327
301
|
if (mode === 'push')
|
|
328
302
|
this.commit();
|
|
329
303
|
else if (mode === 'replace') {
|
|
330
|
-
|
|
331
|
-
|
|
304
|
+
this.#nudgeMergeUntil = 0;
|
|
305
|
+
this.hooks.onDirty?.();
|
|
332
306
|
}
|
|
333
307
|
return true;
|
|
334
|
-
}
|
|
308
|
+
}
|
|
335
309
|
/**
|
|
336
310
|
* Puts a scene on the board with no undo step and without telling the host that the board changed: a repair
|
|
337
311
|
* of what was loaded (a name filled in), not an edit.
|
|
338
312
|
*/
|
|
339
|
-
|
|
340
|
-
if (next ===
|
|
313
|
+
applySilently(next) {
|
|
314
|
+
if (next === this.#state.scene)
|
|
341
315
|
return;
|
|
342
316
|
this.applyScene(next, 'transient');
|
|
343
317
|
// What the board holds now (the arrows tied to shapes drawn again), not the scene that was asked for.
|
|
344
|
-
|
|
345
|
-
}
|
|
318
|
+
this.#history.replaceCurrent(this.#state.scene);
|
|
319
|
+
}
|
|
346
320
|
/** Records the scene as it is now as one undo step (the end of a gesture). False when nothing changed since the last step. */
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
if (!__classPrivateFieldGet(this, _EngineCore_history, "f").push(__classPrivateFieldGet(this, _EngineCore_state, "f").scene))
|
|
321
|
+
commit() {
|
|
322
|
+
this.#nudgeMergeUntil = 0;
|
|
323
|
+
if (!this.#history.push(this.#state.scene))
|
|
351
324
|
return false;
|
|
352
|
-
|
|
353
|
-
|
|
325
|
+
this.#set({ canUndo: this.#history.canUndo, canRedo: this.#history.canRedo });
|
|
326
|
+
this.hooks.onDirty?.();
|
|
354
327
|
return true;
|
|
355
|
-
}
|
|
328
|
+
}
|
|
356
329
|
/** Throws away what a gesture did and goes back to the last step (Escape in the middle of a drag). */
|
|
357
|
-
|
|
358
|
-
this.applyScene(
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
330
|
+
revertToCommitted() {
|
|
331
|
+
this.applyScene(this.#history.current, 'transient');
|
|
332
|
+
this.#set({ overlay: EMPTY_OVERLAY });
|
|
333
|
+
}
|
|
334
|
+
undo() {
|
|
362
335
|
this.endTextEdit();
|
|
363
336
|
// An upright turn still on its way ends here as one step, so that this undo takes that step back.
|
|
364
337
|
this.cancelRotationReset();
|
|
365
|
-
|
|
338
|
+
const previous = this.#history.undo();
|
|
366
339
|
if (!previous)
|
|
367
340
|
return false;
|
|
368
|
-
|
|
341
|
+
this.#afterHistoryMove(previous);
|
|
369
342
|
return true;
|
|
370
|
-
}
|
|
371
|
-
|
|
343
|
+
}
|
|
344
|
+
redo() {
|
|
372
345
|
this.endTextEdit();
|
|
373
346
|
this.cancelRotationReset();
|
|
374
|
-
|
|
347
|
+
const next = this.#history.redo();
|
|
375
348
|
if (!next)
|
|
376
349
|
return false;
|
|
377
|
-
|
|
350
|
+
this.#afterHistoryMove(next);
|
|
378
351
|
return true;
|
|
379
|
-
}
|
|
352
|
+
}
|
|
353
|
+
#afterHistoryMove(scene) {
|
|
354
|
+
this.#nudgeMergeUntil = 0;
|
|
355
|
+
this.applyScene(scene, 'transient');
|
|
356
|
+
this.#set({ canUndo: this.#history.canUndo, canRedo: this.#history.canRedo, overlay: EMPTY_OVERLAY });
|
|
357
|
+
this.hooks.onDirty?.();
|
|
358
|
+
}
|
|
380
359
|
/** Adds objects on top of the board in one step. Returns their ids. */
|
|
381
|
-
|
|
382
|
-
var _this = this;
|
|
383
|
-
if (options === void 0) { options = {}; }
|
|
360
|
+
addObjects(objects, options = {}) {
|
|
384
361
|
if (objects.length === 0)
|
|
385
362
|
return [];
|
|
386
|
-
|
|
387
|
-
this.applyScene(
|
|
388
|
-
|
|
363
|
+
const fitted = objects.map((object) => this.#fitText(object));
|
|
364
|
+
this.applyScene([...this.#state.scene, ...fitted], 'push');
|
|
365
|
+
const ids = fitted.map((object) => object.id);
|
|
389
366
|
if (options.select)
|
|
390
367
|
this.select(ids);
|
|
391
368
|
return ids;
|
|
392
|
-
}
|
|
369
|
+
}
|
|
393
370
|
/** Removes objects (and what is inside them) without asking anyone. */
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
if (next ===
|
|
371
|
+
removeObjects(ids) {
|
|
372
|
+
const next = removeFromScene(this.#state.scene, ids);
|
|
373
|
+
if (next === this.#state.scene)
|
|
397
374
|
return false;
|
|
398
375
|
this.applyScene(next, 'push');
|
|
399
376
|
return true;
|
|
400
|
-
}
|
|
377
|
+
}
|
|
401
378
|
/** Deletes objects, first asking the host about each widget card among them. Resolves false when the host said no. */
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
confirm = this.hooks.confirmWidgetDeletion;
|
|
414
|
-
if (!confirm) return [3 /*break*/, 7];
|
|
415
|
-
_i = 0, widgets_1 = widgets;
|
|
416
|
-
_c.label = 1;
|
|
417
|
-
case 1:
|
|
418
|
-
if (!(_i < widgets_1.length)) return [3 /*break*/, 7];
|
|
419
|
-
widget = widgets_1[_i];
|
|
420
|
-
allowed = true;
|
|
421
|
-
_c.label = 2;
|
|
422
|
-
case 2:
|
|
423
|
-
_c.trys.push([2, 4, , 5]);
|
|
424
|
-
return [4 /*yield*/, confirm(widget.widgetId)];
|
|
425
|
-
case 3:
|
|
426
|
-
allowed = (_c.sent()) !== false;
|
|
427
|
-
return [3 /*break*/, 5];
|
|
428
|
-
case 4:
|
|
429
|
-
error_1 = _c.sent();
|
|
430
|
-
allowed = false;
|
|
431
|
-
(_b = (_a = this.hooks).onError) === null || _b === void 0 ? void 0 : _b.call(_a, 'The widget deletion check threw', error_1);
|
|
432
|
-
return [3 /*break*/, 5];
|
|
433
|
-
case 5:
|
|
434
|
-
if (!allowed)
|
|
435
|
-
return [2 /*return*/, false];
|
|
436
|
-
_c.label = 6;
|
|
437
|
-
case 6:
|
|
438
|
-
_i++;
|
|
439
|
-
return [3 /*break*/, 1];
|
|
440
|
-
case 7: return [2 /*return*/, this.removeObjects(wanted)];
|
|
379
|
+
async deleteObjects(ids) {
|
|
380
|
+
const wanted = [...ids];
|
|
381
|
+
if (wanted.length === 0)
|
|
382
|
+
return false;
|
|
383
|
+
const widgets = leafShapesOf(this.#state.scene, wanted).filter((shape) => shape.type === 'widget');
|
|
384
|
+
const confirm = this.hooks.confirmWidgetDeletion;
|
|
385
|
+
if (confirm) {
|
|
386
|
+
for (const widget of widgets) {
|
|
387
|
+
let allowed = true;
|
|
388
|
+
try {
|
|
389
|
+
allowed = (await confirm(widget.widgetId)) !== false;
|
|
441
390
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
}
|
|
391
|
+
catch (error) {
|
|
392
|
+
allowed = false;
|
|
393
|
+
this.hooks.onError?.('The widget deletion check threw', error);
|
|
394
|
+
}
|
|
395
|
+
if (!allowed)
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return this.removeObjects(wanted);
|
|
400
|
+
}
|
|
401
|
+
async deleteSelection() {
|
|
402
|
+
return this.deleteObjects(this.#state.selectedIds);
|
|
403
|
+
}
|
|
452
404
|
/** Empties the board in one undoable step and tells the host. */
|
|
453
|
-
|
|
454
|
-
var _this = this;
|
|
405
|
+
clearAll() {
|
|
455
406
|
this.endTextEdit();
|
|
456
|
-
if (
|
|
407
|
+
if (this.#state.scene.length === 0)
|
|
457
408
|
return;
|
|
458
409
|
this.applyScene([], 'push');
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
}
|
|
410
|
+
this.#set({ selectedIds: [], enteredGroupId: null, hoverId: null });
|
|
411
|
+
this.#callHook(() => this.hooks.onCleared?.(), 'cleared hook');
|
|
412
|
+
}
|
|
462
413
|
// ──────────────────────────────────────────────────────── selection
|
|
463
414
|
/** Selects units (a group counts once). Everything else is deselected unless `additive`. */
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
};
|
|
415
|
+
select(ids, options = {}) {
|
|
416
|
+
const scene = this.#state.scene;
|
|
417
|
+
const requested = options.additive ? [...this.#state.selectedIds, ...ids] : [...ids];
|
|
418
|
+
const selection = normalizeSelection(scene, requested);
|
|
419
|
+
this.#setSelection(selection);
|
|
420
|
+
}
|
|
471
421
|
/** Adds the unit to the selection, or takes it out when it is in (Shift-click). */
|
|
472
|
-
|
|
473
|
-
|
|
422
|
+
toggleSelection(id) {
|
|
423
|
+
const current = this.#state.selectedIds;
|
|
474
424
|
if (current.includes(id))
|
|
475
|
-
|
|
425
|
+
this.#setSelection(current.filter((candidate) => candidate !== id));
|
|
476
426
|
else
|
|
477
427
|
this.select([id], { additive: true });
|
|
478
|
-
}
|
|
428
|
+
}
|
|
479
429
|
/** Deselects everything and leaves the group that was entered. */
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
430
|
+
clearSelection() {
|
|
431
|
+
this.#setSelection([], null);
|
|
432
|
+
}
|
|
433
|
+
selectAll() {
|
|
434
|
+
const scene = this.#state.scene;
|
|
435
|
+
const entered = this.#state.enteredGroupId;
|
|
436
|
+
const units = scene.filter((object) => (entered ? object.parentId === entered : object.parentId === null));
|
|
437
|
+
this.#setSelection(units.map((object) => object.id), entered);
|
|
438
|
+
}
|
|
439
|
+
#setSelection(selection, enteredGroupId = this.#state.enteredGroupId) {
|
|
440
|
+
let entered = enteredGroupId;
|
|
441
|
+
if (entered) {
|
|
442
|
+
const index = this.sceneIndex();
|
|
443
|
+
// Picking something outside the group that was entered leaves the group.
|
|
444
|
+
const stillInside = selection.length === 0 || selection.every((id) => index.get(id)?.parentId === entered);
|
|
445
|
+
if (!stillInside)
|
|
446
|
+
entered = null;
|
|
447
|
+
}
|
|
448
|
+
const next = sameIds(selection, this.#state.selectedIds) ? this.#state.selectedIds : Object.freeze([...selection]);
|
|
449
|
+
this.#set({ selectedIds: next, enteredGroupId: entered });
|
|
450
|
+
}
|
|
489
451
|
/** Makes the members of a group pickable one by one, and selects one of them. */
|
|
490
|
-
|
|
491
|
-
|
|
452
|
+
enterGroup(groupId, selectId) {
|
|
453
|
+
const group = this.getObject(groupId);
|
|
492
454
|
if (!group || group.type !== 'group')
|
|
493
455
|
return;
|
|
494
|
-
|
|
495
|
-
}
|
|
456
|
+
this.#set({ enteredGroupId: groupId, selectedIds: selectId ? [selectId] : [] });
|
|
457
|
+
}
|
|
496
458
|
/** Goes back out of the entered group and selects the group itself. */
|
|
497
|
-
|
|
498
|
-
|
|
459
|
+
exitGroup() {
|
|
460
|
+
const entered = this.#state.enteredGroupId;
|
|
499
461
|
if (!entered)
|
|
500
462
|
return;
|
|
501
|
-
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
}
|
|
463
|
+
this.#set({ enteredGroupId: null, selectedIds: [entered] });
|
|
464
|
+
}
|
|
465
|
+
setHover(id) {
|
|
466
|
+
this.#set({ hoverId: id });
|
|
467
|
+
}
|
|
506
468
|
/** The pointer is over the turning dot of the selection (or left it): the dot grows while it is. */
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
}
|
|
469
|
+
setRotationDotHover(hovered) {
|
|
470
|
+
this.#set({ rotationDotHovered: hovered });
|
|
471
|
+
}
|
|
510
472
|
/** The pointer is over (or is dragging) an end or the middle of the selected arrow, or left them: that handle grows. */
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
}
|
|
473
|
+
setArrowHandleHover(handle) {
|
|
474
|
+
this.#set({ arrowHandleHovered: handle });
|
|
475
|
+
}
|
|
514
476
|
/** The unit under a world point. */
|
|
515
|
-
|
|
516
|
-
|
|
477
|
+
hitTest(worldPoint, ignore) {
|
|
478
|
+
const state = this.#state;
|
|
517
479
|
return hitTestPoint(state.scene, worldPoint, {
|
|
518
480
|
zoom: state.viewport.zoom,
|
|
519
481
|
enteredGroupId: state.enteredGroupId,
|
|
520
482
|
index: this.sceneIndex(),
|
|
521
|
-
ignore
|
|
483
|
+
ignore
|
|
522
484
|
});
|
|
523
|
-
}
|
|
485
|
+
}
|
|
524
486
|
/** Every unit under a world point, the lowest first (what an Alt-click steps through). */
|
|
525
|
-
|
|
526
|
-
|
|
487
|
+
hitTestStack(worldPoint) {
|
|
488
|
+
const state = this.#state;
|
|
527
489
|
return hitTestStack(state.scene, worldPoint, {
|
|
528
490
|
zoom: state.viewport.zoom,
|
|
529
491
|
enteredGroupId: state.enteredGroupId,
|
|
530
492
|
index: this.sceneIndex()
|
|
531
493
|
});
|
|
532
|
-
}
|
|
494
|
+
}
|
|
533
495
|
/** The units a rectangle of the world takes. */
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
var state = __classPrivateFieldGet(this, _EngineCore_state, "f");
|
|
496
|
+
hitTestRect(rect, mode = 'intersect', ignore) {
|
|
497
|
+
const state = this.#state;
|
|
537
498
|
return hitTestRect(state.scene, rect, {
|
|
538
499
|
zoom: state.viewport.zoom,
|
|
539
500
|
enteredGroupId: state.enteredGroupId,
|
|
540
501
|
index: this.sceneIndex(),
|
|
541
|
-
mode
|
|
542
|
-
ignore
|
|
502
|
+
mode,
|
|
503
|
+
ignore
|
|
543
504
|
});
|
|
544
|
-
}
|
|
505
|
+
}
|
|
545
506
|
// ──────────────────────────────────────────────────────── overlay and cursor
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
for (
|
|
550
|
-
|
|
551
|
-
if (!Object.is(patch[key], __classPrivateFieldGet(this, _EngineCore_state, "f").overlay[key]))
|
|
507
|
+
setOverlay(patch) {
|
|
508
|
+
const next = { ...this.#state.overlay, ...patch };
|
|
509
|
+
let changed = false;
|
|
510
|
+
for (const key of Object.keys(patch)) {
|
|
511
|
+
if (!Object.is(patch[key], this.#state.overlay[key]))
|
|
552
512
|
changed = true;
|
|
553
513
|
}
|
|
554
514
|
if (changed)
|
|
555
|
-
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
}
|
|
515
|
+
this.#set({ overlay: next });
|
|
516
|
+
}
|
|
517
|
+
clearOverlay() {
|
|
518
|
+
this.#set({ overlay: EMPTY_OVERLAY });
|
|
519
|
+
}
|
|
560
520
|
// ──────────────────────────────────────────────────────── the eraser's trail
|
|
561
521
|
/** The eraser's trail begins (the pointer went past the drag threshold). The host's frame clock runs while `state.scribble` is not null. */
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
}
|
|
522
|
+
beginScribble() {
|
|
523
|
+
this.#scribble = new Scribble();
|
|
524
|
+
this.#set({ scribble: this.#scribble.view() });
|
|
525
|
+
}
|
|
566
526
|
/** The pointer reached a place of the board (world units); the next step of the trail takes it in. */
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
};
|
|
527
|
+
extendScribble(world) {
|
|
528
|
+
this.#scribble?.add(world, this.#state.viewport.zoom);
|
|
529
|
+
}
|
|
571
530
|
/** The pointer let go: the trail is eaten away over the next frames. */
|
|
572
|
-
|
|
573
|
-
|
|
531
|
+
stopScribble() {
|
|
532
|
+
const scribble = this.#scribble;
|
|
574
533
|
if (!scribble)
|
|
575
534
|
return;
|
|
576
535
|
scribble.stop();
|
|
577
536
|
// The end of a finished line is drawn capped: the views are told that this one is finished.
|
|
578
|
-
|
|
579
|
-
}
|
|
537
|
+
this.#set({ scribble: scribble.view() });
|
|
538
|
+
}
|
|
580
539
|
/**
|
|
581
540
|
* One frame of the trail's life, `elapsedMs` after the last one. Returns false when there is no trail any more (the
|
|
582
541
|
* host stops its clock), true while it lives.
|
|
583
542
|
*/
|
|
584
|
-
|
|
585
|
-
|
|
543
|
+
stepScribble(elapsedMs) {
|
|
544
|
+
const scribble = this.#scribble;
|
|
586
545
|
if (!scribble) {
|
|
587
|
-
if (
|
|
588
|
-
|
|
546
|
+
if (this.#state.scribble !== null)
|
|
547
|
+
this.#set({ scribble: null });
|
|
589
548
|
return false;
|
|
590
549
|
}
|
|
591
|
-
|
|
550
|
+
const changed = scribble.step(elapsedMs);
|
|
592
551
|
if (scribble.finished) {
|
|
593
|
-
|
|
594
|
-
|
|
552
|
+
this.#scribble = null;
|
|
553
|
+
this.#set({ scribble: null });
|
|
595
554
|
return false;
|
|
596
555
|
}
|
|
597
556
|
if (changed)
|
|
598
|
-
|
|
557
|
+
this.#set({ scribble: scribble.view() });
|
|
599
558
|
return true;
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
559
|
+
}
|
|
560
|
+
/** Takes the trail away at once (the tool changed, the board was replaced). */
|
|
561
|
+
#dropScribble() {
|
|
562
|
+
this.#scribble = null;
|
|
563
|
+
return this.#state.scribble === null ? {} : { scribble: null };
|
|
564
|
+
}
|
|
565
|
+
setCursor(cursor) {
|
|
566
|
+
this.#set({ cursor });
|
|
567
|
+
}
|
|
604
568
|
// ──────────────────────────────────────────────────────── tools
|
|
605
|
-
|
|
606
|
-
var _a;
|
|
607
|
-
if (options === void 0) { options = {}; }
|
|
569
|
+
setTool(tool, options = {}) {
|
|
608
570
|
this.endTextEdit();
|
|
609
|
-
|
|
571
|
+
const patch = {
|
|
572
|
+
...this.#dropScribble(),
|
|
573
|
+
tool,
|
|
574
|
+
overlay: EMPTY_OVERLAY,
|
|
575
|
+
cursor: cursorForTool(tool),
|
|
576
|
+
hoverId: null,
|
|
577
|
+
rotationDotHovered: false,
|
|
578
|
+
arrowHandleHovered: null,
|
|
579
|
+
hoverWidgetId: null
|
|
580
|
+
};
|
|
610
581
|
if (tool === 'library-shape')
|
|
611
|
-
patch.libraryShapeKey =
|
|
582
|
+
patch.libraryShapeKey = options.libraryShapeKey ?? this.#state.libraryShapeKey;
|
|
612
583
|
if (tool !== 'select' && tool !== 'pan' && !options.keepSelection) {
|
|
613
584
|
patch.selectedIds = [];
|
|
614
585
|
patch.enteredGroupId = null;
|
|
615
586
|
}
|
|
616
|
-
|
|
617
|
-
}
|
|
587
|
+
this.#set(patch);
|
|
588
|
+
}
|
|
618
589
|
/** Back to the Select tool, unless the person asked for the tool to stay. */
|
|
619
|
-
|
|
620
|
-
if (
|
|
621
|
-
if (__classPrivateFieldGet(this, _EngineCore_state, "f").stickyTool && !options.force)
|
|
590
|
+
revertToSelect(options = {}) {
|
|
591
|
+
if (this.#state.stickyTool && !options.force)
|
|
622
592
|
return;
|
|
623
|
-
if (
|
|
593
|
+
if (this.#state.tool !== 'select')
|
|
624
594
|
this.setTool('select', { keepSelection: true });
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
}
|
|
595
|
+
}
|
|
596
|
+
setStickyTool(sticky) {
|
|
597
|
+
this.#set({ stickyTool: sticky });
|
|
598
|
+
}
|
|
629
599
|
// ──────────────────────────────────────────────────────── viewport
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
600
|
+
setViewport(viewport) {
|
|
601
|
+
const next = sanitizeViewport(viewport);
|
|
602
|
+
const current = this.#state.viewport;
|
|
633
603
|
if (next.x === current.x && next.y === current.y && next.zoom === current.zoom)
|
|
634
604
|
return;
|
|
635
|
-
|
|
636
|
-
}
|
|
637
|
-
|
|
605
|
+
this.#set({ viewport: next });
|
|
606
|
+
}
|
|
607
|
+
panBy(deltaX, deltaY) {
|
|
638
608
|
if (deltaX === 0 && deltaY === 0)
|
|
639
609
|
return;
|
|
640
|
-
this.setViewport(panViewport(
|
|
641
|
-
}
|
|
610
|
+
this.setViewport(panViewport(this.#state.viewport, deltaX, deltaY));
|
|
611
|
+
}
|
|
642
612
|
/** Zooms and keeps the world point under `screenPoint` where it is. */
|
|
643
|
-
|
|
644
|
-
this.setViewport(zoomAtPoint(
|
|
645
|
-
}
|
|
613
|
+
zoomAt(screenPoint, zoom) {
|
|
614
|
+
this.setViewport(zoomAtPoint(this.#state.viewport, screenPoint, zoom));
|
|
615
|
+
}
|
|
616
|
+
#viewCenter() {
|
|
617
|
+
const { width, height } = this.#state.viewSize;
|
|
618
|
+
return { x: width / 2, y: height / 2 };
|
|
619
|
+
}
|
|
646
620
|
/** One step up or down the zoom ladder, about the centre of the view. */
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
this.zoomAt(
|
|
650
|
-
}
|
|
621
|
+
zoomStep(direction) {
|
|
622
|
+
const viewport = this.#state.viewport;
|
|
623
|
+
this.zoomAt(this.#viewCenter(), nextZoomStep(viewport.zoom, direction));
|
|
624
|
+
}
|
|
651
625
|
/** A zoom level (1 is 100%), about the centre of the view. */
|
|
652
|
-
|
|
653
|
-
this.zoomAt(
|
|
654
|
-
}
|
|
626
|
+
zoomTo(zoom) {
|
|
627
|
+
this.zoomAt(this.#viewCenter(), clampZoom(zoom));
|
|
628
|
+
}
|
|
655
629
|
/** 100%, with the world origin where it was at the centre of the view. */
|
|
656
|
-
|
|
630
|
+
zoomReset() {
|
|
657
631
|
this.zoomTo(1);
|
|
658
|
-
}
|
|
632
|
+
}
|
|
633
|
+
#fittedViewport(scene) {
|
|
634
|
+
const bounds = boardVisualBounds(scene);
|
|
635
|
+
if (!bounds)
|
|
636
|
+
return { ...INITIAL_VIEWPORT };
|
|
637
|
+
return fitViewportToRect(bounds, this.#state.viewSize, VIEW_FIT_PADDING);
|
|
638
|
+
}
|
|
659
639
|
/** Shows everything on the board. An empty board goes back to the start. */
|
|
660
|
-
|
|
661
|
-
this.setViewport(
|
|
662
|
-
}
|
|
640
|
+
zoomToFit() {
|
|
641
|
+
this.setViewport(this.#fittedViewport(this.#state.scene));
|
|
642
|
+
}
|
|
663
643
|
/** Shows the selection, or everything when nothing is selected. */
|
|
664
|
-
|
|
665
|
-
|
|
644
|
+
zoomToSelection() {
|
|
645
|
+
const bounds = boundsOfObjects(this.#state.scene, this.#state.selectedIds);
|
|
666
646
|
if (!bounds) {
|
|
667
647
|
this.zoomToFit();
|
|
668
648
|
return;
|
|
669
649
|
}
|
|
670
|
-
this.setViewport(fitViewportToRect(bounds,
|
|
671
|
-
}
|
|
650
|
+
this.setViewport(fitViewportToRect(bounds, this.#state.viewSize, VIEW_FIT_PADDING, 2));
|
|
651
|
+
}
|
|
672
652
|
/** The rectangle of the world that is on screen. */
|
|
673
|
-
|
|
674
|
-
return visibleWorldRect(
|
|
675
|
-
}
|
|
653
|
+
visibleRect() {
|
|
654
|
+
return visibleWorldRect(this.#state.viewport, this.#state.viewSize);
|
|
655
|
+
}
|
|
676
656
|
/** The point of the world at a point of the board element. */
|
|
677
|
-
|
|
678
|
-
return screenToWorld(
|
|
679
|
-
}
|
|
657
|
+
toWorld(screenPoint) {
|
|
658
|
+
return screenToWorld(this.#state.viewport, screenPoint);
|
|
659
|
+
}
|
|
680
660
|
/** The middle of what is on screen, in world units (where a button puts a new object). */
|
|
681
|
-
|
|
682
|
-
|
|
661
|
+
visibleCenter() {
|
|
662
|
+
const rect = this.visibleRect();
|
|
683
663
|
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
684
|
-
}
|
|
664
|
+
}
|
|
665
|
+
// ──────────────────────────────────────────────────────── text
|
|
666
|
+
#fitText(object) {
|
|
667
|
+
return object.type === 'text' ? fitTextObject(object, this.services.measureText) : object;
|
|
668
|
+
}
|
|
669
|
+
/** Lays out every text of a scene again (after fonts loaded, a file was read, or a style changed). */
|
|
670
|
+
#fitTexts(scene) {
|
|
671
|
+
let changed = false;
|
|
672
|
+
const next = scene.map((object) => {
|
|
673
|
+
if (object.type !== 'text')
|
|
674
|
+
return object;
|
|
675
|
+
const fitted = fitTextObject(object, this.services.measureText);
|
|
676
|
+
if (fitted !== object)
|
|
677
|
+
changed = true;
|
|
678
|
+
return fitted;
|
|
679
|
+
});
|
|
680
|
+
return changed ? next : scene;
|
|
681
|
+
}
|
|
685
682
|
/** Fonts finished loading: letters changed width, so every text is laid out again. */
|
|
686
|
-
|
|
683
|
+
notifyFontsChanged() {
|
|
687
684
|
// Not a change the person made: no undo step, and nothing is saved because of it.
|
|
688
|
-
|
|
689
|
-
|
|
685
|
+
const committed = this.#history.current;
|
|
686
|
+
const refitCommitted = reconcileArrowBindings(this.#fitTexts(committed));
|
|
690
687
|
if (refitCommitted !== committed)
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
if (next !==
|
|
688
|
+
this.#history.replaceCurrent(refitCommitted);
|
|
689
|
+
this.#set({ fontRevision: this.#state.fontRevision + 1 });
|
|
690
|
+
const next = this.#fitTexts(this.#state.scene);
|
|
691
|
+
if (next !== this.#state.scene)
|
|
695
692
|
this.applyScene(next, 'transient');
|
|
696
|
-
}
|
|
693
|
+
}
|
|
697
694
|
/** Remembers the font family for the next text object the person places. */
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
if (!__classPrivateFieldGet(this, _EngineCore_workspacePaintDefaults, "f"))
|
|
695
|
+
setPreferredTextFontFamily(fontFamily) {
|
|
696
|
+
this.#preferredTextFontFamily = fontFamily;
|
|
697
|
+
}
|
|
698
|
+
setWorkspaceToolDefaults(settings) {
|
|
699
|
+
this.#workspaceTextDefaults = settings.text ?? null;
|
|
700
|
+
this.#workspacePaintDefaults = settings.paint ?? null;
|
|
701
|
+
if (settings.text?.fontFamily)
|
|
702
|
+
this.#preferredTextFontFamily = settings.text.fontFamily;
|
|
703
|
+
}
|
|
704
|
+
workspacePaintForNewShape(wireframe) {
|
|
705
|
+
if (!this.#workspacePaintDefaults)
|
|
710
706
|
return {};
|
|
711
707
|
if (wireframe) {
|
|
712
|
-
|
|
713
|
-
return
|
|
708
|
+
const { stroke, strokeWidth, strokeStyle, opacity } = this.#workspacePaintDefaults;
|
|
709
|
+
return {
|
|
710
|
+
...(stroke !== undefined ? { stroke } : {}),
|
|
711
|
+
...(strokeWidth !== undefined ? { strokeWidth } : {}),
|
|
712
|
+
...(strokeStyle !== undefined ? { strokeStyle } : {}),
|
|
713
|
+
...(opacity !== undefined ? { opacity } : {})
|
|
714
|
+
};
|
|
714
715
|
}
|
|
715
|
-
return
|
|
716
|
-
}
|
|
716
|
+
return { ...this.#workspacePaintDefaults };
|
|
717
|
+
}
|
|
717
718
|
/**
|
|
718
719
|
* Makes a text object, puts it on the board and starts editing it. A click makes a label whose width follows its
|
|
719
720
|
* text; a width makes a box whose lines wrap. Nothing is recorded until the editing ends with text in it.
|
|
720
721
|
*/
|
|
721
|
-
|
|
722
|
-
var _a, _b, _c;
|
|
723
|
-
if (options === void 0) { options = {}; }
|
|
722
|
+
createText(point, options = {}) {
|
|
724
723
|
this.endTextEdit();
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
724
|
+
const fixedWidth = options.width !== undefined && options.width > TEXT_DEFAULTS.minimumWidth;
|
|
725
|
+
const base = createTextObject({
|
|
726
|
+
id: this.services.createId('obj'),
|
|
727
|
+
x: point.x,
|
|
728
|
+
y: point.y,
|
|
729
|
+
width: fixedWidth ? options.width : TEXT_DEFAULTS.minimumWidth,
|
|
730
|
+
autoWidth: !fixedWidth,
|
|
731
|
+
fontFamily: this.#preferredTextFontFamily ?? this.#workspaceTextDefaults?.fontFamily ?? TEXT_DEFAULTS.fontFamily,
|
|
732
|
+
...this.#workspaceTextDefaults
|
|
733
|
+
});
|
|
734
|
+
const text = this.#fitText(base);
|
|
735
|
+
const sceneBefore = this.#state.scene;
|
|
736
|
+
this.applyScene([...sceneBefore, text], 'transient');
|
|
737
|
+
this.#textSession = { id: text.id, isNew: true, sceneBefore, typed: false };
|
|
738
|
+
this.#set({ editingTextId: text.id, selectedIds: [text.id], enteredGroupId: null });
|
|
732
739
|
return text.id;
|
|
733
|
-
}
|
|
740
|
+
}
|
|
734
741
|
/** Starts editing the text of an existing text object. */
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
var object = this.getObject(id);
|
|
742
|
+
beginTextEdit(id) {
|
|
743
|
+
const object = this.getObject(id);
|
|
738
744
|
if (!object || object.type !== 'text')
|
|
739
745
|
return false;
|
|
740
|
-
if (
|
|
746
|
+
if (this.#textSession?.id === id)
|
|
741
747
|
return true;
|
|
742
748
|
this.endTextEdit();
|
|
743
|
-
|
|
744
|
-
|
|
749
|
+
this.#textSession = { id, isNew: false, sceneBefore: this.#state.scene, typed: false };
|
|
750
|
+
this.#set({ editingTextId: id, selectedIds: [id] });
|
|
745
751
|
return true;
|
|
746
|
-
}
|
|
752
|
+
}
|
|
747
753
|
/** The text the person has typed so far; the box is laid out again on every call. */
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
var session = __classPrivateFieldGet(this, _EngineCore_textSession, "f");
|
|
754
|
+
updateEditingText(text) {
|
|
755
|
+
const session = this.#textSession;
|
|
751
756
|
if (!session)
|
|
752
757
|
return;
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
});
|
|
760
|
-
var changed = next.some(function (object, position) { return object !== scene[position]; });
|
|
758
|
+
const clipped = text.length > MAX_TEXT_LENGTH ? text.slice(0, MAX_TEXT_LENGTH) : text;
|
|
759
|
+
const scene = this.#state.scene;
|
|
760
|
+
const next = scene.map((object) => object.id === session.id && object.type === 'text' && object.text !== clipped
|
|
761
|
+
? fitTextObject({ ...object, text: clipped }, this.services.measureText)
|
|
762
|
+
: object);
|
|
763
|
+
const changed = next.some((object, position) => object !== scene[position]);
|
|
761
764
|
if (!changed)
|
|
762
765
|
return;
|
|
763
766
|
this.applyScene(next, 'transient');
|
|
764
767
|
session.typed = true;
|
|
765
768
|
// The board is not recorded until the typing ends, but the letters are on it: the host's next save takes them.
|
|
766
|
-
|
|
767
|
-
}
|
|
769
|
+
this.#callHook(() => this.hooks.onDraftChange?.(), 'draft change hook');
|
|
770
|
+
}
|
|
768
771
|
/**
|
|
769
772
|
* Finishes the edit. An empty text is removed (a new one leaves nothing behind, not even an undo step);
|
|
770
773
|
* otherwise creation and typing together are one undo step.
|
|
771
774
|
*/
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
var session = __classPrivateFieldGet(this, _EngineCore_textSession, "f");
|
|
775
|
+
endTextEdit() {
|
|
776
|
+
const session = this.#textSession;
|
|
775
777
|
if (!session)
|
|
776
778
|
return null;
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
779
|
+
this.#textSession = null;
|
|
780
|
+
const object = this.getObject(session.id);
|
|
781
|
+
this.#set({ editingTextId: null });
|
|
782
|
+
let kept = null;
|
|
781
783
|
if (object && object.type === 'text') {
|
|
782
784
|
if (object.text.trim() === '') {
|
|
783
785
|
if (session.isNew) {
|
|
784
786
|
this.applyScene(session.sceneBefore, 'transient');
|
|
785
|
-
|
|
787
|
+
this.#set({ selectedIds: [] });
|
|
786
788
|
// A save may have taken the letters that were typed before they were all deleted: it has to be told they are gone.
|
|
787
789
|
if (session.typed)
|
|
788
|
-
|
|
790
|
+
this.#callHook(() => this.hooks.onDraftChange?.(), 'draft change hook');
|
|
789
791
|
}
|
|
790
792
|
else {
|
|
791
|
-
this.applyScene(removeFromScene(
|
|
793
|
+
this.applyScene(removeFromScene(this.#state.scene, [session.id]), 'push');
|
|
792
794
|
}
|
|
793
795
|
}
|
|
794
796
|
else {
|
|
@@ -797,243 +799,281 @@ var EngineCore = /** @class */ (function () {
|
|
|
797
799
|
}
|
|
798
800
|
}
|
|
799
801
|
// The Text tool is used once: the board goes back to Select when the typing is over (unless the tool is kept).
|
|
800
|
-
if (
|
|
802
|
+
if (this.#state.tool === 'text' && !this.#state.stickyTool)
|
|
801
803
|
this.setTool('select', { keepSelection: true });
|
|
802
804
|
return kept;
|
|
803
|
-
}
|
|
805
|
+
}
|
|
804
806
|
// ──────────────────────────────────────────────────────── commands on the selection
|
|
805
807
|
/** Changes how the selection looks. `commit: false` is for a slider that is still being dragged: call `commit()` when it ends. */
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
var changed = false;
|
|
812
|
-
var next = scene.map(function (object) {
|
|
808
|
+
setStyle(patch, options = {}) {
|
|
809
|
+
const scene = this.#state.scene;
|
|
810
|
+
const targetIds = withDescendantIds(scene, this.#state.selectedIds);
|
|
811
|
+
let changed = false;
|
|
812
|
+
const next = scene.map((object) => {
|
|
813
813
|
if (!targetIds.has(object.id) || object.type === 'group')
|
|
814
814
|
return object;
|
|
815
|
-
|
|
815
|
+
const styled = applyStylePatch(object, patch);
|
|
816
816
|
if (styled === object)
|
|
817
817
|
return object;
|
|
818
818
|
changed = true;
|
|
819
|
-
return
|
|
819
|
+
return this.#fitText(styled);
|
|
820
820
|
});
|
|
821
821
|
if (!changed)
|
|
822
822
|
return false;
|
|
823
823
|
this.applyScene(next, options.commit === false ? 'transient' : 'push');
|
|
824
824
|
return true;
|
|
825
|
-
}
|
|
825
|
+
}
|
|
826
826
|
/**
|
|
827
827
|
* Turns the one selected shape back upright, about its own middle (a double click on the turning dot). One undo step.
|
|
828
828
|
* False when the selection is not a single shape, or the shape stands upright already.
|
|
829
829
|
*/
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
var shapes = this.selectedShapes();
|
|
830
|
+
resetSelectionRotation() {
|
|
831
|
+
const shapes = this.selectedShapes();
|
|
833
832
|
if (shapes.length !== 1 || Math.abs(shapes[0].rotation) < UPRIGHT_TOLERANCE_DEGREES)
|
|
834
833
|
return false;
|
|
835
834
|
this.cancelRotationReset();
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
835
|
+
const turned = shapes[0];
|
|
836
|
+
const startRotation = turned.rotation;
|
|
837
|
+
const startAt = this.services.now();
|
|
838
|
+
let committed = false;
|
|
839
|
+
let cancelFrame = null;
|
|
840
|
+
const commitCurrent = () => {
|
|
842
841
|
if (committed)
|
|
843
842
|
return;
|
|
844
843
|
committed = true;
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
844
|
+
this.#rotationResetCancel = null;
|
|
845
|
+
const shape = this.getObject(turned.id);
|
|
846
|
+
const current = shape && 'rotation' in shape ? shape.rotation : startRotation;
|
|
847
|
+
const next = this.#state.scene.map((object) => (object.id === turned.id ? { ...turned, rotation: current } : object));
|
|
848
|
+
this.applyScene(next, 'push');
|
|
850
849
|
};
|
|
851
850
|
// The time comes from the engine's own clock, not from the frame callback: a browser hands the callback a time since the
|
|
852
851
|
// page opened, while `startAt` is read from `services.now` (the wall clock), so mixing the two would never finish the turn.
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
852
|
+
const tick = () => {
|
|
853
|
+
const elapsed = this.services.now() - startAt;
|
|
854
|
+
const progress = rotationResetProgress(elapsed);
|
|
855
|
+
const angle = rotationAfterResetProgress(startRotation, progress);
|
|
856
|
+
const next = this.#state.scene.map((object) => (object.id === turned.id ? { ...turned, rotation: angle } : object));
|
|
857
|
+
this.applyScene(next, 'transient');
|
|
859
858
|
if (progress >= 1) {
|
|
860
|
-
cancelFrame
|
|
859
|
+
cancelFrame?.();
|
|
861
860
|
committed = true;
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
861
|
+
this.#rotationResetCancel = null;
|
|
862
|
+
const upright = this.#state.scene.map((object) => (object.id === turned.id ? { ...turned, rotation: 0 } : object));
|
|
863
|
+
this.applyScene(upright, 'push');
|
|
865
864
|
return;
|
|
866
865
|
}
|
|
867
|
-
cancelFrame =
|
|
866
|
+
cancelFrame = this.services.requestAnimationFrame(tick);
|
|
868
867
|
};
|
|
869
868
|
cancelFrame = this.services.requestAnimationFrame(tick);
|
|
870
|
-
|
|
871
|
-
cancelFrame
|
|
869
|
+
this.#rotationResetCancel = () => {
|
|
870
|
+
cancelFrame?.();
|
|
872
871
|
commitCurrent();
|
|
873
|
-
}
|
|
872
|
+
};
|
|
874
873
|
return true;
|
|
875
|
-
}
|
|
874
|
+
}
|
|
876
875
|
/**
|
|
877
876
|
* Lays the one selected text out in one line again, as it was when it was made: its width follows its letters (a
|
|
878
877
|
* double click on its side handle). One undo step. False when the selection is not a single text, or it follows its letters already.
|
|
879
878
|
*/
|
|
880
|
-
|
|
881
|
-
|
|
879
|
+
fitSelectedTextToContent() {
|
|
880
|
+
const shapes = this.selectedShapes();
|
|
882
881
|
if (shapes.length !== 1 || shapes[0].type !== 'text' || shapes[0].autoWidth)
|
|
883
882
|
return false;
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
this.applyScene(
|
|
883
|
+
const only = shapes[0];
|
|
884
|
+
const fitted = this.#fitText({ ...only, autoWidth: true });
|
|
885
|
+
this.applyScene(this.#state.scene.map((object) => (object.id === only.id ? fitted : object)), 'push');
|
|
887
886
|
return true;
|
|
888
|
-
}
|
|
887
|
+
}
|
|
889
888
|
/** Moves the selection by a distance (an arrow key). Presses close together are one undo step. */
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
889
|
+
nudgeSelection(deltaX, deltaY) {
|
|
890
|
+
const scene = this.#state.scene;
|
|
891
|
+
const next = translateObjects(scene, this.#state.selectedIds, deltaX, deltaY);
|
|
893
892
|
if (next === scene)
|
|
894
893
|
return false;
|
|
895
|
-
|
|
896
|
-
|
|
894
|
+
const now = this.services.now();
|
|
895
|
+
const merging = now < this.#nudgeMergeUntil;
|
|
897
896
|
this.applyScene(next, merging ? 'replace' : 'push');
|
|
898
|
-
|
|
897
|
+
this.#nudgeMergeUntil = now + NUDGE_MERGE_MS;
|
|
899
898
|
return true;
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
return
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
return
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
899
|
+
}
|
|
900
|
+
alignSelection(mode) {
|
|
901
|
+
return this.#arrange((scene, ids) => alignObjects(scene, ids, mode));
|
|
902
|
+
}
|
|
903
|
+
distributeSelection(axis) {
|
|
904
|
+
return this.#arrange((scene, ids) => distributeObjects(scene, ids, axis));
|
|
905
|
+
}
|
|
906
|
+
#arrange(change) {
|
|
907
|
+
const scene = this.#state.scene;
|
|
908
|
+
const next = change(scene, this.#state.selectedIds);
|
|
909
|
+
if (next === scene)
|
|
910
|
+
return false;
|
|
911
|
+
this.applyScene(next, 'push');
|
|
912
|
+
return true;
|
|
913
|
+
}
|
|
914
|
+
reorderSelection(mode) {
|
|
915
|
+
return this.#arrange((scene, ids) => reorderObjects(scene, ids, mode));
|
|
916
|
+
}
|
|
910
917
|
/** Puts the selected units in a group, which becomes the selection. */
|
|
911
|
-
|
|
912
|
-
|
|
918
|
+
groupSelection() {
|
|
919
|
+
const grouped = groupObjects(this.#state.scene, this.#state.selectedIds, this.services.createId('group'));
|
|
913
920
|
if (!grouped)
|
|
914
921
|
return false;
|
|
915
922
|
this.applyScene(grouped.scene, 'push');
|
|
916
|
-
|
|
923
|
+
this.#set({ selectedIds: [grouped.groupId], enteredGroupId: null });
|
|
917
924
|
return true;
|
|
918
|
-
}
|
|
925
|
+
}
|
|
919
926
|
/** Dissolves the selected groups; their members become the selection. */
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
if (scene ===
|
|
927
|
+
ungroupSelection() {
|
|
928
|
+
const { scene, releasedIds } = ungroupObjects(this.#state.scene, this.#state.selectedIds);
|
|
929
|
+
if (scene === this.#state.scene)
|
|
923
930
|
return false;
|
|
924
931
|
this.applyScene(scene, 'push');
|
|
925
|
-
|
|
932
|
+
this.#set({ selectedIds: releasedIds, enteredGroupId: null });
|
|
926
933
|
return true;
|
|
927
|
-
}
|
|
934
|
+
}
|
|
928
935
|
// ──────────────────────────────────────────────────────── clipboard
|
|
929
|
-
|
|
930
|
-
return
|
|
931
|
-
}
|
|
936
|
+
hasClipboard() {
|
|
937
|
+
return this.#clipboard !== null;
|
|
938
|
+
}
|
|
932
939
|
/** Remembers the selection for a paste. */
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
940
|
+
copySelection() {
|
|
941
|
+
const scene = this.#state.scene;
|
|
942
|
+
const rootIds = normalizeSelection(scene, this.#state.selectedIds);
|
|
936
943
|
if (rootIds.length === 0)
|
|
937
944
|
return false;
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
945
|
+
const included = withDescendantIds(scene, rootIds);
|
|
946
|
+
this.#clipboard = { objects: scene.filter((object) => included.has(object.id)), rootIds };
|
|
947
|
+
this.#pasteSteps = 0;
|
|
941
948
|
return true;
|
|
942
|
-
}
|
|
949
|
+
}
|
|
943
950
|
/** Copies the selection and removes it, without asking about widget cards: the widget itself stays where it is stored. */
|
|
944
|
-
|
|
951
|
+
cutSelection() {
|
|
945
952
|
if (!this.copySelection())
|
|
946
953
|
return false;
|
|
947
|
-
|
|
954
|
+
const removed = this.removeObjects(this.#state.selectedIds);
|
|
948
955
|
if (removed)
|
|
949
|
-
|
|
956
|
+
this.#set({ selectedIds: [] });
|
|
950
957
|
return removed;
|
|
951
|
-
}
|
|
958
|
+
}
|
|
952
959
|
/** Puts a copy of the clipboard on the board, centred on `at` (or shifted a little from the original). Returns the new ids. */
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
var clipboard = __classPrivateFieldGet(this, _EngineCore_clipboard, "f");
|
|
960
|
+
paste(at = this.lastPointerWorld) {
|
|
961
|
+
const clipboard = this.#clipboard;
|
|
956
962
|
if (!clipboard)
|
|
957
963
|
return [];
|
|
958
|
-
|
|
959
|
-
|
|
964
|
+
const bounds = boundsOfObjects(clipboard.objects, clipboard.rootIds);
|
|
965
|
+
this.#pasteSteps += 1;
|
|
960
966
|
// Each paste after the first steps a little further, so copies pasted in a row do not hide each other.
|
|
961
|
-
|
|
962
|
-
|
|
967
|
+
const cascade = (this.#pasteSteps - 1) * PASTE_OFFSET;
|
|
968
|
+
const offset = at && bounds
|
|
963
969
|
? { x: at.x - (bounds.x + bounds.width / 2) + cascade, y: at.y - (bounds.y + bounds.height / 2) + cascade }
|
|
964
|
-
: { x: PASTE_OFFSET *
|
|
965
|
-
return
|
|
966
|
-
}
|
|
970
|
+
: { x: PASTE_OFFSET * this.#pasteSteps, y: PASTE_OFFSET * this.#pasteSteps };
|
|
971
|
+
return this.#insertCopies(clipboard.objects, clipboard.rootIds, offset);
|
|
972
|
+
}
|
|
967
973
|
/** A copy of the selection, a little to the side and below. The copy becomes the selection. */
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
974
|
+
duplicateSelection() {
|
|
975
|
+
const scene = this.#state.scene;
|
|
976
|
+
const rootIds = normalizeSelection(scene, this.#state.selectedIds);
|
|
971
977
|
if (rootIds.length === 0)
|
|
972
978
|
return [];
|
|
973
|
-
return
|
|
974
|
-
}
|
|
979
|
+
return this.#insertCopies(scene, rootIds, { x: PASTE_OFFSET, y: PASTE_OFFSET });
|
|
980
|
+
}
|
|
975
981
|
/** Copies the selection in place for an Alt-drag: one undo step when the drag ends. */
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
982
|
+
duplicateSelectionForDrag() {
|
|
983
|
+
const scene = this.#state.scene;
|
|
984
|
+
const rootIds = normalizeSelection(scene, this.#state.selectedIds);
|
|
979
985
|
if (rootIds.length === 0)
|
|
980
986
|
return [];
|
|
981
|
-
return
|
|
982
|
-
}
|
|
987
|
+
return this.#insertCopies(scene, rootIds, { x: 0, y: 0 }, 'transient');
|
|
988
|
+
}
|
|
989
|
+
#insertCopies(source, rootIds, offset, mode = 'push') {
|
|
990
|
+
const { objects, idMap } = duplicateObjects(source, rootIds, offset, (type) => this.services.createId(type === 'group' ? 'group' : 'obj'));
|
|
991
|
+
if (objects.length === 0)
|
|
992
|
+
return [];
|
|
993
|
+
const duplicatesWidgets = this.hooks.onWidgetDuplicated ?? null;
|
|
994
|
+
const pending = { ...this.#state.pending };
|
|
995
|
+
const widgetClones = [];
|
|
996
|
+
const prepared = objects.map((object) => {
|
|
997
|
+
if (object.type !== 'widget')
|
|
998
|
+
return object;
|
|
999
|
+
if (duplicatesWidgets) {
|
|
1000
|
+
// The card waits, unselected, for the host to make the widget behind it.
|
|
1001
|
+
pending[object.id] = 'duplicating';
|
|
1002
|
+
widgetClones.push({ sourceWidgetId: object.widgetId, clone: object });
|
|
1003
|
+
return object;
|
|
1004
|
+
}
|
|
1005
|
+
// With no host to copy the widget, a copy of a card is only a picture of it.
|
|
1006
|
+
return this.#flattenWidgetCard(object);
|
|
1007
|
+
});
|
|
1008
|
+
// A copy of a member goes into the group of the original; if that group is gone it is repaired to the top level.
|
|
1009
|
+
this.applyScene(normalizeScene([...this.#state.scene, ...prepared.map((object) => this.#fitText(object))]), mode);
|
|
1010
|
+
this.#set({ pending: Object.freeze(pending) });
|
|
1011
|
+
const rootCopyIds = rootIds.map((id) => idMap.get(id)).filter((id) => !!id);
|
|
1012
|
+
const selectable = rootCopyIds.filter((id) => !pending[id]);
|
|
1013
|
+
this.#setSelection(selectable);
|
|
1014
|
+
for (const { sourceWidgetId, clone } of widgetClones) {
|
|
1015
|
+
this.#callHook(() => duplicatesWidgets?.(sourceWidgetId, clone), 'widget duplicated hook');
|
|
1016
|
+
}
|
|
1017
|
+
return rootCopyIds;
|
|
1018
|
+
}
|
|
1019
|
+
/** A widget card with its widget gone: the screenshot it had, or an empty frame. */
|
|
1020
|
+
#flattenWidgetCard(card) {
|
|
1021
|
+
const box = { id: card.id, parentId: card.parentId, x: card.x, y: card.y, width: card.width, height: card.height, rotation: card.rotation, opacity: card.opacity };
|
|
1022
|
+
if (card.thumbnail)
|
|
1023
|
+
return createImageObject({ ...box, src: card.thumbnail, naturalWidth: card.width, naturalHeight: card.height });
|
|
1024
|
+
return createRectObject(box);
|
|
1025
|
+
}
|
|
983
1026
|
// ──────────────────────────────────────────────────────── widget cards (the parts every caller needs)
|
|
984
1027
|
/** Marks cards as waiting for the host (a loader is shown over them). `null` clears the mark. */
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
if (((_a = current[id]) !== null && _a !== void 0 ? _a : null) === state)
|
|
1028
|
+
setPending(id, state) {
|
|
1029
|
+
const current = this.#state.pending;
|
|
1030
|
+
if ((current[id] ?? null) === state)
|
|
989
1031
|
return;
|
|
990
|
-
|
|
1032
|
+
const next = { ...current };
|
|
991
1033
|
if (state)
|
|
992
1034
|
next[id] = state;
|
|
993
1035
|
else
|
|
994
1036
|
delete next[id];
|
|
995
|
-
|
|
996
|
-
}
|
|
1037
|
+
this.#set({ pending: Object.freeze(next) });
|
|
1038
|
+
}
|
|
997
1039
|
/** What the host said about a widget's card, with the defaults for what it never said. */
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
};
|
|
1040
|
+
widgetRuntime(widgetId) {
|
|
1041
|
+
return this.#state.widgets[widgetId] ?? DEFAULT_WIDGET_RUNTIME;
|
|
1042
|
+
}
|
|
1002
1043
|
/** Records what the host says about a widget's card. True when something changed. */
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
var next = __assign(__assign({}, current), patch);
|
|
1044
|
+
setWidgetRuntime(widgetId, patch) {
|
|
1045
|
+
const current = this.widgetRuntime(widgetId);
|
|
1046
|
+
const next = { ...current, ...patch };
|
|
1007
1047
|
if (next.hiddenImage === current.hiddenImage && next.orphaned === current.orphaned && next.bodyDragAllowed === current.bodyDragAllowed) {
|
|
1008
1048
|
return false;
|
|
1009
1049
|
}
|
|
1010
|
-
|
|
1050
|
+
this.#set({ widgets: Object.freeze({ ...this.#state.widgets, [widgetId]: Object.freeze(next) }) });
|
|
1011
1051
|
return true;
|
|
1012
|
-
}
|
|
1052
|
+
}
|
|
1013
1053
|
/** The widget whose card is under the pointer, or null. */
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
}
|
|
1054
|
+
setHoverWidget(widgetId) {
|
|
1055
|
+
this.#set({ hoverWidgetId: widgetId });
|
|
1056
|
+
}
|
|
1017
1057
|
/** The host's own bar is dragging a card. */
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
}
|
|
1058
|
+
setOverlayDragActive(active) {
|
|
1059
|
+
this.#set({ overlayDragActive: active });
|
|
1060
|
+
}
|
|
1021
1061
|
/** Gives a card (or any object, which becomes a card) the widget it stands for. The card becomes the selection. */
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1062
|
+
linkObjectToWidget(target, widgetId, name) {
|
|
1063
|
+
const id = typeof target === 'string' ? target : target.id;
|
|
1064
|
+
const object = this.getObject(id);
|
|
1025
1065
|
if (!object || object.type === 'group')
|
|
1026
1066
|
return false;
|
|
1027
|
-
|
|
1067
|
+
let replacement;
|
|
1028
1068
|
if (object.type === 'widget') {
|
|
1029
|
-
replacement =
|
|
1069
|
+
replacement = { ...object, widgetId, widgetName: name ?? object.widgetName };
|
|
1030
1070
|
}
|
|
1031
1071
|
else {
|
|
1032
1072
|
replacement = createWidgetObject({
|
|
1033
|
-
id
|
|
1073
|
+
id,
|
|
1034
1074
|
parentId: object.parentId,
|
|
1035
|
-
widgetId
|
|
1036
|
-
widgetName: name
|
|
1075
|
+
widgetId,
|
|
1076
|
+
widgetName: name ?? null,
|
|
1037
1077
|
x: object.x,
|
|
1038
1078
|
y: object.y,
|
|
1039
1079
|
width: object.width,
|
|
@@ -1042,160 +1082,21 @@ var EngineCore = /** @class */ (function () {
|
|
|
1042
1082
|
opacity: object.opacity
|
|
1043
1083
|
});
|
|
1044
1084
|
}
|
|
1045
|
-
this.applyScene(replaceObject(
|
|
1085
|
+
this.applyScene(replaceObject(this.#state.scene, id, replacement), 'push');
|
|
1046
1086
|
this.setPending(id, null);
|
|
1047
|
-
|
|
1087
|
+
this.#setSelection([id], null);
|
|
1048
1088
|
return true;
|
|
1049
|
-
}
|
|
1089
|
+
}
|
|
1050
1090
|
/** Drops a card that was waiting for a widget and will never get one. */
|
|
1051
|
-
|
|
1052
|
-
if (!
|
|
1091
|
+
discardPending(id) {
|
|
1092
|
+
if (!this.#state.pending[id])
|
|
1053
1093
|
return false;
|
|
1054
1094
|
this.setPending(id, null);
|
|
1055
1095
|
return this.removeObjects([id]);
|
|
1056
|
-
}
|
|
1096
|
+
}
|
|
1057
1097
|
/** Every widget card that is on the board for good (one still being copied does not count). */
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
return
|
|
1061
|
-
};
|
|
1062
|
-
return EngineCore;
|
|
1063
|
-
}());
|
|
1064
|
-
export { EngineCore };
|
|
1065
|
-
_EngineCore_state = new WeakMap(), _EngineCore_rotationResetCancel = new WeakMap(), _EngineCore_listeners = new WeakMap(), _EngineCore_history = new WeakMap(), _EngineCore_clipboard = new WeakMap(), _EngineCore_pasteSteps = new WeakMap(), _EngineCore_nudgeMergeUntil = new WeakMap(), _EngineCore_textSession = new WeakMap(), _EngineCore_preferredTextFontFamily = new WeakMap(), _EngineCore_workspaceTextDefaults = new WeakMap(), _EngineCore_workspacePaintDefaults = new WeakMap(), _EngineCore_scribble = new WeakMap(), _EngineCore_indexes = new WeakMap(), _EngineCore_selectedShapesMemo = new WeakMap(), _EngineCore_instances = new WeakSet(), _EngineCore_set = function _EngineCore_set(patch) {
|
|
1066
|
-
var _this = this;
|
|
1067
|
-
var changed = false;
|
|
1068
|
-
for (var _i = 0, _a = Object.keys(patch); _i < _a.length; _i++) {
|
|
1069
|
-
var key = _a[_i];
|
|
1070
|
-
if (!Object.is(patch[key], __classPrivateFieldGet(this, _EngineCore_state, "f")[key])) {
|
|
1071
|
-
changed = true;
|
|
1072
|
-
break;
|
|
1073
|
-
}
|
|
1098
|
+
widgetCards() {
|
|
1099
|
+
const pending = this.#state.pending;
|
|
1100
|
+
return this.#state.scene.filter((object) => object.type === 'widget' && !pending[object.id]);
|
|
1074
1101
|
}
|
|
1075
|
-
|
|
1076
|
-
return;
|
|
1077
|
-
var before = __classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds;
|
|
1078
|
-
__classPrivateFieldSet(this, _EngineCore_state, __assign(__assign({}, __classPrivateFieldGet(this, _EngineCore_state, "f")), patch), "f");
|
|
1079
|
-
for (var _b = 0, _c = __spreadArray([], __classPrivateFieldGet(this, _EngineCore_listeners, "f"), true); _b < _c.length; _b++) {
|
|
1080
|
-
var listener = _c[_b];
|
|
1081
|
-
listener(__classPrivateFieldGet(this, _EngineCore_state, "f"));
|
|
1082
|
-
}
|
|
1083
|
-
if (__classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds !== before && !sameIds(__classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds, before)) {
|
|
1084
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_callHook).call(this, function () { var _a, _b; return (_b = (_a = _this.hooks).onSelectionChange) === null || _b === void 0 ? void 0 : _b.call(_a, __classPrivateFieldGet(_this, _EngineCore_state, "f").selectedIds); }, 'selection hook');
|
|
1085
|
-
}
|
|
1086
|
-
}, _EngineCore_callHook = function _EngineCore_callHook(run, name) {
|
|
1087
|
-
var _a, _b;
|
|
1088
|
-
try {
|
|
1089
|
-
run();
|
|
1090
|
-
}
|
|
1091
|
-
catch (error) {
|
|
1092
|
-
(_b = (_a = this.hooks).onError) === null || _b === void 0 ? void 0 : _b.call(_a, "The ".concat(name, " threw"), error);
|
|
1093
|
-
}
|
|
1094
|
-
}, _EngineCore_sceneForSave = function _EngineCore_sceneForSave() {
|
|
1095
|
-
var _a = __classPrivateFieldGet(this, _EngineCore_state, "f"), scene = _a.scene, pending = _a.pending, editingTextId = _a.editingTextId;
|
|
1096
|
-
var unwanted = new Set();
|
|
1097
|
-
for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
|
|
1098
|
-
var object = scene_1[_i];
|
|
1099
|
-
if (pending[object.id])
|
|
1100
|
-
unwanted.add(object.id);
|
|
1101
|
-
if (object.id === editingTextId && object.type === 'text' && object.text.trim() === '')
|
|
1102
|
-
unwanted.add(object.id);
|
|
1103
|
-
}
|
|
1104
|
-
if (unwanted.size === 0)
|
|
1105
|
-
return scene;
|
|
1106
|
-
return normalizeScene(scene.filter(function (object) { return !unwanted.has(object.id); }));
|
|
1107
|
-
}, _EngineCore_afterHistoryMove = function _EngineCore_afterHistoryMove(scene) {
|
|
1108
|
-
var _a, _b;
|
|
1109
|
-
__classPrivateFieldSet(this, _EngineCore_nudgeMergeUntil, 0, "f");
|
|
1110
|
-
this.applyScene(scene, 'transient');
|
|
1111
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_set).call(this, { canUndo: __classPrivateFieldGet(this, _EngineCore_history, "f").canUndo, canRedo: __classPrivateFieldGet(this, _EngineCore_history, "f").canRedo, overlay: EMPTY_OVERLAY });
|
|
1112
|
-
(_b = (_a = this.hooks).onDirty) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
1113
|
-
}, _EngineCore_setSelection = function _EngineCore_setSelection(selection, enteredGroupId) {
|
|
1114
|
-
if (enteredGroupId === void 0) { enteredGroupId = __classPrivateFieldGet(this, _EngineCore_state, "f").enteredGroupId; }
|
|
1115
|
-
var entered = enteredGroupId;
|
|
1116
|
-
if (entered) {
|
|
1117
|
-
var index_2 = this.sceneIndex();
|
|
1118
|
-
// Picking something outside the group that was entered leaves the group.
|
|
1119
|
-
var stillInside = selection.length === 0 || selection.every(function (id) { var _a; return ((_a = index_2.get(id)) === null || _a === void 0 ? void 0 : _a.parentId) === entered; });
|
|
1120
|
-
if (!stillInside)
|
|
1121
|
-
entered = null;
|
|
1122
|
-
}
|
|
1123
|
-
var next = sameIds(selection, __classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds) ? __classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds : Object.freeze(__spreadArray([], selection, true));
|
|
1124
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_set).call(this, { selectedIds: next, enteredGroupId: entered });
|
|
1125
|
-
}, _EngineCore_dropScribble = function _EngineCore_dropScribble() {
|
|
1126
|
-
__classPrivateFieldSet(this, _EngineCore_scribble, null, "f");
|
|
1127
|
-
return __classPrivateFieldGet(this, _EngineCore_state, "f").scribble === null ? {} : { scribble: null };
|
|
1128
|
-
}, _EngineCore_viewCenter = function _EngineCore_viewCenter() {
|
|
1129
|
-
var _a = __classPrivateFieldGet(this, _EngineCore_state, "f").viewSize, width = _a.width, height = _a.height;
|
|
1130
|
-
return { x: width / 2, y: height / 2 };
|
|
1131
|
-
}, _EngineCore_fittedViewport = function _EngineCore_fittedViewport(scene) {
|
|
1132
|
-
var bounds = boardVisualBounds(scene);
|
|
1133
|
-
if (!bounds)
|
|
1134
|
-
return __assign({}, INITIAL_VIEWPORT);
|
|
1135
|
-
return fitViewportToRect(bounds, __classPrivateFieldGet(this, _EngineCore_state, "f").viewSize, VIEW_FIT_PADDING);
|
|
1136
|
-
}, _EngineCore_fitText = function _EngineCore_fitText(object) {
|
|
1137
|
-
return object.type === 'text' ? fitTextObject(object, this.services.measureText) : object;
|
|
1138
|
-
}, _EngineCore_fitTexts = function _EngineCore_fitTexts(scene) {
|
|
1139
|
-
var _this = this;
|
|
1140
|
-
var changed = false;
|
|
1141
|
-
var next = scene.map(function (object) {
|
|
1142
|
-
if (object.type !== 'text')
|
|
1143
|
-
return object;
|
|
1144
|
-
var fitted = fitTextObject(object, _this.services.measureText);
|
|
1145
|
-
if (fitted !== object)
|
|
1146
|
-
changed = true;
|
|
1147
|
-
return fitted;
|
|
1148
|
-
});
|
|
1149
|
-
return changed ? next : scene;
|
|
1150
|
-
}, _EngineCore_arrange = function _EngineCore_arrange(change) {
|
|
1151
|
-
var scene = __classPrivateFieldGet(this, _EngineCore_state, "f").scene;
|
|
1152
|
-
var next = change(scene, __classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds);
|
|
1153
|
-
if (next === scene)
|
|
1154
|
-
return false;
|
|
1155
|
-
this.applyScene(next, 'push');
|
|
1156
|
-
return true;
|
|
1157
|
-
}, _EngineCore_insertCopies = function _EngineCore_insertCopies(source, rootIds, offset, mode) {
|
|
1158
|
-
var _this = this;
|
|
1159
|
-
var _a;
|
|
1160
|
-
if (mode === void 0) { mode = 'push'; }
|
|
1161
|
-
var _b = duplicateObjects(source, rootIds, offset, function (type) {
|
|
1162
|
-
return _this.services.createId(type === 'group' ? 'group' : 'obj');
|
|
1163
|
-
}), objects = _b.objects, idMap = _b.idMap;
|
|
1164
|
-
if (objects.length === 0)
|
|
1165
|
-
return [];
|
|
1166
|
-
var duplicatesWidgets = (_a = this.hooks.onWidgetDuplicated) !== null && _a !== void 0 ? _a : null;
|
|
1167
|
-
var pending = __assign({}, __classPrivateFieldGet(this, _EngineCore_state, "f").pending);
|
|
1168
|
-
var widgetClones = [];
|
|
1169
|
-
var prepared = objects.map(function (object) {
|
|
1170
|
-
if (object.type !== 'widget')
|
|
1171
|
-
return object;
|
|
1172
|
-
if (duplicatesWidgets) {
|
|
1173
|
-
// The card waits, unselected, for the host to make the widget behind it.
|
|
1174
|
-
pending[object.id] = 'duplicating';
|
|
1175
|
-
widgetClones.push({ sourceWidgetId: object.widgetId, clone: object });
|
|
1176
|
-
return object;
|
|
1177
|
-
}
|
|
1178
|
-
// With no host to copy the widget, a copy of a card is only a picture of it.
|
|
1179
|
-
return __classPrivateFieldGet(_this, _EngineCore_instances, "m", _EngineCore_flattenWidgetCard).call(_this, object);
|
|
1180
|
-
});
|
|
1181
|
-
// A copy of a member goes into the group of the original; if that group is gone it is repaired to the top level.
|
|
1182
|
-
this.applyScene(normalizeScene(__spreadArray(__spreadArray([], __classPrivateFieldGet(this, _EngineCore_state, "f").scene, true), prepared.map(function (object) { return __classPrivateFieldGet(_this, _EngineCore_instances, "m", _EngineCore_fitText).call(_this, object); }), true)), mode);
|
|
1183
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_set).call(this, { pending: Object.freeze(pending) });
|
|
1184
|
-
var rootCopyIds = rootIds.map(function (id) { return idMap.get(id); }).filter(function (id) { return !!id; });
|
|
1185
|
-
var selectable = rootCopyIds.filter(function (id) { return !pending[id]; });
|
|
1186
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_setSelection).call(this, selectable);
|
|
1187
|
-
var _loop_1 = function (sourceWidgetId, clone) {
|
|
1188
|
-
__classPrivateFieldGet(this_1, _EngineCore_instances, "m", _EngineCore_callHook).call(this_1, function () { return duplicatesWidgets === null || duplicatesWidgets === void 0 ? void 0 : duplicatesWidgets(sourceWidgetId, clone); }, 'widget duplicated hook');
|
|
1189
|
-
};
|
|
1190
|
-
var this_1 = this;
|
|
1191
|
-
for (var _i = 0, widgetClones_1 = widgetClones; _i < widgetClones_1.length; _i++) {
|
|
1192
|
-
var _c = widgetClones_1[_i], sourceWidgetId = _c.sourceWidgetId, clone = _c.clone;
|
|
1193
|
-
_loop_1(sourceWidgetId, clone);
|
|
1194
|
-
}
|
|
1195
|
-
return rootCopyIds;
|
|
1196
|
-
}, _EngineCore_flattenWidgetCard = function _EngineCore_flattenWidgetCard(card) {
|
|
1197
|
-
var box = { id: card.id, parentId: card.parentId, x: card.x, y: card.y, width: card.width, height: card.height, rotation: card.rotation, opacity: card.opacity };
|
|
1198
|
-
if (card.thumbnail)
|
|
1199
|
-
return createImageObject(__assign(__assign({}, box), { src: card.thumbnail, naturalWidth: card.width, naturalHeight: card.height }));
|
|
1200
|
-
return createRectObject(box);
|
|
1201
|
-
};
|
|
1102
|
+
}
|