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/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
- interface CComponent {
23
- 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()
24
- }
25
-
26
- export type ChildrenH = (H | undefined)[]
27
- export type Children = Expr<ChildrenH>
28
-
29
- type HText =
30
- | string // text node
31
- | number // text node
32
- | boolean // do not allow boolean, that
33
-
34
- export type HFragment = {
35
- cn: Children
36
- }
37
-
38
- export type HElement = {
39
- tag: string
40
- p?: Expr<Props>
41
- cn: Children
42
- i?: any
43
- }
44
-
45
- type HCompFun = {
46
- tag: FComponent
47
- p?: Expr<Props>
48
- cn?: Children
49
- }
50
-
51
- export type HCompClass = {
52
- tag: CComponent
53
- p?: Expr<Props>
54
- cn: Children
55
- i: IClassComponent
56
- }
57
-
58
- export type HComp = HCompFun | HCompClass
59
-
60
- export type H = // a hyperscript atom that describes a ...
61
-
62
- | HText
63
- | HElement // a tag, like p, div with attributes and children
64
- | HComp // a dynamic component computing any other HNode
65
- | HFragment
66
-
67
- declare global {
68
- interface Node {
69
- h?: HElement | HCompFun | HCompClass
70
- }
71
- export interface IUpdateContext {
72
- patchElementOnly?: boolean
73
- replace?: boolean
74
- }
75
-
76
- export interface Window {
77
- jmx?: {
78
- getnamespace: (tag: string) => string | undefined
79
- }
80
- }
81
- }
82
-
83
- export function jsx(): HElement {
84
- throw 'jmx plugin not configured'
85
- } // dumy function for app code - jmx-plugin removes calls to this function, minifyer then removes it
86
- export function jsxf(): HElement {
87
- throw 'jmx plugin not configured'
88
- } // dumy function for app code - jmx-plugin removes calls to this function, minifyer then removes it
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?.h as HCompClass)?.i ?? rebind(new h.tag(props))
113
- ci.props = props
114
-
115
- // if component instance returns truthy for update(), then syncing is susbstituted by the component
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
- let cn = p.childNodes[i]!
132
- cn.h = h // attach h onto the materialized component node
133
- // ;(cn as HTMLElement).setAttribute?.('comp', '')
134
-
135
- if (ci) ci.element = cn
136
- if (!isupdate) ci?.mounted?.()
137
-
138
- return j
139
-
140
- case 'object':
141
- return sync(p, i, h.tag) // tbd: type of h is not correct, h.tag == never
142
- }
143
- }
144
- // text nodes
145
- synctextnode(h as string)
146
- return i + 1
147
- }
148
-
149
- let globaluc: IUpdateContext = {}
150
-
151
- export function patch(e: Node | null, h: Expr<H>) {
152
- if (!e) return
153
- if (globaluc.replace) (e as HTMLElement).replaceChildren()
154
- const p = e.parentElement as HTMLElement
155
- const 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(p, i, h)
158
- }
159
-
160
- // Overload signatures
161
- type Selector = string | Node | undefined | null
162
- type Selectors = Selector[]
163
-
164
- export function updateviewuc(uc: IUpdateContext, ...sels: Selectors): void {
165
- {
166
- globaluc = uc
167
- updateviewinternal(...sels)
168
- }
169
- }
170
- export function updateview(...sels: Selectors): void {
171
- {
172
- globaluc = {}
173
- updateviewinternal(...sels)
174
- }
175
- }
176
-
177
- function updateviewinternal(...sels: Selector[]): void {
178
- if (!sels.length) sels.push('body')
179
- sels.flatMap(s => (typeof s == 'string' ? [...document.querySelectorAll(s)] : s ? [s] : [])).forEach(e => {
180
- if (!e?.h) throw 'jmx: no h exists on the node'
181
- patch(e, e.h)
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
+ }