@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,450 @@
1
+ /**
2
+ * Operations on a scene: the z-ordered list of records, with groups as parents of their members.
3
+ *
4
+ * Every function is pure. A function that changes the scene returns a new array and keeps the identity of
5
+ * every record it did not touch (and returns the very same array when nothing changed), so a store can
6
+ * compare by reference and a renderer can skip what did not move.
7
+ *
8
+ * Structure (reorder, group, ungroup, remove) is done on a tree built from the list and flattened back,
9
+ * which keeps one rule true everywhere: a group comes first, directly followed by its members, and the
10
+ * paint order of the board is the order of the list.
11
+ */
12
+ import { brushOverflow, isBrushStroke } from './brush.js';
13
+ import { rotatedBoxBounds, unionRects, inflateRect } from './geometry.js';
14
+ import { createGroupObject, createObjectId } from './defaults.js';
15
+ import { isBoxedObject, isPaintedObject } from './types.js';
16
+ // ──────────────────────────────────────────────────────────── lookup
17
+ export function indexScene(scene) {
18
+ const index = new Map();
19
+ for (const object of scene)
20
+ index.set(object.id, object);
21
+ return index;
22
+ }
23
+ export function findObject(scene, id) {
24
+ for (const object of scene) {
25
+ if (object.id === id)
26
+ return object;
27
+ }
28
+ return undefined;
29
+ }
30
+ /** The direct members of a group, in paint order. */
31
+ export function childrenOf(scene, groupId) {
32
+ return scene.filter((object) => object.parentId === groupId);
33
+ }
34
+ /** Every object below a group, at any depth, in list order. */
35
+ export function descendantsOf(scene, groupId) {
36
+ const included = new Set([groupId]);
37
+ const result = [];
38
+ // A group precedes its members in the list, so one pass finds them all.
39
+ for (const object of scene) {
40
+ if (object.parentId !== null && included.has(object.parentId)) {
41
+ included.add(object.id);
42
+ result.push(object);
43
+ }
44
+ }
45
+ return result;
46
+ }
47
+ /** The ids given, plus every id below a group among them. */
48
+ export function withDescendantIds(scene, ids) {
49
+ const wanted = new Set(ids);
50
+ const included = new Set(wanted);
51
+ for (const object of scene) {
52
+ if (object.parentId !== null && included.has(object.parentId))
53
+ included.add(object.id);
54
+ }
55
+ return included;
56
+ }
57
+ /** The shapes drawn by the given objects: a shape is itself, a group is its members at any depth. Unique, in paint order. */
58
+ export function leafShapesOf(scene, ids) {
59
+ const wanted = withDescendantIds(scene, ids);
60
+ const shapes = [];
61
+ for (const object of scene) {
62
+ if (wanted.has(object.id) && isBoxedObject(object))
63
+ shapes.push(object);
64
+ }
65
+ return shapes;
66
+ }
67
+ /** The group nearest to the board that contains the object, or the object itself when it is not in a group. */
68
+ export function topLevelAncestorId(index, id) {
69
+ let current = index.get(id);
70
+ // Most objects sit on the board itself, with nothing to climb and nothing to keep track of (a marquee asks for all of them
71
+ // at every step of its drag).
72
+ if (!current || current.parentId === null)
73
+ return current?.id ?? id;
74
+ const visited = new Set();
75
+ while (current && current.parentId !== null && !visited.has(current.id)) {
76
+ visited.add(current.id);
77
+ const parent = index.get(current.parentId);
78
+ if (!parent)
79
+ break;
80
+ current = parent;
81
+ }
82
+ return current?.id ?? id;
83
+ }
84
+ /** True when `id` is `ancestorId` or lies below it. */
85
+ export function isWithin(index, id, ancestorId) {
86
+ let current = index.get(id);
87
+ const visited = new Set();
88
+ while (current && !visited.has(current.id)) {
89
+ if (current.id === ancestorId)
90
+ return true;
91
+ visited.add(current.id);
92
+ current = current.parentId === null ? undefined : index.get(current.parentId);
93
+ }
94
+ return false;
95
+ }
96
+ /** Whether two lists hold the same ids in the same order (the very same list is the quick case). */
97
+ export function sameIds(first, second) {
98
+ if (first === second)
99
+ return true;
100
+ if (first.length !== second.length)
101
+ return false;
102
+ for (let position = 0; position < first.length; position += 1) {
103
+ if (first[position] !== second[position])
104
+ return false;
105
+ }
106
+ return true;
107
+ }
108
+ /** Drops ids that are not on the board, repeats, and members whose group is selected too. */
109
+ export function normalizeSelection(scene, ids) {
110
+ const index = indexScene(scene);
111
+ const wanted = new Set();
112
+ for (const id of ids) {
113
+ if (index.has(id))
114
+ wanted.add(id);
115
+ }
116
+ const result = [];
117
+ for (const object of scene) {
118
+ if (!wanted.has(object.id))
119
+ continue;
120
+ let covered = false;
121
+ let parentId = object.parentId;
122
+ const visited = new Set();
123
+ while (parentId !== null && !visited.has(parentId)) {
124
+ if (wanted.has(parentId)) {
125
+ covered = true;
126
+ break;
127
+ }
128
+ visited.add(parentId);
129
+ parentId = index.get(parentId)?.parentId ?? null;
130
+ }
131
+ if (!covered)
132
+ result.push(object.id);
133
+ }
134
+ return result;
135
+ }
136
+ // ──────────────────────────────────────────────────────────── bounds
137
+ /** The rectangle around a shape on the board (turned boxes measured by their corners), without its stroke. */
138
+ export function shapeBounds(shape) {
139
+ return rotatedBoxBounds(shape);
140
+ }
141
+ /** The same, grown by what the stroke adds outside the outline: what a thumbnail or a screenshot must include. */
142
+ export function shapeVisualBounds(shape) {
143
+ const bounds = rotatedBoxBounds(shape);
144
+ // A brush stroke's outline reaches past the box of its line by up to half the width of the brush at its fullest.
145
+ if (isBrushStroke(shape))
146
+ return inflateRect(bounds, brushOverflow(shape));
147
+ if (!isPaintedObject(shape) || shape.stroke === 'transparent' || shape.strokeWidth <= 0)
148
+ return bounds;
149
+ // A mitred corner can reach well past half the stroke width; the square root of two covers a right angle.
150
+ return inflateRect(bounds, (shape.strokeWidth / 2) * Math.SQRT2);
151
+ }
152
+ /** The rectangle around the given objects, groups included (a group is as big as its members together). */
153
+ export function boundsOfObjects(scene, ids) {
154
+ return unionRects(leafShapesOf(scene, ids).map(shapeBounds));
155
+ }
156
+ /** The rectangle around every shape on the board, strokes included. Null for an empty board. */
157
+ export function boardVisualBounds(scene) {
158
+ const rectangles = [];
159
+ for (const object of scene) {
160
+ if (isBoxedObject(object))
161
+ rectangles.push(shapeVisualBounds(object));
162
+ }
163
+ return unionRects(rectangles);
164
+ }
165
+ // ──────────────────────────────────────────────────────────── tree
166
+ export function buildSceneTree(scene) {
167
+ const nodes = new Map();
168
+ const roots = [];
169
+ for (const object of scene) {
170
+ // A repeated id is a corrupt file; the first record wins.
171
+ if (nodes.has(object.id))
172
+ continue;
173
+ const node = { object, children: [] };
174
+ nodes.set(object.id, node);
175
+ const parent = object.parentId !== null ? nodes.get(object.parentId) : undefined;
176
+ // A group is always before its members. An object whose group is missing (or comes later) goes to the top.
177
+ if (parent && parent.object.type === 'group')
178
+ parent.children.push(node);
179
+ else
180
+ roots.push(node);
181
+ }
182
+ return roots;
183
+ }
184
+ /** Writes a tree back as a list, setting every parent id to the node it hangs from. */
185
+ export function flattenSceneTree(roots) {
186
+ const result = [];
187
+ const visit = (node, parentId) => {
188
+ const object = node.object.parentId === parentId ? node.object : { ...node.object, parentId };
189
+ result.push(object);
190
+ for (const child of node.children)
191
+ visit(child, object.id);
192
+ };
193
+ for (const root of roots)
194
+ visit(root, null);
195
+ return result;
196
+ }
197
+ /** Repairs a list of records into a valid scene: unique ids, groups before their members, no empty groups. */
198
+ export function normalizeScene(scene) {
199
+ const roots = buildSceneTree(scene);
200
+ const withoutEmptyGroups = (nodes) => {
201
+ const kept = [];
202
+ for (const node of nodes) {
203
+ node.children = withoutEmptyGroups(node.children);
204
+ if (node.object.type === 'group' && node.children.length === 0)
205
+ continue;
206
+ kept.push(node);
207
+ }
208
+ return kept;
209
+ };
210
+ return flattenSceneTree(withoutEmptyGroups(roots));
211
+ }
212
+ function findNode(roots, id) {
213
+ for (const root of roots) {
214
+ if (root.object.id === id)
215
+ return root;
216
+ const found = findNode(root.children, id);
217
+ if (found)
218
+ return found;
219
+ }
220
+ return null;
221
+ }
222
+ /** The list of siblings that holds a node, and the node's place in it. */
223
+ function findSiblingList(roots, id) {
224
+ const search = (siblings, parent) => {
225
+ for (let position = 0; position < siblings.length; position += 1) {
226
+ if (siblings[position].object.id === id)
227
+ return { siblings, position, parent };
228
+ const inside = search(siblings[position].children, siblings[position]);
229
+ if (inside)
230
+ return inside;
231
+ }
232
+ return null;
233
+ };
234
+ return search(roots, null);
235
+ }
236
+ // ──────────────────────────────────────────────────────────── edits
237
+ export function addObjects(scene, objects) {
238
+ return objects.length === 0 ? [...scene] : [...scene, ...objects];
239
+ }
240
+ /** Removes the objects and everything below them, then any group left with no members. */
241
+ export function removeObjects(scene, ids) {
242
+ const doomed = withDescendantIds(scene, ids);
243
+ if (doomed.size === 0)
244
+ return scene;
245
+ const kept = scene.filter((object) => !doomed.has(object.id));
246
+ if (kept.length === scene.length)
247
+ return scene;
248
+ return normalizeScene(kept);
249
+ }
250
+ /** Changes one object. Returns the same scene when the object is missing or the patch changes nothing. */
251
+ export function patchObject(scene, id, patch) {
252
+ return patchObjects(scene, new Map([[id, patch]]));
253
+ }
254
+ /** Changes several objects at once, each with its own patch. */
255
+ export function patchObjects(scene, patches) {
256
+ let changed = false;
257
+ const next = scene.map((object) => {
258
+ const patch = patches.get(object.id);
259
+ if (!patch)
260
+ return object;
261
+ let differs = false;
262
+ for (const key of Object.keys(patch)) {
263
+ if (object[key] !== patch[key]) {
264
+ differs = true;
265
+ break;
266
+ }
267
+ }
268
+ if (!differs)
269
+ return object;
270
+ changed = true;
271
+ return { ...object, ...patch };
272
+ });
273
+ return changed ? next : scene;
274
+ }
275
+ /** Replaces one record by another (its id stays the one of the record it replaces). */
276
+ export function replaceObject(scene, id, replacement) {
277
+ let changed = false;
278
+ const next = scene.map((object) => {
279
+ if (object.id !== id)
280
+ return object;
281
+ changed = true;
282
+ return { ...replacement, id, parentId: object.parentId };
283
+ });
284
+ return changed ? next : scene;
285
+ }
286
+ /** The ids of the shapes drawn by the given objects (a group is its members at any depth): who moves when they are carried. */
287
+ export function movingShapeIds(scene, ids) {
288
+ return new Set(leafShapesOf(scene, ids).map((shape) => shape.id));
289
+ }
290
+ /**
291
+ * Moves the shapes with the given ids by a distance. A drag carries the same shapes at every step: it asks `movingShapeIds`
292
+ * once and calls this at every move, so no step walks the groups of the board again to find out who is carried.
293
+ */
294
+ export function translateShapes(scene, moving, deltaX, deltaY) {
295
+ if ((deltaX === 0 && deltaY === 0) || moving.size === 0)
296
+ return scene;
297
+ return scene.map((object) => moving.has(object.id) && isBoxedObject(object)
298
+ ? { ...object, x: object.x + deltaX, y: object.y + deltaY }
299
+ : object);
300
+ }
301
+ /** Moves the shapes drawn by the given objects by a distance. */
302
+ export function translateObjects(scene, ids, deltaX, deltaY) {
303
+ if (deltaX === 0 && deltaY === 0)
304
+ return scene;
305
+ return translateShapes(scene, movingShapeIds(scene, ids), deltaX, deltaY);
306
+ }
307
+ /**
308
+ * Changes the paint order of the objects, each among its own siblings (a member of a group moves inside the
309
+ * group, never out of it).
310
+ */
311
+ export function reorderObjects(scene, ids, mode) {
312
+ const moved = new Set(normalizeSelection(scene, ids));
313
+ if (moved.size === 0)
314
+ return scene;
315
+ const roots = buildSceneTree(scene);
316
+ const lists = new Map();
317
+ for (const id of moved) {
318
+ const found = findSiblingList(roots, id);
319
+ if (!found)
320
+ continue;
321
+ const set = lists.get(found.siblings) ?? new Set();
322
+ set.add(id);
323
+ lists.set(found.siblings, set);
324
+ }
325
+ for (const [siblings, movedIds] of lists) {
326
+ if (mode === 'front' || mode === 'back') {
327
+ const chosen = siblings.filter((node) => movedIds.has(node.object.id));
328
+ const others = siblings.filter((node) => !movedIds.has(node.object.id));
329
+ siblings.splice(0, siblings.length, ...(mode === 'front' ? [...others, ...chosen] : [...chosen, ...others]));
330
+ }
331
+ else if (mode === 'forward') {
332
+ // From the top down, a chosen node swaps with the unchosen one above it.
333
+ for (let position = siblings.length - 2; position >= 0; position -= 1) {
334
+ if (movedIds.has(siblings[position].object.id) && !movedIds.has(siblings[position + 1].object.id)) {
335
+ [siblings[position], siblings[position + 1]] = [siblings[position + 1], siblings[position]];
336
+ }
337
+ }
338
+ }
339
+ else {
340
+ for (let position = 1; position < siblings.length; position += 1) {
341
+ if (movedIds.has(siblings[position].object.id) && !movedIds.has(siblings[position - 1].object.id)) {
342
+ [siblings[position], siblings[position - 1]] = [siblings[position - 1], siblings[position]];
343
+ }
344
+ }
345
+ }
346
+ }
347
+ const next = flattenSceneTree(roots);
348
+ const unchanged = next.length === scene.length && next.every((object, position) => object === scene[position]);
349
+ return unchanged ? scene : next;
350
+ }
351
+ /**
352
+ * Puts the objects in a new group, at the paint position of the highest one.
353
+ * Objects inside a group are grouped inside it when they all share it; otherwise the groups they sit in
354
+ * are grouped whole (a selection never tears a group apart).
355
+ * Returns null when there is nothing to group (fewer than two units).
356
+ */
357
+ export function groupObjects(scene, ids, groupId = createObjectId('group')) {
358
+ const selected = normalizeSelection(scene, ids);
359
+ if (selected.length < 2)
360
+ return null;
361
+ const index = indexScene(scene);
362
+ const sharedParent = selected.every((id) => index.get(id)?.parentId === index.get(selected[0])?.parentId);
363
+ const units = sharedParent
364
+ ? selected
365
+ : Array.from(new Set(selected.map((id) => topLevelAncestorId(index, id))));
366
+ if (units.length < 2)
367
+ return null;
368
+ const roots = buildSceneTree(scene);
369
+ const unitNodes = units.map((id) => findNode(roots, id)).filter((node) => node !== null);
370
+ if (unitNodes.length < 2)
371
+ return null;
372
+ const firstFound = findSiblingList(roots, unitNodes[0].object.id);
373
+ if (!firstFound)
374
+ return null;
375
+ const siblings = firstFound.siblings;
376
+ const unitIds = new Set(unitNodes.map((node) => node.object.id));
377
+ const inOrder = siblings.filter((node) => unitIds.has(node.object.id));
378
+ // The group takes the place of the highest unit among its siblings.
379
+ let insertAt = 0;
380
+ siblings.forEach((node, position) => {
381
+ if (unitIds.has(node.object.id))
382
+ insertAt = position;
383
+ });
384
+ const group = createGroupObject({ id: groupId, parentId: firstFound.parent?.object.id ?? null });
385
+ const groupNode = { object: group, children: inOrder };
386
+ const remaining = siblings.filter((node) => !unitIds.has(node.object.id));
387
+ const removedBefore = siblings.slice(0, insertAt).filter((node) => unitIds.has(node.object.id)).length;
388
+ remaining.splice(insertAt - removedBefore, 0, groupNode);
389
+ siblings.splice(0, siblings.length, ...remaining);
390
+ return { scene: flattenSceneTree(roots), groupId };
391
+ }
392
+ /** Dissolves groups: their members take the group's place in the list and in its parent. */
393
+ export function ungroupObjects(scene, ids) {
394
+ const groupIds = new Set();
395
+ const index = indexScene(scene);
396
+ for (const id of ids) {
397
+ if (index.get(id)?.type === 'group')
398
+ groupIds.add(id);
399
+ }
400
+ if (groupIds.size === 0)
401
+ return { scene, releasedIds: [] };
402
+ const roots = buildSceneTree(scene);
403
+ const releasedIds = [];
404
+ const dissolve = (nodes) => {
405
+ const result = [];
406
+ for (const node of nodes) {
407
+ node.children = dissolve(node.children);
408
+ if (groupIds.has(node.object.id)) {
409
+ for (const child of node.children) {
410
+ releasedIds.push(child.object.id);
411
+ result.push(child);
412
+ }
413
+ }
414
+ else {
415
+ result.push(node);
416
+ }
417
+ }
418
+ return result;
419
+ };
420
+ const dissolved = dissolve(roots);
421
+ return { scene: flattenSceneTree(dissolved), releasedIds };
422
+ }
423
+ /**
424
+ * Copies objects (a group is copied with its members) under new ids, shifted by an offset.
425
+ * The copies are returned in paint order, ready to be added on top of the board.
426
+ */
427
+ export function duplicateObjects(scene, ids, offset = { x: 0, y: 0 }, makeId = (type) => createObjectId(type === 'group' ? 'group' : 'obj')) {
428
+ const sources = withDescendantIds(scene, normalizeSelection(scene, ids));
429
+ const idMap = new Map();
430
+ const objects = [];
431
+ const rootIds = new Set(normalizeSelection(scene, ids));
432
+ for (const object of scene) {
433
+ if (!sources.has(object.id))
434
+ continue;
435
+ const copyId = makeId(object.type);
436
+ idMap.set(object.id, copyId);
437
+ const parentId = rootIds.has(object.id)
438
+ ? object.parentId
439
+ : object.parentId !== null
440
+ ? (idMap.get(object.parentId) ?? null)
441
+ : null;
442
+ const copy = { ...object, id: copyId, parentId };
443
+ if (isBoxedObject(copy) && isBoxedObject(object)) {
444
+ copy.x = object.x + offset.x;
445
+ copy.y = object.y + offset.y;
446
+ }
447
+ objects.push(copy);
448
+ }
449
+ return { objects, idMap };
450
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The smear the eraser leaves behind the pointer while it is dragged.
3
+ *
4
+ * It is a short trail of points that follows the pointer, eats its own tail while the pointer rests, and is eaten away
5
+ * (getting thinner) when the pointer lets go. Which shapes the eraser took is decided elsewhere; this is only the picture
6
+ * of where the pointer has just been, so the person sees the reach of the eraser.
7
+ *
8
+ * The points come from the pointer and the steps from the host's animation clock. Nothing here reads a clock or the page,
9
+ * so the rules of the trail are held to in Node. The rules and the numbers are the ones of tldraw's eraser trail (tldraw
10
+ * is the reference the owner asked for); the code is our own.
11
+ */
12
+ import type { Point } from './types.js';
13
+ /** Non-secret runtime constants of the trail. */
14
+ export declare const SCRIBBLE_CONFIG: {
15
+ /** How wide the head of the trail is, in screen pixels. */
16
+ readonly size: 12;
17
+ /** How opaque the trail is drawn. */
18
+ readonly opacity: 0.8;
19
+ /** The trail grows until it holds more than this many points (nine), and then follows the pointer: the oldest goes as a new one arrives. */
20
+ readonly maxPoints: 8;
21
+ /** The time between two bites of the tail, in milliseconds (one frame of a 60 Hz screen). */
22
+ readonly stepMs: 16;
23
+ /** The share of its width the trail loses at each bite while it is being let go. */
24
+ readonly shrink: 0.1;
25
+ /** The trail never gets thinner than this, in screen pixels. */
26
+ readonly minSize: 1;
27
+ /** A place nearer than this to the last point (screen pixels) is not taken in. */
28
+ readonly minDistance: 1;
29
+ /** How taut the line through the points is drawn (the freehand outline's `streamline`). */
30
+ readonly streamline: 0.32;
31
+ };
32
+ /** How opaque a shape is drawn while the eraser has marked it: it stays behind, faded, until the pointer is released. */
33
+ export declare const ERASING_OPACITY = 0.32;
34
+ /** What the trail is doing: growing from nothing, following the pointer, or being let go. */
35
+ export type ScribblePhase = 'starting' | 'active' | 'stopping';
36
+ /** What a view draws of the trail: a copy that nothing changes afterwards. */
37
+ export interface ScribbleView {
38
+ /** Where the trail has been, oldest first, in world units. */
39
+ readonly points: readonly Point[];
40
+ /** The width of its head in screen pixels (less than `SCRIBBLE_CONFIG.size` while it is let go). */
41
+ readonly size: number;
42
+ /** The pointer let go: the trail is drawn to its end, as a finished stroke is. */
43
+ readonly stopping: boolean;
44
+ }
45
+ export declare class Scribble {
46
+ #private;
47
+ /**
48
+ * The pointer reached a place. The next step takes it in; of several places given between two steps the last one counts.
49
+ * A place less than a pixel (on the screen at `zoom`) from the trail's head adds nothing the eye can see.
50
+ */
51
+ add(point: Point, zoom: number): void;
52
+ /** The pointer let go: from now on the trail is only eaten, from its tail to its head. */
53
+ stop(): void;
54
+ /** True when the trail has been let go and nothing of it is left to show. */
55
+ get finished(): boolean;
56
+ get phase(): ScribblePhase;
57
+ /**
58
+ * One frame of the animation clock. `elapsedMs` is the time since the last frame. Returns whether what the trail looks
59
+ * like changed (a new point, a bitten tail, a thinner head), so the caller draws only then.
60
+ */
61
+ step(elapsedMs: number): boolean;
62
+ /** What to draw now. */
63
+ view(): ScribbleView;
64
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * The smear the eraser leaves behind the pointer while it is dragged.
3
+ *
4
+ * It is a short trail of points that follows the pointer, eats its own tail while the pointer rests, and is eaten away
5
+ * (getting thinner) when the pointer lets go. Which shapes the eraser took is decided elsewhere; this is only the picture
6
+ * of where the pointer has just been, so the person sees the reach of the eraser.
7
+ *
8
+ * The points come from the pointer and the steps from the host's animation clock. Nothing here reads a clock or the page,
9
+ * so the rules of the trail are held to in Node. The rules and the numbers are the ones of tldraw's eraser trail (tldraw
10
+ * is the reference the owner asked for); the code is our own.
11
+ */
12
+ /** Non-secret runtime constants of the trail. */
13
+ export const SCRIBBLE_CONFIG = {
14
+ /** How wide the head of the trail is, in screen pixels. */
15
+ size: 12,
16
+ /** How opaque the trail is drawn. */
17
+ opacity: 0.8,
18
+ /** The trail grows until it holds more than this many points (nine), and then follows the pointer: the oldest goes as a new one arrives. */
19
+ maxPoints: 8,
20
+ /** The time between two bites of the tail, in milliseconds (one frame of a 60 Hz screen). */
21
+ stepMs: 16,
22
+ /** The share of its width the trail loses at each bite while it is being let go. */
23
+ shrink: 0.1,
24
+ /** The trail never gets thinner than this, in screen pixels. */
25
+ minSize: 1,
26
+ /** A place nearer than this to the last point (screen pixels) is not taken in. */
27
+ minDistance: 1,
28
+ /** How taut the line through the points is drawn (the freehand outline's `streamline`). */
29
+ streamline: 0.32
30
+ };
31
+ /** How opaque a shape is drawn while the eraser has marked it: it stays behind, faded, until the pointer is released. */
32
+ export const ERASING_OPACITY = 0.32;
33
+ export class Scribble {
34
+ #points = [];
35
+ #size = SCRIBBLE_CONFIG.size;
36
+ #phase = 'starting';
37
+ // The place the pointer reached that the trail has not taken in yet (the next step takes it).
38
+ #incoming = null;
39
+ // How long it has been since the trail last bit its tail.
40
+ #sinceStepMs = 0;
41
+ /**
42
+ * The pointer reached a place. The next step takes it in; of several places given between two steps the last one counts.
43
+ * A place less than a pixel (on the screen at `zoom`) from the trail's head adds nothing the eye can see.
44
+ */
45
+ add(point, zoom) {
46
+ if (this.#phase === 'stopping')
47
+ return;
48
+ if (!Number.isFinite(point.x) || !Number.isFinite(point.y))
49
+ return;
50
+ const head = this.#points[this.#points.length - 1];
51
+ const safeZoom = Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
52
+ if (!head || Math.hypot(point.x - head.x, point.y - head.y) * safeZoom >= SCRIBBLE_CONFIG.minDistance)
53
+ this.#incoming = point;
54
+ }
55
+ /** The pointer let go: from now on the trail is only eaten, from its tail to its head. */
56
+ stop() {
57
+ this.#phase = 'stopping';
58
+ this.#incoming = null;
59
+ }
60
+ /** True when the trail has been let go and nothing of it is left to show. */
61
+ get finished() {
62
+ return this.#phase === 'stopping' && this.#points.length === 0;
63
+ }
64
+ get phase() {
65
+ return this.#phase;
66
+ }
67
+ /**
68
+ * One frame of the animation clock. `elapsedMs` is the time since the last frame. Returns whether what the trail looks
69
+ * like changed (a new point, a bitten tail, a thinner head), so the caller draws only then.
70
+ */
71
+ step(elapsedMs) {
72
+ if (this.#phase === 'starting') {
73
+ // It only grows at first: nothing is bitten until it holds more than `maxPoints`.
74
+ const grew = this.#takeIncoming();
75
+ if (this.#points.length > SCRIBBLE_CONFIG.maxPoints)
76
+ this.#phase = 'active';
77
+ return grew;
78
+ }
79
+ this.#sinceStepMs += Number.isFinite(elapsedMs) && elapsedMs > 0 ? elapsedMs : 0;
80
+ const atStep = this.#sinceStepMs >= SCRIBBLE_CONFIG.stepMs;
81
+ if (atStep)
82
+ this.#sinceStepMs = 0;
83
+ if (this.#phase === 'active') {
84
+ if (this.#takeIncoming()) {
85
+ // Following the pointer: a new point at the head, the oldest let go.
86
+ if (this.#points.length > SCRIBBLE_CONFIG.maxPoints)
87
+ this.#points.shift();
88
+ return true;
89
+ }
90
+ // The pointer rests: the tail catches up with the head.
91
+ if (atStep && this.#points.length > 1) {
92
+ this.#points.shift();
93
+ return true;
94
+ }
95
+ return false;
96
+ }
97
+ // Let go: every bite takes a point from the tail and a tenth of the width, until one point is left, and then that goes.
98
+ if (!atStep)
99
+ return false;
100
+ if (this.#points.length <= 1) {
101
+ const hadPoints = this.#points.length > 0;
102
+ this.#points = [];
103
+ return hadPoints;
104
+ }
105
+ this.#size = Math.max(SCRIBBLE_CONFIG.minSize, this.#size * (1 - SCRIBBLE_CONFIG.shrink));
106
+ this.#points.shift();
107
+ return true;
108
+ }
109
+ /** What to draw now. */
110
+ view() {
111
+ return { points: this.#points.slice(), size: this.#size, stopping: this.#phase === 'stopping' };
112
+ }
113
+ #takeIncoming() {
114
+ const incoming = this.#incoming;
115
+ if (!incoming)
116
+ return false;
117
+ this.#incoming = null;
118
+ this.#points.push(incoming);
119
+ return true;
120
+ }
121
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The path of the shapes the canvas can build from a key: triangles and the shapes of the library. (The ink of the
3
+ * Draw tool is in `brush.ts`, arrows are in `arrow.ts`.) Each function returns commands in the local space of the
4
+ * box it was asked for, so the result can be written straight into a path object.
5
+ *
6
+ * Shapes that can be rebuilt (a triangle, a library shape whose corners can be rounded) are rebuilt from
7
+ * their key at every resize, so a corner radius stays the same fraction of the shape instead of being
8
+ * stretched with it.
9
+ */
10
+ import type { LibraryShape } from '../../canvas/shapes/ShapeLibrary.js';
11
+ import type { PathCommand } from './pathData.js';
12
+ import type { PathObject } from './types.js';
13
+ /** The `shape` key of a triangle path. */
14
+ export declare const TRIANGLE_SHAPE_KEY = "triangle";
15
+ /** The `shape` key of an arrow path (its geometry is in `arrow.ts`). */
16
+ export declare const ARROW_SHAPE_KEY = "arrow";
17
+ /** Whether a library shape keeps its aspect ratio: its own setting, else the rule of its category. */
18
+ export declare function libraryShapeKeepsAspect(shape: LibraryShape): boolean;
19
+ /**
20
+ * A triangle with its apex at the top middle and its base along the bottom, with corners rounded by
21
+ * `roundness` percent (100 rounds by a quarter of the shorter side).
22
+ */
23
+ export declare function trianglePathCommands(width: number, height: number, roundness?: number): PathCommand[];
24
+ /**
25
+ * A library shape fitted to a box. Shapes that can round their corners are regenerated at the given
26
+ * roundness; the others are the stored outline, scaled. Null for a key the library does not know.
27
+ */
28
+ export declare function libraryShapePathCommands(shapeKey: string, width: number, height: number, roundness?: number): PathCommand[] | null;
29
+ /** Whether a path of this kind is rebuilt from its key when it is resized (rather than scaled). */
30
+ export declare function isRegeneratedShape(shapeKey: string | null): boolean;
31
+ /** Whether the Roundness control applies to this path. */
32
+ export declare function shapeSupportsRoundness(shapeKey: string | null): boolean;
33
+ /** The path of a regenerated shape at a size, or null when the shape is drawn rather than generated. */
34
+ export declare function regeneratedPathCommands(shape: Pick<PathObject, 'shape' | 'roundness'>, width: number, height: number): PathCommand[] | null;