@snupo/jmc 1.0.61 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/base.d.ts +3 -2
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +11 -13
- package/dist/base.js.map +1 -1
- package/dist/events.d.ts +9 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +17 -0
- package/dist/events.js.map +1 -0
- package/dist/h.d.ts +14 -7
- package/dist/h.d.ts.map +1 -1
- package/dist/h.js +7 -4
- package/dist/h.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/jmc.d.ts +1 -9
- package/dist/jmc.d.ts.map +1 -1
- package/dist/jmc.js +80 -107
- package/dist/jmc.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +5 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.js +6 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-runtime.d.ts +14 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +10 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/jsx.d.ts +39 -46
- package/dist/jsx.d.ts.map +1 -1
- package/dist/jsx.js.map +1 -1
- package/dist/lib.d.ts +5 -3
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +4 -5
- package/dist/lib.js.map +1 -1
- package/dist/props.d.ts +11 -0
- package/dist/props.d.ts.map +1 -0
- package/dist/props.js +43 -0
- package/dist/props.js.map +1 -0
- package/dist/update.d.ts +10 -0
- package/dist/update.d.ts.map +1 -0
- package/dist/update.js +48 -0
- package/dist/update.js.map +1 -0
- package/package.json +29 -3
- package/readme.md +303 -34
- package/src/base.ts +11 -15
- package/src/events.ts +25 -0
- package/src/h.ts +29 -18
- package/src/index.ts +2 -1
- package/src/jmc.ts +90 -129
- package/src/jsx-dev-runtime.ts +8 -0
- package/src/jsx-runtime.ts +17 -0
- package/src/jsx.ts +18 -30
- package/src/lib.ts +10 -8
- package/src/props.ts +43 -0
- package/src/update.ts +54 -0
- package/dist/updateview.d.ts +0 -17
- package/dist/updateview.d.ts.map +0 -1
- package/dist/updateview.js +0 -43
- package/dist/updateview.js.map +0 -1
- package/src/updateview.ts +0 -48
package/src/base.ts
CHANGED
|
@@ -5,28 +5,24 @@ export function rebind<T extends object>(o: T, proto = Object.getPrototypeOf(o))
|
|
|
5
5
|
return o
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/** wraps o so that each method call is reported to logger, after the method returned */
|
|
10
9
|
export const loggedmethodsex = <T extends Record<string, any>>(o: T, logger: (name: string, args: any[], result: any) => void) => new Proxy(o, {
|
|
11
10
|
get(target, name: string, receiver) {
|
|
12
|
-
if (typeof target[name]
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return r
|
|
18
|
-
}
|
|
11
|
+
if (typeof target[name] !== 'function') return Reflect.get(target, name, receiver)
|
|
12
|
+
return function (this: T, ...args: any[]) {
|
|
13
|
+
let result = target[name].apply(this, args)
|
|
14
|
+
logger(name, args, result)
|
|
15
|
+
return result
|
|
19
16
|
}
|
|
20
|
-
return Reflect.get(target, name, receiver)
|
|
21
17
|
},
|
|
22
18
|
})
|
|
23
19
|
|
|
24
|
-
export const
|
|
25
|
-
|
|
20
|
+
export const loggedmethodscolored = <T extends Record<string, any>>(bgcolor: string, o: T): T => loggedmethodsex(o, (name, args, result) => console.log('%c' + name, `background:${bgcolor};color:white;padding:2px;font-weight:bold`, args, result))
|
|
21
|
+
|
|
22
|
+
export const loggedmethods = <T extends Record<string, any>>(o: T): T => loggedmethodscolored('#585059', o)
|
|
26
23
|
|
|
27
24
|
export function closestby(e: Node | null, pred: (e: Node) => boolean): Node | null { return (e == null || pred(e)) ? e : closestby(e.parentNode, pred) }
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
// export let evaluate = <T>(expr: Expr<T>): T => (expr instanceof Function ? expr() : expr)
|
|
26
|
+
export function lazy<T>(f: () => T) { (f as any).thunk = 1; return f }
|
|
31
27
|
|
|
32
|
-
export function
|
|
28
|
+
export function mount(o: Record<string, any>) { Object.assign(globalThis, o) }
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// future work: maybe lifecycle callbacks should occur in a phase 2 after a phase 1 that does all dom updates
|
|
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/h.ts
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
|
+
import type { JmcIntrinsicElements } from './jsx.js'
|
|
2
|
+
|
|
1
3
|
export type J = Record<string, any> // a json record
|
|
2
|
-
export type UC = J // update context
|
|
3
4
|
|
|
4
|
-
// Overload signatures
|
|
5
5
|
export type Selector = string | Node
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
export type
|
|
7
|
+
/** an updateview command: a target (selector or node), a depth (number) or an update context (object) */
|
|
8
|
+
export type Command = Selector | J | number
|
|
9
|
+
|
|
10
|
+
// TBD REMOVE the children parameter once we know it is unused: components get their children in props.children.
|
|
11
|
+
// the second parameter remains for function components written before props.children existed
|
|
12
|
+
export type FComponent = (props: J, children?: Children) => HU
|
|
9
13
|
|
|
10
14
|
type HKindElement = 1
|
|
11
15
|
type HKindFunctionComponent = 2
|
|
12
16
|
type HKindClassComponent = 3
|
|
13
17
|
type HKindFragment = 4
|
|
14
18
|
|
|
15
|
-
// export const kindelement : HKindElement = 1
|
|
16
|
-
// export const kindfc : HKindFunctionComponent = 2
|
|
17
|
-
// export const kindcc : HKindClassComponent = 3
|
|
18
|
-
// export const kindfragment : HKindFragment = 4
|
|
19
|
-
|
|
20
19
|
export type HKind = HKindElement | HKindFunctionComponent | HKindClassComponent | HKindFragment
|
|
21
20
|
|
|
22
21
|
export interface IClassComponent {
|
|
@@ -36,7 +35,7 @@ export type Children = HU[]
|
|
|
36
35
|
export type HText =
|
|
37
36
|
| string // text node
|
|
38
37
|
| number // text node
|
|
39
|
-
| boolean //
|
|
38
|
+
| boolean // renders nothing, like null and undefined
|
|
40
39
|
|
|
41
40
|
export type HFragment = {
|
|
42
41
|
kind: HKindFragment
|
|
@@ -46,21 +45,21 @@ export type HFragment = {
|
|
|
46
45
|
export type HElement = {
|
|
47
46
|
kind: HKindElement
|
|
48
47
|
tag: string
|
|
49
|
-
p
|
|
48
|
+
p: J
|
|
50
49
|
cn?: Children
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
export type HFunctionComponent = {
|
|
54
53
|
kind: HKindFunctionComponent
|
|
55
54
|
tag: FComponent
|
|
56
|
-
p
|
|
55
|
+
p: J
|
|
57
56
|
cn?: Children
|
|
58
57
|
}
|
|
59
58
|
|
|
60
59
|
export type HClassComponent = {
|
|
61
60
|
kind: HKindClassComponent
|
|
62
61
|
tag: CComponent
|
|
63
|
-
p
|
|
62
|
+
p: J
|
|
64
63
|
cn?: Children
|
|
65
64
|
i?: IClassComponent
|
|
66
65
|
}
|
|
@@ -72,23 +71,35 @@ export type H =
|
|
|
72
71
|
| HElement
|
|
73
72
|
| HFunctionComponent
|
|
74
73
|
| HClassComponent
|
|
75
|
-
| HFragment
|
|
74
|
+
| HFragment
|
|
76
75
|
|
|
77
76
|
export type HU = H | undefined | null
|
|
78
77
|
|
|
79
78
|
let isclass = (f: Function) => /^class\s|^class\{/.test(f.toString())
|
|
80
79
|
|
|
81
|
-
export function jsx(tag: any, p: any, ...cn: any[]): HElement |
|
|
80
|
+
export function jsx(tag: any, p: any, ...cn: any[]): HElement | HComp | HFragment {
|
|
82
81
|
if (tag?.kind) return tag // already an H node, flatten tree
|
|
83
82
|
|
|
84
83
|
let kind: HKind = tag == jsxf ? 4 : tag instanceof Function ? (isclass(tag) ? 3 : 2) : 1
|
|
85
84
|
|
|
86
|
-
let h: any = { kind }
|
|
85
|
+
let h: any = { kind, p: p ?? {} }
|
|
87
86
|
if (kind != 4) h.tag = tag
|
|
88
|
-
if (
|
|
89
|
-
|
|
87
|
+
if (cn?.length) {
|
|
88
|
+
h.cn = cn
|
|
89
|
+
// components get their children in props.children, as with the automatic runtime (jsx-runtime.ts).
|
|
90
|
+
// elements do not: setprops would write them to the element's read-only children property
|
|
91
|
+
if (kind == 2 || kind == 3) h.p = { ...h.p, children: cn }
|
|
92
|
+
}
|
|
90
93
|
return h
|
|
91
94
|
}
|
|
92
95
|
|
|
96
|
+
// typescript looks up the JSX types on the factory function first: jsx.JSX instead of a global JSX namespace.
|
|
97
|
+
// so jmc's element types cannot clash with other libraries. add your own elements by augmenting jsx.JSX.IntrinsicElements
|
|
98
|
+
export namespace jsx {
|
|
99
|
+
export namespace JSX {
|
|
100
|
+
export interface IntrinsicElements extends JmcIntrinsicElements { }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
93
104
|
// fragment is only used above as a sentinel
|
|
94
105
|
export let jsxf = Symbol()
|
package/src/index.ts
CHANGED
package/src/jmc.ts
CHANGED
|
@@ -1,63 +1,28 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
H,
|
|
3
3
|
HElement,
|
|
4
|
+
HComp,
|
|
4
5
|
J,
|
|
5
|
-
|
|
6
|
+
Command,
|
|
6
7
|
Children,
|
|
7
8
|
HClassComponent,
|
|
8
9
|
HU,
|
|
9
|
-
} from './h'
|
|
10
|
+
} from './h.js'
|
|
10
11
|
import { rebind } from './base.js'
|
|
12
|
+
import { jmcconfig, setprops } from './props.js'
|
|
11
13
|
|
|
12
14
|
const enum NodeType {
|
|
13
15
|
// vaporizes (but for that must be in this file, otherwise not)
|
|
14
16
|
TextNode = 3,
|
|
17
|
+
Comment = 8,
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
let iswebcomponent = (h: HElement) => h.tag.includes('-')
|
|
24
|
-
|
|
25
|
-
let attributeNames = new Set([
|
|
26
|
-
'selected',
|
|
27
|
-
'viewBox',
|
|
28
|
-
// other HTML attributes where your framework wants attribute semantics
|
|
29
|
-
])
|
|
30
|
-
|
|
31
|
-
const isproperty = (e: Element, name: string, value: any) =>
|
|
32
|
-
!attributeNames.has(name) &&
|
|
33
|
-
(value instanceof Object ||
|
|
34
|
-
value instanceof Function || (
|
|
35
|
-
e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
|
|
36
|
-
name in e &&
|
|
37
|
-
name !== 'style'))
|
|
38
|
-
|
|
39
|
-
let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
|
|
40
|
-
old ??= {}
|
|
41
|
-
now ??= {}
|
|
42
|
-
|
|
43
|
-
// removals
|
|
44
|
-
for (let p in old)
|
|
45
|
-
if (!(p in now))
|
|
46
|
-
isproperty(e, p, old[p])
|
|
47
|
-
? (e[p] = typeof old[p] === 'boolean' ? false : '')
|
|
48
|
-
: e.removeAttribute(p)
|
|
49
|
-
|
|
50
|
-
// updates
|
|
51
|
-
for (let p in now) {
|
|
52
|
-
let v = now[p]
|
|
53
|
-
v = v?.thunk ? v() : v
|
|
54
|
-
|
|
55
|
-
if (v != old[p]) {
|
|
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)
|
|
59
|
-
}
|
|
60
|
-
}
|
|
20
|
+
/** a node as jmc sees it. kept module-local, so consumers' Node type stays untouched */
|
|
21
|
+
type JNode<N extends Node = ChildNode> = N & {
|
|
22
|
+
/** the H this node updates from: the element's own H, or the outermost component rendering it */
|
|
23
|
+
h?: HElement | HComp
|
|
24
|
+
/** props last applied to this element. kept apart from h, which may be the owning component's H */
|
|
25
|
+
ep?: J
|
|
61
26
|
}
|
|
62
27
|
|
|
63
28
|
let removeexcesschildren = (n: Element, i: number) => {
|
|
@@ -65,11 +30,11 @@ let removeexcesschildren = (n: Element, i: number) => {
|
|
|
65
30
|
while ((c = n.childNodes[i])) {
|
|
66
31
|
try {
|
|
67
32
|
c.remove()
|
|
68
|
-
} catch
|
|
33
|
+
} catch { } // due to blur-event-handlers, this can fail
|
|
69
34
|
}
|
|
70
35
|
}
|
|
71
36
|
|
|
72
|
-
/** synchronizes children starting at the i-th
|
|
37
|
+
/** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
|
|
73
38
|
let syncchildren = (
|
|
74
39
|
p: Element,
|
|
75
40
|
cn: Children,
|
|
@@ -77,91 +42,102 @@ let syncchildren = (
|
|
|
77
42
|
depth: number,
|
|
78
43
|
uc: J,
|
|
79
44
|
): number => {
|
|
80
|
-
cn.flat(999)
|
|
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)))
|
|
45
|
+
cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
|
|
83
46
|
return i
|
|
84
47
|
}
|
|
85
48
|
|
|
49
|
+
/** class components must render exactly one root node, they are attached to it and update through it */
|
|
50
|
+
let assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {
|
|
51
|
+
if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)
|
|
52
|
+
}
|
|
53
|
+
|
|
86
54
|
function sync(
|
|
87
55
|
parent: Element,
|
|
88
56
|
i: number,
|
|
89
57
|
h: HU,
|
|
90
|
-
hfun:
|
|
58
|
+
hfun: HComp | undefined,
|
|
91
59
|
depth: number,
|
|
92
60
|
uc: J,
|
|
93
61
|
): number {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
62
|
+
let c: JNode | undefined = parent.childNodes[i]
|
|
63
|
+
|
|
64
|
+
// null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.
|
|
65
|
+
// nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state
|
|
66
|
+
if (h == null || typeof h == 'boolean') {
|
|
67
|
+
if (hfun?.kind != 3) h = '' // an empty text node, invisible
|
|
68
|
+
else {
|
|
69
|
+
// a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again
|
|
70
|
+
if (c?.nodeType != NodeType.Comment) {
|
|
71
|
+
let n = new Comment()
|
|
72
|
+
c ? c.replaceWith(n) : parent.append(n)
|
|
73
|
+
c = n
|
|
74
|
+
}
|
|
75
|
+
c.h = hfun
|
|
76
|
+
return i + 1
|
|
77
|
+
}
|
|
78
|
+
}
|
|
99
79
|
|
|
100
80
|
switch (h.kind) {
|
|
101
81
|
case 1: {
|
|
102
|
-
|
|
103
|
-
let
|
|
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)
|
|
116
|
-
}
|
|
117
|
-
}
|
|
82
|
+
// element
|
|
83
|
+
let n: Element & JNode
|
|
84
|
+
let { tag, cn = [], p } = h
|
|
118
85
|
|
|
119
|
-
|
|
86
|
+
let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()
|
|
87
|
+
if (fresh) {
|
|
120
88
|
let ns =
|
|
121
|
-
p
|
|
89
|
+
p.xmlns ??
|
|
122
90
|
(parent.tagName == 'foreignObject'
|
|
123
91
|
? 'http://www.w3.org/1999/xhtml'
|
|
124
92
|
: parent.namespaceURI)
|
|
125
93
|
n = document.createElementNS(ns, tag)
|
|
126
94
|
c ? c.replaceWith(n) : parent.appendChild(n)
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
95
|
+
} else n = c as Element & JNode
|
|
96
|
+
|
|
97
|
+
n.h = hfun ?? h // attach before update(), which may handle the update itself
|
|
98
|
+
if (jmcconfig.debug && hfun) n.setAttribute('comp', hfun.tag.name)
|
|
99
|
+
if (!fresh && p.update?.(n, uc)) return i + 1
|
|
100
|
+
|
|
101
|
+
// a const element is an island: jmc renders it once, then foreign code owns it. update() still runs
|
|
102
|
+
if (fresh || !p.const) {
|
|
103
|
+
n.ep = setprops(n, n.ep, p)
|
|
104
|
+
if (--depth && !tag.includes('-')) { // web components render their own children
|
|
105
|
+
const j = syncchildren(n, cn, 0, depth, uc)
|
|
106
|
+
removeexcesschildren(n, j)
|
|
107
|
+
}
|
|
133
108
|
}
|
|
109
|
+
if (fresh) p.mounted?.(n)
|
|
134
110
|
|
|
135
111
|
return i + 1
|
|
136
112
|
}
|
|
137
113
|
|
|
138
|
-
case 4: {
|
|
139
|
-
return syncchildren(parent, h.cn ?? [], i, --depth, uc)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
114
|
case 2: {
|
|
115
|
+
// function component
|
|
116
|
+
// TBD REMOVE the second argument once we know it is unused: the children are in h.p.children
|
|
143
117
|
let hr = h.tag(h.p, h.cn)
|
|
144
118
|
return sync(parent, i, hr, hfun ?? h, depth, uc)
|
|
145
119
|
}
|
|
146
120
|
|
|
147
121
|
case 3: {
|
|
148
|
-
if
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
//
|
|
153
|
-
|
|
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)))
|
|
158
|
-
let hv = ci.view()
|
|
159
|
-
let j = sync(parent, i, hv, h, depth, uc)
|
|
160
|
-
let n = parent.childNodes[i]!
|
|
161
|
-
ci.element = n
|
|
162
|
-
ci.mounted?.()
|
|
163
|
-
return j
|
|
122
|
+
// class component. reuses the instance attached to c, if c renders the same class
|
|
123
|
+
let old = c?.h?.tag == h.tag ? c.h as HClassComponent : undefined
|
|
124
|
+
let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))
|
|
125
|
+
if (old) {
|
|
126
|
+
ci.props = old.p = h.p // set attached p too, for a repeatable update through c.h
|
|
127
|
+
if (ci.update?.(uc)) return i + 1 // update() substitutes view/sync when returning truthy
|
|
164
128
|
}
|
|
129
|
+
let hv = ci.view()
|
|
130
|
+
let j = sync(parent, i, hv, old ?? h, depth, uc) // the attached H, so updates find the instance
|
|
131
|
+
assertnonfragment(h, hv, i, j)
|
|
132
|
+
ci.element = parent.childNodes[i]
|
|
133
|
+
if (!old) ci.mounted?.()
|
|
134
|
+
return j
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
case 4: {
|
|
138
|
+
// fragment. hfun is deliberately not passed on: a component returning a fragment is not attached
|
|
139
|
+
// to any node, so it cannot update itself, only its parent can update it
|
|
140
|
+
return syncchildren(parent, h.cn ?? [], i, --depth, uc)
|
|
165
141
|
}
|
|
166
142
|
|
|
167
143
|
default:
|
|
@@ -180,45 +156,30 @@ export function patch(
|
|
|
180
156
|
e: Node | null,
|
|
181
157
|
h: H,
|
|
182
158
|
depth = 999,
|
|
183
|
-
uc = {}
|
|
159
|
+
uc: J = {},
|
|
184
160
|
): HTMLElement | undefined {
|
|
185
161
|
if (!e) return
|
|
186
162
|
if (depth == 0) {
|
|
187
|
-
|
|
163
|
+
(e as Element).replaceChildren()
|
|
188
164
|
depth = 999
|
|
189
165
|
}
|
|
190
|
-
let p = e.parentElement
|
|
191
|
-
let i = [].indexOf
|
|
166
|
+
let p = e.parentElement!
|
|
167
|
+
let i = [...p.childNodes].indexOf(e as ChildNode)
|
|
192
168
|
sync(p, i, h, undefined, depth, uc)
|
|
193
169
|
return p.childNodes[i] as HTMLElement
|
|
194
170
|
}
|
|
195
171
|
|
|
196
|
-
export type Command = Selector | J | number
|
|
197
|
-
|
|
198
172
|
export function updateview(...cmds: Command[]): void {
|
|
199
|
-
if (!cmds.
|
|
200
|
-
let uc = {}
|
|
173
|
+
if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body
|
|
174
|
+
let uc: J = {}
|
|
201
175
|
let depth = 999
|
|
202
|
-
let patchcount = 0
|
|
203
|
-
|
|
204
|
-
function f(cmds: Command[]) {
|
|
205
|
-
for (const c of cmds) {
|
|
206
|
-
if (typeof c === 'string') {
|
|
207
|
-
f([...document.querySelectorAll(c)])
|
|
208
|
-
} else if (c instanceof Node) {
|
|
209
|
-
if (c.h) {
|
|
210
|
-
patch(c, c.h, depth, uc)
|
|
211
|
-
patchcount++
|
|
212
|
-
}
|
|
213
|
-
} else if (typeof c === 'number') {
|
|
214
|
-
depth = c
|
|
215
|
-
} else {
|
|
216
|
-
uc = c
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
176
|
|
|
221
|
-
|
|
177
|
+
// isConnected: a selector's matches are collected upfront, patching one can replace a later one
|
|
178
|
+
let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }
|
|
222
179
|
|
|
223
|
-
|
|
180
|
+
for (const c of cmds)
|
|
181
|
+
if (typeof c == 'string') document.querySelectorAll(c).forEach(up)
|
|
182
|
+
else if (c instanceof Node) up(c)
|
|
183
|
+
else if (typeof c == 'number') depth = c
|
|
184
|
+
else uc = c
|
|
224
185
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// entry point of the automatic JSX runtime in development ("jsx": "react-jsxdev", or Vite in dev mode)
|
|
2
|
+
import { jsx, jsxs } from './jsx-runtime.js'
|
|
3
|
+
|
|
4
|
+
export { Fragment } from './jsx-runtime.js'
|
|
5
|
+
export type { JSX } from './jsx-runtime.js'
|
|
6
|
+
|
|
7
|
+
/** like jsx and jsxs. the extra arguments for debugging (key, source, self) are not used */
|
|
8
|
+
export let jsxDEV = (tag: any, props: any, _key: any, isstaticchildren: boolean) => (isstaticchildren ? jsxs : jsx)(tag, props)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// entry point of the automatic JSX runtime, used with "jsx": "react-jsx" and "jsxImportSource": "@snupo/jmc".
|
|
2
|
+
// the compiler then imports jsx, jsxs and Fragment from here by itself, so .tsx files need no jsx import.
|
|
3
|
+
// it passes the children inside the props: jsx(tag, { ...props, children }). this adapts that to jmc's jsx(tag, props, ...children)
|
|
4
|
+
import { jsx as classic, jsxf } from './h.js'
|
|
5
|
+
|
|
6
|
+
export { jsxf as Fragment }
|
|
7
|
+
|
|
8
|
+
/** an element with one child or none */
|
|
9
|
+
export let jsx = (tag: any, { children, ...p }: any) => classic(tag, p, ...(children === undefined ? [] : [children]))
|
|
10
|
+
|
|
11
|
+
/** an element with several children, always an array */
|
|
12
|
+
export let jsxs = (tag: any, { children, ...p }: any) => classic(tag, p, ...children)
|
|
13
|
+
|
|
14
|
+
export namespace JSX {
|
|
15
|
+
// the same elements as in classic mode. the automatic runtime type-checks children as a prop, so every element accepts them
|
|
16
|
+
export type IntrinsicElements = { [K in keyof classic.JSX.IntrinsicElements]: classic.JSX.IntrinsicElements[K] & { children?: any } }
|
|
17
|
+
}
|
package/src/jsx.ts
CHANGED
|
@@ -650,45 +650,33 @@ interface SVGAttributes<T> extends AriaAttributes, HTMLAttributes {
|
|
|
650
650
|
|
|
651
651
|
type SVGProps<T extends SVGElement> = SVGAttributes<T>
|
|
652
652
|
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
interface IntrinsicElements extends IntrinsicElementAttributes {
|
|
659
|
-
|
|
660
|
-
"x-autosuggest": HtmlHTMLAttributes & {
|
|
661
|
-
suggestions: string[]
|
|
662
|
-
value: string
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
label: LabelHTMLAttributes & {
|
|
666
|
-
for?: string
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
svg: SVGProps<SVGSVGElement> & { class?: string | undefined, xmlns: "http://www.w3.org/2000/svg" }
|
|
670
|
-
path: SVGProps<SVGPathElement> & { class?: string | undefined }
|
|
671
|
-
textPath: SVGProps<SVGTextPathElement> & { class?: string | undefined, style?: string | undefined }
|
|
672
|
-
tspan: SVGProps<SVGTSpanElement> & { class?: string | undefined }
|
|
673
|
-
circle: SVGProps<SVGCircleElement> & { class?: string | undefined }
|
|
674
|
-
rect: SVGProps<SVGRectElement> & { class?: string | undefined }
|
|
675
|
-
line: SVGProps<SVGLineElement> & { class?: string | undefined }
|
|
676
|
-
text: SVGProps<SVGTextElement> & { class?: string | undefined, style?: string | undefined }
|
|
677
|
-
g: SVGProps<SVGGElement> & { class?: string | undefined }
|
|
678
|
-
foreignObject: SVGProps<SVGForeignObjectElement> & { class?: string | undefined }
|
|
679
|
-
}
|
|
653
|
+
/** the elements jmc knows. JSX reads them through jsx.JSX.IntrinsicElements, see h.ts */
|
|
654
|
+
export interface JmcIntrinsicElements extends IntrinsicElementAttributes {
|
|
655
|
+
label: LabelHTMLAttributes & {
|
|
656
|
+
for?: string
|
|
680
657
|
}
|
|
658
|
+
|
|
659
|
+
svg: SVGProps<SVGSVGElement> & { class?: string | undefined, xmlns: "http://www.w3.org/2000/svg" }
|
|
660
|
+
path: SVGProps<SVGPathElement> & { class?: string | undefined }
|
|
661
|
+
textPath: SVGProps<SVGTextPathElement> & { class?: string | undefined, style?: string | undefined }
|
|
662
|
+
tspan: SVGProps<SVGTSpanElement> & { class?: string | undefined }
|
|
663
|
+
circle: SVGProps<SVGCircleElement> & { class?: string | undefined }
|
|
664
|
+
rect: SVGProps<SVGRectElement> & { class?: string | undefined }
|
|
665
|
+
line: SVGProps<SVGLineElement> & { class?: string | undefined }
|
|
666
|
+
text: SVGProps<SVGTextElement> & { class?: string | undefined, style?: string | undefined }
|
|
667
|
+
g: SVGProps<SVGGElement> & { class?: string | undefined }
|
|
668
|
+
foreignObject: SVGProps<SVGForeignObjectElement> & { class?: string | undefined }
|
|
681
669
|
}
|
|
682
670
|
|
|
683
671
|
// 1. Define the Thunk transformer for a single type
|
|
684
672
|
type WithThunk<T> = T extends undefined
|
|
685
673
|
? undefined
|
|
686
|
-
: T | (() => T)
|
|
674
|
+
: T | (() => T | undefined)
|
|
687
675
|
|
|
688
676
|
// 2. Map across all keys of the existing interface
|
|
689
677
|
export type HTMLAttributes = {
|
|
690
|
-
[K in keyof HTMLAttributesBase]?: WithThunk<NonNullable<HTMLAttributesBase[K]
|
|
691
|
-
}
|
|
678
|
+
[K in keyof HTMLAttributesBase]?: WithThunk<NonNullable<HTMLAttributesBase[K]>>
|
|
679
|
+
}
|
|
692
680
|
|
|
693
681
|
export interface HTMLAttributesBase extends AdditionalElementAttributes {
|
|
694
682
|
class?: string
|
package/src/lib.ts
CHANGED
|
@@ -1,30 +1,32 @@
|
|
|
1
|
-
import type { Children, IClassComponent,
|
|
1
|
+
import type { Children, IClassComponent, HU, J, HFragment } from './h.js'
|
|
2
2
|
import { updateview } from './jmc.js'
|
|
3
3
|
|
|
4
|
+
export type UC = J // update context
|
|
5
|
+
export type FComponentT<P> = (pcn: P, cn?: Children) => HU // a function component with typed props
|
|
6
|
+
|
|
4
7
|
export type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K]; }
|
|
5
8
|
|
|
6
9
|
export const When = ({ cond }: { cond: boolean }, cn: Children): HFragment | null => cond ? { kind: 4, cn } : null
|
|
7
10
|
|
|
8
|
-
export abstract class
|
|
11
|
+
export abstract class Component<P extends J = {}> implements IClassComponent {
|
|
9
12
|
|
|
10
|
-
element!: HTMLElement // assigned by
|
|
13
|
+
element!: HTMLElement // assigned by jmc. a placeholder Comment while view() renders nothing
|
|
11
14
|
constructor(public props: P) { }
|
|
12
15
|
|
|
13
16
|
// overrides
|
|
14
17
|
mounted() { }
|
|
15
18
|
update(uc?: any): boolean | void { }
|
|
16
|
-
abstract view():
|
|
19
|
+
abstract view(): HU
|
|
17
20
|
|
|
18
21
|
// utility: updates the component's view
|
|
19
22
|
updateview() { updateview(this.element) }
|
|
20
|
-
get ismounted(): boolean { return this.element
|
|
23
|
+
get ismounted(): boolean { return !!this.element }
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
export function cc(...namesOrObjects: (string | J | undefined)[]): string {
|
|
24
27
|
return namesOrObjects
|
|
25
|
-
.filter(
|
|
26
|
-
.
|
|
27
|
-
.join(' ')
|
|
28
|
+
.flatMap(n => typeof n == 'string' ? n : Object.keys(n ?? {}).filter(k => n?.[k])) // objects contribute their truthy keys
|
|
29
|
+
.join(' ')
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
export const focuselement = (e: Node) => (e as HTMLElement).focus()
|
package/src/props.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { J } from './h.js'
|
|
2
|
+
|
|
3
|
+
/** settings to tweak jmc */
|
|
4
|
+
export const jmcconfig = {
|
|
5
|
+
/** marks component roots with a comp="Name" attribute */
|
|
6
|
+
debug: false,
|
|
7
|
+
/** prop names always set as attributes, never as properties */
|
|
8
|
+
attributes: new Set(['selected', 'viewBox']),
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const isproperty = (e: Element, name: string, value: any) =>
|
|
12
|
+
!jmcconfig.attributes.has(name) &&
|
|
13
|
+
(value instanceof Object || ( // objects and functions
|
|
14
|
+
e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
|
|
15
|
+
name in e &&
|
|
16
|
+
name !== 'style'))
|
|
17
|
+
|
|
18
|
+
let removeprop = (e: Element & J, p: string, old: any) => {
|
|
19
|
+
if (isproperty(e, p, old)) e[p] = typeof old === 'boolean' ? false : ''
|
|
20
|
+
if (!(old instanceof Object)) e.removeAttribute(p) // reflected properties (title, id, ...) leave an empty attribute otherwise
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** applies now over old and returns the applied values, with thunks evaluated, to serve as old on the next call */
|
|
24
|
+
export let setprops = (e: Element & J, old: J = {}, now: J): J => {
|
|
25
|
+
// evaluate thunks into a copy: now is h.p, which gets rendered again and must keep its thunks
|
|
26
|
+
now = Object.fromEntries(Object.entries(now).map(([k, v]) => [k, v?.thunk ? v() : v]))
|
|
27
|
+
|
|
28
|
+
// removals
|
|
29
|
+
for (let p in old)
|
|
30
|
+
if (!(p in now)) removeprop(e, p, old[p])
|
|
31
|
+
|
|
32
|
+
// updates
|
|
33
|
+
for (let p in now) {
|
|
34
|
+
let v = now[p]
|
|
35
|
+
if (v !== old[p] && !(v == null && old[p] == null)) { // strict: 0 vs '' or 1 vs true are changes. null vs undefined is not
|
|
36
|
+
if (v == null) removeprop(e, p, old[p])
|
|
37
|
+
else if (isproperty(e, p, v)) e[p] = v
|
|
38
|
+
else if (v === false && !p.startsWith('aria-')) e.removeAttribute(p) // aria-*="false" is meaningful
|
|
39
|
+
else e.setAttribute(p, v)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return now
|
|
43
|
+
}
|