@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.
Files changed (210) hide show
  1. package/README.md +105 -36
  2. package/dist/canvas/DomCanvas.svelte +454 -0
  3. package/dist/canvas/DomCanvas.svelte.d.ts +222 -0
  4. package/dist/canvas/boardFrame.d.ts +13 -0
  5. package/dist/canvas/boardFrame.js +13 -0
  6. package/dist/canvas/boardSurface.d.ts +9 -0
  7. package/dist/canvas/boardSurface.js +9 -0
  8. package/dist/canvas/chromeLayout.d.ts +76 -0
  9. package/dist/canvas/chromeLayout.js +121 -0
  10. package/dist/canvas/emptyBoardHint.d.ts +17 -0
  11. package/dist/canvas/emptyBoardHint.js +12 -0
  12. package/dist/canvas/eventScope.d.ts +1 -1
  13. package/dist/canvas/eventScope.js +1 -1
  14. package/dist/canvas/host/CanvasHost.d.ts +351 -0
  15. package/dist/canvas/host/CanvasHost.js +1740 -0
  16. package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
  17. package/dist/canvas/host/CanvasHostApi.js +95 -0
  18. package/dist/canvas/host/browserServices.d.ts +46 -0
  19. package/dist/canvas/host/browserServices.js +167 -0
  20. package/dist/canvas/host/conversion.d.ts +51 -0
  21. package/dist/canvas/host/conversion.js +123 -0
  22. package/dist/canvas/host/hostObject.d.ts +40 -0
  23. package/dist/canvas/host/hostObject.js +93 -0
  24. package/dist/canvas/host/pageEvents.d.ts +24 -0
  25. package/dist/canvas/host/pageEvents.js +35 -0
  26. package/dist/canvas/host/screenRects.d.ts +66 -0
  27. package/dist/canvas/host/screenRects.js +102 -0
  28. package/dist/canvas/host/strokeSnap.d.ts +67 -0
  29. package/dist/canvas/host/strokeSnap.js +165 -0
  30. package/dist/canvas/index.d.ts +7 -7
  31. package/dist/canvas/index.js +8 -8
  32. package/dist/canvas/openMarks.d.ts +17 -0
  33. package/dist/canvas/openMarks.js +26 -0
  34. package/dist/canvas/panZoomPosition.d.ts +15 -0
  35. package/dist/canvas/panZoomPosition.js +16 -0
  36. package/dist/canvas/preferredTextFont.d.ts +12 -0
  37. package/dist/canvas/preferredTextFont.js +38 -0
  38. package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
  39. package/dist/canvas/shapes/ShapeLibrary.js +13 -1
  40. package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
  41. package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
  42. package/dist/canvas/toolbarPosition.d.ts +11 -11
  43. package/dist/canvas/toolbarPosition.js +11 -24
  44. package/dist/canvas/types.d.ts +14 -0
  45. package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
  46. package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
  47. package/dist/canvas/ui/CanvasHint.svelte +20 -0
  48. package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
  49. package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
  50. package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
  51. package/dist/canvas/ui/ColorField.svelte +155 -0
  52. package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
  53. package/dist/canvas/ui/LoadNotice.svelte +39 -0
  54. package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
  55. package/dist/canvas/ui/SelectionPanel.svelte +300 -0
  56. package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
  57. package/dist/canvas/ui/SelectorBar.svelte +82 -0
  58. package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
  59. package/dist/canvas/ui/ShapesMenu.svelte +185 -0
  60. package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
  61. package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
  62. package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
  63. package/dist/canvas/ui/ToolButton.svelte +78 -0
  64. package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
  65. package/dist/canvas/ui/WidgetActions.svelte +152 -0
  66. package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
  67. package/dist/canvas/ui/ZoomControl.svelte +89 -0
  68. package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
  69. package/dist/canvas/ui/colorModel.d.ts +18 -0
  70. package/dist/canvas/ui/colorModel.js +85 -0
  71. package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
  72. package/dist/canvas/ui/colorPickerMessages.js +15 -0
  73. package/dist/canvas/ui/floatingCard.d.ts +107 -0
  74. package/dist/canvas/ui/floatingCard.js +107 -0
  75. package/dist/canvas/ui/floatingLayer.d.ts +21 -0
  76. package/dist/canvas/ui/floatingLayer.js +52 -0
  77. package/dist/canvas/ui/icons.d.ts +12 -0
  78. package/dist/canvas/ui/icons.js +7 -0
  79. package/dist/canvas/ui/keyHints.d.ts +27 -0
  80. package/dist/canvas/ui/keyHints.js +43 -0
  81. package/dist/canvas/ui/popoverSide.d.ts +5 -0
  82. package/dist/canvas/ui/popoverSide.js +20 -0
  83. package/dist/canvas/ui/selectionTraits.d.ts +61 -0
  84. package/dist/canvas/ui/selectionTraits.js +99 -0
  85. package/dist/canvas/view/CanvasStage.svelte +132 -0
  86. package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
  87. package/dist/canvas/view/EraserTrail.svelte +50 -0
  88. package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
  89. package/dist/canvas/view/ObjectView.svelte +245 -0
  90. package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
  91. package/dist/canvas/view/SelectionOverlay.svelte +487 -0
  92. package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
  93. package/dist/canvas/view/TextEditor.svelte +76 -0
  94. package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
  95. package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
  96. package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
  97. package/dist/canvas/view/erasingMarks.d.ts +13 -0
  98. package/dist/canvas/view/erasingMarks.js +34 -0
  99. package/dist/canvas/view/geometry.d.ts +169 -0
  100. package/dist/canvas/view/geometry.js +272 -0
  101. package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
  102. package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
  103. package/dist/components/index.js +1 -1
  104. package/dist/components/input-controls/ColorIC.svelte +2 -2
  105. package/dist/engine/EngineCore.d.ts +273 -0
  106. package/dist/engine/EngineCore.js +1051 -0
  107. package/dist/engine/codec/canvasDocument.d.ts +57 -0
  108. package/dist/engine/codec/canvasDocument.js +471 -0
  109. package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
  110. package/dist/engine/codec/hostReadableKeys.js +25 -0
  111. package/dist/engine/core/arrange.d.ts +20 -0
  112. package/dist/engine/core/arrange.js +81 -0
  113. package/dist/engine/core/arrow.d.ts +91 -0
  114. package/dist/engine/core/arrow.js +182 -0
  115. package/dist/engine/core/brush.d.ts +68 -0
  116. package/dist/engine/core/brush.js +173 -0
  117. package/dist/engine/core/defaults.d.ts +70 -0
  118. package/dist/engine/core/defaults.js +158 -0
  119. package/dist/engine/core/freehand.d.ts +130 -0
  120. package/dist/engine/core/freehand.js +430 -0
  121. package/dist/engine/core/geometry.d.ts +44 -0
  122. package/dist/engine/core/geometry.js +216 -0
  123. package/dist/engine/core/history.d.ts +39 -0
  124. package/dist/engine/core/history.js +69 -0
  125. package/dist/engine/core/hitTest.d.ts +54 -0
  126. package/dist/engine/core/hitTest.js +298 -0
  127. package/dist/engine/core/matrix.d.ts +47 -0
  128. package/dist/engine/core/matrix.js +101 -0
  129. package/dist/engine/core/pathData.d.ts +50 -0
  130. package/dist/engine/core/pathData.js +550 -0
  131. package/dist/engine/core/rotationReset.d.ts +10 -0
  132. package/dist/engine/core/rotationReset.js +17 -0
  133. package/dist/engine/core/scene.d.ts +99 -0
  134. package/dist/engine/core/scene.js +450 -0
  135. package/dist/engine/core/scribble.d.ts +64 -0
  136. package/dist/engine/core/scribble.js +121 -0
  137. package/dist/engine/core/shapePaths.d.ts +34 -0
  138. package/dist/engine/core/shapePaths.js +69 -0
  139. package/dist/engine/core/shapeTools.d.ts +80 -0
  140. package/dist/engine/core/shapeTools.js +161 -0
  141. package/dist/engine/core/snapping.d.ts +64 -0
  142. package/dist/engine/core/snapping.js +164 -0
  143. package/dist/engine/core/style.d.ts +40 -0
  144. package/dist/engine/core/style.js +151 -0
  145. package/dist/engine/core/transform.d.ts +79 -0
  146. package/dist/engine/core/transform.js +258 -0
  147. package/dist/engine/core/types.d.ts +238 -0
  148. package/dist/engine/core/types.js +31 -0
  149. package/dist/engine/core/viewport.d.ts +32 -0
  150. package/dist/engine/core/viewport.js +84 -0
  151. package/dist/engine/input/PointerController.d.ts +38 -0
  152. package/dist/engine/input/PointerController.js +545 -0
  153. package/dist/engine/input/arrowHandles.d.ts +38 -0
  154. package/dist/engine/input/arrowHandles.js +60 -0
  155. package/dist/engine/input/gestures.d.ts +79 -0
  156. package/dist/engine/input/gestures.js +769 -0
  157. package/dist/engine/input/hoverCursor.d.ts +29 -0
  158. package/dist/engine/input/hoverCursor.js +23 -0
  159. package/dist/engine/input/keyboard.d.ts +75 -0
  160. package/dist/engine/input/keyboard.js +264 -0
  161. package/dist/engine/input/selectionHandles.d.ts +89 -0
  162. package/dist/engine/input/selectionHandles.js +169 -0
  163. package/dist/engine/input/types.d.ts +82 -0
  164. package/dist/engine/input/types.js +5 -0
  165. package/dist/engine/render/rasterize.d.ts +74 -0
  166. package/dist/engine/render/rasterize.js +273 -0
  167. package/dist/engine/state.d.ts +124 -0
  168. package/dist/engine/state.js +47 -0
  169. package/dist/engine/text/layoutText.d.ts +53 -0
  170. package/dist/engine/text/layoutText.js +170 -0
  171. package/dist/engine/widgets.d.ts +124 -0
  172. package/dist/engine/widgets.js +345 -0
  173. package/dist/i18n/labels.d.ts +4 -15
  174. package/dist/i18n/labels.js +33 -53
  175. package/dist/i18n/language.d.ts +3 -3
  176. package/dist/i18n/messages.d.ts +51 -122
  177. package/dist/i18n/messages.js +58 -131
  178. package/dist/i18n/shortcuts.d.ts +0 -1
  179. package/dist/i18n/shortcuts.js +2 -11
  180. package/dist/index.d.ts +4 -2
  181. package/dist/index.js +5 -3
  182. package/package.json +30 -5
  183. package/dist/canvas/Canvas.svelte +0 -13363
  184. package/dist/canvas/Canvas.svelte.d.ts +0 -286
  185. package/dist/canvas/CanvasToolbar.svelte +0 -1498
  186. package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
  187. package/dist/canvas/ContextMenu.svelte +0 -305
  188. package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
  189. package/dist/canvas/PanZoomPanel.svelte +0 -478
  190. package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
  191. package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
  192. package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
  193. package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
  194. package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
  195. package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
  196. package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
  197. package/dist/canvas/shadowRootText.d.ts +0 -5
  198. package/dist/canvas/shadowRootText.js +0 -43
  199. package/dist/canvas/shapes/FrameShape.d.ts +0 -97
  200. package/dist/canvas/shapes/FrameShape.js +0 -953
  201. package/dist/canvas/shapes/ImageShape.d.ts +0 -47
  202. package/dist/canvas/shapes/ImageShape.js +0 -339
  203. package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
  204. package/dist/canvas/shapes/WidgetShape.js +0 -200
  205. package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
  206. package/dist/canvas/widgetPreviewGeometry.js +0 -66
  207. package/dist/i18n/shapeWords.d.ts +0 -30
  208. package/dist/i18n/shapeWords.js +0 -36
  209. package/dist/i18n/slots.d.ts +0 -20
  210. package/dist/i18n/slots.js +0 -40
