@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
@@ -6,27 +6,6 @@
6
6
  * the release. Two fingers make a pinch, the wheel pans and zooms, and a double click enters a group or edits a
7
7
  * text. Nothing here touches the DOM, so every rule is tested with simulated pointers.
8
8
  */
9
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
10
- if (kind === "m") throw new TypeError("Private method is not writable");
11
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
12
- 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");
13
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
14
- };
15
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
16
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
17
- 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");
18
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
19
- };
20
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
21
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
22
- if (ar || !(i in from)) {
23
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
24
- ar[i] = from[i];
25
- }
26
- }
27
- return to.concat(ar || Array.prototype.slice.call(from));
28
- };
29
- var _PointerController_instances, _PointerController_engine, _PointerController_options, _PointerController_context, _PointerController_active, _PointerController_pinch, _PointerController_pointers, _PointerController_spaceHeld, _PointerController_lastClick, _PointerController_lastHandlePress, _PointerController_lastHover, _PointerController_lastInput, _PointerController_cursorWhenIdle, _PointerController_begin, _PointerController_finish, _PointerController_pressWithTextTool, _PointerController_isDoubleClick, _PointerController_rememberHandlePress, _PointerController_isDoublePressOn, _PointerController_pressWithSelectTool, _PointerController_drillThrough, _PointerController_isHeldByLiveWidget, _PointerController_handleDoubleClick, _PointerController_hover, _PointerController_hoverArrowTarget, _PointerController_hoverWidgetCard, _PointerController_forgetStalePointers, _PointerController_pinchGeometry, _PointerController_startPinch, _PointerController_updatePinch, _PointerController_endPinch;
30
9
  import { pickArrowBinding } from '../core/arrowBinding.js';
31
10
  import { hitTestLeaf } from '../core/hitTest.js';
32
11
  import { clampNumber, distanceBetween } from '../core/geometry.js';
@@ -39,101 +18,127 @@ import { arrowHandleMetricsFor, hitArrowHandle, singleArrowOf } from './arrowHan
39
18
  import { resolveHoverCursor } from './hoverCursor.js';
40
19
  import { availableHandles, cursorForHandle, handleMetricsFor, hitSelectionHandle } from './selectionHandles.js';
41
20
  /** Two presses this close in time and place are a double click. */
42
- export var DOUBLE_CLICK_MS = 400;
43
- export var DOUBLE_CLICK_DISTANCE = 6;
21
+ export const DOUBLE_CLICK_MS = 400;
22
+ export const DOUBLE_CLICK_DISTANCE = 6;
44
23
  /** A pointer that has said nothing for this long is not one of two fingers: its release was lost. */
45
- var STALE_POINTER_MS = 1500;
24
+ const STALE_POINTER_MS = 1500;
46
25
  /** One line of a wheel in a line-based device, in pixels. */
