@estjs/template 0.0.14-beta.6 → 0.0.14
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 +3 -23
- package/dist/template.cjs.js.map +1 -1
- package/dist/template.d.cts +76 -40
- package/dist/template.d.ts +76 -40
- package/dist/template.dev.cjs.js +100 -145
- package/dist/template.dev.esm.js +101 -145
- package/dist/template.esm.js +3 -9
- package/dist/template.esm.js.map +1 -1
- package/package.json +3 -3
package/dist/template.dev.esm.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isString, isFunction,
|
|
2
|
-
import {
|
|
1
|
+
import { isString, isFunction, isSymbol, isArray, escape, hasChanged, startsWith, capitalize, isNil, isHTMLElement, coerceArray, isFalsy, kebabCase } from '@estjs/shared';
|
|
2
|
+
import { isSignal, shallowReactive, useReactive, useEffect, shallowSignal, isComputed } from '@estjs/signal';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @estjs/template v0.0.14
|
|
5
|
+
* @estjs/template v0.0.14
|
|
6
6
|
* (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
|
|
7
7
|
* @license MIT
|
|
8
8
|
**/
|
|
@@ -15,6 +15,7 @@ var CHILDREN_PROP = "children";
|
|
|
15
15
|
var EMPTY_TEMPLATE = "";
|
|
16
16
|
var FRAGMENT_PROP_KEY = "0";
|
|
17
17
|
var SINGLE_PROP_KEY = "1";
|
|
18
|
+
var REF_KEY = "ref";
|
|
18
19
|
var PLACEHOLDER = " __PLACEHOLDER__ ";
|
|
19
20
|
var RenderContext = class {
|
|
20
21
|
constructor() {
|
|
@@ -161,7 +162,7 @@ var SSGNode = class extends LifecycleContext {
|
|
|
161
162
|
}
|
|
162
163
|
normalizeProps(props) {
|
|
163
164
|
Object.entries(props).forEach(([key, value]) => {
|
|
164
|
-
if (key ===
|
|
165
|
+
if (key === CHILDREN_PROP) {
|
|
165
166
|
delete props[key];
|
|
166
167
|
} else if (isFunction(value)) {
|
|
167
168
|
delete props[key];
|
|
@@ -171,7 +172,7 @@ var SSGNode = class extends LifecycleContext {
|
|
|
171
172
|
});
|
|
172
173
|
}
|
|
173
174
|
generateAttributes(props) {
|
|
174
|
-
return Object.entries(props).filter(([key, value]) => key !==
|
|
175
|
+
return Object.entries(props).filter(([key, value]) => key !== CHILDREN_PROP && !isFunction(value)).map(([key, value]) => `${key}="${escape(String(value))}"`).join(" ");
|
|
175
176
|
}
|
|
176
177
|
renderChildren(children, findIndex) {
|
|
177
178
|
children.forEach(([child]) => {
|
|
@@ -181,10 +182,10 @@ var SSGNode = class extends LifecycleContext {
|
|
|
181
182
|
});
|
|
182
183
|
}
|
|
183
184
|
renderChild(child) {
|
|
184
|
-
if (
|
|
185
|
-
return this.renderChild(child(this.props));
|
|
186
|
-
} else if (isSignal(child)) {
|
|
185
|
+
if (isSignal(child)) {
|
|
187
186
|
return `<!--${1 /* TEXT_COMPONENT */}-${componentIndex}-->${child.value}<!$>`;
|
|
187
|
+
} else if (isFunction(child)) {
|
|
188
|
+
return this.renderChild(child(this.props));
|
|
188
189
|
} else if (isSSGNode(child)) {
|
|
189
190
|
const childResult = child.mount();
|
|
190
191
|
return isFunction(childResult) ? childResult(this.props) : extractSignal(childResult);
|
|
@@ -196,9 +197,6 @@ var SSGNode = class extends LifecycleContext {
|
|
|
196
197
|
|
|
197
198
|
// src/utils.ts
|
|
198
199
|
var SELF_CLOSING_TAGS = "area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr".split(",");
|
|
199
|
-
var HTML_TAGS = "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".split(
|
|
200
|
-
","
|
|
201
|
-
);
|
|
202
200
|
function coerceNode(data) {
|
|
203
201
|
if (isJsxElement(data) || data instanceof Node || isSSGNode(data)) {
|
|
204
202
|
return data;
|
|
@@ -315,50 +313,10 @@ function bindNode(node, setter) {
|
|
|
315
313
|
});
|
|
316
314
|
}
|
|
317
315
|
}
|
|
318
|
-
Promise.resolve();
|
|
319
316
|
function addEventListener(node, eventName, handler) {
|
|
320
317
|
node.addEventListener(eventName, handler);
|
|
321
318
|
return () => node.removeEventListener(eventName, handler);
|
|
322
319
|
}
|
|
323
|
-
function closeHtmlTags(input) {
|
|
324
|
-
const tagStack = [];
|
|
325
|
-
const output = [];
|
|
326
|
-
const tagPattern = /<\/?([\da-z-]+)([^>]*)>/gi;
|
|
327
|
-
let lastIndex = 0;
|
|
328
|
-
while (true) {
|
|
329
|
-
const match = tagPattern.exec(input);
|
|
330
|
-
if (!match) break;
|
|
331
|
-
const [fullMatch, tagName] = match;
|
|
332
|
-
const isEndTag = fullMatch[1] === "/";
|
|
333
|
-
output.push(input.slice(lastIndex, match.index));
|
|
334
|
-
lastIndex = match.index + fullMatch.length;
|
|
335
|
-
if (isEndTag) {
|
|
336
|
-
while (tagStack.length > 0 && tagStack[tagStack.length - 1] !== tagName) {
|
|
337
|
-
const unclosedTag = tagStack.pop();
|
|
338
|
-
if (unclosedTag) {
|
|
339
|
-
output.push(`</${unclosedTag}>`);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
if (tagStack.length > 0) {
|
|
343
|
-
tagStack.pop();
|
|
344
|
-
}
|
|
345
|
-
} else if (!SELF_CLOSING_TAGS.includes(tagName)) {
|
|
346
|
-
tagStack.push(tagName);
|
|
347
|
-
}
|
|
348
|
-
output.push(fullMatch);
|
|
349
|
-
}
|
|
350
|
-
output.push(input.slice(lastIndex));
|
|
351
|
-
while (tagStack.length > 0) {
|
|
352
|
-
const unclosedTag = tagStack.pop();
|
|
353
|
-
if (unclosedTag) {
|
|
354
|
-
output.push(`</${unclosedTag}>`);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
return output.join("");
|
|
358
|
-
}
|
|
359
|
-
function isHtmlTagName(tagName) {
|
|
360
|
-
return HTML_TAGS.includes(tagName);
|
|
361
|
-
}
|
|
362
320
|
function convertToHtmlTag(tagName) {
|
|
363
321
|
return SELF_CLOSING_TAGS.includes(tagName) ? `<${tagName}/>` : `<${tagName}></${tagName}>`;
|
|
364
322
|
}
|
|
@@ -378,15 +336,11 @@ var ComponentNode = class extends LifecycleContext {
|
|
|
378
336
|
this.emitter = /* @__PURE__ */ new Set();
|
|
379
337
|
this.rootNode = null;
|
|
380
338
|
this.trackMap = /* @__PURE__ */ new Map();
|
|
381
|
-
this.
|
|
382
|
-
this.
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
return signalObject(
|
|
387
|
-
props,
|
|
388
|
-
(key) => startsWith(key, EVENT_PREFIX) || startsWith(key, UPDATE_PREFIX) || key === CHILDREN_PROP
|
|
389
|
-
);
|
|
339
|
+
this.nodes = [];
|
|
340
|
+
this.parent = null;
|
|
341
|
+
this.before = null;
|
|
342
|
+
this.key || (this.key = props && props.key);
|
|
343
|
+
this.proxyProps || (this.proxyProps = shallowReactive(props || {}));
|
|
390
344
|
}
|
|
391
345
|
get firstChild() {
|
|
392
346
|
var _a, _b;
|
|
@@ -398,6 +352,7 @@ var ComponentNode = class extends LifecycleContext {
|
|
|
398
352
|
}
|
|
399
353
|
mount(parent, before) {
|
|
400
354
|
var _a, _b, _c, _d;
|
|
355
|
+
this.parent = parent;
|
|
401
356
|
if (!isFunction(this.template)) {
|
|
402
357
|
throw new Error("Template must be a function");
|
|
403
358
|
}
|
|
@@ -405,21 +360,31 @@ var ComponentNode = class extends LifecycleContext {
|
|
|
405
360
|
return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
|
|
406
361
|
}
|
|
407
362
|
this.initRef();
|
|
408
|
-
this.rootNode = this.template(useReactive(this.proxyProps, [
|
|
409
|
-
|
|
363
|
+
this.rootNode = this.template(useReactive(this.proxyProps, [CHILDREN_PROP]));
|
|
364
|
+
this.nodes = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
|
|
410
365
|
this.callMountHooks();
|
|
411
366
|
this.patchProps(this.props);
|
|
412
367
|
this.removeRef();
|
|
413
|
-
return
|
|
368
|
+
return this.nodes;
|
|
414
369
|
}
|
|
415
370
|
unmount() {
|
|
416
371
|
var _a;
|
|
417
|
-
this.
|
|
418
|
-
this.
|
|
372
|
+
this.callLifecycleHooks("destroy");
|
|
373
|
+
this.cleanup();
|
|
419
374
|
(_a = this.rootNode) == null ? void 0 : _a.unmount();
|
|
375
|
+
this.resetState();
|
|
376
|
+
if (this.key) {
|
|
377
|
+
componentCache.delete(this.key);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
resetState() {
|
|
420
381
|
this.rootNode = null;
|
|
421
382
|
this.proxyProps = {};
|
|
422
|
-
this.
|
|
383
|
+
this.nodes = [];
|
|
384
|
+
this.parent = null;
|
|
385
|
+
}
|
|
386
|
+
callLifecycleHooks(type) {
|
|
387
|
+
this.hooks[type].forEach((handler) => handler());
|
|
423
388
|
}
|
|
424
389
|
callMountHooks() {
|
|
425
390
|
this.hooks.mounted.forEach((handler) => handler());
|
|
@@ -438,9 +403,11 @@ var ComponentNode = class extends LifecycleContext {
|
|
|
438
403
|
this.rootNode = node.rootNode;
|
|
439
404
|
this.trackMap = node.trackMap;
|
|
440
405
|
this.hooks = node.hooks;
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
406
|
+
if (hasChanged(node.props, this.props)) {
|
|
407
|
+
const props = this.props;
|
|
408
|
+
this.props = node.props;
|
|
409
|
+
this.patchProps(props);
|
|
410
|
+
}
|
|
444
411
|
}
|
|
445
412
|
getNodeTrack(trackKey) {
|
|
446
413
|
let track = this.trackMap.get(trackKey);
|
|
@@ -460,7 +427,7 @@ var ComponentNode = class extends LifecycleContext {
|
|
|
460
427
|
for (const [key, prop] of Object.entries(props)) {
|
|
461
428
|
if (startsWith(key, EVENT_PREFIX) && ((_a = this.rootNode) == null ? void 0 : _a.firstChild)) {
|
|
462
429
|
this.patchEventListener(key, prop);
|
|
463
|
-
} else if (key ===
|
|
430
|
+
} else if (key === REF_KEY) {
|
|
464
431
|
this.patchRef(prop);
|
|
465
432
|
} else if (startsWith(key, UPDATE_PREFIX)) {
|
|
466
433
|
this.patchUpdateHandler(key, prop);
|
|
@@ -480,22 +447,25 @@ var ComponentNode = class extends LifecycleContext {
|
|
|
480
447
|
prop.value = (_b = (_a = this.rootNode) == null ? void 0 : _a.firstChild) != null ? _b : null;
|
|
481
448
|
}
|
|
482
449
|
patchUpdateHandler(key, prop) {
|
|
483
|
-
this.
|
|
450
|
+
this.proxyProps[key] = extractSignal(prop);
|
|
484
451
|
}
|
|
485
452
|
patchNormalProp(key, prop) {
|
|
486
|
-
var _a, _b;
|
|
487
|
-
const newValue = (_b = (_a = this.proxyProps)[key]) != null ? _b : _a[key] = useSignal(prop);
|
|
488
453
|
const track = this.getNodeTrack(key);
|
|
489
454
|
track.cleanup = useEffect(() => {
|
|
490
|
-
|
|
455
|
+
this.proxyProps[key] = isFunction(prop) ? prop() : prop;
|
|
491
456
|
});
|
|
492
457
|
}
|
|
458
|
+
cleanup() {
|
|
459
|
+
this.trackMap.forEach((track) => {
|
|
460
|
+
var _a;
|
|
461
|
+
return (_a = track.cleanup) == null ? void 0 : _a.call(track);
|
|
462
|
+
});
|
|
463
|
+
this.trackMap.clear();
|
|
464
|
+
this.emitter.forEach((cleanup) => cleanup());
|
|
465
|
+
this.emitter.clear();
|
|
466
|
+
}
|
|
493
467
|
};
|
|
494
468
|
|
|
495
|
-
// ../shared/src/comm.ts
|
|
496
|
-
var _toString = Object.prototype.toString;
|
|
497
|
-
var isPlainObject = (val) => _toString.call(val) === "[object Object]";
|
|
498
|
-
|
|
499
469
|
// src/patch.ts
|
|
500
470
|
function patchChildren(parent, childrenMap, nextChildren, before) {
|
|
501
471
|
const result = /* @__PURE__ */ new Map();
|
|
@@ -609,14 +579,12 @@ var TemplateNode = class {
|
|
|
609
579
|
this.template = template;
|
|
610
580
|
this.props = props;
|
|
611
581
|
this.key = key;
|
|
612
|
-
// Private properties for managing the node's state
|
|
613
582
|
this.treeMap = /* @__PURE__ */ new Map();
|
|
614
583
|
this.mounted = false;
|
|
615
584
|
this.nodes = [];
|
|
616
585
|
this.trackMap = /* @__PURE__ */ new Map();
|
|
617
586
|
this.bindValueKeys = [];
|
|
618
587
|
this.parent = null;
|
|
619
|
-
this.key || (this.key = props == null ? void 0 : props.key);
|
|
620
588
|
if (renderContext.isSSR) {
|
|
621
589
|
this.componentIndex = getComponentIndex(this.template);
|
|
622
590
|
}
|
|
@@ -646,7 +614,7 @@ var TemplateNode = class {
|
|
|
646
614
|
const firstChild = cloneNode.firstChild;
|
|
647
615
|
if ((_a = firstChild == null ? void 0 : firstChild.hasAttribute) == null ? void 0 : _a.call(firstChild, "_svg_")) {
|
|
648
616
|
firstChild.remove();
|
|
649
|
-
firstChild
|
|
617
|
+
firstChild.childNodes.forEach((node) => {
|
|
650
618
|
cloneNode.append(node);
|
|
651
619
|
});
|
|
652
620
|
}
|
|
@@ -662,40 +630,16 @@ var TemplateNode = class {
|
|
|
662
630
|
return this.nodes;
|
|
663
631
|
}
|
|
664
632
|
unmount() {
|
|
665
|
-
var _a, _b;
|
|
666
633
|
this.trackMap.forEach((track) => {
|
|
667
|
-
|
|
668
|
-
(_a2 = track.cleanup) == null ? void 0 : _a2.call(track);
|
|
634
|
+
track.cleanup && track.cleanup();
|
|
669
635
|
});
|
|
670
636
|
this.trackMap.clear();
|
|
671
637
|
this.treeMap.clear();
|
|
672
638
|
this.nodes.forEach((node) => removeChild(node));
|
|
673
|
-
if (!this.template.innerHTML && !this.nodes.length) {
|
|
674
|
-
const children = (_b = (_a = this.props) == null ? void 0 : _a[FRAGMENT_PROP_KEY]) == null ? void 0 : _b.children;
|
|
675
|
-
if (children) {
|
|
676
|
-
if (isArray(children)) {
|
|
677
|
-
children.forEach((child) => {
|
|
678
|
-
this.deleteFragmentTextNode(child);
|
|
679
|
-
});
|
|
680
|
-
} else {
|
|
681
|
-
this.deleteFragmentTextNode(children);
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
639
|
this.nodes = [];
|
|
686
640
|
this.mounted = false;
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
var _a;
|
|
690
|
-
if (isPrimitive(child)) {
|
|
691
|
-
(_a = this.parent) == null ? void 0 : _a.childNodes.forEach((node) => {
|
|
692
|
-
var _a2;
|
|
693
|
-
if (node.nodeType === Node.TEXT_NODE && node.textContent === `${child}`) {
|
|
694
|
-
(_a2 = this.parent) == null ? void 0 : _a2.removeChild(node);
|
|
695
|
-
}
|
|
696
|
-
});
|
|
697
|
-
} else {
|
|
698
|
-
removeChild(child);
|
|
641
|
+
if (this.key) {
|
|
642
|
+
componentCache.delete(this.key);
|
|
699
643
|
}
|
|
700
644
|
}
|
|
701
645
|
inheritNode(node) {
|
|
@@ -711,7 +655,7 @@ var TemplateNode = class {
|
|
|
711
655
|
this.treeMap.set(0, parent);
|
|
712
656
|
this.walkNodeTree(parent, this.handleSSGNode.bind(this));
|
|
713
657
|
}
|
|
714
|
-
//
|
|
658
|
+
// protected method to map node tree
|
|
715
659
|
mapNodeTree(parent, tree) {
|
|
716
660
|
let index = 1;
|
|
717
661
|
this.treeMap.set(0, parent);
|
|
@@ -763,9 +707,9 @@ var TemplateNode = class {
|
|
|
763
707
|
Object.entries(props).forEach(([attr, value]) => {
|
|
764
708
|
if (attr === CHILDREN_PROP && value) {
|
|
765
709
|
this.patchChildren(key, node, value, isRoot);
|
|
766
|
-
} else if (attr ===
|
|
710
|
+
} else if (attr === REF_KEY) {
|
|
767
711
|
props[attr].value = node;
|
|
768
|
-
} else if (startsWith(attr,
|
|
712
|
+
} else if (startsWith(attr, EVENT_PREFIX)) {
|
|
769
713
|
this.patchEventListener(key, node, attr, value);
|
|
770
714
|
} else {
|
|
771
715
|
if (this.bindValueKeys.includes(attr)) return;
|
|
@@ -775,8 +719,7 @@ var TemplateNode = class {
|
|
|
775
719
|
});
|
|
776
720
|
}
|
|
777
721
|
getBindUpdateValue(props, key, attr) {
|
|
778
|
-
const
|
|
779
|
-
const updateKey = `${UPDATE_PREFIX2}${capitalize(attr)}`;
|
|
722
|
+
const updateKey = `${UPDATE_PREFIX}${capitalize(attr)}`;
|
|
780
723
|
if (updateKey && props[updateKey] && isFunction(props[updateKey])) {
|
|
781
724
|
this.bindValueKeys.push(updateKey);
|
|
782
725
|
return props[updateKey];
|
|
@@ -805,14 +748,9 @@ var TemplateNode = class {
|
|
|
805
748
|
}
|
|
806
749
|
patchAttribute(key, element, attr, value, updateFn) {
|
|
807
750
|
const track = this.getNodeTrack(`${key}:${attr}`);
|
|
808
|
-
const
|
|
809
|
-
const triggerValue = isSignal(val) ? val : shallowSignal(val);
|
|
810
|
-
setAttribute(element, attr, triggerValue.value);
|
|
751
|
+
const triggerValue = shallowSignal();
|
|
811
752
|
const cleanup = useEffect(() => {
|
|
812
|
-
|
|
813
|
-
if (isPlainObject(val2) && isPlainObject(triggerValue.peek()) && JSON.stringify(triggerValue.value) === JSON.stringify(val2))
|
|
814
|
-
return;
|
|
815
|
-
triggerValue.value = isSignal(val2) ? val2.value : val2;
|
|
753
|
+
triggerValue.value = isSignal(value) || isComputed(value) ? value.value : value;
|
|
816
754
|
setAttribute(element, attr, triggerValue.value);
|
|
817
755
|
});
|
|
818
756
|
let cleanupBind;
|
|
@@ -827,7 +765,6 @@ var TemplateNode = class {
|
|
|
827
765
|
};
|
|
828
766
|
}
|
|
829
767
|
getNodeTrack(trackKey, trackLastNodes, isRoot) {
|
|
830
|
-
var _a;
|
|
831
768
|
let track = this.trackMap.get(trackKey);
|
|
832
769
|
if (!track) {
|
|
833
770
|
track = { cleanup: () => {
|
|
@@ -840,7 +777,7 @@ var TemplateNode = class {
|
|
|
840
777
|
}
|
|
841
778
|
this.trackMap.set(trackKey, track);
|
|
842
779
|
}
|
|
843
|
-
|
|
780
|
+
track.cleanup && track.cleanup();
|
|
844
781
|
return track;
|
|
845
782
|
}
|
|
846
783
|
patchChild(track, parent, child, before) {
|
|
@@ -892,18 +829,36 @@ var TemplateNode = class {
|
|
|
892
829
|
};
|
|
893
830
|
|
|
894
831
|
// src/jsxRenderer.ts
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
return
|
|
901
|
-
} else if (template === EMPTY_TEMPLATE) {
|
|
902
|
-
props = { [FRAGMENT_PROP_KEY]: props };
|
|
903
|
-
return new TemplateNode(createTemplate(EMPTY_TEMPLATE), props, key);
|
|
832
|
+
var componentCache = /* @__PURE__ */ new Map();
|
|
833
|
+
function createNodeCache(NodeConstructor, template, props = {}, key) {
|
|
834
|
+
if (key) {
|
|
835
|
+
const cached = componentCache.get(key);
|
|
836
|
+
if (cached) {
|
|
837
|
+
return cached;
|
|
904
838
|
}
|
|
905
839
|
}
|
|
906
|
-
|
|
840
|
+
if (typeof template === "string") {
|
|
841
|
+
template = createTemplate(template);
|
|
842
|
+
}
|
|
843
|
+
const newNode = new NodeConstructor(template, props, key);
|
|
844
|
+
if (key && newNode instanceof ComponentNode) {
|
|
845
|
+
componentCache.set(key, newNode);
|
|
846
|
+
}
|
|
847
|
+
return newNode;
|
|
848
|
+
}
|
|
849
|
+
function h(template, props = {}, key) {
|
|
850
|
+
if (template === EMPTY_TEMPLATE) {
|
|
851
|
+
return Fragment(template, props);
|
|
852
|
+
}
|
|
853
|
+
if (isString(template)) {
|
|
854
|
+
const htmlTemplate = convertToHtmlTag(template);
|
|
855
|
+
const wrappedProps = { [SINGLE_PROP_KEY]: props };
|
|
856
|
+
return createNodeCache(TemplateNode, htmlTemplate, wrappedProps, key);
|
|
857
|
+
}
|
|
858
|
+
if (isFunction(template)) {
|
|
859
|
+
return createNodeCache(ComponentNode, template, props, key);
|
|
860
|
+
}
|
|
861
|
+
return createNodeCache(TemplateNode, template, props, key);
|
|
907
862
|
}
|
|
908
863
|
function isComponent(node) {
|
|
909
864
|
return node instanceof ComponentNode;
|
|
@@ -913,13 +868,17 @@ function isJsxElement(node) {
|
|
|
913
868
|
}
|
|
914
869
|
function createTemplate(html) {
|
|
915
870
|
const template = document.createElement("template");
|
|
916
|
-
template.innerHTML =
|
|
871
|
+
template.innerHTML = html;
|
|
917
872
|
return template;
|
|
918
873
|
}
|
|
919
|
-
function Fragment(props) {
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
874
|
+
function Fragment(template, props) {
|
|
875
|
+
const processedProps = props.children ? {
|
|
876
|
+
[FRAGMENT_PROP_KEY]: {
|
|
877
|
+
children: Array.isArray(props.children) ? props.children.filter(Boolean) : [props.children]
|
|
878
|
+
}
|
|
879
|
+
} : props;
|
|
880
|
+
const templateElement = template === EMPTY_TEMPLATE ? createTemplate(EMPTY_TEMPLATE) : template;
|
|
881
|
+
return createNodeCache(TemplateNode, templateElement, processedProps);
|
|
923
882
|
}
|
|
924
883
|
function onMount(cb) {
|
|
925
884
|
assertInsideComponent("onMounted");
|
|
@@ -937,18 +896,15 @@ function assertInsideComponent(hookName, key) {
|
|
|
937
896
|
);
|
|
938
897
|
}
|
|
939
898
|
}
|
|
940
|
-
function
|
|
941
|
-
assertInsideComponent("
|
|
899
|
+
function provide(key, value) {
|
|
900
|
+
assertInsideComponent("provide", key);
|
|
942
901
|
LifecycleContext.ref && LifecycleContext.ref.setContext(key, value);
|
|
943
902
|
}
|
|
944
|
-
function
|
|
903
|
+
function inject(key, defaultValue) {
|
|
945
904
|
var _a;
|
|
946
|
-
assertInsideComponent("
|
|
905
|
+
assertInsideComponent("inject", key);
|
|
947
906
|
return (_a = LifecycleContext.ref && LifecycleContext.ref.getContext(key)) != null ? _a : defaultValue;
|
|
948
907
|
}
|
|
949
|
-
function useRef() {
|
|
950
|
-
return shallowSignal(null);
|
|
951
|
-
}
|
|
952
908
|
|
|
953
909
|
// src/server.ts
|
|
954
910
|
function renderToString(component, props) {
|
|
@@ -974,4 +930,4 @@ function ssg(component, props) {
|
|
|
974
930
|
return h(component, props);
|
|
975
931
|
}
|
|
976
932
|
|
|
977
|
-
export { Fragment, h, hydrate, isComponent, isJsxElement, onDestroy, onMount, renderToString, ssg, createTemplate as template
|
|
933
|
+
export { Fragment, h, hydrate, inject, isComponent, isJsxElement, onDestroy, onMount, provide, renderToString, ssg, createTemplate as template };
|
package/dist/template.esm.js
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @estjs/template v0.0.14-beta.6
|
|
1
|
+
import {isString,isFunction,isArray,escape,hasChanged,startsWith,capitalize,isNil,isHTMLElement,coerceArray,isFalsy,kebabCase}from'@estjs/shared';import {isSignal,shallowReactive,useReactive,useEffect,shallowSignal,isComputed}from'@estjs/signal';/**
|
|
2
|
+
* @estjs/template v0.0.14
|
|
6
3
|
* (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
|
|
7
4
|
* @license MIT
|
|
8
5
|
**/
|
|
9
|
-
var D="on",G="update",y="children",R="",A="0",q="1";var O=" __PLACEHOLDER__ ";var X=class{constructor(){this.renderMode=0;}get isSSG(){return this.renderMode===1}get isSSR(){return this.renderMode===2}get isClient(){return this.renderMode===0}setSSR(){this.renderMode=2;}setSSG(){this.renderMode=1;}setClient(){this.renderMode=0;}},m=new X,z=new Map;function Q(o,t){z.set(o,{index:t});}function Z(o){var t;return (t=z.get(o))==null?void 0:t.index}var T=class T{constructor(){this.hooks={mounted:new Set,destroy:new Set};}addEventListener(){}removeEventListener(){}addHook(t,e){var n;(n=this.hooks[t])==null||n.add(e);}getContext(t){return T.context[t]}setContext(t,e){T.context[t]=e;}initRef(){T.ref=this;}removeRef(){T.ref=null;}clearHooks(){Object.values(this.hooks).forEach(t=>t.clear());}};T.ref=null,T.context={};var d=T;function H(o){return o instanceof k}var M=1,k=class extends d{constructor(e,n={},r){super();this.template=e;this.props=n;this.key=r;Q(e,M),this.templates=this.processTemplate();}processTemplate(){if(isArray(this.template)){let e=this.template.join(O);return this.processHtmlString(e).split(O)}return []}processHtmlString(e){return e.replaceAll(/(<[^>]+>)|([^<]+)/g,(n,r,i)=>r?r.includes("data-ci")?n:r.replace(/<\s*([\da-z]+)(\s[^>]*)?>/i,(s,a,l)=>`<${a} data-ci="${M}"${l||""}>`):i&&i.replace(O,"").trim()?`<!--0-${M}-->${i}<!$>`:n)}mount(){this.initRef();let e=this.render();return this.removeRef(),e}render(){if(isFunction(this.template)){let e=this.template(this.props);return H(e)?e.mount():String(e)}return this.renderTemplate()}renderTemplate(){return Object.entries(this.props).forEach(([e,n])=>{let r=n.children;this.normalizeProps(n);let i=this.templates.findIndex(s=>s.includes(`data-hk="${e}"`));r&&this.renderChildren(r,i),this.templates[i]=this.templates[i].replace(`data-hk="${e}"`,`data-hk="${e}" ${this.generateAttributes(n)}`);}),this.templates.join("")}normalizeProps(e){Object.entries(e).forEach(([n,r])=>{n==="children"||isFunction(r)?delete e[n]:isSignal(r)&&(e[n]=r.value);});}generateAttributes(e){return Object.entries(e).filter(([n,r])=>n!=="children"&&!isFunction(r)).map(([n,r])=>`${n}="${escape(String(r))}"`).join(" ")}renderChildren(e,n){e.forEach(([r])=>{M++;let i=this.renderChild(r);this.templates[n]+=i;});}renderChild(e){if(isFunction(e))return this.renderChild(e(this.props));if(isSignal(e))return `<!--1-${M}-->${e.value}<!$>`;if(H(e)){let n=e.mount();return isFunction(n)?n(this.props):j(n)}else return `<!--1-${M}-->${e}<!$>`}};var ne="area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr".split(","),Te="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".split(",");function J(o){if(N(o)||o instanceof Node||H(o))return o;let t=isFalsy(o)?"":String(o);return document.createTextNode(t)}function E(o,t,e=null){let n=N(e)?e.firstChild:e,r=m.isSSR;N(t)?t.mount(o,n):n&&!r?n.before(t):r||o.append(t);}function x(o){N(o)?o.unmount():o.parentNode&&o.remove();}function V(o,t,e){E(o,t,e),x(e);}function U(o,t,e){t==="class"?ye(o,e):t==="style"?xe(o,e):ve(o,t,e);}function ye(o,t){typeof t=="string"?o.className=t:isArray(t)?o.className=t.join(" "):t&&typeof t=="object"&&(o.className=Object.entries(t).reduce((e,[n,r])=>e+(r?` ${n}`:""),"").trim());}function xe(o,t){typeof t=="string"?o.style.cssText=t:t&&typeof t=="object"&&Object.entries(t).forEach(([n,r])=>{o.style.setProperty(kebabCase(n),String(r));});}function ve(o,t,e){isFalsy(e)?o.removeAttribute(t):e===!0?o.setAttribute(t,""):o instanceof HTMLInputElement&&t==="value"?o.value=String(e):o.setAttribute(t,String(e));}function oe(o,t){if(o instanceof HTMLInputElement)switch(o.type){case"checkbox":return g(o,"change",()=>{t(!!o.checked);});case"date":return g(o,"change",()=>{t(o.value?o.value:"");});case"file":return g(o,"change",()=>{o.files&&t(o.files);});case"number":return g(o,"input",()=>{let e=Number.parseFloat(o.value);t(Number.isNaN(e)?"":String(e));});case"radio":return g(o,"change",()=>{t(o.checked?o.value:"");});case"text":return g(o,"input",()=>{t(o.value);})}if(o instanceof HTMLSelectElement)return g(o,"change",()=>{t(o.value);});if(o instanceof HTMLTextAreaElement)return g(o,"input",()=>{t(o.value);})}Promise.resolve();function g(o,t,e){return o.addEventListener(t,e),()=>o.removeEventListener(t,e)}function re(o){let t=[],e=[],n=/<\/?([\da-z-]+)([^>]*)>/gi,r=0;for(;;){let i=n.exec(o);if(!i)break;let[s,a]=i,l=s[1]==="/";if(e.push(o.slice(r,i.index)),r=i.index+s.length,l){for(;t.length>0&&t[t.length-1]!==a;){let c=t.pop();c&&e.push(`</${c}>`);}t.length>0&&t.pop();}else ne.includes(a)||t.push(a);e.push(s);}for(e.push(o.slice(r));t.length>0;){let i=t.pop();i&&e.push(`</${i}>`);}return e.join("")}function ie(o){return Te.includes(o)}function se(o){return ne.includes(o)?`<${o}/>`:`<${o}></${o}>`}function j(o){return isSignal(o)?o.value:o}var C=class extends d{constructor(e,n,r){super();this.template=e;this.props=n;this.key=r;this.emitter=new Set;this.rootNode=null;this.trackMap=new Map;this.key||(this.key=n==null?void 0:n.key),this.proxyProps=this.createProxyProps(n);}createProxyProps(e){return e?signalObject(e,n=>startsWith(n,D)||startsWith(n,G)||n===y):{}}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}mount(e,n){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,n))!=null?s:[];this.initRef(),this.rootNode=this.template(useReactive(this.proxyProps,["children"]));let r=(l=(a=this.rootNode)==null?void 0:a.mount(e,n))!=null?l:[];return this.callMountHooks(),this.patchProps(this.props),this.removeRef(),r}unmount(){var e;this.callDestroyHooks(),this.clearHooks(),(e=this.rootNode)==null||e.unmount(),this.rootNode=null,this.proxyProps={},this.clearEmitter();}callMountHooks(){this.hooks.mounted.forEach(e=>e());}callDestroyHooks(){this.hooks.destroy.forEach(e=>e());}clearEmitter(){for(let e of this.emitter)e();this.emitter.clear();}inheritNode(e){Object.assign(this.proxyProps,e.proxyProps),this.rootNode=e.rootNode,this.trackMap=e.trackMap,this.hooks=e.hooks;let n=this.props;this.props=e.props,this.patchProps(n);}getNodeTrack(e){let n=this.trackMap.get(e);return n||(n={cleanup:()=>{}},this.trackMap.set(e,n)),n.cleanup(),n}patchProps(e){var n;if(e){for(let[r,i]of Object.entries(e))startsWith(r,D)&&((n=this.rootNode)!=null&&n.firstChild)?this.patchEventListener(r,i):r==="ref"?this.patchRef(i):startsWith(r,G)?this.patchUpdateHandler(r,i):r!==y&&this.patchNormalProp(r,i);this.props=e;}}patchEventListener(e,n){let r=e.slice(2).toLowerCase(),i=g(this.rootNode.nodes[0],r,n);this.emitter.add(i);}patchRef(e){var n,r;e.value=(r=(n=this.rootNode)==null?void 0:n.firstChild)!=null?r:null;}patchUpdateHandler(e,n){this.props[e]=j(n);}patchNormalProp(e,n){var s,a;let r=(a=(s=this.proxyProps)[e])!=null?a:s[e]=useSignal(n),i=this.getNodeTrack(e);i.cleanup=useEffect(()=>{r.value=isFunction(n)?n():n;});}};var le=Object.prototype.toString;var B=o=>le.call(o)==="[object Object]";function ce(o,t,e,n){let r=new Map,i=Array.from(t.values());if(i.length&&e.length===0)return Ce(o,i,n),r;let s=[],a=we(e),l=0;for(let[c,u]of e.entries()){let p=i[l],b=v(p,c);for(;p&&!a.has(b);)x(p),t.delete(b),p=i[++l],b=v(p,c);let W=v(u,c),K=t.get(W);if(K&&(u=Pe(o,K,u)),p)if(p===K)l++;else {let Y=document.createComment("");E(o,Y,p),s.push([Y,u]);}else E(o,u,n);r.set(W,u);}return s.forEach(([c,u])=>{V(o,u,c);}),t.forEach((c,u)=>{c.isConnected&&!r.has(u)&&x(c);}),r}function Ce(o,t,e){if(o.childNodes.length===t.length+(e?1:0))o.innerHTML="",e&&E(o,e);else {let n=document.createRange(),r=t[0],i=N(r)?r.firstChild:r;n.setStartBefore(i),e?n.setEndBefore(e):n.setEndAfter(o),n.deleteContents();}t.forEach(n=>{N(n)&&n.unmount();});}function Pe(o,t,e){return t===e?t:N(t)&&N(e)&&t.template===e.template?(e.inheritNode(t),e):t instanceof Text&&e instanceof Text?(t.textContent!==e.textContent&&(t.textContent=e.textContent),t):(V(o,e,t),e)}function we(o){let t=new Map;for(let[e,n]of o.entries()){let r=v(n,e);t.set(r,n);}return t}function v(o,t){if(N(o)){let e=o.key;if(e!=null)return String(e)}return `_$${t}$`}var S=class{constructor(t,e,n){this.template=t;this.props=e;this.key=n;this.treeMap=new Map;this.mounted=!1;this.nodes=[];this.trackMap=new Map;this.bindValueKeys=[];this.parent=null;this.key||(this.key=e==null?void 0:e.key),m.isSSR&&(this.componentIndex=Z(this.template));}get firstChild(){var t;return (t=this.nodes[0])!=null?t:null}get isConnected(){return this.mounted}addEventListener(){}removeEventListener(){}mount(t,e){var i;if(this.parent=t,this.isConnected)return this.nodes.forEach(s=>E(t,s,e)),this.nodes;isArray(this.template)&&(this.template=w(this.template.join("")));let n=this.template.content.cloneNode(!0),r=n.firstChild;return (i=r==null?void 0:r.hasAttribute)!=null&&i.call(r,"_svg_")&&(r.remove(),r==null||r.childNodes.forEach(s=>{n.append(s);})),this.nodes=Array.from(n.childNodes),m.isSSR?this.mapSSGNodeTree(t):this.mapNodeTree(t,n),E(t,n,e),this.patchProps(this.props),this.mounted=!0,this.nodes}unmount(){var t,e;if(this.trackMap.forEach(n=>{var r;(r=n.cleanup)==null||r.call(n);}),this.trackMap.clear(),this.treeMap.clear(),this.nodes.forEach(n=>x(n)),!this.template.innerHTML&&!this.nodes.length){let n=(e=(t=this.props)==null?void 0:t[A])==null?void 0:e.children;n&&(isArray(n)?n.forEach(r=>{this.deleteFragmentTextNode(r);}):this.deleteFragmentTextNode(n));}this.nodes=[],this.mounted=!1;}deleteFragmentTextNode(t){var e;isPrimitive(t)?(e=this.parent)==null||e.childNodes.forEach(n=>{var r;n.nodeType===Node.TEXT_NODE&&n.textContent===`${t}`&&((r=this.parent)==null||r.removeChild(n));}):x(t);}inheritNode(t){this.mounted=t.mounted,this.nodes=t.nodes,this.trackMap=t.trackMap,this.treeMap=t.treeMap;let e=this.props;this.props=t.props,this.patchProps(e);}mapSSGNodeTree(t){this.treeMap.set(0,t),this.walkNodeTree(t,this.handleSSGNode.bind(this));}mapNodeTree(t,e){let n=1;this.treeMap.set(0,t);let r=[t],i=s=>{s.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&(this.treeMap.set(n++,s),r.push(s));};this.walkNodeTree(e,i);}walkNodeTree(t,e){t.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&e(t);let n=t.firstChild;for(;n;)this.walkNodeTree(n,e),n=n.nextSibling;}handleSSGNode(t){var e;if(t.nodeType===Node.COMMENT_NODE){let[n,r]=((e=t.textContent)==null?void 0:e.split("-"))||[];if(0===+n&&+r===this.componentIndex){let i=t.nextSibling;this.treeMap.set(+r,i);}}else if(t.nodeType!==Node.TEXT_NODE){let{ci:n="-1",hk:r}=(t==null?void 0:t.dataset)||{};r&&+n===this.componentIndex&&this.treeMap.set(+r,t);}}patchProps(t){t&&(Object.entries(t).forEach(([e,n])=>{let r=Number(e),i=this.treeMap.get(r);i&&this.patchProp(e,i,n,r===0);}),this.props=t);}patchProp(t,e,n,r){n&&Object.entries(n).forEach(([i,s])=>{if(i===y&&s)this.patchChildren(t,e,s,r);else if(i==="ref")n[i].value=e;else if(startsWith(i,"on"))this.patchEventListener(t,e,i,s);else {if(this.bindValueKeys.includes(i))return;let a=this.getBindUpdateValue(n,t,i);this.patchAttribute(t,e,i,s,a);}});}getBindUpdateValue(t,e,n){let i=`update${capitalize(n)}`;if(i&&t[i]&&isFunction(t[i]))return this.bindValueKeys.push(i),t[i]}patchChildren(t,e,n,r){if(isArray(n))n.filter(Boolean).forEach((i,s)=>{var b;let[a,l]=isArray(i)?i:[i,null],c=isNil(l)?null:(b=this.treeMap.get(l))!=null?b:null,u=`${t}:${y}:${s}`,p=this.getNodeTrack(u,!0,r);this.patchChild(p,e,a,c);});else {let i=`${t}:${y}:0`,s=this.getNodeTrack(i,!0,r);this.patchChild(s,e,n,null);}}patchEventListener(t,e,n,r){let i=n.slice(2).toLowerCase(),s=this.getNodeTrack(`${t}:${n}`);s.cleanup=g(e,i,r);}patchAttribute(t,e,n,r,i){let s=this.getNodeTrack(`${t}:${n}`),a=isFunction(r)?r():r,l=isSignal(a)?a:shallowSignal(a);U(e,n,l.value);let c=useEffect(()=>{let p=isFunction(r)?r():r;B(p)&&B(l.peek())&&JSON.stringify(l.value)===JSON.stringify(p)||(l.value=isSignal(p)?p.value:p,U(e,n,l.value));}),u;i&&isHTMLElement(e)&&(u=oe(e,p=>{i(p);})),s.cleanup=()=>{c&&c(),u&&u();};}getNodeTrack(t,e,n){var i;let r=this.trackMap.get(t);return r||(r={cleanup:()=>{}},e&&(r.lastNodes=new Map),n&&(r.isRoot=!0),this.trackMap.set(t,r)),(i=r.cleanup)==null||i.call(r),r}patchChild(t,e,n,r){isFunction(n)?t.cleanup=useEffect(()=>{let i=coerceArray(n()).map(J);m.isSSR?t.lastNodes=this.reconcileChildren(e,i,r):t.lastNodes=ce(e,t.lastNodes,i,r);}):coerceArray(n).forEach((i,s)=>{let a=J(i),l=v(a,s);m.isSSR?t.lastNodes=this.reconcileChildren(e,[a],r):(t.lastNodes.set(l,a),E(e,a,r));});}reconcileChildren(t,e,n){let r=new Map,i=Array.from(t.childNodes).filter(s=>{var a,l;return s.nodeType===Node.TEXT_NODE&&((a=s.previousSibling)==null?void 0:a.nodeType)===Node.COMMENT_NODE&&((l=s.nextSibling)==null?void 0:l.nodeType)===Node.COMMENT_NODE});return e.forEach((s,a)=>{let l=v(s,a);s.nodeType===Node.TEXT_NODE?i.forEach(c=>{s.textContent===c.textContent&&t.replaceChild(s,c);}):E(t,s,n),r.set(l,s);}),r}};function L(o,t,e){if(isString(o)){if(ie(o)){let n=se(o);return t={[q]:t},new S(w(n),t,e)}else if(o===R)return t={[A]:t},new S(w(R),t,e)}return isFunction(o)?new C(o,t,e):new S(o,t,e)}function Fe(o){return o instanceof C}function N(o){return o instanceof C||o instanceof S}function w(o){let t=document.createElement("template");return t.innerHTML=re(o),t}function Ke(o){return L(R,{children:isArray(o.children)?o.children.filter(Boolean):[o.children]})}function De(o){d.ref&&d.ref.addHook("mounted",o);}function Ge(o){d.ref&&d.ref.addHook("destroy",o);}function Je(o,t){d.ref&&d.ref.setContext(o,t);}function Ve(o,t){var e;return (e=d.ref&&d.ref.getContext(o))!=null?e:t}function Ue(){return shallowSignal(null)}function Be(o,t){m.setSSG();let n=new k(o,t||{}).mount();return m.setClient(),n}function We(o,t){let e=typeof t=="string"?document.querySelector(t):t;if(!e)throw new Error(`Could not find container: ${t}`);m.setSSR(),L(o).mount(e),m.setClient();}function Ye(o,t){return m.isSSG?new k(o,t):L(o,t)}
|
|
10
|
-
|
|
11
|
-
export { Ke as Fragment, L as h, We as hydrate, Fe as isComponent, N as isJsxElement, Ge as onDestroy, De as onMount, Be as renderToString, Ye as ssg, w as template, Ve as useInject, Je as useProvide, Ue as useRef };
|
|
12
|
-
//# sourceMappingURL=template.esm.js.map
|
|
6
|
+
var w="on",A="update",g="children",H="",B="0",q="1";var _="ref",O=" __PLACEHOLDER__ ";var D=class{constructor(){this.renderMode=0;}get isSSG(){return this.renderMode===1}get isSSR(){return this.renderMode===2}get isClient(){return this.renderMode===0}setSSR(){this.renderMode=2;}setSSG(){this.renderMode=1;}setClient(){this.renderMode=0;}},u=new D,z=new Map;function W(n,t){z.set(n,{index:t});}function Q(n){var t;return (t=z.get(n))==null?void 0:t.index}var y=class y{constructor(){this.hooks={mounted:new Set,destroy:new Set};}addEventListener(){}removeEventListener(){}addHook(t,e){var o;(o=this.hooks[t])==null||o.add(e);}getContext(t){return y.context[t]}setContext(t,e){y.context[t]=e;}initRef(){y.ref=this;}removeRef(){y.ref=null;}clearHooks(){Object.values(this.hooks).forEach(t=>t.clear());}};y.ref=null,y.context={};var l=y;function $(n){return n instanceof M}var b=1,M=class extends l{constructor(e,o={},r){super();this.template=e;this.props=o;this.key=r;W(e,b),this.templates=this.processTemplate();}processTemplate(){if(isArray(this.template)){let e=this.template.join(O);return this.processHtmlString(e).split(O)}return []}processHtmlString(e){return e.replaceAll(/(<[^>]+>)|([^<]+)/g,(o,r,i)=>r?r.includes("data-ci")?o:r.replace(/<\s*([\da-z]+)(\s[^>]*)?>/i,(s,c,a)=>`<${c} data-ci="${b}"${a||""}>`):i&&i.replace(O,"").trim()?`<!--0-${b}-->${i}<!$>`:o)}mount(){this.initRef();let e=this.render();return this.removeRef(),e}render(){if(isFunction(this.template)){let e=this.template(this.props);return $(e)?e.mount():String(e)}return this.renderTemplate()}renderTemplate(){return Object.entries(this.props).forEach(([e,o])=>{let r=o.children;this.normalizeProps(o);let i=this.templates.findIndex(s=>s.includes(`data-hk="${e}"`));r&&this.renderChildren(r,i),this.templates[i]=this.templates[i].replace(`data-hk="${e}"`,`data-hk="${e}" ${this.generateAttributes(o)}`);}),this.templates.join("")}normalizeProps(e){Object.entries(e).forEach(([o,r])=>{o===g||isFunction(r)?delete e[o]:isSignal(r)&&(e[o]=r.value);});}generateAttributes(e){return Object.entries(e).filter(([o,r])=>o!==g&&!isFunction(r)).map(([o,r])=>`${o}="${escape(String(r))}"`).join(" ")}renderChildren(e,o){e.forEach(([r])=>{b++;let i=this.renderChild(r);this.templates[o]+=i;});}renderChild(e){if(isSignal(e))return `<!--1-${b}-->${e.value}<!$>`;if(isFunction(e))return this.renderChild(e(this.props));if($(e)){let o=e.mount();return isFunction(o)?o(this.props):I(o)}else return `<!--1-${b}-->${e}<!$>`}};var Ee="area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr".split(",");function F(n){if(h(n)||n instanceof Node||$(n))return n;let t=isFalsy(n)?"":String(n);return document.createTextNode(t)}function T(n,t,e=null){let o=h(e)?e.firstChild:e,r=u.isSSR;h(t)?t.mount(n,o):o&&!r?o.before(t):r||n.append(t);}function k(n){h(n)?n.unmount():n.parentNode&&n.remove();}function J(n,t,e){T(n,t,e),k(e);}function te(n,t,e){t==="class"?ge(n,e):t==="style"?Te(n,e):ye(n,t,e);}function ge(n,t){typeof t=="string"?n.className=t:isArray(t)?n.className=t.join(" "):t&&typeof t=="object"&&(n.className=Object.entries(t).reduce((e,[o,r])=>e+(r?` ${o}`:""),"").trim());}function Te(n,t){typeof t=="string"?n.style.cssText=t:t&&typeof t=="object"&&Object.entries(t).forEach(([o,r])=>{n.style.setProperty(kebabCase(o),String(r));});}function ye(n,t,e){isFalsy(e)?n.removeAttribute(t):e===!0?n.setAttribute(t,""):n instanceof HTMLInputElement&&t==="value"?n.value=String(e):n.setAttribute(t,String(e));}function oe(n,t){if(n instanceof HTMLInputElement)switch(n.type){case"checkbox":return m(n,"change",()=>{t(!!n.checked);});case"date":return m(n,"change",()=>{t(n.value?n.value:"");});case"file":return m(n,"change",()=>{n.files&&t(n.files);});case"number":return m(n,"input",()=>{let e=Number.parseFloat(n.value);t(Number.isNaN(e)?"":String(e));});case"radio":return m(n,"change",()=>{t(n.checked?n.value:"");});case"text":return m(n,"input",()=>{t(n.value);})}if(n instanceof HTMLSelectElement)return m(n,"change",()=>{t(n.value);});if(n instanceof HTMLTextAreaElement)return m(n,"input",()=>{t(n.value);})}function m(n,t,e){return n.addEventListener(t,e),()=>n.removeEventListener(t,e)}function ne(n){return Ee.includes(n)?`<${n}/>`:`<${n}></${n}>`}function I(n){return isSignal(n)?n.value:n}var S=class extends l{constructor(e,o,r){super();this.template=e;this.props=o;this.key=r;this.emitter=new Set;this.rootNode=null;this.trackMap=new Map;this.nodes=[];this.parent=null;this.before=null;this.key||(this.key=o&&o.key),this.proxyProps||(this.proxyProps=shallowReactive(o||{}));}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}mount(e,o){var r,i,s,c;if(this.parent=e,!isFunction(this.template))throw new Error("Template must be a function");return this.isConnected?(i=(r=this.rootNode)==null?void 0:r.mount(e,o))!=null?i:[]:(this.initRef(),this.rootNode=this.template(useReactive(this.proxyProps,[g])),this.nodes=(c=(s=this.rootNode)==null?void 0:s.mount(e,o))!=null?c:[],this.callMountHooks(),this.patchProps(this.props),this.removeRef(),this.nodes)}unmount(){var e;this.callLifecycleHooks("destroy"),this.cleanup(),(e=this.rootNode)==null||e.unmount(),this.resetState(),this.key&&L.delete(this.key);}resetState(){this.rootNode=null,this.proxyProps={},this.nodes=[],this.parent=null;}callLifecycleHooks(e){this.hooks[e].forEach(o=>o());}callMountHooks(){this.hooks.mounted.forEach(e=>e());}callDestroyHooks(){this.hooks.destroy.forEach(e=>e());}clearEmitter(){for(let e of this.emitter)e();this.emitter.clear();}inheritNode(e){if(Object.assign(this.proxyProps,e.proxyProps),this.rootNode=e.rootNode,this.trackMap=e.trackMap,this.hooks=e.hooks,hasChanged(e.props,this.props)){let o=this.props;this.props=e.props,this.patchProps(o);}}getNodeTrack(e){let o=this.trackMap.get(e);return o||(o={cleanup:()=>{}},this.trackMap.set(e,o)),o.cleanup(),o}patchProps(e){var o;if(e){for(let[r,i]of Object.entries(e))startsWith(r,w)&&((o=this.rootNode)!=null&&o.firstChild)?this.patchEventListener(r,i):r===_?this.patchRef(i):startsWith(r,A)?this.patchUpdateHandler(r,i):r!==g&&this.patchNormalProp(r,i);this.props=e;}}patchEventListener(e,o){let r=e.slice(2).toLowerCase(),i=m(this.rootNode.nodes[0],r,o);this.emitter.add(i);}patchRef(e){var o,r;e.value=(r=(o=this.rootNode)==null?void 0:o.firstChild)!=null?r:null;}patchUpdateHandler(e,o){this.proxyProps[e]=I(o);}patchNormalProp(e,o){let r=this.getNodeTrack(e);r.cleanup=useEffect(()=>{this.proxyProps[e]=isFunction(o)?o():o;});}cleanup(){this.trackMap.forEach(e=>{var o;return (o=e.cleanup)==null?void 0:o.call(e)}),this.trackMap.clear(),this.emitter.forEach(e=>e()),this.emitter.clear();}};function se(n,t,e,o){let r=new Map,i=Array.from(t.values());if(i.length&&e.length===0)return be(n,i,o),r;let s=[],c=ke(e),a=0;for(let[d,p]of e.entries()){let f=i[a],C=x(f,d);for(;f&&!c.has(C);)k(f),t.delete(C),f=i[++a],C=x(f,d);let Y=x(p,d),G=t.get(Y);if(G&&(p=Me(n,G,p)),f)if(f===G)a++;else {let U=document.createComment("");T(n,U,f),s.push([U,p]);}else T(n,p,o);r.set(Y,p);}return s.forEach(([d,p])=>{J(n,p,d);}),t.forEach((d,p)=>{d.isConnected&&!r.has(p)&&k(d);}),r}function be(n,t,e){if(n.childNodes.length===t.length+(e?1:0))n.innerHTML="",e&&T(n,e);else {let o=document.createRange(),r=t[0],i=h(r)?r.firstChild:r;o.setStartBefore(i),e?o.setEndBefore(e):o.setEndAfter(n),o.deleteContents();}t.forEach(o=>{h(o)&&o.unmount();});}function Me(n,t,e){return t===e?t:h(t)&&h(e)&&t.template===e.template?(e.inheritNode(t),e):t instanceof Text&&e instanceof Text?(t.textContent!==e.textContent&&(t.textContent=e.textContent),t):(J(n,e,t),e)}function ke(n){let t=new Map;for(let[e,o]of n.entries()){let r=x(o,e);t.set(r,o);}return t}function x(n,t){if(h(n)){let e=n.key;if(e!=null)return String(e)}return `_$${t}$`}var v=class{constructor(t,e,o){this.template=t;this.props=e;this.key=o;this.treeMap=new Map;this.mounted=!1;this.nodes=[];this.trackMap=new Map;this.bindValueKeys=[];this.parent=null;u.isSSR&&(this.componentIndex=Q(this.template));}get firstChild(){var t;return (t=this.nodes[0])!=null?t:null}get isConnected(){return this.mounted}addEventListener(){}removeEventListener(){}mount(t,e){var i;if(this.parent=t,this.isConnected)return this.nodes.forEach(s=>T(t,s,e)),this.nodes;isArray(this.template)&&(this.template=R(this.template.join("")));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.childNodes.forEach(s=>{o.append(s);})),this.nodes=Array.from(o.childNodes),u.isSSR?this.mapSSGNodeTree(t):this.mapNodeTree(t,o),T(t,o,e),this.patchProps(this.props),this.mounted=!0,this.nodes}unmount(){this.trackMap.forEach(t=>{t.cleanup&&t.cleanup();}),this.trackMap.clear(),this.treeMap.clear(),this.nodes.forEach(t=>k(t)),this.nodes=[],this.mounted=!1,this.key&&L.delete(this.key);}inheritNode(t){this.mounted=t.mounted,this.nodes=t.nodes,this.trackMap=t.trackMap,this.treeMap=t.treeMap;let e=this.props;this.props=t.props,this.patchProps(e);}mapSSGNodeTree(t){this.treeMap.set(0,t),this.walkNodeTree(t,this.handleSSGNode.bind(this));}mapNodeTree(t,e){let o=1;this.treeMap.set(0,t);let r=[t],i=s=>{s.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&(this.treeMap.set(o++,s),r.push(s));};this.walkNodeTree(e,i);}walkNodeTree(t,e){t.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&e(t);let o=t.firstChild;for(;o;)this.walkNodeTree(o,e),o=o.nextSibling;}handleSSGNode(t){var e;if(t.nodeType===Node.COMMENT_NODE){let[o,r]=((e=t.textContent)==null?void 0:e.split("-"))||[];if(0===+o&&+r===this.componentIndex){let i=t.nextSibling;this.treeMap.set(+r,i);}}else if(t.nodeType!==Node.TEXT_NODE){let{ci:o="-1",hk:r}=(t==null?void 0:t.dataset)||{};r&&+o===this.componentIndex&&this.treeMap.set(+r,t);}}patchProps(t){t&&(Object.entries(t).forEach(([e,o])=>{let r=Number(e),i=this.treeMap.get(r);i&&this.patchProp(e,i,o,r===0);}),this.props=t);}patchProp(t,e,o,r){o&&Object.entries(o).forEach(([i,s])=>{if(i===g&&s)this.patchChildren(t,e,s,r);else if(i===_)o[i].value=e;else if(startsWith(i,w))this.patchEventListener(t,e,i,s);else {if(this.bindValueKeys.includes(i))return;let c=this.getBindUpdateValue(o,t,i);this.patchAttribute(t,e,i,s,c);}});}getBindUpdateValue(t,e,o){let r=`${A}${capitalize(o)}`;if(r&&t[r]&&isFunction(t[r]))return this.bindValueKeys.push(r),t[r]}patchChildren(t,e,o,r){if(isArray(o))o.filter(Boolean).forEach((i,s)=>{var C;let[c,a]=isArray(i)?i:[i,null],d=isNil(a)?null:(C=this.treeMap.get(a))!=null?C:null,p=`${t}:${g}:${s}`,f=this.getNodeTrack(p,!0,r);this.patchChild(f,e,c,d);});else {let i=`${t}:${g}:0`,s=this.getNodeTrack(i,!0,r);this.patchChild(s,e,o,null);}}patchEventListener(t,e,o,r){let i=o.slice(2).toLowerCase(),s=this.getNodeTrack(`${t}:${o}`);s.cleanup=m(e,i,r);}patchAttribute(t,e,o,r,i){let s=this.getNodeTrack(`${t}:${o}`),c=shallowSignal(),a=useEffect(()=>{c.value=isSignal(r)||isComputed(r)?r.value:r,te(e,o,c.value);}),d;i&&isHTMLElement(e)&&(d=oe(e,p=>{i(p);})),s.cleanup=()=>{a&&a(),d&&d();};}getNodeTrack(t,e,o){let r=this.trackMap.get(t);return r||(r={cleanup:()=>{}},e&&(r.lastNodes=new Map),o&&(r.isRoot=!0),this.trackMap.set(t,r)),r.cleanup&&r.cleanup(),r}patchChild(t,e,o,r){isFunction(o)?t.cleanup=useEffect(()=>{let i=coerceArray(o()).map(F);u.isSSR?t.lastNodes=this.reconcileChildren(e,i,r):t.lastNodes=se(e,t.lastNodes,i,r);}):coerceArray(o).forEach((i,s)=>{let c=F(i),a=x(c,s);u.isSSR?t.lastNodes=this.reconcileChildren(e,[c],r):(t.lastNodes.set(a,c),T(e,c,r));});}reconcileChildren(t,e,o){let r=new Map,i=Array.from(t.childNodes).filter(s=>{var c,a;return s.nodeType===Node.TEXT_NODE&&((c=s.previousSibling)==null?void 0:c.nodeType)===Node.COMMENT_NODE&&((a=s.nextSibling)==null?void 0:a.nodeType)===Node.COMMENT_NODE});return e.forEach((s,c)=>{let a=x(s,c);s.nodeType===Node.TEXT_NODE?i.forEach(d=>{s.textContent===d.textContent&&t.replaceChild(s,d);}):T(t,s,o),r.set(a,s);}),r}};var L=new Map;function j(n,t,e={},o){if(o){let i=L.get(o);if(i)return i}typeof t=="string"&&(t=R(t));let r=new n(t,e,o);return o&&r instanceof S&&L.set(o,r),r}function K(n,t={},e){if(n===H)return le(n,t);if(isString(n)){let o=ne(n),r={[q]:t};return j(v,o,r,e)}return isFunction(n)?j(S,n,t,e):j(v,n,t,e)}function Ie(n){return n instanceof S}function h(n){return n instanceof S||n instanceof v}function R(n){let t=document.createElement("template");return t.innerHTML=n,t}function le(n,t){let e=t.children?{[B]:{children:Array.isArray(t.children)?t.children.filter(Boolean):[t.children]}}:t,o=n===H?R(H):n;return j(v,o,e)}function je(n){l.ref&&l.ref.addHook("mounted",n);}function Ke(n){l.ref&&l.ref.addHook("destroy",n);}function Xe(n,t){l.ref&&l.ref.setContext(n,t);}function Ge(n,t){var e;return (e=l.ref&&l.ref.getContext(n))!=null?e:t}function De(n,t){u.setSSG();let o=new M(n,t||{}).mount();return u.setClient(),o}function Fe(n,t){let e=typeof t=="string"?document.querySelector(t):t;if(!e)throw new Error(`Could not find container: ${t}`);u.setSSR(),K(n).mount(e),u.setClient();}function Je(n,t){return u.isSSG?new M(n,t):K(n,t)}export{le as Fragment,K as h,Fe as hydrate,Ge as inject,Ie as isComponent,h as isJsxElement,Ke as onDestroy,je as onMount,Xe as provide,De as renderToString,Je as ssg,R as template};//# sourceMappingURL=template.esm.js.map
|
|
13
7
|
//# sourceMappingURL=template.esm.js.map
|