@widgetic/canvas 0.5.44 → 0.5.45
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/dist/canvas/DomCanvas.svelte +52 -3
- package/dist/canvas/DomCanvas.svelte.d.ts +59 -34
- 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 +21 -19
- package/dist/canvas/eventScope.js +14 -10
- package/dist/canvas/host/CanvasHost.d.ts +68 -1
- package/dist/canvas/host/CanvasHost.js +1901 -1407
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +28 -5
- package/dist/canvas/host/boardFiles.d.ts +90 -0
- package/dist/canvas/host/boardFiles.js +155 -0
- package/dist/canvas/host/browserServices.d.ts +17 -0
- package/dist/canvas/host/browserServices.js +211 -65
- package/dist/canvas/host/conversion.js +170 -82
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.js +21 -18
- package/dist/canvas/host/pageEvents.js +5 -5
- package/dist/canvas/host/pictureSaves.js +11 -9
- package/dist/canvas/host/screenRects.js +31 -28
- package/dist/canvas/host/storedPictures.js +243 -149
- package/dist/canvas/host/strokeSnap.js +143 -88
- package/dist/canvas/index.d.ts +1 -0
- 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 +4 -4
- package/dist/canvas/shapes/strokeShapeSnap.js +233 -188
- package/dist/canvas/toolbarPosition.js +2 -2
- 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/colorModel.js +32 -21
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +26 -4
- package/dist/canvas/ui/floatingCard.js +43 -21
- package/dist/canvas/ui/floatingLayer.js +6 -5
- package/dist/canvas/ui/icons.d.ts +1 -1
- package/dist/canvas/ui/icons.js +1 -1
- package/dist/canvas/ui/keyHints.js +11 -9
- package/dist/canvas/ui/popoverSide.js +3 -2
- package/dist/canvas/ui/selectionTraits.d.ts +12 -0
- package/dist/canvas/ui/selectionTraits.js +66 -35
- 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/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.d.ts +1 -1
- package/dist/canvas/view/geometry.js +90 -71
- package/dist/canvas/view/liftedScene.js +36 -24
- package/dist/canvas/view/mountedScene.js +79 -76
- package/dist/components/CanvasBoardPoster.svelte +206 -0
- package/dist/components/CanvasBoardPoster.svelte.d.ts +17 -0
- package/dist/components/CanvasToolbarPlaceholder.svelte +115 -0
- package/dist/components/CanvasToolbarPlaceholder.svelte.d.ts +9 -0
- package/dist/components/index.d.ts +7 -0
- package/dist/components/index.js +8 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +37 -2
- package/dist/engine/EngineCore.js +946 -768
- package/dist/engine/codec/canvasDocument.d.ts +7 -0
- package/dist/engine/codec/canvasDocument.js +172 -186
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +38 -26
- package/dist/engine/core/arrow.d.ts +18 -8
- package/dist/engine/core/arrow.js +109 -84
- package/dist/engine/core/arrowBinding.d.ts +1 -1
- package/dist/engine/core/arrowBinding.js +126 -101
- package/dist/engine/core/arrowBindingRecords.d.ts +9 -3
- package/dist/engine/core/arrowBindingRecords.js +48 -29
- package/dist/engine/core/brush.d.ts +8 -0
- package/dist/engine/core/brush.js +98 -78
- package/dist/engine/core/cameraMove.d.ts +6 -0
- package/dist/engine/core/cameraMove.js +56 -38
- 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 +55 -38
- package/dist/engine/core/hitTest.js +81 -68
- 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 +22 -0
- package/dist/engine/core/scene.js +288 -139
- 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.d.ts +10 -0
- package/dist/engine/core/style.js +82 -33
- package/dist/engine/core/transform.d.ts +6 -0
- package/dist/engine/core/transform.js +103 -83
- package/dist/engine/core/types.d.ts +7 -0
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +36 -23
- package/dist/engine/input/PointerController.d.ts +15 -1
- package/dist/engine/input/PointerController.js +547 -476
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.d.ts +3 -1
- package/dist/engine/input/gestures.js +340 -266
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.d.ts +22 -3
- package/dist/engine/input/keyboard.js +131 -32
- package/dist/engine/input/selectionHandles.js +50 -45
- package/dist/engine/input/types.d.ts +7 -0
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +158 -86
- package/dist/engine/state.d.ts +18 -0
- package/dist/engine/state.js +25 -4
- 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 +83 -30
- package/dist/fonts/workspaceFonts.d.ts +6 -0
- package/dist/fonts/workspaceFonts.js +108 -34
- 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 +6 -0
- package/dist/i18n/messages.js +7 -1
- package/dist/i18n/shortcuts.d.ts +6 -4
- package/dist/i18n/shortcuts.js +30 -26
- package/dist/index.d.ts +1 -0
- package/dist/poster/boardPlacement.d.ts +80 -0
- package/dist/poster/boardPlacement.js +140 -0
- package/dist/poster/boardPoster.d.ts +80 -0
- package/dist/poster/boardPoster.js +251 -0
- package/dist/poster/paintBoardPoster.d.ts +49 -0
- package/dist/poster/paintBoardPoster.js +112 -0
- package/dist/poster/posterPictures.d.ts +32 -0
- package/dist/poster/posterPictures.js +138 -0
- package/dist/settings/workspaceToolSettings.d.ts +6 -0
- package/dist/settings/workspaceToolSettings.js +58 -34
- package/dist/tools/components/catalog.js +74 -64
- package/dist/tools/components/grouping.js +28 -6
- package/dist/tools/definitions.js +31 -9
- package/dist/tools/executors.js +214 -106
- package/dist/tools/hostAdapter.d.ts +5 -1
- package/dist/tools/hostAdapter.js +15 -11
- package/dist/tools/sceneSummary.js +57 -43
- package/dist/tools/sketchIntent.js +21 -13
- package/dist/tools/styleArguments.d.ts +35 -0
- package/dist/tools/styleArguments.js +93 -0
- package/dist/tools/types.d.ts +26 -1
- package/dist/tools/types.js +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the board stands in the page the creator is in, remembered for the next visit.
|
|
3
|
+
*
|
|
4
|
+
* A page draws something for the board before the creator's code has arrived (the site's `/canvas` route paints the board's colour
|
|
5
|
+
* at once). That first picture covers the whole area, but the board of the creator is a card inside its layout: a bar above it, the
|
|
6
|
+
* chat panel beside it, a margin around. To draw the poster of the objects and the toolbar where they will be, and not shifted by
|
|
7
|
+
* the width of the chat, the page needs to know where the card is, and it cannot know before the creator has laid itself out.
|
|
8
|
+
*
|
|
9
|
+
* So the creator measures its board once it is laid out (`measureBoardPlacement`: the distances from the edges of the area it fills
|
|
10
|
+
* to the edges of the board) and keeps them next to the page (`rememberBoardPlacement`); the page reads them on its next visit
|
|
11
|
+
* (`recallBoardPlacement`) in the first frame, because it is a few bytes in `localStorage`. It is the same measurement the creator's
|
|
12
|
+
* own layout produces, so nothing here copies the layout: when the layout changes, the next visit's measurement is the new one.
|
|
13
|
+
*
|
|
14
|
+
* The distances depend on which layout the creator is in (the chat beside the board, below it, or behind a tab on a phone), so a
|
|
15
|
+
* placement is only used in the layout it was measured in. One that is missing, damaged or from another layout is nothing: the
|
|
16
|
+
* page draws its empty board as before and the creator draws the rest when it arrives.
|
|
17
|
+
*
|
|
18
|
+
* It holds nothing private: pixel distances and the toolbar's place.
|
|
19
|
+
*/
|
|
20
|
+
/** Where the placement is kept. The page and the creator are separate programs on one origin, so they meet here. */
|
|
21
|
+
export var BOARD_PLACEMENT_STORAGE_KEY = 'widgetic.canvas.boardPlacement';
|
|
22
|
+
export var BOARD_PLACEMENT_VERSION = 1;
|
|
23
|
+
/** The window width from which the chat stands beside the board. */
|
|
24
|
+
export var WIDE_LAYOUT_MIN_WIDTH = 1024;
|
|
25
|
+
/** The window width from which the chat stands below the board. */
|
|
26
|
+
export var MEDIUM_LAYOUT_MIN_WIDTH = 768;
|
|
27
|
+
/** The most a distance may be (a placement with more is not a board card, it is a measurement gone wrong). */
|
|
28
|
+
var MAX_DISTANCE = 2000;
|
|
29
|
+
/** The smallest board a placement may describe, in CSS pixels on each side. */
|
|
30
|
+
var MIN_BOARD_SIDE = 120;
|
|
31
|
+
/** The layout the creator is in at this window width. */
|
|
32
|
+
export function boardLayoutOf(viewportWidth) {
|
|
33
|
+
if (viewportWidth >= WIDE_LAYOUT_MIN_WIDTH)
|
|
34
|
+
return 'wide';
|
|
35
|
+
if (viewportWidth >= MEDIUM_LAYOUT_MIN_WIDTH)
|
|
36
|
+
return 'medium';
|
|
37
|
+
return 'narrow';
|
|
38
|
+
}
|
|
39
|
+
function distanceIsUsable(value) {
|
|
40
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= MAX_DISTANCE;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The placement of a board: where `board` stands inside `host` (the element the creator fills). Null when either has no usable
|
|
44
|
+
* size, the board lies outside the host, or it is too small to be a board.
|
|
45
|
+
*/
|
|
46
|
+
export function measureBoardPlacement(host, board, details) {
|
|
47
|
+
var top = Math.round(board.top - host.top);
|
|
48
|
+
var left = Math.round(board.left - host.left);
|
|
49
|
+
var right = Math.round(host.right - board.right);
|
|
50
|
+
var bottom = Math.round(host.bottom - board.bottom);
|
|
51
|
+
if (![top, right, bottom, left].every(distanceIsUsable))
|
|
52
|
+
return null;
|
|
53
|
+
var boardWidth = host.right - host.left - left - right;
|
|
54
|
+
var boardHeight = host.bottom - host.top - top - bottom;
|
|
55
|
+
if (!(boardWidth >= MIN_BOARD_SIDE) || !(boardHeight >= MIN_BOARD_SIDE))
|
|
56
|
+
return null;
|
|
57
|
+
return {
|
|
58
|
+
version: BOARD_PLACEMENT_VERSION,
|
|
59
|
+
layout: boardLayoutOf(details.viewportWidth),
|
|
60
|
+
top: top,
|
|
61
|
+
right: right,
|
|
62
|
+
bottom: bottom,
|
|
63
|
+
left: left,
|
|
64
|
+
toolbarPosition: typeof details.toolbarPosition === 'string' && details.toolbarPosition.length <= 8 ? details.toolbarPosition : null,
|
|
65
|
+
showDrawingTools: details.showDrawingTools !== false
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function defaultStorage() {
|
|
69
|
+
try {
|
|
70
|
+
return typeof localStorage === 'undefined' ? null : localStorage;
|
|
71
|
+
}
|
|
72
|
+
catch (_a) {
|
|
73
|
+
// A sandboxed frame refuses even the lookup.
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** What was written last by this page, so a placement that has not changed is not written again (a measurement is taken at every save). */
|
|
78
|
+
var lastWritten = null;
|
|
79
|
+
/** Keeps the placement for the next visit. Writes only what changed. Returns whether it wrote. Never throws. */
|
|
80
|
+
export function rememberBoardPlacement(placement, storage) {
|
|
81
|
+
if (storage === void 0) { storage = defaultStorage(); }
|
|
82
|
+
if (!storage)
|
|
83
|
+
return false;
|
|
84
|
+
try {
|
|
85
|
+
var text = JSON.stringify(placement);
|
|
86
|
+
if (text === lastWritten)
|
|
87
|
+
return false;
|
|
88
|
+
storage.setItem(BOARD_PLACEMENT_STORAGE_KEY, text);
|
|
89
|
+
lastWritten = text;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
catch (_a) {
|
|
93
|
+
// Storage is full or refused: the next visit draws the empty board, as it did before the placement existed.
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The placement kept by the last visit, for the layout the window is in now. Null when none was kept, it is damaged, or it was
|
|
99
|
+
* measured in another layout (its distances would put the board in the wrong place).
|
|
100
|
+
*/
|
|
101
|
+
export function recallBoardPlacement(viewportWidth, storage) {
|
|
102
|
+
if (storage === void 0) { storage = defaultStorage(); }
|
|
103
|
+
if (!storage)
|
|
104
|
+
return null;
|
|
105
|
+
try {
|
|
106
|
+
var text = storage.getItem(BOARD_PLACEMENT_STORAGE_KEY);
|
|
107
|
+
if (!text)
|
|
108
|
+
return null;
|
|
109
|
+
var value = JSON.parse(text);
|
|
110
|
+
if (typeof value !== 'object' || value === null)
|
|
111
|
+
return null;
|
|
112
|
+
var record = value;
|
|
113
|
+
if (record.version !== BOARD_PLACEMENT_VERSION)
|
|
114
|
+
return null;
|
|
115
|
+
if (record.layout !== boardLayoutOf(viewportWidth))
|
|
116
|
+
return null;
|
|
117
|
+
if (![record.top, record.right, record.bottom, record.left].every(distanceIsUsable))
|
|
118
|
+
return null;
|
|
119
|
+
if (typeof record.showDrawingTools !== 'boolean')
|
|
120
|
+
return null;
|
|
121
|
+
var toolbarPosition = typeof record.toolbarPosition === 'string' && record.toolbarPosition.length <= 8 ? record.toolbarPosition : null;
|
|
122
|
+
return {
|
|
123
|
+
version: BOARD_PLACEMENT_VERSION,
|
|
124
|
+
layout: record.layout,
|
|
125
|
+
top: record.top,
|
|
126
|
+
right: record.right,
|
|
127
|
+
bottom: record.bottom,
|
|
128
|
+
left: record.left,
|
|
129
|
+
toolbarPosition: toolbarPosition,
|
|
130
|
+
showDrawingTools: record.showDrawingTools
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
catch (_a) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** Forgets what this page wrote last (for tests). */
|
|
138
|
+
export function resetBoardPlacementMemory() {
|
|
139
|
+
lastWritten = null;
|
|
140
|
+
}
|
|
@@ -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,251 @@
|
|
|
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
|
+
var __assign = (this && this.__assign) || function () {
|
|
23
|
+
__assign = Object.assign || function(t) {
|
|
24
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
25
|
+
s = arguments[i];
|
|
26
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
27
|
+
t[p] = s[p];
|
|
28
|
+
}
|
|
29
|
+
return t;
|
|
30
|
+
};
|
|
31
|
+
return __assign.apply(this, arguments);
|
|
32
|
+
};
|
|
33
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
34
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
35
|
+
if (ar || !(i in from)) {
|
|
36
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
37
|
+
ar[i] = from[i];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
41
|
+
};
|
|
42
|
+
import { parseCanvasDocument } from '../engine/codec/canvasDocument.js';
|
|
43
|
+
import { HOST_READABLE_KEYS } from '../engine/codec/hostReadableKeys.js';
|
|
44
|
+
import { brushReach, brushSizeFor } from '../engine/core/freehand.js';
|
|
45
|
+
import { inflateRect, rectCenter, rectsIntersect, rotatedBoxBounds } from '../engine/core/geometry.js';
|
|
46
|
+
import { CANVAS_DOCUMENT_VERSION, isBoxedObject } from '../engine/core/types.js';
|
|
47
|
+
import { sanitizeViewport, visibleWorldRect } from '../engine/core/viewport.js';
|
|
48
|
+
/** The version of the poster's own format: a poster of another version is not read (the next visit makes a new one). */
|
|
49
|
+
export var BOARD_POSTER_VERSION = 1;
|
|
50
|
+
/**
|
|
51
|
+
* 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
|
|
52
|
+
* reads it before the creator is there agree on this one name, so it lives with the format.
|
|
53
|
+
*/
|
|
54
|
+
export var BOARD_POSTER_STORAGE_KIND = 'canvasBoardPoster';
|
|
55
|
+
/** The size of the screen the saved view is taken to have when the page does not say (a large window). */
|
|
56
|
+
export var DEFAULT_POSTER_SURFACE = Object.freeze({ width: 1600, height: 1000 });
|
|
57
|
+
/** The most objects a poster holds; a board with more keeps the ones nearest the middle of the view. */
|
|
58
|
+
export var POSTER_MAX_OBJECTS = 400;
|
|
59
|
+
/** The most bytes of saved text a poster holds (what is kept next to the page stays small, and reading it stays fast). */
|
|
60
|
+
export var POSTER_MAX_BYTES = 240000;
|
|
61
|
+
/** Inline picture bytes longer than this are left out of the poster (the picture appears with the board). */
|
|
62
|
+
export var POSTER_MAX_INLINE_PICTURE = 24000;
|
|
63
|
+
/** 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. */
|
|
64
|
+
export var POSTER_MARGIN = 160;
|
|
65
|
+
/** The most records `parseBoardPoster` reads, whatever a poster says it holds. */
|
|
66
|
+
var POSTER_READ_LIMIT = 2000;
|
|
67
|
+
var MIN_SURFACE_SIDE = 200;
|
|
68
|
+
var MAX_SURFACE_SIDE = 8000;
|
|
69
|
+
/** The kinds of object a poster draws (a group has no look of its own). */
|
|
70
|
+
var DRAWN_TYPES = new Set(['rect', 'ellipse', 'path', 'text', 'image', 'widget']);
|
|
71
|
+
/** The keys that may hold the bytes of a picture inline. */
|
|
72
|
+
var INLINE_PICTURE_KEYS = ['src', 'thumbnail'];
|
|
73
|
+
function isRecord(value) {
|
|
74
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
75
|
+
}
|
|
76
|
+
function finiteNumber(value, fallback) {
|
|
77
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
78
|
+
}
|
|
79
|
+
/** 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. */
|
|
80
|
+
function readJson(source) {
|
|
81
|
+
if (typeof source !== 'string')
|
|
82
|
+
return source;
|
|
83
|
+
if (source.trim() === '')
|
|
84
|
+
return null;
|
|
85
|
+
try {
|
|
86
|
+
return JSON.parse(source);
|
|
87
|
+
}
|
|
88
|
+
catch (_a) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** A surface the page named, kept to sizes a board can have; the default when it named none or nonsense. */
|
|
93
|
+
export function sanitizeSurface(value) {
|
|
94
|
+
if (!isRecord(value))
|
|
95
|
+
return __assign({}, DEFAULT_POSTER_SURFACE);
|
|
96
|
+
var width = finiteNumber(value.width, DEFAULT_POSTER_SURFACE.width);
|
|
97
|
+
var height = finiteNumber(value.height, DEFAULT_POSTER_SURFACE.height);
|
|
98
|
+
return {
|
|
99
|
+
width: Math.min(MAX_SURFACE_SIDE, Math.max(MIN_SURFACE_SIDE, Math.round(width))),
|
|
100
|
+
height: Math.min(MAX_SURFACE_SIDE, Math.max(MIN_SURFACE_SIDE, Math.round(height)))
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** A saved record without the picture bytes that are too long to keep (the same record when it has none). */
|
|
104
|
+
function withoutLongPictures(record) {
|
|
105
|
+
var result = record;
|
|
106
|
+
for (var _i = 0, INLINE_PICTURE_KEYS_1 = INLINE_PICTURE_KEYS; _i < INLINE_PICTURE_KEYS_1.length; _i++) {
|
|
107
|
+
var key = INLINE_PICTURE_KEYS_1[_i];
|
|
108
|
+
var value = record[key];
|
|
109
|
+
if (typeof value === 'string' && value.startsWith('data:') && value.length > POSTER_MAX_INLINE_PICTURE) {
|
|
110
|
+
if (result === record)
|
|
111
|
+
result = __assign({}, record);
|
|
112
|
+
delete result[key];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The rectangle a saved record may paint in, read from its raw numbers (the reader's own defaults for what is missing), with
|
|
119
|
+
* room for its line and for a turn. Generous on purpose: it only decides what is kept, and the poster is drawn clipped anyway.
|
|
120
|
+
*/
|
|
121
|
+
function roughBoundsOf(record) {
|
|
122
|
+
var strokeWidth = Math.min(200, Math.max(0, finiteNumber(record.strokeWidth, 3)));
|
|
123
|
+
var isBrush = isRecord(record.ink);
|
|
124
|
+
var reach = isBrush ? brushReach(brushSizeFor(strokeWidth)) : strokeWidth * Math.SQRT1_2 + 1;
|
|
125
|
+
var box = {
|
|
126
|
+
x: finiteNumber(record.x, 0),
|
|
127
|
+
y: finiteNumber(record.y, 0),
|
|
128
|
+
width: Math.max(1, finiteNumber(record.width, 100)),
|
|
129
|
+
height: Math.max(1, finiteNumber(record.height, 100)),
|
|
130
|
+
rotation: finiteNumber(record.rotation, 0)
|
|
131
|
+
};
|
|
132
|
+
return inflateRect(rotatedBoxBounds(box), reach);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 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
|
|
136
|
+
* is not a saved board, an empty board, nothing in view). `source` is the saved text, or the same thing already parsed.
|
|
137
|
+
*/
|
|
138
|
+
export function createBoardPoster(source, options) {
|
|
139
|
+
if (options === void 0) { options = {}; }
|
|
140
|
+
var savedDocument = readJson(source);
|
|
141
|
+
if (!isRecord(savedDocument) || savedDocument.version !== CANVAS_DOCUMENT_VERSION)
|
|
142
|
+
return null;
|
|
143
|
+
var canvas = savedDocument.canvas;
|
|
144
|
+
var list = isRecord(canvas) && Array.isArray(canvas.objects) ? canvas.objects : [];
|
|
145
|
+
if (list.length === 0)
|
|
146
|
+
return null;
|
|
147
|
+
var viewport = sanitizeViewport(savedDocument.viewport);
|
|
148
|
+
var surface = sanitizeSurface(options.surface);
|
|
149
|
+
var maxObjects = Math.max(1, Math.min(POSTER_MAX_OBJECTS, Math.floor(finiteNumber(options.maxObjects, POSTER_MAX_OBJECTS))));
|
|
150
|
+
var maxBytes = Math.max(1000, Math.min(POSTER_MAX_BYTES, Math.floor(finiteNumber(options.maxBytes, POSTER_MAX_BYTES))));
|
|
151
|
+
// What the saved view showed, with room around it, in world units.
|
|
152
|
+
var keptArea = inflateRect(visibleWorldRect(viewport, surface), POSTER_MARGIN / viewport.zoom);
|
|
153
|
+
var middle = rectCenter(keptArea);
|
|
154
|
+
var candidates = [];
|
|
155
|
+
for (var index = 0; index < list.length; index += 1) {
|
|
156
|
+
var raw = list[index];
|
|
157
|
+
if (!isRecord(raw))
|
|
158
|
+
continue;
|
|
159
|
+
var type = raw[HOST_READABLE_KEYS.objectType];
|
|
160
|
+
if (typeof type !== 'string' || !DRAWN_TYPES.has(type))
|
|
161
|
+
continue;
|
|
162
|
+
// The reader treats these as "not there"; so does the poster.
|
|
163
|
+
if (raw[HOST_READABLE_KEYS.visible] === false || raw[HOST_READABLE_KEYS.excludeFromExport] === true)
|
|
164
|
+
continue;
|
|
165
|
+
var bounds = roughBoundsOf(raw);
|
|
166
|
+
if (!rectsIntersect(bounds, keptArea))
|
|
167
|
+
continue;
|
|
168
|
+
var record = withoutLongPictures(raw);
|
|
169
|
+
var bytes = 0;
|
|
170
|
+
try {
|
|
171
|
+
bytes = JSON.stringify(record).length;
|
|
172
|
+
}
|
|
173
|
+
catch (_a) {
|
|
174
|
+
// A record that cannot be written down cannot be kept.
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
var center = rectCenter(bounds);
|
|
178
|
+
candidates.push({ index: index, record: record, bytes: bytes, distance: Math.pow((center.x - middle.x), 2) + Math.pow((center.y - middle.y), 2) });
|
|
179
|
+
}
|
|
180
|
+
if (candidates.length === 0)
|
|
181
|
+
return null;
|
|
182
|
+
var kept = candidates;
|
|
183
|
+
var totalBytes = candidates.reduce(function (total, candidate) { return total + candidate.bytes; }, 0);
|
|
184
|
+
if (candidates.length > maxObjects || totalBytes > maxBytes) {
|
|
185
|
+
// 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.
|
|
186
|
+
var byDistance = __spreadArray([], candidates, true).sort(function (first, second) { return first.distance - second.distance; });
|
|
187
|
+
kept = [];
|
|
188
|
+
var usedBytes = 0;
|
|
189
|
+
for (var _i = 0, byDistance_1 = byDistance; _i < byDistance_1.length; _i++) {
|
|
190
|
+
var candidate = byDistance_1[_i];
|
|
191
|
+
if (kept.length >= maxObjects || usedBytes + candidate.bytes > maxBytes)
|
|
192
|
+
continue;
|
|
193
|
+
kept.push(candidate);
|
|
194
|
+
usedBytes += candidate.bytes;
|
|
195
|
+
}
|
|
196
|
+
kept.sort(function (first, second) { return first.index - second.index; });
|
|
197
|
+
if (kept.length === 0)
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
return { version: BOARD_POSTER_VERSION, viewport: viewport, surface: surface, objects: kept.map(function (candidate) { return candidate.record; }) };
|
|
201
|
+
}
|
|
202
|
+
/** 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). */
|
|
203
|
+
function pictureAddressOf(shape) {
|
|
204
|
+
var _a;
|
|
205
|
+
if (shape.src && !shape.src.startsWith('blob:'))
|
|
206
|
+
return shape.src;
|
|
207
|
+
return (_a = shape.libraryFileUrl) !== null && _a !== void 0 ? _a : null;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* 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.
|
|
211
|
+
* The records go through the canvas's own reader, so every number, colour and address is checked the way a saved board's are.
|
|
212
|
+
*/
|
|
213
|
+
export function parseBoardPoster(value) {
|
|
214
|
+
if (!isRecord(value) || value.version !== BOARD_POSTER_VERSION)
|
|
215
|
+
return null;
|
|
216
|
+
if (!Array.isArray(value.objects) || value.objects.length === 0 || value.objects.length > POSTER_READ_LIMIT)
|
|
217
|
+
return null;
|
|
218
|
+
var parsed = parseCanvasDocument({
|
|
219
|
+
version: CANVAS_DOCUMENT_VERSION,
|
|
220
|
+
viewport: value.viewport,
|
|
221
|
+
canvas: { objects: value.objects }
|
|
222
|
+
});
|
|
223
|
+
if (parsed.status !== 'ok')
|
|
224
|
+
return null;
|
|
225
|
+
var shapes = [];
|
|
226
|
+
for (var _i = 0, _a = parsed.scene; _i < _a.length; _i++) {
|
|
227
|
+
var object = _a[_i];
|
|
228
|
+
if (!isBoxedObject(object))
|
|
229
|
+
continue;
|
|
230
|
+
shapes.push(object.type === 'image' ? __assign(__assign({}, object), { src: pictureAddressOf(object) }) : object);
|
|
231
|
+
}
|
|
232
|
+
if (shapes.length === 0)
|
|
233
|
+
return null;
|
|
234
|
+
return { viewport: parsed.viewport, surface: sanitizeSurface(value.surface), shapes: shapes };
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Whether two posters are the same, for a page that keeps the latest and writes only what changed. Compares what would be written,
|
|
238
|
+
* so it stays right whichever of the fields the caller touched.
|
|
239
|
+
*/
|
|
240
|
+
export function boardPostersMatch(first, second) {
|
|
241
|
+
if (first === second)
|
|
242
|
+
return true;
|
|
243
|
+
if (!first || !second)
|
|
244
|
+
return false;
|
|
245
|
+
try {
|
|
246
|
+
return JSON.stringify(first) === JSON.stringify(second);
|
|
247
|
+
}
|
|
248
|
+
catch (_a) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
@@ -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,112 @@
|
|
|
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
|
+
var __assign = (this && this.__assign) || function () {
|
|
11
|
+
__assign = Object.assign || function(t) {
|
|
12
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
13
|
+
s = arguments[i];
|
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
15
|
+
t[p] = s[p];
|
|
16
|
+
}
|
|
17
|
+
return t;
|
|
18
|
+
};
|
|
19
|
+
return __assign.apply(this, arguments);
|
|
20
|
+
};
|
|
21
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
22
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
23
|
+
if (ar || !(i in from)) {
|
|
24
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
25
|
+
ar[i] = from[i];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
29
|
+
};
|
|
30
|
+
import { inflateRect, rectsIntersect } from '../engine/core/geometry.js';
|
|
31
|
+
import { shapeVisualBounds } from '../engine/core/scene.js';
|
|
32
|
+
import { visibleWorldRect } from '../engine/core/viewport.js';
|
|
33
|
+
import { imageSourcesOf, paintShape } from '../engine/render/rasterize.js';
|
|
34
|
+
import { createFixedAdvanceMeasure } from '../engine/text/layoutText.js';
|
|
35
|
+
import { posterReferenceKey } from './posterPictures.js';
|
|
36
|
+
/** Screen pixels around the area that still get painted, so a stroke that starts just outside it has its edge. */
|
|
37
|
+
var PAINT_MARGIN = 64;
|
|
38
|
+
/** The addresses of the pictures a poster needs (its images, and the screenshots of its widget cards). */
|
|
39
|
+
export function posterPictureSources(poster) {
|
|
40
|
+
return imageSourcesOf(poster.shapes);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The references of the pictures a poster draws that the board keeps in its host's store (a photo dropped on the board, the
|
|
44
|
+
* screenshot of a widget card): they have no address in the saved text, only the reference the host knows them by. Once each.
|
|
45
|
+
*/
|
|
46
|
+
export function posterPictureReferences(poster) {
|
|
47
|
+
var references = new Set();
|
|
48
|
+
for (var _i = 0, _a = poster.shapes; _i < _a.length; _i++) {
|
|
49
|
+
var shape = _a[_i];
|
|
50
|
+
if (shape.type === 'image' && !shape.src && shape.imageRef)
|
|
51
|
+
references.add(shape.imageRef);
|
|
52
|
+
if (shape.type === 'widget' && !shape.thumbnail && shape.thumbnailRef)
|
|
53
|
+
references.add(shape.thumbnailRef);
|
|
54
|
+
}
|
|
55
|
+
return __spreadArray([], references, true);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The poster with each picture that is kept by reference named by its key (`posterReferenceKey`), which is what the painter asks
|
|
59
|
+
* the loaded pictures for. The same poster when there is none. A picture that has an address keeps it.
|
|
60
|
+
*/
|
|
61
|
+
export function withReferencedPictures(poster) {
|
|
62
|
+
var changed = false;
|
|
63
|
+
var shapes = poster.shapes.map(function (shape) {
|
|
64
|
+
if (shape.type === 'image' && !shape.src && shape.imageRef) {
|
|
65
|
+
changed = true;
|
|
66
|
+
return __assign(__assign({}, shape), { src: posterReferenceKey(shape.imageRef) });
|
|
67
|
+
}
|
|
68
|
+
if (shape.type === 'widget' && !shape.thumbnail && shape.thumbnailRef) {
|
|
69
|
+
changed = true;
|
|
70
|
+
return __assign(__assign({}, shape), { thumbnail: posterReferenceKey(shape.thumbnailRef) });
|
|
71
|
+
}
|
|
72
|
+
return shape;
|
|
73
|
+
});
|
|
74
|
+
return changed ? __assign(__assign({}, poster), { shapes: shapes }) : poster;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Paints the poster: clears the area, sets the saved view, and draws the shapes that touch the area, bottom first. A shape that
|
|
78
|
+
* 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
|
|
79
|
+
* handed over, and the painter draws nothing of it).
|
|
80
|
+
*/
|
|
81
|
+
export function paintBoardPoster(context, poster, target, dependencies) {
|
|
82
|
+
var pixelRatio = target.pixelRatio > 0 ? target.pixelRatio : 1;
|
|
83
|
+
var viewport = poster.viewport;
|
|
84
|
+
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
85
|
+
context.clearRect(0, 0, Math.ceil(target.width * pixelRatio), Math.ceil(target.height * pixelRatio));
|
|
86
|
+
// World units to device pixels: the view of the saved board, scaled by the screen's density.
|
|
87
|
+
context.setTransform(pixelRatio * viewport.zoom, 0, 0, pixelRatio * viewport.zoom, pixelRatio * viewport.x, pixelRatio * viewport.y);
|
|
88
|
+
var painted = inflateRect(visibleWorldRect(viewport, target), PAINT_MARGIN / viewport.zoom);
|
|
89
|
+
var count = 0;
|
|
90
|
+
for (var _i = 0, _a = poster.shapes; _i < _a.length; _i++) {
|
|
91
|
+
var shape = _a[_i];
|
|
92
|
+
try {
|
|
93
|
+
if (!rectsIntersect(shapeVisualBounds(shape), painted))
|
|
94
|
+
continue;
|
|
95
|
+
paintShape(context, shape, dependencies);
|
|
96
|
+
count += 1;
|
|
97
|
+
}
|
|
98
|
+
catch (_b) {
|
|
99
|
+
// One shape that cannot be drawn must not take the poster with it.
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return count;
|
|
103
|
+
}
|
|
104
|
+
export function posterPaintDependencies(services) {
|
|
105
|
+
var _a;
|
|
106
|
+
return {
|
|
107
|
+
createPath: services.createPath,
|
|
108
|
+
imageFor: services.pictureFor,
|
|
109
|
+
measureText: (_a = services.measureText) !== null && _a !== void 0 ? _a : createFixedAdvanceMeasure(),
|
|
110
|
+
widgetFallbackName: ''
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -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;
|