@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,185 @@
1
+ <!--
2
+ Figma "Shape Menu": the button with the category icon and a chevron at the end of the toolbar's drawing group, and the menu it
3
+ opens: the eight shapes people reach for (a square, a rounded square, a circle, a triangle, a diamond, a hexagon, a star
4
+ and a pentagon), the media entries (an image, an SVG), and "More shapes", which opens the whole library by group.
5
+
6
+ The menu is the design system's Dropdown Menu, portaled so it is not clipped by a hosting card (see floatingLayer.ts).
7
+ Choosing a shape does not draw it: it arms the tool,
8
+ and the shape is dragged out on the board, as for every tool.
9
+ -->
10
+ <script lang="ts">
11
+ import { DropdownMenu } from '@widgetic/design-system/components';
12
+ import type { ToolId } from '../../engine/state.js';
13
+ import { useCanvasLanguage } from '../../i18n/language.js';
14
+ import { categoryLabel } from '../../i18n/labels.js';
15
+ import { getShapeByKey, getShapesByCategory } from '../shapes/ShapeLibrary.js';
16
+ import type { LibraryShape } from '../shapes/ShapeLibrary.js';
17
+ import type { CanvasHost } from '../host/CanvasHost.js';
18
+ import type { CanvasMessageKey } from '../../i18n/messages.js';
19
+ import type { CanvasIcon } from './icons.js';
20
+ import {
21
+ CANVAS_OVERLAY_Z_CLASS,
22
+ MENU_PADDING_CLASS,
23
+ MENU_TRIGGER_RING_CLASSES,
24
+ OVERLAY_REDUCED_MOTION_CLASSES,
25
+ SHAPE_LIBRARY_STROKE_WIDTH,
26
+ TOOLBAR_ICON_STROKE_CLASSES
27
+ } from './floatingCard.js';
28
+ import { canvasOverlayPortalProps, useFloatingLayer } from './floatingLayer.js';
29
+ import { usePopoverSide } from './popoverSide.js';
30
+ import {
31
+ IconBrowser,
32
+ IconCategory,
33
+ IconChevronDown,
34
+ IconCircle,
35
+ IconDiamond,
36
+ IconHexagon,
37
+ IconPentagon,
38
+ IconPhoto,
39
+ IconSquare,
40
+ IconSquareRounded,
41
+ IconStar,
42
+ IconTriangle
43
+ } from './icons.js';
44
+
45
+ interface Props {
46
+ host: CanvasHost;
47
+ /** The tool in use, so the tile of the shape being drawn is marked. */
48
+ tool: ToolId;
49
+ /** The key of the library shape the library tool draws, when it is the tool. */
50
+ libraryShapeKey: string | null;
51
+ open: boolean;
52
+ /** The key that opens the image chooser, as it is printed on a key cap. */
53
+ imageShortcut: string;
54
+ }
55
+
56
+ let { host, tool, libraryShapeKey, open, imageShortcut }: Props = $props();
57
+
58
+ // The menu is drawn into the canvas's floating layer (see floatingLayer.ts).
59
+ const floatingLayer = useFloatingLayer();
60
+
61
+ // It opens away from the edge the toolbar stands against: upward from the bottom edge, downward from the top edge, and
62
+ // sideways from an upright bar, where it lines up with its button's top edge instead of being centred on it.
63
+ const popoverSide = usePopoverSide();
64
+ const menuSide = $derived(popoverSide());
65
+ const menuAlign = $derived(menuSide === 'left' || menuSide === 'right' ? 'start' : 'center');
66
+
67
+ const { t } = useCanvasLanguage();
68
+
69
+ /** A tile of the first two rows: what it draws, how it is named, and when it is the one in use. */
70
+ interface Tile {
71
+ id: string;
72
+ icon: CanvasIcon;
73
+ label: CanvasMessageKey;
74
+ active: boolean;
75
+ choose: () => void;
76
+ }
77
+
78
+ const tiles = $derived<Tile[]>([
79
+ { id: 'rect', icon: IconSquare, label: 'canvas.tool.rectangle', active: tool === 'rect', choose: () => host.onSelectShapeTool('rect') },
80
+ { id: 'rounded-rect', icon: IconSquareRounded, label: 'canvas.tool.roundedRectangle', active: tool === 'rounded-rect', choose: () => host.onSelectShapeTool('rounded-rect') },
81
+ { id: 'circle', icon: IconCircle, label: 'canvas.tool.circle', active: tool === 'circle', choose: () => host.onSelectShapeTool('circle-true') },
82
+ { id: 'triangle', icon: IconTriangle, label: 'canvas.tool.triangle', active: tool === 'triangle', choose: () => host.onSelectShapeTool('triangle') },
83
+ libraryTile('diamond', IconDiamond),
84
+ libraryTile('hexagon', IconHexagon),
85
+ libraryTile('star', IconStar),
86
+ libraryTile('pentagon', IconPentagon)
87
+ ]);
88
+
89
+ /** A tile that arms a shape of the library. The library says how the shape is named. */
90
+ function libraryTile(shapeKey: string, icon: CanvasIcon): Tile {
91
+ const shape = getShapeByKey(shapeKey);
92
+ return {
93
+ id: shapeKey,
94
+ icon,
95
+ label: shape?.labelKey ?? 'canvas.tool.shapes',
96
+ active: tool === 'library-shape' && libraryShapeKey === shapeKey,
97
+ choose: () => host.onSelectLibraryShapeTool(shapeKey)
98
+ };
99
+ }
100
+
101
+ // "More shapes" lists the whole library under the names of its groups.
102
+ const libraryGroups: [string, LibraryShape[]][] = [...getShapesByCategory().entries()];
103
+
104
+ // The trigger is lit while any shape tool is the one in use.
105
+ const shapeToolInUse = $derived(tool === 'rect' || tool === 'rounded-rect' || tool === 'ellipse' || tool === 'circle' || tool === 'triangle' || tool === 'library-shape');
106
+
107
+ function setOpen(next: boolean): void {
108
+ host.updateUi({ shapesMenuOpen: next });
109
+ }
110
+ </script>
111
+
112
+ <DropdownMenu.Root {open} onOpenChange={setOpen}>
113
+ <DropdownMenu.Trigger>
114
+ {#snippet child({ props }: { props: Record<string, unknown> })}
115
+ <button
116
+ {...props}
117
+ type="button"
118
+ aria-label={$t('canvas.tool.shapes')}
119
+ data-active={shapeToolInUse ? '' : undefined}
120
+ class="flex h-9 w-[51px] shrink-0 items-center justify-center gap-1 rounded-lg text-foreground transition-colors duration-fast hover:bg-accent focus-visible:outline-hidden data-[active]:bg-brand-subtle {MENU_TRIGGER_RING_CLASSES} [&_svg]:size-4 {TOOLBAR_ICON_STROKE_CLASSES}"
121
+ >
122
+ <IconCategory />
123
+ <IconChevronDown class="transition-transform duration-fast motion-reduce:transition-none data-[open]:rotate-180" data-open={open ? '' : undefined} />
124
+ </button>
125
+ {/snippet}
126
+ </DropdownMenu.Trigger>
127
+
128
+ <DropdownMenu.Content side={menuSide} align={menuAlign} sideOffset={8} portalProps={canvasOverlayPortalProps($floatingLayer)} aria-label={$t('canvas.tool.shapes')} class="{CANVAS_OVERLAY_Z_CLASS} w-[196px] {MENU_PADDING_CLASS} shadow-floating focus-visible:outline-hidden {OVERLAY_REDUCED_MOTION_CLASSES}">
129
+ <DropdownMenu.Label>{$t('canvas.tool.shapes')}</DropdownMenu.Label>
130
+ <div class="grid grid-cols-4 gap-x-3 px-1" role="presentation">
131
+ {#each tiles as tile (tile.id)}
132
+ <DropdownMenu.Item
133
+ aria-label={$t(tile.label)}
134
+ aria-current={tile.active ? 'true' : undefined}
135
+ title={$t(tile.label)}
136
+ data-active={tile.active ? '' : undefined}
137
+ class="size-9 min-h-9 justify-center p-0 data-[active]:bg-brand-subtle {TOOLBAR_ICON_STROKE_CLASSES}"
138
+ onSelect={tile.choose}
139
+ >
140
+ <tile.icon class="size-4" />
141
+ </DropdownMenu.Item>
142
+ {/each}
143
+ </div>
144
+
145
+ <DropdownMenu.Separator />
146
+ <DropdownMenu.Label>{$t('canvas.menu.media')}</DropdownMenu.Label>
147
+ <DropdownMenu.Item aria-keyshortcuts={imageShortcut} onSelect={() => void host.addImage()}>
148
+ <IconPhoto />
149
+ <span class="flex-1">{$t('canvas.tool.image')}</span>
150
+ <!-- The key is for the eye: the name a screen reader reads is "Image", and the key is given as `aria-keyshortcuts`. -->
151
+ <DropdownMenu.Shortcut aria-hidden="true">{imageShortcut}</DropdownMenu.Shortcut>
152
+ </DropdownMenu.Item>
153
+ <DropdownMenu.Item onSelect={() => void host.addSvg()}>
154
+ <IconBrowser />
155
+ <span class="flex-1">{$t('canvas.tool.svg')}</span>
156
+ </DropdownMenu.Item>
157
+
158
+ <DropdownMenu.Separator />
159
+ <DropdownMenu.Sub>
160
+ <DropdownMenu.SubTrigger>{$t('canvas.tool.moreShapes')}</DropdownMenu.SubTrigger>
161
+ <DropdownMenu.SubContent aria-label={$t('canvas.tool.moreShapes')} class="{CANVAS_OVERLAY_Z_CLASS} max-h-[min(24rem,70vh)] w-[220px] overflow-y-auto {MENU_PADDING_CLASS} shadow-floating {OVERLAY_REDUCED_MOTION_CLASSES}">
162
+ {#each libraryGroups as [category, shapes] (category)}
163
+ <DropdownMenu.Label>{categoryLabel(category, $t)}</DropdownMenu.Label>
164
+ <div class="grid grid-cols-5 gap-1 px-1 pb-1" role="presentation">
165
+ {#each shapes as shape (shape.key)}
166
+ <DropdownMenu.Item
167
+ aria-label={$t(shape.labelKey)}
168
+ aria-current={tool === 'library-shape' && libraryShapeKey === shape.key ? 'true' : undefined}
169
+ title={$t(shape.labelKey)}
170
+ data-active={tool === 'library-shape' && libraryShapeKey === shape.key ? '' : undefined}
171
+ class="size-9 min-h-9 justify-center p-0 data-[active]:bg-brand-subtle"
172
+ onSelect={() => host.onSelectLibraryShapeTool(shape.key)}
173
+ >
174
+ <!-- The width is an inline style on purpose: the menu item's `[&_svg]:stroke-2` would beat the attribute (see floatingCard.ts). -->
175
+ <svg viewBox="0 0 100 100" class="size-4" fill="none" stroke="currentColor" style:stroke-width={SHAPE_LIBRARY_STROKE_WIDTH} stroke-linejoin="round" stroke-linecap="round" aria-hidden="true">
176
+ <path d={shape.pathData} />
177
+ </svg>
178
+ </DropdownMenu.Item>
179
+ {/each}
180
+ </div>
181
+ {/each}
182
+ </DropdownMenu.SubContent>
183
+ </DropdownMenu.Sub>
184
+ </DropdownMenu.Content>
185
+ </DropdownMenu.Root>
@@ -0,0 +1,15 @@
1
+ import type { ToolId } from '../../engine/state.js';
2
+ import type { CanvasHost } from '../host/CanvasHost.js';
3
+ interface Props {
4
+ host: CanvasHost;
5
+ /** The tool in use, so the tile of the shape being drawn is marked. */
6
+ tool: ToolId;
7
+ /** The key of the library shape the library tool draws, when it is the tool. */
8
+ libraryShapeKey: string | null;
9
+ open: boolean;
10
+ /** The key that opens the image chooser, as it is printed on a key cap. */
11
+ imageShortcut: string;
12
+ }
13
+ declare const ShapesMenu: import("svelte").Component<Props, {}, "">;
14
+ type ShapesMenu = ReturnType<typeof ShapesMenu>;
15
+ export default ShapesMenu;
@@ -0,0 +1,124 @@
1
+ <!--
2
+ The list of keyboard shortcuts, opened by "?" or by the keyboard button of the zoom control.
3
+
4
+ A plain panel of the canvas, drawn over the board inside the canvas's own root: the design system's Dialog is `fixed` to the
5
+ page, which would take it out of an embedded canvas. The groups come from `shortcuts.ts`, the same table the tooltips read,
6
+ so the list and the hints cannot disagree. Escape or a press outside closes it. The panel is modal, so it keeps the
7
+ keyboard: Tab goes round its own controls and does not reach the toolbar behind it, and the focus goes back to the board when
8
+ it closes. Escape is the panel's own to handle: the host leaves the keys pressed inside a dialog alone (a menu's typeahead,
9
+ a field's letters), so a panel that waited for the host to close it would never close from the keyboard.
10
+ -->
11
+ <script lang="ts">
12
+ import { IconButton } from '@widgetic/design-system/components';
13
+ import { getFocusedElementIn } from '../eventScope.js';
14
+ import { useCanvasLanguage } from '../../i18n/language.js';
15
+ import { shortcutGroups } from '../../i18n/shortcuts.js';
16
+ import type { CanvasHost } from '../host/CanvasHost.js';
17
+ import { IconX } from './icons.js';
18
+
19
+ interface Props {
20
+ host: CanvasHost;
21
+ }
22
+
23
+ let { host }: Props = $props();
24
+
25
+ const { t } = useCanvasLanguage();
26
+
27
+ const groups = $derived(shortcutGroups($t));
28
+
29
+ /** Closes the panel and gives the board the keyboard back; a press of the pointer on the backdrop leaves no focus ring. */
30
+ function close(byPointer = false): void {
31
+ host.updateUi({ shortcutsOpen: false });
32
+ host.focusBoard(byPointer ? { focusVisible: false } : {});
33
+ }
34
+
35
+ /** What the keyboard can reach inside the panel. */
36
+ const FOCUSABLE_SELECTOR = 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
37
+
38
+ /** Escape closes the panel; Tab and Shift+Tab go round the panel's controls and stay in it. */
39
+ function handleKeyDown(event: KeyboardEvent): void {
40
+ try {
41
+ if (event.key === 'Escape') {
42
+ event.preventDefault();
43
+ event.stopPropagation();
44
+ close();
45
+ return;
46
+ }
47
+ if (event.key !== 'Tab') return;
48
+ const panel = event.currentTarget as HTMLElement;
49
+ const focusable = Array.from(panel.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR));
50
+ if (focusable.length === 0) {
51
+ event.preventDefault();
52
+ return;
53
+ }
54
+ const first = focusable[0];
55
+ const last = focusable[focusable.length - 1];
56
+ const active = getFocusedElementIn(panel);
57
+ if (event.shiftKey && (active === panel || active === first)) {
58
+ event.preventDefault();
59
+ last.focus();
60
+ } else if (!event.shiftKey && active === last) {
61
+ event.preventDefault();
62
+ first.focus();
63
+ }
64
+ } catch (error) {
65
+ console.warn('Canvas: a key in the shortcuts panel failed', error);
66
+ }
67
+ }
68
+
69
+ /** The panel takes the focus when it opens, so a screen reader starts reading it and Tab stays inside it. */
70
+ function takeFocus(panel: HTMLElement): void {
71
+ panel.focus({ preventScroll: true });
72
+ }
73
+ </script>
74
+
75
+ <div
76
+ role="presentation"
77
+ class="pointer-events-auto absolute inset-0 z-40 flex items-center justify-center bg-foreground/10 p-4"
78
+ data-canvas-shortcuts-backdrop
79
+ onpointerdown={(event) => {
80
+ if (event.target === event.currentTarget) close(true);
81
+ }}
82
+ >
83
+ <div
84
+ role="dialog"
85
+ aria-modal="true"
86
+ aria-label={$t('canvas.shortcuts.title')}
87
+ tabindex="-1"
88
+ onkeydown={handleKeyDown}
89
+ {@attach takeFocus}
90
+ data-canvas-shortcuts
91
+ class="flex max-h-full w-[min(600px,100%)] flex-col gap-3 overflow-y-auto rounded-card border border-border bg-popover p-4 text-foreground shadow-floating outline-hidden"
92
+ >
93
+ <header class="flex items-center justify-between gap-2">
94
+ <h2 class="text-body font-semibold">{$t('canvas.shortcuts.title')}</h2>
95
+ <IconButton label={$t('canvas.shortcuts.close')} onclick={() => close()}>
96
+ <IconX />
97
+ </IconButton>
98
+ </header>
99
+
100
+ <div class="grid gap-x-8 gap-y-4 @lg:grid-cols-2">
101
+ {#each groups as group (group.title)}
102
+ <section class="flex flex-col gap-1.5">
103
+ <h3 class="text-caption-medium text-muted-foreground">{group.title}</h3>
104
+ <ul class="flex flex-col gap-1">
105
+ {#each group.shortcuts as shortcut (shortcut.label)}
106
+ <li class="flex items-center justify-between gap-3 text-body">
107
+ <span class="min-w-0 truncate">{shortcut.label}</span>
108
+ <span class="flex shrink-0 items-center gap-1">
109
+ {#each shortcut.keys as key, index (index)}
110
+ {#if key === null}
111
+ <span class="text-caption text-muted-foreground" aria-hidden="true">+</span>
112
+ {:else}
113
+ <kbd class="min-w-6 rounded-md border border-border bg-muted px-1.5 py-0.5 text-center font-sans text-xs text-muted-foreground">{key}</kbd>
114
+ {/if}
115
+ {/each}
116
+ </span>
117
+ </li>
118
+ {/each}
119
+ </ul>
120
+ </section>
121
+ {/each}
122
+ </div>
123
+ </div>
124
+ </div>
@@ -0,0 +1,7 @@
1
+ import type { CanvasHost } from '../host/CanvasHost.js';
2
+ interface Props {
3
+ host: CanvasHost;
4
+ }
5
+ declare const ShortcutsPanel: import("svelte").Component<Props, {}, "">;
6
+ type ShortcutsPanel = ReturnType<typeof ShortcutsPanel>;
7
+ export default ShortcutsPanel;
@@ -0,0 +1,78 @@
1
+ <!--
2
+ One button of the chrome: the design system's Icon Button with its name in a tooltip, and the key that does the same.
3
+
4
+ Figma "Tool Button": 36 px square, 8 px corners, a 16 px icon; the tool that is on has the brand tint behind it. The name
5
+ is also the button's accessible name, so a screen reader hears what a pointer user reads. The zoom level is the one
6
+ button that holds a word (the percentage) instead of an icon, so it is a plain button of the same look, 40 x 32 px.
7
+ -->
8
+ <script lang="ts">
9
+ import type { Snippet } from 'svelte';
10
+ import { IconButton, Tooltip } from '@widgetic/design-system/components';
11
+ import { CANVAS_OVERLAY_Z_CLASS, OVERLAY_REDUCED_MOTION_CLASSES } from './floatingCard.js';
12
+ import { canvasOverlayPortalProps, useFloatingLayer } from './floatingLayer.js';
13
+ import { usePopoverSide } from './popoverSide.js';
14
+
15
+ interface Props {
16
+ /** What the button does. It is the accessible name and the first line of the tooltip. */
17
+ label: string;
18
+ /** The key that does the same, as it is printed on a key cap ("V"), or nothing. It is also the button's `aria-keyshortcuts`. */
19
+ shortcut?: string | undefined;
20
+ /** The tool this button chooses is the one in use. */
21
+ active?: boolean;
22
+ /** Whether the button acts like a switch (it then says whether it is on to a screen reader). */
23
+ toggle?: boolean;
24
+ disabled?: boolean;
25
+ /** The size: a 36 px square for the toolbar, a 32 px square for the zoom control, and 40 x 32 px for its level. */
26
+ size?: 'toolbar' | 'compact' | 'level';
27
+ class?: string;
28
+ onclick: (event: MouseEvent) => void;
29
+ children: Snippet;
30
+ }
31
+
32
+ let { label, shortcut = undefined, active = false, toggle = false, disabled = false, size = 'toolbar', class: className = '', onclick, children }: Props = $props();
33
+
34
+ // The tooltip is drawn into the canvas's floating layer, not in place: see floatingLayer.ts.
35
+ const floatingLayer = useFloatingLayer();
36
+ // It opens away from the edge the card stands on: upward from the bottom, downward from the top, sideways from a side.
37
+ const popoverSide = usePopoverSide();
38
+
39
+ // Written out in full, so that the stylesheet of a page that installs the canvas picks every class up.
40
+ const SQUARE_CLASSES = { toolbar: 'size-9', compact: 'size-8' } as const;
41
+ const LEVEL_CLASSES =
42
+ 'inline-flex h-8 w-10 shrink-0 items-center justify-center rounded-lg text-xs font-medium text-muted-foreground transition-colors duration-fast motion-reduce:transition-none hover:bg-accent hover:text-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50';
43
+ </script>
44
+
45
+ <Tooltip.Root>
46
+ <Tooltip.Trigger>
47
+ {#snippet child({ props }: { props: Record<string, unknown> })}
48
+ {#if size === 'level'}
49
+ <button {...props} type="button" aria-label={label} aria-keyshortcuts={shortcut} {disabled} {onclick} class="{LEVEL_CLASSES} {className}">
50
+ {@render children()}
51
+ </button>
52
+ {:else}
53
+ <IconButton
54
+ {...props}
55
+ {label}
56
+ {disabled}
57
+ {onclick}
58
+ aria-pressed={toggle ? active : undefined}
59
+ aria-keyshortcuts={shortcut}
60
+ data-active={active ? '' : undefined}
61
+ class="{SQUARE_CLASSES[size]} data-[active]:bg-brand-subtle data-[active]:text-foreground {className}"
62
+ >
63
+ {@render children()}
64
+ </IconButton>
65
+ {/if}
66
+ {/snippet}
67
+ </Tooltip.Trigger>
68
+ <Tooltip.Portal to={canvasOverlayPortalProps($floatingLayer).to}>
69
+ <Tooltip.Content side={popoverSide()} sideOffset={8} class="{CANVAS_OVERLAY_Z_CLASS} {OVERLAY_REDUCED_MOTION_CLASSES}">
70
+ <span class="flex items-center gap-2">
71
+ <span>{label}</span>
72
+ {#if shortcut}
73
+ <kbd class="rounded-sm bg-muted px-1.5 py-0.5 font-sans text-xs text-muted-foreground">{shortcut}</kbd>
74
+ {/if}
75
+ </span>
76
+ </Tooltip.Content>
77
+ </Tooltip.Portal>
78
+ </Tooltip.Root>
@@ -0,0 +1,20 @@
1
+ import type { Snippet } from 'svelte';
2
+ interface Props {
3
+ /** What the button does. It is the accessible name and the first line of the tooltip. */
4
+ label: string;
5
+ /** The key that does the same, as it is printed on a key cap ("V"), or nothing. It is also the button's `aria-keyshortcuts`. */
6
+ shortcut?: string | undefined;
7
+ /** The tool this button chooses is the one in use. */
8
+ active?: boolean;
9
+ /** Whether the button acts like a switch (it then says whether it is on to a screen reader). */
10
+ toggle?: boolean;
11
+ disabled?: boolean;
12
+ /** The size: a 36 px square for the toolbar, a 32 px square for the zoom control, and 40 x 32 px for its level. */
13
+ size?: 'toolbar' | 'compact' | 'level';
14
+ class?: string;
15
+ onclick: (event: MouseEvent) => void;
16
+ children: Snippet;
17
+ }
18
+ declare const ToolButton: import("svelte").Component<Props, {}, "">;
19
+ type ToolButton = ReturnType<typeof ToolButton>;
20
+ export default ToolButton;
@@ -0,0 +1,145 @@
1
+ <!--
2
+ The widget part of the selection panel: what a person can do with the selection that is about widgets and not about paint.
3
+
4
+ - A widget card: its name (edited in place), its id (a way to the widget's page), "Refresh preview", and the mark of the
5
+ primary widget (or the way to make it the primary one).
6
+ - A card whose widget is gone: a notice and the way to take the card off the board.
7
+ - While the page is still working on the selection: what it is doing.
8
+ - With nothing to convert: "Capture", which sends the sketch to the page.
9
+
10
+ All of it is what the page asked for: a button shows only when the page gave the callback behind it.
11
+ -->
12
+ <script lang="ts">
13
+ import { Button, Input } from '@widgetic/design-system/components';
14
+ import type { EngineState } from '../../engine/state.js';
15
+ import { useCanvasLanguage } from '../../i18n/language.js';
16
+ import type { CanvasHost, HostUiState } from '../host/CanvasHost.js';
17
+ import { IconCamera, IconLink, IconLoader2, IconRefresh, IconStar, IconTrash } from './icons.js';
18
+ import type { SelectionTraits } from './selectionTraits.js';
19
+
20
+ interface Props {
21
+ host: CanvasHost;
22
+ state: EngineState;
23
+ ui: HostUiState;
24
+ traits: SelectionTraits;
25
+ /** The page gave `onObjectClick`: a capture can be sent. */
26
+ canCapture: boolean;
27
+ /** The page gave widget card callbacks (rename, preview, details, primary). */
28
+ canConvert: boolean;
29
+ canRename: boolean;
30
+ canRefresh: boolean;
31
+ canOpenDetails: boolean;
32
+ }
33
+
34
+ let { host, state, ui, traits, canCapture, canConvert, canRename, canRefresh, canOpenDetails }: Props = $props();
35
+
36
+ const { t } = useCanvasLanguage();
37
+
38
+ const card = $derived(traits.widgetCard);
39
+ const hasSelection = $derived(traits.units.length > 0);
40
+
41
+ // What the page is doing with the selection, if anything.
42
+ const pendingState = $derived(traits.units.map((unit) => state.pending[unit.id]).find((pending) => pending !== undefined));
43
+ const pendingKey = $derived(
44
+ pendingState === 'converting' ? 'canvas.widget.converting' : pendingState === 'duplicating' ? 'canvas.widget.duplicating' : 'canvas.widget.linking'
45
+ );
46
+
47
+ // The name of the linked widget is the page's to say; it asks again whenever the page says the names changed.
48
+ const widgetName = $derived.by(() => {
49
+ void ui.nameRevision;
50
+ return card ? host.widgetLabel(card) : '';
51
+ });
52
+ const orphaned = $derived(card ? host.isCardOrphaned(card) : false);
53
+
54
+ function commitName(event: FocusEvent & { currentTarget: HTMLInputElement }): void {
55
+ if (!card) return;
56
+ const named = event.currentTarget.value.trim();
57
+ if (named && named !== widgetName) host.props.onWidgetRename?.(card.widgetId, named);
58
+ }
59
+
60
+ function keepOrCancelName(event: KeyboardEvent & { currentTarget: HTMLInputElement }): void {
61
+ // The keys of a field are the field's: the board must not take Delete, a letter or an arrow for a command.
62
+ event.stopPropagation();
63
+ if (event.key === 'Enter') event.currentTarget.blur();
64
+ if (event.key === 'Escape') {
65
+ event.currentTarget.value = widgetName;
66
+ event.currentTarget.blur();
67
+ }
68
+ }
69
+ </script>
70
+
71
+ {#if canCapture && !(canConvert && hasSelection)}
72
+ <div class="flex flex-col gap-2">
73
+ <span class="text-caption-medium text-muted-foreground">{$t('canvas.widget.createPreview')}</span>
74
+ <Button variant="outline" size="sm" onclick={() => void host.captureSelection()}>
75
+ <IconCamera />
76
+ {$t('canvas.widget.capture')}
77
+ </Button>
78
+ </div>
79
+ {/if}
80
+
81
+ {#if canConvert && hasSelection && (card || pendingState)}
82
+ <div class="flex flex-col gap-2">
83
+ {#if pendingState}
84
+ <span class="text-caption-medium text-muted-foreground">{$t('canvas.widget.linked')}</span>
85
+ <div class="flex items-center gap-2 text-body text-muted-foreground" role="status">
86
+ <IconLoader2 class="size-4 animate-spin" />
87
+ <span>{$t(pendingKey)}</span>
88
+ </div>
89
+ {:else if card}
90
+ {#if orphaned}
91
+ <span class="text-caption-medium text-muted-foreground">{$t('canvas.widget.unlinkedTitle')}</span>
92
+ <p class="text-body text-foreground">{$t('canvas.widget.orphanNotice')}</p>
93
+ <span class="truncate text-caption text-muted-foreground" title={card.widgetId}>{card.widgetId}</span>
94
+ <Button variant="outline" size="sm" onclick={() => void host.deleteSelectedObjects()}>
95
+ <IconTrash />
96
+ {$t('canvas.widget.removeFromCanvas')}
97
+ </Button>
98
+ {:else}
99
+ <span class="text-caption-medium text-muted-foreground">{$t('canvas.widget.linked')}</span>
100
+ {#if canRename}
101
+ <Input
102
+ variant="soft"
103
+ value={widgetName}
104
+ placeholder={$t('canvas.widget.namePlaceholder')}
105
+ aria-label={$t('canvas.widget.namePlaceholder')}
106
+ onblur={commitName}
107
+ onkeydown={keepOrCancelName}
108
+ />
109
+ {:else}
110
+ <span class="truncate text-body text-foreground">{widgetName}</span>
111
+ {/if}
112
+ {#if canOpenDetails}
113
+ <button
114
+ type="button"
115
+ title={$t('canvas.widget.openDetails')}
116
+ class="flex min-w-0 items-center gap-1 rounded-md text-start text-caption text-muted-foreground hover:text-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring [&_svg]:size-3 [&_svg]:shrink-0"
117
+ onclick={() => host.props.onWidgetImageClick?.(card.widgetId)}
118
+ >
119
+ <IconLink />
120
+ <span class="truncate">{card.widgetId}</span>
121
+ </button>
122
+ {:else}
123
+ <span class="truncate text-caption text-muted-foreground" title={card.widgetId}>{card.widgetId}</span>
124
+ {/if}
125
+ {#if canRefresh}
126
+ <Button variant="outline" size="sm" onclick={() => host.props.onRefreshScreenshot?.(card.widgetId)}>
127
+ <IconRefresh />
128
+ {$t('canvas.widget.refreshPreview')}
129
+ </Button>
130
+ {/if}
131
+ {#if card.isPrimary}
132
+ <span class="flex items-center gap-1 text-caption-medium text-foreground [&_svg]:size-3.5">
133
+ <IconStar />
134
+ {$t('canvas.widget.primary')}
135
+ </span>
136
+ {:else}
137
+ <Button variant="outline" size="sm" title={$t('canvas.widget.primaryTitle')} onclick={() => host.handleSetPrimaryFromPanel(card.widgetId)}>
138
+ <IconStar />
139
+ {$t('canvas.widget.setPrimary')}
140
+ </Button>
141
+ {/if}
142
+ {/if}
143
+ {/if}
144
+ </div>
145
+ {/if}
@@ -0,0 +1,19 @@
1
+ import type { EngineState } from '../../engine/state.js';
2
+ import type { CanvasHost, HostUiState } from '../host/CanvasHost.js';
3
+ import type { SelectionTraits } from './selectionTraits.js';
4
+ interface Props {
5
+ host: CanvasHost;
6
+ state: EngineState;
7
+ ui: HostUiState;
8
+ traits: SelectionTraits;
9
+ /** The page gave `onObjectClick`: a capture can be sent. */
10
+ canCapture: boolean;
11
+ /** The page gave widget card callbacks (rename, preview, details, primary). */
12
+ canConvert: boolean;
13
+ canRename: boolean;
14
+ canRefresh: boolean;
15
+ canOpenDetails: boolean;
16
+ }
17
+ declare const WidgetActions: import("svelte").Component<Props, {}, "">;
18
+ type WidgetActions = ReturnType<typeof WidgetActions>;
19
+ export default WidgetActions;