@@ -0,0 +1,545 @@
1
+ /**
2
+ * Turns pointer samples into what the board does.
3
+ *
4
+ * The shell hands over presses, moves and releases as plain records (`PointerInput`); the controller decides
5
+ * which gesture a press starts (by the active tool, the key held, what is under the pointer) and feeds it until
6
+ * the release. Two fingers make a pinch, the wheel pans and zooms, and a double click enters a group or edits a
7
+ * text. Nothing here touches the DOM, so every rule is tested with simulated pointers.
8
+ */
9
+ import { hitTestLeaf } from '../core/hitTest.js';
10
+ import { clampNumber, distanceBetween } from '../core/geometry.js';
11
+ import { leafShapesOf } from '../core/scene.js';
12
+ import { selectionFrameOf } from '../core/transform.js';
13
+ import { clampZoom } from '../core/viewport.js';
14
+ import { cursorForTool } from '../EngineCore.js';
15
+ import { createArrowHandleGesture, createDrawGesture, createEraseGesture, createMarqueeGesture, createMoveGesture, createPanGesture, createResizeGesture, createRotateGesture, createShapeGesture, createTextGesture } from './gestures.js';
16
+ import { arrowHandleMetricsFor, hitArrowHandle, singleArrowOf } from './arrowHandles.js';
17
+ import { resolveHoverCursor } from './hoverCursor.js';
18
+ import { availableHandles, cursorForHandle, handleMetricsFor, hitSelectionHandle } from './selectionHandles.js';
19
+ /** Two presses this close in time and place are a double click. */
20
+ export const DOUBLE_CLICK_MS = 400;
21
+ export const DOUBLE_CLICK_DISTANCE = 6;
22
+ /** A pointer that has said nothing for this long is not one of two fingers: its release was lost. */
23
+ const STALE_POINTER_MS = 1500;
24
+ /** One line of a wheel in a line-based device, in pixels. */
25
+ const WHEEL_LINE_PIXELS = 16;
26
+ const WHEEL_PAGE_PIXELS = 400;
27
+ export class PointerController {
28
+ #engine;
29
+ #options;
30
+ #context;
31
+ #active = null;
32
+ #pinch = null;
33
+ #pointers = new Map();
34
+ #spaceHeld = false;
35
+ #lastClick = null;
36
+ /** 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. */
37
+ #lastHandlePress = null;
38
+ #lastHover = null;
39
+ #lastInput = null;
40
+ constructor(engine, options = {}) {
41
+ this.#engine = engine;
42
+ this.#options = options;
43
+ this.#context = { engine, options };
44
+ }
45
+ /** The kind of gesture that is running, or null. */
46
+ get gestureKind() {
47
+ return this.#pinch ? 'pinch' : (this.#active?.kind ?? null);
48
+ }
49
+ /** Space held: a press pans, whatever the tool. */
50
+ setSpaceHeld(held) {
51
+ this.#spaceHeld = held;
52
+ if (!this.#active && !this.#pinch) {
53
+ this.#engine.setCursor(held ? 'grab' : this.#cursorWhenIdle());
54
+ }
55
+ }
56
+ #cursorWhenIdle() {
57
+ return cursorForTool(this.#engine.state.tool);
58
+ }
59
+ #begin(gesture) {
60
+ this.#active = gesture;
61
+ this.#engine.setHover(null);
62
+ this.#options.onGestureChange?.(gesture.kind);
63
+ }
64
+ #finish(mode, input) {
65
+ const gesture = this.#active;
66
+ if (!gesture)
67
+ return;
68
+ this.#active = null;
69
+ try {
70
+ if (mode === 'end' && input)
71
+ gesture.end(input);
72
+ else
73
+ gesture.cancel();
74
+ }
75
+ finally {
76
+ // After the gesture the frame (and dot) may have moved: look again at the pointer without waiting for the next move.
77
+ this.#lastHover = null;
78
+ this.#options.onGestureChange?.(null);
79
+ const hoverInput = input ?? this.#lastInput;
80
+ // No pointer seen yet (a gesture cancelled from the keyboard) or a finger: there is nothing to look at.
81
+ if (hoverInput && hoverInput.pointerType !== 'touch') {
82
+ this.#hover(hoverInput);
83
+ }
84
+ else {
85
+ this.#engine.setRotationDotHover(false);
86
+ this.#engine.setArrowHandleHover(null);
87
+ }
88
+ }
89
+ }
90
+ /** Abandons the gesture that is running (Escape). True when there was one. */
91
+ cancelGesture() {
92
+ if (this.#pinch) {
93
+ this.#endPinch();
94
+ return true;
95
+ }
96
+ if (!this.#active)
97
+ return false;
98
+ this.#finish('cancel');
99
+ return true;
100
+ }
101
+ /** Shift, Alt, Ctrl or Meta changed while the pointer stayed still. */
102
+ modifiersChanged(modifiers) {
103
+ this.#active?.modifiersChanged?.(modifiers, this.#lastInput);
104
+ }
105
+ dispose() {
106
+ this.cancelGesture();
107
+ this.#pointers.clear();
108
+ }
109
+ // ──────────────────────────────────────────────────────── press
110
+ /** Handles a press. Returns whether the shell should capture the pointer and keep the browser from acting on it. */
111
+ pointerDown(input) {
112
+ this.#lastInput = input;
113
+ this.#engine.cancelRotationReset();
114
+ this.#forgetStalePointers(input.time);
115
+ this.#pointers.set(input.pointerId, { x: input.x, y: input.y, seenAt: input.time });
116
+ this.#engine.lastPointerWorld = this.#engine.toWorld({ x: input.x, y: input.y });
117
+ if (input.pointerType === 'touch' && this.#pointers.size === 2) {
118
+ this.#startPinch();
119
+ return true;
120
+ }
121
+ if (this.#pointers.size > 1 || this.#pinch)
122
+ return true;
123
+ if (this.#active)
124
+ this.#finish('cancel');
125
+ // The secondary button belongs to the context menu.
126
+ if (input.button === 2)
127
+ return false;
128
+ if (input.button === 1 || (input.button === 0 && this.#spaceHeld) || (input.button === 0 && this.#engine.state.tool === 'pan')) {
129
+ this.#begin(createPanGesture(this.#context, input));
130
+ return true;
131
+ }
132
+ if (input.button !== 0)
133
+ return false;
134
+ // A click on the board ends the typing in a text.
135
+ if (this.#engine.state.editingTextId)
136
+ this.#engine.endTextEdit();
137
+ const state = this.#engine.state;
138
+ switch (state.tool) {
139
+ case 'draw':
140
+ this.#begin(createDrawGesture(this.#context, input));
141
+ return true;
142
+ case 'rect':
143
+ case 'rounded-rect':
144
+ case 'ellipse':
145
+ case 'circle':
146
+ case 'triangle':
147
+ case 'arrow':
148
+ case 'library-shape':
149
+ this.#begin(createShapeGesture(this.#context, input, state.tool, state.libraryShapeKey));
150
+ return true;
151
+ case 'text':
152
+ return this.#pressWithTextTool(input);
153
+ case 'erase':
154
+ this.#begin(createEraseGesture(this.#context, input));
155
+ return true;
156
+ default:
157
+ return this.#pressWithSelectTool(input);
158
+ }
159
+ }
160
+ #pressWithTextTool(input) {
161
+ const engine = this.#engine;
162
+ const world = engine.toWorld({ x: input.x, y: input.y });
163
+ // A click on a text that is already there edits it instead of making another on top.
164
+ const hit = hitTestLeaf(engine.state.scene, world, { zoom: engine.state.viewport.zoom });
165
+ if (hit && hit.type === 'text') {
166
+ if (hit.parentId)
167
+ engine.enterGroup(hit.parentId, hit.id);
168
+ engine.beginTextEdit(hit.id);
169
+ return true;
170
+ }
171
+ this.#begin(createTextGesture(this.#context, input));
172
+ return true;
173
+ }
174
+ #isDoubleClick(input) {
175
+ const previous = this.#lastClick;
176
+ return (!!previous &&
177
+ input.time - previous.time <= DOUBLE_CLICK_MS &&
178
+ distanceBetween({ x: previous.x, y: previous.y }, { x: input.x, y: input.y }) <= DOUBLE_CLICK_DISTANCE);
179
+ }
180
+ #rememberHandlePress(input, handle) {
181
+ this.#lastHandlePress = { handle, time: input.time, x: input.x, y: input.y };
182
+ }
183
+ /** Whether this press, on the handle `handle`, follows the last press on that same handle soon and close enough to be its double. */
184
+ #isDoublePressOn(input, handle) {
185
+ const previous = this.#lastHandlePress;
186
+ return (!!previous &&
187
+ previous.handle === handle &&
188
+ input.time - previous.time <= DOUBLE_CLICK_MS &&
189
+ distanceBetween({ x: previous.x, y: previous.y }, { x: input.x, y: input.y }) <= DOUBLE_CLICK_DISTANCE);
190
+ }
191
+ #pressWithSelectTool(input) {
192
+ const engine = this.#engine;
193
+ const screenPoint = { x: input.x, y: input.y };
194
+ const world = engine.toWorld(screenPoint);
195
+ const pickMode = this.#options.pickMode?.() ?? false;
196
+ // The handles of the selection come before whatever lies under them.
197
+ if (!pickMode && engine.state.selectedIds.length > 0) {
198
+ const shapes = engine.selectedShapes();
199
+ // One selected arrow has two ends and a middle to take hold of, and no frame; anywhere else on it is the arrow itself.
200
+ const arrow = singleArrowOf(shapes, engine.state.selectedIds);
201
+ const arrowHandle = arrow ? hitArrowHandle(arrow, engine.state.viewport, screenPoint, arrowHandleMetricsFor(input.pointerType)) : null;
202
+ if (arrow && arrowHandle) {
203
+ this.#begin(createArrowHandleGesture(this.#context, input, arrow, arrowHandle));
204
+ return true;
205
+ }
206
+ const frame = arrow ? null : selectionFrameOf(shapes);
207
+ if (frame) {
208
+ const viewport = engine.state.viewport;
209
+ const available = availableHandles(shapes, frame, viewport, this.#options.plainCardChrome?.() ?? false);
210
+ const hit = hitSelectionHandle(frame, viewport, screenPoint, handleMetricsFor(input.pointerType), available);
211
+ if (hit?.type === 'resize') {
212
+ // Two quick presses on the side of a text set its width to its letters, as it was when it was made.
213
+ if ((hit.handle === 'e' || hit.handle === 'w') && this.#isDoublePressOn(input, hit.handle) && engine.fitSelectedTextToContent()) {
214
+ this.#lastHandlePress = null;
215
+ return true;
216
+ }
217
+ this.#rememberHandlePress(input, hit.handle);
218
+ this.#begin(createResizeGesture(this.#context, input, hit.handle, frame, shapes));
219
+ return true;
220
+ }
221
+ if (hit?.type === 'rotate') {
222
+ // Two quick presses on the dot stand the selection upright again.
223
+ if (hit.source === 'dot' && this.#isDoublePressOn(input, 'rotate') && engine.resetSelectionRotation()) {
224
+ this.#lastHandlePress = null;
225
+ return true;
226
+ }
227
+ if (hit.source === 'dot')
228
+ this.#rememberHandlePress(input, 'rotate');
229
+ this.#begin(createRotateGesture(this.#context, input, frame, shapes, hit.source));
230
+ return true;
231
+ }
232
+ }
233
+ }
234
+ const doubleClick = this.#isDoubleClick(input);
235
+ this.#lastClick = doubleClick ? null : { time: input.time, x: input.x, y: input.y };
236
+ if (doubleClick && !pickMode && this.#handleDoubleClick(world))
237
+ return true;
238
+ const unitId = engine.hitTest(world);
239
+ if (pickMode) {
240
+ if (unitId) {
241
+ engine.select([unitId]);
242
+ this.#options.onPick?.(unitId);
243
+ }
244
+ return true;
245
+ }
246
+ // Alt-drag on an object duplicates the selection; Alt-click with no drag steps through the stack.
247
+ if (input.altKey && unitId) {
248
+ if (input.shiftKey) {
249
+ engine.toggleSelection(unitId);
250
+ if (!engine.state.selectedIds.includes(unitId))
251
+ return true;
252
+ }
253
+ const moving = engine.state.selectedIds.length > 0 ? [...engine.state.selectedIds] : [unitId];
254
+ this.#begin(createMoveGesture(this.#context, input, moving, {
255
+ onClick: () => this.#drillThrough(world),
256
+ duplicateOnDrag: true,
257
+ immovable: this.#isHeldByLiveWidget(moving)
258
+ }));
259
+ return true;
260
+ }
261
+ if (input.altKey && this.#drillThrough(world))
262
+ return true;
263
+ if (unitId) {
264
+ const wasSelected = engine.state.selectedIds.includes(unitId);
265
+ if (input.shiftKey) {
266
+ engine.toggleSelection(unitId);
267
+ // Taking an object out of the selection is the whole press.
268
+ if (!engine.state.selectedIds.includes(unitId))
269
+ return true;
270
+ }
271
+ else if (!wasSelected) {
272
+ engine.select([unitId]);
273
+ }
274
+ const moving = [...engine.state.selectedIds];
275
+ // A click on one of several selected objects, without a drag, narrows the selection to it.
276
+ const onClick = !input.shiftKey && wasSelected && moving.length > 1 ? () => engine.select([unitId]) : undefined;
277
+ this.#begin(createMoveGesture(this.#context, input, moving, { onClick, immovable: this.#isHeldByLiveWidget(moving) }));
278
+ return true;
279
+ }
280
+ // Empty board. A finger pans (a tap clears the selection); a mouse drags a marquee.
281
+ if (input.pointerType === 'touch') {
282
+ this.#begin(createPanGesture(this.#context, input, { onTap: () => engine.clearSelection() }));
283
+ return true;
284
+ }
285
+ if (!input.shiftKey)
286
+ engine.clearSelection();
287
+ this.#begin(createMarqueeGesture(this.#context, input));
288
+ return true;
289
+ }
290
+ /**
291
+ * Selects the object one step below the one that is selected, among the objects under the pointer (and the
292
+ * top one when the bottom one was reached or nothing was selected). False when there is nothing to step through.
293
+ */
294
+ #drillThrough(world) {
295
+ const engine = this.#engine;
296
+ const stack = engine.hitTestStack(world);
297
+ if (stack.length === 0)
298
+ return false;
299
+ const selected = engine.state.selectedIds;
300
+ const current = selected.length === 1 ? selected[0] : null;
301
+ const position = current ? stack.indexOf(current) : -1;
302
+ const next = stack[position <= 0 ? stack.length - 1 : position - 1];
303
+ if (next !== current)
304
+ engine.select([next]);
305
+ return true;
306
+ }
307
+ /** Whether everything the units hold is a widget card that a live widget covers: only the host's own bar moves those. */
308
+ #isHeldByLiveWidget(unitIds) {
309
+ const shapes = leafShapesOf(this.#engine.state.scene, unitIds);
310
+ return shapes.length > 0 && shapes.every((shape) => shape.type === 'widget' && !this.#engine.widgetRuntime(shape.widgetId).bodyDragAllowed);
311
+ }
312
+ /** Enters a group, edits a text, or tells the host a card was activated. True when something was done. */
313
+ #handleDoubleClick(world) {
314
+ const engine = this.#engine;
315
+ const state = engine.state;
316
+ const leaf = hitTestLeaf(state.scene, world, { zoom: state.viewport.zoom });
317
+ if (!leaf || state.pending[leaf.id])
318
+ return false;
319
+ if (leaf.type === 'text') {
320
+ if (leaf.parentId)
321
+ engine.enterGroup(leaf.parentId, leaf.id);
322
+ engine.beginTextEdit(leaf.id);
323
+ return true;
324
+ }
325
+ const unitId = engine.hitTest(world);
326
+ const unit = unitId ? engine.getObject(unitId) : undefined;
327
+ if (unit && unit.type === 'group') {
328
+ // Go into the group and pick the member that was double-clicked.
329
+ const index = engine.sceneIndex();
330
+ let member = index.get(leaf.id);
331
+ while (member && member.parentId !== unit.id)
332
+ member = member.parentId ? index.get(member.parentId) : undefined;
333
+ engine.enterGroup(unit.id, member?.id ?? leaf.id);
334
+ return true;
335
+ }
336
+ if (leaf.type === 'widget') {
337
+ this.#options.onActivate?.(leaf.id);
338
+ return true;
339
+ }
340
+ return false;
341
+ }
342
+ /** A right click or a long press: picks the object under the pointer if it is not selected. Returns it. */
343
+ prepareContextMenu(screenPoint) {
344
+ const engine = this.#engine;
345
+ const unitId = engine.hitTest(engine.toWorld(screenPoint));
346
+ if (unitId && !engine.state.selectedIds.includes(unitId))
347
+ engine.select([unitId]);
348
+ return unitId;
349
+ }
350
+ // ──────────────────────────────────────────────────────── move
351
+ pointerMove(input) {
352
+ this.#lastInput = input;
353
+ const tracked = this.#pointers.get(input.pointerId);
354
+ if (tracked) {
355
+ tracked.x = input.x;
356
+ tracked.y = input.y;
357
+ tracked.seenAt = input.time;
358
+ }
359
+ this.#engine.lastPointerWorld = this.#engine.toWorld({ x: input.x, y: input.y });
360
+ if (this.#pinch) {
361
+ this.#updatePinch();
362
+ return;
363
+ }
364
+ if (this.#active) {
365
+ if (input.pointerId === this.#active.pointerId)
366
+ this.#active.move(input);
367
+ return;
368
+ }
369
+ if (input.pointerType === 'touch')
370
+ return;
371
+ this.#hover(input);
372
+ }
373
+ /** What is under the pointer when nothing is being dragged: the cursor, and the object to outline. */
374
+ #hover(input) {
375
+ const engine = this.#engine;
376
+ const state = engine.state;
377
+ if (this.#spaceHeld || state.tool !== 'select')
378
+ return;
379
+ if (this.#lastHover && Math.abs(this.#lastHover.x - input.x) < 1 && Math.abs(this.#lastHover.y - input.y) < 1)
380
+ return;
381
+ this.#lastHover = { x: input.x, y: input.y };
382
+ const screenPoint = { x: input.x, y: input.y };
383
+ let overHandle = false;
384
+ let overDot = false;
385
+ let resizeHandleCursor = null;
386
+ let arrowHandle = null;
387
+ if (state.selectedIds.length > 0) {
388
+ const shapes = engine.selectedShapes();
389
+ const arrow = singleArrowOf(shapes, state.selectedIds);
390
+ if (arrow) {
391
+ arrowHandle = hitArrowHandle(arrow, state.viewport, screenPoint, arrowHandleMetricsFor(input.pointerType));
392
+ if (arrowHandle) {
393
+ overHandle = true;
394
+ }
395
+ }
396
+ const frame = arrow ? null : selectionFrameOf(shapes);
397
+ if (frame) {
398
+ const available = availableHandles(shapes, frame, state.viewport, this.#options.plainCardChrome?.() ?? false);
399
+ const hit = hitSelectionHandle(frame, state.viewport, screenPoint, handleMetricsFor(input.pointerType), available);
400
+ if (hit?.type === 'resize') {
401
+ resizeHandleCursor = cursorForHandle(hit.handle, frame.rotation);
402
+ overHandle = true;
403
+ }
404
+ else if (hit?.type === 'rotate') {
405
+ overDot = hit.source === 'dot';
406
+ overHandle = true;
407
+ }
408
+ }
409
+ }
410
+ const world = engine.toWorld(screenPoint);
411
+ const hovered = overHandle ? null : engine.hitTest(world);
412
+ const hoveredSelected = !overHandle && !!hovered && state.selectedIds.includes(hovered);
413
+ const hoveredUnselected = !overHandle && !!hovered && !state.selectedIds.includes(hovered) ? hovered : null;
414
+ const cursor = resolveHoverCursor({
415
+ spaceHeld: this.#spaceHeld,
416
+ tool: state.tool,
417
+ hoveredUnselectedId: hoveredUnselected,
418
+ hoveredSelected,
419
+ resizeHandleCursor,
420
+ overRotationDot: overDot,
421
+ overArrowHandle: !!arrowHandle,
422
+ hoverBlockedByLiveWidget: hoveredUnselected ? this.#isHeldByLiveWidget([hoveredUnselected]) : false,
423
+ selectionBlockedByLiveWidget: hoveredSelected ? this.#isHeldByLiveWidget(state.selectedIds) : false
424
+ });
425
+ engine.setHover(hovered && !state.selectedIds.includes(hovered) ? hovered : null);
426
+ engine.setRotationDotHover(overDot);
427
+ engine.setArrowHandleHover(arrowHandle);
428
+ engine.setCursor(cursor);
429
+ this.#hoverWidgetCard(overHandle ? null : hitTestLeaf(state.scene, world, { zoom: state.viewport.zoom }));
430
+ }
431
+ /**
432
+ * The host draws the bar of a live widget over its card while the pointer is on it, and the card is picked
433
+ * just by pointing at it (the bar and the widget inside take the pointer from there).
434
+ */
435
+ #hoverWidgetCard(leaf) {
436
+ const engine = this.#engine;
437
+ const state = engine.state;
438
+ const card = leaf && leaf.type === 'widget' && leaf.parentId === null && !state.pending[leaf.id] && !engine.widgetRuntime(leaf.widgetId).orphaned ? leaf : null;
439
+ engine.setHoverWidget(card ? card.widgetId : null);
440
+ if (!card || state.overlayDragActive || state.editingTextId)
441
+ return;
442
+ if (!state.selectedIds.includes(card.id))
443
+ engine.select([card.id]);
444
+ }
445
+ /** The pointer left the board: nothing is hovered any more. */
446
+ pointerLeave() {
447
+ this.#lastHover = null;
448
+ this.#engine.setHoverWidget(null);
449
+ if (!this.#active) {
450
+ this.#engine.setHover(null);
451
+ this.#engine.setRotationDotHover(false);
452
+ this.#engine.setArrowHandleHover(null);
453
+ }
454
+ }
455
+ // ──────────────────────────────────────────────────────── release
456
+ pointerUp(input) {
457
+ this.#lastInput = input;
458
+ this.#pointers.delete(input.pointerId);
459
+ if (this.#pinch) {
460
+ if (this.#pointers.size < 2)
461
+ this.#endPinch();
462
+ return;
463
+ }
464
+ if (this.#active && this.#active.pointerId === input.pointerId)
465
+ this.#finish('end', input);
466
+ }
467
+ pointerCancel(input) {
468
+ this.#pointers.delete(input.pointerId);
469
+ if (this.#pinch) {
470
+ if (this.#pointers.size < 2)
471
+ this.#endPinch();
472
+ return;
473
+ }
474
+ if (this.#active && this.#active.pointerId === input.pointerId)
475
+ this.#finish('cancel');
476
+ }
477
+ #forgetStalePointers(now) {
478
+ for (const [id, pointer] of this.#pointers) {
479
+ if (now - pointer.seenAt > STALE_POINTER_MS)
480
+ this.#pointers.delete(id);
481
+ }
482
+ }
483
+ // ──────────────────────────────────────────────────────── pinch
484
+ #pinchGeometry() {
485
+ const [first, second] = [...this.#pointers.values()];
486
+ if (!first || !second)
487
+ return null;
488
+ return {
489
+ center: { x: (first.x + second.x) / 2, y: (first.y + second.y) / 2 },
490
+ distance: Math.max(1, Math.hypot(first.x - second.x, first.y - second.y))
491
+ };
492
+ }
493
+ #startPinch() {
494
+ // Whatever one finger was doing is abandoned.
495
+ if (this.#active)
496
+ this.#finish('cancel');
497
+ const geometry = this.#pinchGeometry();
498
+ if (!geometry)
499
+ return;
500
+ const viewport = this.#engine.state.viewport;
501
+ this.#pinch = {
502
+ startDistance: geometry.distance,
503
+ startZoom: viewport.zoom,
504
+ anchorWorld: this.#engine.toWorld(geometry.center)
505
+ };
506
+ this.#options.onGestureChange?.('pinch');
507
+ }
508
+ #updatePinch() {
509
+ const pinch = this.#pinch;
510
+ const geometry = this.#pinchGeometry();
511
+ if (!pinch || !geometry)
512
+ return;
513
+ const zoom = clampZoom(pinch.startZoom * (geometry.distance / pinch.startDistance));
514
+ // The world point that was between the fingers stays between them, so the view pans with the hand.
515
+ this.#engine.setViewport({
516
+ x: geometry.center.x - pinch.anchorWorld.x * zoom,
517
+ y: geometry.center.y - pinch.anchorWorld.y * zoom,
518
+ zoom
519
+ });
520
+ }
521
+ #endPinch() {
522
+ if (!this.#pinch)
523
+ return;
524
+ this.#pinch = null;
525
+ this.#options.onGestureChange?.(null);
526
+ }
527
+ // ──────────────────────────────────────────────────────── wheel
528
+ /** The wheel (or two fingers on a trackpad) pans; with Ctrl or Meta, which a trackpad pinch also reports, it zooms about the pointer. */
529
+ wheel(input) {
530
+ const unit = input.deltaMode === 1 ? WHEEL_LINE_PIXELS : input.deltaMode === 2 ? WHEEL_PAGE_PIXELS : 1;
531
+ let deltaX = input.deltaX * unit;
532
+ let deltaY = input.deltaY * unit;
533
+ if (input.ctrlKey || input.metaKey) {
534
+ const factor = Math.exp(-clampNumber(deltaY, -60, 60) * 0.01);
535
+ this.#engine.zoomAt({ x: input.x, y: input.y }, this.#engine.state.viewport.zoom * factor);
536
+ return;
537
+ }
538
+ // A mouse wheel with Shift held scrolls sideways.
539
+ if (input.shiftKey && deltaX === 0) {
540
+ deltaX = deltaY;
541
+ deltaY = 0;
542
+ }
543
+ this.#engine.panBy(-deltaX, -deltaY);
544
+ }
545
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Where the handles of a selected arrow are and which one a pointer is on.
3
+ *
4
+ * A selected arrow is not a box. It has no frame, no corners, no turning dot (it is never turned by hand: it points where its ends
5
+ * are); it has three round handles instead, the way tldraw shows one: one on the start, one on the end (the head), and one in the
6
+ * middle of the shaft which bends it. Like the handles of a frame they are measured on the screen, so they are the same size at
7
+ * every zoom, a finger gets bigger targets than a mouse, and a test can press one without drawing anything.
8
+ */
9
+ import type { ArrowHandleId, ArrowObject } from '../core/arrow.js';
10
+ import type { ObjectId, Point, ShapeObject, Viewport } from '../core/types.js';
11
+ import type { PointerKind } from './types.js';
12
+ export interface ArrowHandleMetrics {
13
+ /** How far from the middle of an end a press still takes it. */
14
+ endReach: number;
15
+ /** How far from the middle of the bend handle a press still takes it. */
16
+ bendReach: number;
17
+ }
18
+ export declare function arrowHandleMetricsFor(pointerType: PointerKind): ArrowHandleMetrics;
19
+ /** An end of the arrow while nobody is near it: a round handle of this diameter (screen pixels). */
20
+ export declare const ARROW_END_HANDLE_SIZE = 10;
21
+ /** The bend handle at rest: a little smaller, so the ends read as the ends. */
22
+ export declare const ARROW_BEND_HANDLE_SIZE = 8;
23
+ /** Any arrow handle under the pointer, and while it is dragged. */
24
+ export declare const ARROW_HANDLE_ACTIVE_SIZE = 14;
25
+ /** The arrow that is selected, when the selection is that arrow and nothing else: the one selection that has handles of its own. */
26
+ export declare function singleArrowOf(shapes: readonly ShapeObject[], selectedIds: readonly ObjectId[]): ArrowObject | null;
27
+ /** The places of the three handles on the screen, in pixels of the board. */
28
+ export declare function arrowHandlePositions(arrow: ArrowObject, viewport: Viewport): Record<ArrowHandleId, Point>;
29
+ /**
30
+ * How far (screen pixels) the middle of an arrow must be from each end for the bend handle to be offered: more than a finger's
31
+ * reach on both (`TOUCH_ARROW_METRICS`), so that on no kind of pointer does its area sit on an end and cover it. A shorter arrow
32
+ * has its two ends only, and the same arrow shows the same handles whoever holds the pointer.
33
+ */
34
+ export declare const BEND_HANDLE_CLEARANCE = 46;
35
+ /** Whether the bend handle is offered for an arrow whose handles are at these places. */
36
+ export declare function bendHandleAvailable(positions: Record<ArrowHandleId, Point>): boolean;
37
+ /** The handle of an arrow under a pixel of the board, or null. An end wins over the middle where their areas meet. */
38
+ export declare function hitArrowHandle(arrow: ArrowObject, viewport: Viewport, screenPoint: Point, metrics: ArrowHandleMetrics): ArrowHandleId | null;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Where the handles of a selected arrow are and which one a pointer is on.
3
+ *
4
+ * A selected arrow is not a box. It has no frame, no corners, no turning dot (it is never turned by hand: it points where its ends
5
+ * are); it has three round handles instead, the way tldraw shows one: one on the start, one on the end (the head), and one in the
6
+ * middle of the shaft which bends it. Like the handles of a frame they are measured on the screen, so they are the same size at
7
+ * every zoom, a finger gets bigger targets than a mouse, and a test can press one without drawing anything.
8
+ */
9
+ import { arrowPointsInWorld, isArrow } from '../core/arrow.js';
10
+ import { distanceBetween } from '../core/geometry.js';
11
+ import { worldToScreen } from '../core/viewport.js';
12
+ const MOUSE_ARROW_METRICS = { endReach: 12, bendReach: 10 };
13
+ const TOUCH_ARROW_METRICS = { endReach: 24, bendReach: 22 };
14
+ export function arrowHandleMetricsFor(pointerType) {
15
+ return pointerType === 'touch' ? TOUCH_ARROW_METRICS : MOUSE_ARROW_METRICS;
16
+ }
17
+ /** An end of the arrow while nobody is near it: a round handle of this diameter (screen pixels). */
18
+ export const ARROW_END_HANDLE_SIZE = 10;
19
+ /** The bend handle at rest: a little smaller, so the ends read as the ends. */
20
+ export const ARROW_BEND_HANDLE_SIZE = 8;
21
+ /** Any arrow handle under the pointer, and while it is dragged. */
22
+ export const ARROW_HANDLE_ACTIVE_SIZE = 14;
23
+ /** The arrow that is selected, when the selection is that arrow and nothing else: the one selection that has handles of its own. */
24
+ export function singleArrowOf(shapes, selectedIds) {
25
+ if (shapes.length !== 1 || selectedIds.length !== 1)
26
+ return null;
27
+ const only = shapes[0];
28
+ return isArrow(only) && only.id === selectedIds[0] ? only : null;
29
+ }
30
+ /** The places of the three handles on the screen, in pixels of the board. */
31
+ export function arrowHandlePositions(arrow, viewport) {
32
+ const points = arrowPointsInWorld(arrow);
33
+ return {
34
+ start: worldToScreen(viewport, points.start),
35
+ end: worldToScreen(viewport, points.end),
36
+ bend: worldToScreen(viewport, points.middle)
37
+ };
38
+ }
39
+ /**
40
+ * How far (screen pixels) the middle of an arrow must be from each end for the bend handle to be offered: more than a finger's
41
+ * reach on both (`TOUCH_ARROW_METRICS`), so that on no kind of pointer does its area sit on an end and cover it. A shorter arrow
42
+ * has its two ends only, and the same arrow shows the same handles whoever holds the pointer.
43
+ */
44
+ export const BEND_HANDLE_CLEARANCE = 46;
45
+ /** Whether the bend handle is offered for an arrow whose handles are at these places. */
46
+ export function bendHandleAvailable(positions) {
47
+ return distanceBetween(positions.start, positions.bend) > BEND_HANDLE_CLEARANCE && distanceBetween(positions.end, positions.bend) > BEND_HANDLE_CLEARANCE;
48
+ }
49
+ /** The handle of an arrow under a pixel of the board, or null. An end wins over the middle where their areas meet. */
50
+ export function hitArrowHandle(arrow, viewport, screenPoint, metrics) {
51
+ const positions = arrowHandlePositions(arrow, viewport);
52
+ const startDistance = distanceBetween(positions.start, screenPoint);
53
+ const endDistance = distanceBetween(positions.end, screenPoint);
54
+ const nearestEnd = startDistance <= endDistance ? 'start' : 'end';
55
+ if (Math.min(startDistance, endDistance) <= metrics.endReach)
56
+ return nearestEnd;
57
+ if (bendHandleAvailable(positions) && distanceBetween(positions.bend, screenPoint) <= metrics.bendReach)
58
+ return 'bend';
59
+ return null;
60
+ }