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