47
- var WHEEL_LINE_PIXELS = 16;
48
- var WHEEL_PAGE_PIXELS = 400;
49
- var PointerController = /** @class */ (function () {
50
- function PointerController(engine, options) {
51
- if (options === void 0) { options = {}; }
52
- _PointerController_instances.add(this);
53
- _PointerController_engine.set(this, void 0);
54
- _PointerController_options.set(this, void 0);
55
- _PointerController_context.set(this, void 0);
56
- _PointerController_active.set(this, null);
57
- _PointerController_pinch.set(this, null);
58
- _PointerController_pointers.set(this, new Map());
59
- _PointerController_spaceHeld.set(this, false);
60
- _PointerController_lastClick.set(this, null);
61
- /** The last press on a handle (the turning dot counts as one): a second press on the same one, soon and close, is a double click on it. */
62
- _PointerController_lastHandlePress.set(this, null);
63
- _PointerController_lastHover.set(this, null);
64
- _PointerController_lastInput.set(this, null);
65
- __classPrivateFieldSet(this, _PointerController_engine, engine, "f");
66
- __classPrivateFieldSet(this, _PointerController_options, options, "f");
67
- __classPrivateFieldSet(this, _PointerController_context, { engine: engine, options: options }, "f");
68
- }
69
- Object.defineProperty(PointerController.prototype, "gestureKind", {
70
- /** The kind of gesture that is running, or null. */
71
- get: function () {
72
- var _a, _b;
73
- return __classPrivateFieldGet(this, _PointerController_pinch, "f") ? 'pinch' : ((_b = (_a = __classPrivateFieldGet(this, _PointerController_active, "f")) === null || _a === void 0 ? void 0 : _a.kind) !== null && _b !== void 0 ? _b : null);
74
- },
75
- enumerable: false,
76
- configurable: true
77
- });
26
+ const WHEEL_LINE_PIXELS = 16;
27
+ const WHEEL_PAGE_PIXELS = 400;
28
+ export class PointerController {
29
+ #engine;
30
+ #options;
31
+ #context;
32
+ #active = null;
33
+ #pinch = null;
34
+ #pointers = new Map();
35
+ #spaceHeld = false;
36
+ #lastClick = null;
37
+ /** The last press on a handle (the turning dot counts as one): a second press on the same one, soon and close, is a double click on it. */
38
+ #lastHandlePress = null;
39
+ #lastHover = null;
40
+ #lastInput = null;
41
+ constructor(engine, options = {}) {
42
+ this.#engine = engine;
43
+ this.#options = options;
44
+ this.#context = { engine, options };
45
+ }
46
+ /** The kind of gesture that is running, or null. */
47
+ get gestureKind() {
48
+ return this.#pinch ? 'pinch' : (this.#active?.kind ?? null);
49
+ }
78
50
  /** Space held: a press pans, whatever the tool. */
79
- PointerController.prototype.setSpaceHeld = function (held) {
80
- __classPrivateFieldSet(this, _PointerController_spaceHeld, held, "f");
81
- if (!__classPrivateFieldGet(this, _PointerController_active, "f") && !__classPrivateFieldGet(this, _PointerController_pinch, "f")) {
82
- __classPrivateFieldGet(this, _PointerController_engine, "f").setCursor(held ? 'grab' : __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_cursorWhenIdle).call(this));
51
+ setSpaceHeld(held) {
52
+ this.#spaceHeld = held;
53
+ if (!this.#active && !this.#pinch) {
54
+ this.#engine.setCursor(held ? 'grab' : this.#cursorWhenIdle());
55
+ }
56
+ }
57
+ #cursorWhenIdle() {
58
+ return cursorForTool(this.#engine.state.tool);
59
+ }
60
+ #begin(gesture) {
61
+ this.#active = gesture;
62
+ this.#engine.setHover(null);
63
+ this.#options.onGestureChange?.(gesture.kind);
64
+ }
65
+ #finish(mode, input) {
66
+ const gesture = this.#active;
67
+ if (!gesture)
68
+ return;
69
+ this.#active = null;
70
+ try {
71
+ if (mode === 'end' && input)
72
+ gesture.end(input);
73
+ else
74
+ gesture.cancel();
83
75
  }
84
- };
76
+ finally {
77
+ // After the gesture the frame (and dot) may have moved: look again at the pointer without waiting for the next move.
78
+ this.#lastHover = null;
79
+ this.#options.onGestureChange?.(null);
80
+ const hoverInput = input ?? this.#lastInput;
81
+ // No pointer seen yet (a gesture cancelled from the keyboard) or a finger: there is nothing to look at.
82
+ if (hoverInput && hoverInput.pointerType !== 'touch') {
83
+ this.#hover(hoverInput);
84
+ }
85
+ else {
86
+ this.#engine.setRotationDotHover(false);
87
+ this.#engine.setArrowHandleHover(null);
88
+ }
89
+ }
90
+ }
85
91
  /** Abandons the gesture that is running (Escape). True when there was one. */
86
- PointerController.prototype.cancelGesture = function () {
87
- if (__classPrivateFieldGet(this, _PointerController_pinch, "f")) {
88
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_endPinch).call(this);
92
+ cancelGesture() {
93
+ if (this.#pinch) {
94
+ this.#endPinch();
89
95
  return true;
90
96
  }
91
- if (!__classPrivateFieldGet(this, _PointerController_active, "f"))
97
+ if (!this.#active)
92
98
  return false;
93
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_finish).call(this, 'cancel');
99
+ this.#finish('cancel');
94
100
  return true;
95
- };
101
+ }
96
102
  /** Shift, Alt, Ctrl or Meta changed while the pointer stayed still. */
97
- PointerController.prototype.modifiersChanged = function (modifiers) {
98
- var _a, _b;
99
- (_b = (_a = __classPrivateFieldGet(this, _PointerController_active, "f")) === null || _a === void 0 ? void 0 : _a.modifiersChanged) === null || _b === void 0 ? void 0 : _b.call(_a, modifiers, __classPrivateFieldGet(this, _PointerController_lastInput, "f"));
100
- };
101
- PointerController.prototype.dispose = function () {
103
+ modifiersChanged(modifiers) {
104
+ this.#active?.modifiersChanged?.(modifiers, this.#lastInput);
105
+ }
106
+ dispose() {
102
107
  this.cancelGesture();
103
- __classPrivateFieldGet(this, _PointerController_pointers, "f").clear();
104
- };
108
+ this.#pointers.clear();
109
+ }
105
110
  // ──────────────────────────────────────────────────────── press
106
111
  /** Handles a press. Returns whether the shell should capture the pointer and keep the browser from acting on it. */
107
- PointerController.prototype.pointerDown = function (input) {
108
- __classPrivateFieldSet(this, _PointerController_lastInput, input, "f");
109
- __classPrivateFieldGet(this, _PointerController_engine, "f").cancelRotationReset();
110
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_forgetStalePointers).call(this, input.time);
111
- __classPrivateFieldGet(this, _PointerController_pointers, "f").set(input.pointerId, { x: input.x, y: input.y, seenAt: input.time });
112
- __classPrivateFieldGet(this, _PointerController_engine, "f").lastPointerWorld = __classPrivateFieldGet(this, _PointerController_engine, "f").toWorld({ x: input.x, y: input.y });
113
- if (input.pointerType === 'touch' && __classPrivateFieldGet(this, _PointerController_pointers, "f").size === 2) {
114
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_startPinch).call(this);
112
+ pointerDown(input) {
113
+ this.#lastInput = input;
114
+ this.#engine.cancelRotationReset();
115
+ this.#forgetStalePointers(input.time);
116
+ this.#pointers.set(input.pointerId, { x: input.x, y: input.y, seenAt: input.time });
117
+ this.#engine.lastPointerWorld = this.#engine.toWorld({ x: input.x, y: input.y });
118
+ if (input.pointerType === 'touch' && this.#pointers.size === 2) {
119
+ this.#startPinch();
115
120
  return true;
116
121
  }
117
- if (__classPrivateFieldGet(this, _PointerController_pointers, "f").size > 1 || __classPrivateFieldGet(this, _PointerController_pinch, "f"))
122
+ if (this.#pointers.size > 1 || this.#pinch)
118
123
  return true;
119
- if (__classPrivateFieldGet(this, _PointerController_active, "f"))
120
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_finish).call(this, 'cancel');
124
+ if (this.#active)
125
+ this.#finish('cancel');
121
126
  // The secondary button belongs to the context menu.
122
127
  if (input.button === 2)
123
128
  return false;
124
- if (input.button === 1 || (input.button === 0 && __classPrivateFieldGet(this, _PointerController_spaceHeld, "f")) || (input.button === 0 && __classPrivateFieldGet(this, _PointerController_engine, "f").state.tool === 'pan')) {
125
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createPanGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input));
129
+ if (input.button === 1 || (input.button === 0 && this.#spaceHeld) || (input.button === 0 && this.#engine.state.tool === 'pan')) {
130
+ this.#begin(createPanGesture(this.#context, input));
126
131
  return true;
127
132
  }
128
133
  if (input.button !== 0)
129
134
  return false;
130
135
  // A click on the board ends the typing in a text.
131
- if (__classPrivateFieldGet(this, _PointerController_engine, "f").state.editingTextId)
132
- __classPrivateFieldGet(this, _PointerController_engine, "f").endTextEdit();
133
- var state = __classPrivateFieldGet(this, _PointerController_engine, "f").state;
136
+ if (this.#engine.state.editingTextId)
137
+ this.#engine.endTextEdit();
138
+ const state = this.#engine.state;
134
139
  switch (state.tool) {
135
140
  case 'draw':
136
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createDrawGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input));
141
+ this.#begin(createDrawGesture(this.#context, input));
137
142
  return true;
138
143
  case 'rect':
139
144
  case 'rounded-rect':
@@ -142,433 +147,421 @@ var PointerController = /** @class */ (function () {
142
147
  case 'triangle':
143
148
  case 'arrow':
144
149
  case 'library-shape':
145
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createShapeGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input, state.tool, state.libraryShapeKey));
150
+ this.#begin(createShapeGesture(this.#context, input, state.tool, state.libraryShapeKey));
146
151
  return true;
147
152
  case 'text':
148
- return __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_pressWithTextTool).call(this, input);
153
+ return this.#pressWithTextTool(input);
149
154
  case 'erase':
150
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createEraseGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input));
155
+ this.#begin(createEraseGesture(this.#context, input));
151
156
  return true;
152
157
  default:
153
- return __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_pressWithSelectTool).call(this, input);
158
+ return this.#pressWithSelectTool(input);
154
159
  }
