@widgetic/canvas 0.5.39 → 0.5.41

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