@widgetic/canvas 0.5.45 → 0.5.47

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 (137) hide show
  1. package/README.md +3 -2
  2. package/dist/canvas/DomCanvas.svelte +1 -2
  3. package/dist/canvas/autosaveScheduler.js +37 -89
  4. package/dist/canvas/boardFrame.js +1 -1
  5. package/dist/canvas/boardSurface.js +1 -1
  6. package/dist/canvas/canvasLogger.js +7 -19
  7. package/dist/canvas/chromeLayout.js +19 -21
  8. package/dist/canvas/eventScope.js +10 -14
  9. package/dist/canvas/host/CanvasHost.d.ts +6 -5
  10. package/dist/canvas/host/CanvasHost.js +1664 -1861
  11. package/dist/canvas/host/CanvasHostApi.js +4 -19
  12. package/dist/canvas/host/boardFiles.js +35 -51
  13. package/dist/canvas/host/browserServices.js +108 -196
  14. package/dist/canvas/host/conversion.js +82 -170
  15. package/dist/canvas/host/hostObject.js +7 -7
  16. package/dist/canvas/host/nearCards.js +18 -21
  17. package/dist/canvas/host/pageEvents.js +5 -5
  18. package/dist/canvas/host/pauseCollector.d.ts +18 -0
  19. package/dist/canvas/host/pauseCollector.js +45 -0
  20. package/dist/canvas/host/pictureSaves.js +9 -11
  21. package/dist/canvas/host/screenRects.js +28 -31
  22. package/dist/canvas/host/storedPictures.js +149 -243
  23. package/dist/canvas/host/strokeSnap.js +88 -143
  24. package/dist/canvas/index.d.ts +1 -1
  25. package/dist/canvas/openMarks.js +2 -2
  26. package/dist/canvas/outsideClickDeselect.js +6 -7
  27. package/dist/canvas/panZoomPosition.js +2 -2
  28. package/dist/canvas/preferredTextFont.js +13 -25
  29. package/dist/canvas/shapes/ShapeLibrary.js +138 -148
  30. package/dist/canvas/shapes/shapeImageStore.js +4 -4
  31. package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
  32. package/dist/canvas/toolbarPosition.js +2 -2
  33. package/dist/canvas/types.d.ts +7 -1
  34. package/dist/canvas/ui/CanvasContextMenu.svelte +40 -3
  35. package/dist/canvas/ui/CanvasHint.svelte +10 -9
  36. package/dist/canvas/ui/SelectionPanel.svelte +14 -12
  37. package/dist/canvas/ui/SelectionPanel.svelte.d.ts +0 -2
  38. package/dist/canvas/ui/ShapesMenu.svelte +6 -4
  39. package/dist/canvas/ui/ToolButton.svelte +7 -6
  40. package/dist/canvas/ui/colorModel.js +21 -32
  41. package/dist/canvas/ui/colorPickerMessages.js +5 -5
  42. package/dist/canvas/ui/floatingCard.d.ts +28 -8
  43. package/dist/canvas/ui/floatingCard.js +48 -28
  44. package/dist/canvas/ui/floatingLayer.js +5 -6
  45. package/dist/canvas/ui/keyHints.js +9 -11
  46. package/dist/canvas/ui/popoverSide.js +2 -3
  47. package/dist/canvas/ui/selectionTraits.d.ts +8 -4
  48. package/dist/canvas/ui/selectionTraits.js +44 -58
  49. package/dist/canvas/view/CanvasStage.svelte +1 -1
  50. package/dist/canvas/view/SelectionOverlay.svelte +2 -2
  51. package/dist/canvas/view/erasingMarks.js +7 -7
  52. package/dist/canvas/view/geometry.d.ts +1 -1
  53. package/dist/canvas/view/geometry.js +72 -91
  54. package/dist/canvas/view/liftedScene.js +24 -36
  55. package/dist/canvas/view/mountedScene.js +76 -79
  56. package/dist/components/CanvasBoardPlaceholder.svelte +68 -10
  57. package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +10 -2
  58. package/dist/components/CanvasToolbarPlaceholder.svelte +25 -11
  59. package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +6 -0
  60. package/dist/components/boardLoaderDelay.d.ts +39 -0
  61. package/dist/components/boardLoaderDelay.js +71 -0
  62. package/dist/components/input-controls/ColorIC.svelte +1 -1
  63. package/dist/components/input-controls/FontSelectorIC.svelte +1 -1
  64. package/dist/components/preferenceStorage.js +2 -2
  65. package/dist/engine/EngineCore.d.ts +42 -10
  66. package/dist/engine/EngineCore.js +843 -916
  67. package/dist/engine/codec/canvasDocument.js +192 -155
  68. package/dist/engine/codec/hostReadableKeys.js +1 -1
  69. package/dist/engine/core/arrange.js +26 -38
  70. package/dist/engine/core/arrow.js +79 -92
  71. package/dist/engine/core/arrowBinding.js +98 -116
  72. package/dist/engine/core/arrowBindingRecords.js +23 -38
  73. package/dist/engine/core/brush.d.ts +24 -0
  74. package/dist/engine/core/brush.js +223 -103
  75. package/dist/engine/core/cameraMove.js +39 -51
  76. package/dist/engine/core/defaults.js +92 -51
  77. package/dist/engine/core/freehand.js +147 -163
  78. package/dist/engine/core/geometry.js +47 -54
  79. package/dist/engine/core/history.js +38 -55
  80. package/dist/engine/core/hitTest.js +68 -81
  81. package/dist/engine/core/matrix.js +18 -20
  82. package/dist/engine/core/pathData.js +183 -210
  83. package/dist/engine/core/rotationReset.js +5 -6
  84. package/dist/engine/core/scene.js +160 -224
  85. package/dist/engine/core/scribble.js +59 -84
  86. package/dist/engine/core/shapePaths.js +12 -15
  87. package/dist/engine/core/shapeTools.d.ts +23 -18
  88. package/dist/engine/core/shapeTools.js +104 -67
  89. package/dist/engine/core/snapping.js +65 -117
  90. package/dist/engine/core/style.d.ts +18 -2
  91. package/dist/engine/core/style.js +79 -80
  92. package/dist/engine/core/styleMemory.d.ts +88 -0
  93. package/dist/engine/core/styleMemory.js +189 -0
  94. package/dist/engine/core/transform.js +84 -95
  95. package/dist/engine/core/types.js +4 -4
  96. package/dist/engine/core/viewport.js +23 -36
  97. package/dist/engine/input/PointerController.js +513 -526
  98. package/dist/engine/input/arrowHandles.js +12 -12
  99. package/dist/engine/input/gestures.d.ts +8 -3
  100. package/dist/engine/input/gestures.js +378 -346
  101. package/dist/engine/input/hoverCursor.js +1 -1
  102. package/dist/engine/input/keyboard.js +42 -41
  103. package/dist/engine/input/selectionHandles.js +45 -50
  104. package/dist/engine/input/types.js +1 -1
  105. package/dist/engine/render/rasterize.js +86 -158
  106. package/dist/engine/state.js +6 -17
  107. package/dist/engine/text/layoutText.js +43 -62
  108. package/dist/engine/text/textMetrics.js +8 -10
  109. package/dist/engine/widgets.js +101 -137
  110. package/dist/fonts/interFont.js +30 -83
  111. package/dist/fonts/workspaceFonts.js +33 -92
  112. package/dist/i18n/labels.js +4 -4
  113. package/dist/i18n/language.js +4 -4
  114. package/dist/i18n/messages.js +1 -1
  115. package/dist/i18n/shortcuts.js +14 -14
  116. package/dist/index.d.ts +1 -1
  117. package/dist/poster/boardPlacement.d.ts +4 -1
  118. package/dist/poster/boardPlacement.js +34 -33
  119. package/dist/poster/boardPoster.js +58 -83
  120. package/dist/poster/paintBoardPoster.js +16 -39
  121. package/dist/poster/posterPictures.js +27 -43
  122. package/dist/settings/rememberedStyles.d.ts +22 -0
  123. package/dist/settings/rememberedStyles.js +47 -0
  124. package/dist/settings/rememberedTool.d.ts +50 -0
  125. package/dist/settings/rememberedTool.js +68 -0
  126. package/dist/settings/workspaceToolSettings.d.ts +25 -0
  127. package/dist/settings/workspaceToolSettings.js +38 -48
  128. package/dist/tools/components/catalog.js +64 -74
  129. package/dist/tools/components/grouping.js +6 -28
  130. package/dist/tools/definitions.js +5 -5
  131. package/dist/tools/executors.js +137 -169
  132. package/dist/tools/hostAdapter.js +10 -13
  133. package/dist/tools/sceneSummary.js +43 -57
  134. package/dist/tools/sketchIntent.js +13 -21
  135. package/dist/tools/styleArguments.js +19 -21
  136. package/dist/tools/types.js +1 -1
  137. package/package.json +5 -5
