@widgetic/canvas 0.5.41 → 0.5.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/canvas/DomCanvas.svelte +49 -20
- package/dist/canvas/DomCanvas.svelte.d.ts +13 -7
- package/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.d.ts +14 -3
- package/dist/canvas/chromeLayout.js +30 -20
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.d.ts +47 -5
- package/dist/canvas/host/CanvasHost.js +1481 -1566
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/browserServices.js +65 -116
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/nearCards.d.ts +33 -0
- package/dist/canvas/host/nearCards.js +78 -0
- package/dist/canvas/host/pageEvents.d.ts +16 -4
- package/dist/canvas/host/pageEvents.js +17 -8
- package/dist/canvas/host/pictureSaves.d.ts +33 -0
- package/dist/canvas/host/pictureSaves.js +42 -0
- package/dist/canvas/host/screenRects.d.ts +5 -0
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/storedPictures.d.ts +88 -0
- package/dist/canvas/host/storedPictures.js +288 -0
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/index.d.ts +1 -1
- package/dist/canvas/index.js +1 -1
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.d.ts +45 -2
- package/dist/canvas/shapes/shapeImageStore.js +10 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.d.ts +2 -2
- package/dist/canvas/toolbarPosition.js +4 -4
- package/dist/canvas/ui/SelectionPanel.svelte +28 -5
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/SelectorIndicator.svelte +81 -0
- package/dist/canvas/ui/SelectorIndicator.svelte.d.ts +11 -0
- package/dist/canvas/ui/ToolButton.svelte +8 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +2 -2
- package/dist/canvas/ui/floatingCard.js +20 -20
- package/dist/canvas/ui/floatingLayer.d.ts +13 -2
- package/dist/canvas/ui/floatingLayer.js +18 -7
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +34 -40
- package/dist/canvas/view/CanvasStage.svelte +101 -67
- package/dist/canvas/view/LeftToRightScope.svelte +21 -0
- package/dist/canvas/view/LeftToRightScope.svelte.d.ts +7 -0
- package/dist/canvas/view/ObjectView.svelte +15 -10
- package/dist/canvas/view/ObjectView.svelte.d.ts +1 -1
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/canvas/view/liftedScene.d.ts +31 -0
- package/dist/canvas/view/liftedScene.js +146 -0
- package/dist/canvas/view/mountedScene.d.ts +71 -0
- package/dist/canvas/view/mountedScene.js +363 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +15 -1
- package/dist/engine/EngineCore.js +724 -797
- package/dist/engine/codec/canvasDocument.d.ts +5 -0
- package/dist/engine/codec/canvasDocument.js +212 -149
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +76 -89
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +67 -94
- package/dist/engine/core/defaults.js +90 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.d.ts +6 -0
- package/dist/engine/core/history.js +49 -50
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.d.ts +10 -0
- package/dist/engine/core/scene.js +153 -187
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +31 -64
- package/dist/engine/core/transform.js +83 -94
- package/dist/engine/core/types.d.ts +8 -2
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +467 -474
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +265 -309
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +27 -30
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.d.ts +20 -0
- package/dist/engine/state.js +5 -15
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.d.ts +10 -1
- package/dist/fonts/interFont.js +47 -75
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.d.ts +8 -1
- package/dist/i18n/messages.js +10 -2
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/settings/workspaceToolSettings.js +32 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +105 -141
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/types.js +1 -1
- package/package.json +1 -1
- package/dist/canvas/ui/SelectorBar.svelte +0 -84
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +0 -12
|
@@ -42,6 +42,11 @@ export interface SerializeOptions {
|
|
|
42
42
|
now?: Date;
|
|
43
43
|
/** When given, inline image bytes go here and only a reference is written. */
|
|
44
44
|
imageStore?: ImageBytesSink | null;
|
|
45
|
+
/**
|
|
46
|
+
* Called for a picture whose bytes the store refused (its `save` threw): they stay in the text instead. The engine writes
|
|
47
|
+
* nothing to the console, so the host decides how to report it (once per save, not once per picture).
|
|
48
|
+
*/
|
|
49
|
+
onImageStoreRefused?: (ref: string, error: unknown) => void;
|
|
45
50
|
}
|
|
46
51
|
/** The document for a board, as a plain object (the form `serializeCanvasDocument` writes). */
|
|
47
52
|
export declare function createCanvasDocument(scene: readonly SceneObject[], viewport: Viewport, options?: SerializeOptions): CanvasDocument;
|
|
@@ -12,26 +12,6 @@
|
|
|
12
12
|
* - Everything read is validated and clamped, because a saved canvas is data from a database and from other
|
|
13
13
|
* people's browsers: colours, fonts and addresses that could carry more than a value are replaced by defaults.
|
|
14
14
|
*/
|
|
15
|
-
var __assign = (this && this.__assign) || function () {
|
|
16
|
-
__assign = Object.assign || function(t) {
|
|
17
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
18
|
-
s = arguments[i];
|
|
19
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
20
|
-
t[p] = s[p];
|
|
21
|
-
}
|
|
22
|
-
return t;
|
|
23
|
-
};
|
|
24
|
-
return __assign.apply(this, arguments);
|
|
25
|
-
};
|
|
26
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
27
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
28
|
-
if (ar || !(i in from)) {
|
|
29
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
30
|
-
ar[i] = from[i];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
34
|
-
};
|
|
35
15
|
import { arrowPointsOfLegacyPath, buildArrow } from '../core/arrow.js';
|
|
36
16
|
import { dropDanglingBindings, withBindings } from '../core/arrowBindingRecords.js';
|
|
37
17
|
import { BRUSH_MAX_POINTS, brushOutlineData } from '../core/brush.js';
|
|
@@ -44,17 +24,18 @@ import { CANVAS_DOCUMENT_VERSION, MIN_OBJECT_SIZE } from '../core/types.js';
|
|
|
44
24
|
import { sanitizeViewport } from '../core/viewport.js';
|
|
45
25
|
import { HOST_READABLE_KEYS } from './hostReadableKeys.js';
|
|
46
26
|
/** The most objects a board may hold when it is read; a text with more is cut here and says so. */
|
|
47
|
-
export
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
27
|
+
export const MAX_OBJECTS_PER_DOCUMENT = 20_000;
|
|
28
|
+
const MAX_TEXT_LENGTH = 100_000;
|
|
29
|
+
const MAX_PATH_DATA_LENGTH = 500_000;
|
|
30
|
+
const MAX_COORDINATE = 1_000_000;
|
|
31
|
+
const MAX_SIZE = 100_000;
|
|
52
32
|
// --- Writing -----------------------------------------------------------------------------------
|
|
53
|
-
|
|
54
|
-
|
|
33
|
+
const BOX_KEYS = ['x', 'y', 'width', 'height', 'rotation', 'opacity'];
|
|
34
|
+
const PAINT_KEYS = ['fill', 'fillOpacity', 'stroke', 'strokeWidth', 'strokeStyle'];
|
|
55
35
|
// The words audit reads every string inside a property called `text` as a sentence a person can read; these are the
|
|
56
36
|
// names of fields, so the list of a text object's keys lives in a constant of its own.
|
|
57
|
-
|
|
37
|
+
const TEXT_OBJECT_KEYS = [
|
|
38
|
+
...BOX_KEYS,
|
|
58
39
|
'text',
|
|
59
40
|
'color',
|
|
60
41
|
'fontSize',
|
|
@@ -67,25 +48,52 @@ var TEXT_OBJECT_KEYS = __spreadArray(__spreadArray([], BOX_KEYS, true), [
|
|
|
67
48
|
'padding',
|
|
68
49
|
'backgroundColor',
|
|
69
50
|
'autoWidth'
|
|
70
|
-
]
|
|
71
|
-
|
|
72
|
-
rect:
|
|
73
|
-
ellipse:
|
|
74
|
-
path:
|
|
51
|
+
];
|
|
52
|
+
const KEYS_BY_TYPE = {
|
|
53
|
+
rect: [...BOX_KEYS, ...PAINT_KEYS, 'roundness'],
|
|
54
|
+
ellipse: [...BOX_KEYS, ...PAINT_KEYS, 'aspectLocked'],
|
|
55
|
+
path: [...BOX_KEYS, ...PAINT_KEYS, 'd', 'shape', 'roundness', 'aspectLocked', 'flipX', 'flipY', 'lineCap', 'lineJoin', 'ink', 'arrow'],
|
|
75
56
|
text: TEXT_OBJECT_KEYS,
|
|
76
|
-
image:
|
|
77
|
-
widget:
|
|
57
|
+
image: [...BOX_KEYS, 'src', 'imageRef', 'fileName', 'naturalWidth', 'naturalHeight'],
|
|
58
|
+
widget: [...BOX_KEYS, 'widgetId', 'widgetName', 'isPrimary', 'thumbnail', 'thumbnailRef'],
|
|
78
59
|
group: []
|
|
79
60
|
};
|
|
61
|
+
/**
|
|
62
|
+
* Whether an address only lives as long as the page that made it (an object URL, `blob:`). A store may answer a reference with
|
|
63
|
+
* one instead of bytes, so that the picture is not a string megabytes long in the board and in every `<img>` that shows it.
|
|
64
|
+
*/
|
|
65
|
+
function isPageLocalAddress(address) {
|
|
66
|
+
return address.startsWith('blob:');
|
|
67
|
+
}
|
|
80
68
|
/** The reference and the (possibly inline) bytes of a picture, with the bytes moved to the store when there is one. */
|
|
81
|
-
function externalizeBytes(inline, existingRef, makeRef,
|
|
69
|
+
function externalizeBytes(inline, existingRef, makeRef, options) {
|
|
70
|
+
const store = options.imageStore;
|
|
71
|
+
// An object URL the store gave for a reference is not bytes, and it must never reach the text: the next page cannot read it, and
|
|
72
|
+
// a board that holds an address is not asked to read the picture again, so the card would stay blank. The picture is the
|
|
73
|
+
// store's already, under the reference it was given for: only the reference is written. With no store, or with no reference to
|
|
74
|
+
// say where the picture is, there is nothing to point at, and the address is left as it always was.
|
|
75
|
+
if (store && existingRef && inline && isPageLocalAddress(inline))
|
|
76
|
+
return { value: null, ref: existingRef };
|
|
82
77
|
if (!store || !inline || !inline.startsWith('data:'))
|
|
83
78
|
return { value: inline, ref: existingRef };
|
|
84
|
-
|
|
79
|
+
const ref = existingRef ?? makeRef();
|
|
85
80
|
if (!ref)
|
|
86
81
|
return { value: inline, ref: existingRef };
|
|
87
|
-
|
|
88
|
-
|
|
82
|
+
try {
|
|
83
|
+
store.save(ref, inline);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
// A store that refuses (it has nowhere to keep the bytes yet) must not take the whole save down with it, and must not
|
|
87
|
+
// cost a picture: the bytes stay in the text, as if there were no store, and are offered again at the next save.
|
|
88
|
+
try {
|
|
89
|
+
options.onImageStoreRefused?.(ref, error);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// The report is a courtesy; a report that fails changes nothing about the save.
|
|
93
|
+
}
|
|
94
|
+
return { value: inline, ref: existingRef };
|
|
95
|
+
}
|
|
96
|
+
return { value: null, ref };
|
|
89
97
|
}
|
|
90
98
|
/** A tie of an arrow's end as it is saved: the shape it holds to (`target`) and, for a point of the outline, where on the shape's box. */
|
|
91
99
|
function writeArrowBinding(binding) {
|
|
@@ -94,21 +102,19 @@ function writeArrowBinding(binding) {
|
|
|
94
102
|
: { target: binding.targetId, kind: 'center' };
|
|
95
103
|
}
|
|
96
104
|
function serializeObject(object, options) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var overrides = {};
|
|
105
|
+
const record = { id: object.id, [HOST_READABLE_KEYS.objectType]: object.type, parentId: object.parentId };
|
|
106
|
+
const source = object;
|
|
107
|
+
let overrides = {};
|
|
101
108
|
if (object.type === 'widget') {
|
|
102
|
-
|
|
109
|
+
const thumbnail = externalizeBytes(object.thumbnail, object.thumbnailRef, () => `widget:${object.widgetId}`, options);
|
|
103
110
|
overrides = { thumbnail: thumbnail.value, thumbnailRef: thumbnail.ref };
|
|
104
111
|
}
|
|
105
112
|
else if (object.type === 'image') {
|
|
106
|
-
|
|
113
|
+
const image = externalizeBytes(object.src, object.imageRef, () => `shape:${object.id}`, options);
|
|
107
114
|
overrides = { src: image.value, imageRef: image.ref };
|
|
108
115
|
}
|
|
109
|
-
for (
|
|
110
|
-
|
|
111
|
-
var value = key in overrides ? overrides[key] : source[key];
|
|
116
|
+
for (const key of KEYS_BY_TYPE[object.type]) {
|
|
117
|
+
const value = key in overrides ? overrides[key] : source[key];
|
|
112
118
|
record[key] = typeof value === 'number' ? roundTo(value, 3) : value;
|
|
113
119
|
}
|
|
114
120
|
if (object.type === 'widget') {
|
|
@@ -129,8 +135,15 @@ function serializeObject(object, options) {
|
|
|
129
135
|
// 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.
|
|
130
136
|
if (object.arrow) {
|
|
131
137
|
delete record.d;
|
|
132
|
-
|
|
133
|
-
record.arrow =
|
|
138
|
+
const { start, end, bend, startBinding, endBinding } = object.arrow;
|
|
139
|
+
record.arrow = {
|
|
140
|
+
start: [roundTo(start.x, 3), roundTo(start.y, 3)],
|
|
141
|
+
end: [roundTo(end.x, 3), roundTo(end.y, 3)],
|
|
142
|
+
bend: roundTo(bend, 3),
|
|
143
|
+
// What its ends are tied to, written only for an end that is tied: a board of free arrows is the text it always was.
|
|
144
|
+
...(startBinding ? { startBinding: writeArrowBinding(startBinding) } : {}),
|
|
145
|
+
...(endBinding ? { endBinding: writeArrowBinding(endBinding) } : {})
|
|
146
|
+
};
|
|
134
147
|
}
|
|
135
148
|
else
|
|
136
149
|
delete record.arrow;
|
|
@@ -138,28 +151,23 @@ function serializeObject(object, options) {
|
|
|
138
151
|
return record;
|
|
139
152
|
}
|
|
140
153
|
/** The document for a board, as a plain object (the form `serializeCanvasDocument` writes). */
|
|
141
|
-
export function createCanvasDocument(scene, viewport, options) {
|
|
142
|
-
var _a;
|
|
143
|
-
if (options === void 0) { options = {}; }
|
|
154
|
+
export function createCanvasDocument(scene, viewport, options = {}) {
|
|
144
155
|
return {
|
|
145
156
|
version: CANVAS_DOCUMENT_VERSION,
|
|
146
|
-
savedAt: (
|
|
157
|
+
savedAt: (options.now ?? new Date()).toISOString(),
|
|
147
158
|
viewport: { x: roundTo(viewport.x, 2), y: roundTo(viewport.y, 2), zoom: roundTo(viewport.zoom, 4) },
|
|
148
|
-
canvas: { objects: scene.map(
|
|
159
|
+
canvas: { objects: scene.map((object) => serializeObject(object, options)) }
|
|
149
160
|
};
|
|
150
161
|
}
|
|
151
162
|
/** The text a board is saved as. */
|
|
152
|
-
export function serializeCanvasDocument(scene, viewport, options) {
|
|
153
|
-
if (options === void 0) { options = {}; }
|
|
163
|
+
export function serializeCanvasDocument(scene, viewport, options = {}) {
|
|
154
164
|
return JSON.stringify(createCanvasDocument(scene, viewport, options));
|
|
155
165
|
}
|
|
156
166
|
function isRecord(value) {
|
|
157
167
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
158
168
|
}
|
|
159
|
-
function readNumber(value, fallback, minimum, maximum) {
|
|
160
|
-
|
|
161
|
-
if (maximum === void 0) { maximum = Infinity; }
|
|
162
|
-
var number = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN;
|
|
169
|
+
function readNumber(value, fallback, minimum = -Infinity, maximum = Infinity) {
|
|
170
|
+
const number = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN;
|
|
163
171
|
if (!Number.isFinite(number))
|
|
164
172
|
return fallback;
|
|
165
173
|
return clampNumber(number, minimum, maximum);
|
|
@@ -167,36 +175,34 @@ function readNumber(value, fallback, minimum, maximum) {
|
|
|
167
175
|
function readBoolean(value, fallback) {
|
|
168
176
|
return typeof value === 'boolean' ? value : fallback;
|
|
169
177
|
}
|
|
170
|
-
function readString(value, fallback, maximumLength) {
|
|
171
|
-
if (maximumLength === void 0) { maximumLength = 1000; }
|
|
178
|
+
function readString(value, fallback, maximumLength = 1000) {
|
|
172
179
|
return typeof value === 'string' ? value.slice(0, maximumLength) : fallback;
|
|
173
180
|
}
|
|
174
|
-
function readNullableString(value, maximumLength) {
|
|
175
|
-
if (maximumLength === void 0) { maximumLength = 1000; }
|
|
181
|
+
function readNullableString(value, maximumLength = 1000) {
|
|
176
182
|
return typeof value === 'string' && value !== '' ? value.slice(0, maximumLength) : null;
|
|
177
183
|
}
|
|
178
184
|
function readOneOf(value, allowed, fallback) {
|
|
179
185
|
return typeof value === 'string' && allowed.includes(value) ? value : fallback;
|
|
180
186
|
}
|
|
181
187
|
/** A hex, `rgb()`, `hsl()` or named colour: nothing that could carry more than a colour into CSS or SVG. */
|
|
182
|
-
|
|
188
|
+
const COLOR_PATTERN = /^(#[0-9a-f]{3,8}|(?:rgb|hsl)a?\([0-9a-z\s.,%/+-]{1,80}\)|[a-z]{3,30})$/i;
|
|
183
189
|
export function readColor(value, fallback) {
|
|
184
190
|
return typeof value === 'string' && COLOR_PATTERN.test(value.trim()) ? value.trim() : fallback;
|
|
185
191
|
}
|
|
186
|
-
|
|
192
|
+
const FONT_FAMILY_PATTERN = /^[\p{L}\p{N}\s,'"\-_.]{1,200}$/u;
|
|
187
193
|
function readFontFamily(value) {
|
|
188
194
|
return typeof value === 'string' && FONT_FAMILY_PATTERN.test(value) ? value : TEXT_DEFAULTS.fontFamily;
|
|
189
195
|
}
|
|
190
|
-
|
|
196
|
+
const IMAGE_ADDRESS_PATTERN = /^(?:data:image\/[a-z0-9.+-]+[;,]|https?:\/\/|blob:|\/(?!\/))/i;
|
|
191
197
|
/** An image address the browser may load into an `<img>`: inline bytes, http(s), a blob or a path of this site. */
|
|
192
198
|
export function readImageAddress(value) {
|
|
193
199
|
if (typeof value !== 'string' || value === '')
|
|
194
200
|
return null;
|
|
195
201
|
return IMAGE_ADDRESS_PATTERN.test(value) ? value : null;
|
|
196
202
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
203
|
+
const STROKE_STYLES = ['solid', 'dashed', 'dotted'];
|
|
204
|
+
const LINE_CAPS = ['butt', 'round', 'square'];
|
|
205
|
+
const LINE_JOINS = ['miter', 'round', 'bevel'];
|
|
200
206
|
function readBox(raw) {
|
|
201
207
|
return {
|
|
202
208
|
x: readNumber(raw.x, 0, -MAX_COORDINATE, MAX_COORDINATE),
|
|
@@ -216,9 +222,9 @@ function readPaint(raw, defaults) {
|
|
|
216
222
|
strokeStyle: readOneOf(raw.strokeStyle, STROKE_STYLES, 'solid')
|
|
217
223
|
};
|
|
218
224
|
}
|
|
219
|
-
|
|
225
|
+
const PAINT_FALLBACK = { fill: '#ffffff', stroke: '#374151', strokeWidth: 2 };
|
|
220
226
|
function readId(raw, seen) {
|
|
221
|
-
|
|
227
|
+
let id = typeof raw.id === 'string' && raw.id !== '' && raw.id.length <= 128 ? raw.id : createObjectId();
|
|
222
228
|
// Two objects with the same id would be one on the board: the second gets a new one.
|
|
223
229
|
while (seen.has(id))
|
|
224
230
|
id = createObjectId();
|
|
@@ -229,19 +235,30 @@ function readParentId(raw) {
|
|
|
229
235
|
return typeof raw.parentId === 'string' && raw.parentId !== '' && raw.parentId.length <= 128 ? raw.parentId : null;
|
|
230
236
|
}
|
|
231
237
|
function readRect(raw, id) {
|
|
232
|
-
return createRectObject(
|
|
238
|
+
return createRectObject({
|
|
239
|
+
id,
|
|
240
|
+
parentId: readParentId(raw),
|
|
241
|
+
...readBox(raw),
|
|
242
|
+
...readPaint(raw, PAINT_FALLBACK),
|
|
243
|
+
roundness: readNumber(raw.roundness, 0, 0, 100)
|
|
244
|
+
});
|
|
233
245
|
}
|
|
234
246
|
function readEllipse(raw, id) {
|
|
235
|
-
return createEllipseObject(
|
|
247
|
+
return createEllipseObject({
|
|
248
|
+
id,
|
|
249
|
+
parentId: readParentId(raw),
|
|
250
|
+
...readBox(raw),
|
|
251
|
+
...readPaint(raw, PAINT_FALLBACK),
|
|
252
|
+
aspectLocked: readBoolean(raw.aspectLocked, false)
|
|
253
|
+
});
|
|
236
254
|
}
|
|
237
|
-
|
|
255
|
+
const BRUSH_PRESSURES = ['pen', 'speed'];
|
|
238
256
|
/** 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. */
|
|
239
257
|
function readInk(value) {
|
|
240
258
|
if (!isRecord(value) || !Array.isArray(value.points))
|
|
241
259
|
return null;
|
|
242
|
-
|
|
243
|
-
for (
|
|
244
|
-
var entry = _a[_i];
|
|
260
|
+
const points = [];
|
|
261
|
+
for (const entry of value.points.slice(0, BRUSH_MAX_POINTS)) {
|
|
245
262
|
if (!Array.isArray(entry) || typeof entry[0] !== 'number' || typeof entry[1] !== 'number')
|
|
246
263
|
continue;
|
|
247
264
|
if (!Number.isFinite(entry[0]) || !Number.isFinite(entry[1]))
|
|
@@ -254,24 +271,24 @@ function readInk(value) {
|
|
|
254
271
|
}
|
|
255
272
|
if (points.length === 0)
|
|
256
273
|
return null;
|
|
257
|
-
return { points
|
|
274
|
+
return { points, pressure: readOneOf(value.pressure, BRUSH_PRESSURES, 'speed') };
|
|
258
275
|
}
|
|
259
276
|
/** The two ends and the bend of an arrow, or null when the record has none that can be read. */
|
|
260
277
|
function readArrowSpec(value) {
|
|
261
278
|
if (!isRecord(value))
|
|
262
279
|
return null;
|
|
263
|
-
|
|
280
|
+
const readPoint = (entry) => {
|
|
264
281
|
if (!Array.isArray(entry) || typeof entry[0] !== 'number' || typeof entry[1] !== 'number')
|
|
265
282
|
return null;
|
|
266
283
|
if (!Number.isFinite(entry[0]) || !Number.isFinite(entry[1]))
|
|
267
284
|
return null;
|
|
268
285
|
return { x: clampNumber(entry[0], -MAX_COORDINATE, MAX_COORDINATE), y: clampNumber(entry[1], -MAX_COORDINATE, MAX_COORDINATE) };
|
|
269
286
|
};
|
|
270
|
-
|
|
271
|
-
|
|
287
|
+
const start = readPoint(value.start);
|
|
288
|
+
const end = readPoint(value.end);
|
|
272
289
|
if (!start || !end)
|
|
273
290
|
return null;
|
|
274
|
-
return withBindings({ start
|
|
291
|
+
return withBindings({ start, end, bend: readNumber(value.bend, 0, -2 * MAX_SIZE, 2 * MAX_SIZE) }, { startBinding: readArrowBinding(value.startBinding), endBinding: readArrowBinding(value.endBinding) });
|
|
275
292
|
}
|
|
276
293
|
/**
|
|
277
294
|
* What an end of an arrow is tied to, or null when the record says nothing that can be read: a shape (by its id), and for a point of
|
|
@@ -280,17 +297,17 @@ function readArrowSpec(value) {
|
|
|
280
297
|
function readArrowBinding(value) {
|
|
281
298
|
if (!isRecord(value))
|
|
282
299
|
return null;
|
|
283
|
-
|
|
300
|
+
const targetId = typeof value.target === 'string' && value.target !== '' && value.target.length <= 128 ? value.target : null;
|
|
284
301
|
if (!targetId)
|
|
285
302
|
return null;
|
|
286
303
|
if (value.kind === 'center')
|
|
287
|
-
return { targetId
|
|
304
|
+
return { targetId, kind: 'center' };
|
|
288
305
|
if (value.kind !== 'edge' || !Array.isArray(value.anchor))
|
|
289
306
|
return null;
|
|
290
|
-
|
|
307
|
+
const [across, down] = value.anchor;
|
|
291
308
|
if (typeof across !== 'number' || typeof down !== 'number' || !Number.isFinite(across) || !Number.isFinite(down))
|
|
292
309
|
return null;
|
|
293
|
-
return { targetId
|
|
310
|
+
return { targetId, kind: 'edge', anchor: { x: clampNumber(across, 0, 1), y: clampNumber(down, 0, 1) } };
|
|
294
311
|
}
|
|
295
312
|
/**
|
|
296
313
|
* 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
|
|
@@ -298,57 +315,56 @@ function readArrowBinding(value) {
|
|
|
298
315
|
* where it was in the world. An arrow is upright: a turn the record carries is applied to its points.
|
|
299
316
|
*/
|
|
300
317
|
function arrowFromSpec(spec, box, strokeWidth) {
|
|
301
|
-
|
|
302
|
-
var start = spec.start, end = spec.end;
|
|
318
|
+
let { start, end } = spec;
|
|
303
319
|
// What its ends are tied to is not a place on the board: it needs no turn.
|
|
304
|
-
|
|
320
|
+
const tiesOfSpec = { startBinding: spec.startBinding ?? null, endBinding: spec.endBinding ?? null };
|
|
305
321
|
if (box.rotation !== 0) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
x:
|
|
313
|
-
y:
|
|
314
|
-
});
|
|
322
|
+
const radians = degreesToRadians(box.rotation);
|
|
323
|
+
const cosine = Math.cos(radians);
|
|
324
|
+
const sine = Math.sin(radians);
|
|
325
|
+
const centerX = box.width / 2;
|
|
326
|
+
const centerY = box.height / 2;
|
|
327
|
+
const turn = (point) => ({
|
|
328
|
+
x: centerX + (point.x - centerX) * cosine - (point.y - centerY) * sine,
|
|
329
|
+
y: centerY + (point.x - centerX) * sine + (point.y - centerY) * cosine
|
|
330
|
+
});
|
|
315
331
|
start = turn(start);
|
|
316
332
|
end = turn(end);
|
|
317
333
|
}
|
|
318
|
-
|
|
319
|
-
|
|
334
|
+
const geometry = buildArrow(start, end, spec.bend, strokeWidth);
|
|
335
|
+
const holdsWhatIsDrawn = box.rotation === 0 &&
|
|
320
336
|
Math.abs(geometry.box.x) <= 0.5 &&
|
|
321
337
|
Math.abs(geometry.box.y) <= 0.5 &&
|
|
322
338
|
Math.abs(geometry.box.width - box.width) <= 0.5 &&
|
|
323
339
|
Math.abs(geometry.box.height - box.height) <= 0.5;
|
|
324
340
|
if (holdsWhatIsDrawn) {
|
|
325
341
|
// 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.
|
|
326
|
-
|
|
327
|
-
return { d: serializePathData(commands), arrow: withBindings({ start
|
|
342
|
+
const commands = translatePathCommands(geometry.commands, geometry.box.x, geometry.box.y);
|
|
343
|
+
return { d: serializePathData(commands), arrow: withBindings({ start, end, bend: geometry.spec.bend }, tiesOfSpec), box };
|
|
328
344
|
}
|
|
329
345
|
return {
|
|
330
346
|
d: serializePathData(geometry.commands),
|
|
331
347
|
arrow: withBindings(geometry.spec, tiesOfSpec),
|
|
332
|
-
box:
|
|
348
|
+
box: { ...box, rotation: 0, x: box.x + geometry.box.x, y: box.y + geometry.box.y, width: geometry.box.width, height: geometry.box.height }
|
|
333
349
|
};
|
|
334
350
|
}
|
|
335
351
|
function readPath(raw, id, index, warnings) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
352
|
+
const paint = readPaint(raw, { fill: 'transparent', stroke: '#1f2937', strokeWidth: 3 });
|
|
353
|
+
const ink = readInk(raw.ink);
|
|
354
|
+
const shape = readNullableString(raw.shape, 100);
|
|
355
|
+
let box = readBox(raw);
|
|
340
356
|
// 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
|
|
341
357
|
// has the outline only, and its arrow is found in that.
|
|
342
|
-
|
|
358
|
+
let arrowSpec = ink ? null : readArrowSpec(raw.arrow);
|
|
343
359
|
if (!ink && !arrowSpec && shape === 'arrow' && typeof raw.d === 'string' && raw.d.length <= MAX_PATH_DATA_LENGTH) {
|
|
344
|
-
|
|
360
|
+
const legacy = arrowPointsOfLegacyPath(parsePathData(raw.d));
|
|
345
361
|
if (legacy)
|
|
346
|
-
arrowSpec =
|
|
362
|
+
arrowSpec = { ...legacy, bend: 0 };
|
|
347
363
|
}
|
|
348
|
-
|
|
349
|
-
|
|
364
|
+
let arrow = null;
|
|
365
|
+
let d;
|
|
350
366
|
if (arrowSpec) {
|
|
351
|
-
|
|
367
|
+
const drawn = arrowFromSpec(arrowSpec, box, paint.strokeWidth);
|
|
352
368
|
d = drawn.d;
|
|
353
369
|
arrow = drawn.arrow;
|
|
354
370
|
box = drawn.box;
|
|
@@ -358,45 +374,93 @@ function readPath(raw, id, index, warnings) {
|
|
|
358
374
|
d = ink ? brushOutlineData(ink, paint.strokeWidth) : typeof raw.d === 'string' ? raw.d : '';
|
|
359
375
|
}
|
|
360
376
|
if (d.length === 0 || d.length > MAX_PATH_DATA_LENGTH || parsePathData(d).length < 2) {
|
|
361
|
-
warnings.push({ code: 'bad-path', index
|
|
377
|
+
warnings.push({ code: 'bad-path', index, detail: 'The outline of the shape is missing or cannot be read.' });
|
|
362
378
|
return null;
|
|
363
379
|
}
|
|
364
|
-
return createPathObject(
|
|
380
|
+
return createPathObject({
|
|
381
|
+
id,
|
|
382
|
+
parentId: readParentId(raw),
|
|
383
|
+
...box,
|
|
384
|
+
...paint,
|
|
385
|
+
d,
|
|
386
|
+
shape,
|
|
387
|
+
roundness: readNumber(raw.roundness, 0, 0, 100),
|
|
388
|
+
aspectLocked: readBoolean(raw.aspectLocked, false),
|
|
389
|
+
flipX: readBoolean(raw.flipX, false),
|
|
390
|
+
flipY: readBoolean(raw.flipY, false),
|
|
391
|
+
lineCap: readOneOf(raw.lineCap, LINE_CAPS, 'round'),
|
|
392
|
+
lineJoin: readOneOf(raw.lineJoin, LINE_JOINS, 'round'),
|
|
393
|
+
ink,
|
|
394
|
+
arrow
|
|
395
|
+
});
|
|
365
396
|
}
|
|
366
397
|
function readText(raw, id) {
|
|
367
|
-
return createTextObject(
|
|
398
|
+
return createTextObject({
|
|
399
|
+
id,
|
|
400
|
+
parentId: readParentId(raw),
|
|
401
|
+
...readBox(raw),
|
|
402
|
+
text: readString(raw.text, '', MAX_TEXT_LENGTH),
|
|
403
|
+
color: readColor(raw.color, TEXT_DEFAULTS.color),
|
|
404
|
+
fontSize: readNumber(raw.fontSize, TEXT_DEFAULTS.fontSize, 4, 600),
|
|
405
|
+
fontFamily: readFontFamily(raw.fontFamily),
|
|
406
|
+
fontWeight: readOneOf(raw.fontWeight, ['normal', 'bold'], 'normal'),
|
|
407
|
+
fontStyle: readOneOf(raw.fontStyle, ['normal', 'italic'], 'normal'),
|
|
408
|
+
underline: readBoolean(raw.underline, false),
|
|
409
|
+
align: readOneOf(raw.align, ['left', 'center', 'right'], 'left'),
|
|
410
|
+
lineHeight: readNumber(raw.lineHeight, 1.2, 0.5, 4),
|
|
411
|
+
padding: readNumber(raw.padding, 0, 0, 200),
|
|
412
|
+
backgroundColor: readColor(raw.backgroundColor, 'transparent'),
|
|
413
|
+
autoWidth: readBoolean(raw.autoWidth, true)
|
|
414
|
+
});
|
|
368
415
|
}
|
|
369
416
|
function readImage(raw, id) {
|
|
370
|
-
return createImageObject(
|
|
417
|
+
return createImageObject({
|
|
418
|
+
id,
|
|
419
|
+
parentId: readParentId(raw),
|
|
420
|
+
...readBox(raw),
|
|
421
|
+
src: readImageAddress(raw.src),
|
|
422
|
+
imageRef: readNullableString(raw.imageRef, 200),
|
|
423
|
+
fileName: readNullableString(raw.fileName, 300),
|
|
424
|
+
naturalWidth: readNumber(raw.naturalWidth, 0, 0, MAX_SIZE),
|
|
425
|
+
naturalHeight: readNumber(raw.naturalHeight, 0, 0, MAX_SIZE)
|
|
426
|
+
});
|
|
371
427
|
}
|
|
372
428
|
function readWidget(raw, id, index, warnings) {
|
|
373
|
-
var _a;
|
|
374
429
|
// The creator reads `_widgetId`; the canvas's own key is `widgetId`. Either names the widget.
|
|
375
|
-
|
|
430
|
+
const widgetId = readNullableString(raw.widgetId, 200) ?? readNullableString(raw[HOST_READABLE_KEYS.widgetId], 200);
|
|
376
431
|
if (!widgetId) {
|
|
377
|
-
warnings.push({ code: 'bad-object', index
|
|
432
|
+
warnings.push({ code: 'bad-object', index, detail: 'A widget card has no widget id.' });
|
|
378
433
|
return null;
|
|
379
434
|
}
|
|
380
|
-
return createWidgetObject(
|
|
435
|
+
return createWidgetObject({
|
|
436
|
+
id,
|
|
437
|
+
widgetId,
|
|
438
|
+
parentId: readParentId(raw),
|
|
439
|
+
...readBox(raw),
|
|
440
|
+
widgetName: readNullableString(raw.widgetName, 300),
|
|
441
|
+
isPrimary: readBoolean(raw.isPrimary, false),
|
|
442
|
+
thumbnail: readImageAddress(raw.thumbnail),
|
|
443
|
+
thumbnailRef: readNullableString(raw.thumbnailRef, 200)
|
|
444
|
+
});
|
|
381
445
|
}
|
|
382
446
|
function readGroup(raw, id) {
|
|
383
|
-
return createGroupObject({ id
|
|
447
|
+
return createGroupObject({ id, parentId: readParentId(raw) });
|
|
384
448
|
}
|
|
385
449
|
function readObject(raw, index, seen, warnings) {
|
|
386
450
|
if (!isRecord(raw)) {
|
|
387
|
-
warnings.push({ code: 'bad-object', index
|
|
451
|
+
warnings.push({ code: 'bad-object', index, detail: 'An entry of the list is not an object.' });
|
|
388
452
|
return null;
|
|
389
453
|
}
|
|
390
454
|
// Readers outside the canvas treat these as "not there": so does the canvas.
|
|
391
455
|
if (raw[HOST_READABLE_KEYS.visible] === false || raw[HOST_READABLE_KEYS.excludeFromExport] === true)
|
|
392
456
|
return null;
|
|
393
|
-
|
|
394
|
-
|
|
457
|
+
const type = raw[HOST_READABLE_KEYS.objectType];
|
|
458
|
+
const known = ['rect', 'ellipse', 'path', 'text', 'image', 'widget', 'group'];
|
|
395
459
|
if (typeof type !== 'string' || !known.includes(type)) {
|
|
396
|
-
warnings.push({ code: 'unknown-type', index
|
|
460
|
+
warnings.push({ code: 'unknown-type', index, detail: typeof type === 'string' ? type : String(type) });
|
|
397
461
|
return null;
|
|
398
462
|
}
|
|
399
|
-
|
|
463
|
+
const id = readId(raw, seen);
|
|
400
464
|
switch (type) {
|
|
401
465
|
case 'rect':
|
|
402
466
|
return readRect(raw, id);
|
|
@@ -414,28 +478,27 @@ function readObject(raw, index, seen, warnings) {
|
|
|
414
478
|
return readGroup(raw, id);
|
|
415
479
|
}
|
|
416
480
|
}
|
|
417
|
-
|
|
481
|
+
const EMPTY_VIEWPORT = { x: 0, y: 0, zoom: 1 };
|
|
418
482
|
/**
|
|
419
483
|
* Reads a saved canvas: the text `serializeCanvasDocument` wrote, or the same thing already parsed.
|
|
420
484
|
* Never throws.
|
|
421
485
|
*/
|
|
422
486
|
export function parseCanvasDocument(input) {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
var nothing = function (status) { return ({
|
|
487
|
+
const warnings = [];
|
|
488
|
+
const nothing = (status) => ({
|
|
426
489
|
scene: [],
|
|
427
|
-
viewport:
|
|
428
|
-
status
|
|
429
|
-
warnings
|
|
430
|
-
});
|
|
490
|
+
viewport: { ...EMPTY_VIEWPORT },
|
|
491
|
+
status,
|
|
492
|
+
warnings
|
|
493
|
+
});
|
|
431
494
|
if (input === null || input === undefined || (typeof input === 'string' && input.trim() === ''))
|
|
432
495
|
return nothing('empty');
|
|
433
|
-
|
|
496
|
+
let document = input;
|
|
434
497
|
if (typeof input === 'string') {
|
|
435
498
|
try {
|
|
436
499
|
document = JSON.parse(input);
|
|
437
500
|
}
|
|
438
|
-
catch
|
|
501
|
+
catch {
|
|
439
502
|
warnings.push({ code: 'not-json' });
|
|
440
503
|
return nothing('invalid');
|
|
441
504
|
}
|
|
@@ -445,19 +508,19 @@ export function parseCanvasDocument(input) {
|
|
|
445
508
|
return nothing('invalid');
|
|
446
509
|
}
|
|
447
510
|
if (document.version !== CANVAS_DOCUMENT_VERSION) {
|
|
448
|
-
warnings.push({ code: 'unsupported-version', detail: String(
|
|
511
|
+
warnings.push({ code: 'unsupported-version', detail: String(document.version ?? 'none') });
|
|
449
512
|
return nothing('unsupported');
|
|
450
513
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
514
|
+
const canvas = document.canvas;
|
|
515
|
+
const list = isRecord(canvas) && Array.isArray(canvas.objects) ? canvas.objects : [];
|
|
516
|
+
const limited = list.length > MAX_OBJECTS_PER_DOCUMENT ? list.slice(0, MAX_OBJECTS_PER_DOCUMENT) : list;
|
|
454
517
|
if (limited.length < list.length) {
|
|
455
|
-
warnings.push({ code: 'too-many-objects', detail:
|
|
518
|
+
warnings.push({ code: 'too-many-objects', detail: `${list.length} objects, the first ${limited.length} were read.` });
|
|
456
519
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
limited.forEach(
|
|
460
|
-
|
|
520
|
+
const seen = new Set();
|
|
521
|
+
const objects = [];
|
|
522
|
+
limited.forEach((raw, index) => {
|
|
523
|
+
const object = readObject(raw, index, seen, warnings);
|
|
461
524
|
if (object)
|
|
462
525
|
objects.push(object);
|
|
463
526
|
});
|
|
@@ -466,6 +529,6 @@ export function parseCanvasDocument(input) {
|
|
|
466
529
|
scene: dropDanglingBindings(normalizeScene(objects)),
|
|
467
530
|
viewport: sanitizeViewport(document.viewport),
|
|
468
531
|
status: 'ok',
|
|
469
|
-
warnings
|
|
532
|
+
warnings
|
|
470
533
|
};
|
|
471
534
|
}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* accepts them, and `tests/engine/codec.test.mjs` fails if either changes. Every other key of the format
|
|
10
10
|
* is the canvas's own and can change with a version.
|
|
11
11
|
*/
|
|
12
|
-
export
|
|
12
|
+
export const HOST_READABLE_KEYS = Object.freeze({
|
|
13
13
|
/** `document.canvas.objects`: always an array. Readers fall back to `document.objects` for a bare list. */
|
|
14
14
|
documentObjects: Object.freeze(['canvas', 'objects']),
|
|
15
15
|
/** The kind of an object: a string. */
|