@widgetic/canvas 0.5.35 → 0.5.37

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 +449 -0
  3. package/dist/canvas/DomCanvas.svelte.d.ts +221 -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 +348 -0
  15. package/dist/canvas/host/CanvasHost.js +1715 -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 +53 -0
  21. package/dist/canvas/host/conversion.js +125 -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 +21 -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 +156 -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 +145 -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 +52 -124
  177. package/dist/i18n/messages.js +59 -133
  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,29 @@
1
+ /**
2
+ * The cursor over the board when nothing is being dragged, from what is under the pointer.
3
+ * Pure function so Node can test the owner's hover rules (item 19).
4
+ */
5
+ import type { ToolId } from '../state.js';
6
+ export interface HoverCursorInput {
7
+ spaceHeld: boolean;
8
+ tool: ToolId;
9
+ /** A shape under the pointer that is not selected, or null. */
10
+ hoveredUnselectedId: string | null;
11
+ /** A selected shape under the pointer (the selection includes it). */
12
+ hoveredSelected: boolean;
13
+ /** A resize or rotate handle is under the pointer. */
14
+ resizeHandleCursor: string | null;
15
+ /** The turning dot is under the pointer. */
16
+ overRotationDot: boolean;
17
+ /** An arrow end or bend handle is under the pointer. */
18
+ overArrowHandle: boolean;
19
+ /** True when the hover target is held by a live widget and must not show a pointing hand. */
20
+ hoverBlockedByLiveWidget: boolean;
21
+ /** True when the selection under the pointer is held by a live widget. */
22
+ selectionBlockedByLiveWidget: boolean;
23
+ }
24
+ /** The CSS cursor keyword for hovering a canvas object (pointing hand, not an open grab). */
25
+ export declare const HOVER_SHAPE_CURSOR = "pointer";
26
+ /**
27
+ * Resolves the board cursor from hover state. Dragging and panning are handled elsewhere; this is idle hover only.
28
+ */
29
+ export declare function resolveHoverCursor(input: HoverCursorInput): string;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The cursor over the board when nothing is being dragged, from what is under the pointer.
3
+ * Pure function so Node can test the owner's hover rules (item 19).
4
+ */
5
+ /** The CSS cursor keyword for hovering a canvas object (pointing hand, not an open grab). */
6
+ export const HOVER_SHAPE_CURSOR = 'pointer';
7
+ /**
8
+ * Resolves the board cursor from hover state. Dragging and panning are handled elsewhere; this is idle hover only.
9
+ */
10
+ export function resolveHoverCursor(input) {
11
+ if (input.spaceHeld || input.tool !== 'select')
12
+ return 'default';
13
+ if (input.resizeHandleCursor)
14
+ return input.resizeHandleCursor;
15
+ // The dot and arrow ends are held (an open hand), not the turning-arrow cursor or a pointing hand.
16
+ if (input.overRotationDot || input.overArrowHandle)
17
+ return 'grab';
18
+ if (input.hoveredSelected && !input.selectionBlockedByLiveWidget)
19
+ return HOVER_SHAPE_CURSOR;
20
+ if (input.hoveredUnselectedId && !input.hoverBlockedByLiveWidget)
21
+ return HOVER_SHAPE_CURSOR;
22
+ return 'default';
23
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * What a key does on the board.
3
+ *
4
+ * The shell hands every key press over as a plain record together with what it knows about the page (is the focus
5
+ * in a form field, is a text being typed on the board, which tools the host allows), and this module answers
6
+ * whether the board used the key. When it did, the shell stops the browser from also acting on it. The rules are
7
+ * the ones the board has always had (delete, arrows, copy and paste, group, undo, layers, save, zoom, the letter
8
+ * of each tool); they live here, apart from the DOM, so each one is tested with a plain record.
9
+ */
10
+ import type { EngineCore } from '../EngineCore.js';
11
+ import type { PointerController } from './PointerController.js';
12
+ /** The part of a keyboard event the board reads. */
13
+ export interface KeyInput {
14
+ /** `KeyboardEvent.key`: what the key types, in the person's layout. */
15
+ key: string;
16
+ /** `KeyboardEvent.code`: the physical key, the same in every layout. */
17
+ code: string;
18
+ shiftKey: boolean;
19
+ altKey: boolean;
20
+ metaKey: boolean;
21
+ ctrlKey: boolean;
22
+ repeat?: boolean;
23
+ }
24
+ /** A tool that has a letter. The frame and video tools are gone with the objects they made. */
25
+ export type ToolShortcut = 'select' | 'erase' | 'draw' | 'pan' | 'arrow' | 'rect' | 'triangle' | 'ellipse' | 'text' | 'image';
26
+ /** The letter of each tool (the same letters the hints and the list of shortcuts show). */
27
+ export declare const TOOL_LETTERS: Readonly<Record<string, ToolShortcut>>;
28
+ /** What the shell does that the engine does not: the things outside the board. */
29
+ export interface KeyboardActions {
30
+ /** Chooses a tool. The image tool opens the file picker, so the shell does it, not the engine. */
31
+ chooseTool(tool: ToolShortcut): void;
32
+ /** Cmd+S. */
33
+ save(): void;
34
+ /** The "?" key. */
35
+ toggleShortcuts(): void;
36
+ /** Closes an open menu or panel. True when something was open (the key was its to take). */
37
+ closePanels(): boolean;
38
+ /** Something was copied inside the board: the shell tells the operating system's clipboard, so a later paste is ours. */
39
+ afterCopy(): void;
40
+ /** The person pressed Space to pan: the shell shows the grid while it is held. */
41
+ spacePanChanged?(held: boolean): void;
42
+ }
43
+ export interface KeyboardHost {
44
+ engine: EngineCore;
45
+ controller: PointerController;
46
+ actions: KeyboardActions;
47
+ /** The tools the host lets the person use: the drawing group can be turned off, the shapes are always there. */
48
+ tools: {
49
+ drawing: boolean;
50
+ };
51
+ /** The focus is in a text field that is not the board's own (a form control, a content-editable). */
52
+ typingInField: boolean;
53
+ /** The page holds a selection of text (a copy then belongs to the page). */
54
+ hasTextSelection: boolean;
55
+ /** The host's element selector is open: it uses the up and down arrows for itself. */
56
+ selectorHintOpen: boolean;
57
+ }
58
+ /** The nudge of an arrow key, and of an arrow key with Shift, in world units. */
59
+ export declare const NUDGE_DISTANCE = 1;
60
+ export declare const NUDGE_DISTANCE_WITH_SHIFT = 10;
61
+ /** Keys the browser zooms the page with: kept from doing so whatever layout the person has. */
62
+ export declare function isBrowserZoomKey(input: KeyInput): boolean;
63
+ /** Whether the host lets the person use a tool (and so answers its letter). */
64
+ export declare function toolIsAvailable(tool: ToolShortcut, tools: {
65
+ drawing: boolean;
66
+ }): boolean;
67
+ /**
68
+ * A key was pressed. Returns whether the board used it: the shell then calls `preventDefault()` so the browser
69
+ * does not also scroll, zoom or select the page.
70
+ */
71
+ export declare function routeKeyDown(input: KeyInput, host: KeyboardHost): boolean;
72
+ /** A key was released. Returns whether the board used it. */
73
+ export declare function routeKeyUp(input: KeyInput, host: KeyboardHost): boolean;
74
+ /** The window lost the focus: nothing is held any more. */
75
+ export declare function releaseHeldKeys(host: KeyboardHost): void;
@@ -0,0 +1,264 @@
1
+ /**
2
+ * What a key does on the board.
3
+ *
4
+ * The shell hands every key press over as a plain record together with what it knows about the page (is the focus
5
+ * in a form field, is a text being typed on the board, which tools the host allows), and this module answers
6
+ * whether the board used the key. When it did, the shell stops the browser from also acting on it. The rules are
7
+ * the ones the board has always had (delete, arrows, copy and paste, group, undo, layers, save, zoom, the letter
8
+ * of each tool); they live here, apart from the DOM, so each one is tested with a plain record.
9
+ */
10
+ /** The letter of each tool (the same letters the hints and the list of shortcuts show). */
11
+ export const TOOL_LETTERS = Object.freeze({
12
+ V: 'select',
13
+ E: 'erase',
14
+ D: 'draw',
15
+ H: 'pan',
16
+ A: 'arrow',
17
+ R: 'rect',
18
+ G: 'triangle',
19
+ O: 'ellipse',
20
+ T: 'text',
21
+ I: 'image'
22
+ });
23
+ /** The tools that belong to the drawing bar: they are hidden, and so are their letters, when the host turns that bar off. */
24
+ const DRAWING_TOOLS = new Set(['select', 'erase', 'draw', 'arrow', 'text']);
25
+ const ARROW_STEPS = Object.freeze({
26
+ ArrowUp: [0, -1],
27
+ ArrowDown: [0, 1],
28
+ ArrowLeft: [-1, 0],
29
+ ArrowRight: [1, 0]
30
+ });
31
+ /** The nudge of an arrow key, and of an arrow key with Shift, in world units. */
32
+ export const NUDGE_DISTANCE = 1;
33
+ export const NUDGE_DISTANCE_WITH_SHIFT = 10;
34
+ /** Keys the browser zooms the page with: kept from doing so whatever layout the person has. */
35
+ export function isBrowserZoomKey(input) {
36
+ if (!(input.ctrlKey || input.metaKey))
37
+ return false;
38
+ const { key, code } = input;
39
+ return (key === '=' ||
40
+ key === '+' ||
41
+ key === '-' ||
42
+ key === '0' ||
43
+ key === '9' ||
44
+ key === 'Add' ||
45
+ key === 'Subtract' ||
46
+ code === 'Equal' ||
47
+ code === 'Minus' ||
48
+ code === 'Digit0' ||
49
+ code === 'Digit9' ||
50
+ code === 'NumpadAdd' ||
51
+ code === 'NumpadSubtract' ||
52
+ code === 'NumpadEqual');
53
+ }
54
+ function selectionExists(engine) {
55
+ return engine.state.selectedIds.length > 0;
56
+ }
57
+ /** Escape: leaves whatever is going on, one level at a time. */
58
+ function handleEscape(host) {
59
+ const { engine, controller, actions } = host;
60
+ if (actions.closePanels())
61
+ return true;
62
+ if (controller.cancelGesture())
63
+ return true;
64
+ if (engine.state.editingTextId) {
65
+ engine.endTextEdit();
66
+ engine.setTool('select');
67
+ engine.clearSelection();
68
+ return true;
69
+ }
70
+ // A field of the page that is not ours keeps its Escape.
71
+ if (host.typingInField)
72
+ return false;
73
+ if (engine.state.tool !== 'select')
74
+ engine.setTool('select');
75
+ if (engine.state.enteredGroupId) {
76
+ engine.exitGroup();
77
+ return true;
78
+ }
79
+ engine.clearSelection();
80
+ return true;
81
+ }
82
+ /** Ctrl or Cmd with a key. Returns undefined when the combination is not one of the board's. */
83
+ function handleShortcut(input, host) {
84
+ const { engine, controller, actions } = host;
85
+ const key = input.key;
86
+ const lowerKey = key.length === 1 ? key.toLowerCase() : key;
87
+ const editingText = !!engine.state.editingTextId;
88
+ const typing = host.typingInField || editingText;
89
+ // The save and the zoom work wherever the focus is.
90
+ if (lowerKey === 's') {
91
+ actions.save();
92
+ return true;
93
+ }
94
+ if (key === '+' || key === '=' || key === 'Add' || input.code === 'NumpadAdd') {
95
+ engine.zoomStep(1);
96
+ return true;
97
+ }
98
+ if (key === '-' || key === 'Subtract' || input.code === 'NumpadSubtract') {
99
+ engine.zoomStep(-1);
100
+ return true;
101
+ }
102
+ if (key === '0') {
103
+ engine.zoomReset();
104
+ return true;
105
+ }
106
+ if (key === '9') {
107
+ engine.zoomToFit();
108
+ return true;
109
+ }
110
+ // The other zoom keys of other layouts are only kept from zooming the page.
111
+ if (isBrowserZoomKey(input))
112
+ return true;
113
+ switch (lowerKey) {
114
+ case 'a':
115
+ if (typing)
116
+ return false;
117
+ if (engine.state.tool !== 'select')
118
+ engine.setTool('select');
119
+ engine.selectAll();
120
+ return true;
121
+ case 'c':
122
+ if (typing || host.hasTextSelection)
123
+ return false;
124
+ if (!selectionExists(engine))
125
+ return false;
126
+ if (engine.copySelection())
127
+ actions.afterCopy();
128
+ return true;
129
+ case 'x':
130
+ if (typing)
131
+ return false;
132
+ if (!selectionExists(engine))
133
+ return false;
134
+ if (engine.cutSelection())
135
+ actions.afterCopy();
136
+ return true;
137
+ case 'd':
138
+ if (typing)
139
+ return false;
140
+ if (!selectionExists(engine))
141
+ return false;
142
+ engine.duplicateSelection();
143
+ return true;
144
+ case 'g':
145
+ if (typing)
146
+ return false;
147
+ if (input.shiftKey)
148
+ engine.ungroupSelection();
149
+ else
150
+ engine.groupSelection();
151
+ return true;
152
+ default:
153
+ break;
154
+ }
155
+ if (input.code === 'KeyZ' || input.code === 'KeyY') {
156
+ if (typing)
157
+ return false;
158
+ // What a drag is doing is not a step yet: the history waits for the release.
159
+ if (controller.gestureKind)
160
+ return true;
161
+ const redo = input.code === 'KeyY' || input.shiftKey;
162
+ if (redo)
163
+ engine.redo();
164
+ else
165
+ engine.undo();
166
+ return true;
167
+ }
168
+ return undefined;
169
+ }
170
+ /**
171
+ * The layer keys, as the layer menu of the design lists them: Cmd or Ctrl with `]` and `[` moves one step
172
+ * (bring forward, send backward), and Shift as well takes it all the way (bring to front, send to back).
173
+ * The bare brackets keep moving one step, as they always did on this board.
174
+ */
175
+ function handleLayerKey(input, host) {
176
+ const { engine } = host;
177
+ const forward = input.code === 'BracketRight';
178
+ if (host.typingInField || engine.state.editingTextId)
179
+ return false;
180
+ if (!selectionExists(engine))
181
+ return false;
182
+ const toEnd = (input.ctrlKey || input.metaKey) && input.shiftKey;
183
+ engine.reorderSelection(forward ? (toEnd ? 'front' : 'forward') : toEnd ? 'back' : 'backward');
184
+ return true;
185
+ }
186
+ /** Whether the host lets the person use a tool (and so answers its letter). */
187
+ export function toolIsAvailable(tool, tools) {
188
+ return DRAWING_TOOLS.has(tool) ? tools.drawing : true;
189
+ }
190
+ /**
191
+ * A key was pressed. Returns whether the board used it: the shell then calls `preventDefault()` so the browser
192
+ * does not also scroll, zoom or select the page.
193
+ */
194
+ export function routeKeyDown(input, host) {
195
+ const { engine, controller, actions } = host;
196
+ const key = input.key;
197
+ if (key === 'Escape')
198
+ return handleEscape(host);
199
+ // The keys that only change what a gesture does while it runs are not answered here: the shell tells the controller.
200
+ if (key === 'Shift' || key === 'Alt' || key === 'Control' || key === 'Meta')
201
+ return false;
202
+ if (input.code === 'BracketRight' || input.code === 'BracketLeft')
203
+ return handleLayerKey(input, host);
204
+ if (input.ctrlKey || input.metaKey) {
205
+ const answered = handleShortcut(input, host);
206
+ // A combination that is not ours (Ctrl+R, Ctrl+P) is the browser's.
207
+ return answered === true;
208
+ }
209
+ const editingText = !!engine.state.editingTextId;
210
+ if (host.typingInField || editingText)
211
+ return false;
212
+ if (input.code === 'Space') {
213
+ // A held key repeats: keep the page from scrolling, and change nothing.
214
+ if (input.repeat)
215
+ return true;
216
+ if (engine.state.tool === 'pan')
217
+ return false;
218
+ controller.setSpaceHeld(true);
219
+ actions.spacePanChanged?.(true);
220
+ return true;
221
+ }
222
+ if (key === 'Delete' || key === 'Backspace') {
223
+ if (!selectionExists(engine))
224
+ return false;
225
+ void engine.deleteSelection();
226
+ return true;
227
+ }
228
+ const step = ARROW_STEPS[key];
229
+ if (step) {
230
+ if (host.selectorHintOpen)
231
+ return false;
232
+ if (!selectionExists(engine))
233
+ return false;
234
+ const distance = input.shiftKey ? NUDGE_DISTANCE_WITH_SHIFT : NUDGE_DISTANCE;
235
+ engine.nudgeSelection(step[0] * distance, step[1] * distance);
236
+ return true;
237
+ }
238
+ if (key === '?') {
239
+ actions.toggleShortcuts();
240
+ return true;
241
+ }
242
+ if (input.altKey)
243
+ return false;
244
+ const tool = key.length === 1 ? TOOL_LETTERS[key.toUpperCase()] : undefined;
245
+ if (tool && toolIsAvailable(tool, host.tools)) {
246
+ actions.chooseTool(tool);
247
+ return true;
248
+ }
249
+ return false;
250
+ }
251
+ /** A key was released. Returns whether the board used it. */
252
+ export function routeKeyUp(input, host) {
253
+ if (input.code === 'Space') {
254
+ host.controller.setSpaceHeld(false);
255
+ host.actions.spacePanChanged?.(false);
256
+ return true;
257
+ }
258
+ return false;
259
+ }
260
+ /** The window lost the focus: nothing is held any more. */
261
+ export function releaseHeldKeys(host) {
262
+ host.controller.setSpaceHeld(false);
263
+ host.actions.spacePanChanged?.(false);
264
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Where the handles of a selection are and which one a pointer is on.
3
+ *
4
+ * Done in screen pixels from the frame and the viewport, not from the DOM, so a handle is the same size at every zoom,
5
+ * a finger gets bigger targets than a mouse, and a test can press a corner without drawing anything.
6
+ *
7
+ * What a pointer may take is much more than what is drawn (the way the Memoreco canvas does it): a corner is an 8 px
8
+ * square, but the 20 px square around it takes the press; an edge has one small handle in its middle, but the whole edge
9
+ * is a 26 px strip; and below the frame a small dot turns it, which grows under the pointer. Nothing here is a DOM
10
+ * element, so the hit areas can be this generous without covering the shape: a selection that is small or thin gives
11
+ * the middle of itself back (see `innerReach`), so it can still be picked up and moved.
12
+ */
13
+ import type { ResizeHandle, SelectionFrame } from '../core/transform.js';
14
+ import type { Point, ShapeObject, Viewport } from '../core/types.js';
15
+ import type { PointerKind } from './types.js';
16
+ /** What turns the selection: the dot under the frame, or the zone just outside a corner. */
17
+ export type RotateSource = 'dot' | 'corner';
18
+ export type HandleHit = {
19
+ type: 'resize';
20
+ handle: ResizeHandle;
21
+ } | {
22
+ type: 'rotate';
23
+ source: RotateSource;
24
+ };
25
+ export interface HandleMetrics {
26
+ /** How far from the middle of a corner a press still takes it, outside the frame: the 8 px handle and 6 px around it. */
27
+ cornerReach: number;
28
+ /** How far outside an edge a press still takes the edge. The strip runs the whole edge, and reaches in by the same, as far as `innerReach` allows. */
29
+ edgeReach: number;
30
+ /** How far outside a corner a press turns the selection instead of doing nothing. */
31
+ rotateZone: number;
32
+ /** How far from the middle of the turning dot a press still takes it: the 24 px dot it grows to, and 6 px around it. */
33
+ dotReach: number;
34
+ }
35
+ export declare function handleMetricsFor(pointerType: PointerKind): HandleMetrics;
36
+ /** When false, only the dot under the frame turns the selection (corner zones stay in code but do not hit). */
37
+ export declare const ENABLE_CORNER_ROTATE_ZONES = false;
38
+ /** How far under the bottom edge of the frame the turning dot rests, to its middle: half an edge handle, 4 px of air, half the grown dot. */
39
+ export declare const ROTATION_DOT_OFFSET = 21;
40
+ /** The turning dot while nobody is near it: a small handle. */
41
+ export declare const ROTATION_DOT_REST_SIZE = 8;
42
+ /** The turning dot under the pointer, and while it turns the selection. */
43
+ export declare const ROTATION_DOT_ACTIVE_SIZE = 24;
44
+ /** A selection thinner than this (screen pixels) has no edge handles: they would cover it, and it could not be moved. */
45
+ export declare const MIN_EDGE_HANDLE_PIXELS = 16;
46
+ export declare const ALL_HANDLES: readonly ResizeHandle[];
47
+ export declare const CORNER_HANDLES: readonly ResizeHandle[];
48
+ /** A text has no top or bottom handle: its height is its lines, so only the corners (which scale the letters) and the sides (which set the width) do anything. */
49
+ export declare const TEXT_HANDLES: readonly ResizeHandle[];
50
+ /** Which handles a selection shows, before its size is looked at (`handleAvailability` does that). */
51
+ export declare function visibleHandles(shapes: readonly ShapeObject[]): readonly ResizeHandle[];
52
+ /** What a selection offers: the handles it shows, and whether the person can turn it. */
53
+ export interface HandleAvailability {
54
+ handles: readonly ResizeHandle[];
55
+ canRotate: boolean;
56
+ }
57
+ /** What a selection with nothing special offers. */
58
+ export declare const FULL_AVAILABILITY: HandleAvailability;
59
+ /**
60
+ * Whether the person can turn a selection. Not when a text is in it, alone, among other shapes, or inside a group: a text
61
+ * is read upright, so it has no turning dot under its frame and no turning zone outside its corners (the owner's call, 8 Nov
62
+ * 2026). A text that was saved turned stays as it was saved; nothing here turns it, and nothing turns it back.
63
+ */
64
+ export declare function canRotateSelection(shapes: readonly ShapeObject[]): boolean;
65
+ /**
66
+ * The handles of a selection. A widget card is plainer while the page's advanced tools are off (its four corners, no
67
+ * turning), as it always was; everything else shows what `visibleHandles` says and can be turned, unless it holds a text
68
+ * (`canRotateSelection`). A selection that is thinner than `MIN_EDGE_HANDLE_PIXELS` on the screen (`screenSize`) keeps its
69
+ * corners only.
70
+ */
71
+ export declare function handleAvailability(shapes: readonly ShapeObject[], plainCards?: boolean, screenSize?: {
72
+ width: number;
73
+ height: number;
74
+ }): HandleAvailability;
75
+ /** The handles of a selection whose frame stands on the board at this zoom: `handleAvailability` with the frame's size on the screen. */
76
+ export declare function availableHandles(shapes: readonly ShapeObject[], frame: SelectionFrame, viewport: Viewport, plainCards?: boolean): HandleAvailability;
77
+ /** Whether a handle sits on a corner of the frame. */
78
+ export declare function isCornerHandle(handle: ResizeHandle): boolean;
79
+ /**
80
+ * The handle or turning zone under a pixel of the board, or null.
81
+ *
82
+ * The turning dot comes first (it is drawn above the others), then the corners, then the edges, and last the zone just
83
+ * outside a corner. Nothing is taken for a selection that offers no turning.
84
+ */
85
+ export declare function hitSelectionHandle(frame: SelectionFrame, viewport: Viewport, screenPoint: Point, metrics: HandleMetrics, available?: HandleAvailability): HandleHit | null;
86
+ /** The CSS cursor of a resize handle on a frame turned by `rotation` degrees. */
87
+ export declare function cursorForHandle(handle: ResizeHandle, rotation: number): string;
88
+ /** The name the board maps to its turning cursor: there is no CSS keyword for it. */
89
+ export declare const ROTATE_CURSOR = "rotate";
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Where the handles of a selection are and which one a pointer is on.
3
+ *
4
+ * Done in screen pixels from the frame and the viewport, not from the DOM, so a handle is the same size at every zoom,
5
+ * a finger gets bigger targets than a mouse, and a test can press a corner without drawing anything.
6
+ *
7
+ * What a pointer may take is much more than what is drawn (the way the Memoreco canvas does it): a corner is an 8 px
8
+ * square, but the 20 px square around it takes the press; an edge has one small handle in its middle, but the whole edge
9
+ * is a 26 px strip; and below the frame a small dot turns it, which grows under the pointer. Nothing here is a DOM
10
+ * element, so the hit areas can be this generous without covering the shape: a selection that is small or thin gives
11
+ * the middle of itself back (see `innerReach`), so it can still be picked up and moved.
12
+ */
13
+ import { degreesToRadians } from '../core/matrix.js';
14
+ import { handleDirection } from '../core/transform.js';
15
+ import { worldToScreen } from '../core/viewport.js';
16
+ const MOUSE_METRICS = { cornerReach: 10, edgeReach: 13, rotateZone: 22, dotReach: 18 };
17
+ const TOUCH_METRICS = { cornerReach: 22, edgeReach: 20, rotateZone: 40, dotReach: 24 };
18
+ export function handleMetricsFor(pointerType) {
19
+ return pointerType === 'touch' ? TOUCH_METRICS : MOUSE_METRICS;
20
+ }
21
+ /** When false, only the dot under the frame turns the selection (corner zones stay in code but do not hit). */
22
+ export const ENABLE_CORNER_ROTATE_ZONES = false;
23
+ /** How far under the bottom edge of the frame the turning dot rests, to its middle: half an edge handle, 4 px of air, half the grown dot. */
24
+ export const ROTATION_DOT_OFFSET = 21;
25
+ /** The turning dot while nobody is near it: a small handle. */
26
+ export const ROTATION_DOT_REST_SIZE = 8;
27
+ /** The turning dot under the pointer, and while it turns the selection. */
28
+ export const ROTATION_DOT_ACTIVE_SIZE = 24;
29
+ /** A selection thinner than this (screen pixels) has no edge handles: they would cover it, and it could not be moved. */
30
+ export const MIN_EDGE_HANDLE_PIXELS = 16;
31
+ export const ALL_HANDLES = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'];
32
+ export const CORNER_HANDLES = ['nw', 'ne', 'se', 'sw'];
33
+ /** A text has no top or bottom handle: its height is its lines, so only the corners (which scale the letters) and the sides (which set the width) do anything. */
34
+ export const TEXT_HANDLES = ['nw', 'ne', 'se', 'sw', 'e', 'w'];
35
+ /** Which handles a selection shows, before its size is looked at (`handleAvailability` does that). */
36
+ export function visibleHandles(shapes) {
37
+ if (shapes.length === 1) {
38
+ const only = shapes[0];
39
+ if ((only.type === 'path' || only.type === 'ellipse') && only.aspectLocked)
40
+ return CORNER_HANDLES;
41
+ if (only.type === 'text')
42
+ return TEXT_HANDLES;
43
+ }
44
+ return ALL_HANDLES;
45
+ }
46
+ /** What a selection with nothing special offers. */
47
+ export const FULL_AVAILABILITY = Object.freeze({ handles: ALL_HANDLES, canRotate: true });
48
+ /**
49
+ * Whether the person can turn a selection. Not when a text is in it, alone, among other shapes, or inside a group: a text
50
+ * is read upright, so it has no turning dot under its frame and no turning zone outside its corners (the owner's call, 8 Nov
51
+ * 2026). A text that was saved turned stays as it was saved; nothing here turns it, and nothing turns it back.
52
+ */
53
+ export function canRotateSelection(shapes) {
54
+ return !shapes.some((shape) => shape.type === 'text');
55
+ }
56
+ /**
57
+ * The handles of a selection. A widget card is plainer while the page's advanced tools are off (its four corners, no
58
+ * turning), as it always was; everything else shows what `visibleHandles` says and can be turned, unless it holds a text
59
+ * (`canRotateSelection`). A selection that is thinner than `MIN_EDGE_HANDLE_PIXELS` on the screen (`screenSize`) keeps its
60
+ * corners only.
61
+ */
62
+ export function handleAvailability(shapes, plainCards = false, screenSize) {
63
+ if (plainCards && shapes.length === 1 && shapes[0].type === 'widget')
64
+ return { handles: CORNER_HANDLES, canRotate: false };
65
+ let handles = visibleHandles(shapes);
66
+ if (screenSize && Math.min(screenSize.width, screenSize.height) < MIN_EDGE_HANDLE_PIXELS) {
67
+ handles = handles.filter(isCornerHandle);
68
+ }
69
+ return { handles, canRotate: canRotateSelection(shapes) };
70
+ }
71
+ /** The handles of a selection whose frame stands on the board at this zoom: `handleAvailability` with the frame's size on the screen. */
72
+ export function availableHandles(shapes, frame, viewport, plainCards = false) {
73
+ return handleAvailability(shapes, plainCards, { width: frame.width * viewport.zoom, height: frame.height * viewport.zoom });
74
+ }
75
+ /** Whether a handle sits on a corner of the frame. */
76
+ export function isCornerHandle(handle) {
77
+ return handle.length === 2;
78
+ }
79
+ /** A point of the screen in the frame's own space: pixels from its centre, with the frame's turn undone. */
80
+ function toFrameSpace(frame, viewport, screenPoint) {
81
+ const center = worldToScreen(viewport, { x: frame.x + frame.width / 2, y: frame.y + frame.height / 2 });
82
+ const radians = -degreesToRadians(frame.rotation);
83
+ const deltaX = screenPoint.x - center.x;
84
+ const deltaY = screenPoint.y - center.y;
85
+ return {
86
+ local: {
87
+ x: deltaX * Math.cos(radians) - deltaY * Math.sin(radians),
88
+ y: deltaX * Math.sin(radians) + deltaY * Math.cos(radians)
89
+ },
90
+ halfWidth: (frame.width * viewport.zoom) / 2,
91
+ halfHeight: (frame.height * viewport.zoom) / 2
92
+ };
93
+ }
94
+ /**
95
+ * How far a handle's area reaches *into* the selection. The area is centred on the frame's edge, so it covers as much of
96
+ * the shape as it covers of the board around it; a small shape keeps its middle by giving up to a fifth of its short side
97
+ * to each handle (a 40 px shape keeps a 24 px square in its middle), and a thin one (a line) all but its two ends.
98
+ */
99
+ function innerReach(reach, shortSide) {
100
+ return Math.min(reach, shortSide / 5);
101
+ }
102
+ /**
103
+ * The handle or turning zone under a pixel of the board, or null.
104
+ *
105
+ * The turning dot comes first (it is drawn above the others), then the corners, then the edges, and last the zone just
106
+ * outside a corner. Nothing is taken for a selection that offers no turning.
107
+ */
108
+ export function hitSelectionHandle(frame, viewport, screenPoint, metrics, available = FULL_AVAILABILITY) {
109
+ const { local, halfWidth, halfHeight } = toFrameSpace(frame, viewport, screenPoint);
110
+ const { handles, canRotate } = available;
111
+ const shortSide = Math.min(halfWidth, halfHeight) * 2;
112
+ // The dot under the bottom edge.
113
+ if (canRotate && Math.abs(local.x) <= metrics.dotReach && Math.abs(local.y - (halfHeight + ROTATION_DOT_OFFSET)) <= metrics.dotReach) {
114
+ return { type: 'rotate', source: 'dot' };
115
+ }
116
+ // The corners: a square that reaches `cornerReach` outside the frame and a little into it. The nearest one wins where squares meet.
117
+ const cornerInner = innerReach(metrics.cornerReach, shortSide);
118
+ let nearest = null;
119
+ for (const handle of handles) {
120
+ if (!isCornerHandle(handle))
121
+ continue;
122
+ const direction = handleDirection(handle);
123
+ const offsetX = local.x - direction.x * halfWidth;
124
+ const offsetY = local.y - direction.y * halfHeight;
125
+ // Positive is out of the frame, on the side the corner points to.
126
+ const outwardX = offsetX * direction.x;
127
+ const outwardY = offsetY * direction.y;
128
+ if (outwardX > metrics.cornerReach || outwardX < -cornerInner || outwardY > metrics.cornerReach || outwardY < -cornerInner)
129
+ continue;
130
+ const distance = Math.hypot(offsetX, offsetY);
131
+ if (!nearest || distance < nearest.distance)
132
+ nearest = { handle, distance };
133
+ }
134
+ if (nearest)
135
+ return { type: 'resize', handle: nearest.handle };
136
+ // The edges: a strip along the whole edge, between the corners.
137
+ const edgeInner = innerReach(metrics.edgeReach, shortSide);
138
+ const onEdge = (outward) => outward <= metrics.edgeReach && outward >= -edgeInner;
139
+ const withinWidth = Math.abs(local.x) <= halfWidth;
140
+ const withinHeight = Math.abs(local.y) <= halfHeight;
141
+ if (withinWidth && handles.includes('n') && onEdge(-(local.y + halfHeight)))
142
+ return { type: 'resize', handle: 'n' };
143
+ if (withinWidth && handles.includes('s') && onEdge(local.y - halfHeight))
144
+ return { type: 'resize', handle: 's' };
145
+ if (withinHeight && handles.includes('w') && onEdge(-(local.x + halfWidth)))
146
+ return { type: 'resize', handle: 'w' };
147
+ if (withinHeight && handles.includes('e') && onEdge(local.x - halfWidth))
148
+ return { type: 'resize', handle: 'e' };
149
+ // Just outside a corner the selection turns (optional: the dot below the frame is the main control).
150
+ if (ENABLE_CORNER_ROTATE_ZONES && canRotate && (!withinWidth || !withinHeight)) {
151
+ for (const handle of CORNER_HANDLES) {
152
+ const direction = handleDirection(handle);
153
+ if (Math.hypot(local.x - direction.x * halfWidth, local.y - direction.y * halfHeight) <= metrics.rotateZone) {
154
+ return { type: 'rotate', source: 'corner' };
155
+ }
156
+ }
157
+ }
158
+ return null;
159
+ }
160
+ /** The CSS cursor of a resize handle on a frame turned by `rotation` degrees. */
161
+ export function cursorForHandle(handle, rotation) {
162
+ const direction = handleDirection(handle);
163
+ const angle = ((Math.atan2(direction.y, direction.x) * 180) / Math.PI + rotation + 360 * 4) % 180;
164
+ // 0: along x, 45: down-right, 90: along y, 135: down-left, measured on a screen where y grows downward.
165
+ const step = Math.round(angle / 45) % 4;
166
+ return ['ew-resize', 'nwse-resize', 'ns-resize', 'nesw-resize'][step];
167
+ }
168
+ /** The name the board maps to its turning cursor: there is no CSS keyword for it. */
169
+ export const ROTATE_CURSOR = 'rotate';