@estjs/template 0.0.10 → 0.0.12-beta.1
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/template.cjs.js +18 -17
- package/dist/template.cjs.js.map +1 -1
- package/dist/template.d.cts +65 -34
- package/dist/template.d.ts +65 -34
- package/dist/template.dev.cjs.js +217 -122
- package/dist/template.dev.esm.js +217 -123
- package/dist/template.esm.js +6 -6
- package/dist/template.esm.js.map +1 -1
- package/package.json +3 -3
package/dist/template.dev.esm.js
CHANGED
|
@@ -1,34 +1,75 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isSignal, useSignal, useEffect,
|
|
1
|
+
import { startsWith, isFunction, isString, isArray, isNil, capitalizeFirstLetter, coerceArray, isSymbol, isFalsy, kebabCase, isObject, escape } from '@estjs/shared';
|
|
2
|
+
import { signalObject, useReactive, isSignal, useSignal, useEffect, shallowSignal } from '@estjs/signal';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @estjs/template v0.0.
|
|
5
|
+
* @estjs/template v0.0.12-beta.1
|
|
6
6
|
* (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
|
|
7
7
|
* @license MIT
|
|
8
8
|
**/
|
|
9
|
-
|
|
10
|
-
var
|
|
11
|
-
|
|
9
|
+
var __defProp = Object.defineProperty;
|
|
10
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
13
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
14
|
+
var __spreadValues = (a, b) => {
|
|
15
|
+
for (var prop in b || (b = {}))
|
|
16
|
+
if (__hasOwnProp.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
if (__getOwnPropSymbols)
|
|
19
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
20
|
+
if (__propIsEnum.call(b, prop))
|
|
21
|
+
__defNormalProp(a, prop, b[prop]);
|
|
22
|
+
}
|
|
23
|
+
return a;
|
|
24
|
+
};
|
|
25
|
+
var _Hooks = class _Hooks {
|
|
26
|
+
constructor() {
|
|
27
|
+
this.hooks = {
|
|
28
|
+
mounted: /* @__PURE__ */ new Set(),
|
|
29
|
+
destroy: /* @__PURE__ */ new Set()
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
addEventListener() {
|
|
33
|
+
}
|
|
34
|
+
removeEventListener() {
|
|
35
|
+
}
|
|
36
|
+
addHook(hook, cb) {
|
|
37
|
+
var _a;
|
|
38
|
+
(_a = this.hooks[hook]) == null ? void 0 : _a.add(cb);
|
|
39
|
+
}
|
|
40
|
+
getContext(context) {
|
|
41
|
+
return _Hooks.context[context];
|
|
42
|
+
}
|
|
43
|
+
setContext(context, value) {
|
|
44
|
+
_Hooks.context[context] = value;
|
|
45
|
+
}
|
|
46
|
+
initRef() {
|
|
47
|
+
_Hooks.ref = this;
|
|
48
|
+
}
|
|
49
|
+
removeRef() {
|
|
50
|
+
_Hooks.ref = null;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
_Hooks.ref = null;
|
|
54
|
+
_Hooks.context = {};
|
|
55
|
+
var Hooks = _Hooks;
|
|
56
|
+
var ComponentNode = class extends Hooks {
|
|
57
|
+
constructor(template2, props, key) {
|
|
58
|
+
super();
|
|
12
59
|
this.template = template2;
|
|
13
60
|
this.props = props;
|
|
61
|
+
this.key = key;
|
|
14
62
|
this.proxyProps = {};
|
|
15
|
-
this.context = {};
|
|
16
63
|
this.emitter = /* @__PURE__ */ new Set();
|
|
17
64
|
this.mounted = false;
|
|
18
65
|
this.rootNode = null;
|
|
19
|
-
this.
|
|
20
|
-
mounted: /* @__PURE__ */ new Set(),
|
|
21
|
-
destroy: /* @__PURE__ */ new Set()
|
|
22
|
-
};
|
|
66
|
+
this.context = {};
|
|
23
67
|
this.trackMap = /* @__PURE__ */ new Map();
|
|
24
68
|
this.proxyProps = signalObject(
|
|
25
69
|
props,
|
|
26
|
-
(
|
|
70
|
+
(key2) => startsWith(key2, "on") || startsWith(key2, "update")
|
|
27
71
|
);
|
|
28
|
-
|
|
29
|
-
addEventListener() {
|
|
30
|
-
}
|
|
31
|
-
removeEventListener() {
|
|
72
|
+
this.key = this.key || props.key;
|
|
32
73
|
}
|
|
33
74
|
get firstChild() {
|
|
34
75
|
var _a, _b;
|
|
@@ -38,16 +79,6 @@ var _ComponentNode = class _ComponentNode {
|
|
|
38
79
|
var _a, _b;
|
|
39
80
|
return (_b = (_a = this.rootNode) == null ? void 0 : _a.isConnected) != null ? _b : false;
|
|
40
81
|
}
|
|
41
|
-
addHook(hook, cb) {
|
|
42
|
-
var _a;
|
|
43
|
-
(_a = this.hooks[hook]) == null ? void 0 : _a.add(cb);
|
|
44
|
-
}
|
|
45
|
-
getContext(context) {
|
|
46
|
-
return _ComponentNode.context[context];
|
|
47
|
-
}
|
|
48
|
-
setContext(context, value) {
|
|
49
|
-
_ComponentNode.context[context] = value;
|
|
50
|
-
}
|
|
51
82
|
inheritNode(node) {
|
|
52
83
|
this.context = node.context;
|
|
53
84
|
this.hooks = node.hooks;
|
|
@@ -66,9 +97,9 @@ var _ComponentNode = class _ComponentNode {
|
|
|
66
97
|
if (this.isConnected) {
|
|
67
98
|
return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
|
|
68
99
|
}
|
|
69
|
-
|
|
100
|
+
this.initRef();
|
|
70
101
|
this.rootNode = this.template(useReactive(this.proxyProps, ["children"]));
|
|
71
|
-
|
|
102
|
+
this.removeRef();
|
|
72
103
|
this.mounted = true;
|
|
73
104
|
const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
|
|
74
105
|
this.hooks.mounted.forEach((handler) => handler());
|
|
@@ -98,7 +129,7 @@ var _ComponentNode = class _ComponentNode {
|
|
|
98
129
|
return track;
|
|
99
130
|
}
|
|
100
131
|
patchProps(props) {
|
|
101
|
-
var _a, _b, _c, _d
|
|
132
|
+
var _a, _b, _c, _d;
|
|
102
133
|
for (const [key, prop] of Object.entries(props)) {
|
|
103
134
|
if (startsWith(key, "on") && ((_a = this.rootNode) == null ? void 0 : _a.nodes)) {
|
|
104
135
|
const event = key.slice(2).toLowerCase();
|
|
@@ -106,15 +137,11 @@ var _ComponentNode = class _ComponentNode {
|
|
|
106
137
|
const cleanup = addEventListener(this.rootNode.nodes[0], event, listener);
|
|
107
138
|
this.emitter.add(cleanup);
|
|
108
139
|
} else if (key === "ref") {
|
|
109
|
-
|
|
110
|
-
props[key].value = (_b = this.rootNode) == null ? void 0 : _b.nodes[0];
|
|
111
|
-
} else if (isFunction(prop)) {
|
|
112
|
-
props[key]((_c = this.rootNode) == null ? void 0 : _c.nodes[0]);
|
|
113
|
-
}
|
|
140
|
+
props[key].value = (_b = this.rootNode) == null ? void 0 : _b.nodes[0];
|
|
114
141
|
} else if (startsWith(key, "update")) {
|
|
115
142
|
props[key] = isSignal(prop) ? prop.value : prop;
|
|
116
143
|
} else if (key !== "children") {
|
|
117
|
-
const newValue = (
|
|
144
|
+
const newValue = (_d = (_c = this.proxyProps)[key]) != null ? _d : _c[key] = useSignal(prop);
|
|
118
145
|
const track = this.getNodeTrack(key);
|
|
119
146
|
track.cleanup = useEffect(() => {
|
|
120
147
|
newValue.value = isFunction(prop) ? prop() : prop;
|
|
@@ -124,12 +151,9 @@ var _ComponentNode = class _ComponentNode {
|
|
|
124
151
|
this.props = props;
|
|
125
152
|
}
|
|
126
153
|
};
|
|
127
|
-
_ComponentNode.ref = null;
|
|
128
|
-
_ComponentNode.context = {};
|
|
129
|
-
var ComponentNode = _ComponentNode;
|
|
130
154
|
|
|
131
155
|
// src/template.ts
|
|
132
|
-
function h(_template, props) {
|
|
156
|
+
function h(_template, props, key) {
|
|
133
157
|
if (isString(_template)) {
|
|
134
158
|
if (isHtmlTagName(_template)) {
|
|
135
159
|
_template = convertToHtmlTag(_template);
|
|
@@ -144,15 +168,17 @@ function h(_template, props) {
|
|
|
144
168
|
}
|
|
145
169
|
_template = template(_template);
|
|
146
170
|
}
|
|
147
|
-
return isFunction(_template) ? new ComponentNode(_template, props) : new TemplateNode(_template, props);
|
|
171
|
+
return isFunction(_template) ? new ComponentNode(_template, props, key) : new TemplateNode(_template, props, key);
|
|
172
|
+
}
|
|
173
|
+
function isComponent(node) {
|
|
174
|
+
return node instanceof ComponentNode;
|
|
148
175
|
}
|
|
149
176
|
function isJsxElement(node) {
|
|
150
177
|
return node instanceof ComponentNode || node instanceof TemplateNode;
|
|
151
178
|
}
|
|
152
179
|
function template(html) {
|
|
153
|
-
html = closeHtmlTags(html);
|
|
154
180
|
const template2 = document.createElement("template");
|
|
155
|
-
template2.innerHTML = html;
|
|
181
|
+
template2.innerHTML = closeHtmlTags(html);
|
|
156
182
|
return template2;
|
|
157
183
|
}
|
|
158
184
|
function Fragment(props) {
|
|
@@ -422,22 +448,28 @@ function mapKeys(children) {
|
|
|
422
448
|
return result;
|
|
423
449
|
}
|
|
424
450
|
function getKey(node, index) {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
451
|
+
if (isJsxElement(node)) {
|
|
452
|
+
const jsxKey = node.key;
|
|
453
|
+
if (jsxKey !== void 0 && jsxKey !== null) {
|
|
454
|
+
return String(jsxKey);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return `_$${index}$`;
|
|
428
458
|
}
|
|
429
459
|
|
|
430
460
|
// src/template-node.ts
|
|
431
461
|
var TemplateNode = class _TemplateNode {
|
|
432
|
-
constructor(template2, props) {
|
|
462
|
+
constructor(template2, props, key) {
|
|
433
463
|
this.template = template2;
|
|
434
464
|
this.props = props;
|
|
465
|
+
this.key = key;
|
|
435
466
|
this.treeMap = /* @__PURE__ */ new Map();
|
|
436
467
|
this.mounted = false;
|
|
437
468
|
this.nodes = [];
|
|
438
469
|
this.provides = {};
|
|
439
470
|
this.trackMap = /* @__PURE__ */ new Map();
|
|
440
471
|
this.parent = null;
|
|
472
|
+
this.key = this.key || props.key;
|
|
441
473
|
}
|
|
442
474
|
get firstChild() {
|
|
443
475
|
var _a;
|
|
@@ -450,24 +482,6 @@ var TemplateNode = class _TemplateNode {
|
|
|
450
482
|
}
|
|
451
483
|
removeEventListener() {
|
|
452
484
|
}
|
|
453
|
-
unmount() {
|
|
454
|
-
this.trackMap.forEach((track) => {
|
|
455
|
-
var _a, _b;
|
|
456
|
-
(_a = track.cleanup) == null ? void 0 : _a.call(track);
|
|
457
|
-
(_b = track.lastNodes) == null ? void 0 : _b.forEach((node) => {
|
|
458
|
-
if (track.isRoot) {
|
|
459
|
-
removeChild(node);
|
|
460
|
-
} else if (node instanceof _TemplateNode) {
|
|
461
|
-
node.unmount();
|
|
462
|
-
}
|
|
463
|
-
});
|
|
464
|
-
});
|
|
465
|
-
this.trackMap.clear();
|
|
466
|
-
this.treeMap.clear();
|
|
467
|
-
this.nodes.forEach((node) => removeChild(node));
|
|
468
|
-
this.nodes = [];
|
|
469
|
-
this.mounted = false;
|
|
470
|
-
}
|
|
471
485
|
mount(parent, before) {
|
|
472
486
|
var _a;
|
|
473
487
|
this.parent = parent;
|
|
@@ -490,6 +504,24 @@ var TemplateNode = class _TemplateNode {
|
|
|
490
504
|
this.mounted = true;
|
|
491
505
|
return this.nodes;
|
|
492
506
|
}
|
|
507
|
+
unmount() {
|
|
508
|
+
this.trackMap.forEach((track) => {
|
|
509
|
+
var _a, _b;
|
|
510
|
+
(_a = track.cleanup) == null ? void 0 : _a.call(track);
|
|
511
|
+
(_b = track.lastNodes) == null ? void 0 : _b.forEach((node) => {
|
|
512
|
+
if (track.isRoot) {
|
|
513
|
+
removeChild(node);
|
|
514
|
+
} else if (node instanceof _TemplateNode) {
|
|
515
|
+
node.unmount();
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
this.trackMap.clear();
|
|
520
|
+
this.treeMap.clear();
|
|
521
|
+
this.nodes.forEach((node) => removeChild(node));
|
|
522
|
+
this.nodes = [];
|
|
523
|
+
this.mounted = false;
|
|
524
|
+
}
|
|
493
525
|
mapNodeTree(parent, tree) {
|
|
494
526
|
let index = 1;
|
|
495
527
|
this.treeMap.set(0, parent);
|
|
@@ -560,11 +592,7 @@ var TemplateNode = class _TemplateNode {
|
|
|
560
592
|
});
|
|
561
593
|
}
|
|
562
594
|
} else if (attr === "ref") {
|
|
563
|
-
|
|
564
|
-
props[attr].value = node;
|
|
565
|
-
} else if (isFunction(props[attr])) {
|
|
566
|
-
props[attr](node);
|
|
567
|
-
}
|
|
595
|
+
props[attr].value = node;
|
|
568
596
|
} else if (startsWith(attr, "on")) {
|
|
569
597
|
const eventName = attr.slice(2).toLocaleLowerCase();
|
|
570
598
|
const track = this.getNodeTrack(`${key}:${attr}`);
|
|
@@ -621,85 +649,151 @@ function patchChild(track, parent, child, before) {
|
|
|
621
649
|
});
|
|
622
650
|
}
|
|
623
651
|
}
|
|
624
|
-
|
|
625
|
-
// src/hooks.ts
|
|
626
652
|
function onMount(cb) {
|
|
627
653
|
var _a;
|
|
628
654
|
throwIfOutsideComponent("onMounted");
|
|
629
|
-
(_a =
|
|
655
|
+
(_a = Hooks.ref) == null ? void 0 : _a.addHook("mounted", cb);
|
|
630
656
|
}
|
|
631
657
|
function onDestroy(cb) {
|
|
632
658
|
var _a;
|
|
633
659
|
throwIfOutsideComponent("onDestroy");
|
|
634
|
-
(_a =
|
|
660
|
+
(_a = Hooks.ref) == null ? void 0 : _a.addHook("destroy", cb);
|
|
635
661
|
}
|
|
636
|
-
function throwIfOutsideComponent(hook) {
|
|
637
|
-
if (!
|
|
662
|
+
function throwIfOutsideComponent(hook, key) {
|
|
663
|
+
if (!Hooks.ref) {
|
|
638
664
|
console.error(
|
|
639
|
-
`"${hook}" can only be called within the component function body
|
|
665
|
+
`"${hook}"(key: ${isSymbol(key) ? key.toString() : key}) can only be called within the component function body
|
|
640
666
|
and cannot be used in asynchronous or deferred calls.`
|
|
641
667
|
);
|
|
642
668
|
}
|
|
643
669
|
}
|
|
644
670
|
function useProvide(key, value) {
|
|
645
671
|
var _a;
|
|
646
|
-
throwIfOutsideComponent("useProvide");
|
|
647
|
-
(_a =
|
|
672
|
+
throwIfOutsideComponent("useProvide", key);
|
|
673
|
+
(_a = Hooks.ref) == null ? void 0 : _a.setContext(key, value);
|
|
648
674
|
}
|
|
649
675
|
function useInject(key, defaultValue) {
|
|
650
676
|
var _a;
|
|
651
|
-
throwIfOutsideComponent("useInject");
|
|
652
|
-
return ((_a =
|
|
653
|
-
}
|
|
654
|
-
function
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
677
|
+
throwIfOutsideComponent("useInject", key);
|
|
678
|
+
return ((_a = Hooks.ref) == null ? void 0 : _a.getContext(key)) || defaultValue;
|
|
679
|
+
}
|
|
680
|
+
function useRef() {
|
|
681
|
+
const ref = shallowSignal(null);
|
|
682
|
+
return ref;
|
|
683
|
+
}
|
|
684
|
+
function generateAttributes(props) {
|
|
685
|
+
return Object.entries(props).map(([key, value]) => {
|
|
686
|
+
if (key === "children" || isFunction(value)) return "";
|
|
687
|
+
return `${key}="${escape(String(value))}"`;
|
|
688
|
+
}).filter(Boolean).join(" ");
|
|
689
|
+
}
|
|
690
|
+
function normalizeProps(props) {
|
|
691
|
+
Object.keys(props).forEach((propKey) => {
|
|
692
|
+
if (isFunction(props[propKey])) {
|
|
693
|
+
delete props[propKey];
|
|
694
|
+
}
|
|
695
|
+
if (isSignal(props[propKey])) {
|
|
696
|
+
props[propKey] = props[propKey].value;
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
function handleChildResult(result, prop, key, tmpl, childNodesMap, path) {
|
|
701
|
+
if (isSignal(result)) {
|
|
702
|
+
tmpl.template += result.value;
|
|
703
|
+
} else if (result instanceof ServerNode) {
|
|
704
|
+
const mapKey = path ? String(path) : `${key}`;
|
|
705
|
+
if (!childNodesMap[mapKey]) childNodesMap[mapKey] = [];
|
|
706
|
+
const childResult = result.mount();
|
|
707
|
+
childNodesMap[mapKey].push(
|
|
708
|
+
isFunction(childResult) ? childResult(prop) : isSignal(childResult) ? childResult.value : childResult
|
|
709
|
+
);
|
|
710
|
+
} else {
|
|
711
|
+
tmpl.template += isFunction(result) ? result(prop) : String(result);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
var ServerNode = class _ServerNode extends Hooks {
|
|
715
|
+
constructor(template2, props = {}, key) {
|
|
716
|
+
super();
|
|
717
|
+
this.template = template2;
|
|
718
|
+
this.props = props;
|
|
719
|
+
this.key = key;
|
|
720
|
+
this.childNodesMap = {};
|
|
721
|
+
this.processedTemplates = {};
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Mount and render the component
|
|
725
|
+
*/
|
|
726
|
+
mount() {
|
|
727
|
+
this.initRef();
|
|
728
|
+
const output = this.render();
|
|
729
|
+
this.removeRef();
|
|
730
|
+
return output;
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Initialize template entries and props
|
|
734
|
+
*/
|
|
735
|
+
initTemplates() {
|
|
736
|
+
const templateCollection = Array.isArray(this.template) ? this.template.reduce((acc, tmpl, index) => {
|
|
737
|
+
acc[index + 1] = { template: tmpl };
|
|
738
|
+
return acc;
|
|
739
|
+
}, {}) : this.template;
|
|
740
|
+
if (isObject(templateCollection)) {
|
|
741
|
+
Object.entries(templateCollection).forEach(([key, tmpl]) => {
|
|
742
|
+
const prop = __spreadValues({}, this.props[key]);
|
|
743
|
+
normalizeProps(prop);
|
|
676
744
|
if (prop.children) {
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
745
|
+
prop.children.forEach((item) => {
|
|
746
|
+
const [child, path] = isArray(item) ? item : [item, null];
|
|
747
|
+
if (isFunction(child)) {
|
|
748
|
+
const result = child(prop);
|
|
749
|
+
handleChildResult(result, prop, key, tmpl, this.childNodesMap, path);
|
|
750
|
+
} else {
|
|
751
|
+
tmpl.template += isSignal(child) ? child.value : String(child);
|
|
752
|
+
}
|
|
753
|
+
});
|
|
682
754
|
}
|
|
683
|
-
|
|
684
|
-
|
|
755
|
+
this.processedTemplates[key] = {
|
|
756
|
+
template: tmpl.template,
|
|
757
|
+
props: prop
|
|
758
|
+
};
|
|
759
|
+
});
|
|
685
760
|
}
|
|
686
761
|
}
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
762
|
+
/**
|
|
763
|
+
* Render component and its children into a string
|
|
764
|
+
*/
|
|
765
|
+
render() {
|
|
766
|
+
if (isFunction(this.template)) {
|
|
767
|
+
const root = this.template(this.props);
|
|
768
|
+
return root instanceof _ServerNode ? root.mount() : String(root);
|
|
691
769
|
}
|
|
692
|
-
if (
|
|
693
|
-
|
|
770
|
+
if (this.template instanceof _ServerNode) {
|
|
771
|
+
return this.template.mount();
|
|
694
772
|
}
|
|
695
|
-
|
|
696
|
-
|
|
773
|
+
this.initTemplates();
|
|
774
|
+
return Object.entries(this.processedTemplates).map(([key, { template: template2, props }]) => {
|
|
775
|
+
let content = template2;
|
|
776
|
+
if (props && Object.keys(props).length > 0) {
|
|
777
|
+
content += ` ${generateAttributes(props)}`;
|
|
778
|
+
}
|
|
779
|
+
if (this.childNodesMap[key]) {
|
|
780
|
+
content = content.replace("<!>", this.renderChildren(this.childNodesMap[key]));
|
|
781
|
+
}
|
|
782
|
+
return content;
|
|
783
|
+
}).join("");
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Render child nodes into a string
|
|
787
|
+
*/
|
|
788
|
+
renderChildren(children) {
|
|
789
|
+
return coerceArray(children).map(String).join("");
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
function ssg(component, props) {
|
|
793
|
+
return new ServerNode(component, props);
|
|
697
794
|
}
|
|
698
795
|
function renderToString(component, props) {
|
|
699
|
-
return
|
|
700
|
-
}
|
|
701
|
-
function renderSSG(component, root, props = {}) {
|
|
702
|
-
root.innerHTML = renderTemplate(component, props);
|
|
796
|
+
return ssg(component, props).mount();
|
|
703
797
|
}
|
|
704
798
|
|
|
705
|
-
export { ComponentNode, Fragment, TemplateNode, h, isJsxElement, nextTick, onDestroy, onMount,
|
|
799
|
+
export { ComponentNode, Fragment, TemplateNode, h, isComponent, isJsxElement, nextTick, onDestroy, onMount, renderToString, ssg, template, useInject, useProvide, useRef };
|
package/dist/template.esm.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isSignal, useSignal, useEffect,
|
|
1
|
+
import { startsWith, isFunction, isString, isArray, isNil, capitalizeFirstLetter, coerceArray, isSymbol, isFalsy, kebabCase, isObject, escape } from '@estjs/shared';
|
|
2
|
+
import { signalObject, useReactive, isSignal, useSignal, useEffect, shallowSignal } from '@estjs/signal';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @estjs/template v0.0.
|
|
5
|
+
* @estjs/template v0.0.12-beta.1
|
|
6
6
|
* (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
|
|
7
7
|
* @license MIT
|
|
8
8
|
**/
|
|
9
|
-
var N=class N{constructor(e,n){this.template=e;this.props=n;this.proxyProps={};this.context={};this.emitter=new Set;this.mounted=!1;this.rootNode=null;this.hooks={mounted:new Set,destroy:new Set};this.trackMap=new Map;this.proxyProps=signalObject(n,o=>startsWith(o,"on")||startsWith(o,"update"));}addEventListener(){}removeEventListener(){}get firstChild(){var e,n;return (n=(e=this.rootNode)==null?void 0:e.firstChild)!=null?n:null}get isConnected(){var e,n;return (n=(e=this.rootNode)==null?void 0:e.isConnected)!=null?n:!1}addHook(e,n){var o;(o=this.hooks[e])==null||o.add(n);}getContext(e){return N.context[e]}setContext(e,n){N.context[e]=n;}inheritNode(e){this.context=e.context,this.hooks=e.hooks,Object.assign(this.proxyProps,e.proxyProps),this.rootNode=e.rootNode,this.trackMap=e.trackMap;let n=this.props;this.props=e.props,this.patchProps(n);}mount(e,n){var r,i,c,s;if(!isFunction(this.template))throw new Error("Template must be a function");if(this.isConnected)return (i=(r=this.rootNode)==null?void 0:r.mount(e,n))!=null?i:[];N.ref=this,this.rootNode=this.template(useReactive(this.proxyProps,["children"])),N.ref=null,this.mounted=!0;let o=(s=(c=this.rootNode)==null?void 0:c.mount(e,n))!=null?s:[];return this.hooks.mounted.forEach(a=>a()),this.patchProps(this.props),o}unmount(){var e;this.hooks.destroy.forEach(n=>n()),Object.values(this.hooks).forEach(n=>n.clear()),(e=this.rootNode)==null||e.unmount(),this.rootNode=null,this.proxyProps={},this.mounted=!1,this.emitter.forEach(n=>n());}getNodeTrack(e,n){let o=this.trackMap.get(e);return o||(o={cleanup:()=>{}},this.trackMap.set(e,o)),n||o.cleanup(),o}patchProps(e){var n,o,r,i,c;for(let[s,a]of Object.entries(e))if(startsWith(s,"on")&&((n=this.rootNode)!=null&&n.nodes)){let d=s.slice(2).toLowerCase(),l=a,u=m(this.rootNode.nodes[0],d,l);this.emitter.add(u);}else if(s==="ref")isSignal(a)?e[s].value=(o=this.rootNode)==null?void 0:o.nodes[0]:isFunction(a)&&e[s]((r=this.rootNode)==null?void 0:r.nodes[0]);else if(startsWith(s,"update"))e[s]=isSignal(a)?a.value:a;else if(s!=="children"){let d=(c=(i=this.proxyProps)[s])!=null?c:i[s]=useSignal(a),l=this.getNodeTrack(s);l.cleanup=useEffect(()=>{d.value=isFunction(a)?a():a;});}this.props=e;}};N.ref=null,N.context={};var f=N;function ie(t,e){return isString(t)&&(O(t)&&(t=X(t),e={1:e}),t===""&&(e={0:e}),t=F(t)),isFunction(t)?new f(t,e):new b(t,e)}function h(t){return t instanceof f||t instanceof b}function F(t){t=I(t);let e=document.createElement("template");return e.innerHTML=t,e}function se(t){return t.children}var D="area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr",ae="a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,nav,nobr,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rb,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp";function w(t){if(h(t)||t instanceof Node)return t;let e=isFalsy(t)?"":String(t);return document.createTextNode(e)}function g(t,e,n=null){let o=h(n)?n.firstChild:n;h(e)?e.mount(t,o):o?o.before(e):t.append(e);}function x(t){h(t)?t.unmount():t.parentNode&&t.remove();}function A(t,e,n){g(t,e,n),x(n);}function H(t,e,n){if(e==="class"){typeof n=="string"?t.className=n:Array.isArray(n)?t.className=n.join(" "):n&&typeof n=="object"&&(t.className=Object.entries(n).reduce((o,[r,i])=>o+(i?` ${r}`:""),"").trim());return}if(e==="style"){if(typeof n=="string")t.style.cssText=n;else if(n&&typeof n=="object"){let o=n;Object.keys(o).forEach(r=>{t.style.setProperty(kebabCase(r),String(o[r]));});}return}isFalsy(n)?t.removeAttribute(e):n===!0?t.setAttribute(e,""):t instanceof HTMLInputElement?t.value=String(n):t.setAttribute(e,String(n));}function q(t,e){if(t instanceof HTMLInputElement)switch(t.type){case"checkbox":return m(t,"change",()=>{e(!!t.checked);});case"date":return m(t,"change",()=>{e(t.value?t.value:"");});case"file":return m(t,"change",()=>{t.files&&e(t.files);});case"number":return m(t,"input",()=>{let n=Number.parseFloat(t.value);e(Number.isNaN(n)?"":String(n));});case"radio":return m(t,"change",()=>{e(t.checked?t.value:"");});case"text":return m(t,"input",()=>{e(t.value);})}if(t instanceof HTMLSelectElement)return m(t,"change",()=>{e(t.value);});if(t instanceof HTMLTextAreaElement)return m(t,"input",()=>{e(t.value);})}var V=Promise.resolve();function le(t){return t?V.then(t):V}function m(t,e,n){return t.addEventListener(e,n),()=>t.removeEventListener(e,n)}function I(t){let e=D.split(","),n=[],o=[],r=/<\/?([\da-z-]+)([^>]*)>/gi,i=0;for(;;){let c=r.exec(t);if(!c)break;let[s,a]=c,d=s[1]==="/";if(o.push(t.slice(i,c.index)),i=c.index+s.length,d){for(;n.length>0&&n[n.length-1]!==a;){let l=n.pop();l&&o.push(`</${l}>`);}n.length>0&&n.pop();}else e.includes(a)||n.push(a);o.push(s);}for(o.push(t.slice(i));n.length>0;){let c=n.pop();c&&o.push(`</${c}>`);}return o.join("")}function O(t){return ae.split(",").includes(t)}function X(t){return D.split(",").includes(t)?`<${t}/>`:`<${t}></${t}>`}function z(t,e,n,o){let r=new Map,i=Array.from(e.values()),c=i.length;if(e.size>0&&n.length===0){if(t.childNodes.length===c+(o?1:0))t.innerHTML="",o&&g(t,o);else {let l=document.createRange(),u=i[0],p=h(u)?u.firstChild:u;l.setStartBefore(p),o?l.setEndBefore(o):l.setEndAfter(t),l.deleteContents();}return i.forEach(l=>{h(l)&&l.unmount();}),r}let s=[],a=ue(n),d=0;for(let[l,u]of n.entries()){let p=i[d],E=k(p,l);for(;p&&!a.has(E);)x(p),e.delete(E),p=i[++d],E=k(p,l);let P=k(u,l),L=e.get(P);if(L&&(u=de(t,L,u)),p)if(p===L)d++;else {let R=document.createComment("");g(t,R,p),s.push([R,u]);}else g(t,u,o);r.set(P,u);}return s.forEach(([l,u])=>A(t,u,l)),e.forEach((l,u)=>{l.isConnected&&!r.has(u)&&x(l);}),r}function de(t,e,n){return e===n?e:h(e)&&h(n)&&e.template===n.template?(n.inheritNode(e),n):e instanceof Text&&n instanceof Text?(e.textContent!==n.textContent&&(e.textContent=n.textContent),e):(A(t,n,e),n)}function ue(t){let e=new Map;for(let[n,o]of t.entries()){let r=k(o,n);e.set(r,o);}return e}function k(t,e){let n=t instanceof Element?t.id:void 0,o=n===""?void 0:n;return o!=null?o:`_$${e}$`}var b=class t{constructor(e,n){this.template=e;this.props=n;this.treeMap=new Map;this.mounted=!1;this.nodes=[];this.provides={};this.trackMap=new Map;this.parent=null;}get firstChild(){var e;return (e=this.nodes[0])!=null?e:null}get isConnected(){return this.mounted}addEventListener(){}removeEventListener(){}unmount(){this.trackMap.forEach(e=>{var n,o;(n=e.cleanup)==null||n.call(e),(o=e.lastNodes)==null||o.forEach(r=>{e.isRoot?x(r):r instanceof t&&r.unmount();});}),this.trackMap.clear(),this.treeMap.clear(),this.nodes.forEach(e=>x(e)),this.nodes=[],this.mounted=!1;}mount(e,n){var i;if(this.parent=e,this.isConnected)return this.nodes.forEach(c=>g(e,c,n)),this.nodes;let o=this.template.content.cloneNode(!0),r=o.firstChild;return (i=r==null?void 0:r.hasAttribute)!=null&&i.call(r,"_svg_")&&(r.remove(),r==null||r.childNodes.forEach(c=>{o.append(c);})),this.nodes=Array.from(o.childNodes),this.mapNodeTree(e,o),g(e,o,n),this.patchNodes(this.props),this.mounted=!0,this.nodes}mapNodeTree(e,n){let o=1;this.treeMap.set(0,e);let r=i=>{i.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&this.treeMap.set(o++,i);let c=i.firstChild;for(;c;)r(c),c=c.nextSibling;};r(n);}patchNodes(e){for(let n in e){let o=Number(n),r=this.treeMap.get(o);if(r){let i=this.props[n];this.patchNode(n,r,i,o===0);}}this.props=e;}getNodeTrack(e,n,o){var i;let r=this.trackMap.get(e);return r||(r={cleanup:()=>{}},n&&(r.lastNodes=new Map),o&&(r.isRoot=!0),this.trackMap.set(e,r)),(i=r.cleanup)==null||i.call(r),r}inheritNode(e){this.mounted=e.mounted,this.nodes=e.nodes,this.trackMap=e.trackMap,this.treeMap=e.treeMap;let n=this.props;this.props=e.props,this.patchNodes(n);}patchNode(e,n,o,r){for(let i in o)if(i==="children"&&o.children)if(isArray(o.children))o.children.filter(Boolean).forEach((c,s)=>{var E;let[a,d]=isArray(c)?c:[c,null],l=isNil(d)?null:(E=this.treeMap.get(d))!=null?E:null,u=`${e}:${i}:${s}`,p=this.getNodeTrack(u,!0,r);Y(p,n,a,l);});else {let c=`${e}:${i}:0`,s=this.getNodeTrack(c,!0,r);Y(s,n,o.children,null);}else if(i==="ref")isSignal(o[i])?o[i].value=n:isFunction(o[i])&&o[i](n);else if(startsWith(i,"on")){let c=i.slice(2).toLocaleLowerCase(),s=this.getNodeTrack(`${e}:${i}`),a=o[i];s.cleanup=m(n,c,a);}else if(!startsWith(i,"update")){let c=this.getNodeTrack(`${e}:${i}`),s=o[i],a=isSignal(s)?s:useSignal(s);Q(c,n,i,a.value);let d=useEffect(()=>{a.value=isSignal(s)?s.value:s,Q(c,n,i,a.value);}),l,u=`update${capitalizeFirstLetter(i)}`;o[u]&&(l=q(n,p=>{o[u](p);})),c.cleanup=()=>{d&&d(),l&&l();};}}};function Q(t,e,n,o){let r=e;r.setAttribute&&(isFunction(o)?t.cleanup=useEffect(()=>{H(r,n,o());}):H(r,n,o));}function Y(t,e,n,o){isFunction(n)?t.cleanup=useEffect(()=>{let r=coerceArray(n()).map(w);t.lastNodes=z(e,t.lastNodes,r,o);}):coerceArray(n).forEach((r,i)=>{let c=w(r);t.lastNodes.set(String(i),c),g(e,c,o);});}function he(t){var e;C("onMounted"),(e=f.ref)==null||e.addHook("mounted",t);}function ge(t){var e;C("onDestroy"),(e=f.ref)==null||e.addHook("destroy",t);}function C(t){f.ref||console.error(`"${t}" can only be called within the component function body
|
|
10
|
-
and cannot be used in asynchronous or deferred calls.`);}function
|
|
9
|
+
var re=Object.defineProperty;var I=Object.getOwnPropertySymbols;var ie=Object.prototype.hasOwnProperty,se=Object.prototype.propertyIsEnumerable;var O=(t,n,e)=>n in t?re(t,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[n]=e,J=(t,n)=>{for(var e in n||(n={}))ie.call(n,e)&&O(t,e,n[e]);if(I)for(var e of I(n))se.call(n,e)&&O(t,e,n[e]);return t};var N=class N{constructor(){this.hooks={mounted:new Set,destroy:new Set};}addEventListener(){}removeEventListener(){}addHook(n,e){var o;(o=this.hooks[n])==null||o.add(e);}getContext(n){return N.context[n]}setContext(n,e){N.context[n]=e;}initRef(){N.ref=this;}removeRef(){N.ref=null;}};N.ref=null,N.context={};var h=N,y=class extends h{constructor(e,o,r){super();this.template=e;this.props=o;this.key=r;this.proxyProps={};this.emitter=new Set;this.mounted=!1;this.rootNode=null;this.context={};this.trackMap=new Map;this.proxyProps=signalObject(o,i=>startsWith(i,"on")||startsWith(i,"update")),this.key=this.key||o.key;}get firstChild(){var e,o;return (o=(e=this.rootNode)==null?void 0:e.firstChild)!=null?o:null}get isConnected(){var e,o;return (o=(e=this.rootNode)==null?void 0:e.isConnected)!=null?o:!1}inheritNode(e){this.context=e.context,this.hooks=e.hooks,Object.assign(this.proxyProps,e.proxyProps),this.rootNode=e.rootNode,this.trackMap=e.trackMap;let o=this.props;this.props=e.props,this.patchProps(o);}mount(e,o){var i,s,a,l;if(!isFunction(this.template))throw new Error("Template must be a function");if(this.isConnected)return (s=(i=this.rootNode)==null?void 0:i.mount(e,o))!=null?s:[];this.initRef(),this.rootNode=this.template(useReactive(this.proxyProps,["children"])),this.removeRef(),this.mounted=!0;let r=(l=(a=this.rootNode)==null?void 0:a.mount(e,o))!=null?l:[];return this.hooks.mounted.forEach(d=>d()),this.patchProps(this.props),r}unmount(){var e;this.hooks.destroy.forEach(o=>o()),Object.values(this.hooks).forEach(o=>o.clear()),(e=this.rootNode)==null||e.unmount(),this.rootNode=null,this.proxyProps={},this.mounted=!1,this.emitter.forEach(o=>o());}getNodeTrack(e,o){let r=this.trackMap.get(e);return r||(r={cleanup:()=>{}},this.trackMap.set(e,r)),o||r.cleanup(),r}patchProps(e){var o,r,i,s;for(let[a,l]of Object.entries(e))if(startsWith(a,"on")&&((o=this.rootNode)!=null&&o.nodes)){let d=a.slice(2).toLowerCase(),c=l,u=m(this.rootNode.nodes[0],d,c);this.emitter.add(u);}else if(a==="ref")e[a].value=(r=this.rootNode)==null?void 0:r.nodes[0];else if(startsWith(a,"update"))e[a]=isSignal(l)?l.value:l;else if(a!=="children"){let d=(s=(i=this.proxyProps)[a])!=null?s:i[a]=useSignal(l),c=this.getNodeTrack(a);c.cleanup=useEffect(()=>{d.value=isFunction(l)?l():l;});}this.props=e;}};function fe(t,n,e){return isString(t)&&(B(t)&&(t=D(t),n={1:n}),t===""&&(n={0:n}),t=X(t)),isFunction(t)?new y(t,n,e):new E(t,n,e)}function he(t){return t instanceof y}function f(t){return t instanceof y||t instanceof E}function X(t){let n=document.createElement("template");return n.innerHTML=V(t),n}function ge(t){return t.children}var W="area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr",ye="a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,nav,nobr,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rb,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp";function j(t){if(f(t)||t instanceof Node)return t;let n=isFalsy(t)?"":String(t);return document.createTextNode(n)}function g(t,n,e=null){let o=f(e)?e.firstChild:e;f(n)?n.mount(t,o):o?o.before(n):t.append(n);}function b(t){f(t)?t.unmount():t.parentNode&&t.remove();}function H(t,n,e){g(t,n,e),b(e);}function R(t,n,e){if(n==="class"){typeof e=="string"?t.className=e:Array.isArray(e)?t.className=e.join(" "):e&&typeof e=="object"&&(t.className=Object.entries(e).reduce((o,[r,i])=>o+(i?` ${r}`:""),"").trim());return}if(n==="style"){if(typeof e=="string")t.style.cssText=e;else if(e&&typeof e=="object"){let o=e;Object.keys(o).forEach(r=>{t.style.setProperty(kebabCase(r),String(o[r]));});}return}isFalsy(e)?t.removeAttribute(n):e===!0?t.setAttribute(n,""):t instanceof HTMLInputElement?t.value=String(e):t.setAttribute(n,String(e));}function G(t,n){if(t instanceof HTMLInputElement)switch(t.type){case"checkbox":return m(t,"change",()=>{n(!!t.checked);});case"date":return m(t,"change",()=>{n(t.value?t.value:"");});case"file":return m(t,"change",()=>{t.files&&n(t.files);});case"number":return m(t,"input",()=>{let e=Number.parseFloat(t.value);n(Number.isNaN(e)?"":String(e));});case"radio":return m(t,"change",()=>{n(t.checked?t.value:"");});case"text":return m(t,"input",()=>{n(t.value);})}if(t instanceof HTMLSelectElement)return m(t,"change",()=>{n(t.value);});if(t instanceof HTMLTextAreaElement)return m(t,"input",()=>{n(t.value);})}var z=Promise.resolve();function Te(t){return t?z.then(t):z}function m(t,n,e){return t.addEventListener(n,e),()=>t.removeEventListener(n,e)}function V(t){let n=W.split(","),e=[],o=[],r=/<\/?([\da-z-]+)([^>]*)>/gi,i=0;for(;;){let s=r.exec(t);if(!s)break;let[a,l]=s,d=a[1]==="/";if(o.push(t.slice(i,s.index)),i=s.index+a.length,d){for(;e.length>0&&e[e.length-1]!==l;){let c=e.pop();c&&o.push(`</${c}>`);}e.length>0&&e.pop();}else n.includes(l)||e.push(l);o.push(a);}for(o.push(t.slice(i));e.length>0;){let s=e.pop();s&&o.push(`</${s}>`);}return o.join("")}function B(t){return ye.split(",").includes(t)}function D(t){return W.split(",").includes(t)?`<${t}/>`:`<${t}></${t}>`}function U(t,n,e,o){let r=new Map,i=Array.from(n.values()),s=i.length;if(n.size>0&&e.length===0){if(t.childNodes.length===s+(o?1:0))t.innerHTML="",o&&g(t,o);else {let c=document.createRange(),u=i[0],p=f(u)?u.firstChild:u;c.setStartBefore(p),o?c.setEndBefore(o):c.setEndAfter(t),c.deleteContents();}return i.forEach(c=>{f(c)&&c.unmount();}),r}let a=[],l=be(e),d=0;for(let[c,u]of e.entries()){let p=i[d],x=M(p,c);for(;p&&!l.has(x);)b(p),n.delete(x),p=i[++d],x=M(p,c);let $=M(u,c),A=n.get($);if(A&&(u=ve(t,A,u)),p)if(p===A)d++;else {let K=document.createComment("");g(t,K,p),a.push([K,u]);}else g(t,u,o);r.set($,u);}return a.forEach(([c,u])=>H(t,u,c)),n.forEach((c,u)=>{c.isConnected&&!r.has(u)&&b(c);}),r}function ve(t,n,e){return n===e?n:f(n)&&f(e)&&n.template===e.template?(e.inheritNode(n),e):n instanceof Text&&e instanceof Text?(n.textContent!==e.textContent&&(n.textContent=e.textContent),n):(H(t,e,n),e)}function be(t){let n=new Map;for(let[e,o]of t.entries()){let r=M(o,e);n.set(r,o);}return n}function M(t,n){if(f(t)){let e=t.key;if(e!=null)return String(e)}return `_$${n}$`}var E=class t{constructor(n,e,o){this.template=n;this.props=e;this.key=o;this.treeMap=new Map;this.mounted=!1;this.nodes=[];this.provides={};this.trackMap=new Map;this.parent=null;this.key=this.key||e.key;}get firstChild(){var n;return (n=this.nodes[0])!=null?n:null}get isConnected(){return this.mounted}addEventListener(){}removeEventListener(){}mount(n,e){var i;if(this.parent=n,this.isConnected)return this.nodes.forEach(s=>g(n,s,e)),this.nodes;let o=this.template.content.cloneNode(!0),r=o.firstChild;return (i=r==null?void 0:r.hasAttribute)!=null&&i.call(r,"_svg_")&&(r.remove(),r==null||r.childNodes.forEach(s=>{o.append(s);})),this.nodes=Array.from(o.childNodes),this.mapNodeTree(n,o),g(n,o,e),this.patchNodes(this.props),this.mounted=!0,this.nodes}unmount(){this.trackMap.forEach(n=>{var e,o;(e=n.cleanup)==null||e.call(n),(o=n.lastNodes)==null||o.forEach(r=>{n.isRoot?b(r):r instanceof t&&r.unmount();});}),this.trackMap.clear(),this.treeMap.clear(),this.nodes.forEach(n=>b(n)),this.nodes=[],this.mounted=!1;}mapNodeTree(n,e){let o=1;this.treeMap.set(0,n);let r=i=>{i.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&this.treeMap.set(o++,i);let s=i.firstChild;for(;s;)r(s),s=s.nextSibling;};r(e);}patchNodes(n){for(let e in n){let o=Number(e),r=this.treeMap.get(o);if(r){let i=this.props[e];this.patchNode(e,r,i,o===0);}}this.props=n;}getNodeTrack(n,e,o){var i;let r=this.trackMap.get(n);return r||(r={cleanup:()=>{}},e&&(r.lastNodes=new Map),o&&(r.isRoot=!0),this.trackMap.set(n,r)),(i=r.cleanup)==null||i.call(r),r}inheritNode(n){this.mounted=n.mounted,this.nodes=n.nodes,this.trackMap=n.trackMap,this.treeMap=n.treeMap;let e=this.props;this.props=n.props,this.patchNodes(e);}patchNode(n,e,o,r){for(let i in o)if(i==="children"&&o.children)if(isArray(o.children))o.children.filter(Boolean).forEach((s,a)=>{var x;let[l,d]=isArray(s)?s:[s,null],c=isNil(d)?null:(x=this.treeMap.get(d))!=null?x:null,u=`${n}:${i}:${a}`,p=this.getNodeTrack(u,!0,r);te(p,e,l,c);});else {let s=`${n}:${i}:0`,a=this.getNodeTrack(s,!0,r);te(a,e,o.children,null);}else if(i==="ref")o[i].value=e;else if(startsWith(i,"on")){let s=i.slice(2).toLocaleLowerCase(),a=this.getNodeTrack(`${n}:${i}`),l=o[i];a.cleanup=m(e,s,l);}else if(!startsWith(i,"update")){let s=this.getNodeTrack(`${n}:${i}`),a=o[i],l=isSignal(a)?a:useSignal(a);ee(s,e,i,l.value);let d=useEffect(()=>{l.value=isSignal(a)?a.value:a,ee(s,e,i,l.value);}),c,u=`update${capitalizeFirstLetter(i)}`;o[u]&&(c=G(e,p=>{o[u](p);})),s.cleanup=()=>{d&&d(),c&&c();};}}};function ee(t,n,e,o){let r=n;r.setAttribute&&(isFunction(o)?t.cleanup=useEffect(()=>{R(r,e,o());}):R(r,e,o));}function te(t,n,e,o){isFunction(e)?t.cleanup=useEffect(()=>{let r=coerceArray(e()).map(j);t.lastNodes=U(n,t.lastNodes,r,o);}):coerceArray(e).forEach((r,i)=>{let s=j(r);t.lastNodes.set(String(i),s),g(n,s,o);});}function Se(t){var n;S("onMounted"),(n=h.ref)==null||n.addHook("mounted",t);}function we(t){var n;S("onDestroy"),(n=h.ref)==null||n.addHook("destroy",t);}function S(t,n){h.ref||console.error(`"${t}"(key: ${isSymbol(n)?n.toString():n}) can only be called within the component function body
|
|
10
|
+
and cannot be used in asynchronous or deferred calls.`);}function Le(t,n){var e;S("useProvide",t),(e=h.ref)==null||e.setContext(t,n);}function Ae(t,n){var e;return S("useInject",t),((e=h.ref)==null?void 0:e.getContext(t))||n}function je(){return shallowSignal(null)}function Ke(t){return Object.entries(t).map(([n,e])=>n==="children"||isFunction(e)?"":`${n}="${escape(String(e))}"`).filter(Boolean).join(" ")}function Ie(t){Object.keys(t).forEach(n=>{isFunction(t[n])&&delete t[n],isSignal(t[n])&&(t[n]=t[n].value);});}function Oe(t,n,e,o,r,i){if(isSignal(t))o.template+=t.value;else if(t instanceof L){let s=i?String(i):`${e}`;r[s]||(r[s]=[]);let a=t.mount();r[s].push(isFunction(a)?a(n):isSignal(a)?a.value:a);}else o.template+=isFunction(t)?t(n):String(t);}var L=class t extends h{constructor(e,o={},r){super();this.template=e;this.props=o;this.key=r;this.childNodesMap={};this.processedTemplates={};}mount(){this.initRef();let e=this.render();return this.removeRef(),e}initTemplates(){let e=Array.isArray(this.template)?this.template.reduce((o,r,i)=>(o[i+1]={template:r},o),{}):this.template;isObject(e)&&Object.entries(e).forEach(([o,r])=>{let i=J({},this.props[o]);Ie(i),i.children&&i.children.forEach(s=>{let[a,l]=isArray(s)?s:[s,null];if(isFunction(a)){let d=a(i);Oe(d,i,o,r,this.childNodesMap,l);}else r.template+=isSignal(a)?a.value:String(a);}),this.processedTemplates[o]={template:r.template,props:i};});}render(){if(isFunction(this.template)){let e=this.template(this.props);return e instanceof t?e.mount():String(e)}return this.template instanceof t?this.template.mount():(this.initTemplates(),Object.entries(this.processedTemplates).map(([e,{template:o,props:r}])=>{let i=o;return r&&Object.keys(r).length>0&&(i+=` ${Ke(r)}`),this.childNodesMap[e]&&(i=i.replace("<!>",this.renderChildren(this.childNodesMap[e]))),i}).join(""))}renderChildren(e){return coerceArray(e).map(String).join("")}};function oe(t,n){return new L(t,n)}function Je(t,n){return oe(t,n).mount()}
|
|
11
11
|
|
|
12
|
-
export {
|
|
12
|
+
export { y as ComponentNode, ge as Fragment, E as TemplateNode, fe as h, he as isComponent, f as isJsxElement, Te as nextTick, we as onDestroy, Se as onMount, Je as renderToString, oe as ssg, X as template, Ae as useInject, Le as useProvide, je as useRef };
|
|
13
13
|
//# sourceMappingURL=template.esm.js.map
|
|
14
14
|
//# sourceMappingURL=template.esm.js.map
|