@widgetic/canvas 0.5.44 → 0.5.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/canvas/DomCanvas.svelte +52 -3
- package/dist/canvas/DomCanvas.svelte.d.ts +59 -34
- package/dist/canvas/host/CanvasHost.d.ts +74 -3
- package/dist/canvas/host/CanvasHost.js +278 -16
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +9 -1
- package/dist/canvas/host/boardFiles.d.ts +90 -0
- package/dist/canvas/host/boardFiles.js +139 -0
- package/dist/canvas/host/browserServices.d.ts +17 -0
- package/dist/canvas/host/browserServices.js +58 -0
- package/dist/canvas/index.d.ts +2 -1
- package/dist/canvas/types.d.ts +7 -1
- package/dist/canvas/ui/CanvasContextMenu.svelte +7 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +4 -1
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +2 -0
- package/dist/canvas/ui/SelectionPanel.svelte +78 -17
- package/dist/canvas/ui/ShapesMenu.svelte +65 -31
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +2 -0
- package/dist/canvas/ui/ShortcutTooltip.svelte +64 -0
- package/dist/canvas/ui/ShortcutTooltip.svelte.d.ts +24 -0
- package/dist/canvas/ui/ToolButton.svelte +34 -55
- package/dist/canvas/ui/ToolButton.svelte.d.ts +4 -1
- package/dist/canvas/ui/floatingCard.d.ts +26 -4
- package/dist/canvas/ui/floatingCard.js +26 -4
- package/dist/canvas/ui/icons.d.ts +1 -1
- package/dist/canvas/ui/icons.js +1 -1
- package/dist/canvas/ui/selectionTraits.d.ts +12 -0
- package/dist/canvas/ui/selectionTraits.js +26 -1
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +4 -2
- package/dist/canvas/ui/sizeLabelVisibility.js +5 -5
- package/dist/canvas/view/CanvasStage.svelte +7 -2
- package/dist/canvas/view/CanvasStage.svelte.d.ts +2 -0
- package/dist/canvas/view/SelectionOverlay.svelte +17 -10
- package/dist/canvas/view/WidgetChromeLayer.svelte +15 -3
- package/dist/canvas/view/geometry.d.ts +1 -1
- package/dist/canvas/view/geometry.js +1 -1
- package/dist/components/CanvasBoardPlaceholder.svelte +68 -10
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +10 -2
- package/dist/components/CanvasBoardPoster.svelte +206 -0
- package/dist/components/CanvasBoardPoster.svelte.d.ts +17 -0
- package/dist/components/CanvasToolbarPlaceholder.svelte +129 -0
- package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +15 -0
- package/dist/components/boardLoaderDelay.d.ts +39 -0
- package/dist/components/boardLoaderDelay.js +71 -0
- package/dist/components/index.d.ts +7 -0
- package/dist/components/index.js +8 -0
- package/dist/engine/EngineCore.d.ts +46 -4
- package/dist/engine/EngineCore.js +85 -16
- package/dist/engine/codec/canvasDocument.d.ts +7 -0
- package/dist/engine/codec/canvasDocument.js +25 -2
- package/dist/engine/core/arrow.d.ts +18 -8
- package/dist/engine/core/arrow.js +24 -12
- package/dist/engine/core/arrowBinding.d.ts +1 -1
- package/dist/engine/core/arrowBinding.js +10 -3
- package/dist/engine/core/arrowBindingRecords.d.ts +9 -3
- package/dist/engine/core/arrowBindingRecords.js +10 -6
- package/dist/engine/core/brush.d.ts +8 -0
- package/dist/engine/core/brush.js +10 -2
- package/dist/engine/core/cameraMove.d.ts +6 -0
- package/dist/engine/core/cameraMove.js +6 -0
- package/dist/engine/core/defaults.js +2 -0
- package/dist/engine/core/scene.d.ts +22 -0
- package/dist/engine/core/scene.js +86 -1
- package/dist/engine/core/style.d.ts +10 -0
- package/dist/engine/core/style.js +19 -3
- package/dist/engine/core/transform.d.ts +6 -0
- package/dist/engine/core/transform.js +9 -0
- package/dist/engine/core/types.d.ts +7 -0
- package/dist/engine/input/PointerController.d.ts +15 -1
- package/dist/engine/input/PointerController.js +70 -12
- package/dist/engine/input/gestures.d.ts +3 -1
- package/dist/engine/input/gestures.js +41 -10
- package/dist/engine/input/keyboard.d.ts +22 -3
- package/dist/engine/input/keyboard.js +100 -5
- package/dist/engine/input/types.d.ts +7 -0
- package/dist/engine/state.d.ts +18 -0
- package/dist/engine/state.js +10 -0
- package/dist/fonts/workspaceFonts.d.ts +6 -0
- package/dist/fonts/workspaceFonts.js +44 -29
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +6 -0
- package/dist/i18n/messages.js +6 -0
- package/dist/i18n/shortcuts.d.ts +6 -4
- package/dist/i18n/shortcuts.js +16 -12
- package/dist/index.d.ts +2 -1
- package/dist/poster/boardPlacement.d.ts +80 -0
- package/dist/poster/boardPlacement.js +138 -0
- package/dist/poster/boardPoster.d.ts +80 -0
- package/dist/poster/boardPoster.js +226 -0
- package/dist/poster/paintBoardPoster.d.ts +49 -0
- package/dist/poster/paintBoardPoster.js +89 -0
- package/dist/poster/posterPictures.d.ts +32 -0
- package/dist/poster/posterPictures.js +122 -0
- package/dist/settings/rememberedTool.d.ts +50 -0
- package/dist/settings/rememberedTool.js +68 -0
- package/dist/settings/workspaceToolSettings.d.ts +14 -0
- package/dist/settings/workspaceToolSettings.js +21 -10
- package/dist/tools/definitions.js +26 -4
- package/dist/tools/executors.js +81 -5
- package/dist/tools/hostAdapter.d.ts +5 -1
- package/dist/tools/hostAdapter.js +2 -0
- package/dist/tools/styleArguments.d.ts +35 -0
- package/dist/tools/styleArguments.js +91 -0
- package/dist/tools/types.d.ts +26 -1
- package/dist/tools/types.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The board poster: a small, static copy of the part of a saved board that was on the screen, kept so that a page can paint it
|
|
3
|
+
* before the canvas engine is there.
|
|
4
|
+
*
|
|
5
|
+
* Why it exists. The saved board is already kept next to the page (the creator's Dexie copy, the Home page's draft), but nothing
|
|
6
|
+
* can draw an object from it until the canvas code has been downloaded, mounted and has read the saved text, and that comes after
|
|
7
|
+
* the empty board is on the screen. So on a refresh the board showed first and its objects a moment later. The poster closes
|
|
8
|
+
* that gap: the page paints the objects that were in the saved view from the first frame, with light code (`paintBoardPoster`),
|
|
9
|
+
* and the real board takes over when it has painted the same objects (`CanvasBoardPoster`).
|
|
10
|
+
*
|
|
11
|
+
* What it holds. The saved records themselves (the same text `parseCanvasDocument` reads) of the shapes whose box touches the
|
|
12
|
+
* saved view, in their z-order, and that view (`viewport`). It is made from the saved text (`createBoardPoster`), so the page that
|
|
13
|
+
* keeps it needs no scene, and it is read back through the same reader (`parseBoardPoster`), so a poster that is damaged, old or
|
|
14
|
+
* written by something else is checked like any saved board and never draws more than a saved board could.
|
|
15
|
+
*
|
|
16
|
+
* What it leaves out, on purpose: groups (they draw nothing), pictures whose bytes live in the host's image store (the poster has
|
|
17
|
+
* no store; a picture with an address of its own, such as a file of the library, is drawn), and whatever is beyond the caps below.
|
|
18
|
+
* It is a first impression of the board, not a second copy of it: the saved board stays the one that is edited and saved.
|
|
19
|
+
*
|
|
20
|
+
* Nothing here touches the DOM or the page, so it runs in Node tests.
|
|
21
|
+
*/
|
|
22
|
+
import type { ShapeObject, Size, Viewport } from '../engine/core/types.js';
|
|
23
|
+
/** The version of the poster's own format: a poster of another version is not read (the next visit makes a new one). */
|
|
24
|
+
export declare const BOARD_POSTER_VERSION: 1;
|
|
25
|
+
/**
|
|
26
|
+
* The kind a page keeps the poster under in its cache (the canvas id is the key). The creator, which writes it, and the page that
|
|
27
|
+
* reads it before the creator is there agree on this one name, so it lives with the format.
|
|
28
|
+
*/
|
|
29
|
+
export declare const BOARD_POSTER_STORAGE_KIND = "canvasBoardPoster";
|
|
30
|
+
/** The size of the screen the saved view is taken to have when the page does not say (a large window). */
|
|
31
|
+
export declare const DEFAULT_POSTER_SURFACE: Readonly<Size>;
|
|
32
|
+
/** The most objects a poster holds; a board with more keeps the ones nearest the middle of the view. */
|
|
33
|
+
export declare const POSTER_MAX_OBJECTS = 400;
|
|
34
|
+
/** The most bytes of saved text a poster holds (what is kept next to the page stays small, and reading it stays fast). */
|
|
35
|
+
export declare const POSTER_MAX_BYTES = 240000;
|
|
36
|
+
/** Inline picture bytes longer than this are left out of the poster (the picture appears with the board). */
|
|
37
|
+
export declare const POSTER_MAX_INLINE_PICTURE = 24000;
|
|
38
|
+
/** Screen pixels kept around the saved view on every side, so a small pan, or a larger window than last time, still has its objects. */
|
|
39
|
+
export declare const POSTER_MARGIN = 160;
|
|
40
|
+
/** What a page keeps: the saved view and the saved records that touch it. Plain JSON, so any store can hold it. */
|
|
41
|
+
export interface BoardPoster {
|
|
42
|
+
version: typeof BOARD_POSTER_VERSION;
|
|
43
|
+
/** Where the world was on the board when it was saved: the same view the board opens at. */
|
|
44
|
+
viewport: Viewport;
|
|
45
|
+
/** The size of the board the objects were chosen for, in CSS pixels (what the saved view showed). */
|
|
46
|
+
surface: Size;
|
|
47
|
+
/** The saved records (`canvas.objects` of the saved text), bottom first. */
|
|
48
|
+
objects: ReadonlyArray<Record<string, unknown>>;
|
|
49
|
+
}
|
|
50
|
+
export interface BoardPosterOptions {
|
|
51
|
+
/** The size of the board the saved view is on, when the page knows it. */
|
|
52
|
+
surface?: Size;
|
|
53
|
+
/** Lowers `POSTER_MAX_OBJECTS`. */
|
|
54
|
+
maxObjects?: number;
|
|
55
|
+
/** Lowers `POSTER_MAX_BYTES`. */
|
|
56
|
+
maxBytes?: number;
|
|
57
|
+
}
|
|
58
|
+
/** A poster read back: the view and the shapes, checked and ready to draw. */
|
|
59
|
+
export interface ParsedBoardPoster {
|
|
60
|
+
viewport: Viewport;
|
|
61
|
+
surface: Size;
|
|
62
|
+
shapes: ShapeObject[];
|
|
63
|
+
}
|
|
64
|
+
/** A surface the page named, kept to sizes a board can have; the default when it named none or nonsense. */
|
|
65
|
+
export declare function sanitizeSurface(value: unknown): Size;
|
|
66
|
+
/**
|
|
67
|
+
* The poster of a saved board: the records that touch the view it was saved at, or null when there is nothing to show (a text that
|
|
68
|
+
* is not a saved board, an empty board, nothing in view). `source` is the saved text, or the same thing already parsed.
|
|
69
|
+
*/
|
|
70
|
+
export declare function createBoardPoster(source: unknown, options?: BoardPosterOptions): BoardPoster | null;
|
|
71
|
+
/**
|
|
72
|
+
* A poster read back from whatever a page kept. Null when it is not a poster of this version or holds nothing that can be drawn.
|
|
73
|
+
* The records go through the canvas's own reader, so every number, colour and address is checked the way a saved board's are.
|
|
74
|
+
*/
|
|
75
|
+
export declare function parseBoardPoster(value: unknown): ParsedBoardPoster | null;
|
|
76
|
+
/**
|
|
77
|
+
* Whether two posters are the same, for a page that keeps the latest and writes only what changed. Compares what would be written,
|
|
78
|
+
* so it stays right whichever of the fields the caller touched.
|
|
79
|
+
*/
|
|
80
|
+
export declare function boardPostersMatch(first: BoardPoster | null, second: BoardPoster | null): boolean;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The board poster: a small, static copy of the part of a saved board that was on the screen, kept so that a page can paint it
|
|
3
|
+
* before the canvas engine is there.
|
|
4
|
+
*
|
|
5
|
+
* Why it exists. The saved board is already kept next to the page (the creator's Dexie copy, the Home page's draft), but nothing
|
|
6
|
+
* can draw an object from it until the canvas code has been downloaded, mounted and has read the saved text, and that comes after
|
|
7
|
+
* the empty board is on the screen. So on a refresh the board showed first and its objects a moment later. The poster closes
|
|
8
|
+
* that gap: the page paints the objects that were in the saved view from the first frame, with light code (`paintBoardPoster`),
|
|
9
|
+
* and the real board takes over when it has painted the same objects (`CanvasBoardPoster`).
|
|
10
|
+
*
|
|
11
|
+
* What it holds. The saved records themselves (the same text `parseCanvasDocument` reads) of the shapes whose box touches the
|
|
12
|
+
* saved view, in their z-order, and that view (`viewport`). It is made from the saved text (`createBoardPoster`), so the page that
|
|
13
|
+
* keeps it needs no scene, and it is read back through the same reader (`parseBoardPoster`), so a poster that is damaged, old or
|
|
14
|
+
* written by something else is checked like any saved board and never draws more than a saved board could.
|
|
15
|
+
*
|
|
16
|
+
* What it leaves out, on purpose: groups (they draw nothing), pictures whose bytes live in the host's image store (the poster has
|
|
17
|
+
* no store; a picture with an address of its own, such as a file of the library, is drawn), and whatever is beyond the caps below.
|
|
18
|
+
* It is a first impression of the board, not a second copy of it: the saved board stays the one that is edited and saved.
|
|
19
|
+
*
|
|
20
|
+
* Nothing here touches the DOM or the page, so it runs in Node tests.
|
|
21
|
+
*/
|
|
22
|
+
import { parseCanvasDocument } from '../engine/codec/canvasDocument.js';
|
|
23
|
+
import { HOST_READABLE_KEYS } from '../engine/codec/hostReadableKeys.js';
|
|
24
|
+
import { brushReach, brushSizeFor } from '../engine/core/freehand.js';
|
|
25
|
+
import { inflateRect, rectCenter, rectsIntersect, rotatedBoxBounds } from '../engine/core/geometry.js';
|
|
26
|
+
import { CANVAS_DOCUMENT_VERSION, isBoxedObject } from '../engine/core/types.js';
|
|
27
|
+
import { sanitizeViewport, visibleWorldRect } from '../engine/core/viewport.js';
|
|
28
|
+
/** The version of the poster's own format: a poster of another version is not read (the next visit makes a new one). */
|
|
29
|
+
export const BOARD_POSTER_VERSION = 1;
|
|
30
|
+
/**
|
|
31
|
+
* The kind a page keeps the poster under in its cache (the canvas id is the key). The creator, which writes it, and the page that
|
|
32
|
+
* reads it before the creator is there agree on this one name, so it lives with the format.
|
|
33
|
+
*/
|
|
34
|
+
export const BOARD_POSTER_STORAGE_KIND = 'canvasBoardPoster';
|
|
35
|
+
/** The size of the screen the saved view is taken to have when the page does not say (a large window). */
|
|
36
|
+
export const DEFAULT_POSTER_SURFACE = Object.freeze({ width: 1600, height: 1000 });
|
|
37
|
+
/** The most objects a poster holds; a board with more keeps the ones nearest the middle of the view. */
|
|
38
|
+
export const POSTER_MAX_OBJECTS = 400;
|
|
39
|
+
/** The most bytes of saved text a poster holds (what is kept next to the page stays small, and reading it stays fast). */
|
|
40
|
+
export const POSTER_MAX_BYTES = 240_000;
|
|
41
|
+
/** Inline picture bytes longer than this are left out of the poster (the picture appears with the board). */
|
|
42
|
+
export const POSTER_MAX_INLINE_PICTURE = 24_000;
|
|
43
|
+
/** Screen pixels kept around the saved view on every side, so a small pan, or a larger window than last time, still has its objects. */
|
|
44
|
+
export const POSTER_MARGIN = 160;
|
|
45
|
+
/** The most records `parseBoardPoster` reads, whatever a poster says it holds. */
|
|
46
|
+
const POSTER_READ_LIMIT = 2_000;
|
|
47
|
+
const MIN_SURFACE_SIDE = 200;
|
|
48
|
+
const MAX_SURFACE_SIDE = 8_000;
|
|
49
|
+
/** The kinds of object a poster draws (a group has no look of its own). */
|
|
50
|
+
const DRAWN_TYPES = new Set(['rect', 'ellipse', 'path', 'text', 'image', 'widget']);
|
|
51
|
+
/** The keys that may hold the bytes of a picture inline. */
|
|
52
|
+
const INLINE_PICTURE_KEYS = ['src', 'thumbnail'];
|
|
53
|
+
function isRecord(value) {
|
|
54
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
55
|
+
}
|
|
56
|
+
function finiteNumber(value, fallback) {
|
|
57
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
58
|
+
}
|
|
59
|
+
/** The saved text as a value: text is parsed (and a text that is not JSON is nothing), anything else is taken as it is. */
|
|
60
|
+
function readJson(source) {
|
|
61
|
+
if (typeof source !== 'string')
|
|
62
|
+
return source;
|
|
63
|
+
if (source.trim() === '')
|
|
64
|
+
return null;
|
|
65
|
+
try {
|
|
66
|
+
return JSON.parse(source);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** A surface the page named, kept to sizes a board can have; the default when it named none or nonsense. */
|
|
73
|
+
export function sanitizeSurface(value) {
|
|
74
|
+
if (!isRecord(value))
|
|
75
|
+
return { ...DEFAULT_POSTER_SURFACE };
|
|
76
|
+
const width = finiteNumber(value.width, DEFAULT_POSTER_SURFACE.width);
|
|
77
|
+
const height = finiteNumber(value.height, DEFAULT_POSTER_SURFACE.height);
|
|
78
|
+
return {
|
|
79
|
+
width: Math.min(MAX_SURFACE_SIDE, Math.max(MIN_SURFACE_SIDE, Math.round(width))),
|
|
80
|
+
height: Math.min(MAX_SURFACE_SIDE, Math.max(MIN_SURFACE_SIDE, Math.round(height)))
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** A saved record without the picture bytes that are too long to keep (the same record when it has none). */
|
|
84
|
+
function withoutLongPictures(record) {
|
|
85
|
+
let result = record;
|
|
86
|
+
for (const key of INLINE_PICTURE_KEYS) {
|
|
87
|
+
const value = record[key];
|
|
88
|
+
if (typeof value === 'string' && value.startsWith('data:') && value.length > POSTER_MAX_INLINE_PICTURE) {
|
|
89
|
+
if (result === record)
|
|
90
|
+
result = { ...record };
|
|
91
|
+
delete result[key];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The rectangle a saved record may paint in, read from its raw numbers (the reader's own defaults for what is missing), with
|
|
98
|
+
* room for its line and for a turn. Generous on purpose: it only decides what is kept, and the poster is drawn clipped anyway.
|
|
99
|
+
*/
|
|
100
|
+
function roughBoundsOf(record) {
|
|
101
|
+
const strokeWidth = Math.min(200, Math.max(0, finiteNumber(record.strokeWidth, 3)));
|
|
102
|
+
const isBrush = isRecord(record.ink);
|
|
103
|
+
const reach = isBrush ? brushReach(brushSizeFor(strokeWidth)) : strokeWidth * Math.SQRT1_2 + 1;
|
|
104
|
+
const box = {
|
|
105
|
+
x: finiteNumber(record.x, 0),
|
|
106
|
+
y: finiteNumber(record.y, 0),
|
|
107
|
+
width: Math.max(1, finiteNumber(record.width, 100)),
|
|
108
|
+
height: Math.max(1, finiteNumber(record.height, 100)),
|
|
109
|
+
rotation: finiteNumber(record.rotation, 0)
|
|
110
|
+
};
|
|
111
|
+
return inflateRect(rotatedBoxBounds(box), reach);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The poster of a saved board: the records that touch the view it was saved at, or null when there is nothing to show (a text that
|
|
115
|
+
* is not a saved board, an empty board, nothing in view). `source` is the saved text, or the same thing already parsed.
|
|
116
|
+
*/
|
|
117
|
+
export function createBoardPoster(source, options = {}) {
|
|
118
|
+
const savedDocument = readJson(source);
|
|
119
|
+
if (!isRecord(savedDocument) || savedDocument.version !== CANVAS_DOCUMENT_VERSION)
|
|
120
|
+
return null;
|
|
121
|
+
const canvas = savedDocument.canvas;
|
|
122
|
+
const list = isRecord(canvas) && Array.isArray(canvas.objects) ? canvas.objects : [];
|
|
123
|
+
if (list.length === 0)
|
|
124
|
+
return null;
|
|
125
|
+
const viewport = sanitizeViewport(savedDocument.viewport);
|
|
126
|
+
const surface = sanitizeSurface(options.surface);
|
|
127
|
+
const maxObjects = Math.max(1, Math.min(POSTER_MAX_OBJECTS, Math.floor(finiteNumber(options.maxObjects, POSTER_MAX_OBJECTS))));
|
|
128
|
+
const maxBytes = Math.max(1_000, Math.min(POSTER_MAX_BYTES, Math.floor(finiteNumber(options.maxBytes, POSTER_MAX_BYTES))));
|
|
129
|
+
// What the saved view showed, with room around it, in world units.
|
|
130
|
+
const keptArea = inflateRect(visibleWorldRect(viewport, surface), POSTER_MARGIN / viewport.zoom);
|
|
131
|
+
const middle = rectCenter(keptArea);
|
|
132
|
+
const candidates = [];
|
|
133
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
134
|
+
const raw = list[index];
|
|
135
|
+
if (!isRecord(raw))
|
|
136
|
+
continue;
|
|
137
|
+
const type = raw[HOST_READABLE_KEYS.objectType];
|
|
138
|
+
if (typeof type !== 'string' || !DRAWN_TYPES.has(type))
|
|
139
|
+
continue;
|
|
140
|
+
// The reader treats these as "not there"; so does the poster.
|
|
141
|
+
if (raw[HOST_READABLE_KEYS.visible] === false || raw[HOST_READABLE_KEYS.excludeFromExport] === true)
|
|
142
|
+
continue;
|
|
143
|
+
const bounds = roughBoundsOf(raw);
|
|
144
|
+
if (!rectsIntersect(bounds, keptArea))
|
|
145
|
+
continue;
|
|
146
|
+
const record = withoutLongPictures(raw);
|
|
147
|
+
let bytes = 0;
|
|
148
|
+
try {
|
|
149
|
+
bytes = JSON.stringify(record).length;
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// A record that cannot be written down cannot be kept.
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const center = rectCenter(bounds);
|
|
156
|
+
candidates.push({ index, record, bytes, distance: (center.x - middle.x) ** 2 + (center.y - middle.y) ** 2 });
|
|
157
|
+
}
|
|
158
|
+
if (candidates.length === 0)
|
|
159
|
+
return null;
|
|
160
|
+
let kept = candidates;
|
|
161
|
+
const totalBytes = candidates.reduce((total, candidate) => total + candidate.bytes, 0);
|
|
162
|
+
if (candidates.length > maxObjects || totalBytes > maxBytes) {
|
|
163
|
+
// Too much for a poster: keep what is nearest the middle of the view, as long as it fits, and put it back in z-order.
|
|
164
|
+
const byDistance = [...candidates].sort((first, second) => first.distance - second.distance);
|
|
165
|
+
kept = [];
|
|
166
|
+
let usedBytes = 0;
|
|
167
|
+
for (const candidate of byDistance) {
|
|
168
|
+
if (kept.length >= maxObjects || usedBytes + candidate.bytes > maxBytes)
|
|
169
|
+
continue;
|
|
170
|
+
kept.push(candidate);
|
|
171
|
+
usedBytes += candidate.bytes;
|
|
172
|
+
}
|
|
173
|
+
kept.sort((first, second) => first.index - second.index);
|
|
174
|
+
if (kept.length === 0)
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
return { version: BOARD_POSTER_VERSION, viewport, surface, objects: kept.map((candidate) => candidate.record) };
|
|
178
|
+
}
|
|
179
|
+
/** The address a picture is drawn from: its own, or the library's when the board's own is gone (an object URL does not outlive a page). */
|
|
180
|
+
function pictureAddressOf(shape) {
|
|
181
|
+
if (shape.src && !shape.src.startsWith('blob:'))
|
|
182
|
+
return shape.src;
|
|
183
|
+
return shape.libraryFileUrl ?? null;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* A poster read back from whatever a page kept. Null when it is not a poster of this version or holds nothing that can be drawn.
|
|
187
|
+
* The records go through the canvas's own reader, so every number, colour and address is checked the way a saved board's are.
|
|
188
|
+
*/
|
|
189
|
+
export function parseBoardPoster(value) {
|
|
190
|
+
if (!isRecord(value) || value.version !== BOARD_POSTER_VERSION)
|
|
191
|
+
return null;
|
|
192
|
+
if (!Array.isArray(value.objects) || value.objects.length === 0 || value.objects.length > POSTER_READ_LIMIT)
|
|
193
|
+
return null;
|
|
194
|
+
const parsed = parseCanvasDocument({
|
|
195
|
+
version: CANVAS_DOCUMENT_VERSION,
|
|
196
|
+
viewport: value.viewport,
|
|
197
|
+
canvas: { objects: value.objects }
|
|
198
|
+
});
|
|
199
|
+
if (parsed.status !== 'ok')
|
|
200
|
+
return null;
|
|
201
|
+
const shapes = [];
|
|
202
|
+
for (const object of parsed.scene) {
|
|
203
|
+
if (!isBoxedObject(object))
|
|
204
|
+
continue;
|
|
205
|
+
shapes.push(object.type === 'image' ? { ...object, src: pictureAddressOf(object) } : object);
|
|
206
|
+
}
|
|
207
|
+
if (shapes.length === 0)
|
|
208
|
+
return null;
|
|
209
|
+
return { viewport: parsed.viewport, surface: sanitizeSurface(value.surface), shapes };
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Whether two posters are the same, for a page that keeps the latest and writes only what changed. Compares what would be written,
|
|
213
|
+
* so it stays right whichever of the fields the caller touched.
|
|
214
|
+
*/
|
|
215
|
+
export function boardPostersMatch(first, second) {
|
|
216
|
+
if (first === second)
|
|
217
|
+
return true;
|
|
218
|
+
if (!first || !second)
|
|
219
|
+
return false;
|
|
220
|
+
try {
|
|
221
|
+
return JSON.stringify(first) === JSON.stringify(second);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawing a board poster onto a 2D canvas.
|
|
3
|
+
*
|
|
4
|
+
* The shapes are drawn by the engine's own `paintShape` (the one that makes the picture the AI is shown), at the view the poster
|
|
5
|
+
* was saved with, so what the poster shows is what the board shows: the same box, rotation, fill, stroke, dash, brush outline
|
|
6
|
+
* and the lines of a text from `layoutText`. This file only sets the view and walks the shapes. It touches neither `document` nor
|
|
7
|
+
* `window`: the context and the services are handed in, so the drawing order and the geometry are tested in Node with a recording
|
|
8
|
+
* context.
|
|
9
|
+
*/
|
|
10
|
+
import type { PaintContext, PaintDependencies } from '../engine/render/rasterize.js';
|
|
11
|
+
import type { MeasureText } from '../engine/text/layoutText.js';
|
|
12
|
+
import type { ParsedBoardPoster } from './boardPoster.js';
|
|
13
|
+
/** The area a poster is painted into. */
|
|
14
|
+
export interface PosterPaintTarget {
|
|
15
|
+
/** The size of the area, in CSS pixels (the board it stands in for). */
|
|
16
|
+
width: number;
|
|
17
|
+
/** See `width`. */
|
|
18
|
+
height: number;
|
|
19
|
+
/** Device pixels per CSS pixel: the context is `width × pixelRatio` pixels wide. */
|
|
20
|
+
pixelRatio: number;
|
|
21
|
+
}
|
|
22
|
+
/** The addresses of the pictures a poster needs (its images, and the screenshots of its widget cards). */
|
|
23
|
+
export declare function posterPictureSources(poster: ParsedBoardPoster): string[];
|
|
24
|
+
/**
|
|
25
|
+
* The references of the pictures a poster draws that the board keeps in its host's store (a photo dropped on the board, the
|
|
26
|
+
* screenshot of a widget card): they have no address in the saved text, only the reference the host knows them by. Once each.
|
|
27
|
+
*/
|
|
28
|
+
export declare function posterPictureReferences(poster: ParsedBoardPoster): string[];
|
|
29
|
+
/**
|
|
30
|
+
* The poster with each picture that is kept by reference named by its key (`posterReferenceKey`), which is what the painter asks
|
|
31
|
+
* the loaded pictures for. The same poster when there is none. A picture that has an address keeps it.
|
|
32
|
+
*/
|
|
33
|
+
export declare function withReferencedPictures(poster: ParsedBoardPoster): ParsedBoardPoster;
|
|
34
|
+
/**
|
|
35
|
+
* Paints the poster: clears the area, sets the saved view, and draws the shapes that touch the area, bottom first. A shape that
|
|
36
|
+
* cannot be drawn is left out and the rest are drawn. Returns how many shapes it handed to the painter (one that is see-through is
|
|
37
|
+
* handed over, and the painter draws nothing of it).
|
|
38
|
+
*/
|
|
39
|
+
export declare function paintBoardPoster(context: PaintContext, poster: ParsedBoardPoster, target: PosterPaintTarget, dependencies: PaintDependencies): number;
|
|
40
|
+
/** What `paintShape` needs, made of the browser's canvas (or of what a test hands in). */
|
|
41
|
+
export interface PosterServices {
|
|
42
|
+
/** The picture behind an address, once it has loaded (null while it has not, or when it cannot). */
|
|
43
|
+
pictureFor(source: string | null | undefined): unknown | null;
|
|
44
|
+
/** A path object for the `d` of an SVG path. */
|
|
45
|
+
createPath(data: string): unknown;
|
|
46
|
+
/** Measures a line of text in a CSS font; leave out for a fixed advance (a server, a test). */
|
|
47
|
+
measureText?: MeasureText;
|
|
48
|
+
}
|
|
49
|
+
export declare function posterPaintDependencies(services: PosterServices): PaintDependencies;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawing a board poster onto a 2D canvas.
|
|
3
|
+
*
|
|
4
|
+
* The shapes are drawn by the engine's own `paintShape` (the one that makes the picture the AI is shown), at the view the poster
|
|
5
|
+
* was saved with, so what the poster shows is what the board shows: the same box, rotation, fill, stroke, dash, brush outline
|
|
6
|
+
* and the lines of a text from `layoutText`. This file only sets the view and walks the shapes. It touches neither `document` nor
|
|
7
|
+
* `window`: the context and the services are handed in, so the drawing order and the geometry are tested in Node with a recording
|
|
8
|
+
* context.
|
|
9
|
+
*/
|
|
10
|
+
import { inflateRect, rectsIntersect } from '../engine/core/geometry.js';
|
|
11
|
+
import { shapeVisualBounds } from '../engine/core/scene.js';
|
|
12
|
+
import { visibleWorldRect } from '../engine/core/viewport.js';
|
|
13
|
+
import { imageSourcesOf, paintShape } from '../engine/render/rasterize.js';
|
|
14
|
+
import { createFixedAdvanceMeasure } from '../engine/text/layoutText.js';
|
|
15
|
+
import { posterReferenceKey } from './posterPictures.js';
|
|
16
|
+
/** Screen pixels around the area that still get painted, so a stroke that starts just outside it has its edge. */
|
|
17
|
+
const PAINT_MARGIN = 64;
|
|
18
|
+
/** The addresses of the pictures a poster needs (its images, and the screenshots of its widget cards). */
|
|
19
|
+
export function posterPictureSources(poster) {
|
|
20
|
+
return imageSourcesOf(poster.shapes);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The references of the pictures a poster draws that the board keeps in its host's store (a photo dropped on the board, the
|
|
24
|
+
* screenshot of a widget card): they have no address in the saved text, only the reference the host knows them by. Once each.
|
|
25
|
+
*/
|
|
26
|
+
export function posterPictureReferences(poster) {
|
|
27
|
+
const references = new Set();
|
|
28
|
+
for (const shape of poster.shapes) {
|
|
29
|
+
if (shape.type === 'image' && !shape.src && shape.imageRef)
|
|
30
|
+
references.add(shape.imageRef);
|
|
31
|
+
if (shape.type === 'widget' && !shape.thumbnail && shape.thumbnailRef)
|
|
32
|
+
references.add(shape.thumbnailRef);
|
|
33
|
+
}
|
|
34
|
+
return [...references];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The poster with each picture that is kept by reference named by its key (`posterReferenceKey`), which is what the painter asks
|
|
38
|
+
* the loaded pictures for. The same poster when there is none. A picture that has an address keeps it.
|
|
39
|
+
*/
|
|
40
|
+
export function withReferencedPictures(poster) {
|
|
41
|
+
let changed = false;
|
|
42
|
+
const shapes = poster.shapes.map((shape) => {
|
|
43
|
+
if (shape.type === 'image' && !shape.src && shape.imageRef) {
|
|
44
|
+
changed = true;
|
|
45
|
+
return { ...shape, src: posterReferenceKey(shape.imageRef) };
|
|
46
|
+
}
|
|
47
|
+
if (shape.type === 'widget' && !shape.thumbnail && shape.thumbnailRef) {
|
|
48
|
+
changed = true;
|
|
49
|
+
return { ...shape, thumbnail: posterReferenceKey(shape.thumbnailRef) };
|
|
50
|
+
}
|
|
51
|
+
return shape;
|
|
52
|
+
});
|
|
53
|
+
return changed ? { ...poster, shapes } : poster;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Paints the poster: clears the area, sets the saved view, and draws the shapes that touch the area, bottom first. A shape that
|
|
57
|
+
* cannot be drawn is left out and the rest are drawn. Returns how many shapes it handed to the painter (one that is see-through is
|
|
58
|
+
* handed over, and the painter draws nothing of it).
|
|
59
|
+
*/
|
|
60
|
+
export function paintBoardPoster(context, poster, target, dependencies) {
|
|
61
|
+
const pixelRatio = target.pixelRatio > 0 ? target.pixelRatio : 1;
|
|
62
|
+
const { viewport } = poster;
|
|
63
|
+
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
64
|
+
context.clearRect(0, 0, Math.ceil(target.width * pixelRatio), Math.ceil(target.height * pixelRatio));
|
|
65
|
+
// World units to device pixels: the view of the saved board, scaled by the screen's density.
|
|
66
|
+
context.setTransform(pixelRatio * viewport.zoom, 0, 0, pixelRatio * viewport.zoom, pixelRatio * viewport.x, pixelRatio * viewport.y);
|
|
67
|
+
const painted = inflateRect(visibleWorldRect(viewport, target), PAINT_MARGIN / viewport.zoom);
|
|
68
|
+
let count = 0;
|
|
69
|
+
for (const shape of poster.shapes) {
|
|
70
|
+
try {
|
|
71
|
+
if (!rectsIntersect(shapeVisualBounds(shape), painted))
|
|
72
|
+
continue;
|
|
73
|
+
paintShape(context, shape, dependencies);
|
|
74
|
+
count += 1;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// One shape that cannot be drawn must not take the poster with it.
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return count;
|
|
81
|
+
}
|
|
82
|
+
export function posterPaintDependencies(services) {
|
|
83
|
+
return {
|
|
84
|
+
createPath: services.createPath,
|
|
85
|
+
imageFor: services.pictureFor,
|
|
86
|
+
measureText: services.measureText ?? createFixedAdvanceMeasure(),
|
|
87
|
+
widgetFallbackName: ''
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pictures a poster draws: the images of the board that have an address of their own, the screenshots of widget cards that were
|
|
3
|
+
* saved with one, and the pictures the board keeps in its host's store under a reference (the host is asked for those).
|
|
4
|
+
*
|
|
5
|
+
* They are asked for once and kept for the life of the page, so the second poster of a page (the one inside the canvas takes over
|
|
6
|
+
* from the one the host drew before it) paints them at once instead of waiting for them again. A picture that cannot be loaded is
|
|
7
|
+
* left out of the poster, not drawn broken. Everything checks for the browser first: nothing here may throw on the server.
|
|
8
|
+
*
|
|
9
|
+
* The painter asks for a picture by a key. For a picture with an address the key is the address; for one kept by reference it is
|
|
10
|
+
* `posterReferenceKey(reference)`, so the bytes the host hands over (a data URL, possibly megabytes) are never a map key.
|
|
11
|
+
*/
|
|
12
|
+
/** Gives the picture a reference stands for: its bytes as a data URL, or an address. Null when the host does not have it. */
|
|
13
|
+
export type PosterPictureResolver = (reference: string) => Promise<string | null> | string | null;
|
|
14
|
+
/** The key the painter asks for a picture by when the board keeps it in the host's store under `reference`. */
|
|
15
|
+
export declare function posterReferenceKey(reference: string): string;
|
|
16
|
+
/** The picture for a key, once it has loaded. Null while it has not, or when it never will. */
|
|
17
|
+
export declare function posterPictureFor(key: string | null | undefined): CanvasImageSource | null;
|
|
18
|
+
/** Calls `listener` whenever a picture has arrived. Returns what stops it. */
|
|
19
|
+
export declare function subscribeToPosterPictures(listener: () => void): () => void;
|
|
20
|
+
/**
|
|
21
|
+
* Starts loading the pictures that are not loaded yet and not asked for, each under its own address as the key. A picture that
|
|
22
|
+
* arrives tells every subscriber, once. `createImage` is for tests; the browser's `Image` is used otherwise.
|
|
23
|
+
*/
|
|
24
|
+
export declare function requestPosterPictures(sources: readonly string[], createImage?: () => HTMLImageElement): void;
|
|
25
|
+
/**
|
|
26
|
+
* Asks the host for the pictures the board keeps by reference, and loads what it hands over, under `posterReferenceKey(reference)`.
|
|
27
|
+
* Each reference is asked once at a time; one the host does not have (yet) is asked again at the next draw. Never throws: a host
|
|
28
|
+
* that fails, or answers with nothing, only means the picture is not in the poster.
|
|
29
|
+
*/
|
|
30
|
+
export declare function requestReferencedPictures(references: readonly string[], resolve: PosterPictureResolver, createImage?: () => HTMLImageElement): void;
|
|
31
|
+
/** Forgets every picture (for tests, and for a page that signs someone out). */
|
|
32
|
+
export declare function clearPosterPictures(): void;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pictures a poster draws: the images of the board that have an address of their own, the screenshots of widget cards that were
|
|
3
|
+
* saved with one, and the pictures the board keeps in its host's store under a reference (the host is asked for those).
|
|
4
|
+
*
|
|
5
|
+
* They are asked for once and kept for the life of the page, so the second poster of a page (the one inside the canvas takes over
|
|
6
|
+
* from the one the host drew before it) paints them at once instead of waiting for them again. A picture that cannot be loaded is
|
|
7
|
+
* left out of the poster, not drawn broken. Everything checks for the browser first: nothing here may throw on the server.
|
|
8
|
+
*
|
|
9
|
+
* The painter asks for a picture by a key. For a picture with an address the key is the address; for one kept by reference it is
|
|
10
|
+
* `posterReferenceKey(reference)`, so the bytes the host hands over (a data URL, possibly megabytes) are never a map key.
|
|
11
|
+
*/
|
|
12
|
+
/** What the page has loaded, by key. */
|
|
13
|
+
const loadedPictures = new Map();
|
|
14
|
+
/** What has been asked for already, loaded or not, so one picture is never fetched twice. */
|
|
15
|
+
const askedFor = new Set();
|
|
16
|
+
/** Who wants to know when a picture arrives: every poster that is on the screen. */
|
|
17
|
+
const listeners = new Set();
|
|
18
|
+
/** The most pictures kept; the oldest go first. A poster holds few, so this is a ceiling, not a working size. */
|
|
19
|
+
const MAX_KEPT_PICTURES = 64;
|
|
20
|
+
/** The key the painter asks for a picture by when the board keeps it in the host's store under `reference`. */
|
|
21
|
+
export function posterReferenceKey(reference) {
|
|
22
|
+
return `ref:${reference}`;
|
|
23
|
+
}
|
|
24
|
+
/** The picture for a key, once it has loaded. Null while it has not, or when it never will. */
|
|
25
|
+
export function posterPictureFor(key) {
|
|
26
|
+
return key ? (loadedPictures.get(key) ?? null) : null;
|
|
27
|
+
}
|
|
28
|
+
/** Calls `listener` whenever a picture has arrived. Returns what stops it. */
|
|
29
|
+
export function subscribeToPosterPictures(listener) {
|
|
30
|
+
listeners.add(listener);
|
|
31
|
+
return () => {
|
|
32
|
+
listeners.delete(listener);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** The browser's way to make an image, or null where there is none (the server). `createImage` is for tests. */
|
|
36
|
+
function imageMaker(createImage) {
|
|
37
|
+
if (createImage)
|
|
38
|
+
return createImage;
|
|
39
|
+
return typeof Image === 'undefined' ? null : () => new Image();
|
|
40
|
+
}
|
|
41
|
+
/** Tells every poster on the screen that a picture has arrived, once each; one that fails does not stop the others from hearing. */
|
|
42
|
+
function notifyListeners() {
|
|
43
|
+
for (const listener of [...listeners]) {
|
|
44
|
+
try {
|
|
45
|
+
listener();
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// A subscriber that fails does not stop the others from hearing.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Starts loading `source` (an address or a data URL) under `key`. A picture that cannot be loaded stays asked for: it costs one try. */
|
|
53
|
+
function startLoading(key, source, make) {
|
|
54
|
+
try {
|
|
55
|
+
const image = make();
|
|
56
|
+
image.decoding = 'async';
|
|
57
|
+
image.onload = () => {
|
|
58
|
+
loadedPictures.set(key, image);
|
|
59
|
+
if (loadedPictures.size > MAX_KEPT_PICTURES) {
|
|
60
|
+
const oldest = loadedPictures.keys().next().value;
|
|
61
|
+
if (oldest !== undefined)
|
|
62
|
+
loadedPictures.delete(oldest);
|
|
63
|
+
}
|
|
64
|
+
notifyListeners();
|
|
65
|
+
};
|
|
66
|
+
// Left out: the board draws it when it has it.
|
|
67
|
+
image.onerror = () => { };
|
|
68
|
+
image.src = source;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// The browser refused the picture; the poster goes on without it.
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Starts loading the pictures that are not loaded yet and not asked for, each under its own address as the key. A picture that
|
|
76
|
+
* arrives tells every subscriber, once. `createImage` is for tests; the browser's `Image` is used otherwise.
|
|
77
|
+
*/
|
|
78
|
+
export function requestPosterPictures(sources, createImage) {
|
|
79
|
+
const make = imageMaker(createImage);
|
|
80
|
+
if (!make)
|
|
81
|
+
return;
|
|
82
|
+
for (const source of sources) {
|
|
83
|
+
if (loadedPictures.has(source) || askedFor.has(source))
|
|
84
|
+
continue;
|
|
85
|
+
askedFor.add(source);
|
|
86
|
+
startLoading(source, source, make);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Asks the host for the pictures the board keeps by reference, and loads what it hands over, under `posterReferenceKey(reference)`.
|
|
91
|
+
* Each reference is asked once at a time; one the host does not have (yet) is asked again at the next draw. Never throws: a host
|
|
92
|
+
* that fails, or answers with nothing, only means the picture is not in the poster.
|
|
93
|
+
*/
|
|
94
|
+
export function requestReferencedPictures(references, resolve, createImage) {
|
|
95
|
+
const make = imageMaker(createImage);
|
|
96
|
+
if (!make)
|
|
97
|
+
return;
|
|
98
|
+
for (const reference of references) {
|
|
99
|
+
const key = posterReferenceKey(reference);
|
|
100
|
+
if (loadedPictures.has(key) || askedFor.has(key))
|
|
101
|
+
continue;
|
|
102
|
+
askedFor.add(key);
|
|
103
|
+
void Promise.resolve()
|
|
104
|
+
.then(() => resolve(reference))
|
|
105
|
+
.then((source) => {
|
|
106
|
+
if (typeof source === 'string' && source !== '') {
|
|
107
|
+
startLoading(key, source, make);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
// The host does not have it (yet): it is asked again the next time the poster is drawn.
|
|
111
|
+
askedFor.delete(key);
|
|
112
|
+
})
|
|
113
|
+
.catch(() => {
|
|
114
|
+
askedFor.delete(key);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Forgets every picture (for tests, and for a page that signs someone out). */
|
|
119
|
+
export function clearPosterPictures() {
|
|
120
|
+
loadedPictures.clear();
|
|
121
|
+
askedFor.clear();
|
|
122
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool a board opens with.
|
|
3
|
+
*
|
|
4
|
+
* A board used to open with the pencil every time (where the page shows the drawing tools), so a refresh took the person back to the
|
|
5
|
+
* pencil from whatever tool they had chosen. It now opens with the tool they had last, kept for the workspace beside the rest of their
|
|
6
|
+
* tool settings (`workspaceToolSettings.ts`), and with the pencil (Select, where the page hides the drawing tools) when none was kept.
|
|
7
|
+
*
|
|
8
|
+
* One rule decides it for the board (`CanvasHost`) and for the picture of the toolbar that is drawn before the board is there
|
|
9
|
+
* (`CanvasToolbarPlaceholder`), so the tool that is lit does not change at the hand-over.
|
|
10
|
+
*/
|
|
11
|
+
import type { ToolId } from '../engine/state.js';
|
|
12
|
+
import { type SettingsStorageAdapter } from './workspaceToolSettings.js';
|
|
13
|
+
/**
|
|
14
|
+
* The tools a board can open with: the ones that have a button on the toolbar, the shapes of the Shapes menu included. `pan` has no
|
|
15
|
+
* button (it is the H key and the space bar) and a library shape needs the key of its shape, so neither is kept.
|
|
16
|
+
*/
|
|
17
|
+
export declare const REMEMBERED_TOOL_IDS: readonly ["select", "draw", "erase", "arrow", "text", "rect", "rounded-rect", "ellipse", "circle", "triangle"];
|
|
18
|
+
export type RememberedToolId = (typeof REMEMBERED_TOOL_IDS)[number];
|
|
19
|
+
/** The tools of the Shapes menu (its button is the one that is lit while one of them is the tool). */
|
|
20
|
+
export declare const SHAPE_TOOL_IDS: readonly ["rect", "rounded-rect", "ellipse", "circle", "triangle"];
|
|
21
|
+
/** The tools of the drawing group: the page takes their buttons off (`showDrawingTools: false`), and a board does not open with a tool it has no button for. */
|
|
22
|
+
export declare const DRAWING_GROUP_TOOL_IDS: readonly ["draw", "erase", "arrow", "text"];
|
|
23
|
+
/** Whether a value is a tool that is kept (anything else that a storage holds is the same as nothing). */
|
|
24
|
+
export declare function isRememberedTool(value: unknown): value is RememberedToolId;
|
|
25
|
+
/** The tool of this change of tool, when it is one that is kept; null for the others (the hand, a library shape). */
|
|
26
|
+
export declare function toolToRemember(tool: ToolId): RememberedToolId | null;
|
|
27
|
+
/** Whether the tool is one of the drawing group, which a page may take off its toolbar. */
|
|
28
|
+
export declare function isDrawingGroupTool(tool: ToolId): boolean;
|
|
29
|
+
/** The tool of a board where none was kept: the pencil where the page shows the drawing tools (a board opens ready for a sketch), else Select. */
|
|
30
|
+
export declare function defaultFirstTool(showDrawingTools: boolean): ToolId;
|
|
31
|
+
/**
|
|
32
|
+
* The tool a board opens with, given what was kept (anything, as it came from a storage) and whether the page shows the drawing tools:
|
|
33
|
+
* what was kept, when the board has a button for it, else the tool of a board where none was kept.
|
|
34
|
+
*/
|
|
35
|
+
export declare function toolTheBoardOpensWith(options: {
|
|
36
|
+
rememberedToolId: unknown;
|
|
37
|
+
showDrawingTools: boolean;
|
|
38
|
+
}): ToolId;
|
|
39
|
+
/** Where the tool was kept: the workspace, and the person where the page says who they are. */
|
|
40
|
+
export interface RememberedToolScope {
|
|
41
|
+
workspaceId?: string | null;
|
|
42
|
+
userId?: string | null;
|
|
43
|
+
}
|
|
44
|
+
/** The tool kept for this workspace and person, or null (nothing kept, no workspace to keep it for, a store that refuses, or a value that is no tool). */
|
|
45
|
+
export declare function readRememberedToolId(scope: RememberedToolScope, adapter?: SettingsStorageAdapter): RememberedToolId | null;
|
|
46
|
+
/** The tool a board opens with: read from the storage of the workspace, decided by `toolTheBoardOpensWith`. */
|
|
47
|
+
export declare function firstToolOfBoard(options: RememberedToolScope & {
|
|
48
|
+
showDrawingTools: boolean;
|
|
49
|
+
adapter?: SettingsStorageAdapter;
|
|
50
|
+
}): ToolId;
|