@snupo/jmc 1.0.59 → 1.0.61
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/dist/jmc.d.ts.map +1 -1
- package/dist/jmc.js +60 -47
- package/dist/jmc.js.map +1 -1
- package/package.json +1 -1
- package/src/jmc.ts +102 -60
package/dist/jmc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,CAAC,EAED,CAAC,EACD,QAAQ,EAIX,MAAM,KAAK,CAAA;AAQZ,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,IAAI;QACV,CAAC,CAAC,EAAE,CAAC,GAAG;YAAE,CAAC,CAAC,EAAE,CAAC,CAAA;SAAE,CAAA;KACpB;CACJ;AA8JD,wBAAgB,KAAK,CACjB,CAAC,EAAE,IAAI,GAAG,IAAI,EACd,CAAC,EAAE,CAAC,EACJ,KAAK,SAAM,EACX,EAAE,GAAS,CAAC,GACb,WAAW,GAAG,SAAS,CAUzB;AAED,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,CAAC,GAAG,MAAM,CAAA;AAE3C,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CA0BnD"}
|
package/dist/jmc.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { rebind } from './base.js';
|
|
2
2
|
let iswebcomponent = (h) => h.tag.includes('-');
|
|
3
|
-
|
|
3
|
+
let attributeNames = new Set([
|
|
4
|
+
'selected',
|
|
5
|
+
'viewBox',
|
|
6
|
+
// other HTML attributes where your framework wants attribute semantics
|
|
7
|
+
]);
|
|
8
|
+
const isproperty = (e, name, value) => !attributeNames.has(name) &&
|
|
4
9
|
(value instanceof Object ||
|
|
5
|
-
value instanceof Function ||
|
|
6
|
-
|
|
10
|
+
value instanceof Function || (e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
|
|
11
|
+
name in e &&
|
|
12
|
+
name !== 'style'));
|
|
7
13
|
let setprops = (e, old, now) => {
|
|
8
14
|
old ??= {};
|
|
9
15
|
now ??= {};
|
|
@@ -11,16 +17,19 @@ let setprops = (e, old, now) => {
|
|
|
11
17
|
for (let p in old)
|
|
12
18
|
if (!(p in now))
|
|
13
19
|
isproperty(e, p, old[p])
|
|
14
|
-
? e[p] = typeof old[p] === 'boolean' ? false : ''
|
|
20
|
+
? (e[p] = typeof old[p] === 'boolean' ? false : '')
|
|
15
21
|
: e.removeAttribute(p);
|
|
16
22
|
// updates
|
|
17
23
|
for (let p in now) {
|
|
18
24
|
let v = now[p];
|
|
19
25
|
v = v?.thunk ? v() : v;
|
|
20
26
|
if (v != old[p]) {
|
|
21
|
-
isproperty(e, p, v)
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
if (isproperty(e, p, v))
|
|
28
|
+
e[p] = v;
|
|
29
|
+
else if ((v === false || v == null) && !p.startsWith('aria-'))
|
|
30
|
+
e.removeAttribute(p);
|
|
31
|
+
else
|
|
32
|
+
e.setAttribute(p, v);
|
|
24
33
|
}
|
|
25
34
|
}
|
|
26
35
|
};
|
|
@@ -36,8 +45,8 @@ let removeexcesschildren = (n, i) => {
|
|
|
36
45
|
/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */
|
|
37
46
|
let syncchildren = (p, cn, i, depth, uc) => {
|
|
38
47
|
cn.flat(999)
|
|
39
|
-
.filter(hc => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
|
|
40
|
-
.forEach(hc => (i = sync(p, i, hc, undefined, depth, uc)));
|
|
48
|
+
.filter((hc) => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
|
|
49
|
+
.forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)));
|
|
41
50
|
return i;
|
|
42
51
|
};
|
|
43
52
|
function sync(parent, i, h, hfun, depth, uc) {
|
|
@@ -46,53 +55,57 @@ function sync(parent, i, h, hfun, depth, uc) {
|
|
|
46
55
|
return i; // skip. keep 0 and false renderable, per comment below
|
|
47
56
|
let c = parent.childNodes[i];
|
|
48
57
|
switch (h.kind) {
|
|
49
|
-
case 1:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
};
|
|
62
|
-
if (c?.tagName?.toLowerCase() != tag.toLowerCase()) {
|
|
63
|
-
let ns = p?.xmlns ?? (parent.tagName == "foreignObject" ? 'http://www.w3.org/1999/xhtml' : parent.namespaceURI);
|
|
64
|
-
n = document.createElementNS(ns, tag);
|
|
65
|
-
c ? c.replaceWith(n) : parent.appendChild(n);
|
|
66
|
-
process();
|
|
67
|
-
p?.mounted?.(n);
|
|
58
|
+
case 1: {
|
|
59
|
+
let n;
|
|
60
|
+
let { tag, cn, p } = h;
|
|
61
|
+
cn ??= [];
|
|
62
|
+
let process = () => {
|
|
63
|
+
setprops(n, n.h?.p, p);
|
|
64
|
+
n.h = hfun ?? h; // sets the new property values
|
|
65
|
+
if (--depth &&
|
|
66
|
+
!n.hasAttribute('const') &&
|
|
67
|
+
!iswebcomponent(h)) {
|
|
68
|
+
const j = syncchildren(n, cn, 0, depth, uc);
|
|
69
|
+
removeexcesschildren(n, j);
|
|
68
70
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return syncchildren(parent, h.cn ?? [], i, --depth, uc);
|
|
71
|
+
};
|
|
72
|
+
if (c?.tagName?.toLowerCase() != tag.toLowerCase()) {
|
|
73
|
+
let ns = p?.xmlns ??
|
|
74
|
+
(parent.tagName == 'foreignObject'
|
|
75
|
+
? 'http://www.w3.org/1999/xhtml'
|
|
76
|
+
: parent.namespaceURI);
|
|
77
|
+
n = document.createElementNS(ns, tag);
|
|
78
|
+
c ? c.replaceWith(n) : parent.appendChild(n);
|
|
79
|
+
process();
|
|
80
|
+
p?.mounted?.(n);
|
|
80
81
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
else {
|
|
83
|
+
n = c;
|
|
84
|
+
if (p?.update?.(c, uc))
|
|
85
|
+
return i + 1;
|
|
86
|
+
process();
|
|
85
87
|
}
|
|
88
|
+
return i + 1;
|
|
89
|
+
}
|
|
90
|
+
case 4: {
|
|
91
|
+
return syncchildren(parent, h.cn ?? [], i, --depth, uc);
|
|
92
|
+
}
|
|
93
|
+
case 2: {
|
|
94
|
+
let hr = h.tag(h.p, h.cn);
|
|
95
|
+
return sync(parent, i, hr, hfun ?? h, depth, uc);
|
|
96
|
+
}
|
|
86
97
|
case 3: {
|
|
87
98
|
if (c?.h?.tag == h.tag) {
|
|
88
99
|
let ci = c.h.i;
|
|
89
100
|
ci.props = c.h.p = h.p;
|
|
90
101
|
c.h.p = h.p; // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined
|
|
91
102
|
// update() substitutes view/sync when returning truthy
|
|
92
|
-
return
|
|
103
|
+
return ci.update?.(uc)
|
|
104
|
+
? i + 1
|
|
105
|
+
: sync(parent, i, ci.view(), c.h, depth, uc); // hfun because outermost function shall be attached
|
|
93
106
|
}
|
|
94
107
|
else {
|
|
95
|
-
let ci = h.i = rebind(new h.tag(h.p));
|
|
108
|
+
let ci = (h.i = rebind(new h.tag(h.p)));
|
|
96
109
|
let hv = ci.view();
|
|
97
110
|
let j = sync(parent, i, hv, h, depth, uc);
|
|
98
111
|
let n = parent.childNodes[i];
|
|
@@ -118,6 +131,7 @@ export function patch(e, h, depth = 999, uc = {}) {
|
|
|
118
131
|
if (!e)
|
|
119
132
|
return;
|
|
120
133
|
if (depth == 0) {
|
|
134
|
+
;
|
|
121
135
|
e.replaceChildren();
|
|
122
136
|
depth = 999;
|
|
123
137
|
}
|
|
@@ -152,7 +166,6 @@ export function updateview(...cmds) {
|
|
|
152
166
|
}
|
|
153
167
|
}
|
|
154
168
|
f(cmds);
|
|
155
|
-
if (!patchcount)
|
|
156
|
-
console.warn("updateview did not update anything. cmds=", ...cmds);
|
|
169
|
+
// if (!patchcount) console.warn("updateview did not update anything. cmds=", ...cmds)
|
|
157
170
|
}
|
|
158
171
|
//# sourceMappingURL=jmc.js.map
|
package/dist/jmc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAYlC,IAAI,cAAc,GAAG,CAAC,CAAW,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AAEzD,MAAM,UAAU,GAAG,CAAC,CAAU,EAAE,IAAY,EAAE,KAAU,EAAE,EAAE,CACxD,IAAI,KAAK,SAAS;IAClB,CAAC,KAAK,YAAY,MAAM;QACpB,KAAK,YAAY,QAAQ;QACzB,CAAC,CAAC,CAAC,YAAY,KAAK,4BAA4B,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,OAAO,CAAC,CAAC,CAAA;AAE1F,IAAI,QAAQ,GAAG,CAAC,CAAc,EAAE,GAAkB,EAAE,GAAkB,EAAE,EAAE;IAEtE,GAAG,KAAK,EAAE,CAAA;IACV,GAAG,KAAK,EAAE,CAAA;IAEV,WAAW;IACX,KAAK,IAAI,CAAC,IAAI,GAAG;QACb,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;YACX,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;gB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ACxB,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;YACxC,CAAC;iBAAM,IAAI,CAAC,YAAY,IAAI,EAAE,CAAC;gBAC3B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;oBACN,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAA;oBACxB,UAAU,EAAE,CAAA;gBAChB,CAAC;YACL,CAAC;iBAAM,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;gBAC/B,KAAK,GAAG,CAAC,CAAA;YACb,CAAC;iBAAM,CAAC;gBACJ,EAAE,GAAG,CAAC,CAAA;YACV,CAAC;QACL,CAAC;IACL,CAAC;IAED,CAAC,CAAC,IAAI,CAAC,CAAA;IAEP,IAAI,CAAC,UAAU;QAAE,OAAO,CAAC,IAAI,CAAC,2CAA2C,EAAE,GAAG,IAAI,CAAC,CAAA;AACvF,CAAC","sourcesContent":["import type { H, HElement, J, Selector, Children, HClassComponent, HU } from './h'\r\nimport { rebind } from './base.js'\r\n\r\nconst enum NodeType { // vaporizes (but for that must be in this file, otherwise not)\r\n TextNode = 3\r\n}\r\n\r\ndeclare global {\r\n interface Node {\r\n h?: H & { p?: J }\r\n }\r\n}\r\n\r\nlet iswebcomponent = (h: HElement) => h.tag.includes('-')\r\n\r\nconst isproperty = (e: Element, name: string, value: any) =>\r\n name !== \"viewBox\" &&\r\n (value instanceof Object ||\r\n value instanceof Function ||\r\n (e.namespaceURI !== \"http://www.w3.org/2000/svg\" && name in e && name != 'style'))\r\n\r\nlet setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {\r\n\r\n old ??= {}\r\n now ??= {}\r\n\r\n // removals\r\n for (let p in old)\r\n if (!(p in now))\r\n isproperty(e, p, old[p])\r\n ? e[p] = typeof old[p] === 'boolean' ? false : ''\r\n : e.removeAttribute(p)\r\n // updates\r\n for (let p in now) {\r\n let v = now[p]\r\n v = v?.thunk ? v() : v\r\n if (v != old[p]) {\r\n isproperty(e, p, v)\r\n ? e[p] = v\r\n : e.setAttribute(p, v)\r\n }\r\n }\r\n}\r\n\r\nlet removeexcesschildren = (n: Element, i: number) => {\r\n let c: ChildNode\r\n while ((c = n.childNodes[i])) {\r\n try { c.remove() } catch (_) { } // due to blur-event-handlers, this can fail\r\n }\r\n}\r\n\r\n/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */\r\nlet syncchildren = (p: Element, cn: Children, i: number, depth: number, uc: J): number => {\r\n cn.flat(999)\r\n .filter(hc => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)\r\n .forEach(hc => (i = sync(p, i, hc, undefined, depth, uc)))\r\n return i\r\n}\r\n\r\nfunction sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: number, uc: J): number {\r\n\r\n // console.log('%csync', \"background:orange\", parent.tagName, i, h, hfun, depth)\r\n\r\n if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below\r\n\r\n let c = parent.childNodes[i]\r\n\r\n switch (h.kind) {\r\n case 1:\r\n {\r\n let n: Element\r\n let { tag, cn, p } = h\r\n cn ??= []\r\n\r\n let process = () => {\r\n setprops(n, n.h?.p, p)\r\n n.h = hfun ?? h // sets the new property values\r\n if (--depth && !n.hasAttribute('const') && !iswebcomponent(h as HElement)) {\r\n const j = syncchildren(n, cn, 0, depth, uc)\r\n removeexcesschildren(n, j)\r\n }\r\n }\r\n\r\n if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {\r\n let ns = p?.xmlns ?? (parent.tagName == \"foreignObject\" ? 'http://www.w3.org/1999/xhtml' : parent.namespaceURI)\r\n n = document.createElementNS(ns, tag)\r\n c ? c.replaceWith(n) : parent.appendChild(n)\r\n process()\r\n p?.mounted?.(n)\r\n } else {\r\n n = c as Element\r\n if (p?.update?.(c, uc)) return i + 1\r\n process()\r\n }\r\n\r\n return i + 1\r\n }\r\n\r\n case 4:\r\n {\r\n return syncchildren(parent, h.cn ?? [], i, --depth, uc)\r\n }\r\n\r\n case 2:\r\n {\r\n let hr = h.tag(h.p, h.cn)\r\n return sync(parent, i, hr, hfun ?? h, depth, uc)\r\n }\r\n\r\n case 3: {\r\n if ((c?.h as any)?.tag == h.tag) {\r\n let ci = (c.h as HClassComponent).i!\r\n ci.props = c.h!.p = h.p\r\n c.h!.p = h.p // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined\r\n // update() substitutes view/sync when returning truthy\r\n return (ci.update?.(uc)) ? i + 1 : sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached\r\n }\r\n else {\r\n let ci = h.i = rebind(new h.tag(h.p))\r\n let hv = ci.view()\r\n let j = sync(parent, i, hv, h, depth, uc)\r\n let n = parent.childNodes[i]!\r\n ci.element = n\r\n ci.mounted?.()\r\n return j\r\n }\r\n }\r\n\r\n default:\r\n // textnode\r\n let s = h.toString()\r\n if (c?.nodeType == NodeType.TextNode) {\r\n if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection\r\n } else {\r\n c ? c.replaceWith(s) : parent.append(s)\r\n }\r\n return i + 1\r\n }\r\n}\r\n\r\nexport function patch(e: Node | null, h: H, depth = 999, uc = {} as J): HTMLElement | undefined {\r\n if (!e) return\r\n if (depth == 0) {\r\n (e as HTMLElement).replaceChildren()\r\n depth = 999\r\n }\r\n let p = e.parentElement as HTMLElement\r\n let i = [].indexOf.call<any, any, any>(p.childNodes, e)\r\n sync(p, i, h, undefined, depth, uc)\r\n return p.childNodes[i] as HTMLElement\r\n}\r\n\r\nexport type Command = Selector | J | number\r\n\r\nexport function updateview(...cmds: Command[]): void {\r\n\r\n if (!cmds.length) cmds.push('body')\r\n let uc = {}\r\n let depth = 999\r\n let patchcount = 0\r\n\r\n function f(cmds: Command[]) {\r\n for (const c of cmds) {\r\n if (typeof c === 'string') {\r\n f([...document.querySelectorAll(c)])\r\n } else if (c instanceof Node) {\r\n if (c.h) {\r\n patch(c, c.h, depth, uc)\r\n patchcount++\r\n }\r\n } else if (typeof c === 'number') {\r\n depth = c\r\n } else {\r\n uc = c\r\n }\r\n }\r\n }\r\n\r\n f(cmds)\r\n\r\n if (!patchcount) console.warn(\"updateview did not update anything. cmds=\", ...cmds)\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAalC,IAAI,cAAc,GAAG,CAAC,CAAW,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AAEzD,IAAI,cAAc,GAAG,IAAI,GAAG,CAAC;IACzB,UAAU;IACV,SAAS;IACT,uEAAuE;CAC1E,CAAC,CAAA;AAEF,MAAM,UAAU,GAAG,CAAC,CAAU,EAAE,IAAY,EAAE,KAAU,EAAE,EAAE,CACxD,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC;IACzB,CAAC,KAAK,YAAY,MAAM;QACvB,KAAK,YAAY,QAAQ,IAAI,CAC7B,CAAC,CAAC,YAAY,KAAK,4BAA4B;QACxC,IAAI,IAAI,CAAC;QACT,IAAI,KAAK,OAAO,CAAC,CAAC,CAAA;AAE9B,IAAI,QAAQ,GAAG,CAAC,CAAc,EAAE,GAAkB,EAAE,GAAkB,EAAE,EAAE;IACtE,GAAG,KAAK,EAAE,CAAA;IACV,GAAG,KAAK,EAAE,CAAA;IAEV,WAAW;IACX,KAAK,IAAI,CAAC,IAAI,GAAG;QACb,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;YACX,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;gBACpB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAA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AE,CAAC;YACnB,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;gBACxB,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;YACxC,CAAC;iBAAM,IAAI,CAAC,YAAY,IAAI,EAAE,CAAC;gBAC3B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;oBACN,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAA;oBACxB,UAAU,EAAE,CAAA;gBAChB,CAAC;YACL,CAAC;iBAAM,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;gBAC/B,KAAK,GAAG,CAAC,CAAA;YACb,CAAC;iBAAM,CAAC;gBACJ,EAAE,GAAG,CAAC,CAAA;YACV,CAAC;QACL,CAAC;IACL,CAAC;IAED,CAAC,CAAC,IAAI,CAAC,CAAA;IAEP,sFAAsF;AAC1F,CAAC","sourcesContent":["import type {\r\n H,\r\n HElement,\r\n J,\r\n Selector,\r\n Children,\r\n HClassComponent,\r\n HU,\r\n} from './h'\r\nimport { rebind } from './base.js'\r\n\r\nconst enum NodeType {\r\n // vaporizes (but for that must be in this file, otherwise not)\r\n TextNode = 3,\r\n}\r\n\r\ndeclare global {\r\n interface Node {\r\n h?: H & { p?: J }\r\n }\r\n}\r\n\r\nlet iswebcomponent = (h: HElement) => h.tag.includes('-')\r\n\r\nlet attributeNames = new Set([\r\n 'selected',\r\n 'viewBox',\r\n // other HTML attributes where your framework wants attribute semantics\r\n])\r\n\r\nconst isproperty = (e: Element, name: string, value: any) =>\r\n !attributeNames.has(name) &&\r\n (value instanceof Object ||\r\n value instanceof Function || (\r\n e.namespaceURI !== 'http://www.w3.org/2000/svg' &&\r\n name in e &&\r\n name !== 'style'))\r\n\r\nlet setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {\r\n old ??= {}\r\n now ??= {}\r\n\r\n // removals\r\n for (let p in old)\r\n if (!(p in now))\r\n isproperty(e, p, old[p])\r\n ? (e[p] = typeof old[p] === 'boolean' ? false : '')\r\n : e.removeAttribute(p)\r\n\r\n // updates\r\n for (let p in now) {\r\n let v = now[p]\r\n v = v?.thunk ? v() : v\r\n\r\n if (v != old[p]) {\r\n if (isproperty(e, p, v)) e[p] = v\r\n else if ((v === false || v == null) && !p.startsWith('aria-')) e.removeAttribute(p)\r\n else e.setAttribute(p, v)\r\n }\r\n }\r\n}\r\n\r\nlet removeexcesschildren = (n: Element, i: number) => {\r\n let c: ChildNode\r\n while ((c = n.childNodes[i])) {\r\n try {\r\n c.remove()\r\n } catch (_) {} // due to blur-event-handlers, this can fail\r\n }\r\n}\r\n\r\n/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */\r\nlet syncchildren = (\r\n p: Element,\r\n cn: Children,\r\n i: number,\r\n depth: number,\r\n uc: J,\r\n): number => {\r\n cn.flat(999)\r\n .filter((hc) => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)\r\n .forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))\r\n return i\r\n}\r\n\r\nfunction sync(\r\n parent: Element,\r\n i: number,\r\n h: HU,\r\n hfun: H | undefined,\r\n depth: number,\r\n uc: J,\r\n): number {\r\n // console.log('%csync', \"background:orange\", parent.tagName, i, h, hfun, depth)\r\n\r\n if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below\r\n\r\n let c = parent.childNodes[i]\r\n\r\n switch (h.kind) {\r\n case 1: {\r\n let n: Element\r\n let { tag, cn, p } = h\r\n cn ??= []\r\n\r\n let process = () => {\r\n setprops(n, n.h?.p, p)\r\n n.h = hfun ?? h // sets the new property values\r\n if (\r\n --depth &&\r\n !n.hasAttribute('const') &&\r\n !iswebcomponent(h as HElement)\r\n ) {\r\n const j = syncchildren(n, cn, 0, depth, uc)\r\n removeexcesschildren(n, j)\r\n }\r\n }\r\n\r\n if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {\r\n let ns =\r\n p?.xmlns ??\r\n (parent.tagName == 'foreignObject'\r\n ? 'http://www.w3.org/1999/xhtml'\r\n : parent.namespaceURI)\r\n n = document.createElementNS(ns, tag)\r\n c ? c.replaceWith(n) : parent.appendChild(n)\r\n process()\r\n p?.mounted?.(n)\r\n } else {\r\n n = c as Element\r\n if (p?.update?.(c, uc)) return i + 1\r\n process()\r\n }\r\n\r\n return i + 1\r\n }\r\n\r\n case 4: {\r\n return syncchildren(parent, h.cn ?? [], i, --depth, uc)\r\n }\r\n\r\n case 2: {\r\n let hr = h.tag(h.p, h.cn)\r\n return sync(parent, i, hr, hfun ?? h, depth, uc)\r\n }\r\n\r\n case 3: {\r\n if ((c?.h as any)?.tag == h.tag) {\r\n let ci = (c.h as HClassComponent).i!\r\n ci.props = c.h!.p = h.p\r\n c.h!.p = h.p // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined\r\n // update() substitutes view/sync when returning truthy\r\n return ci.update?.(uc)\r\n ? i + 1\r\n : sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached\r\n } else {\r\n let ci = (h.i = rebind(new h.tag(h.p)))\r\n let hv = ci.view()\r\n let j = sync(parent, i, hv, h, depth, uc)\r\n let n = parent.childNodes[i]!\r\n ci.element = n\r\n ci.mounted?.()\r\n return j\r\n }\r\n }\r\n\r\n default:\r\n // textnode\r\n let s = h.toString()\r\n if (c?.nodeType == NodeType.TextNode) {\r\n if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection\r\n } else {\r\n c ? c.replaceWith(s) : parent.append(s)\r\n }\r\n return i + 1\r\n }\r\n}\r\n\r\nexport function patch(\r\n e: Node | null,\r\n h: H,\r\n depth = 999,\r\n uc = {} as J,\r\n): HTMLElement | undefined {\r\n if (!e) return\r\n if (depth == 0) {\r\n ;(e as HTMLElement).replaceChildren()\r\n depth = 999\r\n }\r\n let p = e.parentElement as HTMLElement\r\n let i = [].indexOf.call<any, any, any>(p.childNodes, e)\r\n sync(p, i, h, undefined, depth, uc)\r\n return p.childNodes[i] as HTMLElement\r\n}\r\n\r\nexport type Command = Selector | J | number\r\n\r\nexport function updateview(...cmds: Command[]): void {\r\n if (!cmds.length) cmds.push('body')\r\n let uc = {}\r\n let depth = 999\r\n let patchcount = 0\r\n\r\n function f(cmds: Command[]) {\r\n for (const c of cmds) {\r\n if (typeof c === 'string') {\r\n f([...document.querySelectorAll(c)])\r\n } else if (c instanceof Node) {\r\n if (c.h) {\r\n patch(c, c.h, depth, uc)\r\n patchcount++\r\n }\r\n } else if (typeof c === 'number') {\r\n depth = c\r\n } else {\r\n uc = c\r\n }\r\n }\r\n }\r\n\r\n f(cmds)\r\n\r\n // if (!patchcount) console.warn(\"updateview did not update anything. cmds=\", ...cmds)\r\n}\r\n"]}
|
package/package.json
CHANGED
package/src/jmc.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
H,
|
|
3
|
+
HElement,
|
|
4
|
+
J,
|
|
5
|
+
Selector,
|
|
6
|
+
Children,
|
|
7
|
+
HClassComponent,
|
|
8
|
+
HU,
|
|
9
|
+
} from './h'
|
|
2
10
|
import { rebind } from './base.js'
|
|
3
11
|
|
|
4
|
-
const enum NodeType {
|
|
5
|
-
|
|
12
|
+
const enum NodeType {
|
|
13
|
+
// vaporizes (but for that must be in this file, otherwise not)
|
|
14
|
+
TextNode = 3,
|
|
6
15
|
}
|
|
7
16
|
|
|
8
17
|
declare global {
|
|
@@ -13,14 +22,21 @@ declare global {
|
|
|
13
22
|
|
|
14
23
|
let iswebcomponent = (h: HElement) => h.tag.includes('-')
|
|
15
24
|
|
|
25
|
+
let attributeNames = new Set([
|
|
26
|
+
'selected',
|
|
27
|
+
'viewBox',
|
|
28
|
+
// other HTML attributes where your framework wants attribute semantics
|
|
29
|
+
])
|
|
30
|
+
|
|
16
31
|
const isproperty = (e: Element, name: string, value: any) =>
|
|
17
|
-
name
|
|
32
|
+
!attributeNames.has(name) &&
|
|
18
33
|
(value instanceof Object ||
|
|
19
|
-
|
|
20
|
-
|
|
34
|
+
value instanceof Function || (
|
|
35
|
+
e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
|
|
36
|
+
name in e &&
|
|
37
|
+
name !== 'style'))
|
|
21
38
|
|
|
22
39
|
let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
|
|
23
|
-
|
|
24
40
|
old ??= {}
|
|
25
41
|
now ??= {}
|
|
26
42
|
|
|
@@ -28,16 +44,18 @@ let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
|
|
|
28
44
|
for (let p in old)
|
|
29
45
|
if (!(p in now))
|
|
30
46
|
isproperty(e, p, old[p])
|
|
31
|
-
? e[p] = typeof old[p] === 'boolean' ? false : ''
|
|
47
|
+
? (e[p] = typeof old[p] === 'boolean' ? false : '')
|
|
32
48
|
: e.removeAttribute(p)
|
|
49
|
+
|
|
33
50
|
// updates
|
|
34
51
|
for (let p in now) {
|
|
35
52
|
let v = now[p]
|
|
36
53
|
v = v?.thunk ? v() : v
|
|
54
|
+
|
|
37
55
|
if (v != old[p]) {
|
|
38
|
-
isproperty(e, p, v)
|
|
39
|
-
|
|
40
|
-
|
|
56
|
+
if (isproperty(e, p, v)) e[p] = v
|
|
57
|
+
else if ((v === false || v == null) && !p.startsWith('aria-')) e.removeAttribute(p)
|
|
58
|
+
else e.setAttribute(p, v)
|
|
41
59
|
}
|
|
42
60
|
}
|
|
43
61
|
}
|
|
@@ -45,20 +63,34 @@ let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
|
|
|
45
63
|
let removeexcesschildren = (n: Element, i: number) => {
|
|
46
64
|
let c: ChildNode
|
|
47
65
|
while ((c = n.childNodes[i])) {
|
|
48
|
-
try {
|
|
66
|
+
try {
|
|
67
|
+
c.remove()
|
|
68
|
+
} catch (_) {} // due to blur-event-handlers, this can fail
|
|
49
69
|
}
|
|
50
70
|
}
|
|
51
71
|
|
|
52
72
|
/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */
|
|
53
|
-
let syncchildren = (
|
|
73
|
+
let syncchildren = (
|
|
74
|
+
p: Element,
|
|
75
|
+
cn: Children,
|
|
76
|
+
i: number,
|
|
77
|
+
depth: number,
|
|
78
|
+
uc: J,
|
|
79
|
+
): number => {
|
|
54
80
|
cn.flat(999)
|
|
55
|
-
.filter(hc => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
|
|
56
|
-
.forEach(hc => (i = sync(p, i, hc, undefined, depth, uc)))
|
|
81
|
+
.filter((hc) => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
|
|
82
|
+
.forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
|
|
57
83
|
return i
|
|
58
84
|
}
|
|
59
85
|
|
|
60
|
-
function sync(
|
|
61
|
-
|
|
86
|
+
function sync(
|
|
87
|
+
parent: Element,
|
|
88
|
+
i: number,
|
|
89
|
+
h: HU,
|
|
90
|
+
hfun: H | undefined,
|
|
91
|
+
depth: number,
|
|
92
|
+
uc: J,
|
|
93
|
+
): number {
|
|
62
94
|
// console.log('%csync', "background:orange", parent.tagName, i, h, hfun, depth)
|
|
63
95
|
|
|
64
96
|
if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below
|
|
@@ -66,46 +98,51 @@ function sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: num
|
|
|
66
98
|
let c = parent.childNodes[i]
|
|
67
99
|
|
|
68
100
|
switch (h.kind) {
|
|
69
|
-
case 1:
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
101
|
+
case 1: {
|
|
102
|
+
let n: Element
|
|
103
|
+
let { tag, cn, p } = h
|
|
104
|
+
cn ??= []
|
|
105
|
+
|
|
106
|
+
let process = () => {
|
|
107
|
+
setprops(n, n.h?.p, p)
|
|
108
|
+
n.h = hfun ?? h // sets the new property values
|
|
109
|
+
if (
|
|
110
|
+
--depth &&
|
|
111
|
+
!n.hasAttribute('const') &&
|
|
112
|
+
!iswebcomponent(h as HElement)
|
|
113
|
+
) {
|
|
114
|
+
const j = syncchildren(n, cn, 0, depth, uc)
|
|
115
|
+
removeexcesschildren(n, j)
|
|
82
116
|
}
|
|
83
|
-
|
|
84
|
-
if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {
|
|
85
|
-
let ns = p?.xmlns ?? (parent.tagName == "foreignObject" ? 'http://www.w3.org/1999/xhtml' : parent.namespaceURI)
|
|
86
|
-
n = document.createElementNS(ns, tag)
|
|
87
|
-
c ? c.replaceWith(n) : parent.appendChild(n)
|
|
88
|
-
process()
|
|
89
|
-
p?.mounted?.(n)
|
|
90
|
-
} else {
|
|
91
|
-
n = c as Element
|
|
92
|
-
if (p?.update?.(c, uc)) return i + 1
|
|
93
|
-
process()
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return i + 1
|
|
97
117
|
}
|
|
98
118
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
119
|
+
if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {
|
|
120
|
+
let ns =
|
|
121
|
+
p?.xmlns ??
|
|
122
|
+
(parent.tagName == 'foreignObject'
|
|
123
|
+
? 'http://www.w3.org/1999/xhtml'
|
|
124
|
+
: parent.namespaceURI)
|
|
125
|
+
n = document.createElementNS(ns, tag)
|
|
126
|
+
c ? c.replaceWith(n) : parent.appendChild(n)
|
|
127
|
+
process()
|
|
128
|
+
p?.mounted?.(n)
|
|
129
|
+
} else {
|
|
130
|
+
n = c as Element
|
|
131
|
+
if (p?.update?.(c, uc)) return i + 1
|
|
132
|
+
process()
|
|
102
133
|
}
|
|
103
134
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
135
|
+
return i + 1
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
case 4: {
|
|
139
|
+
return syncchildren(parent, h.cn ?? [], i, --depth, uc)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
case 2: {
|
|
143
|
+
let hr = h.tag(h.p, h.cn)
|
|
144
|
+
return sync(parent, i, hr, hfun ?? h, depth, uc)
|
|
145
|
+
}
|
|
109
146
|
|
|
110
147
|
case 3: {
|
|
111
148
|
if ((c?.h as any)?.tag == h.tag) {
|
|
@@ -113,10 +150,11 @@ function sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: num
|
|
|
113
150
|
ci.props = c.h!.p = h.p
|
|
114
151
|
c.h!.p = h.p // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined
|
|
115
152
|
// update() substitutes view/sync when returning truthy
|
|
116
|
-
return
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
153
|
+
return ci.update?.(uc)
|
|
154
|
+
? i + 1
|
|
155
|
+
: sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached
|
|
156
|
+
} else {
|
|
157
|
+
let ci = (h.i = rebind(new h.tag(h.p)))
|
|
120
158
|
let hv = ci.view()
|
|
121
159
|
let j = sync(parent, i, hv, h, depth, uc)
|
|
122
160
|
let n = parent.childNodes[i]!
|
|
@@ -138,10 +176,15 @@ function sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: num
|
|
|
138
176
|
}
|
|
139
177
|
}
|
|
140
178
|
|
|
141
|
-
export function patch(
|
|
179
|
+
export function patch(
|
|
180
|
+
e: Node | null,
|
|
181
|
+
h: H,
|
|
182
|
+
depth = 999,
|
|
183
|
+
uc = {} as J,
|
|
184
|
+
): HTMLElement | undefined {
|
|
142
185
|
if (!e) return
|
|
143
186
|
if (depth == 0) {
|
|
144
|
-
(e as HTMLElement).replaceChildren()
|
|
187
|
+
;(e as HTMLElement).replaceChildren()
|
|
145
188
|
depth = 999
|
|
146
189
|
}
|
|
147
190
|
let p = e.parentElement as HTMLElement
|
|
@@ -153,7 +196,6 @@ export function patch(e: Node | null, h: H, depth = 999, uc = {} as J): HTMLElem
|
|
|
153
196
|
export type Command = Selector | J | number
|
|
154
197
|
|
|
155
198
|
export function updateview(...cmds: Command[]): void {
|
|
156
|
-
|
|
157
199
|
if (!cmds.length) cmds.push('body')
|
|
158
200
|
let uc = {}
|
|
159
201
|
let depth = 999
|
|
@@ -178,5 +220,5 @@ export function updateview(...cmds: Command[]): void {
|
|
|
178
220
|
|
|
179
221
|
f(cmds)
|
|
180
222
|
|
|
181
|
-
if (!patchcount) console.warn("updateview did not update anything. cmds=", ...cmds)
|
|
223
|
+
// if (!patchcount) console.warn("updateview did not update anything. cmds=", ...cmds)
|
|
182
224
|
}
|