@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/html.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small, tolerant HTML parser that keeps source offsets.
|
|
3
|
+
*
|
|
4
|
+
* The Design Component format needs three things a browser's DOM parser does
|
|
5
|
+
* not give: the exact source range of every node (so an edit rewrites only the
|
|
6
|
+
* bytes it changes), the attribute names as written (`onClick`, `viewBox`), and
|
|
7
|
+
* the same element order on every platform (Node checks and the browser
|
|
8
|
+
* editor must number elements identically). This parser follows source order
|
|
9
|
+
* strictly: no foster parenting, no implied `<tbody>`, no auto-closed `<p>`.
|
|
10
|
+
* The format requires authors to close every non-void element, so that is
|
|
11
|
+
* enough.
|
|
12
|
+
*/
|
|
13
|
+
/** Elements that never have children or an end tag. */
|
|
14
|
+
export declare const VOID_ELEMENTS: ReadonlySet<string>;
|
|
15
|
+
/** Elements whose content is raw text up to the matching end tag. */
|
|
16
|
+
export declare const RAW_TEXT_ELEMENTS: ReadonlySet<string>;
|
|
17
|
+
/** One attribute as written. */
|
|
18
|
+
export interface HtmlAttribute {
|
|
19
|
+
/** Name exactly as written (case kept). */
|
|
20
|
+
name: string;
|
|
21
|
+
/** Decoded value; `null` for a bare attribute (`disabled`). */
|
|
22
|
+
value: string | null;
|
|
23
|
+
/** Source offsets of the whole `name="value"` text (`-1` when synthesized). */
|
|
24
|
+
start: number;
|
|
25
|
+
end: number;
|
|
26
|
+
}
|
|
27
|
+
/** Fields shared by every node. */
|
|
28
|
+
interface NodeBase {
|
|
29
|
+
/** Source offset of the first character, `-1` for nodes created by edits. */
|
|
30
|
+
start: number;
|
|
31
|
+
/** Source offset just past the last character, `-1` for created nodes. */
|
|
32
|
+
end: number;
|
|
33
|
+
/** The parent element, `null` at the top level. Not serialized. */
|
|
34
|
+
parent: HtmlElement | null;
|
|
35
|
+
}
|
|
36
|
+
/** An element. */
|
|
37
|
+
export interface HtmlElement extends NodeBase {
|
|
38
|
+
type: 'element';
|
|
39
|
+
/** Lower-case tag name. */
|
|
40
|
+
tag: string;
|
|
41
|
+
/** Tag name as written (for faithful re-serialization). */
|
|
42
|
+
rawTag: string;
|
|
43
|
+
attrs: HtmlAttribute[];
|
|
44
|
+
children: HtmlNode[];
|
|
45
|
+
/** Offset just past the start tag's `>`. */
|
|
46
|
+
openEnd: number;
|
|
47
|
+
/** Offset of the end tag's `<`, or `-1` when there is none. */
|
|
48
|
+
closeStart: number;
|
|
49
|
+
/** Written as `<x/>`. */
|
|
50
|
+
selfClosing: boolean;
|
|
51
|
+
/** Edit bookkeeping: the start tag must be rebuilt from `attrs`. */
|
|
52
|
+
dirtyTag?: boolean;
|
|
53
|
+
/** Edit bookkeeping: some descendant changed; children must be re-serialized. */
|
|
54
|
+
dirtyChildren?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/** A run of text. */
|
|
57
|
+
export interface HtmlText extends NodeBase {
|
|
58
|
+
type: 'text';
|
|
59
|
+
/** Decoded text (raw for script/style content). */
|
|
60
|
+
value: string;
|
|
61
|
+
/** Edit bookkeeping: `value` changed and must be re-escaped. */
|
|
62
|
+
dirty?: boolean;
|
|
63
|
+
}
|
|
64
|
+
/** A comment (`<!-- … -->`) or a doctype / processing instruction. */
|
|
65
|
+
export interface HtmlComment extends NodeBase {
|
|
66
|
+
type: 'comment';
|
|
67
|
+
/** The full source text of the node. */
|
|
68
|
+
raw: string;
|
|
69
|
+
}
|
|
70
|
+
export type HtmlNode = HtmlElement | HtmlText | HtmlComment;
|
|
71
|
+
/** Decodes HTML character references; unknown names are left as written. */
|
|
72
|
+
export declare function decodeEntities(text: string): string;
|
|
73
|
+
/** Escapes text content. */
|
|
74
|
+
export declare function escapeText(text: string): string;
|
|
75
|
+
/** Escapes a double-quoted attribute value. */
|
|
76
|
+
export declare function escapeAttribute(value: string): string;
|
|
77
|
+
/** Options of {@link parseHtml}. */
|
|
78
|
+
export interface ParseOptions {
|
|
79
|
+
/** Added to every offset (when parsing a slice of a larger source). */
|
|
80
|
+
offset?: number;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Parses an HTML fragment into nodes with source offsets.
|
|
84
|
+
*
|
|
85
|
+
* Unmatched end tags are ignored; an end tag closes the nearest open element
|
|
86
|
+
* with that name, implicitly closing any elements opened after it. Elements
|
|
87
|
+
* still open at the end of input end there.
|
|
88
|
+
*/
|
|
89
|
+
export declare function parseHtml(source: string, options?: ParseOptions): HtmlNode[];
|
|
90
|
+
/** Element children only. */
|
|
91
|
+
export declare function elementChildren(node: {
|
|
92
|
+
children: HtmlNode[];
|
|
93
|
+
} | HtmlNode[]): HtmlElement[];
|
|
94
|
+
/** Case-insensitive attribute lookup. */
|
|
95
|
+
export declare function getAttribute(element: HtmlElement, name: string): HtmlAttribute | undefined;
|
|
96
|
+
/** The value of an attribute, `''` for a bare one, `undefined` when absent. */
|
|
97
|
+
export declare function attributeValue(element: HtmlElement, name: string): string | undefined;
|
|
98
|
+
/** Visits every element depth-first in source order. */
|
|
99
|
+
export declare function walkElements(nodes: HtmlNode[], visit: (element: HtmlElement) => void): void;
|
|
100
|
+
/** The concatenated text content of a node. */
|
|
101
|
+
export declare function textContent(node: HtmlNode): string;
|
|
102
|
+
/** Builds a start tag from an element's current attributes. */
|
|
103
|
+
export declare function buildStartTag(element: HtmlElement): string;
|
|
104
|
+
/**
|
|
105
|
+
* Serializes nodes back to HTML. Untouched nodes are copied byte for byte
|
|
106
|
+
* from `source`, so an edit changes only the text of what it touched.
|
|
107
|
+
*/
|
|
108
|
+
export declare function serializeNodes(nodes: HtmlNode[], source: string, offset?: number): string;
|
|
109
|
+
/** Serializes one node; see {@link serializeNodes}. */
|
|
110
|
+
export declare function serializeNode(node: HtmlNode, source: string, offset?: number): string;
|
|
111
|
+
/** Marks an element's ancestors so serialization descends into them. */
|
|
112
|
+
export declare function markAncestorsDirty(node: HtmlNode): void;
|
|
113
|
+
export {};
|
package/src/html.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small, tolerant HTML parser that keeps source offsets.
|
|
3
|
+
*
|
|
4
|
+
* The Design Component format needs three things a browser's DOM parser does
|
|
5
|
+
* not give: the exact source range of every node (so an edit rewrites only the
|
|
6
|
+
* bytes it changes), the attribute names as written (`onClick`, `viewBox`), and
|
|
7
|
+
* the same element order on every platform (Node checks and the browser
|
|
8
|
+
* editor must number elements identically). This parser follows source order
|
|
9
|
+
* strictly: no foster parenting, no implied `<tbody>`, no auto-closed `<p>`.
|
|
10
|
+
* The format requires authors to close every non-void element, so that is
|
|
11
|
+
* enough.
|
|
12
|
+
*/
|
|
13
|
+
/** Elements that never have children or an end tag. */
|
|
14
|
+
export const VOID_ELEMENTS = new Set([
|
|
15
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', 'track', 'wbr',
|
|
16
|
+
]);
|
|
17
|
+
/** Elements whose content is raw text up to the matching end tag. */
|
|
18
|
+
export const RAW_TEXT_ELEMENTS = new Set(['script', 'style', 'textarea', 'title']);
|
|
19
|
+
/* -------------------------------------------------------------------------- */
|
|
20
|
+
/* Entities */
|
|
21
|
+
/* -------------------------------------------------------------------------- */
|
|
22
|
+
const NAMED_ENTITIES = {
|
|
23
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', copy: '©', reg: '®', trade: '™',
|
|
24
|
+
hellip: '…', mdash: '—', ndash: '–', lsquo: '‘', rsquo: '’', ldquo: '“', rdquo: '”', laquo: '«',
|
|
25
|
+
raquo: '»', middot: '·', bull: '•', times: '×', divide: '÷', deg: '°', euro: '€', pound: '£',
|
|
26
|
+
yen: '¥', cent: '¢', sect: '§', para: '¶', larr: '←', rarr: '→', uarr: '↑', darr: '↓', harr: '↔',
|
|
27
|
+
plusmn: '±', le: '≤', ge: '≥', ne: '≠', minus: '−', infin: '∞', hearts: '♥', star: '☆', check: '✓',
|
|
28
|
+
ensp: ' ', emsp: ' ', thinsp: ' ', zwj: '', zwnj: '', shy: '',
|
|
29
|
+
};
|
|
30
|
+
/** Decodes HTML character references; unknown names are left as written. */
|
|
31
|
+
export function decodeEntities(text) {
|
|
32
|
+
if (!text.includes('&'))
|
|
33
|
+
return text;
|
|
34
|
+
return text.replace(/&(#x[0-9a-f]+|#\d+|[a-z][a-z0-9]*);/gi, (match, body) => {
|
|
35
|
+
if (body[0] === '#') {
|
|
36
|
+
const code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
|
37
|
+
return Number.isFinite(code) && code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : match;
|
|
38
|
+
}
|
|
39
|
+
return NAMED_ENTITIES[body] ?? NAMED_ENTITIES[body.toLowerCase()] ?? match;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/** Escapes text content. */
|
|
43
|
+
export function escapeText(text) {
|
|
44
|
+
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
45
|
+
}
|
|
46
|
+
/** Escapes a double-quoted attribute value. */
|
|
47
|
+
export function escapeAttribute(value) {
|
|
48
|
+
return value.replace(/&/g, '&').replace(/"/g, '"');
|
|
49
|
+
}
|
|
50
|
+
/* -------------------------------------------------------------------------- */
|
|
51
|
+
/* Parser */
|
|
52
|
+
/* -------------------------------------------------------------------------- */
|
|
53
|
+
const NAME_CHAR = /[^\s/>"'=<]/;
|
|
54
|
+
/**
|
|
55
|
+
* Parses an HTML fragment into nodes with source offsets.
|
|
56
|
+
*
|
|
57
|
+
* Unmatched end tags are ignored; an end tag closes the nearest open element
|
|
58
|
+
* with that name, implicitly closing any elements opened after it. Elements
|
|
59
|
+
* still open at the end of input end there.
|
|
60
|
+
*/
|
|
61
|
+
export function parseHtml(source, options = {}) {
|
|
62
|
+
const base = options.offset ?? 0;
|
|
63
|
+
const roots = [];
|
|
64
|
+
const stack = [];
|
|
65
|
+
let i = 0;
|
|
66
|
+
const n = source.length;
|
|
67
|
+
const append = (node) => {
|
|
68
|
+
const parent = stack[stack.length - 1] ?? null;
|
|
69
|
+
node.parent = parent;
|
|
70
|
+
(parent ? parent.children : roots).push(node);
|
|
71
|
+
};
|
|
72
|
+
const pushText = (from, to) => {
|
|
73
|
+
if (to <= from)
|
|
74
|
+
return;
|
|
75
|
+
const top = stack[stack.length - 1];
|
|
76
|
+
const raw = source.slice(from, to);
|
|
77
|
+
const value = top && (top.tag === 'script' || top.tag === 'style') ? raw : decodeEntities(raw);
|
|
78
|
+
append({ type: 'text', value, start: base + from, end: base + to, parent: null });
|
|
79
|
+
};
|
|
80
|
+
let textStart = 0;
|
|
81
|
+
while (i < n) {
|
|
82
|
+
const lt = source.indexOf('<', i);
|
|
83
|
+
if (lt === -1)
|
|
84
|
+
break;
|
|
85
|
+
// Comments, doctype and processing instructions.
|
|
86
|
+
if (source.startsWith('<!--', lt)) {
|
|
87
|
+
pushText(textStart, lt);
|
|
88
|
+
const close = source.indexOf('-->', lt + 4);
|
|
89
|
+
const end = close === -1 ? n : close + 3;
|
|
90
|
+
append({ type: 'comment', raw: source.slice(lt, end), start: base + lt, end: base + end, parent: null });
|
|
91
|
+
i = textStart = end;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (source[lt + 1] === '!' || source[lt + 1] === '?') {
|
|
95
|
+
pushText(textStart, lt);
|
|
96
|
+
const close = source.indexOf('>', lt);
|
|
97
|
+
const end = close === -1 ? n : close + 1;
|
|
98
|
+
append({ type: 'comment', raw: source.slice(lt, end), start: base + lt, end: base + end, parent: null });
|
|
99
|
+
i = textStart = end;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
// End tag.
|
|
103
|
+
if (source[lt + 1] === '/') {
|
|
104
|
+
const match = /^<\/([A-Za-z][^\s/>]*)\s*>/.exec(source.slice(lt, lt + 200));
|
|
105
|
+
if (!match) {
|
|
106
|
+
i = lt + 1;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
pushText(textStart, lt);
|
|
110
|
+
const tag = match[1].toLowerCase();
|
|
111
|
+
const end = lt + match[0].length;
|
|
112
|
+
for (let depth = stack.length - 1; depth >= 0; depth--) {
|
|
113
|
+
if (stack[depth].tag !== tag)
|
|
114
|
+
continue;
|
|
115
|
+
// Elements opened after the matching one end where this end tag starts.
|
|
116
|
+
while (stack.length - 1 > depth) {
|
|
117
|
+
const open = stack.pop();
|
|
118
|
+
open.end = base + lt;
|
|
119
|
+
}
|
|
120
|
+
const element = stack.pop();
|
|
121
|
+
element.closeStart = base + lt;
|
|
122
|
+
element.end = base + end;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
i = textStart = end;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
// Start tag.
|
|
129
|
+
if (!/[A-Za-z]/.test(source[lt + 1] ?? '')) {
|
|
130
|
+
i = lt + 1;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
pushText(textStart, lt);
|
|
134
|
+
let j = lt + 1;
|
|
135
|
+
while (j < n && NAME_CHAR.test(source[j]))
|
|
136
|
+
j++;
|
|
137
|
+
const rawTag = source.slice(lt + 1, j);
|
|
138
|
+
const tag = rawTag.toLowerCase();
|
|
139
|
+
const attrs = [];
|
|
140
|
+
let selfClosing = false;
|
|
141
|
+
// Attributes.
|
|
142
|
+
for (;;) {
|
|
143
|
+
while (j < n && /\s/.test(source[j]))
|
|
144
|
+
j++;
|
|
145
|
+
if (j >= n)
|
|
146
|
+
break;
|
|
147
|
+
if (source[j] === '>') {
|
|
148
|
+
j++;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
if (source[j] === '/' && source[j + 1] === '>') {
|
|
152
|
+
selfClosing = true;
|
|
153
|
+
j += 2;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
if (source[j] === '/') {
|
|
157
|
+
j++;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const attrStart = j;
|
|
161
|
+
while (j < n && NAME_CHAR.test(source[j]))
|
|
162
|
+
j++;
|
|
163
|
+
if (j === attrStart) {
|
|
164
|
+
j++; // stray character such as `"` or `=`: skip it
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const name = source.slice(attrStart, j);
|
|
168
|
+
let k = j;
|
|
169
|
+
while (k < n && /\s/.test(source[k]))
|
|
170
|
+
k++;
|
|
171
|
+
if (source[k] !== '=') {
|
|
172
|
+
attrs.push({ name, value: null, start: base + attrStart, end: base + j });
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
k++;
|
|
176
|
+
while (k < n && /\s/.test(source[k]))
|
|
177
|
+
k++;
|
|
178
|
+
const quote = source[k];
|
|
179
|
+
let value;
|
|
180
|
+
if (quote === '"' || quote === "'") {
|
|
181
|
+
const close = source.indexOf(quote, k + 1);
|
|
182
|
+
const valueEnd = close === -1 ? n : close;
|
|
183
|
+
value = source.slice(k + 1, valueEnd);
|
|
184
|
+
j = close === -1 ? n : close + 1;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
let e = k;
|
|
188
|
+
while (e < n && !/[\s>]/.test(source[e]))
|
|
189
|
+
e++;
|
|
190
|
+
value = source.slice(k, e);
|
|
191
|
+
j = e;
|
|
192
|
+
}
|
|
193
|
+
attrs.push({ name, value: decodeEntities(value), start: base + attrStart, end: base + j });
|
|
194
|
+
}
|
|
195
|
+
const element = {
|
|
196
|
+
type: 'element',
|
|
197
|
+
tag,
|
|
198
|
+
rawTag,
|
|
199
|
+
attrs,
|
|
200
|
+
children: [],
|
|
201
|
+
start: base + lt,
|
|
202
|
+
end: base + j,
|
|
203
|
+
openEnd: base + j,
|
|
204
|
+
closeStart: -1,
|
|
205
|
+
selfClosing,
|
|
206
|
+
parent: null,
|
|
207
|
+
};
|
|
208
|
+
append(element);
|
|
209
|
+
i = textStart = j;
|
|
210
|
+
if (VOID_ELEMENTS.has(tag) || selfClosing)
|
|
211
|
+
continue;
|
|
212
|
+
if (RAW_TEXT_ELEMENTS.has(tag)) {
|
|
213
|
+
// Raw text up to the matching end tag, case-insensitively.
|
|
214
|
+
const re = new RegExp(`</${tag}\\s*>`, 'ig');
|
|
215
|
+
re.lastIndex = j;
|
|
216
|
+
const match = re.exec(source);
|
|
217
|
+
const contentEnd = match ? match.index : n;
|
|
218
|
+
if (contentEnd > j) {
|
|
219
|
+
const raw = source.slice(j, contentEnd);
|
|
220
|
+
element.children.push({
|
|
221
|
+
type: 'text',
|
|
222
|
+
value: tag === 'script' || tag === 'style' ? raw : decodeEntities(raw),
|
|
223
|
+
start: base + j,
|
|
224
|
+
end: base + contentEnd,
|
|
225
|
+
parent: element,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
element.closeStart = match ? base + contentEnd : -1;
|
|
229
|
+
element.end = match ? base + contentEnd + match[0].length : base + n;
|
|
230
|
+
i = textStart = match ? contentEnd + match[0].length : n;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
stack.push(element);
|
|
234
|
+
}
|
|
235
|
+
pushText(textStart, n);
|
|
236
|
+
// Elements never closed end at the end of input.
|
|
237
|
+
while (stack.length)
|
|
238
|
+
stack.pop().end = base + n;
|
|
239
|
+
return roots;
|
|
240
|
+
}
|
|
241
|
+
/* -------------------------------------------------------------------------- */
|
|
242
|
+
/* Helpers */
|
|
243
|
+
/* -------------------------------------------------------------------------- */
|
|
244
|
+
/** Element children only. */
|
|
245
|
+
export function elementChildren(node) {
|
|
246
|
+
const list = Array.isArray(node) ? node : node.children;
|
|
247
|
+
return list.filter((child) => child.type === 'element');
|
|
248
|
+
}
|
|
249
|
+
/** Case-insensitive attribute lookup. */
|
|
250
|
+
export function getAttribute(element, name) {
|
|
251
|
+
const lower = name.toLowerCase();
|
|
252
|
+
return element.attrs.find((attr) => attr.name.toLowerCase() === lower);
|
|
253
|
+
}
|
|
254
|
+
/** The value of an attribute, `''` for a bare one, `undefined` when absent. */
|
|
255
|
+
export function attributeValue(element, name) {
|
|
256
|
+
const attr = getAttribute(element, name);
|
|
257
|
+
return attr ? (attr.value ?? '') : undefined;
|
|
258
|
+
}
|
|
259
|
+
/** Visits every element depth-first in source order. */
|
|
260
|
+
export function walkElements(nodes, visit) {
|
|
261
|
+
for (const node of nodes) {
|
|
262
|
+
if (node.type !== 'element')
|
|
263
|
+
continue;
|
|
264
|
+
visit(node);
|
|
265
|
+
walkElements(node.children, visit);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/** The concatenated text content of a node. */
|
|
269
|
+
export function textContent(node) {
|
|
270
|
+
if (node.type === 'text')
|
|
271
|
+
return node.value;
|
|
272
|
+
if (node.type === 'comment')
|
|
273
|
+
return '';
|
|
274
|
+
return node.children.map(textContent).join('');
|
|
275
|
+
}
|
|
276
|
+
/* -------------------------------------------------------------------------- */
|
|
277
|
+
/* Serialization */
|
|
278
|
+
/* -------------------------------------------------------------------------- */
|
|
279
|
+
/** Builds a start tag from an element's current attributes. */
|
|
280
|
+
export function buildStartTag(element) {
|
|
281
|
+
const parts = [element.rawTag || element.tag];
|
|
282
|
+
for (const attr of element.attrs) {
|
|
283
|
+
if (attr.value === null)
|
|
284
|
+
parts.push(attr.name);
|
|
285
|
+
else if (attr.value.includes('"') && !attr.value.includes("'"))
|
|
286
|
+
parts.push(`${attr.name}='${attr.value.replace(/&/g, '&')}'`);
|
|
287
|
+
else
|
|
288
|
+
parts.push(`${attr.name}="${escapeAttribute(attr.value)}"`);
|
|
289
|
+
}
|
|
290
|
+
return `<${parts.join(' ')}${element.selfClosing ? '/' : ''}>`;
|
|
291
|
+
}
|
|
292
|
+
/** True when the node, or anything inside it, was changed or created by an edit. */
|
|
293
|
+
function isDirty(node) {
|
|
294
|
+
if (node.start < 0)
|
|
295
|
+
return true;
|
|
296
|
+
if (node.type === 'text')
|
|
297
|
+
return Boolean(node.dirty);
|
|
298
|
+
if (node.type === 'comment')
|
|
299
|
+
return false;
|
|
300
|
+
return Boolean(node.dirtyTag || node.dirtyChildren);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Serializes nodes back to HTML. Untouched nodes are copied byte for byte
|
|
304
|
+
* from `source`, so an edit changes only the text of what it touched.
|
|
305
|
+
*/
|
|
306
|
+
export function serializeNodes(nodes, source, offset = 0) {
|
|
307
|
+
let out = '';
|
|
308
|
+
for (const node of nodes)
|
|
309
|
+
out += serializeNode(node, source, offset);
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
/** Serializes one node; see {@link serializeNodes}. */
|
|
313
|
+
export function serializeNode(node, source, offset = 0) {
|
|
314
|
+
const slice = (from, to) => source.slice(from - offset, to - offset);
|
|
315
|
+
if (!isDirty(node))
|
|
316
|
+
return slice(node.start, node.end);
|
|
317
|
+
if (node.type === 'text') {
|
|
318
|
+
const raw = node.parent && (node.parent.tag === 'script' || node.parent.tag === 'style');
|
|
319
|
+
return raw ? node.value : escapeText(node.value);
|
|
320
|
+
}
|
|
321
|
+
if (node.type === 'comment')
|
|
322
|
+
return node.raw;
|
|
323
|
+
const created = node.start < 0;
|
|
324
|
+
const startTag = created || node.dirtyTag ? buildStartTag(node) : slice(node.start, node.openEnd);
|
|
325
|
+
if (VOID_ELEMENTS.has(node.tag) || node.selfClosing)
|
|
326
|
+
return startTag;
|
|
327
|
+
const inner = serializeNodes(node.children, source, offset);
|
|
328
|
+
const endTag = !created && node.closeStart >= 0 ? slice(node.closeStart, node.end) : `</${node.rawTag || node.tag}>`;
|
|
329
|
+
return startTag + inner + endTag;
|
|
330
|
+
}
|
|
331
|
+
/** Marks an element's ancestors so serialization descends into them. */
|
|
332
|
+
export function markAncestorsDirty(node) {
|
|
333
|
+
for (let parent = node.parent; parent; parent = parent.parent)
|
|
334
|
+
parent.dirtyChildren = true;
|
|
335
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Design Component (`.dc.html`) format, without React: parsing,
|
|
3
|
+
* addressing, edits and checks. Safe to use in Node and in the editor.
|
|
4
|
+
* The renderer (`./render`) and the board runtime (`./runtime`) are
|
|
5
|
+
* separate entry points because they pull in React.
|
|
6
|
+
*/
|
|
7
|
+
export * from './html.ts';
|
|
8
|
+
export * from './props.ts';
|
|
9
|
+
export * from './holes.ts';
|
|
10
|
+
export * from './style.ts';
|
|
11
|
+
export * from './document.ts';
|
|
12
|
+
export * from './template.ts';
|
|
13
|
+
export * from './edit.ts';
|
|
14
|
+
export * from './rich-text.ts';
|
|
15
|
+
export * from './check.ts';
|
|
16
|
+
export type * from './protocol.ts';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Design Component (`.dc.html`) format, without React: parsing,
|
|
3
|
+
* addressing, edits and checks. Safe to use in Node and in the editor.
|
|
4
|
+
* The renderer (`./render`) and the board runtime (`./runtime`) are
|
|
5
|
+
* separate entry points because they pull in React.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./html.js";
|
|
8
|
+
export * from "./props.js";
|
|
9
|
+
export * from "./holes.js";
|
|
10
|
+
export * from "./style.js";
|
|
11
|
+
export * from "./document.js";
|
|
12
|
+
export * from "./template.js";
|
|
13
|
+
export * from "./edit.js";
|
|
14
|
+
export * from "./rich-text.js";
|
|
15
|
+
export * from "./check.js";
|
package/src/props.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `data-props`: the declaration of an artboard's tweakable props.
|
|
3
|
+
*
|
|
4
|
+
* Each key is a prop the logic class reads as `this.props.<key>`. An entry
|
|
5
|
+
* with an `editor` becomes a control in the Tweaks panel; `default` seeds the
|
|
6
|
+
* control (the class still falls back with `this.props.x ?? …`). The special
|
|
7
|
+
* key `$preview` gives the preferred preview size.
|
|
8
|
+
*/
|
|
9
|
+
/** Which control edits a prop; `null` for callbacks and objects. */
|
|
10
|
+
export type PropEditor = 'text' | 'color' | 'int' | 'float' | 'range' | 'boolean' | 'enum' | null;
|
|
11
|
+
/** One prop declaration. */
|
|
12
|
+
export interface PropSpec {
|
|
13
|
+
editor: PropEditor;
|
|
14
|
+
default?: unknown;
|
|
15
|
+
/** Documentation only. */
|
|
16
|
+
tsType?: string;
|
|
17
|
+
/** `enum`: the choices; `color`: 3–4 swatch hexes. */
|
|
18
|
+
options?: unknown[];
|
|
19
|
+
min?: number;
|
|
20
|
+
max?: number;
|
|
21
|
+
step?: number;
|
|
22
|
+
unit?: string;
|
|
23
|
+
/** Groups controls under a heading in the Tweaks panel. */
|
|
24
|
+
section?: string;
|
|
25
|
+
/** Optional label; defaults to the key. */
|
|
26
|
+
label?: string;
|
|
27
|
+
}
|
|
28
|
+
/** The preferred preview size. */
|
|
29
|
+
export interface PreviewSize {
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
}
|
|
33
|
+
/** A whole `data-props` object. */
|
|
34
|
+
export interface PropsSpec {
|
|
35
|
+
$preview?: PreviewSize;
|
|
36
|
+
[key: string]: PropSpec | PreviewSize | undefined;
|
|
37
|
+
}
|
|
38
|
+
/** Result of {@link parseProps}. */
|
|
39
|
+
export type ParsePropsResult = {
|
|
40
|
+
ok: true;
|
|
41
|
+
props: PropsSpec;
|
|
42
|
+
} | {
|
|
43
|
+
ok: false;
|
|
44
|
+
error: string;
|
|
45
|
+
};
|
|
46
|
+
/** Parses and validates the (entity-decoded) JSON of a `data-props` attribute. */
|
|
47
|
+
export declare function parseProps(json: string): ParsePropsResult;
|
|
48
|
+
/** The declared tweakable props (entries with a non-null editor), in declaration order. */
|
|
49
|
+
export declare function tweakEntries(props: PropsSpec | null): [string, PropSpec][];
|
|
50
|
+
/** Default values of every declared prop. */
|
|
51
|
+
export declare function defaultProps(props: PropsSpec | null): Record<string, unknown>;
|
package/src/props.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `data-props`: the declaration of an artboard's tweakable props.
|
|
3
|
+
*
|
|
4
|
+
* Each key is a prop the logic class reads as `this.props.<key>`. An entry
|
|
5
|
+
* with an `editor` becomes a control in the Tweaks panel; `default` seeds the
|
|
6
|
+
* control (the class still falls back with `this.props.x ?? …`). The special
|
|
7
|
+
* key `$preview` gives the preferred preview size.
|
|
8
|
+
*/
|
|
9
|
+
const EDITORS = new Set(['text', 'color', 'int', 'float', 'range', 'boolean', 'enum']);
|
|
10
|
+
/** Parses and validates the (entity-decoded) JSON of a `data-props` attribute. */
|
|
11
|
+
export function parseProps(json) {
|
|
12
|
+
let value;
|
|
13
|
+
try {
|
|
14
|
+
value = JSON.parse(json);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
return { ok: false, error: `data-props is not valid JSON: ${error instanceof Error ? error.message : String(error)}` };
|
|
18
|
+
}
|
|
19
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
20
|
+
return { ok: false, error: 'data-props must be a JSON object.' };
|
|
21
|
+
}
|
|
22
|
+
const props = value;
|
|
23
|
+
for (const [key, entry] of Object.entries(props)) {
|
|
24
|
+
if (key === '$preview') {
|
|
25
|
+
const preview = entry;
|
|
26
|
+
if (!preview || typeof preview.width !== 'number' || typeof preview.height !== 'number') {
|
|
27
|
+
return { ok: false, error: '$preview must be {"width": number, "height": number}.' };
|
|
28
|
+
}
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
32
|
+
return { ok: false, error: `data-props.${key} must be an object.` };
|
|
33
|
+
}
|
|
34
|
+
const editor = entry.editor;
|
|
35
|
+
if (editor !== null && editor !== undefined && (typeof editor !== 'string' || !EDITORS.has(editor))) {
|
|
36
|
+
return { ok: false, error: `data-props.${key}.editor "${String(editor)}" is not a known editor.` };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return { ok: true, props: props };
|
|
40
|
+
}
|
|
41
|
+
/** The declared tweakable props (entries with a non-null editor), in declaration order. */
|
|
42
|
+
export function tweakEntries(props) {
|
|
43
|
+
if (!props)
|
|
44
|
+
return [];
|
|
45
|
+
return Object.entries(props).filter((entry) => entry[0] !== '$preview' && Boolean(entry[1]) && entry[1].editor != null);
|
|
46
|
+
}
|
|
47
|
+
/** Default values of every declared prop. */
|
|
48
|
+
export function defaultProps(props) {
|
|
49
|
+
const values = {};
|
|
50
|
+
for (const [key, entry] of Object.entries(props ?? {})) {
|
|
51
|
+
if (key === '$preview' || !entry || !('editor' in entry))
|
|
52
|
+
continue;
|
|
53
|
+
if ('default' in entry)
|
|
54
|
+
values[key] = entry.default;
|
|
55
|
+
}
|
|
56
|
+
return values;
|
|
57
|
+
}
|