vaderjs 1.3.3-alpha-17 → 1.3.3-alpha-18

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/client/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  window.params = {};
3
3
  window.Vader = {
4
- version: "1.3.3",
4
+ version: "1.3.2",
5
5
  };
6
6
 
7
7
  let errors = {
@@ -42,12 +42,6 @@ let invokes = []
42
42
  let hasran = [];
43
43
  let states = {};
44
44
  let mounts = [];
45
- /**
46
- * @method strictMount
47
- * @description This method allows you to call a function only when a component has been mounted
48
- * @param {*} key
49
- * @param {*} callback
50
- */
51
45
  export const strictMount = (key, callback) => {
52
46
  let interval = setInterval(() => {
53
47
  if(mounts.find(mount => mount.key === key)
@@ -61,11 +55,54 @@ export const strictMount = (key, callback) => {
61
55
  },0);
62
56
  };
63
57
 
64
-
58
+ window.delegate = (event) => {
59
+ return event.detail.target
60
+ }
65
61
 
66
62
  let components = {};
67
63
 
68
-
64
+ let style = document.createElement("style");
65
+ document.head.appendChild(style);
66
+
67
+ const parseStyles = async (styles, className = '') => {
68
+ let css = await fetch(styles).then((res) => res.text());
69
+ let classes = css.split("}");
70
+ let parsedClasses = {};
71
+ classes.forEach((cls) => {
72
+
73
+ let name = cls.split(".")[1];
74
+ let value = cls.split("{")[1]
75
+ let keys = value.split(";");
76
+ let newKeys = [];
77
+ keys.forEach((key) => {
78
+ if (key.includes(":")) {
79
+ let newKey = key.split(":")[0].trim();
80
+ let newValue = key.split(":")[1].trim();
81
+ newKeys.push(`${newKey}: "${newValue}"`);
82
+ }
83
+ });
84
+ value = `{${newKeys.join(",")}}`;
85
+
86
+
87
+ parsedClasses[name] = JSON.stringify(value);
88
+ });
89
+ return parsedClasses;
90
+ };
91
+
92
+
93
+ export const stylis = {
94
+ /**
95
+ * @method create
96
+ * @param {*} styles
97
+ * @returns {Object} classes
98
+ * @description This method allows you to create css classes from an object
99
+ */
100
+ create: async (/**@type {string} */ styles) => {
101
+
102
+ return await parseStyles(styles);
103
+ },
104
+ };
105
+
69
106
  /**
70
107
  * @method mem
71
108
  * @param {Component} component
@@ -101,7 +138,18 @@ export const mem = (/**@type {Component}**/ component) => {
101
138
 
102
139
  let functions = {};
103
140
 
104
-
141
+ export const invoke = (func, params) => {
142
+ let name = func.name;
143
+
144
+ window[name] = function (params) {
145
+ return func(params);
146
+ }
147
+ window[name] = window[name].bind(this);
148
+
149
+
150
+ return `${name}(${params})`;
151
+
152
+ };
105
153
 
106
154
  /**
107
155
  * Represents a component in the Vader framework.
@@ -114,37 +162,15 @@ export class Component {
114
162
  this.state = {};
115
163
  this.key = null;
116
164
  this.components = {};
117
- this.mounted = false;
118
- /**
119
- * @private
120
- */
121
- this.checkIFMounted();
165
+ this.mounted = false;
122
166
  this.currenthtml = null;
123
- /**
124
- * @private
125
- */
126
167
  window.listeners = [];
127
- /**
128
- * @private
129
- */
130
168
  this.functionMap = new Map();
131
- /**
132
- * @private
133
- */
134
169
  this.freeMemoryFromFunctions();
135
- /**
136
- * @private
137
- */
138
170
  this.memoizes = []
139
171
  this.children = []
140
- /**
141
- * @property {Object} props - The component props.
142
- */
143
- this.props = {}
144
172
  }
145
- /**
146
- * @private
147
- */
173
+
148
174
  createComponent(/**@type {Component}**/component, props, children) {
149
175
 
150
176
  if (!component) {
@@ -154,7 +180,20 @@ export class Component {
154
180
  throw new Error('new components must have a key')
155
181
  }
156
182
  let comp = new component();
157
-
183
+ Object.keys(props).forEach((key) => {
184
+ // some props are this.bind inside of "" so we need to handle that
185
+ let newFunc = props[key].match(/this.bind\((.*)\)/gs);
186
+ if (newFunc) {
187
+ newFunc = newFunc[0].replace(/this.bind\((.*)\)/gs, "$1");
188
+ newFunc = newFunc.split('`')[1]
189
+ props[key] = this.bind(newFunc, { jsx: true, params: props[key], ref: props.ref });
190
+ return
191
+ }
192
+
193
+
194
+
195
+ props[key] = props[key].toString().replace('"', '').replace("'", '').replace('${', '').replace('}', '')
196
+ });
158
197
  comp['props'] = props;
159
198
  comp.children = children;
160
199
  comp.props.children = children.join('')
@@ -164,9 +203,6 @@ export class Component {
164
203
  this.children.push(comp)
165
204
  return this.components[props.key]
166
205
  }
167
- /**
168
- * @private
169
- */
170
206
  memoize(/**@type {Component}**/component){
171
207
  if(!component.key){
172
208
  throw new Error('Component must have a static key')
@@ -187,9 +223,6 @@ export class Component {
187
223
 
188
224
  return `<div key="${component.key}">${h}</div>`
189
225
  }
190
- /**
191
- * @private
192
- */
193
226
  parseStyle(styles){
194
227
  let css = ''
195
228
  Object.keys(styles).forEach((key) => {
@@ -199,9 +232,6 @@ export class Component {
199
232
  })
200
233
  return css
201
234
  }
202
- /**
203
- * @private
204
- */
205
235
  bindMount(){
206
236
  mounts.push(this)
207
237
  }
@@ -268,15 +298,13 @@ export class Component {
268
298
  * @param {string} p - The parameter.
269
299
  * @param {string} ref - The reference.
270
300
  * @returns {string} - A valid inline JS function call.
271
- * @private
272
301
  */
273
302
  bind(funcData, d) {
274
303
 
275
304
  const name = `func_${crypto ? crypto.getRandomValues(new Uint32Array(1))[0] : Math.random()}`;
276
305
 
277
306
  var dynamicFunction = (params) => {
278
- let func = new Function(`return (async (${params}) => {
279
- console.log('called')
307
+ let func = new Function(`return (async () => {
280
308
  ${funcData}
281
309
  })()`);
282
310
  func = func.bind(this);
@@ -324,7 +352,6 @@ export class Component {
324
352
  * Calls a function with the specified parameters. and dispatches an event.
325
353
  * @param {string} func - The function name.
326
354
  * @param {...*} params - The function parameters.
327
- * @private
328
355
  */
329
356
  callFunction(func, isInlineJsx, ...params) {
330
357
  if(!isInlineJsx && params[0] && params[0].detail){
@@ -342,7 +369,6 @@ export class Component {
342
369
  * @param {string} params - The function parameters.
343
370
  * @param {boolean} [isInlineJsx=false] - Indicates if the function is an inline JSX.
344
371
  * @returns {string} - The function call.
345
- * @private
346
372
  */
347
373
  useFunction(func, params, isInlineJsx = false) {
348
374
  const sanitizedFuncName = func.name.trim().replace(/\s+/g, '_');
@@ -514,20 +540,6 @@ export const useState = (initialState) => {
514
540
  return [value, (newValue) => {}];
515
541
  };
516
542
 
517
- export const useRef = (initialState) => {
518
- let value = initialState;
519
- return {
520
- /**
521
- * @property {Bind} bind - Bind the current ref key to the element
522
- */
523
- bind: key,
524
- /**
525
- * @property {Function} current - Get the current ref value - or the element with the ref key
526
- */
527
- current: () => document.querySelector(`[ref="${key}"]`) || value,
528
- }
529
- }
530
-
531
543
  const constants = {};
532
544
  let constantCounter = 0;
533
545
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "vaderjs",
3
3
  "description": "A Reactive library aimed to helping you build reactive applications inspired by react.js",
4
4
  "module": "vader.js",
5
- "version": "1.3.3-alpha-17",
5
+ "version": "1.3.3-alpha-18",
6
6
  "bin": {
7
7
  "vader": "./vader.js"
8
8
  },
package/runtime/vader.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  window.params = {};
3
3
  window.Vader = {
4
- version: "1.3.3",
4
+ version: "1.3.2",
5
5
  };
6
6
 
7
7
  let errors = {
@@ -42,12 +42,6 @@ let invokes = []
42
42
  let hasran = [];
43
43
  let states = {};
44
44
  let mounts = [];
45
- /**
46
- * @method strictMount
47
- * @description This method allows you to call a function only when a component has been mounted
48
- * @param {*} key
49
- * @param {*} callback
50
- */
51
45
  export const strictMount = (key, callback) => {
52
46
  let interval = setInterval(() => {
53
47
  if(mounts.find(mount => mount.key === key)
@@ -61,11 +55,54 @@ export const strictMount = (key, callback) => {
61
55
  },0);
62
56
  };
63
57
 
64
-
58
+ window.delegate = (event) => {
59
+ return event.detail.target
60
+ }
65
61
 
66
62
  let components = {};
67
63
 
68
-
64
+ let style = document.createElement("style");
65
+ document.head.appendChild(style);
66
+
67
+ const parseStyles = async (styles, className = '') => {
68
+ let css = await fetch(styles).then((res) => res.text());
69
+ let classes = css.split("}");
70
+ let parsedClasses = {};
71
+ classes.forEach((cls) => {
72
+
73
+ let name = cls.split(".")[1];
74
+ let value = cls.split("{")[1]
75
+ let keys = value.split(";");
76
+ let newKeys = [];
77
+ keys.forEach((key) => {
78
+ if (key.includes(":")) {
79
+ let newKey = key.split(":")[0].trim();
80
+ let newValue = key.split(":")[1].trim();
81
+ newKeys.push(`${newKey}: "${newValue}"`);
82
+ }
83
+ });
84
+ value = `{${newKeys.join(",")}}`;
85
+
86
+
87
+ parsedClasses[name] = JSON.stringify(value);
88
+ });
89
+ return parsedClasses;
90
+ };
91
+
92
+
93
+ export const stylis = {
94
+ /**
95
+ * @method create
96
+ * @param {*} styles
97
+ * @returns {Object} classes
98
+ * @description This method allows you to create css classes from an object
99
+ */
100
+ create: async (/**@type {string} */ styles) => {
101
+
102
+ return await parseStyles(styles);
103
+ },
104
+ };
105
+
69
106
  /**
70
107
  * @method mem
71
108
  * @param {Component} component
@@ -101,7 +138,18 @@ export const mem = (/**@type {Component}**/ component) => {
101
138
 
102
139
  let functions = {};
103
140
 
104
-
141
+ export const invoke = (func, params) => {
142
+ let name = func.name;
143
+
144
+ window[name] = function (params) {
145
+ return func(params);
146
+ }
147
+ window[name] = window[name].bind(this);
148
+
149
+
150
+ return `${name}(${params})`;
151
+
152
+ };
105
153
 
106
154
  /**
107
155
  * Represents a component in the Vader framework.
@@ -114,37 +162,15 @@ export class Component {
114
162
  this.state = {};
115
163
  this.key = null;
116
164
  this.components = {};
117
- this.mounted = false;
118
- /**
119
- * @private
120
- */
121
- this.checkIFMounted();
165
+ this.mounted = false;
122
166
  this.currenthtml = null;
123
- /**
124
- * @private
125
- */
126
167
  window.listeners = [];
127
- /**
128
- * @private
129
- */
130
168
  this.functionMap = new Map();
131
- /**
132
- * @private
133
- */
134
169
  this.freeMemoryFromFunctions();
135
- /**
136
- * @private
137
- */
138
170
  this.memoizes = []
139
171
  this.children = []
140
- /**
141
- * @property {Object} props - The component props.
142
- */
143
- this.props = {}
144
172
  }
145
- /**
146
- * @private
147
- */
173
+
148
174
  createComponent(/**@type {Component}**/component, props, children) {
149
175
 
150
176
  if (!component) {
@@ -154,7 +180,20 @@ export class Component {
154
180
  throw new Error('new components must have a key')
155
181
  }
156
182
  let comp = new component();
157
-
183
+ Object.keys(props).forEach((key) => {
184
+ // some props are this.bind inside of "" so we need to handle that
185
+ let newFunc = props[key].match(/this.bind\((.*)\)/gs);
186
+ if (newFunc) {
187
+ newFunc = newFunc[0].replace(/this.bind\((.*)\)/gs, "$1");
188
+ newFunc = newFunc.split('`')[1]
189
+ props[key] = this.bind(newFunc, { jsx: true, params: props[key], ref: props.ref });
190
+ return
191
+ }
192
+
193
+
194
+
195
+ props[key] = props[key].toString().replace('"', '').replace("'", '').replace('${', '').replace('}', '')
196
+ });
158
197
  comp['props'] = props;
159
198
  comp.children = children;
160
199
  comp.props.children = children.join('')
@@ -164,9 +203,6 @@ export class Component {
164
203
  this.children.push(comp)
165
204
  return this.components[props.key]
166
205
  }
167
- /**
168
- * @private
169
- */
170
206
  memoize(/**@type {Component}**/component){
171
207
  if(!component.key){
172
208
  throw new Error('Component must have a static key')
@@ -187,9 +223,6 @@ export class Component {
187
223
 
188
224
  return `<div key="${component.key}">${h}</div>`
189
225
  }
190
- /**
191
- * @private
192
- */
193
226
  parseStyle(styles){
194
227
  let css = ''
195
228
  Object.keys(styles).forEach((key) => {
@@ -199,9 +232,6 @@ export class Component {
199
232
  })
200
233
  return css
201
234
  }
202
- /**
203
- * @private
204
- */
205
235
  bindMount(){
206
236
  mounts.push(this)
207
237
  }
@@ -268,15 +298,13 @@ export class Component {
268
298
  * @param {string} p - The parameter.
269
299
  * @param {string} ref - The reference.
270
300
  * @returns {string} - A valid inline JS function call.
271
- * @private
272
301
  */
273
302
  bind(funcData, d) {
274
303
 
275
304
  const name = `func_${crypto ? crypto.getRandomValues(new Uint32Array(1))[0] : Math.random()}`;
276
305
 
277
306
  var dynamicFunction = (params) => {
278
- let func = new Function(`return (async (${params}) => {
279
- console.log('called')
307
+ let func = new Function(`return (async () => {
280
308
  ${funcData}
281
309
  })()`);
282
310
  func = func.bind(this);
@@ -324,7 +352,6 @@ export class Component {
324
352
  * Calls a function with the specified parameters. and dispatches an event.
325
353
  * @param {string} func - The function name.
326
354
  * @param {...*} params - The function parameters.
327
- * @private
328
355
  */
329
356
  callFunction(func, isInlineJsx, ...params) {
330
357
  if(!isInlineJsx && params[0] && params[0].detail){
@@ -342,7 +369,6 @@ export class Component {
342
369
  * @param {string} params - The function parameters.
343
370
  * @param {boolean} [isInlineJsx=false] - Indicates if the function is an inline JSX.
344
371
  * @returns {string} - The function call.
345
- * @private
346
372
  */
347
373
  useFunction(func, params, isInlineJsx = false) {
348
374
  const sanitizedFuncName = func.name.trim().replace(/\s+/g, '_');
@@ -514,20 +540,6 @@ export const useState = (initialState) => {
514
540
  return [value, (newValue) => {}];
515
541
  };
516
542
 
517
- export const useRef = (initialState) => {
518
- let value = initialState;
519
- return {
520
- /**
521
- * @property {Bind} bind - Bind the current ref key to the element
522
- */
523
- bind: key,
524
- /**
525
- * @property {Function} current - Get the current ref value - or the element with the ref key
526
- */
527
- current: () => document.querySelector(`[ref="${key}"]`) || value,
528
- }
529
- }
530
-
531
543
  const constants = {};
532
544
  let constantCounter = 0;
533
545
 
package/vader.js CHANGED
@@ -223,11 +223,9 @@ function Compiler(func) {
223
223
  otherdata["ref"] = ref;
224
224
 
225
225
  newvalue = newvalue.split('\n').map(line => line.trim() ? line.trim() + ';' : line).join('\n');
226
- newvalue = newvalue.replaceAll('"', "'")
227
- otherdata = JSON.stringify(otherdata).replaceAll('"', "'")
228
- let newatribute = `${attributeName}="\${this.bind(\`${newvalue}\`${isJSXComponent ? "" : ","}${otherdata} )}"`;
229
-
230
-
226
+
227
+ let newatribute = `${attributeName}="\${this.bind(\`${newvalue}\`, {jsx: ${isJSXComponent}, ref: '${ref}'})}"`;
228
+
231
229
  attribute[attributeName] = {
232
230
  old: old,
233
231
  new: newatribute,
@@ -261,7 +259,9 @@ function Compiler(func) {
261
259
  otherdata["ref"] = ref;
262
260
  // since js is all in one line split it
263
261
  newvalue = newvalue.split('\n').map(line => line.trim() ? line.trim() + ';' : line).join('\n');
262
+
264
263
  let newattribute = `${attributeName}="\${this.bind(\`${newvalue}\` ${isJSXComponent ? "" : ","}${JSON.stringify(otherdata)} )}"`;
264
+ console.log(newattribute)
265
265
  newattribute = newattribute.replace(/\s+/g, " ")
266
266
  string = string.replace(old, newattribute);
267
267
  }
@@ -499,7 +499,28 @@ function Compiler(func) {
499
499
 
500
500
  let name = component.split("<")[1].split(">")[0].split(" ")[0].replace("/", "");
501
501
  let props = component.split(`<${name}`)[1].split(">")[0].trim();
502
-
502
+ function parseProps(attributes) {
503
+ console.log(attributes)
504
+ let props = {};
505
+ if (attributes) {
506
+ let propsMatch;
507
+ let regex = /([a-zA-Z0-9_-]+)(\s*=\s*("([^"\\]*(\\.[^"\\]*)*)"|'([^'\\]*(\\.[^'\\]*)*)'|\{(?:[^{}]|(?:\{(?:[^{}]|(?:\{[^{}]*\})*)*\})*)*\}|(?:\([^)]*\)|\{[^}]*\}|()=>\s*(?:\{[^}]*\})?)|\[[^\]]*\]))?/gs
508
+ while ((propsMatch = regex.exec(attributes)) !== null) {
509
+ let [, attributeName, attributeValue] = propsMatch;
510
+ console.log(attributeName, attributeValue)
511
+ attributeValue = attributeValue.replaceAll('="', '').replace("='", '')
512
+ if(attributeValue.startsWith("'")){
513
+ attributeValue = attributeValue.replace("'", '')
514
+ }
515
+ props[attributeName] = attributeValue || null;
516
+ }
517
+
518
+ }
519
+
520
+ return props;
521
+ }
522
+
523
+
503
524
  let savedname = name;
504
525
  let children = props
505
526
  ? component
@@ -534,20 +555,14 @@ function Compiler(func) {
534
555
  myChildrens.push(child.children);
535
556
  }
536
557
  });
537
-
558
+
559
+ props = JSON.stringify(parseProps(props));
538
560
 
539
- // turn props into object
540
- props = props.replaceAll('=', ':').replaceAll('/', '')
541
-
542
- // class="text-red-500" => class:"text-red-500", omit quotes if value is a variable
543
- props = props.replaceAll(/([a-zA-Z0-9_-]+)\s*:\s*("([^"\\]*(\\.[^"\\]*)*)"|'([^'\\]*(\\.[^'\\]*)*)')/gs, (match, p1, p2) => {
544
- return match + ','
545
- });
546
561
  let replace = "";
547
562
  replace = isChild
548
- ? `this.memoize(this.createComponent(${savedname.replaceAll('/', '')}, {${props}}, [${myChildrens.length > 0 ? myChildrens.join(",") : ""
563
+ ? `this.memoize(this.createComponent(${savedname.replaceAll('/', '')}, ${props} , [${myChildrens.length > 0 ? myChildrens.join(",") : ""
549
564
  }])),`
550
- : `\${this.memoize(this.createComponent(${savedname.replaceAll('/', '')}, {${props}}, [${myChildrens.length > 0 ? myChildrens.join(",") : ""
565
+ : `\${this.memoize(this.createComponent(${savedname.replaceAll('/', '')}, ${props} , [${myChildrens.length > 0 ? myChildrens.join(",") : ""
551
566
  }]))}`;
552
567
 
553
568
  body = body.replace(before, replace);