@snupo/jmc 1.4.1 → 1.5.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/dist/h.d.ts CHANGED
@@ -3,7 +3,8 @@ export type J = Record<string, any>;
3
3
  export type Selector = string | Node;
4
4
  /** an updateview command: a target (selector or node), a depth (number) or an update context (object) */
5
5
  export type Command = Selector | J | number;
6
- export type FComponent = (props: J, children?: Children) => HU;
6
+ /** a function component. it gets its children in props.children */
7
+ export type FComponent = (props: J) => HU;
7
8
  type HKindElement = 1;
8
9
  type HKindFunctionComponent = 2;
9
10
  type HKindClassComponent = 3;
package/dist/h.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"h.d.ts","sourceRoot":"","sources":["../src/h.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAA;AAEpD,MAAM,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAEnC,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,IAAI,CAAA;AAEpC,yGAAyG;AACzG,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,CAAC,GAAG,MAAM,CAAA;AAI3C,MAAM,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,QAAQ,KAAK,EAAE,CAAA;AAE9D,KAAK,YAAY,GAAG,CAAC,CAAA;AACrB,KAAK,sBAAsB,GAAG,CAAC,CAAA;AAC/B,KAAK,mBAAmB,GAAG,CAAC,CAAA;AAC5B,KAAK,aAAa,GAAG,CAAC,CAAA;AAEtB,MAAM,MAAM,KAAK,GAAG,YAAY,GAAG,sBAAsB,GAAG,mBAAmB,GAAG,aAAa,CAAA;AAE/F,MAAM,WAAW,eAAe;IAC5B,OAAO,EAAE,IAAI,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,C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UD,4CAA4C;AAC5C,MAAM,CAAC,IAAI,IAAI,GAAG,MAAM,EAAE,CAAA","sourcesContent":["import type { JmcIntrinsicElements } from './jsx.js'\r\n\r\nexport type J = Record<string, any> // a json record\r\n\r\nexport type Selector = string | Node\r\n\r\n/** an updateview command: a target (selector or node), a depth (number) or an update context (object) */\r\nexport type Command = Selector | J | number\r\n\r\n// TBD REMOVE the children parameter once we know it is unused: components get their children in props.children.\r\n// the second parameter remains for function components written before props.children existed\r\nexport type FComponent = (props: J, children?: Children) => HU\r\n\r\ntype HKindElement = 1\r\ntype HKindFunctionComponent = 2\r\ntype HKindClassComponent = 3\r\ntype HKindFragment = 4\r\n\r\nexport type HKind = HKindElement | HKindFunctionComponent | HKindClassComponent | HKindFragment\r\n\r\nexport interface IClassComponent {\r\n element: Node\r\n props?: Record<string, any>\r\n view(): HU\r\n update(uc: any): boolean | void\r\n mounted?(): void\r\n}\r\n\r\nexport interface CComponent {\r\n new(props: any): IClassComponent\r\n}\r\n\r\nexport type Children = HU[]\r\n\r\nexport type HText =\r\n | string // text node\r\n | number // text node\r\n | boolean // renders nothing, like null and undefined\r\n\r\nexport type HFragment = {\r\n kind: HKindFragment\r\n cn?: Children\r\n}\r\n\r\nexport type HElement = {\r\n kind: HKindElement\r\n tag: string\r\n p: J\r\n cn?: Children\r\n}\r\n\r\nexport type HFunctionComponent = {\r\n kind: HKindFunctionComponent\r\n tag: FComponent\r\n p: J\r\n cn?: Children\r\n}\r\n\r\nexport type HClassComponent = {\r\n kind: HKindClassComponent\r\n tag: CComponent\r\n p: J\r\n cn?: Children\r\n i?: IClassComponent\r\n}\r\n\r\nexport type HComp = HFunctionComponent | HClassComponent\r\n\r\nexport type H =\r\n | HText & { kind?: undefined } // text nodes never have a kind. but this type addition makes H a full DU\r\n | HElement\r\n | HFunctionComponent\r\n | HClassComponent\r\n | HFragment\r\n\r\nexport type HU = H | undefined | null\r\n\r\nlet isclass = (f: Function) => /^class\\s|^class\\{/.test(f.toString())\r\n\r\nexport function jsx(tag: any, p: any, ...cn: any[]): HElement | HComp | HFragment {\r\n if (tag?.kind) return tag // already an H node, flatten tree\r\n\r\n let kind: HKind = tag == jsxf ? 4 : tag instanceof Function ? (isclass(tag) ? 3 : 2) : 1\r\n\r\n let h: any = { kind, p: p ?? {} }\r\n if (kind != 4) h.tag = tag\r\n if (cn?.length) {\r\n h.cn = cn\r\n // components get their children in props.children, as with the automatic runtime (jsx-runtime.ts).\r\n // elements do not: setprops would write them to the element's read-only children property\r\n if (kind == 2 || kind == 3) h.p = { ...h.p, children: cn }\r\n }\r\n return h\r\n}\r\n\r\n// typescript looks up the JSX types on the factory function first: jsx.JSX instead of a global JSX namespace.\r\n// so jmc's element types cannot clash with other libraries. add your own elements by augmenting jsx.JSX.IntrinsicElements\r\nexport namespace jsx {\r\n export namespace JSX {\r\n export interface IntrinsicElements extends JmcIntrinsicElements { }\r\n }\r\n}\r\n\r\n// fragment is only used above as a sentinel\r\nexport let jsxf = Symbol()"]}
1
+ {"version":3,"file":"h.js","sourceRoot":"","sources":["../src/h.ts"],"names":[],"mappings":"AA4EA,IAAI,OAAO,GAAG,CAAC,CAAW,EAAE,EAAE,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAA;AAErE,MAAM,UAAU,GAAG,CAAC,GAAQ,EAAE,CAAM,EAAE,GAAG,EAAS;IAC9C,IAAI,GAAG,EAAE,IAAI;QAAE,OAAO,GAAG,CAAA,CAAC,kCAAkC;IAE5D,IAAI,IAAI,GAAU,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,YAAY,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAExF,IAAI,CAAC,GAAQ,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,IAAI,EAAE,EAAE,CAAA;IACjC,IAAI,IAAI,IAAI,CAAC;QAAE,CAAC,CAAC,GAAG,GAAG,GAAG,CAAA;IAC1B,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC;QACb,CAAC,CAAC,EAAE,GAAG,EAAE,CAAA;QACT,mGAAmG;QACnG,0FAA0F;QAC1F,IAAI,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC;YAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAA;IAC9D,CAAC;IACD,OAAO,CAAC,CAAA;AACZ,CAAC;AAUD,4CAA4C;AAC5C,MAAM,CAAC,IAAI,IAAI,GAAG,MAAM,EAAE,CAAA","sourcesContent":["import type { JmcIntrinsicElements } from './jsx.js'\r\n\r\nexport type J = Record<string, any> // a json record\r\n\r\nexport type Selector = string | Node\r\n\r\n/** an updateview command: a target (selector or node), a depth (number) or an update context (object) */\r\nexport type Command = Selector | J | number\r\n\r\n/** a function component. it gets its children in props.children */\r\nexport type FComponent = (props: J) => HU\r\n\r\ntype HKindElement = 1\r\ntype HKindFunctionComponent = 2\r\ntype HKindClassComponent = 3\r\ntype HKindFragment = 4\r\n\r\nexport type HKind = HKindElement | HKindFunctionComponent | HKindClassComponent | HKindFragment\r\n\r\nexport interface IClassComponent {\r\n element: Node\r\n props?: Record<string, any>\r\n view(): HU\r\n update(uc: any): boolean | void\r\n mounted?(): void\r\n}\r\n\r\nexport interface CComponent {\r\n new(props: any): IClassComponent\r\n}\r\n\r\nexport type Children = HU[]\r\n\r\nexport type HText =\r\n | string // text node\r\n | number // text node\r\n | boolean // renders nothing, like null and undefined\r\n\r\nexport type HFragment = {\r\n kind: HKindFragment\r\n cn?: Children\r\n}\r\n\r\nexport type HElement = {\r\n kind: HKindElement\r\n tag: string\r\n p: J\r\n cn?: Children\r\n}\r\n\r\nexport type HFunctionComponent = {\r\n kind: HKindFunctionComponent\r\n tag: FComponent\r\n p: J\r\n cn?: Children\r\n}\r\n\r\nexport type HClassComponent = {\r\n kind: HKindClassComponent\r\n tag: CComponent\r\n p: J\r\n cn?: Children\r\n i?: IClassComponent\r\n}\r\n\r\nexport type HComp = HFunctionComponent | HClassComponent\r\n\r\nexport type H =\r\n | HText & { kind?: undefined } // text nodes never have a kind. but this type addition makes H a full DU\r\n | HElement\r\n | HFunctionComponent\r\n | HClassComponent\r\n | HFragment\r\n\r\nexport type HU = H | undefined | null\r\n\r\nlet isclass = (f: Function) => /^class\\s|^class\\{/.test(f.toString())\r\n\r\nexport function jsx(tag: any, p: any, ...cn: any[]): HElement | HComp | HFragment {\r\n if (tag?.kind) return tag // already an H node, flatten tree\r\n\r\n let kind: HKind = tag == jsxf ? 4 : tag instanceof Function ? (isclass(tag) ? 3 : 2) : 1\r\n\r\n let h: any = { kind, p: p ?? {} }\r\n if (kind != 4) h.tag = tag\r\n if (cn?.length) {\r\n h.cn = cn\r\n // components get their children in props.children, as with the automatic runtime (jsx-runtime.ts).\r\n // elements do not: setprops would write them to the element's read-only children property\r\n if (kind == 2 || kind == 3) h.p = { ...h.p, children: cn }\r\n }\r\n return h\r\n}\r\n\r\n// typescript looks up the JSX types on the factory function first: jsx.JSX instead of a global JSX namespace.\r\n// so jmc's element types cannot clash with other libraries. add your own elements by augmenting jsx.JSX.IntrinsicElements\r\nexport namespace jsx {\r\n export namespace JSX {\r\n export interface IntrinsicElements extends JmcIntrinsicElements { }\r\n }\r\n}\r\n\r\n// fragment is only used above as a sentinel\r\nexport let jsxf = Symbol()"]}
package/dist/jmc.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,CAAC,EAGD,CAAC,EACD,OAAO,EAIV,MAAM,QAAQ,CAAA;AAoJf,wBAAgB,KAAK,CACjB,CAAC,EAAE,IAAI,GAAG,IAAI,EACd,CAAC,EAAE,CAAC,EACJ,KAAK,SAAM,EACX,EAAE,GAAE,CAAM,GACX,WAAW,GAAG,SAAS,CAgBzB;AAED,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAanD"}
1
+ {"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,CAAC,EAGD,CAAC,EACD,OAAO,EAIV,MAAM,QAAQ,CAAA;AAiKf,wBAAgB,KAAK,CACjB,CAAC,EAAE,IAAI,GAAG,IAAI,EACd,CAAC,EAAE,CAAC,EACJ,KAAK,SAAM,EACX,EAAE,GAAE,CAAM,GACX,WAAW,GAAG,SAAS,CAgBzB;AAED,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAanD"}
package/dist/jmc.js CHANGED
@@ -11,9 +11,14 @@ let removeexcesschildren = (n, i) => {
11
11
  catch { } // due to blur-event-handlers, this can fail
12
12
  }
