@widgetic/canvas 0.5.43 → 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 +56 -3
- package/dist/canvas/DomCanvas.svelte.d.ts +59 -28
- 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 +87 -2
- package/dist/canvas/host/CanvasHost.js +1948 -1394
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +32 -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 +2 -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/types.d.ts +17 -0
- 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/ColorField.svelte +7 -2
- package/dist/canvas/ui/SelectionPanel.svelte +105 -42
- 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 +35 -54
- 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 +111 -2
- package/dist/engine/EngineCore.js +1013 -707
- 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 +73 -0
- package/dist/engine/core/cameraMove.js +172 -0
- 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 +549 -466
- 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 +2 -1
- 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
|
@@ -12,6 +12,26 @@
|
|
|
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
|
+
};
|
|
15
35
|
import { arrowPointsOfLegacyPath, buildArrow } from '../core/arrow.js';
|
|
16
36
|
import { dropDanglingBindings, withBindings } from '../core/arrowBindingRecords.js';
|
|
17
37
|
import { BRUSH_MAX_POINTS, brushOutlineData } from '../core/brush.js';
|
|
@@ -20,22 +40,22 @@ import { clampNumber, roundTo } from '../core/geometry.js';
|
|
|
20
40
|
import { degreesToRadians, normalizeDegrees } from '../core/matrix.js';
|
|
21
41
|
import { parsePathData, serializePathData, translatePathCommands } from '../core/pathData.js';
|
|
22
42
|
import { normalizeScene } from '../core/scene.js';
|
|
43
|
+
import { minimumStrokeWidthOf } from '../core/style.js';
|
|
23
44
|
import { CANVAS_DOCUMENT_VERSION, MIN_OBJECT_SIZE } from '../core/types.js';
|
|
24
45
|
import { sanitizeViewport } from '../core/viewport.js';
|
|
25
46
|
import { HOST_READABLE_KEYS } from './hostReadableKeys.js';
|
|
26
47
|
/** The most objects a board may hold when it is read; a text with more is cut here and says so. */
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
48
|
+
export var MAX_OBJECTS_PER_DOCUMENT = 20000;
|
|
49
|
+
var MAX_TEXT_LENGTH = 100000;
|
|
50
|
+
var MAX_PATH_DATA_LENGTH = 500000;
|
|
51
|
+
var MAX_COORDINATE = 1000000;
|
|
52
|
+
var MAX_SIZE = 100000;
|
|
32
53
|
// --- Writing -----------------------------------------------------------------------------------
|
|
33
|
-
|
|
34
|
-
|
|
54
|
+
var BOX_KEYS = ['x', 'y', 'width', 'height', 'rotation', 'opacity'];
|
|
55
|
+
var PAINT_KEYS = ['fill', 'fillOpacity', 'stroke', 'strokeWidth', 'strokeStyle'];
|
|
35
56
|
// The words audit reads every string inside a property called `text` as a sentence a person can read; these are the
|
|
36
57
|
// names of fields, so the list of a text object's keys lives in a constant of its own.
|
|
37
|
-
|
|
38
|
-
...BOX_KEYS,
|
|
58
|
+
var TEXT_OBJECT_KEYS = __spreadArray(__spreadArray([], BOX_KEYS, true), [
|
|
39
59
|
'text',
|
|
40
60
|
'color',
|
|
41
61
|
'fontSize',
|
|
@@ -48,14 +68,14 @@ const TEXT_OBJECT_KEYS = [
|
|
|
48
68
|
'padding',
|
|
49
69
|
'backgroundColor',
|
|
50
70
|
'autoWidth'
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
rect: [
|
|
54
|
-
ellipse: [
|
|
55
|
-
path: [
|
|
71
|
+
], false);
|
|
72
|
+
var KEYS_BY_TYPE = {
|
|
73
|
+
rect: __spreadArray(__spreadArray(__spreadArray([], BOX_KEYS, true), PAINT_KEYS, true), ['roundness'], false),
|
|
74
|
+
ellipse: __spreadArray(__spreadArray(__spreadArray([], BOX_KEYS, true), PAINT_KEYS, true), ['aspectLocked'], false),
|
|
75
|
+
path: __spreadArray(__spreadArray(__spreadArray([], BOX_KEYS, true), PAINT_KEYS, true), ['d', 'shape', 'roundness', 'aspectLocked', 'flipX', 'flipY', 'lineCap', 'lineJoin', 'ink', 'arrow'], false),
|
|
56
76
|
text: TEXT_OBJECT_KEYS,
|
|
57
|
-
image: [
|
|
58
|
-
widget: [
|
|
77
|
+
image: __spreadArray(__spreadArray([], BOX_KEYS, true), ['src', 'imageRef', 'fileName', 'libraryFileId', 'libraryFileUrl', 'naturalWidth', 'naturalHeight'], false),
|
|
78
|
+
widget: __spreadArray(__spreadArray([], BOX_KEYS, true), ['widgetId', 'widgetName', 'isPrimary', 'thumbnail', 'thumbnailRef'], false),
|
|
59
79
|
group: []
|
|
60
80
|
};
|
|
61
81
|
/**
|
|
@@ -67,7 +87,8 @@ function isPageLocalAddress(address) {
|
|
|
67
87
|
}
|
|
68
88
|
/** The reference and the (possibly inline) bytes of a picture, with the bytes moved to the store when there is one. */
|
|
69
89
|
function externalizeBytes(inline, existingRef, makeRef, options) {
|
|
70
|
-
|
|
90
|
+
var _a;
|
|
91
|
+
var store = options.imageStore;
|
|
71
92
|
// 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
93
|
// 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
94
|
// store's already, under the reference it was given for: only the reference is written. With no store, or with no reference to
|
|
@@ -76,7 +97,7 @@ function externalizeBytes(inline, existingRef, makeRef, options) {
|
|
|
76
97
|
return { value: null, ref: existingRef };
|
|
77
98
|
if (!store || !inline || !inline.startsWith('data:'))
|
|
78
99
|
return { value: inline, ref: existingRef };
|
|
79
|
-
|
|
100
|
+
var ref = existingRef !== null && existingRef !== void 0 ? existingRef : makeRef();
|
|
80
101
|
if (!ref)
|
|
81
102
|
return { value: inline, ref: existingRef };
|
|
82
103
|
try {
|
|
@@ -86,14 +107,14 @@ function externalizeBytes(inline, existingRef, makeRef, options) {
|
|
|
86
107
|
// 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
108
|
// cost a picture: the bytes stay in the text, as if there were no store, and are offered again at the next save.
|
|
88
109
|
try {
|
|
89
|
-
options.onImageStoreRefused
|
|
110
|
+
(_a = options.onImageStoreRefused) === null || _a === void 0 ? void 0 : _a.call(options, ref, error);
|
|
90
111
|
}
|
|
91
|
-
catch {
|
|
112
|
+
catch (_b) {
|
|
92
113
|
// The report is a courtesy; a report that fails changes nothing about the save.
|
|
93
114
|
}
|
|
94
115
|
return { value: inline, ref: existingRef };
|
|
95
116
|
}
|
|
96
|
-
return { value: null, ref };
|
|
117
|
+
return { value: null, ref: ref };
|
|
97
118
|
}
|
|
98
119
|
/** 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. */
|
|
99
120
|
function writeArrowBinding(binding) {
|
|
@@ -102,19 +123,21 @@ function writeArrowBinding(binding) {
|
|
|
102
123
|
: { target: binding.targetId, kind: 'center' };
|
|
103
124
|
}
|
|
104
125
|
function serializeObject(object, options) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
126
|
+
var _a;
|
|
127
|
+
var record = (_a = { id: object.id }, _a[HOST_READABLE_KEYS.objectType] = object.type, _a.parentId = object.parentId, _a);
|
|
128
|
+
var source = object;
|
|
129
|
+
var overrides = {};
|
|
108
130
|
if (object.type === 'widget') {
|
|
109
|
-
|
|
131
|
+
var thumbnail = externalizeBytes(object.thumbnail, object.thumbnailRef, function () { return "widget:".concat(object.widgetId); }, options);
|
|
110
132
|
overrides = { thumbnail: thumbnail.value, thumbnailRef: thumbnail.ref };
|
|
111
133
|
}
|
|
112
134
|
else if (object.type === 'image') {
|
|
113
|
-
|
|
135
|
+
var image = externalizeBytes(object.src, object.imageRef, function () { return "shape:".concat(object.id); }, options);
|
|
114
136
|
overrides = { src: image.value, imageRef: image.ref };
|
|
115
137
|
}
|
|
116
|
-
for (
|
|
117
|
-
|
|
138
|
+
for (var _i = 0, _b = KEYS_BY_TYPE[object.type]; _i < _b.length; _i++) {
|
|
139
|
+
var key = _b[_i];
|
|
140
|
+
var value = key in overrides ? overrides[key] : source[key];
|
|
118
141
|
record[key] = typeof value === 'number' ? roundTo(value, 3) : value;
|
|
119
142
|
}
|
|
120
143
|
if (object.type === 'widget') {
|
|
@@ -135,15 +158,8 @@ function serializeObject(object, options) {
|
|
|
135
158
|
// 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.
|
|
136
159
|
if (object.arrow) {
|
|
137
160
|
delete record.d;
|
|
138
|
-
|
|
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
|
-
};
|
|
161
|
+
var _c = object.arrow, start = _c.start, end = _c.end, bend = _c.bend, startBinding = _c.startBinding, endBinding = _c.endBinding;
|
|
162
|
+
record.arrow = __assign(__assign({ start: [roundTo(start.x, 3), roundTo(start.y, 3)], end: [roundTo(end.x, 3), roundTo(end.y, 3)], bend: roundTo(bend, 3) }, (startBinding ? { startBinding: writeArrowBinding(startBinding) } : {})), (endBinding ? { endBinding: writeArrowBinding(endBinding) } : {}));
|
|
147
163
|
}
|
|
148
164
|
else
|
|
149
165
|
delete record.arrow;
|
|
@@ -151,23 +167,28 @@ function serializeObject(object, options) {
|
|
|
151
167
|
return record;
|
|
152
168
|
}
|
|
153
169
|
/** The document for a board, as a plain object (the form `serializeCanvasDocument` writes). */
|
|
154
|
-
export function createCanvasDocument(scene, viewport, options
|
|
170
|
+
export function createCanvasDocument(scene, viewport, options) {
|
|
171
|
+
var _a;
|
|
172
|
+
if (options === void 0) { options = {}; }
|
|
155
173
|
return {
|
|
156
174
|
version: CANVAS_DOCUMENT_VERSION,
|
|
157
|
-
savedAt: (options.now
|
|
175
|
+
savedAt: ((_a = options.now) !== null && _a !== void 0 ? _a : new Date()).toISOString(),
|
|
158
176
|
viewport: { x: roundTo(viewport.x, 2), y: roundTo(viewport.y, 2), zoom: roundTo(viewport.zoom, 4) },
|
|
159
|
-
canvas: { objects: scene.map((object)
|
|
177
|
+
canvas: { objects: scene.map(function (object) { return serializeObject(object, options); }) }
|
|
160
178
|
};
|
|
161
179
|
}
|
|
162
180
|
/** The text a board is saved as. */
|
|
163
|
-
export function serializeCanvasDocument(scene, viewport, options
|
|
181
|
+
export function serializeCanvasDocument(scene, viewport, options) {
|
|
182
|
+
if (options === void 0) { options = {}; }
|
|
164
183
|
return JSON.stringify(createCanvasDocument(scene, viewport, options));
|
|
165
184
|
}
|
|
166
185
|
function isRecord(value) {
|
|
167
186
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
168
187
|
}
|
|
169
|
-
function readNumber(value, fallback, minimum
|
|
170
|
-
|
|
188
|
+
function readNumber(value, fallback, minimum, maximum) {
|
|
189
|
+
if (minimum === void 0) { minimum = -Infinity; }
|
|
190
|
+
if (maximum === void 0) { maximum = Infinity; }
|
|
191
|
+
var number = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN;
|
|
171
192
|
if (!Number.isFinite(number))
|
|
172
193
|
return fallback;
|
|
173
194
|
return clampNumber(number, minimum, maximum);
|
|
@@ -175,34 +196,51 @@ function readNumber(value, fallback, minimum = -Infinity, maximum = Infinity) {
|
|
|
175
196
|
function readBoolean(value, fallback) {
|
|
176
197
|
return typeof value === 'boolean' ? value : fallback;
|
|
177
198
|
}
|
|
178
|
-
function readString(value, fallback, maximumLength
|
|
199
|
+
function readString(value, fallback, maximumLength) {
|
|
200
|
+
if (maximumLength === void 0) { maximumLength = 1000; }
|
|
179
201
|
return typeof value === 'string' ? value.slice(0, maximumLength) : fallback;
|
|
180
202
|
}
|
|
181
|
-
function readNullableString(value, maximumLength
|
|
203
|
+
function readNullableString(value, maximumLength) {
|
|
204
|
+
if (maximumLength === void 0) { maximumLength = 1000; }
|
|
182
205
|
return typeof value === 'string' && value !== '' ? value.slice(0, maximumLength) : null;
|
|
183
206
|
}
|
|
184
207
|
function readOneOf(value, allowed, fallback) {
|
|
185
208
|
return typeof value === 'string' && allowed.includes(value) ? value : fallback;
|
|
186
209
|
}
|
|
187
210
|
/** A hex, `rgb()`, `hsl()` or named colour: nothing that could carry more than a colour into CSS or SVG. */
|
|
188
|
-
|
|
211
|
+
var COLOR_PATTERN = /^(#[0-9a-f]{3,8}|(?:rgb|hsl)a?\([0-9a-z\s.,%/+-]{1,80}\)|[a-z]{3,30})$/i;
|
|
189
212
|
export function readColor(value, fallback) {
|
|
190
213
|
return typeof value === 'string' && COLOR_PATTERN.test(value.trim()) ? value.trim() : fallback;
|
|
191
214
|
}
|
|
192
|
-
|
|
215
|
+
var FONT_FAMILY_PATTERN = /^[\p{L}\p{N}\s,'"\-_.]{1,200}$/u;
|
|
216
|
+
/** Whether a text is a well-formed font family name (what a saved board accepts: letters, digits, spaces and a few marks). */
|
|
217
|
+
export function isFontFamilyName(value) {
|
|
218
|
+
return typeof value === 'string' && FONT_FAMILY_PATTERN.test(value);
|
|
219
|
+
}
|
|
193
220
|
function readFontFamily(value) {
|
|
194
221
|
return typeof value === 'string' && FONT_FAMILY_PATTERN.test(value) ? value : TEXT_DEFAULTS.fontFamily;
|
|
195
222
|
}
|
|
196
|
-
|
|
223
|
+
var IMAGE_ADDRESS_PATTERN = /^(?:data:image\/[a-z0-9.+-]+[;,]|https?:\/\/|blob:|\/(?!\/))/i;
|
|
197
224
|
/** An image address the browser may load into an `<img>`: inline bytes, http(s), a blob or a path of this site. */
|
|
198
225
|
export function readImageAddress(value) {
|
|
199
226
|
if (typeof value !== 'string' || value === '')
|
|
200
227
|
return null;
|
|
201
228
|
return IMAGE_ADDRESS_PATTERN.test(value) ? value : null;
|
|
202
229
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
230
|
+
var LIBRARY_ADDRESS_PATTERN = /^(?:https?:\/\/|\/(?!\/))/i;
|
|
231
|
+
var LIBRARY_ADDRESS_MAX_LENGTH = 2000;
|
|
232
|
+
/**
|
|
233
|
+
* Where the person's file library keeps a picture: an http(s) address or a path of the page. Never inline bytes (the library
|
|
234
|
+
* address only names the file; the board's own bytes live in `src`/`imageRef`), and never anything a link could abuse.
|
|
235
|
+
*/
|
|
236
|
+
export function readLibraryFileAddress(value) {
|
|
237
|
+
if (typeof value !== 'string' || value === '' || value.length > LIBRARY_ADDRESS_MAX_LENGTH)
|
|
238
|
+
return null;
|
|
239
|
+
return LIBRARY_ADDRESS_PATTERN.test(value) ? value : null;
|
|
240
|
+
}
|
|
241
|
+
var STROKE_STYLES = ['solid', 'dashed', 'dotted'];
|
|
242
|
+
var LINE_CAPS = ['butt', 'round', 'square'];
|
|
243
|
+
var LINE_JOINS = ['miter', 'round', 'bevel'];
|
|
206
244
|
function readBox(raw) {
|
|
207
245
|
return {
|
|
208
246
|
x: readNumber(raw.x, 0, -MAX_COORDINATE, MAX_COORDINATE),
|
|
@@ -222,9 +260,9 @@ function readPaint(raw, defaults) {
|
|
|
222
260
|
strokeStyle: readOneOf(raw.strokeStyle, STROKE_STYLES, 'solid')
|
|
223
261
|
};
|
|
224
262
|
}
|
|
225
|
-
|
|
263
|
+
var PAINT_FALLBACK = { fill: '#ffffff', stroke: '#374151', strokeWidth: 2 };
|
|
226
264
|
function readId(raw, seen) {
|
|
227
|
-
|
|
265
|
+
var id = typeof raw.id === 'string' && raw.id !== '' && raw.id.length <= 128 ? raw.id : createObjectId();
|
|
228
266
|
// Two objects with the same id would be one on the board: the second gets a new one.
|
|
229
267
|
while (seen.has(id))
|
|
230
268
|
id = createObjectId();
|
|
@@ -235,30 +273,19 @@ function readParentId(raw) {
|
|
|
235
273
|
return typeof raw.parentId === 'string' && raw.parentId !== '' && raw.parentId.length <= 128 ? raw.parentId : null;
|
|
236
274
|
}
|
|
237
275
|
function readRect(raw, id) {
|
|
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
|
-
});
|
|
276
|
+
return createRectObject(__assign(__assign(__assign({ id: id, parentId: readParentId(raw) }, readBox(raw)), readPaint(raw, PAINT_FALLBACK)), { roundness: readNumber(raw.roundness, 0, 0, 100) }));
|
|
245
277
|
}
|
|
246
278
|
function readEllipse(raw, id) {
|
|
247
|
-
return createEllipseObject({
|
|
248
|
-
id,
|
|
249
|
-
parentId: readParentId(raw),
|
|
250
|
-
...readBox(raw),
|
|
251
|
-
...readPaint(raw, PAINT_FALLBACK),
|
|
252
|
-
aspectLocked: readBoolean(raw.aspectLocked, false)
|
|
253
|
-
});
|
|
279
|
+
return createEllipseObject(__assign(__assign(__assign({ id: id, parentId: readParentId(raw) }, readBox(raw)), readPaint(raw, PAINT_FALLBACK)), { aspectLocked: readBoolean(raw.aspectLocked, false) }));
|
|
254
280
|
}
|
|
255
|
-
|
|
281
|
+
var BRUSH_PRESSURES = ['pen', 'speed'];
|
|
256
282
|
/** 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. */
|
|
257
283
|
function readInk(value) {
|
|
258
284
|
if (!isRecord(value) || !Array.isArray(value.points))
|
|
259
285
|
return null;
|
|
260
|
-
|
|
261
|
-
for (
|
|
286
|
+
var points = [];
|
|
287
|
+
for (var _i = 0, _a = value.points.slice(0, BRUSH_MAX_POINTS); _i < _a.length; _i++) {
|
|
288
|
+
var entry = _a[_i];
|
|
262
289
|
if (!Array.isArray(entry) || typeof entry[0] !== 'number' || typeof entry[1] !== 'number')
|
|
263
290
|
continue;
|
|
264
291
|
if (!Number.isFinite(entry[0]) || !Number.isFinite(entry[1]))
|
|
@@ -271,24 +298,24 @@ function readInk(value) {
|
|
|
271
298
|
}
|
|
272
299
|
if (points.length === 0)
|
|
273
300
|
return null;
|
|
274
|
-
return { points, pressure: readOneOf(value.pressure, BRUSH_PRESSURES, 'speed') };
|
|
301
|
+
return { points: points, pressure: readOneOf(value.pressure, BRUSH_PRESSURES, 'speed') };
|
|
275
302
|
}
|
|
276
303
|
/** The two ends and the bend of an arrow, or null when the record has none that can be read. */
|
|
277
304
|
function readArrowSpec(value) {
|
|
278
305
|
if (!isRecord(value))
|
|
279
306
|
return null;
|
|
280
|
-
|
|
307
|
+
var readPoint = function (entry) {
|
|
281
308
|
if (!Array.isArray(entry) || typeof entry[0] !== 'number' || typeof entry[1] !== 'number')
|
|
282
309
|
return null;
|
|
283
310
|
if (!Number.isFinite(entry[0]) || !Number.isFinite(entry[1]))
|
|
284
311
|
return null;
|
|
285
312
|
return { x: clampNumber(entry[0], -MAX_COORDINATE, MAX_COORDINATE), y: clampNumber(entry[1], -MAX_COORDINATE, MAX_COORDINATE) };
|
|
286
313
|
};
|
|
287
|
-
|
|
288
|
-
|
|
314
|
+
var start = readPoint(value.start);
|
|
315
|
+
var end = readPoint(value.end);
|
|
289
316
|
if (!start || !end)
|
|
290
317
|
return null;
|
|
291
|
-
return withBindings({ start, end, bend: readNumber(value.bend, 0, -2 * MAX_SIZE, 2 * MAX_SIZE) }, { startBinding: readArrowBinding(value.startBinding), endBinding: readArrowBinding(value.endBinding) });
|
|
318
|
+
return withBindings({ start: start, end: end, bend: readNumber(value.bend, 0, -2 * MAX_SIZE, 2 * MAX_SIZE) }, { startBinding: readArrowBinding(value.startBinding), endBinding: readArrowBinding(value.endBinding) });
|
|
292
319
|
}
|
|
293
320
|
/**
|
|
294
321
|
* 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
|
|
@@ -297,17 +324,17 @@ function readArrowSpec(value) {
|
|
|
297
324
|
function readArrowBinding(value) {
|
|
298
325
|
if (!isRecord(value))
|
|
299
326
|
return null;
|
|
300
|
-
|
|
327
|
+
var targetId = typeof value.target === 'string' && value.target !== '' && value.target.length <= 128 ? value.target : null;
|
|
301
328
|
if (!targetId)
|
|
302
329
|
return null;
|
|
303
330
|
if (value.kind === 'center')
|
|
304
|
-
return { targetId, kind: 'center' };
|
|
331
|
+
return { targetId: targetId, kind: 'center' };
|
|
305
332
|
if (value.kind !== 'edge' || !Array.isArray(value.anchor))
|
|
306
333
|
return null;
|
|
307
|
-
|
|
334
|
+
var _a = value.anchor, across = _a[0], down = _a[1];
|
|
308
335
|
if (typeof across !== 'number' || typeof down !== 'number' || !Number.isFinite(across) || !Number.isFinite(down))
|
|
309
336
|
return null;
|
|
310
|
-
return { targetId, kind: 'edge', anchor: { x: clampNumber(across, 0, 1), y: clampNumber(down, 0, 1) } };
|
|
337
|
+
return { targetId: targetId, kind: 'edge', anchor: { x: clampNumber(across, 0, 1), y: clampNumber(down, 0, 1) } };
|
|
311
338
|
}
|
|
312
339
|
/**
|
|
313
340
|
* 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
|
|
@@ -315,56 +342,57 @@ function readArrowBinding(value) {
|
|
|
315
342
|
* where it was in the world. An arrow is upright: a turn the record carries is applied to its points.
|
|
316
343
|
*/
|
|
317
344
|
function arrowFromSpec(spec, box, strokeWidth) {
|
|
318
|
-
|
|
345
|
+
var _a, _b;
|
|
346
|
+
var start = spec.start, end = spec.end;
|
|
319
347
|
// What its ends are tied to is not a place on the board: it needs no turn.
|
|
320
|
-
|
|
348
|
+
var tiesOfSpec = { startBinding: (_a = spec.startBinding) !== null && _a !== void 0 ? _a : null, endBinding: (_b = spec.endBinding) !== null && _b !== void 0 ? _b : null };
|
|
321
349
|
if (box.rotation !== 0) {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
x:
|
|
329
|
-
y:
|
|
330
|
-
});
|
|
350
|
+
var radians = degreesToRadians(box.rotation);
|
|
351
|
+
var cosine_1 = Math.cos(radians);
|
|
352
|
+
var sine_1 = Math.sin(radians);
|
|
353
|
+
var centerX_1 = box.width / 2;
|
|
354
|
+
var centerY_1 = box.height / 2;
|
|
355
|
+
var turn = function (point) { return ({
|
|
356
|
+
x: centerX_1 + (point.x - centerX_1) * cosine_1 - (point.y - centerY_1) * sine_1,
|
|
357
|
+
y: centerY_1 + (point.x - centerX_1) * sine_1 + (point.y - centerY_1) * cosine_1
|
|
358
|
+
}); };
|
|
331
359
|
start = turn(start);
|
|
332
360
|
end = turn(end);
|
|
333
361
|
}
|
|
334
|
-
|
|
335
|
-
|
|
362
|
+
var geometry = buildArrow(start, end, spec.bend, strokeWidth);
|
|
363
|
+
var holdsWhatIsDrawn = box.rotation === 0 &&
|
|
336
364
|
Math.abs(geometry.box.x) <= 0.5 &&
|
|
337
365
|
Math.abs(geometry.box.y) <= 0.5 &&
|
|
338
366
|
Math.abs(geometry.box.width - box.width) <= 0.5 &&
|
|
339
367
|
Math.abs(geometry.box.height - box.height) <= 0.5;
|
|
340
368
|
if (holdsWhatIsDrawn) {
|
|
341
369
|
// 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.
|
|
342
|
-
|
|
343
|
-
return { d: serializePathData(commands), arrow: withBindings({ start, end, bend: geometry.spec.bend }, tiesOfSpec), box };
|
|
370
|
+
var commands = translatePathCommands(geometry.commands, geometry.box.x, geometry.box.y);
|
|
371
|
+
return { d: serializePathData(commands), arrow: withBindings({ start: start, end: end, bend: geometry.spec.bend }, tiesOfSpec), box: box };
|
|
344
372
|
}
|
|
345
373
|
return {
|
|
346
374
|
d: serializePathData(geometry.commands),
|
|
347
375
|
arrow: withBindings(geometry.spec, tiesOfSpec),
|
|
348
|
-
box: {
|
|
376
|
+
box: __assign(__assign({}, box), { rotation: 0, x: box.x + geometry.box.x, y: box.y + geometry.box.y, width: geometry.box.width, height: geometry.box.height })
|
|
349
377
|
};
|
|
350
378
|
}
|
|
351
379
|
function readPath(raw, id, index, warnings) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
380
|
+
var paint = readPaint(raw, { fill: 'transparent', stroke: '#1f2937', strokeWidth: 3 });
|
|
381
|
+
var ink = readInk(raw.ink);
|
|
382
|
+
var shape = readNullableString(raw.shape, 100);
|
|
383
|
+
var box = readBox(raw);
|
|
356
384
|
// 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
|
|
357
385
|
// has the outline only, and its arrow is found in that.
|
|
358
|
-
|
|
386
|
+
var arrowSpec = ink ? null : readArrowSpec(raw.arrow);
|
|
359
387
|
if (!ink && !arrowSpec && shape === 'arrow' && typeof raw.d === 'string' && raw.d.length <= MAX_PATH_DATA_LENGTH) {
|
|
360
|
-
|
|
388
|
+
var legacy = arrowPointsOfLegacyPath(parsePathData(raw.d));
|
|
361
389
|
if (legacy)
|
|
362
|
-
arrowSpec = {
|
|
390
|
+
arrowSpec = __assign(__assign({}, legacy), { bend: 0 });
|
|
363
391
|
}
|
|
364
|
-
|
|
365
|
-
|
|
392
|
+
var arrow = null;
|
|
393
|
+
var d;
|
|
366
394
|
if (arrowSpec) {
|
|
367
|
-
|
|
395
|
+
var drawn = arrowFromSpec(arrowSpec, box, paint.strokeWidth);
|
|
368
396
|
d = drawn.d;
|
|
369
397
|
arrow = drawn.arrow;
|
|
370
398
|
box = drawn.box;
|
|
@@ -374,93 +402,50 @@ function readPath(raw, id, index, warnings) {
|
|
|
374
402
|
d = ink ? brushOutlineData(ink, paint.strokeWidth) : typeof raw.d === 'string' ? raw.d : '';
|
|
375
403
|
}
|
|
376
404
|
if (d.length === 0 || d.length > MAX_PATH_DATA_LENGTH || parsePathData(d).length < 2) {
|
|
377
|
-
warnings.push({ code: 'bad-path', index, detail: 'The outline of the shape is missing or cannot be read.' });
|
|
405
|
+
warnings.push({ code: 'bad-path', index: index, detail: 'The outline of the shape is missing or cannot be read.' });
|
|
378
406
|
return null;
|
|
379
407
|
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
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
|
-
});
|
|
408
|
+
var path = createPathObject(__assign(__assign(__assign({ id: id, parentId: readParentId(raw) }, box), paint), { d: d, shape: shape, roundness: readNumber(raw.roundness, 0, 0, 100), aspectLocked: readBoolean(raw.aspectLocked, false), flipX: readBoolean(raw.flipX, false), flipY: readBoolean(raw.flipY, false), lineCap: readOneOf(raw.lineCap, LINE_CAPS, 'round'), lineJoin: readOneOf(raw.lineJoin, LINE_JOINS, 'round'), ink: ink, arrow: arrow }));
|
|
409
|
+
// A line (an arrow, a line-only icon) saved with no width, as the Width control once allowed, would stay invisible on the board:
|
|
410
|
+
// it comes back with the thinnest line it can have. A brush stroke is drawn from its ink and keeps the width it was drawn with.
|
|
411
|
+
// (The head of an arrow drawn above is the same for a width of 0 as for the thinnest line, so it needs no drawing again.)
|
|
412
|
+
var thinnestLine = ink ? 0 : minimumStrokeWidthOf(path);
|
|
413
|
+
return path.strokeWidth < thinnestLine ? __assign(__assign({}, path), { strokeWidth: thinnestLine }) : path;
|
|
396
414
|
}
|
|
397
415
|
function readText(raw, id) {
|
|
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
|
-
});
|
|
416
|
+
return createTextObject(__assign(__assign({ id: id, parentId: readParentId(raw) }, readBox(raw)), { text: readString(raw.text, '', MAX_TEXT_LENGTH), color: readColor(raw.color, TEXT_DEFAULTS.color), fontSize: readNumber(raw.fontSize, TEXT_DEFAULTS.fontSize, 4, 600), fontFamily: readFontFamily(raw.fontFamily), fontWeight: readOneOf(raw.fontWeight, ['normal', 'bold'], 'normal'), fontStyle: readOneOf(raw.fontStyle, ['normal', 'italic'], 'normal'), underline: readBoolean(raw.underline, false), align: readOneOf(raw.align, ['left', 'center', 'right'], 'left'), lineHeight: readNumber(raw.lineHeight, 1.2, 0.5, 4), padding: readNumber(raw.padding, 0, 0, 200), backgroundColor: readColor(raw.backgroundColor, 'transparent'), autoWidth: readBoolean(raw.autoWidth, true) }));
|
|
415
417
|
}
|
|
416
418
|
function readImage(raw, id) {
|
|
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
|
-
});
|
|
419
|
+
return createImageObject(__assign(__assign({ id: id, parentId: readParentId(raw) }, readBox(raw)), { src: readImageAddress(raw.src), imageRef: readNullableString(raw.imageRef, 200), fileName: readNullableString(raw.fileName, 300), libraryFileId: readNullableString(raw.libraryFileId, 200), libraryFileUrl: readLibraryFileAddress(raw.libraryFileUrl), naturalWidth: readNumber(raw.naturalWidth, 0, 0, MAX_SIZE), naturalHeight: readNumber(raw.naturalHeight, 0, 0, MAX_SIZE) }));
|
|
427
420
|
}
|
|
428
421
|
function readWidget(raw, id, index, warnings) {
|
|
422
|
+
var _a;
|
|
429
423
|
// The creator reads `_widgetId`; the canvas's own key is `widgetId`. Either names the widget.
|
|
430
|
-
|
|
424
|
+
var widgetId = (_a = readNullableString(raw.widgetId, 200)) !== null && _a !== void 0 ? _a : readNullableString(raw[HOST_READABLE_KEYS.widgetId], 200);
|
|
431
425
|
if (!widgetId) {
|
|
432
|
-
warnings.push({ code: 'bad-object', index, detail: 'A widget card has no widget id.' });
|
|
426
|
+
warnings.push({ code: 'bad-object', index: index, detail: 'A widget card has no widget id.' });
|
|
433
427
|
return null;
|
|
434
428
|
}
|
|
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
|
-
});
|
|
429
|
+
return createWidgetObject(__assign(__assign({ id: id, widgetId: widgetId, parentId: readParentId(raw) }, readBox(raw)), { widgetName: readNullableString(raw.widgetName, 300), isPrimary: readBoolean(raw.isPrimary, false), thumbnail: readImageAddress(raw.thumbnail), thumbnailRef: readNullableString(raw.thumbnailRef, 200) }));
|
|
445
430
|
}
|
|
446
431
|
function readGroup(raw, id) {
|
|
447
|
-
return createGroupObject({ id, parentId: readParentId(raw) });
|
|
432
|
+
return createGroupObject({ id: id, parentId: readParentId(raw) });
|
|
448
433
|
}
|
|
449
434
|
function readObject(raw, index, seen, warnings) {
|
|
450
435
|
if (!isRecord(raw)) {
|
|
451
|
-
warnings.push({ code: 'bad-object', index, detail: 'An entry of the list is not an object.' });
|
|
436
|
+
warnings.push({ code: 'bad-object', index: index, detail: 'An entry of the list is not an object.' });
|
|
452
437
|
return null;
|
|
453
438
|
}
|
|
454
439
|
// Readers outside the canvas treat these as "not there": so does the canvas.
|
|
455
440
|
if (raw[HOST_READABLE_KEYS.visible] === false || raw[HOST_READABLE_KEYS.excludeFromExport] === true)
|
|
456
441
|
return null;
|
|
457
|
-
|
|
458
|
-
|
|
442
|
+
var type = raw[HOST_READABLE_KEYS.objectType];
|
|
443
|
+
var known = ['rect', 'ellipse', 'path', 'text', 'image', 'widget', 'group'];
|
|
459
444
|
if (typeof type !== 'string' || !known.includes(type)) {
|
|
460
|
-
warnings.push({ code: 'unknown-type', index, detail: typeof type === 'string' ? type : String(type) });
|
|
445
|
+
warnings.push({ code: 'unknown-type', index: index, detail: typeof type === 'string' ? type : String(type) });
|
|
461
446
|
return null;
|
|
462
447
|
}
|
|
463
|
-
|
|
448
|
+
var id = readId(raw, seen);
|
|
464
449
|
switch (type) {
|
|
465
450
|
case 'rect':
|
|
466
451
|
return readRect(raw, id);
|
|
@@ -478,27 +463,28 @@ function readObject(raw, index, seen, warnings) {
|
|
|
478
463
|
return readGroup(raw, id);
|
|
479
464
|
}
|
|
480
465
|
}
|
|
481
|
-
|
|
466
|
+
var EMPTY_VIEWPORT = { x: 0, y: 0, zoom: 1 };
|
|
482
467
|
/**
|
|
483
468
|
* Reads a saved canvas: the text `serializeCanvasDocument` wrote, or the same thing already parsed.
|
|
484
469
|
* Never throws.
|
|
485
470
|
*/
|
|
486
471
|
export function parseCanvasDocument(input) {
|
|
487
|
-
|
|
488
|
-
|
|
472
|
+
var _a;
|
|
473
|
+
var warnings = [];
|
|
474
|
+
var nothing = function (status) { return ({
|
|
489
475
|
scene: [],
|
|
490
|
-
viewport: {
|
|
491
|
-
status,
|
|
492
|
-
warnings
|
|
493
|
-
});
|
|
476
|
+
viewport: __assign({}, EMPTY_VIEWPORT),
|
|
477
|
+
status: status,
|
|
478
|
+
warnings: warnings
|
|
479
|
+
}); };
|
|
494
480
|
if (input === null || input === undefined || (typeof input === 'string' && input.trim() === ''))
|
|
495
481
|
return nothing('empty');
|
|
496
|
-
|
|
482
|
+
var document = input;
|
|
497
483
|
if (typeof input === 'string') {
|
|
498
484
|
try {
|
|
499
485
|
document = JSON.parse(input);
|
|
500
486
|
}
|
|
501
|
-
catch {
|
|
487
|
+
catch (_b) {
|
|
502
488
|
warnings.push({ code: 'not-json' });
|
|
503
489
|
return nothing('invalid');
|
|
504
490
|
}
|
|
@@ -508,19 +494,19 @@ export function parseCanvasDocument(input) {
|
|
|
508
494
|
return nothing('invalid');
|
|
509
495
|
}
|
|
510
496
|
if (document.version !== CANVAS_DOCUMENT_VERSION) {
|
|
511
|
-
warnings.push({ code: 'unsupported-version', detail: String(document.version
|
|
497
|
+
warnings.push({ code: 'unsupported-version', detail: String((_a = document.version) !== null && _a !== void 0 ? _a : 'none') });
|
|
512
498
|
return nothing('unsupported');
|
|
513
499
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
500
|
+
var canvas = document.canvas;
|
|
501
|
+
var list = isRecord(canvas) && Array.isArray(canvas.objects) ? canvas.objects : [];
|
|
502
|
+
var limited = list.length > MAX_OBJECTS_PER_DOCUMENT ? list.slice(0, MAX_OBJECTS_PER_DOCUMENT) : list;
|
|
517
503
|
if (limited.length < list.length) {
|
|
518
|
-
warnings.push({ code: 'too-many-objects', detail:
|
|
504
|
+
warnings.push({ code: 'too-many-objects', detail: "".concat(list.length, " objects, the first ").concat(limited.length, " were read.") });
|
|
519
505
|
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
limited.forEach((raw, index)
|
|
523
|
-
|
|
506
|
+
var seen = new Set();
|
|
507
|
+
var objects = [];
|
|
508
|
+
limited.forEach(function (raw, index) {
|
|
509
|
+
var object = readObject(raw, index, seen, warnings);
|
|
524
510
|
if (object)
|
|
525
511
|
objects.push(object);
|
|
526
512
|
});
|
|
@@ -529,6 +515,6 @@ export function parseCanvasDocument(input) {
|
|
|
529
515
|
scene: dropDanglingBindings(normalizeScene(objects)),
|
|
530
516
|
viewport: sanitizeViewport(document.viewport),
|
|
531
517
|
status: 'ok',
|
|
532
|
-
warnings
|
|
518
|
+
warnings: warnings
|
|
533
519
|
};
|
|
534
520
|
}
|
|
@@ -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 var 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. */
|