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