@widgetic/canvas 0.5.34 → 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 (212) 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/autosaveScheduler.d.ts +58 -0
  5. package/dist/canvas/autosaveScheduler.js +157 -0
  6. package/dist/canvas/boardFrame.d.ts +13 -0
  7. package/dist/canvas/boardFrame.js +13 -0
  8. package/dist/canvas/boardSurface.d.ts +9 -0
  9. package/dist/canvas/boardSurface.js +9 -0
  10. package/dist/canvas/chromeLayout.d.ts +76 -0
  11. package/dist/canvas/chromeLayout.js +121 -0
  12. package/dist/canvas/emptyBoardHint.d.ts +17 -0
  13. package/dist/canvas/emptyBoardHint.js +12 -0
  14. package/dist/canvas/eventScope.d.ts +1 -1
  15. package/dist/canvas/eventScope.js +1 -1
  16. package/dist/canvas/host/CanvasHost.d.ts +351 -0
  17. package/dist/canvas/host/CanvasHost.js +1740 -0
  18. package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
  19. package/dist/canvas/host/CanvasHostApi.js +95 -0
  20. package/dist/canvas/host/browserServices.d.ts +46 -0
  21. package/dist/canvas/host/browserServices.js +167 -0
  22. package/dist/canvas/host/conversion.d.ts +51 -0
  23. package/dist/canvas/host/conversion.js +123 -0
  24. package/dist/canvas/host/hostObject.d.ts +40 -0
  25. package/dist/canvas/host/hostObject.js +93 -0
  26. package/dist/canvas/host/pageEvents.d.ts +24 -0
  27. package/dist/canvas/host/pageEvents.js +35 -0
  28. package/dist/canvas/host/screenRects.d.ts +66 -0
  29. package/dist/canvas/host/screenRects.js +102 -0
  30. package/dist/canvas/host/strokeSnap.d.ts +67 -0
  31. package/dist/canvas/host/strokeSnap.js +165 -0
  32. package/dist/canvas/index.d.ts +9 -7
  33. package/dist/canvas/index.js +9 -7
  34. package/dist/canvas/openMarks.d.ts +17 -0
  35. package/dist/canvas/openMarks.js +26 -0
  36. package/dist/canvas/panZoomPosition.d.ts +15 -0
  37. package/dist/canvas/panZoomPosition.js +16 -0
  38. package/dist/canvas/preferredTextFont.d.ts +12 -0
  39. package/dist/canvas/preferredTextFont.js +38 -0
  40. package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
  41. package/dist/canvas/shapes/ShapeLibrary.js +13 -1
  42. package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
  43. package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
  44. package/dist/canvas/toolbarPosition.d.ts +24 -0
  45. package/dist/canvas/toolbarPosition.js +24 -0
  46. package/dist/canvas/types.d.ts +14 -0
  47. package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
  48. package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
  49. package/dist/canvas/ui/CanvasHint.svelte +20 -0
  50. package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
  51. package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
  52. package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
  53. package/dist/canvas/ui/ColorField.svelte +155 -0
  54. package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
  55. package/dist/canvas/ui/LoadNotice.svelte +39 -0
  56. package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
  57. package/dist/canvas/ui/SelectionPanel.svelte +300 -0
  58. package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
  59. package/dist/canvas/ui/SelectorBar.svelte +82 -0
  60. package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
  61. package/dist/canvas/ui/ShapesMenu.svelte +185 -0
  62. package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
  63. package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
  64. package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
  65. package/dist/canvas/ui/ToolButton.svelte +78 -0
  66. package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
  67. package/dist/canvas/ui/WidgetActions.svelte +152 -0
  68. package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
  69. package/dist/canvas/ui/ZoomControl.svelte +89 -0
  70. package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
  71. package/dist/canvas/ui/colorModel.d.ts +18 -0
  72. package/dist/canvas/ui/colorModel.js +85 -0
  73. package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
  74. package/dist/canvas/ui/colorPickerMessages.js +15 -0
  75. package/dist/canvas/ui/floatingCard.d.ts +107 -0
  76. package/dist/canvas/ui/floatingCard.js +107 -0
  77. package/dist/canvas/ui/floatingLayer.d.ts +21 -0
  78. package/dist/canvas/ui/floatingLayer.js +52 -0
  79. package/dist/canvas/ui/icons.d.ts +12 -0
  80. package/dist/canvas/ui/icons.js +7 -0
  81. package/dist/canvas/ui/keyHints.d.ts +27 -0
  82. package/dist/canvas/ui/keyHints.js +43 -0
  83. package/dist/canvas/ui/popoverSide.d.ts +5 -0
  84. package/dist/canvas/ui/popoverSide.js +20 -0
  85. package/dist/canvas/ui/selectionTraits.d.ts +61 -0
  86. package/dist/canvas/ui/selectionTraits.js +99 -0
  87. package/dist/canvas/view/CanvasStage.svelte +132 -0
  88. package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
  89. package/dist/canvas/view/EraserTrail.svelte +50 -0
  90. package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
  91. package/dist/canvas/view/ObjectView.svelte +245 -0
  92. package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
  93. package/dist/canvas/view/SelectionOverlay.svelte +487 -0
  94. package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
  95. package/dist/canvas/view/TextEditor.svelte +76 -0
  96. package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
  97. package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
  98. package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
  99. package/dist/canvas/view/erasingMarks.d.ts +13 -0
  100. package/dist/canvas/view/erasingMarks.js +34 -0
  101. package/dist/canvas/view/geometry.d.ts +169 -0
  102. package/dist/canvas/view/geometry.js +272 -0
  103. package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
  104. package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
  105. package/dist/components/index.js +1 -1
  106. package/dist/components/input-controls/ColorIC.svelte +2 -2
  107. package/dist/engine/EngineCore.d.ts +273 -0
  108. package/dist/engine/EngineCore.js +1051 -0
  109. package/dist/engine/codec/canvasDocument.d.ts +57 -0
  110. package/dist/engine/codec/canvasDocument.js +471 -0
  111. package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
  112. package/dist/engine/codec/hostReadableKeys.js +25 -0
  113. package/dist/engine/core/arrange.d.ts +20 -0
  114. package/dist/engine/core/arrange.js +81 -0
  115. package/dist/engine/core/arrow.d.ts +91 -0
  116. package/dist/engine/core/arrow.js +182 -0
  117. package/dist/engine/core/brush.d.ts +68 -0
  118. package/dist/engine/core/brush.js +173 -0
  119. package/dist/engine/core/defaults.d.ts +70 -0
  120. package/dist/engine/core/defaults.js +158 -0
  121. package/dist/engine/core/freehand.d.ts +130 -0
  122. package/dist/engine/core/freehand.js +430 -0
  123. package/dist/engine/core/geometry.d.ts +44 -0
  124. package/dist/engine/core/geometry.js +216 -0
  125. package/dist/engine/core/history.d.ts +39 -0
  126. package/dist/engine/core/history.js +69 -0
  127. package/dist/engine/core/hitTest.d.ts +54 -0
  128. package/dist/engine/core/hitTest.js +298 -0
  129. package/dist/engine/core/matrix.d.ts +47 -0
  130. package/dist/engine/core/matrix.js +101 -0
  131. package/dist/engine/core/pathData.d.ts +50 -0
  132. package/dist/engine/core/pathData.js +550 -0
  133. package/dist/engine/core/rotationReset.d.ts +10 -0
  134. package/dist/engine/core/rotationReset.js +17 -0
  135. package/dist/engine/core/scene.d.ts +99 -0
  136. package/dist/engine/core/scene.js +450 -0
  137. package/dist/engine/core/scribble.d.ts +64 -0
  138. package/dist/engine/core/scribble.js +121 -0
  139. package/dist/engine/core/shapePaths.d.ts +34 -0
  140. package/dist/engine/core/shapePaths.js +69 -0
  141. package/dist/engine/core/shapeTools.d.ts +80 -0
  142. package/dist/engine/core/shapeTools.js +161 -0
  143. package/dist/engine/core/snapping.d.ts +64 -0
  144. package/dist/engine/core/snapping.js +164 -0
  145. package/dist/engine/core/style.d.ts +40 -0
  146. package/dist/engine/core/style.js +151 -0
  147. package/dist/engine/core/transform.d.ts +79 -0
  148. package/dist/engine/core/transform.js +258 -0
  149. package/dist/engine/core/types.d.ts +238 -0
  150. package/dist/engine/core/types.js +31 -0
  151. package/dist/engine/core/viewport.d.ts +32 -0
  152. package/dist/engine/core/viewport.js +84 -0
  153. package/dist/engine/input/PointerController.d.ts +38 -0
  154. package/dist/engine/input/PointerController.js +545 -0
  155. package/dist/engine/input/arrowHandles.d.ts +38 -0
  156. package/dist/engine/input/arrowHandles.js +60 -0
  157. package/dist/engine/input/gestures.d.ts +79 -0
  158. package/dist/engine/input/gestures.js +769 -0
  159. package/dist/engine/input/hoverCursor.d.ts +29 -0
  160. package/dist/engine/input/hoverCursor.js +23 -0
  161. package/dist/engine/input/keyboard.d.ts +75 -0
  162. package/dist/engine/input/keyboard.js +264 -0
  163. package/dist/engine/input/selectionHandles.d.ts +89 -0
  164. package/dist/engine/input/selectionHandles.js +169 -0
  165. package/dist/engine/input/types.d.ts +82 -0
  166. package/dist/engine/input/types.js +5 -0
  167. package/dist/engine/render/rasterize.d.ts +74 -0
  168. package/dist/engine/render/rasterize.js +273 -0
  169. package/dist/engine/state.d.ts +124 -0
  170. package/dist/engine/state.js +47 -0
  171. package/dist/engine/text/layoutText.d.ts +53 -0
  172. package/dist/engine/text/layoutText.js +170 -0
  173. package/dist/engine/widgets.d.ts +124 -0
  174. package/dist/engine/widgets.js +345 -0
  175. package/dist/i18n/labels.d.ts +4 -15
  176. package/dist/i18n/labels.js +33 -53
  177. package/dist/i18n/language.d.ts +3 -3
  178. package/dist/i18n/messages.d.ts +51 -122
  179. package/dist/i18n/messages.js +58 -131
  180. package/dist/i18n/shortcuts.d.ts +0 -1
  181. package/dist/i18n/shortcuts.js +2 -11
  182. package/dist/index.d.ts +6 -2
  183. package/dist/index.js +6 -2
  184. package/package.json +30 -5
  185. package/dist/canvas/Canvas.svelte +0 -13299
  186. package/dist/canvas/Canvas.svelte.d.ts +0 -281
  187. package/dist/canvas/CanvasToolbar.svelte +0 -1498
  188. package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
  189. package/dist/canvas/ContextMenu.svelte +0 -305
  190. package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
  191. package/dist/canvas/PanZoomPanel.svelte +0 -478
  192. package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
  193. package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
  194. package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
  195. package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
  196. package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
  197. package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
  198. package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
  199. package/dist/canvas/shadowRootText.d.ts +0 -5
  200. package/dist/canvas/shadowRootText.js +0 -43
  201. package/dist/canvas/shapes/FrameShape.d.ts +0 -97
  202. package/dist/canvas/shapes/FrameShape.js +0 -953
  203. package/dist/canvas/shapes/ImageShape.d.ts +0 -47
  204. package/dist/canvas/shapes/ImageShape.js +0 -339
  205. package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
  206. package/dist/canvas/shapes/WidgetShape.js +0 -200
  207. package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
  208. package/dist/canvas/widgetPreviewGeometry.js +0 -66
  209. package/dist/i18n/shapeWords.d.ts +0 -30
  210. package/dist/i18n/shapeWords.js +0 -36
  211. package/dist/i18n/slots.d.ts +0 -20
  212. package/dist/i18n/slots.js +0 -40
