vaderjs 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/vader.js CHANGED
@@ -1,23 +1,31 @@
1
- let dom = []
1
+ let dom = [];
2
2
  let states = {};
3
- let worker = new Worker(new URL('./worker.js', import.meta.url));
3
+ let worker = new Worker(new URL("./worker.js", import.meta.url));
4
4
  /**
5
5
  * @function markdown
6
6
  * @param {String} content
7
- * @description Allows you to convert markdown to html
7
+ * @description Allows you to convert markdown to html
8
8
  */
9
9
  function markdown(content) {
10
-
11
10
  let headers = content.match(/(#+)(.*)/g);
12
11
  if (headers) {
13
- headers.forEach((header) => {
14
- if(header.includes('/') || header.includes('<') || header.includes('>')){
15
- return
16
-
17
- }
18
- let level = header.split('#').length;
19
- content = content.replace(header, `<h${level} class="markdown_heading">${header.replace(/#/g, '')}</h${level}>`);
20
- });
12
+ headers.forEach((header) => {
13
+ if (
14
+ header.includes("/") ||
15
+ header.includes("<") ||
16
+ header.includes(">")
17
+ ) {
18
+ return;
19
+ }
20
+ let level = header.split("#").length;
21
+ content = content.replace(
22
+ header,
23
+ `<h${level} class="markdown_heading">${header.replace(
24
+ /#/g,
25
+ ""
26
+ )}</h${level}>`
27
+ );
28
+ });
21
29
  }
22
30
 
23
31
  content = content.replace(/\*\*(.*?)\*\*/g, (match, text) => {
@@ -25,7 +33,7 @@ function markdown(content) {
25
33
  });
26
34
  content = content.replace(/\*(.*?)\*/g, (match, text) => {
27
35
  return `<i class="markdown_italic">${text}</i>`;
28
- })
36
+ });
29
37
  content = content.replace(/`(.*?)`/g, (match, text) => {
30
38
  return `<code>${text}</code>`;
31
39
  });
@@ -35,48 +43,62 @@ function markdown(content) {
35
43
  content = content.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, (match, alt, src) => {
36
44
  return `<img class="markdown_image" src="${src}" alt="${alt}" />`;
37
45
  });
38
- content = content.split('\n').map((line, index, arr) => {
39
- if (line.match(/^\s*-\s+(.*?)$/gm)) {
40
- if (index === 0 || !arr[index - 1].match(/^\s*-\s+(.*?)$/gm)) {
41
- return `<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li>`;
42
- } else if (index === arr.length - 1 || !arr[index + 1].match(/^\s*-\s+(.*?)$/gm)) {
43
- return `<li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li></ul>`;
46
+ content = content
47
+ .split("\n")
48
+ .map((line, index, arr) => {
49
+ if (line.match(/^\s*-\s+(.*?)$/gm)) {
50
+ if (index === 0 || !arr[index - 1].match(/^\s*-\s+(.*?)$/gm)) {
51
+ return `<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${line.replace(
52
+ /^\s*-\s+(.*?)$/gm,
53
+ "$1"
54
+ )}</li>`;
55
+ } else if (
56
+ index === arr.length - 1 ||
57
+ !arr[index + 1].match(/^\s*-\s+(.*?)$/gm)
58
+ ) {
59
+ return `<li>${line.replace(/^\s*-\s+(.*?)$/gm, "$1")}</li></ul>`;
60
+ } else {
61
+ return `<li>${line.replace(/^\s*-\s+(.*?)$/gm, "$1")}</li>`;
62
+ }
44
63
  } else {
45
- return `<li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li>`;
64
+ return line;
46
65
  }
47
- } else {
48
- return line;
49
- }
50
- }).join('\n');
51
-
52
- content = content.split('\n').map((line, index, arr) => {
53
- if (line.match(/^\s*\d+\.\s+(.*?)$/gm)) {
54
- if (index === 0 || !arr[index - 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
55
- return `<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li>`;
56
- } else if (index === arr.length - 1 || !arr[index + 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
57
- return `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li></ol>`;
66
+ })
67
+ .join("\n");
68
+
69
+ content = content
70
+ .split("\n")
71
+ .map((line, index, arr) => {
72
+ if (line.match(/^\s*\d+\.\s+(.*?)$/gm)) {
73
+ if (index === 0 || !arr[index - 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
74
+ return `<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${line.replace(
75
+ /^\s*\d+\.\s+(.*?)$/gm,
76
+ "$1"
77
+ )}</li>`;
78
+ } else if (
79
+ index === arr.length - 1 ||
80
+ !arr[index + 1].match(/^\s*\d+\.\s+(.*?)$/gm)
81
+ ) {
82
+ return `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, "$1")}</li></ol>`;
83
+ } else {
84
+ return `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, "$1")}</li>`;
85
+ }
58
86
  } else {
59
- return `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li>`;
87
+ return line;
60
88
  }
61
- } else {
62
- return line;
63
- }
64
- }).join('\n');
65
-
89
+ })
90
+ .join("\n");
66
91
 
67
- return content
68
-
92
+ return content;
69
93
  }
70
94
 
71
-
72
-
73
95
  /**
74
96
  * @function useRef
75
97
  * @description Allows you to get reference to DOM element
76
98
  * @param {String} ref
77
99
  * @returns {void | Object} {current, update}
78
100
  */
79
-
101
+
80
102
  export const useRef = (ref) => {
81
103
  const element = document.querySelector(`[ref="${ref}"]`);
82
104
  const getElement = () => element;
@@ -97,7 +119,7 @@ export const useRef = (ref) => {
97
119
 
98
120
  return {
99
121
  current: getElement(),
100
- update,
122
+ update
101
123
  };
102
124
  };
103
125
 
@@ -141,47 +163,65 @@ export class Component {
141
163
  this.$_useStore_subscribers = [];
142
164
  this.init();
143
165
  this.Componentcontent = null;
144
- this.$_signal_dispatch_event = new CustomEvent("signalDispatch", {
166
+ this.$_signal_dispatch_event = new CustomEvent("SignalDispatch", {
145
167
  detail: {
146
168
  hasUpdated: false,
147
- state: null,
148
- },
169
+ state: null
170
+ }
149
171
  });
150
- this.snapshots = [];
172
+ /**
173
+ * @property {Object} $_signal_dispatch_cleanup_event
174
+ * @description Allows you to dispatch a signal cleanup event
175
+ * @private
176
+ */
177
+ this.$_signal_dispatch_cleanup_event = new CustomEvent(
178
+ "Signal_Cleanup_Dispatch",
179
+ {
180
+ detail: {
181
+ state: null,
182
+ lastState: null
183
+ }
184
+ }
185
+ );
186
+ /**
187
+ * @property {Array} snapshots
188
+ * @private
189
+ */
190
+ this.snapshots = [];
151
191
  /**
152
192
  * @property {Object} dom
153
193
  * @description Allows you to get reference to DOM element
154
194
  * @returns {void | HTMLElement}
155
- *
195
+ *
156
196
  */
157
- this.dom = []
158
-
197
+ this.dom = [];
198
+
159
199
  /**
160
200
  * @property {Boolean} cfr
161
- * @description Allows you to compile html code on the fly - client fly rendering
162
- *
201
+ * @description Allows you to compile html code on the fly - client fly rendering
202
+ *
163
203
  */
164
- this.cfr = false
204
+ this.cfr = false;
165
205
  /**
166
206
  * @property {Boolean} worker
167
207
  * @description Allows you to use a web worker to compile html code on the fly - client fly rendering
168
208
 
169
209
  */
170
-
171
210
  }
172
211
 
173
212
  /**
174
213
  * @method adapter
175
214
  * @description Allows you to create an adapter - this is used to create custom logic
176
- *
177
- *
215
+ *
216
+ *
178
217
  */
179
- adapter() {
180
- return
218
+ adapter(options) {
219
+ // allow you to override the compoent logic
220
+
221
+
181
222
  }
182
223
  init() {
183
224
  this.registerComponent();
184
-
185
225
  }
186
226
 
187
227
  registerComponent() {
@@ -303,13 +343,20 @@ export class Component {
303
343
  this.$_signal_subscribe = (fn, runonce) => {
304
344
  this.$_signal_subscribers.push({
305
345
  function: fn,
306
- runonce: runonce,
346
+ runonce: runonce
307
347
  });
348
+ return fn;
308
349
  };
309
350
  this.$_signal_cleanup = (fn) => {
351
+ this.lastState = state;
310
352
  this.$_signal_subscribers = this.$_signal_subscribers.filter(
311
353
  (subscriber) => subscriber.function !== fn
312
354
  );
355
+ // @ts-ignore
356
+ this.$_signal_dispatch_cleanup_event.detail.state = this.states;
357
+ // @ts-ignore
358
+ this.$_signal_dispatch_cleanup_event.detail.lastState = this.lastState;
359
+ window.dispatchEvent(this.$_signal_dispatch_event);
313
360
  };
314
361
  this.$_signal_dispatch = () => {
315
362
  for (var i = 0; i < this.$_signal_subscribers.length; i++) {
@@ -339,6 +386,7 @@ export class Component {
339
386
  * @param {*} detail
340
387
  */
341
388
  this.$_signal_set = (detail) => {
389
+
342
390
  setState(detail);
343
391
  };
344
392
 
@@ -382,7 +430,7 @@ export class Component {
382
430
  * @description Allows you to get the value of a signal
383
431
  * @returns {any}
384
432
  */
385
- get: this.$_signal_get,
433
+ get: this.$_signal_get
386
434
  };
387
435
  };
388
436
  /**
@@ -466,7 +514,7 @@ export class Component {
466
514
  return logicalOperator === "any"
467
515
  ? auth.canAnyOf(actions)
468
516
  : auth.canAllOf(actions);
469
- },
517
+ }
470
518
  };
471
519
  return auth;
472
520
  }
@@ -506,11 +554,10 @@ export class Component {
506
554
  (action) => {
507
555
  this.states[key] = reducer(this.states[key], action);
508
556
  this.updateComponent();
509
- },
557
+ }
510
558
  ];
511
559
  }
512
560
 
513
-
514
561
  runEffects() {
515
562
  Object.keys(this.effects).forEach((component) => {
516
563
  this.effects[component].forEach((effect) => {
@@ -521,7 +568,7 @@ export class Component {
521
568
  });
522
569
  });
523
570
  }
524
-
571
+
525
572
  /**
526
573
  * @method useSyncStore
527
574
  * @description Allows you to create a store
@@ -545,8 +592,7 @@ export class Component {
545
592
  subscriber(s);
546
593
  });
547
594
  }) ||
548
- {},
549
-
595
+ {}
550
596
  );
551
597
 
552
598
  const getField = (fieldName) => {
@@ -569,7 +615,7 @@ export class Component {
569
615
  getField,
570
616
  setField,
571
617
  subscribe,
572
- clear,
618
+ clear
573
619
  };
574
620
  }
575
621
  /**
@@ -588,12 +634,10 @@ export class Component {
588
634
  * setCount(count + 1)
589
635
  */
590
636
  useState(key, initialValue, callback = null) {
591
-
592
- if(!this.states[key]){
593
- this.states[key] = initialValue;
594
- }
595
-
596
-
637
+ if (!this.states[key]) {
638
+ this.states[key] = initialValue;
639
+ }
640
+
597
641
  return [
598
642
  this.states[key],
599
643
  /**
@@ -607,7 +651,7 @@ export class Component {
607
651
  this.updateComponent();
608
652
  // @ts-ignore
609
653
  typeof callback === "function" ? callback() : null;
610
- },
654
+ }
611
655
  ];
612
656
  }
613
657
  /**
@@ -624,9 +668,8 @@ export class Component {
624
668
 
625
669
  useRef(ref) {
626
670
  // get ref from array
627
- console.log(this.dom)
628
- const element = this.dom[ref]
629
-
671
+ const element = this.dom[ref];
672
+
630
673
  const getElement = () => element;
631
674
 
632
675
  const update = (data) => {
@@ -648,12 +691,12 @@ export class Component {
648
691
  // @ts-ignore
649
692
  current: getElement,
650
693
  /**@type {Function} */
651
- update,
694
+ update
652
695
  };
653
696
  }
654
697
 
655
698
  /**
656
- *
699
+ *
657
700
  * @function useEffect
658
701
  * @param {*} effectFn
659
702
  * @param {*} dependencies
@@ -685,7 +728,7 @@ export class Component {
685
728
  this.effects[this.name] = this.effects[this.name].filter(
686
729
  (effect) => effect !== effectFn
687
730
  );
688
- },
731
+ }
689
732
  };
690
733
  }
691
734
  /**
@@ -718,8 +761,7 @@ export class Component {
718
761
  const fragment = document.createDocumentFragment();
719
762
  Object.keys(components).forEach(async (component) => {
720
763
  const { name } = components[component];
721
-
722
-
764
+
723
765
  let componentContainer = document.querySelector(
724
766
  `[data-component="${name}"]`
725
767
  );
@@ -736,7 +778,7 @@ export class Component {
736
778
  prev_state: this.states,
737
779
  prev_props: this.storedProps,
738
780
  // @ts-ignore
739
- content: componentContainer.innerHTML,
781
+ content: componentContainer.innerHTML
740
782
  };
741
783
 
742
784
  if (!componentContainer) return;
@@ -796,11 +838,8 @@ export class Component {
796
838
  return /^[a-zA-Z0-9-_]+$/.test(className);
797
839
  }
798
840
 
799
-
800
841
  parseHTML(result) {
801
-
802
842
  const dom = new DOMParser().parseFromString(result, "text/html");
803
- console.log(dom)
804
843
  const elements = dom.documentElement.querySelectorAll("*");
805
844
 
806
845
  elements.forEach((element) => {
@@ -826,19 +865,23 @@ export class Component {
826
865
  throw new SyntaxError(
827
866
  `Image: ${element.outerHTML} alt attribute cannot be empty`
828
867
  );
829
-
830
868
  } else if (
831
- element.hasAttribute("src") &&
832
- !element.getAttribute("src")?.includes("http") || !element.getAttribute("src")?.includes("https") &&
833
- !document.documentElement.outerHTML
834
- .trim()
835
- .includes("<!-- #vader-disable_accessibility -->")
869
+ (element.hasAttribute("src") &&
870
+ !element.getAttribute("src")?.includes("http")) ||
871
+ (!element.getAttribute("src")?.includes("https") &&
872
+ !document.documentElement.outerHTML
873
+ .trim()
874
+ .includes("<!-- #vader-disable_accessibility -->"))
836
875
  ) {
837
876
  let prevurl = element.getAttribute("src");
838
877
  element.setAttribute("aria-hidden", "true");
839
878
  element.setAttribute("hidden", "true");
840
- // if window.lcoation.pathname includes a html file remove it and only use the path
841
- let url = window.location.origin + window.location.pathname.replace(/\/[^\/]*$/, '') + '/public/' + element.getAttribute("src");
879
+ // if window.lcoation.pathname includes a html file remove it and only use the path
880
+ let url =
881
+ window.location.origin +
882
+ window.location.pathname.replace(/\/[^\/]*$/, "") +
883
+ "/public/" +
884
+ element.getAttribute("src");
842
885
  let image = new Image();
843
886
  image.src = url;
844
887
  image.onerror = () => {
@@ -863,15 +906,16 @@ export class Component {
863
906
  // @ts-ignore
864
907
  dom[element.getAttribute("ref")] = element;
865
908
  }
866
- if(element.nodeName === "MARKDOWN"){
867
- element.innerHTML = markdown(element.innerHTML.replace(/\\n/g, '\n').trim())
909
+ if (element.nodeName === "MARKDOWN") {
910
+ element.innerHTML = markdown(
911
+ element.innerHTML.replace(/\\n/g, "\n").trim()
912
+ );
868
913
  }
869
914
 
870
915
  if (element.hasAttribute("class")) {
871
- const allowClassComments =
872
- document.documentElement.outerHTML.includes(
873
- "<!-- #vader-allow_class -->"
874
- );
916
+ const allowClassComments = document.documentElement.outerHTML.includes(
917
+ "<!-- #vader-allow_class -->"
918
+ );
875
919
  if (!allowClassComments) {
876
920
  console.warn(
877
921
  "you can disable class errors using, <!-- #vader-allow_class -->"
@@ -902,12 +946,14 @@ export class Component {
902
946
  }
903
947
 
904
948
  if (
905
- element.hasAttribute("src") &&
906
- // @ts-ignore
907
- !element.getAttribute("src").includes("http") &&
908
- // @ts-ignore
909
- !element.getAttribute("src").includes("https") &&
910
- !document.documentElement.outerHTML.includes(`<!-- #vader-disable_relative-paths -->`)
949
+ element.hasAttribute("src") &&
950
+ // @ts-ignore
951
+ !element.getAttribute("src").includes("http") &&
952
+ // @ts-ignore
953
+ !element.getAttribute("src").includes("https") &&
954
+ !document.documentElement.outerHTML.includes(
955
+ `<!-- #vader-disable_relative-paths -->`
956
+ )
911
957
  ) {
912
958
  element.setAttribute(
913
959
  "src",
@@ -917,7 +963,6 @@ export class Component {
917
963
  }
918
964
  break;
919
965
  }
920
-
921
966
  });
922
967
 
923
968
  result = dom.body.innerHTML;
@@ -925,12 +970,11 @@ export class Component {
925
970
  this.Componentcontent = result;
926
971
 
927
972
  if (!result.includes("<div data-component")) {
928
- result = `<div data-component="${this.name}">${result}</div>`;
973
+ result = `<div data-component="${this.name}">${result}</div>`;
929
974
  }
930
- return markdown(result.replace(/\\n/g, '\n').trim())
931
-
975
+ return markdown(result.replace(/\\n/g, "\n").trim());
932
976
  }
933
-
977
+
934
978
  /**
935
979
  * The `html` method generates and processes HTML content for a component, performing various validations and tasks.
936
980
  *
@@ -977,47 +1021,52 @@ export class Component {
977
1021
  * @see {@link Component}
978
1022
  * @see {@link Component#componentDidMount}
979
1023
  */
980
-
981
-
982
-
1024
+
983
1025
  html(strings, ...args) {
984
1026
  // @ts-ignore
985
- let tiemr = setInterval(()=>{
986
- if(document.querySelector(`[data-component="${this.name}"]`)){
987
- clearInterval(tiemr)
1027
+ let tiemr = setInterval(() => {
1028
+ if (document.querySelector(`[data-component="${this.name}"]`)) {
1029
+ clearInterval(tiemr);
988
1030
  this.componentMounted = true;
989
-
990
- document.querySelector(`[data-component="${this.name}"]`)?.querySelectorAll("*").forEach((element)=>{
991
- if(element.hasAttribute("ref")){
992
- // @ts-ignore
993
- this.dom[element.getAttribute("ref")] = element
994
- }
995
- })
1031
+
1032
+ document
1033
+ .querySelector(`[data-component="${this.name}"]`)
1034
+ ?.querySelectorAll("*")
1035
+ .forEach((element) => {
1036
+ if (element.hasAttribute("ref")) {
1037
+ // @ts-ignore
1038
+ this.dom[element.getAttribute("ref")] = element;
1039
+ }
1040
+ });
996
1041
  this.componentDidMount();
997
1042
  }
998
- }, 100)
1043
+ }, 100);
999
1044
  let script = document.createElement("script");
1000
1045
  script.setAttribute("type", "text/javascript");
1001
1046
  script.setAttribute(`data-component-script`, this.name);
1002
-
1003
1047
 
1004
-
1005
- let dom = this.dom
1006
-
1007
- if(this.cfr){
1008
-
1009
- worker.postMessage({strings, args, location: window.location.origin + window.location.pathname.replace(/\/[^\/]*$/, '') + '/public/', name: this.name})
1010
- let promise = new Promise((resolve, reject)=>{
1011
- worker.onmessage = (e)=>{
1012
- if(e.data.error){
1013
- throw new Error(e.data.error)
1048
+ let dom = this.dom;
1049
+
1050
+ if (this.cfr) {
1051
+ worker.postMessage({
1052
+ strings,
1053
+ args,
1054
+ location:
1055
+ window.location.origin +
1056
+ window.location.pathname.replace(/\/[^\/]*$/, "") +
1057
+ "/public/",
1058
+ name: this.name
1059
+ });
1060
+ let promise = new Promise((resolve, reject) => {
1061
+ worker.onmessage = (e) => {
1062
+ if (e.data.error) {
1063
+ throw new Error(e.data.error);
1014
1064
  }
1015
1065
  const dom = this.dom; // Assuming this.dom is an object
1016
- console.log(this.dom)
1017
- let js = e.data.js
1018
- let template = e.data.template
1019
- // Bind the component's context
1020
-
1066
+ let js = e.data.js;
1067
+ let template = e.data.template;
1068
+ // Bind the component's context
1069
+
1021
1070
  const useState = this.useState.bind(this); // Bind the component's context
1022
1071
  const useEffect = this.useEffect.bind(this); // Bind the component's context
1023
1072
  const useReducer = this.useReducer.bind(this); // Bind the component's context
@@ -1025,9 +1074,22 @@ export class Component {
1025
1074
  const useSyncStore = this.useSyncStore.bind(this); // Bind the component's context
1026
1075
  const signal = this.signal.bind(this); // Bind the component's context
1027
1076
  const rf = this.$Function.bind(this); // Bind the component's context
1028
- let states = this.states
1029
- const useRef = this.useRef.bind(this); // Bind the component's context
1030
- new Function("useState", "useEffect", "useAuth", "useReducer", "useSyncStore", "signal", "rf", "dom", "render", "states", "useRef", js)(
1077
+ let states = this.states;
1078
+ const useRef = this.useRef.bind(this); // Bind the component's context
1079
+ new Function(
1080
+ "useState",
1081
+ "useEffect",
1082
+ "useAuth",
1083
+ "useReducer",
1084
+ "useSyncStore",
1085
+ "signal",
1086
+ "rf",
1087
+ "dom",
1088
+ "render",
1089
+ "states",
1090
+ "useRef",
1091
+ js
1092
+ )(
1031
1093
  useState,
1032
1094
  useEffect,
1033
1095
  useAuth,
@@ -1039,21 +1101,22 @@ export class Component {
1039
1101
  this.render,
1040
1102
  this.states,
1041
1103
  useRef
1042
- )
1043
-
1044
- resolve(new Function("useRef", "states", "return" + "`" + template + "`")(useRef, states))
1045
-
1046
-
1047
-
1048
-
1049
- }
1050
- worker.onerror = (e)=>{
1051
- reject(e)
1052
- }
1053
- })
1104
+ );
1105
+
1106
+ resolve(
1107
+ new Function("useRef", "states", "return" + "`" + template + "`")(
1108
+ useRef,
1109
+ states
1110
+ )
1111
+ );
1112
+ };
1113
+ worker.onerror = (e) => {
1114
+ reject(e);
1115
+ };
1116
+ });
1054
1117
  // @ts-ignore
1055
1118
  return promise;
1056
- }else{
1119
+ } else {
1057
1120
  let result = "";
1058
1121
  for (let i = 0; i < strings.length; i++) {
1059
1122
  result += strings[i];
@@ -1061,20 +1124,16 @@ export class Component {
1061
1124
  result += args[i];
1062
1125
  }
1063
1126
  }
1064
- result = new Function("useRef", `return \`${result}\``)(useRef)
1127
+ result = new Function("useRef", `return \`${result}\``)(useRef);
1065
1128
 
1066
1129
  if (!result.trim().startsWith("<body>")) {
1067
1130
  console.warn(
1068
1131
  "You should wrap your html in a body tag, vader may not grab all html!"
1069
1132
  );
1070
1133
  }
1071
-
1072
-
1073
-
1074
- return this.parseHTML(result);
1075
- }
1076
1134
 
1077
-
1135
+ return this.parseHTML(result);
1136
+ }
1078
1137
  }
1079
1138
  // write types to ensure it returns a string
1080
1139
  /**
@@ -1133,10 +1192,15 @@ const Vader = {
1133
1192
  * }
1134
1193
  */
1135
1194
  Component: Component,
1136
- useRef: useRef,
1195
+ useRef: useRef
1137
1196
  };
1138
- export const component = (name) => {
1139
- return new Component();
1197
+ /**
1198
+ * @function component
1199
+ * @description Allows you to create a component
1200
+ * @returns {Component}
1201
+ */
1202
+ export const component = () => {
1203
+ return new Component()
1140
1204
  };
1141
1205
 
1142
1206
  /**
@@ -1151,81 +1215,85 @@ export const rf = (name, fn) => {
1151
1215
  window[name] = fn;
1152
1216
  };
1153
1217
  let cache = {};
1154
- async function handletemplate(data){
1218
+ async function handletemplate(data) {
1155
1219
  let dom = new DOMParser().parseFromString(data, "text/html");
1156
1220
  let elements = dom.documentElement.querySelectorAll("*");
1157
-
1221
+
1158
1222
  if (elements.length > 0) {
1159
1223
  for (var i = 0; i < elements.length; i++) {
1160
-
1161
1224
  if (elements[i].nodeName === "INCLUDE") {
1162
- if(!elements[i].getAttribute("src") || elements[i].getAttribute("src") === ""){
1163
- throw new Error("Include tag must have src attribute")
1225
+ if (
1226
+ !elements[i].getAttribute("src") ||
1227
+ elements[i].getAttribute("src") === ""
1228
+ ) {
1229
+ throw new Error("Include tag must have src attribute");
1164
1230
  }
1165
-
1166
- let componentName = elements[i].getAttribute("src")?.split("/").pop()?.split(".")[0]
1167
- // @ts-ignore
1168
- let filedata = await include(elements[i].getAttribute("src"))
1169
- // replace ` with \`\` to allow for template literals
1170
- filedata = filedata.replace(/`/g, "\\`")
1171
- cache[elements[i].getAttribute("src")] = filedata
1172
- filedata = new Function(`return \`${filedata}\`;`)();
1173
- let newdom = new DOMParser().parseFromString(filedata, "text/html");
1174
-
1175
- newdom.querySelectorAll("include").forEach((el)=>{
1176
- el.remove()
1177
- })
1178
- // @ts-ignore
1179
-
1180
- let els = dom.querySelectorAll(componentName)
1181
-
1182
- els.forEach((el)=>{
1183
-
1184
- if(el.attributes.length > 0){
1185
- for(var i = 0; i < el.attributes.length; i++){
1186
- newdom.body.outerHTML = newdom.body.outerHTML.replace(`{{${el.attributes[i].name}}}`, el.attributes[i].value)
1187
- }
1188
-
1189
- }
1190
- if(el.children.length > 0 && newdom.body.querySelector('slot')){
1191
- for(var i = 0; i < el.children.length; i++){
1192
- let slots = newdom.body.querySelectorAll("slot")
1193
- slots.forEach((slot)=>{
1194
- let id = slot.getAttribute("id")
1195
- if(id === el.nodeName.toLowerCase()){
1196
- slot.outerHTML = `<div>${el.innerHTML}</div>`
1197
- }
1198
- })
1199
-
1200
-
1231
+
1232
+ let componentName = elements[i]
1233
+ .getAttribute("src")
1234
+ ?.split("/")
1235
+ .pop()
1236
+ ?.split(".")[0];
1237
+ // @ts-ignore
1238
+ let filedata = await include(elements[i].getAttribute("src"));
1239
+ // replace ` with \`\` to allow for template literals
1240
+ filedata = filedata.replace(/`/g, "\\`");
1241
+ cache[elements[i].getAttribute("src")] = filedata;
1242
+ filedata = new Function(`return \`${filedata}\`;`)();
1243
+ let newdom = new DOMParser().parseFromString(filedata, "text/html");
1244
+
1245
+
1246
+ newdom.querySelectorAll("include").forEach((el) => {
1247
+ el.remove();
1248
+ });
1249
+ // @ts-ignore
1250
+
1251
+ let els = dom.querySelectorAll(componentName);
1252
+
1253
+ els.forEach((el) => {
1254
+ if (el.attributes.length > 0) {
1255
+ for (var i = 0; i < el.attributes.length; i++) {
1256
+ // @ts-ignore
1257
+ newdom.body.outerHTML = newdom.body.outerHTML.replaceAll(
1258
+ `{{${el.attributes[i].name}}}`,
1259
+ el.attributes[i].value
1260
+ );
1261
+ }
1262
+ }
1263
+ if (el.children.length > 0 && newdom.body.querySelector("slot")) {
1264
+ for (var i = 0; i < el.children.length; i++) {
1265
+ let slots = newdom.body.querySelectorAll("slot");
1266
+ slots.forEach((slot) => {
1267
+
1268
+ let id = slot.getAttribute("id");
1269
+ console.log(id)
1270
+ if(el.hasAttribute('key') && el.getAttribute('key') === id || !el.hasAttribute('key') && el.nodeName === id){
1271
+
1272
+ slot.outerHTML = `<div>${el.innerHTML}</div>`
1201
1273
  }
1202
-
1203
- }
1204
-
1205
- dom.body.querySelectorAll('include').forEach((el)=>{
1206
- el.remove()
1207
- })
1208
- // replace ` with \`\` to allow for template literals
1209
- dom.body.outerHTML = dom.body.outerHTML.replace(/`/g, "\\`")
1210
- dom.body.outerHTML = dom.body.outerHTML.replace(el.outerHTML, new Function(`return \`${newdom.body.outerHTML}\`;`)())
1211
-
1212
-
1213
- })
1214
-
1215
-
1216
-
1274
+ });
1275
+ }
1276
+ }
1217
1277
 
1278
+ dom.body.querySelectorAll("include").forEach((el) => {
1279
+ el.remove();
1280
+ });
1281
+ // replace ` with \`\` to allow for template literals
1282
+ dom.body.outerHTML = dom.body.outerHTML.replace(/`/g, "\\`");
1283
+ dom.body.outerHTML = dom.body.outerHTML.replace(
1284
+ el.outerHTML,
1285
+ new Function(`return \`${newdom.body.outerHTML}\`;`)()
1286
+ );
1287
+ });
1218
1288
  }
1219
1289
  }
1220
-
1221
-
1222
1290
  }
1223
-
1291
+
1224
1292
  // replace ` with \`\` to allow for template literals
1225
- dom.body.outerHTML = dom.body.outerHTML.replace(/`/g, "\\`")
1293
+ dom.body.outerHTML = dom.body.outerHTML.replace(/`/g, "\\`");
1226
1294
  data = new Function(`return \`${dom.body.outerHTML}\`;`)();
1227
-
1228
- return data;
1295
+
1296
+ return data;
1229
1297
  }
1230
1298
  /**
1231
1299
  * @function include
@@ -1234,10 +1302,7 @@ async function handletemplate(data){
1234
1302
  * @param {string} path
1235
1303
  */
1236
1304
 
1237
-
1238
-
1239
1305
  export const include = async (path) => {
1240
-
1241
1306
  if (
1242
1307
  path.startsWith("/") &&
1243
1308
  !path.includes("/src/") &&
@@ -1248,25 +1313,23 @@ export const include = async (path) => {
1248
1313
  path = "/src/" + path;
1249
1314
  }
1250
1315
  if (cache[path]) {
1251
- return await handletemplate(new Function(`return \`${cache[path]}\`;`)())
1252
-
1253
- }else{
1316
+ return await handletemplate(new Function(`return \`${cache[path]}\`;`)());
1317
+ } else {
1254
1318
  return fetch(`./${path}`)
1255
- .then((res) => {
1256
- if (res.status === 404) {
1257
- throw new Error(`No file found at ${path}`);
1258
- }
1259
- return res.text();
1260
- })
1261
- .then(async (data) => {
1262
- cache[path] = data
1263
-
1264
- data = await handletemplate(new Function(`return \`${data}\`;`)())
1265
-
1266
- return data
1267
- });
1319
+ .then((res) => {
1320
+ if (res.status === 404) {
1321
+ throw new Error(`No file found at ${path}`);
1322
+ }
1323
+ return res.text();
1324
+ })
1325
+ .then(async (data) => {
1326
+ cache[path] = data;
1327
+
1328
+ data = await handletemplate(new Function(`return \`${data}\`;`)());
1329
+
1330
+ return data;
1331
+ });
1268
1332
  }
1269
-
1270
1333
  };
1271
1334
 
1272
1335
  export default Vader;