@widgetic/canvas 0.5.40 → 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 +4 -0
- 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.js +18 -17
- package/dist/canvas/eventScope.js +14 -10
- package/dist/canvas/host/CanvasHost.d.ts +6 -0
- package/dist/canvas/host/CanvasHost.js +1556 -1267
- 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/CanvasHint.svelte +16 -3
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +2 -17
- package/dist/canvas/ui/CanvasToolbar.svelte +7 -7
- package/dist/canvas/ui/ToolButton.svelte +11 -3
- package/dist/canvas/ui/ToolButton.svelte.d.ts +2 -0
- package/dist/canvas/ui/colorModel.js +32 -21
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +18 -18
- package/dist/canvas/ui/floatingLayer.js +6 -5
- package/dist/canvas/ui/keyHints.js +11 -9
- package/dist/canvas/ui/popoverSide.js +3 -2
- package/dist/canvas/ui/selectionTraits.js +40 -34
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +1 -1
- package/dist/canvas/ui/sizeLabelVisibility.js +1 -1
- package/dist/canvas/view/SelectionOverlay.svelte +33 -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 +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- 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
|
@@ -4,14 +4,15 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { normalizeDegrees, normalizeSignedDegrees } from './matrix.js';
|
|
6
6
|
/** How long the upright animation runs, in milliseconds. */
|
|
7
|
-
export
|
|
7
|
+
export var ROTATION_RESET_DURATION_MS = 220;
|
|
8
8
|
/** Ease-out progress in [0, 1] for `elapsed` ms since the animation started. */
|
|
9
|
-
export function rotationResetProgress(elapsed, duration
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
export function rotationResetProgress(elapsed, duration) {
|
|
10
|
+
if (duration === void 0) { duration = ROTATION_RESET_DURATION_MS; }
|
|
11
|
+
var t = Math.min(1, Math.max(0, elapsed / duration));
|
|
12
|
+
return 1 - Math.pow((1 - t), 3);
|
|
12
13
|
}
|
|
13
14
|
/** Rotation in degrees after `progress` of the upright animation from `startRotation`. */
|
|
14
15
|
export function rotationAfterResetProgress(startRotation, progress) {
|
|
15
|
-
|
|
16
|
+
var delta = -normalizeSignedDegrees(startRotation);
|
|
16
17
|
return normalizeDegrees(startRotation + delta * progress);
|
|
17
18
|
}
|
|
@@ -62,6 +62,9 @@ export declare function movingShapeIds(scene: Scene, ids: Iterable<ObjectId>): S
|
|
|
62
62
|
/**
|
|
63
63
|
* Moves the shapes with the given ids by a distance. A drag carries the same shapes at every step: it asks `movingShapeIds`
|
|
64
64
|
* once and calls this at every move, so no step walks the groups of the board again to find out who is carried.
|
|
65
|
+
*
|
|
66
|
+
* An arrow that is carried comes off the shapes it is tied to, unless they are carried with it (an arrow moved by itself is let go,
|
|
67
|
+
* the way it is in tldraw); the arrows of the shapes that are carried while they stay follow them, in `reconcileArrowBindings`.
|
|
65
68
|
*/
|
|
66
69
|
export declare function translateShapes(scene: Scene, moving: ReadonlySet<ObjectId>, deltaX: number, deltaY: number): Scene;
|
|
67
70
|
/** Moves the shapes drawn by the given objects by a distance. */
|
|
@@ -9,19 +9,43 @@
|
|
|
9
9
|
* which keeps one rule true everywhere: a group comes first, directly followed by its members, and the
|
|
10
10
|
* paint order of the board is the order of the list.
|
|
11
11
|
*/
|
|
12
|
+
var __assign = (this && this.__assign) || function () {
|
|
13
|
+
__assign = Object.assign || function(t) {
|
|
14
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
15
|
+
s = arguments[i];
|
|
16
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
17
|
+
t[p] = s[p];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
return __assign.apply(this, arguments);
|
|
22
|
+
};
|
|
23
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
24
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
25
|
+
if (ar || !(i in from)) {
|
|
26
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
27
|
+
ar[i] = from[i];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
31
|
+
};
|
|
32
|
+
import { releaseUnmovedEnds, remapEnds } from './arrowBindingRecords.js';
|
|
12
33
|
import { brushOverflow, isBrushStroke } from './brush.js';
|
|
13
34
|
import { rotatedBoxBounds, unionRects, inflateRect } from './geometry.js';
|
|
14
35
|
import { createGroupObject, createObjectId } from './defaults.js';
|
|
15
36
|
import { isBoxedObject, isPaintedObject } from './types.js';
|
|
16
37
|
// ──────────────────────────────────────────────────────────── lookup
|
|
17
38
|
export function indexScene(scene) {
|
|
18
|
-
|
|
19
|
-
for (
|
|
39
|
+
var index = new Map();
|
|
40
|
+
for (var _i = 0, scene_1 = scene; _i < scene_1.length; _i++) {
|
|
41
|
+
var object = scene_1[_i];
|
|
20
42
|
index.set(object.id, object);
|
|
43
|
+
}
|
|
21
44
|
return index;
|
|
22
45
|
}
|
|
23
46
|
export function findObject(scene, id) {
|
|
24
|
-
for (
|
|
47
|
+
for (var _i = 0, scene_2 = scene; _i < scene_2.length; _i++) {
|
|
48
|
+
var object = scene_2[_i];
|
|
25
49
|
if (object.id === id)
|
|
26
50
|
return object;
|
|
27
51
|
}
|
|
@@ -29,14 +53,15 @@ export function findObject(scene, id) {
|
|
|
29
53
|
}
|
|
30
54
|
/** The direct members of a group, in paint order. */
|
|
31
55
|
export function childrenOf(scene, groupId) {
|
|
32
|
-
return scene.filter((object)
|
|
56
|
+
return scene.filter(function (object) { return object.parentId === groupId; });
|
|
33
57
|
}
|
|
34
58
|
/** Every object below a group, at any depth, in list order. */
|
|
35
59
|
export function descendantsOf(scene, groupId) {
|
|
36
|
-
|
|
37
|
-
|
|
60
|
+
var included = new Set([groupId]);
|
|
61
|
+
var result = [];
|
|
38
62
|
// A group precedes its members in the list, so one pass finds them all.
|
|
39
|
-
for (
|
|
63
|
+
for (var _i = 0, scene_3 = scene; _i < scene_3.length; _i++) {
|
|
64
|
+
var object = scene_3[_i];
|
|
40
65
|
if (object.parentId !== null && included.has(object.parentId)) {
|
|
41
66
|
included.add(object.id);
|
|
42
67
|
result.push(object);
|
|
@@ -46,9 +71,10 @@ export function descendantsOf(scene, groupId) {
|
|
|
46
71
|
}
|
|
47
72
|
/** The ids given, plus every id below a group among them. */
|
|
48
73
|
export function withDescendantIds(scene, ids) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
for (
|
|
74
|
+
var wanted = new Set(ids);
|
|
75
|
+
var included = new Set(wanted);
|
|
76
|
+
for (var _i = 0, scene_4 = scene; _i < scene_4.length; _i++) {
|
|
77
|
+
var object = scene_4[_i];
|
|
52
78
|
if (object.parentId !== null && included.has(object.parentId))
|
|
53
79
|
included.add(object.id);
|
|
54
80
|
}
|
|
@@ -56,9 +82,10 @@ export function withDescendantIds(scene, ids) {
|
|
|
56
82
|
}
|
|
57
83
|
/** The shapes drawn by the given objects: a shape is itself, a group is its members at any depth. Unique, in paint order. */
|
|
58
84
|
export function leafShapesOf(scene, ids) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
for (
|
|
85
|
+
var wanted = withDescendantIds(scene, ids);
|
|
86
|
+
var shapes = [];
|
|
87
|
+
for (var _i = 0, scene_5 = scene; _i < scene_5.length; _i++) {
|
|
88
|
+
var object = scene_5[_i];
|
|
62
89
|
if (wanted.has(object.id) && isBoxedObject(object))
|
|
63
90
|
shapes.push(object);
|
|
64
91
|
}
|
|
@@ -66,25 +93,26 @@ export function leafShapesOf(scene, ids) {
|
|
|
66
93
|
}
|
|
67
94
|
/** The group nearest to the board that contains the object, or the object itself when it is not in a group. */
|
|
68
95
|
export function topLevelAncestorId(index, id) {
|
|
69
|
-
|
|
96
|
+
var _a, _b;
|
|
97
|
+
var current = index.get(id);
|
|
70
98
|
// Most objects sit on the board itself, with nothing to climb and nothing to keep track of (a marquee asks for all of them
|
|
71
99
|
// at every step of its drag).
|
|
72
100
|
if (!current || current.parentId === null)
|
|
73
|
-
return current
|
|
74
|
-
|
|
101
|
+
return (_a = current === null || current === void 0 ? void 0 : current.id) !== null && _a !== void 0 ? _a : id;
|
|
102
|
+
var visited = new Set();
|
|
75
103
|
while (current && current.parentId !== null && !visited.has(current.id)) {
|
|
76
104
|
visited.add(current.id);
|
|
77
|
-
|
|
105
|
+
var parent = index.get(current.parentId);
|
|
78
106
|
if (!parent)
|
|
79
107
|
break;
|
|
80
108
|
current = parent;
|
|
81
109
|
}
|
|
82
|
-
return current
|
|
110
|
+
return (_b = current === null || current === void 0 ? void 0 : current.id) !== null && _b !== void 0 ? _b : id;
|
|
83
111
|
}
|
|
84
112
|
/** True when `id` is `ancestorId` or lies below it. */
|
|
85
113
|
export function isWithin(index, id, ancestorId) {
|
|
86
|
-
|
|
87
|
-
|
|
114
|
+
var current = index.get(id);
|
|
115
|
+
var visited = new Set();
|
|
88
116
|
while (current && !visited.has(current.id)) {
|
|
89
117
|
if (current.id === ancestorId)
|
|
90
118
|
return true;
|
|
@@ -99,7 +127,7 @@ export function sameIds(first, second) {
|
|
|
99
127
|
return true;
|
|
100
128
|
if (first.length !== second.length)
|
|
101
129
|
return false;
|
|
102
|
-
for (
|
|
130
|
+
for (var position = 0; position < first.length; position += 1) {
|
|
103
131
|
if (first[position] !== second[position])
|
|
104
132
|
return false;
|
|
105
133
|
}
|
|
@@ -107,26 +135,29 @@ export function sameIds(first, second) {
|
|
|
107
135
|
}
|
|
108
136
|
/** Drops ids that are not on the board, repeats, and members whose group is selected too. */
|
|
109
137
|
export function normalizeSelection(scene, ids) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
138
|
+
var _a, _b;
|
|
139
|
+
var index = indexScene(scene);
|
|
140
|
+
var wanted = new Set();
|
|
141
|
+
for (var _i = 0, ids_1 = ids; _i < ids_1.length; _i++) {
|
|
142
|
+
var id = ids_1[_i];
|
|
113
143
|
if (index.has(id))
|
|
114
144
|
wanted.add(id);
|
|
115
145
|
}
|
|
116
|
-
|
|
117
|
-
for (
|
|
146
|
+
var result = [];
|
|
147
|
+
for (var _c = 0, scene_6 = scene; _c < scene_6.length; _c++) {
|
|
148
|
+
var object = scene_6[_c];
|
|
118
149
|
if (!wanted.has(object.id))
|
|
119
150
|
continue;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
151
|
+
var covered = false;
|
|
152
|
+
var parentId = object.parentId;
|
|
153
|
+
var visited = new Set();
|
|
123
154
|
while (parentId !== null && !visited.has(parentId)) {
|
|
124
155
|
if (wanted.has(parentId)) {
|
|
125
156
|
covered = true;
|
|
126
157
|
break;
|
|
127
158
|
}
|
|
128
159
|
visited.add(parentId);
|
|
129
|
-
parentId = index.get(parentId)
|
|
160
|
+
parentId = (_b = (_a = index.get(parentId)) === null || _a === void 0 ? void 0 : _a.parentId) !== null && _b !== void 0 ? _b : null;
|
|
130
161
|
}
|
|
131
162
|
if (!covered)
|
|
132
163
|
result.push(object.id);
|
|
@@ -140,7 +171,7 @@ export function shapeBounds(shape) {
|
|
|
140
171
|
}
|
|
141
172
|
/** The same, grown by what the stroke adds outside the outline: what a thumbnail or a screenshot must include. */
|
|
142
173
|
export function shapeVisualBounds(shape) {
|
|
143
|
-
|
|
174
|
+
var bounds = rotatedBoxBounds(shape);
|
|
144
175
|
// A brush stroke's outline reaches past the box of its line by up to half the width of the brush at its fullest.
|
|
145
176
|
if (isBrushStroke(shape))
|
|
146
177
|
return inflateRect(bounds, brushOverflow(shape));
|
|
@@ -155,8 +186,9 @@ export function boundsOfObjects(scene, ids) {
|
|
|
155
186
|
}
|
|
156
187
|
/** The rectangle around every shape on the board, strokes included. Null for an empty board. */
|
|
157
188
|
export function boardVisualBounds(scene) {
|
|
158
|
-
|
|
159
|
-
for (
|
|
189
|
+
var rectangles = [];
|
|
190
|
+
for (var _i = 0, scene_7 = scene; _i < scene_7.length; _i++) {
|
|
191
|
+
var object = scene_7[_i];
|
|
160
192
|
if (isBoxedObject(object))
|
|
161
193
|
rectangles.push(shapeVisualBounds(object));
|
|
162
194
|
}
|
|
@@ -164,15 +196,16 @@ export function boardVisualBounds(scene) {
|
|
|
164
196
|
}
|
|
165
197
|
// ──────────────────────────────────────────────────────────── tree
|
|
166
198
|
export function buildSceneTree(scene) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
for (
|
|
199
|
+
var nodes = new Map();
|
|
200
|
+
var roots = [];
|
|
201
|
+
for (var _i = 0, scene_8 = scene; _i < scene_8.length; _i++) {
|
|
202
|
+
var object = scene_8[_i];
|
|
170
203
|
// A repeated id is a corrupt file; the first record wins.
|
|
171
204
|
if (nodes.has(object.id))
|
|
172
205
|
continue;
|
|
173
|
-
|
|
206
|
+
var node = { object: object, children: [] };
|
|
174
207
|
nodes.set(object.id, node);
|
|
175
|
-
|
|
208
|
+
var parent = object.parentId !== null ? nodes.get(object.parentId) : undefined;
|
|
176
209
|
// A group is always before its members. An object whose group is missing (or comes later) goes to the top.
|
|
177
210
|
if (parent && parent.object.type === 'group')
|
|
178
211
|
parent.children.push(node);
|
|
@@ -183,23 +216,28 @@ export function buildSceneTree(scene) {
|
|
|
183
216
|
}
|
|
184
217
|
/** Writes a tree back as a list, setting every parent id to the node it hangs from. */
|
|
185
218
|
export function flattenSceneTree(roots) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
219
|
+
var result = [];
|
|
220
|
+
var visit = function (node, parentId) {
|
|
221
|
+
var object = node.object.parentId === parentId ? node.object : __assign(__assign({}, node.object), { parentId: parentId });
|
|
189
222
|
result.push(object);
|
|
190
|
-
for (
|
|
223
|
+
for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
|
|
224
|
+
var child = _a[_i];
|
|
191
225
|
visit(child, object.id);
|
|
226
|
+
}
|
|
192
227
|
};
|
|
193
|
-
for (
|
|
228
|
+
for (var _i = 0, roots_1 = roots; _i < roots_1.length; _i++) {
|
|
229
|
+
var root = roots_1[_i];
|
|
194
230
|
visit(root, null);
|
|
231
|
+
}
|
|
195
232
|
return result;
|
|
196
233
|
}
|
|
197
234
|
/** Repairs a list of records into a valid scene: unique ids, groups before their members, no empty groups. */
|
|
198
235
|
export function normalizeScene(scene) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
for (
|
|
236
|
+
var roots = buildSceneTree(scene);
|
|
237
|
+
var withoutEmptyGroups = function (nodes) {
|
|
238
|
+
var kept = [];
|
|
239
|
+
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
|
|
240
|
+
var node = nodes_1[_i];
|
|
203
241
|
node.children = withoutEmptyGroups(node.children);
|
|
204
242
|
if (node.object.type === 'group' && node.children.length === 0)
|
|
205
243
|
continue;
|
|
@@ -210,10 +248,11 @@ export function normalizeScene(scene) {
|
|
|
210
248
|
return flattenSceneTree(withoutEmptyGroups(roots));
|
|
211
249
|
}
|
|
212
250
|
function findNode(roots, id) {
|
|
213
|
-
for (
|
|
251
|
+
for (var _i = 0, roots_2 = roots; _i < roots_2.length; _i++) {
|
|
252
|
+
var root = roots_2[_i];
|
|
214
253
|
if (root.object.id === id)
|
|
215
254
|
return root;
|
|
216
|
-
|
|
255
|
+
var found = findNode(root.children, id);
|
|
217
256
|
if (found)
|
|
218
257
|
return found;
|
|
219
258
|
}
|
|
@@ -221,11 +260,11 @@ function findNode(roots, id) {
|
|
|
221
260
|
}
|
|
222
261
|
/** The list of siblings that holds a node, and the node's place in it. */
|
|
223
262
|
function findSiblingList(roots, id) {
|
|
224
|
-
|
|
225
|
-
for (
|
|
263
|
+
var search = function (siblings, parent) {
|
|
264
|
+
for (var position = 0; position < siblings.length; position += 1) {
|
|
226
265
|
if (siblings[position].object.id === id)
|
|
227
|
-
return { siblings, position, parent };
|
|
228
|
-
|
|
266
|
+
return { siblings: siblings, position: position, parent: parent };
|
|
267
|
+
var inside = search(siblings[position].children, siblings[position]);
|
|
229
268
|
if (inside)
|
|
230
269
|
return inside;
|
|
231
270
|
}
|
|
@@ -235,14 +274,14 @@ function findSiblingList(roots, id) {
|
|
|
235
274
|
}
|
|
236
275
|
// ──────────────────────────────────────────────────────────── edits
|
|
237
276
|
export function addObjects(scene, objects) {
|
|
238
|
-
return objects.length === 0 ? [
|
|
277
|
+
return objects.length === 0 ? __spreadArray([], scene, true) : __spreadArray(__spreadArray([], scene, true), objects, true);
|
|
239
278
|
}
|
|
240
279
|
/** Removes the objects and everything below them, then any group left with no members. */
|
|
241
280
|
export function removeObjects(scene, ids) {
|
|
242
|
-
|
|
281
|
+
var doomed = withDescendantIds(scene, ids);
|
|
243
282
|
if (doomed.size === 0)
|
|
244
283
|
return scene;
|
|
245
|
-
|
|
284
|
+
var kept = scene.filter(function (object) { return !doomed.has(object.id); });
|
|
246
285
|
if (kept.length === scene.length)
|
|
247
286
|
return scene;
|
|
248
287
|
return normalizeScene(kept);
|
|
@@ -253,13 +292,14 @@ export function patchObject(scene, id, patch) {
|
|
|
253
292
|
}
|
|
254
293
|
/** Changes several objects at once, each with its own patch. */
|
|
255
294
|
export function patchObjects(scene, patches) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
295
|
+
var changed = false;
|
|
296
|
+
var next = scene.map(function (object) {
|
|
297
|
+
var patch = patches.get(object.id);
|
|
259
298
|
if (!patch)
|
|
260
299
|
return object;
|
|
261
|
-
|
|
262
|
-
for (
|
|
300
|
+
var differs = false;
|
|
301
|
+
for (var _i = 0, _a = Object.keys(patch); _i < _a.length; _i++) {
|
|
302
|
+
var key = _a[_i];
|
|
263
303
|
if (object[key] !== patch[key]) {
|
|
264
304
|
differs = true;
|
|
265
305
|
break;
|
|
@@ -268,35 +308,40 @@ export function patchObjects(scene, patches) {
|
|
|
268
308
|
if (!differs)
|
|
269
309
|
return object;
|
|
270
310
|
changed = true;
|
|
271
|
-
return {
|
|
311
|
+
return __assign(__assign({}, object), patch);
|
|
272
312
|
});
|
|
273
313
|
return changed ? next : scene;
|
|
274
314
|
}
|
|
275
315
|
/** Replaces one record by another (its id stays the one of the record it replaces). */
|
|
276
316
|
export function replaceObject(scene, id, replacement) {
|
|
277
|
-
|
|
278
|
-
|
|
317
|
+
var changed = false;
|
|
318
|
+
var next = scene.map(function (object) {
|
|
279
319
|
if (object.id !== id)
|
|
280
320
|
return object;
|
|
281
321
|
changed = true;
|
|
282
|
-
return {
|
|
322
|
+
return __assign(__assign({}, replacement), { id: id, parentId: object.parentId });
|
|
283
323
|
});
|
|
284
324
|
return changed ? next : scene;
|
|
285
325
|
}
|
|
286
326
|
/** The ids of the shapes drawn by the given objects (a group is its members at any depth): who moves when they are carried. */
|
|
287
327
|
export function movingShapeIds(scene, ids) {
|
|
288
|
-
return new Set(leafShapesOf(scene, ids).map((shape)
|
|
328
|
+
return new Set(leafShapesOf(scene, ids).map(function (shape) { return shape.id; }));
|
|
289
329
|
}
|
|
290
330
|
/**
|
|
291
331
|
* Moves the shapes with the given ids by a distance. A drag carries the same shapes at every step: it asks `movingShapeIds`
|
|
292
332
|
* once and calls this at every move, so no step walks the groups of the board again to find out who is carried.
|
|
333
|
+
*
|
|
334
|
+
* An arrow that is carried comes off the shapes it is tied to, unless they are carried with it (an arrow moved by itself is let go,
|
|
335
|
+
* the way it is in tldraw); the arrows of the shapes that are carried while they stay follow them, in `reconcileArrowBindings`.
|
|
293
336
|
*/
|
|
294
337
|
export function translateShapes(scene, moving, deltaX, deltaY) {
|
|
295
338
|
if ((deltaX === 0 && deltaY === 0) || moving.size === 0)
|
|
296
339
|
return scene;
|
|
297
|
-
return scene.map(
|
|
298
|
-
|
|
299
|
-
|
|
340
|
+
return scene.map(function (object) {
|
|
341
|
+
return moving.has(object.id) && isBoxedObject(object)
|
|
342
|
+
? releaseUnmovedEnds(__assign(__assign({}, object), { x: object.x + deltaX, y: object.y + deltaY }), moving)
|
|
343
|
+
: object;
|
|
344
|
+
});
|
|
300
345
|
}
|
|
301
346
|
/** Moves the shapes drawn by the given objects by a distance. */
|
|
302
347
|
export function translateObjects(scene, ids, deltaX, deltaY) {
|
|
@@ -309,43 +354,50 @@ export function translateObjects(scene, ids, deltaX, deltaY) {
|
|
|
309
354
|
* group, never out of it).
|
|
310
355
|
*/
|
|
311
356
|
export function reorderObjects(scene, ids, mode) {
|
|
312
|
-
|
|
357
|
+
var _a;
|
|
358
|
+
var moved = new Set(normalizeSelection(scene, ids));
|
|
313
359
|
if (moved.size === 0)
|
|
314
360
|
return scene;
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
for (
|
|
318
|
-
|
|
361
|
+
var roots = buildSceneTree(scene);
|
|
362
|
+
var lists = new Map();
|
|
363
|
+
for (var _i = 0, moved_1 = moved; _i < moved_1.length; _i++) {
|
|
364
|
+
var id = moved_1[_i];
|
|
365
|
+
var found = findSiblingList(roots, id);
|
|
319
366
|
if (!found)
|
|
320
367
|
continue;
|
|
321
|
-
|
|
368
|
+
var set = (_a = lists.get(found.siblings)) !== null && _a !== void 0 ? _a : new Set();
|
|
322
369
|
set.add(id);
|
|
323
370
|
lists.set(found.siblings, set);
|
|
324
371
|
}
|
|
325
|
-
|
|
372
|
+
var _loop_1 = function (siblings, movedIds) {
|
|
373
|
+
var _d, _e;
|
|
326
374
|
if (mode === 'front' || mode === 'back') {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
siblings.splice(0, siblings.length,
|
|
375
|
+
var chosen = siblings.filter(function (node) { return movedIds.has(node.object.id); });
|
|
376
|
+
var others = siblings.filter(function (node) { return !movedIds.has(node.object.id); });
|
|
377
|
+
siblings.splice.apply(siblings, __spreadArray([0, siblings.length], (mode === 'front' ? __spreadArray(__spreadArray([], others, true), chosen, true) : __spreadArray(__spreadArray([], chosen, true), others, true)), false));
|
|
330
378
|
}
|
|
331
379
|
else if (mode === 'forward') {
|
|
332
380
|
// From the top down, a chosen node swaps with the unchosen one above it.
|
|
333
|
-
for (
|
|
381
|
+
for (var position = siblings.length - 2; position >= 0; position -= 1) {
|
|
334
382
|
if (movedIds.has(siblings[position].object.id) && !movedIds.has(siblings[position + 1].object.id)) {
|
|
335
|
-
[siblings[position], siblings[position
|
|
383
|
+
_d = [siblings[position + 1], siblings[position]], siblings[position] = _d[0], siblings[position + 1] = _d[1];
|
|
336
384
|
}
|
|
337
385
|
}
|
|
338
386
|
}
|
|
339
387
|
else {
|
|
340
|
-
for (
|
|
388
|
+
for (var position = 1; position < siblings.length; position += 1) {
|
|
341
389
|
if (movedIds.has(siblings[position].object.id) && !movedIds.has(siblings[position - 1].object.id)) {
|
|
342
|
-
[siblings[position], siblings[position
|
|
390
|
+
_e = [siblings[position - 1], siblings[position]], siblings[position] = _e[0], siblings[position - 1] = _e[1];
|
|
343
391
|
}
|
|
344
392
|
}
|
|
345
393
|
}
|
|
394
|
+
};
|
|
395
|
+
for (var _b = 0, lists_1 = lists; _b < lists_1.length; _b++) {
|
|
396
|
+
var _c = lists_1[_b], siblings = _c[0], movedIds = _c[1];
|
|
397
|
+
_loop_1(siblings, movedIds);
|
|
346
398
|
}
|
|
347
|
-
|
|
348
|
-
|
|
399
|
+
var next = flattenSceneTree(roots);
|
|
400
|
+
var unchanged = next.length === scene.length && next.every(function (object, position) { return object === scene[position]; });
|
|
349
401
|
return unchanged ? scene : next;
|
|
350
402
|
}
|
|
351
403
|
/**
|
|
@@ -354,59 +406,65 @@ export function reorderObjects(scene, ids, mode) {
|
|
|
354
406
|
* are grouped whole (a selection never tears a group apart).
|
|
355
407
|
* Returns null when there is nothing to group (fewer than two units).
|
|
356
408
|
*/
|
|
357
|
-
export function groupObjects(scene, ids, groupId
|
|
358
|
-
|
|
409
|
+
export function groupObjects(scene, ids, groupId) {
|
|
410
|
+
var _a, _b;
|
|
411
|
+
if (groupId === void 0) { groupId = createObjectId('group'); }
|
|
412
|
+
var selected = normalizeSelection(scene, ids);
|
|
359
413
|
if (selected.length < 2)
|
|
360
414
|
return null;
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
415
|
+
var index = indexScene(scene);
|
|
416
|
+
var sharedParent = selected.every(function (id) { var _a, _b; return ((_a = index.get(id)) === null || _a === void 0 ? void 0 : _a.parentId) === ((_b = index.get(selected[0])) === null || _b === void 0 ? void 0 : _b.parentId); });
|
|
417
|
+
var units = sharedParent
|
|
364
418
|
? selected
|
|
365
|
-
: Array.from(new Set(selected.map((id)
|
|
419
|
+
: Array.from(new Set(selected.map(function (id) { return topLevelAncestorId(index, id); })));
|
|
366
420
|
if (units.length < 2)
|
|
367
421
|
return null;
|
|
368
|
-
|
|
369
|
-
|
|
422
|
+
var roots = buildSceneTree(scene);
|
|
423
|
+
var unitNodes = units.map(function (id) { return findNode(roots, id); }).filter(function (node) { return node !== null; });
|
|
370
424
|
if (unitNodes.length < 2)
|
|
371
425
|
return null;
|
|
372
|
-
|
|
426
|
+
var firstFound = findSiblingList(roots, unitNodes[0].object.id);
|
|
373
427
|
if (!firstFound)
|
|
374
428
|
return null;
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
429
|
+
var siblings = firstFound.siblings;
|
|
430
|
+
var unitIds = new Set(unitNodes.map(function (node) { return node.object.id; }));
|
|
431
|
+
var inOrder = siblings.filter(function (node) { return unitIds.has(node.object.id); });
|
|
378
432
|
// The group takes the place of the highest unit among its siblings.
|
|
379
|
-
|
|
380
|
-
siblings.forEach((node, position)
|
|
433
|
+
var insertAt = 0;
|
|
434
|
+
siblings.forEach(function (node, position) {
|
|
381
435
|
if (unitIds.has(node.object.id))
|
|
382
436
|
insertAt = position;
|
|
383
437
|
});
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
438
|
+
var group = createGroupObject({ id: groupId, parentId: (_b = (_a = firstFound.parent) === null || _a === void 0 ? void 0 : _a.object.id) !== null && _b !== void 0 ? _b : null });
|
|
439
|
+
var groupNode = { object: group, children: inOrder };
|
|
440
|
+
var remaining = siblings.filter(function (node) { return !unitIds.has(node.object.id); });
|
|
441
|
+
var removedBefore = siblings.slice(0, insertAt).filter(function (node) { return unitIds.has(node.object.id); }).length;
|
|
388
442
|
remaining.splice(insertAt - removedBefore, 0, groupNode);
|
|
389
|
-
siblings.splice(0, siblings.length,
|
|
390
|
-
return { scene: flattenSceneTree(roots), groupId };
|
|
443
|
+
siblings.splice.apply(siblings, __spreadArray([0, siblings.length], remaining, false));
|
|
444
|
+
return { scene: flattenSceneTree(roots), groupId: groupId };
|
|
391
445
|
}
|
|
392
446
|
/** Dissolves groups: their members take the group's place in the list and in its parent. */
|
|
393
447
|
export function ungroupObjects(scene, ids) {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
448
|
+
var _a;
|
|
449
|
+
var groupIds = new Set();
|
|
450
|
+
var index = indexScene(scene);
|
|
451
|
+
for (var _i = 0, ids_2 = ids; _i < ids_2.length; _i++) {
|
|
452
|
+
var id = ids_2[_i];
|
|
453
|
+
if (((_a = index.get(id)) === null || _a === void 0 ? void 0 : _a.type) === 'group')
|
|
398
454
|
groupIds.add(id);
|
|
399
455
|
}
|
|
400
456
|
if (groupIds.size === 0)
|
|
401
|
-
return { scene, releasedIds: [] };
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
for (
|
|
457
|
+
return { scene: scene, releasedIds: [] };
|
|
458
|
+
var roots = buildSceneTree(scene);
|
|
459
|
+
var releasedIds = [];
|
|
460
|
+
var dissolve = function (nodes) {
|
|
461
|
+
var result = [];
|
|
462
|
+
for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
|
|
463
|
+
var node = nodes_2[_i];
|
|
407
464
|
node.children = dissolve(node.children);
|
|
408
465
|
if (groupIds.has(node.object.id)) {
|
|
409
|
-
for (
|
|
466
|
+
for (var _a = 0, _b = node.children; _a < _b.length; _a++) {
|
|
467
|
+
var child = _b[_a];
|
|
410
468
|
releasedIds.push(child.object.id);
|
|
411
469
|
result.push(child);
|
|
412
470
|
}
|
|
@@ -417,34 +475,39 @@ export function ungroupObjects(scene, ids) {
|
|
|
417
475
|
}
|
|
418
476
|
return result;
|
|
419
477
|
};
|
|
420
|
-
|
|
421
|
-
return { scene: flattenSceneTree(dissolved), releasedIds };
|
|
478
|
+
var dissolved = dissolve(roots);
|
|
479
|
+
return { scene: flattenSceneTree(dissolved), releasedIds: releasedIds };
|
|
422
480
|
}
|
|
423
481
|
/**
|
|
424
482
|
* Copies objects (a group is copied with its members) under new ids, shifted by an offset.
|
|
425
483
|
* The copies are returned in paint order, ready to be added on top of the board.
|
|
426
484
|
*/
|
|
427
|
-
export function duplicateObjects(scene, ids, offset
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
485
|
+
export function duplicateObjects(scene, ids, offset, makeId) {
|
|
486
|
+
var _a;
|
|
487
|
+
if (offset === void 0) { offset = { x: 0, y: 0 }; }
|
|
488
|
+
if (makeId === void 0) { makeId = function (type) { return createObjectId(type === 'group' ? 'group' : 'obj'); }; }
|
|
489
|
+
var sources = withDescendantIds(scene, normalizeSelection(scene, ids));
|
|
490
|
+
var idMap = new Map();
|
|
491
|
+
var objects = [];
|
|
492
|
+
var rootIds = new Set(normalizeSelection(scene, ids));
|
|
493
|
+
for (var _i = 0, scene_9 = scene; _i < scene_9.length; _i++) {
|
|
494
|
+
var object = scene_9[_i];
|
|
433
495
|
if (!sources.has(object.id))
|
|
434
496
|
continue;
|
|
435
|
-
|
|
497
|
+
var copyId = makeId(object.type);
|
|
436
498
|
idMap.set(object.id, copyId);
|
|
437
|
-
|
|
499
|
+
var parentId = rootIds.has(object.id)
|
|
438
500
|
? object.parentId
|
|
439
501
|
: object.parentId !== null
|
|
440
|
-
? (idMap.get(object.parentId)
|
|
502
|
+
? ((_a = idMap.get(object.parentId)) !== null && _a !== void 0 ? _a : null)
|
|
441
503
|
: null;
|
|
442
|
-
|
|
504
|
+
var copy = __assign(__assign({}, object), { id: copyId, parentId: parentId });
|
|
443
505
|
if (isBoxedObject(copy) && isBoxedObject(object)) {
|
|
444
506
|
copy.x = object.x + offset.x;
|
|
445
507
|
copy.y = object.y + offset.y;
|
|
446
508
|
}
|
|
447
509
|
objects.push(copy);
|
|
448
510
|
}
|
|
449
|
-
|
|
511
|
+
// An arrow that was tied to a shape that is copied with it is tied to the copy; one whose shape stays behind is let go.
|
|
512
|
+
return { objects: objects.map(function (copy) { return remapEnds(copy, idMap); }), idMap: idMap };
|
|
450
513
|
}
|