155
- };
160
+ }
161
+ #pressWithTextTool(input) {
162
+ const engine = this.#engine;
163
+ const world = engine.toWorld({ x: input.x, y: input.y });
164
+ // A click on a text that is already there edits it instead of making another on top.
165
+ const hit = hitTestLeaf(engine.state.scene, world, { zoom: engine.state.viewport.zoom });
166
+ if (hit && hit.type === 'text') {
167
+ if (hit.parentId)
168
+ engine.enterGroup(hit.parentId, hit.id);
169
+ engine.beginTextEdit(hit.id);
170
+ return true;
171
+ }
172
+ this.#begin(createTextGesture(this.#context, input));
173
+ return true;
174
+ }
175
+ #isDoubleClick(input) {
176
+ const previous = this.#lastClick;
177
+ return (!!previous &&
178
+ input.time - previous.time <= DOUBLE_CLICK_MS &&
179
+ distanceBetween({ x: previous.x, y: previous.y }, { x: input.x, y: input.y }) <= DOUBLE_CLICK_DISTANCE);
180
+ }
181
+ #rememberHandlePress(input, handle) {
182
+ this.#lastHandlePress = { handle, time: input.time, x: input.x, y: input.y };
183
+ }
184
+ /** Whether this press, on the handle `handle`, follows the last press on that same handle soon and close enough to be its double. */
185
+ #isDoublePressOn(input, handle) {
186
+ const previous = this.#lastHandlePress;
187
+ return (!!previous &&
188
+ previous.handle === handle &&
189
+ input.time - previous.time <= DOUBLE_CLICK_MS &&
190
+ distanceBetween({ x: previous.x, y: previous.y }, { x: input.x, y: input.y }) <= DOUBLE_CLICK_DISTANCE);
191
+ }
192
+ #pressWithSelectTool(input) {
193
+ const engine = this.#engine;
194
+ const screenPoint = { x: input.x, y: input.y };
195
+ const world = engine.toWorld(screenPoint);
196
+ const pickMode = this.#options.pickMode?.() ?? false;
197
+ // The handles of the selection come before whatever lies under them.
198
+ if (!pickMode && engine.state.selectedIds.length > 0) {
199
+ const shapes = engine.selectedShapes();
200
+ // One selected arrow has two ends and a middle to take hold of, and no frame; anywhere else on it is the arrow itself.
201
+ const arrow = singleArrowOf(shapes, engine.state.selectedIds);
202
+ const arrowHandle = arrow ? hitArrowHandle(arrow, engine.state.viewport, screenPoint, arrowHandleMetricsFor(input.pointerType)) : null;
203
+ if (arrow && arrowHandle) {
204
+ this.#begin(createArrowHandleGesture(this.#context, input, arrow, arrowHandle));
205
+ return true;
206
+ }
207
+ const frame = arrow ? null : selectionFrameOf(shapes);
208
+ if (frame) {
209
+ const viewport = engine.state.viewport;
210
+ const available = availableHandles(shapes, frame, viewport, this.#options.plainCardChrome?.() ?? false);
211
+ const hit = hitSelectionHandle(frame, viewport, screenPoint, handleMetricsFor(input.pointerType), available);
212
+ if (hit?.type === 'resize') {
213
+ // Two quick presses on the side of a text set its width to its letters, as it was when it was made.
214
+ if ((hit.handle === 'e' || hit.handle === 'w') && this.#isDoublePressOn(input, hit.handle) && engine.fitSelectedTextToContent()) {
215
+ this.#lastHandlePress = null;
216
+ return true;
217
+ }
218
+ this.#rememberHandlePress(input, hit.handle);
219
+ this.#begin(createResizeGesture(this.#context, input, hit.handle, frame, shapes));
220
+ return true;
221
+ }
222
+ if (hit?.type === 'rotate') {
223
+ // Two quick presses on the dot stand the selection upright again.
224
+ if (hit.source === 'dot' && this.#isDoublePressOn(input, 'rotate') && engine.resetSelectionRotation()) {
225
+ this.#lastHandlePress = null;
226
+ return true;
227
+ }
228
+ if (hit.source === 'dot')
229
+ this.#rememberHandlePress(input, 'rotate');
230
+ this.#begin(createRotateGesture(this.#context, input, frame, shapes, hit.source));
231
+ return true;
232
+ }
233
+ }
234
+ }
235
+ const doubleClick = this.#isDoubleClick(input);
236
+ this.#lastClick = doubleClick ? null : { time: input.time, x: input.x, y: input.y };
237
+ if (doubleClick && !pickMode && this.#handleDoubleClick(world))
238
+ return true;
239
+ const unitId = engine.hitTest(world);
240
+ if (pickMode) {
241
+ if (unitId) {
242
+ engine.select([unitId]);
243
+ this.#options.onPick?.(unitId);
244
+ }
245
+ return true;
246
+ }
247
+ // Alt-drag on an object duplicates the selection; Alt-click with no drag steps through the stack.
248
+ if (input.altKey && unitId) {
249
+ if (input.shiftKey) {
250
+ engine.toggleSelection(unitId);
251
+ if (!engine.state.selectedIds.includes(unitId))
252
+ return true;
253
+ }
254
+ const moving = engine.state.selectedIds.length > 0 ? [...engine.state.selectedIds] : [unitId];
255
+ this.#begin(createMoveGesture(this.#context, input, moving, {
256
+ onClick: () => this.#drillThrough(world),
257
+ duplicateOnDrag: true,
258
+ immovable: this.#isHeldByLiveWidget(moving)
259
+ }));
260
+ return true;
261
+ }
262
+ if (input.altKey && this.#drillThrough(world))
263
+ return true;
264
+ if (unitId) {
265
+ const wasSelected = engine.state.selectedIds.includes(unitId);
266
+ if (input.shiftKey) {
267
+ engine.toggleSelection(unitId);
268
+ // Taking an object out of the selection is the whole press.
269
+ if (!engine.state.selectedIds.includes(unitId))
270
+ return true;
271
+ }
272
+ else if (!wasSelected) {
273
+ engine.select([unitId]);
274
+ }
275
+ const moving = [...engine.state.selectedIds];
276
+ // A click on one of several selected objects, without a drag, narrows the selection to it.
277
+ const onClick = !input.shiftKey && wasSelected && moving.length > 1 ? () => engine.select([unitId]) : undefined;
278
+ this.#begin(createMoveGesture(this.#context, input, moving, { onClick, immovable: this.#isHeldByLiveWidget(moving) }));
279
+ return true;
280
+ }
281
+ // Empty board. A finger pans (a tap clears the selection); a mouse drags a marquee.
282
+ if (input.pointerType === 'touch') {
283
+ this.#begin(createPanGesture(this.#context, input, { onTap: () => engine.clearSelection() }));
284
+ return true;
285
+ }
286
+ if (!input.shiftKey)
287
+ engine.clearSelection();
288
+ this.#begin(createMarqueeGesture(this.#context, input));
289
+ return true;
290
+ }
291
+ /**
292
+ * Selects the object one step below the one that is selected, among the objects under the pointer (and the
293
+ * top one when the bottom one was reached or nothing was selected). False when there is nothing to step through.
294
+ */
295
+ #drillThrough(world) {
296
+ const engine = this.#engine;
297
+ const stack = engine.hitTestStack(world);
298
+ if (stack.length === 0)
299
+ return false;
300
+ const selected = engine.state.selectedIds;
301
+ const current = selected.length === 1 ? selected[0] : null;
302
+ const position = current ? stack.indexOf(current) : -1;
303
+ const next = stack[position <= 0 ? stack.length - 1 : position - 1];
304
+ if (next !== current)
305
+ engine.select([next]);
306
+ return true;
307
+ }
308
+ /** Whether everything the units hold is a widget card that a live widget covers: only the host's own bar moves those. */
309
+ #isHeldByLiveWidget(unitIds) {
310
+ const shapes = leafShapesOf(this.#engine.state.scene, unitIds);
311
+ return shapes.length > 0 && shapes.every((shape) => shape.type === 'widget' && !this.#engine.widgetRuntime(shape.widgetId).bodyDragAllowed);
312
+ }
313
+ /** Enters a group, edits a text, or tells the host a card was activated. True when something was done. */
314
+ #handleDoubleClick(world) {
315
+ const engine = this.#engine;
316
+ const state = engine.state;
317
+ const leaf = hitTestLeaf(state.scene, world, { zoom: state.viewport.zoom });
318
+ if (!leaf || state.pending[leaf.id])
319
+ return false;
320
+ if (leaf.type === 'text') {
321
+ if (leaf.parentId)
322
+ engine.enterGroup(leaf.parentId, leaf.id);
323
+ engine.beginTextEdit(leaf.id);
324
+ return true;
325
+ }
326
+ const unitId = engine.hitTest(world);
327
+ const unit = unitId ? engine.getObject(unitId) : undefined;
328
+ if (unit && unit.type === 'group') {
329
+ // Go into the group and pick the member that was double-clicked.
330
+ const index = engine.sceneIndex();
331
+ let member = index.get(leaf.id);
332
+ while (member && member.parentId !== unit.id)
333
+ member = member.parentId ? index.get(member.parentId) : undefined;
334
+ engine.enterGroup(unit.id, member?.id ?? leaf.id);
335
+ return true;
336
+ }
337
+ if (leaf.type === 'widget') {
338
+ this.#options.onActivate?.(leaf.id);
339
+ return true;
340
+ }
341
+ return false;
342
+ }
156
343
  /** A right click or a long press: picks the object under the pointer if it is not selected. Returns it. */
