@snupo/jmc 1.0.20 → 1.0.22

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/src/jmc.ts ADDED
@@ -0,0 +1,169 @@
1
+ import { rebind } from './base.js'
2
+ import type { H, HElement, IUpdateContext, J, Selector, Children, HClassComponent } from './h'
3
+ import { HKind } from './h'
4
+
5
+ const enum NodeType { // vaporizes (but for that must be in this file, otherwise not)
6
+ TextNode = 3
7
+ }
8
+
9
+ declare global {
10
+ interface Node {
11
+ h?: H & { p?: J }
12
+ }
13
+ }
14
+
15
+ let removeexcesschildren = (n: Element, i: number) => {
16
+ let c: ChildNode
17
+ while ((c = n.childNodes[i])) {
18
+ try { c.remove() } catch (_) { } // due to blur-event-handlers, this can fail
19
+ }
20
+ }
21
+
22
+ let iswebcomponent = (h: HElement) => h.tag.includes('-')
23
+
24
+ const isproperty = (e: Element, name: string, value: any) =>
25
+ name !== "viewBox" &&
26
+ (value instanceof Object ||
27
+ value instanceof Function ||
28
+ (e.namespaceURI !== "http://www.w3.org/2000/svg" && name in e && name != 'style'))
29
+
30
+ let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
31
+
32
+ old ??= {}
33
+ now ??= {}
34
+
35
+ // removals
36
+ for (let p in old)
37
+ if (!(p in now))
38
+ isproperty(e, p, old[p])
39
+ ? e[p] = typeof old[p] === 'boolean' ? false : ''
40
+ : e.removeAttribute(p)
41
+ // updates
42
+ for (let p in now) {
43
+ let v = now[p]
44
+ if (v != old[p]) {
45
+ isproperty(e, p, v)
46
+ ? e[p] = v
47
+ : e.setAttribute(p, v)
48
+ }
49
+ }
50
+ }
51
+
52
+ /** synchronizes children starting at the i-th element. returns the index of the last child synchronized */
53
+ let syncchildren = (uc: IUpdateContext, p: Element, cn: Children, i: number, ns: string): number => {
54
+ cn.flat().forEach(hc => (i = sync(uc, p, i, hc, ns)))
55
+ return i
56
+ }
57
+
58
+ function sync(uc: IUpdateContext, parent: Element, i: number, h: H | undefined, ns: string): number {
59
+
60
+ // console.log('%csync2', "background:orange", parent.tagName, i, h)
61
+
62
+ if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below
63
+ let c = parent.childNodes[i]
64
+
65
+ switch (h.kind) {
66
+ case HKind.Element:
67
+ {
68
+ let n: Element
69
+ let { tag, cn, p } = h
70
+ cn ??= []
71
+
72
+ if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {
73
+ n = document.createElementNS(ns = p?.xmlns ?? ns, tag) // replace namespace with propertyvalue if set. otherwise pass forward to children
74
+ c ? c.replaceWith(n) : parent.appendChild(n)
75
+ setprops(n, {}, p)
76
+ p?.mounted?.(n)
77
+ } else {
78
+ n = c as Element
79
+ setprops(n, n.h?.p, p) // tbd: this is wrong - try to break this with property updates, just patch sequences - create an own patch test file
80
+ if (p?.update?.(c, uc)) return i + 1
81
+ }
82
+
83
+ n.h = h // sets the new property values
84
+
85
+ if (!uc.patchElementOnly && !iswebcomponent(h as HElement)) {
86
+
87
+ // island attribute
88
+ if (n.h?.p?.const) return i + 1
89
+
90
+ const j = syncchildren(uc, n, cn, 0, ns)
91
+ removeexcesschildren(n, j)
92
+ }
93
+
94
+ return i + 1
95
+ }
96
+
97
+ case HKind.Fragment:
98
+ {
99
+ return syncchildren(uc, parent, h.cn ?? [], i, ns)
100
+ }
101
+
102
+ case HKind.FunctionComponent:
103
+ {
104
+ let hr = h.tag(h.p, h.cn)
105
+ let j = sync(uc, parent, i, hr, ns)
106
+ let n = parent.childNodes[i]! // now the dom element exists, unless the function returned undefined or null
107
+ if (n) n.h = h
108
+ return j
109
+ }
110
+
111
+ case HKind.ClassComponent: {
112
+ let isupdate = (c?.h as any)?.tag == h.tag
113
+
114
+ if (isupdate) {
115
+ let ci = (c!.h as HClassComponent).i!
116
+ ci.props = h.p
117
+
118
+ // update() substitutes view/sync when returning truthy
119
+ return (ci.update?.(uc)) ? i + 1 : sync(uc, parent, i, ci.view(), ns)
120
+ }
121
+
122
+ let ci = h.i = rebind(new h.tag(h.p))
123
+ let j = sync(uc, parent, i, ci.view(), ns)
124
+ let n = parent.childNodes[i]!
125
+ ci.element = n
126
+ n.h = h
127
+ ci.mounted?.()
128
+ return j
129
+ }
130
+
131
+ case undefined:
132
+ // textnode
133
+ let s = h as string // coerce type as the js runtime does
134
+ if (c?.nodeType == NodeType.TextNode) {
135
+ if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection
136
+ } else {
137
+ c ? c.replaceWith(s) : parent.append(s)
138
+ }
139
+ return i + 1
140
+ }
141
+ }
142
+
143
+ export function patch(e: Node | null, h?: H, uc = {} as IUpdateContext) {
144
+ if (!e) return
145
+ if (uc.replace) (e as HTMLElement).replaceChildren()
146
+
147
+ // walk up
148
+ while (!h && e) h = (e = e.parentNode)?.h
149
+ if (!h || !e) {
150
+ console.warn("patch not possible, h is undefined in whole parent chain", e, h)
151
+ return
152
+ }
153
+
154
+ let p = e.parentElement as HTMLElement
155
+ let i = [].indexOf.call<any, any, any>(p.childNodes, e)
156
+ // always called deferred, because removing elements can trigger events and their handlers (like blur)
157
+ sync(uc, p, i, h, p.namespaceURI!) // is this safe? use default html namespace?
158
+ }
159
+
160
+ export function updateviewuc(u: IUpdateContext, ...sels: Selector[]): void { updateviewinternal(u, ...sels) }
161
+ export function updateview(...sels: Selector[]): void { updateviewinternal({}, ...sels) }
162
+
163
+ function updateviewinternal(uc: IUpdateContext, ...sels: Selector[]): void {
164
+ if (!sels.length) sels.push('body')
165
+ sels.flatMap(s => (typeof s == 'string' ? [...document.querySelectorAll(s)] : s ? [s] : []))
166
+ .forEach(e => { patch(e, e.h, uc) })
167
+ }
168
+
169
+ // console.log("jmc version 1.0.16");