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