vaderjs 1.1.9 → 1.2.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/index.js +5 -5
- package/package.json +1 -1
- package/readme.md +7 -6
- package/vader.js +124 -13
package/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
1
|
/**
|
|
3
2
|
* @file index.js
|
|
4
3
|
* @description This is the entry point for the library. This file exports all the necessary classes and functions.
|
|
5
4
|
* @version 1.1.5
|
|
6
|
-
*
|
|
5
|
+
*
|
|
7
6
|
*/
|
|
8
|
-
import Vader from
|
|
7
|
+
import Vader, { include, useRef } from './vader.js';
|
|
9
8
|
// @ts-ignore
|
|
10
|
-
import
|
|
9
|
+
import VaderRouter from './vaderRouter.js';
|
|
11
10
|
|
|
12
|
-
export {
|
|
11
|
+
export { include, VaderRouter };
|
|
12
|
+
export default Vader;
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -47,7 +47,7 @@ or
|
|
|
47
47
|
- Then you can import like this
|
|
48
48
|
|
|
49
49
|
```js
|
|
50
|
-
import
|
|
50
|
+
import Vader, { VaderRouter, include } from 'vaderjs'
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
3. Use VaderJS features for routing, state management, auth, and more.
|
|
@@ -65,7 +65,7 @@ or
|
|
|
65
65
|
### Declarative Routing
|
|
66
66
|
|
|
67
67
|
```javascript
|
|
68
|
-
import { VaderRouter
|
|
68
|
+
import { VaderRouter } from 'vaderjs'
|
|
69
69
|
import { MyComponent } from './components/my-component.js'
|
|
70
70
|
const app = new VaderRouter('/') // intial route
|
|
71
71
|
|
|
@@ -89,7 +89,7 @@ app.start()
|
|
|
89
89
|
### State Management
|
|
90
90
|
|
|
91
91
|
```javascript
|
|
92
|
-
import
|
|
92
|
+
import Vader from "vaderjs"
|
|
93
93
|
|
|
94
94
|
class MyApp extends Vader.Component{
|
|
95
95
|
contructor(){
|
|
@@ -158,11 +158,12 @@ window.addEventListener('signalDispatch', (e)=>{
|
|
|
158
158
|
### Function Binding
|
|
159
159
|
|
|
160
160
|
```javascript
|
|
161
|
+
|
|
161
162
|
const fn = this.$Function(function fn() {
|
|
162
163
|
console.log("Hello World");
|
|
163
164
|
});
|
|
164
165
|
|
|
165
|
-
return html`<button onclick="${fn}">Click Me</button>`
|
|
166
|
+
return html(`<button onclick="${fn}">Click Me</button>`)
|
|
166
167
|
```
|
|
167
168
|
|
|
168
169
|
### Authentication & Authorization
|
|
@@ -181,7 +182,7 @@ if (auth.can('edit')) {
|
|
|
181
182
|
### Simplified Component Creation
|
|
182
183
|
|
|
183
184
|
```javascript
|
|
184
|
-
import
|
|
185
|
+
import Vader from 'vaderjs';
|
|
185
186
|
|
|
186
187
|
export class App extends Vader.Component{
|
|
187
188
|
constructor(){
|
|
@@ -209,7 +210,7 @@ ${
|
|
|
209
210
|
|
|
210
211
|
```js
|
|
211
212
|
// home.js
|
|
212
|
-
import
|
|
213
|
+
import Vader from "vaderjs";
|
|
213
214
|
|
|
214
215
|
class Home extends Vader.Component {
|
|
215
216
|
constructor() {
|
package/vader.js
CHANGED
|
@@ -1,10 +1,108 @@
|
|
|
1
1
|
let dom = /**@type {Obect} **/ {};
|
|
2
|
+
let states = {};
|
|
3
|
+
/**
|
|
4
|
+
* @function markdown
|
|
5
|
+
* @param {String} content
|
|
6
|
+
* @description Allows you to convert markdown to html
|
|
7
|
+
*/
|
|
8
|
+
function markdown(content) {
|
|
9
|
+
const lines = content.split('\n');
|
|
10
|
+
let result = '';
|
|
11
|
+
|
|
12
|
+
lines.forEach((line) => {
|
|
13
|
+
let heading = line.match(/^#{1,6}\s/);
|
|
14
|
+
let bold = line.match(/\*\*(.*?)\*\*/g);
|
|
15
|
+
let italic = line.match(/\*(.*?)\*/g);
|
|
16
|
+
|
|
17
|
+
let link = line.match(/\[(.*?)\]\((.*?)\)/g);
|
|
18
|
+
let ul = line.match(/^\-\s/);
|
|
19
|
+
let ol = line.match(/^\d\.\s/);
|
|
20
|
+
let hr = line.match(/^\-\-\-\s/);
|
|
21
|
+
let blockquote = line.match(/^\>\s/);
|
|
22
|
+
let image = line.match(/\!\[(.*?)\]\((.*?)\)/g);
|
|
23
|
+
|
|
24
|
+
let codeBlock = line.match(/\`\`\`/g);
|
|
25
|
+
let codeBlockEnd = line.match(/\`\`\`/g);
|
|
26
|
+
|
|
27
|
+
if (heading) {
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
let headingLevel = heading[0].match(/#/g).length;
|
|
30
|
+
line = line.replace(heading[0], `<h${headingLevel}>`);
|
|
31
|
+
line += `</h${headingLevel}>`;
|
|
32
|
+
}
|
|
33
|
+
if (bold) {
|
|
34
|
+
bold.forEach((b) => {
|
|
35
|
+
line = line.replace(b, `<strong>${b.replace(/\*\*/g, "")}</strong>`);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (italic) {
|
|
39
|
+
italic.forEach((i) => {
|
|
40
|
+
line = line.replace(i, `<em>${i.replace(/\*/g, "")}</em>`);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
if(link){
|
|
46
|
+
link.forEach((l) => {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
let text = l.match(/\[(.*?)\]/g)[0].replace(/\[|\]/g, "");
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
let href = l.match(/\((.*?)\)/g)[0].replace(/\(|\)/g, "");
|
|
51
|
+
line = line.replace(l, `<a href="${href}">${text}</a>`);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (ul) {
|
|
55
|
+
line = line.replace(ul[0], `<li
|
|
56
|
+
style="list-style-type: disc;"
|
|
57
|
+
>`);
|
|
58
|
+
line += `</li>`;
|
|
59
|
+
}
|
|
60
|
+
if (ol) {
|
|
61
|
+
line = line.replace(ol[0], `<li
|
|
62
|
+
style="list-style-type: decimal;"
|
|
63
|
+
>`);
|
|
64
|
+
line += `</li>`;
|
|
65
|
+
}
|
|
66
|
+
if (hr) {
|
|
67
|
+
line = line.replace(hr[0], `<hr/>`);
|
|
68
|
+
}
|
|
69
|
+
if (blockquote) {
|
|
70
|
+
line = line.replace(blockquote[0], `<blockquote>`);
|
|
71
|
+
line += `</blockquote>`;
|
|
72
|
+
}
|
|
73
|
+
if (image) {
|
|
74
|
+
image.forEach((i) => {
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
let alt = i.match(/\[(.*?)\]/g)[0].replace(/\[|\]/g, "");
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
let src = i.match(/\((.*?)\)/g)[0].replace(/\(|\)/g, "");
|
|
79
|
+
i.replace(i, `<img src="${src}" alt="${alt}"/>`);
|
|
80
|
+
line = line.replace(i, `<img src="${src}" alt="${alt}"/>`).replace('!','')
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if(codeBlock){
|
|
84
|
+
line = line.replace(codeBlock[0], `<pre><code>`);
|
|
85
|
+
}
|
|
86
|
+
if(codeBlockEnd){
|
|
87
|
+
line = line.replace(codeBlockEnd[0], `</code></pre>`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
result += `${line}\n`;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
2
99
|
/**
|
|
3
100
|
* @function useRef
|
|
4
101
|
* @description Allows you to get reference to DOM element
|
|
5
102
|
* @param {String} ref
|
|
6
103
|
* @returns {void | Object} {current, update}
|
|
7
104
|
*/
|
|
105
|
+
|
|
8
106
|
export const useRef = (ref) => {
|
|
9
107
|
const element = document.querySelector(`[ref="${ref}"]`);
|
|
10
108
|
const getElement = () => element;
|
|
@@ -441,7 +539,7 @@ export class Component {
|
|
|
441
539
|
});
|
|
442
540
|
}) ||
|
|
443
541
|
{},
|
|
444
|
-
|
|
542
|
+
|
|
445
543
|
);
|
|
446
544
|
|
|
447
545
|
const getField = (fieldName) => {
|
|
@@ -472,7 +570,7 @@ export class Component {
|
|
|
472
570
|
* @description Allows you to create a state
|
|
473
571
|
* @param {String} key
|
|
474
572
|
* @param {*} initialValue
|
|
475
|
-
* @param {
|
|
573
|
+
* @param {*} callback
|
|
476
574
|
* @url - useState works similarly to - https://react.dev/reference/react/useState
|
|
477
575
|
* @returns {Array} [state, setState]
|
|
478
576
|
* @example
|
|
@@ -482,10 +580,13 @@ export class Component {
|
|
|
482
580
|
*
|
|
483
581
|
* setCount(count + 1)
|
|
484
582
|
*/
|
|
485
|
-
useState(key, initialValue, callback) {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
583
|
+
useState(key, initialValue, callback = null) {
|
|
584
|
+
|
|
585
|
+
if(!this.states[key]){
|
|
586
|
+
this.states[key] = initialValue;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
|
|
489
590
|
return [
|
|
490
591
|
this.states[key],
|
|
491
592
|
/**
|
|
@@ -497,6 +598,7 @@ export class Component {
|
|
|
497
598
|
(value) => {
|
|
498
599
|
this.states[key] = value;
|
|
499
600
|
this.updateComponent();
|
|
601
|
+
// @ts-ignore
|
|
500
602
|
typeof callback === "function" ? callback() : null;
|
|
501
603
|
},
|
|
502
604
|
];
|
|
@@ -690,6 +792,7 @@ export class Component {
|
|
|
690
792
|
* @see {@link Component}
|
|
691
793
|
* @see {@link Component#componentDidMount}
|
|
692
794
|
*/
|
|
795
|
+
|
|
693
796
|
html(strings, ...args) {
|
|
694
797
|
// @ts-ignore
|
|
695
798
|
if (
|
|
@@ -716,6 +819,8 @@ export class Component {
|
|
|
716
819
|
}
|
|
717
820
|
}
|
|
718
821
|
|
|
822
|
+
result = new Function("useRef", `return \`${result}\``)(useRef)
|
|
823
|
+
|
|
719
824
|
if (!result.trim().startsWith("<body>")) {
|
|
720
825
|
console.warn(
|
|
721
826
|
"You should wrap your html in a body tag, vader may not grab all html!"
|
|
@@ -785,6 +890,9 @@ export class Component {
|
|
|
785
890
|
// @ts-ignore
|
|
786
891
|
dom[element.getAttribute("ref")] = element;
|
|
787
892
|
}
|
|
893
|
+
if(element.nodeName === "MARKDOWN"){
|
|
894
|
+
element.innerHTML = markdown(element.innerHTML.trim())
|
|
895
|
+
}
|
|
788
896
|
|
|
789
897
|
if (element.hasAttribute("class")) {
|
|
790
898
|
const allowClassComments =
|
|
@@ -883,7 +991,7 @@ export class Component {
|
|
|
883
991
|
* </div>
|
|
884
992
|
* `);
|
|
885
993
|
*/
|
|
886
|
-
async render() {}
|
|
994
|
+
async render(props) {}
|
|
887
995
|
}
|
|
888
996
|
|
|
889
997
|
/**
|
|
@@ -951,8 +1059,9 @@ let cache = {};
|
|
|
951
1059
|
* @returns {Promise} - modified string with html content
|
|
952
1060
|
* @param {string} path
|
|
953
1061
|
*/
|
|
954
|
-
|
|
1062
|
+
|
|
955
1063
|
export const include = async (path) => {
|
|
1064
|
+
|
|
956
1065
|
if (
|
|
957
1066
|
path.startsWith("/") &&
|
|
958
1067
|
!path.includes("/src/") &&
|
|
@@ -975,10 +1084,11 @@ export const include = async (path) => {
|
|
|
975
1084
|
})
|
|
976
1085
|
.then(async (data) => {
|
|
977
1086
|
// Handle includes
|
|
978
|
-
let includes = data.match(/<include src="(.*)"\/>/
|
|
1087
|
+
let includes = data.match(/<include src="(.*)"\/>/g);
|
|
979
1088
|
if (includes) {
|
|
980
|
-
|
|
1089
|
+
|
|
981
1090
|
const includePromises = includes.map((e) => {
|
|
1091
|
+
|
|
982
1092
|
// @ts-ignore
|
|
983
1093
|
let includePath = e.match(/<include src="(.*)"\/>/)[1];
|
|
984
1094
|
|
|
@@ -991,19 +1101,20 @@ export const include = async (path) => {
|
|
|
991
1101
|
includePath = "/src" + includePath;
|
|
992
1102
|
}
|
|
993
1103
|
return include(includePath).then((includeData) => {
|
|
994
|
-
// Replace the include tag with the fetched data
|
|
995
1104
|
data = data.replace(e, includeData);
|
|
1105
|
+
console.log("included", includePath);
|
|
996
1106
|
});
|
|
997
1107
|
});
|
|
998
1108
|
|
|
999
1109
|
// Wait for all includes to be fetched and replaced
|
|
1000
1110
|
return Promise.all(includePromises).then(() => {
|
|
1001
1111
|
cache[path] = data;
|
|
1002
|
-
|
|
1112
|
+
|
|
1113
|
+
return data
|
|
1003
1114
|
});
|
|
1004
1115
|
} else {
|
|
1005
1116
|
cache[path] = data;
|
|
1006
|
-
return
|
|
1117
|
+
return data;
|
|
1007
1118
|
}
|
|
1008
1119
|
});
|
|
1009
1120
|
};
|