13
13
  };
14
+ /** attaches node n, rendered from h by the components hs (outermost first), to them */
15
+ let attach = (n, h, hs) => {
16
+ n.h = hs[0] ?? h;
17
+ n.cs = hs.filter(x => x.kind == 3);
18
+ };
14
19
  /** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
15
20
  let syncchildren = (p, cn, i, depth, uc) => {
16
- cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)));
21
+ cn.flat(999).forEach((hc) => (i = sync(p, i, hc, [], depth, uc)));
17
22
  return i;
18
23
  };
19
24
  /** class components must render exactly one root node, they are attached to it and update through it */
@@ -21,12 +26,13 @@ let assertnonfragment = (h, hv, i, j) => {
21
26
  if (j - i != 1 || hv?.kind == 4)
22
27
  throw new Error(`${h.tag.name}.view() must render exactly one root element`);
23
28
  };
24
- function sync(parent, i, h, hfun, depth, uc) {
29
+ function sync(parent, i, h, hs, // the components rendering h, outermost first
30
+ depth, uc) {
25
31
  let c = parent.childNodes[i];
26
32
  // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.
27
33
  // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state
28
34
  if (h == null || typeof h == 'boolean') {
29
- if (hfun?.kind != 3)
35
+ if (!hs.some(x => x.kind == 3))
30
36
  h = ''; // an empty text node, invisible
31
37
  else {
32
38
  // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again
@@ -35,7 +41,7 @@ function sync(parent, i, h, hfun, depth, uc) {
35
41
  c ? c.replaceWith(n) : parent.append(n);
36
42
  c = n;
37
43
  }
38
- c.h = hfun;
44
+ attach(c, hs[0], hs);
39
45
  return i + 1;
40
46
  }
41
47
  }
@@ -55,9 +61,9 @@ function sync(parent, i, h, hfun, depth, uc) {
55
61
  }
56
62
  else
57
63
  n = c;
58
- n.h = hfun ?? h; // attach before update(), which may handle the update itself
59
- if (jmcconfig.debug && hfun)
60
- n.setAttribute('comp', hfun.tag.name);
64
+ attach(n, h, hs); // before update(), which may handle the update itself
65
+ if (jmcconfig.debug && hs[0])
66
+ n.setAttribute('comp', hs[0].tag.name);
61
67
  if (!fresh && p.update?.(n, uc))
62
68
  return i + 1;
63
69
  // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs
@@ -74,21 +80,26 @@ function sync(parent, i, h, hfun, depth, uc) {
74
80
  }
75
81
  case 2: {
76
82
  // function component
77
- // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children
78
- let hr = h.tag(h.p, h.cn);
79
- return sync(parent, i, hr, hfun ?? h, depth, uc);
83
+ let hr = h.tag(h.p);
84
+ return sync(parent, i, hr, [...hs, h], depth, uc);
80
85
  }
81
86
  case 3: {
82
- // class component. reuses the instance attached to c, if c renders the same class
83
- let old = c?.h?.tag == h.tag ? c.h : undefined;
87
+ // class component. reuses the instance attached to c, if c renders the same class at the same position
88
+ let outer = hs.filter(x => x.kind == 3);
89
+ let old = c?.cs?.[outer.length];
90
+ if (old?.tag != h.tag)
91
+ old = undefined;
84
92
  let ci = old ? old.i : (h.i = rebind(new h.tag(h.p)));
85
93
  if (old) {
86
- ci.props = old.p = h.p; // set attached p too, for a repeatable update through c.h
87
- if (ci.update?.(uc))
88
- return i + 1; // update() substitutes view/sync when returning truthy
94
+ ci.props = old.p = h.p; // set attached p too, for a repeatable update through the node
95
+ if (ci.update?.(uc)) { // update() substitutes view/sync when returning truthy
96
+ c.h = hs[0] ?? old; // the components below old stay attached
97
+ c.cs = [...outer, ...c.cs.slice(outer.length)];
98
+ return i + 1;
99
+ }
89
100
  }
90
101
  let hv = ci.view();
91
- let j = sync(parent, i, hv, old ?? h, depth, uc); // the attached H, so updates find the instance
102
+ let j = sync(parent, i, hv, [...hs, old ?? h], depth, uc); // the attached H, so updates find the instance
92
103
  assertnonfragment(h, hv, i, j);
93
104
  ci.element = parent.childNodes[i];
94
105
  if (!old && ci.mounted)
@@ -96,7 +107,7 @@ function sync(parent, i, h, hfun, depth, uc) {
96
107
  return j;
97
108
  }
98
109
  case 4: {
99
- // fragment. hfun is deliberately not passed on: a component returning a fragment is not attached
110
+ // fragment. hs is deliberately not passed on: a component returning a fragment is not attached
100
111
  // to any node, so it cannot update itself, only its parent can update it
101
112
  return syncchildren(parent, h.cn ?? [], i, --depth, uc);
102
113
  }
@@ -122,7 +133,7 @@ export function patch(e, h, depth = 999, uc = {}) {
122
133
  }
123
134
  let p = e.parentElement;
124
135
  let i = [...p.childNodes].indexOf(e);
125
- sync(p, i, h, undefined, depth, uc);
136
+ sync(p, i, h, [], depth, uc);
126
137
  // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew
127
138
  let ms = mounts;
128
139
  mounts = [];
package/dist/jmc.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;A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,CAAA;IAEpB,OAAO,CAAC,CAAC,UAAU,CAAC,CAAC,CAAgB,CAAA;AACzC,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,GAAG,IAAe;IACzC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,QAAQ,IAAI,CAAC,YAAY,IAAI,CAAC;QAAE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA,CAAC,wBAAwB;IAC1G,IAAI,EAAE,GAAM,EAAE,CAAA;IACd,IAAI,KAAK,GAAG,GAAG,CAAA;IAEf,gGAAgG;IAChG,IAAI,EAAE,GAAG,CAAC,CAAc,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW;QAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAA,CAAC,CAAC,CAAA;IAEnF,KAAK,MAAM,CAAC,IAAI,IAAI;QAChB,IAAI,OAAO,CAAC,IAAI,QAAQ;YAAE,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;aAC7D,IAAI,CAAC,YAAY,IAAI;YAAE,EAAE,CAAC,CAAC,CAAC,CAAA;aAC5B,IAAI,OAAO,CAAC,IAAI,QAAQ;YAAE,KAAK,GAAG,CAAC,CAAA;;YACnC,EAAE,GAAG,CAAC,CAAA;AACnB,CAAC","sourcesContent":["import type {\r\n H,\r\n HElement,\r\n HComp,\r\n J,\r\n Command,\r\n Children,\r\n HClassComponent,\r\n HU,\r\n} from './h.js'\r\nimport { rebind } from './base.js'\r\nimport { jmcconfig, setprops } from './props.js'\r\n\r\nconst enum NodeType {\r\n // vaporizes (but for that must be in this file, otherwise not)\r\n TextNode = 3,\r\n Comment = 8,\r\n}\r\n\r\n/** a node as jmc sees it. kept module-local, so consumers' Node type stays untouched */\r\ntype JNode<N extends Node = ChildNode> = N & {\r\n /** the H this node updates from: the element's own H, or the outermost component rendering it */\r\n h?: HElement | HComp\r\n /** props last applied to this element. kept apart from h, which may be the owning component's H */\r\n ep?: J\r\n}\r\n\r\n/** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */\r\nlet mounts: (() => void)[] = []\r\n\r\nlet removeexcesschildren = (n: Element, i: number) => {\r\n let c: ChildNode\r\n while ((c = n.childNodes[i])) {\r\n try {\r\n c.remove()\r\n } catch { } // due to blur-event-handlers, this can fail\r\n }\r\n}\r\n\r\n/** synchronizes children starting at the i-th node. returns the index after the last child synchronized */\r\nlet syncchildren = (\r\n p: Element,\r\n cn: Children,\r\n i: number,\r\n depth: number,\r\n uc: J,\r\n): number => {\r\n cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))\r\n return i\r\n}\r\n\r\n/** class components must render exactly one root node, they are attached to it and update through it */\r\nlet assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {\r\n if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)\r\n}\r\n\r\nfunction sync(\r\n parent: Element,\r\n i: number,\r\n h: HU,\r\n hfun: HComp | undefined,\r\n depth: number,\r\n uc: J,\r\n): number {\r\n let c: JNode | undefined = parent.childNodes[i]\r\n\r\n // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.\r\n // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state\r\n if (h == null || typeof h == 'boolean') {\r\n if (hfun?.kind != 3) h = '' // an empty text node, invisible\r\n else {\r\n // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again\r\n if (c?.nodeType != NodeType.Comment) {\r\n let n = new Comment()\r\n c ? c.replaceWith(n) : parent.append(n)\r\n c = n\r\n }\r\n c.h = hfun\r\n return i + 1\r\n }\r\n }\r\n\r\n switch (h.kind) {\r\n case 1: {\r\n // element\r\n let n: Element & JNode\r\n let { tag, cn = [], p } = h\r\n\r\n let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()\r\n if (fresh) {\r\n let ns =\r\n p.xmlns ??\r\n (parent.tagName == 'foreignObject'\r\n ? 'http://www.w3.org/1999/xhtml'\r\n : parent.namespaceURI)\r\n n = document.createElementNS(ns, tag)\r\n c ? c.replaceWith(n) : parent.appendChild(n)\r\n } else n = c as Element & JNode\r\n\r\n n.h = hfun ?? h // attach before update(), which may handle the update itself\r\n if (jmcconfig.debug && hfun) n.setAttribute('comp', hfun.tag.name)\r\n if (!fresh && p.update?.(n, uc)) return i + 1\r\n\r\n // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs\r\n if (fresh || !p.const) {\r\n n.ep = setprops(n, n.ep, p)\r\n if (--depth && !tag.includes('-')) { // web components render their own children\r\n const j = syncchildren(n, cn, 0, depth, uc)\r\n removeexcesschildren(n, j)\r\n }\r\n }\r\n if (fresh && p.mounted) mounts.push(() => p.mounted(n))\r\n\r\n return i + 1\r\n }\r\n\r\n case 2: {\r\n // function component\r\n // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children\r\n let hr = h.tag(h.p, h.cn)\r\n return sync(parent, i, hr, hfun ?? h, depth, uc)\r\n }\r\n\r\n case 3: {\r\n // class component. reuses the instance attached to c, if c renders the same class\r\n let old = c?.h?.tag == h.tag ? c.h as HClassComponent : undefined\r\n let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))\r\n if (old) {\r\n ci.props = old.p = h.p // set attached p too, for a repeatable update through c.h\r\n if (ci.update?.(uc)) return i + 1 // update() substitutes view/sync when returning truthy\r\n }\r\n let hv = ci.view()\r\n let j = sync(parent, i, hv, old ?? h, depth, uc) // the attached H, so updates find the instance\r\n assertnonfragment(h, hv, i, j)\r\n ci.element = parent.childNodes[i]\r\n if (!old && ci.mounted) mounts.push(() => ci.mounted!())\r\n return j\r\n }\r\n\r\n case 4: {\r\n // fragment. hfun is deliberately not passed on: a component returning a fragment is not attached\r\n // to any node, so it cannot update itself, only its parent can update it\r\n return syncchildren(parent, h.cn ?? [], i, --depth, uc)\r\n }\r\n\r\n default:\r\n // textnode\r\n let s = h.toString()\r\n if (c?.nodeType == NodeType.TextNode) {\r\n if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection\r\n } else {\r\n c ? c.replaceWith(s) : parent.append(s)\r\n }\r\n return i + 1\r\n }\r\n}\r\n\r\nexport function patch(\r\n e: Node | null,\r\n h: H,\r\n depth = 999,\r\n uc: J = {},\r\n): HTMLElement | undefined {\r\n if (!e) return\r\n if (depth == 0) {\r\n (e as Element).replaceChildren()\r\n depth = 999\r\n }\r\n let p = e.parentElement!\r\n let i = [...p.childNodes].indexOf(e as ChildNode)\r\n sync(p, i, h, undefined, depth, uc)\r\n\r\n // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew\r\n let ms = mounts\r\n mounts = []\r\n ms.forEach(m => m())\r\n\r\n return p.childNodes[i] as HTMLElement\r\n}\r\n\r\nexport function updateview(...cmds: Command[]): void {\r\n if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body\r\n let uc: J = {}\r\n let depth = 999\r\n\r\n // isConnected: a selector's matches are collected upfront, patching one can replace a later one\r\n let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }\r\n\r\n for (const c of cmds)\r\n if (typeof c == 'string') document.querySelectorAll(c).forEach(up)\r\n else if (c instanceof Node) up(c)\r\n else if (typeof c == 'number') depth = c\r\n else uc = c\r\n}\r\n"]}
1
+ {"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAkBhD,8GAA8G;AAC9G,IAAI,MAAM,GAAmB,EAAE,CAAA;AAE/B,IAAI,oBAAoB,GAAG,CAAC,CAAU,EAAE,CAAS,EAAE,EAAE;IACjD,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AAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAA,CAAC,CAAC,CAAA;IAEnF,KAAK,MAAM,CAAC,IAAI,IAAI;QAChB,IAAI,OAAO,CAAC,IAAI,QAAQ;YAAE,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;aAC7D,IAAI,CAAC,YAAY,IAAI;YAAE,EAAE,CAAC,CAAC,CAAC,CAAA;aAC5B,IAAI,OAAO,CAAC,IAAI,QAAQ;YAAE,KAAK,GAAG,CAAC,CAAA;;YACnC,EAAE,GAAG,CAAC,CAAA;AACnB,CAAC","sourcesContent":["import type {\n H,\n HElement,\n HComp,\n J,\n Command,\n Children,\n HClassComponent,\n HU,\n} from './h.js'\nimport { rebind } from './base.js'\nimport { jmcconfig, setprops } from './props.js'\n\nconst enum NodeType {\n // vaporizes (but for that must be in this file, otherwise not)\n TextNode = 3,\n Comment = 8,\n}\n\n/** a node as jmc sees it. kept module-local, so consumers' Node type stays untouched */\ntype JNode<N extends Node = ChildNode> = N & {\n /** the H this node updates from: the element's own H, or the outermost component rendering it */\n h?: HElement | HComp\n /** the class components rendering this node, outermost first. they hold the instances, found again by position */\n cs?: HClassComponent[]\n /** props last applied to this element. kept apart from h, which may be the owning component's H */\n ep?: J\n}\n\n/** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */\nlet mounts: (() => void)[] = []\n\nlet removeexcesschildren = (n: Element, i: number) => {\n let c: ChildNode\n while ((c = n.childNodes[i])) {\n try {\n c.remove()\n } catch { } // due to blur-event-handlers, this can fail\n }\n}\n\n/** attaches node n, rendered from h by the components hs (outermost first), to them */\nlet attach = (n: JNode, h: HElement | HComp, hs: HComp[]) => {\n n.h = hs[0] ?? h\n n.cs = hs.filter(x => x.kind == 3)\n}\n\n/** synchronizes children starting at the i-th node. returns the index after the last child synchronized */\nlet syncchildren = (\n p: Element,\n cn: Children,\n i: number,\n depth: number,\n uc: J,\n): number => {\n cn.flat(999).forEach((hc) => (i = sync(p, i, hc, [], depth, uc)))\n return i\n}\n\n/** class components must render exactly one root node, they are attached to it and update through it */\nlet assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {\n if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)\n}\n\nfunction sync(\n parent: Element,\n i: number,\n h: HU,\n hs: HComp[], // the components rendering h, outermost first\n depth: number,\n uc: J,\n): number {\n let c: JNode | undefined = parent.childNodes[i]\n\n // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.\n // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state\n if (h == null || typeof h == 'boolean') {\n if (!hs.some(x => x.kind == 3)) h = '' // an empty text node, invisible\n else {\n // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again\n if (c?.nodeType != NodeType.Comment) {\n let n = new Comment()\n c ? c.replaceWith(n) : parent.append(n)\n c = n\n }\n attach(c, hs[0], hs)\n return i + 1\n }\n }\n\n switch (h.kind) {\n case 1: {\n // element\n let n: Element & JNode\n let { tag, cn = [], p } = h\n\n let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()\n if (fresh) {\n let ns =\n p.xmlns ??\n (parent.tagName == 'foreignObject'\n ? 'http://www.w3.org/1999/xhtml'\n : parent.namespaceURI)\n n = document.createElementNS(ns, tag)\n c ? c.replaceWith(n) : parent.appendChild(n)\n } else n = c as Element & JNode\n\n attach(n, h, hs) // before update(), which may handle the update itself\n if (jmcconfig.debug && hs[0]) n.setAttribute('comp', hs[0].tag.name)\n if (!fresh && p.update?.(n, uc)) return i + 1\n\n // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs\n if (fresh || !p.const) {\n n.ep = setprops(n, n.ep, p)\n if (--depth && !tag.includes('-')) { // web components render their own children\n const j = syncchildren(n, cn, 0, depth, uc)\n removeexcesschildren(n, j)\n }\n }\n if (fresh && p.mounted) mounts.push(() => p.mounted(n))\n\n return i + 1\n }\n\n case 2: {\n // function component\n let hr = h.tag(h.p)\n return sync(parent, i, hr, [...hs, h], depth, uc)\n }\n\n case 3: {\n // class component. reuses the instance attached to c, if c renders the same class at the same position\n let outer = hs.filter(x => x.kind == 3)\n let old = c?.cs?.[outer.length]\n if (old?.tag != h.tag) old = undefined\n let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))\n if (old) {\n ci.props = old.p = h.p // set attached p too, for a repeatable update through the node\n if (ci.update?.(uc)) { // update() substitutes view/sync when returning truthy\n c!.h = hs[0] ?? old // the components below old stay attached\n c!.cs = [...outer, ...c!.cs!.slice(outer.length)]\n return i + 1\n }\n }\n let hv = ci.view()\n let j = sync(parent, i, hv, [...hs, old ?? h], depth, uc) // the attached H, so updates find the instance\n assertnonfragment(h, hv, i, j)\n ci.element = parent.childNodes[i]\n if (!old && ci.mounted) mounts.push(() => ci.mounted!())\n return j\n }\n\n case 4: {\n // fragment. hs is deliberately not passed on: a component returning a fragment is not attached\n // to any node, so it cannot update itself, only its parent can update it\n return syncchildren(parent, h.cn ?? [], i, --depth, uc)\n }\n\n default:\n // textnode\n let s = h.toString()\n if (c?.nodeType == NodeType.TextNode) {\n if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection\n } else {\n c ? c.replaceWith(s) : parent.append(s)\n }\n return i + 1\n }\n}\n\nexport function patch(\n e: Node | null,\n h: H,\n depth = 999,\n uc: J = {},\n): HTMLElement | undefined {\n if (!e) return\n if (depth == 0) {\n (e as Element).replaceChildren()\n depth = 999\n }\n let p = e.parentElement!\n let i = [...p.childNodes].indexOf(e as ChildNode)\n sync(p, i, h, [], depth, uc)\n\n // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew\n let ms = mounts\n mounts = []\n ms.forEach(m => m())\n\n return p.childNodes[i] as HTMLElement\n}\n\nexport function updateview(...cmds: Command[]): void {\n if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body\n let uc: J = {}\n let depth = 999\n\n // isConnected: a selector's matches are collected upfront, patching one can replace a later one\n let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }\n\n for (const c of cmds)\n if (typeof c == 'string') document.querySelectorAll(c).forEach(up)\n else if (c instanceof Node) up(c)\n else if (typeof c == 'number') depth = c\n else uc = c\n}\n"]}
package/dist/lib.d.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import type { Children, IClassComponent, HU, J, HFragment } from './h.js';
2
2
  export type UC = J;
3
- export type FComponentT<P> = (pcn: P, cn?: Children) => HU;
3
+ export type FComponentT<P> = (props: P) => HU;
4
4
  export type DeepReadonly<T> = {
5
5
  readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K];
6
6
  };
7
- export declare const When: ({ cond }: {
7
+ export declare const When: ({ cond, children }: {
8
8
  cond: boolean;
9
- }, cn: Children) => HFragment | null;
9
+ children?: Children;
10
+ }) => HFragment | null;
10
11
  export declare abstract class Component<P extends J = {}> implements IClassComponent {
11
12
  props: P;
12
13
  element: HTMLElement;
package/dist/lib.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,eAAe,EAAE,EAAE,EAAE,CAAC,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAGzE,MAAM,MAAM,EAAE,GAAG,CAAC,CAAA;AAClB,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,QAAQ,KAAK,EAAE,CAAA;AAE1D,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI;IAAE,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC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H,eAAO,MAAM,IAAI,GAAI,oBAAoB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAE,QAAQ,CAAA;CAAE,KAAG,SAAS,GAAG,IAA+C,CAAA;AAE7I,8BAAsB,SAAS,CAAC,CAAC,SAAS,CAAC,GAAG,EAAE,CAAE,YAAW,eAAe;IAGrD,KAAK,EAAE,CAAC;IAD3B,OAAO,EAAG,WAAW,CAAA;gBACF,KAAK,EAAE,CAAC;IAG3B,OAAO;IACP,MAAM,CAAC,EAAE,CAAC,EAAE,GAAG,GAAG,OAAO,GAAG,IAAI;IAChC,QAAQ,CAAC,IAAI,IAAI,EAAE;IAGnB,UAAU;IACV,IAAI,SAAS,IAAI,OAAO,CAA0B;CACrD;AAED,wBAAgB,EAAE,CAAC,GAAG,cAAc,EAAE,CAAC,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC,EAAE,GAAG,MAAM,CAIxE;AAED,eAAO,MAAM,YAAY,GAAI,GAAG,IAAI,SAA+B,CAAA"}
package/dist/lib.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { updateview } from './jmc.js';
2
- export const When = ({ cond }, cn) => cond ? { kind: 4, cn } : null;
2
+ export const When = ({ cond, children }) => cond ? { kind: 4, cn: children } : null;
3
3
  export class Component {
4
4
  props;
5
5
  element; // assigned by jmc. a placeholder Comment while view() renders nothing
package/dist/lib.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAOrC,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,EAAE,IAAI,EAAqB,EAAE,EAAY,EAAoB,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;AAElH,MAAM,OAAgB,SAAS;IAGR;IADnB,OAAO,CAAc,CAAC,sEAAsE;IAC5F,YAAmB,KAAQ;QAAR,UAAK,GAAL,KAAK,CAAG;IAAI,CAAC;IAEhC,YAAY;IACZ,OAAO,KAAK,CAAC;IACb,MAAM,CAAC,EAAQ,IAAoB,CAAC;IAGpC,wCAAwC;IACxC,UAAU,KAAK,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA,CAAC,CAAC;IACzC,IAAI,SAAS,KAAc,OAAO,CAAC,CAAC,IAAI,CAAC,OAAO,CAAA,CAAC,CAAC;CACrD;AAED,MAAM,UAAU,EAAE,CAAC,GAAG,cAA0C;IAC5D,OAAO,cAAc;SAChB,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,uCAAuC;SACzH,IAAI,CAAC,GAAG,CAAC,CAAA;AAClB,CAAC;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAO,EAAE,EAAE,CAAE,CAAiB,CAAC,KAAK,EAAE,CAAA","sourcesContent":["import type { Children, IClassComponent, HU, J, HFragment } from './h.js'\r\nimport { updateview } from './jmc.js'\r\n\r\nexport type UC = J // update context\r\nexport type FComponentT<P> = (pcn: P, cn?: Children) => HU // a function component with typed props\r\n\r\nexport type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K]; }\r\n\r\nexport const When = ({ cond }: { cond: boolean }, cn: Children): HFragment | null => cond ? { kind: 4, cn } : null\r\n\r\nexport abstract class Component<P extends J = {}> implements IClassComponent {\r\n\r\n element!: HTMLElement // assigned by jmc. a placeholder Comment while view() renders nothing\r\n constructor(public props: P) { }\r\n\r\n // overrides\r\n mounted() { }\r\n update(uc?: any): boolean | void { }\r\n abstract view(): HU\r\n\r\n // utility: updates the component's view\r\n updateview() { updateview(this.element) }\r\n get ismounted(): boolean { return !!this.element }\r\n}\r\n\r\nexport function cc(...namesOrObjects: (string | J | undefined)[]): string {\r\n return namesOrObjects\r\n .flatMap(n => typeof n == 'string' ? n : Object.keys(n ?? {}).filter(k => n?.[k])) // objects contribute their truthy keys\r\n .join(' ')\r\n}\r\n\r\nexport const focuselement = (e: Node) => (e as HTMLElement).focus()"]}
1
+ {"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAOrC,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,EAAE,IAAI,EAAE,QAAQ,EAA0C,EAAoB,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;AAE7I,MAAM,OAAgB,SAAS;IAGR;IADnB,OAAO,CAAc,CAAC,sEAAsE;IAC5F,YAAmB,KAAQ;QAAR,UAAK,GAAL,KAAK,CAAG;IAAI,CAAC;IAEhC,YAAY;IACZ,OAAO,KAAK,CAAC;IACb,MAAM,CAAC,EAAQ,IAAoB,CAAC;IAGpC,wCAAwC;IACxC,UAAU,KAAK,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA,CAAC,CAAC;IACzC,IAAI,SAAS,KAAc,OAAO,CAAC,CAAC,IAAI,CAAC,OAAO,CAAA,CAAC,CAAC;CACrD;AAED,MAAM,UAAU,EAAE,CAAC,GAAG,cAA0C;IAC5D,OAAO,cAAc;SAChB,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,uCAAuC;SACzH,IAAI,CAAC,GAAG,CAAC,CAAA;AAClB,CAAC;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAO,EAAE,EAAE,CAAE,CAAiB,CAAC,KAAK,EAAE,CAAA","sourcesContent":["import type { Children, IClassComponent, HU, J, HFragment } from './h.js'\r\nimport { updateview } from './jmc.js'\r\n\r\nexport type UC = J // update context\r\nexport type FComponentT<P> = (props: P) => HU // a function component with typed props. children come in props.children\r\n\r\nexport type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K]; }\r\n\r\nexport const When = ({ cond, children }: { cond: boolean, children?: Children }): HFragment | null => cond ? { kind: 4, cn: children } : null\r\n\r\nexport abstract class Component<P extends J = {}> implements IClassComponent {\r\n\r\n element!: HTMLElement // assigned by jmc. a placeholder Comment while view() renders nothing\r\n constructor(public props: P) { }\r\n\r\n // overrides\r\n mounted() { }\r\n update(uc?: any): boolean | void { }\r\n abstract view(): HU\r\n\r\n // utility: updates the component's view\r\n updateview() { updateview(this.element) }\r\n get ismounted(): boolean { return !!this.element }\r\n}\r\n\r\nexport function cc(...namesOrObjects: (string | J | undefined)[]): string {\r\n return namesOrObjects\r\n .flatMap(n => typeof n == 'string' ? n : Object.keys(n ?? {}).filter(k => n?.[k])) // objects contribute their truthy keys\r\n .join(' ')\r\n}\r\n\r\nexport const focuselement = (e: Node) => (e as HTMLElement).focus()"]}
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/snuup/jmc#readme",
19
19
  "bugs": "https://github.com/snuup/jmc/issues",
20
- "version": "1.4.1",
20
+ "version": "1.5.0",
21
21
  "publishConfig": {
22
22
  "access": "public"
23
23
  },
package/src/h.ts CHANGED
@@ -7,9 +7,8 @@ export type Selector = string | Node
7
7
  /** an updateview command: a target (selector or node), a depth (number) or an update context (object) */
8
8
  export type Command = Selector | J | number
9
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
10
+ /** a function component. it gets its children in props.children */
11
+ export type FComponent = (props: J) => HU
13
12
 
14
13
  type HKindElement = 1
15
14
  type HKindFunctionComponent = 2
package/src/jmc.ts CHANGED
@@ -1,194 +1,207 @@
1
- import type {
2
- H,
3
- HElement,
4
- HComp,
5
- J,
6
- Command,
7
- Children,
8
- HClassComponent,
9
- HU,
10
- } from './h.js'
11
- import { rebind } from './base.js'
12
- import { jmcconfig, setprops } from './props.js'
13
-
14
- const enum NodeType {
15
- // vaporizes (but for that must be in this file, otherwise not)
16
- TextNode = 3,
17
- Comment = 8,
18
- }
19
-
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
26
- }
27
-
28
- /** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */
29
- let mounts: (() => void)[] = []
30
-
31
- let removeexcesschildren = (n: Element, i: number) => {
32
- let c: ChildNode
33
- while ((c = n.childNodes[i])) {
34
- try {
35
- c.remove()
36
- } catch { } // due to blur-event-handlers, this can fail
37
- }
38
- }
39
-
40
- /** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
41
- let syncchildren = (
42
- p: Element,
43
- cn: Children,
44
- i: number,
45
- depth: number,
46
- uc: J,
47
- ): number => {
48
- cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
49
- return i
50
- }
51
-
52
- /** class components must render exactly one root node, they are attached to it and update through it */
53
- let assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {
54
- if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)
55
- }
56
-
57
- function sync(
58
- parent: Element,
59
- i: number,
60
- h: HU,
61
- hfun: HComp | undefined,
62
- depth: number,
63
- uc: J,
64
- ): number {
65
- let c: JNode | undefined = parent.childNodes[i]
66
-
67
- // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.
68
- // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state
69
- if (h == null || typeof h == 'boolean') {
70
- if (hfun?.kind != 3) h = '' // an empty text node, invisible
71
- else {
72
- // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again
73
- if (c?.nodeType != NodeType.Comment) {
74
- let n = new Comment()
75
- c ? c.replaceWith(n) : parent.append(n)
76
- c = n
77
- }
78
- c.h = hfun
79
- return i + 1
80
- }
81
- }
82
-
83
- switch (h.kind) {
84
- case 1: {
85
- // element
86
- let n: Element & JNode
87
- let { tag, cn = [], p } = h
88
-
89
- let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()
90
- if (fresh) {
91
- let ns =
92
- p.xmlns ??
93
- (parent.tagName == 'foreignObject'
94
- ? 'http://www.w3.org/1999/xhtml'
95
- : parent.namespaceURI)
96
- n = document.createElementNS(ns, tag)
97
- c ? c.replaceWith(n) : parent.appendChild(n)
98
- } else n = c as Element & JNode
99
-
100
- n.h = hfun ?? h // attach before update(), which may handle the update itself
101
- if (jmcconfig.debug && hfun) n.setAttribute('comp', hfun.tag.name)
102
- if (!fresh && p.update?.(n, uc)) return i + 1
103
-
104
- // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs
105
- if (fresh || !p.const) {
106
- n.ep = setprops(n, n.ep, p)
107
- if (--depth && !tag.includes('-')) { // web components render their own children
108
- const j = syncchildren(n, cn, 0, depth, uc)
109
- removeexcesschildren(n, j)
110
- }
111
- }
112
- if (fresh && p.mounted) mounts.push(() => p.mounted(n))
113
-
114
- return i + 1
115
- }
116
-
117
- case 2: {
118
- // function component
119
- // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children
120
- let hr = h.tag(h.p, h.cn)
121
- return sync(parent, i, hr, hfun ?? h, depth, uc)
122
- }
123
-
124
- case 3: {
125
- // class component. reuses the instance attached to c, if c renders the same class
126
- let old = c?.h?.tag == h.tag ? c.h as HClassComponent : undefined
127
- let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))
128
- if (old) {
129
- ci.props = old.p = h.p // set attached p too, for a repeatable update through c.h
130
- if (ci.update?.(uc)) return i + 1 // update() substitutes view/sync when returning truthy
131
- }
132
- let hv = ci.view()
133
- let j = sync(parent, i, hv, old ?? h, depth, uc) // the attached H, so updates find the instance
134
- assertnonfragment(h, hv, i, j)
135
- ci.element = parent.childNodes[i]
136
- if (!old && ci.mounted) mounts.push(() => ci.mounted!())
137
- return j
138
- }
139
-
140
- case 4: {
141
- // fragment. hfun is deliberately not passed on: a component returning a fragment is not attached
142
- // to any node, so it cannot update itself, only its parent can update it
143
- return syncchildren(parent, h.cn ?? [], i, --depth, uc)
144
- }
145
-
146
- default:
147
- // textnode
148
- let s = h.toString()
149
- if (c?.nodeType == NodeType.TextNode) {
150
- if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection
151
- } else {
152
- c ? c.replaceWith(s) : parent.append(s)
153
- }
154
- return i + 1
155
- }
156
- }
157
-
158
- export function patch(
159
- e: Node | null,
160
- h: H,
161
- depth = 999,
162
- uc: J = {},
163
- ): HTMLElement | undefined {
164
- if (!e) return
165
- if (depth == 0) {
166
- (e as Element).replaceChildren()
167
- depth = 999
168
- }
169
- let p = e.parentElement!
170
- let i = [...p.childNodes].indexOf(e as ChildNode)
171
- sync(p, i, h, undefined, depth, uc)
172
-
173
- // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew
174
- let ms = mounts
175
- mounts = []
176
- ms.forEach(m => m())
177
-
178
- return p.childNodes[i] as HTMLElement
179
- }
180
-
181
- export function updateview(...cmds: Command[]): void {
182
- if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body
183
- let uc: J = {}
184
- let depth = 999
185
-
186
- // isConnected: a selector's matches are collected upfront, patching one can replace a later one
187
- let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }
188
-
189
- for (const c of cmds)
190
- if (typeof c == 'string') document.querySelectorAll(c).forEach(up)
191
- else if (c instanceof Node) up(c)
192
- else if (typeof c == 'number') depth = c
193
- else uc = c
194
- }
1
+ import type {
2
+ H,
3
+ HElement,
4
+ HComp,
5
+ J,
6
+ Command,
7
+ Children,
8
+ HClassComponent,
9
+ HU,
10
+ } from './h.js'
11
+ import { rebind } from './base.js'
12
+ import { jmcconfig, setprops } from './props.js'
13
+
14
+ const enum NodeType {
15
+ // vaporizes (but for that must be in this file, otherwise not)
16
+ TextNode = 3,
17
+ Comment = 8,
18
+ }
19
+
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
+ /** the class components rendering this node, outermost first. they hold the instances, found again by position */
25
+ cs?: HClassComponent[]
26
+ /** props last applied to this element. kept apart from h, which may be the owning component's H */
27
+ ep?: J
28
+ }
29
+
30
+ /** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */
31
+ let mounts: (() => void)[] = []
32
+
33
+ let removeexcesschildren = (n: Element, i: number) => {
34
+ let c: ChildNode
35
+ while ((c = n.childNodes[i])) {
36
+ try {
37
+ c.remove()
38
+ } catch { } // due to blur-event-handlers, this can fail
39
+ }
40
+ }
41
+
42
+ /** attaches node n, rendered from h by the components hs (outermost first), to them */
43
+ let attach = (n: JNode, h: HElement | HComp, hs: HComp[]) => {
44
+ n.h = hs[0] ?? h
45
+ n.cs = hs.filter(x => x.kind == 3)
46
+ }
47
+
48
+ /** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
49
+ let syncchildren = (
50
+ p: Element,
51
+ cn: Children,
52
+ i: number,
53
+ depth: number,
54
+ uc: J,
55
+ ): number => {
56
+ cn.flat(999).forEach((hc) => (i = sync(p, i, hc, [], depth, uc)))
57
+ return i
58
+ }
59
+
60
+ /** class components must render exactly one root node, they are attached to it and update through it */
61
+ let assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {
62
+ if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)
63
+ }
64
+
65
+ function sync(
66
+ parent: Element,
67
+ i: number,
68
+ h: HU,
69
+ hs: HComp[], // the components rendering h, outermost first
70
+ depth: number,
71
+ uc: J,
72
+ ): number {
73
+ let c: JNode | undefined = parent.childNodes[i]
74
+
75
+ // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.
76
+ // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state
77
+ if (h == null || typeof h == 'boolean') {
78
+ if (!hs.some(x => x.kind == 3)) h = '' // an empty text node, invisible
79
+ else {
80
+ // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again
81
+ if (c?.nodeType != NodeType.Comment) {
82
+ let n = new Comment()
83
+ c ? c.replaceWith(n) : parent.append(n)
84
+ c = n
85
+ }
86
+ attach(c, hs[0], hs)
87
+ return i + 1
88
+ }
89
+ }
90
+
91
+ switch (h.kind) {
92
+ case 1: {
93
+ // element
94
+ let n: Element & JNode
95
+ let { tag, cn = [], p } = h
96
+
97
+ let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()
98
+ if (fresh) {
99
+ let ns =
100
+ p.xmlns ??
101
+ (parent.tagName == 'foreignObject'
102
+ ? 'http://www.w3.org/1999/xhtml'
103
+ : parent.namespaceURI)
104
+ n = document.createElementNS(ns, tag)
105
+ c ? c.replaceWith(n) : parent.appendChild(n)
106
+ } else n = c as Element & JNode
107
+
108
+ attach(n, h, hs) // before update(), which may handle the update itself
109
+ if (jmcconfig.debug && hs[0]) n.setAttribute('comp', hs[0].tag.name)
110
+ if (!fresh && p.update?.(n, uc)) return i + 1
111
+
112
+ // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs
113
+ if (fresh || !p.const) {
114
+ n.ep = setprops(n, n.ep, p)
115
+ if (--depth && !tag.includes('-')) { // web components render their own children
116
+ const j = syncchildren(n, cn, 0, depth, uc)
117
+ removeexcesschildren(n, j)
118
+ }
119
+ }
120
+ if (fresh && p.mounted) mounts.push(() => p.mounted(n))
121
+
122
+ return i + 1
123
+ }
124
+
125
+ case 2: {
126
+ // function component
127
+ let hr = h.tag(h.p)
128
+ return sync(parent, i, hr, [...hs, h], depth, uc)
129
+ }
130
+
131
+ case 3: {
132
+ // class component. reuses the instance attached to c, if c renders the same class at the same position
133
+ let outer = hs.filter(x => x.kind == 3)
134
+ let old = c?.cs?.[outer.length]
135
+ if (old?.tag != h.tag) old = undefined
136
+ let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))
137
+ if (old) {
138
+ ci.props = old.p = h.p // set attached p too, for a repeatable update through the node
139
+ if (ci.update?.(uc)) { // update() substitutes view/sync when returning truthy
140
+ c!.h = hs[0] ?? old // the components below old stay attached
141
+ c!.cs = [...outer, ...c!.cs!.slice(outer.length)]
142
+ return i + 1
143
+ }
144
+ }
145
+ let hv = ci.view()
146
+ let j = sync(parent, i, hv, [...hs, old ?? h], depth, uc) // the attached H, so updates find the instance
147
+ assertnonfragment(h, hv, i, j)
148
+ ci.element = parent.childNodes[i]
149
+ if (!old && ci.mounted) mounts.push(() => ci.mounted!())
150
+ return j
151
+ }
152
+
153
+ case 4: {
154
+ // fragment. hs is deliberately not passed on: a component returning a fragment is not attached
155
+ // to any node, so it cannot update itself, only its parent can update it
156
+ return syncchildren(parent, h.cn ?? [], i, --depth, uc)
157
+ }
158
+
159
+ default:
160
+ // textnode
161
+ let s = h.toString()
162
+ if (c?.nodeType == NodeType.TextNode) {
163
+ if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection
164
+ } else {
165
+ c ? c.replaceWith(s) : parent.append(s)
166
+ }
167
+ return i + 1
168
+ }
169
+ }
170
+
171
+ export function patch(
172
+ e: Node | null,
173
+ h: H,
174
+ depth = 999,
175
+ uc: J = {},
176
+ ): HTMLElement | undefined {
177
+ if (!e) return
178
+ if (depth == 0) {
179
+ (e as Element).replaceChildren()
180
+ depth = 999
181
+ }
182
+ let p = e.parentElement!
183
+ let i = [...p.childNodes].indexOf(e as ChildNode)
184
+ sync(p, i, h, [], depth, uc)
185
+
186
+ // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew
187
+ let ms = mounts
188
+ mounts = []
189
+ ms.forEach(m => m())
190
+
191
+ return p.childNodes[i] as HTMLElement
192
+ }
193
+
194
+ export function updateview(...cmds: Command[]): void {
195
+ if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body
196
+ let uc: J = {}
197
+ let depth = 999
198
+
199
+ // isConnected: a selector's matches are collected upfront, patching one can replace a later one
200
+ let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }
201
+
202
+ for (const c of cmds)
203
+ if (typeof c == 'string') document.querySelectorAll(c).forEach(up)
204
+ else if (c instanceof Node) up(c)
205
+ else if (typeof c == 'number') depth = c
206
+ else uc = c
207
+ }
package/src/lib.ts CHANGED
@@ -2,11 +2,11 @@ import type { Children, IClassComponent, HU, J, HFragment } from './h.js'
2
2
  import { updateview } from './jmc.js'
3
3
 
4
4
  export type UC = J // update context
5
- export type FComponentT<P> = (pcn: P, cn?: Children) => HU // a function component with typed props
5
+ export type FComponentT<P> = (props: P) => HU // a function component with typed props. children come in props.children
6
6
 
7
7
  export type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K]; }
8
8
 
9
- export const When = ({ cond }: { cond: boolean }, cn: Children): HFragment | null => cond ? { kind: 4, cn } : null
9
+ export const When = ({ cond, children }: { cond: boolean, children?: Children }): HFragment | null => cond ? { kind: 4, cn: children } : null
10
10
 
11
11
  export abstract class Component<P extends J = {}> implements IClassComponent {
12
12