@estjs/template 0.0.14-beta.3 → 0.0.14-beta.5

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.3
7
+ * @estjs/template v0.0.14-beta.5
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.shallowReactive(
388
+ return signal.signalObject(
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)) {
@@ -430,14 +407,13 @@ var ComponentNode = class extends LifecycleContext {
430
407
  return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
431
408
  }
432
409
  this.initRef();
433
- this.rootNode = this.template(this.proxyProps);
410
+ this.rootNode = this.template(signal.useReactive(this.proxyProps, ["children"]));
434
411
  const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
435
412
  this.callMountHooks();
436
413
  this.patchProps(this.props);
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,30 +472,32 @@ 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) {
488
+ var _a, _b;
489
+ const newValue = (_b = (_a = this.proxyProps)[key]) != null ? _b : _a[key] = signal.useSignal(prop);
522
490
  const track = this.getNodeTrack(key);
523
491
  track.cleanup = signal.useEffect(() => {
524
- this.proxyProps[key] = shared.isFunction(prop) ? prop() : prop;
492
+ newValue.value = shared.isFunction(prop) ? prop() : prop;
525
493
  });
526
494
  }
527
495
  };
528
496
 
497
+ // ../shared/src/comm.ts
498
+ var _toString = Object.prototype.toString;
499
+ var isPlainObject = (val) => _toString.call(val) === "[object Object]";
500
+
529
501
  // src/patch.ts