157
- PointerController.prototype.prepareContextMenu = function (screenPoint) {
158
- var engine = __classPrivateFieldGet(this, _PointerController_engine, "f");
159
- var unitId = engine.hitTest(engine.toWorld(screenPoint));
344
+ prepareContextMenu(screenPoint) {
345
+ const engine = this.#engine;
346
+ const unitId = engine.hitTest(engine.toWorld(screenPoint));
160
347
  if (unitId && !engine.state.selectedIds.includes(unitId))
161
348
  engine.select([unitId]);
162
349
  return unitId;
163
- };
350
+ }
164
351
  // ──────────────────────────────────────────────────────── move
165
- PointerController.prototype.pointerMove = function (input) {
166
- __classPrivateFieldSet(this, _PointerController_lastInput, input, "f");
167
- var tracked = __classPrivateFieldGet(this, _PointerController_pointers, "f").get(input.pointerId);
352
+ pointerMove(input) {
353
+ this.#lastInput = input;
354
+ const tracked = this.#pointers.get(input.pointerId);
168
355
  if (tracked) {
169
356
  tracked.x = input.x;
170
357
  tracked.y = input.y;
171
358
  tracked.seenAt = input.time;
172
359
  }
173
- __classPrivateFieldGet(this, _PointerController_engine, "f").lastPointerWorld = __classPrivateFieldGet(this, _PointerController_engine, "f").toWorld({ x: input.x, y: input.y });
174
- if (__classPrivateFieldGet(this, _PointerController_pinch, "f")) {
175
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_updatePinch).call(this);
360
+ this.#engine.lastPointerWorld = this.#engine.toWorld({ x: input.x, y: input.y });
361
+ if (this.#pinch) {
362
+ this.#updatePinch();
176
363
  return;
177
364
  }
178
- if (__classPrivateFieldGet(this, _PointerController_active, "f")) {
179
- if (input.pointerId === __classPrivateFieldGet(this, _PointerController_active, "f").pointerId)
180
- __classPrivateFieldGet(this, _PointerController_active, "f").move(input);
365
+ if (this.#active) {
366
+ if (input.pointerId === this.#active.pointerId)
367
+ this.#active.move(input);
181
368
  return;
182
369
  }
183
370
  if (input.pointerType === 'touch')
184
371
  return;
185
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_hover).call(this, input);
186
- };
187
- /** The pointer left the board: nothing is hovered any more. */
188
- PointerController.prototype.pointerLeave = function () {
189
- __classPrivateFieldSet(this, _PointerController_lastHover, null, "f");
190
- __classPrivateFieldGet(this, _PointerController_engine, "f").setHoverWidget(null);
191
- if (!__classPrivateFieldGet(this, _PointerController_active, "f")) {
192
- __classPrivateFieldGet(this, _PointerController_engine, "f").setHover(null);
193
- __classPrivateFieldGet(this, _PointerController_engine, "f").setRotationDotHover(false);
194
- __classPrivateFieldGet(this, _PointerController_engine, "f").setArrowHandleHover(null);
195
- if (__classPrivateFieldGet(this, _PointerController_engine, "f").state.overlay.arrowBind)
196
- __classPrivateFieldGet(this, _PointerController_engine, "f").setOverlay({ arrowBind: null });
197
- }
198
- };
199
- // ──────────────────────────────────────────────────────── release
200
- PointerController.prototype.pointerUp = function (input) {
201
- __classPrivateFieldSet(this, _PointerController_lastInput, input, "f");
202
- __classPrivateFieldGet(this, _PointerController_pointers, "f").delete(input.pointerId);
203
- if (__classPrivateFieldGet(this, _PointerController_pinch, "f")) {
204
- if (__classPrivateFieldGet(this, _PointerController_pointers, "f").size < 2)
205
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_endPinch).call(this);
372
+ this.#hover(input);
373
+ }
374
+ /** What is under the pointer when nothing is being dragged: the cursor, and the object to outline. */
375
+ #hover(input) {
376
+ const engine = this.#engine;
377
+ const state = engine.state;
378
+ if (this.#spaceHeld || (state.tool !== 'select' && state.tool !== 'arrow'))
206
379
  return;
207
- }
208
- if (__classPrivateFieldGet(this, _PointerController_active, "f") && __classPrivateFieldGet(this, _PointerController_active, "f").pointerId === input.pointerId)
209
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_finish).call(this, 'end', input);
210
- };
211
- PointerController.prototype.pointerCancel = function (input) {
212
- __classPrivateFieldGet(this, _PointerController_pointers, "f").delete(input.pointerId);
213
- if (__classPrivateFieldGet(this, _PointerController_pinch, "f")) {
214
- if (__classPrivateFieldGet(this, _PointerController_pointers, "f").size < 2)
215
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_endPinch).call(this);
380
+ if (this.#lastHover && Math.abs(this.#lastHover.x - input.x) < 1 && Math.abs(this.#lastHover.y - input.y) < 1)
216
381
  return;
217
- }
218
- if (__classPrivateFieldGet(this, _PointerController_active, "f") && __classPrivateFieldGet(this, _PointerController_active, "f").pointerId === input.pointerId)
219
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_finish).call(this, 'cancel');
220
- };
221
- // ──────────────────────────────────────────────────────── wheel
222
- /** The wheel (or two fingers on a trackpad) pans; with Ctrl or Meta, which a trackpad pinch also reports, it zooms about the pointer. */
223
- PointerController.prototype.wheel = function (input) {
224
- var unit = input.deltaMode === 1 ? WHEEL_LINE_PIXELS : input.deltaMode === 2 ? WHEEL_PAGE_PIXELS : 1;
225
- var deltaX = input.deltaX * unit;
226
- var deltaY = input.deltaY * unit;
227
- if (input.ctrlKey || input.metaKey) {
228
- var factor = Math.exp(-clampNumber(deltaY, -60, 60) * 0.01);
229
- __classPrivateFieldGet(this, _PointerController_engine, "f").zoomAt({ x: input.x, y: input.y }, __classPrivateFieldGet(this, _PointerController_engine, "f").state.viewport.zoom * factor);
382
+ this.#lastHover = { x: input.x, y: input.y };
383
+ if (state.tool === 'arrow') {
384
+ this.#hoverArrowTarget(input);
230
385
  return;
231
386
  }
232
- // A mouse wheel with Shift held scrolls sideways.
233
- if (input.shiftKey && deltaX === 0) {
234
- deltaX = deltaY;
235
- deltaY = 0;
236
- }
237
- __classPrivateFieldGet(this, _PointerController_engine, "f").panBy(-deltaX, -deltaY);
238
- };
239
- return PointerController;
240
- }());
241
- export { PointerController };
242
- _PointerController_engine = new WeakMap(), _PointerController_options = new WeakMap(), _PointerController_context = new WeakMap(), _PointerController_active = new WeakMap(), _PointerController_pinch = new WeakMap(), _PointerController_pointers = new WeakMap(), _PointerController_spaceHeld = new WeakMap(), _PointerController_lastClick = new WeakMap(), _PointerController_lastHandlePress = new WeakMap(), _PointerController_lastHover = new WeakMap(), _PointerController_lastInput = new WeakMap(), _PointerController_instances = new WeakSet(), _PointerController_cursorWhenIdle = function _PointerController_cursorWhenIdle() {
243
- return cursorForTool(__classPrivateFieldGet(this, _PointerController_engine, "f").state.tool);
244
- }, _PointerController_begin = function _PointerController_begin(gesture) {
245
- var _a, _b;
246
- __classPrivateFieldSet(this, _PointerController_active, gesture, "f");
247
- __classPrivateFieldGet(this, _PointerController_engine, "f").setHover(null);
248
- (_b = (_a = __classPrivateFieldGet(this, _PointerController_options, "f")).onGestureChange) === null || _b === void 0 ? void 0 : _b.call(_a, gesture.kind);
249
- }, _PointerController_finish = function _PointerController_finish(mode, input) {
250
- var _a, _b;
251
- var gesture = __classPrivateFieldGet(this, _PointerController_active, "f");
252
- if (!gesture)
253
- return;
254
- __classPrivateFieldSet(this, _PointerController_active, null, "f");
255
- try {
256
- if (mode === 'end' && input)
257
- gesture.end(input);
258
- else
259
- gesture.cancel();
260
- }
261
- finally {
262
- // After the gesture the frame (and dot) may have moved: look again at the pointer without waiting for the next move.
263
- __classPrivateFieldSet(this, _PointerController_lastHover, null, "f");
264
- (_b = (_a = __classPrivateFieldGet(this, _PointerController_options, "f")).onGestureChange) === null || _b === void 0 ? void 0 : _b.call(_a, null);
265
- var hoverInput = input !== null && input !== void 0 ? input : __classPrivateFieldGet(this, _PointerController_lastInput, "f");
266
- // No pointer seen yet (a gesture cancelled from the keyboard) or a finger: there is nothing to look at.
267
- if (hoverInput && hoverInput.pointerType !== 'touch') {
268
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_hover).call(this, hoverInput);
269
- }
270
- else {
271
- __classPrivateFieldGet(this, _PointerController_engine, "f").setRotationDotHover(false);
272
- __classPrivateFieldGet(this, _PointerController_engine, "f").setArrowHandleHover(null);
273
- }
274
- }
275
- }, _PointerController_pressWithTextTool = function _PointerController_pressWithTextTool(input) {
276
- var engine = __classPrivateFieldGet(this, _PointerController_engine, "f");
277
- var world = engine.toWorld({ x: input.x, y: input.y });
278
- // A click on a text that is already there edits it instead of making another on top.
279
- var hit = hitTestLeaf(engine.state.scene, world, { zoom: engine.state.viewport.zoom });
280
- if (hit && hit.type === 'text') {
281
- if (hit.parentId)
282
- engine.enterGroup(hit.parentId, hit.id);
283
- engine.beginTextEdit(hit.id);
284
- return true;
285
- }
286
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createTextGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input));
287
- return true;
288
- }, _PointerController_isDoubleClick = function _PointerController_isDoubleClick(input) {
289
- var previous = __classPrivateFieldGet(this, _PointerController_lastClick, "f");
290
- return (!!previous &&
291
- input.time - previous.time <= DOUBLE_CLICK_MS &&
292
- distanceBetween({ x: previous.x, y: previous.y }, { x: input.x, y: input.y }) <= DOUBLE_CLICK_DISTANCE);
293
- }, _PointerController_rememberHandlePress = function _PointerController_rememberHandlePress(input, handle) {
294
- __classPrivateFieldSet(this, _PointerController_lastHandlePress, { handle: handle, time: input.time, x: input.x, y: input.y }, "f");
295
- }, _PointerController_isDoublePressOn = function _PointerController_isDoublePressOn(input, handle) {
296
- var previous = __classPrivateFieldGet(this, _PointerController_lastHandlePress, "f");
297
- return (!!previous &&
298
- previous.handle === handle &&
299
- input.time - previous.time <= DOUBLE_CLICK_MS &&
300
- distanceBetween({ x: previous.x, y: previous.y }, { x: input.x, y: input.y }) <= DOUBLE_CLICK_DISTANCE);
301
- }, _PointerController_pressWithSelectTool = function _PointerController_pressWithSelectTool(input) {
302
- var _this = this;
303
- var _a, _b, _c, _d, _e, _f, _g, _h;
304
- var engine = __classPrivateFieldGet(this, _PointerController_engine, "f");
305
- var screenPoint = { x: input.x, y: input.y };
306
- var world = engine.toWorld(screenPoint);
307
- var pickMode = (_c = (_b = (_a = __classPrivateFieldGet(this, _PointerController_options, "f")).pickMode) === null || _b === void 0 ? void 0 : _b.call(_a)) !== null && _c !== void 0 ? _c : false;
308
- // The handles of the selection come before whatever lies under them.
309
- if (!pickMode && engine.state.selectedIds.length > 0) {
310
- var shapes = engine.selectedShapes();
311
- // One selected arrow has two ends and a middle to take hold of, and no frame; anywhere else on it is the arrow itself.
312
- var arrow = singleArrowOf(shapes, engine.state.selectedIds);
313
- var arrowHandle = arrow ? hitArrowHandle(arrow, engine.state.viewport, screenPoint, arrowHandleMetricsFor(input.pointerType)) : null;
314
- if (arrow && arrowHandle) {
315
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createArrowHandleGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input, arrow, arrowHandle));
316
- return true;
317
- }
318
- var frame = arrow ? null : selectionFrameOf(shapes);
319
- if (frame) {
320
- var viewport = engine.state.viewport;
321
- var available = availableHandles(shapes, frame, viewport, (_f = (_e = (_d = __classPrivateFieldGet(this, _PointerController_options, "f")).plainCardChrome) === null || _e === void 0 ? void 0 : _e.call(_d)) !== null && _f !== void 0 ? _f : false);
322
- var hit = hitSelectionHandle(frame, viewport, screenPoint, handleMetricsFor(input.pointerType), available);
323
- if ((hit === null || hit === void 0 ? void 0 : hit.type) === 'resize') {
324
- // Two quick presses on the side of a text set its width to its letters, as it was when it was made.
325
- if ((hit.handle === 'e' || hit.handle === 'w') && __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_isDoublePressOn).call(this, input, hit.handle) && engine.fitSelectedTextToContent()) {
326
- __classPrivateFieldSet(this, _PointerController_lastHandlePress, null, "f");
327
- return true;
387
+ const screenPoint = { x: input.x, y: input.y };
388
+ let overHandle = false;
389
+ let overDot = false;
390
+ let resizeHandleCursor = null;
391
+ let arrowHandle = null;
392
+ if (state.selectedIds.length > 0) {
393
+ const shapes = engine.selectedShapes();
394
+ const arrow = singleArrowOf(shapes, state.selectedIds);
395
+ if (arrow) {
396
+ arrowHandle = hitArrowHandle(arrow, state.viewport, screenPoint, arrowHandleMetricsFor(input.pointerType));
397
+ if (arrowHandle) {
398
+ overHandle = true;
328
399
  }
329
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_rememberHandlePress).call(this, input, hit.handle);
330
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createResizeGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input, hit.handle, frame, shapes));
331
- return true;
332
400
  }