@@ -3,7 +3,7 @@
3
3
  * rasterizer are built from. Pure functions on plain numbers.
4
4
  */
5
5
  import { boxToWorldMatrix, applyMatrixToPoint, invertMatrix } from './matrix.js';
6
- var EPSILON = 1e-9;
6
+ const EPSILON = 1e-9;
7
7
  export function rectRight(rect) {
8
8
  return rect.x + rect.width;
9
9
  }
@@ -52,12 +52,11 @@ export function inflateRect(rect, amount) {
52
52
  export function unionRects(rects) {
53
53
  if (rects.length === 0)
54
54
  return null;
55
- var left = Infinity;
56
- var top = Infinity;
57
- var right = -Infinity;
58
- var bottom = -Infinity;
59
- for (var _i = 0, rects_1 = rects; _i < rects_1.length; _i++) {
60
- var rect = rects_1[_i];
55
+ let left = Infinity;
56
+ let top = Infinity;
57
+ let right = -Infinity;
58
+ let bottom = -Infinity;
59
+ for (const rect of rects) {
61
60
  left = Math.min(left, rect.x);
62
61
  top = Math.min(top, rect.y);
63
62
  right = Math.max(right, rect.x + rect.width);
@@ -69,12 +68,11 @@ export function unionRects(rects) {
69
68
  export function boundsOfPoints(points) {
70
69
  if (points.length === 0)
71
70
  return null;
72
- var left = Infinity;
73
- var top = Infinity;
74
- var right = -Infinity;
75
- var bottom = -Infinity;
76
- for (var _i = 0, points_1 = points; _i < points_1.length; _i++) {
77
- var point = points_1[_i];
71
+ let left = Infinity;
72
+ let top = Infinity;
73
+ let right = -Infinity;
74
+ let bottom = -Infinity;
75
+ for (const point of points) {
78
76
  left = Math.min(left, point.x);
79
77
  top = Math.min(top, point.y);
80
78
  right = Math.max(right, point.x);
@@ -84,7 +82,7 @@ export function boundsOfPoints(points) {
84
82
  }
85
83
  /** The four corners of a box in world space, clockwise from the top-left (before rotation). */
86
84
  export function boxCorners(box) {
87
- var matrix = boxToWorldMatrix(box);
85
+ const matrix = boxToWorldMatrix(box);
88
86
  return [
89
87
  applyMatrixToPoint(matrix, { x: 0, y: 0 }),
90
88
  applyMatrixToPoint(matrix, { x: box.width, y: 0 }),
@@ -99,12 +97,11 @@ export function rotatedBoxBounds(box) {
99
97
  return boundsOfPoints(boxCorners(box));
100
98
  }
101
99
  /** Whether a world point is inside a (possibly rotated) box, with `slop` world units of leeway on every side. */
102
- export function pointInBox(box, point, slop) {
103
- if (slop === void 0) { slop = 0; }
104
- var inverse = invertMatrix(boxToWorldMatrix(box));
100
+ export function pointInBox(box, point, slop = 0) {
101
+ const inverse = invertMatrix(boxToWorldMatrix(box));
105
102
  if (!inverse)
106
103
  return false;
107
- var local = applyMatrixToPoint(inverse, point);
104
+ const local = applyMatrixToPoint(inverse, point);
108
105
  return (local.x >= -slop &&
109
106
  local.x <= box.width + slop &&
110
107
  local.y >= -slop &&
@@ -112,7 +109,7 @@ export function pointInBox(box, point, slop) {
112
109
  }
113
110
  /** A world point in the local space of a box. Null when the box is degenerate. */
114
111
  export function worldToBoxLocal(box, point) {
115
- var inverse = invertMatrix(boxToWorldMatrix(box));
112
+ const inverse = invertMatrix(boxToWorldMatrix(box));
116
113
  return inverse ? applyMatrixToPoint(inverse, point) : null;
117
114
  }
118
115
  export function distanceBetween(first, second) {
@@ -120,13 +117,13 @@ export function distanceBetween(first, second) {
120
117
  }
121
118
  /** The distance from a point to the segment a-b. */
122
119
  export function distanceToSegment(point, a, b) {
123
- var deltaX = b.x - a.x;
124
- var deltaY = b.y - a.y;
125
- var lengthSquared = deltaX * deltaX + deltaY * deltaY;
120
+ const deltaX = b.x - a.x;
121
+ const deltaY = b.y - a.y;
122
+ const lengthSquared = deltaX * deltaX + deltaY * deltaY;
126
123
  if (lengthSquared < EPSILON)
127
124
  return distanceBetween(point, a);
128
- var along = ((point.x - a.x) * deltaX + (point.y - a.y) * deltaY) / lengthSquared;
129
- var clamped = Math.max(0, Math.min(1, along));
125
+ const along = ((point.x - a.x) * deltaX + (point.y - a.y) * deltaY) / lengthSquared;
126
+ const clamped = Math.max(0, Math.min(1, along));
130
127
  return Math.hypot(point.x - (a.x + clamped * deltaX), point.y - (a.y + clamped * deltaY));
131
128
  }
132
129
  /** The distance from a point to a polyline (closed: the last point joins the first). */
@@ -135,10 +132,10 @@ export function distanceToPolyline(point, vertices, closed) {
135
132
  return Infinity;
136
133
  if (vertices.length === 1)
137
134
  return distanceBetween(point, vertices[0]);
138
- var best = Infinity;
139
- var last = closed ? vertices.length : vertices.length - 1;
140
- for (var index = 0; index < last; index += 1) {
141
- var distance = distanceToSegment(point, vertices[index], vertices[(index + 1) % vertices.length]);
135
+ let best = Infinity;
136
+ const last = closed ? vertices.length : vertices.length - 1;
137
+ for (let index = 0; index < last; index += 1) {
138
+ const distance = distanceToSegment(point, vertices[index], vertices[(index + 1) % vertices.length]);
142
139
  if (distance < best)
143
140
  best = distance;
144
141
  }
@@ -146,11 +143,11 @@ export function distanceToPolyline(point, vertices, closed) {
146
143
  }
147
144
  /** Even-odd test: whether the point lies inside the polygon. */
148
145
  export function pointInPolygon(point, vertices) {
149
- var inside = false;
150
- for (var index = 0, previous = vertices.length - 1; index < vertices.length; previous = index, index += 1) {
151
- var current = vertices[index];
152
- var before = vertices[previous];
153
- var crosses = current.y > point.y !== before.y > point.y &&
146
+ let inside = false;
147
+ for (let index = 0, previous = vertices.length - 1; index < vertices.length; previous = index, index += 1) {
148
+ const current = vertices[index];
149
+ const before = vertices[previous];
150
+ const crosses = current.y > point.y !== before.y > point.y &&
154
151
  point.x < ((before.x - current.x) * (point.y - current.y)) / (before.y - current.y) + current.x;
155
152
  if (crosses)
156
153
  inside = !inside;
@@ -159,19 +156,17 @@ export function pointInPolygon(point, vertices) {
159
156
  }
160
157
  /** Whether segment p1-p2 crosses segment p3-p4. */
161
158
  export function segmentsIntersect(p1, p2, p3, p4) {
162
- var orientation = function (a, b, c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); };
163
- var d1 = orientation(p3, p4, p1);
164
- var d2 = orientation(p3, p4, p2);
165
- var d3 = orientation(p1, p2, p3);
166
- var d4 = orientation(p1, p2, p4);
159
+ const orientation = (a, b, c) => (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
160
+ const d1 = orientation(p3, p4, p1);
161
+ const d2 = orientation(p3, p4, p2);
162
+ const d3 = orientation(p1, p2, p3);
163
+ const d4 = orientation(p1, p2, p4);
167
164
  if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
168
165
  return true;
169
- var onSegment = function (a, b, c) {
170
- return Math.min(a.x, b.x) - EPSILON <= c.x &&
171
- c.x <= Math.max(a.x, b.x) + EPSILON &&
172
- Math.min(a.y, b.y) - EPSILON <= c.y &&
173
- c.y <= Math.max(a.y, b.y) + EPSILON;
174
- };
166
+ const onSegment = (a, b, c) => Math.min(a.x, b.x) - EPSILON <= c.x &&
167
+ c.x <= Math.max(a.x, b.x) + EPSILON &&
168
+ Math.min(a.y, b.y) - EPSILON <= c.y &&
169
+ c.y <= Math.max(a.y, b.y) + EPSILON;
175
170
  if (Math.abs(d1) < EPSILON && onSegment(p3, p4, p1))
176
171
  return true;
177
172
  if (Math.abs(d2) < EPSILON && onSegment(p3, p4, p2))
@@ -187,8 +182,7 @@ export function polygonIntersectsRect(vertices, rect) {
187
182
  if (vertices.length === 0)
188
183
  return false;
189
184
  // Any vertex inside the rectangle.
190
- for (var _i = 0, vertices_1 = vertices; _i < vertices_1.length; _i++) {
191
- var vertex = vertices_1[_i];
185
+ for (const vertex of vertices) {
192
186
  if (rectContainsPoint(rect, vertex))
193
187
  return true;
194
188
  }
@@ -196,16 +190,16 @@ export function polygonIntersectsRect(vertices, rect) {
196
190
  if (pointInPolygon(rectCenter(rect), vertices))
197
191
  return true;
198
192
  // Any edge crossing any edge of the rectangle.
199
- var rectangleCorners = [
193
+ const rectangleCorners = [
200
194
  { x: rect.x, y: rect.y },
201
195
  { x: rect.x + rect.width, y: rect.y },
202
196
  { x: rect.x + rect.width, y: rect.y + rect.height },
203
197
  { x: rect.x, y: rect.y + rect.height }
204
198
  ];
205
- for (var index = 0; index < vertices.length; index += 1) {
206
- var start = vertices[index];
207
- var end = vertices[(index + 1) % vertices.length];
208
- for (var side = 0; side < 4; side += 1) {
199
+ for (let index = 0; index < vertices.length; index += 1) {
200
+ const start = vertices[index];
201
+ const end = vertices[(index + 1) % vertices.length];
202
+ for (let side = 0; side < 4; side += 1) {
209
203
  if (segmentsIntersect(start, end, rectangleCorners[side], rectangleCorners[(side + 1) % 4]))
210
204
  return true;
211
205
  }
@@ -213,9 +207,8 @@ export function polygonIntersectsRect(vertices, rect) {
213
207
  return false;
214
208
  }
215
209
  /** Rounds to a number of decimals (default 2): for stored geometry, so JSON does not carry noise digits. */
216
- export function roundTo(value, decimals) {
217
- if (decimals === void 0) { decimals = 2; }
218
- var factor = Math.pow(10, decimals);
210
+ export function roundTo(value, decimals = 2) {
211
+ const factor = 10 ** decimals;
219
212
  return Math.round(value * factor) / factor;
220
213
  }
221
214
  export function clampNumber(value, minimum, maximum) {
@@ -5,50 +5,35 @@
5
5
  * hundred steps costs a hundred arrays of pointers, not a hundred copies of the board. One step is pushed
6
6
  * when a gesture ends (a drag, a resize, a stroke), never while it runs.
7
7
  */
8
- var HistoryStack = /** @class */ (function () {
9
- function HistoryStack(initial, options) {
10
- if (options === void 0) { options = {}; }
11
- var _a, _b;
12
- this.cursor = 0;
8
+ export class HistoryStack {
9
+ entries;
10
+ cursor = 0;
11
+ limit;
12
+ isSame;
13
+ constructor(initial, options = {}) {
13
14
  this.entries = [initial];
14
- this.limit = Math.max(2, (_a = options.limit) !== null && _a !== void 0 ? _a : 100);
15
- this.isSame = (_b = options.isSame) !== null && _b !== void 0 ? _b : Object.is;
15
+ this.limit = Math.max(2, options.limit ?? 100);
16
+ this.isSame = options.isSame ?? Object.is;
17
+ }
18
+ /** The state the board is in now. */
19
+ get current() {
20
+ return this.entries[this.cursor];
21
+ }
22
+ get canUndo() {
23
+ return this.cursor > 0;
24
+ }
25
+ get canRedo() {
26
+ return this.cursor < this.entries.length - 1;
27
+ }
28
+ /** How many states the stack holds (the start included). */
29
+ get length() {
30
+ return this.entries.length;
16
31
  }
17
- Object.defineProperty(HistoryStack.prototype, "current", {
18
- /** The state the board is in now. */
19
- get: function () {
20
- return this.entries[this.cursor];
21
- },
22
- enumerable: false,
23
- configurable: true
24
- });
25
- Object.defineProperty(HistoryStack.prototype, "canUndo", {
26
- get: function () {
27
- return this.cursor > 0;
28
- },
29
- enumerable: false,
30
- configurable: true
31
- });
32
- Object.defineProperty(HistoryStack.prototype, "canRedo", {
33
- get: function () {
34
- return this.cursor < this.entries.length - 1;
35
- },
36
- enumerable: false,
37
- configurable: true
38
- });
39
- Object.defineProperty(HistoryStack.prototype, "length", {
40
- /** How many states the stack holds (the start included). */
41
- get: function () {
42
- return this.entries.length;
43
- },
44
- enumerable: false,
45
- configurable: true
46
- });
47
32
  /**
48
33
  * Adds a step after the current one and forgets the steps that were undone.
49
34
  * Returns false (and records nothing) when the state is the same as the current one.
50
35
  */
51
- HistoryStack.prototype.push = function (state) {
36
+ push(state) {
52
37
  if (this.isSame(this.current, state))
53
38
  return false;
54
39
  this.entries = this.entries.slice(0, this.cursor + 1);
@@ -57,46 +42,44 @@ var HistoryStack = /** @class */ (function () {
57
42
  this.entries.splice(0, this.entries.length - this.limit);
58
43
  this.cursor = this.entries.length - 1;
59
44
  return true;
60
- };
45
+ }
61
46
  /** Goes one step back and returns that state, or null at the start. */
62
- HistoryStack.prototype.undo = function () {
47
+ undo() {
63
48
  if (!this.canUndo)
64
49
  return null;
65
50
  this.cursor -= 1;
66
51
  return this.entries[this.cursor];
67
- };
52
+ }
68
53
  /** Goes one step forward and returns that state, or null at the end. */
69
- HistoryStack.prototype.redo = function () {
54
+ redo() {
70
55
  if (!this.canRedo)
71
56
  return null;
72
57
  this.cursor += 1;
73
58
  return this.entries[this.cursor];
74
- };
59
+ }
75
60
  /** Replaces the current step without adding one (for a change that must not be undone on its own). */
76
- HistoryStack.prototype.replaceCurrent = function (state) {
61
+ replaceCurrent(state) {
77
62
  this.entries[this.cursor] = state;
78
- };
63
+ }
79
64
  /**
80
65
  * Rewrites every step, the past and the future of the cursor too, with no step added (a repair that all of them must have, as
81
66
  * a picture that arrived late: going back to a step from before it came must not take it away). A step that the rewrite
82
67
  * leaves as it is keeps its identity, and a state that is held by two steps is rewritten once.
83
68
  */
84
- HistoryStack.prototype.mapAll = function (rewrite) {
85
- var rewritten = new Map();
86
- this.entries = this.entries.map(function (entry) {
87
- var result = rewritten.get(entry);
69
+ mapAll(rewrite) {
70
+ const rewritten = new Map();
71
+ this.entries = this.entries.map((entry) => {
72
+ let result = rewritten.get(entry);
88
73
  if (result === undefined) {
89
74
  result = rewrite(entry);
90
75
  rewritten.set(entry, result);
91
76
  }
92
77
  return result;
93
78
  });
94
- };
79
+ }
95
80
  /** Forgets everything and starts again from this state (a canvas was loaded). */
96
- HistoryStack.prototype.reset = function (state) {
81
+ reset(state) {
97
82
  this.entries = [state];
98
83
  this.cursor = 0;
99
- };
100
- return HistoryStack;
101
- }());
102
- export { HistoryStack };
84
+ }
85
+ }
@@ -16,16 +16,16 @@ import { applyMatrixToPoint, boxToWorldMatrix } from './matrix.js';
16
16
  import { flattenPath, parsePathData } from './pathData.js';
17
17
  import { indexScene, isWithin, topLevelAncestorId } from './scene.js';
18
18
  import { isBoxedObject } from './types.js';
19
- var DEFAULT_TOLERANCE_PIXELS = 5;
20
- var FLATTENED_PATH_CACHE_LIMIT = 400;
19
+ const DEFAULT_TOLERANCE_PIXELS = 5;
20
+ const FLATTENED_PATH_CACHE_LIMIT = 400;
21
21
  /** Flattened outlines by path data, so a hover over a busy board does not parse the same strokes again. */
22
- var flattenedPathCache = new Map();
22
+ const flattenedPathCache = new Map();
23
23
  /** The outline of a path flattened to straight pieces, in the coordinates of its box (the same pieces for as long as the same data is asked for). */
24
24
  export function flattenedOutline(data) {
25
- var cached = flattenedPathCache.get(data);
25
+ const cached = flattenedPathCache.get(data);
26
26
  if (cached)
27
27
  return cached;
28
- var flattened = flattenPath(parsePathData(data));
28
+ const flattened = flattenPath(parsePathData(data));
29
29
  if (flattenedPathCache.size >= FLATTENED_PATH_CACHE_LIMIT)
30
30
  flattenedPathCache.clear();
31
31
  flattenedPathCache.set(data, flattened);
@@ -46,41 +46,39 @@ function halfStroke(shape) {
46
46
  }
47
47
  /** Whether a world point is on a shape (inside it, or near its outline, depending on the kind of shape). */
48
48
  export function shapeContainsPoint(shape, worldPoint, toleranceWorld) {
49
- var reach = toleranceWorld + halfStroke(shape);
49
+ const reach = toleranceWorld + halfStroke(shape);
50
50
  // Cheap rejection first: outside the box grown by the reach, nothing else can say yes. A brush stroke's outline spills past
51
51
  // the box of its line, so the box grows by that as well (the outline itself is the shape: the reach on it stays the tolerance).
52
52
  if (!pointInBox(shape, worldPoint, reach + brushOverflow(shape)))
53
53
  return false;
54
- var local = worldToBoxLocal(shape, worldPoint);
54
+ const local = worldToBoxLocal(shape, worldPoint);
55
55
  if (!local)
56
56
  return false;
57
57
  switch (shape.type) {
58
58
  case 'rect': {
59
59
  if (!isHollow(shape))
60
60
  return true;
61
- var edgeDistance = Math.min(Math.abs(local.x), Math.abs(local.x - shape.width), Math.abs(local.y), Math.abs(local.y - shape.height));
61
+ const edgeDistance = Math.min(Math.abs(local.x), Math.abs(local.x - shape.width), Math.abs(local.y), Math.abs(local.y - shape.height));
62
62
  return edgeDistance <= reach;
63
63
  }
64
64
  case 'ellipse': {
65
- var radiusX = Math.max(0.5, shape.width / 2);
66
- var radiusY = Math.max(0.5, shape.height / 2);
67
- var normalized = Math.hypot((local.x - radiusX) / radiusX, (local.y - radiusY) / radiusY);
68
- var smallestRadius = Math.min(radiusX, radiusY);
65
+ const radiusX = Math.max(0.5, shape.width / 2);
66
+ const radiusY = Math.max(0.5, shape.height / 2);
67
+ const normalized = Math.hypot((local.x - radiusX) / radiusX, (local.y - radiusY) / radiusY);
68
+ const smallestRadius = Math.min(radiusX, radiusY);
69
69
  if (!isHollow(shape))
70
70
  return normalized <= 1 + reach / smallestRadius;
71
71
  return Math.abs(normalized - 1) * smallestRadius <= reach;
72
72
  }
73
73
  case 'path': {
74
- var outlines = flattenedOutline(shape.d);
75
- for (var _i = 0, outlines_1 = outlines; _i < outlines_1.length; _i++) {
76
- var outline = outlines_1[_i];
74
+ const outlines = flattenedOutline(shape.d);
75
+ for (const outline of outlines) {
77
76
  if (distanceToPolyline(local, outline.points, outline.closed) <= reach)
78
77
  return true;
79
78
  }
80
79
  if (!isHollow(shape)) {
81
80
  // A fill closes an open outline implicitly, so every outline counts as a polygon here.
82
- for (var _a = 0, outlines_2 = outlines; _a < outlines_2.length; _a++) {
83
- var outline = outlines_2[_a];
81
+ for (const outline of outlines) {
84
82
  if (outline.points.length >= 3 && pointInPolygon(local, outline.points))
85
83
  return true;
86
84
  }
@@ -94,26 +92,24 @@ export function shapeContainsPoint(shape, worldPoint, toleranceWorld) {
94
92
  }
95
93
  /** The unit a person selects when a leaf shape is picked: the group nearest the board, or the entered group's member. */
96
94
  function selectableIdFor(index, leafId, enteredGroupId) {
97
- var _a;
98
95
  if (!enteredGroupId || !isWithin(index, leafId, enteredGroupId) || leafId === enteredGroupId) {
99
96
  return topLevelAncestorId(index, leafId);
100
97
  }
101
- var current = index.get(leafId);
98
+ let current = index.get(leafId);
102
99
  while (current && current.parentId !== null && current.parentId !== enteredGroupId) {
103
100
  current = index.get(current.parentId);
104
101
  }
105
- return (_a = current === null || current === void 0 ? void 0 : current.id) !== null && _a !== void 0 ? _a : leafId;
102
+ return current?.id ?? leafId;
106
103
  }
107
104
  /** The topmost selectable object under a world point, or null. */
108
105
  export function hitTestPoint(scene, worldPoint, options) {
109
- var _a, _b, _c;
110
- var index = (_a = options.index) !== null && _a !== void 0 ? _a : indexScene(scene);
111
- var toleranceWorld = ((_b = options.tolerancePixels) !== null && _b !== void 0 ? _b : DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
112
- for (var position = scene.length - 1; position >= 0; position -= 1) {
113
- var object = scene[position];
106
+ const index = options.index ?? indexScene(scene);
107
+ const toleranceWorld = (options.tolerancePixels ?? DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
108
+ for (let position = scene.length - 1; position >= 0; position -= 1) {
109
+ const object = scene[position];
114
110
  if (!isBoxedObject(object))
115
111
  continue;
116
- if ((_c = options.ignore) === null || _c === void 0 ? void 0 : _c.has(object.id))
112
+ if (options.ignore?.has(object.id))
117
113
  continue;
118
114
  if (shapeContainsPoint(object, worldPoint, toleranceWorld)) {
119
115
  return selectableIdFor(index, object.id, options.enteredGroupId);
@@ -126,20 +122,18 @@ export function hitTestPoint(scene, worldPoint, options) {
126
122
  * through, one object further down at every click.
127
123
  */
128
124
  export function hitTestStack(scene, worldPoint, options) {
129
- var _a, _b, _c;
130
- var index = (_a = options.index) !== null && _a !== void 0 ? _a : indexScene(scene);
131
- var toleranceWorld = ((_b = options.tolerancePixels) !== null && _b !== void 0 ? _b : DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
132
- var stack = [];
133
- var seen = new Set();
134
- for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
135
- var object = scene_1[_i];
125
+ const index = options.index ?? indexScene(scene);
126
+ const toleranceWorld = (options.tolerancePixels ?? DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
127
+ const stack = [];
128
+ const seen = new Set();
129
+ for (const object of scene) {
136
130
  if (!isBoxedObject(object))
137
131
  continue;
138
- if ((_c = options.ignore) === null || _c === void 0 ? void 0 : _c.has(object.id))
132
+ if (options.ignore?.has(object.id))
139
133
  continue;
140
134
  if (!shapeContainsPoint(object, worldPoint, toleranceWorld))
141
135
  continue;
142
- var unitId = selectableIdFor(index, object.id, options.enteredGroupId);
136
+ const unitId = selectableIdFor(index, object.id, options.enteredGroupId);
143
137
  // A unit that is hit again by a later member moves up to that member's place in the paint order.
144
138
  if (seen.has(unitId))
145
139
  stack.splice(stack.indexOf(unitId), 1);
@@ -150,13 +144,12 @@ export function hitTestStack(scene, worldPoint, options) {
150
144
  }
151
145
  /** The leaf shape under a world point, ignoring groups (what the eraser removes, what a double-click enters). */
152
146
  export function hitTestLeaf(scene, worldPoint, options) {
153
- var _a, _b;
154
- var toleranceWorld = ((_a = options.tolerancePixels) !== null && _a !== void 0 ? _a : DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
155
- for (var position = scene.length - 1; position >= 0; position -= 1) {
156
- var object = scene[position];
147
+ const toleranceWorld = (options.tolerancePixels ?? DEFAULT_TOLERANCE_PIXELS) / Math.max(options.zoom, 1e-6);
148
+ for (let position = scene.length - 1; position >= 0; position -= 1) {
149
+ const object = scene[position];
157
150
  if (!isBoxedObject(object))
158
151
  continue;
159
- if ((_b = options.ignore) === null || _b === void 0 ? void 0 : _b.has(object.id))
152
+ if (options.ignore?.has(object.id))
160
153
  continue;
161
154
  if (shapeContainsPoint(object, worldPoint, toleranceWorld))
162
155
  return object;
@@ -165,32 +158,31 @@ export function hitTestLeaf(scene, worldPoint, options) {
165
158
  }
166
159
  /** Whether a polyline (open or closed) touches a rectangle. */
167
160
  function polylineTouchesRect(points, closed, rect) {
168
- for (var _i = 0, points_1 = points; _i < points_1.length; _i++) {
169
- var point = points_1[_i];
161
+ for (const point of points) {
170
162
  if (rectContainsPoint(rect, point))
171
163
  return true;
172
164
  }
173
- var corners = [
165
+ const corners = [
174
166
  { x: rect.x, y: rect.y },
175
167
  { x: rect.x + rect.width, y: rect.y },
176
168
  { x: rect.x + rect.width, y: rect.y + rect.height },
177
169
  { x: rect.x, y: rect.y + rect.height }
178
170
  ];
179
- var last = closed ? points.length : points.length - 1;
180
- for (var position = 0; position < last; position += 1) {
181
- var start = points[position];
182
- var end = points[(position + 1) % points.length];
183
- for (var side = 0; side < 4; side += 1) {
171
+ const last = closed ? points.length : points.length - 1;
172
+ for (let position = 0; position < last; position += 1) {
173
+ const start = points[position];
174
+ const end = points[(position + 1) % points.length];
175
+ for (let side = 0; side < 4; side += 1) {
184
176
  if (segmentsIntersect(start, end, corners[side], corners[(side + 1) % 4]))
185
177
  return true;
186
178
  }
187
179
  }
188
180
  return false;
189
181
  }
190
- var shapesInTheWorld = new WeakMap();
182
+ const shapesInTheWorld = new WeakMap();
191
183
  function shapeInTheWorld(shape) {
192
- var data = shape.type === 'path' ? shape.d : null;
193
- var cached = shapesInTheWorld.get(shape);
184
+ const data = shape.type === 'path' ? shape.d : null;
185
+ const cached = shapesInTheWorld.get(shape);
194
186
  if (cached &&
195
187
  cached.x === shape.x &&
196
188
  cached.y === shape.y &&
@@ -200,35 +192,34 @@ function shapeInTheWorld(shape) {
200
192
  cached.data === data) {
201
193
  return cached;
202
194
  }
203
- var outlines = [];
204
- var corners = [];
195
+ const outlines = [];
196
+ let corners = [];
205
197
  if (data !== null) {
206
- var matrix_1 = boxToWorldMatrix(shape);
207
- for (var _i = 0, _a = flattenedOutline(data); _i < _a.length; _i++) {
208
- var outline = _a[_i];
209
- outlines.push({ points: outline.points.map(function (point) { return applyMatrixToPoint(matrix_1, point); }), closed: outline.closed });
198
+ const matrix = boxToWorldMatrix(shape);
199
+ for (const outline of flattenedOutline(data)) {
200
+ outlines.push({ points: outline.points.map((point) => applyMatrixToPoint(matrix, point)), closed: outline.closed });
210
201
  }
211
202
  }
212
203
  else {
213
204
  corners = boxCorners(shape);
214
205
  }
215
- var made = {
206
+ const made = {
216
207
  x: shape.x,
217
208
  y: shape.y,
218
209
  width: shape.width,
219
210
  height: shape.height,
220
211
  rotation: shape.rotation,
221
- data: data,
222
- corners: corners,
223
- outlines: outlines,
224
- bounds: boundsOfPoints(data !== null ? outlines.flatMap(function (outline) { return outline.points; }) : corners)
212
+ data,
213
+ corners,
214
+ outlines,
215
+ bounds: boundsOfPoints(data !== null ? outlines.flatMap((outline) => outline.points) : corners)
225
216
  };
226
217
  shapesInTheWorld.set(shape, made);
227
218
  return made;
228
219
  }
229
220
  // How far a shape must lie from a rectangle, box to box, before nothing of it can touch the rectangle: far more than the slack
230
221
  // the segment tests allow themselves (1e-9), so that the quick answer below never says no where those tests would say yes.
231
- var CLEAR_OF_RECT_MARGIN = 1e-6;
222
+ const CLEAR_OF_RECT_MARGIN = 1e-6;
232
223
  /**
233
224
  * Whether a shape touches a rectangle of the world (by its outline or fill, not only by its box).
234
225
  *
@@ -238,10 +229,10 @@ var CLEAR_OF_RECT_MARGIN = 1e-6;
238
229
  * tested point by point.
239
230
  */
240
231
  export function shapeIntersectsRect(shape, rect) {
241
- var world = shapeInTheWorld(shape);
242
- var bounds = world.bounds;
232
+ const world = shapeInTheWorld(shape);
233
+ const bounds = world.bounds;
243
234
  if (bounds) {
244
- var clear = bounds.x - CLEAR_OF_RECT_MARGIN > rect.x + rect.width ||
235
+ const clear = bounds.x - CLEAR_OF_RECT_MARGIN > rect.x + rect.width ||
245
236
  bounds.x + bounds.width + CLEAR_OF_RECT_MARGIN < rect.x ||
246
237
  bounds.y - CLEAR_OF_RECT_MARGIN > rect.y + rect.height ||
247
238
  bounds.y + bounds.height + CLEAR_OF_RECT_MARGIN < rect.y;
@@ -251,8 +242,7 @@ export function shapeIntersectsRect(shape, rect) {
251
242
  return true;
252
243
  }
253
244
  if (shape.type === 'path') {
254
- for (var _i = 0, _a = world.outlines; _i < _a.length; _i++) {
255
- var outline = _a[_i];
245
+ for (const outline of world.outlines) {
256
246
  if (polylineTouchesRect(outline.points, outline.closed, rect))
257
247
  return true;
258
248
  if (!isHollow(shape) && outline.points.length >= 3 && pointInPolygon(rectCenter(rect), outline.points))
@@ -268,22 +258,20 @@ export function shapeIntersectsRect(shape, rect) {
268
258
  }
269
259
  /** The selectable objects a marquee takes, each once, in paint order. */
270
260
  export function hitTestRect(scene, rect, options) {
271
- var _a, _b;
272
- var index = (_a = options.index) !== null && _a !== void 0 ? _a : indexScene(scene);
273
- var found = [];
274
- var seen = new Set();
275
- for (var _i = 0, scene_2 = scene; _i < scene_2.length; _i++) {
276
- var object = scene_2[_i];
261
+ const index = options.index ?? indexScene(scene);
262
+ const found = [];
263
+ const seen = new Set();
264
+ for (const object of scene) {
277
265
  if (!isBoxedObject(object))
278
266
  continue;
279
- if ((_b = options.ignore) === null || _b === void 0 ? void 0 : _b.has(object.id))
267
+ if (options.ignore?.has(object.id))
280
268
  continue;
281
- var taken = options.mode === 'contain'
269
+ const taken = options.mode === 'contain'
282
270
  ? rectContainsRect(rect, rotatedBoxBounds(object))
283
271
  : shapeIntersectsRect(object, rect);
284
272
  if (!taken)
285
273
  continue;
286
- var unitId = selectableIdFor(index, object.id, options.enteredGroupId);
274
+ const unitId = selectableIdFor(index, object.id, options.enteredGroupId);
287
275
  if (!seen.has(unitId)) {
288
276
  seen.add(unitId);
289
277
  found.push(unitId);
@@ -291,12 +279,11 @@ export function hitTestRect(scene, rect, options) {
291
279
  }
292
280
  // A group is taken whole or not at all in `contain` mode: one member left out leaves the group out.
293
281
  if (options.mode === 'contain') {
294
- return found.filter(function (id) {
295
- var unit = index.get(id);
282
+ return found.filter((id) => {
283
+ const unit = index.get(id);
296
284
  if (!unit || unit.type !== 'group')
297
285
  return true;
298
- for (var _i = 0, scene_3 = scene; _i < scene_3.length; _i++) {
299
- var object = scene_3[_i];
286
+ for (const object of scene) {
300
287
  if (isBoxedObject(object) && isWithin(index, object.id, id) && !rectContainsRect(rect, rotatedBoxBounds(object))) {
301
288
  return false;
302
289
  }