@open-artifact/whiteboard-core 0.2.2 → 0.9.0
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/package.json +4 -4
- package/src/board-file.js +35 -2
- package/src/clipboard.d.ts +1 -1
- package/src/clipboard.js +6 -5
- package/src/image-source.js +2 -0
- package/src/links.js +6 -3
- package/src/normalize.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/whiteboard-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "The Whiteboard artifact model: element schema and normalization, the document codec, geometry, arrows, hand-drawn ink, history, view context and the live sync engine.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"NOTICE"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@open-artifact/live-store": "0.
|
|
20
|
-
"@open-artifact/protocol": "0.
|
|
21
|
-
"@open-artifact/wasm-wb-geom": "0.
|
|
19
|
+
"@open-artifact/live-store": "0.9.0",
|
|
20
|
+
"@open-artifact/protocol": "0.9.0",
|
|
21
|
+
"@open-artifact/wasm-wb-geom": "0.9.0"
|
|
22
22
|
},
|
|
23
23
|
"license": "Apache-2.0",
|
|
24
24
|
"author": {
|
package/src/board-file.js
CHANGED
|
@@ -37,13 +37,46 @@ const UNPAIRED_SURROGATE_PATTERN = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD8
|
|
|
37
37
|
function replaceUnpairedSurrogates(text) {
|
|
38
38
|
return text.replace(UNPAIRED_SURROGATE_PATTERN, '\uFFFD');
|
|
39
39
|
}
|
|
40
|
+
/** Remove a share parameter without changing the other query parameters or their escaping. */
|
|
41
|
+
function withoutShareKeys(word) {
|
|
42
|
+
return word.replace(/([?&#;])(?:sk=[A-Za-z0-9_-]{16,64}&)+/gi, '$1')
|
|
43
|
+
.replace(/[?&#;]sk=[A-Za-z0-9_-]{16,64}(?![A-Za-z0-9_-])/gi, '')
|
|
44
|
+
.replace(/(^|[^A-Za-z0-9])sk=[A-Za-z0-9_-]{16,64}(?![A-Za-z0-9_-])/gi, '$1');
|
|
45
|
+
}
|
|
46
|
+
/** Links in board text can also be URL- or HTML-encoded. Keep ordinary text byte-for-byte. */
|
|
47
|
+
function scrubExportLinks(text) {
|
|
48
|
+
return text.replace(/\S+/g, (word) => {
|
|
49
|
+
const clean = withoutShareKeys(word);
|
|
50
|
+
if (!/[%&]/.test(clean))
|
|
51
|
+
return clean;
|
|
52
|
+
let decoded = clean;
|
|
53
|
+
for (let round = 0; round < 8; round++) {
|
|
54
|
+
const next = decoded
|
|
55
|
+
.replace(/(?:%[0-9a-f]{2})+/gi, (bytes) => {
|
|
56
|
+
try {
|
|
57
|
+
return decodeURIComponent(bytes);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return bytes;
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
.replace(/&#(?:x([0-9a-f]{1,6})|(\d{1,7}));/gi, (_, hex, decimal) => String.fromCodePoint(Math.min(0x10ffff, hex ? parseInt(hex, 16) : Number(decimal))))
|
|
64
|
+
.replace(/&/gi, '&');
|
|
65
|
+
if (decoded === next)
|
|
66
|
+
break;
|
|
67
|
+
decoded = next;
|
|
68
|
+
}
|
|
69
|
+
const scrubbed = withoutShareKeys(decoded);
|
|
70
|
+
return scrubbed === decoded ? clean : scrubbed;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
40
73
|
/**
|
|
41
74
|
* `JSON.stringify` replacer for board files: strings (other than image data
|
|
42
75
|
* URLs, kept as they are) lose unpaired surrogates, so every reader parses
|
|
43
76
|
* the file the same way.
|
|
44
77
|
*/
|
|
45
78
|
export function boardFileJsonReplacer(_key, value) {
|
|
46
|
-
return typeof value === 'string' && !IMAGE_DATA_URL_PATTERN.test(value) ? replaceUnpairedSurrogates(value) : value;
|
|
79
|
+
return typeof value === 'string' && !IMAGE_DATA_URL_PATTERN.test(value) ? replaceUnpairedSurrogates(scrubExportLinks(value)) : value;
|
|
47
80
|
}
|
|
48
81
|
/** The title of an HTML document (its `<title>`, whitespace collapsed), if any. */
|
|
49
82
|
export function htmlDocumentTitle(html) {
|
|
@@ -107,7 +140,7 @@ export function serializeBoardFile(file) {
|
|
|
107
140
|
}
|
|
108
141
|
/** The download name of a board file: its title (at most 100 characters) plus `.json`. */
|
|
109
142
|
export function boardFileName(title) {
|
|
110
|
-
const stem = [...title.trim()].slice(0, BOARD_FILE_NAME_MAX_CHARS).join('').trim();
|
|
143
|
+
const stem = [...scrubExportLinks(title).trim()].slice(0, BOARD_FILE_NAME_MAX_CHARS).join('').trim();
|
|
111
144
|
return `${stem !== '' ? stem : BOARD_FILE_DEFAULT_TITLE}.json`;
|
|
112
145
|
}
|
|
113
146
|
/**
|
package/src/clipboard.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { type BoardElement } from './schema.ts';
|
|
|
16
16
|
/** The payload kind whiteboard copies carry. */
|
|
17
17
|
export declare const BOARD_CLIPBOARD_KIND = "whiteboard-elements";
|
|
18
18
|
/** The HTML attribute holding an artifact clipboard payload. */
|
|
19
|
-
export declare const ARTIFACT_CLIPBOARD_ATTRIBUTE = "data-
|
|
19
|
+
export declare const ARTIFACT_CLIPBOARD_ATTRIBUTE = "data-appifact-object";
|
|
20
20
|
/** Finds the payload attribute in clipboard HTML; group 1 is the base64 payload. */
|
|
21
21
|
export declare const ARTIFACT_CLIPBOARD_PATTERN: RegExp;
|
|
22
22
|
/** The version marker inside every payload (bumped on incompatible changes). */
|
package/src/clipboard.js
CHANGED
|
@@ -21,9 +21,9 @@ import { stripInlineMarkers, stripMarkdownLinks, tableToTsv } from "./table.js";
|
|
|
21
21
|
/** The payload kind whiteboard copies carry. */
|
|
22
22
|
export const BOARD_CLIPBOARD_KIND = 'whiteboard-elements';
|
|
23
23
|
/** The HTML attribute holding an artifact clipboard payload. */
|
|
24
|
-
export const ARTIFACT_CLIPBOARD_ATTRIBUTE = 'data-
|
|
24
|
+
export const ARTIFACT_CLIPBOARD_ATTRIBUTE = 'data-appifact-object';
|
|
25
25
|
/** Finds the payload attribute in clipboard HTML; group 1 is the base64 payload. */
|
|
26
|
-
export const ARTIFACT_CLIPBOARD_PATTERN =
|
|
26
|
+
export const ARTIFACT_CLIPBOARD_PATTERN = /\bdata-(?:open-artifact|appifact)-object="([A-Za-z0-9+/=]+)"/;
|
|
27
27
|
/** The version marker inside every payload (bumped on incompatible changes). */
|
|
28
28
|
export const ARTIFACT_CLIPBOARD_VERSION = 1;
|
|
29
29
|
/** Bytes per `String.fromCharCode` call, so large payloads do not overflow the stack. */
|
|
@@ -59,8 +59,9 @@ export function escapeClipboardHtml(text) {
|
|
|
59
59
|
* {@link ARTIFACT_CLIPBOARD_ATTRIBUTE} and showing `text`.
|
|
60
60
|
*/
|
|
61
61
|
export function artifactClipboardHtml(payload, text) {
|
|
62
|
-
const stored = { openArtifact: ARTIFACT_CLIPBOARD_VERSION, kind: payload.kind, data: payload.data };
|
|
63
|
-
|
|
62
|
+
const stored = { appifact: ARTIFACT_CLIPBOARD_VERSION, openArtifact: ARTIFACT_CLIPBOARD_VERSION, kind: payload.kind, data: payload.data };
|
|
63
|
+
const encoded = encodeClipboardUtf8(JSON.stringify(stored));
|
|
64
|
+
return `<span ${ARTIFACT_CLIPBOARD_ATTRIBUTE}="${encoded}" data-open-artifact-object="${encoded}">${escapeClipboardHtml(text)}</span>`;
|
|
64
65
|
}
|
|
65
66
|
/** The payload inside clipboard HTML, or `undefined` when it carries none (or a broken one). */
|
|
66
67
|
export function parseArtifactClipboardHtml(html) {
|
|
@@ -75,7 +76,7 @@ export function parseArtifactClipboardHtml(html) {
|
|
|
75
76
|
if (typeof value !== 'object' || value === null)
|
|
76
77
|
return undefined;
|
|
77
78
|
const record = value;
|
|
78
|
-
if (record.openArtifact !== ARTIFACT_CLIPBOARD_VERSION || typeof record.kind !== 'string' || !Object.hasOwn(record, 'data'))
|
|
79
|
+
if ((record.appifact ?? record.openArtifact) !== ARTIFACT_CLIPBOARD_VERSION || typeof record.kind !== 'string' || !Object.hasOwn(record, 'data'))
|
|
79
80
|
return undefined;
|
|
80
81
|
return { kind: record.kind, data: record.data };
|
|
81
82
|
}
|
package/src/image-source.js
CHANGED
|
@@ -154,6 +154,8 @@ export function isAllowedImageSource(src) {
|
|
|
154
154
|
/** The storable form of an image source, or `undefined` when it is not allowed. */
|
|
155
155
|
export function normalizeImageSource(src) {
|
|
156
156
|
const normalized = typeof src === 'string' ? (svgTextToBase64DataUrl(src) ?? src) : '';
|
|
157
|
+
if (isUploadedBlobSource(normalized))
|
|
158
|
+
return normalized.startsWith('/') ? normalized : '/' + normalized;
|
|
157
159
|
return isAllowedImageSource(normalized) ? normalized : undefined;
|
|
158
160
|
}
|
|
159
161
|
/** True when the image is an SVG drawing (not a photo or upload). */
|
package/src/links.js
CHANGED
|
@@ -34,8 +34,11 @@ export function normalizeHttpUrl(href, maxChars = MAX_IMAGE_HREF_CHARS) {
|
|
|
34
34
|
export function normalizeArtifactAddress(href) {
|
|
35
35
|
if (typeof href !== 'string')
|
|
36
36
|
return undefined;
|
|
37
|
-
const
|
|
38
|
-
|
|
37
|
+
const text = href.trim();
|
|
38
|
+
if (text.length > MAX_LINK_HREF_CHARS || /[\s\p{Cc}]/u.test(text))
|
|
39
|
+
return undefined;
|
|
40
|
+
const match = /^open-artifact:\/\/artifact\/([^/?#]+)$/.exec(text);
|
|
41
|
+
return match !== null && ARTIFACT_ID.test(match[1]) ? text : undefined;
|
|
39
42
|
}
|
|
40
43
|
/** A link card's address: an artifact address or a web URL. */
|
|
41
44
|
export function normalizeLinkHref(href) {
|
|
@@ -43,7 +46,7 @@ export function normalizeLinkHref(href) {
|
|
|
43
46
|
}
|
|
44
47
|
/** True when the card points at another artifact. */
|
|
45
48
|
export function isArtifactLink(href) {
|
|
46
|
-
return href
|
|
49
|
+
return normalizeArtifactAddress(href) !== undefined;
|
|
47
50
|
}
|
|
48
51
|
/** A card title: whitespace collapsed, invisible characters dropped, capped. */
|
|
49
52
|
export function sanitizeLinkTitle(title) {
|
package/src/normalize.js
CHANGED
|
@@ -304,7 +304,7 @@ function normalizeElementFields(raw, takenIds) {
|
|
|
304
304
|
if (src === undefined)
|
|
305
305
|
return undefined;
|
|
306
306
|
const crop = normalizeImageCrop(input.crop);
|
|
307
|
-
const video = typeof input.video === 'string' && isUploadedBlobSource(input.video) ? input.video : undefined;
|
|
307
|
+
const video = typeof input.video === 'string' && isUploadedBlobSource(input.video) ? normalizeImageSource(input.video) : undefined;
|
|
308
308
|
const href = video === undefined ? normalizeHttpUrl(input.href) : undefined;
|
|
309
309
|
return {
|
|
310
310
|
type: 'image',
|