jmx-runtime 0.0.27 → 0.0.30
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/base.ts +27 -25
- package/dist/base.d.ts +6 -5
- package/dist/base.d.ts.map +1 -1
- package/dist/base.ts +27 -0
- package/dist/h.d.ts.map +1 -1
- package/dist/h.ts +89 -0
- package/dist/index.js +182 -6
- package/dist/index.js.map +1 -1
- package/dist/index.ts +5 -0
- package/dist/jmx.d.ts.map +1 -1
- package/dist/jmx.ts +186 -0
- package/dist/jsx.ts +748 -0
- package/dist/lib.ts +29 -0
- package/h.ts +89 -88
- package/index.ts +5 -5
- package/jmx.ts +186 -183
- package/jsx.ts +748 -748
- package/lib.ts +28 -28
- package/package.json +14 -14
- package/rollup.config.js +30 -30
- package/tsconfig.json +37 -37
- package/dist/base.js +0 -22
- package/dist/base.js.map +0 -1
- package/dist/config.d.ts +0 -9
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -5
- package/dist/config.js.map +0 -1
- package/dist/h.js +0 -7
- package/dist/h.js.map +0 -1
- package/dist/jmx.js +0 -135
- package/dist/jmx.js.map +0 -1
- package/dist/jsx.js +0 -2
- package/dist/jsx.js.map +0 -1
- package/dist/lib.js +0 -17
- package/dist/lib.js.map +0 -1
package/dist/lib.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Props, IClassComponent, H, FComponentT, Children } from 'h'
|
|
2
|
+
import { updateview } from './jmx'
|
|
3
|
+
|
|
4
|
+
export type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K]; }
|
|
5
|
+
|
|
6
|
+
export const When = ({ cond }: { cond: boolean }, cn: Children) => cond ? { cn } : void 0;
|
|
7
|
+
|
|
8
|
+
export abstract class JMXComp<P extends Props = {}> implements IClassComponent {
|
|
9
|
+
|
|
10
|
+
// assigned by jmx framework
|
|
11
|
+
element!: HTMLElement
|
|
12
|
+
|
|
13
|
+
// we provide this ctor for jsx which uses ctor arguments as properties of class components.
|
|
14
|
+
// at runtime, we pass the props directly
|
|
15
|
+
constructor(public props: P) { }
|
|
16
|
+
|
|
17
|
+
// overrides
|
|
18
|
+
mounted() { }
|
|
19
|
+
update(uc?: IUpdateContext): boolean | void { }
|
|
20
|
+
abstract view(): H
|
|
21
|
+
|
|
22
|
+
// utility: updates the component's view
|
|
23
|
+
updateview() { updateview(this.element) }
|
|
24
|
+
get ismounted():boolean { return this.element as any }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function cc(...namesOrObjects: (string | any)[]): string {
|
|
28
|
+
return namesOrObjects.flatMap(n => (n.trim ? n : Object.keys(n).filter(k => n[k]))).join(' ') // n.trim distinguishes strings from objects
|
|
29
|
+
}
|
package/h.ts
CHANGED
|
@@ -1,88 +1,89 @@
|
|
|
1
|
-
// jmx internal types - hopst
|
|
2
|
-
// the following types describe the js expression we get from tsx after conversion be our jmx plugin
|
|
3
|
-
// they can be useful for users as well, components might return them.
|
|
4
|
-
|
|
5
|
-
type Func<T> = () => T
|
|
6
|
-
export type Expr<T> = T | Func<T>
|
|
7
|
-
|
|
8
|
-
export type Props = Record<string, any>
|
|
9
|
-
|
|
10
|
-
export type FComponent = (props: Props | undefined, children?: ChildrenH) => HElement // show an example for usage of children
|
|
11
|
-
|
|
12
|
-
export type FComponentT<P> = (pcn: P, cn?: Children) => H | void
|
|
13
|
-
|
|
14
|
-
export interface IClassComponent {
|
|
15
|
-
element: Node
|
|
16
|
-
props?: Record<string, any>
|
|
17
|
-
view(): H
|
|
18
|
-
update(uc: IUpdateContext): boolean | void
|
|
19
|
-
mounted?(): void
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
export type
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
|
32
|
-
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
|
64
|
-
|
|
|
65
|
-
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
1
|
+
// jmx internal types - hopst
|
|
2
|
+
// the following types describe the js expression we get from tsx after conversion be our jmx plugin
|
|
3
|
+
// they can be useful for users as well, components might return them.
|
|
4
|
+
|
|
5
|
+
type Func<T> = () => T
|
|
6
|
+
export type Expr<T> = T | Func<T>
|
|
7
|
+
|
|
8
|
+
export type Props = Record<string, any>
|
|
9
|
+
|
|
10
|
+
export type FComponent = (props: Props | undefined, children?: ChildrenH) => HElement // show an example for usage of children
|
|
11
|
+
|
|
12
|
+
export type FComponentT<P> = (pcn: P, cn?: Children) => H | void
|
|
13
|
+
|
|
14
|
+
export interface IClassComponent {
|
|
15
|
+
element: Node
|
|
16
|
+
props?: Record<string, any>
|
|
17
|
+
view(): H
|
|
18
|
+
update(uc: IUpdateContext): boolean | void
|
|
19
|
+
mounted?(): void
|
|
20
|
+
//new(props: any): IClassComponent
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface CComponent {
|
|
24
|
+
new (props: any): IClassComponent // while a real component expresses its interface via props pass to the ctor, internally we assign props after construction with new()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type ChildrenH = (H | undefined)[]
|
|
28
|
+
export type Children = Expr<ChildrenH>
|
|
29
|
+
|
|
30
|
+
type HText =
|
|
31
|
+
| string // text node
|
|
32
|
+
| number // text node
|
|
33
|
+
| boolean // do not allow boolean, that
|
|
34
|
+
|
|
35
|
+
export type HFragment = {
|
|
36
|
+
cn: Children
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type HElement = {
|
|
40
|
+
tag: string
|
|
41
|
+
p?: Expr<Props>
|
|
42
|
+
cn: Children
|
|
43
|
+
i?: any
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type HCompFun = {
|
|
47
|
+
tag: FComponent
|
|
48
|
+
p?: Expr<Props>
|
|
49
|
+
cn?: Children
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type HCompClass = {
|
|
53
|
+
tag: CComponent
|
|
54
|
+
p?: Expr<Props>
|
|
55
|
+
cn: Children
|
|
56
|
+
i: IClassComponent
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type HComp = HCompFun | HCompClass
|
|
60
|
+
|
|
61
|
+
export type H = // a hyperscript atom that describes a ...
|
|
62
|
+
|
|
63
|
+
| HText
|
|
64
|
+
| HElement // a tag, like p, div with attributes and children
|
|
65
|
+
| HComp // a dynamic component computing any other HNode
|
|
66
|
+
| HFragment
|
|
67
|
+
|
|
68
|
+
declare global {
|
|
69
|
+
interface Node {
|
|
70
|
+
h?: HElement | HCompFun | HCompClass
|
|
71
|
+
}
|
|
72
|
+
export interface IUpdateContext {
|
|
73
|
+
patchElementOnly?: boolean
|
|
74
|
+
replace?: boolean
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface Window {
|
|
78
|
+
jmx?: {
|
|
79
|
+
getnamespace: (tag: string) => string | undefined
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function jsx(): HElement {
|
|
85
|
+
throw 'jmx plugin not configured'
|
|
86
|
+
} // dumy function for app code - jmx-plugin removes calls to this function, minifyer then removes it
|
|
87
|
+
export function jsxf(): HElement {
|
|
88
|
+
throw 'jmx plugin not configured'
|
|
89
|
+
} // dumy function for app code - jmx-plugin removes calls to this function, minifyer then removes it
|
package/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './jmx';
|
|
2
|
-
export * from './lib';
|
|
3
|
-
export * from './base';
|
|
4
|
-
export * from './h';
|
|
5
|
-
export * from './jsx';
|
|
1
|
+
export * from './jmx';
|
|
2
|
+
export * from './lib';
|
|
3
|
+
export * from './base';
|
|
4
|
+
export * from './h';
|
|
5
|
+
export * from './jsx';
|
package/jmx.ts
CHANGED
|
@@ -1,183 +1,186 @@
|
|
|
1
|
-
import { rebind } from './base'
|
|
2
|
-
import { Expr, FComponent, H, HComp, HCompClass, HElement, HFragment, IClassComponent, Props } from './h'
|
|
3
|
-
|
|
4
|
-
// config
|
|
5
|
-
export function createElement(tag: string) {
|
|
6
|
-
let ns = window.jmx?.getnamespace?.(tag)
|
|
7
|
-
return ns ? document.createElementNS(ns, tag) : document.createElement(tag)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const enum NodeType { // vaporizes (but for that must be in this file, otherwise not)
|
|
11
|
-
TextNode = 3,
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
let evaluate = <T>(expr: Expr<T>): T => (expr instanceof Function ? expr() : expr)
|
|
15
|
-
let removeexcesschildren = (n: Element, i: number) => {
|
|
16
|
-
let c: ChildNode
|
|
17
|
-
while ((c = n.childNodes[i])) {
|
|
18
|
-
c.remove()
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
let iswebcomponent = (h: HElement) => (h.tag as string).includes('-')
|
|
22
|
-
let isclasscomponent = (h: HComp): h is HCompClass => (h.tag as any)?.prototype?.view
|
|
23
|
-
let iselement = (h: any): h is HElement => typeof h.tag == 'string'
|
|
24
|
-
let isfragment = (h: any): h is HFragment => {
|
|
25
|
-
return h.tag == undefined && h.cn != undefined
|
|
26
|
-
}
|
|
27
|
-
let isobject = (o: any): o is object => typeof o === 'object'
|
|
28
|
-
|
|
29
|
-
let isproperty = (name: string, value: any) =>
|
|
30
|
-
['value', 'checked', 'disabled', 'className', 'style', 'href', 'src', 'selected', 'readOnly', 'tabIndex'].includes(
|
|
31
|
-
name
|
|
32
|
-
) ||
|
|
33
|
-
value instanceof Object ||
|
|
34
|
-
value instanceof Function
|
|
35
|
-
|
|
36
|
-
let setprops = (e: Element, newprops: Props = {}) => {
|
|
37
|
-
let oldprops = evaluate(e.h?.p) ?? {}
|
|
38
|
-
for (let p in oldprops)
|
|
39
|
-
!(p in newprops) && isproperty(p, oldprops[p]) ? ((e as any)[p] = null) : e.removeAttribute(p)
|
|
40
|
-
for (let p in newprops) isproperty(p, newprops[p]) ? ((e as any)[p] = newprops[p]) : e.setAttribute(p, newprops[p])
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** syncs at position i of p. returns the number of the element past the last added element.
|
|
44
|
-
* if no element was added (eg when h=null) then it returns i
|
|
45
|
-
* if a fragment with 5 nodes was added, it returns i + 5
|
|
46
|
-
* when a single element or component is added, it is i+1 since they always create exactly 1 node
|
|
47
|
-
*/
|
|
48
|
-
function sync(p: Element, i: number, h: Expr<H | undefined>): number {
|
|
49
|
-
// console.log('%csync', "background:orange", p.tagName, i, h)
|
|
50
|
-
|
|
51
|
-
h = evaluate(h)
|
|
52
|
-
if (h === null || h === undefined) return i // skip this element. not that !!h would forbid to render the number 0 or the boolean value false
|
|
53
|
-
|
|
54
|
-
let c = p.childNodes[i] // is often null, eg during fresh creation
|
|
55
|
-
|
|
56
|
-
function synctextnode(text: string) {
|
|
57
|
-
if (c && c.nodeType == NodeType.TextNode) {
|
|
58
|
-
if (c.textContent != text) c.textContent = text // firefox updates even equal text, loosing an existing text selection
|
|
59
|
-
} else {
|
|
60
|
-
let tn = document.createTextNode(text)
|
|
61
|
-
c ? c.replaceWith(tn) : p.appendChild(tn)
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (isobject(h)) {
|
|
66
|
-
// element nodes
|
|
67
|
-
|
|
68
|
-
/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */
|
|
69
|
-
function syncchildren(p: Element, h: HElement | HComp | HFragment, i: number): number {
|
|
70
|
-
evaluate(h.cn)
|
|
71
|
-
?.flat()
|
|
72
|
-
.forEach(hc => (i = sync(p, i, hc)))
|
|
73
|
-
return i
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (isfragment(h)) return syncchildren(p, h, i)
|
|
77
|
-
|
|
78
|
-
const props = evaluate(h.p)
|
|
79
|
-
|
|
80
|
-
if (iselement(h)) {
|
|
81
|
-
let n: Element
|
|
82
|
-
|
|
83
|
-
if ((<Element>c)?.tagName?.toLowerCase() != h.tag.toLowerCase()) {
|
|
84
|
-
n = createElement(h.tag)
|
|
85
|
-
c ? c.replaceWith(n) : p.appendChild(n)
|
|
86
|
-
setprops(n, props)
|
|
87
|
-
props?.mounted?.(n)
|
|
88
|
-
} else {
|
|
89
|
-
n = c as Element
|
|
90
|
-
setprops(n, props)
|
|
91
|
-
if (props?.update?.(c, globaluc)) return i + 1
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// if only components shall be updateable (advantage: close variables inside component functions are always fresh materialized, avoids surprises), comment this out
|
|
95
|
-
n.h = h
|
|
96
|
-
|
|
97
|
-
if (!globaluc.patchElementOnly && !iswebcomponent(h as HElement)) {
|
|
98
|
-
// tbd: make "island" attribute
|
|
99
|
-
const j = syncchildren(n, h, 0)
|
|
100
|
-
removeexcesschildren(n, j)
|
|
101
|
-
}
|
|
102
|
-
return i + 1
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
switch (typeof h.tag) {
|
|
106
|
-
case 'function':
|
|
107
|
-
let isupdate = c?.h?.tag == h.tag
|
|
108
|
-
|
|
109
|
-
let ci: IClassComponent | undefined
|
|
110
|
-
|
|
111
|
-
if (isclasscomponent(h)) {
|
|
112
|
-
h.i = ci = (c
|
|
113
|
-
ci.props = props
|
|
114
|
-
|
|
115
|
-
// if component instance returns truthy for update(), then
|
|
116
|
-
if (isupdate && ci.update(globaluc)) return i + 1
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// materialize the component
|
|
120
|
-
// we run compoents view() and fun code often, we do not compare properties to avoid their computation
|
|
121
|
-
// this means that the inner hr (h resolved) is run often
|
|
122
|
-
//
|
|
123
|
-
// if ci has a view, return ci.view() even if it is falsy, this is perfectly valid
|
|
124
|
-
let hr = ci?.view ? ci?.view() : (h.tag as FComponent)(props, evaluate(h.cn))
|
|
125
|
-
|
|
126
|
-
// a component can return undefined or null if it has no elements to show
|
|
127
|
-
if (hr === undefined || hr == null) return i
|
|
128
|
-
|
|
129
|
-
let j = sync(p, i, hr)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
1
|
+
import { rebind } from './base'
|
|
2
|
+
import { Expr, FComponent, H, HComp, HCompClass, HElement, HFragment, IClassComponent, Props } from './h'
|
|
3
|
+
|
|
4
|
+
// config
|
|
5
|
+
export function createElement(tag: string) {
|
|
6
|
+
let ns = window.jmx?.getnamespace?.(tag)
|
|
7
|
+
return ns ? document.createElementNS(ns, tag) : document.createElement(tag)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const enum NodeType { // vaporizes (but for that must be in this file, otherwise not)
|
|
11
|
+
TextNode = 3,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let evaluate = <T>(expr: Expr<T>): T => (expr instanceof Function ? expr() : expr)
|
|
15
|
+
let removeexcesschildren = (n: Element, i: number) => {
|
|
16
|
+
let c: ChildNode
|
|
17
|
+
while ((c = n.childNodes[i])) {
|
|
18
|
+
c.remove()
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
let iswebcomponent = (h: HElement) => (h.tag as string).includes('-')
|
|
22
|
+
let isclasscomponent = (h: HComp): h is HCompClass => (h.tag as any)?.prototype?.view
|
|
23
|
+
let iselement = (h: any): h is HElement => typeof h.tag == 'string'
|
|
24
|
+
let isfragment = (h: any): h is HFragment => {
|
|
25
|
+
return h.tag == undefined && h.cn != undefined
|
|
26
|
+
}
|
|
27
|
+
let isobject = (o: any): o is object => typeof o === 'object'
|
|
28
|
+
|
|
29
|
+
let isproperty = (name: string, value: any) =>
|
|
30
|
+
['value', 'checked', 'disabled', 'className', 'style', 'href', 'src', 'selected', 'readOnly', 'tabIndex'].includes(
|
|
31
|
+
name
|
|
32
|
+
) ||
|
|
33
|
+
value instanceof Object ||
|
|
34
|
+
value instanceof Function
|
|
35
|
+
|
|
36
|
+
let setprops = (e: Element, newprops: Props = {}) => {
|
|
37
|
+
let oldprops = evaluate(e.h?.p) ?? {}
|
|
38
|
+
for (let p in oldprops)
|
|
39
|
+
!(p in newprops) && isproperty(p, oldprops[p]) ? ((e as any)[p] = null) : e.removeAttribute(p)
|
|
40
|
+
for (let p in newprops) isproperty(p, newprops[p]) ? ((e as any)[p] = newprops[p]) : e.setAttribute(p, newprops[p])
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** syncs at position i of p. returns the number of the element past the last added element.
|
|
44
|
+
* if no element was added (eg when h=null) then it returns i
|
|
45
|
+
* if a fragment with 5 nodes was added, it returns i + 5
|
|
46
|
+
* when a single element or component is added, it is i+1 since they always create exactly 1 node
|
|
47
|
+
*/
|
|
48
|
+
function sync(p: Element, i: number, h: Expr<H | undefined>): number {
|
|
49
|
+
// console.log('%csync', "background:orange", p.tagName, i, h)
|
|
50
|
+
|
|
51
|
+
h = evaluate(h)
|
|
52
|
+
if (h === null || h === undefined) return i // skip this element. not that !!h would forbid to render the number 0 or the boolean value false
|
|
53
|
+
|
|
54
|
+
let c = p.childNodes[i] // is often null, eg during fresh creation
|
|
55
|
+
|
|
56
|
+
function synctextnode(text: string) {
|
|
57
|
+
if (c && c.nodeType == NodeType.TextNode) {
|
|
58
|
+
if (c.textContent != text) c.textContent = text // firefox updates even equal text, loosing an existing text selection
|
|
59
|
+
} else {
|
|
60
|
+
let tn = document.createTextNode(text)
|
|
61
|
+
c ? c.replaceWith(tn) : p.appendChild(tn)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (isobject(h)) {
|
|
66
|
+
// element nodes
|
|
67
|
+
|
|
68
|
+
/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */
|
|
69
|
+
function syncchildren(p: Element, h: HElement | HComp | HFragment, i: number): number {
|
|
70
|
+
evaluate(h.cn)
|
|
71
|
+
?.flat()
|
|
72
|
+
.forEach(hc => (i = sync(p, i, hc)))
|
|
73
|
+
return i
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (isfragment(h)) return syncchildren(p, h, i)
|
|
77
|
+
|
|
78
|
+
const props = evaluate(h.p)
|
|
79
|
+
|
|
80
|
+
if (iselement(h)) {
|
|
81
|
+
let n: Element
|
|
82
|
+
|
|
83
|
+
if ((<Element>c)?.tagName?.toLowerCase() != h.tag.toLowerCase()) {
|
|
84
|
+
n = createElement(h.tag)
|
|
85
|
+
c ? c.replaceWith(n) : p.appendChild(n)
|
|
86
|
+
setprops(n, props)
|
|
87
|
+
props?.mounted?.(n)
|
|
88
|
+
} else {
|
|
89
|
+
n = c as Element
|
|
90
|
+
setprops(n, props)
|
|
91
|
+
if (props?.update?.(c, globaluc)) return i + 1
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// if only components shall be updateable (advantage: close variables inside component functions are always fresh materialized, avoids surprises), comment this out
|
|
95
|
+
n.h = h
|
|
96
|
+
|
|
97
|
+
if (!globaluc.patchElementOnly && !iswebcomponent(h as HElement)) {
|
|
98
|
+
// tbd: make "island" attribute
|
|
99
|
+
const j = syncchildren(n, h, 0)
|
|
100
|
+
removeexcesschildren(n, j)
|
|
101
|
+
}
|
|
102
|
+
return i + 1
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
switch (typeof h.tag) {
|
|
106
|
+
case 'function':
|
|
107
|
+
let isupdate = c?.h?.tag == h.tag
|
|
108
|
+
|
|
109
|
+
let ci: IClassComponent | undefined
|
|
110
|
+
|
|
111
|
+
if (isclasscomponent(h)) {
|
|
112
|
+
h.i = ci = isupdate ? (c.h as HCompClass)?.i : rebind(new h.tag(props))
|
|
113
|
+
ci.props = props
|
|
114
|
+
|
|
115
|
+
// if component instance returns truthy for update(), then this update substitutes syncing
|
|
116
|
+
if (isupdate && ci.update(globaluc)) return i + 1
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// materialize the component
|
|
120
|
+
// we run compoents view() and fun code often, we do not compare properties to avoid their computation
|
|
121
|
+
// this means that the inner hr (h resolved) is run often
|
|
122
|
+
//
|
|
123
|
+
// if ci has a view, return ci.view() even if it is falsy, this is perfectly valid
|
|
124
|
+
let hr = ci?.view ? ci?.view() : (h.tag as FComponent)(props, evaluate(h.cn))
|
|
125
|
+
|
|
126
|
+
// a component can return undefined or null if it has no elements to show
|
|
127
|
+
if (hr === undefined || hr == null) return i
|
|
128
|
+
|
|
129
|
+
let j = sync(p, i, hr)
|
|
130
|
+
|
|
131
|
+
// now the dom element exists
|
|
132
|
+
|
|
133
|
+
let cn = p.childNodes[i]!
|
|
134
|
+
cn.h = h // attach h onto the materialized component node
|
|
135
|
+
// ;(cn as HTMLElement).setAttribute?.('comp', '')
|
|
136
|
+
|
|
137
|
+
// class component life cycle calls
|
|
138
|
+
if (ci) ci.element = cn
|
|
139
|
+
if (!isupdate) ci?.mounted?.()
|
|
140
|
+
|
|
141
|
+
return j
|
|
142
|
+
|
|
143
|
+
case 'object':
|
|
144
|
+
return sync(p, i, h.tag) // tbd: type of h is not correct, h.tag == never
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// text nodes
|
|
148
|
+
synctextnode(h as string)
|
|
149
|
+
return i + 1
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let globaluc: IUpdateContext = {}
|
|
153
|
+
|
|
154
|
+
export function patch(e: Node | null, h: Expr<H>) {
|
|
155
|
+
if (!e) return
|
|
156
|
+
if (globaluc.replace) (e as HTMLElement).replaceChildren()
|
|
157
|
+
const p = e.parentElement as HTMLElement
|
|
158
|
+
const i = [].indexOf.call<any, any, any>(p.childNodes, e)
|
|
159
|
+
// always called deferred, because removing elements can trigger events and their handlers (like blur)
|
|
160
|
+
sync(p, i, h)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Overload signatures
|
|
164
|
+
type Selector = string | Node | undefined | null
|
|
165
|
+
type Selectors = Selector[]
|
|
166
|
+
|
|
167
|
+
export function updateviewuc(uc: IUpdateContext, ...sels: Selectors): void {
|
|
168
|
+
{
|
|
169
|
+
globaluc = uc
|
|
170
|
+
updateviewinternal(...sels)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
export function updateview(...sels: Selectors): void {
|
|
174
|
+
{
|
|
175
|
+
globaluc = {}
|
|
176
|
+
updateviewinternal(...sels)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function updateviewinternal(...sels: Selector[]): void {
|
|
181
|
+
if (!sels.length) sels.push('body')
|
|
182
|
+
sels.flatMap(s => (typeof s == 'string' ? [...document.querySelectorAll(s)] : s ? [s] : [])).forEach(e => {
|
|
183
|
+
if (!e?.h) throw 'jmx: no h exists on the node'
|
|
184
|
+
patch(e, e.h)
|
|
185
|
+
})
|
|
186
|
+
}
|