333
- if ((hit === null || hit === void 0 ? void 0 : hit.type) === 'rotate') {
334
- // Two quick presses on the dot stand the selection upright again.
335
- if (hit.source === 'dot' && __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_isDoublePressOn).call(this, input, 'rotate') && engine.resetSelectionRotation()) {
336
- __classPrivateFieldSet(this, _PointerController_lastHandlePress, null, "f");
337
- return true;
401
+ const frame = arrow ? null : selectionFrameOf(shapes);
402
+ if (frame) {
403
+ const available = availableHandles(shapes, frame, state.viewport, this.#options.plainCardChrome?.() ?? false);
404
+ const hit = hitSelectionHandle(frame, state.viewport, screenPoint, handleMetricsFor(input.pointerType), available);
405
+ if (hit?.type === 'resize') {
406
+ resizeHandleCursor = cursorForHandle(hit.handle, frame.rotation);
407
+ overHandle = true;
408
+ }
409
+ else if (hit?.type === 'rotate') {
410
+ overDot = hit.source === 'dot';
411
+ overHandle = true;
338
412
  }
339
- if (hit.source === 'dot')
340
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_rememberHandlePress).call(this, input, 'rotate');
341
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createRotateGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input, frame, shapes, hit.source));
342
- return true;
343
413
  }
