@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,238 @@
1
+ /**
2
+ * The scene model of the DOM canvas.
3
+ *
4
+ * The board is a flat, z-ordered list of plain records. A record is immutable: an edit replaces it with a
5
+ * copy, so a history snapshot is only the array that held the records, and an unchanged object keeps its
6
+ * identity (which is what lets the renderer skip it).
7
+ *
8
+ * Geometry rules, shared by every shape:
9
+ * - `x`, `y`, `width`, `height` are the *geometry box* in world units (CSS pixels at zoom 1, y down):
10
+ * the outline of the shape without its stroke. `x` and `y` are the top-left corner of the box
11
+ * *before* rotation.
12
+ * - `rotation` is in degrees, clockwise, about the centre of the box.
13
+ * - A stroke is centred on the outline and is never part of the box, so a selection box hugs the shape.
14
+ * - Children of a group keep their own world-space box. A group has no transform of its own: moving or
15
+ * rotating a group changes its members.
16
+ *
17
+ * Nothing here knows about the DOM, Svelte or the host, so the same records run in Node tests.
18
+ */
19
+ /** Opaque, stable id of one object on the board. */
20
+ export type ObjectId = string;
21
+ export interface Point {
22
+ x: number;
23
+ y: number;
24
+ }
25
+ export interface Size {
26
+ width: number;
27
+ height: number;
28
+ }
29
+ /** An axis-aligned rectangle (x, y is the top-left corner). */
30
+ export interface Rect {
31
+ x: number;
32
+ y: number;
33
+ width: number;
34
+ height: number;
35
+ }
36
+ /** A 2D affine matrix in the order of CSS `matrix(a, b, c, d, e, f)`. */
37
+ export type Matrix = readonly [a: number, b: number, c: number, d: number, e: number, f: number];
38
+ export type StrokeStyle = 'solid' | 'dashed' | 'dotted';
39
+ export type LineCap = 'butt' | 'round' | 'square';
40
+ export type LineJoin = 'miter' | 'round' | 'bevel';
41
+ export type TextAlign = 'left' | 'center' | 'right';
42
+ export type FontStyle = 'normal' | 'italic';
43
+ /** What a vector shape is painted with. A colour of `transparent` means "none". */
44
+ export interface PaintProps {
45
+ fill: string;
46
+ /** 0..1, applied to the fill only (the object's own `opacity` applies to everything). */
47
+ fillOpacity: number;
48
+ stroke: string;
49
+ strokeWidth: number;
50
+ strokeStyle: StrokeStyle;
51
+ }
52
+ /** The box every drawable object has. */
53
+ export interface BoxProps {
54
+ x: number;
55
+ y: number;
56
+ width: number;
57
+ height: number;
58
+ /** Degrees, clockwise, about the box centre. */
59
+ rotation: number;
60
+ /** 0..1 */
61
+ opacity: number;
62
+ }
63
+ interface ObjectBase {
64
+ id: ObjectId;
65
+ /** The group that owns this object, or null when it sits at the top level of the board. */
66
+ parentId: ObjectId | null;
67
+ }
68
+ export interface RectObject extends ObjectBase, BoxProps, PaintProps {
69
+ type: 'rect';
70
+ /** 0..100: 100 is a pill (the radius is half of the shorter side). */
71
+ roundness: number;
72
+ }
73
+ export interface EllipseObject extends ObjectBase, BoxProps, PaintProps {
74
+ type: 'ellipse';
75
+ /** A true circle: dragging a handle keeps the width equal to the height. */
76
+ aspectLocked: boolean;
77
+ }
78
+ /** Where the pressure of a brush stroke came from: a pen's own, or the speed of a mouse or a finger (a hand that dwells presses). */
79
+ export type BrushPressure = 'pen' | 'speed';
80
+ /** One point of the line a brush stroke follows: x and y in the object's own coordinates (as `d`), and the pressure there, 0 to 1. */
81
+ export type InkPoint = readonly [x: number, y: number, pressure: number];
82
+ /**
83
+ * The line a brush stroke was drawn along, with what the brush did on it. The outline in `d` is made from this, so a stroke
84
+ * that is resized or given another width is drawn again at the width asked for instead of being stretched.
85
+ */
86
+ export interface BrushInk {
87
+ readonly points: readonly InkPoint[];
88
+ /** Tells the nib: a pen's is a little broader and follows its pressure closely, the other follows the speed. */
89
+ readonly pressure: BrushPressure;
90
+ }
91
+ /**
92
+ * What an arrow is, apart from how it is drawn: where it starts, where its head is, and how far its middle is pushed to the
93
+ * side. The two points are in the object's own coordinates (as `d`, from the top left corner of the box); `bend` is the
94
+ * distance, in world units, from the middle of the straight line between them to the middle of the arrow, measured to the
95
+ * right of the way it points (on the screen: a line pointing right with a positive bend curves downward). A bend smaller than
96
+ * `MIN_ARROW_BEND` is zero: the arrow is straight. The head and the shaft are made from this by `core/arrow.ts`, at every change.
97
+ */
98
+ export interface ArrowSpec {
99
+ readonly start: Point;
100
+ readonly end: Point;
101
+ readonly bend: number;
102
+ }
103
+ /**
104
+ * Anything drawn as an SVG path: freehand ink, arrows, triangles and the shapes of the library.
105
+ *
106
+ * `d` is written in the *local* coordinates of the box (0, 0 is the top-left corner of the unrotated box,
107
+ * and the path spans exactly 0..width by 0..height), so it renders as it is and a stroke keeps its width
108
+ * at any size. A resize rewrites `d`.
109
+ *
110
+ * The one exception is a brush stroke (`ink` is set): the box is the one of the line it follows, and `d` is
111
+ * the filled outline of that line, which reaches past the box by half the width of the brush (as the stroke of
112
+ * any other shape does). Its colour is the `fill`, its `stroke` is `transparent`, and `strokeWidth` is the width
113
+ * the brush was asked for.
114
+ */
115
+ export interface PathObject extends ObjectBase, BoxProps, PaintProps {
116
+ type: 'path';
117
+ d: string;
118
+ /**
119
+ * What made the path: `null` for freehand ink, `triangle` and `arrow` for those tools, otherwise the key
120
+ * of a shape of the library. Shapes that can be regenerated (triangle, rounded library shapes) are
121
+ * rebuilt from this key at every resize; the others are scaled.
122
+ */
123
+ shape: string | null;
124
+ /** 0..100, only meaningful for shapes that can round their corners. */
125
+ roundness: number;
126
+ /** Resizing keeps the aspect ratio (symbols and icons of the library). */
127
+ aspectLocked: boolean;
128
+ /**
129
+ * The shape was mirrored by dragging a handle past the opposite edge. `d` already holds the mirror; these
130
+ * are only remembered so that a shape that is rebuilt from its key (a triangle, a rounded library shape)
131
+ * is mirrored again instead of standing up straight.
132
+ */
133
+ flipX: boolean;
134
+ flipY: boolean;
135
+ lineCap: LineCap;
136
+ lineJoin: LineJoin;
137
+ /** Set for a brush stroke (the Draw tool): the line it follows. `null` for every other path. */
138
+ ink: BrushInk | null;
139
+ /**
140
+ * Set for an arrow made by the Arrow tool: the points and the bend it is drawn from. `d` and the box are made from it, so the
141
+ * head keeps its size when the arrow is stretched and the arrow keeps its look at any stroke width. An arrow is never turned
142
+ * (its `rotation` stays 0): turning a selection moves its points. `null` for every other path.
143
+ */
144
+ arrow: ArrowSpec | null;
145
+ }
146
+ export interface TextObject extends ObjectBase, BoxProps {
147
+ type: 'text';
148
+ text: string;
149
+ /** The colour of the letters. */
150
+ color: string;
151
+ fontSize: number;
152
+ fontFamily: string;
153
+ fontWeight: 'normal' | 'bold';
154
+ fontStyle: FontStyle;
155
+ underline: boolean;
156
+ align: TextAlign;
157
+ lineHeight: number;
158
+ /** Space between the box edge and the letters, in world units. */
159
+ padding: number;
160
+ /** `transparent` for no background. */
161
+ backgroundColor: string;
162
+ /**
163
+ * true: the width follows the longest line (a label typed with the Text tool by a click).
164
+ * false: the width is fixed and long lines wrap (a text box dragged out, or resized by hand).
165
+ */
166
+ autoWidth: boolean;
167
+ }
168
+ export interface ImageObject extends ObjectBase, BoxProps {
169
+ type: 'image';
170
+ /** A `data:` or `http(s)` address, or null while the image is only a placeholder. */
171
+ src: string | null;
172
+ /** The reference under which the host's image store keeps the bytes, when they are not inline. */
173
+ imageRef: string | null;
174
+ fileName: string | null;
175
+ /** The size of the picture itself, to keep its aspect ratio when it is resized. */
176
+ naturalWidth: number;
177
+ naturalHeight: number;
178
+ }
179
+ /**
180
+ * A widget card on the board. The host draws the live widget over it; the canvas only owns the rectangle,
181
+ * the screenshot it falls back on, and the selection around it.
182
+ */
183
+ export interface WidgetObject extends ObjectBase, BoxProps {
184
+ type: 'widget';
185
+ widgetId: string;
186
+ widgetName: string | null;
187
+ /** The widget the board is about (the host draws a mark around it in the advanced mode). */
188
+ isPrimary: boolean;
189
+ /** The last screenshot of the widget: inline bytes or an address. */
190
+ thumbnail: string | null;
191
+ /** The reference under which the host's image store keeps the screenshot. */
192
+ thumbnailRef: string | null;
193
+ }
194
+ /** A group has no box of its own: it is the union of its members. */
195
+ export interface GroupObject extends ObjectBase {
196
+ type: 'group';
197
+ }
198
+ export type ShapeObject = RectObject | EllipseObject | PathObject | TextObject | ImageObject | WidgetObject;
199
+ export type SceneObject = ShapeObject | GroupObject;
200
+ export type ObjectType = SceneObject['type'];
201
+ /** Every object type that has a box (everything but a group). */
202
+ export type BoxedObject = ShapeObject;
203
+ /** What a change to one object may carry: any field but its identity. */
204
+ export type ObjectPatch<T extends SceneObject = SceneObject> = Partial<Omit<T, 'id' | 'type'>>;
205
+ /** The view of the board: where the world is on the screen. */
206
+ export interface Viewport {
207
+ /** Screen position of the world origin, in CSS pixels, from the top-left corner of the board. */
208
+ x: number;
209
+ y: number;
210
+ /** 1 is 100%. */
211
+ zoom: number;
212
+ }
213
+ export interface CanvasDocumentBody {
214
+ objects: SceneObject[];
215
+ }
216
+ /**
217
+ * What `saveCanvas()` writes and `loadCanvas()` reads.
218
+ *
219
+ * `canvas.objects` and the two widget keys (`_widgetId`, `_isWidgetImage`) are read by the widget creator
220
+ * straight from the saved text, before any canvas exists, so their names are part of the format
221
+ * (see `hostReadableKeys.ts`).
222
+ */
223
+ export interface CanvasDocument {
224
+ version: typeof CANVAS_DOCUMENT_VERSION;
225
+ savedAt: string;
226
+ viewport: Viewport;
227
+ canvas: CanvasDocumentBody;
228
+ }
229
+ /** The version of the saved format. A file of any other version (the previous canvas wrote version 1, a string, `1.0`) is not read: there is no converter. */
230
+ export declare const CANVAS_DOCUMENT_VERSION: 2;
231
+ export declare const MIN_OBJECT_SIZE = 1;
232
+ export declare const MIN_ZOOM = 0.05;
233
+ export declare const MAX_ZOOM = 8;
234
+ /** True for the objects that carry a box (so not for a group). */
235
+ export declare function isBoxedObject(object: SceneObject): object is BoxedObject;
236
+ /** True for the objects that carry paint (fill and stroke). */
237
+ export declare function isPaintedObject(object: SceneObject): object is RectObject | EllipseObject | PathObject;
238
+ export {};
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The scene model of the DOM canvas.
3
+ *
4
+ * The board is a flat, z-ordered list of plain records. A record is immutable: an edit replaces it with a
5
+ * copy, so a history snapshot is only the array that held the records, and an unchanged object keeps its
6
+ * identity (which is what lets the renderer skip it).
7
+ *
8
+ * Geometry rules, shared by every shape:
9
+ * - `x`, `y`, `width`, `height` are the *geometry box* in world units (CSS pixels at zoom 1, y down):
10
+ * the outline of the shape without its stroke. `x` and `y` are the top-left corner of the box
11
+ * *before* rotation.
12
+ * - `rotation` is in degrees, clockwise, about the centre of the box.
13
+ * - A stroke is centred on the outline and is never part of the box, so a selection box hugs the shape.
14
+ * - Children of a group keep their own world-space box. A group has no transform of its own: moving or
15
+ * rotating a group changes its members.
16
+ *
17
+ * Nothing here knows about the DOM, Svelte or the host, so the same records run in Node tests.
18
+ */
19
+ /** The version of the saved format. A file of any other version (the previous canvas wrote version 1, a string, `1.0`) is not read: there is no converter. */
20
+ export const CANVAS_DOCUMENT_VERSION = 2;
21
+ export const MIN_OBJECT_SIZE = 1;
22
+ export const MIN_ZOOM = 0.05;
23
+ export const MAX_ZOOM = 8;
24
+ /** True for the objects that carry a box (so not for a group). */
25
+ export function isBoxedObject(object) {
26
+ return object.type !== 'group';
27
+ }
28
+ /** True for the objects that carry paint (fill and stroke). */
29
+ export function isPaintedObject(object) {
30
+ return object.type === 'rect' || object.type === 'ellipse' || object.type === 'path';
31
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Where the world is on the screen: pan, zoom and the conversions between a point of the board element and
3
+ * a point of the world.
4
+ *
5
+ * `viewport.x` and `viewport.y` are where the world origin lands on the board element, in CSS pixels, and
6
+ * `viewport.zoom` is the scale (1 is 100%). The world layer is drawn with `translate(x, y) scale(zoom)`.
7
+ */
8
+ import type { Point, Rect, Size, Viewport } from './types.js';
9
+ export declare const INITIAL_VIEWPORT: Viewport;
10
+ /** The zoom levels the zoom buttons and the menu step through. */
11
+ export declare const ZOOM_STEPS: readonly number[];
12
+ export declare function clampZoom(zoom: number): number;
13
+ /** A point of the board element (CSS pixels from its top-left corner) in world units. */
14
+ export declare function screenToWorld(viewport: Viewport, screenPoint: Point): Point;
15
+ /** A point of the world as a point of the board element. */
16
+ export declare function worldToScreen(viewport: Viewport, worldPoint: Point): Point;
17
+ /** The rectangle of the world that is on screen. */
18
+ export declare function visibleWorldRect(viewport: Viewport, viewSize: Size): Rect;
19
+ /** Changes the zoom and keeps the world point under `screenPoint` where it is (what the wheel and pinch do). */
20
+ export declare function zoomAtPoint(viewport: Viewport, screenPoint: Point, nextZoom: number): Viewport;
21
+ export declare function panViewport(viewport: Viewport, deltaX: number, deltaY: number): Viewport;
22
+ /** The next step above (direction 1) or below (-1) the current zoom, or the same zoom at the end of the ladder. */
23
+ export declare function nextZoomStep(zoom: number, direction: 1 | -1): number;
24
+ /**
25
+ * The view that shows a rectangle of the world, centred, with room around it.
26
+ * Never zooms in past `maxZoom` (a single small shape is not blown up to fill the board).
27
+ */
28
+ export declare function fitViewportToRect(rect: Rect, viewSize: Size, padding?: number, maxZoom?: number): Viewport;
29
+ /** The CSS transform of the world layer. */
30
+ export declare function viewportToCssTransform(viewport: Viewport): string;
31
+ /** A viewport read from untrusted data: anything that is not a finite number falls back to the default. */
32
+ export declare function sanitizeViewport(value: unknown): Viewport;
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Where the world is on the screen: pan, zoom and the conversions between a point of the board element and
3
+ * a point of the world.
4
+ *
5
+ * `viewport.x` and `viewport.y` are where the world origin lands on the board element, in CSS pixels, and
6
+ * `viewport.zoom` is the scale (1 is 100%). The world layer is drawn with `translate(x, y) scale(zoom)`.
7
+ */
8
+ import { clampNumber } from './geometry.js';
9
+ import { MAX_ZOOM, MIN_ZOOM } from './types.js';
10
+ export const INITIAL_VIEWPORT = { x: 0, y: 0, zoom: 1 };
11
+ /** The zoom levels the zoom buttons and the menu step through. */
12
+ export const ZOOM_STEPS = [0.1, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 3, 4, 6, 8];
13
+ export function clampZoom(zoom) {
14
+ if (!Number.isFinite(zoom))
15
+ return 1;
16
+ return clampNumber(zoom, MIN_ZOOM, MAX_ZOOM);
17
+ }
18
+ /** A point of the board element (CSS pixels from its top-left corner) in world units. */
19
+ export function screenToWorld(viewport, screenPoint) {
20
+ return { x: (screenPoint.x - viewport.x) / viewport.zoom, y: (screenPoint.y - viewport.y) / viewport.zoom };
21
+ }
22
+ /** A point of the world as a point of the board element. */
23
+ export function worldToScreen(viewport, worldPoint) {
24
+ return { x: worldPoint.x * viewport.zoom + viewport.x, y: worldPoint.y * viewport.zoom + viewport.y };
25
+ }
26
+ /** The rectangle of the world that is on screen. */
27
+ export function visibleWorldRect(viewport, viewSize) {
28
+ return {
29
+ x: -viewport.x / viewport.zoom,
30
+ y: -viewport.y / viewport.zoom,
31
+ width: viewSize.width / viewport.zoom,
32
+ height: viewSize.height / viewport.zoom
33
+ };
34
+ }
35
+ /** Changes the zoom and keeps the world point under `screenPoint` where it is (what the wheel and pinch do). */
36
+ export function zoomAtPoint(viewport, screenPoint, nextZoom) {
37
+ const zoom = clampZoom(nextZoom);
38
+ const worldPoint = screenToWorld(viewport, screenPoint);
39
+ return { x: screenPoint.x - worldPoint.x * zoom, y: screenPoint.y - worldPoint.y * zoom, zoom };
40
+ }
41
+ export function panViewport(viewport, deltaX, deltaY) {
42
+ return { ...viewport, x: viewport.x + deltaX, y: viewport.y + deltaY };
43
+ }
44
+ /** The next step above (direction 1) or below (-1) the current zoom, or the same zoom at the end of the ladder. */
45
+ export function nextZoomStep(zoom, direction) {
46
+ const tolerance = 1e-6;
47
+ if (direction === 1) {
48
+ const step = ZOOM_STEPS.find((candidate) => candidate > zoom + tolerance);
49
+ return step ?? ZOOM_STEPS[ZOOM_STEPS.length - 1];
50
+ }
51
+ for (let index = ZOOM_STEPS.length - 1; index >= 0; index -= 1) {
52
+ if (ZOOM_STEPS[index] < zoom - tolerance)
53
+ return ZOOM_STEPS[index];
54
+ }
55
+ return ZOOM_STEPS[0];
56
+ }
57
+ /**
58
+ * The view that shows a rectangle of the world, centred, with room around it.
59
+ * Never zooms in past `maxZoom` (a single small shape is not blown up to fill the board).
60
+ */
61
+ export function fitViewportToRect(rect, viewSize, padding = 48, maxZoom = 1) {
62
+ if (viewSize.width <= 0 || viewSize.height <= 0)
63
+ return INITIAL_VIEWPORT;
64
+ const availableWidth = Math.max(1, viewSize.width - padding * 2);
65
+ const availableHeight = Math.max(1, viewSize.height - padding * 2);
66
+ const zoom = clampZoom(Math.min(maxZoom, rect.width > 0 ? availableWidth / rect.width : maxZoom, rect.height > 0 ? availableHeight / rect.height : maxZoom));
67
+ const centerX = rect.x + rect.width / 2;
68
+ const centerY = rect.y + rect.height / 2;
69
+ return { x: viewSize.width / 2 - centerX * zoom, y: viewSize.height / 2 - centerY * zoom, zoom };
70
+ }
71
+ /** The CSS transform of the world layer. */
72
+ export function viewportToCssTransform(viewport) {
73
+ return `translate(${Number(viewport.x.toFixed(3))}px, ${Number(viewport.y.toFixed(3))}px) scale(${Number(viewport.zoom.toFixed(5))})`;
74
+ }
75
+ /** A viewport read from untrusted data: anything that is not a finite number falls back to the default. */
76
+ export function sanitizeViewport(value) {
77
+ if (!value || typeof value !== 'object')
78
+ return INITIAL_VIEWPORT;
79
+ const candidate = value;
80
+ const x = typeof candidate.x === 'number' && Number.isFinite(candidate.x) ? candidate.x : 0;
81
+ const y = typeof candidate.y === 'number' && Number.isFinite(candidate.y) ? candidate.y : 0;
82
+ const zoom = typeof candidate.zoom === 'number' ? clampZoom(candidate.zoom) : 1;
83
+ return { x, y, zoom };
84
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Turns pointer samples into what the board does.
3
+ *
4
+ * The shell hands over presses, moves and releases as plain records (`PointerInput`); the controller decides
5
+ * which gesture a press starts (by the active tool, the key held, what is under the pointer) and feeds it until
6
+ * the release. Two fingers make a pinch, the wheel pans and zooms, and a double click enters a group or edits a
7
+ * text. Nothing here touches the DOM, so every rule is tested with simulated pointers.
8
+ */
9
+ import type { Point } from '../core/types.js';
10
+ import type { EngineCore } from '../EngineCore.js';
11
+ import type { Modifiers, PointerControllerOptions, PointerInput, WheelInput } from './types.js';
12
+ /** Two presses this close in time and place are a double click. */
13
+ export declare const DOUBLE_CLICK_MS = 400;
14
+ export declare const DOUBLE_CLICK_DISTANCE = 6;
15
+ export declare class PointerController {
16
+ #private;
17
+ constructor(engine: EngineCore, options?: PointerControllerOptions);
18
+ /** The kind of gesture that is running, or null. */
19
+ get gestureKind(): string | null;
20
+ /** Space held: a press pans, whatever the tool. */
21
+ setSpaceHeld(held: boolean): void;
22
+ /** Abandons the gesture that is running (Escape). True when there was one. */
23
+ cancelGesture(): boolean;
24
+ /** Shift, Alt, Ctrl or Meta changed while the pointer stayed still. */
25
+ modifiersChanged(modifiers: Modifiers): void;
26
+ dispose(): void;
27
+ /** Handles a press. Returns whether the shell should capture the pointer and keep the browser from acting on it. */
28
+ pointerDown(input: PointerInput): boolean;
29
+ /** A right click or a long press: picks the object under the pointer if it is not selected. Returns it. */
30
+ prepareContextMenu(screenPoint: Point): string | null;
31
+ pointerMove(input: PointerInput): void;
32
+ /** The pointer left the board: nothing is hovered any more. */
33
+ pointerLeave(): void;
34
+ pointerUp(input: PointerInput): void;
35
+ pointerCancel(input: Pick<PointerInput, 'pointerId'>): void;
36
+ /** The wheel (or two fingers on a trackpad) pans; with Ctrl or Meta, which a trackpad pinch also reports, it zooms about the pointer. */
37
+ wheel(input: WheelInput): void;
38
+ }