@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
|
@@ -9,8 +9,20 @@
|
|
|
9
9
|
* so the rules of the trail are held to in Node. The rules and the numbers are the ones of tldraw's eraser trail (tldraw
|
|
10
10
|
* is the reference the owner asked for); the code is our own.
|
|
11
11
|
*/
|
|
12
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
15
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
18
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
19
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
20
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
21
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
22
|
+
};
|
|
23
|
+
var _Scribble_instances, _Scribble_points, _Scribble_size, _Scribble_phase, _Scribble_incoming, _Scribble_sinceStepMs, _Scribble_takeIncoming;
|
|
12
24
|
/** Non-secret runtime constants of the trail. */
|
|
13
|
-
export
|
|
25
|
+
export var SCRIBBLE_CONFIG = {
|
|
14
26
|
/** How wide the head of the trail is, in screen pixels. */
|
|
15
27
|
size: 12,
|
|
16
28
|
/** How opaque the trail is drawn. */
|
|
@@ -29,67 +41,78 @@ export const SCRIBBLE_CONFIG = {
|
|
|
29
41
|
streamline: 0.32
|
|
30
42
|
};
|
|
31
43
|
/** How opaque a shape is drawn while the eraser has marked it: it stays behind, faded, until the pointer is released. */
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
44
|
+
export var ERASING_OPACITY = 0.32;
|
|
45
|
+
var Scribble = /** @class */ (function () {
|
|
46
|
+
function Scribble() {
|
|
47
|
+
_Scribble_instances.add(this);
|
|
48
|
+
_Scribble_points.set(this, []);
|
|
49
|
+
_Scribble_size.set(this, SCRIBBLE_CONFIG.size);
|
|
50
|
+
_Scribble_phase.set(this, 'starting');
|
|
51
|
+
// The place the pointer reached that the trail has not taken in yet (the next step takes it).
|
|
52
|
+
_Scribble_incoming.set(this, null);
|
|
53
|
+
// How long it has been since the trail last bit its tail.
|
|
54
|
+
_Scribble_sinceStepMs.set(this, 0);
|
|
55
|
+
}
|
|
41
56
|
/**
|
|
42
57
|
* The pointer reached a place. The next step takes it in; of several places given between two steps the last one counts.
|
|
43
58
|
* A place less than a pixel (on the screen at `zoom`) from the trail's head adds nothing the eye can see.
|
|
44
59
|
*/
|
|
45
|
-
add(point, zoom) {
|
|
46
|
-
if (this
|
|
60
|
+
Scribble.prototype.add = function (point, zoom) {
|
|
61
|
+
if (__classPrivateFieldGet(this, _Scribble_phase, "f") === 'stopping')
|
|
47
62
|
return;
|
|
48
63
|
if (!Number.isFinite(point.x) || !Number.isFinite(point.y))
|
|
49
64
|
return;
|
|
50
|
-
|
|
51
|
-
|
|
65
|
+
var head = __classPrivateFieldGet(this, _Scribble_points, "f")[__classPrivateFieldGet(this, _Scribble_points, "f").length - 1];
|
|
66
|
+
var safeZoom = Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
|
|
52
67
|
if (!head || Math.hypot(point.x - head.x, point.y - head.y) * safeZoom >= SCRIBBLE_CONFIG.minDistance)
|
|
53
|
-
this
|
|
54
|
-
}
|
|
68
|
+
__classPrivateFieldSet(this, _Scribble_incoming, point, "f");
|
|
69
|
+
};
|
|
55
70
|
/** The pointer let go: from now on the trail is only eaten, from its tail to its head. */
|
|
56
|
-
stop() {
|
|
57
|
-
this
|
|
58
|
-
this
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
Scribble.prototype.stop = function () {
|
|
72
|
+
__classPrivateFieldSet(this, _Scribble_phase, 'stopping', "f");
|
|
73
|
+
__classPrivateFieldSet(this, _Scribble_incoming, null, "f");
|
|
74
|
+
};
|
|
75
|
+
Object.defineProperty(Scribble.prototype, "finished", {
|
|
76
|
+
/** True when the trail has been let go and nothing of it is left to show. */
|
|
77
|
+
get: function () {
|
|
78
|
+
return __classPrivateFieldGet(this, _Scribble_phase, "f") === 'stopping' && __classPrivateFieldGet(this, _Scribble_points, "f").length === 0;
|
|
79
|
+
},
|
|
80
|
+
enumerable: false,
|
|
81
|
+
configurable: true
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(Scribble.prototype, "phase", {
|
|
84
|
+
get: function () {
|
|
85
|
+
return __classPrivateFieldGet(this, _Scribble_phase, "f");
|
|
86
|
+
},
|
|
87
|
+
enumerable: false,
|
|
88
|
+
configurable: true
|
|
89
|
+
});
|
|
67
90
|
/**
|
|
68
91
|
* One frame of the animation clock. `elapsedMs` is the time since the last frame. Returns whether what the trail looks
|
|
69
92
|
* like changed (a new point, a bitten tail, a thinner head), so the caller draws only then.
|
|
70
93
|
*/
|
|
71
|
-
step(elapsedMs) {
|
|
72
|
-
if (this
|
|
94
|
+
Scribble.prototype.step = function (elapsedMs) {
|
|
95
|
+
if (__classPrivateFieldGet(this, _Scribble_phase, "f") === 'starting') {
|
|
73
96
|
// It only grows at first: nothing is bitten until it holds more than `maxPoints`.
|
|
74
|
-
|
|
75
|
-
if (this
|
|
76
|
-
this
|
|
97
|
+
var grew = __classPrivateFieldGet(this, _Scribble_instances, "m", _Scribble_takeIncoming).call(this);
|
|
98
|
+
if (__classPrivateFieldGet(this, _Scribble_points, "f").length > SCRIBBLE_CONFIG.maxPoints)
|
|
99
|
+
__classPrivateFieldSet(this, _Scribble_phase, 'active', "f");
|
|
77
100
|
return grew;
|
|
78
101
|
}
|
|
79
|
-
this
|
|
80
|
-
|
|
102
|
+
__classPrivateFieldSet(this, _Scribble_sinceStepMs, __classPrivateFieldGet(this, _Scribble_sinceStepMs, "f") + (Number.isFinite(elapsedMs) && elapsedMs > 0 ? elapsedMs : 0), "f");
|
|
103
|
+
var atStep = __classPrivateFieldGet(this, _Scribble_sinceStepMs, "f") >= SCRIBBLE_CONFIG.stepMs;
|
|
81
104
|
if (atStep)
|
|
82
|
-
this
|
|
83
|
-
if (this
|
|
84
|
-
if (this
|
|
105
|
+
__classPrivateFieldSet(this, _Scribble_sinceStepMs, 0, "f");
|
|
106
|
+
if (__classPrivateFieldGet(this, _Scribble_phase, "f") === 'active') {
|
|
107
|
+
if (__classPrivateFieldGet(this, _Scribble_instances, "m", _Scribble_takeIncoming).call(this)) {
|
|
85
108
|
// Following the pointer: a new point at the head, the oldest let go.
|
|
86
|
-
if (this
|
|
87
|
-
this
|
|
109
|
+
if (__classPrivateFieldGet(this, _Scribble_points, "f").length > SCRIBBLE_CONFIG.maxPoints)
|
|
110
|
+
__classPrivateFieldGet(this, _Scribble_points, "f").shift();
|
|
88
111
|
return true;
|
|
89
112
|
}
|
|
90
113
|
// The pointer rests: the tail catches up with the head.
|
|
91
|
-
if (atStep && this
|
|
92
|
-
this
|
|
114
|
+
if (atStep && __classPrivateFieldGet(this, _Scribble_points, "f").length > 1) {
|
|
115
|
+
__classPrivateFieldGet(this, _Scribble_points, "f").shift();
|
|
93
116
|
return true;
|
|
94
117
|
}
|
|
95
118
|
return false;
|
|
@@ -97,25 +120,27 @@ export class Scribble {
|
|
|
97
120
|
// Let go: every bite takes a point from the tail and a tenth of the width, until one point is left, and then that goes.
|
|
98
121
|
if (!atStep)
|
|
99
122
|
return false;
|
|
100
|
-
if (this
|
|
101
|
-
|
|
102
|
-
this
|
|
123
|
+
if (__classPrivateFieldGet(this, _Scribble_points, "f").length <= 1) {
|
|
124
|
+
var hadPoints = __classPrivateFieldGet(this, _Scribble_points, "f").length > 0;
|
|
125
|
+
__classPrivateFieldSet(this, _Scribble_points, [], "f");
|
|
103
126
|
return hadPoints;
|
|
104
127
|
}
|
|
105
|
-
this
|
|
106
|
-
this
|
|
128
|
+
__classPrivateFieldSet(this, _Scribble_size, Math.max(SCRIBBLE_CONFIG.minSize, __classPrivateFieldGet(this, _Scribble_size, "f") * (1 - SCRIBBLE_CONFIG.shrink)), "f");
|
|
129
|
+
__classPrivateFieldGet(this, _Scribble_points, "f").shift();
|
|
107
130
|
return true;
|
|
108
|
-
}
|
|
131
|
+
};
|
|
109
132
|
/** What to draw now. */
|
|
110
|
-
view() {
|
|
111
|
-
return { points: this
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return
|
|
120
|
-
|
|
121
|
-
|
|
133
|
+
Scribble.prototype.view = function () {
|
|
134
|
+
return { points: __classPrivateFieldGet(this, _Scribble_points, "f").slice(), size: __classPrivateFieldGet(this, _Scribble_size, "f"), stopping: __classPrivateFieldGet(this, _Scribble_phase, "f") === 'stopping' };
|
|
135
|
+
};
|
|
136
|
+
return Scribble;
|
|
137
|
+
}());
|
|
138
|
+
export { Scribble };
|
|
139
|
+
_Scribble_points = new WeakMap(), _Scribble_size = new WeakMap(), _Scribble_phase = new WeakMap(), _Scribble_incoming = new WeakMap(), _Scribble_sinceStepMs = new WeakMap(), _Scribble_instances = new WeakSet(), _Scribble_takeIncoming = function _Scribble_takeIncoming() {
|
|
140
|
+
var incoming = __classPrivateFieldGet(this, _Scribble_incoming, "f");
|
|
141
|
+
if (!incoming)
|
|
142
|
+
return false;
|
|
143
|
+
__classPrivateFieldSet(this, _Scribble_incoming, null, "f");
|
|
144
|
+
__classPrivateFieldGet(this, _Scribble_points, "f").push(incoming);
|
|
145
|
+
return true;
|
|
146
|
+
};
|
|
@@ -11,22 +11,24 @@ import { generatePathForShape, getShapeByKey, roundedPolygonPath } from '../../c
|
|
|
11
11
|
import { clampNumber } from './geometry.js';
|
|
12
12
|
import { fitPathToBox, parsePathData } from './pathData.js';
|
|
13
13
|
/** The `shape` key of a triangle path. */
|
|
14
|
-
export
|
|
14
|
+
export var TRIANGLE_SHAPE_KEY = 'triangle';
|
|
15
15
|
/** The `shape` key of an arrow path (its geometry is in `arrow.ts`). */
|
|
16
|
-
export
|
|
16
|
+
export var ARROW_SHAPE_KEY = 'arrow';
|
|
17
17
|
/** The categories of the library whose shapes keep their proportions when resized (symbols and icons). */
|
|
18
|
-
|
|
18
|
+
var ASPECT_LOCKED_CATEGORIES = new Set(['Symbol Shapes', 'Media Icons', 'UI Icons']);
|
|
19
19
|
/** Whether a library shape keeps its aspect ratio: its own setting, else the rule of its category. */
|
|
20
20
|
export function libraryShapeKeepsAspect(shape) {
|
|
21
|
-
|
|
21
|
+
var _a;
|
|
22
|
+
return (_a = shape.lockAspectRatio) !== null && _a !== void 0 ? _a : ASPECT_LOCKED_CATEGORIES.has(shape.category);
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
25
|
* A triangle with its apex at the top middle and its base along the bottom, with corners rounded by
|
|
25
26
|
* `roundness` percent (100 rounds by a quarter of the shorter side).
|
|
26
27
|
*/
|
|
27
|
-
export function trianglePathCommands(width, height, roundness
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
export function trianglePathCommands(width, height, roundness) {
|
|
29
|
+
if (roundness === void 0) { roundness = 0; }
|
|
30
|
+
var radius = (clampNumber(roundness, 0, 100) / 100) * (Math.min(width, height) / 4);
|
|
31
|
+
var data = roundedPolygonPath([
|
|
30
32
|
[width / 2, 0],
|
|
31
33
|
[width, height],
|
|
32
34
|
[0, height]
|
|
@@ -37,12 +39,13 @@ export function trianglePathCommands(width, height, roundness = 0) {
|
|
|
37
39
|
* A library shape fitted to a box. Shapes that can round their corners are regenerated at the given
|
|
38
40
|
* roundness; the others are the stored outline, scaled. Null for a key the library does not know.
|
|
39
41
|
*/
|
|
40
|
-
export function libraryShapePathCommands(shapeKey, width, height, roundness
|
|
41
|
-
|
|
42
|
+
export function libraryShapePathCommands(shapeKey, width, height, roundness) {
|
|
43
|
+
if (roundness === void 0) { roundness = 0; }
|
|
44
|
+
var shape = getShapeByKey(shapeKey);
|
|
42
45
|
if (!shape)
|
|
43
46
|
return null;
|
|
44
|
-
|
|
45
|
-
return fitPathToBox(parsePathData(regenerated
|
|
47
|
+
var regenerated = shape.cornerRadiusMode === 'pathRegen' ? generatePathForShape(shape, roundness) : null;
|
|
48
|
+
return fitPathToBox(parsePathData(regenerated !== null && regenerated !== void 0 ? regenerated : shape.pathData), width, height);
|
|
46
49
|
}
|
|
47
50
|
/** Whether a path of this kind is rebuilt from its key when it is resized (rather than scaled). */
|
|
48
51
|
export function isRegeneratedShape(shapeKey) {
|
|
@@ -50,7 +53,7 @@ export function isRegeneratedShape(shapeKey) {
|
|
|
50
53
|
return false;
|
|
51
54
|
if (shapeKey === TRIANGLE_SHAPE_KEY)
|
|
52
55
|
return true;
|
|
53
|
-
|
|
56
|
+
var shape = getShapeByKey(shapeKey);
|
|
54
57
|
return !!shape && shape.cornerRadiusMode === 'pathRegen';
|
|
55
58
|
}
|
|
56
59
|
/** Whether the Roundness control applies to this path. */
|
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
* place. Nothing here touches the board; the pointer controller calls these on every move for the preview and
|
|
4
4
|
* once more on release for the shape that is kept.
|
|
5
5
|
*/
|
|
6
|
+
var __assign = (this && this.__assign) || function () {
|
|
7
|
+
__assign = Object.assign || function(t) {
|
|
8
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
9
|
+
s = arguments[i];
|
|
10
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
11
|
+
t[p] = s[p];
|
|
12
|
+
}
|
|
13
|
+
return t;
|
|
14
|
+
};
|
|
15
|
+
return __assign.apply(this, arguments);
|
|
16
|
+
};
|
|
6
17
|
import { getShapeByKey } from '../../canvas/shapes/ShapeLibrary.js';
|
|
7
18
|
import { DEFAULT_OBJECT_SIZE, SHAPE_STYLE_DEFAULTS, createEllipseObject, createObjectId, createPathObject, createRectObject, randomShapeFill } from './defaults.js';
|
|
8
19
|
import { buildArrow } from './arrow.js';
|
|
@@ -12,56 +23,58 @@ import { serializePathData } from './pathData.js';
|
|
|
12
23
|
import { snapAngle } from './transform.js';
|
|
13
24
|
import { MIN_OBJECT_SIZE } from './types.js';
|
|
14
25
|
/** A drag shorter than this (in screen pixels) is a click: the shape is placed at its default size. */
|
|
15
|
-
export
|
|
26
|
+
export var MIN_DRAW_DISTANCE = 4;
|
|
16
27
|
/** The size of a library shape placed by a click. */
|
|
17
|
-
export
|
|
28
|
+
export var LIBRARY_SHAPE_CLICK_SIZE = 50;
|
|
18
29
|
/** The length of an arrow placed by a click. */
|
|
19
|
-
export
|
|
30
|
+
export var ARROW_CLICK_LENGTH = 100;
|
|
20
31
|
/** Shift holds an arrow to a multiple of this many degrees (tldraw divides the circle into 24: 15 degrees). */
|
|
21
|
-
export
|
|
32
|
+
export var ARROW_SNAP_DEGREES = 15;
|
|
22
33
|
/** A triangle's height for a width, when it is equilateral. */
|
|
23
|
-
export
|
|
34
|
+
export var EQUILATERAL_RATIO = Math.sqrt(3) / 2;
|
|
24
35
|
/** A triangle added by the toolbar button rather than drawn. */
|
|
25
|
-
export
|
|
36
|
+
export var BUTTON_TRIANGLE_SIZE = { width: 100, height: 150 };
|
|
26
37
|
/** How soft the corners of a rounded square start (0 to 100: 100 is a pill). The Roundness control moves it afterwards. */
|
|
27
|
-
export
|
|
38
|
+
export var ROUNDED_RECT_ROUNDNESS = 35;
|
|
28
39
|
/** A box of a given aspect (width / height) that has the dragged corner for one of its corners. */
|
|
29
40
|
function boxWithAspect(start, end, aspect) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
41
|
+
var deltaX = end.x - start.x;
|
|
42
|
+
var deltaY = end.y - start.y;
|
|
43
|
+
var signX = deltaX < 0 ? -1 : 1;
|
|
44
|
+
var signY = deltaY < 0 ? -1 : 1;
|
|
34
45
|
// The box takes the larger of what the pointer asked for, so the shape follows the pointer's far corner.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
var width = Math.max(Math.abs(deltaX), Math.abs(deltaY) * aspect);
|
|
47
|
+
var height = width / aspect;
|
|
48
|
+
var right = start.x + signX * width;
|
|
49
|
+
var bottom = start.y + signY * height;
|
|
39
50
|
return rectFromPoints(start, { x: right, y: bottom });
|
|
40
51
|
}
|
|
41
52
|
/** A box of a given size centred on a point. */
|
|
42
53
|
function centeredBox(center, width, height) {
|
|
43
|
-
return { x: center.x - width / 2, y: center.y - height / 2, width, height };
|
|
54
|
+
return { x: center.x - width / 2, y: center.y - height / 2, width: width, height: height };
|
|
44
55
|
}
|
|
45
56
|
/** The paint of a new shape of the tools, by the look the board is in. */
|
|
46
|
-
export function newShapePaint(wireframe, random
|
|
47
|
-
|
|
57
|
+
export function newShapePaint(wireframe, random, workspacePaint) {
|
|
58
|
+
if (random === void 0) { random = Math.random; }
|
|
59
|
+
if (workspacePaint === void 0) { workspacePaint = {}; }
|
|
60
|
+
var base = wireframe
|
|
48
61
|
? {
|
|
49
62
|
fill: SHAPE_STYLE_DEFAULTS.wireframeFill,
|
|
50
63
|
stroke: SHAPE_STYLE_DEFAULTS.wireframeStroke,
|
|
51
64
|
strokeWidth: SHAPE_STYLE_DEFAULTS.wireframeStrokeWidth
|
|
52
65
|
}
|
|
53
66
|
: { fill: randomShapeFill(random), stroke: SHAPE_STYLE_DEFAULTS.stroke, strokeWidth: SHAPE_STYLE_DEFAULTS.strokeWidth };
|
|
54
|
-
return {
|
|
67
|
+
return __assign(__assign({}, base), workspacePaint);
|
|
55
68
|
}
|
|
56
69
|
/** The look of a library shape, and of an arrow. */
|
|
57
|
-
export
|
|
58
|
-
export
|
|
70
|
+
export var LIBRARY_SHAPE_PAINT = { fill: '#ffffff', stroke: '#374151', strokeWidth: 3 };
|
|
71
|
+
export var ARROW_PAINT = { fill: 'transparent', stroke: '#111827', strokeWidth: 3 };
|
|
59
72
|
/** The box a shape tool would place for a drag. */
|
|
60
73
|
export function shapeDragBox(options) {
|
|
61
|
-
|
|
74
|
+
var kind = options.kind, start = options.start, end = options.end, constrain = options.constrain, dragged = options.dragged;
|
|
62
75
|
if (kind === 'arrow')
|
|
63
76
|
return arrowDragGeometry(options).box;
|
|
64
|
-
|
|
77
|
+
var aspectLockedLibrary = kind === 'library-shape' && libraryKeepsAspect(options.libraryKey);
|
|
65
78
|
if (!dragged) {
|
|
66
79
|
if (kind === 'triangle')
|
|
67
80
|
return centeredBox(start, DEFAULT_OBJECT_SIZE, DEFAULT_OBJECT_SIZE * EQUILATERAL_RATIO);
|
|
@@ -77,12 +90,12 @@ export function shapeDragBox(options) {
|
|
|
77
90
|
return rectFromPoints(start, end);
|
|
78
91
|
}
|
|
79
92
|
function libraryKeepsAspect(key) {
|
|
80
|
-
|
|
93
|
+
var shape = getShapeByKey(key !== null && key !== void 0 ? key : null);
|
|
81
94
|
return !!shape && libraryShapeKeepsAspect(shape);
|
|
82
95
|
}
|
|
83
96
|
/** The end of an arrow for a drag: where the pointer is, held to a multiple of 15 degrees with Shift, or the default length. */
|
|
84
97
|
export function arrowEnd(options) {
|
|
85
|
-
|
|
98
|
+
var start = options.start, end = options.end, constrain = options.constrain, dragged = options.dragged;
|
|
86
99
|
if (!dragged)
|
|
87
100
|
return { x: start.x + ARROW_CLICK_LENGTH, y: start.y };
|
|
88
101
|
if (!constrain)
|
|
@@ -91,8 +104,8 @@ export function arrowEnd(options) {
|
|
|
91
104
|
}
|
|
92
105
|
/** A point turned about a centre, to the nearest multiple of `step` degrees of direction, at the same distance. */
|
|
93
106
|
export function snapPointAround(center, point, step) {
|
|
94
|
-
|
|
95
|
-
|
|
107
|
+
var length = distanceBetween(center, point);
|
|
108
|
+
var angle = (snapAngle((Math.atan2(point.y - center.y, point.x - center.x) * 180) / Math.PI, step) * Math.PI) / 180;
|
|
96
109
|
return { x: center.x + length * Math.cos(angle), y: center.y + length * Math.sin(angle) };
|
|
97
110
|
}
|
|
98
111
|
function arrowDragGeometry(options) {
|
|
@@ -100,64 +113,42 @@ function arrowDragGeometry(options) {
|
|
|
100
113
|
}
|
|
101
114
|
/** The shape a tool would place for a drag. */
|
|
102
115
|
export function buildDraggedShape(options) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
116
|
+
var _a, _b;
|
|
117
|
+
var createId = (_a = options.createId) !== null && _a !== void 0 ? _a : createObjectId;
|
|
118
|
+
var id = createId('obj');
|
|
119
|
+
var kind = options.kind, wireframe = options.wireframe;
|
|
106
120
|
if (kind === 'arrow') {
|
|
107
|
-
|
|
108
|
-
return createPathObject({
|
|
109
|
-
id,
|
|
110
|
-
...geometry.box,
|
|
111
|
-
d: serializePathData(geometry.commands),
|
|
112
|
-
arrow: geometry.spec,
|
|
113
|
-
shape: ARROW_SHAPE_KEY,
|
|
114
|
-
...ARROW_PAINT,
|
|
115
|
-
lineCap: 'round',
|
|
116
|
-
lineJoin: 'round'
|
|
117
|
-
});
|
|
121
|
+
var geometry = arrowDragGeometry(options);
|
|
122
|
+
return createPathObject(__assign(__assign(__assign(__assign({ id: id }, geometry.box), { d: serializePathData(geometry.commands), arrow: geometry.spec, shape: ARROW_SHAPE_KEY }), ARROW_PAINT), { lineCap: 'round', lineJoin: 'round' }));
|
|
118
123
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
+
var box = shapeDragBox(options);
|
|
125
|
+
var width = Math.max(box.width, MIN_OBJECT_SIZE);
|
|
126
|
+
var height = Math.max(box.height, MIN_OBJECT_SIZE);
|
|
127
|
+
var frame = { id: id, x: box.x, y: box.y, width: width, height: height };
|
|
128
|
+
var paint = newShapePaint(wireframe, options.random, options.workspacePaint);
|
|
124
129
|
if (kind === 'rect')
|
|
125
|
-
return createRectObject({
|
|
130
|
+
return createRectObject(__assign(__assign({}, frame), paint));
|
|
126
131
|
// The rounded square of the shapes menu: a rectangle that starts with soft corners (its Roundness control still moves them).
|
|
127
132
|
if (kind === 'rounded-rect') {
|
|
128
|
-
return createRectObject({
|
|
133
|
+
return createRectObject(__assign(__assign(__assign({}, frame), paint), { roundness: ROUNDED_RECT_ROUNDNESS }));
|
|
129
134
|
}
|
|
130
135
|
if (kind === 'ellipse' || kind === 'circle') {
|
|
131
|
-
return createEllipseObject({
|
|
136
|
+
return createEllipseObject(__assign(__assign(__assign({}, frame), paint), { aspectLocked: kind === 'circle' }));
|
|
132
137
|
}
|
|
133
138
|
if (kind === 'triangle') {
|
|
134
|
-
return createPathObject({
|
|
135
|
-
...frame,
|
|
136
|
-
...paint,
|
|
137
|
-
d: serializePathData(trianglePathCommands(width, height, 0)),
|
|
138
|
-
shape: TRIANGLE_SHAPE_KEY,
|
|
139
|
-
lineJoin: 'miter'
|
|
140
|
-
});
|
|
139
|
+
return createPathObject(__assign(__assign(__assign({}, frame), paint), { d: serializePathData(trianglePathCommands(width, height, 0)), shape: TRIANGLE_SHAPE_KEY, lineJoin: 'miter' }));
|
|
141
140
|
}
|
|
142
|
-
return buildLibraryShape({
|
|
141
|
+
return buildLibraryShape(__assign(__assign({}, frame), { key: (_b = options.libraryKey) !== null && _b !== void 0 ? _b : null }));
|
|
143
142
|
}
|
|
144
143
|
/** A library shape in a box, with the look and the line ends its kind has. Null key or an unknown key gives a plain rectangle. */
|
|
145
144
|
export function buildLibraryShape(fields) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
145
|
+
var shape = getShapeByKey(fields.key);
|
|
146
|
+
var frame = { id: fields.id, x: fields.x, y: fields.y, width: fields.width, height: fields.height };
|
|
147
|
+
var commands = fields.key && shape ? libraryShapePathCommands(fields.key, fields.width, fields.height, 0) : null;
|
|
149
148
|
if (!shape || !commands)
|
|
150
|
-
return createRectObject({
|
|
151
|
-
|
|
152
|
-
return createPathObject({
|
|
153
|
-
...frame,
|
|
154
|
-
...LIBRARY_SHAPE_PAINT,
|
|
149
|
+
return createRectObject(__assign(__assign({}, frame), newShapePaint(true)));
|
|
150
|
+
var linesOnly = shape.cornerRadiusMode === 'strokeRound';
|
|
151
|
+
return createPathObject(__assign(__assign(__assign({}, frame), LIBRARY_SHAPE_PAINT), {
|
|
155
152
|
// A line-only icon (a check, a cross) has no inside to fill.
|
|
156
|
-
fill: linesOnly ? 'transparent' : LIBRARY_SHAPE_PAINT.fill,
|
|
157
|
-
d: serializePathData(commands),
|
|
158
|
-
shape: shape.key,
|
|
159
|
-
aspectLocked: libraryShapeKeepsAspect(shape),
|
|
160
|
-
lineCap: linesOnly ? 'butt' : 'round',
|
|
161
|
-
lineJoin: linesOnly ? 'miter' : 'round'
|
|
162
|
-
});
|
|
153
|
+
fill: linesOnly ? 'transparent' : LIBRARY_SHAPE_PAINT.fill, d: serializePathData(commands), shape: shape.key, aspectLocked: libraryShapeKeepsAspect(shape), lineCap: linesOnly ? 'butt' : 'round', lineJoin: linesOnly ? 'miter' : 'round' }));
|
|
163
154
|
}
|