@widgetic/canvas 0.5.41 → 0.5.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/canvas/DomCanvas.svelte +49 -20
- package/dist/canvas/DomCanvas.svelte.d.ts +13 -7
- package/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.d.ts +14 -3
- package/dist/canvas/chromeLayout.js +30 -20
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.d.ts +47 -5
- package/dist/canvas/host/CanvasHost.js +1481 -1566
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/browserServices.js +65 -116
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.d.ts +33 -0
- package/dist/canvas/host/nearCards.js +78 -0
- package/dist/canvas/host/pageEvents.d.ts +16 -4
- package/dist/canvas/host/pageEvents.js +17 -8
- package/dist/canvas/host/pictureSaves.d.ts +33 -0
- package/dist/canvas/host/pictureSaves.js +42 -0
- package/dist/canvas/host/screenRects.d.ts +5 -0
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/storedPictures.d.ts +88 -0
- package/dist/canvas/host/storedPictures.js +288 -0
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/index.d.ts +1 -1
- package/dist/canvas/index.js +1 -1
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.d.ts +45 -2
- package/dist/canvas/shapes/shapeImageStore.js +10 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.d.ts +2 -2
- package/dist/canvas/toolbarPosition.js +4 -4
- package/dist/canvas/ui/SelectionPanel.svelte +28 -5
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/SelectorIndicator.svelte +81 -0
- package/dist/canvas/ui/SelectorIndicator.svelte.d.ts +11 -0
- package/dist/canvas/ui/ToolButton.svelte +8 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +2 -2
- package/dist/canvas/ui/floatingCard.js +20 -20
- package/dist/canvas/ui/floatingLayer.d.ts +13 -2
- package/dist/canvas/ui/floatingLayer.js +18 -7
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +34 -40
- package/dist/canvas/view/CanvasStage.svelte +101 -67
- package/dist/canvas/view/LeftToRightScope.svelte +21 -0
- package/dist/canvas/view/LeftToRightScope.svelte.d.ts +7 -0
- package/dist/canvas/view/ObjectView.svelte +15 -10
- package/dist/canvas/view/ObjectView.svelte.d.ts +1 -1
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/canvas/view/liftedScene.d.ts +31 -0
- package/dist/canvas/view/liftedScene.js +146 -0
- package/dist/canvas/view/mountedScene.d.ts +71 -0
- package/dist/canvas/view/mountedScene.js +363 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +15 -1
- package/dist/engine/EngineCore.js +724 -797
- package/dist/engine/codec/canvasDocument.d.ts +5 -0
- package/dist/engine/codec/canvasDocument.js +212 -149
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +76 -89
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +67 -94
- package/dist/engine/core/defaults.js +90 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.d.ts +6 -0
- package/dist/engine/core/history.js +49 -50
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.d.ts +10 -0
- package/dist/engine/core/scene.js +153 -187
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +31 -64
- package/dist/engine/core/transform.js +83 -94
- package/dist/engine/core/types.d.ts +8 -2
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +467 -474
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +265 -309
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +27 -30
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.d.ts +20 -0
- package/dist/engine/state.js +5 -15
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.d.ts +10 -1
- package/dist/fonts/interFont.js +47 -75
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.d.ts +8 -1
- package/dist/i18n/messages.js +10 -2
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/settings/workspaceToolSettings.js +32 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +105 -141
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/types.js +1 -1
- package/package.json +1 -1
- package/dist/canvas/ui/SelectorBar.svelte +0 -84
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +0 -12
|
@@ -9,18 +9,16 @@ export function estimateFontVerticalMetrics(fontSize) {
|
|
|
9
9
|
/**
|
|
10
10
|
* The height of one line in world units: glyph ascent+descent plus balanced leading inside the CSS line-height box.
|
|
11
11
|
*/
|
|
12
|
-
export function textLineExtent(style, metrics) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
var leading = Math.max(0, lineHeight - glyphHeight);
|
|
12
|
+
export function textLineExtent(style, metrics = estimateFontVerticalMetrics(style.fontSize)) {
|
|
13
|
+
const glyphHeight = metrics.ascent + metrics.descent;
|
|
14
|
+
const lineHeight = lineBoxHeight(style);
|
|
15
|
+
const leading = Math.max(0, lineHeight - glyphHeight);
|
|
17
16
|
return glyphHeight + leading;
|
|
18
17
|
}
|
|
19
18
|
/** Splits leading evenly above and below the glyph box inside one line. */
|
|
20
|
-
export function lineVerticalInsets(style, metrics) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
var slack = Math.max(0, extent - glyphHeight);
|
|
19
|
+
export function lineVerticalInsets(style, metrics = estimateFontVerticalMetrics(style.fontSize)) {
|
|
20
|
+
const extent = textLineExtent(style, metrics);
|
|
21
|
+
const glyphHeight = metrics.ascent + metrics.descent;
|
|
22
|
+
const slack = Math.max(0, extent - glyphHeight);
|
|
25
23
|
return { top: slack / 2, bottom: slack / 2 };
|
|
26
24
|
}
|
package/dist/engine/widgets.js
CHANGED
|
@@ -6,55 +6,29 @@
|
|
|
6
6
|
* card for a widget, turn a sketch into one, find the card of a widget, say which cards are orphans), written
|
|
7
7
|
* against the engine alone so each rule is tested in Node. The component forwards them one to one.
|
|
8
8
|
*/
|
|
9
|
-
var __assign = (this && this.__assign) || function () {
|
|
10
|
-
__assign = Object.assign || function(t) {
|
|
11
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
12
|
-
s = arguments[i];
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
}
|
|
16
|
-
return t;
|
|
17
|
-
};
|
|
18
|
-
return __assign.apply(this, arguments);
|
|
19
|
-
};
|
|
20
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
21
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
22
|
-
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");
|
|
23
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
24
|
-
};
|
|
25
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
26
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
27
|
-
if (ar || !(i in from)) {
|
|
28
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
29
|
-
ar[i] = from[i];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
33
|
-
};
|
|
34
|
-
var _ConversionRegistry_snapshots;
|
|
35
9
|
import { createWidgetObject } from './core/defaults.js';
|
|
36
10
|
import { unionRects } from './core/geometry.js';
|
|
37
11
|
import { boundsOfObjects, leafShapesOf, normalizeScene, removeObjects as removeFromScene, shapeVisualBounds, topLevelAncestorId, withDescendantIds } from './core/scene.js';
|
|
38
12
|
/** The size of the card a new widget gets. */
|
|
39
|
-
export
|
|
13
|
+
export const WIDGET_CARD_SIZE = Object.freeze({ width: 400, height: 300 });
|
|
40
14
|
/** The space between cards placed side by side. */
|
|
41
|
-
export
|
|
15
|
+
export const WIDGET_CARD_GAP = 48;
|
|
42
16
|
/** How many cards stand in a row before the next row starts. */
|
|
43
|
-
export
|
|
17
|
+
export const WIDGET_CARD_MAX_COLUMNS = 4;
|
|
44
18
|
/** A sketch turned into a card is never smaller than this, however little was drawn. */
|
|
45
|
-
export
|
|
19
|
+
export const ABSORBED_CARD_MIN_SIZE = Object.freeze({ width: 120, height: 80 });
|
|
46
20
|
/** Where the card goes when nothing on the board has a size to take. */
|
|
47
|
-
export
|
|
21
|
+
export const ABSORBED_CARD_FALLBACK_ORIGIN = Object.freeze({ x: 80, y: 80 });
|
|
48
22
|
/** The prefix of the id of a card made here. */
|
|
49
|
-
|
|
23
|
+
const CARD_ID_PREFIX = 'widget_img';
|
|
50
24
|
function cardsOf(scene) {
|
|
51
|
-
return scene.filter(
|
|
25
|
+
return scene.filter((object) => object.type === 'widget');
|
|
52
26
|
}
|
|
53
27
|
/** The first card that stands for the widget, or undefined. */
|
|
54
28
|
export function findWidgetCard(engine, widgetId) {
|
|
55
29
|
if (!widgetId)
|
|
56
30
|
return undefined;
|
|
57
|
-
return cardsOf(engine.state.scene).find(
|
|
31
|
+
return cardsOf(engine.state.scene).find((card) => card.widgetId === widgetId);
|
|
58
32
|
}
|
|
59
33
|
/** The rectangle around the shapes of some units, strokes included: what a screenshot of them would show. */
|
|
60
34
|
function visualBoundsOfUnits(scene, ids) {
|
|
@@ -65,23 +39,21 @@ function visualBoundsOfUnits(scene, ids) {
|
|
|
65
39
|
* cards in rows of up to four around that middle, so a batch of widgets does not land on top of each other.
|
|
66
40
|
* The card becomes the selection. Returns its id.
|
|
67
41
|
*/
|
|
68
|
-
export function addWidgetCard(engine, widgetId, options) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
var column = gridIndex % columns;
|
|
77
|
-
var row = Math.floor(gridIndex / columns);
|
|
42
|
+
export function addWidgetCard(engine, widgetId, options = {}) {
|
|
43
|
+
const totalCount = Math.max(1, Math.floor(Number.isFinite(options.totalCount) ? options.totalCount : 1));
|
|
44
|
+
const gridIndex = Math.max(0, Math.floor(Number.isFinite(options.gridIndex) ? options.gridIndex : 0));
|
|
45
|
+
const center = engine.visibleCenter();
|
|
46
|
+
const columns = Math.min(totalCount, WIDGET_CARD_MAX_COLUMNS);
|
|
47
|
+
const rows = Math.max(1, Math.ceil(totalCount / columns));
|
|
48
|
+
const column = gridIndex % columns;
|
|
49
|
+
const row = Math.floor(gridIndex / columns);
|
|
78
50
|
// The whole grid is centred on the middle of the view: each card is shifted by half of the extent of the grid.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
51
|
+
const offsetX = (column - (columns - 1) / 2) * (WIDGET_CARD_SIZE.width + WIDGET_CARD_GAP);
|
|
52
|
+
const offsetY = (row - (rows - 1) / 2) * (WIDGET_CARD_SIZE.height + WIDGET_CARD_GAP);
|
|
53
|
+
const card = createWidgetObject({
|
|
82
54
|
id: engine.services.createId(CARD_ID_PREFIX),
|
|
83
|
-
widgetId
|
|
84
|
-
widgetName:
|
|
55
|
+
widgetId,
|
|
56
|
+
widgetName: options.name ?? null,
|
|
85
57
|
x: center.x - WIDGET_CARD_SIZE.width / 2 + offsetX,
|
|
86
58
|
y: center.y - WIDGET_CARD_SIZE.height / 2 + offsetY,
|
|
87
59
|
width: WIDGET_CARD_SIZE.width,
|
|
@@ -102,22 +74,27 @@ export function absorbSketchIntoWidget(engine, widgetId, screenshot) {
|
|
|
102
74
|
return null;
|
|
103
75
|
engine.endTextEdit();
|
|
104
76
|
engine.clearSelection();
|
|
105
|
-
|
|
106
|
-
|
|
77
|
+
const scene = engine.state.scene;
|
|
78
|
+
const shapes = scene.filter((object) => object.type !== 'group');
|
|
107
79
|
if (shapes.length === 0) {
|
|
108
80
|
return addWidgetCard(engine, widgetId, { previewImage: screenshot || null });
|
|
109
81
|
}
|
|
110
|
-
if (shapes.every(
|
|
82
|
+
if (shapes.every((shape) => shape.type === 'widget' && shape.widgetId === widgetId)) {
|
|
111
83
|
return shapes[0].id;
|
|
112
84
|
}
|
|
113
|
-
|
|
114
|
-
|
|
85
|
+
const bounds = visualBoundsOfUnits(scene, scene.map((object) => object.id));
|
|
86
|
+
const box = {
|
|
115
87
|
x: bounds ? bounds.x : ABSORBED_CARD_FALLBACK_ORIGIN.x,
|
|
116
88
|
y: bounds ? bounds.y : ABSORBED_CARD_FALLBACK_ORIGIN.y,
|
|
117
89
|
width: Math.max(ABSORBED_CARD_MIN_SIZE.width, bounds ? bounds.width : 0),
|
|
118
90
|
height: Math.max(ABSORBED_CARD_MIN_SIZE.height, bounds ? bounds.height : 0)
|
|
119
91
|
};
|
|
120
|
-
|
|
92
|
+
const card = createWidgetObject({
|
|
93
|
+
id: engine.services.createId(CARD_ID_PREFIX),
|
|
94
|
+
widgetId,
|
|
95
|
+
...box,
|
|
96
|
+
thumbnail: screenshot || null
|
|
97
|
+
});
|
|
121
98
|
engine.applyScene([card], 'push');
|
|
122
99
|
engine.select([card.id]);
|
|
123
100
|
return card.id;
|
|
@@ -127,30 +104,25 @@ export function absorbSketchIntoWidget(engine, widgetId, screenshot) {
|
|
|
127
104
|
* selection can be gone by the time the host answers (clicking elsewhere clears it), and the answer still has to
|
|
128
105
|
* replace exactly those objects, so the choice is kept here, not looked up again.
|
|
129
106
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
107
|
+
export class ConversionRegistry {
|
|
108
|
+
#snapshots = new Map();
|
|
109
|
+
remember(id, snapshot) {
|
|
110
|
+
this.#snapshots.set(id, snapshot);
|
|
133
111
|
}
|
|
134
|
-
ConversionRegistry.prototype.remember = function (id, snapshot) {
|
|
135
|
-
__classPrivateFieldGet(this, _ConversionRegistry_snapshots, "f").set(id, snapshot);
|
|
136
|
-
};
|
|
137
112
|
/** The snapshot for an id, left in place (the host asks where a draft is on the screen while it makes the widget). */
|
|
138
|
-
|
|
139
|
-
return
|
|
140
|
-
}
|
|
113
|
+
peek(id) {
|
|
114
|
+
return this.#snapshots.get(id);
|
|
115
|
+
}
|
|
141
116
|
/** The snapshot for an id, forgotten at the same time (a conversion is answered once). */
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
117
|
+
take(id) {
|
|
118
|
+
const snapshot = this.#snapshots.get(id);
|
|
119
|
+
this.#snapshots.delete(id);
|
|
145
120
|
return snapshot;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
}());
|
|
152
|
-
export { ConversionRegistry };
|
|
153
|
-
_ConversionRegistry_snapshots = new WeakMap();
|
|
121
|
+
}
|
|
122
|
+
clear() {
|
|
123
|
+
this.#snapshots.clear();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
154
126
|
/**
|
|
155
127
|
* Replaces an object (or what a conversion remembered under that id) with a card for the widget, at the same
|
|
156
128
|
* place and size, showing the screenshot. What was drawn there is removed with it, groups included. One undo
|
|
@@ -159,22 +131,22 @@ _ConversionRegistry_snapshots = new WeakMap();
|
|
|
159
131
|
export function replaceObjectWithWidgetCard(engine, conversions, objectId, widgetId, screenshot) {
|
|
160
132
|
if (!objectId || !widgetId)
|
|
161
133
|
return null;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
134
|
+
const scene = engine.state.scene;
|
|
135
|
+
const index = engine.sceneIndex(scene);
|
|
136
|
+
const remembered = conversions.take(objectId);
|
|
137
|
+
let unitIds;
|
|
138
|
+
let bounds;
|
|
139
|
+
let opacity = 1;
|
|
168
140
|
if (remembered) {
|
|
169
|
-
unitIds = remembered.unitIds.filter(
|
|
141
|
+
unitIds = remembered.unitIds.filter((id) => index.has(id));
|
|
170
142
|
bounds = remembered.bounds;
|
|
171
143
|
}
|
|
172
144
|
else if (index.has(objectId)) {
|
|
173
145
|
// A member of a group is replaced together with its group: the group is the thing that was converted.
|
|
174
|
-
|
|
146
|
+
const unitId = topLevelAncestorId(index, objectId);
|
|
175
147
|
unitIds = [unitId];
|
|
176
148
|
bounds = visualBoundsOfUnits(scene, unitIds);
|
|
177
|
-
|
|
149
|
+
const first = leafShapesOf(scene, unitIds)[0];
|
|
178
150
|
if (first)
|
|
179
151
|
opacity = first.opacity;
|
|
180
152
|
}
|
|
@@ -184,36 +156,34 @@ export function replaceObjectWithWidgetCard(engine, conversions, objectId, widge
|
|
|
184
156
|
if (unitIds.length === 0 || !bounds)
|
|
185
157
|
return null;
|
|
186
158
|
engine.endTextEdit();
|
|
187
|
-
|
|
188
|
-
|
|
159
|
+
const doomed = withDescendantIds(scene, unitIds);
|
|
160
|
+
const card = createWidgetObject({
|
|
189
161
|
id: engine.services.createId(CARD_ID_PREFIX),
|
|
190
|
-
widgetId
|
|
162
|
+
widgetId,
|
|
191
163
|
x: bounds.x,
|
|
192
164
|
y: bounds.y,
|
|
193
165
|
width: Math.max(1, bounds.width),
|
|
194
166
|
height: Math.max(1, bounds.height),
|
|
195
|
-
opacity
|
|
167
|
+
opacity,
|
|
196
168
|
thumbnail: screenshot || null
|
|
197
169
|
});
|
|
198
|
-
|
|
170
|
+
const next = normalizeScene([...scene.filter((object) => !doomed.has(object.id)), card]);
|
|
199
171
|
engine.applyScene(next, 'push');
|
|
200
172
|
engine.select([card.id]);
|
|
201
173
|
return card.id;
|
|
202
174
|
}
|
|
203
175
|
/** Gives a card a new screenshot (a refresh of its preview). It is not an undo step, but the board is marked as changed. */
|
|
204
176
|
export function updateWidgetThumbnail(engine, widgetId, dataUrl) {
|
|
205
|
-
|
|
177
|
+
const card = findWidgetCard(engine, widgetId);
|
|
206
178
|
if (!card)
|
|
207
179
|
return false;
|
|
208
|
-
|
|
209
|
-
return object.id === card.id ? __assign(__assign({}, card), { thumbnail: dataUrl || null, thumbnailRef: null }) : object;
|
|
210
|
-
});
|
|
180
|
+
const next = engine.state.scene.map((object) => object.id === card.id ? { ...card, thumbnail: dataUrl || null, thumbnailRef: null } : object);
|
|
211
181
|
engine.applyScene(next, 'replace');
|
|
212
182
|
return true;
|
|
213
183
|
}
|
|
214
184
|
/** Picks the card of a widget. False when the board has none. */
|
|
215
185
|
export function selectWidgetCard(engine, widgetId) {
|
|
216
|
-
|
|
186
|
+
const card = findWidgetCard(engine, widgetId);
|
|
217
187
|
if (!card)
|
|
218
188
|
return false;
|
|
219
189
|
engine.select([card.id]);
|
|
@@ -221,10 +191,10 @@ export function selectWidgetCard(engine, widgetId) {
|
|
|
221
191
|
}
|
|
222
192
|
/** The widget of the card that is the whole selection, or null. */
|
|
223
193
|
export function selectedWidgetId(engine) {
|
|
224
|
-
|
|
194
|
+
const { selectedIds } = engine.state;
|
|
225
195
|
if (selectedIds.length !== 1)
|
|
226
196
|
return null;
|
|
227
|
-
|
|
197
|
+
const object = engine.getObject(selectedIds[0]);
|
|
228
198
|
return object && object.type === 'widget' ? object.widgetId : null;
|
|
229
199
|
}
|
|
230
200
|
/**
|
|
@@ -234,14 +204,13 @@ export function selectedWidgetId(engine) {
|
|
|
234
204
|
export function onlyCardIsSelected(state) {
|
|
235
205
|
if (state.selectedIds.length !== 1)
|
|
236
206
|
return false;
|
|
237
|
-
|
|
238
|
-
return state.scene.some(
|
|
207
|
+
const selectedId = state.selectedIds[0];
|
|
208
|
+
return state.scene.some((object) => object.id === selectedId && object.type === 'widget');
|
|
239
209
|
}
|
|
240
210
|
/** The ids of the widgets that have a card on the board. */
|
|
241
211
|
export function linkedWidgetIds(engine) {
|
|
242
|
-
|
|
243
|
-
for (
|
|
244
|
-
var card = _a[_i];
|
|
212
|
+
const ids = new Set();
|
|
213
|
+
for (const card of cardsOf(engine.state.scene)) {
|
|
245
214
|
if (card.widgetId)
|
|
246
215
|
ids.add(card.widgetId);
|
|
247
216
|
}
|
|
@@ -251,10 +220,10 @@ export function linkedWidgetIds(engine) {
|
|
|
251
220
|
export function keepOnlyWidgetCard(engine, widgetId) {
|
|
252
221
|
if (!widgetId)
|
|
253
222
|
return 0;
|
|
254
|
-
|
|
223
|
+
const extras = cardsOf(engine.state.scene).filter((card) => card.widgetId && card.widgetId !== widgetId);
|
|
255
224
|
if (extras.length === 0)
|
|
256
225
|
return 0;
|
|
257
|
-
engine.removeObjects(extras.map(
|
|
226
|
+
engine.removeObjects(extras.map((card) => card.id));
|
|
258
227
|
engine.clearSelection();
|
|
259
228
|
return extras.length;
|
|
260
229
|
}
|
|
@@ -265,24 +234,23 @@ export function keepOnlyWidgetCard(engine, widgetId) {
|
|
|
265
234
|
export function removeSketchObjectsKeeping(engine, widgetId) {
|
|
266
235
|
if (!widgetId)
|
|
267
236
|
return 0;
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
for (
|
|
272
|
-
var card = _a[_i];
|
|
237
|
+
const scene = engine.state.scene;
|
|
238
|
+
const index = engine.sceneIndex(scene);
|
|
239
|
+
const keep = new Set();
|
|
240
|
+
for (const card of cardsOf(scene)) {
|
|
273
241
|
if (card.widgetId !== widgetId)
|
|
274
242
|
continue;
|
|
275
|
-
|
|
243
|
+
let current = card;
|
|
276
244
|
while (current && !keep.has(current.id)) {
|
|
277
245
|
keep.add(current.id);
|
|
278
246
|
current = current.parentId ? index.get(current.parentId) : undefined;
|
|
279
247
|
}
|
|
280
248
|
}
|
|
281
|
-
|
|
249
|
+
const doomed = scene.filter((object) => !keep.has(object.id) && object.type !== 'group').map((object) => object.id);
|
|
282
250
|
if (doomed.length === 0)
|
|
283
251
|
return 0;
|
|
284
252
|
engine.endTextEdit();
|
|
285
|
-
|
|
253
|
+
const next = removeFromScene(scene, doomed);
|
|
286
254
|
engine.applyScene(next, 'push');
|
|
287
255
|
engine.clearSelection();
|
|
288
256
|
return doomed.length;
|
|
@@ -292,20 +260,19 @@ export function removeSketchObjectsKeeping(engine, widgetId) {
|
|
|
292
260
|
* keeps it. Not an undo step and not a change of the board. Returns how many cards changed.
|
|
293
261
|
*/
|
|
294
262
|
export function hydrateWidgetMetadata(engine, widgetsById) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
var _a, _b;
|
|
263
|
+
let updated = 0;
|
|
264
|
+
const next = engine.state.scene.map((object) => {
|
|
298
265
|
if (object.type !== 'widget' || !object.widgetId)
|
|
299
266
|
return object;
|
|
300
|
-
|
|
267
|
+
const metadata = widgetsById.get(object.widgetId);
|
|
301
268
|
if (!metadata)
|
|
302
269
|
return object;
|
|
303
|
-
|
|
304
|
-
|
|
270
|
+
const preview = metadata.thumbnailUrl ?? metadata.thumbnail_url ?? null;
|
|
271
|
+
let card = object;
|
|
305
272
|
if (metadata.name && card.widgetName !== metadata.name)
|
|
306
|
-
card =
|
|
273
|
+
card = { ...card, widgetName: metadata.name };
|
|
307
274
|
if (preview && !card.thumbnail && !card.thumbnailRef)
|
|
308
|
-
card =
|
|
275
|
+
card = { ...card, thumbnail: preview };
|
|
309
276
|
if (card === object)
|
|
310
277
|
return object;
|
|
311
278
|
updated += 1;
|
|
@@ -320,13 +287,12 @@ export function hydrateWidgetMetadata(engine, widgetsById) {
|
|
|
320
287
|
* Returns how many cards changed their mark.
|
|
321
288
|
*/
|
|
322
289
|
export function markOrphanedWidgetCards(engine, validWidgetIds) {
|
|
323
|
-
|
|
324
|
-
for (
|
|
325
|
-
var card = _a[_i];
|
|
290
|
+
let changed = 0;
|
|
291
|
+
for (const card of cardsOf(engine.state.scene)) {
|
|
326
292
|
if (!card.widgetId)
|
|
327
293
|
continue;
|
|
328
|
-
|
|
329
|
-
if (engine.setWidgetRuntime(card.widgetId, { orphaned
|
|
294
|
+
const orphaned = !validWidgetIds.has(card.widgetId);
|
|
295
|
+
if (engine.setWidgetRuntime(card.widgetId, { orphaned }))
|
|
330
296
|
changed += 1;
|
|
331
297
|
}
|
|
332
298
|
return changed;
|
|
@@ -335,15 +301,15 @@ export function markOrphanedWidgetCards(engine, validWidgetIds) {
|
|
|
335
301
|
export function setPrimaryWidgetCard(engine, widgetId) {
|
|
336
302
|
if (!widgetId)
|
|
337
303
|
return false;
|
|
338
|
-
|
|
339
|
-
|
|
304
|
+
let changed = false;
|
|
305
|
+
const next = engine.state.scene.map((object) => {
|
|
340
306
|
if (object.type !== 'widget')
|
|
341
307
|
return object;
|
|
342
|
-
|
|
308
|
+
const primary = object.widgetId === widgetId;
|
|
343
309
|
if (object.isPrimary === primary)
|
|
344
310
|
return object;
|
|
345
311
|
changed = true;
|
|
346
|
-
return
|
|
312
|
+
return { ...object, isPrimary: primary };
|
|
347
313
|
});
|
|
348
314
|
if (changed)
|
|
349
315
|
engine.applyScene(next, 'replace');
|
|
@@ -351,23 +317,22 @@ export function setPrimaryWidgetCard(engine, widgetId) {
|
|
|
351
317
|
}
|
|
352
318
|
/** The widget the board is about, or null. */
|
|
353
319
|
export function primaryWidgetId(engine) {
|
|
354
|
-
|
|
355
|
-
return (_b = (_a = cardsOf(engine.state.scene).find(function (card) { return card.isPrimary; })) === null || _a === void 0 ? void 0 : _a.widgetId) !== null && _b !== void 0 ? _b : null;
|
|
320
|
+
return cardsOf(engine.state.scene).find((card) => card.isPrimary)?.widgetId ?? null;
|
|
356
321
|
}
|
|
357
322
|
/**
|
|
358
323
|
* Moves the card of a widget by a distance in screen pixels (the host's own bar drags it). Nothing is recorded
|
|
359
324
|
* until `commitWidgetCardMove`.
|
|
360
325
|
*/
|
|
361
326
|
export function moveWidgetCardByScreen(engine, widgetId, deltaScreenX, deltaScreenY) {
|
|
362
|
-
|
|
327
|
+
const card = findWidgetCard(engine, widgetId);
|
|
363
328
|
if (!card)
|
|
364
329
|
return false;
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
330
|
+
const zoom = engine.state.viewport.zoom || 1;
|
|
331
|
+
const deltaX = deltaScreenX / zoom;
|
|
332
|
+
const deltaY = deltaScreenY / zoom;
|
|
368
333
|
if (!Number.isFinite(deltaX) || !Number.isFinite(deltaY))
|
|
369
334
|
return false;
|
|
370
|
-
engine.applyScene(engine.state.scene.map(
|
|
335
|
+
engine.applyScene(engine.state.scene.map((object) => (object.id === card.id ? { ...card, x: card.x + deltaX, y: card.y + deltaY } : object)), 'transient');
|
|
371
336
|
return true;
|
|
372
337
|
}
|
|
373
338
|
/** The host's bar let go of a card: the move is one undo step. */
|
|
@@ -376,6 +341,5 @@ export function commitWidgetCardMove(engine) {
|
|
|
376
341
|
}
|
|
377
342
|
/** The bounds of what a conversion of the selection covers, or null when nothing is selected. */
|
|
378
343
|
export function selectionConversionBounds(engine) {
|
|
379
|
-
|
|
380
|
-
return (_a = visualBoundsOfUnits(engine.state.scene, engine.state.selectedIds)) !== null && _a !== void 0 ? _a : boundsOfObjects(engine.state.scene, engine.state.selectedIds);
|
|
344
|
+
return visualBoundsOfUnits(engine.state.scene, engine.state.selectedIds) ?? boundsOfObjects(engine.state.scene, engine.state.selectedIds);
|
|
381
345
|
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Ensures Inter is loaded before text is measured (item B14). Host pages usually ship Inter via the design system.
|
|
3
|
+
*
|
|
4
|
+
* The page's own `@font-face` for Inter is what the canvas waits for. A page that does not declare Inter in its
|
|
5
|
+
* stylesheets can give the canvas the WOFF2 addresses of the font (`interFontSources`, URLs or `data:` URLs), and the
|
|
6
|
+
* canvas registers them as the family `Inter`. On its own the canvas asks nobody for a font file: on a page that is not
|
|
7
|
+
* ours (a partner's) a request for a path of its origin is a 404 in its logs, or a violation of its `font-src`.
|
|
3
8
|
*/
|
|
4
9
|
export declare const INTER_FONT_FAMILY = "Inter";
|
|
5
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Registers the Inter files the host gave (none by default) and waits for Inter to be usable, the host's or the page's own.
|
|
12
|
+
* It never rejects: when a file cannot be loaded the page's fonts are used and one warning says so.
|
|
13
|
+
*/
|
|
14
|
+
export declare function ensureInterFontLoaded(sources?: readonly string[]): Promise<void>;
|
package/dist/fonts/interFont.js
CHANGED
|
@@ -1,82 +1,54 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Ensures Inter is loaded before text is measured (item B14). Host pages usually ship Inter via the design system.
|
|
3
|
+
*
|
|
4
|
+
* The page's own `@font-face` for Inter is what the canvas waits for. A page that does not declare Inter in its
|
|
5
|
+
* stylesheets can give the canvas the WOFF2 addresses of the font (`interFontSources`, URLs or `data:` URLs), and the
|
|
6
|
+
* canvas registers them as the family `Inter`. On its own the canvas asks nobody for a font file: on a page that is not
|
|
7
|
+
* ours (a partner's) a request for a path of its origin is a 404 in its logs, or a violation of its `font-src`.
|
|
3
8
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
17
|
-
function step(op) {
|
|
18
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
19
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
20
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
21
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
22
|
-
switch (op[0]) {
|
|
23
|
-
case 0: case 1: t = op; break;
|
|
24
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
25
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
26
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
27
|
-
default:
|
|
28
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
29
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
30
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
31
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
32
|
-
if (t[2]) _.ops.pop();
|
|
33
|
-
_.trys.pop(); continue;
|
|
34
|
-
}
|
|
35
|
-
op = body.call(thisArg, _);
|
|
36
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
37
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
export var INTER_FONT_FAMILY = 'Inter';
|
|
41
|
-
var INTER_SOURCES = [
|
|
42
|
-
"url('/fonts/Inter/InterVariable.woff2') format('woff2')",
|
|
43
|
-
"url('/fonts/Inter/Inter-Regular.woff2') format('woff2')"
|
|
44
|
-
];
|
|
45
|
-
var interLoad = null;
|
|
46
|
-
export function ensureInterFontLoaded() {
|
|
47
|
-
var _this = this;
|
|
9
|
+
export const INTER_FONT_FAMILY = 'Inter';
|
|
10
|
+
/** One load per list of addresses: the page's fonts are global, so asking twice for the same list changes nothing. */
|
|
11
|
+
const interLoads = new Map();
|
|
12
|
+
/** The `src` of a font face for one address the host gave. Quotes and backslashes are escaped, so an address cannot end the string. */
|
|
13
|
+
function toFontFaceSource(address) {
|
|
14
|
+
return `url("${address.replace(/["\\]/g, '\\$&')}") format('woff2')`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Registers the Inter files the host gave (none by default) and waits for Inter to be usable, the host's or the page's own.
|
|
18
|
+
* It never rejects: when a file cannot be loaded the page's fonts are used and one warning says so.
|
|
19
|
+
*/
|
|
20
|
+
export function ensureInterFontLoaded(sources = []) {
|
|
48
21
|
if (typeof document === 'undefined')
|
|
49
22
|
return Promise.resolve();
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
case 5:
|
|
74
|
-
error_1 = _b.sent();
|
|
75
|
-
console.warn('Canvas: Inter could not be preloaded, fallback fonts will be used', error_1);
|
|
76
|
-
return [3 /*break*/, 6];
|
|
77
|
-
case 6: return [2 /*return*/];
|
|
23
|
+
const loadKey = sources.join('\n');
|
|
24
|
+
const existingLoad = interLoads.get(loadKey);
|
|
25
|
+
if (existingLoad)
|
|
26
|
+
return existingLoad;
|
|
27
|
+
const load = (async () => {
|
|
28
|
+
if (sources.length > 0 && typeof FontFace !== 'undefined' && document.fonts?.add) {
|
|
29
|
+
try {
|
|
30
|
+
const face = new FontFace(INTER_FONT_FAMILY, sources.map(toFontFaceSource).join(', '), {
|
|
31
|
+
weight: '100 900',
|
|
32
|
+
style: 'normal',
|
|
33
|
+
display: 'swap'
|
|
34
|
+
});
|
|
35
|
+
const loaded = await face.load();
|
|
36
|
+
document.fonts.add(loaded);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
console.warn('Canvas: the Inter files the page gave could not be loaded, the fonts of the page are used', error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// A failed file above must not skip this: it waits for whatever Inter the page declares itself.
|
|
43
|
+
try {
|
|
44
|
+
if (document.fonts?.load) {
|
|
45
|
+
await document.fonts.load('16px Inter');
|
|
78
46
|
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
console.warn('Canvas: Inter could not be preloaded, fallback fonts will be used', error);
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
interLoads.set(loadKey, load);
|
|
53
|
+
return load;
|
|
82
54
|
}
|