@widgetic/canvas 0.5.45 → 0.5.46
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/README.md +1 -1
- 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 +19 -21
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.d.ts +6 -2
- package/dist/canvas/host/CanvasHost.js +1615 -1847
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/boardFiles.js +35 -51
- package/dist/canvas/host/browserServices.js +108 -196
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.js +18 -21
- package/dist/canvas/host/pageEvents.js +5 -5
- package/dist/canvas/host/pictureSaves.js +9 -11
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/storedPictures.js +149 -243
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/index.d.ts +1 -1
- 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 +4 -4
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/types.d.ts +7 -1
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +21 -21
- 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 +35 -41
- package/dist/canvas/view/CanvasStage.svelte +1 -1
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/canvas/view/liftedScene.js +24 -36
- package/dist/canvas/view/mountedScene.js +76 -79
- package/dist/components/CanvasBoardPlaceholder.svelte +68 -10
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +10 -2
- package/dist/components/CanvasToolbarPlaceholder.svelte +25 -11
- package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +6 -0
- package/dist/components/boardLoaderDelay.d.ts +39 -0
- package/dist/components/boardLoaderDelay.js +71 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +9 -2
- package/dist/engine/EngineCore.js +798 -907
- package/dist/engine/codec/canvasDocument.js +192 -155
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +79 -92
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +84 -96
- package/dist/engine/core/cameraMove.js +39 -51
- package/dist/engine/core/defaults.js +92 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.js +38 -55
- 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 +160 -224
- 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 +32 -65
- package/dist/engine/core/transform.js +84 -95
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +513 -526
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +267 -310
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +35 -39
- 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 +6 -17
- 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 +30 -83
- package/dist/fonts/workspaceFonts.js +33 -92
- 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/index.d.ts +1 -1
- package/dist/poster/boardPlacement.js +28 -30
- package/dist/poster/boardPoster.js +58 -83
- package/dist/poster/paintBoardPoster.js +16 -39
- package/dist/poster/posterPictures.js +27 -43
- package/dist/settings/rememberedTool.d.ts +50 -0
- package/dist/settings/rememberedTool.js +68 -0
- package/dist/settings/workspaceToolSettings.d.ts +8 -0
- package/dist/settings/workspaceToolSettings.js +35 -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 +137 -169
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/styleArguments.js +19 -21
- package/dist/tools/types.js +1 -1
- package/package.json +2 -2
|
@@ -10,85 +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 __rest = (this && this.__rest) || function (s, e) {
|
|
72
|
-
var t = {};
|
|
73
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
74
|
-
t[p] = s[p];
|
|
75
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
76
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
77
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
78
|
-
t[p[i]] = s[p[i]];
|
|
79
|
-
}
|
|
80
|
-
return t;
|
|
81
|
-
};
|
|
82
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
83
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
84
|
-
if (ar || !(i in from)) {
|
|
85
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
86
|
-
ar[i] = from[i];
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
90
|
-
};
|
|
91
|
-
var _EngineCore_instances, _EngineCore_state, _EngineCore_rotationResetCancel, _EngineCore_viewportMoveCancel, _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_writeViewport, _EngineCore_viewCenter, _EngineCore_fittedViewport, _EngineCore_fitText, _EngineCore_fitTexts, _EngineCore_arrange, _EngineCore_insertCopies, _EngineCore_flattenWidgetCard;
|
|
92
13
|
import { parseCanvasDocument, serializeCanvasDocument } from './codec/canvasDocument.js';
|
|
93
14
|
import { rotationAfterResetProgress, rotationResetProgress } from './core/rotationReset.js';
|
|
94
15
|
import { cameraOfViewport, createCameraPath, easeInOutCubic, viewportOfCamera, viewportsMatch } from './core/cameraMove.js';
|
|
@@ -107,17 +28,17 @@ import { INITIAL_VIEWPORT, clampZoom, fitViewportToRect, nextZoomStep, panViewpo
|
|
|
107
28
|
import { DEFAULT_WIDGET_RUNTIME, EMPTY_OVERLAY, MIN_PEN_OPACITY, createInitialState } from './state.js';
|
|
108
29
|
import { createFixedAdvanceMeasure, fitTextObject } from './text/layoutText.js';
|
|
109
30
|
/** How many undo steps the board keeps (as the canvas it replaces did). */
|
|
110
|
-
export
|
|
31
|
+
export const HISTORY_LIMIT = 50;
|
|
111
32
|
/** How far a duplicate or a repeated paste lands from the original, in world units. */
|
|
112
|
-
export
|
|
33
|
+
export const PASTE_OFFSET = 20;
|
|
113
34
|
/** Arrow-key nudges within this time of each other are one undo step. */
|
|
114
|
-
export
|
|
35
|
+
export const NUDGE_MERGE_MS = 700;
|
|
115
36
|
/** Room left around the board when it is fitted to the view, in screen pixels. */
|
|
116
|
-
export
|
|
37
|
+
export const VIEW_FIT_PADDING = 48;
|
|
117
38
|
/** The longest a text may be, in letters. */
|
|
118
|
-
export
|
|
39
|
+
export const MAX_TEXT_LENGTH = 100_000;
|
|
119
40
|
/** A shape turned by less than this (degrees) stands upright: there is nothing for a reset to do. */
|
|
120
|
-
export
|
|
41
|
+
export const UPRIGHT_TOLERANCE_DEGREES = 0.01;
|
|
121
42
|
/** The cursor a tool shows over the board when nothing is under the pointer. */
|
|
122
43
|
export function cursorForTool(tool) {
|
|
123
44
|
switch (tool) {
|
|
@@ -131,181 +52,229 @@ export function cursorForTool(tool) {
|
|
|
131
52
|
return 'crosshair';
|
|
132
53
|
}
|
|
133
54
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
_EngineCore_scribble.set(this, null);
|
|
162
|
-
_EngineCore_indexes.set(this, new WeakMap());
|
|
163
|
-
_EngineCore_selectedShapesMemo.set(this, null);
|
|
55
|
+
export class EngineCore {
|
|
56
|
+
/** What the host shell listens to; the shell sets its own functions here. */
|
|
57
|
+
hooks = {};
|
|
58
|
+
/** Whether a new shape is plain white with a grey outline (true) or gets a random colour. */
|
|
59
|
+
wireframeMode = true;
|
|
60
|
+
/** The last place the pointer was over the board, in world units: where a paste lands. */
|
|
61
|
+
lastPointerWorld = null;
|
|
62
|
+
services;
|
|
63
|
+
#state = createInitialState();
|
|
64
|
+
#rotationResetCancel = null;
|
|
65
|
+
/** Stops the move of the view that is on its way (see `flyTo`); null when the view is not moving by itself. */
|
|
66
|
+
#viewportMoveCancel = null;
|
|
67
|
+
#listeners = new Set();
|
|
68
|
+
#history;
|
|
69
|
+
#clipboard = null;
|
|
70
|
+
#pasteSteps = 0;
|
|
71
|
+
#nudgeMergeUntil = 0;
|
|
72
|
+
#textSession = null;
|
|
73
|
+
/** The last font family chosen in the panel; new text objects start with this instead of the catalog default. */
|
|
74
|
+
#preferredTextFontFamily = null;
|
|
75
|
+
#workspaceTextDefaults = null;
|
|
76
|
+
#workspacePaintDefaults = null;
|
|
77
|
+
// The eraser's trail while it lives; what the views draw of it is `state.scribble`.
|
|
78
|
+
#scribble = null;
|
|
79
|
+
#indexes = new WeakMap();
|
|
80
|
+
#selectedShapesMemo = null;
|
|
81
|
+
constructor(services = {}) {
|
|
164
82
|
this.services = {
|
|
165
|
-
createId:
|
|
166
|
-
measureText:
|
|
167
|
-
now:
|
|
168
|
-
random:
|
|
169
|
-
log:
|
|
170
|
-
prefersReducedMotion:
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
83
|
+
createId: services.createId ?? ((prefix) => createObjectId(prefix)),
|
|
84
|
+
measureText: services.measureText ?? createFixedAdvanceMeasure(),
|
|
85
|
+
now: services.now ?? (() => Date.now()),
|
|
86
|
+
random: services.random ?? Math.random,
|
|
87
|
+
log: services.log ?? (() => { }),
|
|
88
|
+
prefersReducedMotion: services.prefersReducedMotion ??
|
|
89
|
+
(() => typeof globalThis.matchMedia === 'function' && globalThis.matchMedia('(prefers-reduced-motion: reduce)').matches),
|
|
90
|
+
requestAnimationFrame: services.requestAnimationFrame ??
|
|
91
|
+
((callback) => {
|
|
92
|
+
if (typeof globalThis.requestAnimationFrame === 'function') {
|
|
93
|
+
const id = globalThis.requestAnimationFrame(callback);
|
|
94
|
+
return () => globalThis.cancelAnimationFrame?.(id);
|
|
95
|
+
}
|
|
96
|
+
const id = globalThis.setTimeout(() => callback(this.services.now()), 16);
|
|
97
|
+
return () => globalThis.clearTimeout(id);
|
|
98
|
+
})
|
|
179
99
|
};
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
},
|
|
187
|
-
enumerable: false,
|
|
188
|
-
configurable: true
|
|
189
|
-
});
|
|
100
|
+
this.#history = new HistoryStack(this.#state.scene, { limit: HISTORY_LIMIT });
|
|
101
|
+
}
|
|
102
|
+
// ──────────────────────────────────────────────────────── state and subscriptions
|
|
103
|
+
get state() {
|
|
104
|
+
return this.#state;
|
|
105
|
+
}
|
|
190
106
|
/** Calls the listener after every change of the state. Returns the function that stops it. */
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
__classPrivateFieldGet(_this, _EngineCore_listeners, "f").delete(listener);
|
|
107
|
+
subscribe(listener) {
|
|
108
|
+
this.#listeners.add(listener);
|
|
109
|
+
return () => {
|
|
110
|
+
this.#listeners.delete(listener);
|
|
196
111
|
};
|
|
197
|
-
}
|
|
112
|
+
}
|
|
198
113
|
/** Lets go of every listener and hook (the board was removed from the page). */
|
|
199
|
-
|
|
114
|
+
dispose() {
|
|
200
115
|
this.cancelRotationReset();
|
|
201
116
|
this.cancelViewportMove();
|
|
202
|
-
|
|
117
|
+
this.#listeners.clear();
|
|
203
118
|
this.hooks = {};
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
119
|
+
this.#textSession = null;
|
|
120
|
+
this.#scribble = null;
|
|
121
|
+
}
|
|
207
122
|
/** Stops an upright animation in progress and records the angle on screen as one undo step. */
|
|
208
|
-
|
|
209
|
-
if (!
|
|
123
|
+
cancelRotationReset() {
|
|
124
|
+
if (!this.#rotationResetCancel)
|
|
210
125
|
return;
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
126
|
+
this.#rotationResetCancel();
|
|
127
|
+
this.#rotationResetCancel = null;
|
|
128
|
+
}
|
|
129
|
+
#set(patch) {
|
|
130
|
+
let changed = false;
|
|
131
|
+
for (const key of Object.keys(patch)) {
|
|
132
|
+
if (!Object.is(patch[key], this.#state[key])) {
|
|
133
|
+
changed = true;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (!changed)
|
|
138
|
+
return;
|
|
139
|
+
const before = this.#state.selectedIds;
|
|
140
|
+
this.#state = { ...this.#state, ...patch };
|
|
141
|
+
for (const listener of [...this.#listeners])
|
|
142
|
+
listener(this.#state);
|
|
143
|
+
if (this.#state.selectedIds !== before && !sameIds(this.#state.selectedIds, before)) {
|
|
144
|
+
this.#callHook(() => this.hooks.onSelectionChange?.(this.#state.selectedIds), 'selection hook');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
#callHook(run, name) {
|
|
148
|
+
try {
|
|
149
|
+
run();
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
this.hooks.onError?.(`The ${name} threw`, error);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
214
155
|
/** The objects of the scene by id. Built once per scene and kept while the scene is the same array. */
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
var index = __classPrivateFieldGet(this, _EngineCore_indexes, "f").get(scene);
|
|
156
|
+
sceneIndex(scene = this.#state.scene) {
|
|
157
|
+
let index = this.#indexes.get(scene);
|
|
218
158
|
if (!index) {
|
|
219
159
|
index = indexScene(scene);
|
|
220
|
-
|
|
160
|
+
this.#indexes.set(scene, index);
|
|
221
161
|
}
|
|
222
162
|
return index;
|
|
223
|
-
}
|
|
224
|
-
|
|
163
|
+
}
|
|
164
|
+
getObject(id) {
|
|
225
165
|
return this.sceneIndex().get(id);
|
|
226
|
-
}
|
|
166
|
+
}
|
|
227
167
|
/** The objects that are selected (a group counts once). */
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
for (
|
|
232
|
-
|
|
233
|
-
var object = index.get(id);
|
|
168
|
+
selectedObjects() {
|
|
169
|
+
const index = this.sceneIndex();
|
|
170
|
+
const objects = [];
|
|
171
|
+
for (const id of this.#state.selectedIds) {
|
|
172
|
+
const object = index.get(id);
|
|
234
173
|
if (object)
|
|
235
174
|
objects.push(object);
|
|
236
175
|
}
|
|
237
176
|
return objects;
|
|
238
|
-
}
|
|
177
|
+
}
|
|
239
178
|
/**
|
|
240
179
|
* The shapes the selection draws: its members when a group is selected. Found once for a scene and a selection and kept
|
|
241
180
|
* while they are the same ones: the frame, the handles and the panel each ask at every step of a drag.
|
|
242
181
|
*/
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
182
|
+
selectedShapes() {
|
|
183
|
+
const { scene, selectedIds } = this.#state;
|
|
184
|
+
const memo = this.#selectedShapesMemo;
|
|
246
185
|
if (memo && memo.scene === scene && memo.selectedIds === selectedIds)
|
|
247
186
|
return memo.shapes;
|
|
248
|
-
|
|
249
|
-
|
|
187
|
+
const shapes = leafShapesOf(scene, selectedIds);
|
|
188
|
+
this.#selectedShapesMemo = { scene, selectedIds, shapes };
|
|
250
189
|
return shapes;
|
|
251
|
-
}
|
|
190
|
+
}
|
|
252
191
|
/** The box the selection handles surround, or null when nothing is selected. */
|
|
253
|
-
|
|
192
|
+
selectionFrame() {
|
|
254
193
|
return selectionFrameOf(this.selectedShapes());
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
return
|
|
258
|
-
}
|
|
194
|
+
}
|
|
195
|
+
isEmpty() {
|
|
196
|
+
return this.#state.scene.length === 0;
|
|
197
|
+
}
|
|
259
198
|
// ──────────────────────────────────────────────────────── loading and saving
|
|
260
199
|
/** Shows a canvas that was read from saved data. History starts again from it, and it is not "changed". */
|
|
261
|
-
|
|
262
|
-
if (options === void 0) { options = {}; }
|
|
200
|
+
load(parsed, options = {}) {
|
|
263
201
|
// A new board has its own view: a move that was on its way to a place of the old board is over.
|
|
264
202
|
this.cancelViewportMove();
|
|
265
|
-
|
|
203
|
+
this.#textSession = null;
|
|
266
204
|
// The texts are laid out first: an arrow tied to one rests on the box its letters make.
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
205
|
+
const scene = reconcileArrowBindings(this.#fitTexts(parsed.scene));
|
|
206
|
+
this.#history.reset(scene);
|
|
207
|
+
const viewport = options.fitView ? this.#fittedViewport(scene) : parsed.viewport;
|
|
208
|
+
this.#set({
|
|
209
|
+
...this.#dropScribble(),
|
|
210
|
+
scene,
|
|
211
|
+
viewport,
|
|
212
|
+
selectedIds: [],
|
|
213
|
+
enteredGroupId: null,
|
|
214
|
+
hoverId: null,
|
|
215
|
+
editingTextId: null,
|
|
216
|
+
overlay: EMPTY_OVERLAY,
|
|
217
|
+
pending: Object.freeze({}),
|
|
218
|
+
hoverWidgetId: null,
|
|
219
|
+
canUndo: false,
|
|
220
|
+
canRedo: false,
|
|
221
|
+
revision: this.#state.revision + 1
|
|
222
|
+
});
|
|
223
|
+
}
|
|
272
224
|
/** Reads saved data (text or an object) and shows it. Never throws: a bad document loads as an empty board. */
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
var parsed = parseCanvasDocument(input);
|
|
225
|
+
loadDocument(input, options = {}) {
|
|
226
|
+
const parsed = parseCanvasDocument(input);
|
|
276
227
|
this.load(parsed, options);
|
|
277
228
|
return parsed;
|
|
278
|
-
}
|
|
229
|
+
}
|
|
279
230
|
/** The board as text to store. A card still waiting for its widget, and a text nobody typed in, are left out. */
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
231
|
+
serialize(options = {}) {
|
|
232
|
+
return serializeCanvasDocument(this.#sceneForSave(), this.#state.viewport, {
|
|
233
|
+
now: new Date(this.services.now()),
|
|
234
|
+
...options
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
#sceneForSave() {
|
|
238
|
+
const { scene, pending, editingTextId } = this.#state;
|
|
239
|
+
const unwanted = new Set();
|
|
240
|
+
for (const object of scene) {
|
|
241
|
+
if (pending[object.id])
|
|
242
|
+
unwanted.add(object.id);
|
|
243
|
+
if (object.id === editingTextId && object.type === 'text' && object.text.trim() === '')
|
|
244
|
+
unwanted.add(object.id);
|
|
245
|
+
}
|
|
246
|
+
if (unwanted.size === 0)
|
|
247
|
+
return scene;
|
|
248
|
+
return normalizeScene(scene.filter((object) => !unwanted.has(object.id)));
|
|
249
|
+
}
|
|
284
250
|
/** Forgets what can be undone (after the host replaced the board). */
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
251
|
+
resetHistory() {
|
|
252
|
+
this.#history.reset(this.#state.scene);
|
|
253
|
+
this.#set({ canUndo: false, canRedo: false });
|
|
254
|
+
}
|
|
255
|
+
setLoading(loading) {
|
|
256
|
+
this.#set({ loading });
|
|
257
|
+
}
|
|
292
258
|
/**
|
|
293
259
|
* Tells the engine how big the board element is. With `keepCentre` the view stays on the same point of the world: when
|
|
294
260
|
* 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
|
|
295
261
|
* that its page resizes by dragging its edges). The first measure (from 0 by 0) never moves the pan, whatever `keepCentre` says.
|
|
296
262
|
*/
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
var current = __classPrivateFieldGet(this, _EngineCore_state, "f").viewSize;
|
|
263
|
+
setViewSize(size, keepCentre = false) {
|
|
264
|
+
const current = this.#state.viewSize;
|
|
300
265
|
if (current.width === size.width && current.height === size.height)
|
|
301
266
|
return;
|
|
302
|
-
|
|
267
|
+
const patch = { viewSize: { width: size.width, height: size.height } };
|
|
303
268
|
if (keepCentre && current.width > 0 && current.height > 0 && size.width > 0 && size.height > 0) {
|
|
304
|
-
|
|
305
|
-
patch.viewport =
|
|
269
|
+
const viewport = this.#state.viewport;
|
|
270
|
+
patch.viewport = {
|
|
271
|
+
...viewport,
|
|
272
|
+
x: viewport.x + (size.width - current.width) / 2,
|
|
273
|
+
y: viewport.y + (size.height - current.height) / 2
|
|
274
|
+
};
|
|
306
275
|
}
|
|
307
|
-
|
|
308
|
-
}
|
|
276
|
+
this.#set(patch);
|
|
277
|
+
}
|
|
309
278
|
// ──────────────────────────────────────────────────────── scene changes
|
|
310
279
|
/**
|
|
311
280
|
* Puts a new scene on the board. `transient` is for the frames of a gesture (nothing is recorded), `push`
|
|
@@ -314,56 +283,51 @@ var EngineCore = /** @class */ (function () {
|
|
|
314
283
|
* Whatever the change was (a drag, a stretch, a word typed, an undo), the arrows that are tied to shapes are drawn again from
|
|
315
284
|
* where their shapes are now, in the same scene and the same step: nothing else has to know about the ties.
|
|
316
285
|
*/
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
var previous = __classPrivateFieldGet(this, _EngineCore_state, "f").scene;
|
|
321
|
-
var next = reconcileArrowBindings(requested);
|
|
286
|
+
applyScene(requested, mode = 'push') {
|
|
287
|
+
const previous = this.#state.scene;
|
|
288
|
+
const next = reconcileArrowBindings(requested);
|
|
322
289
|
if (next === previous)
|
|
323
290
|
return mode === 'push' ? this.commit() : false;
|
|
324
|
-
|
|
291
|
+
const patch = { scene: next, revision: this.#state.revision + 1 };
|
|
325
292
|
if (mode !== 'transient' || next.length !== previous.length) {
|
|
326
293
|
// Keep everything that points at an object pointing at one that is still there.
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (selection.length !==
|
|
294
|
+
const index = this.sceneIndex(next);
|
|
295
|
+
const selection = this.#state.selectedIds.filter((id) => index.has(id));
|
|
296
|
+
if (selection.length !== this.#state.selectedIds.length)
|
|
330
297
|
patch.selectedIds = selection;
|
|
331
|
-
if (
|
|
298
|
+
if (this.#state.hoverId && !index.has(this.#state.hoverId))
|
|
332
299
|
patch.hoverId = null;
|
|
333
|
-
if (
|
|
300
|
+
if (this.#state.enteredGroupId && !index.has(this.#state.enteredGroupId))
|
|
334
301
|
patch.enteredGroupId = null;
|
|
335
|
-
if (
|
|
302
|
+
if (this.#state.editingTextId && !index.has(this.#state.editingTextId))
|
|
336
303
|
patch.editingTextId = null;
|
|
337
|
-
|
|
338
|
-
if (pendingIds.some(
|
|
339
|
-
patch.pending = Object.freeze(Object.fromEntries(Object.entries(
|
|
340
|
-
var id = _a[0];
|
|
341
|
-
return index_1.has(id);
|
|
342
|
-
})));
|
|
304
|
+
const pendingIds = Object.keys(this.#state.pending);
|
|
305
|
+
if (pendingIds.some((id) => !index.has(id))) {
|
|
306
|
+
patch.pending = Object.freeze(Object.fromEntries(Object.entries(this.#state.pending).filter(([id]) => index.has(id))));
|
|
343
307
|
}
|
|
344
308
|
}
|
|
345
309
|
if (mode === 'replace')
|
|
346
|
-
|
|
347
|
-
|
|
310
|
+
this.#history.replaceCurrent(next);
|
|
311
|
+
this.#set(patch);
|
|
348
312
|
if (mode === 'push')
|
|
349
313
|
this.commit();
|
|
350
314
|
else if (mode === 'replace') {
|
|
351
|
-
|
|
352
|
-
|
|
315
|
+
this.#nudgeMergeUntil = 0;
|
|
316
|
+
this.hooks.onDirty?.();
|
|
353
317
|
}
|
|
354
318
|
return true;
|
|
355
|
-
}
|
|
319
|
+
}
|
|
356
320
|
/**
|
|
357
321
|
* Puts a scene on the board with no undo step and without telling the host that the board changed: a repair
|
|
358
322
|
* of what was loaded (a name filled in), not an edit.
|
|
359
323
|
*/
|
|
360
|
-
|
|
361
|
-
if (next ===
|
|
324
|
+
applySilently(next) {
|
|
325
|
+
if (next === this.#state.scene)
|
|
362
326
|
return;
|
|
363
327
|
this.applyScene(next, 'transient');
|
|
364
328
|
// What the board holds now (the arrows tied to shapes drawn again), not the scene that was asked for.
|
|
365
|
-
|
|
366
|
-
}
|
|
329
|
+
this.#history.replaceCurrent(this.#state.scene);
|
|
330
|
+
}
|
|
367
331
|
/**
|
|
368
332
|
* Fills in what was loaded late (a picture that arrived after the board was opened and used) in the board and in every step of
|
|
369
333
|
* its history, with no undo step and without telling the host that the board changed: a repair of what was loaded, not an edit.
|
|
@@ -374,354 +338,372 @@ var EngineCore = /** @class */ (function () {
|
|
|
374
338
|
* of its own (the board is then not any step), and it would be written into the step the gesture started from, so that Escape
|
|
375
339
|
* could not take it back; and the steps before this one would still be without the picture, so an undo would take it away.
|
|
376
340
|
*/
|
|
377
|
-
|
|
341
|
+
repairLoaded(repair) {
|
|
378
342
|
// At rest the board is the current step itself; during a gesture it is not.
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
if (next ===
|
|
343
|
+
const atRest = this.#history.current === this.#state.scene;
|
|
344
|
+
this.#history.mapAll(repair);
|
|
345
|
+
const next = atRest ? this.#history.current : repair(this.#state.scene);
|
|
346
|
+
if (next === this.#state.scene)
|
|
383
347
|
return;
|
|
384
348
|
this.applyScene(next, 'transient');
|
|
385
349
|
// What the board holds now (the arrows tied to shapes drawn again), so that the board stays the very same list as its step.
|
|
386
350
|
if (atRest)
|
|
387
|
-
|
|
388
|
-
}
|
|
351
|
+
this.#history.replaceCurrent(this.#state.scene);
|
|
352
|
+
}
|
|
389
353
|
/** Records the scene as it is now as one undo step (the end of a gesture). False when nothing changed since the last step. */
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (!__classPrivateFieldGet(this, _EngineCore_history, "f").push(__classPrivateFieldGet(this, _EngineCore_state, "f").scene))
|
|
354
|
+
commit() {
|
|
355
|
+
this.#nudgeMergeUntil = 0;
|
|
356
|
+
if (!this.#history.push(this.#state.scene))
|
|
394
357
|
return false;
|
|
395
|
-
|
|
396
|
-
|
|
358
|
+
this.#set({ canUndo: this.#history.canUndo, canRedo: this.#history.canRedo });
|
|
359
|
+
this.hooks.onDirty?.();
|
|
397
360
|
return true;
|
|
398
|
-
}
|
|
361
|
+
}
|
|
399
362
|
/** Throws away what a gesture did and goes back to the last step (Escape in the middle of a drag). */
|
|
400
|
-
|
|
401
|
-
this.applyScene(
|
|
402
|
-
|
|
403
|
-
}
|
|
404
|
-
|
|
363
|
+
revertToCommitted() {
|
|
364
|
+
this.applyScene(this.#history.current, 'transient');
|
|
365
|
+
this.#set({ overlay: EMPTY_OVERLAY });
|
|
366
|
+
}
|
|
367
|
+
undo() {
|
|
405
368
|
this.endTextEdit();
|
|
406
369
|
// An upright turn still on its way ends here as one step, so that this undo takes that step back.
|
|
407
370
|
this.cancelRotationReset();
|
|
408
|
-
|
|
371
|
+
const previous = this.#history.undo();
|
|
409
372
|
if (!previous)
|
|
410
373
|
return false;
|
|
411
|
-
|
|
374
|
+
this.#afterHistoryMove(previous);
|
|
412
375
|
return true;
|
|
413
|
-
}
|
|
414
|
-
|
|
376
|
+
}
|
|
377
|
+
redo() {
|
|
415
378
|
this.endTextEdit();
|
|
416
379
|
this.cancelRotationReset();
|
|
417
|
-
|
|
380
|
+
const next = this.#history.redo();
|
|
418
381
|
if (!next)
|
|
419
382
|
return false;
|
|
420
|
-
|
|
383
|
+
this.#afterHistoryMove(next);
|
|
421
384
|
return true;
|
|
422
|
-
}
|
|
385
|
+
}
|
|
386
|
+
#afterHistoryMove(scene) {
|
|
387
|
+
this.#nudgeMergeUntil = 0;
|
|
388
|
+
this.applyScene(scene, 'transient');
|
|
389
|
+
this.#set({ canUndo: this.#history.canUndo, canRedo: this.#history.canRedo, overlay: EMPTY_OVERLAY });
|
|
390
|
+
this.hooks.onDirty?.();
|
|
391
|
+
}
|
|
423
392
|
/**
|
|
424
393
|
* Adds objects on top of the board in one step, except that a drawing goes under the widget cards (a card is added on top).
|
|
425
394
|
* Returns their ids.
|
|
426
395
|
*/
|
|
427
|
-
|
|
428
|
-
var _this = this;
|
|
429
|
-
if (options === void 0) { options = {}; }
|
|
396
|
+
addObjects(objects, options = {}) {
|
|
430
397
|
if (objects.length === 0)
|
|
431
398
|
return [];
|
|
432
|
-
|
|
433
|
-
this.applyScene(addObjectsBehindWidgetCards(
|
|
434
|
-
|
|
399
|
+
const fitted = objects.map((object) => this.#fitText(object));
|
|
400
|
+
this.applyScene(addObjectsBehindWidgetCards(this.#state.scene, fitted), 'push');
|
|
401
|
+
const ids = fitted.map((object) => object.id);
|
|
435
402
|
if (options.select)
|
|
436
403
|
this.select(ids);
|
|
437
404
|
return ids;
|
|
438
|
-
}
|
|
405
|
+
}
|
|
439
406
|
/** Removes objects (and what is inside them) without asking anyone. */
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
if (next ===
|
|
407
|
+
removeObjects(ids) {
|
|
408
|
+
const next = removeFromScene(this.#state.scene, ids);
|
|
409
|
+
if (next === this.#state.scene)
|
|
443
410
|
return false;
|
|
444
411
|
this.applyScene(next, 'push');
|
|
445
412
|
return true;
|
|
446
|
-
}
|
|
413
|
+
}
|
|
447
414
|
/** Deletes objects, first asking the host about each widget card among them. Resolves false when the host said no. */
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
confirm = this.hooks.confirmWidgetDeletion;
|
|
460
|
-
if (!confirm) return [3 /*break*/, 7];
|
|
461
|
-
_i = 0, widgets_1 = widgets;
|
|
462
|
-
_c.label = 1;
|
|
463
|
-
case 1:
|
|
464
|
-
if (!(_i < widgets_1.length)) return [3 /*break*/, 7];
|
|
465
|
-
widget = widgets_1[_i];
|
|
466
|
-
allowed = true;
|
|
467
|
-
_c.label = 2;
|
|
468
|
-
case 2:
|
|
469
|
-
_c.trys.push([2, 4, , 5]);
|
|
470
|
-
return [4 /*yield*/, confirm(widget.widgetId)];
|
|
471
|
-
case 3:
|
|
472
|
-
allowed = (_c.sent()) !== false;
|
|
473
|
-
return [3 /*break*/, 5];
|
|
474
|
-
case 4:
|
|
475
|
-
error_1 = _c.sent();
|
|
476
|
-
allowed = false;
|
|
477
|
-
(_b = (_a = this.hooks).onError) === null || _b === void 0 ? void 0 : _b.call(_a, 'The widget deletion check threw', error_1);
|
|
478
|
-
return [3 /*break*/, 5];
|
|
479
|
-
case 5:
|
|
480
|
-
if (!allowed)
|
|
481
|
-
return [2 /*return*/, false];
|
|
482
|
-
_c.label = 6;
|
|
483
|
-
case 6:
|
|
484
|
-
_i++;
|
|
485
|
-
return [3 /*break*/, 1];
|
|
486
|
-
case 7: return [2 /*return*/, this.removeObjects(wanted)];
|
|
415
|
+
async deleteObjects(ids) {
|
|
416
|
+
const wanted = [...ids];
|
|
417
|
+
if (wanted.length === 0)
|
|
418
|
+
return false;
|
|
419
|
+
const widgets = leafShapesOf(this.#state.scene, wanted).filter((shape) => shape.type === 'widget');
|
|
420
|
+
const confirm = this.hooks.confirmWidgetDeletion;
|
|
421
|
+
if (confirm) {
|
|
422
|
+
for (const widget of widgets) {
|
|
423
|
+
let allowed = true;
|
|
424
|
+
try {
|
|
425
|
+
allowed = (await confirm(widget.widgetId)) !== false;
|
|
487
426
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
427
|
+
catch (error) {
|
|
428
|
+
allowed = false;
|
|
429
|
+
this.hooks.onError?.('The widget deletion check threw', error);
|
|
430
|
+
}
|
|
431
|
+
if (!allowed)
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const removed = this.removeObjects(wanted);
|
|
436
|
+
this.#tellHostWhenPersonEmptiedBoard(removed);
|
|
437
|
+
return removed;
|
|
438
|
+
}
|
|
439
|
+
async deleteSelection() {
|
|
440
|
+
return this.deleteObjects(this.#state.selectedIds);
|
|
441
|
+
}
|
|
498
442
|
/** Empties the board in one undoable step and tells the host. */
|
|
499
|
-
|
|
500
|
-
var _this = this;
|
|
443
|
+
clearAll() {
|
|
501
444
|
this.endTextEdit();
|
|
502
|
-
if (
|
|
445
|
+
if (this.#state.scene.length === 0)
|
|
503
446
|
return;
|
|
504
447
|
this.applyScene([], 'push');
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
448
|
+
this.#set({ selectedIds: [], enteredGroupId: null, hoverId: null });
|
|
449
|
+
this.#callHook(() => this.hooks.onCleared?.('clear-all'), 'cleared hook');
|
|
450
|
+
}
|
|
451
|
+
/** After what the person removed (delete, cut): when it took the last object off the board, the host hears `onCleared('deleted')`. */
|
|
452
|
+
#tellHostWhenPersonEmptiedBoard(removed) {
|
|
453
|
+
if (!removed || this.#state.scene.length > 0)
|
|
454
|
+
return;
|
|
455
|
+
this.#callHook(() => this.hooks.onCleared?.('deleted'), 'cleared hook');
|
|
456
|
+
}
|
|
508
457
|
// ──────────────────────────────────────────────────────── selection
|
|
509
458
|
/** Selects units (a group counts once). Everything else is deselected unless `additive`. */
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
};
|
|
459
|
+
select(ids, options = {}) {
|
|
460
|
+
const scene = this.#state.scene;
|
|
461
|
+
const requested = options.additive ? [...this.#state.selectedIds, ...ids] : [...ids];
|
|
462
|
+
const selection = normalizeSelection(scene, requested);
|
|
463
|
+
this.#setSelection(selection);
|
|
464
|
+
}
|
|
517
465
|
/** Adds the unit to the selection, or takes it out when it is in (Shift-click). */
|
|
518
|
-
|
|
519
|
-
|
|
466
|
+
toggleSelection(id) {
|
|
467
|
+
const current = this.#state.selectedIds;
|
|
520
468
|
if (current.includes(id))
|
|
521
|
-
|
|
469
|
+
this.#setSelection(current.filter((candidate) => candidate !== id));
|
|
522
470
|
else
|
|
523
471
|
this.select([id], { additive: true });
|
|
524
|
-
}
|
|
472
|
+
}
|
|
525
473
|
/** Deselects everything and leaves the group that was entered. */
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
}
|
|
474
|
+
clearSelection() {
|
|
475
|
+
this.#setSelection([], null);
|
|
476
|
+
}
|
|
477
|
+
selectAll() {
|
|
478
|
+
const scene = this.#state.scene;
|
|
479
|
+
const entered = this.#state.enteredGroupId;
|
|
480
|
+
const units = scene.filter((object) => (entered ? object.parentId === entered : object.parentId === null));
|
|
481
|
+
this.#setSelection(units.map((object) => object.id), entered);
|
|
482
|
+
}
|
|
483
|
+
#setSelection(selection, enteredGroupId = this.#state.enteredGroupId) {
|
|
484
|
+
let entered = enteredGroupId;
|
|
485
|
+
if (entered) {
|
|
486
|
+
const index = this.sceneIndex();
|
|
487
|
+
// Picking something outside the group that was entered leaves the group.
|
|
488
|
+
const stillInside = selection.length === 0 || selection.every((id) => index.get(id)?.parentId === entered);
|
|
489
|
+
if (!stillInside)
|
|
490
|
+
entered = null;
|
|
491
|
+
}
|
|
492
|
+
const next = sameIds(selection, this.#state.selectedIds) ? this.#state.selectedIds : Object.freeze([...selection]);
|
|
493
|
+
this.#set({ selectedIds: next, enteredGroupId: entered });
|
|
494
|
+
}
|
|
535
495
|
/** Makes the members of a group pickable one by one, and selects one of them. */
|
|
536
|
-
|
|
537
|
-
|
|
496
|
+
enterGroup(groupId, selectId) {
|
|
497
|
+
const group = this.getObject(groupId);
|
|
538
498
|
if (!group || group.type !== 'group')
|
|
539
499
|
return;
|
|
540
|
-
|
|
541
|
-
}
|
|
500
|
+
this.#set({ enteredGroupId: groupId, selectedIds: selectId ? [selectId] : [] });
|
|
501
|
+
}
|
|
542
502
|
/** Goes back out of the entered group and selects the group itself. */
|
|
543
|
-
|
|
544
|
-
|
|
503
|
+
exitGroup() {
|
|
504
|
+
const entered = this.#state.enteredGroupId;
|
|
545
505
|
if (!entered)
|
|
546
506
|
return;
|
|
547
|
-
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
}
|
|
507
|
+
this.#set({ enteredGroupId: null, selectedIds: [entered] });
|
|
508
|
+
}
|
|
509
|
+
setHover(id) {
|
|
510
|
+
this.#set({ hoverId: id });
|
|
511
|
+
}
|
|
552
512
|
/** The pointer is over the turning dot of the selection (or left it): the dot grows while it is. */
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
}
|
|
513
|
+
setRotationDotHover(hovered) {
|
|
514
|
+
this.#set({ rotationDotHovered: hovered });
|
|
515
|
+
}
|
|
556
516
|
/** The pointer is over (or is dragging) an end or the middle of the selected arrow, or left them: that handle grows. */
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
}
|
|
517
|
+
setArrowHandleHover(handle) {
|
|
518
|
+
this.#set({ arrowHandleHovered: handle });
|
|
519
|
+
}
|
|
560
520
|
/** The unit under a world point. */
|
|
561
|
-
|
|
562
|
-
|
|
521
|
+
hitTest(worldPoint, ignore) {
|
|
522
|
+
const state = this.#state;
|
|
563
523
|
return hitTestPoint(state.scene, worldPoint, {
|
|
564
524
|
zoom: state.viewport.zoom,
|
|
565
525
|
enteredGroupId: state.enteredGroupId,
|
|
566
526
|
index: this.sceneIndex(),
|
|
567
|
-
ignore
|
|
527
|
+
ignore
|
|
568
528
|
});
|
|
569
|
-
}
|
|
529
|
+
}
|
|
570
530
|
/** Every unit under a world point, the lowest first (what an Alt-click steps through). */
|
|
571
|
-
|
|
572
|
-
|
|
531
|
+
hitTestStack(worldPoint) {
|
|
532
|
+
const state = this.#state;
|
|
573
533
|
return hitTestStack(state.scene, worldPoint, {
|
|
574
534
|
zoom: state.viewport.zoom,
|
|
575
535
|
enteredGroupId: state.enteredGroupId,
|
|
576
536
|
index: this.sceneIndex()
|
|
577
537
|
});
|
|
578
|
-
}
|
|
538
|
+
}
|
|
579
539
|
/** The units a rectangle of the world takes. */
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
var state = __classPrivateFieldGet(this, _EngineCore_state, "f");
|
|
540
|
+
hitTestRect(rect, mode = 'intersect', ignore) {
|
|
541
|
+
const state = this.#state;
|
|
583
542
|
return hitTestRect(state.scene, rect, {
|
|
584
543
|
zoom: state.viewport.zoom,
|
|
585
544
|
enteredGroupId: state.enteredGroupId,
|
|
586
545
|
index: this.sceneIndex(),
|
|
587
|
-
mode
|
|
588
|
-
ignore
|
|
546
|
+
mode,
|
|
547
|
+
ignore
|
|
589
548
|
});
|
|
590
|
-
}
|
|
549
|
+
}
|
|
591
550
|
// ──────────────────────────────────────────────────────── overlay and cursor
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
for (
|
|
596
|
-
|
|
597
|
-
if (!Object.is(patch[key], __classPrivateFieldGet(this, _EngineCore_state, "f").overlay[key]))
|
|
551
|
+
setOverlay(patch) {
|
|
552
|
+
const next = { ...this.#state.overlay, ...patch };
|
|
553
|
+
let changed = false;
|
|
554
|
+
for (const key of Object.keys(patch)) {
|
|
555
|
+
if (!Object.is(patch[key], this.#state.overlay[key]))
|
|
598
556
|
changed = true;
|
|
599
557
|
}
|
|
600
558
|
if (changed)
|
|
601
|
-
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
}
|
|
559
|
+
this.#set({ overlay: next });
|
|
560
|
+
}
|
|
561
|
+
clearOverlay() {
|
|
562
|
+
this.#set({ overlay: EMPTY_OVERLAY });
|
|
563
|
+
}
|
|
606
564
|
// ──────────────────────────────────────────────────────── the eraser's trail
|
|
607
565
|
/** The eraser's trail begins (the pointer went past the drag threshold). The host's frame clock runs while `state.scribble` is not null. */
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
}
|
|
566
|
+
beginScribble() {
|
|
567
|
+
this.#scribble = new Scribble();
|
|
568
|
+
this.#set({ scribble: this.#scribble.view() });
|
|
569
|
+
}
|
|
612
570
|
/** The pointer reached a place of the board (world units); the next step of the trail takes it in. */
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
};
|
|
571
|
+
extendScribble(world) {
|
|
572
|
+
this.#scribble?.add(world, this.#state.viewport.zoom);
|
|
573
|
+
}
|
|
617
574
|
/** The pointer let go: the trail is eaten away over the next frames. */
|
|
618
|
-
|
|
619
|
-
|
|
575
|
+
stopScribble() {
|
|
576
|
+
const scribble = this.#scribble;
|
|
620
577
|
if (!scribble)
|
|
621
578
|
return;
|
|
622
579
|
scribble.stop();
|
|
623
580
|
// The end of a finished line is drawn capped: the views are told that this one is finished.
|
|
624
|
-
|
|
625
|
-
}
|
|
581
|
+
this.#set({ scribble: scribble.view() });
|
|
582
|
+
}
|
|
626
583
|
/**
|
|
627
584
|
* One frame of the trail's life, `elapsedMs` after the last one. Returns false when there is no trail any more (the
|
|
628
585
|
* host stops its clock), true while it lives.
|
|
629
586
|
*/
|
|
630
|
-
|
|
631
|
-
|
|
587
|
+
stepScribble(elapsedMs) {
|
|
588
|
+
const scribble = this.#scribble;
|
|
632
589
|
if (!scribble) {
|
|
633
|
-
if (
|
|
634
|
-
|
|
590
|
+
if (this.#state.scribble !== null)
|
|
591
|
+
this.#set({ scribble: null });
|
|
635
592
|
return false;
|
|
636
593
|
}
|
|
637
|
-
|
|
594
|
+
const changed = scribble.step(elapsedMs);
|
|
638
595
|
if (scribble.finished) {
|
|
639
|
-
|
|
640
|
-
|
|
596
|
+
this.#scribble = null;
|
|
597
|
+
this.#set({ scribble: null });
|
|
641
598
|
return false;
|
|
642
599
|
}
|
|
643
600
|
if (changed)
|
|
644
|
-
|
|
601
|
+
this.#set({ scribble: scribble.view() });
|
|
645
602
|
return true;
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
603
|
+
}
|
|
604
|
+
/** Takes the trail away at once (the tool changed, the board was replaced). */
|
|
605
|
+
#dropScribble() {
|
|
606
|
+
this.#scribble = null;
|
|
607
|
+
return this.#state.scribble === null ? {} : { scribble: null };
|
|
608
|
+
}
|
|
609
|
+
setCursor(cursor) {
|
|
610
|
+
this.#set({ cursor });
|
|
611
|
+
}
|
|
650
612
|
// ──────────────────────────────────────────────────────── tools
|
|
651
|
-
|
|
652
|
-
var _a;
|
|
653
|
-
if (options === void 0) { options = {}; }
|
|
613
|
+
setTool(tool, options = {}) {
|
|
654
614
|
this.endTextEdit();
|
|
655
|
-
|
|
615
|
+
const patch = {
|
|
616
|
+
...this.#dropScribble(),
|
|
617
|
+
tool,
|
|
618
|
+
overlay: EMPTY_OVERLAY,
|
|
619
|
+
cursor: cursorForTool(tool),
|
|
620
|
+
hoverId: null,
|
|
621
|
+
rotationDotHovered: false,
|
|
622
|
+
arrowHandleHovered: null,
|
|
623
|
+
hoverWidgetId: null
|
|
624
|
+
};
|
|
656
625
|
if (tool === 'library-shape')
|
|
657
|
-
patch.libraryShapeKey =
|
|
626
|
+
patch.libraryShapeKey = options.libraryShapeKey ?? this.#state.libraryShapeKey;
|
|
658
627
|
if (tool !== 'select' && tool !== 'pan' && !options.keepSelection) {
|
|
659
628
|
patch.selectedIds = [];
|
|
660
629
|
patch.enteredGroupId = null;
|
|
661
630
|
}
|
|
662
|
-
|
|
663
|
-
}
|
|
631
|
+
this.#set(patch);
|
|
632
|
+
}
|
|
664
633
|
/** Back to the Select tool, unless the person asked for the tool to stay. */
|
|
665
|
-
|
|
666
|
-
if (
|
|
667
|
-
if (__classPrivateFieldGet(this, _EngineCore_state, "f").stickyTool && !options.force)
|
|
634
|
+
revertToSelect(options = {}) {
|
|
635
|
+
if (this.#state.stickyTool && !options.force)
|
|
668
636
|
return;
|
|
669
|
-
if (
|
|
637
|
+
if (this.#state.tool !== 'select')
|
|
670
638
|
this.setTool('select', { keepSelection: true });
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
}
|
|
639
|
+
}
|
|
640
|
+
setStickyTool(sticky) {
|
|
641
|
+
this.#set({ stickyTool: sticky });
|
|
642
|
+
}
|
|
675
643
|
// ──────────────────────────────────────────────────────── viewport
|
|
676
644
|
/** Puts the view where it is asked at once. A move of the view that was on its way (see `flyTo`) is over: the view is the person's. */
|
|
677
|
-
|
|
645
|
+
setViewport(viewport) {
|
|
678
646
|
this.cancelViewportMove();
|
|
679
|
-
|
|
680
|
-
}
|
|
681
|
-
|
|
647
|
+
this.#writeViewport(viewport);
|
|
648
|
+
}
|
|
649
|
+
/** Writes the view without ending a move in progress: the one way the move itself puts the view where the frame says. */
|
|
650
|
+
#writeViewport(viewport) {
|
|
651
|
+
const next = sanitizeViewport(viewport);
|
|
652
|
+
const current = this.#state.viewport;
|
|
653
|
+
if (next.x === current.x && next.y === current.y && next.zoom === current.zoom)
|
|
654
|
+
return;
|
|
655
|
+
this.#set({ viewport: next });
|
|
656
|
+
}
|
|
657
|
+
panBy(deltaX, deltaY) {
|
|
682
658
|
if (deltaX === 0 && deltaY === 0)
|
|
683
659
|
return;
|
|
684
|
-
this.setViewport(panViewport(
|
|
685
|
-
}
|
|
660
|
+
this.setViewport(panViewport(this.#state.viewport, deltaX, deltaY));
|
|
661
|
+
}
|
|
686
662
|
/** Zooms and keeps the world point under `screenPoint` where it is. */
|
|
687
|
-
|
|
688
|
-
this.setViewport(zoomAtPoint(
|
|
689
|
-
}
|
|
663
|
+
zoomAt(screenPoint, zoom) {
|
|
664
|
+
this.setViewport(zoomAtPoint(this.#state.viewport, screenPoint, zoom));
|
|
665
|
+
}
|
|
666
|
+
#viewCenter() {
|
|
667
|
+
const { width, height } = this.#state.viewSize;
|
|
668
|
+
return { x: width / 2, y: height / 2 };
|
|
669
|
+
}
|
|
690
670
|
/** One step up or down the zoom ladder, about the centre of the view. */
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
this.zoomAt(
|
|
694
|
-
}
|
|
671
|
+
zoomStep(direction) {
|
|
672
|
+
const viewport = this.#state.viewport;
|
|
673
|
+
this.zoomAt(this.#viewCenter(), nextZoomStep(viewport.zoom, direction));
|
|
674
|
+
}
|
|
695
675
|
/** A zoom level (1 is 100%), about the centre of the view. */
|
|
696
|
-
|
|
697
|
-
this.zoomAt(
|
|
698
|
-
}
|
|
676
|
+
zoomTo(zoom) {
|
|
677
|
+
this.zoomAt(this.#viewCenter(), clampZoom(zoom));
|
|
678
|
+
}
|
|
699
679
|
/** 100%, with the world origin where it was at the centre of the view. */
|
|
700
|
-
|
|
680
|
+
zoomReset() {
|
|
701
681
|
this.zoomTo(1);
|
|
702
|
-
}
|
|
682
|
+
}
|
|
683
|
+
#fittedViewport(scene) {
|
|
684
|
+
const bounds = boardVisualBounds(scene);
|
|
685
|
+
if (!bounds)
|
|
686
|
+
return { ...INITIAL_VIEWPORT };
|
|
687
|
+
return fitViewportToRect(bounds, this.#state.viewSize, VIEW_FIT_PADDING);
|
|
688
|
+
}
|
|
703
689
|
/** Shows everything on the board. An empty board goes back to the start. */
|
|
704
|
-
|
|
705
|
-
this.setViewport(
|
|
706
|
-
}
|
|
690
|
+
zoomToFit() {
|
|
691
|
+
this.setViewport(this.#fittedViewport(this.#state.scene));
|
|
692
|
+
}
|
|
707
693
|
/** Shows the selection, or everything when nothing is selected. */
|
|
708
|
-
|
|
709
|
-
|
|
694
|
+
zoomToSelection() {
|
|
695
|
+
const bounds = boundsOfObjects(this.#state.scene, this.#state.selectedIds);
|
|
710
696
|
if (!bounds) {
|
|
711
697
|
this.zoomToFit();
|
|
712
698
|
return;
|
|
713
699
|
}
|
|
714
|
-
this.setViewport(fitViewportToRect(bounds,
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
},
|
|
722
|
-
enumerable: false,
|
|
723
|
-
configurable: true
|
|
724
|
-
});
|
|
700
|
+
this.setViewport(fitViewportToRect(bounds, this.#state.viewSize, VIEW_FIT_PADDING, 2));
|
|
701
|
+
}
|
|
702
|
+
// ──────────────────────────────────────────────────────── moving the view with an ease
|
|
703
|
+
/** Whether the view is on its way to a place (a move started by `flyTo` or one of the other `flyTo…` calls has not ended). */
|
|
704
|
+
get isViewMoving() {
|
|
705
|
+
return this.#viewportMoveCancel !== null;
|
|
706
|
+
}
|
|
725
707
|
/**
|
|
726
708
|
* Eases the view to a place: the point that ends in the middle of the board and the zoom change together, on one progress,
|
|
727
709
|
* so the move is a single motion and not a pan followed by a zoom, and both arrive at the same moment. With `options.path` set
|
|
@@ -734,62 +716,59 @@ var EngineCore = /** @class */ (function () {
|
|
|
734
716
|
*
|
|
735
717
|
* Returns whether the view moved (or jumped); false when it is there already or the place is not a place.
|
|
736
718
|
*/
|
|
737
|
-
|
|
738
|
-
var _this = this;
|
|
739
|
-
var _a, _b, _c, _d;
|
|
740
|
-
if (options === void 0) { options = {}; }
|
|
719
|
+
flyTo(target, options = {}) {
|
|
741
720
|
// A move on its way is over: this one starts from where the view is now.
|
|
742
721
|
this.cancelViewportMove();
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
722
|
+
const viewSize = this.#state.viewSize;
|
|
723
|
+
const startView = cameraOfViewport(this.#state.viewport, viewSize);
|
|
724
|
+
const endView = {
|
|
746
725
|
center: { x: target.center.x, y: target.center.y },
|
|
747
|
-
zoom: clampZoom(
|
|
726
|
+
zoom: clampZoom(target.zoom ?? startView.zoom)
|
|
748
727
|
};
|
|
749
728
|
// A board that is not on the page yet has no middle to put a place in, and a place that is not a point is the caller's mistake:
|
|
750
729
|
// the view stays as it is.
|
|
751
730
|
if (!(viewSize.width > 0) || !(viewSize.height > 0) || !Number.isFinite(endView.center.x) || !Number.isFinite(endView.center.y)) {
|
|
752
|
-
|
|
731
|
+
this.#callHook(() => options.onEnd?.(false), 'view move end callback');
|
|
753
732
|
return false;
|
|
754
733
|
}
|
|
755
|
-
|
|
756
|
-
if (viewportsMatch(endViewport,
|
|
734
|
+
const endViewport = viewportOfCamera(endView, viewSize);
|
|
735
|
+
if (viewportsMatch(endViewport, this.#state.viewport)) {
|
|
757
736
|
// Nothing to move, and nothing to wait for.
|
|
758
|
-
|
|
737
|
+
this.#callHook(() => options.onEnd?.(true), 'view move end callback');
|
|
759
738
|
return false;
|
|
760
739
|
}
|
|
761
|
-
|
|
740
|
+
const path = createCameraPath(startView, endView, viewSize, options.path);
|
|
762
741
|
// The time of the move: the one given, or the one its length gives, made quicker or slower by `durationScale` (which has to be a number above 0).
|
|
763
|
-
|
|
764
|
-
|
|
742
|
+
const durationScale = typeof options.durationScale === 'number' && Number.isFinite(options.durationScale) && options.durationScale > 0 ? options.durationScale : 1;
|
|
743
|
+
const duration = options.duration ?? (path?.duration ?? 0) * durationScale;
|
|
765
744
|
if (!path || !(duration > 0) || this.services.prefersReducedMotion()) {
|
|
766
745
|
// No time to move in, or a person who asked their system for no motion: the view is in its place at once.
|
|
767
|
-
|
|
768
|
-
|
|
746
|
+
this.#writeViewport(endViewport);
|
|
747
|
+
this.#callHook(() => options.onEnd?.(true), 'view move end callback');
|
|
769
748
|
return true;
|
|
770
749
|
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
750
|
+
const easing = options.easing ?? easeInOutCubic;
|
|
751
|
+
const startedAt = this.services.now();
|
|
752
|
+
let cancelFrame = null;
|
|
753
|
+
let over = false;
|
|
754
|
+
const finish = (arrived) => {
|
|
776
755
|
if (over)
|
|
777
756
|
return;
|
|
778
757
|
over = true;
|
|
779
|
-
cancelFrame
|
|
780
|
-
if (
|
|
781
|
-
|
|
782
|
-
|
|
758
|
+
cancelFrame?.();
|
|
759
|
+
if (this.#viewportMoveCancel === stopMove)
|
|
760
|
+
this.#viewportMoveCancel = null;
|
|
761
|
+
this.#callHook(() => options.onEnd?.(arrived), 'view move end callback');
|
|
783
762
|
};
|
|
784
|
-
|
|
785
|
-
|
|
763
|
+
const stopMove = () => finish(false);
|
|
764
|
+
const advance = () => {
|
|
786
765
|
if (over)
|
|
787
766
|
return;
|
|
788
|
-
|
|
789
|
-
|
|
767
|
+
const timeProgress = Math.min(1, Math.max(0, (this.services.now() - startedAt) / duration));
|
|
768
|
+
const arrived = timeProgress >= 1;
|
|
790
769
|
// The board is measured now, not when the move began, and the last frame is the place itself, not a step close to it.
|
|
791
|
-
|
|
792
|
-
|
|
770
|
+
const view = arrived ? endView : path.at(easing(timeProgress));
|
|
771
|
+
this.#writeViewport(viewportOfCamera(view, this.#state.viewSize));
|
|
793
772
|
// What listens to the view may have taken it in the meantime (a call to `setViewport` from a listener ends the move).
|
|
794
773
|
if (over)
|
|
795
774
|
return;
|
|
@@ -797,83 +776,97 @@ var EngineCore = /** @class */ (function () {
|
|
|
797
776
|
finish(true);
|
|
798
777
|
return;
|
|
799
778
|
}
|
|
800
|
-
cancelFrame =
|
|
779
|
+
cancelFrame = this.services.requestAnimationFrame(advance);
|
|
801
780
|
};
|
|
802
|
-
|
|
781
|
+
this.#viewportMoveCancel = stopMove;
|
|
803
782
|
cancelFrame = this.services.requestAnimationFrame(advance);
|
|
804
783
|
return true;
|
|
805
|
-
}
|
|
784
|
+
}
|
|
806
785
|
/**
|
|
807
786
|
* Eases the view to some objects (groups count as one, as in a selection): the middle of the objects goes to the middle of the
|
|
808
787
|
* board while the zoom changes to what `options.zoom` asks (the objects fill the board unless it says otherwise).
|
|
809
788
|
* False when none of the ids is an object of the board.
|
|
810
789
|
*/
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
var bounds = boundsOfObjects(__classPrivateFieldGet(this, _EngineCore_state, "f").scene, ids);
|
|
790
|
+
flyToObjects(ids, options = {}) {
|
|
791
|
+
const { zoom = 'fit', ...moveOptions } = options;
|
|
792
|
+
const bounds = boundsOfObjects(this.#state.scene, ids);
|
|
815
793
|
if (!bounds)
|
|
816
794
|
return false;
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
return this.flyTo({ center
|
|
820
|
-
}
|
|
795
|
+
const center = { x: bounds.x + bounds.width / 2, y: bounds.y + bounds.height / 2 };
|
|
796
|
+
const targetZoom = zoom === 'keep' ? undefined : zoom === 'fit' ? fitViewportToRect(bounds, this.#state.viewSize, VIEW_FIT_PADDING, 2).zoom : zoom;
|
|
797
|
+
return this.flyTo({ center, zoom: targetZoom }, moveOptions);
|
|
798
|
+
}
|
|
821
799
|
/**
|
|
822
800
|
* Eases the view back to 100% with the middle of everything that is drawn in the middle of the board (a double click on the
|
|
823
801
|
* empty board does this). An empty board goes back to where a new board starts.
|
|
824
802
|
*/
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
var center = bounds
|
|
803
|
+
flyToContent(options = {}) {
|
|
804
|
+
const bounds = boardVisualBounds(this.#state.scene);
|
|
805
|
+
const center = bounds
|
|
829
806
|
? { x: bounds.x + bounds.width / 2, y: bounds.y + bounds.height / 2 }
|
|
830
|
-
: cameraOfViewport(INITIAL_VIEWPORT,
|
|
831
|
-
return this.flyTo({ center
|
|
832
|
-
}
|
|
807
|
+
: cameraOfViewport(INITIAL_VIEWPORT, this.#state.viewSize).center;
|
|
808
|
+
return this.flyTo({ center, zoom: 1 }, options);
|
|
809
|
+
}
|
|
833
810
|
/** Ends a move of the view where it is (the view keeps the place it had reached) and tells its `onEnd` that it did not arrive. */
|
|
834
|
-
|
|
835
|
-
|
|
811
|
+
cancelViewportMove() {
|
|
812
|
+
const stopMove = this.#viewportMoveCancel;
|
|
836
813
|
if (!stopMove)
|
|
837
814
|
return;
|
|
838
|
-
|
|
815
|
+
this.#viewportMoveCancel = null;
|
|
839
816
|
stopMove();
|
|
840
|
-
}
|
|
817
|
+
}
|
|
841
818
|
/** The rectangle of the world that is on screen. */
|
|
842
|
-
|
|
843
|
-
return visibleWorldRect(
|
|
844
|
-
}
|
|
819
|
+
visibleRect() {
|
|
820
|
+
return visibleWorldRect(this.#state.viewport, this.#state.viewSize);
|
|
821
|
+
}
|
|
845
822
|
/** The point of the world at a point of the board element. */
|
|
846
|
-
|
|
847
|
-
return screenToWorld(
|
|
848
|
-
}
|
|
823
|
+
toWorld(screenPoint) {
|
|
824
|
+
return screenToWorld(this.#state.viewport, screenPoint);
|
|
825
|
+
}
|
|
849
826
|
/** The middle of what is on screen, in world units (where a button puts a new object). */
|
|
850
|
-
|
|
851
|
-
|
|
827
|
+
visibleCenter() {
|
|
828
|
+
const rect = this.visibleRect();
|
|
852
829
|
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
853
|
-
}
|
|
830
|
+
}
|
|
831
|
+
// ──────────────────────────────────────────────────────── text
|
|
832
|
+
#fitText(object) {
|
|
833
|
+
return object.type === 'text' ? fitTextObject(object, this.services.measureText) : object;
|
|
834
|
+
}
|
|
835
|
+
/** Lays out every text of a scene again (after fonts loaded, a file was read, or a style changed). */
|
|
836
|
+
#fitTexts(scene) {
|
|
837
|
+
let changed = false;
|
|
838
|
+
const next = scene.map((object) => {
|
|
839
|
+
if (object.type !== 'text')
|
|
840
|
+
return object;
|
|
841
|
+
const fitted = fitTextObject(object, this.services.measureText);
|
|
842
|
+
if (fitted !== object)
|
|
843
|
+
changed = true;
|
|
844
|
+
return fitted;
|
|
845
|
+
});
|
|
846
|
+
return changed ? next : scene;
|
|
847
|
+
}
|
|
854
848
|
/** Fonts finished loading: letters changed width, so every text is laid out again. */
|
|
855
|
-
|
|
849
|
+
notifyFontsChanged() {
|
|
856
850
|
// Not a change the person made: no undo step, and nothing is saved because of it.
|
|
857
|
-
|
|
858
|
-
|
|
851
|
+
const committed = this.#history.current;
|
|
852
|
+
const refitCommitted = reconcileArrowBindings(this.#fitTexts(committed));
|
|
859
853
|
if (refitCommitted !== committed)
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
if (next !==
|
|
854
|
+
this.#history.replaceCurrent(refitCommitted);
|
|
855
|
+
this.#set({ fontRevision: this.#state.fontRevision + 1 });
|
|
856
|
+
const next = this.#fitTexts(this.#state.scene);
|
|
857
|
+
if (next !== this.#state.scene)
|
|
864
858
|
this.applyScene(next, 'transient');
|
|
865
|
-
}
|
|
859
|
+
}
|
|
866
860
|
/** Remembers the font family for the next text object the person places. */
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
};
|
|
861
|
+
setPreferredTextFontFamily(fontFamily) {
|
|
862
|
+
this.#preferredTextFontFamily = fontFamily;
|
|
863
|
+
}
|
|
864
|
+
setWorkspaceToolDefaults(settings) {
|
|
865
|
+
this.#workspaceTextDefaults = settings.text ?? null;
|
|
866
|
+
this.#workspacePaintDefaults = settings.paint ?? null;
|
|
867
|
+
if (settings.text?.fontFamily)
|
|
868
|
+
this.#preferredTextFontFamily = settings.text.fontFamily;
|
|
869
|
+
}
|
|
877
870
|
/**
|
|
878
871
|
* Changes how the Draw tool draws: the lines drawn from now on take the colour, the width and the opacity that are asked for. A key
|
|
879
872
|
* that is left out stays as it was; a value that cannot be a line's (a width under 1 or above 200, an opacity out of range, a
|
|
@@ -881,25 +874,30 @@ var EngineCore = /** @class */ (function () {
|
|
|
881
874
|
* at all. Nothing on the board changes (that is `setStyle`, for the selection), and nothing is added to the history. Returns
|
|
882
875
|
* whether the pen came out different.
|
|
883
876
|
*/
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
877
|
+
setPenStyle(patch) {
|
|
878
|
+
const current = this.#state.penStyle;
|
|
879
|
+
const color = typeof patch.color === 'string' && patch.color.trim() !== '' ? patch.color.trim() : current.color;
|
|
880
|
+
const width = typeof patch.width === 'number' && Number.isFinite(patch.width) ? clampNumber(patch.width, MIN_BRUSH_WIDTH, MAX_STROKE_WIDTH) : current.width;
|
|
881
|
+
const opacity = typeof patch.opacity === 'number' && Number.isFinite(patch.opacity) ? clampNumber(patch.opacity, MIN_PEN_OPACITY, 1) : current.opacity;
|
|
889
882
|
if (color === current.color && width === current.width && opacity === current.opacity)
|
|
890
883
|
return false;
|
|
891
|
-
|
|
884
|
+
this.#set({ penStyle: Object.freeze({ color, width, opacity }) });
|
|
892
885
|
return true;
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
if (!
|
|
886
|
+
}
|
|
887
|
+
workspacePaintForNewShape(wireframe) {
|
|
888
|
+
if (!this.#workspacePaintDefaults)
|
|
896
889
|
return {};
|
|
897
890
|
if (wireframe) {
|
|
898
|
-
|
|
899
|
-
return
|
|
891
|
+
const { stroke, strokeWidth, strokeStyle, opacity } = this.#workspacePaintDefaults;
|
|
892
|
+
return {
|
|
893
|
+
...(stroke !== undefined ? { stroke } : {}),
|
|
894
|
+
...(strokeWidth !== undefined ? { strokeWidth } : {}),
|
|
895
|
+
...(strokeStyle !== undefined ? { strokeStyle } : {}),
|
|
896
|
+
...(opacity !== undefined ? { opacity } : {})
|
|
897
|
+
};
|
|
900
898
|
}
|
|
901
|
-
return
|
|
902
|
-
}
|
|
899
|
+
return { ...this.#workspacePaintDefaults };
|
|
900
|
+
}
|
|
903
901
|
/**
|
|
904
902
|
* Makes a text object, puts it on the board and starts editing it. A click makes a label whose width follows its
|
|
905
903
|
* text; a width makes a box whose lines wrap. Nothing is recorded until the editing ends with text in it.
|
|
@@ -908,56 +906,58 @@ var EngineCore = /** @class */ (function () {
|
|
|
908
906
|
* prompt) is centred on `point` instead of starting there, and a press elsewhere on the board while it is still empty keeps the
|
|
909
907
|
* tool, so that press (or drag) places the text there (see `endTextEdit`).
|
|
910
908
|
*/
|
|
911
|
-
|
|
912
|
-
var _a, _b, _c;
|
|
913
|
-
if (options === void 0) { options = {}; }
|
|
909
|
+
createText(point, options = {}) {
|
|
914
910
|
this.endTextEdit();
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
911
|
+
const fixedWidth = options.width !== undefined && options.width > TEXT_DEFAULTS.minimumWidth;
|
|
912
|
+
const base = createTextObject({
|
|
913
|
+
id: this.services.createId('obj'),
|
|
914
|
+
x: point.x,
|
|
915
|
+
y: point.y,
|
|
916
|
+
width: fixedWidth ? options.width : TEXT_DEFAULTS.minimumWidth,
|
|
917
|
+
autoWidth: !fixedWidth,
|
|
918
|
+
fontFamily: this.#preferredTextFontFamily ?? this.#workspaceTextDefaults?.fontFamily ?? TEXT_DEFAULTS.fontFamily,
|
|
919
|
+
...this.#workspaceTextDefaults
|
|
920
|
+
});
|
|
921
|
+
const fitted = this.#fitText(base);
|
|
918
922
|
// The placeholder is centred on the point it is asked for, whatever its size (a bigger font makes a bigger prompt).
|
|
919
|
-
|
|
920
|
-
|
|
923
|
+
const text = options.placeholder ? { ...fitted, x: point.x - fitted.width / 2, y: point.y - fitted.height / 2 } : fitted;
|
|
924
|
+
const sceneBefore = this.#state.scene;
|
|
921
925
|
// A text typed after a widget stands goes behind it, like every other drawing.
|
|
922
926
|
this.applyScene(addObjectsBehindWidgetCards(sceneBefore, [text]), 'transient');
|
|
923
|
-
|
|
924
|
-
|
|
927
|
+
this.#textSession = { id: text.id, isNew: true, sceneBefore, typed: false, placeholder: options.placeholder === true };
|
|
928
|
+
this.#set({ editingTextId: text.id, selectedIds: [text.id], enteredGroupId: null });
|
|
925
929
|
return text.id;
|
|
926
|
-
}
|
|
930
|
+
}
|
|
927
931
|
/** Starts editing the text of an existing text object. */
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
var object = this.getObject(id);
|
|
932
|
+
beginTextEdit(id) {
|
|
933
|
+
const object = this.getObject(id);
|
|
931
934
|
if (!object || object.type !== 'text')
|
|
932
935
|
return false;
|
|
933
|
-
if (
|
|
936
|
+
if (this.#textSession?.id === id)
|
|
934
937
|
return true;
|
|
935
938
|
this.endTextEdit();
|
|
936
|
-
|
|
937
|
-
|
|
939
|
+
this.#textSession = { id, isNew: false, sceneBefore: this.#state.scene, typed: false, placeholder: false };
|
|
940
|
+
this.#set({ editingTextId: id, selectedIds: [id] });
|
|
938
941
|
return true;
|
|
939
|
-
}
|
|
942
|
+
}
|
|
940
943
|
/** The text the person has typed so far; the box is laid out again on every call. */
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
var session = __classPrivateFieldGet(this, _EngineCore_textSession, "f");
|
|
944
|
+
updateEditingText(text) {
|
|
945
|
+
const session = this.#textSession;
|
|
944
946
|
if (!session)
|
|
945
947
|
return;
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
});
|
|
953
|
-
var changed = next.some(function (object, position) { return object !== scene[position]; });
|
|
948
|
+
const clipped = text.length > MAX_TEXT_LENGTH ? text.slice(0, MAX_TEXT_LENGTH) : text;
|
|
949
|
+
const scene = this.#state.scene;
|
|
950
|
+
const next = scene.map((object) => object.id === session.id && object.type === 'text' && object.text !== clipped
|
|
951
|
+
? fitTextObject({ ...object, text: clipped }, this.services.measureText)
|
|
952
|
+
: object);
|
|
953
|
+
const changed = next.some((object, position) => object !== scene[position]);
|
|
954
954
|
if (!changed)
|
|
955
955
|
return;
|
|
956
956
|
this.applyScene(next, 'transient');
|
|
957
957
|
session.typed = true;
|
|
958
958
|
// The board is not recorded until the typing ends, but the letters are on it: the host's next save takes them.
|
|
959
|
-
|
|
960
|
-
}
|
|
959
|
+
this.#callHook(() => this.hooks.onDraftChange?.(), 'draft change hook');
|
|
960
|
+
}
|
|
961
961
|
/**
|
|
962
962
|
* Finishes the edit. An empty text is removed (a new one leaves nothing behind, not even an undo step);
|
|
963
963
|
* otherwise creation and typing together are one undo step.
|
|
@@ -966,30 +966,28 @@ var EngineCore = /** @class */ (function () {
|
|
|
966
966
|
* put on the board (see `createText`), the tool is not left, so the press goes on to be a press of the Text tool and puts the
|
|
967
967
|
* text where the person pressed. Any other end of the edit leaves the Text tool, as before.
|
|
968
968
|
*/
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
if (options === void 0) { options = {}; }
|
|
972
|
-
var session = __classPrivateFieldGet(this, _EngineCore_textSession, "f");
|
|
969
|
+
endTextEdit(options = {}) {
|
|
970
|
+
const session = this.#textSession;
|
|
973
971
|
if (!session)
|
|
974
972
|
return null;
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
973
|
+
this.#textSession = null;
|
|
974
|
+
const object = this.getObject(session.id);
|
|
975
|
+
this.#set({ editingTextId: null });
|
|
976
|
+
let kept = null;
|
|
979
977
|
// The placeholder of the Text tool ended without a letter in it.
|
|
980
|
-
|
|
978
|
+
let emptyPlaceholderDropped = false;
|
|
981
979
|
if (object && object.type === 'text') {
|
|
982
980
|
if (object.text.trim() === '') {
|
|
983
981
|
if (session.isNew) {
|
|
984
982
|
this.applyScene(session.sceneBefore, 'transient');
|
|
985
|
-
|
|
983
|
+
this.#set({ selectedIds: [] });
|
|
986
984
|
emptyPlaceholderDropped = session.placeholder;
|
|
987
985
|
// A save may have taken the letters that were typed before they were all deleted: it has to be told they are gone.
|
|
988
986
|
if (session.typed)
|
|
989
|
-
|
|
987
|
+
this.#callHook(() => this.hooks.onDraftChange?.(), 'draft change hook');
|
|
990
988
|
}
|
|
991
989
|
else {
|
|
992
|
-
this.applyScene(removeFromScene(
|
|
990
|
+
this.applyScene(removeFromScene(this.#state.scene, [session.id]), 'push');
|
|
993
991
|
}
|
|
994
992
|
}
|
|
995
993
|
else {
|
|
@@ -998,260 +996,307 @@ var EngineCore = /** @class */ (function () {
|
|
|
998
996
|
}
|
|
999
997
|
}
|
|
1000
998
|
// The Text tool is used once: the board goes back to Select when the typing is over (unless the tool is kept).
|
|
1001
|
-
|
|
1002
|
-
if (
|
|
999
|
+
const keepsTool = this.#state.stickyTool || (options.keepPlaceholderTool === true && emptyPlaceholderDropped);
|
|
1000
|
+
if (this.#state.tool === 'text' && !keepsTool)
|
|
1003
1001
|
this.setTool('select', { keepSelection: true });
|
|
1004
1002
|
return kept;
|
|
1005
|
-
}
|
|
1003
|
+
}
|
|
1006
1004
|
// ──────────────────────────────────────────────────────── commands on the selection
|
|
1007
1005
|
/** Changes how the selection looks. `commit: false` is for a slider that is still being dragged: call `commit()` when it ends. */
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
var changed = false;
|
|
1014
|
-
var next = scene.map(function (object) {
|
|
1006
|
+
setStyle(patch, options = {}) {
|
|
1007
|
+
const scene = this.#state.scene;
|
|
1008
|
+
const targetIds = withDescendantIds(scene, this.#state.selectedIds);
|
|
1009
|
+
let changed = false;
|
|
1010
|
+
const next = scene.map((object) => {
|
|
1015
1011
|
if (!targetIds.has(object.id) || object.type === 'group')
|
|
1016
1012
|
return object;
|
|
1017
|
-
|
|
1013
|
+
const styled = applyStylePatch(object, patch);
|
|
1018
1014
|
if (styled === object)
|
|
1019
1015
|
return object;
|
|
1020
1016
|
changed = true;
|
|
1021
|
-
return
|
|
1017
|
+
return this.#fitText(styled);
|
|
1022
1018
|
});
|
|
1023
1019
|
if (!changed)
|
|
1024
1020
|
return false;
|
|
1025
1021
|
this.applyScene(next, options.commit === false ? 'transient' : 'push');
|
|
1026
1022
|
return true;
|
|
1027
|
-
}
|
|
1023
|
+
}
|
|
1028
1024
|
/**
|
|
1029
1025
|
* Turns the one selected shape back upright, about its own middle (a double click on the turning dot). One undo step.
|
|
1030
1026
|
* False when the selection is not a single shape, or the shape stands upright already.
|
|
1031
1027
|
*/
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
var shapes = this.selectedShapes();
|
|
1028
|
+
resetSelectionRotation() {
|
|
1029
|
+
const shapes = this.selectedShapes();
|
|
1035
1030
|
if (shapes.length !== 1 || Math.abs(shapes[0].rotation) < UPRIGHT_TOLERANCE_DEGREES)
|
|
1036
1031
|
return false;
|
|
1037
1032
|
this.cancelRotationReset();
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1033
|
+
const turned = shapes[0];
|
|
1034
|
+
const startRotation = turned.rotation;
|
|
1035
|
+
const startAt = this.services.now();
|
|
1036
|
+
let committed = false;
|
|
1037
|
+
let cancelFrame = null;
|
|
1038
|
+
const commitCurrent = () => {
|
|
1044
1039
|
if (committed)
|
|
1045
1040
|
return;
|
|
1046
1041
|
committed = true;
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1042
|
+
this.#rotationResetCancel = null;
|
|
1043
|
+
const shape = this.getObject(turned.id);
|
|
1044
|
+
const current = shape && 'rotation' in shape ? shape.rotation : startRotation;
|
|
1045
|
+
const next = this.#state.scene.map((object) => (object.id === turned.id ? { ...turned, rotation: current } : object));
|
|
1046
|
+
this.applyScene(next, 'push');
|
|
1052
1047
|
};
|
|
1053
1048
|
// The time comes from the engine's own clock, not from the frame callback: a browser hands the callback a time since the
|
|
1054
1049
|
// page opened, while `startAt` is read from `services.now` (the wall clock), so mixing the two would never finish the turn.
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1050
|
+
const tick = () => {
|
|
1051
|
+
const elapsed = this.services.now() - startAt;
|
|
1052
|
+
const progress = rotationResetProgress(elapsed);
|
|
1053
|
+
const angle = rotationAfterResetProgress(startRotation, progress);
|
|
1054
|
+
const next = this.#state.scene.map((object) => (object.id === turned.id ? { ...turned, rotation: angle } : object));
|
|
1055
|
+
this.applyScene(next, 'transient');
|
|
1061
1056
|
if (progress >= 1) {
|
|
1062
|
-
cancelFrame
|
|
1057
|
+
cancelFrame?.();
|
|
1063
1058
|
committed = true;
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1059
|
+
this.#rotationResetCancel = null;
|
|
1060
|
+
const upright = this.#state.scene.map((object) => (object.id === turned.id ? { ...turned, rotation: 0 } : object));
|
|
1061
|
+
this.applyScene(upright, 'push');
|
|
1067
1062
|
return;
|
|
1068
1063
|
}
|
|
1069
|
-
cancelFrame =
|
|
1064
|
+
cancelFrame = this.services.requestAnimationFrame(tick);
|
|
1070
1065
|
};
|
|
1071
1066
|
cancelFrame = this.services.requestAnimationFrame(tick);
|
|
1072
|
-
|
|
1073
|
-
cancelFrame
|
|
1067
|
+
this.#rotationResetCancel = () => {
|
|
1068
|
+
cancelFrame?.();
|
|
1074
1069
|
commitCurrent();
|
|
1075
|
-
}
|
|
1070
|
+
};
|
|
1076
1071
|
return true;
|
|
1077
|
-
}
|
|
1072
|
+
}
|
|
1078
1073
|
/**
|
|
1079
1074
|
* Lays the one selected text out in one line again, as it was when it was made: its width follows its letters (a
|
|
1080
1075
|
* double click on its side handle). One undo step. False when the selection is not a single text, or it follows its letters already.
|
|
1081
1076
|
*/
|
|
1082
|
-
|
|
1083
|
-
|
|
1077
|
+
fitSelectedTextToContent() {
|
|
1078
|
+
const shapes = this.selectedShapes();
|
|
1084
1079
|
if (shapes.length !== 1 || shapes[0].type !== 'text' || shapes[0].autoWidth)
|
|
1085
1080
|
return false;
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
this.applyScene(
|
|
1081
|
+
const only = shapes[0];
|
|
1082
|
+
const fitted = this.#fitText({ ...only, autoWidth: true });
|
|
1083
|
+
this.applyScene(this.#state.scene.map((object) => (object.id === only.id ? fitted : object)), 'push');
|
|
1089
1084
|
return true;
|
|
1090
|
-
}
|
|
1085
|
+
}
|
|
1091
1086
|
/** Moves the selection by a distance (an arrow key). Presses close together are one undo step. */
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1087
|
+
nudgeSelection(deltaX, deltaY) {
|
|
1088
|
+
const scene = this.#state.scene;
|
|
1089
|
+
const next = translateObjects(scene, this.#state.selectedIds, deltaX, deltaY);
|
|
1095
1090
|
if (next === scene)
|
|
1096
1091
|
return false;
|
|
1097
|
-
|
|
1098
|
-
|
|
1092
|
+
const now = this.services.now();
|
|
1093
|
+
const merging = now < this.#nudgeMergeUntil;
|
|
1099
1094
|
this.applyScene(next, merging ? 'replace' : 'push');
|
|
1100
|
-
|
|
1095
|
+
this.#nudgeMergeUntil = now + NUDGE_MERGE_MS;
|
|
1096
|
+
return true;
|
|
1097
|
+
}
|
|
1098
|
+
alignSelection(mode) {
|
|
1099
|
+
return this.#arrange((scene, ids) => alignObjects(scene, ids, mode));
|
|
1100
|
+
}
|
|
1101
|
+
distributeSelection(axis) {
|
|
1102
|
+
return this.#arrange((scene, ids) => distributeObjects(scene, ids, axis));
|
|
1103
|
+
}
|
|
1104
|
+
#arrange(change) {
|
|
1105
|
+
const scene = this.#state.scene;
|
|
1106
|
+
const next = change(scene, this.#state.selectedIds);
|
|
1107
|
+
if (next === scene)
|
|
1108
|
+
return false;
|
|
1109
|
+
this.applyScene(next, 'push');
|
|
1101
1110
|
return true;
|
|
1102
|
-
}
|
|
1103
|
-
EngineCore.prototype.alignSelection = function (mode) {
|
|
1104
|
-
return __classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_arrange).call(this, function (scene, ids) { return alignObjects(scene, ids, mode); });
|
|
1105
|
-
};
|
|
1106
|
-
EngineCore.prototype.distributeSelection = function (axis) {
|
|
1107
|
-
return __classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_arrange).call(this, function (scene, ids) { return distributeObjects(scene, ids, axis); });
|
|
1108
|
-
};
|
|
1111
|
+
}
|
|
1109
1112
|
/**
|
|
1110
1113
|
* Puts the selection in front of or behind the others (`front` and `back` all the way, `forward` and `backward` one step).
|
|
1111
1114
|
* Shapes that arrows tie together are one diagram: what is joined to the selection by arrows, at any distance, goes with it.
|
|
1112
1115
|
*/
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
return __classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_arrange).call(this, function (scene, ids) {
|
|
1116
|
+
reorderSelection(mode) {
|
|
1117
|
+
return this.#arrange((scene, ids) => {
|
|
1116
1118
|
try {
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
+
const withConnected = withConnectedObjects(scene, ids);
|
|
1120
|
+
this.services.log('Canvas: layer order', { mode, selected: ids.length, moved: withConnected.length });
|
|
1119
1121
|
return reorderObjects(scene, withConnected, mode);
|
|
1120
1122
|
}
|
|
1121
1123
|
catch (error) {
|
|
1122
1124
|
// A board that cannot be followed through its arrows still moves what is selected, which is what the person asked for.
|
|
1123
|
-
|
|
1125
|
+
this.services.log('Canvas: the shapes tied by arrows could not be followed, only the selection is moved', { mode, error: String(error) });
|
|
1124
1126
|
return reorderObjects(scene, ids, mode);
|
|
1125
1127
|
}
|
|
1126
1128
|
});
|
|
1127
|
-
}
|
|
1129
|
+
}
|
|
1128
1130
|
/** Puts the selected units in a group, which becomes the selection. */
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
+
groupSelection() {
|
|
1132
|
+
const grouped = groupObjects(this.#state.scene, this.#state.selectedIds, this.services.createId('group'));
|
|
1131
1133
|
if (!grouped)
|
|
1132
1134
|
return false;
|
|
1133
1135
|
this.applyScene(grouped.scene, 'push');
|
|
1134
|
-
|
|
1136
|
+
this.#set({ selectedIds: [grouped.groupId], enteredGroupId: null });
|
|
1135
1137
|
return true;
|
|
1136
|
-
}
|
|
1138
|
+
}
|
|
1137
1139
|
/** Dissolves the selected groups; their members become the selection. */
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
if (scene ===
|
|
1140
|
+
ungroupSelection() {
|
|
1141
|
+
const { scene, releasedIds } = ungroupObjects(this.#state.scene, this.#state.selectedIds);
|
|
1142
|
+
if (scene === this.#state.scene)
|
|
1141
1143
|
return false;
|
|
1142
1144
|
this.applyScene(scene, 'push');
|
|
1143
|
-
|
|
1145
|
+
this.#set({ selectedIds: releasedIds, enteredGroupId: null });
|
|
1144
1146
|
return true;
|
|
1145
|
-
}
|
|
1147
|
+
}
|
|
1146
1148
|
// ──────────────────────────────────────────────────────── clipboard
|
|
1147
|
-
|
|
1148
|
-
return
|
|
1149
|
-
}
|
|
1149
|
+
hasClipboard() {
|
|
1150
|
+
return this.#clipboard !== null;
|
|
1151
|
+
}
|
|
1150
1152
|
/** Remembers the selection for a paste. */
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1153
|
+
copySelection() {
|
|
1154
|
+
const scene = this.#state.scene;
|
|
1155
|
+
const rootIds = normalizeSelection(scene, this.#state.selectedIds);
|
|
1154
1156
|
if (rootIds.length === 0)
|
|
1155
1157
|
return false;
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1158
|
+
const included = withDescendantIds(scene, rootIds);
|
|
1159
|
+
this.#clipboard = { objects: scene.filter((object) => included.has(object.id)), rootIds };
|
|
1160
|
+
this.#pasteSteps = 0;
|
|
1159
1161
|
return true;
|
|
1160
|
-
}
|
|
1162
|
+
}
|
|
1161
1163
|
/** Copies the selection and removes it, without asking about widget cards: the widget itself stays where it is stored. */
|
|
1162
|
-
|
|
1164
|
+
cutSelection() {
|
|
1163
1165
|
if (!this.copySelection())
|
|
1164
1166
|
return false;
|
|
1165
|
-
|
|
1167
|
+
const removed = this.removeObjects(this.#state.selectedIds);
|
|
1166
1168
|
if (removed)
|
|
1167
|
-
|
|
1169
|
+
this.#set({ selectedIds: [] });
|
|
1170
|
+
this.#tellHostWhenPersonEmptiedBoard(removed);
|
|
1168
1171
|
return removed;
|
|
1169
|
-
}
|
|
1172
|
+
}
|
|
1170
1173
|
/** Puts a copy of the clipboard on the board, centred on `at` (or shifted a little from the original). Returns the new ids. */
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
var clipboard = __classPrivateFieldGet(this, _EngineCore_clipboard, "f");
|
|
1174
|
+
paste(at = this.lastPointerWorld) {
|
|
1175
|
+
const clipboard = this.#clipboard;
|
|
1174
1176
|
if (!clipboard)
|
|
1175
1177
|
return [];
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
+
const bounds = boundsOfObjects(clipboard.objects, clipboard.rootIds);
|
|
1179
|
+
this.#pasteSteps += 1;
|
|
1178
1180
|
// Each paste after the first steps a little further, so copies pasted in a row do not hide each other.
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
+
const cascade = (this.#pasteSteps - 1) * PASTE_OFFSET;
|
|
1182
|
+
const offset = at && bounds
|
|
1181
1183
|
? { x: at.x - (bounds.x + bounds.width / 2) + cascade, y: at.y - (bounds.y + bounds.height / 2) + cascade }
|
|
1182
|
-
: { x: PASTE_OFFSET *
|
|
1183
|
-
return
|
|
1184
|
-
}
|
|
1184
|
+
: { x: PASTE_OFFSET * this.#pasteSteps, y: PASTE_OFFSET * this.#pasteSteps };
|
|
1185
|
+
return this.#insertCopies(clipboard.objects, clipboard.rootIds, offset);
|
|
1186
|
+
}
|
|
1185
1187
|
/** A copy of the selection, a little to the side and below. The copy becomes the selection. */
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1188
|
+
duplicateSelection() {
|
|
1189
|
+
const scene = this.#state.scene;
|
|
1190
|
+
const rootIds = normalizeSelection(scene, this.#state.selectedIds);
|
|
1189
1191
|
if (rootIds.length === 0)
|
|
1190
1192
|
return [];
|
|
1191
|
-
return
|
|
1192
|
-
}
|
|
1193
|
+
return this.#insertCopies(scene, rootIds, { x: PASTE_OFFSET, y: PASTE_OFFSET });
|
|
1194
|
+
}
|
|
1193
1195
|
/** Copies the selection in place for an Alt-drag: one undo step when the drag ends. */
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1196
|
+
duplicateSelectionForDrag() {
|
|
1197
|
+
const scene = this.#state.scene;
|
|
1198
|
+
const rootIds = normalizeSelection(scene, this.#state.selectedIds);
|
|
1197
1199
|
if (rootIds.length === 0)
|
|
1198
1200
|
return [];
|
|
1199
|
-
return
|
|
1200
|
-
}
|
|
1201
|
+
return this.#insertCopies(scene, rootIds, { x: 0, y: 0 }, 'transient');
|
|
1202
|
+
}
|
|
1203
|
+
#insertCopies(source, rootIds, offset, mode = 'push') {
|
|
1204
|
+
const widgetDuplicationAllowed = this.hooks.canDuplicateWidget ? this.hooks.canDuplicateWidget() : true;
|
|
1205
|
+
const allowedRootIds = widgetDuplicationAllowed
|
|
1206
|
+
? rootIds
|
|
1207
|
+
: rootIds.filter((id) => source.find((o) => o.id === id)?.type !== 'widget');
|
|
1208
|
+
if (allowedRootIds.length === 0)
|
|
1209
|
+
return [];
|
|
1210
|
+
const { objects, idMap } = duplicateObjects(source, allowedRootIds, offset, (type) => this.services.createId(type === 'group' ? 'group' : 'obj'));
|
|
1211
|
+
const filteredObjects = widgetDuplicationAllowed
|
|
1212
|
+
? objects
|
|
1213
|
+
: objects.filter((o) => o.type !== 'widget');
|
|
1214
|
+
if (filteredObjects.length === 0)
|
|
1215
|
+
return [];
|
|
1216
|
+
const duplicatesWidgets = this.hooks.onWidgetDuplicated ?? null;
|
|
1217
|
+
const pending = { ...this.#state.pending };
|
|
1218
|
+
const widgetClones = [];
|
|
1219
|
+
const prepared = filteredObjects.map((object) => {
|
|
1220
|
+
if (object.type !== 'widget')
|
|
1221
|
+
return object;
|
|
1222
|
+
if (duplicatesWidgets) {
|
|
1223
|
+
// The card waits, unselected, for the host to make the widget behind it.
|
|
1224
|
+
pending[object.id] = 'duplicating';
|
|
1225
|
+
widgetClones.push({ sourceWidgetId: object.widgetId, clone: object });
|
|
1226
|
+
return object;
|
|
1227
|
+
}
|
|
1228
|
+
// With no host to copy the widget, a copy of a card is only a picture of it.
|
|
1229
|
+
return this.#flattenWidgetCard(object);
|
|
1230
|
+
});
|
|
1231
|
+
// A copy of a member goes into the group of the original; if that group is gone it is repaired to the top level.
|
|
1232
|
+
this.applyScene(normalizeScene([...this.#state.scene, ...prepared.map((object) => this.#fitText(object))]), mode);
|
|
1233
|
+
this.#set({ pending: Object.freeze(pending) });
|
|
1234
|
+
const rootCopyIds = allowedRootIds.map((id) => idMap.get(id)).filter((id) => !!id);
|
|
1235
|
+
const selectable = rootCopyIds.filter((id) => !pending[id]);
|
|
1236
|
+
this.#setSelection(selectable);
|
|
1237
|
+
for (const { sourceWidgetId, clone } of widgetClones) {
|
|
1238
|
+
this.#callHook(() => duplicatesWidgets?.(sourceWidgetId, clone), 'widget duplicated hook');
|
|
1239
|
+
}
|
|
1240
|
+
return rootCopyIds;
|
|
1241
|
+
}
|
|
1242
|
+
/** A widget card with its widget gone: the screenshot it had, or an empty frame. */
|
|
1243
|
+
#flattenWidgetCard(card) {
|
|
1244
|
+
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 };
|
|
1245
|
+
if (card.thumbnail)
|
|
1246
|
+
return createImageObject({ ...box, src: card.thumbnail, naturalWidth: card.width, naturalHeight: card.height });
|
|
1247
|
+
return createRectObject(box);
|
|
1248
|
+
}
|
|
1201
1249
|
// ──────────────────────────────────────────────────────── widget cards (the parts every caller needs)
|
|
1202
1250
|
/** Marks cards as waiting for the host (a loader is shown over them). `null` clears the mark. */
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
if (((_a = current[id]) !== null && _a !== void 0 ? _a : null) === state)
|
|
1251
|
+
setPending(id, state) {
|
|
1252
|
+
const current = this.#state.pending;
|
|
1253
|
+
if ((current[id] ?? null) === state)
|
|
1207
1254
|
return;
|
|
1208
|
-
|
|
1255
|
+
const next = { ...current };
|
|
1209
1256
|
if (state)
|
|
1210
1257
|
next[id] = state;
|
|
1211
1258
|
else
|
|
1212
1259
|
delete next[id];
|
|
1213
|
-
|
|
1214
|
-
}
|
|
1260
|
+
this.#set({ pending: Object.freeze(next) });
|
|
1261
|
+
}
|
|
1215
1262
|
/** What the host said about a widget's card, with the defaults for what it never said. */
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
};
|
|
1263
|
+
widgetRuntime(widgetId) {
|
|
1264
|
+
return this.#state.widgets[widgetId] ?? DEFAULT_WIDGET_RUNTIME;
|
|
1265
|
+
}
|
|
1220
1266
|
/** Records what the host says about a widget's card. True when something changed. */
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
var next = __assign(__assign({}, current), patch);
|
|
1267
|
+
setWidgetRuntime(widgetId, patch) {
|
|
1268
|
+
const current = this.widgetRuntime(widgetId);
|
|
1269
|
+
const next = { ...current, ...patch };
|
|
1225
1270
|
if (next.hiddenImage === current.hiddenImage && next.orphaned === current.orphaned && next.bodyDragAllowed === current.bodyDragAllowed) {
|
|
1226
1271
|
return false;
|
|
1227
1272
|
}
|
|
1228
|
-
|
|
1273
|
+
this.#set({ widgets: Object.freeze({ ...this.#state.widgets, [widgetId]: Object.freeze(next) }) });
|
|
1229
1274
|
return true;
|
|
1230
|
-
}
|
|
1275
|
+
}
|
|
1231
1276
|
/** The widget whose card is under the pointer, or null. */
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
}
|
|
1277
|
+
setHoverWidget(widgetId) {
|
|
1278
|
+
this.#set({ hoverWidgetId: widgetId });
|
|
1279
|
+
}
|
|
1235
1280
|
/** The host's own bar is dragging a card. */
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
}
|
|
1281
|
+
setOverlayDragActive(active) {
|
|
1282
|
+
this.#set({ overlayDragActive: active });
|
|
1283
|
+
}
|
|
1239
1284
|
/** Gives a card (or any object, which becomes a card) the widget it stands for. The card becomes the selection. */
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1285
|
+
linkObjectToWidget(target, widgetId, name) {
|
|
1286
|
+
const id = typeof target === 'string' ? target : target.id;
|
|
1287
|
+
const object = this.getObject(id);
|
|
1243
1288
|
if (!object || object.type === 'group')
|
|
1244
1289
|
return false;
|
|
1245
|
-
|
|
1290
|
+
let replacement;
|
|
1246
1291
|
if (object.type === 'widget') {
|
|
1247
|
-
replacement =
|
|
1292
|
+
replacement = { ...object, widgetId, widgetName: name ?? object.widgetName };
|
|
1248
1293
|
}
|
|
1249
1294
|
else {
|
|
1250
1295
|
replacement = createWidgetObject({
|
|
1251
|
-
id
|
|
1296
|
+
id,
|
|
1252
1297
|
parentId: object.parentId,
|
|
1253
|
-
widgetId
|
|
1254
|
-
widgetName: name
|
|
1298
|
+
widgetId,
|
|
1299
|
+
widgetName: name ?? null,
|
|
1255
1300
|
x: object.x,
|
|
1256
1301
|
y: object.y,
|
|
1257
1302
|
width: object.width,
|
|
@@ -1260,175 +1305,21 @@ var EngineCore = /** @class */ (function () {
|
|
|
1260
1305
|
opacity: object.opacity
|
|
1261
1306
|
});
|
|
1262
1307
|
}
|
|
1263
|
-
this.applyScene(replaceObject(
|
|
1308
|
+
this.applyScene(replaceObject(this.#state.scene, id, replacement), 'push');
|
|
1264
1309
|
this.setPending(id, null);
|
|
1265
|
-
|
|
1310
|
+
this.#setSelection([id], null);
|
|
1266
1311
|
return true;
|
|
1267
|
-
}
|
|
1312
|
+
}
|
|
1268
1313
|
/** Drops a card that was waiting for a widget and will never get one. */
|
|
1269
|
-
|
|
1270
|
-
if (!
|
|
1314
|
+
discardPending(id) {
|
|
1315
|
+
if (!this.#state.pending[id])
|
|
1271
1316
|
return false;
|
|
1272
1317
|
this.setPending(id, null);
|
|
1273
1318
|
return this.removeObjects([id]);
|
|
1274
|
-
}
|
|
1319
|
+
}
|
|
1275
1320
|
/** Every widget card that is on the board for good (one still being copied does not count). */
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
return
|
|
1279
|
-
};
|
|
1280
|
-
return EngineCore;
|
|
1281
|
-
}());
|
|
1282
|
-
export { EngineCore };
|
|
1283
|
-
_EngineCore_state = new WeakMap(), _EngineCore_rotationResetCancel = new WeakMap(), _EngineCore_viewportMoveCancel = 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) {
|
|
1284
|
-
var _this = this;
|
|
1285
|
-
var changed = false;
|
|
1286
|
-
for (var _i = 0, _a = Object.keys(patch); _i < _a.length; _i++) {
|
|
1287
|
-
var key = _a[_i];
|
|
1288
|
-
if (!Object.is(patch[key], __classPrivateFieldGet(this, _EngineCore_state, "f")[key])) {
|
|
1289
|
-
changed = true;
|
|
1290
|
-
break;
|
|
1291
|
-
}
|
|
1321
|
+
widgetCards() {
|
|
1322
|
+
const pending = this.#state.pending;
|
|
1323
|
+
return this.#state.scene.filter((object) => object.type === 'widget' && !pending[object.id]);
|
|
1292
1324
|
}
|
|
1293
|
-
|
|
1294
|
-
return;
|
|
1295
|
-
var before = __classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds;
|
|
1296
|
-
__classPrivateFieldSet(this, _EngineCore_state, __assign(__assign({}, __classPrivateFieldGet(this, _EngineCore_state, "f")), patch), "f");
|
|
1297
|
-
for (var _b = 0, _c = __spreadArray([], __classPrivateFieldGet(this, _EngineCore_listeners, "f"), true); _b < _c.length; _b++) {
|
|
1298
|
-
var listener = _c[_b];
|
|
1299
|
-
listener(__classPrivateFieldGet(this, _EngineCore_state, "f"));
|
|
1300
|
-
}
|
|
1301
|
-
if (__classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds !== before && !sameIds(__classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds, before)) {
|
|
1302
|
-
__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');
|
|
1303
|
-
}
|
|
1304
|
-
}, _EngineCore_callHook = function _EngineCore_callHook(run, name) {
|
|
1305
|
-
var _a, _b;
|
|
1306
|
-
try {
|
|
1307
|
-
run();
|
|
1308
|
-
}
|
|
1309
|
-
catch (error) {
|
|
1310
|
-
(_b = (_a = this.hooks).onError) === null || _b === void 0 ? void 0 : _b.call(_a, "The ".concat(name, " threw"), error);
|
|
1311
|
-
}
|
|
1312
|
-
}, _EngineCore_sceneForSave = function _EngineCore_sceneForSave() {
|
|
1313
|
-
var _a = __classPrivateFieldGet(this, _EngineCore_state, "f"), scene = _a.scene, pending = _a.pending, editingTextId = _a.editingTextId;
|
|
1314
|
-
var unwanted = new Set();
|
|
1315
|
-
for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
|
|
1316
|
-
var object = scene_1[_i];
|
|
1317
|
-
if (pending[object.id])
|
|
1318
|
-
unwanted.add(object.id);
|
|
1319
|
-
if (object.id === editingTextId && object.type === 'text' && object.text.trim() === '')
|
|
1320
|
-
unwanted.add(object.id);
|
|
1321
|
-
}
|
|
1322
|
-
if (unwanted.size === 0)
|
|
1323
|
-
return scene;
|
|
1324
|
-
return normalizeScene(scene.filter(function (object) { return !unwanted.has(object.id); }));
|
|
1325
|
-
}, _EngineCore_afterHistoryMove = function _EngineCore_afterHistoryMove(scene) {
|
|
1326
|
-
var _a, _b;
|
|
1327
|
-
__classPrivateFieldSet(this, _EngineCore_nudgeMergeUntil, 0, "f");
|
|
1328
|
-
this.applyScene(scene, 'transient');
|
|
1329
|
-
__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 });
|
|
1330
|
-
(_b = (_a = this.hooks).onDirty) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
1331
|
-
}, _EngineCore_setSelection = function _EngineCore_setSelection(selection, enteredGroupId) {
|
|
1332
|
-
if (enteredGroupId === void 0) { enteredGroupId = __classPrivateFieldGet(this, _EngineCore_state, "f").enteredGroupId; }
|
|
1333
|
-
var entered = enteredGroupId;
|
|
1334
|
-
if (entered) {
|
|
1335
|
-
var index_2 = this.sceneIndex();
|
|
1336
|
-
// Picking something outside the group that was entered leaves the group.
|
|
1337
|
-
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; });
|
|
1338
|
-
if (!stillInside)
|
|
1339
|
-
entered = null;
|
|
1340
|
-
}
|
|
1341
|
-
var next = sameIds(selection, __classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds) ? __classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds : Object.freeze(__spreadArray([], selection, true));
|
|
1342
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_set).call(this, { selectedIds: next, enteredGroupId: entered });
|
|
1343
|
-
}, _EngineCore_dropScribble = function _EngineCore_dropScribble() {
|
|
1344
|
-
__classPrivateFieldSet(this, _EngineCore_scribble, null, "f");
|
|
1345
|
-
return __classPrivateFieldGet(this, _EngineCore_state, "f").scribble === null ? {} : { scribble: null };
|
|
1346
|
-
}, _EngineCore_writeViewport = function _EngineCore_writeViewport(viewport) {
|
|
1347
|
-
var next = sanitizeViewport(viewport);
|
|
1348
|
-
var current = __classPrivateFieldGet(this, _EngineCore_state, "f").viewport;
|
|
1349
|
-
if (next.x === current.x && next.y === current.y && next.zoom === current.zoom)
|
|
1350
|
-
return;
|
|
1351
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_set).call(this, { viewport: next });
|
|
1352
|
-
}, _EngineCore_viewCenter = function _EngineCore_viewCenter() {
|
|
1353
|
-
var _a = __classPrivateFieldGet(this, _EngineCore_state, "f").viewSize, width = _a.width, height = _a.height;
|
|
1354
|
-
return { x: width / 2, y: height / 2 };
|
|
1355
|
-
}, _EngineCore_fittedViewport = function _EngineCore_fittedViewport(scene) {
|
|
1356
|
-
var bounds = boardVisualBounds(scene);
|
|
1357
|
-
if (!bounds)
|
|
1358
|
-
return __assign({}, INITIAL_VIEWPORT);
|
|
1359
|
-
return fitViewportToRect(bounds, __classPrivateFieldGet(this, _EngineCore_state, "f").viewSize, VIEW_FIT_PADDING);
|
|
1360
|
-
}, _EngineCore_fitText = function _EngineCore_fitText(object) {
|
|
1361
|
-
return object.type === 'text' ? fitTextObject(object, this.services.measureText) : object;
|
|
1362
|
-
}, _EngineCore_fitTexts = function _EngineCore_fitTexts(scene) {
|
|
1363
|
-
var _this = this;
|
|
1364
|
-
var changed = false;
|
|
1365
|
-
var next = scene.map(function (object) {
|
|
1366
|
-
if (object.type !== 'text')
|
|
1367
|
-
return object;
|
|
1368
|
-
var fitted = fitTextObject(object, _this.services.measureText);
|
|
1369
|
-
if (fitted !== object)
|
|
1370
|
-
changed = true;
|
|
1371
|
-
return fitted;
|
|
1372
|
-
});
|
|
1373
|
-
return changed ? next : scene;
|
|
1374
|
-
}, _EngineCore_arrange = function _EngineCore_arrange(change) {
|
|
1375
|
-
var scene = __classPrivateFieldGet(this, _EngineCore_state, "f").scene;
|
|
1376
|
-
var next = change(scene, __classPrivateFieldGet(this, _EngineCore_state, "f").selectedIds);
|
|
1377
|
-
if (next === scene)
|
|
1378
|
-
return false;
|
|
1379
|
-
this.applyScene(next, 'push');
|
|
1380
|
-
return true;
|
|
1381
|
-
}, _EngineCore_insertCopies = function _EngineCore_insertCopies(source, rootIds, offset, mode) {
|
|
1382
|
-
var _this = this;
|
|
1383
|
-
var _a;
|
|
1384
|
-
if (mode === void 0) { mode = 'push'; }
|
|
1385
|
-
var widgetDuplicationAllowed = this.hooks.canDuplicateWidget ? this.hooks.canDuplicateWidget() : true;
|
|
1386
|
-
var allowedRootIds = widgetDuplicationAllowed
|
|
1387
|
-
? rootIds
|
|
1388
|
-
: rootIds.filter(function (id) { var _a; return ((_a = source.find(function (o) { return o.id === id; })) === null || _a === void 0 ? void 0 : _a.type) !== 'widget'; });
|
|
1389
|
-
if (allowedRootIds.length === 0)
|
|
1390
|
-
return [];
|
|
1391
|
-
var _b = duplicateObjects(source, allowedRootIds, offset, function (type) {
|
|
1392
|
-
return _this.services.createId(type === 'group' ? 'group' : 'obj');
|
|
1393
|
-
}), objects = _b.objects, idMap = _b.idMap;
|
|
1394
|
-
var filteredObjects = widgetDuplicationAllowed
|
|
1395
|
-
? objects
|
|
1396
|
-
: objects.filter(function (o) { return o.type !== 'widget'; });
|
|
1397
|
-
if (filteredObjects.length === 0)
|
|
1398
|
-
return [];
|
|
1399
|
-
var duplicatesWidgets = (_a = this.hooks.onWidgetDuplicated) !== null && _a !== void 0 ? _a : null;
|
|
1400
|
-
var pending = __assign({}, __classPrivateFieldGet(this, _EngineCore_state, "f").pending);
|
|
1401
|
-
var widgetClones = [];
|
|
1402
|
-
var prepared = filteredObjects.map(function (object) {
|
|
1403
|
-
if (object.type !== 'widget')
|
|
1404
|
-
return object;
|
|
1405
|
-
if (duplicatesWidgets) {
|
|
1406
|
-
// The card waits, unselected, for the host to make the widget behind it.
|
|
1407
|
-
pending[object.id] = 'duplicating';
|
|
1408
|
-
widgetClones.push({ sourceWidgetId: object.widgetId, clone: object });
|
|
1409
|
-
return object;
|
|
1410
|
-
}
|
|
1411
|
-
// With no host to copy the widget, a copy of a card is only a picture of it.
|
|
1412
|
-
return __classPrivateFieldGet(_this, _EngineCore_instances, "m", _EngineCore_flattenWidgetCard).call(_this, object);
|
|
1413
|
-
});
|
|
1414
|
-
// A copy of a member goes into the group of the original; if that group is gone it is repaired to the top level.
|
|
1415
|
-
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);
|
|
1416
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_set).call(this, { pending: Object.freeze(pending) });
|
|
1417
|
-
var rootCopyIds = allowedRootIds.map(function (id) { return idMap.get(id); }).filter(function (id) { return !!id; });
|
|
1418
|
-
var selectable = rootCopyIds.filter(function (id) { return !pending[id]; });
|
|
1419
|
-
__classPrivateFieldGet(this, _EngineCore_instances, "m", _EngineCore_setSelection).call(this, selectable);
|
|
1420
|
-
var _loop_1 = function (sourceWidgetId, clone) {
|
|
1421
|
-
__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');
|
|
1422
|
-
};
|
|
1423
|
-
var this_1 = this;
|
|
1424
|
-
for (var _i = 0, widgetClones_1 = widgetClones; _i < widgetClones_1.length; _i++) {
|
|
1425
|
-
var _c = widgetClones_1[_i], sourceWidgetId = _c.sourceWidgetId, clone = _c.clone;
|
|
1426
|
-
_loop_1(sourceWidgetId, clone);
|
|
1427
|
-
}
|
|
1428
|
-
return rootCopyIds;
|
|
1429
|
-
}, _EngineCore_flattenWidgetCard = function _EngineCore_flattenWidgetCard(card) {
|
|
1430
|
-
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 };
|
|
1431
|
-
if (card.thumbnail)
|
|
1432
|
-
return createImageObject(__assign(__assign({}, box), { src: card.thumbnail, naturalWidth: card.width, naturalHeight: card.height }));
|
|
1433
|
-
return createRectObject(box);
|
|
1434
|
-
};
|
|
1325
|
+
}
|