@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.
- package/build/html/index.d.ts +3 -2
- package/build/html/index.js +9 -25
- package/build/html/parser.js +3 -12
- package/build/index.d.ts +1 -1
- package/build/types.d.ts +4 -10
- package/package.json +1 -1
- package/src/attributes.ts +3 -0
- package/src/html/index.ts +42 -29
- package/src/html/parser.ts +17 -14
- package/src/index.ts +1 -1
- package/src/types.ts +10 -14
package/build/html/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ReactiveArray } from '@esportsplus/reactivity';
|
|
2
|
-
import {
|
|
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: (
|
|
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;
|
package/build/html/index.js
CHANGED
|
@@ -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
|
|
9
|
-
for (let i =
|
|
10
|
-
let { fn, path, slot } = slots[i]
|
|
11
|
-
if (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
19
|
+
return clone;
|
|
36
20
|
};
|
|
37
21
|
html.reactive = (array, template) => {
|
|
38
22
|
return {
|
package/build/html/parser.js
CHANGED
|
@@ -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)
|
|
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
|
|
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
|
|
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> = () =>
|
|
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,
|
|
43
|
+
export type { Attributes, Effect, Element, Renderable, RenderableReactive, SlotGroup, Template };
|
package/package.json
CHANGED
package/src/attributes.ts
CHANGED
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 {
|
|
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
|
-
|
|
9
|
-
let { fragment, slots } = parser.parse(literals);
|
|
8
|
+
type Values<T = unknown> = Attributes | Attributes[] | Readonly<Attributes> | Readonly<Attributes[]> | Renderable<T>;
|
|
10
9
|
|
|
11
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
parent = node;
|
|
24
|
-
parentPath = nodePath;
|
|
23
|
+
// pp = path.parent,
|
|
24
|
+
// pr = path.relative;
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
parentPath = pp;
|
|
26
|
+
// if (pp !== parentPath) {
|
|
27
|
+
// if (pp === nodePath) {
|
|
28
|
+
// parent = node;
|
|
29
|
+
// parentPath = nodePath;
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
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 (
|
|
39
|
-
node =
|
|
40
|
-
nodePath = path.absolute;
|
|
52
|
+
if (nodePath !== path) {
|
|
53
|
+
node = clone;
|
|
41
54
|
|
|
42
|
-
for (let i = 0, n =
|
|
43
|
-
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
|
|
65
|
+
return clone;
|
|
53
66
|
};
|
|
54
67
|
|
|
55
68
|
html.reactive = <T>(array: ReactiveArray<T[]>, template: RenderableReactive['template']): RenderableReactive => {
|
package/src/html/parser.ts
CHANGED
|
@@ -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']
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
|
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> = () =>
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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,
|
|
69
|
+
Renderable, RenderableReactive,
|
|
74
70
|
SlotGroup,
|
|
75
71
|
Template
|
|
76
72
|
};
|