344
414
  }
415
+ const world = engine.toWorld(screenPoint);
416
+ const hovered = overHandle ? null : engine.hitTest(world);
417
+ const hoveredSelected = !overHandle && !!hovered && state.selectedIds.includes(hovered);
418
+ const hoveredUnselected = !overHandle && !!hovered && !state.selectedIds.includes(hovered) ? hovered : null;
419
+ const cursor = resolveHoverCursor({
420
+ spaceHeld: this.#spaceHeld,
421
+ tool: state.tool,
422
+ hoveredUnselectedId: hoveredUnselected,
423
+ hoveredSelected,
424
+ resizeHandleCursor,
425
+ overRotationDot: overDot,
426
+ overArrowHandle: !!arrowHandle,
427
+ hoverBlockedByLiveWidget: hoveredUnselected ? this.#isHeldByLiveWidget([hoveredUnselected]) : false,
428
+ selectionBlockedByLiveWidget: hoveredSelected ? this.#isHeldByLiveWidget(state.selectedIds) : false
429
+ });
430
+ engine.setHover(hovered && !state.selectedIds.includes(hovered) ? hovered : null);
431
+ engine.setRotationDotHover(overDot);
432
+ engine.setArrowHandleHover(arrowHandle);
433
+ engine.setCursor(cursor);
434
+ this.#hoverWidgetCard(overHandle ? null : hitTestLeaf(state.scene, world, { zoom: state.viewport.zoom }));
345
435
  }
346
- var doubleClick = __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_isDoubleClick).call(this, input);
347
- __classPrivateFieldSet(this, _PointerController_lastClick, doubleClick ? null : { time: input.time, x: input.x, y: input.y }, "f");
348
- if (doubleClick && !pickMode && __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_handleDoubleClick).call(this, world))
349
- return true;
350
- var unitId = engine.hitTest(world);
351
- if (pickMode) {
352
- if (unitId) {
353
- engine.select([unitId]);
354
- (_h = (_g = __classPrivateFieldGet(this, _PointerController_options, "f")).onPick) === null || _h === void 0 ? void 0 : _h.call(_g, unitId);
436
+ /**
437
+ * The Arrow tool, nothing pressed: over a shape, the board outlines it and puts a dot where the tail of an arrow pressed here would
438
+ * rest (Alt held: the arrow would be tied to nothing, so nothing is shown).
439
+ */
440
+ #hoverArrowTarget(input) {
441
+ const engine = this.#engine;
442
+ const world = engine.toWorld({ x: input.x, y: input.y });
443
+ const hint = input.altKey ? null : (pickArrowBinding(engine.state.scene, world, { zoom: engine.state.viewport.zoom })?.hint ?? null);
444
+ const shown = engine.state.overlay.arrowBind;
445
+ const same = hint === null
446
+ ? shown === null
447
+ : shown !== null && shown.targetId === hint.targetId && shown.kind === hint.kind && shown.point.x === hint.point.x && shown.point.y === hint.point.y;
448
+ if (!same)
449
+ engine.setOverlay({ arrowBind: hint });
450
+ }
451
+ /**
452
+ * The host draws the bar of a live widget over its card while the pointer is on it, and the card is picked
453
+ * just by pointing at it (the bar and the widget inside take the pointer from there).
454
+ */
455
+ #hoverWidgetCard(leaf) {
456
+ const engine = this.#engine;
457
+ const state = engine.state;
458
+ const card = leaf && leaf.type === 'widget' && leaf.parentId === null && !state.pending[leaf.id] && !engine.widgetRuntime(leaf.widgetId).orphaned ? leaf : null;
459
+ engine.setHoverWidget(card ? card.widgetId : null);
460
+ if (!card || state.overlayDragActive || state.editingTextId)
461
+ return;
462
+ if (!state.selectedIds.includes(card.id))
463
+ engine.select([card.id]);
464
+ }
465
+ /** The pointer left the board: nothing is hovered any more. */
466
+ pointerLeave() {
467
+ this.#lastHover = null;
468
+ this.#engine.setHoverWidget(null);
469
+ if (!this.#active) {
470
+ this.#engine.setHover(null);
471
+ this.#engine.setRotationDotHover(false);
472
+ this.#engine.setArrowHandleHover(null);
473
+ if (this.#engine.state.overlay.arrowBind)
474
+ this.#engine.setOverlay({ arrowBind: null });
355
475
  }
356
- return true;
357
476
  }
358
- // Alt-drag on an object duplicates the selection; Alt-click with no drag steps through the stack.
359
- if (input.altKey && unitId) {
360
- if (input.shiftKey) {
361
- engine.toggleSelection(unitId);
362
- if (!engine.state.selectedIds.includes(unitId))
363
- return true;
477
+ // ──────────────────────────────────────────────────────── release
478
+ pointerUp(input) {
479
+ this.#lastInput = input;
480
+ this.#pointers.delete(input.pointerId);
481
+ if (this.#pinch) {
482
+ if (this.#pointers.size < 2)
483
+ this.#endPinch();
484
+ return;
364
485
  }
365
- var moving = engine.state.selectedIds.length > 0 ? __spreadArray([], engine.state.selectedIds, true) : [unitId];
366
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createMoveGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input, moving, {
367
- onClick: function () { return __classPrivateFieldGet(_this, _PointerController_instances, "m", _PointerController_drillThrough).call(_this, world); },
368
- duplicateOnDrag: true,
369
- immovable: __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_isHeldByLiveWidget).call(this, moving)
370
- }));
371
- return true;
486
+ if (this.#active && this.#active.pointerId === input.pointerId)
487
+ this.#finish('end', input);
372
488
  }
373
- if (input.altKey && __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_drillThrough).call(this, world))
374
- return true;
375
- if (unitId) {
376
- var wasSelected = engine.state.selectedIds.includes(unitId);
377
- if (input.shiftKey) {
378
- engine.toggleSelection(unitId);
379
- // Taking an object out of the selection is the whole press.
380
- if (!engine.state.selectedIds.includes(unitId))
381
- return true;
489
+ pointerCancel(input) {
490
+ this.#pointers.delete(input.pointerId);
491
+ if (this.#pinch) {
492
+ if (this.#pointers.size < 2)
493
+ this.#endPinch();
494
+ return;
382
495
  }
383
- else if (!wasSelected) {
384
- engine.select([unitId]);
496
+ if (this.#active && this.#active.pointerId === input.pointerId)
497
+ this.#finish('cancel');
498
+ }
499
+ #forgetStalePointers(now) {
500
+ for (const [id, pointer] of this.#pointers) {
501
+ if (now - pointer.seenAt > STALE_POINTER_MS)
502
+ this.#pointers.delete(id);
385
503
  }
386
- var moving = __spreadArray([], engine.state.selectedIds, true);
387
- // A click on one of several selected objects, without a drag, narrows the selection to it.
388
- var onClick = !input.shiftKey && wasSelected && moving.length > 1 ? function () { return engine.select([unitId]); } : undefined;
389
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createMoveGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input, moving, { onClick: onClick, immovable: __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_isHeldByLiveWidget).call(this, moving) }));
390
- return true;
391
504
  }
392
- // Empty board. A finger pans (a tap clears the selection); a mouse drags a marquee.
393
- if (input.pointerType === 'touch') {
394
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createPanGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input, { onTap: function () { return engine.clearSelection(); } }));
395
- return true;
505
+ // ──────────────────────────────────────────────────────── pinch
506
+ #pinchGeometry() {
507
+ const [first, second] = [...this.#pointers.values()];
508
+ if (!first || !second)
509
+ return null;
510
+ return {
511
+ center: { x: (first.x + second.x) / 2, y: (first.y + second.y) / 2 },
512
+ distance: Math.max(1, Math.hypot(first.x - second.x, first.y - second.y))
513
+ };
396
514
  }
397
- if (!input.shiftKey)
398
- engine.clearSelection();
399
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_begin).call(this, createMarqueeGesture(__classPrivateFieldGet(this, _PointerController_context, "f"), input));
400
- return true;
401
- }, _PointerController_drillThrough = function _PointerController_drillThrough(world) {
402
- var engine = __classPrivateFieldGet(this, _PointerController_engine, "f");
403
- var stack = engine.hitTestStack(world);
404
- if (stack.length === 0)
405
- return false;
406
- var selected = engine.state.selectedIds;
407
- var current = selected.length === 1 ? selected[0] : null;
408
- var position = current ? stack.indexOf(current) : -1;
409
- var next = stack[position <= 0 ? stack.length - 1 : position - 1];
410
- if (next !== current)
411
- engine.select([next]);
412
- return true;
413
- }, _PointerController_isHeldByLiveWidget = function _PointerController_isHeldByLiveWidget(unitIds) {
414
- var _this = this;
415
- var shapes = leafShapesOf(__classPrivateFieldGet(this, _PointerController_engine, "f").state.scene, unitIds);
416
- return shapes.length > 0 && shapes.every(function (shape) { return shape.type === 'widget' && !__classPrivateFieldGet(_this, _PointerController_engine, "f").widgetRuntime(shape.widgetId).bodyDragAllowed; });
417
- }, _PointerController_handleDoubleClick = function _PointerController_handleDoubleClick(world) {
418
- var _a, _b, _c;
419
- var engine = __classPrivateFieldGet(this, _PointerController_engine, "f");
420
- var state = engine.state;
421
- var leaf = hitTestLeaf(state.scene, world, { zoom: state.viewport.zoom });
422
- if (!leaf || state.pending[leaf.id])
423
- return false;
424
- if (leaf.type === 'text') {
425
- if (leaf.parentId)
426
- engine.enterGroup(leaf.parentId, leaf.id);
427
- engine.beginTextEdit(leaf.id);
428
- return true;
515
+ #startPinch() {
516
+ // Whatever one finger was doing is abandoned.
517
+ if (this.#active)
518
+ this.#finish('cancel');
519
+ const geometry = this.#pinchGeometry();
520
+ if (!geometry)
521
+ return;
522
+ const viewport = this.#engine.state.viewport;
523
+ this.#pinch = {
524
+ startDistance: geometry.distance,
525
+ startZoom: viewport.zoom,
526
+ anchorWorld: this.#engine.toWorld(geometry.center)
527
+ };
528
+ this.#options.onGestureChange?.('pinch');
429
529
  }
430
- var unitId = engine.hitTest(world);
431
- var unit = unitId ? engine.getObject(unitId) : undefined;
432
- if (unit && unit.type === 'group') {
433
- // Go into the group and pick the member that was double-clicked.
434
- var index = engine.sceneIndex();
435
- var member = index.get(leaf.id);
436
- while (member && member.parentId !== unit.id)
437
- member = member.parentId ? index.get(member.parentId) : undefined;
438
- engine.enterGroup(unit.id, (_a = member === null || member === void 0 ? void 0 : member.id) !== null && _a !== void 0 ? _a : leaf.id);
439
- return true;
530
+ #updatePinch() {
531
+ const pinch = this.#pinch;
532
+ const geometry = this.#pinchGeometry();
533
+ if (!pinch || !geometry)
534
+ return;
535
+ const zoom = clampZoom(pinch.startZoom * (geometry.distance / pinch.startDistance));
536
+ // The world point that was between the fingers stays between them, so the view pans with the hand.
537
+ this.#engine.setViewport({
538
+ x: geometry.center.x - pinch.anchorWorld.x * zoom,
539
+ y: geometry.center.y - pinch.anchorWorld.y * zoom,
540
+ zoom
541
+ });
440
542
  }
