@estjs/template 0.0.14-beta.2 → 0.0.14-beta.4

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.
@@ -4,13 +4,13 @@ var shared = require('@estjs/shared');
4
4
  var signal = require('@estjs/signal');
5
5
 
6
6
  /**
7
- * @estjs/template v0.0.14-beta.2
7
+ * @estjs/template v0.0.14-beta.4
8
8
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
9
9
  * @license MIT
10
10
  **/
11
11
 
12
12
 
13
- // src/shared-config.ts
13
+ // src/sharedConfig.ts
14
14
  var EVENT_PREFIX = "on";
15
15
  var UPDATE_PREFIX = "update";
16
16
  var CHILDREN_PROP = "children";
@@ -22,27 +22,21 @@ var RenderContext = class {
22
22
  constructor() {
23
23
  this.renderMode = 0 /* CLIENT */;
24
24
  }
25
- // Getter to check if the current mode is SSG
26
25
  get isSSG() {
27
26
  return this.renderMode === 1 /* SSG */;
28
27
  }
29
- // Getter to check if the current mode is SSR
30
28
  get isSSR() {
31
29
  return this.renderMode === 2 /* SSR */;
32
30
  }
33
- // Getter to check if the current mode is Client
34
31
  get isClient() {
35
32
  return this.renderMode === 0 /* CLIENT */;
36
33
  }
37
- // Set render mode to SSR
38
34
  setSSR() {
39
35
  this.renderMode = 2 /* SSR */;
40
36
  }
41
- // Set render mode to SSG
42
37
  setSSG() {
43
38
  this.renderMode = 1 /* SSG */;
44
39
  }
45
- // Set render mode to Client
46
40
  setClient() {
47
41
  this.renderMode = 0 /* CLIENT */;
48
42
  }
@@ -59,7 +53,7 @@ function getComponentIndex(temp) {
59
53
  return (_a = componentMap.get(temp)) == null ? void 0 : _a.index;
60
54
  }
61
55
 
62
- // src/lifecycle-context.ts
56
+ // src/lifecycleContext.ts
63
57
  var _LifecycleContext = class _LifecycleContext {
64
58
  constructor() {
65
59
  // Hooks for different lifecycle stages
@@ -85,11 +79,9 @@ var _LifecycleContext = class _LifecycleContext {
85
79
  setContext(context, value) {
86
80
  _LifecycleContext.context[context] = value;
87
81
  }
88
- // Initialize the static reference
89
82
  initRef() {
90
83
  _LifecycleContext.ref = this;
91
84
  }
92
- // Remove the static reference
93
85
  removeRef() {
94
86
  _LifecycleContext.ref = null;
95
87
  }
@@ -98,13 +90,11 @@ var _LifecycleContext = class _LifecycleContext {
98
90
  Object.values(this.hooks).forEach((set) => set.clear());
99
91
  }
100
92
  };
101
- // Static reference to the current context
102
93
  _LifecycleContext.ref = null;
103
- // Static context to store shared values
104
94
  _LifecycleContext.context = {};
105
95
  var LifecycleContext = _LifecycleContext;
106
96
 
107
- // src/ssg-node.ts
97
+ // src/ssgNode.ts
108
98
  function isSSGNode(node) {
109
99
  return node instanceof SSGNode;
110
100
  }
@@ -118,7 +108,6 @@ var SSGNode = class extends LifecycleContext {
118
108
  enterComponent(template, componentIndex);
119
109
  this.templates = this.processTemplate();
120
110
  }
121
- // Process the template and return an array of processed strings
122
111
  processTemplate() {
123
112
  if (shared.isArray(this.template)) {
124
113
  const htmlString = this.template.join(PLACEHOLDER);
@@ -127,7 +116,6 @@ var SSGNode = class extends LifecycleContext {
127
116
  }
128
117
  return [];
129
118
  }
130
- // Process HTML string by adding component index and handling text nodes
131
119
  processHtmlString(htmlString) {
132
120
  return htmlString.replaceAll(/(<[^>]+>)|([^<]+)/g, (match, p1, p2) => {
133
121
  if (p1) {
@@ -141,14 +129,12 @@ var SSGNode = class extends LifecycleContext {
141
129
  return match;
142
130
  });
143
131
  }
144
- // Mount the SSGNode and return the rendered string
145
132
  mount() {
146
133
  this.initRef();
147
134
  const output = this.render();
148
135
  this.removeRef();
149
136
  return output;
150
137
  }
151
- // Render the SSGNode
152
138
  render() {
153
139
  if (shared.isFunction(this.template)) {
154
140
  const root = this.template(this.props);
@@ -160,7 +146,6 @@ var SSGNode = class extends LifecycleContext {
160
146
  }
161
147
  return this.renderTemplate();
162
148
  }
163
- // Render the template by processing props and children
164
149
  renderTemplate() {
165
150
  Object.entries(this.props).forEach(([key, cur]) => {
166
151
  const children = cur.children;
@@ -176,7 +161,6 @@ var SSGNode = class extends LifecycleContext {
176
161
  });
177
162
  return this.templates.join("");
178
163
  }
179
- // Normalize props by removing children and handling signals
180
164
  normalizeProps(props) {
181
165
  Object.entries(props).forEach(([key, value]) => {
182
166
  if (key === "children") {
@@ -188,11 +172,9 @@ var SSGNode = class extends LifecycleContext {
188
172
  }
189
173
  });
190
174
  }
191
- // Generate HTML attributes string from props
192
175
  generateAttributes(props) {
193
176
  return Object.entries(props).filter(([key, value]) => key !== "children" && !shared.isFunction(value)).map(([key, value]) => `${key}="${shared.escape(String(value))}"`).join(" ");
194
177
  }
195
- // Render children and append them to the template
196
178
  renderChildren(children, findIndex) {
197
179
  children.forEach(([child]) => {
198
180
  componentIndex++;
@@ -200,7 +182,6 @@ var SSGNode = class extends LifecycleContext {
200
182
  this.templates[findIndex] += renderedChild;
201
183
  });
202
184
  }
203
- // Render a single child node
204
185
  renderChild(child) {
205
186
  if (shared.isFunction(child)) {
206
187
  return this.renderChild(child(this.props));
@@ -402,25 +383,21 @@ var ComponentNode = class extends LifecycleContext {
402
383
  this.key || (this.key = props == null ? void 0 : props.key);
403
384
  this.proxyProps = this.createProxyProps(props);
404
385
  }
405
- // Create reactive props
406
386
  createProxyProps(props) {
407
387
  if (!props) return {};
408
- return signal.useReactive(
388
+ return signal.shallowReactive(
409
389
  props,
410
390
  (key) => shared.startsWith(key, EVENT_PREFIX) || shared.startsWith(key, UPDATE_PREFIX) || key === CHILDREN_PROP
411
391
  );
412
392
  }
413
- // Getter for the first child node
414
393
  get firstChild() {
415
394
  var _a, _b;
416
395
  return (_b = (_a = this.rootNode) == null ? void 0 : _a.firstChild) != null ? _b : null;
417
396
  }
418
- // Getter to check if the node is connected to the DOM
419
397
  get isConnected() {
420
398
  var _a, _b;
421
399
  return (_b = (_a = this.rootNode) == null ? void 0 : _a.isConnected) != null ? _b : false;
422
400
  }
423
- // Method to mount the component to the DOM
424
401
  mount(parent, before) {
425
402
  var _a, _b, _c, _d;
426
403
  if (!shared.isFunction(this.template)) {
@@ -437,7 +414,6 @@ var ComponentNode = class extends LifecycleContext {
437
414
  this.removeRef();
438
415
  return mountedNode;
439
416
  }
440
- // Method to unmount the component from the DOM
441
417
  unmount() {
442
418
  var _a;
443
419
  this.callDestroyHooks();
@@ -447,22 +423,18 @@ var ComponentNode = class extends LifecycleContext {
447
423
  this.proxyProps = {};
448
424
  this.clearEmitter();
449
425
  }
450
- // Private method to call mount hooks
451
426
  callMountHooks() {
452
427
  this.hooks.mounted.forEach((handler) => handler());
453
428
  }
454
- // Private method to call destroy hooks
455
429
  callDestroyHooks() {
456
430
  this.hooks.destroy.forEach((handler) => handler());
457
431
  }
458
- // Private method to clear the event emitter
459
432
  clearEmitter() {
460
433
  for (const cleanup of this.emitter) {
461
434
  cleanup();
462
435
  }
463
436
  this.emitter.clear();
464
437
  }
465
- // Method to inherit properties from another ComponentNode
466
438
  inheritNode(node) {
467
439
  Object.assign(this.proxyProps, node.proxyProps);
468
440
  this.rootNode = node.rootNode;
@@ -472,7 +444,6 @@ var ComponentNode = class extends LifecycleContext {
472
444
  this.props = node.props;
473
445
  this.patchProps(props);
474
446
  }
475
- // Private method to get or create a NodeTrack
476
447
  getNodeTrack(trackKey) {
477
448
  let track = this.trackMap.get(trackKey);
478
449
  if (!track) {
@@ -483,7 +454,6 @@ var ComponentNode = class extends LifecycleContext {
483
454
  track.cleanup();
484
455
  return track;
485
456
  }
486
- // Method to patch props onto the component
487
457
  patchProps(props) {
488
458
  var _a;
489
459
  if (!props) {
@@ -502,22 +472,18 @@ var ComponentNode = class extends LifecycleContext {
502
472
  }
503
473
  this.props = props;
504
474
  }
505
- // Private method to patch event listeners
506
475
  patchEventListener(key, prop) {
507
476
  const event = key.slice(2).toLowerCase();
508
477
  const cleanup = addEventListener(this.rootNode.nodes[0], event, prop);
509
478
  this.emitter.add(cleanup);
510
479
  }
511
- // Private method to patch ref
512
480
  patchRef(prop) {
513
481
  var _a, _b;
514
482
  prop.value = (_b = (_a = this.rootNode) == null ? void 0 : _a.firstChild) != null ? _b : null;
515
483
  }
516
- // Private method to patch update handlers
517
484
  patchUpdateHandler(key, prop) {
518
485
  this.props[key] = extractSignal(prop);
519
486
  }
520
- // Private method to patch normal props
521
487
  patchNormalProp(key, prop) {
522
488
  const track = this.getNodeTrack(key);
523
489
  track.cleanup = signal.useEffect(() => {
@@ -633,7 +599,7 @@ function getKey(node, index) {
633
599
  return `_$${index}$`;
634
600
  }
635
601
 
636
- // src/template-node.ts
602
+ // src/templateNode.ts
637
603
  var TemplateNode = class {
638
604
  constructor(template, props, key) {
639
605
  this.template = template;
@@ -651,21 +617,17 @@ var TemplateNode = class {
651
617
  this.componentIndex = getComponentIndex(this.template);
652
618
  }
653
619
  }
654
- // Getter for the first child node
655
620
  get firstChild() {
656
621
  var _a;
657
622
  return (_a = this.nodes[0]) != null ? _a : null;
658
623
  }
659
- // Getter to check if the node is connected to the DOM
660
624
  get isConnected() {
661
625
  return this.mounted;
662
626
  }
663
- // Placeholder methods for event handling
664
627
  addEventListener() {
665
628
  }
666
629
  removeEventListener() {
667
630
  }
668
- // Method to mount the node to the DOM
669
631
  mount(parent, before) {
670
632
  var _a;
671
633
  this.parent = parent;
@@ -695,7 +657,6 @@ var TemplateNode = class {
695
657
  this.mounted = true;
696
658
  return this.nodes;
697
659
  }
698
- // Method to unmount the node from the DOM
699
660
  unmount() {
700
661
  var _a, _b;
701
662
  this.trackMap.forEach((track) => {
@@ -733,7 +694,6 @@ var TemplateNode = class {
733
694
  removeChild(child);
734
695
  }
735
696
  }
736
- // Method to inherit properties from another TemplateNode
737
697
  inheritNode(node) {
738
698
  this.mounted = node.mounted;
739
699
  this.nodes = node.nodes;
@@ -743,7 +703,6 @@ var TemplateNode = class {
743
703
  this.props = node.props;
744
704
  this.patchProps(props);
745
705
  }
746
- // Private method to map SSG node tree
747
706
  mapSSGNodeTree(parent) {
748
707
  this.treeMap.set(0, parent);
749
708
  this.walkNodeTree(parent, this.handleSSGNode.bind(this));
@@ -759,7 +718,6 @@ var TemplateNode = class {
759
718
  };
760
719
  this.walkNodeTree(tree, handleNode);
761
720
  }
762
- // Private method to walk through the node tree
763
721
  walkNodeTree(node, handler) {
764
722
  if (node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
765
723
  handler(node);
@@ -770,7 +728,6 @@ var TemplateNode = class {
770
728
  child = child.nextSibling;
771
729
  }
772
730
  }
773
- // Private method to handle SSG nodes
774
731
  handleSSGNode(node) {
775
732
  var _a;
776
733
  if (node.nodeType === Node.COMMENT_NODE) {
@@ -786,7 +743,6 @@ var TemplateNode = class {
786
743
  }
787
744
  }
788
745
  }
789
- // Method to patch props onto the node
790
746
  patchProps(props) {
791
747
  if (!props) return;
792
748
  Object.entries(props).forEach(([key, value]) => {
@@ -798,7 +754,6 @@ var TemplateNode = class {
798
754
  });
799
755
  this.props = props;
800
756
  }
801
- // Private method to patch a single prop
802
757
  patchProp(key, node, props, isRoot) {
803
758
  if (!props) return;
804
759
  Object.entries(props).forEach(([attr, value]) => {
@@ -817,13 +772,12 @@ var TemplateNode = class {
817
772
  }
818
773
  getBindUpdateValue(props, key, attr) {
819
774
  const UPDATE_PREFIX2 = "update";
820
- const updateKey = `${UPDATE_PREFIX2}${shared.capitalizeFirstLetter(attr)}`;
775
+ const updateKey = `${UPDATE_PREFIX2}${shared.capitalize(attr)}`;
821
776
  if (updateKey && props[updateKey] && shared.isFunction(props[updateKey])) {
822
777
  this.bindValueKeys.push(updateKey);
823
778
  return props[updateKey];
824
779
  }
825
780
  }
826
- // Private method to patch children
827
781
  patchChildren(key, node, children, isRoot) {
828
782
  if (!shared.isArray(children)) {
829
783
  const trackKey = `${key}:${CHILDREN_PROP}:0`;
@@ -840,20 +794,19 @@ var TemplateNode = class {
840
794
  });
841
795
  }
842
796
  }
843
- // Private method to patch event listeners
844
797
  patchEventListener(key, node, attr, listener) {
845
798
  const eventName = attr.slice(2).toLowerCase();
846
799
  const track = this.getNodeTrack(`${key}:${attr}`);
847
800
  track.cleanup = addEventListener(node, eventName, listener);
848
801
  }
849
- // Private method to patch attributes
850
802
  patchAttribute(key, element, attr, value, updateFn) {
851
803
  const track = this.getNodeTrack(`${key}:${attr}`);
852
804
  const val = shared.isFunction(value) ? value() : value;
853
- const triggerValue = signal.isSignal(val) ? val : signal.useSignal(val);
805
+ const triggerValue = signal.isSignal(val) ? val : signal.shallowSignal(val);
854
806
  setAttribute(element, attr, triggerValue.value);
855
807
  const cleanup = signal.useEffect(() => {
856
808
  const val2 = shared.isFunction(value) ? value() : value;
809
+ if (JSON.stringify(triggerValue.value) === JSON.stringify(val2)) return;
857
810
  triggerValue.value = signal.isSignal(val2) ? val2.value : val2;
858
811
  setAttribute(element, attr, triggerValue.value);
859
812
  });
@@ -868,7 +821,6 @@ var TemplateNode = class {
868
821
  cleanupBind && cleanupBind();
869
822
  };
870
823
  }
871
- // Private method to get or create a NodeTrack
872
824
  getNodeTrack(trackKey, trackLastNodes, isRoot) {
873
825
  var _a;
874
826
  let track = this.trackMap.get(trackKey);
@@ -886,7 +838,6 @@ var TemplateNode = class {
886
838
  (_a = track.cleanup) == null ? void 0 : _a.call(track);
887
839
  return track;
888
840
  }
889
- // Private method to patch a child node
890
841
  patchChild(track, parent, child, before) {
891
842
  if (shared.isFunction(child)) {
892
843
  track.cleanup = signal.useEffect(() => {
@@ -910,7 +861,6 @@ var TemplateNode = class {
910
861
  });
911
862
  }
912
863
  }
913
- // Private method to reconcile children nodes
914
864
  reconcileChildren(parent, nextNodes, before) {
915
865
  const result = /* @__PURE__ */ new Map();
916
866
  const textNodes = Array.from(parent.childNodes).filter(
@@ -936,7 +886,7 @@ var TemplateNode = class {
936
886
  }
937
887
  };
938
888
 
939
- // src/jsx-renderer.ts
889
+ // src/jsxRenderer.ts
940
890
  function h(template, props, key) {
941
891
  if (shared.isString(template)) {
942
892
  if (isHtmlTagName(template)) {