@snupo/jmc 1.0.62 → 1.2.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/design-guide.md +193 -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.js +1 -0
- package/dist/events.js.map +1 -1
- 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 -12
- package/dist/jmc.d.ts.map +1 -1
- package/dist/jmc.js +80 -119
- 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 +31 -4
- package/readme.md +313 -34
- package/src/base.ts +11 -15
- package/src/events.ts +1 -1
- package/src/h.ts +29 -18
- package/src/index.ts +2 -1
- package/src/jmc.ts +90 -138
- 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/jmc.ts
CHANGED
|
@@ -1,72 +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'
|
|
11
|
-
import {
|
|
12
|
+
import { jmcconfig, setprops } from './props.js'
|
|
12
13
|
|
|
13
14
|
const enum NodeType {
|
|
14
15
|
// vaporizes (but for that must be in this file, otherwise not)
|
|
15
16
|
TextNode = 3,
|
|
17
|
+
Comment = 8,
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
let iswebcomponent = (h: HElement) => h.tag.includes('-')
|
|
25
|
-
|
|
26
|
-
let attributeNames = new Set([
|
|
27
|
-
'selected',
|
|
28
|
-
'viewBox',
|
|
29
|
-
'list'
|
|
30
|
-
// other HTML attributes where your framework wants attribute semantics
|
|
31
|
-
])
|
|
32
|
-
|
|
33
|
-
export let jmcconfig = {
|
|
34
|
-
attributeNames
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const isproperty = (e: Element, name: string, value: any) =>
|
|
38
|
-
!attributeNames.has(name) &&
|
|
39
|
-
(value instanceof Object ||
|
|
40
|
-
value instanceof Function || (
|
|
41
|
-
e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
|
|
42
|
-
name in e &&
|
|
43
|
-
name !== 'style'))
|
|
44
|
-
|
|
45
|
-
let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
|
|
46
|
-
old ??= {}
|
|
47
|
-
now ??= {}
|
|
48
|
-
|
|
49
|
-
// removals
|
|
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
|
-
}
|
|
57
|
-
|
|
58
|
-
// updates
|
|
59
|
-
for (let p in now) {
|
|
60
|
-
let v = now[p]
|
|
61
|
-
v = v?.thunk ? v() : v
|
|
62
|
-
|
|
63
|
-
if (p.startsWith('on')) setevent(e, p.slice(2), v)
|
|
64
|
-
else if (v != old[p]) {
|
|
65
|
-
if (isproperty(e, p, v)) e[p] = v
|
|
66
|
-
else if ((v === false || v == null) && !p.startsWith('aria-')) e.removeAttribute(p)
|
|
67
|
-
else e.setAttribute(p, v)
|
|
68
|
-
}
|
|
69
|
-
}
|
|
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
|
|
70
26
|
}
|
|
71
27
|
|
|
72
28
|
let removeexcesschildren = (n: Element, i: number) => {
|
|
@@ -74,11 +30,11 @@ let removeexcesschildren = (n: Element, i: number) => {
|
|
|
74
30
|
while ((c = n.childNodes[i])) {
|
|
75
31
|
try {
|
|
76
32
|
c.remove()
|
|
77
|
-
} catch
|
|
33
|
+
} catch { } // due to blur-event-handlers, this can fail
|
|
78
34
|
}
|
|
79
35
|
}
|
|
80
36
|
|
|
81
|
-
/** synchronizes children starting at the i-th
|
|
37
|
+
/** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
|
|
82
38
|
let syncchildren = (
|
|
83
39
|
p: Element,
|
|
84
40
|
cn: Children,
|
|
@@ -86,91 +42,102 @@ let syncchildren = (
|
|
|
86
42
|
depth: number,
|
|
87
43
|
uc: J,
|
|
88
44
|
): number => {
|
|
89
|
-
cn.flat(999)
|
|
90
|
-
.filter((hc) => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
|
|
91
|
-
.forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
|
|
45
|
+
cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
|
|
92
46
|
return i
|
|
93
47
|
}
|
|
94
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
|
+
|
|
95
54
|
function sync(
|
|
96
55
|
parent: Element,
|
|
97
56
|
i: number,
|
|
98
57
|
h: HU,
|
|
99
|
-
hfun:
|
|
58
|
+
hfun: HComp | undefined,
|
|
100
59
|
depth: number,
|
|
101
60
|
uc: J,
|
|
102
61
|
): number {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
+
}
|
|
108
79
|
|
|
109
80
|
switch (h.kind) {
|
|
110
81
|
case 1: {
|
|
111
|
-
|
|
112
|
-
let
|
|
113
|
-
cn
|
|
114
|
-
|
|
115
|
-
let process = () => {
|
|
116
|
-
setprops(n, n.h?.p, p)
|
|
117
|
-
n.h = hfun ?? h // sets the new property values
|
|
118
|
-
if (
|
|
119
|
-
--depth &&
|
|
120
|
-
!n.hasAttribute('const') &&
|
|
121
|
-
!iswebcomponent(h as HElement)
|
|
122
|
-
) {
|
|
123
|
-
const j = syncchildren(n, cn, 0, depth, uc)
|
|
124
|
-
removeexcesschildren(n, j)
|
|
125
|
-
}
|
|
126
|
-
}
|
|
82
|
+
// element
|
|
83
|
+
let n: Element & JNode
|
|
84
|
+
let { tag, cn = [], p } = h
|
|
127
85
|
|
|
128
|
-
|
|
86
|
+
let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()
|
|
87
|
+
if (fresh) {
|
|
129
88
|
let ns =
|
|
130
|
-
p
|
|
89
|
+
p.xmlns ??
|
|
131
90
|
(parent.tagName == 'foreignObject'
|
|
132
91
|
? 'http://www.w3.org/1999/xhtml'
|
|
133
92
|
: parent.namespaceURI)
|
|
134
93
|
n = document.createElementNS(ns, tag)
|
|
135
94
|
c ? c.replaceWith(n) : parent.appendChild(n)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
+
}
|
|
142
108
|
}
|
|
109
|
+
if (fresh) p.mounted?.(n)
|
|
143
110
|
|
|
144
111
|
return i + 1
|
|
145
112
|
}
|
|
146
113
|
|
|
147
|
-
case 4: {
|
|
148
|
-
return syncchildren(parent, h.cn ?? [], i, --depth, uc)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
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
|
|
152
117
|
let hr = h.tag(h.p, h.cn)
|
|
153
118
|
return sync(parent, i, hr, hfun ?? h, depth, uc)
|
|
154
119
|
}
|
|
155
120
|
|
|
156
121
|
case 3: {
|
|
157
|
-
if
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
? i + 1
|
|
164
|
-
: sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached
|
|
165
|
-
} else {
|
|
166
|
-
let ci = (h.i = rebind(new h.tag(h.p)))
|
|
167
|
-
let hv = ci.view()
|
|
168
|
-
let j = sync(parent, i, hv, h, depth, uc)
|
|
169
|
-
let n = parent.childNodes[i]!
|
|
170
|
-
ci.element = n
|
|
171
|
-
ci.mounted?.()
|
|
172
|
-
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
|
|
173
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)
|
|
174
141
|
}
|
|
175
142
|
|
|
176
143
|
default:
|
|
@@ -189,45 +156,30 @@ export function patch(
|
|
|
189
156
|
e: Node | null,
|
|
190
157
|
h: H,
|
|
191
158
|
depth = 999,
|
|
192
|
-
uc = {}
|
|
159
|
+
uc: J = {},
|
|
193
160
|
): HTMLElement | undefined {
|
|
194
161
|
if (!e) return
|
|
195
162
|
if (depth == 0) {
|
|
196
|
-
|
|
163
|
+
(e as Element).replaceChildren()
|
|
197
164
|
depth = 999
|
|
198
165
|
}
|
|
199
|
-
let p = e.parentElement
|
|
200
|
-
let i = [].indexOf
|
|
166
|
+
let p = e.parentElement!
|
|
167
|
+
let i = [...p.childNodes].indexOf(e as ChildNode)
|
|
201
168
|
sync(p, i, h, undefined, depth, uc)
|
|
202
169
|
return p.childNodes[i] as HTMLElement
|
|
203
170
|
}
|
|
204
171
|
|
|
205
|
-
export type Command = Selector | J | number
|
|
206
|
-
|
|
207
172
|
export function updateview(...cmds: Command[]): void {
|
|
208
|
-
if (!cmds.
|
|
209
|
-
let uc = {}
|
|
173
|
+
if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body
|
|
174
|
+
let uc: J = {}
|
|
210
175
|
let depth = 999
|
|
211
|
-
let patchcount = 0
|
|
212
|
-
|
|
213
|
-
function f(cmds: Command[]) {
|
|
214
|
-
for (const c of cmds) {
|
|
215
|
-
if (typeof c === 'string') {
|
|
216
|
-
f([...document.querySelectorAll(c)])
|
|
217
|
-
} else if (c instanceof Node) {
|
|
218
|
-
if (c.h) {
|
|
219
|
-
patch(c, c.h, depth, uc)
|
|
220
|
-
patchcount++
|
|
221
|
-
}
|
|
222
|
-
} else if (typeof c === 'number') {
|
|
223
|
-
depth = c
|
|
224
|
-
} else {
|
|
225
|
-
uc = c
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
176
|
|
|
230
|
-
|
|
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) }
|
|
231
179
|
|
|
232
|
-
|
|
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
|
|
233
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
|
+
}
|
package/src/update.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { updateview } from './jmc.js'
|
|
2
|
+
import type { Command, J, Selector } from './h.js'
|
|
3
|
+
|
|
4
|
+
// one queued update: a target with the depth and update context of the call that queued it
|
|
5
|
+
type Pending = [target: Selector, depth: number, uc: J | undefined]
|
|
6
|
+
|
|
7
|
+
let pendings: Pending[] = []
|
|
8
|
+
let isScheduled = false
|
|
9
|
+
|
|
10
|
+
/** resolves a call's commands into pendings, so depth and uc stay with their own call */
|
|
11
|
+
let enqueue = (cmds: Command[]) => {
|
|
12
|
+
if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds = [...cmds, 'body'] // like updateview()
|
|
13
|
+
let depth = 999
|
|
14
|
+
let uc: J | undefined
|
|
15
|
+
for (const c of cmds)
|
|
16
|
+
if (typeof c == 'number') depth = c
|
|
17
|
+
else if (typeof c == 'string' || c instanceof Node) pendings.push([c, depth, uc])
|
|
18
|
+
else uc = c
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** synchronously runs all queued updates. selectors are resolved now, and each node is updated once per depth and uc */
|
|
22
|
+
export const flush = (): void => {
|
|
23
|
+
isScheduled = false
|
|
24
|
+
const ps = pendings
|
|
25
|
+
pendings = []
|
|
26
|
+
const done: Pending[] = []
|
|
27
|
+
for (const [target, depth, uc] of ps)
|
|
28
|
+
for (const n of typeof target == 'string' ? document.querySelectorAll(target) : [target])
|
|
29
|
+
if (!done.some(([t, d, u]) => t == n && d == depth && u == uc)) {
|
|
30
|
+
done.push([n, depth, uc])
|
|
31
|
+
updateview(depth, uc ?? {}, n)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** queues the update and schedules a flush, so that all calls before it collapse into one batch */
|
|
36
|
+
let batch = (schedule: (f: () => void) => void) => (...cmds: Command[]): void => {
|
|
37
|
+
enqueue(cmds)
|
|
38
|
+
if (!isScheduled) {
|
|
39
|
+
isScheduled = true
|
|
40
|
+
schedule(flush)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** batches updates until the next microtask */
|
|
45
|
+
export const updateviewmicro = batch(f => queueMicrotask(f))
|
|
46
|
+
|
|
47
|
+
/** batches updates until the next animation frame */
|
|
48
|
+
export const updateviewaf = batch(f => requestAnimationFrame(f)) // looked up on call, so importing works without a DOM
|
|
49
|
+
|
|
50
|
+
/** updates immediately, together with all queued updates */
|
|
51
|
+
export const updateviewsync = (...cmds: Command[]): void => {
|
|
52
|
+
enqueue(cmds)
|
|
53
|
+
flush()
|
|
54
|
+
}
|
package/dist/updateview.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Command } from "./jmc";
|
|
2
|
-
/**
|
|
3
|
-
* Synchronously processes all pending commands queued in pendingCommands.
|
|
4
|
-
*/
|
|
5
|
-
export declare const flush: () => void;
|
|
6
|
-
/**
|
|
7
|
-
* Asynchronously batches view updates.
|
|
8
|
-
* Multiple synchronous calls collapse into a single execution during the microtask queue tick.
|
|
9
|
-
*/
|
|
10
|
-
export declare const updateviewmicro: (...args: Command[]) => void;
|
|
11
|
-
export declare const updateviewaf: (...args: Command[]) => void;
|
|
12
|
-
/**
|
|
13
|
-
* Synchronously updates the view immediately.
|
|
14
|
-
* Pushes new commands onto the queue and flushes all pending commands in a single batch.
|
|
15
|
-
*/
|
|
16
|
-
export declare const updateviewsync: (...args: Command[]) => void;
|
|
17
|
-
//# sourceMappingURL=updateview.d.ts.map
|
package/dist/updateview.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"updateview.d.ts","sourceRoot":"","sources":["../src/updateview.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAc,MAAM,OAAO,CAAA;AAK3C;;GAEG;AACH,eAAO,MAAM,KAAK,QAAO,IAUxB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,GAAG,MAAM,OAAO,EAAE,KAAG,IAMpD,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,GAAG,MAAM,OAAO,EAAE,KAAG,IAMjD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,GAAG,MAAM,OAAO,EAAE,KAAG,IAGnD,CAAA"}
|
package/dist/updateview.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { updateview } from "./jmc";
|
|
2
|
-
let pendings = [];
|
|
3
|
-
let isScheduled = false;
|
|
4
|
-
/**
|
|
5
|
-
* Synchronously processes all pending commands queued in pendingCommands.
|
|
6
|
-
*/
|
|
7
|
-
export const flush = () => {
|
|
8
|
-
if (pendings.length === 0)
|
|
9
|
-
return;
|
|
10
|
-
// Clear scheduling state and extract queued commands
|
|
11
|
-
isScheduled = false;
|
|
12
|
-
const cmds = [...new Set(pendings)]; // this is guaranteed stable, keeping order
|
|
13
|
-
pendings = [];
|
|
14
|
-
// Execute the single, unified update
|
|
15
|
-
updateview(...cmds);
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Asynchronously batches view updates.
|
|
19
|
-
* Multiple synchronous calls collapse into a single execution during the microtask queue tick.
|
|
20
|
-
*/
|
|
21
|
-
export const updateviewmicro = (...args) => {
|
|
22
|
-
pendings.push(...args);
|
|
23
|
-
if (!isScheduled) {
|
|
24
|
-
isScheduled = true;
|
|
25
|
-
queueMicrotask(flush);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
export const updateviewaf = (...args) => {
|
|
29
|
-
pendings.push(...args);
|
|
30
|
-
if (!isScheduled) {
|
|
31
|
-
isScheduled = true;
|
|
32
|
-
requestAnimationFrame(flush);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Synchronously updates the view immediately.
|
|
37
|
-
* Pushes new commands onto the queue and flushes all pending commands in a single batch.
|
|
38
|
-
*/
|
|
39
|
-
export const updateviewsync = (...args) => {
|
|
40
|
-
pendings.push(...args);
|
|
41
|
-
flush();
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=updateview.js.map
|
package/dist/updateview.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"updateview.js","sourceRoot":"","sources":["../src/updateview.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,UAAU,EAAE,MAAM,OAAO,CAAA;AAE3C,IAAI,QAAQ,GAAc,EAAE,CAAA;AAC5B,IAAI,WAAW,GAAG,KAAK,CAAA;AAEvB;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,GAAS,EAAE;IAC5B,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,OAAM;IAEjC,qDAAqD;IACrD,WAAW,GAAG,KAAK,CAAA;IACnB,MAAM,IAAI,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAA,CAAC,2CAA2C;IAC/E,QAAQ,GAAG,EAAE,CAAA;IAEb,qCAAqC;IACrC,UAAU,CAAC,GAAG,IAAI,CAAC,CAAA;AACvB,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,GAAG,IAAe,EAAQ,EAAE;IACxD,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAA;IACtB,IAAI,CAAC,WAAW,EAAE,CAAC;QACf,WAAW,GAAG,IAAI,CAAA;QAClB,cAAc,CAAC,KAAK,CAAC,CAAA;IACzB,CAAC;AACL,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,GAAG,IAAe,EAAQ,EAAE;IACrD,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAA;IACtB,IAAI,CAAC,WAAW,EAAE,CAAC;QACf,WAAW,GAAG,IAAI,CAAA;QAClB,qBAAqB,CAAC,KAAK,CAAC,CAAA;IAChC,CAAC;AACL,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,GAAG,IAAe,EAAQ,EAAE;IACvD,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAA;IACtB,KAAK,EAAE,CAAA;AACX,CAAC,CAAA","sourcesContent":["import { Command, updateview } from \"./jmc\"\r\n\r\nlet pendings: Command[] = []\r\nlet isScheduled = false\r\n\r\n/**\r\n * Synchronously processes all pending commands queued in pendingCommands.\r\n */\r\nexport const flush = (): void => {\r\n if (pendings.length === 0) return\r\n\r\n // Clear scheduling state and extract queued commands\r\n isScheduled = false\r\n const cmds = [...new Set(pendings)] // this is guaranteed stable, keeping order\r\n pendings = []\r\n\r\n // Execute the single, unified update\r\n updateview(...cmds)\r\n}\r\n\r\n/**\r\n * Asynchronously batches view updates.\r\n * Multiple synchronous calls collapse into a single execution during the microtask queue tick.\r\n */\r\nexport const updateviewmicro = (...args: Command[]): void => {\r\n pendings.push(...args)\r\n if (!isScheduled) {\r\n isScheduled = true\r\n queueMicrotask(flush)\r\n }\r\n}\r\n\r\nexport const updateviewaf = (...args: Command[]): void => {\r\n pendings.push(...args)\r\n if (!isScheduled) {\r\n isScheduled = true\r\n requestAnimationFrame(flush)\r\n }\r\n}\r\n\r\n/**\r\n * Synchronously updates the view immediately.\r\n * Pushes new commands onto the queue and flushes all pending commands in a single batch.\r\n */\r\nexport const updateviewsync = (...args: Command[]): void => {\r\n pendings.push(...args)\r\n flush()\r\n}\r\n"]}
|
package/src/updateview.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { Command, updateview } from "./jmc"
|
|
2
|
-
|
|
3
|
-
let pendings: Command[] = []
|
|
4
|
-
let isScheduled = false
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Synchronously processes all pending commands queued in pendingCommands.
|
|
8
|
-
*/
|
|
9
|
-
export const flush = (): void => {
|
|
10
|
-
if (pendings.length === 0) return
|
|
11
|
-
|
|
12
|
-
// Clear scheduling state and extract queued commands
|
|
13
|
-
isScheduled = false
|
|
14
|
-
const cmds = [...new Set(pendings)] // this is guaranteed stable, keeping order
|
|
15
|
-
pendings = []
|
|
16
|
-
|
|
17
|
-
// Execute the single, unified update
|
|
18
|
-
updateview(...cmds)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Asynchronously batches view updates.
|
|
23
|
-
* Multiple synchronous calls collapse into a single execution during the microtask queue tick.
|
|
24
|
-
*/
|
|
25
|
-
export const updateviewmicro = (...args: Command[]): void => {
|
|
26
|
-
pendings.push(...args)
|
|
27
|
-
if (!isScheduled) {
|
|
28
|
-
isScheduled = true
|
|
29
|
-
queueMicrotask(flush)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const updateviewaf = (...args: Command[]): void => {
|
|
34
|
-
pendings.push(...args)
|
|
35
|
-
if (!isScheduled) {
|
|
36
|
-
isScheduled = true
|
|
37
|
-
requestAnimationFrame(flush)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Synchronously updates the view immediately.
|
|
43
|
-
* Pushes new commands onto the queue and flushes all pending commands in a single batch.
|
|
44
|
-
*/
|
|
45
|
-
export const updateviewsync = (...args: Command[]): void => {
|
|
46
|
-
pendings.push(...args)
|
|
47
|
-
flush()
|
|
48
|
-
}
|