@silkui/core 0.1.9 → 0.1.11
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 +49 -1
- package/package.json +5 -1
- package/src/globals.d.ts +3 -0
- package/src/globals.js +38 -0
- package/src/index.d.ts +197 -6
- package/src/index.js +1685 -111
package/src/index.js
CHANGED
|
@@ -12,15 +12,16 @@ if (!native) throw new Error("@silkui/core needs the __ui functions of a ui-js h
|
|
|
12
12
|
const KINDS = { box: 0, text: 1, input: 2, image: 3, icon: 4, canvas: 5 };
|
|
13
13
|
const STATES = { base: 0, hover: 1, active: 2, focus: 3, focusVisible: 4, starting: 5 };
|
|
14
14
|
|
|
15
|
-
// Event types
|
|
16
|
-
//
|
|
15
|
+
// Event types by the kind number the host dispatches. Up to `selectionchange`, `pointerover`
|
|
16
|
+
// and `pointerout` the kind is also the bit of the tree's LISTEN_* flags a node listens with;
|
|
17
|
+
// copy, cut and paste share bit 30, and beforeinput has bit 31 (see `bitOf`).
|
|
17
18
|
const TYPES = [
|
|
18
19
|
"pointerdown", "pointerup", "click", "pointermove", "pointerenter", "pointerleave", "wheel",
|
|
19
20
|
"keydown", "keyup", "focus", "blur", "input", "scroll", "layout", "pointercancel",
|
|
20
21
|
"gotpointercapture", "lostpointercapture", "load", "error", "transitionrun", "transitionstart",
|
|
21
22
|
"transitionend", "transitioncancel", "animationstart", "animationiteration", "animationend",
|
|
22
23
|
"animationcancel", "selectionchange", "pointerover", "pointerout", "touchstart", "touchmove",
|
|
23
|
-
"touchend", "touchcancel",
|
|
24
|
+
"touchend", "touchcancel", "copy", "cut", "paste", "beforeinput",
|
|
24
25
|
];
|
|
25
26
|
const KIND_OF = new Map(TYPES.map((type, kind) => [type, kind]));
|
|
26
27
|
|
|
@@ -36,10 +37,17 @@ function eachLiveRange(update) {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
const CLICK = KIND_OF.get("click");
|
|
40
|
+
const FOCUS = KIND_OF.get("focus");
|
|
41
|
+
const BLUR = KIND_OF.get("blur");
|
|
39
42
|
const SELECTION_CHANGE = KIND_OF.get("selectionchange");
|
|
40
43
|
const TOUCH_START = KIND_OF.get("touchstart");
|
|
41
|
-
|
|
42
|
-
const
|
|
44
|
+
const COPY = KIND_OF.get("copy");
|
|
45
|
+
const CUT = KIND_OF.get("cut");
|
|
46
|
+
const PASTE = KIND_OF.get("paste");
|
|
47
|
+
const BEFORE_INPUT = KIND_OF.get("beforeinput");
|
|
48
|
+
// The listen bit of a kind: the touch kinds share bit 30, copy, cut, paste and beforeinput bit 31.
|
|
49
|
+
const bitOf = (kind) => (kind < TOUCH_START ? kind : kind < COPY ? 30 : 31);
|
|
50
|
+
const KINDS_OF_BIT = (bit) => TYPES.map((_, kind) => kind).filter((kind) => bitOf(kind) === bit);
|
|
43
51
|
// Compatibility mouse events, as the web fires them after pointer events: each listens
|
|
44
52
|
// through the pointer kind it follows.
|
|
45
53
|
const MOUSE_OF = new Map([
|
|
@@ -59,7 +67,7 @@ const NO_BUBBLE = new Set([
|
|
|
59
67
|
// The tree has a default action to skip for these. Touch records say for themselves.
|
|
60
68
|
const CANCELABLE = new Set([
|
|
61
69
|
"pointerdown", "pointerup", "click", "wheel", "keydown", "keyup", "dragenter", "dragover", "drop",
|
|
62
|
-
"mousedown", "mouseup", "mousemove", "mouseover", "mouseout", "selectstart",
|
|
70
|
+
"mousedown", "mouseup", "mousemove", "mouseover", "mouseout", "selectstart", "copy", "cut", "paste", "beforeinput",
|
|
63
71
|
]);
|
|
64
72
|
// Files dragged from the desktop: the host dispatches these itself, so nodes need no flag.
|
|
65
73
|
const DRAG_TYPES = new Set(["dragenter", "dragover", "dragleave", "drop"]);
|
|
@@ -72,6 +80,10 @@ const UNITLESS = new Set([
|
|
|
72
80
|
]);
|
|
73
81
|
|
|
74
82
|
const nodes = new Map();
|
|
83
|
+
// HTML boolean attributes that are also the tree's own.
|
|
84
|
+
const BOOLEAN_TREE_ATTRIBUTES = new Set(["disabled", "checked", "selected", "inert", "readonly", "multiline"]);
|
|
85
|
+
// The tag stylesheets match a tree node by, by its kind (see ui-tree's `SelectorTree`).
|
|
86
|
+
const LOCAL_NAMES = { box: "div", input: "input", image: "img", canvas: "canvas", svg: "svg", text: "#text" };
|
|
75
87
|
const NO_CHILDREN = Object.freeze([]);
|
|
76
88
|
let nextId = 0;
|
|
77
89
|
|
|
@@ -88,6 +100,68 @@ function cssName(name) {
|
|
|
88
100
|
return css;
|
|
89
101
|
}
|
|
90
102
|
|
|
103
|
+
// A CSS name as the camel case name looks take: `background-color` is `backgroundColor`,
|
|
104
|
+
// `-webkit-line-clamp` is `WebkitLineClamp`; custom properties stay as they are.
|
|
105
|
+
function camelName(name) {
|
|
106
|
+
if (name.startsWith("--") || !name.includes("-")) return name;
|
|
107
|
+
return name.replace(/^-ms-/, "ms-").replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// `color: red; --gap: 4px` as camel case properties. Semicolons inside parentheses or
|
|
111
|
+
// quotes (`url("a;b")`) stay in their value.
|
|
112
|
+
function parseDeclarations(text) {
|
|
113
|
+
const properties = {};
|
|
114
|
+
let depth = 0;
|
|
115
|
+
let quote = "";
|
|
116
|
+
let start = 0;
|
|
117
|
+
const take = (end) => {
|
|
118
|
+
const part = text.slice(start, end);
|
|
119
|
+
const colon = part.indexOf(":");
|
|
120
|
+
if (colon > 0) {
|
|
121
|
+
const name = part.slice(0, colon).trim();
|
|
122
|
+
const value = part.slice(colon + 1).trim();
|
|
123
|
+
if (name && value) properties[camelName(name.startsWith("--") ? name : name.toLowerCase())] = value;
|
|
124
|
+
}
|
|
125
|
+
start = end + 1;
|
|
126
|
+
};
|
|
127
|
+
for (let i = 0; i < text.length; i++) {
|
|
128
|
+
const c = text[i];
|
|
129
|
+
if (quote) {
|
|
130
|
+
if (c === "\\") i++;
|
|
131
|
+
else if (c === quote) quote = "";
|
|
132
|
+
} else if (c === '"' || c === "'") quote = c;
|
|
133
|
+
else if (c === "(") depth++;
|
|
134
|
+
else if (c === ")") depth--;
|
|
135
|
+
else if (c === ";" && depth <= 0) take(i);
|
|
136
|
+
}
|
|
137
|
+
take(text.length);
|
|
138
|
+
return properties;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// `element.style` of an HTML element: the base look, whose CSS properties also read and write
|
|
142
|
+
// as fields (`style.height = "10px"`), as a CSSStyleDeclaration's do.
|
|
143
|
+
const styleProxies = new WeakMap();
|
|
144
|
+
function styleProxy(look) {
|
|
145
|
+
let proxy = styleProxies.get(look);
|
|
146
|
+
if (proxy) return proxy;
|
|
147
|
+
proxy = new Proxy(look, {
|
|
148
|
+
get(target, key) {
|
|
149
|
+
if (typeof key !== "string" || key in target) {
|
|
150
|
+
const value = Reflect.get(target, key, target);
|
|
151
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
152
|
+
}
|
|
153
|
+
return target.get(key) ?? "";
|
|
154
|
+
},
|
|
155
|
+
set(target, key, value) {
|
|
156
|
+
if (typeof key !== "string" || key in target) return Reflect.set(target, key, value, target);
|
|
157
|
+
target.set({ [key]: value });
|
|
158
|
+
return true;
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
styleProxies.set(look, proxy);
|
|
162
|
+
return proxy;
|
|
163
|
+
}
|
|
164
|
+
|
|
91
165
|
// The tree names font family lists by number: each `fontFamily` text is defined once, as a
|
|
92
166
|
// list of its own. The numbers start high, clear of the lists a host defines, and stay
|
|
93
167
|
// small enough to be exact in the 32-bit float a style value is.
|
|
@@ -131,6 +205,8 @@ class Look {
|
|
|
131
205
|
#node;
|
|
132
206
|
#state;
|
|
133
207
|
// Plain objects share their shape across nodes: a Map here costs four times the memory.
|
|
208
|
+
// A number or string is kept as given, which the caller's style object holds too, and
|
|
209
|
+
// turned into CSS text when it is read: no node keeps a string of its own per value.
|
|
134
210
|
#values = Object.create(null);
|
|
135
211
|
|
|
136
212
|
constructor(node, state) {
|
|
@@ -155,9 +231,9 @@ class Look {
|
|
|
155
231
|
}
|
|
156
232
|
continue;
|
|
157
233
|
}
|
|
234
|
+
if (this.#values[name] === value) continue;
|
|
158
235
|
const text = cssValue(name, value);
|
|
159
|
-
|
|
160
|
-
this.#values[name] = text;
|
|
236
|
+
this.#values[name] = typeof value === "number" || typeof value === "string" ? value : text;
|
|
161
237
|
css += cssName(name) + ":" + text + ";";
|
|
162
238
|
}
|
|
163
239
|
if (removed) {
|
|
@@ -177,15 +253,51 @@ class Look {
|
|
|
177
253
|
}
|
|
178
254
|
|
|
179
255
|
get(name) {
|
|
180
|
-
|
|
256
|
+
const value = this.#values[name];
|
|
257
|
+
return value === undefined ? undefined : cssValue(name, value);
|
|
181
258
|
}
|
|
182
259
|
|
|
183
260
|
get cssText() {
|
|
184
261
|
let css = "";
|
|
185
|
-
for (const name in this.#values) css += cssName(name) + ":" + this.#values[name] + ";";
|
|
262
|
+
for (const name in this.#values) css += cssName(name) + ":" + cssValue(name, this.#values[name]) + ";";
|
|
186
263
|
return css;
|
|
187
264
|
}
|
|
188
265
|
|
|
266
|
+
/** Replaces the look with declarations in CSS text, as `element.style.cssText`. */
|
|
267
|
+
set cssText(text) {
|
|
268
|
+
this.replace(parseDeclarations(String(text ?? "")));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
get length() {
|
|
272
|
+
return Object.keys(this.#values).length;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
item(index) {
|
|
276
|
+
const name = Object.keys(this.#values)[index];
|
|
277
|
+
return name === undefined ? "" : cssName(name);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// CSSStyleDeclaration's methods, which take CSS names (`background-color`, `--gap`).
|
|
281
|
+
setProperty(name, value, priority) {
|
|
282
|
+
const text = value === null || value === undefined ? "" : String(value) + (priority === "important" ? " !important" : "");
|
|
283
|
+
this.set({ [camelName(String(name))]: text });
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
removeProperty(name) {
|
|
287
|
+
const camel = camelName(String(name));
|
|
288
|
+
const old = this.get(camel) ?? "";
|
|
289
|
+
this.set({ [camel]: null });
|
|
290
|
+
return old;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
getPropertyValue(name) {
|
|
294
|
+
return this.get(camelName(String(name))) ?? "";
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
getPropertyPriority() {
|
|
298
|
+
return "";
|
|
299
|
+
}
|
|
300
|
+
|
|
189
301
|
_copyTo(look) {
|
|
190
302
|
look.#values = Object.assign(Object.create(null), this.#values);
|
|
191
303
|
const css = this.cssText;
|
|
@@ -407,28 +519,50 @@ export function updateStyleSheet(id, compiled) {
|
|
|
407
519
|
}
|
|
408
520
|
}
|
|
409
521
|
|
|
522
|
+
// What few nodes have, made on first use (see `Node`).
|
|
523
|
+
class NodeState {
|
|
524
|
+
listeners = null;
|
|
525
|
+
// The state looks by name.
|
|
526
|
+
looks = null;
|
|
527
|
+
flags = 0;
|
|
528
|
+
// Kinds the node hears whatever its listeners: an input keeps its value and selection current.
|
|
529
|
+
kept = 0;
|
|
530
|
+
attributes = null;
|
|
531
|
+
classList = null;
|
|
532
|
+
}
|
|
533
|
+
|
|
410
534
|
/** A node of the tree. Make one with `createBox`, `createText`, `createInput` or `createNode`. */
|
|
411
535
|
export class Node {
|
|
412
|
-
|
|
413
|
-
//
|
|
536
|
+
// A node is a handle on the tree's node: it keeps only what the tree cannot answer at once
|
|
537
|
+
// (its place among its siblings, its class, its base look), and puts the state few nodes
|
|
538
|
+
// have (listeners, attributes, state looks) in one record made on first use. Every field
|
|
539
|
+
// is a slot in every node, and a node of up to eight fits its object's own cell.
|
|
540
|
+
// The base look: most nodes have the base alone.
|
|
414
541
|
#base = null;
|
|
415
|
-
#
|
|
416
|
-
#
|
|
417
|
-
// Kinds the node hears whatever its listeners: an input keeps its value and selection current.
|
|
418
|
-
#kept = 0;
|
|
419
|
-
#attributes = null;
|
|
420
|
-
#classList = null;
|
|
542
|
+
#className = null;
|
|
543
|
+
#rare = null;
|
|
421
544
|
|
|
422
545
|
constructor(kind) {
|
|
423
546
|
Object.defineProperty(this, "nodeId", { value: ++nextId, enumerable: true });
|
|
424
|
-
|
|
547
|
+
// A text node's kind is its prototype's: one slot fewer in the most numerous node.
|
|
548
|
+
if (kind !== "text") this.kind = kind;
|
|
425
549
|
this.parentNode = null;
|
|
426
|
-
// A
|
|
427
|
-
this.childNodes =
|
|
550
|
+
// A node with no children shares one empty list, and gets its own at its first child.
|
|
551
|
+
this.childNodes = NO_CHILDREN;
|
|
428
552
|
nodes.set(this.nodeId, this);
|
|
429
553
|
native.create(this.nodeId, KINDS[kind]);
|
|
430
554
|
}
|
|
431
555
|
|
|
556
|
+
#more() {
|
|
557
|
+
return (this.#rare ??= new NodeState());
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// The node's own child list, made on its first child.
|
|
561
|
+
_ownChildren() {
|
|
562
|
+
if (this.childNodes === NO_CHILDREN) this.childNodes = [];
|
|
563
|
+
return this.childNodes;
|
|
564
|
+
}
|
|
565
|
+
|
|
432
566
|
get adoptedStyleSheets() { return adoptedList(this).proxy; }
|
|
433
567
|
set adoptedStyleSheets(value) { adoptedList(this).assign(Array.from(value)); }
|
|
434
568
|
|
|
@@ -455,6 +589,30 @@ export class Node {
|
|
|
455
589
|
return 1;
|
|
456
590
|
}
|
|
457
591
|
|
|
592
|
+
/**
|
|
593
|
+
* The element's tag: the HTML tag it was made with (see `createElement`), or for a tree
|
|
594
|
+
* node the tag stylesheets match it by (`div` for a box).
|
|
595
|
+
*/
|
|
596
|
+
get localName() {
|
|
597
|
+
return this._tag ?? LOCAL_NAMES[this.kind] ?? "div";
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
set localName(value) {
|
|
601
|
+
this._tag = String(value);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/** The tag in upper case for HTML elements, as in the DOM. */
|
|
605
|
+
get tagName() {
|
|
606
|
+
const name = this.localName;
|
|
607
|
+
return this.namespaceURI === undefined ? name.toUpperCase() : name;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
set tagName(_value) {}
|
|
611
|
+
|
|
612
|
+
get nodeName() {
|
|
613
|
+
return this.tagName;
|
|
614
|
+
}
|
|
615
|
+
|
|
458
616
|
hasChildNodes() {
|
|
459
617
|
return this.childNodes.length > 0;
|
|
460
618
|
}
|
|
@@ -463,8 +621,9 @@ export class Node {
|
|
|
463
621
|
cloneNode(deep = false) {
|
|
464
622
|
const copy = this._shallowClone();
|
|
465
623
|
if (this.#base) this.#base._copyTo(copy.look("base"));
|
|
466
|
-
|
|
467
|
-
for (const
|
|
624
|
+
const looks = this.#rare?.looks;
|
|
625
|
+
for (const state in looks ?? {}) looks[state]._copyTo(copy.look(state));
|
|
626
|
+
for (const [name, value] of this.#rare?.attributes ?? []) copy.setAttribute(name, value);
|
|
468
627
|
if (deep) for (const child of this.childNodes) copy.appendChild(child.cloneNode(true));
|
|
469
628
|
return copy;
|
|
470
629
|
}
|
|
@@ -536,7 +695,7 @@ export class Node {
|
|
|
536
695
|
this.childNodes.splice(this.childNodes.indexOf(before), 0, child);
|
|
537
696
|
native.insertBefore(this.nodeId, child.nodeId, before.nodeId);
|
|
538
697
|
} else {
|
|
539
|
-
this.
|
|
698
|
+
this._ownChildren().push(child);
|
|
540
699
|
native.append(this.nodeId, child.nodeId);
|
|
541
700
|
}
|
|
542
701
|
return child;
|
|
@@ -596,11 +755,12 @@ export class Node {
|
|
|
596
755
|
* `node.style = { ... }` replaces the look.
|
|
597
756
|
*/
|
|
598
757
|
get style() {
|
|
599
|
-
return this.look("base");
|
|
758
|
+
return this._tag === undefined ? this.look("base") : styleProxy(this.look("base"));
|
|
600
759
|
}
|
|
601
760
|
|
|
602
761
|
set style(properties) {
|
|
603
|
-
this.look("base").
|
|
762
|
+
if (typeof properties === "string") this.look("base").cssText = properties;
|
|
763
|
+
else this.look("base").replace(properties);
|
|
604
764
|
}
|
|
605
765
|
|
|
606
766
|
/** The look the tree lays over the base in `state`: hover, active, focus, focusVisible, or
|
|
@@ -608,8 +768,9 @@ export class Node {
|
|
|
608
768
|
look(state) {
|
|
609
769
|
if (state === "base") return (this.#base ??= new Look(this, 0));
|
|
610
770
|
if (!(state in STATES)) throw new TypeError("the tree has no look '" + state + "'");
|
|
611
|
-
this.#
|
|
612
|
-
|
|
771
|
+
const rare = this.#more();
|
|
772
|
+
rare.looks ??= Object.create(null);
|
|
773
|
+
return (rare.looks[state] ??= new Look(this, STATES[state]));
|
|
613
774
|
}
|
|
614
775
|
|
|
615
776
|
/**
|
|
@@ -625,23 +786,50 @@ export class Node {
|
|
|
625
786
|
set id(value) { this.setAttribute("id", String(value)); }
|
|
626
787
|
get className() { return this.getAttribute("class") ?? ""; }
|
|
627
788
|
set className(value) { this.setAttribute("class", String(value)); }
|
|
628
|
-
get classList() { return this.#classList ??= new ClassList(this); }
|
|
789
|
+
get classList() { return this.#more().classList ??= new ClassList(this); }
|
|
629
790
|
|
|
630
791
|
getAttribute(name) {
|
|
631
792
|
name = String(name);
|
|
793
|
+
if (name === "className" || name === "class") return this.#className;
|
|
632
794
|
const aria = ariaName(name);
|
|
633
|
-
|
|
634
|
-
return this.#attributes?.get(name) ?? null;
|
|
795
|
+
return this.#rare?.attributes?.get(aria === null ? name : "aria-" + aria) ?? null;
|
|
635
796
|
}
|
|
636
797
|
|
|
637
798
|
hasAttribute(name) { return this.getAttribute(name) !== null; }
|
|
799
|
+
|
|
800
|
+
getAttributeNames() {
|
|
801
|
+
const names = this.#className === null ? [] : ["class"];
|
|
802
|
+
for (const name of this.#rare?.attributes?.keys() ?? []) names.push(name);
|
|
803
|
+
return names;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/** The children as HTML; setting it replaces them with nodes parsed from HTML. */
|
|
807
|
+
get innerHTML() {
|
|
808
|
+
let html = "";
|
|
809
|
+
for (const child of this.childNodes) html += markupOf(child);
|
|
810
|
+
return html;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
set innerHTML(html) {
|
|
814
|
+
for (const child of this.childNodes.slice()) child.destroy();
|
|
815
|
+
parseHTML(String(html ?? ""), this);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
get outerHTML() {
|
|
819
|
+
return markupOf(this);
|
|
820
|
+
}
|
|
638
821
|
removeAttribute(name) { this.setAttribute(name, null); }
|
|
639
822
|
|
|
640
823
|
setAttribute(name, value) {
|
|
641
824
|
name = String(name);
|
|
825
|
+
let text = value == null ? null : String(value);
|
|
826
|
+
if (name === "className" || name === "class") {
|
|
827
|
+
native.attribute(this.nodeId, "class", text);
|
|
828
|
+
this.#className = text;
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
642
831
|
const aria = ariaName(name);
|
|
643
|
-
|
|
644
|
-
const text = value == null ? null : String(value);
|
|
832
|
+
if (aria !== null) name = "aria-" + aria;
|
|
645
833
|
if (name === "role") {
|
|
646
834
|
if (!native.attribute(this.nodeId, "role", text ?? "")) warnOnce("the tree has no role '" + text + "': it is ignored");
|
|
647
835
|
} else if (name === "id") {
|
|
@@ -654,16 +842,30 @@ export class Node {
|
|
|
654
842
|
else warnOnce("only an image has a src: it is ignored");
|
|
655
843
|
} else if (name === "alt") {
|
|
656
844
|
native.attribute(this.nodeId, "label", text ?? "");
|
|
845
|
+
} else if (this._tag !== undefined && (name === "tabindex" || name === "tabIndex")) {
|
|
846
|
+
this._keep("focus", "blur");
|
|
847
|
+
native.attribute(this.nodeId, "tabIndex", text === null ? -2147483648 : Number(text));
|
|
848
|
+
} else if (this._tag !== undefined && name === "style") {
|
|
849
|
+
this.style.cssText = text ?? "";
|
|
850
|
+
} else if (this._tag !== undefined && BOOLEAN_TREE_ATTRIBUTES.has(name)) {
|
|
851
|
+
// An HTML boolean attribute is on whatever its text, `disabled=""` included.
|
|
852
|
+
const on = value !== null && value !== undefined && value !== false;
|
|
853
|
+
text = on ? "" : null;
|
|
854
|
+
if (name === "checked" && "checked" in this) this.checked = on;
|
|
855
|
+
else native.attribute(this.nodeId, name === "readonly" ? "readOnly" : name, on);
|
|
856
|
+
} else if (this._tag !== undefined && name !== "href" && name !== "placeholder" && name !== "label") {
|
|
857
|
+
// Any other HTML attribute is text the tree keeps for selectors (`[type=checkbox]`).
|
|
858
|
+
native.attribute(this.nodeId, name, text);
|
|
657
859
|
} else {
|
|
658
860
|
if (name === "href") {
|
|
659
861
|
this._href = text || null;
|
|
660
862
|
this.#want(KIND_OF.get("click"));
|
|
661
863
|
}
|
|
662
864
|
native.attribute(this.nodeId, name,
|
|
663
|
-
name === "
|
|
865
|
+
name === "lang" || name === "dir" || name.startsWith("data-") ? text : value);
|
|
664
866
|
}
|
|
665
|
-
if (text === null) this.#attributes?.delete(name);
|
|
666
|
-
else (this.#attributes ??= new Map()).set(name, text);
|
|
867
|
+
if (text === null) this.#rare?.attributes?.delete(name);
|
|
868
|
+
else (this.#more().attributes ??= new Map()).set(name, text);
|
|
667
869
|
}
|
|
668
870
|
|
|
669
871
|
// The `id` other nodes' relations name this node by.
|
|
@@ -676,24 +878,38 @@ export class Node {
|
|
|
676
878
|
|
|
677
879
|
// Tells the tree which events this node wants: its listeners', and clicks on a link.
|
|
678
880
|
#want(kind) {
|
|
679
|
-
const
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
881
|
+
const [wanted, bit] = this.#wanted(kind);
|
|
882
|
+
const rare = this.#rare;
|
|
883
|
+
const old = rare?.flags ?? 0;
|
|
884
|
+
const flags = ((wanted ? old | bit : old & ~bit) | (rare?.kept ?? 0) | (this._extra ?? 0)) >>> 0;
|
|
885
|
+
if (flags !== old) native.listen(this.nodeId, (this.#more().flags = flags));
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// Whether this node's listeners (or link) want `kind`, and the flag bit that carries it.
|
|
889
|
+
#wanted(kind) {
|
|
890
|
+
const listeners = this.#rare?.listeners;
|
|
891
|
+
const has = (type) => (listeners?.get(type)?.length ?? 0) > 0;
|
|
892
|
+
if (kind >= TOUCH_START) return [KINDS_OF_BIT(bitOf(kind)).some((each) => has(TYPES[each])), (1 << bitOf(kind)) >>> 0];
|
|
893
|
+
const type = TYPES[kind];
|
|
894
|
+
const wanted = has(type) || (MOUSE_OF.has(type) && has(MOUSE_OF.get(type))) || (kind === CLICK && (this._href || this._activate))
|
|
895
|
+
|| (kind === SELECTION_CHANGE && has("selectstart"));
|
|
896
|
+
return [wanted, 1 << kind];
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/** Listens again for every kind its listeners, kept kinds and `_extra` flags ask for. */
|
|
900
|
+
_relisten() {
|
|
901
|
+
let flags = (this.#rare?.kept ?? 0) | (this._extra ?? 0);
|
|
902
|
+
for (let kind = 0; kind < TYPES.length; kind++) {
|
|
903
|
+
const [wanted, bit] = this.#wanted(kind);
|
|
904
|
+
if (wanted) flags |= bit;
|
|
690
905
|
}
|
|
691
|
-
|
|
692
|
-
if (flags !== this.#flags) native.listen(this.nodeId, (this.#flags = flags));
|
|
906
|
+
flags >>>= 0;
|
|
907
|
+
if (flags !== (this.#rare?.flags ?? 0)) native.listen(this.nodeId, (this.#more().flags = flags));
|
|
693
908
|
}
|
|
694
909
|
|
|
695
910
|
_keep(...types) {
|
|
696
|
-
|
|
911
|
+
const rare = this.#more();
|
|
912
|
+
for (const type of types) rare.kept = (rare.kept | (1 << bitOf(KIND_OF.get(type)))) >>> 0;
|
|
697
913
|
this.#want(KIND_OF.get(types[0]));
|
|
698
914
|
}
|
|
699
915
|
|
|
@@ -829,9 +1045,9 @@ export class Node {
|
|
|
829
1045
|
if (listener == null || signal?.aborted) return;
|
|
830
1046
|
const kind = KIND_OF.get(LISTENS_AS.get(type) ?? type);
|
|
831
1047
|
const capture = options === true || !!options?.capture;
|
|
832
|
-
this.#listeners ??= new Map();
|
|
833
|
-
let list =
|
|
834
|
-
if (!list)
|
|
1048
|
+
const listeners = (this.#more().listeners ??= new Map());
|
|
1049
|
+
let list = listeners.get(type);
|
|
1050
|
+
if (!list) listeners.set(type, (list = []));
|
|
835
1051
|
if (list.some((entry) => entry.listener === listener && entry.capture === capture)) return;
|
|
836
1052
|
list.push({ listener, capture, once: !!options?.once });
|
|
837
1053
|
if (kind !== undefined) this.#want(kind);
|
|
@@ -839,7 +1055,7 @@ export class Node {
|
|
|
839
1055
|
}
|
|
840
1056
|
|
|
841
1057
|
removeEventListener(type, listener, options) {
|
|
842
|
-
const list = this.#listeners?.get(type);
|
|
1058
|
+
const list = this.#rare?.listeners?.get(type);
|
|
843
1059
|
if (!list) return;
|
|
844
1060
|
const capture = options === true || !!options?.capture;
|
|
845
1061
|
const index = list.findIndex((entry) => entry.listener === listener && entry.capture === capture);
|
|
@@ -857,7 +1073,7 @@ export class Node {
|
|
|
857
1073
|
|
|
858
1074
|
/** Runs the capture or the bubble listeners of `event.type`. Used by the dispatcher. */
|
|
859
1075
|
_run(event, capture) {
|
|
860
|
-
const list = this.#listeners?.get(event.type);
|
|
1076
|
+
const list = this.#rare?.listeners?.get(event.type);
|
|
861
1077
|
if (!list) return;
|
|
862
1078
|
for (const entry of list.slice()) {
|
|
863
1079
|
if (event._immediate) return;
|
|
@@ -894,6 +1110,10 @@ export class Text extends Node {
|
|
|
894
1110
|
return 3;
|
|
895
1111
|
}
|
|
896
1112
|
|
|
1113
|
+
get nodeName() {
|
|
1114
|
+
return "#text";
|
|
1115
|
+
}
|
|
1116
|
+
|
|
897
1117
|
get nodeValue() {
|
|
898
1118
|
return this.#data;
|
|
899
1119
|
}
|
|
@@ -968,6 +1188,8 @@ export class Text extends Node {
|
|
|
968
1188
|
}
|
|
969
1189
|
}
|
|
970
1190
|
|
|
1191
|
+
Object.defineProperty(Text.prototype, "kind", { value: "text", writable: true, configurable: true });
|
|
1192
|
+
|
|
971
1193
|
/**
|
|
972
1194
|
* An editable text field. `value` follows what the user types. Selection offsets are UTF-16
|
|
973
1195
|
* string indices, as on the web; the tree counts UTF-8 bytes, converted here.
|
|
@@ -1211,6 +1433,72 @@ export class Image extends Node {
|
|
|
1211
1433
|
}
|
|
1212
1434
|
}
|
|
1213
1435
|
|
|
1436
|
+
/**
|
|
1437
|
+
* A canvas the application draws with WebGPU, as a `<canvas>`: `width` and `height` are the
|
|
1438
|
+
* size of what is drawn in pixels (300 by 150 at first) and `getContext("webgpu")` gives a
|
|
1439
|
+
* `GPUCanvasContext`. The node lays out as an image of that size does: its style can
|
|
1440
|
+
* stretch it, and `objectFit` says how the drawing fits. Drawing happens on a GPU device of
|
|
1441
|
+
* the application's own, so a canvas that is slow to draw does not slow the rest of the window.
|
|
1442
|
+
*/
|
|
1443
|
+
export class Canvas extends Node {
|
|
1444
|
+
#width = 300;
|
|
1445
|
+
#height = 150;
|
|
1446
|
+
#context = null;
|
|
1447
|
+
|
|
1448
|
+
constructor() {
|
|
1449
|
+
super("canvas");
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
get width() {
|
|
1453
|
+
return this.#width;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
set width(value) {
|
|
1457
|
+
value = Math.max(0, Number(value) | 0);
|
|
1458
|
+
if (value === this.#width) return;
|
|
1459
|
+
this.#width = value;
|
|
1460
|
+
this.#context?._resized();
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
get height() {
|
|
1464
|
+
return this.#height;
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
set height(value) {
|
|
1468
|
+
value = Math.max(0, Number(value) | 0);
|
|
1469
|
+
if (value === this.#height) return;
|
|
1470
|
+
this.#height = value;
|
|
1471
|
+
this.#context?._resized();
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
/** The canvas's `GPUCanvasContext` for "webgpu"; null for any other kind of context. */
|
|
1475
|
+
getContext(kind) {
|
|
1476
|
+
if (kind !== "webgpu") return null;
|
|
1477
|
+
if (this.#context) return this.#context;
|
|
1478
|
+
const api = native.gpuLoad?.();
|
|
1479
|
+
return api ? (this.#context = api.context(this)) : null;
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
setAttribute(name, value) {
|
|
1483
|
+
if (name === "width") this.width = value == null ? 300 : value;
|
|
1484
|
+
else if (name === "height") this.height = value == null ? 150 : value;
|
|
1485
|
+
else super.setAttribute(name, value);
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
getAttribute(name) {
|
|
1489
|
+
if (name === "width") return String(this.#width);
|
|
1490
|
+
if (name === "height") return String(this.#height);
|
|
1491
|
+
return super.getAttribute(name);
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
_shallowClone() {
|
|
1495
|
+
const copy = new Canvas();
|
|
1496
|
+
copy.width = this.#width;
|
|
1497
|
+
copy.height = this.#height;
|
|
1498
|
+
return copy;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1214
1502
|
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
1215
1503
|
|
|
1216
1504
|
// SVG attribute names that are camel case in SVG itself. Any other name with a capital letter
|
|
@@ -1266,6 +1554,38 @@ function svgChanged(node) {
|
|
|
1266
1554
|
changedSvgs.add(svg);
|
|
1267
1555
|
}
|
|
1268
1556
|
|
|
1557
|
+
// A live svg: after its first drawing, an `<svg>` whose elements change sends each change to
|
|
1558
|
+
// the tree as it happens (`svgInsert`, `svgAttribute`, `svgRemove`), and the tree draws its
|
|
1559
|
+
// shapes as paths, so a frame of an animation costs what it changes. This needs content the
|
|
1560
|
+
// tree's live svg draws; anything else keeps the svg drawn as markup.
|
|
1561
|
+
const LIVE_TAGS = new Set([
|
|
1562
|
+
"g", "path", "rect", "circle", "ellipse", "line", "polyline", "polygon", "defs", "clipPath",
|
|
1563
|
+
"linearGradient", "radialGradient", "stop", "title", "desc", "metadata",
|
|
1564
|
+
]);
|
|
1565
|
+
const QUIET_TAGS = new Set(["title", "desc", "metadata"]);
|
|
1566
|
+
const UNLIVE_ATTRIBUTES = new Set(["mask", "filter", "marker", "marker-start", "marker-mid", "marker-end"]);
|
|
1567
|
+
const UNLIVE_STYLE = /(^|;)\s*(mask|filter|marker)[\w-]*\s*:/;
|
|
1568
|
+
|
|
1569
|
+
function liveAttribute(name, text) {
|
|
1570
|
+
return !UNLIVE_ATTRIBUTES.has(name) && !(name === "style" && UNLIVE_STYLE.test(text));
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
/** Whether the tree's live svg can draw `child` and what is in it. */
|
|
1574
|
+
function liveable(child, quiet = false) {
|
|
1575
|
+
if (!(child instanceof SvgElement)) return quiet;
|
|
1576
|
+
if (!LIVE_TAGS.has(child.localName)) return false;
|
|
1577
|
+
for (const [name, value] of child._attributes()) if (!liveAttribute(name, value)) return false;
|
|
1578
|
+
const inner = quiet || QUIET_TAGS.has(child.localName);
|
|
1579
|
+
for (const grandchild of child.childNodes) if (!liveable(grandchild, inner)) return false;
|
|
1580
|
+
return true;
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
/** The live svg `node` is in, or null. */
|
|
1584
|
+
function liveOwner(node) {
|
|
1585
|
+
const svg = svgOwner(node);
|
|
1586
|
+
return svg?._live ? svg : null;
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1269
1589
|
function svgStyle(value) {
|
|
1270
1590
|
if (typeof value !== "object" || value === null) return String(value);
|
|
1271
1591
|
let css = "";
|
|
@@ -1284,15 +1604,20 @@ const svgChildren = {
|
|
|
1284
1604
|
if (before === child) return child;
|
|
1285
1605
|
for (let at = this; at; at = at.parentNode) if (at === child) throw new Error("a node cannot be inserted into itself or its own subtree");
|
|
1286
1606
|
const old = child.parentNode;
|
|
1607
|
+
const listening = shapeListeners(child);
|
|
1287
1608
|
if (old) {
|
|
1609
|
+
liveOwner(old)?._removed(child);
|
|
1288
1610
|
svgChanged(old);
|
|
1289
1611
|
old.childNodes.splice(old.childNodes.indexOf(child), 1);
|
|
1612
|
+
if (listening) svgOwner(old)?._listenShapes();
|
|
1290
1613
|
}
|
|
1291
1614
|
child.parentNode = this;
|
|
1292
1615
|
if (before) this.childNodes.splice(this.childNodes.indexOf(before), 0, child);
|
|
1293
|
-
else this.childNodes.push(child);
|
|
1616
|
+
else (this.childNodes === NO_CHILDREN ? (this.childNodes = []) : this.childNodes).push(child);
|
|
1294
1617
|
if (child instanceof Text) child._svgText = true;
|
|
1618
|
+
liveOwner(this)?._inserted(child, this, before);
|
|
1295
1619
|
svgChanged(this);
|
|
1620
|
+
if (listening) svgOwner(this)?._listenShapes();
|
|
1296
1621
|
return child;
|
|
1297
1622
|
},
|
|
1298
1623
|
appendChild(child) {
|
|
@@ -1303,9 +1628,11 @@ const svgChildren = {
|
|
|
1303
1628
|
},
|
|
1304
1629
|
removeChild(child) {
|
|
1305
1630
|
if (child.parentNode !== this) throw new Error("the node to remove is not a child of this node");
|
|
1631
|
+
liveOwner(this)?._removed(child);
|
|
1306
1632
|
svgChanged(this);
|
|
1307
1633
|
this.childNodes.splice(this.childNodes.indexOf(child), 1);
|
|
1308
1634
|
child.parentNode = null;
|
|
1635
|
+
if (shapeListeners(child)) svgOwner(this)?._listenShapes();
|
|
1309
1636
|
return child;
|
|
1310
1637
|
},
|
|
1311
1638
|
get textContent() {
|
|
@@ -1327,6 +1654,85 @@ const svgChildren = {
|
|
|
1327
1654
|
},
|
|
1328
1655
|
};
|
|
1329
1656
|
|
|
1657
|
+
// `element` and what is inside it as `__ui.svgMeasure` reads them (ui-tree `live_svg.measure`):
|
|
1658
|
+
// records split by U+001E, each the parent's record number, the tag, then name/value pairs.
|
|
1659
|
+
function svgPacked(element) {
|
|
1660
|
+
const records = [];
|
|
1661
|
+
const visit = (at, parent) => {
|
|
1662
|
+
const number = records.length + 1;
|
|
1663
|
+
let record = parent + "\x1f" + at.localName;
|
|
1664
|
+
for (const [name, value] of at._attributes()) record += "\x1f" + name + "\x1f" + value;
|
|
1665
|
+
records.push(record);
|
|
1666
|
+
for (const child of at.childNodes) if (child instanceof SvgElement) visit(child, number);
|
|
1667
|
+
};
|
|
1668
|
+
visit(element, 0);
|
|
1669
|
+
return records.join("\x1e");
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
function svgBox(element) {
|
|
1673
|
+
const [x, y, width, height] = native.svgMeasure(0, svgPacked(element), 0).split(" ").map(Number);
|
|
1674
|
+
const Rect = globalThis.DOMRect;
|
|
1675
|
+
return Rect ? new Rect(x, y, width, height) : { x, y, width, height };
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
// Whether `element` or an element inside it has listeners.
|
|
1679
|
+
function shapeListeners(element) {
|
|
1680
|
+
if (!(element instanceof SvgElement)) return false;
|
|
1681
|
+
for (const list of element._listeners?.values() ?? []) if (list.length > 0) return true;
|
|
1682
|
+
return element.childNodes.some(shapeListeners);
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
function quietAncestor(element) {
|
|
1686
|
+
for (let at = element; at && at instanceof SvgElement; at = at.parentNode) if (QUIET_TAGS.has(at.localName)) return true;
|
|
1687
|
+
return false;
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
/** The declarations of `element`'s `style` attribute, read and written as properties. */
|
|
1691
|
+
function svgDeclarations(element) {
|
|
1692
|
+
const read = () => {
|
|
1693
|
+
const map = new Map();
|
|
1694
|
+
for (const part of (element.getAttribute("style") ?? "").split(";")) {
|
|
1695
|
+
const colon = part.indexOf(":");
|
|
1696
|
+
if (colon > 0) map.set(part.slice(0, colon).trim(), part.slice(colon + 1).trim());
|
|
1697
|
+
}
|
|
1698
|
+
return map;
|
|
1699
|
+
};
|
|
1700
|
+
const write = (map) => {
|
|
1701
|
+
let css = "";
|
|
1702
|
+
for (const [name, value] of map) css += name + ": " + value + ";";
|
|
1703
|
+
if (css) element.setAttribute("style", css);
|
|
1704
|
+
else element.removeAttribute("style");
|
|
1705
|
+
};
|
|
1706
|
+
const set = (name, value) => {
|
|
1707
|
+
const map = read();
|
|
1708
|
+
if (value === null || value === undefined || value === "") map.delete(name);
|
|
1709
|
+
else map.set(name, String(value));
|
|
1710
|
+
write(map);
|
|
1711
|
+
};
|
|
1712
|
+
const methods = {
|
|
1713
|
+
setProperty: (name, value) => set(String(name), value),
|
|
1714
|
+
removeProperty: (name) => {
|
|
1715
|
+
const old = read().get(String(name)) ?? "";
|
|
1716
|
+
set(String(name), null);
|
|
1717
|
+
return old;
|
|
1718
|
+
},
|
|
1719
|
+
getPropertyValue: (name) => read().get(String(name)) ?? "",
|
|
1720
|
+
};
|
|
1721
|
+
return new Proxy(methods, {
|
|
1722
|
+
get(target, key) {
|
|
1723
|
+
if (typeof key !== "string") return undefined;
|
|
1724
|
+
if (key in target) return target[key];
|
|
1725
|
+
if (key === "cssText") return element.getAttribute("style") ?? "";
|
|
1726
|
+
return read().get(cssName(key)) ?? "";
|
|
1727
|
+
},
|
|
1728
|
+
set(target, key, value) {
|
|
1729
|
+
if (key === "cssText") element.setAttribute("style", String(value));
|
|
1730
|
+
else if (typeof key === "string") set(cssName(key), value);
|
|
1731
|
+
return true;
|
|
1732
|
+
},
|
|
1733
|
+
});
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1330
1736
|
/** Text in an `<svg>`: its `<text>` and `<title>` contents. */
|
|
1331
1737
|
export class SvgText {
|
|
1332
1738
|
#data;
|
|
@@ -1345,6 +1751,8 @@ export class SvgText {
|
|
|
1345
1751
|
value = String(value);
|
|
1346
1752
|
if (value === this.#data) return;
|
|
1347
1753
|
this.#data = value;
|
|
1754
|
+
const live = liveOwner(this);
|
|
1755
|
+
if (live && !QUIET_TAGS.has(this.parentNode?.localName)) live._leave();
|
|
1348
1756
|
svgChanged(this);
|
|
1349
1757
|
}
|
|
1350
1758
|
|
|
@@ -1413,11 +1821,40 @@ export class SvgElement {
|
|
|
1413
1821
|
const text = name === "style" ? svgStyle(value) : String(value);
|
|
1414
1822
|
if (this.#attributes.get(name) === text) return;
|
|
1415
1823
|
this.#attributes.set(name, text);
|
|
1416
|
-
|
|
1824
|
+
this._written(name, text);
|
|
1417
1825
|
}
|
|
1418
1826
|
|
|
1419
1827
|
removeAttribute(name) {
|
|
1420
|
-
|
|
1828
|
+
name = svgName(name);
|
|
1829
|
+
if (this.#attributes.delete(name)) this._written(name, null);
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
setAttributeNS(namespace, name, value) {
|
|
1833
|
+
this.setAttribute(name, value);
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
getAttributeNS(namespace, name) {
|
|
1837
|
+
return this.getAttribute(name);
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
removeAttributeNS(namespace, name) {
|
|
1841
|
+
this.removeAttribute(name);
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
/** The element's `style` attribute as a declaration object: `style.display = "none"`. */
|
|
1845
|
+
get style() {
|
|
1846
|
+
return this._style ??= svgDeclarations(this);
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
_attributes() {
|
|
1850
|
+
return this.#attributes;
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
_written(name, text) {
|
|
1854
|
+
const live = liveOwner(this);
|
|
1855
|
+
if (!live) svgChanged(this);
|
|
1856
|
+
else if (text !== null && !liveAttribute(name, text)) live._leave();
|
|
1857
|
+
else native.svgAttribute(live.nodeId, this._n, name, text);
|
|
1421
1858
|
}
|
|
1422
1859
|
|
|
1423
1860
|
get firstChild() {
|
|
@@ -1452,11 +1889,67 @@ export class SvgElement {
|
|
|
1452
1889
|
this.remove();
|
|
1453
1890
|
}
|
|
1454
1891
|
|
|
1455
|
-
|
|
1456
|
-
|
|
1892
|
+
/**
|
|
1893
|
+
* Hears pointer events on this element's shapes, as on the web: its `<svg>` turns live (see
|
|
1894
|
+
* `Svg`) and the tree tells which shape is under the pointer.
|
|
1895
|
+
*/
|
|
1896
|
+
addEventListener(type, listener, options) {
|
|
1897
|
+
if (listener == null) return;
|
|
1898
|
+
const capture = options === true || !!options?.capture;
|
|
1899
|
+
this._listeners ??= new Map();
|
|
1900
|
+
let list = this._listeners.get(type);
|
|
1901
|
+
if (!list) this._listeners.set(type, (list = []));
|
|
1902
|
+
if (list.some((entry) => entry.listener === listener && entry.capture === capture)) return;
|
|
1903
|
+
list.push({ listener, capture, once: !!options?.once });
|
|
1904
|
+
svgOwner(this)?._listenShapes();
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
removeEventListener(type, listener, options) {
|
|
1908
|
+
const list = this._listeners?.get(type);
|
|
1909
|
+
if (!list) return;
|
|
1910
|
+
const capture = options === true || !!options?.capture;
|
|
1911
|
+
const index = list.findIndex((entry) => entry.listener === listener && entry.capture === capture);
|
|
1912
|
+
if (index < 0) return;
|
|
1913
|
+
list.splice(index, 1);
|
|
1914
|
+
svgOwner(this)?._listenShapes();
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
dispatchEvent(event) {
|
|
1918
|
+
if (typeof event?.type !== "string") throw new TypeError("dispatchEvent takes an Event");
|
|
1919
|
+
return dispatchEvent(adopt(event, this));
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
/** The box of its geometry in its own user space: a shape's fill, a group's children. */
|
|
1923
|
+
getBBox() {
|
|
1924
|
+
return svgBox(this);
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
/** The length of a shape's outline in user units. */
|
|
1928
|
+
getTotalLength() {
|
|
1929
|
+
return Number(native.svgMeasure(1, svgPacked(this), 0));
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
/** The point `distance` along a shape's outline, clamped to its ends. */
|
|
1933
|
+
getPointAtLength(distance) {
|
|
1934
|
+
const [x, y] = native.svgMeasure(2, svgPacked(this), Number(distance) || 0).split(" ").map(Number);
|
|
1935
|
+
return { x, y };
|
|
1457
1936
|
}
|
|
1458
1937
|
|
|
1459
|
-
|
|
1938
|
+
_run(event, capture) {
|
|
1939
|
+
const list = this._listeners?.get(event.type);
|
|
1940
|
+
if (!list) return;
|
|
1941
|
+
for (const entry of list.slice()) {
|
|
1942
|
+
if (event._immediate) return;
|
|
1943
|
+
if (entry.capture !== capture) continue;
|
|
1944
|
+
if (entry.once) this.removeEventListener(event.type, entry.listener, entry.capture);
|
|
1945
|
+
try {
|
|
1946
|
+
if (typeof entry.listener === "function") entry.listener.call(this, event);
|
|
1947
|
+
else entry.listener.handleEvent(event);
|
|
1948
|
+
} catch (error) {
|
|
1949
|
+
reportError(error);
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1460
1953
|
|
|
1461
1954
|
_markup() {
|
|
1462
1955
|
let markup = "<" + this.localName;
|
|
@@ -1486,13 +1979,15 @@ function treeAttribute(name) {
|
|
|
1486
1979
|
export class Svg extends Node {
|
|
1487
1980
|
#attributes = new Map();
|
|
1488
1981
|
#markup = "";
|
|
1982
|
+
// Live: element numbers in use, and freed ones to use again.
|
|
1983
|
+
_live = false;
|
|
1984
|
+
_count = 0;
|
|
1985
|
+
_free = [];
|
|
1489
1986
|
|
|
1490
1987
|
constructor() {
|
|
1491
1988
|
// The tree's node for drawn SVG markup is an icon; to code it is an `svg`.
|
|
1492
1989
|
super("icon");
|
|
1493
1990
|
this.kind = "svg";
|
|
1494
|
-
this.localName = "svg";
|
|
1495
|
-
this.tagName = "svg";
|
|
1496
1991
|
this.namespaceURI = SVG_NAMESPACE;
|
|
1497
1992
|
svgChanged(this);
|
|
1498
1993
|
}
|
|
@@ -1520,13 +2015,84 @@ export class Svg extends Node {
|
|
|
1520
2015
|
const text = name === "style" ? svgStyle(value) : String(value);
|
|
1521
2016
|
if (this.#attributes.get(name) === text) return;
|
|
1522
2017
|
this.#attributes.set(name, text);
|
|
1523
|
-
|
|
2018
|
+
this._written(name, text);
|
|
1524
2019
|
}
|
|
1525
2020
|
|
|
1526
2021
|
removeAttribute(name) {
|
|
1527
2022
|
name = String(name);
|
|
1528
2023
|
if (treeAttribute(name)) super.removeAttribute(name);
|
|
1529
|
-
else if (this.#attributes.delete(svgName(name)))
|
|
2024
|
+
else if (this.#attributes.delete(svgName(name))) this._written(svgName(name), null);
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
setAttributeNS(namespace, name, value) {
|
|
2028
|
+
this.setAttribute(name, value);
|
|
2029
|
+
}
|
|
2030
|
+
|
|
2031
|
+
getAttributeNS(namespace, name) {
|
|
2032
|
+
return this.getAttribute(name);
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
/** The box of what it draws, in its user space (inside `viewBox`). */
|
|
2036
|
+
getBBox() {
|
|
2037
|
+
return svgBox(this);
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
_attributes() {
|
|
2041
|
+
return this.#attributes;
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
_written(name, text) {
|
|
2045
|
+
if (!this._live) svgChanged(this);
|
|
2046
|
+
else if (text !== null && !liveAttribute(name, text)) this._leave();
|
|
2047
|
+
else native.svgAttribute(this.nodeId, 0, name, text);
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
/** Sends the elements to the tree as a live svg, once its markup was drawn and changed. */
|
|
2051
|
+
_goLive() {
|
|
2052
|
+
this._forget();
|
|
2053
|
+
this._live = true;
|
|
2054
|
+
for (const [name, value] of this.#attributes) native.svgAttribute(this.nodeId, 0, name, value);
|
|
2055
|
+
for (const child of this.childNodes) this._inserted(child, this, null);
|
|
2056
|
+
}
|
|
2057
|
+
|
|
2058
|
+
/** Back to markup: content the live svg does not draw came in. */
|
|
2059
|
+
_leave() {
|
|
2060
|
+
if (!this._live) return;
|
|
2061
|
+
this._live = false;
|
|
2062
|
+
this._count = 0;
|
|
2063
|
+
this._free = [];
|
|
2064
|
+
native.svgRemove(this.nodeId, 0);
|
|
2065
|
+
svgChanged(this);
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
_inserted(child, parent, before) {
|
|
2069
|
+
if (!(child instanceof SvgElement)) {
|
|
2070
|
+
if (!QUIET_TAGS.has(parent.localName) && !(parent !== this && svgOwner(parent) && quietAncestor(parent))) this._leave();
|
|
2071
|
+
return;
|
|
2072
|
+
}
|
|
2073
|
+
if (!liveable(child, quietAncestor(parent))) {
|
|
2074
|
+
this._leave();
|
|
2075
|
+
return;
|
|
2076
|
+
}
|
|
2077
|
+
this._send(child, parent === this ? 0 : parent._n, before instanceof SvgElement ? before._n : -1);
|
|
2078
|
+
}
|
|
2079
|
+
|
|
2080
|
+
_send(element, parent, before) {
|
|
2081
|
+
element._n = this._free.pop() ?? ++this._count;
|
|
2082
|
+
native.svgInsert(this.nodeId, element._n, element.localName, parent, before);
|
|
2083
|
+
for (const [name, value] of element._attributes()) native.svgAttribute(this.nodeId, element._n, name, value);
|
|
2084
|
+
for (const child of element.childNodes) if (child instanceof SvgElement) this._send(child, element._n, -1);
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
_removed(child) {
|
|
2088
|
+
if (!(child instanceof SvgElement) || !child._n) return;
|
|
2089
|
+
native.svgRemove(this.nodeId, child._n);
|
|
2090
|
+
const free = (element) => {
|
|
2091
|
+
if (element._n) this._free.push(element._n);
|
|
2092
|
+
element._n = 0;
|
|
2093
|
+
for (const grandchild of element.childNodes) if (grandchild instanceof SvgElement) free(grandchild);
|
|
2094
|
+
};
|
|
2095
|
+
free(child);
|
|
1530
2096
|
}
|
|
1531
2097
|
|
|
1532
2098
|
/** The markup the tree draws, as `outerHTML` gives it. */
|
|
@@ -1542,9 +2108,17 @@ export class Svg extends Node {
|
|
|
1542
2108
|
}
|
|
1543
2109
|
|
|
1544
2110
|
_draw() {
|
|
1545
|
-
if (!nodes.has(this.nodeId)) return;
|
|
2111
|
+
if (!nodes.has(this.nodeId) || this._live) return;
|
|
2112
|
+
if (this._extra && this.childNodes.every((child) => liveable(child))) {
|
|
2113
|
+
this._goLive();
|
|
2114
|
+
return;
|
|
2115
|
+
}
|
|
1546
2116
|
const markup = this.outerHTML;
|
|
1547
2117
|
if (markup === this.#markup) return;
|
|
2118
|
+
if (this.#markup && this.childNodes.every((child) => liveable(child))) {
|
|
2119
|
+
this._goLive();
|
|
2120
|
+
return;
|
|
2121
|
+
}
|
|
1548
2122
|
this._forget();
|
|
1549
2123
|
this.#markup = markup;
|
|
1550
2124
|
native.source(this.nodeId, holdIcon(markup));
|
|
@@ -1554,6 +2128,117 @@ export class Svg extends Node {
|
|
|
1554
2128
|
if (this.#markup) dropIcon(this.#markup);
|
|
1555
2129
|
this.#markup = "";
|
|
1556
2130
|
}
|
|
2131
|
+
|
|
2132
|
+
/**
|
|
2133
|
+
* Listens for what the listeners on its elements need. Elements are told apart only once
|
|
2134
|
+
* the svg is live, so listeners make it go live.
|
|
2135
|
+
*/
|
|
2136
|
+
_listenShapes() {
|
|
2137
|
+
let types = new Set();
|
|
2138
|
+
const visit = (element) => {
|
|
2139
|
+
for (const [type, list] of element._listeners ?? []) if (list.length > 0) types.add(type);
|
|
2140
|
+
for (const child of element.childNodes) if (child instanceof SvgElement) visit(child);
|
|
2141
|
+
};
|
|
2142
|
+
for (const child of this.childNodes) if (child instanceof SvgElement) visit(child);
|
|
2143
|
+
let extra = 0;
|
|
2144
|
+
for (const type of types) {
|
|
2145
|
+
const kind = KIND_OF.get(type);
|
|
2146
|
+
if (kind === undefined) continue;
|
|
2147
|
+
extra = (extra | (1 << bitOf(kind))) >>> 0;
|
|
2148
|
+
if (HOVER_TYPES.has(type)) extra |= HOVER_FLAGS;
|
|
2149
|
+
}
|
|
2150
|
+
if (extra === (this._extra ?? 0)) return;
|
|
2151
|
+
this._extra = extra;
|
|
2152
|
+
this._relisten();
|
|
2153
|
+
if (extra && !this._live) svgChanged(this);
|
|
2154
|
+
}
|
|
2155
|
+
|
|
2156
|
+
/** The element numbered `n`, or null. */
|
|
2157
|
+
_numbered(n) {
|
|
2158
|
+
if (!n) return null;
|
|
2159
|
+
const find = (element) => {
|
|
2160
|
+
if (element._n === n) return element;
|
|
2161
|
+
for (const child of element.childNodes) {
|
|
2162
|
+
if (child instanceof SvgElement) {
|
|
2163
|
+
const found = find(child);
|
|
2164
|
+
if (found) return found;
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
return null;
|
|
2168
|
+
};
|
|
2169
|
+
for (const child of this.childNodes) {
|
|
2170
|
+
if (child instanceof SvgElement) {
|
|
2171
|
+
const found = find(child);
|
|
2172
|
+
if (found) return found;
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
return null;
|
|
2176
|
+
}
|
|
2177
|
+
|
|
2178
|
+
/**
|
|
2179
|
+
* Sends a pointer record that came to the svg to the element under the pointer, with the
|
|
2180
|
+
* over, out, enter and leave events the web sends as the pointer moves between elements.
|
|
2181
|
+
*/
|
|
2182
|
+
_pointer(event) {
|
|
2183
|
+
const type = event.type;
|
|
2184
|
+
const leaving = type === "pointerout" || type === "pointerleave" || type === "pointercancel";
|
|
2185
|
+
const shape = leaving ? null : this._numbered(event._element);
|
|
2186
|
+
const old = this._hovered && this._hovered.parentNode && svgOwner(this._hovered) === this ? this._hovered : null;
|
|
2187
|
+
let replaced = false;
|
|
2188
|
+
if (shape !== old && type !== "pointerenter" && type !== "pointerleave") {
|
|
2189
|
+
this._hovered = shape;
|
|
2190
|
+
if (old) {
|
|
2191
|
+
dispatchEvent(pointerCopy("pointerout", old, event));
|
|
2192
|
+
for (let at = old; at instanceof SvgElement && !at.contains(shape); at = at.parentNode) {
|
|
2193
|
+
dispatchEvent(pointerCopy("pointerleave", at, event));
|
|
2194
|
+
}
|
|
2195
|
+
if (type === "pointerout") replaced = true;
|
|
2196
|
+
}
|
|
2197
|
+
if (shape) {
|
|
2198
|
+
dispatchEvent(pointerCopy("pointerover", shape, event));
|
|
2199
|
+
if (type === "pointerover") {
|
|
2200
|
+
replaced = true;
|
|
2201
|
+
this._entering = true;
|
|
2202
|
+
} else {
|
|
2203
|
+
enterChain(shape, old, event);
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
if (replaced) return false;
|
|
2208
|
+
if (type === "pointerenter") {
|
|
2209
|
+
dispatchEvent(event);
|
|
2210
|
+
if (this._entering && this._hovered) enterChain(this._hovered, null, event);
|
|
2211
|
+
this._entering = false;
|
|
2212
|
+
return event.defaultPrevented;
|
|
2213
|
+
}
|
|
2214
|
+
if (shape && type !== "pointerleave") event.target = shape;
|
|
2215
|
+
dispatchEvent(event);
|
|
2216
|
+
return event.defaultPrevented;
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
// Element listeners for these need the svg's moves, enters and leaves to tell hovering apart.
|
|
2221
|
+
const HOVER_TYPES = new Set(["pointerover", "pointerout", "pointerenter", "pointerleave"]);
|
|
2222
|
+
const HOVER_FLAGS = (1 << 3) | (1 << 4) | (1 << 5) | (1 << 28) | (1 << 29);
|
|
2223
|
+
|
|
2224
|
+
// A pointer event like `base` of type `type` at `target`.
|
|
2225
|
+
function pointerCopy(type, target, base) {
|
|
2226
|
+
const event = new Event(type, target);
|
|
2227
|
+
for (const key of Object.keys(base)) {
|
|
2228
|
+
if (!(key in event) || key === "clientX" || key === "clientY" || key === "x" || key === "y") event[key] = base[key];
|
|
2229
|
+
}
|
|
2230
|
+
event.type = type;
|
|
2231
|
+
event.target = target;
|
|
2232
|
+
event.bubbles = !NO_BUBBLE.has(type);
|
|
2233
|
+
event.cancelable = CANCELABLE.has(type);
|
|
2234
|
+
return event;
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
// pointerenter at `shape` and its element ancestors not containing `from`, outermost first.
|
|
2238
|
+
function enterChain(shape, from, base) {
|
|
2239
|
+
const chain = [];
|
|
2240
|
+
for (let at = shape; at instanceof SvgElement && !(from && at.contains(from)); at = at.parentNode) chain.push(at);
|
|
2241
|
+
for (let i = chain.length - 1; i >= 0; i--) dispatchEvent(pointerCopy("pointerenter", chain[i], base));
|
|
1557
2242
|
}
|
|
1558
2243
|
|
|
1559
2244
|
for (const name of ["insertBefore", "appendChild", "append", "removeChild"]) Svg.prototype[name] = svgChildren[name];
|
|
@@ -1790,7 +2475,7 @@ function forget(node) {
|
|
|
1790
2475
|
child.parentNode = null;
|
|
1791
2476
|
forget(child);
|
|
1792
2477
|
}
|
|
1793
|
-
node.childNodes =
|
|
2478
|
+
node.childNodes = NO_CHILDREN;
|
|
1794
2479
|
}
|
|
1795
2480
|
|
|
1796
2481
|
export function createNode(kind) {
|
|
@@ -1801,27 +2486,300 @@ export function createNode(kind) {
|
|
|
1801
2486
|
if (kind === "text") return new Text();
|
|
1802
2487
|
if (kind === "input") return new Input();
|
|
1803
2488
|
if (kind === "image") return new Image();
|
|
2489
|
+
if (kind === "canvas") return new Canvas();
|
|
1804
2490
|
return new Node(kind);
|
|
1805
2491
|
}
|
|
1806
2492
|
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
box.append(...children);
|
|
1812
|
-
return box;
|
|
1813
|
-
}
|
|
2493
|
+
// `<input>` types that are not a text field.
|
|
2494
|
+
const BOX_INPUTS = new Set(["checkbox", "radio", "range", "color", "file", "hidden", "submit", "button", "reset", "image"]);
|
|
2495
|
+
// Tags that take focus without a tabindex, as on the web.
|
|
2496
|
+
const FOCUSABLE_TAGS = new Set(["button", "select", "summary", "iframe"]);
|
|
1814
2497
|
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
2498
|
+
/**
|
|
2499
|
+
* `<input type="checkbox">` and `type="radio"`: a box a click checks, as on the web. It fires
|
|
2500
|
+
* `input` and `change` when the click changes it.
|
|
2501
|
+
*/
|
|
2502
|
+
export class CheckInput extends Node {
|
|
2503
|
+
#checked = false;
|
|
1818
2504
|
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
2505
|
+
constructor(type = "checkbox") {
|
|
2506
|
+
super("box");
|
|
2507
|
+
this._tag = "input";
|
|
2508
|
+
this.type = type;
|
|
2509
|
+
this.value = "on";
|
|
2510
|
+
this.defaultChecked = false;
|
|
2511
|
+
native.attribute(this.nodeId, "element", "input");
|
|
2512
|
+
native.attribute(this.nodeId, "type", type);
|
|
2513
|
+
native.attribute(this.nodeId, "role", type === "radio" ? "radio" : "checkbox");
|
|
2514
|
+
native.attribute(this.nodeId, "tabIndex", 0);
|
|
2515
|
+
this._keep("focus", "blur");
|
|
2516
|
+
this._relisten();
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
_shallowClone() {
|
|
2520
|
+
const copy = new CheckInput(this.type);
|
|
2521
|
+
copy.checked = this.#checked;
|
|
2522
|
+
return copy;
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
get name() {
|
|
2526
|
+
return this.getAttribute("name") ?? "";
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
get checked() {
|
|
2530
|
+
return this.#checked;
|
|
2531
|
+
}
|
|
2532
|
+
|
|
2533
|
+
set checked(value) {
|
|
2534
|
+
value = !!value;
|
|
2535
|
+
if (value === this.#checked) return;
|
|
2536
|
+
this.#checked = value;
|
|
2537
|
+
native.attribute(this.nodeId, "checked", value);
|
|
2538
|
+
if (value && this.type === "radio" && this.name) {
|
|
2539
|
+
let top = this;
|
|
2540
|
+
while (top.parentNode) top = top.parentNode;
|
|
2541
|
+
eachDescendant(top, (node) => {
|
|
2542
|
+
if (node !== this && node instanceof CheckInput && node.type === "radio" && node.name === this.name) node.checked = false;
|
|
2543
|
+
});
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
|
|
2547
|
+
_activate() {
|
|
2548
|
+
if (this.hasAttribute("disabled")) return true;
|
|
2549
|
+
if (this.type === "radio" && this.#checked) return true;
|
|
2550
|
+
this.checked = this.type === "radio" ? true : !this.#checked;
|
|
2551
|
+
const input = new Event("input", this);
|
|
2552
|
+
input.bubbles = true;
|
|
2553
|
+
dispatchEvent(input);
|
|
2554
|
+
const change = new Event("change", this);
|
|
2555
|
+
change.bubbles = true;
|
|
2556
|
+
dispatchEvent(change);
|
|
2557
|
+
return true;
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
|
|
2561
|
+
function eachDescendant(node, visit) {
|
|
2562
|
+
for (const child of node.childNodes) {
|
|
2563
|
+
visit(child);
|
|
2564
|
+
if (child.childNodes.length) eachDescendant(child, visit);
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
|
|
2568
|
+
function findById(id) {
|
|
2569
|
+
for (const node of nodes.values()) if (node._elementId === id && (node.parentNode || node === rootNode)) return node;
|
|
2570
|
+
return null;
|
|
2571
|
+
}
|
|
2572
|
+
|
|
2573
|
+
// What a click on a label, summary or submit button does (see `activate`).
|
|
2574
|
+
function labelActivate() {
|
|
2575
|
+
const id = this.getAttribute("for");
|
|
2576
|
+
let control = id ? findById(id) : null;
|
|
2577
|
+
if (!control) eachDescendant(this, (node) => {
|
|
2578
|
+
if (!control && (node instanceof Input || node instanceof CheckInput)) control = node;
|
|
2579
|
+
});
|
|
2580
|
+
if (!control) return false;
|
|
2581
|
+
if (control instanceof CheckInput) control._activate();
|
|
2582
|
+
else control.focus();
|
|
2583
|
+
return true;
|
|
2584
|
+
}
|
|
2585
|
+
|
|
2586
|
+
function summaryActivate() {
|
|
2587
|
+
const details = this.parentNode;
|
|
2588
|
+
if (details?.localName !== "details") return false;
|
|
2589
|
+
details.setAttribute("open", details.hasAttribute("open") ? null : "");
|
|
2590
|
+
const toggle = new Event("toggle", details);
|
|
2591
|
+
dispatchEvent(toggle);
|
|
2592
|
+
return true;
|
|
2593
|
+
}
|
|
2594
|
+
|
|
2595
|
+
function buttonActivate() {
|
|
2596
|
+
if (this.hasAttribute("disabled")) return true;
|
|
2597
|
+
const type = this.getAttribute("type") ?? "submit";
|
|
2598
|
+
if (type !== "submit" && type !== "reset") return false;
|
|
2599
|
+
let form = this.parentNode;
|
|
2600
|
+
while (form && form.localName !== "form") form = form.parentNode;
|
|
2601
|
+
if (!form) return false;
|
|
2602
|
+
const event = new Event(type, form);
|
|
2603
|
+
event.bubbles = true;
|
|
2604
|
+
event.cancelable = true;
|
|
2605
|
+
event.submitter = this;
|
|
2606
|
+
dispatchEvent(event);
|
|
2607
|
+
return true;
|
|
2608
|
+
}
|
|
2609
|
+
|
|
2610
|
+
/**
|
|
2611
|
+
* The node for HTML element `tag`, as `document.createElement` makes it: `<input>` and
|
|
2612
|
+
* `<textarea>` are text fields (an `<input>` whose `type` is not a text field's is a box),
|
|
2613
|
+
* `<img>` an image, `<canvas>` a canvas, `<svg>` inline SVG, and every other tag a box the
|
|
2614
|
+
* tree knows by its tag, for selectors, its implicit role and the default stylesheet of
|
|
2615
|
+
* `@silkui/react`. `localName` and `tagName` give the tag.
|
|
2616
|
+
*/
|
|
2617
|
+
export function createElement(tag, options) {
|
|
2618
|
+
tag = String(tag).toLowerCase();
|
|
2619
|
+
const type = String(options?.type ?? "text").toLowerCase();
|
|
2620
|
+
let node;
|
|
2621
|
+
if (tag === "svg") return new Svg();
|
|
2622
|
+
if (tag === "input" && (type === "checkbox" || type === "radio")) return new CheckInput(type);
|
|
2623
|
+
if (tag === "input" && !BOX_INPUTS.has(type)) node = new Input();
|
|
2624
|
+
else if (tag === "textarea") node = new Input();
|
|
2625
|
+
else if (tag === "img") node = new Image();
|
|
2626
|
+
else if (tag === "canvas") node = new Canvas();
|
|
2627
|
+
else node = new Node("box");
|
|
2628
|
+
node._tag = tag;
|
|
2629
|
+
native.attribute(node.nodeId, "element", tag);
|
|
2630
|
+
if (tag === "textarea") native.attribute(node.nodeId, "multiline", true);
|
|
2631
|
+
if (tag === "input" && type !== "text") native.attribute(node.nodeId, "type", type);
|
|
2632
|
+
if (node instanceof Input || FOCUSABLE_TAGS.has(tag)) {
|
|
2633
|
+
if (!(node instanceof Input)) native.attribute(node.nodeId, "tabIndex", 0);
|
|
2634
|
+
node._keep("focus", "blur");
|
|
2635
|
+
}
|
|
2636
|
+
if (tag === "label") node._activate = labelActivate;
|
|
2637
|
+
else if (tag === "summary") node._activate = summaryActivate;
|
|
2638
|
+
else if (tag === "button") node._activate = buttonActivate;
|
|
2639
|
+
if (node._activate) node._relisten();
|
|
2640
|
+
return node;
|
|
2641
|
+
}
|
|
2642
|
+
|
|
2643
|
+
const VOID_TAGS = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr"]);
|
|
2644
|
+
// Elements whose content is not markup. Scripts and styles in parsed HTML are left out.
|
|
2645
|
+
const RAW_TAGS = new Set(["script", "style", "textarea", "title"]);
|
|
2646
|
+
const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " ", copy: "©", reg: "®", hellip: "…", mdash: "—", ndash: "–", laquo: "«", raquo: "»", times: "×", middot: "·", bull: "•" };
|
|
2647
|
+
const ATTRIBUTE = /\s*([^\s"'>\/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/y;
|
|
2648
|
+
|
|
2649
|
+
function decodeEntities(text) {
|
|
2650
|
+
if (!text.includes("&")) return text;
|
|
2651
|
+
return text.replace(/&(#[xX][0-9a-fA-F]+|#[0-9]+|[a-zA-Z]+);/g, (whole, name) => {
|
|
2652
|
+
if (name[0] === "#") {
|
|
2653
|
+
const code = name[1] === "x" || name[1] === "X" ? parseInt(name.slice(2), 16) : parseInt(name.slice(1), 10);
|
|
2654
|
+
return Number.isFinite(code) ? String.fromCodePoint(code) : whole;
|
|
2655
|
+
}
|
|
2656
|
+
return ENTITIES[name] ?? whole;
|
|
2657
|
+
});
|
|
2658
|
+
}
|
|
2659
|
+
|
|
2660
|
+
const escapeText = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/ /g, " ");
|
|
2661
|
+
const escapeAttribute = (text) => text.replace(/&/g, "&").replace(/"/g, """);
|
|
2662
|
+
|
|
2663
|
+
function markupOf(node) {
|
|
2664
|
+
if (node.nodeType === 3) return escapeText(node.data);
|
|
2665
|
+
if (node instanceof Svg || node instanceof SvgElement) return node.outerHTML ?? node._markup();
|
|
2666
|
+
if (node.nodeType === 11) return node.innerHTML;
|
|
2667
|
+
const tag = node.localName;
|
|
2668
|
+
let html = "<" + tag;
|
|
2669
|
+
for (const name of node.getAttributeNames()) if (name !== "style") html += " " + name + '="' + escapeAttribute(node.getAttribute(name)) + '"';
|
|
2670
|
+
const css = node.look("base").cssText;
|
|
2671
|
+
if (css) html += ' style="' + escapeAttribute(css) + '"';
|
|
2672
|
+
html += ">";
|
|
2673
|
+
if (VOID_TAGS.has(tag)) return html;
|
|
2674
|
+
if (node instanceof Input) html += escapeText(node.value);
|
|
2675
|
+
else html += node.innerHTML;
|
|
2676
|
+
return html + "</" + tag + ">";
|
|
2677
|
+
}
|
|
2678
|
+
|
|
2679
|
+
/**
|
|
2680
|
+
* Parses `html` and appends its nodes to `parent`, as `innerHTML` does: elements, text with
|
|
2681
|
+
* character references, and `<svg>` markup. Comments, scripts and styles are left out.
|
|
2682
|
+
*/
|
|
2683
|
+
export function parseHTML(html, parent) {
|
|
2684
|
+
const stack = [parent];
|
|
2685
|
+
const top = () => stack[stack.length - 1];
|
|
2686
|
+
const inSvg = () => top() instanceof Svg || top() instanceof SvgElement;
|
|
2687
|
+
let i = 0;
|
|
2688
|
+
const text = (data) => {
|
|
2689
|
+
if (!data) return;
|
|
2690
|
+
const value = decodeEntities(data);
|
|
2691
|
+
top().appendChild(inSvg() ? new SvgText(value) : createText(value));
|
|
2692
|
+
};
|
|
2693
|
+
while (i < html.length) {
|
|
2694
|
+
const open = html.indexOf("<", i);
|
|
2695
|
+
if (open < 0) {
|
|
2696
|
+
text(html.slice(i));
|
|
2697
|
+
break;
|
|
2698
|
+
}
|
|
2699
|
+
text(html.slice(i, open));
|
|
2700
|
+
if (html.startsWith("<!--", open)) {
|
|
2701
|
+
const end = html.indexOf("-->", open + 4);
|
|
2702
|
+
i = end < 0 ? html.length : end + 3;
|
|
2703
|
+
continue;
|
|
2704
|
+
}
|
|
2705
|
+
if (html[open + 1] === "!" || html[open + 1] === "?") {
|
|
2706
|
+
const end = html.indexOf(">", open);
|
|
2707
|
+
i = end < 0 ? html.length : end + 1;
|
|
2708
|
+
continue;
|
|
2709
|
+
}
|
|
2710
|
+
if (html[open + 1] === "/") {
|
|
2711
|
+
const end = html.indexOf(">", open);
|
|
2712
|
+
const name = html.slice(open + 2, end < 0 ? html.length : end).trim().toLowerCase();
|
|
2713
|
+
i = end < 0 ? html.length : end + 1;
|
|
2714
|
+
for (let at = stack.length - 1; at > 0; at--) {
|
|
2715
|
+
if (stack[at].localName.toLowerCase() === name) {
|
|
2716
|
+
stack.length = at;
|
|
2717
|
+
break;
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
continue;
|
|
2721
|
+
}
|
|
2722
|
+
const nameMatch = /^[a-zA-Z][^\s\/>]*/.exec(html.slice(open + 1, open + 64));
|
|
2723
|
+
if (!nameMatch) {
|
|
2724
|
+
text("<");
|
|
2725
|
+
i = open + 1;
|
|
2726
|
+
continue;
|
|
2727
|
+
}
|
|
2728
|
+
const written = nameMatch[0];
|
|
2729
|
+
const tag = inSvg() ? written : written.toLowerCase();
|
|
2730
|
+
let at = open + 1 + written.length;
|
|
2731
|
+
const attributes = [];
|
|
2732
|
+
ATTRIBUTE.lastIndex = at;
|
|
2733
|
+
for (let match; (match = ATTRIBUTE.exec(html)); ATTRIBUTE.lastIndex = at) {
|
|
2734
|
+
at = ATTRIBUTE.lastIndex;
|
|
2735
|
+
attributes.push([match[1], decodeEntities(match[2] ?? match[3] ?? match[4] ?? "")]);
|
|
2736
|
+
}
|
|
2737
|
+
while (at < html.length && html[at] !== ">") at++;
|
|
2738
|
+
const selfClosing = html[at - 1] === "/";
|
|
2739
|
+
i = at + 1;
|
|
2740
|
+
if (RAW_TAGS.has(tag)) {
|
|
2741
|
+
const close = html.toLowerCase().indexOf("</" + tag, i);
|
|
2742
|
+
const content = html.slice(i, close < 0 ? html.length : close);
|
|
2743
|
+
const end = close < 0 ? html.length : html.indexOf(">", close);
|
|
2744
|
+
i = end < 0 ? html.length : end + 1;
|
|
2745
|
+
if (tag === "script" || tag === "style") continue;
|
|
2746
|
+
const node = createElement(tag);
|
|
2747
|
+
for (const [name, value] of attributes) node.setAttribute(name, value);
|
|
2748
|
+
if (tag === "textarea") node.value = decodeEntities(content);
|
|
2749
|
+
else node.appendChild(createText(decodeEntities(content)));
|
|
2750
|
+
top().appendChild(node);
|
|
2751
|
+
continue;
|
|
2752
|
+
}
|
|
2753
|
+
const svg = inSvg() || tag === "svg";
|
|
2754
|
+
const type = attributes.find(([name]) => name.toLowerCase() === "type")?.[1];
|
|
2755
|
+
const node = svg ? createSvgElement(tag) : createElement(tag, { type });
|
|
2756
|
+
for (const [name, value] of attributes) {
|
|
2757
|
+
if (!svg && name.toLowerCase() === "value" && node instanceof Input) node.value = value;
|
|
2758
|
+
else node.setAttribute(svg ? name : name.toLowerCase(), value);
|
|
2759
|
+
}
|
|
2760
|
+
top().appendChild(node);
|
|
2761
|
+
if (!selfClosing && !(VOID_TAGS.has(tag) && !svg)) stack.push(node);
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
|
|
2765
|
+
/** A box with `style` as its base look and `children` appended. */
|
|
2766
|
+
export function createBox(style, ...children) {
|
|
2767
|
+
const box = new Node("box");
|
|
2768
|
+
if (style) box.style.set(style);
|
|
2769
|
+
box.append(...children);
|
|
2770
|
+
return box;
|
|
2771
|
+
}
|
|
2772
|
+
|
|
2773
|
+
export function createText(data = "") {
|
|
2774
|
+
return new Text(data);
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
/** An image showing `src` (see `Image`), with `style` as its base look. */
|
|
2778
|
+
export function createImage(src, style) {
|
|
2779
|
+
const image = new Image(src);
|
|
2780
|
+
if (style) image.style.set(style);
|
|
2781
|
+
return image;
|
|
2782
|
+
}
|
|
1825
2783
|
|
|
1826
2784
|
export function createInput(style) {
|
|
1827
2785
|
const input = new Input();
|
|
@@ -2879,7 +3837,7 @@ function touchList(list) {
|
|
|
2879
3837
|
// goes to the node the finger went down on. No host reports contact size or force: a touch is
|
|
2880
3838
|
// 1 by 1 pixel, as its pointer is, with force 0.
|
|
2881
3839
|
function fillTouch(event, kind, a, b, bits, d) {
|
|
2882
|
-
const identifier = Math.floor(bits / 256);
|
|
3840
|
+
const identifier = Math.floor(bits / 256) % 16777216;
|
|
2883
3841
|
const touch = {
|
|
2884
3842
|
identifier,
|
|
2885
3843
|
target: event.target,
|
|
@@ -2908,7 +3866,8 @@ function fillTouch(event, kind, a, b, bits, d) {
|
|
|
2908
3866
|
// spec defaults for what no host reports: pressure 0.5 while a button is down, 0 otherwise, and
|
|
2909
3867
|
// a contact of 1 by 1 pixel with no tilt.
|
|
2910
3868
|
function fillPointer(event, kind, bits, d) {
|
|
2911
|
-
const pointerId = Math.floor(bits / 256);
|
|
3869
|
+
const pointerId = Math.floor(bits / 256) % 16777216;
|
|
3870
|
+
event._element = Math.floor(bits / 4294967296);
|
|
2912
3871
|
const pointerType = POINTER_TYPES[Math.floor(bits / 16) % 4] ?? "mouse";
|
|
2913
3872
|
const isPrimary = Math.floor(bits / 64) % 2 === 1;
|
|
2914
3873
|
let pointer = pointers.get(pointerId);
|
|
@@ -2944,7 +3903,7 @@ function fillPointer(event, kind, bits, d) {
|
|
|
2944
3903
|
}
|
|
2945
3904
|
|
|
2946
3905
|
function fill(event, kind, a, b, modifiers, d, e, first, second, third, fourth) {
|
|
2947
|
-
if (kind >= TOUCH_START) {
|
|
3906
|
+
if (kind >= TOUCH_START && kind < COPY) {
|
|
2948
3907
|
fillTouch(event, kind, a, b, modifiers, d);
|
|
2949
3908
|
event.shiftKey = (modifiers & 1) !== 0;
|
|
2950
3909
|
event.ctrlKey = (modifiers & 2) !== 0;
|
|
@@ -3091,24 +4050,136 @@ function compatTap(click, target) {
|
|
|
3091
4050
|
native.dispatch = function (kind, id, a, b, modifiers, d, e, first, second, third, fourth) {
|
|
3092
4051
|
const target = nodes.get(id);
|
|
3093
4052
|
if (!target) return false;
|
|
4053
|
+
if (kind >= COPY) return dispatchEditing(kind, target, first, second, third);
|
|
3094
4054
|
if (kind === SELECTION_CHANGE && !(target instanceof Input)) return selectionRecord(target, a);
|
|
4055
|
+
if (kind === FOCUS) focusing(target);
|
|
3095
4056
|
const event = new Event(TYPES[kind], target);
|
|
3096
4057
|
fill(event, kind, a, b, modifiers, d, e, first, second, third, fourth);
|
|
4058
|
+
if (kind === FOCUS || kind === BLUR) event.relatedTarget = kind === FOCUS ? blurredNode : null;
|
|
4059
|
+
const live = target instanceof Svg && target._live && isPointerKind(kind);
|
|
3097
4060
|
if (kind === CLICK && event.pointerType === "touch" && event.isPrimary) compatTap(event, target);
|
|
3098
|
-
|
|
3099
|
-
|
|
4061
|
+
// A live svg sends pointer records on to the element under the pointer.
|
|
4062
|
+
let prevented;
|
|
4063
|
+
if (live) prevented = target._pointer(event);
|
|
4064
|
+
else {
|
|
4065
|
+
dispatchEvent(event);
|
|
4066
|
+
prevented = event.defaultPrevented;
|
|
4067
|
+
}
|
|
4068
|
+
if (kind === FOCUS) {
|
|
4069
|
+
focusEvent("focusin", target, blurredNode);
|
|
4070
|
+
blurredNode = null;
|
|
4071
|
+
}
|
|
4072
|
+
else if (kind === BLUR) blurring(target);
|
|
4073
|
+
if (kind === CLICK && !prevented && !activate(target)) follow(target);
|
|
3100
4074
|
if (kind !== CLICK && isPointerKind(kind)) {
|
|
3101
|
-
if (event.pointerType !== "touch") compatMouse(kind, event, target);
|
|
4075
|
+
if (event.pointerType !== "touch") compatMouse(kind, event, live && event.target instanceof SvgElement ? event.target : target);
|
|
3102
4076
|
// A finger's defaults (pan, focus, the tap's click) follow its touch events, not its
|
|
3103
4077
|
// pointer events, as on the web.
|
|
3104
4078
|
else {
|
|
3105
|
-
if (kind === 0 && event.isPrimary) mutedTap =
|
|
4079
|
+
if (kind === 0 && event.isPrimary) mutedTap = prevented ? event.pointerId : null;
|
|
3106
4080
|
return false;
|
|
3107
4081
|
}
|
|
3108
4082
|
}
|
|
4083
|
+
return prevented;
|
|
4084
|
+
};
|
|
4085
|
+
|
|
4086
|
+
// Copy, cut and paste as the Clipboard API and events spec has them, and beforeinput. A
|
|
4087
|
+
// paste carries the clipboard's text, HTML and a PNG (bytes as a Latin-1 string). A copy or
|
|
4088
|
+
// cut a listener prevented puts what it set on `clipboardData` on the clipboard; one it did
|
|
4089
|
+
// not prevent lets the tree copy the selection.
|
|
4090
|
+
function dispatchEditing(kind, target, first, second, third) {
|
|
4091
|
+
let event;
|
|
4092
|
+
if (kind === BEFORE_INPUT) {
|
|
4093
|
+
event = new InputEvent("beforeinput", { bubbles: true, cancelable: true, composed: true, inputType: second ?? "", data: first ?? "" });
|
|
4094
|
+
} else {
|
|
4095
|
+
const data = new DataTransfer();
|
|
4096
|
+
if (kind === PASTE) {
|
|
4097
|
+
if (first) data.setData("text/plain", first);
|
|
4098
|
+
if (second) data.setData("text/html", second);
|
|
4099
|
+
if (third) data.items.add(new FileClass([bytesOf(third)], "image.png", { type: "image/png" }));
|
|
4100
|
+
data._mode = "readonly";
|
|
4101
|
+
}
|
|
4102
|
+
event = new ClipboardEvent(TYPES[kind], { bubbles: true, cancelable: true, composed: true, clipboardData: data });
|
|
4103
|
+
}
|
|
4104
|
+
event.target = target;
|
|
4105
|
+
dispatchEvent(event);
|
|
4106
|
+
if (event.clipboardData) {
|
|
4107
|
+
if ((kind === COPY || kind === CUT) && event.defaultPrevented) writeClipboard(event.clipboardData);
|
|
4108
|
+
event.clipboardData._mode = "protected";
|
|
4109
|
+
}
|
|
3109
4110
|
return event.defaultPrevented;
|
|
4111
|
+
}
|
|
4112
|
+
|
|
4113
|
+
const bytesOf = (text) => {
|
|
4114
|
+
const bytes = new Uint8Array(text.length);
|
|
4115
|
+
for (let i = 0; i < text.length; i++) bytes[i] = text.charCodeAt(i);
|
|
4116
|
+
return bytes;
|
|
3110
4117
|
};
|
|
3111
4118
|
|
|
4119
|
+
// What a prevented copy or cut set, onto the system clipboard.
|
|
4120
|
+
function writeClipboard(data) {
|
|
4121
|
+
const clipboard = globalThis.navigator?.clipboard;
|
|
4122
|
+
const entries = data._entries();
|
|
4123
|
+
if (!clipboard || entries.length === 0) return;
|
|
4124
|
+
const item = {};
|
|
4125
|
+
for (const [type, value] of entries) item[type] = value;
|
|
4126
|
+
const written = entries.length === 1 && entries[0][0] === "text/plain" || typeof globalThis.ClipboardItem !== "function"
|
|
4127
|
+
? clipboard.writeText(item["text/plain"] ?? "")
|
|
4128
|
+
: clipboard.write([new globalThis.ClipboardItem(item)]);
|
|
4129
|
+
Promise.resolve(written).catch(reportError);
|
|
4130
|
+
}
|
|
4131
|
+
|
|
4132
|
+
// The focused node, as far as focus and blur records tell (HTML elements that can take focus
|
|
4133
|
+
// listen for both: see `createElement`).
|
|
4134
|
+
let focusedNode = null;
|
|
4135
|
+
// The node a blur record left, until the focus record that follows it.
|
|
4136
|
+
let blurredNode = null;
|
|
4137
|
+
let pendingFocusOut = null;
|
|
4138
|
+
|
|
4139
|
+
/** The focused node, or null. */
|
|
4140
|
+
export function activeElement() {
|
|
4141
|
+
return focusedNode?.parentNode || focusedNode === rootNode ? focusedNode : null;
|
|
4142
|
+
}
|
|
4143
|
+
|
|
4144
|
+
function focusEvent(type, target, relatedTarget) {
|
|
4145
|
+
const event = new Event(type, target);
|
|
4146
|
+
event.bubbles = true;
|
|
4147
|
+
event.relatedTarget = relatedTarget;
|
|
4148
|
+
dispatchEvent(event);
|
|
4149
|
+
}
|
|
4150
|
+
|
|
4151
|
+
// `focusout` waits for the focus record that follows a blur, so its `relatedTarget` is where
|
|
4152
|
+
// focus went, as on the web; with none, it goes out on its own.
|
|
4153
|
+
function blurring(target) {
|
|
4154
|
+
if (focusedNode === target) focusedNode = null;
|
|
4155
|
+
blurredNode = target;
|
|
4156
|
+
pendingFocusOut = target;
|
|
4157
|
+
queueMicrotask(() => {
|
|
4158
|
+
if (pendingFocusOut !== target) return;
|
|
4159
|
+
pendingFocusOut = null;
|
|
4160
|
+
blurredNode = null;
|
|
4161
|
+
focusEvent("focusout", target, null);
|
|
4162
|
+
});
|
|
4163
|
+
}
|
|
4164
|
+
|
|
4165
|
+
function focusing(target) {
|
|
4166
|
+
focusedNode = target;
|
|
4167
|
+
const left = pendingFocusOut;
|
|
4168
|
+
pendingFocusOut = null;
|
|
4169
|
+
if (left) focusEvent("focusout", left, target);
|
|
4170
|
+
else blurredNode = null;
|
|
4171
|
+
}
|
|
4172
|
+
|
|
4173
|
+
// HTML defaults of a click: a checkbox or radio checks, a label passes the click on to its
|
|
4174
|
+
// control, a summary opens its details, a submit button submits its form. True when one ran.
|
|
4175
|
+
function activate(target) {
|
|
4176
|
+
for (let node = target; node; node = node.parentNode) {
|
|
4177
|
+
if (node._activate) return node._activate() !== false;
|
|
4178
|
+
if (node._href) return false;
|
|
4179
|
+
}
|
|
4180
|
+
return false;
|
|
4181
|
+
}
|
|
4182
|
+
|
|
3112
4183
|
// The default action of a click: the nearest link around the target opens.
|
|
3113
4184
|
function follow(target) {
|
|
3114
4185
|
for (let node = target; node; node = node.parentNode) {
|
|
@@ -3170,24 +4241,219 @@ export class DroppedFile {
|
|
|
3170
4241
|
}
|
|
3171
4242
|
}
|
|
3172
4243
|
|
|
4244
|
+
// `File` where the runtime has one, else a `Blob` with a name.
|
|
4245
|
+
const FileClass = globalThis.File ?? (globalThis.Blob ? class File extends globalThis.Blob {
|
|
4246
|
+
constructor(parts, name, options = {}) {
|
|
4247
|
+
super(parts, options);
|
|
4248
|
+
this.name = String(name);
|
|
4249
|
+
this.lastModified = options.lastModified ?? Date.now();
|
|
4250
|
+
}
|
|
4251
|
+
} : null);
|
|
4252
|
+
|
|
4253
|
+
const normalFormat = (format) => {
|
|
4254
|
+
const lower = String(format).toLowerCase();
|
|
4255
|
+
return lower === "text" ? "text/plain" : lower === "url" ? "text/uri-list" : lower;
|
|
4256
|
+
};
|
|
4257
|
+
|
|
4258
|
+
/** One entry of a `DataTransfer`, as the web's `DataTransferItem`. */
|
|
4259
|
+
class DataTransferItem {
|
|
4260
|
+
#owner;
|
|
4261
|
+
#value;
|
|
4262
|
+
constructor(owner, kind, type, value) {
|
|
4263
|
+
this.#owner = owner;
|
|
4264
|
+
this.kind = kind;
|
|
4265
|
+
this.type = type;
|
|
4266
|
+
this.#value = value;
|
|
4267
|
+
}
|
|
4268
|
+
|
|
4269
|
+
getAsString(callback) {
|
|
4270
|
+
if (this.kind !== "string" || typeof callback !== "function" || this.#owner._mode === "protected") return;
|
|
4271
|
+
const value = this.#value;
|
|
4272
|
+
queueMicrotask(() => callback(value));
|
|
4273
|
+
}
|
|
4274
|
+
|
|
4275
|
+
getAsFile() {
|
|
4276
|
+
return this.kind === "file" && this.#owner._mode !== "protected" ? this.#value : null;
|
|
4277
|
+
}
|
|
4278
|
+
|
|
4279
|
+
_value() {
|
|
4280
|
+
return this.#value;
|
|
4281
|
+
}
|
|
4282
|
+
}
|
|
4283
|
+
|
|
4284
|
+
/** The entries of a `DataTransfer`, as the web's `DataTransferItemList`. */
|
|
4285
|
+
class DataTransferItemList {
|
|
4286
|
+
#owner;
|
|
4287
|
+
#list = [];
|
|
4288
|
+
constructor(owner) {
|
|
4289
|
+
this.#owner = owner;
|
|
4290
|
+
return new Proxy(this, {
|
|
4291
|
+
get: (self, key) => {
|
|
4292
|
+
if (typeof key === "string" && /^\d+$/.test(key)) return self.length > Number(key) ? self.#list[Number(key)] : undefined;
|
|
4293
|
+
const value = Reflect.get(self, key, self);
|
|
4294
|
+
return typeof value === "function" ? value.bind(self) : value;
|
|
4295
|
+
},
|
|
4296
|
+
});
|
|
4297
|
+
}
|
|
4298
|
+
|
|
4299
|
+
get length() {
|
|
4300
|
+
return this.#owner._mode === "protected" ? 0 : this.#list.length;
|
|
4301
|
+
}
|
|
4302
|
+
|
|
4303
|
+
/** `add(data, type)` for a string, `add(file)` for a file. Read-only data throws. */
|
|
4304
|
+
add(data, type) {
|
|
4305
|
+
this.#owner._writable();
|
|
4306
|
+
if (typeof data === "string") {
|
|
4307
|
+
const format = normalFormat(type ?? "");
|
|
4308
|
+
if (this.#list.some((item) => item.kind === "string" && item.type === format)) {
|
|
4309
|
+
throw domError("NotSupportedError", "The data already holds an item of type " + format);
|
|
4310
|
+
}
|
|
4311
|
+
const item = new DataTransferItem(this.#owner, "string", format, data);
|
|
4312
|
+
this.#list.push(item);
|
|
4313
|
+
return item;
|
|
4314
|
+
}
|
|
4315
|
+
if (data && typeof data === "object") {
|
|
4316
|
+
const item = new DataTransferItem(this.#owner, "file", String(data.type ?? "").toLowerCase(), data);
|
|
4317
|
+
this.#list.push(item);
|
|
4318
|
+
return item;
|
|
4319
|
+
}
|
|
4320
|
+
throw new TypeError("DataTransferItemList.add takes a string and a type, or a File");
|
|
4321
|
+
}
|
|
4322
|
+
|
|
4323
|
+
remove(index) {
|
|
4324
|
+
this.#owner._writable();
|
|
4325
|
+
if (index >= 0 && index < this.#list.length) this.#list.splice(index, 1);
|
|
4326
|
+
}
|
|
4327
|
+
|
|
4328
|
+
clear() {
|
|
4329
|
+
this.#owner._writable();
|
|
4330
|
+
this.#list.length = 0;
|
|
4331
|
+
}
|
|
4332
|
+
|
|
4333
|
+
[Symbol.iterator]() {
|
|
4334
|
+
return (this.#owner._mode === "protected" ? [] : this.#list)[Symbol.iterator]();
|
|
4335
|
+
}
|
|
4336
|
+
|
|
4337
|
+
_all() {
|
|
4338
|
+
return this.#list;
|
|
4339
|
+
}
|
|
4340
|
+
}
|
|
4341
|
+
|
|
3173
4342
|
/**
|
|
3174
|
-
* What a file drag carries, as the web's `DataTransfer
|
|
3175
|
-
*
|
|
4343
|
+
* What a copy, cut, paste or file drag carries, as the web's `DataTransfer`. `new
|
|
4344
|
+
* DataTransfer()` is empty and writable. A file drag lists the dragged `paths` in `files`;
|
|
4345
|
+
* its `types` is `["Files"]` while they are over the window.
|
|
3176
4346
|
*/
|
|
3177
4347
|
export class DataTransfer {
|
|
4348
|
+
#items;
|
|
3178
4349
|
constructor(paths) {
|
|
3179
|
-
|
|
3180
|
-
this.
|
|
3181
|
-
this.
|
|
3182
|
-
|
|
3183
|
-
this
|
|
4350
|
+
// "readwrite", "readonly" (paste, drop) or "protected" (after the event).
|
|
4351
|
+
this._mode = "readwrite";
|
|
4352
|
+
this._drag = Array.isArray(paths);
|
|
4353
|
+
paths ??= [];
|
|
4354
|
+
this.#items = new DataTransferItemList(this);
|
|
4355
|
+
for (const path of paths) this.#items.add(new DroppedFile(path));
|
|
4356
|
+
this.dropEffect = paths.length ? "copy" : "none";
|
|
4357
|
+
this.effectAllowed = paths.length ? "copy" : "uninitialized";
|
|
3184
4358
|
}
|
|
3185
4359
|
|
|
3186
|
-
|
|
3187
|
-
|
|
4360
|
+
_writable() {
|
|
4361
|
+
if (this._mode !== "readwrite") throw domError("InvalidStateError", "The DataTransfer is read-only now");
|
|
4362
|
+
}
|
|
4363
|
+
|
|
4364
|
+
get items() {
|
|
4365
|
+
return this.#items;
|
|
4366
|
+
}
|
|
4367
|
+
|
|
4368
|
+
get types() {
|
|
4369
|
+
if (this._mode === "protected") return Object.freeze([]);
|
|
4370
|
+
const types = [];
|
|
4371
|
+
let files = false;
|
|
4372
|
+
for (const item of this.#items._all()) {
|
|
4373
|
+
if (item.kind === "string") types.push(item.type);
|
|
4374
|
+
else files = true;
|
|
4375
|
+
}
|
|
4376
|
+
if (files || this._drag) types.push("Files");
|
|
4377
|
+
return Object.freeze(types);
|
|
4378
|
+
}
|
|
4379
|
+
|
|
4380
|
+
get files() {
|
|
4381
|
+
if (this._mode === "protected") return Object.freeze([]);
|
|
4382
|
+
return Object.freeze(this.#items._all().filter((item) => item.kind === "file").map((item) => item._value()));
|
|
4383
|
+
}
|
|
4384
|
+
|
|
4385
|
+
getData(format) {
|
|
4386
|
+
if (this._mode === "protected") return "";
|
|
4387
|
+
const type = normalFormat(format);
|
|
4388
|
+
const item = this.#items._all().find((each) => each.kind === "string" && each.type === type);
|
|
4389
|
+
const value = item ? item._value() : "";
|
|
4390
|
+
return type === "text/uri-list" ? (value.split(/\r?\n/).find((line) => line && !line.startsWith("#")) ?? "") : value;
|
|
4391
|
+
}
|
|
4392
|
+
|
|
4393
|
+
setData(format, data) {
|
|
4394
|
+
if (this._mode !== "readwrite") return;
|
|
4395
|
+
const type = normalFormat(format);
|
|
4396
|
+
const list = this.#items._all();
|
|
4397
|
+
const at = list.findIndex((each) => each.kind === "string" && each.type === type);
|
|
4398
|
+
if (at >= 0) list.splice(at, 1);
|
|
4399
|
+
this.#items.add(String(data), type);
|
|
4400
|
+
}
|
|
4401
|
+
|
|
4402
|
+
clearData(format) {
|
|
4403
|
+
if (this._mode !== "readwrite") return;
|
|
4404
|
+
const list = this.#items._all();
|
|
4405
|
+
const type = format === undefined ? null : normalFormat(format);
|
|
4406
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
4407
|
+
if (list[i].kind === "string" && (type === null || list[i].type === type)) list.splice(i, 1);
|
|
4408
|
+
}
|
|
4409
|
+
}
|
|
4410
|
+
|
|
4411
|
+
setDragImage() {}
|
|
4412
|
+
|
|
4413
|
+
// The strings, and the PNG files, to put on the clipboard.
|
|
4414
|
+
_entries() {
|
|
4415
|
+
const entries = [];
|
|
4416
|
+
for (const item of this.#items._all()) {
|
|
4417
|
+
if (item.kind === "string") entries.push([item.type, item._value()]);
|
|
4418
|
+
else if (item.type === "image/png" && !entries.some(([type]) => type === "image/png")) entries.push(["image/png", item._value()]);
|
|
4419
|
+
}
|
|
4420
|
+
return entries;
|
|
4421
|
+
}
|
|
4422
|
+
}
|
|
4423
|
+
|
|
4424
|
+
/** A copy, cut or paste, as the web's `ClipboardEvent`. */
|
|
4425
|
+
export class ClipboardEvent extends Event {
|
|
4426
|
+
constructor(type, init = {}) {
|
|
4427
|
+
super(String(type), null);
|
|
4428
|
+
this.bubbles = !!init.bubbles;
|
|
4429
|
+
this.cancelable = !!init.cancelable;
|
|
4430
|
+
this.composed = !!init.composed;
|
|
4431
|
+
this.clipboardData = init.clipboardData ?? null;
|
|
3188
4432
|
}
|
|
3189
4433
|
}
|
|
3190
4434
|
|
|
4435
|
+
/** An edit about to happen (`beforeinput`), as the web's `InputEvent`. */
|
|
4436
|
+
class InputEvent extends Event {
|
|
4437
|
+
constructor(type, init = {}) {
|
|
4438
|
+
super(String(type), null);
|
|
4439
|
+
this.bubbles = !!init.bubbles;
|
|
4440
|
+
this.cancelable = !!init.cancelable;
|
|
4441
|
+
this.composed = !!init.composed;
|
|
4442
|
+
this.inputType = init.inputType ?? "";
|
|
4443
|
+
this.data = init.data ?? null;
|
|
4444
|
+
this.dataTransfer = init.dataTransfer ?? null;
|
|
4445
|
+
this.isComposing = !!init.isComposing;
|
|
4446
|
+
}
|
|
4447
|
+
|
|
4448
|
+
getTargetRanges() {
|
|
4449
|
+
return [];
|
|
4450
|
+
}
|
|
4451
|
+
}
|
|
4452
|
+
|
|
4453
|
+
globalThis.ClipboardEvent ??= ClipboardEvent;
|
|
4454
|
+
globalThis.InputEvent ??= InputEvent;
|
|
4455
|
+
globalThis.DataTransfer ??= DataTransfer;
|
|
4456
|
+
|
|
3191
4457
|
let dragTarget = null;
|
|
3192
4458
|
|
|
3193
4459
|
function dragEvent(type, target, x, y, paths) {
|
|
@@ -3210,7 +4476,11 @@ function dragTo(target, x, y) {
|
|
|
3210
4476
|
if (target) dragEvent("dragover", target, x, y, []);
|
|
3211
4477
|
}
|
|
3212
4478
|
|
|
3213
|
-
|
|
4479
|
+
// Menu item callbacks by action id. Each menu (the tray's, the app's, a context menu) owns
|
|
4480
|
+
// the ids it packed, and drops them when it is replaced or closes.
|
|
4481
|
+
const menuActions = new Map();
|
|
4482
|
+
const menuOwners = { tray: [], app: [], context: [] };
|
|
4483
|
+
let nextMenuAction = 0;
|
|
3214
4484
|
let trayClick = null;
|
|
3215
4485
|
const shortcuts = new Map();
|
|
3216
4486
|
const shortcutIds = new Map();
|
|
@@ -3236,12 +4506,30 @@ native.shellEvent = function (kind, a, x, y, text) {
|
|
|
3236
4506
|
} else if (kind === 5) {
|
|
3237
4507
|
trayClick?.({ button: a });
|
|
3238
4508
|
} else if (kind === 6) {
|
|
3239
|
-
|
|
4509
|
+
const run = menuActions.get(a);
|
|
4510
|
+
if (menuOwners.context.includes(a)) closeContextMenu();
|
|
4511
|
+
run?.();
|
|
3240
4512
|
} else if (kind === 7) {
|
|
3241
4513
|
const notification = notifications.get(a);
|
|
3242
4514
|
if (!notification) return;
|
|
3243
4515
|
notifications.delete(a);
|
|
3244
4516
|
notification._fire("click");
|
|
4517
|
+
} else if (kind === 8) {
|
|
4518
|
+
const resolve = dialogs.get(a);
|
|
4519
|
+
if (!resolve) return;
|
|
4520
|
+
dialogs.delete(a);
|
|
4521
|
+
resolve(text ? text.split("\n") : []);
|
|
4522
|
+
} else if (kind === 9) {
|
|
4523
|
+
closeContextMenu();
|
|
4524
|
+
} else if (kind === 10) {
|
|
4525
|
+
const fullScreen = a === 1;
|
|
4526
|
+
if (fullScreen === appWindow.fullScreen) return;
|
|
4527
|
+
appWindow.fullScreen = fullScreen;
|
|
4528
|
+
appWindow._fire("fullscreenchange", { fullScreen });
|
|
4529
|
+
} else if (kind === 11) {
|
|
4530
|
+
if (a === appWindow.zoomFactor) return;
|
|
4531
|
+
appWindow.zoomFactor = a;
|
|
4532
|
+
appWindow._fire("zoomchange", { zoomFactor: a });
|
|
3245
4533
|
}
|
|
3246
4534
|
};
|
|
3247
4535
|
|
|
@@ -3315,22 +4603,88 @@ function hex(bytes) {
|
|
|
3315
4603
|
return text;
|
|
3316
4604
|
}
|
|
3317
4605
|
|
|
3318
|
-
|
|
4606
|
+
// The roles the system implements, by `SystemItem` index; `about` and the others the
|
|
4607
|
+
// platform has no counterpart for are left out there.
|
|
4608
|
+
const SYSTEM_ROLES = {
|
|
4609
|
+
about: 0, services: 1, hide: 2, hideothers: 3, unhide: 4, quit: 5, close: 6, minimize: 7, zoom: 8,
|
|
4610
|
+
togglefullscreen: 9, front: 10, undo: 11, redo: 12, cut: 13, copy: 14, paste: 15, selectall: 16,
|
|
4611
|
+
};
|
|
4612
|
+
|
|
4613
|
+
// Roles JavaScript does itself, with their usual labels and shortcuts.
|
|
4614
|
+
const SCRIPT_ROLES = {
|
|
4615
|
+
resetzoom: { label: "Actual Size", accelerator: "Mod+0", click: () => appWindow.setZoomFactor(1) },
|
|
4616
|
+
zoomin: { label: "Zoom In", accelerator: "Mod+Plus", click: () => appWindow.setZoomFactor(zoomStep(1)) },
|
|
4617
|
+
zoomout: { label: "Zoom Out", accelerator: "Mod+-", click: () => appWindow.setZoomFactor(zoomStep(-1)) },
|
|
4618
|
+
};
|
|
4619
|
+
|
|
4620
|
+
// Roles that stand for a whole menu, as Electron's.
|
|
4621
|
+
const MENU_ROLES = {
|
|
4622
|
+
appmenu: () => [{ role: "about" }, { type: "separator" }, { role: "services" }, { type: "separator" }, { role: "hide" }, { role: "hideOthers" }, { role: "unhide" }, { type: "separator" }, { role: "quit" }],
|
|
4623
|
+
filemenu: () => [{ role: IS_MAC ? "close" : "quit" }],
|
|
4624
|
+
editmenu: () => [{ role: "undo" }, { role: "redo" }, { type: "separator" }, { role: "cut" }, { role: "copy" }, { role: "paste" }, { role: "selectAll" }],
|
|
4625
|
+
viewmenu: () => [{ role: "resetZoom" }, { role: "zoomIn" }, { role: "zoomOut" }, { type: "separator" }, { role: "togglefullscreen" }],
|
|
4626
|
+
windowmenu: () => [{ role: "minimize" }, { role: "zoom" }, ...(IS_MAC ? [{ type: "separator" }, { role: "front" }] : [{ role: "close" }])],
|
|
4627
|
+
};
|
|
4628
|
+
|
|
4629
|
+
const IS_MAC = /mac/i.test(globalThis.navigator?.platform ?? "") || globalThis.process?.platform === "darwin";
|
|
4630
|
+
|
|
4631
|
+
// A menu shortcut as the host reads it: "" for none, else the key (`c` and a character, `n`
|
|
4632
|
+
// and a key name) and the modifier bits.
|
|
4633
|
+
function packAccelerator(accelerator) {
|
|
4634
|
+
if (!accelerator) return ",0";
|
|
4635
|
+
const parts = String(accelerator).split("+").map((part) => part.trim());
|
|
4636
|
+
// "Mod+Plus" and "Mod++" both name the plus key.
|
|
4637
|
+
if (parts.at(-1) === "" && parts.at(-2) === "") parts.splice(-2, 2, "+");
|
|
4638
|
+
let bits = 0;
|
|
4639
|
+
let key = "";
|
|
4640
|
+
for (const part of parts) {
|
|
4641
|
+
const modifier = MODIFIERS[part.toLowerCase()];
|
|
4642
|
+
if (modifier !== undefined) bits |= modifier;
|
|
4643
|
+
else if (part) key = part;
|
|
4644
|
+
}
|
|
4645
|
+
const lower = key.toLowerCase();
|
|
4646
|
+
const named = { up: "ArrowUp", down: "ArrowDown", left: "ArrowLeft", right: "ArrowRight", esc: "Escape", escape: "Escape", return: "Enter", enter: "Enter", tab: "Tab", backspace: "Backspace", delete: "Delete", home: "Home", end: "End", pageup: "PageUp", pagedown: "PageDown" }[lower];
|
|
4647
|
+
if (named) return "n" + named + "," + bits;
|
|
4648
|
+
if (/^f([1-9]|1[0-9]|2[0-4])$/.test(lower)) return "nF" + lower.slice(1) + "," + bits;
|
|
4649
|
+
if (lower === "plus") return "c+," + bits;
|
|
4650
|
+
if (lower === "space") return "c ," + bits;
|
|
4651
|
+
if ([...key].length === 1) return "c" + lower + "," + bits;
|
|
4652
|
+
return "," + bits;
|
|
4653
|
+
}
|
|
4654
|
+
|
|
4655
|
+
// `items` as the host's menu lines, with their callbacks kept under `owner`.
|
|
4656
|
+
function packMenu(items, lines, owner) {
|
|
3319
4657
|
for (const item of items) {
|
|
4658
|
+
if (!item || item.visible === false) continue;
|
|
4659
|
+
const role = String(item.role ?? "").toLowerCase();
|
|
3320
4660
|
if (item.type === "separator") lines.push("S");
|
|
3321
|
-
else if (item.submenu) {
|
|
4661
|
+
else if (MENU_ROLES[role] && !item.submenu) {
|
|
4662
|
+
lines.push("B" + String(item.label ?? role.replace(/menu$/, "").replace(/^./, (c) => c.toUpperCase())).replace(/\n/g, " "));
|
|
4663
|
+
packMenu(MENU_ROLES[role](), lines, owner);
|
|
4664
|
+
lines.push("E");
|
|
4665
|
+
} else if (item.submenu) {
|
|
3322
4666
|
lines.push("B" + String(item.label ?? "").replace(/\n/g, " "));
|
|
3323
|
-
packMenu(item.submenu, lines);
|
|
4667
|
+
packMenu(item.submenu, lines, owner);
|
|
3324
4668
|
lines.push("E");
|
|
4669
|
+
} else if (SYSTEM_ROLES[role] !== undefined && !item.click) {
|
|
4670
|
+
lines.push("R" + SYSTEM_ROLES[role]);
|
|
3325
4671
|
} else {
|
|
3326
|
-
const
|
|
3327
|
-
|
|
3328
|
-
const
|
|
3329
|
-
|
|
4672
|
+
const known = SCRIPT_ROLES[role] ?? {};
|
|
4673
|
+
const action = ++nextMenuAction;
|
|
4674
|
+
const click = item.click ?? known.click;
|
|
4675
|
+
menuActions.set(action, typeof click === "function" ? () => click(item) : () => {});
|
|
4676
|
+
menuOwners[owner].push(action);
|
|
4677
|
+
const label = String(item.label ?? known.label ?? "").replace(/\n/g, " ");
|
|
4678
|
+
lines.push("A" + action + "," + (item.enabled === false ? 0 : 1) + "," + (item.checked ? 1 : 0) + "," + packAccelerator(item.accelerator ?? known.accelerator) + "," + label);
|
|
3330
4679
|
}
|
|
3331
4680
|
}
|
|
3332
4681
|
}
|
|
3333
4682
|
|
|
4683
|
+
function dropMenu(owner) {
|
|
4684
|
+
for (const action of menuOwners[owner]) menuActions.delete(action);
|
|
4685
|
+
menuOwners[owner] = [];
|
|
4686
|
+
}
|
|
4687
|
+
|
|
3334
4688
|
/**
|
|
3335
4689
|
* Shows an icon in the system tray (the macOS menu bar, the Windows notification area, a
|
|
3336
4690
|
* StatusNotifierItem on Linux). `icon` is RGBA pixels, as an `ImageData`: `{ width, height,
|
|
@@ -3343,17 +4697,17 @@ export function setTray({ icon, tooltip = "", template = false, menu = [], onCli
|
|
|
3343
4697
|
if (!icon || !(icon.width > 0) || !(icon.height > 0) || icon.data?.length !== icon.width * icon.height * 4) {
|
|
3344
4698
|
throw new TypeError("setTray needs an icon of RGBA pixels: { width, height, data }");
|
|
3345
4699
|
}
|
|
3346
|
-
|
|
4700
|
+
dropMenu("tray");
|
|
3347
4701
|
trayClick = typeof onClick === "function" ? onClick : null;
|
|
3348
4702
|
const lines = [];
|
|
3349
|
-
packMenu(menu, lines);
|
|
4703
|
+
packMenu(menu, lines, "tray");
|
|
3350
4704
|
const fields = [String(tooltip), icon.width, icon.height, template ? 1 : 0, hex(icon.data), lines.join("\n")];
|
|
3351
4705
|
return shellCall(1, fields).then((shown) => shown === 1);
|
|
3352
4706
|
}
|
|
3353
4707
|
|
|
3354
4708
|
/** Takes the tray icon away. */
|
|
3355
4709
|
export function removeTray() {
|
|
3356
|
-
|
|
4710
|
+
dropMenu("tray");
|
|
3357
4711
|
trayClick = null;
|
|
3358
4712
|
shellCall(2, []);
|
|
3359
4713
|
}
|
|
@@ -3413,6 +4767,226 @@ export function unregisterShortcut(accelerator) {
|
|
|
3413
4767
|
shellCall(4, [id]);
|
|
3414
4768
|
}
|
|
3415
4769
|
|
|
4770
|
+
// File dialogs waiting for `DialogClosed`, by dialog id.
|
|
4771
|
+
const dialogs = new Map();
|
|
4772
|
+
|
|
4773
|
+
function extensionsOf(filters) {
|
|
4774
|
+
const all = [];
|
|
4775
|
+
for (const filter of filters ?? []) {
|
|
4776
|
+
for (const extension of filter.extensions ?? []) {
|
|
4777
|
+
if (extension === "*") return "";
|
|
4778
|
+
all.push(String(extension).replace(/^\./, ""));
|
|
4779
|
+
}
|
|
4780
|
+
}
|
|
4781
|
+
return all.join(",");
|
|
4782
|
+
}
|
|
4783
|
+
|
|
4784
|
+
function dialog(op, fields) {
|
|
4785
|
+
return shellCall(op, fields).then((id) => (id ? new Promise((resolve) => dialogs.set(id, resolve)) : []));
|
|
4786
|
+
}
|
|
4787
|
+
|
|
4788
|
+
/**
|
|
4789
|
+
* Opens the system's file dialog, as Electron's `dialog.showOpenDialog`: `properties` takes
|
|
4790
|
+
* `openFile`, `openDirectory` and `multiSelections`; `filters` are `{ name, extensions }`
|
|
4791
|
+
* without dots (`"*"` for every file). It attaches to the window as a sheet on macOS and never
|
|
4792
|
+
* blocks. Resolves to `{ canceled, filePaths }`.
|
|
4793
|
+
*/
|
|
4794
|
+
export function showOpenDialog(options = {}) {
|
|
4795
|
+
const properties = options.properties ?? ["openFile"];
|
|
4796
|
+
const directories = properties.includes("openDirectory");
|
|
4797
|
+
const files = properties.includes("openFile") || !directories;
|
|
4798
|
+
const flags = (files ? 1 : 0) | (directories ? 2 : 0) | (properties.includes("multiSelections") ? 4 : 0);
|
|
4799
|
+
return dialog(5, [String(options.title ?? ""), String(options.defaultPath ?? ""), flags, extensionsOf(options.filters)]).then(
|
|
4800
|
+
(filePaths) => ({ canceled: filePaths.length === 0, filePaths }),
|
|
4801
|
+
);
|
|
4802
|
+
}
|
|
4803
|
+
|
|
4804
|
+
/**
|
|
4805
|
+
* Opens the system's save dialog, as Electron's `dialog.showSaveDialog`. `defaultPath` may
|
|
4806
|
+
* name a folder, a file, or both. Resolves to `{ canceled, filePath }`.
|
|
4807
|
+
*/
|
|
4808
|
+
export function showSaveDialog(options = {}) {
|
|
4809
|
+
const path = String(options.defaultPath ?? "");
|
|
4810
|
+
const slash = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
|
|
4811
|
+
const looksLikeFolder = slash === path.length - 1 || !/\.[^./\\]+$/.test(path.slice(slash + 1));
|
|
4812
|
+
const directory = looksLikeFolder ? path : path.slice(0, Math.max(slash, 0));
|
|
4813
|
+
const name = looksLikeFolder ? "" : path.slice(slash + 1);
|
|
4814
|
+
return dialog(6, [String(options.title ?? ""), directory, name, extensionsOf(options.filters)]).then((paths) => ({
|
|
4815
|
+
canceled: paths.length === 0,
|
|
4816
|
+
filePath: paths[0] ?? "",
|
|
4817
|
+
}));
|
|
4818
|
+
}
|
|
4819
|
+
|
|
4820
|
+
/**
|
|
4821
|
+
* Replaces the app's menu bar, as Electron's `Menu.setApplicationMenu(Menu.buildFromTemplate(t))`.
|
|
4822
|
+
* Items are `{ label, click, accelerator, enabled, checked, type, role, submenu, visible }`.
|
|
4823
|
+
* `accelerator` is `"Mod+Shift+K"` (`Mod`, `CmdOrCtrl`: Command on macOS, Control elsewhere).
|
|
4824
|
+
* The system roles (`about`, `quit`, `undo`, `copy`, `togglefullscreen`, `minimize`, `front`
|
|
4825
|
+
* and so on) are the system's own items; `zoomIn`, `zoomOut` and `resetZoom` zoom the page;
|
|
4826
|
+
* `appMenu`, `fileMenu`, `editMenu`, `viewMenu` and `windowMenu` are whole menus. On macOS
|
|
4827
|
+
* the first menu is the app menu. Edit shortcuts reach the focused node as keys first.
|
|
4828
|
+
*/
|
|
4829
|
+
export function setApplicationMenu(template) {
|
|
4830
|
+
dropMenu("app");
|
|
4831
|
+
const lines = [];
|
|
4832
|
+
packMenu(template ?? [], lines, "app");
|
|
4833
|
+
return shellCall(7, [lines.join("\n")]).then(() => undefined);
|
|
4834
|
+
}
|
|
4835
|
+
|
|
4836
|
+
let contextMenuClosed = null;
|
|
4837
|
+
|
|
4838
|
+
function closeContextMenu() {
|
|
4839
|
+
dropMenu("context");
|
|
4840
|
+
const closed = contextMenuClosed;
|
|
4841
|
+
contextMenuClosed = null;
|
|
4842
|
+
closed?.();
|
|
4843
|
+
}
|
|
4844
|
+
|
|
4845
|
+
/**
|
|
4846
|
+
* Opens a native context menu at `x`, `y` (CSS pixels in the window, a `MouseEvent`'s
|
|
4847
|
+
* `clientX`/`clientY`), as Electron's `menu.popup()`. Items are as `setApplicationMenu`'s.
|
|
4848
|
+
* Resolves when the menu closes, with or without a choice, to `true`; or at once to `false`
|
|
4849
|
+
* when the system draws no context menus (Linux, the browser) and the app should draw its own.
|
|
4850
|
+
*/
|
|
4851
|
+
export function showContextMenu(template, { x = 0, y = 0 } = {}) {
|
|
4852
|
+
closeContextMenu();
|
|
4853
|
+
const lines = [];
|
|
4854
|
+
packMenu(template ?? [], lines, "context");
|
|
4855
|
+
const zoom = appWindow.zoomFactor || 1;
|
|
4856
|
+
return shellCall(8, [x * zoom, y * zoom, lines.join("\n")]).then((shown) => {
|
|
4857
|
+
if (shown !== 1) {
|
|
4858
|
+
dropMenu("context");
|
|
4859
|
+
return false;
|
|
4860
|
+
}
|
|
4861
|
+
return new Promise((resolve) => (contextMenuClosed = () => resolve(true)));
|
|
4862
|
+
});
|
|
4863
|
+
}
|
|
4864
|
+
|
|
4865
|
+
/** Puts `text` on the app's dock or taskbar icon (an unread count); `""` takes it off. */
|
|
4866
|
+
export function setBadge(text) {
|
|
4867
|
+
return shellCall(9, [String(text ?? "")]).then((shown) => shown === 1);
|
|
4868
|
+
}
|
|
4869
|
+
|
|
4870
|
+
/** Shows progress from 0 to 1 on the app's dock or taskbar icon; a negative number takes it off. */
|
|
4871
|
+
export function setProgress(fraction) {
|
|
4872
|
+
return shellCall(10, [Number(fraction)]).then((shown) => shown === 1);
|
|
4873
|
+
}
|
|
4874
|
+
|
|
4875
|
+
/** Opens `url` with the system's handler: a web page in the default browser, `mailto:` in the mail app. */
|
|
4876
|
+
export function openExternal(url) {
|
|
4877
|
+
return shellCall(11, [String(url)]).then((opened) => opened === 1);
|
|
4878
|
+
}
|
|
4879
|
+
|
|
4880
|
+
/** Shows the file or folder at `path` selected in Finder, Explorer or the file manager. */
|
|
4881
|
+
export function showItemInFolder(path) {
|
|
4882
|
+
return shellCall(12, [String(path)]).then((shown) => shown === 1);
|
|
4883
|
+
}
|
|
4884
|
+
|
|
4885
|
+
/**
|
|
4886
|
+
* Drags the files at `paths` out of the window, as Electron's `webContents.startDrag`, so
|
|
4887
|
+
* Finder, a mail draft or a terminal can take them. Call it from a `pointerdown` or
|
|
4888
|
+
* `pointermove` listener while the button is down. Resolves to whether the drag started
|
|
4889
|
+
* (macOS and Windows drag files out; Linux and the browser do not).
|
|
4890
|
+
*/
|
|
4891
|
+
export function startFileDrag(paths) {
|
|
4892
|
+
const list = (Array.isArray(paths) ? paths : [paths]).map(String).filter(Boolean);
|
|
4893
|
+
return shellCall(13, [list.join("\n")]).then((started) => started === 1);
|
|
4894
|
+
}
|
|
4895
|
+
|
|
4896
|
+
const ZOOM_STEPS = [0.25, 0.33, 0.5, 0.67, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5];
|
|
4897
|
+
|
|
4898
|
+
function zoomStep(direction) {
|
|
4899
|
+
const now = appWindow.zoomFactor;
|
|
4900
|
+
if (direction > 0) return ZOOM_STEPS.find((step) => step > now + 0.001) ?? now;
|
|
4901
|
+
return ZOOM_STEPS.findLast((step) => step < now - 0.001) ?? now;
|
|
4902
|
+
}
|
|
4903
|
+
|
|
4904
|
+
/**
|
|
4905
|
+
* The window the app runs in. `fullScreen` and `zoomFactor` (the page zoom, as Electron's
|
|
4906
|
+
* `webFrame.getZoomFactor()`; Mod with `=`, `-` and `0` change it too) are current, and
|
|
4907
|
+
* `fullscreenchange` and `zoomchange` say when they change. The title bar is the app's
|
|
4908
|
+
* configuration: a transparent one, with the window controls placed in it, comes from
|
|
4909
|
+
* `TitleBar.transparent` in its `tasks.silk`.
|
|
4910
|
+
*/
|
|
4911
|
+
class AppWindow {
|
|
4912
|
+
fullScreen = false;
|
|
4913
|
+
zoomFactor = 1;
|
|
4914
|
+
#listeners = new Map();
|
|
4915
|
+
|
|
4916
|
+
setFullScreen(on) {
|
|
4917
|
+
if (!!on !== this.fullScreen) shellCall(14, [0]);
|
|
4918
|
+
}
|
|
4919
|
+
|
|
4920
|
+
toggleFullScreen() {
|
|
4921
|
+
shellCall(14, [0]);
|
|
4922
|
+
}
|
|
4923
|
+
|
|
4924
|
+
minimize() {
|
|
4925
|
+
shellCall(14, [1]);
|
|
4926
|
+
}
|
|
4927
|
+
|
|
4928
|
+
/** Shows the window and brings it to the front with keyboard focus. */
|
|
4929
|
+
focus() {
|
|
4930
|
+
shellCall(14, [2]);
|
|
4931
|
+
}
|
|
4932
|
+
|
|
4933
|
+
show() {
|
|
4934
|
+
shellCall(14, [2]);
|
|
4935
|
+
}
|
|
4936
|
+
|
|
4937
|
+
hide() {
|
|
4938
|
+
shellCall(14, [3]);
|
|
4939
|
+
}
|
|
4940
|
+
|
|
4941
|
+
setTitle(title) {
|
|
4942
|
+
shellCall(14, [4, String(title)]);
|
|
4943
|
+
}
|
|
4944
|
+
|
|
4945
|
+
/** Zooms the page, from 0.25 to 5. Resolves to the zoom now. */
|
|
4946
|
+
setZoomFactor(zoom) {
|
|
4947
|
+
return shellCall(15, [Math.min(5, Math.max(0.25, Number(zoom) || 1))]).then((now) => {
|
|
4948
|
+
if (now !== this.zoomFactor) {
|
|
4949
|
+
this.zoomFactor = now;
|
|
4950
|
+
this._fire("zoomchange", { zoomFactor: now });
|
|
4951
|
+
}
|
|
4952
|
+
return now;
|
|
4953
|
+
});
|
|
4954
|
+
}
|
|
4955
|
+
|
|
4956
|
+
getZoomFactor() {
|
|
4957
|
+
return this.zoomFactor;
|
|
4958
|
+
}
|
|
4959
|
+
|
|
4960
|
+
isFullScreen() {
|
|
4961
|
+
return this.fullScreen;
|
|
4962
|
+
}
|
|
4963
|
+
|
|
4964
|
+
addEventListener(type, listener) {
|
|
4965
|
+
let list = this.#listeners.get(type);
|
|
4966
|
+
if (!list) this.#listeners.set(type, (list = []));
|
|
4967
|
+
if (!list.includes(listener)) list.push(listener);
|
|
4968
|
+
}
|
|
4969
|
+
|
|
4970
|
+
removeEventListener(type, listener) {
|
|
4971
|
+
const list = this.#listeners.get(type);
|
|
4972
|
+
const index = list ? list.indexOf(listener) : -1;
|
|
4973
|
+
if (index >= 0) list.splice(index, 1);
|
|
4974
|
+
}
|
|
4975
|
+
|
|
4976
|
+
_fire(type, detail) {
|
|
4977
|
+
const event = { type, target: this, ...detail };
|
|
4978
|
+
for (const listener of (this.#listeners.get(type) ?? []).slice()) {
|
|
4979
|
+
try {
|
|
4980
|
+
listener.call(this, event);
|
|
4981
|
+
} catch (error) {
|
|
4982
|
+
reportError(error);
|
|
4983
|
+
}
|
|
4984
|
+
}
|
|
4985
|
+
}
|
|
4986
|
+
}
|
|
4987
|
+
|
|
4988
|
+
export const appWindow = new AppWindow();
|
|
4989
|
+
|
|
3416
4990
|
/**
|
|
3417
4991
|
* The app's memory in bytes, as the system shows it to users: `phys_footprint` on macOS
|
|
3418
4992
|
* (Activity Monitor's "Memory"), `Pss` on Linux and `PrivateUsage` on Windows. `processes` is
|