@open-artifact/dc 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +53 -4
- package/src/check.d.ts +30 -0
- package/src/check.js +337 -0
- package/src/document.d.ts +63 -0
- package/src/document.js +126 -0
- package/src/edit.d.ts +72 -0
- package/src/edit.js +249 -0
- package/src/holes.d.ts +46 -0
- package/src/holes.js +91 -0
- package/src/html.d.ts +113 -0
- package/src/html.js +335 -0
- package/src/index.d.ts +16 -0
- package/src/index.js +15 -0
- package/src/props.d.ts +51 -0
- package/src/props.js +57 -0
- package/src/protocol.d.ts +266 -0
- package/src/protocol.js +16 -0
- package/src/render.d.ts +34 -0
- package/src/render.js +256 -0
- package/src/rich-text.d.ts +106 -0
- package/src/rich-text.js +515 -0
- package/src/runtime/board.d.ts +6 -0
- package/src/runtime/board.js +509 -0
- package/src/runtime/focus-guard.d.ts +18 -0
- package/src/runtime/focus-guard.js +40 -0
- package/src/runtime/index.d.ts +16 -0
- package/src/runtime/index.js +15 -0
- package/src/runtime/logic.d.ts +31 -0
- package/src/runtime/logic.js +47 -0
- package/src/runtime/text-edit.d.ts +121 -0
- package/src/runtime/text-edit.js +1251 -0
- package/src/style.d.ts +30 -0
- package/src/style.js +108 -0
- package/src/template.d.ts +80 -0
- package/src/template.js +186 -0
package/src/rich-text.js
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rich text in templates in-place text editing:
|
|
3
|
+
*
|
|
4
|
+
* - which elements are edited in place ({@link isRichTextElement}): their
|
|
5
|
+
* content is words and inline formatting (`<b>`, `<i>`, `<a>`, `<br>`…);
|
|
6
|
+
* - cleaning the HTML an edit produced before it is written back
|
|
7
|
+
* ({@link sanitizeTextHtml});
|
|
8
|
+
* - the string literals of a logic script ({@link scriptLiterals}), so text
|
|
9
|
+
* a script supplies can be edited where it is written.
|
|
10
|
+
*/
|
|
11
|
+
import { RAW_TEXT_ELEMENTS, VOID_ELEMENTS, attributeValue, parseHtml } from "./html.js";
|
|
12
|
+
import { hasHoles } from "./holes.js";
|
|
13
|
+
/** Inline formatting an editable text may hold. */
|
|
14
|
+
export const INLINE_TEXT_TAGS = new Set('a abbr b bdi bdo br cite code del dfn em i ins kbd mark q s samp small span strike strong sub sup u var wbr'.split(' '));
|
|
15
|
+
/** Tags whose content is never edited as text. */
|
|
16
|
+
const NOT_TEXT = new Set([
|
|
17
|
+
...VOID_ELEMENTS,
|
|
18
|
+
...RAW_TEXT_ELEMENTS,
|
|
19
|
+
'helmet', 'sc-for', 'sc-if', 'dc-import', 'x-import', 'svg', 'canvas', 'iframe', 'video', 'audio', 'select',
|
|
20
|
+
'option', 'noscript', 'template', 'xmp', 'noembed', 'noframes', 'plaintext', 'object', 'embed', 'math',
|
|
21
|
+
]);
|
|
22
|
+
/** Tags that may be edited while still empty (a fresh text box). */
|
|
23
|
+
const EMPTY_TEXT_TAGS = new Set('p h1 h2 h3 h4 h5 h6 span li dt dd td th blockquote q cite label button figcaption caption code pre em strong b i u s small mark sub sup a'.split(' '));
|
|
24
|
+
/** Display values that take a link out of the text flow. */
|
|
25
|
+
const BOXED_DISPLAY = /^(block|flex|grid|flow-root|list-item|table|inline-(block|flex|grid))$/;
|
|
26
|
+
/** Private-use characters: icon-font glyphs, not words. */
|
|
27
|
+
const ICON_GLYPHS = /[\uE000-\uF8FF\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}]/gu;
|
|
28
|
+
/** The `display` an element's inline style sets, or `''`. */
|
|
29
|
+
function inlineDisplay(element) {
|
|
30
|
+
const match = /(?:^|;)\s*display\s*:\s*([^;!]+)/i.exec(attributeValue(element, 'style') ?? '');
|
|
31
|
+
return match ? match[1].trim().toLowerCase() : '';
|
|
32
|
+
}
|
|
33
|
+
/** The text an element's subtree spells out in the template (holes included as written). */
|
|
34
|
+
function templateText(node) {
|
|
35
|
+
if (node.type === 'text')
|
|
36
|
+
return node.value;
|
|
37
|
+
if (node.type === 'comment')
|
|
38
|
+
return '';
|
|
39
|
+
return node.children.map(templateText).join('');
|
|
40
|
+
}
|
|
41
|
+
/** True when the element is inside an `<svg>` (or is one). */
|
|
42
|
+
function inSvg(element) {
|
|
43
|
+
for (let node = element; node; node = node.parent)
|
|
44
|
+
if (node.tag === 'svg')
|
|
45
|
+
return true;
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
/** An inline element that is words in the flow (a boxed link counts only when it is inline). */
|
|
49
|
+
function inlineChild(element) {
|
|
50
|
+
if (!INLINE_TEXT_TAGS.has(element.tag))
|
|
51
|
+
return false;
|
|
52
|
+
if (element.tag !== 'a')
|
|
53
|
+
return true;
|
|
54
|
+
const display = inlineDisplay(element);
|
|
55
|
+
return !BOXED_DISPLAY.test(display) || display.startsWith('inline-');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* An icon-and-label wrapper: no words of its own, several
|
|
59
|
+
* inline children of which at most one has words. Its label is edited, not
|
|
60
|
+
* the wrapper.
|
|
61
|
+
*/
|
|
62
|
+
function iconLabelWrapper(element) {
|
|
63
|
+
if (element.children.some((child) => child.type === 'text' && child.value.trim() !== ''))
|
|
64
|
+
return false;
|
|
65
|
+
const kids = element.children.filter((child) => child.type === 'element' && child.tag !== 'br' && child.tag !== 'wbr');
|
|
66
|
+
const worded = kids.filter((kid) => templateText(kid).replace(ICON_GLYPHS, '').trim() !== '');
|
|
67
|
+
return (kids.length > 1 && worded.length <= 1) || kids.some(iconLabelWrapper);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* True when the element's content is edited in place as rich text: words
|
|
71
|
+
* and inline formatting only, written in the template (holes allowed: they
|
|
72
|
+
* are edited as written).
|
|
73
|
+
*/
|
|
74
|
+
export function isRichTextElement(element) {
|
|
75
|
+
if (NOT_TEXT.has(element.tag) || element.closeStart < 0)
|
|
76
|
+
return false;
|
|
77
|
+
// In a drawing, a run of text (`<text>`, `<tspan>`, `<textPath>`) holding words only.
|
|
78
|
+
if (inSvg(element))
|
|
79
|
+
return isSvgText(element);
|
|
80
|
+
const text = templateText(element);
|
|
81
|
+
if (text.trim() === '')
|
|
82
|
+
return EMPTY_TEXT_TAGS.has(element.tag) && element.children.every((child) => child.type !== 'element' || child.tag === 'br');
|
|
83
|
+
const inline = (nodes) => nodes.every((node) => node.type !== 'element' || (inlineChild(node) && inline(node.children)));
|
|
84
|
+
if (!inline(element.children))
|
|
85
|
+
return false;
|
|
86
|
+
// Inside code, words are code: edit them as a whole.
|
|
87
|
+
let code = false;
|
|
88
|
+
for (let node = element; node; node = node.parent)
|
|
89
|
+
if (node.tag === 'pre' || node.tag === 'code')
|
|
90
|
+
code = true;
|
|
91
|
+
return code || !iconLabelWrapper(element);
|
|
92
|
+
}
|
|
93
|
+
/** SVG elements whose words are edited in place. */
|
|
94
|
+
const SVG_TEXT_TAGS = new Set(['text', 'tspan', 'textpath']);
|
|
95
|
+
/** True for an SVG text element holding words and nothing else: edited as plain text. */
|
|
96
|
+
export function isSvgText(element) {
|
|
97
|
+
return (SVG_TEXT_TAGS.has(element.tag) &&
|
|
98
|
+
element.closeStart >= 0 &&
|
|
99
|
+
inSvg(element) &&
|
|
100
|
+
element.children.every((child) => child.type !== 'element') &&
|
|
101
|
+
templateText(element).trim() !== '');
|
|
102
|
+
}
|
|
103
|
+
/** True when text inside the element has `{{ … }}` holes. */
|
|
104
|
+
export function hasTextHoles(element) {
|
|
105
|
+
const walk = (nodes) => nodes.some((node) => (node.type === 'text' ? hasHoles(node.value) : node.type === 'element' && walk(node.children)));
|
|
106
|
+
return walk(element.children);
|
|
107
|
+
}
|
|
108
|
+
/* -------------------------------------------------------------------------- */
|
|
109
|
+
/* Sanitizing */
|
|
110
|
+
/* -------------------------------------------------------------------------- */
|
|
111
|
+
/** Elements dropped with their content. */
|
|
112
|
+
const DROPPED = new Set([
|
|
113
|
+
'script', 'style', 'xmp', 'iframe', 'noembed', 'noframes', 'plaintext', 'noscript', 'template', 'object', 'embed',
|
|
114
|
+
'link', 'meta', 'base', 'title', 'frame', 'frameset', 'form', 'input', 'textarea', 'select', 'button', 'dialog',
|
|
115
|
+
]);
|
|
116
|
+
/** Attributes an edit's HTML never keeps. */
|
|
117
|
+
const DROPPED_ATTRIBUTES = /^(on|sc-|data-dc-|data-sc-)|^(is|contenteditable|spellcheck|autofocus|srcdoc|formaction|action)$/i;
|
|
118
|
+
/** Attributes that hold an address. */
|
|
119
|
+
const URL_ATTRIBUTES = new Set(['href', 'src', 'xlink:href', 'action', 'poster', 'background', 'cite']);
|
|
120
|
+
/** True for an address that runs code. */
|
|
121
|
+
function unsafeUrl(value) {
|
|
122
|
+
// eslint-disable-next-line no-control-regex
|
|
123
|
+
const bare = value.replace(/[\u0000-\u0020\u007f-\u009f]/g, '').toLowerCase();
|
|
124
|
+
return /^(javascript|vbscript|data:text\/html)/.test(bare) || bare.startsWith('data:') && !/^data:image\/(png|jpe?g|gif|webp|avif);/.test(bare);
|
|
125
|
+
}
|
|
126
|
+
/** Escapes text content. */
|
|
127
|
+
const escapeHtmlText = (text) => text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/\u00a0/g, ' ');
|
|
128
|
+
/** Serializes one attribute, double-quoted unless the value holds `"`. */
|
|
129
|
+
function attributeText(name, value) {
|
|
130
|
+
if (value === null)
|
|
131
|
+
return ` ${name}`;
|
|
132
|
+
const amp = value.replace(/&/g, '&');
|
|
133
|
+
return amp.includes('"') && !amp.includes("'") ? ` ${name}='${amp}'` : ` ${name}="${amp.replace(/"/g, '"')}"`;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Cleans HTML an in-place edit produced (`innerHTML` of the edited
|
|
137
|
+
* element) for writing into the template: comments, scripts, frames, form
|
|
138
|
+
* controls, event handlers, code-running addresses and the editor's own
|
|
139
|
+
* attributes go; the words and their formatting stay. A trailing `<br>`
|
|
140
|
+
* the browser keeps for the caret goes too.
|
|
141
|
+
*/
|
|
142
|
+
export function sanitizeTextHtml(html) {
|
|
143
|
+
const out = [];
|
|
144
|
+
const walk = (nodes) => {
|
|
145
|
+
for (const node of nodes) {
|
|
146
|
+
if (node.type === 'comment')
|
|
147
|
+
continue;
|
|
148
|
+
if (node.type === 'text') {
|
|
149
|
+
out.push(escapeHtmlText(node.value));
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const tag = node.tag;
|
|
153
|
+
if (DROPPED.has(tag) || tag.includes(':'))
|
|
154
|
+
continue;
|
|
155
|
+
let attrs = '';
|
|
156
|
+
for (const attr of node.attrs) {
|
|
157
|
+
const name = attr.name.toLowerCase();
|
|
158
|
+
if (DROPPED_ATTRIBUTES.test(name))
|
|
159
|
+
continue;
|
|
160
|
+
if (name === 'role' || name === 'aria-label' || name === 'aria-multiline')
|
|
161
|
+
continue;
|
|
162
|
+
if (URL_ATTRIBUTES.has(name) && attr.value !== null && unsafeUrl(attr.value))
|
|
163
|
+
continue;
|
|
164
|
+
if (name === 'style' && attr.value !== null && /expression\s*\(|javascript:|-moz-binding/i.test(attr.value))
|
|
165
|
+
continue;
|
|
166
|
+
attrs += attributeText(attr.name, attr.value);
|
|
167
|
+
}
|
|
168
|
+
out.push(`<${tag}${attrs}>`);
|
|
169
|
+
if (VOID_ELEMENTS.has(tag))
|
|
170
|
+
continue;
|
|
171
|
+
walk(node.children);
|
|
172
|
+
out.push(`</${tag}>`);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
walk(parseHtml(html));
|
|
176
|
+
let result = out.join('');
|
|
177
|
+
// Chrome keeps a `<br>` after the last line so the caret has a place.
|
|
178
|
+
if (/<br>$/.test(result) && result !== '<br>' && !/<br><br>$/.test(result))
|
|
179
|
+
result = result.slice(0, -4);
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
/** Block content in an edit's HTML (a list, a paragraph): a `<p>` holding it must become a `<div>`. */
|
|
183
|
+
export function hasBlockContent(html) {
|
|
184
|
+
return /<(ul|ol|li|div|p|h[1-6]|blockquote|pre|table)\b/i.test(html);
|
|
185
|
+
}
|
|
186
|
+
const ESCAPES = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', v: '\v', 0: '\0' };
|
|
187
|
+
/** Decodes the body of a JS string literal; `null` when it uses escapes we do not read. */
|
|
188
|
+
function decodeLiteral(body) {
|
|
189
|
+
let out = '';
|
|
190
|
+
for (let i = 0; i < body.length; i++) {
|
|
191
|
+
const ch = body[i];
|
|
192
|
+
if (ch !== '\\') {
|
|
193
|
+
out += ch;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const next = body[++i];
|
|
197
|
+
if (next === undefined)
|
|
198
|
+
return null;
|
|
199
|
+
if (next === 'u') {
|
|
200
|
+
const braced = /^\{([0-9a-fA-F]{1,6})\}/.exec(body.slice(i + 1));
|
|
201
|
+
const plain = /^[0-9a-fA-F]{4}/.exec(body.slice(i + 1));
|
|
202
|
+
if (braced) {
|
|
203
|
+
out += String.fromCodePoint(parseInt(braced[1], 16));
|
|
204
|
+
i += braced[0].length;
|
|
205
|
+
}
|
|
206
|
+
else if (plain) {
|
|
207
|
+
out += String.fromCharCode(parseInt(plain[0], 16));
|
|
208
|
+
i += 4;
|
|
209
|
+
}
|
|
210
|
+
else
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
else if (next === 'x') {
|
|
214
|
+
const hex = /^[0-9a-fA-F]{2}/.exec(body.slice(i + 1));
|
|
215
|
+
if (!hex)
|
|
216
|
+
return null;
|
|
217
|
+
out += String.fromCharCode(parseInt(hex[0], 16));
|
|
218
|
+
i += 2;
|
|
219
|
+
}
|
|
220
|
+
else if (next === '\n') {
|
|
221
|
+
// A line continuation.
|
|
222
|
+
}
|
|
223
|
+
else if (next === '\r') {
|
|
224
|
+
if (body[i + 1] === '\n')
|
|
225
|
+
i++;
|
|
226
|
+
}
|
|
227
|
+
else
|
|
228
|
+
out += ESCAPES[next] ?? next;
|
|
229
|
+
}
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* The string literals of a script, in order. Comments and regular
|
|
234
|
+
* expressions are skipped (a `/` after a value is division); template
|
|
235
|
+
* literals with `${}` are not literals.
|
|
236
|
+
*/
|
|
237
|
+
export function scriptLiterals(js) {
|
|
238
|
+
const out = [];
|
|
239
|
+
let i = 0;
|
|
240
|
+
// Whether a `/` here starts a regular expression (no value before it).
|
|
241
|
+
let regexAllowed = true;
|
|
242
|
+
while (i < js.length) {
|
|
243
|
+
const ch = js[i];
|
|
244
|
+
if (ch === '/' && js[i + 1] === '/') {
|
|
245
|
+
const end = js.indexOf('\n', i);
|
|
246
|
+
i = end < 0 ? js.length : end;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (ch === '/' && js[i + 1] === '*') {
|
|
250
|
+
const end = js.indexOf('*/', i + 2);
|
|
251
|
+
i = end < 0 ? js.length : end + 2;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (ch === '"' || ch === "'" || ch === '`') {
|
|
255
|
+
let j = i + 1;
|
|
256
|
+
let dynamic = false;
|
|
257
|
+
while (j < js.length && js[j] !== ch) {
|
|
258
|
+
if (js[j] === '\\')
|
|
259
|
+
j++;
|
|
260
|
+
else if (ch === '`' && js[j] === '$' && js[j + 1] === '{') {
|
|
261
|
+
dynamic = true;
|
|
262
|
+
// Skip the substitution, counting braces.
|
|
263
|
+
let depth = 0;
|
|
264
|
+
for (j += 1; j < js.length; j++) {
|
|
265
|
+
if (js[j] === '{')
|
|
266
|
+
depth++;
|
|
267
|
+
else if (js[j] === '}' && --depth === 0)
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
else if (ch !== '`' && js[j] === '\n')
|
|
272
|
+
break;
|
|
273
|
+
j++;
|
|
274
|
+
}
|
|
275
|
+
const end = Math.min(j + 1, js.length);
|
|
276
|
+
if (!dynamic && js[j] === ch) {
|
|
277
|
+
const value = decodeLiteral(js.slice(i + 1, j));
|
|
278
|
+
if (value !== null)
|
|
279
|
+
out.push({ start: i, end, quote: ch, value });
|
|
280
|
+
}
|
|
281
|
+
i = end;
|
|
282
|
+
regexAllowed = false;
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (ch === '/' && regexAllowed) {
|
|
286
|
+
let j = i + 1;
|
|
287
|
+
let inClass = false;
|
|
288
|
+
while (j < js.length && js[j] !== '\n') {
|
|
289
|
+
if (js[j] === '\\')
|
|
290
|
+
j++;
|
|
291
|
+
else if (js[j] === '[')
|
|
292
|
+
inClass = true;
|
|
293
|
+
else if (js[j] === ']')
|
|
294
|
+
inClass = false;
|
|
295
|
+
else if (js[j] === '/' && !inClass)
|
|
296
|
+
break;
|
|
297
|
+
j++;
|
|
298
|
+
}
|
|
299
|
+
i = j + 1;
|
|
300
|
+
while (i < js.length && /[a-z]/i.test(js[i]))
|
|
301
|
+
i++;
|
|
302
|
+
regexAllowed = false;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (/\s/.test(ch)) {
|
|
306
|
+
i++;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
if (/[\w$]/.test(ch)) {
|
|
310
|
+
let j = i;
|
|
311
|
+
while (j < js.length && /[\w$]/.test(js[j]))
|
|
312
|
+
j++;
|
|
313
|
+
const word = js.slice(i, j);
|
|
314
|
+
regexAllowed = /^(return|typeof|instanceof|in|of|new|delete|void|throw|case|do|else|yield|await)$/.test(word);
|
|
315
|
+
i = j;
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
regexAllowed = !/[)\]}]/.test(ch);
|
|
319
|
+
i++;
|
|
320
|
+
}
|
|
321
|
+
return out;
|
|
322
|
+
}
|
|
323
|
+
/** Writes text as a JS string literal with the given quote. */
|
|
324
|
+
export function quoteScriptString(text, quote) {
|
|
325
|
+
let body = text.replace(/\\/g, '\\\\');
|
|
326
|
+
if (quote === '`')
|
|
327
|
+
body = body.replace(/`/g, '\\`').replace(/\$\{/g, '\\${');
|
|
328
|
+
else
|
|
329
|
+
body = body.split(quote).join(`\\${quote}`).replace(/\r/g, '\\r').replace(/\n/g, '\\n');
|
|
330
|
+
body = body.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
|
|
331
|
+
// `</script>` inside would end the script block.
|
|
332
|
+
body = body.replace(/<\/(script)/gi, '<\\/$1');
|
|
333
|
+
return `${quote}${body}${quote}`;
|
|
334
|
+
}
|
|
335
|
+
/* -------------------------------------------------------------------------- */
|
|
336
|
+
/* Loose words */
|
|
337
|
+
/* -------------------------------------------------------------------------- */
|
|
338
|
+
/** Elements whose loose words are never wrapped. */
|
|
339
|
+
const NO_WRAP = new Set([...VOID_ELEMENTS, ...RAW_TEXT_ELEMENTS, 'helmet', 'x-import', 'dc-import', 'svg', 'math', 'select', 'option', 'template']);
|
|
340
|
+
/** An inline element that is words in the flow, all the way down. */
|
|
341
|
+
function inlineThroughout(element) {
|
|
342
|
+
return inlineChild(element) && element.children.every((child) => child.type !== 'element' || inlineThroughout(child));
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* The runs of loose words in an element that mixes words with other layout
|
|
346
|
+
* each run of text — with the inline formatting
|
|
347
|
+
* around it unless the element lays its children out as items (`inline`
|
|
348
|
+
* false) — trimmed of the spaces at its ends. `<sc-if>` / `<sc-for>`
|
|
349
|
+
* children are looked into. `text` is the template text.
|
|
350
|
+
*/
|
|
351
|
+
export function looseTextRuns(element, text, inline) {
|
|
352
|
+
if (NO_WRAP.has(element.tag) || inSvg(element) || element.closeStart < 0)
|
|
353
|
+
return [];
|
|
354
|
+
const worded = (node) => node.type === 'text' && node.value.trim() !== '';
|
|
355
|
+
const blank = (node) => (node.type === 'text' && node.value.trim() === '') || node.type === 'comment';
|
|
356
|
+
const phrasing = (node) => inline && node.type === 'element' && inlineThroughout(node);
|
|
357
|
+
const runs = [];
|
|
358
|
+
let run = [];
|
|
359
|
+
const close = () => {
|
|
360
|
+
while (run.length && blank(run[run.length - 1]))
|
|
361
|
+
run.pop();
|
|
362
|
+
if (run.some(worded) && run[0].start >= 0 && run[run.length - 1].end >= 0) {
|
|
363
|
+
const first = run[0];
|
|
364
|
+
const last = run[run.length - 1];
|
|
365
|
+
let start = first.start;
|
|
366
|
+
if (first.type === 'text')
|
|
367
|
+
start += text.slice(first.start, first.end).length - text.slice(first.start, first.end).trimStart().length;
|
|
368
|
+
let end = last.end;
|
|
369
|
+
if (last.type === 'text')
|
|
370
|
+
end -= text.slice(last.start, last.end).length - text.slice(last.start, last.end).trimEnd().length;
|
|
371
|
+
if (end > start)
|
|
372
|
+
runs.push({ start, end });
|
|
373
|
+
}
|
|
374
|
+
run = [];
|
|
375
|
+
};
|
|
376
|
+
for (const child of element.children) {
|
|
377
|
+
if (worded(child) || phrasing(child) || (blank(child) && run.length > 0))
|
|
378
|
+
run.push(child);
|
|
379
|
+
else
|
|
380
|
+
close();
|
|
381
|
+
}
|
|
382
|
+
close();
|
|
383
|
+
for (const child of element.children) {
|
|
384
|
+
if (child.type === 'element' && (child.tag === 'sc-if' || child.tag === 'sc-for'))
|
|
385
|
+
runs.push(...looseTextRuns(child, text, inline));
|
|
386
|
+
}
|
|
387
|
+
return runs;
|
|
388
|
+
}
|
|
389
|
+
/** Spaces squashed and trimmed, as text is drawn. */
|
|
390
|
+
export const squashSpaces = (text) => text.replace(/[ \t\n\r\f]+/g, ' ').replace(/^ | $/g, '');
|
|
391
|
+
/** Attributes of an instance that are never its words, plus code and addresses. */
|
|
392
|
+
const NOT_WORDS = /^(component(-from-global-scope|FromGlobalScope)?|from|dc-props|dcProps|sc-name|hint-size|hintSize|style-.*|data-(dc|om)-.*|class|style|id|slot|key|ref|href|src|srcset|srcdoc|on.*)$/;
|
|
393
|
+
/** A hole that is a literal: a number, a boolean or a quoted string. */
|
|
394
|
+
const LITERAL_HOLE = /^\s*\{\{\s*(?:(-?\d+(?:\.\d+)?|true|false)|(['"])([^{}]*)\2)\s*\}\}\s*$/;
|
|
395
|
+
/** A value that is a number or a boolean as written. */
|
|
396
|
+
const PLAIN_SCALAR = /^(-?\d+(\.\d+)?|true|false)$/;
|
|
397
|
+
/** Words that could be anything (numbers, separators). */
|
|
398
|
+
const NUMBERISH = /^-?[\d.,\s]+$/;
|
|
399
|
+
/** Longest words taken as an instance's own. */
|
|
400
|
+
export const MAX_IMPORT_WORDS = 2000;
|
|
401
|
+
/**
|
|
402
|
+
* The words of an `<x-import>` instance that are written on it — between
|
|
403
|
+
* its tags or in an attribute — and appear as-is where the component draws
|
|
404
|
+
* them. Words written twice, or that a hole
|
|
405
|
+
* could also be supplying (`known`: the script's strings and the props'
|
|
406
|
+
* values), are left out; so is everything when an attribute mixes words
|
|
407
|
+
* with holes.
|
|
408
|
+
*/
|
|
409
|
+
export function importTextTargets(element, known) {
|
|
410
|
+
if (element.tag !== 'x-import' || element.closeStart < 0)
|
|
411
|
+
return [];
|
|
412
|
+
const component = element.attrs.find((attr) => attr.name === 'component-from-global-scope' || attr.name === 'component')?.value ?? '';
|
|
413
|
+
if (!component || component.includes('{{'))
|
|
414
|
+
return [];
|
|
415
|
+
for (let node = element.parent; node; node = node.parent)
|
|
416
|
+
if (node.tag === 'sc-for')
|
|
417
|
+
return [];
|
|
418
|
+
const found = new Map();
|
|
419
|
+
const numbers = new Set();
|
|
420
|
+
let dynamic = false;
|
|
421
|
+
let mixed = false;
|
|
422
|
+
const add = (words, key) => {
|
|
423
|
+
if (words !== '')
|
|
424
|
+
found.set(words, found.has(words) ? null : key);
|
|
425
|
+
};
|
|
426
|
+
const consider = (key, value, kind) => {
|
|
427
|
+
const literal = LITERAL_HOLE.exec(value);
|
|
428
|
+
if (literal) {
|
|
429
|
+
const scalar = literal[1];
|
|
430
|
+
const words = squashSpaces(scalar ?? literal[3] ?? '');
|
|
431
|
+
const number = scalar != null && kind === 'attr' && key != null && /^-?\d/.test(scalar);
|
|
432
|
+
if (number)
|
|
433
|
+
numbers.add(key);
|
|
434
|
+
// Only a number can be retyped in a hole; other literals just claim their words.
|
|
435
|
+
add(words, number ? key : null);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (!value.includes('{{')) {
|
|
439
|
+
const words = squashSpaces(value);
|
|
440
|
+
add(words, words.length > MAX_IMPORT_WORDS ? null : key);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (kind !== 'handler')
|
|
444
|
+
dynamic = true;
|
|
445
|
+
const parts = value.split(/\{\{[^{}]*\}\}/);
|
|
446
|
+
if (kind === 'deep')
|
|
447
|
+
for (const part of parts)
|
|
448
|
+
add(squashSpaces(part), null);
|
|
449
|
+
else if (parts.length > 2 || parts.some((part) => /[^\s{}]/.test(part)))
|
|
450
|
+
mixed = true;
|
|
451
|
+
};
|
|
452
|
+
const own = [];
|
|
453
|
+
const deep = [];
|
|
454
|
+
const collect = (nodes, inside) => {
|
|
455
|
+
for (const node of nodes) {
|
|
456
|
+
if (node.type === 'text')
|
|
457
|
+
(inside ? deep : own).push(node.value);
|
|
458
|
+
else if (node.type === 'element')
|
|
459
|
+
collect(node.children, true);
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
collect(element.children, false);
|
|
463
|
+
if (own.filter((value) => value.trim() !== '').length > 1)
|
|
464
|
+
deep.push(...own);
|
|
465
|
+
// Words between its tags are retyped as its text only when nothing else is there.
|
|
466
|
+
consider(element.children.some((child) => child.type === 'element') ? null : '__text', own.join(''), 'own');
|
|
467
|
+
for (const value of deep)
|
|
468
|
+
consider(null, value, 'deep');
|
|
469
|
+
for (const attr of element.attrs) {
|
|
470
|
+
if (attr.value == null)
|
|
471
|
+
continue;
|
|
472
|
+
if (attr.name === 'dc-props' || attr.name === 'dcProps')
|
|
473
|
+
dynamic = true;
|
|
474
|
+
if (NOT_WORDS.test(attr.name)) {
|
|
475
|
+
if (/^on/.test(attr.name))
|
|
476
|
+
consider(null, attr.value, 'handler');
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
consider(PLAIN_SCALAR.test(attr.value.trim()) ? null : `@${attr.name}`, attr.value, 'attr');
|
|
480
|
+
}
|
|
481
|
+
if (mixed)
|
|
482
|
+
return [];
|
|
483
|
+
const out = [];
|
|
484
|
+
for (const [words, key] of found) {
|
|
485
|
+
if (key == null)
|
|
486
|
+
continue;
|
|
487
|
+
if (dynamic && (known == null || words.length < 2 || NUMBERISH.test(words) || known.has(words)))
|
|
488
|
+
continue;
|
|
489
|
+
out.push(numbers.has(key) ? { words, key, number: true } : { words, key });
|
|
490
|
+
}
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
/** Normalizes retyped words. */
|
|
494
|
+
export const retypedWords = (text) => text.replace(/[ \t\n\r\f ]*[ \t\n\r\f][ \t\n\r\f ]*/g, ' ').replace(/^[\s ]+|[\s ]+$/g, '');
|
|
495
|
+
/** Checks retyped words against the instance's targets now. */
|
|
496
|
+
export function importTextOutcome(target, text, targets) {
|
|
497
|
+
const found = targets.find((each) => each.key === target.key && each.words === target.words);
|
|
498
|
+
if (!found)
|
|
499
|
+
return { kind: 'refuse' };
|
|
500
|
+
if (text.length > 16 * 1024)
|
|
501
|
+
return { kind: 'toolong' };
|
|
502
|
+
const words = retypedWords(text);
|
|
503
|
+
if (words.length > MAX_IMPORT_WORDS)
|
|
504
|
+
return { kind: 'toolong' };
|
|
505
|
+
if (words === '' || words === target.words)
|
|
506
|
+
return { kind: 'noop' };
|
|
507
|
+
if (!found.number)
|
|
508
|
+
return { kind: 'commit', key: target.key, text: words };
|
|
509
|
+
if (!/^-?\d+(\.\d+)?$/.test(words))
|
|
510
|
+
return { kind: 'notnumber' };
|
|
511
|
+
const value = Number(words);
|
|
512
|
+
if (!Number.isFinite(value))
|
|
513
|
+
return { kind: 'notnumber' };
|
|
514
|
+
return value === Number(target.words) ? { kind: 'noop' } : { kind: 'commit', key: target.key, text: `{{ ${String(value)} }}` };
|
|
515
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The board runtime: renders one artboard inside its iframe and exposes
|
|
3
|
+
* `window.__dc` to the editor. Bundled as the classic script `support.js`.
|
|
4
|
+
*/
|
|
5
|
+
/** Starts the runtime: hides the raw template, waits for the DOM, then renders. */
|
|
6
|
+
export declare function startBoardRuntime(): void;
|