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