@silkui/core 0.0.0-canary.0 → 0.1.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/README.md +2 -1
- package/package.json +20 -5
- package/src/index.d.ts +319 -0
- package/src/index.js +1072 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,1072 @@
|
|
|
1
|
+
// @silkui/core: the nodes of a Silk UI tree and their events, for JavaScript that runs on the
|
|
2
|
+
// embedded SilkJS engine. The primitives are the tree's own (box, text, input, image, icon,
|
|
3
|
+
// canvas); the methods and the event flow are the DOM's, so code written for the web reads
|
|
4
|
+
// the same here.
|
|
5
|
+
//
|
|
6
|
+
// Every call queues a mutation through the native `__ui` functions. The host applies the
|
|
7
|
+
// queue after the script, and after each event it delivers.
|
|
8
|
+
|
|
9
|
+
const native = globalThis.__ui;
|
|
10
|
+
if (!native) throw new Error("@silkui/core needs the __ui functions of a ui-js host");
|
|
11
|
+
|
|
12
|
+
const KINDS = { box: 0, text: 1, input: 2, image: 3, icon: 4, canvas: 5 };
|
|
13
|
+
const STATES = { base: 0, hover: 1, active: 2, focus: 3, focusVisible: 4, starting: 5 };
|
|
14
|
+
|
|
15
|
+
// Event types in the order of the tree's LISTEN_* flags: the index is the kind number the
|
|
16
|
+
// host dispatches and the bit a node listens with.
|
|
17
|
+
const TYPES = [
|
|
18
|
+
"pointerdown", "pointerup", "click", "pointermove", "pointerenter", "pointerleave", "wheel",
|
|
19
|
+
"keydown", "keyup", "focus", "blur", "input", "scroll", "layout", "pointercancel",
|
|
20
|
+
"gotpointercapture", "lostpointercapture", "load", "error", "transitionrun", "transitionstart",
|
|
21
|
+
"transitionend", "transitioncancel", "animationstart", "animationiteration", "animationend",
|
|
22
|
+
"animationcancel", "selectionchange",
|
|
23
|
+
];
|
|
24
|
+
const KIND_OF = new Map(TYPES.map((type, kind) => [type, kind]));
|
|
25
|
+
const CLICK = KIND_OF.get("click");
|
|
26
|
+
// As on the web: these reach the target alone.
|
|
27
|
+
const NO_BUBBLE = new Set(["pointerenter", "pointerleave", "focus", "blur", "scroll", "layout", "load", "error"]);
|
|
28
|
+
// The tree has a default action to skip for these.
|
|
29
|
+
const CANCELABLE = new Set(["pointerdown", "pointerup", "click", "wheel", "keydown", "keyup"]);
|
|
30
|
+
|
|
31
|
+
// Properties whose numbers are not lengths. Every other number is CSS pixels.
|
|
32
|
+
const UNITLESS = new Set([
|
|
33
|
+
"opacity", "flexGrow", "flexShrink", "flex", "order", "zIndex", "fontWeight", "lineHeight",
|
|
34
|
+
"aspectRatio", "tabSize", "scale", "animationIterationCount", "gridRow", "gridRowStart", "gridRowEnd",
|
|
35
|
+
"gridColumn", "gridColumnStart", "gridColumnEnd", "gridArea",
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
const nodes = new Map();
|
|
39
|
+
const NO_CHILDREN = Object.freeze([]);
|
|
40
|
+
let nextId = 0;
|
|
41
|
+
|
|
42
|
+
// Property names repeat across nodes: each is converted once.
|
|
43
|
+
const CSS_NAMES = new Map();
|
|
44
|
+
|
|
45
|
+
function cssName(name) {
|
|
46
|
+
let css = CSS_NAMES.get(name);
|
|
47
|
+
if (css === undefined) {
|
|
48
|
+
css = name.replace(/[A-Z]/g, (letter) => "-" + letter.toLowerCase());
|
|
49
|
+
CSS_NAMES.set(name, css);
|
|
50
|
+
}
|
|
51
|
+
return css;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// The tree names font family lists by number: each `fontFamily` text is defined once, as a
|
|
55
|
+
// list of its own. The numbers start high, clear of the lists a host defines, and stay
|
|
56
|
+
// small enough to be exact in the 32-bit float a style value is.
|
|
57
|
+
const FAMILIES = new Map();
|
|
58
|
+
let nextFamily = 0x10000;
|
|
59
|
+
|
|
60
|
+
function familyOf(names) {
|
|
61
|
+
let id = FAMILIES.get(names);
|
|
62
|
+
if (id === undefined) {
|
|
63
|
+
id = nextFamily++;
|
|
64
|
+
FAMILIES.set(names, id);
|
|
65
|
+
native.fontFamily(id, names);
|
|
66
|
+
}
|
|
67
|
+
return String(id);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function cssValue(name, value) {
|
|
71
|
+
if (name === "fontFamily") return familyOf(String(value));
|
|
72
|
+
return typeof value === "number" && !UNITLESS.has(name) ? value + "px" : String(value);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ARIA states with a tree attribute of their own, by the name after `aria-`. The host takes
|
|
76
|
+
// the other ARIA names as they are; one it does not know is ignored, with one warning.
|
|
77
|
+
const ARIA_STATES = { checked: "checked", selected: "selected", expanded: "expanded" };
|
|
78
|
+
const ARIA_RANGE = { valuemin: 0, valuemax: 1, valuenow: 2 };
|
|
79
|
+
// ARIA attributes that name other elements by their ids. The tree takes node numbers, so a
|
|
80
|
+
// relation is sent again whenever an id it names comes, goes or moves to another node.
|
|
81
|
+
const ARIA_RELATIONS = new Set(["labelledby", "describedby", "controls", "activedescendant"]);
|
|
82
|
+
// Nodes by their `id`, and the nodes whose relations name each id.
|
|
83
|
+
const byId = new Map();
|
|
84
|
+
const naming = new Map();
|
|
85
|
+
const warned = new Set();
|
|
86
|
+
|
|
87
|
+
function warnOnce(message) {
|
|
88
|
+
if (warned.has(message)) return;
|
|
89
|
+
warned.add(message);
|
|
90
|
+
console.warn(message);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// The part of an ARIA attribute name after `aria`, lowercase: `aria-valuenow` (the DOM
|
|
94
|
+
// attribute) and `ariaValueNow` (the DOM property) are both `valuenow`. Null for other names.
|
|
95
|
+
function ariaName(name) {
|
|
96
|
+
if (name.startsWith("aria-")) return name.slice(5).toLowerCase();
|
|
97
|
+
if (name.length > 4 && name.startsWith("aria") && name[4] >= "A" && name[4] <= "Z") return name.slice(4).toLowerCase();
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The declarations of one look of a node: the base look, or what a state lays over it. */
|
|
102
|
+
class Look {
|
|
103
|
+
#node;
|
|
104
|
+
#state;
|
|
105
|
+
// Plain objects share their shape across nodes: a Map here costs four times the memory.
|
|
106
|
+
#values = Object.create(null);
|
|
107
|
+
|
|
108
|
+
constructor(node, state) {
|
|
109
|
+
this.#node = node;
|
|
110
|
+
this.#state = state;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Sets properties by their camel case names: `{ width: 120, backgroundColor: "red" }`.
|
|
115
|
+
* Numbers are CSS pixels except for unitless properties. `null`, `undefined` and `""`
|
|
116
|
+
* take a property out.
|
|
117
|
+
*/
|
|
118
|
+
set(properties) {
|
|
119
|
+
let css = "";
|
|
120
|
+
let removed = false;
|
|
121
|
+
for (const name of Object.keys(properties)) {
|
|
122
|
+
const value = properties[name];
|
|
123
|
+
if (value === null || value === undefined || value === "") {
|
|
124
|
+
if (name in this.#values) {
|
|
125
|
+
delete this.#values[name];
|
|
126
|
+
removed = true;
|
|
127
|
+
}
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const text = cssValue(name, value);
|
|
131
|
+
if (this.#values[name] === text) continue;
|
|
132
|
+
this.#values[name] = text;
|
|
133
|
+
css += cssName(name) + ":" + text + ";";
|
|
134
|
+
}
|
|
135
|
+
if (removed) {
|
|
136
|
+
// The tree sets declarations over earlier ones, so taking one out means starting the
|
|
137
|
+
// look again.
|
|
138
|
+
native.clearStyle(this.#node.id, this.#state);
|
|
139
|
+
css = this.cssText;
|
|
140
|
+
}
|
|
141
|
+
if (css) native.style(this.#node.id, css, this.#state);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Replaces the whole look. */
|
|
145
|
+
replace(properties) {
|
|
146
|
+
native.clearStyle(this.#node.id, this.#state);
|
|
147
|
+
this.#values = Object.create(null);
|
|
148
|
+
this.set(properties);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
get(name) {
|
|
152
|
+
return this.#values[name];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get cssText() {
|
|
156
|
+
let css = "";
|
|
157
|
+
for (const name in this.#values) css += cssName(name) + ":" + this.#values[name] + ";";
|
|
158
|
+
return css;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A node of the tree. Make one with `createBox`, `createText`, `createInput` or `createNode`. */
|
|
163
|
+
export class Node {
|
|
164
|
+
#listeners = null;
|
|
165
|
+
// The base look, and the state looks by name: most nodes have the base alone.
|
|
166
|
+
#base = null;
|
|
167
|
+
#looks = null;
|
|
168
|
+
#flags = 0;
|
|
169
|
+
// aria-valuemin, aria-valuemax and aria-valuenow, which the tree takes as one range.
|
|
170
|
+
#range = null;
|
|
171
|
+
|
|
172
|
+
constructor(kind) {
|
|
173
|
+
this.id = ++nextId;
|
|
174
|
+
this.kind = kind;
|
|
175
|
+
this.parentNode = null;
|
|
176
|
+
// A text leaf has no children: they all share one empty list.
|
|
177
|
+
this.childNodes = kind === "text" ? NO_CHILDREN : [];
|
|
178
|
+
nodes.set(this.id, this);
|
|
179
|
+
native.create(this.id, KINDS[kind]);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
get firstChild() {
|
|
183
|
+
return this.childNodes[0] ?? null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
get lastChild() {
|
|
187
|
+
return this.childNodes[this.childNodes.length - 1] ?? null;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
get nextSibling() {
|
|
191
|
+
const siblings = this.parentNode?.childNodes;
|
|
192
|
+
return siblings ? siblings[siblings.indexOf(this) + 1] ?? null : null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
get previousSibling() {
|
|
196
|
+
const siblings = this.parentNode?.childNodes;
|
|
197
|
+
return siblings ? siblings[siblings.indexOf(this) - 1] ?? null : null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Whether `other` is this node or inside it. */
|
|
201
|
+
contains(other) {
|
|
202
|
+
for (let node = other; node; node = node.parentNode) if (node === this) return true;
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Inserts `child` before `before`, or last without it. A child already in the tree moves. */
|
|
207
|
+
insertBefore(child, before = null) {
|
|
208
|
+
if (child.contains(this)) throw new Error("a node cannot be inserted into itself or its own subtree");
|
|
209
|
+
if (before && before.parentNode !== this) throw new Error("the node to insert before is not a child of this node");
|
|
210
|
+
if (before === child) return child;
|
|
211
|
+
const old = child.parentNode;
|
|
212
|
+
if (old) old.childNodes.splice(old.childNodes.indexOf(child), 1);
|
|
213
|
+
child.parentNode = this;
|
|
214
|
+
if (before) {
|
|
215
|
+
this.childNodes.splice(this.childNodes.indexOf(before), 0, child);
|
|
216
|
+
native.insertBefore(this.id, child.id, before.id);
|
|
217
|
+
} else {
|
|
218
|
+
this.childNodes.push(child);
|
|
219
|
+
native.append(this.id, child.id);
|
|
220
|
+
}
|
|
221
|
+
return child;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
appendChild(child) {
|
|
225
|
+
return this.insertBefore(child, null);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Appends nodes, and strings as text nodes. */
|
|
229
|
+
append(...children) {
|
|
230
|
+
for (const child of children) this.insertBefore(typeof child === "string" ? createText(child) : child, null);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Detaches `child` and its subtree. It keeps its state and can be inserted again. */
|
|
234
|
+
removeChild(child) {
|
|
235
|
+
if (child.parentNode !== this) throw new Error("the node to remove is not a child of this node");
|
|
236
|
+
this.childNodes.splice(this.childNodes.indexOf(child), 1);
|
|
237
|
+
child.parentNode = null;
|
|
238
|
+
native.remove(child.id);
|
|
239
|
+
return child;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
remove() {
|
|
243
|
+
this.parentNode?.removeChild(this);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Detaches and frees this node and its subtree. They cannot be used again. */
|
|
247
|
+
destroy() {
|
|
248
|
+
const parent = this.parentNode;
|
|
249
|
+
if (parent) {
|
|
250
|
+
parent.childNodes.splice(parent.childNodes.indexOf(this), 1);
|
|
251
|
+
this.parentNode = null;
|
|
252
|
+
}
|
|
253
|
+
native.destroy(this.id);
|
|
254
|
+
forget(this);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The base look. `node.style.set({ width: 120 })` sets properties,
|
|
259
|
+
* `node.style = { ... }` replaces the look.
|
|
260
|
+
*/
|
|
261
|
+
get style() {
|
|
262
|
+
return this.look("base");
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
set style(properties) {
|
|
266
|
+
this.look("base").replace(properties);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** The look the tree lays over the base in `state`: hover, active, focus, focusVisible, or
|
|
270
|
+
* starting (what transitions start from when the node first shows, as `@starting-style`). */
|
|
271
|
+
look(state) {
|
|
272
|
+
if (state === "base") return (this.#base ??= new Look(this, 0));
|
|
273
|
+
if (!(state in STATES)) throw new TypeError("the tree has no look '" + state + "'");
|
|
274
|
+
this.#looks ??= Object.create(null);
|
|
275
|
+
return (this.#looks[state] ??= new Look(this, STATES[state]));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Sets a tree attribute: tabIndex, disabled, label, testId, placeholder and so on. A node
|
|
280
|
+
* with an `href` is a link: a click on it or inside it that no listener prevented opens
|
|
281
|
+
* the address with `open`, as a click on an `<a>` follows it.
|
|
282
|
+
*
|
|
283
|
+
* `role` and `aria-*` work as in the DOM, spelled `aria-checked` or `ariaChecked`. A role
|
|
284
|
+
* or ARIA name the tree does not know is ignored with a warning.
|
|
285
|
+
*/
|
|
286
|
+
setAttribute(name, value) {
|
|
287
|
+
if (name === "role") {
|
|
288
|
+
const words = value == null ? "" : String(value);
|
|
289
|
+
if (!native.attribute(this.id, "role", words)) warnOnce("the tree has no role '" + words + "': it is ignored");
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (name === "id") {
|
|
293
|
+
this.#identify(value == null ? "" : String(value));
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const aria = ariaName(name);
|
|
297
|
+
if (aria !== null) {
|
|
298
|
+
this.#aria(aria, value);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (name === "href") {
|
|
302
|
+
this._href = value ? String(value) : null;
|
|
303
|
+
this.#want(KIND_OF.get("click"));
|
|
304
|
+
}
|
|
305
|
+
native.attribute(this.id, name, value);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
#aria(name, value) {
|
|
309
|
+
if (name === "label") {
|
|
310
|
+
native.attribute(this.id, "label", value == null ? "" : String(value));
|
|
311
|
+
} else if (name in ARIA_STATES) {
|
|
312
|
+
// ARIA states are the words "true" and "false"; anything else is false.
|
|
313
|
+
if (name === "checked") native.attribute(this.id, "indeterminate", value === "mixed");
|
|
314
|
+
native.attribute(this.id, ARIA_STATES[name], value === true || value === "true");
|
|
315
|
+
} else if (ARIA_RELATIONS.has(name)) {
|
|
316
|
+
this.#relate(name, value == null ? [] : String(value).split(/\s+/).filter(Boolean));
|
|
317
|
+
} else if (name in ARIA_RANGE) {
|
|
318
|
+
const range = (this.#range ??= [undefined, undefined, undefined]);
|
|
319
|
+
range[ARIA_RANGE[name]] = value == null || value === "" ? undefined : Number(value);
|
|
320
|
+
const [min = 0, max = 100, now] = range;
|
|
321
|
+
if (now === undefined || Number.isNaN(now)) native.attribute(this.id, "valueRange");
|
|
322
|
+
else native.attribute(this.id, "valueRange", min, max, now);
|
|
323
|
+
} else if (!native.attribute(this.id, "aria-" + name, value == null ? "" : String(value))) {
|
|
324
|
+
warnOnce("the tree has no 'aria-" + name + "' yet: it is ignored");
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// The `id` other nodes' relations name this node by.
|
|
329
|
+
#identify(id) {
|
|
330
|
+
const old = this._elementId ?? "";
|
|
331
|
+
if (old === id) return;
|
|
332
|
+
if (old && byId.get(old) === this) {
|
|
333
|
+
byId.delete(old);
|
|
334
|
+
resolveNaming(old);
|
|
335
|
+
}
|
|
336
|
+
this._elementId = id || undefined;
|
|
337
|
+
native.attribute(this.id, "id", id);
|
|
338
|
+
if (id) {
|
|
339
|
+
byId.set(id, this);
|
|
340
|
+
resolveNaming(id);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Sets relation `name` (`labelledby`) to the nodes with ids `refs`, as they are now and
|
|
345
|
+
// as they change.
|
|
346
|
+
#relate(name, refs) {
|
|
347
|
+
const relations = (this._relations ??= new Map());
|
|
348
|
+
for (const ref of relations.get(name) ?? []) naming.get(ref)?.delete(this);
|
|
349
|
+
if (refs.length) relations.set(name, refs);
|
|
350
|
+
else relations.delete(name);
|
|
351
|
+
for (const ref of refs) {
|
|
352
|
+
let sources = naming.get(ref);
|
|
353
|
+
if (!sources) naming.set(ref, (sources = new Set()));
|
|
354
|
+
sources.add(this);
|
|
355
|
+
}
|
|
356
|
+
this._sendRelation(name);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
_sendRelation(name) {
|
|
360
|
+
const numbers = [];
|
|
361
|
+
for (const ref of this._relations?.get(name) ?? []) {
|
|
362
|
+
const target = byId.get(ref);
|
|
363
|
+
if (target) numbers.push(target.id);
|
|
364
|
+
}
|
|
365
|
+
native.attribute(this.id, "aria-" + name, numbers.join(" "));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Tells the tree which events this node wants: its listeners', and clicks on a link.
|
|
369
|
+
#want(kind) {
|
|
370
|
+
const list = this.#listeners?.get(TYPES[kind]);
|
|
371
|
+
const wanted = (list && list.length > 0) || (kind === CLICK && this._href);
|
|
372
|
+
const flags = wanted ? this.#flags | (1 << kind) : this.#flags & ~(1 << kind);
|
|
373
|
+
if (flags !== this.#flags) native.listen(this.id, (this.#flags = flags));
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
set tabIndex(value) {
|
|
377
|
+
native.attribute(this.id, "tabIndex", value);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
set disabled(value) {
|
|
381
|
+
native.attribute(this.id, "disabled", value);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
focus() {
|
|
385
|
+
native.focus(this.id);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
scrollTo(x, y) {
|
|
389
|
+
native.scrollTo(this.id, x, y);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* As on the web. `options` is `true` or `{ capture: true }` for the capture phase, and
|
|
394
|
+
* `{ once: true }` for a listener that runs once.
|
|
395
|
+
*/
|
|
396
|
+
addEventListener(type, listener, options) {
|
|
397
|
+
const kind = KIND_OF.get(type);
|
|
398
|
+
if (kind === undefined) throw new TypeError("the tree has no event '" + type + "'");
|
|
399
|
+
const capture = options === true || !!options?.capture;
|
|
400
|
+
this.#listeners ??= new Map();
|
|
401
|
+
let list = this.#listeners.get(type);
|
|
402
|
+
if (!list) this.#listeners.set(type, (list = []));
|
|
403
|
+
if (list.some((entry) => entry.listener === listener && entry.capture === capture)) return;
|
|
404
|
+
list.push({ listener, capture, once: !!options?.once });
|
|
405
|
+
this.#want(kind);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
removeEventListener(type, listener, options) {
|
|
409
|
+
const list = this.#listeners?.get(type);
|
|
410
|
+
if (!list) return;
|
|
411
|
+
const capture = options === true || !!options?.capture;
|
|
412
|
+
const index = list.findIndex((entry) => entry.listener === listener && entry.capture === capture);
|
|
413
|
+
if (index < 0) return;
|
|
414
|
+
list.splice(index, 1);
|
|
415
|
+
this.#want(KIND_OF.get(type));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** Runs the capture or the bubble listeners of `event.type`. Used by the dispatcher. */
|
|
419
|
+
_run(event, capture) {
|
|
420
|
+
const list = this.#listeners?.get(event.type);
|
|
421
|
+
if (!list) return;
|
|
422
|
+
for (const entry of list.slice()) {
|
|
423
|
+
if (event._immediate) return;
|
|
424
|
+
if (entry.capture !== capture) continue;
|
|
425
|
+
if (entry.once) this.removeEventListener(event.type, entry.listener, entry.capture);
|
|
426
|
+
try {
|
|
427
|
+
if (typeof entry.listener === "function") entry.listener.call(this, event);
|
|
428
|
+
else entry.listener.handleEvent(event);
|
|
429
|
+
} catch (error) {
|
|
430
|
+
reportError(error);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** A text leaf. It takes its look from its parent. */
|
|
437
|
+
export class Text extends Node {
|
|
438
|
+
#data = "";
|
|
439
|
+
|
|
440
|
+
constructor(data = "") {
|
|
441
|
+
super("text");
|
|
442
|
+
this.data = data;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
get data() {
|
|
446
|
+
return this.#data;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
set data(value) {
|
|
450
|
+
value = String(value);
|
|
451
|
+
if (value === this.#data && value !== "") return;
|
|
452
|
+
this.#data = value;
|
|
453
|
+
native.setText(this.id, value);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
get textContent() {
|
|
457
|
+
return this.#data;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
set textContent(value) {
|
|
461
|
+
this.data = value;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** An editable text field. `value` follows what the user types. */
|
|
466
|
+
export class Input extends Node {
|
|
467
|
+
#value = "";
|
|
468
|
+
|
|
469
|
+
constructor() {
|
|
470
|
+
super("input");
|
|
471
|
+
this.selectionStart = 0;
|
|
472
|
+
this.selectionEnd = 0;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
get value() {
|
|
476
|
+
return this.#value;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
set value(value) {
|
|
480
|
+
this.#value = String(value);
|
|
481
|
+
native.setText(this.id, this.#value);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** Selects from `anchor` to `focus`, as UTF-8 byte offsets in the value. */
|
|
485
|
+
setSelectionRange(anchor, focus) {
|
|
486
|
+
native.select(this.id, anchor, focus);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
_changed(value, anchor, focus) {
|
|
490
|
+
this.#value = value;
|
|
491
|
+
this.selectionStart = Math.min(anchor, focus);
|
|
492
|
+
this.selectionEnd = Math.max(anchor, focus);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
_selected(start, end) {
|
|
496
|
+
this.selectionStart = start;
|
|
497
|
+
this.selectionEnd = end;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Inline SVG markup as icon resources: identical markup shares one, freed when no icon shows
|
|
502
|
+
// it. The ids start high, clear of resources a host or a URL table numbers from 1.
|
|
503
|
+
const ICONS = new Map();
|
|
504
|
+
let nextIcon = 0x40000000;
|
|
505
|
+
|
|
506
|
+
function holdIcon(markup) {
|
|
507
|
+
let entry = ICONS.get(markup);
|
|
508
|
+
if (!entry) {
|
|
509
|
+
entry = { id: nextIcon++, count: 0 };
|
|
510
|
+
ICONS.set(markup, entry);
|
|
511
|
+
native.defineIcon(entry.id, markup);
|
|
512
|
+
}
|
|
513
|
+
entry.count++;
|
|
514
|
+
return entry.id;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function dropIcon(markup) {
|
|
518
|
+
const entry = ICONS.get(markup);
|
|
519
|
+
if (entry && --entry.count === 0) {
|
|
520
|
+
ICONS.delete(markup);
|
|
521
|
+
native.release(entry.id);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* An icon: SVG drawn at the node's CSS size, as an inline `<svg>` is. Its `currentColor` is
|
|
527
|
+
* the node's `color`, so an icon that paints only with it (Lucide's, for example) takes the
|
|
528
|
+
* text color, and any other keeps its own colors.
|
|
529
|
+
*/
|
|
530
|
+
export class Icon extends Node {
|
|
531
|
+
#svg = "";
|
|
532
|
+
|
|
533
|
+
constructor(svg = "") {
|
|
534
|
+
super("icon");
|
|
535
|
+
this.svg = svg;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** The SVG markup, as `outerHTML` of an `<svg>` element would give it. */
|
|
539
|
+
get svg() {
|
|
540
|
+
return this.#svg;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
set svg(markup) {
|
|
544
|
+
markup = markup == null ? "" : String(markup);
|
|
545
|
+
if (markup === this.#svg) return;
|
|
546
|
+
if (this.#svg) dropIcon(this.#svg);
|
|
547
|
+
this.#svg = markup;
|
|
548
|
+
native.source(this.id, markup ? holdIcon(markup) : 0);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
_forget() {
|
|
552
|
+
if (this.#svg) dropIcon(this.#svg);
|
|
553
|
+
this.#svg = "";
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// Offsets cross the bridge as UTF-8 bytes; a rich input speaks JavaScript string indices.
|
|
558
|
+
const ASCII = /^[\x00-\x7f]*$/;
|
|
559
|
+
|
|
560
|
+
function byteOf(text, index) {
|
|
561
|
+
if (ASCII.test(text)) return Math.min(index, text.length);
|
|
562
|
+
let bytes = 0;
|
|
563
|
+
for (let i = 0; i < index && i < text.length; i++) {
|
|
564
|
+
const code = text.charCodeAt(i);
|
|
565
|
+
if (code < 0x80) bytes += 1;
|
|
566
|
+
else if (code < 0x800) bytes += 2;
|
|
567
|
+
else if (code >= 0xd800 && code < 0xdc00 && i + 1 < text.length) {
|
|
568
|
+
bytes += 4;
|
|
569
|
+
i++;
|
|
570
|
+
} else bytes += 3;
|
|
571
|
+
}
|
|
572
|
+
return bytes;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function indexOf(text, byte) {
|
|
576
|
+
if (ASCII.test(text)) return Math.min(byte, text.length);
|
|
577
|
+
let bytes = 0;
|
|
578
|
+
let i = 0;
|
|
579
|
+
while (i < text.length && bytes < byte) {
|
|
580
|
+
const code = text.charCodeAt(i);
|
|
581
|
+
if (code < 0x80) bytes += 1;
|
|
582
|
+
else if (code < 0x800) bytes += 2;
|
|
583
|
+
else if (code >= 0xd800 && code < 0xdc00 && i + 1 < text.length) {
|
|
584
|
+
bytes += 4;
|
|
585
|
+
i++;
|
|
586
|
+
} else bytes += 3;
|
|
587
|
+
i++;
|
|
588
|
+
}
|
|
589
|
+
return i;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function cssTextOf(properties) {
|
|
593
|
+
let css = "";
|
|
594
|
+
for (const name of Object.keys(properties ?? {})) {
|
|
595
|
+
const value = properties[name];
|
|
596
|
+
if (value === null || value === undefined || value === "") continue;
|
|
597
|
+
css += cssName(name) + ":" + cssValue(name, value) + ";";
|
|
598
|
+
}
|
|
599
|
+
return css;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** Where an atom sits in a rich input's text: U+FFFC OBJECT REPLACEMENT CHARACTER. */
|
|
603
|
+
export const OBJECT = "\ufffc";
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* A rich text field: styled runs (marks) and inline atoms (mentions, chips) over the text the
|
|
607
|
+
* user edits, with the caret, selection, undo and input method composition of `Input`.
|
|
608
|
+
*
|
|
609
|
+
* Its `document` is `{ text, marks, atoms }`. Each atom sits in `text` as one `OBJECT`
|
|
610
|
+
* character, so the caret steps over it as one unit; `atoms` lists them in text order as
|
|
611
|
+
* `{ kind, label, data }`. `marks` lists `{ start, end, marks }` spans with mark names.
|
|
612
|
+
* Offsets here, in `selectionStart` and in `insertAtom`, are string indices.
|
|
613
|
+
*/
|
|
614
|
+
export class RichInput extends Input {
|
|
615
|
+
#markBits = new Map();
|
|
616
|
+
#markNames = [];
|
|
617
|
+
#kinds = new Map();
|
|
618
|
+
#kindNames = [];
|
|
619
|
+
#document = { text: "", marks: [], atoms: [] };
|
|
620
|
+
|
|
621
|
+
constructor() {
|
|
622
|
+
super();
|
|
623
|
+
/** The marks text typed at the caret gets, as of the last change. */
|
|
624
|
+
this.activeMarks = [];
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
#bit(name) {
|
|
628
|
+
let bit = this.#markBits.get(name);
|
|
629
|
+
if (bit === undefined) {
|
|
630
|
+
bit = this.#markNames.length;
|
|
631
|
+
if (bit >= 32) throw new RangeError("a rich input has at most 32 marks");
|
|
632
|
+
this.#markBits.set(name, bit);
|
|
633
|
+
this.#markNames.push(name);
|
|
634
|
+
}
|
|
635
|
+
return bit;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
#kind(name) {
|
|
639
|
+
let kind = this.#kinds.get(name);
|
|
640
|
+
if (kind === undefined) {
|
|
641
|
+
kind = this.#kindNames.length;
|
|
642
|
+
this.#kinds.set(name, kind);
|
|
643
|
+
this.#kindNames.push(name);
|
|
644
|
+
}
|
|
645
|
+
return kind;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
#names(bits) {
|
|
649
|
+
const names = [];
|
|
650
|
+
for (let bit = 0; bit < this.#markNames.length; bit++) if (bits & (1 << bit)) names.push(this.#markNames[bit]);
|
|
651
|
+
return names;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/** What text with mark `name` looks like: properties as `style.set` takes them. */
|
|
655
|
+
defineMark(name, style) {
|
|
656
|
+
native.mark(this.id, this.#bit(name), cssTextOf(style));
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/** What atoms of `kind` look like: the inline block holding the label. */
|
|
660
|
+
defineAtom(kind, style) {
|
|
661
|
+
native.atomStyle(this.id, this.#kind(kind), cssTextOf(style));
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
get document() {
|
|
665
|
+
return this.#document;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/** Replaces the document. The caret goes to the end and the undo history is forgotten. */
|
|
669
|
+
set document(document) {
|
|
670
|
+
const text = String(document?.text ?? "");
|
|
671
|
+
const marks = (document?.marks ?? []).map((span) => ({ start: span.start, end: span.end, marks: [...span.marks] }));
|
|
672
|
+
const atoms = (document?.atoms ?? []).map((atom) => ({ kind: atom.kind, label: String(atom.label ?? ""), data: atom.data ?? "" }));
|
|
673
|
+
this.#document = { text, marks, atoms };
|
|
674
|
+
let packedMarks = "";
|
|
675
|
+
for (const span of marks) {
|
|
676
|
+
let bits = 0;
|
|
677
|
+
for (const name of span.marks) bits |= 1 << this.#bit(name);
|
|
678
|
+
packedMarks += byteOf(text, span.start) + "," + byteOf(text, span.end) + "," + (bits >>> 0) + ";";
|
|
679
|
+
}
|
|
680
|
+
let packedAtoms = "";
|
|
681
|
+
for (const atom of atoms) packedAtoms += this.#packAtom(atom);
|
|
682
|
+
native.richText(this.id, text, packedMarks, packedAtoms);
|
|
683
|
+
super._changed(text, text.length, text.length);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
#packAtom(atom) {
|
|
687
|
+
return this.#kind(atom.kind) + "\0" + String(atom.label ?? "") + "\0" + String(atom.data ?? "") + "\0";
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/** Turns mark `name` on or off over the selection, or for the text typed next. */
|
|
691
|
+
format(name, on = true) {
|
|
692
|
+
native.format(this.id, this.#bit(name), !!on);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** Turns mark `name` off where the caret's text has it, on where it does not. */
|
|
696
|
+
toggleMark(name) {
|
|
697
|
+
this.format(name, !this.activeMarks.includes(name));
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/** Replaces the text from `start` to `end` with `atom` (`{ kind, label, data }`). */
|
|
701
|
+
insertAtom(start, end, atom) {
|
|
702
|
+
const text = this.#document.text;
|
|
703
|
+
native.insertAtom(this.id, byteOf(text, start), byteOf(text, end), this.#packAtom(atom));
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
setSelectionRange(anchor, focus) {
|
|
707
|
+
const text = this.#document.text;
|
|
708
|
+
native.select(this.id, byteOf(text, anchor), byteOf(text, focus));
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
_changed(value, anchor, focus, marks, atoms, active) {
|
|
712
|
+
super._changed(value, indexOf(value, anchor), indexOf(value, focus));
|
|
713
|
+
if (marks === undefined) return;
|
|
714
|
+
const spans = [];
|
|
715
|
+
for (const part of String(marks).split(";")) {
|
|
716
|
+
if (!part) continue;
|
|
717
|
+
const [start, end, bits] = part.split(",").map(Number);
|
|
718
|
+
spans.push({ start: indexOf(value, start), end: indexOf(value, end), marks: this.#names(bits) });
|
|
719
|
+
}
|
|
720
|
+
const list = [];
|
|
721
|
+
const fields = String(atoms ?? "").split("\0");
|
|
722
|
+
for (let i = 0; i + 2 < fields.length; i += 3) {
|
|
723
|
+
list.push({ kind: this.#kindNames[Number(fields[i])] ?? Number(fields[i]), label: fields[i + 1], data: fields[i + 2] });
|
|
724
|
+
}
|
|
725
|
+
this.#document = { text: value, marks: spans, atoms: list };
|
|
726
|
+
this.activeMarks = this.#names(active ?? 0);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
_selected(start, end, active) {
|
|
730
|
+
const text = this.#document.text;
|
|
731
|
+
super._selected(indexOf(text, start), indexOf(text, end));
|
|
732
|
+
this.activeMarks = this.#names(active ?? 0);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/** A rich text field with `style` as its base look. */
|
|
737
|
+
export function createRichInput(style) {
|
|
738
|
+
const input = new RichInput();
|
|
739
|
+
if (style) input.style.set(style);
|
|
740
|
+
return input;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// Sends again the relations that name `ref`, after the node with that id changed.
|
|
744
|
+
function resolveNaming(ref) {
|
|
745
|
+
for (const source of naming.get(ref) ?? []) {
|
|
746
|
+
for (const [name, refs] of source._relations) if (refs.includes(ref)) source._sendRelation(name);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
function forget(node) {
|
|
751
|
+
nodes.delete(node.id);
|
|
752
|
+
if (node._relations) {
|
|
753
|
+
for (const refs of node._relations.values()) for (const ref of refs) naming.get(ref)?.delete(node);
|
|
754
|
+
}
|
|
755
|
+
if (node._elementId && byId.get(node._elementId) === node) {
|
|
756
|
+
byId.delete(node._elementId);
|
|
757
|
+
resolveNaming(node._elementId);
|
|
758
|
+
}
|
|
759
|
+
node._forget?.();
|
|
760
|
+
for (const child of node.childNodes) {
|
|
761
|
+
child.parentNode = null;
|
|
762
|
+
forget(child);
|
|
763
|
+
}
|
|
764
|
+
node.childNodes = [];
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
export function createNode(kind) {
|
|
768
|
+
if (kind === "richinput") return new RichInput();
|
|
769
|
+
if (!(kind in KINDS)) throw new TypeError("the tree has no node kind '" + kind + "'");
|
|
770
|
+
if (kind === "text") return new Text();
|
|
771
|
+
if (kind === "input") return new Input();
|
|
772
|
+
if (kind === "icon") return new Icon();
|
|
773
|
+
return new Node(kind);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/** A box with `style` as its base look and `children` appended. */
|
|
777
|
+
export function createBox(style, ...children) {
|
|
778
|
+
const box = new Node("box");
|
|
779
|
+
if (style) box.style.set(style);
|
|
780
|
+
box.append(...children);
|
|
781
|
+
return box;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
export function createText(data = "") {
|
|
785
|
+
return new Text(data);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/** An icon showing `svg` markup, with `style` as its base look. */
|
|
789
|
+
export function createIcon(svg, style) {
|
|
790
|
+
const icon = new Icon(svg);
|
|
791
|
+
if (style) icon.style.set(style);
|
|
792
|
+
return icon;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
export function createInput(style) {
|
|
796
|
+
const input = new Input();
|
|
797
|
+
if (style) input.style.set(style);
|
|
798
|
+
return input;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
let rootNode = null;
|
|
802
|
+
|
|
803
|
+
/** Makes `node` the root of the window's tree. */
|
|
804
|
+
/**
|
|
805
|
+
* Defines `@keyframes name` as CSS would: `{ from: { opacity: 0 }, "50%": {...}, to: {...} }`,
|
|
806
|
+
* with selectors such as `"0%, 100%"` too. Style objects name it in `animation` or
|
|
807
|
+
* `animationName`. Defining a name again replaces its rule; `null` removes it.
|
|
808
|
+
*/
|
|
809
|
+
export function defineKeyframes(name, frames) {
|
|
810
|
+
for (const selector of Object.keys(frames ?? {})) {
|
|
811
|
+
let css = "";
|
|
812
|
+
const declarations = frames[selector];
|
|
813
|
+
for (const property of Object.keys(declarations)) {
|
|
814
|
+
const value = declarations[property];
|
|
815
|
+
if (value === null || value === undefined || value === "") continue;
|
|
816
|
+
css += cssName(property) + ":" + cssValue(property, value) + ";";
|
|
817
|
+
}
|
|
818
|
+
for (const part of selector.split(",")) {
|
|
819
|
+
const text = part.trim().toLowerCase();
|
|
820
|
+
const offset = text === "from" ? 0 : text === "to" ? 1 : parseFloat(text) / 100;
|
|
821
|
+
if (!(offset >= 0 && offset <= 1)) throw new Error(`@silkui/core: bad keyframe selector '${part}'`);
|
|
822
|
+
native.keyframe(offset, css);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
native.keyframes(String(name));
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
export function mount(node) {
|
|
829
|
+
rootNode = node;
|
|
830
|
+
native.root(node.id);
|
|
831
|
+
return node;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
export function root() {
|
|
835
|
+
return rootNode;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
// The live regions `announce` speaks through, one per politeness, made on first use.
|
|
839
|
+
const regions = Object.create(null);
|
|
840
|
+
// A box screen readers read and nothing shows, as the web's visually hidden pattern.
|
|
841
|
+
const VISUALLY_HIDDEN = {
|
|
842
|
+
position: "absolute",
|
|
843
|
+
width: 1,
|
|
844
|
+
height: 1,
|
|
845
|
+
margin: -1,
|
|
846
|
+
overflow: "hidden",
|
|
847
|
+
clipPath: "inset(50%)",
|
|
848
|
+
whiteSpace: "nowrap",
|
|
849
|
+
pointerEvents: "none",
|
|
850
|
+
};
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* Has screen readers say `message` without showing it, as a toast or a "Saved" would. Polite
|
|
854
|
+
* messages wait for the reader to finish; `{ politeness: "assertive" }` interrupts it. The
|
|
855
|
+
* same message twice is said twice. Nothing happens before `mount`.
|
|
856
|
+
*/
|
|
857
|
+
export function announce(message, { politeness = "polite" } = {}) {
|
|
858
|
+
if (!rootNode) return;
|
|
859
|
+
const key = politeness === "assertive" ? "assertive" : "polite";
|
|
860
|
+
let region = regions[key];
|
|
861
|
+
if (!region || region.parentNode !== rootNode) {
|
|
862
|
+
region = regions[key] = createBox(VISUALLY_HIDDEN);
|
|
863
|
+
region.setAttribute("aria-live", key);
|
|
864
|
+
region.setAttribute("aria-atomic", "true");
|
|
865
|
+
rootNode.appendChild(region);
|
|
866
|
+
}
|
|
867
|
+
// A region announces changes, not what it holds when it appears, so the text waits a
|
|
868
|
+
// moment; each message is a node of its own so a repeat is a change too.
|
|
869
|
+
setTimeout(() => {
|
|
870
|
+
if (region.parentNode !== rootNode) return;
|
|
871
|
+
for (const child of region.childNodes.slice()) child.destroy();
|
|
872
|
+
const text = createText(String(message));
|
|
873
|
+
region.appendChild(text);
|
|
874
|
+
setTimeout(() => {
|
|
875
|
+
if (text.parentNode === region) text.destroy();
|
|
876
|
+
}, ANNOUNCEMENT_LIFETIME);
|
|
877
|
+
}, ANNOUNCEMENT_DELAY);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
const ANNOUNCEMENT_DELAY = 100;
|
|
881
|
+
const ANNOUNCEMENT_LIFETIME = 7000;
|
|
882
|
+
|
|
883
|
+
/** The node with tree id `id`, for hosts and tools. */
|
|
884
|
+
export function nodeById(id) {
|
|
885
|
+
return nodes.get(id) ?? null;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// The option bits of `__ui.markdown`.
|
|
889
|
+
const MARKDOWN_FLAGS = { tables: 1, strikethrough: 2, taskLists: 4, autolinkLiterals: 8, footnotes: 16, frontmatter: 32, alerts: 64 };
|
|
890
|
+
const GFM = 1 | 2 | 4 | 8;
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* The syntax tree of Markdown `text`, in the shape of mdast (what remark gives): plain
|
|
894
|
+
* objects with `type` and `children`. CommonMark, with GitHub's tables, strikethrough,
|
|
895
|
+
* task lists and autolinks unless `gfm` is false. `footnotes`, `frontmatter` and
|
|
896
|
+
* `alerts` (`> [!NOTE]`, a node of type `alert` with a `kind`) turn those on.
|
|
897
|
+
*/
|
|
898
|
+
export function parseMarkdown(text, options = {}) {
|
|
899
|
+
let flags = options.gfm === false ? 0 : GFM;
|
|
900
|
+
for (const name in MARKDOWN_FLAGS) {
|
|
901
|
+
if (options[name] === true) flags |= MARKDOWN_FLAGS[name];
|
|
902
|
+
else if (options[name] === false) flags &= ~MARKDOWN_FLAGS[name];
|
|
903
|
+
}
|
|
904
|
+
return JSON.parse(native.markdown(String(text), flags));
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
let errorHandler = null;
|
|
908
|
+
let pendingError = null;
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* What happens to an exception a listener throws. As on the web, it does not stop the
|
|
912
|
+
* dispatch. Without a handler, the host reports the first one after the input is handled.
|
|
913
|
+
*/
|
|
914
|
+
export function setErrorHandler(handler) {
|
|
915
|
+
errorHandler = handler;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
function reportError(error) {
|
|
919
|
+
if (errorHandler) errorHandler(error);
|
|
920
|
+
else pendingError ??= { error };
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/** An event of the tree, with the members of the DOM event it corresponds to. */
|
|
924
|
+
export class Event {
|
|
925
|
+
constructor(type, target) {
|
|
926
|
+
this.type = type;
|
|
927
|
+
this.target = target;
|
|
928
|
+
this.currentTarget = null;
|
|
929
|
+
this.eventPhase = 0;
|
|
930
|
+
this.bubbles = !NO_BUBBLE.has(type);
|
|
931
|
+
this.cancelable = CANCELABLE.has(type);
|
|
932
|
+
this.defaultPrevented = false;
|
|
933
|
+
this._stopped = false;
|
|
934
|
+
this._immediate = false;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
preventDefault() {
|
|
938
|
+
if (this.cancelable) this.defaultPrevented = true;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
stopPropagation() {
|
|
942
|
+
this._stopped = true;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
stopImmediatePropagation() {
|
|
946
|
+
this._stopped = true;
|
|
947
|
+
this._immediate = true;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/** The nodes the event passes, target first. */
|
|
951
|
+
composedPath() {
|
|
952
|
+
const path = [];
|
|
953
|
+
for (let node = this.target; node; node = node.parentNode) path.push(node);
|
|
954
|
+
return path;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
function fill(event, kind, a, b, modifiers, d, e, first, second, third, fourth) {
|
|
959
|
+
if (kind <= 6 || kind === 14) {
|
|
960
|
+
event.clientX = event.x = a;
|
|
961
|
+
event.clientY = event.y = b;
|
|
962
|
+
}
|
|
963
|
+
if (kind <= 8 || kind === 14) {
|
|
964
|
+
event.shiftKey = (modifiers & 1) !== 0;
|
|
965
|
+
event.ctrlKey = (modifiers & 2) !== 0;
|
|
966
|
+
event.altKey = (modifiers & 4) !== 0;
|
|
967
|
+
event.metaKey = (modifiers & 8) !== 0;
|
|
968
|
+
}
|
|
969
|
+
if (kind <= 2) {
|
|
970
|
+
event.button = d;
|
|
971
|
+
event.detail = e;
|
|
972
|
+
} else if (kind === 6) {
|
|
973
|
+
event.deltaX = d;
|
|
974
|
+
event.deltaY = e;
|
|
975
|
+
} else if (kind === 7 || kind === 8) {
|
|
976
|
+
event.key = first;
|
|
977
|
+
event.code = second;
|
|
978
|
+
event.repeat = d !== 0;
|
|
979
|
+
} else if (kind === 11) {
|
|
980
|
+
event.value = first;
|
|
981
|
+
event.inputType = second;
|
|
982
|
+
event.target._changed?.(first, a, b, third, fourth, d);
|
|
983
|
+
if (event.target instanceof RichInput) event.document = event.target.document;
|
|
984
|
+
} else if (kind === 12) {
|
|
985
|
+
event.scrollLeft = a;
|
|
986
|
+
event.scrollTop = b;
|
|
987
|
+
} else if (kind === 13) {
|
|
988
|
+
event.bounds = { x: a, y: b, width: d, height: e };
|
|
989
|
+
} else if (kind >= 19 && kind <= 22) {
|
|
990
|
+
event.propertyName = first;
|
|
991
|
+
event.elapsedTime = a;
|
|
992
|
+
event.pseudoElement = "";
|
|
993
|
+
} else if (kind === 27) {
|
|
994
|
+
event.target._selected?.(a, b, d);
|
|
995
|
+
event.selectionStart = event.target.selectionStart;
|
|
996
|
+
event.selectionEnd = event.target.selectionEnd;
|
|
997
|
+
} else if (kind >= 23) {
|
|
998
|
+
event.animationName = first;
|
|
999
|
+
event.elapsedTime = a;
|
|
1000
|
+
event.pseudoElement = "";
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* Runs `event` through the tree as the DOM does: the capture phase from the root down, the
|
|
1006
|
+
* target, then the bubble phase back up for events that bubble. Returns false when a
|
|
1007
|
+
* listener prevented the default.
|
|
1008
|
+
*/
|
|
1009
|
+
export function dispatchEvent(event) {
|
|
1010
|
+
const path = event.composedPath();
|
|
1011
|
+
for (let i = path.length - 1; i > 0 && !event._stopped; i--) {
|
|
1012
|
+
event.eventPhase = 1;
|
|
1013
|
+
event.currentTarget = path[i];
|
|
1014
|
+
path[i]._run(event, true);
|
|
1015
|
+
}
|
|
1016
|
+
if (!event._stopped) {
|
|
1017
|
+
event.eventPhase = 2;
|
|
1018
|
+
event.currentTarget = path[0];
|
|
1019
|
+
path[0]._run(event, true);
|
|
1020
|
+
if (!event._stopped) path[0]._run(event, false);
|
|
1021
|
+
}
|
|
1022
|
+
if (event.bubbles) {
|
|
1023
|
+
for (let i = 1; i < path.length && !event._stopped; i++) {
|
|
1024
|
+
event.eventPhase = 3;
|
|
1025
|
+
event.currentTarget = path[i];
|
|
1026
|
+
path[i]._run(event, false);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
event.eventPhase = 0;
|
|
1030
|
+
event.currentTarget = null;
|
|
1031
|
+
return !event.defaultPrevented;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
// The host calls this for every event record, before the tree runs the default action.
|
|
1035
|
+
native.dispatch = function (kind, id, a, b, modifiers, d, e, first, second, third, fourth) {
|
|
1036
|
+
const target = nodes.get(id);
|
|
1037
|
+
if (!target) return false;
|
|
1038
|
+
const event = new Event(TYPES[kind], target);
|
|
1039
|
+
fill(event, kind, a, b, modifiers, d, e, first, second, third, fourth);
|
|
1040
|
+
dispatchEvent(event);
|
|
1041
|
+
if (kind === CLICK && !event.defaultPrevented) follow(target);
|
|
1042
|
+
return event.defaultPrevented;
|
|
1043
|
+
};
|
|
1044
|
+
|
|
1045
|
+
// The default action of a click: the nearest link around the target opens.
|
|
1046
|
+
function follow(target) {
|
|
1047
|
+
for (let node = target; node; node = node.parentNode) {
|
|
1048
|
+
if (node._href) {
|
|
1049
|
+
try {
|
|
1050
|
+
globalThis.open(node._href);
|
|
1051
|
+
} catch (error) {
|
|
1052
|
+
reportError(error);
|
|
1053
|
+
}
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
// The host calls this before it replaces the realm, on a reload: every node goes, so the
|
|
1060
|
+
// next realm starts on an empty tree.
|
|
1061
|
+
native.unload = function () {
|
|
1062
|
+
for (const node of [...nodes.values()]) if (!node.parentNode && nodes.has(node.id)) node.destroy();
|
|
1063
|
+
rootNode = null;
|
|
1064
|
+
};
|
|
1065
|
+
|
|
1066
|
+
// The host calls this after it handled an input: an exception a listener threw surfaces here.
|
|
1067
|
+
native.takeError = function () {
|
|
1068
|
+
if (!pendingError) return;
|
|
1069
|
+
const { error } = pendingError;
|
|
1070
|
+
pendingError = null;
|
|
1071
|
+
throw error;
|
|
1072
|
+
};
|