530
502
  function patchChildren(parent, childrenMap, nextChildren, before) {
531
503
  const result = /* @__PURE__ */ new Map();
@@ -633,7 +605,7 @@ function getKey(node, index) {
633
605
  return `_$${index}$`;
634
606
  }
635
607
 
636
- // src/template-node.ts
608
+ // src/templateNode.ts
637
609
  var TemplateNode = class {
638
610
  constructor(template, props, key) {
639
611
  this.template = template;
@@ -651,21 +623,17 @@ var TemplateNode = class {
651
623
  this.componentIndex = getComponentIndex(this.template);
652
624
  }
653
625
  }
654
- // Getter for the first child node
655
626
  get firstChild() {
656
627
  var _a;
657
628
  return (_a = this.nodes[0]) != null ? _a : null;
658
629
  }
659
- // Getter to check if the node is connected to the DOM
660
630
  get isConnected() {
661
631
  return this.mounted;
662
632
  }
663
- // Placeholder methods for event handling
664
633
  addEventListener() {
665
634
  }
666
635
  removeEventListener() {
667
636
  }
668
- // Method to mount the node to the DOM
669
637
  mount(parent, before) {
670
638
  var _a;
671
639
  this.parent = parent;
@@ -695,7 +663,6 @@ var TemplateNode = class {
695
663
  this.mounted = true;
696
664
  return this.nodes;
697
665
  }
698
- // Method to unmount the node from the DOM
699
666
  unmount() {
700
667
  var _a, _b;
701
668
  this.trackMap.forEach((track) => {
@@ -733,7 +700,6 @@ var TemplateNode = class {
733
700
  removeChild(child);
734
701
  }
735
702
  }
736
- // Method to inherit properties from another TemplateNode
737
703
  inheritNode(node) {
738
704
  this.mounted = node.mounted;
739
705
  this.nodes = node.nodes;
@@ -743,7 +709,6 @@ var TemplateNode = class {
743
709
  this.props = node.props;
744
710
  this.patchProps(props);
745
711
  }
746
- // Private method to map SSG node tree
747
712
  mapSSGNodeTree(parent) {
748
713
  this.treeMap.set(0, parent);
749
714
  this.walkNodeTree(parent, this.handleSSGNode.bind(this));
@@ -759,7 +724,6 @@ var TemplateNode = class {
759
724
  };
760
725
  this.walkNodeTree(tree, handleNode);
761
726
  }
762
- // Private method to walk through the node tree
763
727
  walkNodeTree(node, handler) {
764
728
  if (node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
765
729
  handler(node);
@@ -770,7 +734,6 @@ var TemplateNode = class {
770
734
  child = child.nextSibling;
771
735
  }
772
736
  }
773
- // Private method to handle SSG nodes
774
737
  handleSSGNode(node) {
775
738
  var _a;
776
739
  if (node.nodeType === Node.COMMENT_NODE) {
@@ -786,7 +749,6 @@ var TemplateNode = class {
786
749
  }
787
750
  }
788
751
  }
789
- // Method to patch props onto the node
790
752
  patchProps(props) {
791
753
  if (!props) return;
792
754
  Object.entries(props).forEach(([key, value]) => {
@@ -798,7 +760,6 @@ var TemplateNode = class {
798
760
  });
799
761
  this.props = props;
800
762
  }
801
- // Private method to patch a single prop
802
763
  patchProp(key, node, props, isRoot) {
803
764
  if (!props) return;
804
765
  Object.entries(props).forEach(([attr, value]) => {
@@ -817,13 +778,12 @@ var TemplateNode = class {
817
778
  }
818
779
  getBindUpdateValue(props, key, attr) {
819
780
  const UPDATE_PREFIX2 = "update";
820
- const updateKey = `${UPDATE_PREFIX2}${shared.capitalizeFirstLetter(attr)}`;
781
+ const updateKey = `${UPDATE_PREFIX2}${shared.capitalize(attr)}`;
821
782
  if (updateKey && props[updateKey] && shared.isFunction(props[updateKey])) {
822
783
  this.bindValueKeys.push(updateKey);
823
784
  return props[updateKey];
824
785
  }
825
786
  }
826
- // Private method to patch children
827
787
  patchChildren(key, node, children, isRoot) {
828
788
  if (!shared.isArray(children)) {
829
789
  const trackKey = `${key}:${CHILDREN_PROP}:0`;
@@ -840,20 +800,20 @@ var TemplateNode = class {
840
800
  });
841
801
  }
842
802
  }
843
- // Private method to patch event listeners
844
803
  patchEventListener(key, node, attr, listener) {
845
804
  const eventName = attr.slice(2).toLowerCase();
846
805
  const track = this.getNodeTrack(`${key}:${attr}`);
847
806
  track.cleanup = addEventListener(node, eventName, listener);
848
807
  }
849
- // Private method to patch attributes
850
808
  patchAttribute(key, element, attr, value, updateFn) {
851
809
  const track = this.getNodeTrack(`${key}:${attr}`);
852
810
  const val = shared.isFunction(value) ? value() : value;
853
- const triggerValue = signal.isSignal(val) ? val : signal.useSignal(val);
811
+ const triggerValue = signal.isSignal(val) ? val : signal.shallowSignal(val);
854
812
  setAttribute(element, attr, triggerValue.value);
855
813
  const cleanup = signal.useEffect(() => {
856
814
  const val2 = shared.isFunction(value) ? value() : value;
815
+ if (isPlainObject(val2) && isPlainObject(triggerValue.peek()) && JSON.stringify(triggerValue.value) === JSON.stringify(val2))
816
+ return;
857
817
  triggerValue.value = signal.isSignal(val2) ? val2.value : val2;
858
818
  setAttribute(element, attr, triggerValue.value);
859
819
  });
@@ -868,7 +828,6 @@ var TemplateNode = class {
868
828
  cleanupBind && cleanupBind();
869
829
  };
870
830
  }
871
- // Private method to get or create a NodeTrack
872
831
  getNodeTrack(trackKey, trackLastNodes, isRoot) {
873
832
  var _a;
874
833
  let track = this.trackMap.get(trackKey);
@@ -886,7 +845,6 @@ var TemplateNode = class {
886
845
  (_a = track.cleanup) == null ? void 0 : _a.call(track);
887
846
  return track;
888
847
  }
889
- // Private method to patch a child node
890
848
  patchChild(track, parent, child, before) {
891
849
  if (shared.isFunction(child)) {
892
850
  track.cleanup = signal.useEffect(() => {
@@ -910,7 +868,6 @@ var TemplateNode = class {
910
868
  });
911
869
  }
912
870
  }
913
- // Private method to reconcile children nodes
914
871
  reconcileChildren(parent, nextNodes, before) {
915
872
  const result = /* @__PURE__ */ new Map();
916
873
  const textNodes = Array.from(parent.childNodes).filter(
@@ -936,7 +893,7 @@ var TemplateNode = class {
936
893
  }
937
894
  };
938
895
 
939
- // src/jsx-renderer.ts
896
+ // src/jsxRenderer.ts
940
897
  function h(template, props, key) {
941
898
  if (shared.isString(template)) {
942
899
  if (isHtmlTagName(template)) {