@widgetic/canvas 0.5.41 → 0.5.42

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