@widgetic/canvas 0.5.35 → 0.5.36
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 +105 -36
- package/dist/canvas/DomCanvas.svelte +454 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +222 -0
- package/dist/canvas/boardFrame.d.ts +13 -0
- package/dist/canvas/boardFrame.js +13 -0
- package/dist/canvas/boardSurface.d.ts +9 -0
- package/dist/canvas/boardSurface.js +9 -0
- package/dist/canvas/chromeLayout.d.ts +76 -0
- package/dist/canvas/chromeLayout.js +121 -0
- package/dist/canvas/emptyBoardHint.d.ts +17 -0
- package/dist/canvas/emptyBoardHint.js +12 -0
- package/dist/canvas/eventScope.d.ts +1 -1
- package/dist/canvas/eventScope.js +1 -1
- package/dist/canvas/host/CanvasHost.d.ts +351 -0
- package/dist/canvas/host/CanvasHost.js +1740 -0
- package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
- package/dist/canvas/host/CanvasHostApi.js +95 -0
- package/dist/canvas/host/browserServices.d.ts +46 -0
- package/dist/canvas/host/browserServices.js +167 -0
- package/dist/canvas/host/conversion.d.ts +51 -0
- package/dist/canvas/host/conversion.js +123 -0
- package/dist/canvas/host/hostObject.d.ts +40 -0
- package/dist/canvas/host/hostObject.js +93 -0
- package/dist/canvas/host/pageEvents.d.ts +24 -0
- package/dist/canvas/host/pageEvents.js +35 -0
- package/dist/canvas/host/screenRects.d.ts +66 -0
- package/dist/canvas/host/screenRects.js +102 -0
- package/dist/canvas/host/strokeSnap.d.ts +67 -0
- package/dist/canvas/host/strokeSnap.js +165 -0
- package/dist/canvas/index.d.ts +7 -7
- package/dist/canvas/index.js +8 -8
- package/dist/canvas/openMarks.d.ts +17 -0
- package/dist/canvas/openMarks.js +26 -0
- package/dist/canvas/panZoomPosition.d.ts +15 -0
- package/dist/canvas/panZoomPosition.js +16 -0
- package/dist/canvas/preferredTextFont.d.ts +12 -0
- package/dist/canvas/preferredTextFont.js +38 -0
- package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
- package/dist/canvas/shapes/ShapeLibrary.js +13 -1
- package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
- package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
- package/dist/canvas/toolbarPosition.d.ts +11 -11
- package/dist/canvas/toolbarPosition.js +11 -24
- package/dist/canvas/types.d.ts +14 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
- package/dist/canvas/ui/CanvasHint.svelte +20 -0
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
- package/dist/canvas/ui/ColorField.svelte +155 -0
- package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
- package/dist/canvas/ui/LoadNotice.svelte +39 -0
- package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
- package/dist/canvas/ui/SelectionPanel.svelte +300 -0
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
- package/dist/canvas/ui/SelectorBar.svelte +82 -0
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
- package/dist/canvas/ui/ShapesMenu.svelte +185 -0
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/ToolButton.svelte +78 -0
- package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
- package/dist/canvas/ui/WidgetActions.svelte +152 -0
- package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
- package/dist/canvas/ui/ZoomControl.svelte +89 -0
- package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
- package/dist/canvas/ui/colorModel.d.ts +18 -0
- package/dist/canvas/ui/colorModel.js +85 -0
- package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
- package/dist/canvas/ui/colorPickerMessages.js +15 -0
- package/dist/canvas/ui/floatingCard.d.ts +107 -0
- package/dist/canvas/ui/floatingCard.js +107 -0
- package/dist/canvas/ui/floatingLayer.d.ts +21 -0
- package/dist/canvas/ui/floatingLayer.js +52 -0
- package/dist/canvas/ui/icons.d.ts +12 -0
- package/dist/canvas/ui/icons.js +7 -0
- package/dist/canvas/ui/keyHints.d.ts +27 -0
- package/dist/canvas/ui/keyHints.js +43 -0
- package/dist/canvas/ui/popoverSide.d.ts +5 -0
- package/dist/canvas/ui/popoverSide.js +20 -0
- package/dist/canvas/ui/selectionTraits.d.ts +61 -0
- package/dist/canvas/ui/selectionTraits.js +99 -0
- package/dist/canvas/view/CanvasStage.svelte +132 -0
- package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
- package/dist/canvas/view/EraserTrail.svelte +50 -0
- package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
- package/dist/canvas/view/ObjectView.svelte +245 -0
- package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
- package/dist/canvas/view/SelectionOverlay.svelte +487 -0
- package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
- package/dist/canvas/view/TextEditor.svelte +76 -0
- package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.js +34 -0
- package/dist/canvas/view/geometry.d.ts +169 -0
- package/dist/canvas/view/geometry.js +272 -0
- package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
- package/dist/components/index.js +1 -1
- package/dist/components/input-controls/ColorIC.svelte +2 -2
- package/dist/engine/EngineCore.d.ts +273 -0
- package/dist/engine/EngineCore.js +1051 -0
- package/dist/engine/codec/canvasDocument.d.ts +57 -0
- package/dist/engine/codec/canvasDocument.js +471 -0
- package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
- package/dist/engine/codec/hostReadableKeys.js +25 -0
- package/dist/engine/core/arrange.d.ts +20 -0
- package/dist/engine/core/arrange.js +81 -0
- package/dist/engine/core/arrow.d.ts +91 -0
- package/dist/engine/core/arrow.js +182 -0
- package/dist/engine/core/brush.d.ts +68 -0
- package/dist/engine/core/brush.js +173 -0
- package/dist/engine/core/defaults.d.ts +70 -0
- package/dist/engine/core/defaults.js +158 -0
- package/dist/engine/core/freehand.d.ts +130 -0
- package/dist/engine/core/freehand.js +430 -0
- package/dist/engine/core/geometry.d.ts +44 -0
- package/dist/engine/core/geometry.js +216 -0
- package/dist/engine/core/history.d.ts +39 -0
- package/dist/engine/core/history.js +69 -0
- package/dist/engine/core/hitTest.d.ts +54 -0
- package/dist/engine/core/hitTest.js +298 -0
- package/dist/engine/core/matrix.d.ts +47 -0
- package/dist/engine/core/matrix.js +101 -0
- package/dist/engine/core/pathData.d.ts +50 -0
- package/dist/engine/core/pathData.js +550 -0
- package/dist/engine/core/rotationReset.d.ts +10 -0
- package/dist/engine/core/rotationReset.js +17 -0
- package/dist/engine/core/scene.d.ts +99 -0
- package/dist/engine/core/scene.js +450 -0
- package/dist/engine/core/scribble.d.ts +64 -0
- package/dist/engine/core/scribble.js +121 -0
- package/dist/engine/core/shapePaths.d.ts +34 -0
- package/dist/engine/core/shapePaths.js +69 -0
- package/dist/engine/core/shapeTools.d.ts +80 -0
- package/dist/engine/core/shapeTools.js +161 -0
- package/dist/engine/core/snapping.d.ts +64 -0
- package/dist/engine/core/snapping.js +164 -0
- package/dist/engine/core/style.d.ts +40 -0
- package/dist/engine/core/style.js +151 -0
- package/dist/engine/core/transform.d.ts +79 -0
- package/dist/engine/core/transform.js +258 -0
- package/dist/engine/core/types.d.ts +238 -0
- package/dist/engine/core/types.js +31 -0
- package/dist/engine/core/viewport.d.ts +32 -0
- package/dist/engine/core/viewport.js +84 -0
- package/dist/engine/input/PointerController.d.ts +38 -0
- package/dist/engine/input/PointerController.js +545 -0
- package/dist/engine/input/arrowHandles.d.ts +38 -0
- package/dist/engine/input/arrowHandles.js +60 -0
- package/dist/engine/input/gestures.d.ts +79 -0
- package/dist/engine/input/gestures.js +769 -0
- package/dist/engine/input/hoverCursor.d.ts +29 -0
- package/dist/engine/input/hoverCursor.js +23 -0
- package/dist/engine/input/keyboard.d.ts +75 -0
- package/dist/engine/input/keyboard.js +264 -0
- package/dist/engine/input/selectionHandles.d.ts +89 -0
- package/dist/engine/input/selectionHandles.js +169 -0
- package/dist/engine/input/types.d.ts +82 -0
- package/dist/engine/input/types.js +5 -0
- package/dist/engine/render/rasterize.d.ts +74 -0
- package/dist/engine/render/rasterize.js +273 -0
- package/dist/engine/state.d.ts +124 -0
- package/dist/engine/state.js +47 -0
- package/dist/engine/text/layoutText.d.ts +53 -0
- package/dist/engine/text/layoutText.js +170 -0
- package/dist/engine/widgets.d.ts +124 -0
- package/dist/engine/widgets.js +345 -0
- package/dist/i18n/labels.d.ts +4 -15
- package/dist/i18n/labels.js +33 -53
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +51 -122
- package/dist/i18n/messages.js +58 -131
- package/dist/i18n/shortcuts.d.ts +0 -1
- package/dist/i18n/shortcuts.js +2 -11
- package/dist/index.d.ts +4 -2
- package/dist/index.js +5 -3
- package/package.json +30 -5
- package/dist/canvas/Canvas.svelte +0 -13363
- package/dist/canvas/Canvas.svelte.d.ts +0 -286
- package/dist/canvas/CanvasToolbar.svelte +0 -1498
- package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
- package/dist/canvas/ContextMenu.svelte +0 -305
- package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
- package/dist/canvas/PanZoomPanel.svelte +0 -478
- package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
- package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
- package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
- package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
- package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
- package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
- package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
- package/dist/canvas/shadowRootText.d.ts +0 -5
- package/dist/canvas/shadowRootText.js +0 -43
- package/dist/canvas/shapes/FrameShape.d.ts +0 -97
- package/dist/canvas/shapes/FrameShape.js +0 -953
- package/dist/canvas/shapes/ImageShape.d.ts +0 -47
- package/dist/canvas/shapes/ImageShape.js +0 -339
- package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
- package/dist/canvas/shapes/WidgetShape.js +0 -200
- package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
- package/dist/canvas/widgetPreviewGeometry.js +0 -66
- package/dist/i18n/shapeWords.d.ts +0 -30
- package/dist/i18n/shapeWords.js +0 -36
- package/dist/i18n/slots.d.ts +0 -20
- package/dist/i18n/slots.js +0 -40
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading and writing a saved canvas.
|
|
3
|
+
*
|
|
4
|
+
* The format is the canvas's own (version 2): a small envelope around a flat, z-ordered list of the records
|
|
5
|
+
* of `core/types.ts`. Only the keys named in `hostReadableKeys.ts` are read by code outside the canvas.
|
|
6
|
+
*
|
|
7
|
+
* - The writer is deterministic: the same board and viewport at the same moment give the same text.
|
|
8
|
+
* - The reader never throws. A saved text that is not JSON, is not a version 2 document or holds objects it
|
|
9
|
+
* does not understand gives an empty (or shorter) board and says why in `warnings`; the host decides what to
|
|
10
|
+
* show. Nothing is written back until the user changes the board, so a board that failed to open is never
|
|
11
|
+
* overwritten by the empty one.
|
|
12
|
+
* - Everything read is validated and clamped, because a saved canvas is data from a database and from other
|
|
13
|
+
* people's browsers: colours, fonts and addresses that could carry more than a value are replaced by defaults.
|
|
14
|
+
*/
|
|
15
|
+
import type { CanvasDocument, SceneObject, Viewport } from '../core/types.js';
|
|
16
|
+
/** The most objects a board may hold when it is read; a text with more is cut here and says so. */
|
|
17
|
+
export declare const MAX_OBJECTS_PER_DOCUMENT = 20000;
|
|
18
|
+
export type CanvasDocumentWarningCode = 'not-json' | 'not-a-document' | 'unsupported-version' | 'too-many-objects' | 'unknown-type' | 'bad-object' | 'bad-path';
|
|
19
|
+
export interface CanvasDocumentWarning {
|
|
20
|
+
code: CanvasDocumentWarningCode;
|
|
21
|
+
/** The position of the object in the saved list, when the warning is about one. */
|
|
22
|
+
index?: number;
|
|
23
|
+
detail?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface ParsedCanvas {
|
|
26
|
+
scene: SceneObject[];
|
|
27
|
+
viewport: Viewport;
|
|
28
|
+
/**
|
|
29
|
+
* `ok`: a version 2 document (it may hold no objects). `empty`: there was nothing to read.
|
|
30
|
+
* `unsupported`: a document of another version, such as the files the previous canvas wrote (they are not converted).
|
|
31
|
+
* `invalid`: not a document.
|
|
32
|
+
*/
|
|
33
|
+
status: 'ok' | 'empty' | 'unsupported' | 'invalid';
|
|
34
|
+
warnings: CanvasDocumentWarning[];
|
|
35
|
+
}
|
|
36
|
+
/** The part of the host's image store the writer needs: where to put the bytes it leaves out of the text. */
|
|
37
|
+
export interface ImageBytesSink {
|
|
38
|
+
save(ref: string, dataUrl: string): void;
|
|
39
|
+
}
|
|
40
|
+
export interface SerializeOptions {
|
|
41
|
+
/** The moment written as `savedAt`. */
|
|
42
|
+
now?: Date;
|
|
43
|
+
/** When given, inline image bytes go here and only a reference is written. */
|
|
44
|
+
imageStore?: ImageBytesSink | null;
|
|
45
|
+
}
|
|
46
|
+
/** The document for a board, as a plain object (the form `serializeCanvasDocument` writes). */
|
|
47
|
+
export declare function createCanvasDocument(scene: readonly SceneObject[], viewport: Viewport, options?: SerializeOptions): CanvasDocument;
|
|
48
|
+
/** The text a board is saved as. */
|
|
49
|
+
export declare function serializeCanvasDocument(scene: readonly SceneObject[], viewport: Viewport, options?: SerializeOptions): string;
|
|
50
|
+
export declare function readColor(value: unknown, fallback: string): string;
|
|
51
|
+
/** An image address the browser may load into an `<img>`: inline bytes, http(s), a blob or a path of this site. */
|
|
52
|
+
export declare function readImageAddress(value: unknown): string | null;
|
|
53
|
+
/**
|
|
54
|
+
* Reads a saved canvas: the text `serializeCanvasDocument` wrote, or the same thing already parsed.
|
|
55
|
+
* Never throws.
|
|
56
|
+
*/
|
|
57
|
+
export declare function parseCanvasDocument(input: unknown): ParsedCanvas;
|
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading and writing a saved canvas.
|
|
3
|
+
*
|
|
4
|
+
* The format is the canvas's own (version 2): a small envelope around a flat, z-ordered list of the records
|
|
5
|
+
* of `core/types.ts`. Only the keys named in `hostReadableKeys.ts` are read by code outside the canvas.
|
|
6
|
+
*
|
|
7
|
+
* - The writer is deterministic: the same board and viewport at the same moment give the same text.
|
|
8
|
+
* - The reader never throws. A saved text that is not JSON, is not a version 2 document or holds objects it
|
|
9
|
+
* does not understand gives an empty (or shorter) board and says why in `warnings`; the host decides what to
|
|
10
|
+
* show. Nothing is written back until the user changes the board, so a board that failed to open is never
|
|
11
|
+
* overwritten by the empty one.
|
|
12
|
+
* - Everything read is validated and clamped, because a saved canvas is data from a database and from other
|
|
13
|
+
* people's browsers: colours, fonts and addresses that could carry more than a value are replaced by defaults.
|
|
14
|
+
*/
|
|
15
|
+
import { arrowPointsOfLegacyPath, buildArrow } from '../core/arrow.js';
|
|
16
|
+
import { BRUSH_MAX_POINTS, brushOutlineData } from '../core/brush.js';
|
|
17
|
+
import { createEllipseObject, createGroupObject, createImageObject, createObjectId, createPathObject, createRectObject, createTextObject, createWidgetObject, TEXT_DEFAULTS } from '../core/defaults.js';
|
|
18
|
+
import { clampNumber, roundTo } from '../core/geometry.js';
|
|
19
|
+
import { degreesToRadians, normalizeDegrees } from '../core/matrix.js';
|
|
20
|
+
import { parsePathData, serializePathData, translatePathCommands } from '../core/pathData.js';
|
|
21
|
+
import { normalizeScene } from '../core/scene.js';
|
|
22
|
+
import { CANVAS_DOCUMENT_VERSION, MIN_OBJECT_SIZE } from '../core/types.js';
|
|
23
|
+
import { sanitizeViewport } from '../core/viewport.js';
|
|
24
|
+
import { HOST_READABLE_KEYS } from './hostReadableKeys.js';
|
|
25
|
+
/** The most objects a board may hold when it is read; a text with more is cut here and says so. */
|
|
26
|
+
export const MAX_OBJECTS_PER_DOCUMENT = 20_000;
|
|
27
|
+
const MAX_TEXT_LENGTH = 100_000;
|
|
28
|
+
const MAX_PATH_DATA_LENGTH = 500_000;
|
|
29
|
+
const MAX_COORDINATE = 1_000_000;
|
|
30
|
+
const MAX_SIZE = 100_000;
|
|
31
|
+
// --- Writing -----------------------------------------------------------------------------------
|
|
32
|
+
const BOX_KEYS = ['x', 'y', 'width', 'height', 'rotation', 'opacity'];
|
|
33
|
+
const PAINT_KEYS = ['fill', 'fillOpacity', 'stroke', 'strokeWidth', 'strokeStyle'];
|
|
34
|
+
// The words audit reads every string inside a property called `text` as a sentence a person can read; these are the
|
|
35
|
+
// names of fields, so the list of a text object's keys lives in a constant of its own.
|
|
36
|
+
const TEXT_OBJECT_KEYS = [
|
|
37
|
+
...BOX_KEYS,
|
|
38
|
+
'text',
|
|
39
|
+
'color',
|
|
40
|
+
'fontSize',
|
|
41
|
+
'fontFamily',
|
|
42
|
+
'fontWeight',
|
|
43
|
+
'fontStyle',
|
|
44
|
+
'underline',
|
|
45
|
+
'align',
|
|
46
|
+
'lineHeight',
|
|
47
|
+
'padding',
|
|
48
|
+
'backgroundColor',
|
|
49
|
+
'autoWidth'
|
|
50
|
+
];
|
|
51
|
+
const KEYS_BY_TYPE = {
|
|
52
|
+
rect: [...BOX_KEYS, ...PAINT_KEYS, 'roundness'],
|
|
53
|
+
ellipse: [...BOX_KEYS, ...PAINT_KEYS, 'aspectLocked'],
|
|
54
|
+
path: [...BOX_KEYS, ...PAINT_KEYS, 'd', 'shape', 'roundness', 'aspectLocked', 'flipX', 'flipY', 'lineCap', 'lineJoin', 'ink', 'arrow'],
|
|
55
|
+
text: TEXT_OBJECT_KEYS,
|
|
56
|
+
image: [...BOX_KEYS, 'src', 'imageRef', 'fileName', 'naturalWidth', 'naturalHeight'],
|
|
57
|
+
widget: [...BOX_KEYS, 'widgetId', 'widgetName', 'isPrimary', 'thumbnail', 'thumbnailRef'],
|
|
58
|
+
group: []
|
|
59
|
+
};
|
|
60
|
+
/** The reference and the (possibly inline) bytes of a picture, with the bytes moved to the store when there is one. */
|
|
61
|
+
function externalizeBytes(inline, existingRef, makeRef, store) {
|
|
62
|
+
if (!store || !inline || !inline.startsWith('data:'))
|
|
63
|
+
return { value: inline, ref: existingRef };
|
|
64
|
+
const ref = existingRef ?? makeRef();
|
|
65
|
+
if (!ref)
|
|
66
|
+
return { value: inline, ref: existingRef };
|
|
67
|
+
store.save(ref, inline);
|
|
68
|
+
return { value: null, ref };
|
|
69
|
+
}
|
|
70
|
+
function serializeObject(object, options) {
|
|
71
|
+
const record = { id: object.id, [HOST_READABLE_KEYS.objectType]: object.type, parentId: object.parentId };
|
|
72
|
+
const source = object;
|
|
73
|
+
let overrides = {};
|
|
74
|
+
if (object.type === 'widget') {
|
|
75
|
+
const thumbnail = externalizeBytes(object.thumbnail, object.thumbnailRef, () => `widget:${object.widgetId}`, options.imageStore);
|
|
76
|
+
overrides = { thumbnail: thumbnail.value, thumbnailRef: thumbnail.ref };
|
|
77
|
+
}
|
|
78
|
+
else if (object.type === 'image') {
|
|
79
|
+
const image = externalizeBytes(object.src, object.imageRef, () => `shape:${object.id}`, options.imageStore);
|
|
80
|
+
overrides = { src: image.value, imageRef: image.ref };
|
|
81
|
+
}
|
|
82
|
+
for (const key of KEYS_BY_TYPE[object.type]) {
|
|
83
|
+
const value = key in overrides ? overrides[key] : source[key];
|
|
84
|
+
record[key] = typeof value === 'number' ? roundTo(value, 3) : value;
|
|
85
|
+
}
|
|
86
|
+
if (object.type === 'widget') {
|
|
87
|
+
// The two keys the creator reads from the saved text.
|
|
88
|
+
record[HOST_READABLE_KEYS.widgetId] = object.widgetId;
|
|
89
|
+
record[HOST_READABLE_KEYS.isWidgetImage] = true;
|
|
90
|
+
}
|
|
91
|
+
if (object.type === 'path') {
|
|
92
|
+
// A brush stroke is saved as its line: the outline is drawn again from it when the board is read, so it is never saved
|
|
93
|
+
// twice and cannot be out of step with the line it belongs to. Any other path has no line to save.
|
|
94
|
+
if (object.ink) {
|
|
95
|
+
delete record.d;
|
|
96
|
+
// In the order the reader gives it back, so that a board saved, read and saved again is the same text.
|
|
97
|
+
record.ink = { points: object.ink.points, pressure: object.ink.pressure };
|
|
98
|
+
}
|
|
99
|
+
else
|
|
100
|
+
delete record.ink;
|
|
101
|
+
// An arrow is saved as its two ends and its bend, for the same reason: its head and shaft are drawn from them when it is read.
|
|
102
|
+
if (object.arrow) {
|
|
103
|
+
delete record.d;
|
|
104
|
+
const { start, end, bend } = object.arrow;
|
|
105
|
+
record.arrow = { start: [roundTo(start.x, 3), roundTo(start.y, 3)], end: [roundTo(end.x, 3), roundTo(end.y, 3)], bend: roundTo(bend, 3) };
|
|
106
|
+
}
|
|
107
|
+
else
|
|
108
|
+
delete record.arrow;
|
|
109
|
+
}
|
|
110
|
+
return record;
|
|
111
|
+
}
|
|
112
|
+
/** The document for a board, as a plain object (the form `serializeCanvasDocument` writes). */
|
|
113
|
+
export function createCanvasDocument(scene, viewport, options = {}) {
|
|
114
|
+
return {
|
|
115
|
+
version: CANVAS_DOCUMENT_VERSION,
|
|
116
|
+
savedAt: (options.now ?? new Date()).toISOString(),
|
|
117
|
+
viewport: { x: roundTo(viewport.x, 2), y: roundTo(viewport.y, 2), zoom: roundTo(viewport.zoom, 4) },
|
|
118
|
+
canvas: { objects: scene.map((object) => serializeObject(object, options)) }
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** The text a board is saved as. */
|
|
122
|
+
export function serializeCanvasDocument(scene, viewport, options = {}) {
|
|
123
|
+
return JSON.stringify(createCanvasDocument(scene, viewport, options));
|
|
124
|
+
}
|
|
125
|
+
function isRecord(value) {
|
|
126
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
127
|
+
}
|
|
128
|
+
function readNumber(value, fallback, minimum = -Infinity, maximum = Infinity) {
|
|
129
|
+
const number = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN;
|
|
130
|
+
if (!Number.isFinite(number))
|
|
131
|
+
return fallback;
|
|
132
|
+
return clampNumber(number, minimum, maximum);
|
|
133
|
+
}
|
|
134
|
+
function readBoolean(value, fallback) {
|
|
135
|
+
return typeof value === 'boolean' ? value : fallback;
|
|
136
|
+
}
|
|
137
|
+
function readString(value, fallback, maximumLength = 1000) {
|
|
138
|
+
return typeof value === 'string' ? value.slice(0, maximumLength) : fallback;
|
|
139
|
+
}
|
|
140
|
+
function readNullableString(value, maximumLength = 1000) {
|
|
141
|
+
return typeof value === 'string' && value !== '' ? value.slice(0, maximumLength) : null;
|
|
142
|
+
}
|
|
143
|
+
function readOneOf(value, allowed, fallback) {
|
|
144
|
+
return typeof value === 'string' && allowed.includes(value) ? value : fallback;
|
|
145
|
+
}
|
|
146
|
+
/** A hex, `rgb()`, `hsl()` or named colour: nothing that could carry more than a colour into CSS or SVG. */
|
|
147
|
+
const COLOR_PATTERN = /^(#[0-9a-f]{3,8}|(?:rgb|hsl)a?\([0-9a-z\s.,%/+-]{1,80}\)|[a-z]{3,30})$/i;
|
|
148
|
+
export function readColor(value, fallback) {
|
|
149
|
+
return typeof value === 'string' && COLOR_PATTERN.test(value.trim()) ? value.trim() : fallback;
|
|
150
|
+
}
|
|
151
|
+
const FONT_FAMILY_PATTERN = /^[\p{L}\p{N}\s,'"\-_.]{1,200}$/u;
|
|
152
|
+
function readFontFamily(value) {
|
|
153
|
+
return typeof value === 'string' && FONT_FAMILY_PATTERN.test(value) ? value : TEXT_DEFAULTS.fontFamily;
|
|
154
|
+
}
|
|
155
|
+
const IMAGE_ADDRESS_PATTERN = /^(?:data:image\/[a-z0-9.+-]+[;,]|https?:\/\/|blob:|\/(?!\/))/i;
|
|
156
|
+
/** An image address the browser may load into an `<img>`: inline bytes, http(s), a blob or a path of this site. */
|
|
157
|
+
export function readImageAddress(value) {
|
|
158
|
+
if (typeof value !== 'string' || value === '')
|
|
159
|
+
return null;
|
|
160
|
+
return IMAGE_ADDRESS_PATTERN.test(value) ? value : null;
|
|
161
|
+
}
|
|
162
|
+
const STROKE_STYLES = ['solid', 'dashed', 'dotted'];
|
|
163
|
+
const LINE_CAPS = ['butt', 'round', 'square'];
|
|
164
|
+
const LINE_JOINS = ['miter', 'round', 'bevel'];
|
|
165
|
+
function readBox(raw) {
|
|
166
|
+
return {
|
|
167
|
+
x: readNumber(raw.x, 0, -MAX_COORDINATE, MAX_COORDINATE),
|
|
168
|
+
y: readNumber(raw.y, 0, -MAX_COORDINATE, MAX_COORDINATE),
|
|
169
|
+
width: readNumber(raw.width, 100, MIN_OBJECT_SIZE, MAX_SIZE),
|
|
170
|
+
height: readNumber(raw.height, 100, MIN_OBJECT_SIZE, MAX_SIZE),
|
|
171
|
+
rotation: normalizeDegrees(readNumber(raw.rotation, 0, -1e6, 1e6)),
|
|
172
|
+
opacity: readNumber(raw.opacity, 1, 0, 1)
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function readPaint(raw, defaults) {
|
|
176
|
+
return {
|
|
177
|
+
fill: readColor(raw.fill, defaults.fill),
|
|
178
|
+
fillOpacity: readNumber(raw.fillOpacity, 1, 0, 1),
|
|
179
|
+
stroke: readColor(raw.stroke, defaults.stroke),
|
|
180
|
+
strokeWidth: readNumber(raw.strokeWidth, defaults.strokeWidth, 0, 200),
|
|
181
|
+
strokeStyle: readOneOf(raw.strokeStyle, STROKE_STYLES, 'solid')
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const PAINT_FALLBACK = { fill: '#ffffff', stroke: '#374151', strokeWidth: 2 };
|
|
185
|
+
function readId(raw, seen) {
|
|
186
|
+
let id = typeof raw.id === 'string' && raw.id !== '' && raw.id.length <= 128 ? raw.id : createObjectId();
|
|
187
|
+
// Two objects with the same id would be one on the board: the second gets a new one.
|
|
188
|
+
while (seen.has(id))
|
|
189
|
+
id = createObjectId();
|
|
190
|
+
seen.add(id);
|
|
191
|
+
return id;
|
|
192
|
+
}
|
|
193
|
+
function readParentId(raw) {
|
|
194
|
+
return typeof raw.parentId === 'string' && raw.parentId !== '' && raw.parentId.length <= 128 ? raw.parentId : null;
|
|
195
|
+
}
|
|
196
|
+
function readRect(raw, id) {
|
|
197
|
+
return createRectObject({
|
|
198
|
+
id,
|
|
199
|
+
parentId: readParentId(raw),
|
|
200
|
+
...readBox(raw),
|
|
201
|
+
...readPaint(raw, PAINT_FALLBACK),
|
|
202
|
+
roundness: readNumber(raw.roundness, 0, 0, 100)
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function readEllipse(raw, id) {
|
|
206
|
+
return createEllipseObject({
|
|
207
|
+
id,
|
|
208
|
+
parentId: readParentId(raw),
|
|
209
|
+
...readBox(raw),
|
|
210
|
+
...readPaint(raw, PAINT_FALLBACK),
|
|
211
|
+
aspectLocked: readBoolean(raw.aspectLocked, false)
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
const BRUSH_PRESSURES = ['pen', 'speed'];
|
|
215
|
+
/** The line of a brush stroke, or null when there is none to draw: points that are not numbers are dropped, and a long line is cut. */
|
|
216
|
+
function readInk(value) {
|
|
217
|
+
if (!isRecord(value) || !Array.isArray(value.points))
|
|
218
|
+
return null;
|
|
219
|
+
const points = [];
|
|
220
|
+
for (const entry of value.points.slice(0, BRUSH_MAX_POINTS)) {
|
|
221
|
+
if (!Array.isArray(entry) || typeof entry[0] !== 'number' || typeof entry[1] !== 'number')
|
|
222
|
+
continue;
|
|
223
|
+
if (!Number.isFinite(entry[0]) || !Number.isFinite(entry[1]))
|
|
224
|
+
continue;
|
|
225
|
+
points.push([
|
|
226
|
+
clampNumber(entry[0], -MAX_COORDINATE, MAX_COORDINATE),
|
|
227
|
+
clampNumber(entry[1], -MAX_COORDINATE, MAX_COORDINATE),
|
|
228
|
+
readNumber(entry[2], 0.5, 0, 1)
|
|
229
|
+
]);
|
|
230
|
+
}
|
|
231
|
+
if (points.length === 0)
|
|
232
|
+
return null;
|
|
233
|
+
return { points, pressure: readOneOf(value.pressure, BRUSH_PRESSURES, 'speed') };
|
|
234
|
+
}
|
|
235
|
+
/** The two ends and the bend of an arrow, or null when the record has none that can be read. */
|
|
236
|
+
function readArrowSpec(value) {
|
|
237
|
+
if (!isRecord(value))
|
|
238
|
+
return null;
|
|
239
|
+
const readPoint = (entry) => {
|
|
240
|
+
if (!Array.isArray(entry) || typeof entry[0] !== 'number' || typeof entry[1] !== 'number')
|
|
241
|
+
return null;
|
|
242
|
+
if (!Number.isFinite(entry[0]) || !Number.isFinite(entry[1]))
|
|
243
|
+
return null;
|
|
244
|
+
return { x: clampNumber(entry[0], -MAX_COORDINATE, MAX_COORDINATE), y: clampNumber(entry[1], -MAX_COORDINATE, MAX_COORDINATE) };
|
|
245
|
+
};
|
|
246
|
+
const start = readPoint(value.start);
|
|
247
|
+
const end = readPoint(value.end);
|
|
248
|
+
if (!start || !end)
|
|
249
|
+
return null;
|
|
250
|
+
return { start, end, bend: readNumber(value.bend, 0, -2 * MAX_SIZE, 2 * MAX_SIZE) };
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* An arrow drawn from its points. They are in the coordinates of the saved box, which is kept as it is when it holds what is drawn
|
|
254
|
+
* (to within half a unit); a box that does not (a text written by hand) is replaced by the one that does, with every point staying
|
|
255
|
+
* where it was in the world. An arrow is upright: a turn the record carries is applied to its points.
|
|
256
|
+
*/
|
|
257
|
+
function arrowFromSpec(spec, box, strokeWidth) {
|
|
258
|
+
let { start, end } = spec;
|
|
259
|
+
if (box.rotation !== 0) {
|
|
260
|
+
const radians = degreesToRadians(box.rotation);
|
|
261
|
+
const cosine = Math.cos(radians);
|
|
262
|
+
const sine = Math.sin(radians);
|
|
263
|
+
const centerX = box.width / 2;
|
|
264
|
+
const centerY = box.height / 2;
|
|
265
|
+
const turn = (point) => ({
|
|
266
|
+
x: centerX + (point.x - centerX) * cosine - (point.y - centerY) * sine,
|
|
267
|
+
y: centerY + (point.x - centerX) * sine + (point.y - centerY) * cosine
|
|
268
|
+
});
|
|
269
|
+
start = turn(start);
|
|
270
|
+
end = turn(end);
|
|
271
|
+
}
|
|
272
|
+
const geometry = buildArrow(start, end, spec.bend, strokeWidth);
|
|
273
|
+
const holdsWhatIsDrawn = box.rotation === 0 &&
|
|
274
|
+
Math.abs(geometry.box.x) <= 0.5 &&
|
|
275
|
+
Math.abs(geometry.box.y) <= 0.5 &&
|
|
276
|
+
Math.abs(geometry.box.width - box.width) <= 0.5 &&
|
|
277
|
+
Math.abs(geometry.box.height - box.height) <= 0.5;
|
|
278
|
+
if (holdsWhatIsDrawn) {
|
|
279
|
+
// In the frame of the points, which is the frame of the saved box: the path comes back in the frame of its own bounds.
|
|
280
|
+
const commands = translatePathCommands(geometry.commands, geometry.box.x, geometry.box.y);
|
|
281
|
+
return { d: serializePathData(commands), arrow: { start, end, bend: geometry.spec.bend }, box };
|
|
282
|
+
}
|
|
283
|
+
return {
|
|
284
|
+
d: serializePathData(geometry.commands),
|
|
285
|
+
arrow: geometry.spec,
|
|
286
|
+
box: { ...box, rotation: 0, x: box.x + geometry.box.x, y: box.y + geometry.box.y, width: geometry.box.width, height: geometry.box.height }
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
function readPath(raw, id, index, warnings) {
|
|
290
|
+
const paint = readPaint(raw, { fill: 'transparent', stroke: '#1f2937', strokeWidth: 3 });
|
|
291
|
+
const ink = readInk(raw.ink);
|
|
292
|
+
const shape = readNullableString(raw.shape, 100);
|
|
293
|
+
let box = readBox(raw);
|
|
294
|
+
// An arrow has no outline of its own in the file either: it is drawn from its points. A board saved before arrows had points
|
|
295
|
+
// has the outline only, and its arrow is found in that.
|
|
296
|
+
let arrowSpec = ink ? null : readArrowSpec(raw.arrow);
|
|
297
|
+
if (!ink && !arrowSpec && shape === 'arrow' && typeof raw.d === 'string' && raw.d.length <= MAX_PATH_DATA_LENGTH) {
|
|
298
|
+
const legacy = arrowPointsOfLegacyPath(parsePathData(raw.d));
|
|
299
|
+
if (legacy)
|
|
300
|
+
arrowSpec = { ...legacy, bend: 0 };
|
|
301
|
+
}
|
|
302
|
+
let arrow = null;
|
|
303
|
+
let d;
|
|
304
|
+
if (arrowSpec) {
|
|
305
|
+
const drawn = arrowFromSpec(arrowSpec, box, paint.strokeWidth);
|
|
306
|
+
d = drawn.d;
|
|
307
|
+
arrow = drawn.arrow;
|
|
308
|
+
box = drawn.box;
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
// A brush stroke has no outline of its own in the file: it is drawn from its line.
|
|
312
|
+
d = ink ? brushOutlineData(ink, paint.strokeWidth) : typeof raw.d === 'string' ? raw.d : '';
|
|
313
|
+
}
|
|
314
|
+
if (d.length === 0 || d.length > MAX_PATH_DATA_LENGTH || parsePathData(d).length < 2) {
|
|
315
|
+
warnings.push({ code: 'bad-path', index, detail: 'The outline of the shape is missing or cannot be read.' });
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
return createPathObject({
|
|
319
|
+
id,
|
|
320
|
+
parentId: readParentId(raw),
|
|
321
|
+
...box,
|
|
322
|
+
...paint,
|
|
323
|
+
d,
|
|
324
|
+
shape,
|
|
325
|
+
roundness: readNumber(raw.roundness, 0, 0, 100),
|
|
326
|
+
aspectLocked: readBoolean(raw.aspectLocked, false),
|
|
327
|
+
flipX: readBoolean(raw.flipX, false),
|
|
328
|
+
flipY: readBoolean(raw.flipY, false),
|
|
329
|
+
lineCap: readOneOf(raw.lineCap, LINE_CAPS, 'round'),
|
|
330
|
+
lineJoin: readOneOf(raw.lineJoin, LINE_JOINS, 'round'),
|
|
331
|
+
ink,
|
|
332
|
+
arrow
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
function readText(raw, id) {
|
|
336
|
+
return createTextObject({
|
|
337
|
+
id,
|
|
338
|
+
parentId: readParentId(raw),
|
|
339
|
+
...readBox(raw),
|
|
340
|
+
text: readString(raw.text, '', MAX_TEXT_LENGTH),
|
|
341
|
+
color: readColor(raw.color, TEXT_DEFAULTS.color),
|
|
342
|
+
fontSize: readNumber(raw.fontSize, TEXT_DEFAULTS.fontSize, 4, 600),
|
|
343
|
+
fontFamily: readFontFamily(raw.fontFamily),
|
|
344
|
+
fontWeight: readOneOf(raw.fontWeight, ['normal', 'bold'], 'normal'),
|
|
345
|
+
fontStyle: readOneOf(raw.fontStyle, ['normal', 'italic'], 'normal'),
|
|
346
|
+
underline: readBoolean(raw.underline, false),
|
|
347
|
+
align: readOneOf(raw.align, ['left', 'center', 'right'], 'left'),
|
|
348
|
+
lineHeight: readNumber(raw.lineHeight, 1.2, 0.5, 4),
|
|
349
|
+
padding: readNumber(raw.padding, 0, 0, 200),
|
|
350
|
+
backgroundColor: readColor(raw.backgroundColor, 'transparent'),
|
|
351
|
+
autoWidth: readBoolean(raw.autoWidth, true)
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
function readImage(raw, id) {
|
|
355
|
+
return createImageObject({
|
|
356
|
+
id,
|
|
357
|
+
parentId: readParentId(raw),
|
|
358
|
+
...readBox(raw),
|
|
359
|
+
src: readImageAddress(raw.src),
|
|
360
|
+
imageRef: readNullableString(raw.imageRef, 200),
|
|
361
|
+
fileName: readNullableString(raw.fileName, 300),
|
|
362
|
+
naturalWidth: readNumber(raw.naturalWidth, 0, 0, MAX_SIZE),
|
|
363
|
+
naturalHeight: readNumber(raw.naturalHeight, 0, 0, MAX_SIZE)
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
function readWidget(raw, id, index, warnings) {
|
|
367
|
+
// The creator reads `_widgetId`; the canvas's own key is `widgetId`. Either names the widget.
|
|
368
|
+
const widgetId = readNullableString(raw.widgetId, 200) ?? readNullableString(raw[HOST_READABLE_KEYS.widgetId], 200);
|
|
369
|
+
if (!widgetId) {
|
|
370
|
+
warnings.push({ code: 'bad-object', index, detail: 'A widget card has no widget id.' });
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
return createWidgetObject({
|
|
374
|
+
id,
|
|
375
|
+
widgetId,
|
|
376
|
+
parentId: readParentId(raw),
|
|
377
|
+
...readBox(raw),
|
|
378
|
+
widgetName: readNullableString(raw.widgetName, 300),
|
|
379
|
+
isPrimary: readBoolean(raw.isPrimary, false),
|
|
380
|
+
thumbnail: readImageAddress(raw.thumbnail),
|
|
381
|
+
thumbnailRef: readNullableString(raw.thumbnailRef, 200)
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
function readGroup(raw, id) {
|
|
385
|
+
return createGroupObject({ id, parentId: readParentId(raw) });
|
|
386
|
+
}
|
|
387
|
+
function readObject(raw, index, seen, warnings) {
|
|
388
|
+
if (!isRecord(raw)) {
|
|
389
|
+
warnings.push({ code: 'bad-object', index, detail: 'An entry of the list is not an object.' });
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
// Readers outside the canvas treat these as "not there": so does the canvas.
|
|
393
|
+
if (raw[HOST_READABLE_KEYS.visible] === false || raw[HOST_READABLE_KEYS.excludeFromExport] === true)
|
|
394
|
+
return null;
|
|
395
|
+
const type = raw[HOST_READABLE_KEYS.objectType];
|
|
396
|
+
const known = ['rect', 'ellipse', 'path', 'text', 'image', 'widget', 'group'];
|
|
397
|
+
if (typeof type !== 'string' || !known.includes(type)) {
|
|
398
|
+
warnings.push({ code: 'unknown-type', index, detail: typeof type === 'string' ? type : String(type) });
|
|
399
|
+
return null;
|
|
400
|
+
}
|
|
401
|
+
const id = readId(raw, seen);
|
|
402
|
+
switch (type) {
|
|
403
|
+
case 'rect':
|
|
404
|
+
return readRect(raw, id);
|
|
405
|
+
case 'ellipse':
|
|
406
|
+
return readEllipse(raw, id);
|
|
407
|
+
case 'path':
|
|
408
|
+
return readPath(raw, id, index, warnings);
|
|
409
|
+
case 'text':
|
|
410
|
+
return readText(raw, id);
|
|
411
|
+
case 'image':
|
|
412
|
+
return readImage(raw, id);
|
|
413
|
+
case 'widget':
|
|
414
|
+
return readWidget(raw, id, index, warnings);
|
|
415
|
+
default:
|
|
416
|
+
return readGroup(raw, id);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const EMPTY_VIEWPORT = { x: 0, y: 0, zoom: 1 };
|
|
420
|
+
/**
|
|
421
|
+
* Reads a saved canvas: the text `serializeCanvasDocument` wrote, or the same thing already parsed.
|
|
422
|
+
* Never throws.
|
|
423
|
+
*/
|
|
424
|
+
export function parseCanvasDocument(input) {
|
|
425
|
+
const warnings = [];
|
|
426
|
+
const nothing = (status) => ({
|
|
427
|
+
scene: [],
|
|
428
|
+
viewport: { ...EMPTY_VIEWPORT },
|
|
429
|
+
status,
|
|
430
|
+
warnings
|
|
431
|
+
});
|
|
432
|
+
if (input === null || input === undefined || (typeof input === 'string' && input.trim() === ''))
|
|
433
|
+
return nothing('empty');
|
|
434
|
+
let document = input;
|
|
435
|
+
if (typeof input === 'string') {
|
|
436
|
+
try {
|
|
437
|
+
document = JSON.parse(input);
|
|
438
|
+
}
|
|
439
|
+
catch {
|
|
440
|
+
warnings.push({ code: 'not-json' });
|
|
441
|
+
return nothing('invalid');
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (!isRecord(document)) {
|
|
445
|
+
warnings.push({ code: 'not-a-document' });
|
|
446
|
+
return nothing('invalid');
|
|
447
|
+
}
|
|
448
|
+
if (document.version !== CANVAS_DOCUMENT_VERSION) {
|
|
449
|
+
warnings.push({ code: 'unsupported-version', detail: String(document.version ?? 'none') });
|
|
450
|
+
return nothing('unsupported');
|
|
451
|
+
}
|
|
452
|
+
const canvas = document.canvas;
|
|
453
|
+
const list = isRecord(canvas) && Array.isArray(canvas.objects) ? canvas.objects : [];
|
|
454
|
+
const limited = list.length > MAX_OBJECTS_PER_DOCUMENT ? list.slice(0, MAX_OBJECTS_PER_DOCUMENT) : list;
|
|
455
|
+
if (limited.length < list.length) {
|
|
456
|
+
warnings.push({ code: 'too-many-objects', detail: `${list.length} objects, the first ${limited.length} were read.` });
|
|
457
|
+
}
|
|
458
|
+
const seen = new Set();
|
|
459
|
+
const objects = [];
|
|
460
|
+
limited.forEach((raw, index) => {
|
|
461
|
+
const object = readObject(raw, index, seen, warnings);
|
|
462
|
+
if (object)
|
|
463
|
+
objects.push(object);
|
|
464
|
+
});
|
|
465
|
+
return {
|
|
466
|
+
scene: normalizeScene(objects),
|
|
467
|
+
viewport: sanitizeViewport(document.viewport),
|
|
468
|
+
status: 'ok',
|
|
469
|
+
warnings
|
|
470
|
+
};
|
|
471
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What code outside the canvas reads straight from a saved canvas.
|
|
3
|
+
*
|
|
4
|
+
* The widget creator parses the saved text itself, before (and without) any canvas: to count the objects
|
|
5
|
+
* (so it never autosaves an empty board over a full one), to find the widget cards (`_widgetId`), and to
|
|
6
|
+
* tell a card from a sketch (`_isWidgetImage`). The gateway stores the text without looking inside.
|
|
7
|
+
*
|
|
8
|
+
* These names are therefore part of the format and are frozen: the writer always emits them, the reader
|
|
9
|
+
* accepts them, and `tests/engine/codec.test.mjs` fails if either changes. Every other key of the format
|
|
10
|
+
* is the canvas's own and can change with a version.
|
|
11
|
+
*/
|
|
12
|
+
export declare const HOST_READABLE_KEYS: Readonly<{
|
|
13
|
+
/** `document.canvas.objects`: always an array. Readers fall back to `document.objects` for a bare list. */
|
|
14
|
+
documentObjects: readonly ["canvas", "objects"];
|
|
15
|
+
/** The kind of an object: a string. */
|
|
16
|
+
objectType: "type";
|
|
17
|
+
/** On a widget card: the id of the widget it stands for (a string). */
|
|
18
|
+
widgetId: "_widgetId";
|
|
19
|
+
/** On a widget card: always `true`. A sketch never has it. */
|
|
20
|
+
isWidgetImage: "_isWidgetImage";
|
|
21
|
+
/** A reader treats an object with `visible: false` as absent. The writer never writes it. */
|
|
22
|
+
visible: "visible";
|
|
23
|
+
/** A reader treats an object with `excludeFromExport: true` as absent. The writer never writes it. */
|
|
24
|
+
excludeFromExport: "excludeFromExport";
|
|
25
|
+
}>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What code outside the canvas reads straight from a saved canvas.
|
|
3
|
+
*
|
|
4
|
+
* The widget creator parses the saved text itself, before (and without) any canvas: to count the objects
|
|
5
|
+
* (so it never autosaves an empty board over a full one), to find the widget cards (`_widgetId`), and to
|
|
6
|
+
* tell a card from a sketch (`_isWidgetImage`). The gateway stores the text without looking inside.
|
|
7
|
+
*
|
|
8
|
+
* These names are therefore part of the format and are frozen: the writer always emits them, the reader
|
|
9
|
+
* accepts them, and `tests/engine/codec.test.mjs` fails if either changes. Every other key of the format
|
|
10
|
+
* is the canvas's own and can change with a version.
|
|
11
|
+
*/
|
|
12
|
+
export const HOST_READABLE_KEYS = Object.freeze({
|
|
13
|
+
/** `document.canvas.objects`: always an array. Readers fall back to `document.objects` for a bare list. */
|
|
14
|
+
documentObjects: Object.freeze(['canvas', 'objects']),
|
|
15
|
+
/** The kind of an object: a string. */
|
|
16
|
+
objectType: 'type',
|
|
17
|
+
/** On a widget card: the id of the widget it stands for (a string). */
|
|
18
|
+
widgetId: '_widgetId',
|
|
19
|
+
/** On a widget card: always `true`. A sketch never has it. */
|
|
20
|
+
isWidgetImage: '_isWidgetImage',
|
|
21
|
+
/** A reader treats an object with `visible: false` as absent. The writer never writes it. */
|
|
22
|
+
visible: 'visible',
|
|
23
|
+
/** A reader treats an object with `excludeFromExport: true` as absent. The writer never writes it. */
|
|
24
|
+
excludeFromExport: 'excludeFromExport'
|
|
25
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lining objects up with each other: align their edges or middles, and space them out evenly.
|
|
3
|
+
*
|
|
4
|
+
* Both work on the units of a selection (a group is one unit), measured by the rectangle around the shapes
|
|
5
|
+
* they draw, and move whole units: nothing is resized.
|
|
6
|
+
*/
|
|
7
|
+
import type { Scene } from './scene.js';
|
|
8
|
+
import type { ObjectId } from './types.js';
|
|
9
|
+
export type AlignMode = 'left' | 'center' | 'right' | 'top' | 'middle' | 'bottom';
|
|
10
|
+
export type DistributeAxis = 'horizontal' | 'vertical';
|
|
11
|
+
/**
|
|
12
|
+
* Moves every unit so that its chosen edge (or middle) is where the same edge of the whole selection is.
|
|
13
|
+
* Needs two units or more; returns the same scene otherwise, or when everything is already in line.
|
|
14
|
+
*/
|
|
15
|
+
export declare function alignObjects(scene: Scene, ids: Iterable<ObjectId>, mode: AlignMode): Scene;
|
|
16
|
+
/**
|
|
17
|
+
* Spaces the units so the gaps between neighbours are equal, keeping the first and the last where they are.
|
|
18
|
+
* Needs three units or more.
|
|
19
|
+
*/
|
|
20
|
+
export declare function distributeObjects(scene: Scene, ids: Iterable<ObjectId>, axis: DistributeAxis): Scene;
|