@snupo/jmc 1.0.61 → 1.0.62
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/events.d.ts +9 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +16 -0
- package/dist/events.js.map +1 -0
- package/dist/jmc.d.ts +3 -0
- package/dist/jmc.d.ts.map +1 -1
- package/dist/jmc.js +18 -6
- package/dist/jmc.js.map +1 -1
- package/package.json +1 -1
- package/src/events.ts +25 -0
- package/src/jmc.ts +20 -11
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAEA,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,OAAO;QACb,QAAQ,CAAC,EAAE;YAAE,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,CAAA;SAAE,CAAA;KACtD;CACJ;AAED,wBAAgB,QAAQ,CACpB,EAAE,EAAE,OAAO,EACX,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,aAAa,GAAG,IAAI,QAahC"}
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function setevent(el, type, handler) {
|
|
2
|
+
let map = el.eventmap ??= {};
|
|
3
|
+
const old = map[type];
|
|
4
|
+
if (old === handler)
|
|
5
|
+
return;
|
|
6
|
+
if (old)
|
|
7
|
+
el.removeEventListener(type, old);
|
|
8
|
+
if (handler) {
|
|
9
|
+
el.addEventListener(type, handler);
|
|
10
|
+
map[type] = handler;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
delete map[type];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,QAAQ,CACpB,EAAW,EACX,IAAY,EACZ,OAA6B;IAE7B,IAAI,GAAG,GAAG,EAAE,CAAC,QAAQ,KAAK,EAAE,CAAA;IAE5B,MAAM,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,CAAA;IACrB,IAAI,GAAG,KAAK,OAAO;QAAE,OAAM;IAC3B,IAAI,GAAG;QAAE,EAAE,CAAC,mBAAmB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IAC1C,IAAI,OAAO,EAAE,CAAC;QACV,EAAE,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAClC,GAAG,CAAC,IAAI,CAAC,GAAG,OAAO,CAAA;IACvB,CAAC;SAAM,CAAC;QACJ,OAAO,GAAG,CAAC,IAAI,CAAC,CAAA;IACpB,CAAC;AACL,CAAC","sourcesContent":["\r\n\r\ndeclare global {\r\n interface Element {\r\n eventmap?: { [type: string]: EventListener | null }\r\n }\r\n}\r\n\r\nexport function setevent(\r\n el: Element,\r\n type: string,\r\n handler: EventListener | null) {\r\n\r\n let map = el.eventmap ??= {}\r\n\r\n const old = map[type]\r\n if (old === handler) return\r\n if (old) el.removeEventListener(type, old)\r\n if (handler) {\r\n el.addEventListener(type, handler)\r\n map[type] = handler\r\n } else {\r\n delete map[type]\r\n }\r\n}"]}
|
package/dist/jmc.d.ts
CHANGED
|
@@ -6,6 +6,9 @@ declare global {
|
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
export declare let jmcconfig: {
|
|
10
|
+
attributeNames: Set<string>;
|
|
11
|
+
};
|
|
9
12
|
export declare function patch(e: Node | null, h: H, depth?: number, uc?: J): HTMLElement | undefined;
|
|
10
13
|
export type Command = Selector | J | number;
|
|
11
14
|
export declare function updateview(...cmds: Command[]): void;
|
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,EACR,CAAC,EAED,CAAC,EACD,QAAQ,EAIX,MAAM,KAAK,CAAA;
|
|
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;AASZ,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,IAAI;QACV,CAAC,CAAC,EAAE,CAAC,GAAG;YAAE,CAAC,CAAC,EAAE,CAAC,CAAA;SAAE,CAAA;KACpB;CACJ;AAWD,eAAO,IAAI,SAAS;;CAEnB,CAAA;AAyJD,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,10 +1,15 @@
|
|
|
1
1
|
import { rebind } from './base.js';
|
|
2
|
+
import { setevent } from './events';
|
|
2
3
|
let iswebcomponent = (h) => h.tag.includes('-');
|
|
3
4
|
let attributeNames = new Set([
|
|
4
5
|
'selected',
|
|
5
6
|
'viewBox',
|
|
7
|
+
'list'
|
|
6
8
|
// other HTML attributes where your framework wants attribute semantics
|
|
7
9
|
]);
|
|
10
|
+
export let jmcconfig = {
|
|
11
|
+
attributeNames
|
|
12
|
+
};
|
|
8
13
|
const isproperty = (e, name, value) => !attributeNames.has(name) &&
|
|
9
14
|
(value instanceof Object ||
|
|
10
15
|
value instanceof Function || (e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
|
|
@@ -14,16 +19,23 @@ let setprops = (e, old, now) => {
|
|
|
14
19
|
old ??= {};
|
|
15
20
|
now ??= {};
|
|
16
21
|
// removals
|
|
17
|
-
for (let p in old)
|
|
18
|
-
if (!(p in now))
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
for (let p in old) {
|
|
23
|
+
if (!(p in now)) {
|
|
24
|
+
if (p.startsWith('on'))
|
|
25
|
+
setevent(e, p.slice(2), null);
|
|
26
|
+
else if (isproperty(e, p, old[p]))
|
|
27
|
+
e[p] = typeof old[p] === 'boolean' ? false : '';
|
|
28
|
+
else
|
|
29
|
+
e.removeAttribute(p);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
22
32
|
// updates
|
|
23
33
|
for (let p in now) {
|
|
24
34
|
let v = now[p];
|
|
25
35
|
v = v?.thunk ? v() : v;
|
|
26
|
-
if (
|
|
36
|
+
if (p.startsWith('on'))
|
|
37
|
+
setevent(e, p.slice(2), v);
|
|
38
|
+
else if (v != old[p]) {
|
|
27
39
|
if (isproperty(e, p, v))
|
|
28
40
|
e[p] = v;
|
|
29
41
|
else if ((v === false || v == null) && !p.startsWith('aria-'))
|
package/dist/jmc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,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port { 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"]}
|
|
1
|
+
{"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAanC,IAAI,cAAc,GAAG,CAAC,CAAW,EAAE,EAAE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CAAC,IAAI,IAAI,EAAE,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\nimport { setevent } from './events'\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 'list'\r\n // other HTML attributes where your framework wants attribute semantics\r\n])\r\n\r\nexport let jmcconfig = {\r\n attributeNames\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 if (p.startsWith('on')) setevent(e, p.slice(2), null)\r\n else if (isproperty(e, p, old[p])) e[p] = typeof old[p] === 'boolean' ? false : ''\r\n else e.removeAttribute(p)\r\n }\r\n }\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 (p.startsWith('on')) setevent(e, p.slice(2), v)\r\n else 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/events.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface Element {
|
|
5
|
+
eventmap?: { [type: string]: EventListener | null }
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function setevent(
|
|
10
|
+
el: Element,
|
|
11
|
+
type: string,
|
|
12
|
+
handler: EventListener | null) {
|
|
13
|
+
|
|
14
|
+
let map = el.eventmap ??= {}
|
|
15
|
+
|
|
16
|
+
const old = map[type]
|
|
17
|
+
if (old === handler) return
|
|
18
|
+
if (old) el.removeEventListener(type, old)
|
|
19
|
+
if (handler) {
|
|
20
|
+
el.addEventListener(type, handler)
|
|
21
|
+
map[type] = handler
|
|
22
|
+
} else {
|
|
23
|
+
delete map[type]
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/jmc.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
HU,
|
|
9
9
|
} from './h'
|
|
10
10
|
import { rebind } from './base.js'
|
|
11
|
+
import { setevent } from './events'
|
|
11
12
|
|
|
12
13
|
const enum NodeType {
|
|
13
14
|
// vaporizes (but for that must be in this file, otherwise not)
|
|
@@ -24,15 +25,20 @@ let iswebcomponent = (h: HElement) => h.tag.includes('-')
|
|
|
24
25
|
|
|
25
26
|
let attributeNames = new Set([
|
|
26
27
|
'selected',
|
|
27
|
-
'viewBox',
|
|
28
|
+
'viewBox',
|
|
29
|
+
'list'
|
|
28
30
|
// other HTML attributes where your framework wants attribute semantics
|
|
29
31
|
])
|
|
30
32
|
|
|
33
|
+
export let jmcconfig = {
|
|
34
|
+
attributeNames
|
|
35
|
+
}
|
|
36
|
+
|
|
31
37
|
const isproperty = (e: Element, name: string, value: any) =>
|
|
32
38
|
!attributeNames.has(name) &&
|
|
33
39
|
(value instanceof Object ||
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
value instanceof Function || (
|
|
41
|
+
e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
|
|
36
42
|
name in e &&
|
|
37
43
|
name !== 'style'))
|
|
38
44
|
|
|
@@ -41,18 +47,21 @@ let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
|
|
|
41
47
|
now ??= {}
|
|
42
48
|
|
|
43
49
|
// removals
|
|
44
|
-
for (let p in old)
|
|
45
|
-
if (!(p in now))
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
for (let p in old) {
|
|
51
|
+
if (!(p in now)) {
|
|
52
|
+
if (p.startsWith('on')) setevent(e, p.slice(2), null)
|
|
53
|
+
else if (isproperty(e, p, old[p])) e[p] = typeof old[p] === 'boolean' ? false : ''
|
|
54
|
+
else e.removeAttribute(p)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
49
57
|
|
|
50
58
|
// updates
|
|
51
59
|
for (let p in now) {
|
|
52
60
|
let v = now[p]
|
|
53
61
|
v = v?.thunk ? v() : v
|
|
54
62
|
|
|
55
|
-
if (
|
|
63
|
+
if (p.startsWith('on')) setevent(e, p.slice(2), v)
|
|
64
|
+
else if (v != old[p]) {
|
|
56
65
|
if (isproperty(e, p, v)) e[p] = v
|
|
57
66
|
else if ((v === false || v == null) && !p.startsWith('aria-')) e.removeAttribute(p)
|
|
58
67
|
else e.setAttribute(p, v)
|
|
@@ -65,7 +74,7 @@ let removeexcesschildren = (n: Element, i: number) => {
|
|
|
65
74
|
while ((c = n.childNodes[i])) {
|
|
66
75
|
try {
|
|
67
76
|
c.remove()
|
|
68
|
-
} catch (_) {} // due to blur-event-handlers, this can fail
|
|
77
|
+
} catch (_) { } // due to blur-event-handlers, this can fail
|
|
69
78
|
}
|
|
70
79
|
}
|
|
71
80
|
|
|
@@ -184,7 +193,7 @@ export function patch(
|
|
|
184
193
|
): HTMLElement | undefined {
|
|
185
194
|
if (!e) return
|
|
186
195
|
if (depth == 0) {
|
|
187
|
-
;(e as HTMLElement).replaceChildren()
|
|
196
|
+
; (e as HTMLElement).replaceChildren()
|
|
188
197
|
depth = 999
|
|
189
198
|
}
|
|
190
199
|
let p = e.parentElement as HTMLElement
|