@@ -0,0 +1,454 @@
1
+ <!--
2
+ The Widgetic canvas, drawn with the DOM. This is the one public `Canvas` component of the package.
3
+
4
+ It is a thin shell: the state and the rules live in `CanvasHost` (which owns the engine, the autosave and the
5
+ conversions) and in the engine under it, and every piece of what is drawn is its own small component under `view/` and
6
+ `ui/`. This file does four things:
7
+
8
+ 1. It takes the props the page sets (the names and defaults a page already uses, in the classic syntax so that
9
+ `<svelte:component this={Canvas} bind:this={ref} bind:wireframeMode …>` and the page's `InstanceType` typing keep
10
+ working) and hands them to the host, which reads each one at the moment it needs it.
11
+ 2. It exports the functions the page calls (`CANVAS_EXPORTED_FUNCTION_NAMES`), each forwarding to the host's method of
12
+ the same name. `tests/host/hostSurface.test.mjs` holds the names, the props and this file to the surface that is
13
+ frozen in `tests/fixtures/hostSurface.json`.
14
+ 3. It makes the language of the canvas (`locale`, `messages`) that every part reads its words from.
15
+ 4. It lays the parts out: the board (the pointer surface) with the chrome as its siblings, never inside it, so a press on
16
+ a button is never a press on the board, and the overlay portal (see `ui/floatingLayer.ts`) that every menu, popover and
17
+ tooltip is drawn into.
18
+
19
+ Nothing here knows how an object is drawn or what a gesture does.
20
+ -->
21
+ <script lang="ts">
22
+ import { onDestroy } from 'svelte';
23
+ import { writable } from 'svelte/store';
24
+ import { Tooltip } from '@widgetic/design-system/components';
25
+ import { onlyCardIsSelected } from '../engine/widgets.js';
26
+ import { provideCanvasLanguage } from '../i18n/language.js';
27
+ import { BOARD_FRAME_CLASSES } from './boardFrame.js';
28
+ import { CanvasHost, DEFAULT_HOST_PROPS } from './host/CanvasHost.js';
29
+ import type { CanvasHostProps, SelectorGuide } from './host/CanvasHost.js';
30
+ import { forwardToHost } from './host/CanvasHostApi.js';
31
+ import { chromeSlots, panelClearanceFor, zoomVisibilityClasses } from './chromeLayout.js';
32
+ import { DEFAULT_PAN_ZOOM_POSITION, normalizePanZoomPosition } from './panZoomPosition.js';
33
+ import type { PanZoomPosition } from './panZoomPosition.js';
34
+ import { DEFAULT_TOOLBAR_POSITION, normalizeToolbarPosition } from './toolbarPosition.js';
35
+ import type { ToolbarPosition } from './toolbarPosition.js';
36
+ import CanvasContextMenu from './ui/CanvasContextMenu.svelte';
37
+ import CanvasToolbar from './ui/CanvasToolbar.svelte';
38
+ import { FLOATING_LAYER_CLASSES, provideFloatingLayer } from './ui/floatingLayer.js';
39
+ import SelectionPanel from './ui/SelectionPanel.svelte';
40
+ import SelectorBar from './ui/SelectorBar.svelte';
41
+ import ShortcutsPanel from './ui/ShortcutsPanel.svelte';
42
+ import ZoomControl from './ui/ZoomControl.svelte';
43
+ import { shouldShowEmptyBoardHint } from './emptyBoardHint.js';
44
+ import CanvasStage from './view/CanvasStage.svelte';
45
+ import WidgetChromeLayer from './view/WidgetChromeLayer.svelte';
46
+
47
+ /**
48
+ * COMPONENT PROPS
49
+ */
50
+ // Class
51
+ let className = '';
52
+ export { className as class };
53
+ // Style
54
+ export let style = '';
55
+
56
+ /** Site /canvas embed sits flush under AppHeader: skip the frame (double stroke). */
57
+ export let showContainerFrame: CanvasHostProps['showContainerFrame'] = DEFAULT_HOST_PROPS.showContainerFrame;
58
+ /** The id or class of an element of the page that stands above its live widgets: the frame of a selected widget card is drawn there. */
59
+ export let widgetSelectionLayerTarget: CanvasHostProps['widgetSelectionLayerTarget'] = DEFAULT_HOST_PROPS.widgetSelectionLayerTarget;
60
+
61
+ // Callback: fires when the user clicks/selects an object with screenshot
62
+ export let onObjectClick: CanvasHostProps['onObjectClick'] = DEFAULT_HOST_PROPS.onObjectClick;
63
+
64
+ // Where the zoom control stands: one of eight places, a row letter and a column letter. TL TC TR (the top edge), CL CR
65
+ // (the middle of the left and of the right edge, where it stands upright) and BL BC BR (the bottom edge). The Figma
66
+ // design has it at the bottom left, the default. Anything that is not one of the eight is the bottom left.
67
+ export let panZoomPosition: PanZoomPosition = DEFAULT_PAN_ZOOM_POSITION;
68
+
69
+ // Where the drawing toolbar stands: any of the same eight places (BC, the bottom centre, is the default). On the left or
70
+ // the right edge it stands upright, and its menus open sideways. The selector bar that replaces the toolbar after the
71
+ // first generation stands in the same place. A host that has something of its own at the bottom centre of the page
72
+ // (Shopify's Sidekick) names the place it wants; two cards named for one place stand side by side. Anything that is not
73
+ // one of the eight is the bottom centre.
74
+ export let toolbarPosition: ToolbarPosition = DEFAULT_TOOLBAR_POSITION;
75
+
76
+ // Wireframe mode: hides the colour fields of the selection panel and keeps new objects white with a dark stroke
77
+ export let wireframeMode: CanvasHostProps['wireframeMode'] = DEFAULT_HOST_PROPS.wireframeMode;
78
+
79
+ // The advanced chrome of a widget card: with it on a card can be turned, and the primary widget has its ring. With it off
80
+ // a card shows its four corners only. Everything else (the Shapes menu, undo and redo, the shape tools, the selection
81
+ // panel) is always there. Default off for the sketch MVP.
82
+ export let advancedFeatures: CanvasHostProps['advancedFeatures'] = DEFAULT_HOST_PROPS.advancedFeatures;
83
+
84
+ /** The element selector on the toolbar. The host owns whether it is on. */
85
+ export let showElementSelector: CanvasHostProps['showElementSelector'] = DEFAULT_HOST_PROPS.showElementSelector;
86
+ /** Drawing tools stay until the first generation. After that, only the selector remains. */
87
+ export let showDrawingTools: CanvasHostProps['showDrawingTools'] = DEFAULT_HOST_PROPS.showDrawingTools;
88
+ export let elementSelectorActive: CanvasHostProps['elementSelectorActive'] = DEFAULT_HOST_PROPS.elementSelectorActive;
89
+ export let onToggleElementSelector: CanvasHostProps['onToggleElementSelector'] = DEFAULT_HOST_PROPS.onToggleElementSelector;
90
+ /** Words for the selector bar that replaces the drawing tools. */
91
+ export let selectorGuide: SelectorGuide | null = DEFAULT_HOST_PROPS.selectorGuide;
92
+ /** The zoom card. Off on the site when debug is off. The save control stays. */
93
+ export let showZoomPanel: CanvasHostProps['showZoomPanel'] = DEFAULT_HOST_PROPS.showZoomPanel;
94
+ /** Accepted for the pages that set it. The design puts Image in the Shapes menu, so it no longer changes what is drawn. */
95
+ export let mediaBtsOnBar: CanvasHostProps['mediaBtsOnBar'] = DEFAULT_HOST_PROPS.mediaBtsOnBar;
96
+
97
+ // Initial canvas data, read as soon as the board is on the screen.
98
+ // Avoids flashing an empty board and double-mount cycles.
99
+ export let initialData: CanvasHostProps['initialData'] = DEFAULT_HOST_PROPS.initialData;
100
+
101
+ // Callback fired once when the canvas is ready: the page can call its functions from then on
102
+ export let onReady: CanvasHostProps['onReady'] = DEFAULT_HOST_PROPS.onReady;
103
+
104
+ // Host-controlled loading flag: when true, the canvas shows its loading overlay.
105
+ // Use this to keep the loader visible while the host fetches data from API before calling loadCanvas().
106
+ export let isExternalDataLoading: CanvasHostProps['isExternalDataLoading'] = DEFAULT_HOST_PROPS.isExternalDataLoading;
107
+
108
+ // Callback fired at the very start of component teardown.
109
+ // Use this in the host page to save canvas state when Vite HMR or navigation destroys this component
110
+ // before the auto-save timer fires.
111
+ export let onBeforeUnmount: CanvasHostProps['onBeforeUnmount'] = DEFAULT_HOST_PROPS.onBeforeUnmount;
112
+
113
+ // Callback fired once the changes have been quiet for autosaveDelayMs, and when the host or the page asks to flush.
114
+ // Receives the serialized canvas JSON so the host doesn't need to call saveCanvas().
115
+ // May return a promise: while it is pending no second autosave starts, and what changes meanwhile goes out in one
116
+ // save after it, so a slow server is never flooded. A promise that rejects counts as a failed save (the changes stay
117
+ // unsaved until the next change or flush).
118
+ export let onAutoSave: CanvasHostProps['onAutoSave'] = DEFAULT_HOST_PROPS.onAutoSave;
119
+
120
+ // Delay in ms for the internal auto-save debounce: how long the canvas waits after the last change before onAutoSave.
121
+ // Only real modifications trigger autosave, not pan/zoom. Default: 3000ms.
122
+ export let autosaveDelayMs: CanvasHostProps['autosaveDelayMs'] = DEFAULT_HOST_PROPS.autosaveDelayMs;
123
+
124
+ // The longest a change waits before it is saved, however busy the person stays. 0 means no limit. Default: 10000ms.
125
+ export let autosaveMaxWaitMs: CanvasHostProps['autosaveMaxWaitMs'] = DEFAULT_HOST_PROPS.autosaveMaxWaitMs;
126
+
127
+ // While set, a due autosave waits another delay instead of serializing.
128
+ // (Flushing, when the page is hidden or the editor closes, saves whatever the hold.)
129
+ export let suspendAutosave: CanvasHostProps['suspendAutosave'] = DEFAULT_HOST_PROPS.suspendAutosave;
130
+
131
+ // Show the Save button next to the zoom control, with the state of the save.
132
+ export let showSaveButton: CanvasHostProps['showSaveButton'] = DEFAULT_HOST_PROPS.showSaveButton;
133
+
134
+ // Callback fired IMMEDIATELY when the canvas becomes dirty, BEFORE the debounced auto-save.
135
+ // Fires frequently: implement lightweight (e.g. set a boolean flag only).
136
+ export let onCanvasDirty: CanvasHostProps['onCanvasDirty'] = DEFAULT_HOST_PROPS.onCanvasDirty;
137
+
138
+ // Callback fired when the user intentionally clears all canvas objects
139
+ export let onCanvasCleared: CanvasHostProps['onCanvasCleared'] = DEFAULT_HOST_PROPS.onCanvasCleared;
140
+
141
+ // Callback: fires when the user asks to convert an object to a widget.
142
+ // Widget-creator handles the API call and links the resulting widget ID back.
143
+ export let onConvertToWidget: CanvasHostProps['onConvertToWidget'] = DEFAULT_HOST_PROPS.onConvertToWidget;
144
+
145
+ // Callback: fires when a widget card is selected (clicked). The host can use this to open the details of the widget.
146
+ export let onWidgetImageClick: CanvasHostProps['onWidgetImageClick'] = DEFAULT_HOST_PROPS.onWidgetImageClick;
147
+
148
+ // Callback: classify a freehand stroke the geometry engine could not decide on its own. The host owns the model
149
+ // call; returning null, or a shape the geometry ruled out, leaves the stroke exactly as drawn.
150
+ export let onRecognizeStroke: CanvasHostProps['onRecognizeStroke'] = DEFAULT_HOST_PROPS.onRecognizeStroke;
151
+
152
+ // Callback: fires when something that is not a widget card is selected. The host can use this to close the details panel.
153
+ export let onNonWidgetSelected: CanvasHostProps['onNonWidgetSelected'] = DEFAULT_HOST_PROPS.onNonWidgetSelected;
154
+
155
+ // Callback: fires when a widget card is duplicated/pasted.
156
+ // Receives the source widgetId and the cloned card so the host can duplicate the widget in DB and re-link the clone.
157
+ export let onWidgetShapeDuplicated: CanvasHostProps['onWidgetShapeDuplicated'] = DEFAULT_HOST_PROPS.onWidgetShapeDuplicated;
158
+
159
+ // Callback: fires when a widget card is about to be deleted.
160
+ // Returns a Promise<boolean>: if true, proceed with deletion; if false, cancel.
161
+ export let onWidgetShapeDeleting: CanvasHostProps['onWidgetShapeDeleting'] = DEFAULT_HOST_PROPS.onWidgetShapeDeleting;
162
+
163
+ // Callback: fires when the user renames a widget from the selection panel.
164
+ export let onWidgetRename: CanvasHostProps['onWidgetRename'] = DEFAULT_HOST_PROPS.onWidgetRename;
165
+
166
+ // Callback: fires when the user requests a screenshot refresh for the linked widget.
167
+ export let onRefreshScreenshot: CanvasHostProps['onRefreshScreenshot'] = DEFAULT_HOST_PROPS.onRefreshScreenshot;
168
+
169
+ /** Host persists canvases.primary_widget_id after the canvas marks the primary widget. */
170
+ export let onSetPrimaryWidget: CanvasHostProps['onSetPrimaryWidget'] = DEFAULT_HOST_PROPS.onSetPrimaryWidget;
171
+
172
+ /** Throttled paint hook for HTML overlays (e.g. the live widgets laid over the cards). */
173
+ export let onAfterRender: CanvasHostProps['onAfterRender'] = DEFAULT_HOST_PROPS.onAfterRender;
174
+
175
+ // Widget name resolver: given a widgetId, returns the widget name (or null). Used for the name on a card and in the panel.
176
+ export let getWidgetName: CanvasHostProps['getWidgetName'] = DEFAULT_HOST_PROPS.getWidgetName;
177
+ /** Bumped when names change so the names are resolved again. */
178
+ export let widgetNameRevision: CanvasHostProps['widgetNameRevision'] = DEFAULT_HOST_PROPS.widgetNameRevision;
179
+ /** Returns true when widgetId still exists in the host widget list. */
180
+ export let isWidgetKnown: CanvasHostProps['isWidgetKnown'] = DEFAULT_HOST_PROPS.isWidgetKnown;
181
+
182
+ // Controls whether canvas debug/info logs are printed to the console.
183
+ // Set to false in production or when logs clutter the console.
184
+ export let showLogs: CanvasHostProps['showLogs'] = DEFAULT_HOST_PROPS.showLogs;
185
+
186
+ /**
187
+ * Whether a freehand stroke that is a clean shape (a circle, an oval, a rectangle, a triangle, a diamond, a hexagon)
188
+ * becomes that shape. Off unless a page passes true: the package's own flag turns it on for every page.
189
+ */
190
+ export let shapeSnap: CanvasHostProps['shapeSnap'] = DEFAULT_HOST_PROPS.shapeSnap;
191
+
192
+ /**
193
+ * The language of the canvas: `locale` ("de", "pt-BR") picks the plural rules, the number format and the language
194
+ * the page's own words are in; `messages` are the page's own words, keyed like CANVAS_MESSAGES (a key left out
195
+ * keeps its English text). Without them the canvas speaks English. Both can change while the canvas is open.
196
+ */
197
+ export let locale: string | null = null;
198
+ export let messages: Readonly<Record<string, unknown>> | null = null;
199
+
200
+ /**
201
+ * THE HOST AND ITS STORES
202
+ */
203
+ // The one host of this canvas: it owns the engine, the autosave and the conversions. Made with the defaults; the first
204
+ // `setProps` below (it runs before the first paint) gives it what the page set.
205
+ const host = new CanvasHost();
206
+ const { engineStore, uiStore } = host;
207
+
208
+ // The language of everything on the canvas: the toolbar, the panels and the menus read their words from it, and the
209
+ // host reads the ones it says itself (the names on the widget cards).
210
+ const language = provideCanvasLanguage({ locale, messages });
211
+ const { t } = language;
212
+ $: language.update({ locale, messages });
213
+ $: host.setTranslator($t);
214
+
215
+ // Typed as the host's full set of props, so a prop that is added to the host and forgotten here does not compile.
216
+ let hostProps: CanvasHostProps;
217
+ $: hostProps = {
218
+ showContainerFrame,
219
+ widgetSelectionLayerTarget,
220
+ onObjectClick,
221
+ panZoomPosition,
222
+ toolbarPosition,
223
+ wireframeMode,
224
+ advancedFeatures,
225
+ showElementSelector,
226
+ showDrawingTools,
227
+ elementSelectorActive,
228
+ onToggleElementSelector,
229
+ selectorGuide,
230
+ showZoomPanel,
231
+ mediaBtsOnBar,
232
+ initialData,
233
+ onReady,
234
+ isExternalDataLoading,
235
+ onBeforeUnmount,
236
+ onAutoSave,
237
+ autosaveDelayMs,
238
+ autosaveMaxWaitMs,
239
+ suspendAutosave,
240
+ showSaveButton,
241
+ onCanvasDirty,
242
+ onCanvasCleared,
243
+ onConvertToWidget,
244
+ onWidgetImageClick,
245
+ onRecognizeStroke,
246
+ onNonWidgetSelected,
247
+ onWidgetShapeDuplicated,
248
+ onWidgetShapeDeleting,
249
+ onWidgetRename,
250
+ onRefreshScreenshot,
251
+ onSetPrimaryWidget,
252
+ onAfterRender,
253
+ getWidgetName,
254
+ widgetNameRevision,
255
+ isWidgetKnown,
256
+ showLogs,
257
+ shapeSnap
258
+ };
259
+ $: host.setProps(hostProps);
260
+
261
+ /**
262
+ * THE DOM
263
+ */
264
+ let rootElement: HTMLElement | null = null;
265
+ let boardElement: HTMLElement | null = null;
266
+ // The layer menus, popovers and tooltips are drawn into. It exists a moment after the first render, and they open later:
267
+ // they ask for it through a store, so the one that opens first finds it.
268
+ let floatingLayerElement: HTMLElement | null = null;
269
+ const floatingLayer = writable<HTMLElement | null>(null);
270
+ provideFloatingLayer(floatingLayer);
271
+ $: floatingLayer.set(floatingLayerElement);
272
+ // The host starts listening once, when both elements exist (the board is made by the stage inside the root).
273
+ let attached = false;
274
+ $: if (!attached && rootElement && boardElement) {
275
+ attached = true;
276
+ host.attach(rootElement, boardElement);
277
+ }
278
+
279
+ // Only a canvas that was on a screen has a board to hand over: a page rendered on the server also runs this, and must
280
+ // not call `onBeforeUnmount` with nothing.
281
+ onDestroy(() => {
282
+ if (attached) host.destroy();
283
+ });
284
+
285
+ /**
286
+ * WHAT IS SHOWN
287
+ */
288
+ $: engineState = $engineStore;
289
+ $: uiState = $uiStore;
290
+ // The board is loading, or the page says its data is: input waits and the loader shows.
291
+ $: loading = uiState.loadPhase !== 'ready' || isExternalDataLoading;
292
+ $: showEmptyBoardHint = shouldShowEmptyBoardHint({
293
+ showDrawingHint: showDrawingTools,
294
+ emptyHintGateOpen: uiState.emptyHintGateOpen,
295
+ loading,
296
+ sceneObjectCount: engineState.scene.length,
297
+ hasPreviewOverlay: engineState.overlay.preview !== null,
298
+ editingText: engineState.editingTextId !== null
299
+ });
300
+ // A live widget laid over a selected card hides the frame the board draws, so the page's own layer draws it instead.
301
+ $: frameElsewhere = widgetSelectionLayerTarget !== '' && onlyCardIsSelected(engineState);
302
+ $: showSelectorBar = !showDrawingTools && showElementSelector;
303
+ $: showZoomControl = showZoomPanel || showSaveButton;
304
+ // Where the two cards of the chrome stand. Each names one of eight anchors; two named for the same one share a slot, side by side.
305
+ $: toolbarAnchor = normalizeToolbarPosition(toolbarPosition);
306
+ $: zoomAnchor = normalizePanZoomPosition(panZoomPosition);
307
+ $: slots = chromeSlots(toolbarAnchor, zoomAnchor, { toolbar: showDrawingTools || showSelectorBar, zoom: showZoomControl });
308
+ $: zoomVisibleFrom = zoomVisibilityClasses(toolbarAnchor, zoomAnchor);
309
+ // The selection panel stands in the top corner at the end of the line: it keeps clear of the cards the page put there.
310
+ $: panelClearance = panelClearanceFor(slots);
311
+
312
+ /**
313
+ * THE FUNCTIONS THE PAGE CALLS
314
+ *
315
+ * Each one is the host's method of the same name, with its type. The list is `CANVAS_EXPORTED_FUNCTION_NAMES`.
316
+ */
317
+ export const shapeFillColor = forwardToHost(host, 'shapeFillColor');
318
+ export const captureCanvasSketch = forwardToHost(host, 'captureCanvasSketch');
319
+ export const absorbCanvasSketchIntoWidget = forwardToHost(host, 'absorbCanvasSketchIntoWidget');
320
+ export const deselectAll = forwardToHost(host, 'deselectAll');
321
+ export const duplicateSelectedObjects = forwardToHost(host, 'duplicateSelectedObjects');
322
+ export const deleteSelectedObjects = forwardToHost(host, 'deleteSelectedObjects');
323
+ export const markSelectedAsDuplicating = forwardToHost(host, 'markSelectedAsDuplicating');
324
+ export const linkObjectToWidget = forwardToHost(host, 'linkObjectToWidget');
325
+ export const linkFrameToWidget = forwardToHost(host, 'linkFrameToWidget');
326
+ export const replaceObjectWithWidgetImage = forwardToHost(host, 'replaceObjectWithWidgetImage');
327
+ export const updateWidgetImage = forwardToHost(host, 'updateWidgetImage');
328
+ export const selectWidgetShape = forwardToHost(host, 'selectWidgetShape');
329
+ export const listWidgetShapeScreenRects = forwardToHost(host, 'listWidgetShapeScreenRects');
330
+ export const getWidgetShapeScreenRect = forwardToHost(host, 'getWidgetShapeScreenRect');
331
+ export const getObjectScreenRect = forwardToHost(host, 'getObjectScreenRect');
332
+ export const getWidgetShapeDataUrl = forwardToHost(host, 'getWidgetShapeDataUrl');
333
+ export const setWidgetShapeLivePreviewHiddenImage = forwardToHost(host, 'setWidgetShapeLivePreviewHiddenImage');
334
+ export const addWidgetShape = forwardToHost(host, 'addWidgetShape');
335
+ export const getLinkedWidgetIds = forwardToHost(host, 'getLinkedWidgetIds');
336
+ export const keepOnlyWidgetShape = forwardToHost(host, 'keepOnlyWidgetShape');
337
+ export const hydrateWidgetShapeMetadata = forwardToHost(host, 'hydrateWidgetShapeMetadata');
338
+ export const markOrphanedWidgetShapes = forwardToHost(host, 'markOrphanedWidgetShapes');
339
+ export const onSelectSelectTool = forwardToHost(host, 'onSelectSelectTool');
340
+ export const onSelectLibraryShapeTool = forwardToHost(host, 'onSelectLibraryShapeTool');
341
+ export const onSelectEraseTool = forwardToHost(host, 'onSelectEraseTool');
342
+ export const onSelectDrawTool = forwardToHost(host, 'onSelectDrawTool');
343
+ export const onSelectTextTool = forwardToHost(host, 'onSelectTextTool');
344
+ export const onSelectArrowTool = forwardToHost(host, 'onSelectArrowTool');
345
+ export const onSelectShapeTool = forwardToHost(host, 'onSelectShapeTool');
346
+ export const onSelectFrameTool = forwardToHost(host, 'onSelectFrameTool');
347
+ export const setPrimaryWidgetShape = forwardToHost(host, 'setPrimaryWidgetShape');
348
+ export const getPrimaryWidgetId = forwardToHost(host, 'getPrimaryWidgetId');
349
+ export const setWidgetShapeBodyDragAllowed = forwardToHost(host, 'setWidgetShapeBodyDragAllowed');
350
+ export const translateWidgetShapeByScreen = forwardToHost(host, 'translateWidgetShapeByScreen');
351
+ export const commitWidgetShapeTranslate = forwardToHost(host, 'commitWidgetShapeTranslate');
352
+ export const setWidgetShapeOverlayDragActive = forwardToHost(host, 'setWidgetShapeOverlayDragActive');
353
+ export const selectWidgetShapeById = forwardToHost(host, 'selectWidgetShapeById');
354
+ export const getSelectedWidgetShapeId = forwardToHost(host, 'getSelectedWidgetShapeId');
355
+ export const getSelectedObject = forwardToHost(host, 'getSelectedObject');
356
+ export const getHoveredWidgetShapeId = forwardToHost(host, 'getHoveredWidgetShapeId');
357
+ export const onSelectImageTool = forwardToHost(host, 'onSelectImageTool');
358
+ export const onSelectVideoTool = forwardToHost(host, 'onSelectVideoTool');
359
+ export const onSelectPanTool = forwardToHost(host, 'onSelectPanTool');
360
+ export const zoomIn = forwardToHost(host, 'zoomIn');
361
+ export const zoomOut = forwardToHost(host, 'zoomOut');
362
+ export const zoomTo = forwardToHost(host, 'zoomTo');
363
+ export const zoomReset = forwardToHost(host, 'zoomReset');
364
+ export const zoomFit = forwardToHost(host, 'zoomFit');
365
+ export const onGroupSelection = forwardToHost(host, 'onGroupSelection');
366
+ export const onUngroupSelection = forwardToHost(host, 'onUngroupSelection');
367
+ export const onSelectAllObjects = forwardToHost(host, 'onSelectAllObjects');
368
+ export const onDeselectAllObjects = forwardToHost(host, 'onDeselectAllObjects');
369
+ export const flushAutosave = forwardToHost(host, 'flushAutosave');
370
+ export const saveNow = forwardToHost(host, 'saveNow');
371
+ export const undo = forwardToHost(host, 'undo');
372
+ export const redo = forwardToHost(host, 'redo');
373
+ export const saveCanvas = forwardToHost(host, 'saveCanvas');
374
+ export const loadCanvas = forwardToHost(host, 'loadCanvas');
375
+ export const onClearAllObjects = forwardToHost(host, 'onClearAllObjects');
376
+ export const addText = forwardToHost(host, 'addText');
377
+ export const addTriangle = forwardToHost(host, 'addTriangle');
378
+ export const addRect = forwardToHost(host, 'addRect');
379
+ export const addCircle = forwardToHost(host, 'addCircle');
380
+ export const addFrame = forwardToHost(host, 'addFrame');
381
+ export const addImage = forwardToHost(host, 'addImage');
382
+ // Added to the contract. The widget creator's in-flight branches call it, and the simple-board branch
383
+ // (feat/shopify-feedback-canvas) adds the same function (same name, same answer).
384
+ export const removeSketchObjectsKeepingWidget = forwardToHost(host, 'removeSketchObjectsKeepingWidget');
385
+ </script>
386
+
387
+ <Tooltip.Provider delayDuration={400} skipDelayDuration={200}>
388
+ <div
389
+ bind:this={rootElement}
390
+ class="{className ? className + ' ' : ''}canvas-container @container relative isolate h-full w-full overflow-visible bg-muted text-foreground outline-hidden {showContainerFrame ? BOARD_FRAME_CLASSES : ''}"
391
+ {style}
392
+ data-canvas-root
393
+ >
394
+ <!-- The board: the pointer surface, with the objects, the selection, the text field and the loader. -->
395
+ <CanvasStage
396
+ {host}
397
+ state={engineState}
398
+ ui={uiState}
399
+ {advancedFeatures}
400
+ {loading}
401
+ {showEmptyBoardHint}
402
+ {frameElsewhere}
403
+ bind:board={boardElement}
404
+ />
405
+
406
+ <!-- The chrome: siblings of the board, so a press on a button never reaches it. It waits while the board loads. -->
407
+ <div class="pointer-events-none absolute inset-0 z-10 transition-opacity duration-200 motion-reduce:transition-none {loading ? 'opacity-60' : ''}" inert={loading}>
408
+ <!-- One slot for each place that has a card: the toolbar (or the selector bar) and the zoom control, side by side when they share a place. -->
409
+ {#each slots as slot (slot.anchor)}
410
+ <div class={slot.classes} data-canvas-slot={slot.anchor}>
411
+ {#each slot.cards as card (card)}
412
+ {#if card === 'toolbar'}
413
+ {#if showDrawingTools}
414
+ <CanvasToolbar {host} state={engineState} ui={uiState} {showDrawingTools} anchor={toolbarAnchor} />
415
+ {:else}
416
+ <SelectorBar active={elementSelectorActive} guide={selectorGuide} anchor={toolbarAnchor} onToggle={() => onToggleElementSelector?.()} />
417
+ {/if}
418
+ {:else}
419
+ <ZoomControl {host} state={engineState} ui={uiState} anchor={zoomAnchor} visibleFrom={zoomVisibleFrom} showZoom={showZoomPanel} {showSaveButton} />
420
+ {/if}
421
+ {/each}
422
+ </div>
423
+ {/each}
424
+ </div>
425
+
426
+ <!-- The panel that edits the selection: a selection opens it, and its close button puts it away. -->
427
+ <SelectionPanel
428
+ {host}
429
+ state={engineState}
430
+ ui={uiState}
431
+ {wireframeMode}
432
+ canCapture={!!onObjectClick}
433
+ canConvert={!!onConvertToWidget}
434
+ canRename={!!onWidgetRename}
435
+ canRefresh={!!onRefreshScreenshot}
436
+ canOpenDetails={!!onWidgetImageClick}
437
+ clearance={panelClearance}
438
+ />
439
+
440
+ <CanvasContextMenu {host} state={engineState} ui={uiState} board={boardElement} />
441
+
442
+ {#if uiState.shortcutsOpen}
443
+ <ShortcutsPanel {host} />
444
+ {/if}
445
+
446
+ <!-- The frame of a selected widget card, in the page's layer above its live widgets. -->
447
+ {#if frameElsewhere}
448
+ <WidgetChromeLayer {host} state={engineState} gestureKind={uiState.gestureKind} {advancedFeatures} target={widgetSelectionLayerTarget} />
449
+ {/if}
450
+
451
+ <!-- Where every menu, popover and tooltip is drawn: a layer without a size that is its own containing block. -->
452
+ <div bind:this={floatingLayerElement} class={FLOATING_LAYER_CLASSES} data-canvas-floating-layer></div>
453
+ </div>
454
+ </Tooltip.Provider>