@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,24 @@
1
+ /**
2
+ * The two events the canvas sends to its page, on `window`.
3
+ *
4
+ * The widget creator listens to both, so their names and what they carry are part of the host contract:
5
+ *
6
+ * - `widgetic:canvas-select-tool` (no detail): the Select tool was chosen. The creator closes the element selector of every
7
+ * widget when it hears it.
8
+ * - `widgetic:widget-shape-pointer-block` (`{ widgetId, active }`): a widget card is being stretched (`active: true`, with
9
+ * the widget's id) or the stretch ended (`active: false`, `widgetId: null`). The creator lets the pointer through the live
10
+ * widget laid over the card while the stretch lasts, so the iframe does not take the gesture.
11
+ *
12
+ * Kept free of Svelte so that Node can test it. A page rendered on the server has no `window` and gets nothing.
13
+ */
14
+ export declare const CANVAS_SELECT_TOOL_EVENT = "widgetic:canvas-select-tool";
15
+ export declare const WIDGET_POINTER_BLOCK_EVENT = "widgetic:widget-shape-pointer-block";
16
+ export interface WidgetPointerBlockDetail {
17
+ /** The widget whose card is being stretched, or null when the stretch ended. */
18
+ widgetId: string | null;
19
+ active: boolean;
20
+ }
21
+ /** The Select tool was chosen. */
22
+ export declare function announceSelectTool(): void;
23
+ /** A widget card is being stretched, or no longer is. */
24
+ export declare function announceWidgetPointerBlock(detail: WidgetPointerBlockDetail): void;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The two events the canvas sends to its page, on `window`.
3
+ *
4
+ * The widget creator listens to both, so their names and what they carry are part of the host contract:
5
+ *
6
+ * - `widgetic:canvas-select-tool` (no detail): the Select tool was chosen. The creator closes the element selector of every
7
+ * widget when it hears it.
8
+ * - `widgetic:widget-shape-pointer-block` (`{ widgetId, active }`): a widget card is being stretched (`active: true`, with
9
+ * the widget's id) or the stretch ended (`active: false`, `widgetId: null`). The creator lets the pointer through the live
10
+ * widget laid over the card while the stretch lasts, so the iframe does not take the gesture.
11
+ *
12
+ * Kept free of Svelte so that Node can test it. A page rendered on the server has no `window` and gets nothing.
13
+ */
14
+ import { canvasWarn } from '../canvasLogger.js';
15
+ export const CANVAS_SELECT_TOOL_EVENT = 'widgetic:canvas-select-tool';
16
+ export const WIDGET_POINTER_BLOCK_EVENT = 'widgetic:widget-shape-pointer-block';
17
+ function dispatchOnWindow(name, detail) {
18
+ if (typeof window === 'undefined' || typeof CustomEvent === 'undefined')
19
+ return;
20
+ try {
21
+ window.dispatchEvent(detail === undefined ? new CustomEvent(name) : new CustomEvent(name, { detail }));
22
+ }
23
+ catch (error) {
24
+ // A page that cannot take the event keeps its canvas.
25
+ canvasWarn(`Canvas: could not send ${name}`, error);
26
+ }
27
+ }
28
+ /** The Select tool was chosen. */
29
+ export function announceSelectTool() {
30
+ dispatchOnWindow(CANVAS_SELECT_TOOL_EVENT);
31
+ }
32
+ /** A widget card is being stretched, or no longer is. */
33
+ export function announceWidgetPointerBlock(detail) {
34
+ dispatchOnWindow(WIDGET_POINTER_BLOCK_EVENT, { widgetId: detail.widgetId === null ? null : String(detail.widgetId), active: detail.active });
35
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Where things on the board are on the page: the numbers the host needs to lay its own elements over the board
3
+ * (the live widget over a card, the Widget Details panel beside it).
4
+ *
5
+ * Positions are CSS pixels of the page viewport, from `getBoundingClientRect()` of the board. The board's own layout
6
+ * size is passed too, so a board inside a scaled parent (a host that zooms its whole screen) still reports the
7
+ * pixels that are really on the screen.
8
+ *
9
+ * Kept free of Svelte and the DOM so Node can test it.
10
+ */
11
+ import type { Matrix } from '../../engine/core/types.js';
12
+ import type { Rect, Size, Viewport } from '../../engine/core/types.js';
13
+ import type { ShapeObject } from '../../engine/core/types.js';
14
+ /** The board element on the page: its rectangle in the viewport and its layout size (before any CSS scale). */
15
+ export interface BoardFrame {
16
+ left: number;
17
+ top: number;
18
+ width: number;
19
+ height: number;
20
+ /** The size the board lays itself out in. Equal to width/height unless a parent scales it. */
21
+ layoutWidth?: number;
22
+ layoutHeight?: number;
23
+ }
24
+ /** A box on the page plus the board it is on (what `getWidgetShapeScreenRect` and `getObjectScreenRect` answer). */
25
+ export interface ScreenRect {
26
+ left: number;
27
+ top: number;
28
+ width: number;
29
+ height: number;
30
+ viewportLeft: number;
31
+ viewportTop: number;
32
+ viewportWidth: number;
33
+ viewportHeight: number;
34
+ }
35
+ /** Where a widget card is, and how to move a live widget onto it. `matrix` maps the card's own pixels (0..localWidth by 0..localHeight) to the page. */
36
+ export interface WidgetPreviewGeometry {
37
+ left: number;
38
+ top: number;
39
+ width: number;
40
+ height: number;
41
+ localWidth: number;
42
+ localHeight: number;
43
+ matrix: [number, number, number, number, number, number];
44
+ }
45
+ /** One entry of `listWidgetShapeScreenRects()`. */
46
+ export interface WidgetScreenRect extends WidgetPreviewGeometry {
47
+ widgetId: string;
48
+ hovered: boolean;
49
+ }
50
+ /** A point of the world on the page. */
51
+ export declare function worldPointToPage(viewport: Viewport, frame: BoardFrame, x: number, y: number): {
52
+ x: number;
53
+ y: number;
54
+ };
55
+ /** The matrix from a box's own pixels to the page, or null when something is not a finite number. */
56
+ export declare function boxToPageMatrix(box: Rect & {
57
+ rotation?: number;
58
+ }, viewport: Viewport, frame: BoardFrame): Matrix | null;
59
+ /** The geometry of a card on the page. Null when the numbers are not usable. */
60
+ export declare function widgetPreviewGeometry(shape: ShapeObject, viewport: Viewport, frame: BoardFrame): WidgetPreviewGeometry | null;
61
+ /** The box of any object on the page (a turned box is measured by its corners), with the board it sits on. */
62
+ export declare function screenRectOfShape(shape: ShapeObject, viewport: Viewport, frame: BoardFrame): ScreenRect;
63
+ /** The box of a rectangle of the world on the page, with the board it sits on. */
64
+ export declare function screenRectOfBounds(bounds: Rect, viewport: Viewport, frame: BoardFrame): ScreenRect;
65
+ /** The size of the board in layout pixels, for the engine (the board before any CSS scale). */
66
+ export declare function layoutSizeOf(frame: BoardFrame): Size;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Where things on the board are on the page: the numbers the host needs to lay its own elements over the board
3
+ * (the live widget over a card, the Widget Details panel beside it).
4
+ *
5
+ * Positions are CSS pixels of the page viewport, from `getBoundingClientRect()` of the board. The board's own layout
6
+ * size is passed too, so a board inside a scaled parent (a host that zooms its whole screen) still reports the
7
+ * pixels that are really on the screen.
8
+ *
9
+ * Kept free of Svelte and the DOM so Node can test it.
10
+ */
11
+ import { boxToWorldMatrix } from '../../engine/core/matrix.js';
12
+ import { shapeBounds } from '../../engine/core/scene.js';
13
+ function cssScaleOf(frame) {
14
+ const layoutWidth = frame.layoutWidth && frame.layoutWidth > 0 ? frame.layoutWidth : frame.width;
15
+ const layoutHeight = frame.layoutHeight && frame.layoutHeight > 0 ? frame.layoutHeight : frame.height;
16
+ return {
17
+ scaleX: layoutWidth > 0 && frame.width > 0 ? frame.width / layoutWidth : 1,
18
+ scaleY: layoutHeight > 0 && frame.height > 0 ? frame.height / layoutHeight : 1
19
+ };
20
+ }
21
+ /** A point of the world on the page. */
22
+ export function worldPointToPage(viewport, frame, x, y) {
23
+ const { scaleX, scaleY } = cssScaleOf(frame);
24
+ return {
25
+ x: frame.left + (viewport.x + x * viewport.zoom) * scaleX,
26
+ y: frame.top + (viewport.y + y * viewport.zoom) * scaleY
27
+ };
28
+ }
29
+ /** The matrix from a box's own pixels to the page, or null when something is not a finite number. */
30
+ export function boxToPageMatrix(box, viewport, frame) {
31
+ const { scaleX, scaleY } = cssScaleOf(frame);
32
+ const world = boxToWorldMatrix(box);
33
+ // page = frame + scale * (viewport.translate + zoom * world)
34
+ const zoomX = viewport.zoom * scaleX;
35
+ const zoomY = viewport.zoom * scaleY;
36
+ const matrix = [
37
+ world[0] * zoomX,
38
+ world[1] * zoomY,
39
+ world[2] * zoomX,
40
+ world[3] * zoomY,
41
+ frame.left + viewport.x * scaleX + world[4] * zoomX,
42
+ frame.top + viewport.y * scaleY + world[5] * zoomY
43
+ ];
44
+ return matrix.every(Number.isFinite) ? matrix : null;
45
+ }
46
+ /** The geometry of a card on the page. Null when the numbers are not usable. */
47
+ export function widgetPreviewGeometry(shape, viewport, frame) {
48
+ const matrix = boxToPageMatrix(shape, viewport, frame);
49
+ if (!matrix)
50
+ return null;
51
+ const localWidth = shape.width || 1;
52
+ const localHeight = shape.height || 1;
53
+ const corners = [
54
+ [0, 0],
55
+ [localWidth, 0],
56
+ [0, localHeight],
57
+ [localWidth, localHeight]
58
+ ].map(([x, y]) => ({
59
+ x: matrix[0] * x + matrix[2] * y + matrix[4],
60
+ y: matrix[1] * x + matrix[3] * y + matrix[5]
61
+ }));
62
+ const xs = corners.map((corner) => corner.x);
63
+ const ys = corners.map((corner) => corner.y);
64
+ const left = Math.min(...xs);
65
+ const top = Math.min(...ys);
66
+ return {
67
+ left,
68
+ top,
69
+ width: Math.max(...xs) - left,
70
+ height: Math.max(...ys) - top,
71
+ localWidth,
72
+ localHeight,
73
+ // The old canvas answered with the matrix of the card's pixels, one unit per local pixel.
74
+ matrix: [matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]]
75
+ };
76
+ }
77
+ /** The box of any object on the page (a turned box is measured by its corners), with the board it sits on. */
78
+ export function screenRectOfShape(shape, viewport, frame) {
79
+ return screenRectOfBounds(shapeBounds(shape), viewport, frame);
80
+ }
81
+ /** The box of a rectangle of the world on the page, with the board it sits on. */
82
+ export function screenRectOfBounds(bounds, viewport, frame) {
83
+ const topLeft = worldPointToPage(viewport, frame, bounds.x, bounds.y);
84
+ const bottomRight = worldPointToPage(viewport, frame, bounds.x + bounds.width, bounds.y + bounds.height);
85
+ return {
86
+ left: Math.min(topLeft.x, bottomRight.x),
87
+ top: Math.min(topLeft.y, bottomRight.y),
88
+ width: Math.abs(bottomRight.x - topLeft.x),
89
+ height: Math.abs(bottomRight.y - topLeft.y),
90
+ viewportLeft: frame.left,
91
+ viewportTop: frame.top,
92
+ viewportWidth: frame.width,
93
+ viewportHeight: frame.height
94
+ };
95
+ }
96
+ /** The size of the board in layout pixels, for the engine (the board before any CSS scale). */
97
+ export function layoutSizeOf(frame) {
98
+ return {
99
+ width: frame.layoutWidth && frame.layoutWidth > 0 ? frame.layoutWidth : frame.width,
100
+ height: frame.layoutHeight && frame.layoutHeight > 0 ? frame.layoutHeight : frame.height
101
+ };
102
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Shape snap: swaps a freehand stroke the geometry recognises for the clean shape.
3
+ *
4
+ * The geometry (`shapes/strokeShapeSnap.ts`) decides on its own whenever it can; only a stroke whose
5
+ * measurements are genuinely ambiguous is handed to the host's recognizer, and only a shape the geometry
6
+ * allowed can ever be applied. Nothing here may be fatal: no recognizer, a declined or ruled-out
7
+ * answer, a stroke that was moved or removed while the host was thinking, all leave the drawing exactly as
8
+ * the person made it. Whether a finished stroke is looked at at all is `isShapeSnapOn` (off unless the page
9
+ * asked for it).
10
+ *
11
+ * The functions are plain TypeScript over the engine's records, so they run (and are tested) in Node.
12
+ */
13
+ import type { ObjectId, PathObject, Point, ShapeObject } from '../../engine/core/types.js';
14
+ import type { EngineCore } from '../../engine/EngineCore.js';
15
+ import type { CanvasSnapShape, StrokeMeasurements, StrokePoint } from '../shapes/strokeShapeSnap.js';
16
+ import type { StrokeRecognizer } from '../types.js';
17
+ /** Which shape the stroke becomes, who decided, and how tilted the stroke was (for the log). */
18
+ export interface SnapDecision {
19
+ shape: CanvasSnapShape;
20
+ source: 'geometry' | 'recognizer';
21
+ axisTilt: number;
22
+ }
23
+ /** What the snap needs to know from the host while it decides. */
24
+ export interface SnapContext {
25
+ /** The host's model call, or null when it has none. */
26
+ recognize: StrokeRecognizer | null;
27
+ /** Writes a line to the host's log. */
28
+ log?: (message: string, details?: unknown) => void;
29
+ }
30
+ /**
31
+ * Whether a finished stroke may be snapped at all. It is **off unless the page asks**: the prop `shapeSnap` is false
32
+ * by default, and a page that wants the behaviour passes true. The package's own flag
33
+ * (`CANVAS_SHAPE_SNAP_ENABLED`, false as shipped) turns it on for every page when it is flipped, which is the one
34
+ * line to roll the behaviour out or back. The flag is a parameter so a test can hold both of its values.
35
+ */
36
+ export declare function isShapeSnapOn(pageAsked: boolean, packageFlag?: boolean): boolean;
37
+ /**
38
+ * The points of a stroke in the coordinates of its own box. A shape is recognised by what the hand did, as the pointer
39
+ * reported it (`handLine`, in the units of the board): the line a brush stroke holds is smoothed, which rounds the very
40
+ * corners a rectangle is told by. Without it, a brush stroke is measured through the line it holds (its path is the
41
+ * outline of that line, with an edge on each side), any other path through its commands.
42
+ */
43
+ export declare function strokeMeasurementPoints(stroke: PathObject, handLine?: readonly Point[]): StrokePoint[];
44
+ /** What the geometry says about a stroke: its measurements, or null when it is too small or too plain to measure. */
45
+ export declare function measureDrawnStroke(stroke: PathObject, handLine?: readonly Point[]): {
46
+ points: StrokePoint[];
47
+ measurements: StrokeMeasurements;
48
+ } | null;
49
+ /**
50
+ * Decides whether a stroke is a clean shape. Resolves to null for "leave it as drawn".
51
+ * Deliberately never rejects.
52
+ */
53
+ export declare function decideStrokeSnap(stroke: PathObject, context: SnapContext, handLine?: readonly Point[]): Promise<SnapDecision | null>;
54
+ /**
55
+ * The clean shape for a stroke: the same place and size (where the hand went, when it is known, else the box of the
56
+ * stroke), the same line colour and width, no fill (a freehand stroke is never filled, so its replacement is not either).
57
+ * The circle and the two library shapes stay square. Null when the shape cannot be built.
58
+ */
59
+ export declare function buildSnappedShape(stroke: PathObject, shape: CanvasSnapShape, services: {
60
+ createId: (prefix: string) => ObjectId;
61
+ random?: () => number;
62
+ }, handLine?: readonly Point[]): ShapeObject | null;
63
+ /**
64
+ * Decides and, when the answer is a shape, replaces the stroke on the board in one undo step. Resolves to the
65
+ * decision that was applied, or null when the stroke stays.
66
+ */
67
+ export declare function snapDrawnStroke(engine: EngineCore, stroke: PathObject, context: SnapContext, handLine?: readonly Point[]): Promise<SnapDecision | null>;
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Shape snap: swaps a freehand stroke the geometry recognises for the clean shape.
3
+ *
4
+ * The geometry (`shapes/strokeShapeSnap.ts`) decides on its own whenever it can; only a stroke whose
5
+ * measurements are genuinely ambiguous is handed to the host's recognizer, and only a shape the geometry
6
+ * allowed can ever be applied. Nothing here may be fatal: no recognizer, a declined or ruled-out
7
+ * answer, a stroke that was moved or removed while the host was thinking, all leave the drawing exactly as
8
+ * the person made it. Whether a finished stroke is looked at at all is `isShapeSnapOn` (off unless the page
9
+ * asked for it).
10
+ *
11
+ * The functions are plain TypeScript over the engine's records, so they run (and are tested) in Node.
12
+ */
13
+ import { brushCentreline, isBrushStroke } from '../../engine/core/brush.js';
14
+ import { outlineBounds } from '../../engine/core/hitTest.js';
15
+ import { parsePathData } from '../../engine/core/pathData.js';
16
+ import { replaceObject, shapeBounds } from '../../engine/core/scene.js';
17
+ import { buildDraggedShape, buildLibraryShape } from '../../engine/core/shapeTools.js';
18
+ import { lineColorOf } from '../../engine/core/style.js';
19
+ import { CANVAS_SHAPE_SNAP_ENABLED, CANVAS_SNAP_SHAPE_LABELS, confidentSnapShape, flattenPathCommands, measureStroke, planSnapPlacement, rankSnapShapes, renderStrokeSnapImage } from '../shapes/strokeShapeSnap.js';
20
+ /**
21
+ * Whether a finished stroke may be snapped at all. It is **off unless the page asks**: the prop `shapeSnap` is false
22
+ * by default, and a page that wants the behaviour passes true. The package's own flag
23
+ * (`CANVAS_SHAPE_SNAP_ENABLED`, false as shipped) turns it on for every page when it is flipped, which is the one
24
+ * line to roll the behaviour out or back. The flag is a parameter so a test can hold both of its values.
25
+ */
26
+ export function isShapeSnapOn(pageAsked, packageFlag = CANVAS_SHAPE_SNAP_ENABLED) {
27
+ return packageFlag === true || pageAsked === true;
28
+ }
29
+ /**
30
+ * The points of a stroke in the coordinates of its own box. A shape is recognised by what the hand did, as the pointer
31
+ * reported it (`handLine`, in the units of the board): the line a brush stroke holds is smoothed, which rounds the very
32
+ * corners a rectangle is told by. Without it, a brush stroke is measured through the line it holds (its path is the
33
+ * outline of that line, with an edge on each side), any other path through its commands.
34
+ */
35
+ export function strokeMeasurementPoints(stroke, handLine) {
36
+ if (handLine && handLine.length >= 2)
37
+ return handLine.map((point) => ({ x: point.x - stroke.x, y: point.y - stroke.y }));
38
+ if (isBrushStroke(stroke))
39
+ return brushCentreline(stroke);
40
+ return flattenPathCommands(parsePathData(stroke.d));
41
+ }
42
+ /** What the geometry says about a stroke: its measurements, or null when it is too small or too plain to measure. */
43
+ export function measureDrawnStroke(stroke, handLine) {
44
+ const points = strokeMeasurementPoints(stroke, handLine);
45
+ if (points.length < 2)
46
+ return null;
47
+ const measurements = measureStroke(points);
48
+ if (!measurements)
49
+ return null;
50
+ return { points, measurements };
51
+ }
52
+ /**
53
+ * Decides whether a stroke is a clean shape. Resolves to null for "leave it as drawn".
54
+ * Deliberately never rejects.
55
+ */
56
+ export async function decideStrokeSnap(stroke, context, handLine) {
57
+ try {
58
+ const measured = measureDrawnStroke(stroke, handLine);
59
+ if (!measured)
60
+ return null;
61
+ const { points, measurements } = measured;
62
+ // Only shapes the measurements cannot rule out are ever considered, and the same list is the only roster
63
+ // the host may choose from.
64
+ const rankings = rankSnapShapes(measurements);
65
+ if (rankings.length === 0)
66
+ return null;
67
+ const candidates = rankings.map((ranking) => ({ ...ranking, label: CANVAS_SNAP_SHAPE_LABELS[ranking.shape] }));
68
+ const decisive = confidentSnapShape(rankings);
69
+ if (decisive)
70
+ return { shape: decisive.shape, source: 'geometry', axisTilt: measurements.axisTilt };
71
+ if (!context.recognize) {
72
+ context.log?.('Shape snap: ambiguous and no recognizer configured, leaving the stroke as drawn', {
73
+ candidates: candidates.map((candidate) => candidate.shape)
74
+ });
75
+ return null;
76
+ }
77
+ const strokeImage = renderStrokeSnapImage(points);
78
+ const answer = await context.recognize({
79
+ measurements,
80
+ candidates,
81
+ best: candidates[0] ?? null,
82
+ ...(strokeImage ? { image: strokeImage } : {})
83
+ });
84
+ const named = answer?.shape ?? null;
85
+ if (!named || !candidates.some((candidate) => candidate.shape === named)) {
86
+ context.log?.('Shape snap: the recognizer declined or named a shape the geometry ruled out', {
87
+ shape: named,
88
+ reason: answer?.reason ?? 'no_answer'
89
+ });
90
+ return null;
91
+ }
92
+ return { shape: named, source: 'recognizer', axisTilt: measurements.axisTilt };
93
+ }
94
+ catch (error) {
95
+ context.log?.('Shape snap: failed, leaving the stroke as drawn', error);
96
+ return null;
97
+ }
98
+ }
99
+ /**
100
+ * The clean shape for a stroke: the same place and size (where the hand went, when it is known, else the box of the
101
+ * stroke), the same line colour and width, no fill (a freehand stroke is never filled, so its replacement is not either).
102
+ * The circle and the two library shapes stay square. Null when the shape cannot be built.
103
+ */
104
+ export function buildSnappedShape(stroke, shape, services, handLine) {
105
+ const box = (handLine && handLine.length >= 2 ? outlineBounds(handLine) : null) ?? shapeBounds(stroke);
106
+ const placement = planSnapPlacement(shape, { left: box.x, top: box.y, width: box.width, height: box.height });
107
+ const area = placement.bounds;
108
+ const start = { x: area.left, y: area.top };
109
+ const end = { x: area.left + area.width, y: area.top + area.height };
110
+ const id = services.createId('obj');
111
+ let replacement = null;
112
+ if (shape === 'diamond' || shape === 'hexagon') {
113
+ replacement = buildLibraryShape({ id, x: area.left, y: area.top, width: area.width, height: area.height, key: shape });
114
+ }
115
+ else {
116
+ const kind = shape === 'oval' ? 'ellipse' : shape;
117
+ replacement = buildDraggedShape({
118
+ kind,
119
+ start,
120
+ end,
121
+ constrain: false,
122
+ dragged: true,
123
+ wireframe: true,
124
+ createId: () => id,
125
+ random: services.random
126
+ });
127
+ }
128
+ if (!replacement)
129
+ return null;
130
+ return {
131
+ ...replacement,
132
+ parentId: stroke.parentId,
133
+ opacity: stroke.opacity,
134
+ fill: 'transparent',
135
+ fillOpacity: 0,
136
+ // A brush stroke keeps its colour in the fill of its outline: the clean shape draws it as a line.
137
+ stroke: lineColorOf(stroke),
138
+ strokeWidth: stroke.strokeWidth,
139
+ strokeStyle: stroke.strokeStyle
140
+ };
141
+ }
142
+ /**
143
+ * Decides and, when the answer is a shape, replaces the stroke on the board in one undo step. Resolves to the
144
+ * decision that was applied, or null when the stroke stays.
145
+ */
146
+ export async function snapDrawnStroke(engine, stroke, context, handLine) {
147
+ const decision = await decideStrokeSnap(stroke, context, handLine);
148
+ if (!decision)
149
+ return null;
150
+ // While the host was thinking the person may have moved, restyled or erased the stroke: then it is theirs.
151
+ if (engine.getObject(stroke.id) !== stroke)
152
+ return null;
153
+ const replacement = buildSnappedShape(stroke, decision.shape, engine.services, handLine);
154
+ if (!replacement)
155
+ return null;
156
+ try {
157
+ engine.applyScene(replaceObject(engine.state.scene, stroke.id, replacement), 'push');
158
+ }
159
+ catch (error) {
160
+ context.log?.('Shape snap: could not put the clean shape on the board', error);
161
+ return null;
162
+ }
163
+ context.log?.('Shape snap: stroke replaced with the clean shape', { ...decision, id: replacement.id });
164
+ return decision;
165
+ }
@@ -1,17 +1,17 @@
1
1
  /**
2
- * Canvas barrel — exports the heavy Canvas component and canvas-specific sub-components.
2
+ * Canvas barrel — exports the canvas component and what a page needs to configure it.
3
3
  *
4
- * Canvas.svelte is 10K+ lines. Use dynamic import in consuming apps:
4
+ * The canvas is a Svelte DOM canvas: boxes and SVG on a board, one engine behind them (see host/CanvasHost.ts and
5
+ * engine/EngineCore.ts). It is a good deal heavier than the components barrel, so a page that shows it later loads it
6
+ * with a dynamic import:
5
7
  * const { Canvas } = await import('@widgetic/canvas/canvas');
6
8
  */
7
- export { default as Canvas } from './Canvas.svelte';
8
- export { default as CanvasToolbar } from './CanvasToolbar.svelte';
9
- export { default as ContextMenu } from './ContextMenu.svelte';
10
- export { default as PanZoomPanel } from './PanZoomPanel.svelte';
11
- export { default as PropsPanel } from './props-panel/PropsPanel.svelte';
9
+ export { default as Canvas } from './DomCanvas.svelte';
12
10
  export type { WidgetConversionPayload } from './types';
13
11
  export { TOOLBAR_POSITIONS, DEFAULT_TOOLBAR_POSITION } from './toolbarPosition';
14
12
  export type { ToolbarPosition } from './toolbarPosition';
13
+ export { PAN_ZOOM_POSITIONS, DEFAULT_PAN_ZOOM_POSITION } from './panZoomPosition';
14
+ export type { PanZoomPosition } from './panZoomPosition';
15
15
  export type { StrokeRecognizer, StrokeRecognitionRequest, StrokeRecognitionResult } from './types';
16
16
  export { CANVAS_SHAPE_SNAP_ENABLED, STROKE_SNAP_IMAGE_SIDE, projectStrokeToImagePoints, renderStrokeSnapImage } from './shapes/strokeShapeSnap';
17
17
  export { canvasLog, canvasWarn, canvasDebug, setCanvasLogsEnabled, isCanvasLogsEnabled } from './canvasLogger';
@@ -1,16 +1,16 @@
1
1
  /**
2
- * Canvas barrel — exports the heavy Canvas component and canvas-specific sub-components.
2
+ * Canvas barrel — exports the canvas component and what a page needs to configure it.
3
3
  *
4
- * Canvas.svelte is 10K+ lines. Use dynamic import in consuming apps:
4
+ * The canvas is a Svelte DOM canvas: boxes and SVG on a board, one engine behind them (see host/CanvasHost.ts and
5
+ * engine/EngineCore.ts). It is a good deal heavier than the components barrel, so a page that shows it later loads it
6
+ * with a dynamic import:
5
7
  * const { Canvas } = await import('@widgetic/canvas/canvas');
6
8
  */
7
- export { default as Canvas } from './Canvas.svelte';
8
- export { default as CanvasToolbar } from './CanvasToolbar.svelte';
9
- export { default as ContextMenu } from './ContextMenu.svelte';
10
- export { default as PanZoomPanel } from './PanZoomPanel.svelte';
11
- export { default as PropsPanel } from './props-panel/PropsPanel.svelte';
12
- // Where the drawing toolbar stands along the bottom edge of the board (the `toolbarPosition` option of <Canvas>)
9
+ export { default as Canvas } from './DomCanvas.svelte';
10
+ // Where the drawing toolbar stands: one of eight places on the board (the `toolbarPosition` option of <Canvas>)
13
11
  export { TOOLBAR_POSITIONS, DEFAULT_TOOLBAR_POSITION } from './toolbarPosition';
12
+ // Where the zoom control stands: one of the same eight places (the `panZoomPosition` option of <Canvas>)
13
+ export { PAN_ZOOM_POSITIONS, DEFAULT_PAN_ZOOM_POSITION } from './panZoomPosition';
14
14
  export { CANVAS_SHAPE_SNAP_ENABLED, STROKE_SNAP_IMAGE_SIDE, projectStrokeToImagePoints, renderStrokeSnapImage } from './shapes/strokeShapeSnap';
15
15
  export { canvasLog, canvasWarn, canvasDebug, setCanvasLogsEnabled, isCanvasLogsEnabled } from './canvasLogger';
16
16
  export { setShapeImageStore, getShapeImageStore } from './shapes/shapeImageStore';
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Open-time marks for profiling: they show in the browser's Performance panel and in
3
+ * `performance.getEntriesByType('mark')`, so how long the canvas takes to open can be read without a debugger.
4
+ *
5
+ * Nothing reads them but a person with the panel open, so a page that has no User Timing API (or a browser that refuses a
6
+ * mark) loses nothing.
7
+ */
8
+ export declare const CANVAS_OPEN_MARKS: {
9
+ /** The board is in place with the data the page gave it, a moment before `onReady` is called. */
10
+ readonly READY: "canvas:ready";
11
+ /** `loadCanvas` began reading a saved canvas. */
12
+ readonly LOAD_START: "canvas:load-json-start";
13
+ /** `loadCanvas` finished (whatever it found: a board, an empty board, or text it could not read). */
14
+ readonly LOAD_END: "canvas:load-json-end";
15
+ };
16
+ /** Writes one mark. Never throws: marks are diagnostics only. */
17
+ export declare function markCanvasOpenStep(stepName: string): void;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Open-time marks for profiling: they show in the browser's Performance panel and in
3
+ * `performance.getEntriesByType('mark')`, so how long the canvas takes to open can be read without a debugger.
4
+ *
5
+ * Nothing reads them but a person with the panel open, so a page that has no User Timing API (or a browser that refuses a
6
+ * mark) loses nothing.
7
+ */
8
+ export const CANVAS_OPEN_MARKS = {
9
+ /** The board is in place with the data the page gave it, a moment before `onReady` is called. */
10
+ READY: 'canvas:ready',
11
+ /** `loadCanvas` began reading a saved canvas. */
12
+ LOAD_START: 'canvas:load-json-start',
13
+ /** `loadCanvas` finished (whatever it found: a board, an empty board, or text it could not read). */
14
+ LOAD_END: 'canvas:load-json-end'
15
+ };
16
+ /** Writes one mark. Never throws: marks are diagnostics only. */
17
+ export function markCanvasOpenStep(stepName) {
18
+ try {
19
+ if (typeof performance === 'undefined' || typeof performance.mark !== 'function')
20
+ return;
21
+ performance.mark(stepName);
22
+ }
23
+ catch {
24
+ // Marks are diagnostics only.
25
+ }
26
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Where the zoom control stands on the board.
3
+ *
4
+ * The page names one of eight places with `panZoomPosition` (the ones `chromeLayout.ts` describes): the three places of the
5
+ * top edge, the three of the bottom edge, and the middle of the left and of the right edge, where the control stands
6
+ * upright. The Figma design puts it at the bottom left, 16 px from both edges, and that is the default. Kept free of
7
+ * Svelte and of the DOM so that Node can test it.
8
+ */
9
+ import type { ChromeAnchor } from './chromeLayout.js';
10
+ /** The places the zoom control can stand: `TL TC TR` (top), `CL CR` (the sides) and `BL BC BR` (bottom). */
11
+ export declare const PAN_ZOOM_POSITIONS: readonly ["TL", "TC", "TR", "CL", "CR", "BL", "BC", "BR"];
12
+ export type PanZoomPosition = ChromeAnchor;
13
+ export declare const DEFAULT_PAN_ZOOM_POSITION: PanZoomPosition;
14
+ /** The position to use for what a page passed: anything that is not one of the eight is the default. */
15
+ export declare function normalizePanZoomPosition(value: unknown): PanZoomPosition;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Where the zoom control stands on the board.
3
+ *
4
+ * The page names one of eight places with `panZoomPosition` (the ones `chromeLayout.ts` describes): the three places of the
5
+ * top edge, the three of the bottom edge, and the middle of the left and of the right edge, where the control stands
6
+ * upright. The Figma design puts it at the bottom left, 16 px from both edges, and that is the default. Kept free of
7
+ * Svelte and of the DOM so that Node can test it.
8
+ */
9
+ import { CHROME_ANCHORS, normalizeAnchor } from './chromeLayout.js';
10
+ /** The places the zoom control can stand: `TL TC TR` (top), `CL CR` (the sides) and `BL BC BR` (bottom). */
11
+ export const PAN_ZOOM_POSITIONS = CHROME_ANCHORS;
12
+ export const DEFAULT_PAN_ZOOM_POSITION = 'BL';
13
+ /** The position to use for what a page passed: anything that is not one of the eight is the default. */
14
+ export function normalizePanZoomPosition(value) {
15
+ return normalizeAnchor(value, DEFAULT_PAN_ZOOM_POSITION);
16
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The font family a person chose for a text, kept in the canvas's preference store (the browser's `localStorage`, or the
3
+ * store a partner page installed with `setCanvasPreferenceStorage`), so that the next text they place starts with it.
4
+ */
5
+ /** The font families the selection panel offers for a text, in the order of its list. */
6
+ export declare const TEXT_FONT_FAMILIES: readonly ["Arial", "Helvetica", "Times New Roman", "Georgia", "Verdana", "Comic Sans MS", "Courier New", "Impact"];
7
+ /** The key the choice is kept under. */
8
+ export declare const PREFERRED_TEXT_FONT_STORAGE_KEY = "widgetic-canvas:preferred-text-font";
9
+ /** The font family kept from an earlier choice, or null when none was made (or the store refuses, or the name is no longer offered). */
10
+ export declare function readPreferredTextFontFamily(): string | null;
11
+ /** Keeps the choice for the next text. A store that refuses the write is not an error: the choice then lasts until the page closes. */
12
+ export declare function writePreferredTextFontFamily(fontFamily: string): void;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * The font family a person chose for a text, kept in the canvas's preference store (the browser's `localStorage`, or the
3
+ * store a partner page installed with `setCanvasPreferenceStorage`), so that the next text they place starts with it.
4
+ */
5
+ import { getCanvasPreferenceStorage } from '../components/preferenceStorage.js';
6
+ /** The font families the selection panel offers for a text, in the order of its list. */
7
+ export const TEXT_FONT_FAMILIES = [
8
+ 'Arial',
9
+ 'Helvetica',
10
+ 'Times New Roman',
11
+ 'Georgia',
12
+ 'Verdana',
13
+ 'Comic Sans MS',
14
+ 'Courier New',
15
+ 'Impact'
16
+ ];
17
+ /** The key the choice is kept under. */
18
+ export const PREFERRED_TEXT_FONT_STORAGE_KEY = 'widgetic-canvas:preferred-text-font';
19
+ /** The font family kept from an earlier choice, or null when none was made (or the store refuses, or the name is no longer offered). */
20
+ export function readPreferredTextFontFamily() {
21
+ try {
22
+ const stored = getCanvasPreferenceStorage()?.getItem(PREFERRED_TEXT_FONT_STORAGE_KEY);
23
+ return stored && TEXT_FONT_FAMILIES.includes(stored) ? stored : null;
24
+ }
25
+ catch {
26
+ // A store that blocks reading is the same as a store with nothing in it.
27
+ return null;
28
+ }
29
+ }
30
+ /** Keeps the choice for the next text. A store that refuses the write is not an error: the choice then lasts until the page closes. */
31
+ export function writePreferredTextFontFamily(fontFamily) {
32
+ try {
33
+ getCanvasPreferenceStorage()?.setItem(PREFERRED_TEXT_FONT_STORAGE_KEY, fontFamily);
34
+ }
35
+ catch {
36
+ // Nothing to do: the engine still holds the choice for this session.
37
+ }
38
+ }