@widgetic/canvas 0.5.41 → 0.5.42
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/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.js +17 -18
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.js +1286 -1536
- 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/pageEvents.js +4 -4
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/strokeSnap.js +88 -143
- 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.js +3 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +18 -18
- package/dist/canvas/ui/floatingLayer.js +5 -6
- 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/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.js +696 -795
- package/dist/engine/codec/canvasDocument.js +183 -147
- 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.js +33 -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.js +129 -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.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 +262 -308
- 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.js +4 -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.js +17 -68
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- package/dist/i18n/shortcuts.js +14 -14
- 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
|
@@ -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,25 @@ 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
|
};
|
|
80
61
|
/** The reference and the (possibly inline) bytes of a picture, with the bytes moved to the store when there is one. */
|
|
81
62
|
function externalizeBytes(inline, existingRef, makeRef, store) {
|
|
82
63
|
if (!store || !inline || !inline.startsWith('data:'))
|
|
83
64
|
return { value: inline, ref: existingRef };
|
|
84
|
-
|
|
65
|
+
const ref = existingRef ?? makeRef();
|
|
85
66
|
if (!ref)
|
|
86
67
|
return { value: inline, ref: existingRef };
|
|
87
68
|
store.save(ref, inline);
|
|
88
|
-
return { value: null, ref
|
|
69
|
+
return { value: null, ref };
|
|
89
70
|
}
|
|
90
71
|
/** 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
72
|
function writeArrowBinding(binding) {
|
|
@@ -94,21 +75,19 @@ function writeArrowBinding(binding) {
|
|
|
94
75
|
: { target: binding.targetId, kind: 'center' };
|
|
95
76
|
}
|
|
96
77
|
function serializeObject(object, options) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var overrides = {};
|
|
78
|
+
const record = { id: object.id, [HOST_READABLE_KEYS.objectType]: object.type, parentId: object.parentId };
|
|
79
|
+
const source = object;
|
|
80
|
+
let overrides = {};
|
|
101
81
|
if (object.type === 'widget') {
|
|
102
|
-
|
|
82
|
+
const thumbnail = externalizeBytes(object.thumbnail, object.thumbnailRef, () => `widget:${object.widgetId}`, options.imageStore);
|
|
103
83
|
overrides = { thumbnail: thumbnail.value, thumbnailRef: thumbnail.ref };
|
|
104
84
|
}
|
|
105
85
|
else if (object.type === 'image') {
|
|
106
|
-
|
|
86
|
+
const image = externalizeBytes(object.src, object.imageRef, () => `shape:${object.id}`, options.imageStore);
|
|
107
87
|
overrides = { src: image.value, imageRef: image.ref };
|
|
108
88
|
}
|
|
109
|
-
for (
|
|
110
|
-
|
|
111
|
-
var value = key in overrides ? overrides[key] : source[key];
|
|
89
|
+
for (const key of KEYS_BY_TYPE[object.type]) {
|
|
90
|
+
const value = key in overrides ? overrides[key] : source[key];
|
|
112
91
|
record[key] = typeof value === 'number' ? roundTo(value, 3) : value;
|
|
113
92
|
}
|
|
114
93
|
if (object.type === 'widget') {
|
|
@@ -129,8 +108,15 @@ function serializeObject(object, options) {
|
|
|
129
108
|
// 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
109
|
if (object.arrow) {
|
|
131
110
|
delete record.d;
|
|
132
|
-
|
|
133
|
-
record.arrow =
|
|
111
|
+
const { start, end, bend, startBinding, endBinding } = object.arrow;
|
|
112
|
+
record.arrow = {
|
|
113
|
+
start: [roundTo(start.x, 3), roundTo(start.y, 3)],
|
|
114
|
+
end: [roundTo(end.x, 3), roundTo(end.y, 3)],
|
|
115
|
+
bend: roundTo(bend, 3),
|
|
116
|
+
// 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.
|
|
117
|
+
...(startBinding ? { startBinding: writeArrowBinding(startBinding) } : {}),
|
|
118
|
+
...(endBinding ? { endBinding: writeArrowBinding(endBinding) } : {})
|
|
119
|
+
};
|
|
134
120
|
}
|
|
135
121
|
else
|
|
136
122
|
delete record.arrow;
|
|
@@ -138,28 +124,23 @@ function serializeObject(object, options) {
|
|
|
138
124
|
return record;
|
|
139
125
|
}
|
|
140
126
|
/** 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 = {}; }
|
|
127
|
+
export function createCanvasDocument(scene, viewport, options = {}) {
|
|
144
128
|
return {
|
|
145
129
|
version: CANVAS_DOCUMENT_VERSION,
|
|
146
|
-
savedAt: (
|
|
130
|
+
savedAt: (options.now ?? new Date()).toISOString(),
|
|
147
131
|
viewport: { x: roundTo(viewport.x, 2), y: roundTo(viewport.y, 2), zoom: roundTo(viewport.zoom, 4) },
|
|
148
|
-
canvas: { objects: scene.map(
|
|
132
|
+
canvas: { objects: scene.map((object) => serializeObject(object, options)) }
|
|
149
133
|
};
|
|
150
134
|
}
|
|
151
135
|
/** The text a board is saved as. */
|
|
152
|
-
export function serializeCanvasDocument(scene, viewport, options) {
|
|
153
|
-
if (options === void 0) { options = {}; }
|
|
136
|
+
export function serializeCanvasDocument(scene, viewport, options = {}) {
|
|
154
137
|
return JSON.stringify(createCanvasDocument(scene, viewport, options));
|
|
155
138
|
}
|
|
156
139
|
function isRecord(value) {
|
|
157
140
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
158
141
|
}
|
|
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;
|
|
142
|
+
function readNumber(value, fallback, minimum = -Infinity, maximum = Infinity) {
|
|
143
|
+
const number = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value) : NaN;
|
|
163
144
|
if (!Number.isFinite(number))
|
|
164
145
|
return fallback;
|
|
165
146
|
return clampNumber(number, minimum, maximum);
|
|
@@ -167,36 +148,34 @@ function readNumber(value, fallback, minimum, maximum) {
|
|
|
167
148
|
function readBoolean(value, fallback) {
|
|
168
149
|
return typeof value === 'boolean' ? value : fallback;
|
|
169
150
|
}
|
|
170
|
-
function readString(value, fallback, maximumLength) {
|
|
171
|
-
if (maximumLength === void 0) { maximumLength = 1000; }
|
|
151
|
+
function readString(value, fallback, maximumLength = 1000) {
|
|
172
152
|
return typeof value === 'string' ? value.slice(0, maximumLength) : fallback;
|
|
173
153
|
}
|
|
174
|
-
function readNullableString(value, maximumLength) {
|
|
175
|
-
if (maximumLength === void 0) { maximumLength = 1000; }
|
|
154
|
+
function readNullableString(value, maximumLength = 1000) {
|
|
176
155
|
return typeof value === 'string' && value !== '' ? value.slice(0, maximumLength) : null;
|
|
177
156
|
}
|
|
178
157
|
function readOneOf(value, allowed, fallback) {
|
|
179
158
|
return typeof value === 'string' && allowed.includes(value) ? value : fallback;
|
|
180
159
|
}
|
|
181
160
|
/** A hex, `rgb()`, `hsl()` or named colour: nothing that could carry more than a colour into CSS or SVG. */
|
|
182
|
-
|
|
161
|
+
const COLOR_PATTERN = /^(#[0-9a-f]{3,8}|(?:rgb|hsl)a?\([0-9a-z\s.,%/+-]{1,80}\)|[a-z]{3,30})$/i;
|
|
183
162
|
export function readColor(value, fallback) {
|
|
184
163
|
return typeof value === 'string' && COLOR_PATTERN.test(value.trim()) ? value.trim() : fallback;
|
|
185
164
|
}
|
|
186
|
-
|
|
165
|
+
const FONT_FAMILY_PATTERN = /^[\p{L}\p{N}\s,'"\-_.]{1,200}$/u;
|
|
187
166
|
function readFontFamily(value) {
|
|
188
167
|
return typeof value === 'string' && FONT_FAMILY_PATTERN.test(value) ? value : TEXT_DEFAULTS.fontFamily;
|
|
189
168
|
}
|
|
190
|
-
|
|
169
|
+
const IMAGE_ADDRESS_PATTERN = /^(?:data:image\/[a-z0-9.+-]+[;,]|https?:\/\/|blob:|\/(?!\/))/i;
|
|
191
170
|
/** An image address the browser may load into an `<img>`: inline bytes, http(s), a blob or a path of this site. */
|
|
192
171
|
export function readImageAddress(value) {
|
|
193
172
|
if (typeof value !== 'string' || value === '')
|
|
194
173
|
return null;
|
|
195
174
|
return IMAGE_ADDRESS_PATTERN.test(value) ? value : null;
|
|
196
175
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
176
|
+
const STROKE_STYLES = ['solid', 'dashed', 'dotted'];
|
|
177
|
+
const LINE_CAPS = ['butt', 'round', 'square'];
|
|
178
|
+
const LINE_JOINS = ['miter', 'round', 'bevel'];
|
|
200
179
|
function readBox(raw) {
|
|
201
180
|
return {
|
|
202
181
|
x: readNumber(raw.x, 0, -MAX_COORDINATE, MAX_COORDINATE),
|
|
@@ -216,9 +195,9 @@ function readPaint(raw, defaults) {
|
|
|
216
195
|
strokeStyle: readOneOf(raw.strokeStyle, STROKE_STYLES, 'solid')
|
|
217
196
|
};
|
|
218
197
|
}
|
|
219
|
-
|
|
198
|
+
const PAINT_FALLBACK = { fill: '#ffffff', stroke: '#374151', strokeWidth: 2 };
|
|
220
199
|
function readId(raw, seen) {
|
|
221
|
-
|
|
200
|
+
let id = typeof raw.id === 'string' && raw.id !== '' && raw.id.length <= 128 ? raw.id : createObjectId();
|
|
222
201
|
// Two objects with the same id would be one on the board: the second gets a new one.
|
|
223
202
|
while (seen.has(id))
|
|
224
203
|
id = createObjectId();
|
|
@@ -229,19 +208,30 @@ function readParentId(raw) {
|
|
|
229
208
|
return typeof raw.parentId === 'string' && raw.parentId !== '' && raw.parentId.length <= 128 ? raw.parentId : null;
|
|
230
209
|
}
|
|
231
210
|
function readRect(raw, id) {
|
|
232
|
-
return createRectObject(
|
|
211
|
+
return createRectObject({
|
|
212
|
+
id,
|
|
213
|
+
parentId: readParentId(raw),
|
|
214
|
+
...readBox(raw),
|
|
215
|
+
...readPaint(raw, PAINT_FALLBACK),
|
|
216
|
+
roundness: readNumber(raw.roundness, 0, 0, 100)
|
|
217
|
+
});
|
|
233
218
|
}
|
|
234
219
|
function readEllipse(raw, id) {
|
|
235
|
-
return createEllipseObject(
|
|
220
|
+
return createEllipseObject({
|
|
221
|
+
id,
|
|
222
|
+
parentId: readParentId(raw),
|
|
223
|
+
...readBox(raw),
|
|
224
|
+
...readPaint(raw, PAINT_FALLBACK),
|
|
225
|
+
aspectLocked: readBoolean(raw.aspectLocked, false)
|
|
226
|
+
});
|
|
236
227
|
}
|
|
237
|
-
|
|
228
|
+
const BRUSH_PRESSURES = ['pen', 'speed'];
|
|
238
229
|
/** 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
230
|
function readInk(value) {
|
|
240
231
|
if (!isRecord(value) || !Array.isArray(value.points))
|
|
241
232
|
return null;
|
|
242
|
-
|
|
243
|
-
for (
|
|
244
|
-
var entry = _a[_i];
|
|
233
|
+
const points = [];
|
|
234
|
+
for (const entry of value.points.slice(0, BRUSH_MAX_POINTS)) {
|
|
245
235
|
if (!Array.isArray(entry) || typeof entry[0] !== 'number' || typeof entry[1] !== 'number')
|
|
246
236
|
continue;
|
|
247
237
|
if (!Number.isFinite(entry[0]) || !Number.isFinite(entry[1]))
|
|
@@ -254,24 +244,24 @@ function readInk(value) {
|
|
|
254
244
|
}
|
|
255
245
|
if (points.length === 0)
|
|
256
246
|
return null;
|
|
257
|
-
return { points
|
|
247
|
+
return { points, pressure: readOneOf(value.pressure, BRUSH_PRESSURES, 'speed') };
|
|
258
248
|
}
|
|
259
249
|
/** The two ends and the bend of an arrow, or null when the record has none that can be read. */
|
|
260
250
|
function readArrowSpec(value) {
|
|
261
251
|
if (!isRecord(value))
|
|
262
252
|
return null;
|
|
263
|
-
|
|
253
|
+
const readPoint = (entry) => {
|
|
264
254
|
if (!Array.isArray(entry) || typeof entry[0] !== 'number' || typeof entry[1] !== 'number')
|
|
265
255
|
return null;
|
|
266
256
|
if (!Number.isFinite(entry[0]) || !Number.isFinite(entry[1]))
|
|
267
257
|
return null;
|
|
268
258
|
return { x: clampNumber(entry[0], -MAX_COORDINATE, MAX_COORDINATE), y: clampNumber(entry[1], -MAX_COORDINATE, MAX_COORDINATE) };
|
|
269
259
|
};
|
|
270
|
-
|
|
271
|
-
|
|
260
|
+
const start = readPoint(value.start);
|
|
261
|
+
const end = readPoint(value.end);
|
|
272
262
|
if (!start || !end)
|
|
273
263
|
return null;
|
|
274
|
-
return withBindings({ start
|
|
264
|
+
return withBindings({ start, end, bend: readNumber(value.bend, 0, -2 * MAX_SIZE, 2 * MAX_SIZE) }, { startBinding: readArrowBinding(value.startBinding), endBinding: readArrowBinding(value.endBinding) });
|
|
275
265
|
}
|
|
276
266
|
/**
|
|
277
267
|
* 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 +270,17 @@ function readArrowSpec(value) {
|
|
|
280
270
|
function readArrowBinding(value) {
|
|
281
271
|
if (!isRecord(value))
|
|
282
272
|
return null;
|
|
283
|
-
|
|
273
|
+
const targetId = typeof value.target === 'string' && value.target !== '' && value.target.length <= 128 ? value.target : null;
|
|
284
274
|
if (!targetId)
|
|
285
275
|
return null;
|
|
286
276
|
if (value.kind === 'center')
|
|
287
|
-
return { targetId
|
|
277
|
+
return { targetId, kind: 'center' };
|
|
288
278
|
if (value.kind !== 'edge' || !Array.isArray(value.anchor))
|
|
289
279
|
return null;
|
|
290
|
-
|
|
280
|
+
const [across, down] = value.anchor;
|
|
291
281
|
if (typeof across !== 'number' || typeof down !== 'number' || !Number.isFinite(across) || !Number.isFinite(down))
|
|
292
282
|
return null;
|
|
293
|
-
return { targetId
|
|
283
|
+
return { targetId, kind: 'edge', anchor: { x: clampNumber(across, 0, 1), y: clampNumber(down, 0, 1) } };
|
|
294
284
|
}
|
|
295
285
|
/**
|
|
296
286
|
* 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 +288,56 @@ function readArrowBinding(value) {
|
|
|
298
288
|
* where it was in the world. An arrow is upright: a turn the record carries is applied to its points.
|
|
299
289
|
*/
|
|
300
290
|
function arrowFromSpec(spec, box, strokeWidth) {
|
|
301
|
-
|
|
302
|
-
var start = spec.start, end = spec.end;
|
|
291
|
+
let { start, end } = spec;
|
|
303
292
|
// What its ends are tied to is not a place on the board: it needs no turn.
|
|
304
|
-
|
|
293
|
+
const tiesOfSpec = { startBinding: spec.startBinding ?? null, endBinding: spec.endBinding ?? null };
|
|
305
294
|
if (box.rotation !== 0) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
x:
|
|
313
|
-
y:
|
|
314
|
-
});
|
|
295
|
+
const radians = degreesToRadians(box.rotation);
|
|
296
|
+
const cosine = Math.cos(radians);
|
|
297
|
+
const sine = Math.sin(radians);
|
|
298
|
+
const centerX = box.width / 2;
|
|
299
|
+
const centerY = box.height / 2;
|
|
300
|
+
const turn = (point) => ({
|
|
301
|
+
x: centerX + (point.x - centerX) * cosine - (point.y - centerY) * sine,
|
|
302
|
+
y: centerY + (point.x - centerX) * sine + (point.y - centerY) * cosine
|
|
303
|
+
});
|
|
315
304
|
start = turn(start);
|
|
316
305
|
end = turn(end);
|
|
317
306
|
}
|
|
318
|
-
|
|
319
|
-
|
|
307
|
+
const geometry = buildArrow(start, end, spec.bend, strokeWidth);
|
|
308
|
+
const holdsWhatIsDrawn = box.rotation === 0 &&
|
|
320
309
|
Math.abs(geometry.box.x) <= 0.5 &&
|
|
321
310
|
Math.abs(geometry.box.y) <= 0.5 &&
|
|
322
311
|
Math.abs(geometry.box.width - box.width) <= 0.5 &&
|
|
323
312
|
Math.abs(geometry.box.height - box.height) <= 0.5;
|
|
324
313
|
if (holdsWhatIsDrawn) {
|
|
325
314
|
// 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
|
|
315
|
+
const commands = translatePathCommands(geometry.commands, geometry.box.x, geometry.box.y);
|
|
316
|
+
return { d: serializePathData(commands), arrow: withBindings({ start, end, bend: geometry.spec.bend }, tiesOfSpec), box };
|
|
328
317
|
}
|
|
329
318
|
return {
|
|
330
319
|
d: serializePathData(geometry.commands),
|
|
331
320
|
arrow: withBindings(geometry.spec, tiesOfSpec),
|
|
332
|
-
box:
|
|
321
|
+
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
322
|
};
|
|
334
323
|
}
|
|
335
324
|
function readPath(raw, id, index, warnings) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
325
|
+
const paint = readPaint(raw, { fill: 'transparent', stroke: '#1f2937', strokeWidth: 3 });
|
|
326
|
+
const ink = readInk(raw.ink);
|
|
327
|
+
const shape = readNullableString(raw.shape, 100);
|
|
328
|
+
let box = readBox(raw);
|
|
340
329
|
// 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
330
|
// has the outline only, and its arrow is found in that.
|
|
342
|
-
|
|
331
|
+
let arrowSpec = ink ? null : readArrowSpec(raw.arrow);
|
|
343
332
|
if (!ink && !arrowSpec && shape === 'arrow' && typeof raw.d === 'string' && raw.d.length <= MAX_PATH_DATA_LENGTH) {
|
|
344
|
-
|
|
333
|
+
const legacy = arrowPointsOfLegacyPath(parsePathData(raw.d));
|
|
345
334
|
if (legacy)
|
|
346
|
-
arrowSpec =
|
|
335
|
+
arrowSpec = { ...legacy, bend: 0 };
|
|
347
336
|
}
|
|
348
|
-
|
|
349
|
-
|
|
337
|
+
let arrow = null;
|
|
338
|
+
let d;
|
|
350
339
|
if (arrowSpec) {
|
|
351
|
-
|
|
340
|
+
const drawn = arrowFromSpec(arrowSpec, box, paint.strokeWidth);
|
|
352
341
|
d = drawn.d;
|
|
353
342
|
arrow = drawn.arrow;
|
|
354
343
|
box = drawn.box;
|
|
@@ -358,45 +347,93 @@ function readPath(raw, id, index, warnings) {
|
|
|
358
347
|
d = ink ? brushOutlineData(ink, paint.strokeWidth) : typeof raw.d === 'string' ? raw.d : '';
|
|
359
348
|
}
|
|
360
349
|
if (d.length === 0 || d.length > MAX_PATH_DATA_LENGTH || parsePathData(d).length < 2) {
|
|
361
|
-
warnings.push({ code: 'bad-path', index
|
|
350
|
+
warnings.push({ code: 'bad-path', index, detail: 'The outline of the shape is missing or cannot be read.' });
|
|
362
351
|
return null;
|
|
363
352
|
}
|
|
364
|
-
return createPathObject(
|
|
353
|
+
return createPathObject({
|
|
354
|
+
id,
|
|
355
|
+
parentId: readParentId(raw),
|
|
356
|
+
...box,
|
|
357
|
+
...paint,
|
|
358
|
+
d,
|
|
359
|
+
shape,
|
|
360
|
+
roundness: readNumber(raw.roundness, 0, 0, 100),
|
|
361
|
+
aspectLocked: readBoolean(raw.aspectLocked, false),
|
|
362
|
+
flipX: readBoolean(raw.flipX, false),
|
|
363
|
+
flipY: readBoolean(raw.flipY, false),
|
|
364
|
+
lineCap: readOneOf(raw.lineCap, LINE_CAPS, 'round'),
|
|
365
|
+
lineJoin: readOneOf(raw.lineJoin, LINE_JOINS, 'round'),
|
|
366
|
+
ink,
|
|
367
|
+
arrow
|
|
368
|
+
});
|
|
365
369
|
}
|
|
366
370
|
function readText(raw, id) {
|
|
367
|
-
return createTextObject(
|
|
371
|
+
return createTextObject({
|
|
372
|
+
id,
|
|
373
|
+
parentId: readParentId(raw),
|
|
374
|
+
...readBox(raw),
|
|
375
|
+
text: readString(raw.text, '', MAX_TEXT_LENGTH),
|
|
376
|
+
color: readColor(raw.color, TEXT_DEFAULTS.color),
|
|
377
|
+
fontSize: readNumber(raw.fontSize, TEXT_DEFAULTS.fontSize, 4, 600),
|
|
378
|
+
fontFamily: readFontFamily(raw.fontFamily),
|
|
379
|
+
fontWeight: readOneOf(raw.fontWeight, ['normal', 'bold'], 'normal'),
|
|
380
|
+
fontStyle: readOneOf(raw.fontStyle, ['normal', 'italic'], 'normal'),
|
|
381
|
+
underline: readBoolean(raw.underline, false),
|
|
382
|
+
align: readOneOf(raw.align, ['left', 'center', 'right'], 'left'),
|
|
383
|
+
lineHeight: readNumber(raw.lineHeight, 1.2, 0.5, 4),
|
|
384
|
+
padding: readNumber(raw.padding, 0, 0, 200),
|
|
385
|
+
backgroundColor: readColor(raw.backgroundColor, 'transparent'),
|
|
386
|
+
autoWidth: readBoolean(raw.autoWidth, true)
|
|
387
|
+
});
|
|
368
388
|
}
|
|
369
389
|
function readImage(raw, id) {
|
|
370
|
-
return createImageObject(
|
|
390
|
+
return createImageObject({
|
|
391
|
+
id,
|
|
392
|
+
parentId: readParentId(raw),
|
|
393
|
+
...readBox(raw),
|
|
394
|
+
src: readImageAddress(raw.src),
|
|
395
|
+
imageRef: readNullableString(raw.imageRef, 200),
|
|
396
|
+
fileName: readNullableString(raw.fileName, 300),
|
|
397
|
+
naturalWidth: readNumber(raw.naturalWidth, 0, 0, MAX_SIZE),
|
|
398
|
+
naturalHeight: readNumber(raw.naturalHeight, 0, 0, MAX_SIZE)
|
|
399
|
+
});
|
|
371
400
|
}
|
|
372
401
|
function readWidget(raw, id, index, warnings) {
|
|
373
|
-
var _a;
|
|
374
402
|
// The creator reads `_widgetId`; the canvas's own key is `widgetId`. Either names the widget.
|
|
375
|
-
|
|
403
|
+
const widgetId = readNullableString(raw.widgetId, 200) ?? readNullableString(raw[HOST_READABLE_KEYS.widgetId], 200);
|
|
376
404
|
if (!widgetId) {
|
|
377
|
-
warnings.push({ code: 'bad-object', index
|
|
405
|
+
warnings.push({ code: 'bad-object', index, detail: 'A widget card has no widget id.' });
|
|
378
406
|
return null;
|
|
379
407
|
}
|
|
380
|
-
return createWidgetObject(
|
|
408
|
+
return createWidgetObject({
|
|
409
|
+
id,
|
|
410
|
+
widgetId,
|
|
411
|
+
parentId: readParentId(raw),
|
|
412
|
+
...readBox(raw),
|
|
413
|
+
widgetName: readNullableString(raw.widgetName, 300),
|
|
414
|
+
isPrimary: readBoolean(raw.isPrimary, false),
|
|
415
|
+
thumbnail: readImageAddress(raw.thumbnail),
|
|
416
|
+
thumbnailRef: readNullableString(raw.thumbnailRef, 200)
|
|
417
|
+
});
|
|
381
418
|
}
|
|
382
419
|
function readGroup(raw, id) {
|
|
383
|
-
return createGroupObject({ id
|
|
420
|
+
return createGroupObject({ id, parentId: readParentId(raw) });
|
|
384
421
|
}
|
|
385
422
|
function readObject(raw, index, seen, warnings) {
|
|
386
423
|
if (!isRecord(raw)) {
|
|
387
|
-
warnings.push({ code: 'bad-object', index
|
|
424
|
+
warnings.push({ code: 'bad-object', index, detail: 'An entry of the list is not an object.' });
|
|
388
425
|
return null;
|
|
389
426
|
}
|
|
390
427
|
// Readers outside the canvas treat these as "not there": so does the canvas.
|
|
391
428
|
if (raw[HOST_READABLE_KEYS.visible] === false || raw[HOST_READABLE_KEYS.excludeFromExport] === true)
|
|
392
429
|
return null;
|
|
393
|
-
|
|
394
|
-
|
|
430
|
+
const type = raw[HOST_READABLE_KEYS.objectType];
|
|
431
|
+
const known = ['rect', 'ellipse', 'path', 'text', 'image', 'widget', 'group'];
|
|
395
432
|
if (typeof type !== 'string' || !known.includes(type)) {
|
|
396
|
-
warnings.push({ code: 'unknown-type', index
|
|
433
|
+
warnings.push({ code: 'unknown-type', index, detail: typeof type === 'string' ? type : String(type) });
|
|
397
434
|
return null;
|
|
398
435
|
}
|
|
399
|
-
|
|
436
|
+
const id = readId(raw, seen);
|
|
400
437
|
switch (type) {
|
|
401
438
|
case 'rect':
|
|
402
439
|
return readRect(raw, id);
|
|
@@ -414,28 +451,27 @@ function readObject(raw, index, seen, warnings) {
|
|
|
414
451
|
return readGroup(raw, id);
|
|
415
452
|
}
|
|
416
453
|
}
|
|
417
|
-
|
|
454
|
+
const EMPTY_VIEWPORT = { x: 0, y: 0, zoom: 1 };
|
|
418
455
|
/**
|
|
419
456
|
* Reads a saved canvas: the text `serializeCanvasDocument` wrote, or the same thing already parsed.
|
|
420
457
|
* Never throws.
|
|
421
458
|
*/
|
|
422
459
|
export function parseCanvasDocument(input) {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
var nothing = function (status) { return ({
|
|
460
|
+
const warnings = [];
|
|
461
|
+
const nothing = (status) => ({
|
|
426
462
|
scene: [],
|
|
427
|
-
viewport:
|
|
428
|
-
status
|
|
429
|
-
warnings
|
|
430
|
-
});
|
|
463
|
+
viewport: { ...EMPTY_VIEWPORT },
|
|
464
|
+
status,
|
|
465
|
+
warnings
|
|
466
|
+
});
|
|
431
467
|
if (input === null || input === undefined || (typeof input === 'string' && input.trim() === ''))
|
|
432
468
|
return nothing('empty');
|
|
433
|
-
|
|
469
|
+
let document = input;
|
|
434
470
|
if (typeof input === 'string') {
|
|
435
471
|
try {
|
|
436
472
|
document = JSON.parse(input);
|
|
437
473
|
}
|
|
438
|
-
catch
|
|
474
|
+
catch {
|
|
439
475
|
warnings.push({ code: 'not-json' });
|
|
440
476
|
return nothing('invalid');
|
|
441
477
|
}
|
|
@@ -445,19 +481,19 @@ export function parseCanvasDocument(input) {
|
|
|
445
481
|
return nothing('invalid');
|
|
446
482
|
}
|
|
447
483
|
if (document.version !== CANVAS_DOCUMENT_VERSION) {
|
|
448
|
-
warnings.push({ code: 'unsupported-version', detail: String(
|
|
484
|
+
warnings.push({ code: 'unsupported-version', detail: String(document.version ?? 'none') });
|
|
449
485
|
return nothing('unsupported');
|
|
450
486
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
487
|
+
const canvas = document.canvas;
|
|
488
|
+
const list = isRecord(canvas) && Array.isArray(canvas.objects) ? canvas.objects : [];
|
|
489
|
+
const limited = list.length > MAX_OBJECTS_PER_DOCUMENT ? list.slice(0, MAX_OBJECTS_PER_DOCUMENT) : list;
|
|
454
490
|
if (limited.length < list.length) {
|
|
455
|
-
warnings.push({ code: 'too-many-objects', detail:
|
|
491
|
+
warnings.push({ code: 'too-many-objects', detail: `${list.length} objects, the first ${limited.length} were read.` });
|
|
456
492
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
limited.forEach(
|
|
460
|
-
|
|
493
|
+
const seen = new Set();
|
|
494
|
+
const objects = [];
|
|
495
|
+
limited.forEach((raw, index) => {
|
|
496
|
+
const object = readObject(raw, index, seen, warnings);
|
|
461
497
|
if (object)
|
|
462
498
|
objects.push(object);
|
|
463
499
|
});
|
|
@@ -466,6 +502,6 @@ export function parseCanvasDocument(input) {
|
|
|
466
502
|
scene: dropDanglingBindings(normalizeScene(objects)),
|
|
467
503
|
viewport: sanitizeViewport(document.viewport),
|
|
468
504
|
status: 'ok',
|
|
469
|
-
warnings
|
|
505
|
+
warnings
|
|
470
506
|
};
|
|
471
507
|
}
|
|
@@ -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. */
|