@esportsplus/template 0.19.0 → 0.19.2

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.
@@ -1,7 +1,8 @@
1
1
  import { ReactiveArray } from '@esportsplus/reactivity';
2
- import { RenderableReactive, RenderableValue } from '../types.js';
2
+ import { Attributes, Renderable, RenderableReactive } from '../types.js';
3
+ type Values<T = unknown> = Attributes | Attributes[] | Readonly<Attributes> | Readonly<Attributes[]> | Renderable<T>;
3
4
  declare const html: {
4
- (literals: TemplateStringsArray, ...values: (RenderableValue | RenderableValue[])[]): DocumentFragment;
5
+ (literals: TemplateStringsArray, ...values: (Values | Values[])[]): Node;
5
6
  reactive<T>(array: ReactiveArray<T[]>, template: RenderableReactive["template"]): RenderableReactive;
6
7
  };
7
8
  export default html;
@@ -2,37 +2,21 @@ import { RENDERABLE, RENDERABLE_HTML_REACTIVE_ARRAY } from '../constants.js';
2
2
  import { cloneNode } from '../utilities/node.js';
3
3
  import parser from './parser.js';
4
4
  const html = (literals, ...values) => {
5
- let { fragment, slots } = parser.parse(literals);
6
- fragment = cloneNode.call(fragment, true);
5
+ let { fragment, slots } = parser.parse(literals), clone = cloneNode.call(fragment, true);
7
6
  if (slots !== null) {
8
- let node, nodePath, parent, parentPath;
9
- for (let i = 0, n = slots.length; i < n; i++) {
10
- let { fn, path, slot } = slots[i], pp = path.parent, pr = path.relative;
11
- if (pp !== parentPath) {
12
- if (pp === nodePath) {
13
- parent = node;
14
- parentPath = nodePath;
15
- nodePath = undefined;
16
- }
17
- else {
18
- parent = fragment;
19
- parentPath = pp;
20
- for (let i = 0, n = pp.length; i < n; i++) {
21
- parent = pp[i].call(parent);
22
- }
23
- }
24
- }
25
- if (pr !== nodePath) {
26
- node = parent;
27
- nodePath = path.absolute;
28
- for (let i = 0, n = pr.length; i < n; i++) {
29
- node = pr[i].call(node);
7
+ let node, nodePath;
8
+ for (let i = slots.length - 1; i >= 0; i--) {
9
+ let { fn, path, slot } = slots[i];
10
+ if (nodePath !== path) {
11
+ node = clone;
12
+ for (let i = 0, n = path.length; i < n; i++) {
13
+ node = path[i].call(node);
30
14
  }
31
15
  }
32
16
  fn(node, values[slot]);
33
17
  }
34
18
  }
35
- return fragment;
19
+ return clone;
36
20
  };
37
21
  html.reactive = (array, template) => {
38
22
  return {
@@ -27,15 +27,11 @@ function build(literals) {
27
27
  ? methods(parent.elements, parent.path, firstElementChild, nextElementSibling)
28
28
  : methods(parent.children, [], firstChild, nextSibling);
29
29
  if (attr) {
30
- let i = attr.indexOf(SLOT_MARKER), p = {
31
- absolute: path,
32
- parent: parent.path,
33
- relative: path.slice(parent.path.length)
34
- };
30
+ let i = attr.indexOf(SLOT_MARKER);
35
31
  while (i !== -1) {
36
32
  slots.push({
37
33
  fn: spread,
38
- path: p,
34
+ path,
39
35
  slot
40
36
  });
41
37
  buffer += parsed[slot++];
@@ -52,15 +48,10 @@ function build(literals) {
52
48
  parent.elements++;
53
49
  }
54
50
  else if (type === NODE_SLOT) {
55
- let relative = methods(parent.children, [], firstChild, nextSibling);
56
51
  buffer += parsed[slot] + SLOT_HTML;
57
52
  slots.push({
58
53
  fn: s,
59
- path: {
60
- absolute: [...parent.path, ...relative],
61
- parent: parent.path,
62
- relative
63
- },
54
+ path: methods(parent.children, parent.path, firstChild, nextSibling),
64
55
  slot: slot++
65
56
  });
66
57
  }
package/build/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { default as html } from './html/index.js';
2
2
  export { default as render } from './render.js';
3
3
  export { default as svg } from './svg.js';
4
- export type { Attributes, Element, Renderable, RenderableValue } from './types.js';
4
+ export type { Attributes, Element, Renderable } from './types.js';
package/build/types.d.ts CHANGED
@@ -17,17 +17,15 @@ type Attributes = {
17
17
  ondisconnect?: (element: Element) => void;
18
18
  onrender?: (element: Element) => void;
19
19
  } & Record<PropertyKey, unknown>;
20
- type Effect<T> = () => EffectResponse<T>;
21
- type EffectResponse<T> = T extends [] ? (Primitive | Renderable)[] : Primitive | Renderable;
20
+ type Effect<T> = () => T extends [] ? Renderable[] : Renderable;
22
21
  type Element = HTMLElement & Attributes & Record<PropertyKey, unknown>;
23
22
  type Primitive = bigint | boolean | null | number | string | undefined;
24
- type Renderable = DocumentFragment | Node | NodeList | Primitive | RenderableReactive | Renderable[];
23
+ type Renderable<T = unknown> = DocumentFragment | Effect<T> | Node | NodeList | Primitive | RenderableReactive | Renderable[];
25
24
  type RenderableReactive = Readonly<{
26
25
  [RENDERABLE]: typeof RENDERABLE_HTML_REACTIVE_ARRAY;
27
26
  array: ReactiveArray<unknown[]>;
28
27
  template: (this: ReactiveArray<unknown[]>, ...args: Parameters<Parameters<ReactiveArray<unknown[]>['map']>[0]>) => ReturnType<typeof html>;
29
28
  }>;
30
- type RenderableValue<T = unknown> = Attributes | Readonly<Attributes> | Readonly<Attributes[]> | Effect<T> | Primitive | RenderableReactive;
31
29
  type SlotGroup = {
32
30
  head: Element;
33
31
  tail: Element;
@@ -38,12 +36,8 @@ type Template = {
38
36
  literals: TemplateStringsArray;
39
37
  slots: {
40
38
  fn: typeof attributes.spread | typeof slot;
41
- path: {
42
- absolute: typeof firstChild[];
43
- parent: typeof firstChild[];
44
- relative: typeof firstChild[];
45
- };
39
+ path: typeof firstChild[];
46
40
  slot: number;
47
41
  }[] | null;
48
42
  };
49
- export type { Attributes, Effect, Element, Renderable, RenderableReactive, RenderableValue, SlotGroup, Template };
43
+ export type { Attributes, Effect, Element, Renderable, RenderableReactive, SlotGroup, Template };
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "private": false,
15
15
  "type": "module",
16
16
  "types": "./build/index.d.ts",
17
- "version": "0.19.0",
17
+ "version": "0.19.2",
18
18
  "scripts": {
19
19
  "build": "tsc && tsc-alias",
20
20
  "-": "-"
package/src/attributes.ts CHANGED
@@ -8,6 +8,9 @@ import q from '@esportsplus/queue';
8
8
  import event from './event';
9
9
 
10
10
 
11
+ // TODO: Refactor
12
+
13
+
11
14
  const EFFECT = Symbol();
12
15
 
13
16
  const STORE = Symbol();
package/src/html/index.ts CHANGED
@@ -1,46 +1,59 @@
1
1
  import { ReactiveArray } from '@esportsplus/reactivity';
2
2
  import { RENDERABLE, RENDERABLE_HTML_REACTIVE_ARRAY } from '~/constants';
3
- import { RenderableReactive, RenderableValue } from '~/types';
3
+ import { Attributes, Renderable, RenderableReactive } from '~/types';
4
4
  import { cloneNode } from '~/utilities/node';
5
5
  import parser from './parser';
6
6
 
7
7
 
8
- const html = (literals: TemplateStringsArray, ...values: (RenderableValue | RenderableValue[])[]): DocumentFragment => {
9
- let { fragment, slots } = parser.parse(literals);
8
+ type Values<T = unknown> = Attributes | Attributes[] | Readonly<Attributes> | Readonly<Attributes[]> | Renderable<T>;
10
9
 
11
- fragment = cloneNode.call(fragment, true) as DocumentFragment;
10
+
11
+ const html = (literals: TemplateStringsArray, ...values: (Values | Values[])[]) => {
12
+ let { fragment, slots } = parser.parse(literals),
13
+ clone = cloneNode.call(fragment, true);
12
14
 
13
15
  if (slots !== null) {
14
- let node, nodePath, parent, parentPath;
16
+ let node, nodePath; // , parent, parentPath;
15
17
 
16
- for (let i = 0, n = slots.length; i < n; i++) {
17
- let { fn, path, slot } = slots[i],
18
- pp = path.parent,
19
- pr = path.relative;
18
+ // TODO: when a new slot is added it breaks pathfinding for the next slot
19
+ // for (let i = 0, n = slots.length; i < n; i++) {
20
+ for (let i = slots.length - 1; i >= 0; i--) {
21
+ let { fn, path, slot } = slots[i];
20
22
 
21
- if (pp !== parentPath) {
22
- if (pp === nodePath) {
23
- parent = node;
24
- parentPath = nodePath;
23
+ // pp = path.parent,
24
+ // pr = path.relative;
25
25
 
26
- nodePath = undefined;
27
- }
28
- else {
29
- parent = fragment;
30
- parentPath = pp;
26
+ // if (pp !== parentPath) {
27
+ // if (pp === nodePath) {
28
+ // parent = node;
29
+ // parentPath = nodePath;
31
30
 
32
- for (let i = 0, n = pp.length; i < n; i++) {
33
- parent = pp[i].call(parent);
34
- }
35
- }
36
- }
31
+ // nodePath = undefined;
32
+ // }
33
+ // else {
34
+ // parent = clone;
35
+ // parentPath = pp;
36
+
37
+ // for (let i = 0, n = pp.length; i < n; i++) {
38
+ // parent = pp[i].call(parent);
39
+ // }
40
+ // }
41
+ // }
42
+
43
+ // if (pr !== nodePath) {
44
+ // node = parent;
45
+ // nodePath = path.absolute;
46
+
47
+ // for (let i = 0, n = pr.length; i < n; i++) {
48
+ // node = pr[i].call(node);
49
+ // }
50
+ // }
37
51
 
38
- if (pr !== nodePath) {
39
- node = parent;
40
- nodePath = path.absolute;
52
+ if (nodePath !== path) {
53
+ node = clone;
41
54
 
42
- for (let i = 0, n = pr.length; i < n; i++) {
43
- node = pr[i].call(node);
55
+ for (let i = 0, n = path.length; i < n; i++) {
56
+ node = path[i].call(node);
44
57
  }
45
58
  }
46
59
 
@@ -49,7 +62,7 @@ const html = (literals: TemplateStringsArray, ...values: (RenderableValue | Rend
49
62
  }
50
63
  }
51
64
 
52
- return fragment;
65
+ return clone;
53
66
  };
54
67
 
55
68
  html.reactive = <T>(array: ReactiveArray<T[]>, template: RenderableReactive['template']): RenderableReactive => {
@@ -29,7 +29,7 @@ function build(literals: TemplateStringsArray) {
29
29
  levels = [{
30
30
  children: 0,
31
31
  elements: 0,
32
- path: [] as NonNullable<Template['slots']>[number]['path']['parent']
32
+ path: [] as NonNullable<Template['slots']>[number]['path']
33
33
  }],
34
34
  parsed = html.split(SLOT_MARKER),
35
35
  slot = 0,
@@ -51,17 +51,18 @@ function build(literals: TemplateStringsArray) {
51
51
  : methods(parent.children, [], firstChild, nextSibling);
52
52
 
53
53
  if (attr) {
54
- let i = attr.indexOf(SLOT_MARKER),
55
- p = {
56
- absolute: path,
57
- parent: parent.path,
58
- relative: path.slice(parent.path.length)
59
- };
54
+ let i = attr.indexOf(SLOT_MARKER);
55
+ // @see ./index.ts comments
56
+ // p = {
57
+ // absolute: path,
58
+ // parent: parent.path,
59
+ // relative: path.slice(parent.path.length)
60
+ // };
60
61
 
61
62
  while (i !== -1) {
62
63
  slots.push({
63
64
  fn: spread,
64
- path: p,
65
+ path,
65
66
  slot
66
67
  });
67
68
 
@@ -81,16 +82,18 @@ function build(literals: TemplateStringsArray) {
81
82
  parent.elements++;
82
83
  }
83
84
  else if (type === NODE_SLOT) {
84
- let relative = methods(parent.children, [], firstChild, nextSibling);
85
+ // @see ./index.ts comments
86
+ // let relative = methods(parent.children, [], firstChild, nextSibling);
85
87
 
86
88
  buffer += parsed[slot] + SLOT_HTML;
87
89
  slots.push({
88
90
  fn: s,
89
- path: {
90
- absolute: [...parent.path, ...relative],
91
- parent: parent.path,
92
- relative
93
- },
91
+ path: methods(parent.children, parent.path, firstChild, nextSibling),
92
+ // path: {
93
+ // absolute: [...parent.path, ...relative],
94
+ // parent: parent.path,
95
+ // relative
96
+ // },
94
97
  slot: slot++
95
98
  });
96
99
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { default as html } from './html';
2
2
  export { default as render } from './render';
3
3
  export { default as svg } from './svg';
4
- export type { Attributes, Element, Renderable, RenderableValue } from './types';
4
+ export type { Attributes, Element, Renderable } from './types';
package/src/types.ts CHANGED
@@ -18,14 +18,10 @@ type Attributes = {
18
18
  [key: `data-${string}`]: string | undefined;
19
19
  onconnect?: (element: Element) => void;
20
20
  ondisconnect?: (element: Element) => void;
21
- // Rendered in fragment
22
- // - Used to retrieve reference to the element
23
21
  onrender?: (element: Element) => void;
24
22
  } & Record<PropertyKey, unknown>;
25
23
 
26
- type Effect<T> = () => EffectResponse<T>;
27
-
28
- type EffectResponse<T> = T extends [] ? (Primitive | Renderable)[] : Primitive | Renderable;
24
+ type Effect<T> = () => T extends [] ? Renderable[] : Renderable;
29
25
 
30
26
  type Element = HTMLElement & Attributes & Record<PropertyKey, unknown>;
31
27
 
@@ -33,7 +29,7 @@ type Element = HTMLElement & Attributes & Record<PropertyKey, unknown>;
33
29
  // - Importing from ^ causes 'cannot be named without a reference to...' error
34
30
  type Primitive = bigint | boolean | null | number | string | undefined;
35
31
 
36
- type Renderable = DocumentFragment | Node | NodeList | Primitive | RenderableReactive | Renderable[];
32
+ type Renderable<T = unknown> = DocumentFragment | Effect<T> | Node | NodeList | Primitive | RenderableReactive | Renderable[];
37
33
 
38
34
  type RenderableReactive = Readonly<{
39
35
  [RENDERABLE]: typeof RENDERABLE_HTML_REACTIVE_ARRAY;
@@ -44,8 +40,6 @@ type RenderableReactive = Readonly<{
44
40
  ) => ReturnType<typeof html>;
45
41
  }>;
46
42
 
47
- type RenderableValue<T = unknown> = Attributes | Readonly<Attributes> | Readonly<Attributes[]> | Effect<T> | Primitive | RenderableReactive;
48
-
49
43
  type SlotGroup = {
50
44
  head: Element;
51
45
  tail: Element;
@@ -57,11 +51,13 @@ type Template = {
57
51
  literals: TemplateStringsArray;
58
52
  slots: {
59
53
  fn: typeof attributes.spread | typeof slot;
60
- path: {
61
- absolute: typeof firstChild[],
62
- parent: typeof firstChild[],
63
- relative: typeof firstChild[]
64
- };
54
+ // @see ./html/index.ts comments
55
+ path: typeof firstChild[];
56
+ // path: {
57
+ // absolute: typeof firstChild[],
58
+ // parent: typeof firstChild[],
59
+ // relative: typeof firstChild[]
60
+ // };
65
61
  slot: number;
66
62
  }[] | null;
67
63
  };
@@ -70,7 +66,7 @@ type Template = {
70
66
  export type {
71
67
  Attributes,
72
68
  Effect, Element,
73
- Renderable, RenderableReactive, RenderableValue,
69
+ Renderable, RenderableReactive,
74
70
  SlotGroup,
75
71
  Template
76
72
  };