441
- if (leaf.type === 'widget') {
442
- (_c = (_b = __classPrivateFieldGet(this, _PointerController_options, "f")).onActivate) === null || _c === void 0 ? void 0 : _c.call(_b, leaf.id);
443
- return true;
543
+ #endPinch() {
544
+ if (!this.#pinch)
545
+ return;
546
+ this.#pinch = null;
547
+ this.#options.onGestureChange?.(null);
444
548
  }
445
- return false;
446
- }, _PointerController_hover = function _PointerController_hover(input) {
447
- var _a, _b, _c;
448
- var engine = __classPrivateFieldGet(this, _PointerController_engine, "f");
449
- var state = engine.state;
450
- if (__classPrivateFieldGet(this, _PointerController_spaceHeld, "f") || (state.tool !== 'select' && state.tool !== 'arrow'))
451
- return;
452
- if (__classPrivateFieldGet(this, _PointerController_lastHover, "f") && Math.abs(__classPrivateFieldGet(this, _PointerController_lastHover, "f").x - input.x) < 1 && Math.abs(__classPrivateFieldGet(this, _PointerController_lastHover, "f").y - input.y) < 1)
453
- return;
454
- __classPrivateFieldSet(this, _PointerController_lastHover, { x: input.x, y: input.y }, "f");
455
- if (state.tool === 'arrow') {
456
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_hoverArrowTarget).call(this, input);
457
- return;
458
- }
459
- var screenPoint = { x: input.x, y: input.y };
460
- var overHandle = false;
461
- var overDot = false;
462
- var resizeHandleCursor = null;
463
- var arrowHandle = null;
464
- if (state.selectedIds.length > 0) {
465
- var shapes = engine.selectedShapes();
466
- var arrow = singleArrowOf(shapes, state.selectedIds);
467
- if (arrow) {
468
- arrowHandle = hitArrowHandle(arrow, state.viewport, screenPoint, arrowHandleMetricsFor(input.pointerType));
469
- if (arrowHandle) {
470
- overHandle = true;
471
- }
549
+ // ──────────────────────────────────────────────────────── wheel
550
+ /** The wheel (or two fingers on a trackpad) pans; with Ctrl or Meta, which a trackpad pinch also reports, it zooms about the pointer. */
551
+ wheel(input) {
552
+ const unit = input.deltaMode === 1 ? WHEEL_LINE_PIXELS : input.deltaMode === 2 ? WHEEL_PAGE_PIXELS : 1;
553
+ let deltaX = input.deltaX * unit;
554
+ let deltaY = input.deltaY * unit;
555
+ if (input.ctrlKey || input.metaKey) {
556
+ const factor = Math.exp(-clampNumber(deltaY, -60, 60) * 0.01);
557
+ this.#engine.zoomAt({ x: input.x, y: input.y }, this.#engine.state.viewport.zoom * factor);
558
+ return;
472
559
  }
473
- var frame = arrow ? null : selectionFrameOf(shapes);
474
- if (frame) {
475
- var available = availableHandles(shapes, frame, state.viewport, (_c = (_b = (_a = __classPrivateFieldGet(this, _PointerController_options, "f")).plainCardChrome) === null || _b === void 0 ? void 0 : _b.call(_a)) !== null && _c !== void 0 ? _c : false);
476
- var hit = hitSelectionHandle(frame, state.viewport, screenPoint, handleMetricsFor(input.pointerType), available);
477
- if ((hit === null || hit === void 0 ? void 0 : hit.type) === 'resize') {
478
- resizeHandleCursor = cursorForHandle(hit.handle, frame.rotation);
479
- overHandle = true;
480
- }
481
- else if ((hit === null || hit === void 0 ? void 0 : hit.type) === 'rotate') {
482
- overDot = hit.source === 'dot';
483
- overHandle = true;
484
- }
560
+ // A mouse wheel with Shift held scrolls sideways.
561
+ if (input.shiftKey && deltaX === 0) {
562
+ deltaX = deltaY;
563
+ deltaY = 0;
485
564
  }
565
+ this.#engine.panBy(-deltaX, -deltaY);
486
566
  }
487
- var world = engine.toWorld(screenPoint);
488
- var hovered = overHandle ? null : engine.hitTest(world);
489
- var hoveredSelected = !overHandle && !!hovered && state.selectedIds.includes(hovered);
490
- var hoveredUnselected = !overHandle && !!hovered && !state.selectedIds.includes(hovered) ? hovered : null;
491
- var cursor = resolveHoverCursor({
492
- spaceHeld: __classPrivateFieldGet(this, _PointerController_spaceHeld, "f"),
493
- tool: state.tool,
494
- hoveredUnselectedId: hoveredUnselected,
495
- hoveredSelected: hoveredSelected,
496
- resizeHandleCursor: resizeHandleCursor,
497
- overRotationDot: overDot,
498
- overArrowHandle: !!arrowHandle,
499
- hoverBlockedByLiveWidget: hoveredUnselected ? __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_isHeldByLiveWidget).call(this, [hoveredUnselected]) : false,
500
- selectionBlockedByLiveWidget: hoveredSelected ? __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_isHeldByLiveWidget).call(this, state.selectedIds) : false
501
- });
502
- engine.setHover(hovered && !state.selectedIds.includes(hovered) ? hovered : null);
503
- engine.setRotationDotHover(overDot);
504
- engine.setArrowHandleHover(arrowHandle);
505
- engine.setCursor(cursor);
506
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_hoverWidgetCard).call(this, overHandle ? null : hitTestLeaf(state.scene, world, { zoom: state.viewport.zoom }));
507
- }, _PointerController_hoverArrowTarget = function _PointerController_hoverArrowTarget(input) {
508
- var _a, _b;
509
- var engine = __classPrivateFieldGet(this, _PointerController_engine, "f");
510
- var world = engine.toWorld({ x: input.x, y: input.y });
511
- var hint = input.altKey ? null : ((_b = (_a = pickArrowBinding(engine.state.scene, world, { zoom: engine.state.viewport.zoom })) === null || _a === void 0 ? void 0 : _a.hint) !== null && _b !== void 0 ? _b : null);
512
- var shown = engine.state.overlay.arrowBind;
513
- var same = hint === null
514
- ? shown === null
515
- : shown !== null && shown.targetId === hint.targetId && shown.kind === hint.kind && shown.point.x === hint.point.x && shown.point.y === hint.point.y;
516
- if (!same)
517
- engine.setOverlay({ arrowBind: hint });
518
- }, _PointerController_hoverWidgetCard = function _PointerController_hoverWidgetCard(leaf) {
519
- var engine = __classPrivateFieldGet(this, _PointerController_engine, "f");
520
- var state = engine.state;
521
- var card = leaf && leaf.type === 'widget' && leaf.parentId === null && !state.pending[leaf.id] && !engine.widgetRuntime(leaf.widgetId).orphaned ? leaf : null;
522
- engine.setHoverWidget(card ? card.widgetId : null);
523
- if (!card || state.overlayDragActive || state.editingTextId)
524
- return;
525
- if (!state.selectedIds.includes(card.id))
526
- engine.select([card.id]);
527
- }, _PointerController_forgetStalePointers = function _PointerController_forgetStalePointers(now) {
528
- for (var _i = 0, _a = __classPrivateFieldGet(this, _PointerController_pointers, "f"); _i < _a.length; _i++) {
529
- var _b = _a[_i], id = _b[0], pointer = _b[1];
530
- if (now - pointer.seenAt > STALE_POINTER_MS)
531
- __classPrivateFieldGet(this, _PointerController_pointers, "f").delete(id);
532
- }
533
- }, _PointerController_pinchGeometry = function _PointerController_pinchGeometry() {
534
- var _a = __spreadArray([], __classPrivateFieldGet(this, _PointerController_pointers, "f").values(), true), first = _a[0], second = _a[1];
535
- if (!first || !second)
536
- return null;
537
- return {
538
- center: { x: (first.x + second.x) / 2, y: (first.y + second.y) / 2 },
539
- distance: Math.max(1, Math.hypot(first.x - second.x, first.y - second.y))
540
- };
541
- }, _PointerController_startPinch = function _PointerController_startPinch() {
542
- var _a, _b;
543
- // Whatever one finger was doing is abandoned.
544
- if (__classPrivateFieldGet(this, _PointerController_active, "f"))
545
- __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_finish).call(this, 'cancel');
546
- var geometry = __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_pinchGeometry).call(this);
547
- if (!geometry)
548
- return;
549
- var viewport = __classPrivateFieldGet(this, _PointerController_engine, "f").state.viewport;
550
- __classPrivateFieldSet(this, _PointerController_pinch, {
551
- startDistance: geometry.distance,
552
- startZoom: viewport.zoom,
553
- anchorWorld: __classPrivateFieldGet(this, _PointerController_engine, "f").toWorld(geometry.center)
554
- }, "f");
555
- (_b = (_a = __classPrivateFieldGet(this, _PointerController_options, "f")).onGestureChange) === null || _b === void 0 ? void 0 : _b.call(_a, 'pinch');
556
- }, _PointerController_updatePinch = function _PointerController_updatePinch() {
557
- var pinch = __classPrivateFieldGet(this, _PointerController_pinch, "f");
558
- var geometry = __classPrivateFieldGet(this, _PointerController_instances, "m", _PointerController_pinchGeometry).call(this);
559
- if (!pinch || !geometry)
560
- return;
561
- var zoom = clampZoom(pinch.startZoom * (geometry.distance / pinch.startDistance));
562
- // The world point that was between the fingers stays between them, so the view pans with the hand.
563
- __classPrivateFieldGet(this, _PointerController_engine, "f").setViewport({
564
- x: geometry.center.x - pinch.anchorWorld.x * zoom,
565
- y: geometry.center.y - pinch.anchorWorld.y * zoom,
566
- zoom: zoom
567
- });
568
- }, _PointerController_endPinch = function _PointerController_endPinch() {
569
- var _a, _b;
570
- if (!__classPrivateFieldGet(this, _PointerController_pinch, "f"))
571
- return;
572
- __classPrivateFieldSet(this, _PointerController_pinch, null, "f");
573
- (_b = (_a = __classPrivateFieldGet(this, _PointerController_options, "f")).onGestureChange) === null || _b === void 0 ? void 0 : _b.call(_a, null);
574
- };
567
+ }