@widgetic/canvas 0.5.41 → 0.5.42
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.
- package/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.js +17 -18
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.js +1286 -1536
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/browserServices.js +65 -116
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/pageEvents.js +4 -4
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.js +3 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +18 -18
- package/dist/canvas/ui/floatingLayer.js +5 -6
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +34 -40
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.js +696 -795
- package/dist/engine/codec/canvasDocument.js +183 -147
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +76 -89
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +67 -94
- package/dist/engine/core/defaults.js +90 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.js +33 -50
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.js +129 -187
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +31 -64
- package/dist/engine/core/transform.js +83 -94
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +467 -474
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +262 -308
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +27 -30
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.js +4 -15
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.js +17 -68
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/settings/workspaceToolSettings.js +32 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +105 -141
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/types.js +1 -1
- package/package.json +1 -1
|
@@ -19,43 +19,32 @@
|
|
|
19
19
|
* to; a hollow shape only near its line, as for any click on it) and how (`edge` when the pointer is near the outline, `center` when
|
|
20
20
|
* it is further in).
|
|
21
21
|
*/
|
|
22
|
-
var __assign = (this && this.__assign) || function () {
|
|
23
|
-
__assign = Object.assign || function(t) {
|
|
24
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
25
|
-
s = arguments[i];
|
|
26
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
27
|
-
t[p] = s[p];
|
|
28
|
-
}
|
|
29
|
-
return t;
|
|
30
|
-
};
|
|
31
|
-
return __assign.apply(this, arguments);
|
|
32
|
-
};
|
|
33
22
|
import { arrowPointsInWorld, isArrow, reshapeArrow } from './arrow.js';
|
|
34
23
|
import { ARROW_ENDS, bindingOfEnd, canBeTiedTo, hasBindings } from './arrowBindingRecords.js';
|
|
35
24
|
import { clampNumber, distanceBetween, pointInPolygon, worldToBoxLocal } from './geometry.js';
|
|
36
25
|
import { flattenedOutline, shapeContainsPoint } from './hitTest.js';
|
|
37
26
|
import { applyMatrixToPoint, boxToWorldMatrix } from './matrix.js';
|
|
38
27
|
/** How far from a shape (screen pixels) the pointer may be and still tie an end to it: the reach a click has on a line. */
|
|
39
|
-
export
|
|
28
|
+
export const ARROW_BIND_REACH_PIXELS = 6;
|
|
40
29
|
/** Within this many screen pixels of the outline (inside or outside) an end rests on that point of the outline; further in it aims at the middle. */
|
|
41
|
-
export
|
|
30
|
+
export const ARROW_EDGE_BAND_PIXELS = 14;
|
|
42
31
|
/** The band never takes more than this fraction of the shorter side of the shape, so that a small shape still has a middle to aim at. */
|
|
43
|
-
export
|
|
32
|
+
export const ARROW_EDGE_BAND_FRACTION = 0.25;
|
|
44
33
|
/** Two points closer than this (world units) are the same place: an arrow that is there already is not drawn again. */
|
|
45
|
-
|
|
34
|
+
const SAME_PLACE = 1e-6;
|
|
46
35
|
/** The straight pieces a quarter of a rounded corner is cut into when the outline of a rounded rectangle is walked. */
|
|
47
|
-
|
|
36
|
+
const CORNER_STEPS = 8;
|
|
48
37
|
/** The radius of the corners of a rectangle with a roundness of 0 to 100 (100 is a pill): the one the board draws (`cornerRadiusOf`). */
|
|
49
38
|
function cornerRadius(shape) {
|
|
50
39
|
return (clampNumber(shape.roundness, 0, 100) / 100) * (Math.min(shape.width, shape.height) / 2);
|
|
51
40
|
}
|
|
52
41
|
/** The outline of a rectangle with rounded corners, clockwise on the screen from the left end of its top edge. */
|
|
53
42
|
function roundedBoxLines(width, height, radius) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
for (
|
|
58
|
-
|
|
43
|
+
const reach = clampNumber(radius, 0, Math.min(width, height) / 2);
|
|
44
|
+
const points = [];
|
|
45
|
+
const corner = (centerX, centerY, startAngle) => {
|
|
46
|
+
for (let step = 0; step <= CORNER_STEPS; step += 1) {
|
|
47
|
+
const angle = startAngle + (step / CORNER_STEPS) * (Math.PI / 2);
|
|
59
48
|
points.push({ x: centerX + reach * Math.cos(angle), y: centerY + reach * Math.sin(angle) });
|
|
60
49
|
}
|
|
61
50
|
};
|
|
@@ -63,44 +52,44 @@ function roundedBoxLines(width, height, radius) {
|
|
|
63
52
|
corner(width - reach, reach, Math.PI * 1.5);
|
|
64
53
|
corner(width - reach, height - reach, 0);
|
|
65
54
|
corner(reach, height - reach, Math.PI / 2);
|
|
66
|
-
return { points
|
|
55
|
+
return { points, closed: true };
|
|
67
56
|
}
|
|
68
57
|
function outlineOf(shape) {
|
|
69
|
-
|
|
58
|
+
const { width, height } = shape;
|
|
70
59
|
switch (shape.type) {
|
|
71
60
|
case 'ellipse':
|
|
72
|
-
return { kind: 'ellipse', width
|
|
61
|
+
return { kind: 'ellipse', width, height };
|
|
73
62
|
case 'rect': {
|
|
74
|
-
|
|
75
|
-
return radius > 0.5 ? { kind: 'lines', width
|
|
63
|
+
const radius = cornerRadius(shape);
|
|
64
|
+
return radius > 0.5 ? { kind: 'lines', width, height, lines: [roundedBoxLines(width, height, radius)] } : { kind: 'box', width, height };
|
|
76
65
|
}
|
|
77
66
|
case 'path': {
|
|
78
|
-
|
|
79
|
-
return lines.length > 0 ? { kind: 'lines', width
|
|
67
|
+
const lines = flattenedOutline(shape.d).filter((outline) => outline.points.length >= 2);
|
|
68
|
+
return lines.length > 0 ? { kind: 'lines', width, height, lines } : { kind: 'box', width, height };
|
|
80
69
|
}
|
|
81
70
|
default:
|
|
82
71
|
// Text, pictures and widget cards are their box.
|
|
83
|
-
return { kind: 'box', width
|
|
72
|
+
return { kind: 'box', width, height };
|
|
84
73
|
}
|
|
85
74
|
}
|
|
86
75
|
function closestOnSegment(point, first, second) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
76
|
+
const deltaX = second.x - first.x;
|
|
77
|
+
const deltaY = second.y - first.y;
|
|
78
|
+
const lengthSquared = deltaX * deltaX + deltaY * deltaY;
|
|
90
79
|
if (lengthSquared < 1e-12)
|
|
91
80
|
return first;
|
|
92
|
-
|
|
81
|
+
const along = clampNumber(((point.x - first.x) * deltaX + (point.y - first.y) * deltaY) / lengthSquared, 0, 1);
|
|
93
82
|
return { x: first.x + along * deltaX, y: first.y + along * deltaY };
|
|
94
83
|
}
|
|
95
84
|
/** The point of the outline nearest to a point (both in the coordinates of the box). */
|
|
96
85
|
function nearestOnOutline(outline, point) {
|
|
97
|
-
|
|
86
|
+
const { width, height } = outline;
|
|
98
87
|
if (outline.kind === 'box') {
|
|
99
88
|
if (point.x < 0 || point.x > width || point.y < 0 || point.y > height) {
|
|
100
89
|
return { x: clampNumber(point.x, 0, width), y: clampNumber(point.y, 0, height) };
|
|
101
90
|
}
|
|
102
91
|
// Inside: the nearest of the four sides.
|
|
103
|
-
|
|
92
|
+
const least = Math.min(point.x, width - point.x, point.y, height - point.y);
|
|
104
93
|
if (least === point.x)
|
|
105
94
|
return { x: 0, y: point.y };
|
|
106
95
|
if (least === width - point.x)
|
|
@@ -110,25 +99,24 @@ function nearestOnOutline(outline, point) {
|
|
|
110
99
|
return { x: point.x, y: height };
|
|
111
100
|
}
|
|
112
101
|
if (outline.kind === 'ellipse') {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
102
|
+
const radiusX = Math.max(width / 2, 1e-6);
|
|
103
|
+
const radiusY = Math.max(height / 2, 1e-6);
|
|
104
|
+
const offsetX = point.x - width / 2;
|
|
105
|
+
const offsetY = point.y - height / 2;
|
|
106
|
+
const reach = Math.hypot(offsetX / radiusX, offsetY / radiusY);
|
|
118
107
|
// The middle has no nearest point: the one at the right will do.
|
|
119
108
|
if (reach < 1e-9)
|
|
120
109
|
return { x: width, y: height / 2 };
|
|
121
110
|
// The point of the ellipse on the line from the middle through the point.
|
|
122
111
|
return { x: width / 2 + offsetX / reach, y: height / 2 + offsetY / reach };
|
|
123
112
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
for (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
var distance = distanceBetween(point, candidate);
|
|
113
|
+
let nearest = outline.lines[0].points[0];
|
|
114
|
+
let nearestDistance = Infinity;
|
|
115
|
+
for (const line of outline.lines) {
|
|
116
|
+
const last = line.closed ? line.points.length : line.points.length - 1;
|
|
117
|
+
for (let index = 0; index < last; index += 1) {
|
|
118
|
+
const candidate = closestOnSegment(point, line.points[index], line.points[(index + 1) % line.points.length]);
|
|
119
|
+
const distance = distanceBetween(point, candidate);
|
|
132
120
|
if (distance < nearestDistance) {
|
|
133
121
|
nearest = candidate;
|
|
134
122
|
nearestDistance = distance;
|
|
@@ -139,15 +127,15 @@ function nearestOnOutline(outline, point) {
|
|
|
139
127
|
}
|
|
140
128
|
/** Where the segment from `first` to `second` meets the segment from `a` to `b`, as the fraction of the first it is along; null when they do not meet. */
|
|
141
129
|
function segmentMeeting(first, second, a, b) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
130
|
+
const directionX = second.x - first.x;
|
|
131
|
+
const directionY = second.y - first.y;
|
|
132
|
+
const edgeX = b.x - a.x;
|
|
133
|
+
const edgeY = b.y - a.y;
|
|
134
|
+
const denominator = directionX * edgeY - directionY * edgeX;
|
|
147
135
|
if (Math.abs(denominator) < 1e-12)
|
|
148
136
|
return null;
|
|
149
|
-
|
|
150
|
-
|
|
137
|
+
const along = ((a.x - first.x) * edgeY - (a.y - first.y) * edgeX) / denominator;
|
|
138
|
+
const across = ((a.x - first.x) * directionY - (a.y - first.y) * directionX) / denominator;
|
|
151
139
|
return along >= 0 && along <= 1 && across >= 0 && across <= 1 ? along : null;
|
|
152
140
|
}
|
|
153
141
|
/**
|
|
@@ -155,27 +143,26 @@ function segmentMeeting(first, second, a, b) {
|
|
|
155
143
|
* point is inside the shape, or on it: the line meets nothing then.
|
|
156
144
|
*/
|
|
157
145
|
function crossingTowardMiddle(outline, from) {
|
|
158
|
-
|
|
159
|
-
|
|
146
|
+
const { width, height } = outline;
|
|
147
|
+
const middle = { x: width / 2, y: height / 2 };
|
|
160
148
|
if (outline.kind === 'lines') {
|
|
161
|
-
|
|
162
|
-
for (
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
var along = segmentMeeting(from, middle, line.points[index], line.points[(index + 1) % line.points.length]);
|
|
149
|
+
let nearestAlong = Infinity;
|
|
150
|
+
for (const line of outline.lines) {
|
|
151
|
+
const last = line.closed ? line.points.length : line.points.length - 1;
|
|
152
|
+
for (let index = 0; index < last; index += 1) {
|
|
153
|
+
const along = segmentMeeting(from, middle, line.points[index], line.points[(index + 1) % line.points.length]);
|
|
167
154
|
if (along !== null && along < nearestAlong)
|
|
168
155
|
nearestAlong = along;
|
|
169
156
|
}
|
|
170
157
|
}
|
|
171
158
|
return nearestAlong === Infinity ? null : { x: from.x + (middle.x - from.x) * nearestAlong, y: from.y + (middle.y - from.y) * nearestAlong };
|
|
172
159
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
160
|
+
const radiusX = Math.max(width / 2, 1e-6);
|
|
161
|
+
const radiusY = Math.max(height / 2, 1e-6);
|
|
162
|
+
const offsetX = (from.x - middle.x) / radiusX;
|
|
163
|
+
const offsetY = (from.y - middle.y) / radiusY;
|
|
177
164
|
// How many times the box (or the ellipse) the point is from the middle: past 1 it is outside.
|
|
178
|
-
|
|
165
|
+
const reach = outline.kind === 'box' ? Math.max(Math.abs(offsetX), Math.abs(offsetY)) : Math.hypot(offsetX, offsetY);
|
|
179
166
|
if (reach <= 1 + 1e-9)
|
|
180
167
|
return null;
|
|
181
168
|
return { x: middle.x + (from.x - middle.x) / reach, y: middle.y + (from.y - middle.y) / reach };
|
|
@@ -189,7 +176,7 @@ export function centerOfShape(shape) {
|
|
|
189
176
|
}
|
|
190
177
|
/** The point of the outline of a shape that an `edge` tie rests on, in the world. */
|
|
191
178
|
export function edgePointOf(shape, anchor) {
|
|
192
|
-
|
|
179
|
+
const local = { x: anchor.x * shape.width, y: anchor.y * shape.height };
|
|
193
180
|
// Put back on the outline: a shape that was rebuilt (a rounded corner, a regenerated path) may have moved it a little.
|
|
194
181
|
return toWorld(shape, nearestOnOutline(outlineOf(shape), local));
|
|
195
182
|
}
|
|
@@ -198,15 +185,15 @@ export function edgePointOf(shape, anchor) {
|
|
|
198
185
|
* from inside the shape (or from the middle), so that it still points at it.
|
|
199
186
|
*/
|
|
200
187
|
export function centerCrossingOf(shape, aim) {
|
|
201
|
-
|
|
202
|
-
|
|
188
|
+
const local = worldToBoxLocal(shape, aim);
|
|
189
|
+
const crossing = local ? crossingTowardMiddle(outlineOf(shape), local) : null;
|
|
203
190
|
return crossing ? toWorld(shape, crossing) : centerOfShape(shape);
|
|
204
191
|
}
|
|
205
192
|
/** The shape an arrow's tie holds to, or null when there is none to hold to (it is gone, or an arrow cannot be tied to it). */
|
|
206
193
|
function targetOf(arrow, binding, lookup) {
|
|
207
194
|
if (!binding || binding.targetId === arrow.id)
|
|
208
195
|
return null;
|
|
209
|
-
|
|
196
|
+
const target = lookup(binding.targetId);
|
|
210
197
|
return target && canBeTiedTo(target) ? target : null;
|
|
211
198
|
}
|
|
212
199
|
/**
|
|
@@ -216,14 +203,13 @@ function targetOf(arrow, binding, lookup) {
|
|
|
216
203
|
export function positionBoundArrow(arrow, lookup) {
|
|
217
204
|
if (!hasBindings(arrow.arrow))
|
|
218
205
|
return arrow;
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
for (
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
var target = targetOf(arrow, binding, lookup);
|
|
206
|
+
const current = arrowPointsInWorld(arrow);
|
|
207
|
+
const targets = { start: null, end: null };
|
|
208
|
+
const bindings = { start: null, end: null };
|
|
209
|
+
const cut = {};
|
|
210
|
+
for (const end of ARROW_ENDS) {
|
|
211
|
+
const binding = bindingOfEnd(arrow.arrow, end);
|
|
212
|
+
const target = targetOf(arrow, binding, lookup);
|
|
227
213
|
if (binding && target) {
|
|
228
214
|
bindings[end] = binding;
|
|
229
215
|
targets[end] = target;
|
|
@@ -233,45 +219,42 @@ export function positionBoundArrow(arrow, lookup) {
|
|
|
233
219
|
}
|
|
234
220
|
}
|
|
235
221
|
// What the line to the far end is drawn to: its place on the outline, the middle of its shape, or its own place when it is free.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
222
|
+
const lookedAt = (end) => {
|
|
223
|
+
const target = targets[end];
|
|
224
|
+
const binding = bindings[end];
|
|
239
225
|
if (!target || !binding)
|
|
240
226
|
return current[end];
|
|
241
227
|
return binding.kind === 'edge' ? edgePointOf(target, binding.anchor) : centerOfShape(target);
|
|
242
228
|
};
|
|
243
|
-
|
|
244
|
-
for (
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
var binding = bindings[end];
|
|
229
|
+
const placed = { start: current.start, end: current.end };
|
|
230
|
+
for (const end of ARROW_ENDS) {
|
|
231
|
+
const target = targets[end];
|
|
232
|
+
const binding = bindings[end];
|
|
248
233
|
if (!target || !binding)
|
|
249
234
|
continue;
|
|
250
235
|
placed[end] = binding.kind === 'edge' ? edgePointOf(target, binding.anchor) : centerCrossingOf(target, lookedAt(end === 'start' ? 'end' : 'start'));
|
|
251
236
|
}
|
|
252
|
-
|
|
237
|
+
const moved = ARROW_ENDS.some((end) => distanceBetween(placed[end], current[end]) > SAME_PLACE);
|
|
253
238
|
if (!moved && Object.keys(cut).length === 0)
|
|
254
239
|
return arrow;
|
|
255
|
-
return reshapeArrow(arrow,
|
|
240
|
+
return reshapeArrow(arrow, { start: placed.start, end: placed.end, ...cut });
|
|
256
241
|
}
|
|
257
242
|
/**
|
|
258
243
|
* The scene with every tied arrow where its ties say. The very same array when none has to move (the engine runs this over every
|
|
259
244
|
* scene it is given, so that it must cost nothing when no arrow is tied, and must not make a change out of nothing).
|
|
260
245
|
*/
|
|
261
246
|
export function reconcileArrowBindings(scene) {
|
|
262
|
-
if (!scene.some(
|
|
247
|
+
if (!scene.some((object) => object.type === 'path' && !!object.arrow && hasBindings(object.arrow)))
|
|
263
248
|
return scene;
|
|
264
|
-
|
|
265
|
-
for (
|
|
266
|
-
var object = scene_1[_i];
|
|
249
|
+
const index = new Map();
|
|
250
|
+
for (const object of scene)
|
|
267
251
|
index.set(object.id, object);
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
var next = scene.map(function (object) {
|
|
252
|
+
const lookup = (id) => index.get(id);
|
|
253
|
+
let changed = false;
|
|
254
|
+
const next = scene.map((object) => {
|
|
272
255
|
if (object.type !== 'path' || !isArrow(object) || !hasBindings(object.arrow))
|
|
273
256
|
return object;
|
|
274
|
-
|
|
257
|
+
const placed = positionBoundArrow(object, lookup);
|
|
275
258
|
if (placed !== object)
|
|
276
259
|
changed = true;
|
|
277
260
|
return placed;
|
|
@@ -284,7 +267,7 @@ function edgeBandOf(shape, zoom) {
|
|
|
284
267
|
}
|
|
285
268
|
/** Whether a point is inside a shape, a hollow one included (the way a click is not: a click on the empty middle of a frame reaches what lies under it). */
|
|
286
269
|
function interiorContains(shape, point) {
|
|
287
|
-
|
|
270
|
+
const local = worldToBoxLocal(shape, point);
|
|
288
271
|
if (!local)
|
|
289
272
|
return false;
|
|
290
273
|
switch (shape.type) {
|
|
@@ -293,20 +276,20 @@ function interiorContains(shape, point) {
|
|
|
293
276
|
case 'ellipse':
|
|
294
277
|
return Math.hypot((local.x - shape.width / 2) / Math.max(shape.width / 2, 1e-6), (local.y - shape.height / 2) / Math.max(shape.height / 2, 1e-6)) <= 1;
|
|
295
278
|
case 'path':
|
|
296
|
-
return flattenedOutline(shape.d).some(
|
|
279
|
+
return flattenedOutline(shape.d).some((outline) => outline.points.length >= 3 && pointInPolygon(local, outline.points));
|
|
297
280
|
default:
|
|
298
281
|
return false;
|
|
299
282
|
}
|
|
300
283
|
}
|
|
301
284
|
/** The tie a pointer at a point makes on one shape it is over: on the outline within the band of it, at the middle further in. */
|
|
302
285
|
function tieOnShape(shape, point, zoom) {
|
|
303
|
-
|
|
286
|
+
const local = worldToBoxLocal(shape, point);
|
|
304
287
|
if (!local)
|
|
305
288
|
return null;
|
|
306
|
-
|
|
289
|
+
const nearest = nearestOnOutline(outlineOf(shape), local);
|
|
307
290
|
if (distanceBetween(local, nearest) <= edgeBandOf(shape, zoom)) {
|
|
308
|
-
|
|
309
|
-
return { binding: { targetId: shape.id, kind: 'edge', anchor
|
|
291
|
+
const anchor = { x: clampNumber(nearest.x / Math.max(shape.width, 1e-6), 0, 1), y: clampNumber(nearest.y / Math.max(shape.height, 1e-6), 0, 1) };
|
|
292
|
+
return { binding: { targetId: shape.id, kind: 'edge', anchor }, hint: { targetId: shape.id, kind: 'edge', point: toWorld(shape, nearest) } };
|
|
310
293
|
}
|
|
311
294
|
return { binding: { targetId: shape.id, kind: 'center' }, hint: { targetId: shape.id, kind: 'center', point: centerOfShape(shape) } };
|
|
312
295
|
}
|
|
@@ -317,26 +300,25 @@ function tieOnShape(shape, point, zoom) {
|
|
|
317
300
|
* middle of a hollow shape taken as the shape. Null when the pointer is over nothing of the kind.
|
|
318
301
|
*/
|
|
319
302
|
export function pickArrowBinding(scene, point, options) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
if (!canBeTiedTo(object) || ((_a = options.ignore) === null || _a === void 0 ? void 0 : _a.has(object.id)))
|
|
303
|
+
const zoom = Math.max(options.zoom, 1e-6);
|
|
304
|
+
const reach = ARROW_BIND_REACH_PIXELS / zoom;
|
|
305
|
+
for (let position = scene.length - 1; position >= 0; position -= 1) {
|
|
306
|
+
const object = scene[position];
|
|
307
|
+
if (!canBeTiedTo(object) || options.ignore?.has(object.id))
|
|
326
308
|
continue;
|
|
327
309
|
if (!shapeContainsPoint(object, point, reach))
|
|
328
310
|
continue;
|
|
329
|
-
|
|
311
|
+
const pick = tieOnShape(object, point, zoom);
|
|
330
312
|
if (pick)
|
|
331
313
|
return pick;
|
|
332
314
|
}
|
|
333
|
-
for (
|
|
334
|
-
|
|
335
|
-
if (!canBeTiedTo(object) ||
|
|
315
|
+
for (let position = scene.length - 1; position >= 0; position -= 1) {
|
|
316
|
+
const object = scene[position];
|
|
317
|
+
if (!canBeTiedTo(object) || options.ignore?.has(object.id))
|
|
336
318
|
continue;
|
|
337
319
|
if (!interiorContains(object, point))
|
|
338
320
|
continue;
|
|
339
|
-
|
|
321
|
+
const pick = tieOnShape(object, point, zoom);
|
|
340
322
|
if (pick)
|
|
341
323
|
return pick;
|
|
342
324
|
}
|
|
@@ -7,23 +7,11 @@
|
|
|
7
7
|
* every function that makes a spec writes `startBinding` and `endBinding` only while they are set, so an arrow that is tied to
|
|
8
8
|
* nothing is the very same record it was before ties existed.
|
|
9
9
|
*/
|
|
10
|
-
var __assign = (this && this.__assign) || function () {
|
|
11
|
-
__assign = Object.assign || function(t) {
|
|
12
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
13
|
-
s = arguments[i];
|
|
14
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
15
|
-
t[p] = s[p];
|
|
16
|
-
}
|
|
17
|
-
return t;
|
|
18
|
-
};
|
|
19
|
-
return __assign.apply(this, arguments);
|
|
20
|
-
};
|
|
21
10
|
/** The two ends of an arrow, the tail first. */
|
|
22
|
-
export
|
|
11
|
+
export const ARROW_ENDS = Object.freeze(['start', 'end']);
|
|
23
12
|
/** The tie of one end of an arrow, or null while that end is free. */
|
|
24
13
|
export function bindingOfEnd(spec, end) {
|
|
25
|
-
|
|
26
|
-
return (_a = (end === 'start' ? spec.startBinding : spec.endBinding)) !== null && _a !== void 0 ? _a : null;
|
|
14
|
+
return (end === 'start' ? spec.startBinding : spec.endBinding) ?? null;
|
|
27
15
|
}
|
|
28
16
|
/** Whether either end of an arrow is tied. */
|
|
29
17
|
export function hasBindings(spec) {
|
|
@@ -42,12 +30,11 @@ export function sameBinding(first, second) {
|
|
|
42
30
|
}
|
|
43
31
|
/** The spec with its ties changed. The same spec when the change says what it says already. */
|
|
44
32
|
export function withBindings(spec, change) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
var end = 'endBinding' in change ? ((_c = change.endBinding) !== null && _c !== void 0 ? _c : null) : ((_d = spec.endBinding) !== null && _d !== void 0 ? _d : null);
|
|
33
|
+
const start = 'startBinding' in change ? (change.startBinding ?? null) : (spec.startBinding ?? null);
|
|
34
|
+
const end = 'endBinding' in change ? (change.endBinding ?? null) : (spec.endBinding ?? null);
|
|
48
35
|
if (sameBinding(start, spec.startBinding) && sameBinding(end, spec.endBinding))
|
|
49
36
|
return spec;
|
|
50
|
-
return
|
|
37
|
+
return { start: spec.start, end: spec.end, bend: spec.bend, ...(start ? { startBinding: start } : {}), ...(end ? { endBinding: end } : {}) };
|
|
51
38
|
}
|
|
52
39
|
/** Whether an arrow can be tied to this object: a shape that has an outline, and is not itself a line (an arrow, a brush stroke) or a group. */
|
|
53
40
|
export function canBeTiedTo(object) {
|
|
@@ -70,17 +57,17 @@ function arrowSpecOf(object) {
|
|
|
70
57
|
}
|
|
71
58
|
/** The same arrow with its spec replaced, or the very same object when the spec did not change. */
|
|
72
59
|
function withSpec(object, spec) {
|
|
73
|
-
return spec === object.arrow ? object :
|
|
60
|
+
return spec === object.arrow ? object : { ...object, arrow: spec };
|
|
74
61
|
}
|
|
75
62
|
/**
|
|
76
63
|
* An arrow that is carried, stretched or turned together with some shapes: the ties to shapes that are not among them are cut (an
|
|
77
64
|
* arrow that is moved by itself comes off the shapes it was tied to, the way it does in tldraw). The same object when nothing is cut.
|
|
78
65
|
*/
|
|
79
66
|
export function releaseUnmovedEnds(object, moving) {
|
|
80
|
-
|
|
67
|
+
const spec = arrowSpecOf(object);
|
|
81
68
|
if (!spec || !hasBindings(spec))
|
|
82
69
|
return object;
|
|
83
|
-
|
|
70
|
+
const change = {};
|
|
84
71
|
if (spec.startBinding && !moving.has(spec.startBinding.targetId))
|
|
85
72
|
change.startBinding = null;
|
|
86
73
|
if (spec.endBinding && !moving.has(spec.endBinding.targetId))
|
|
@@ -92,12 +79,12 @@ export function releaseUnmovedEnds(object, moving) {
|
|
|
92
79
|
* alone is a free arrow). `idMap` says which id was copied to which.
|
|
93
80
|
*/
|
|
94
81
|
export function remapEnds(object, idMap) {
|
|
95
|
-
|
|
82
|
+
const spec = arrowSpecOf(object);
|
|
96
83
|
if (!spec || !hasBindings(spec))
|
|
97
84
|
return object;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return binding && copyId ?
|
|
85
|
+
const remap = (binding) => {
|
|
86
|
+
const copyId = binding ? idMap.get(binding.targetId) : undefined;
|
|
87
|
+
return binding && copyId ? { ...binding, targetId: copyId } : null;
|
|
101
88
|
};
|
|
102
89
|
return withSpec(object, withBindings(spec, { startBinding: remap(spec.startBinding), endBinding: remap(spec.endBinding) }));
|
|
103
90
|
}
|
|
@@ -107,32 +94,30 @@ export function remapEnds(object, idMap) {
|
|
|
107
94
|
* array when nothing is cut.
|
|
108
95
|
*/
|
|
109
96
|
export function dropDanglingBindings(scene) {
|
|
110
|
-
if (!scene.some(
|
|
111
|
-
|
|
97
|
+
if (!scene.some((object) => {
|
|
98
|
+
const spec = arrowSpecOf(object);
|
|
112
99
|
return !!spec && hasBindings(spec);
|
|
113
100
|
})) {
|
|
114
101
|
return scene;
|
|
115
102
|
}
|
|
116
|
-
|
|
117
|
-
for (
|
|
118
|
-
var object = scene_1[_i];
|
|
103
|
+
const index = new Map();
|
|
104
|
+
for (const object of scene)
|
|
119
105
|
index.set(object.id, object);
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
var spec = arrowSpecOf(object);
|
|
106
|
+
let changed = false;
|
|
107
|
+
const next = scene.map((object) => {
|
|
108
|
+
const spec = arrowSpecOf(object);
|
|
124
109
|
if (!spec || !hasBindings(spec))
|
|
125
110
|
return object;
|
|
126
|
-
|
|
127
|
-
|
|
111
|
+
const holds = (binding) => {
|
|
112
|
+
const target = binding ? index.get(binding.targetId) : undefined;
|
|
128
113
|
return !!binding && !!target && target.id !== object.id && canBeTiedTo(target);
|
|
129
114
|
};
|
|
130
|
-
|
|
115
|
+
const change = {};
|
|
131
116
|
if (spec.startBinding && !holds(spec.startBinding))
|
|
132
117
|
change.startBinding = null;
|
|
133
118
|
if (spec.endBinding && !holds(spec.endBinding))
|
|
134
119
|
change.endBinding = null;
|
|
135
|
-
|
|
120
|
+
const cut = withSpec(object, withBindings(spec, change));
|
|
136
121
|
if (cut !== object)
|
|
137
122
|
changed = true;
|
|
138
123
|
return cut;
|