elegance-js 1.10.2 → 1.11.0

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.
@@ -1,20 +1,31 @@
1
- // src/internal/deprecate.ts
2
- var ShowDeprecationWarning = (msg) => {
3
- console.warn("\x1B[31m", msg, "\x1B[0m");
4
- console.trace("Stack Trace:");
1
+ // src/server/loadHook.ts
2
+ var loadHook = (deps, fn, bind) => {
3
+ const stringFn = fn.toString();
4
+ const depsArray = (deps || []).map((dep) => ({
5
+ id: dep.id,
6
+ bind: dep.bind
7
+ }));
8
+ let dependencyString = "[";
9
+ for (const dep of depsArray) {
10
+ dependencyString += `{id:${dep.id}`;
11
+ if (dep.bind) dependencyString += `,bind:${dep.bind}`;
12
+ dependencyString += `},`;
13
+ }
14
+ dependencyString += "]";
15
+ const isAsync = fn.constructor.name === "AsyncFunction";
16
+ const wrapperFn = isAsync ? `async (state) => await (${stringFn})(state, ...state.getAll(${dependencyString}))` : `(state) => (${stringFn})(state, ...state.getAll(${dependencyString}))`;
17
+ globalThis.__SERVER_CURRENT_LOADHOOKS__.push({
18
+ fn: wrapperFn,
19
+ bind: bind || ""
20
+ });
5
21
  };
6
22
 
7
- // src/server/createState.ts
23
+ // src/server/state.ts
8
24
  if (!globalThis.__SERVER_CURRENT_STATE_ID__) {
9
25
  globalThis.__SERVER_CURRENT_STATE_ID__ = 0;
10
26
  }
11
27
  var currentId = globalThis.__SERVER_CURRENT_STATE_ID__;
12
- var createEventListener = ({
13
- eventListener,
14
- dependencies = [],
15
- params
16
- }) => {
17
- ShowDeprecationWarning("WARNING: The createEventListener() and function is deprecated. Please use eventListener() instead, from elegance-js/state.");
28
+ var eventListener = (dependencies, eventListener2) => {
18
29
  const deps = dependencies.map((dep) => ({ id: dep.id, bind: dep.bind }));
19
30
  let dependencyString = "[";
20
31
  for (const dep of deps) {
@@ -29,39 +40,17 @@ var createEventListener = ({
29
40
  value: new Function(
30
41
  "state",
31
42
  "event",
32
- `(${eventListener.toString()})({ event, ...${JSON.stringify(params || {})} }, ...state.getAll(${dependencyString}))`
43
+ `(${eventListener2.toString()})(event, ...state.getAll(${dependencyString}))`
33
44
  )
34
45
  };
35
46
  globalThis.__SERVER_CURRENT_STATE__.push(value);
36
47
  return value;
37
48
  };
38
49
 
39
- // src/server/loadHook.ts
40
- var createLoadHook = (options) => {
41
- ShowDeprecationWarning("WARNING: createLoadHook() is a deprecated function. Use loadHook() from elegance-js/loadHook instead.");
42
- const stringFn = options.fn.toString();
43
- const deps = (options.deps || []).map((dep) => ({
44
- id: dep.id,
45
- bind: dep.bind
46
- }));
47
- let dependencyString = "[";
48
- for (const dep of deps) {
49
- dependencyString += `{id:${dep.id}`;
50
- if (dep.bind) dependencyString += `,bind:${dep.bind}`;
51
- dependencyString += `},`;
52
- }
53
- dependencyString += "]";
54
- const isAsync = options.fn.constructor.name === "AsyncFunction";
55
- const wrapperFn = isAsync ? `async (state) => await (${stringFn})(state, ...state.getAll(${dependencyString}))` : `(state) => (${stringFn})(state, ...state.getAll(${dependencyString}))`;
56
- globalThis.__SERVER_CURRENT_LOADHOOKS__.push({
57
- fn: wrapperFn,
58
- bind: options.bind || ""
59
- });
60
- };
61
-
62
50
  // src/components/Link.ts
63
- createLoadHook({
64
- fn: () => {
51
+ loadHook(
52
+ [],
53
+ () => {
65
54
  const anchors = Array.from(document.querySelectorAll("a[prefetch]"));
66
55
  const elsToClear = [];
67
56
  for (const anchor of anchors) {
@@ -89,21 +78,22 @@ createLoadHook({
89
78
  }
90
79
  };
91
80
  }
92
- });
93
- var navigate = createEventListener({
94
- eventListener: (params) => {
95
- const target = new URL(params.event.currentTarget.href);
81
+ );
82
+ var navigate = eventListener(
83
+ [],
84
+ (event) => {
85
+ const target = new URL(event.currentTarget.href);
96
86
  const client2 = globalThis.client;
97
87
  const sanitizedTarget = client2.sanitizePathname(target.pathname);
98
88
  const sanitizedCurrent = client2.sanitizePathname(window.location.pathname);
99
89
  if (sanitizedTarget === sanitizedCurrent) {
100
- if (target.hash === window.location.hash) return params.event.preventDefault();
90
+ if (target.hash === window.location.hash) return event.preventDefault();
101
91
  return;
102
92
  }
103
- params.event.preventDefault();
93
+ event.preventDefault();
104
94
  client2.navigateLocally(target.href);
105
95
  }
106
- });
96
+ );
107
97
  var Link = (options, ...children) => {
108
98
  if (!options.href) {
109
99
  throw `Link elements must have a HREF attribute set.`;
@@ -20,32 +20,6 @@ var createState = (value, options) => {
20
20
  globalThis.__SERVER_CURRENT_STATE__.push(serverStateEntry);
21
21
  return serverStateEntry;
22
22
  };
23
- var createEventListener = ({
24
- eventListener,
25
- dependencies = [],
26
- params
27
- }) => {
28
- ShowDeprecationWarning("WARNING: The createEventListener() and function is deprecated. Please use eventListener() instead, from elegance-js/state.");
29
- const deps = dependencies.map((dep) => ({ id: dep.id, bind: dep.bind }));
30
- let dependencyString = "[";
31
- for (const dep of deps) {
32
- dependencyString += `{id:${dep.id}`;
33
- if (dep.bind) dependencyString += `,bind:${dep.bind}`;
34
- dependencyString += `},`;
35
- }
36
- dependencyString += "]";
37
- const value = {
38
- id: currentId += 1,
39
- type: 1 /* STATE */,
40
- value: new Function(
41
- "state",
42
- "event",
43
- `(${eventListener.toString()})({ event, ...${JSON.stringify(params || {})} }, ...state.getAll(${dependencyString}))`
44
- )
45
- };
46
- globalThis.__SERVER_CURRENT_STATE__.push(value);
47
- return value;
48
- };
49
23
 
50
24
  // src/server/observe.ts
51
25
  var observe = (refs, update) => {
@@ -62,6 +36,26 @@ var observe = (refs, update) => {
62
36
  };
63
37
 
64
38
  // src/server/loadHook.ts
39
+ var loadHook = (deps, fn, bind) => {
40
+ const stringFn = fn.toString();
41
+ const depsArray = (deps || []).map((dep) => ({
42
+ id: dep.id,
43
+ bind: dep.bind
44
+ }));
45
+ let dependencyString = "[";
46
+ for (const dep of depsArray) {
47
+ dependencyString += `{id:${dep.id}`;
48
+ if (dep.bind) dependencyString += `,bind:${dep.bind}`;
49
+ dependencyString += `},`;
50
+ }
51
+ dependencyString += "]";
52
+ const isAsync = fn.constructor.name === "AsyncFunction";
53
+ const wrapperFn = isAsync ? `async (state) => await (${stringFn})(state, ...state.getAll(${dependencyString}))` : `(state) => (${stringFn})(state, ...state.getAll(${dependencyString}))`;
54
+ globalThis.__SERVER_CURRENT_LOADHOOKS__.push({
55
+ fn: wrapperFn,
56
+ bind: bind || ""
57
+ });
58
+ };
65
59
  var createLoadHook = (options) => {
66
60
  ShowDeprecationWarning("WARNING: createLoadHook() is a deprecated function. Use loadHook() from elegance-js/loadHook instead.");
67
61
  const stringFn = options.fn.toString();
@@ -84,9 +78,37 @@ var createLoadHook = (options) => {
84
78
  });
85
79
  };
86
80
 
81
+ // src/server/state.ts
82
+ if (!globalThis.__SERVER_CURRENT_STATE_ID__) {
83
+ globalThis.__SERVER_CURRENT_STATE_ID__ = 0;
84
+ }
85
+ var currentId2 = globalThis.__SERVER_CURRENT_STATE_ID__;
86
+ var eventListener = (dependencies, eventListener2) => {
87
+ const deps = dependencies.map((dep) => ({ id: dep.id, bind: dep.bind }));
88
+ let dependencyString = "[";
89
+ for (const dep of deps) {
90
+ dependencyString += `{id:${dep.id}`;
91
+ if (dep.bind) dependencyString += `,bind:${dep.bind}`;
92
+ dependencyString += `},`;
93
+ }
94
+ dependencyString += "]";
95
+ const value = {
96
+ id: currentId2 += 1,
97
+ type: 1 /* STATE */,
98
+ value: new Function(
99
+ "state",
100
+ "event",
101
+ `(${eventListener2.toString()})(event, ...state.getAll(${dependencyString}))`
102
+ )
103
+ };
104
+ globalThis.__SERVER_CURRENT_STATE__.push(value);
105
+ return value;
106
+ };
107
+
87
108
  // src/components/Link.ts
88
- createLoadHook({
89
- fn: () => {
109
+ loadHook(
110
+ [],
111
+ () => {
90
112
  const anchors = Array.from(document.querySelectorAll("a[prefetch]"));
91
113
  const elsToClear = [];
92
114
  for (const anchor of anchors) {
@@ -114,21 +136,22 @@ createLoadHook({
114
136
  }
115
137
  };
116
138
  }
117
- });
118
- var navigate = createEventListener({
119
- eventListener: (params) => {
120
- const target = new URL(params.event.currentTarget.href);
139
+ );
140
+ var navigate = eventListener(
141
+ [],
142
+ (event) => {
143
+ const target = new URL(event.currentTarget.href);
121
144
  const client2 = globalThis.client;
122
145
  const sanitizedTarget = client2.sanitizePathname(target.pathname);
123
146
  const sanitizedCurrent = client2.sanitizePathname(window.location.pathname);
124
147
  if (sanitizedTarget === sanitizedCurrent) {
125
- if (target.hash === window.location.hash) return params.event.preventDefault();
148
+ if (target.hash === window.location.hash) return event.preventDefault();
126
149
  return;
127
150
  }
128
- params.event.preventDefault();
151
+ event.preventDefault();
129
152
  client2.navigateLocally(target.href);
130
153
  }
131
- });
154
+ );
132
155
  var Link = (options, ...children) => {
133
156
  if (!options.href) {
134
157
  throw `Link elements must have a HREF attribute set.`;
@@ -149,7 +172,7 @@ var Link = (options, ...children) => {
149
172
  var hasUserScrolled = createState(false);
150
173
  createLoadHook({
151
174
  deps: [hasUserScrolled],
152
- fn: (state, hasUserScrolled2) => {
175
+ fn: (state2, hasUserScrolled2) => {
153
176
  const handleScroll = () => {
154
177
  const pos = {
155
178
  x: window.scrollX,
@@ -33,50 +33,27 @@ var ShowDeprecationWarning = (msg) => {
33
33
  console.trace("Stack Trace:");
34
34
  };
35
35
 
36
- // src/server/createState.ts
37
- if (!globalThis.__SERVER_CURRENT_STATE_ID__) {
38
- globalThis.__SERVER_CURRENT_STATE_ID__ = 0;
39
- }
40
- var currentId = globalThis.__SERVER_CURRENT_STATE_ID__;
41
- var createState = (value, options) => {
42
- ShowDeprecationWarning("WARNING: The createState() and function is deprecated. Please use state() instead, from elegance-js/state.");
43
- const serverStateEntry = {
44
- id: currentId += 1,
45
- value,
46
- type: 1 /* STATE */,
47
- bind: options?.bind
48
- };
49
- globalThis.__SERVER_CURRENT_STATE__.push(serverStateEntry);
50
- return serverStateEntry;
51
- };
52
- var createEventListener = ({
53
- eventListener,
54
- dependencies = [],
55
- params
56
- }) => {
57
- ShowDeprecationWarning("WARNING: The createEventListener() and function is deprecated. Please use eventListener() instead, from elegance-js/state.");
58
- const deps = dependencies.map((dep) => ({ id: dep.id, bind: dep.bind }));
36
+ // src/server/loadHook.ts
37
+ var loadHook = (deps, fn, bind) => {
38
+ const stringFn = fn.toString();
39
+ const depsArray = (deps || []).map((dep) => ({
40
+ id: dep.id,
41
+ bind: dep.bind
42
+ }));
59
43
  let dependencyString = "[";
60
- for (const dep of deps) {
44
+ for (const dep of depsArray) {
61
45
  dependencyString += `{id:${dep.id}`;
62
46
  if (dep.bind) dependencyString += `,bind:${dep.bind}`;
63
47
  dependencyString += `},`;
64
48
  }
65
49
  dependencyString += "]";
66
- const value = {
67
- id: currentId += 1,
68
- type: 1 /* STATE */,
69
- value: new Function(
70
- "state",
71
- "event",
72
- `(${eventListener.toString()})({ event, ...${JSON.stringify(params || {})} }, ...state.getAll(${dependencyString}))`
73
- )
74
- };
75
- globalThis.__SERVER_CURRENT_STATE__.push(value);
76
- return value;
50
+ const isAsync = fn.constructor.name === "AsyncFunction";
51
+ const wrapperFn = isAsync ? `async (state) => await (${stringFn})(state, ...state.getAll(${dependencyString}))` : `(state) => (${stringFn})(state, ...state.getAll(${dependencyString}))`;
52
+ globalThis.__SERVER_CURRENT_LOADHOOKS__.push({
53
+ fn: wrapperFn,
54
+ bind: bind || ""
55
+ });
77
56
  };
78
-
79
- // src/server/loadHook.ts
80
57
  var createLoadHook = (options) => {
81
58
  ShowDeprecationWarning("WARNING: createLoadHook() is a deprecated function. Use loadHook() from elegance-js/loadHook instead.");
82
59
  const stringFn = options.fn.toString();
@@ -99,9 +76,51 @@ var createLoadHook = (options) => {
99
76
  });
100
77
  };
101
78
 
79
+ // src/server/state.ts
80
+ if (!globalThis.__SERVER_CURRENT_STATE_ID__) {
81
+ globalThis.__SERVER_CURRENT_STATE_ID__ = 0;
82
+ }
83
+ var currentId = globalThis.__SERVER_CURRENT_STATE_ID__;
84
+ var eventListener = (dependencies, eventListener2) => {
85
+ const deps = dependencies.map((dep) => ({ id: dep.id, bind: dep.bind }));
86
+ let dependencyString = "[";
87
+ for (const dep of deps) {
88
+ dependencyString += `{id:${dep.id}`;
89
+ if (dep.bind) dependencyString += `,bind:${dep.bind}`;
90
+ dependencyString += `},`;
91
+ }
92
+ dependencyString += "]";
93
+ const value = {
94
+ id: currentId += 1,
95
+ type: 1 /* STATE */,
96
+ value: new Function(
97
+ "state",
98
+ "event",
99
+ `(${eventListener2.toString()})(event, ...state.getAll(${dependencyString}))`
100
+ )
101
+ };
102
+ globalThis.__SERVER_CURRENT_STATE__.push(value);
103
+ return value;
104
+ };
105
+
106
+ // src/server/observe.ts
107
+ var observe = (refs, update) => {
108
+ const returnValue = {
109
+ type: 2 /* OBSERVER */,
110
+ initialValues: refs.map((ref) => ref.value),
111
+ update,
112
+ refs: refs.map((ref) => ({
113
+ id: ref.id,
114
+ bind: ref.bind
115
+ }))
116
+ };
117
+ return returnValue;
118
+ };
119
+
102
120
  // src/components/Link.ts
103
- createLoadHook({
104
- fn: () => {
121
+ loadHook(
122
+ [],
123
+ () => {
105
124
  const anchors = Array.from(document.querySelectorAll("a[prefetch]"));
106
125
  const elsToClear = [];
107
126
  for (const anchor of anchors) {
@@ -129,21 +148,22 @@ createLoadHook({
129
148
  }
130
149
  };
131
150
  }
132
- });
133
- var navigate = createEventListener({
134
- eventListener: (params) => {
135
- const target = new URL(params.event.currentTarget.href);
151
+ );
152
+ var navigate = eventListener(
153
+ [],
154
+ (event) => {
155
+ const target = new URL(event.currentTarget.href);
136
156
  const client2 = globalThis.client;
137
157
  const sanitizedTarget = client2.sanitizePathname(target.pathname);
138
158
  const sanitizedCurrent = client2.sanitizePathname(window.location.pathname);
139
159
  if (sanitizedTarget === sanitizedCurrent) {
140
- if (target.hash === window.location.hash) return params.event.preventDefault();
160
+ if (target.hash === window.location.hash) return event.preventDefault();
141
161
  return;
142
162
  }
143
- params.event.preventDefault();
163
+ event.preventDefault();
144
164
  client2.navigateLocally(target.href);
145
165
  }
146
- });
166
+ );
147
167
  var Link = (options, ...children) => {
148
168
  if (!options.href) {
149
169
  throw `Link elements must have a HREF attribute set.`;
@@ -160,6 +180,49 @@ var Link = (options, ...children) => {
160
180
  );
161
181
  };
162
182
 
183
+ // src/server/createState.ts
184
+ if (!globalThis.__SERVER_CURRENT_STATE_ID__) {
185
+ globalThis.__SERVER_CURRENT_STATE_ID__ = 0;
186
+ }
187
+ var currentId2 = globalThis.__SERVER_CURRENT_STATE_ID__;
188
+ var createState = (value, options) => {
189
+ ShowDeprecationWarning("WARNING: The createState() and function is deprecated. Please use state() instead, from elegance-js/state.");
190
+ const serverStateEntry = {
191
+ id: currentId2 += 1,
192
+ value,
193
+ type: 1 /* STATE */,
194
+ bind: options?.bind
195
+ };
196
+ globalThis.__SERVER_CURRENT_STATE__.push(serverStateEntry);
197
+ return serverStateEntry;
198
+ };
199
+ var createEventListener = ({
200
+ eventListener: eventListener2,
201
+ dependencies = [],
202
+ params
203
+ }) => {
204
+ ShowDeprecationWarning("WARNING: The createEventListener() and function is deprecated. Please use eventListener() instead, from elegance-js/state.");
205
+ const deps = dependencies.map((dep) => ({ id: dep.id, bind: dep.bind }));
206
+ let dependencyString = "[";
207
+ for (const dep of deps) {
208
+ dependencyString += `{id:${dep.id}`;
209
+ if (dep.bind) dependencyString += `,bind:${dep.bind}`;
210
+ dependencyString += `},`;
211
+ }
212
+ dependencyString += "]";
213
+ const value = {
214
+ id: currentId2 += 1,
215
+ type: 1 /* STATE */,
216
+ value: new Function(
217
+ "state",
218
+ "event",
219
+ `(${eventListener2.toString()})({ event, ...${JSON.stringify(params || {})} }, ...state.getAll(${dependencyString}))`
220
+ )
221
+ };
222
+ globalThis.__SERVER_CURRENT_STATE__.push(value);
223
+ return value;
224
+ };
225
+
163
226
  // src/docs/docs/components/Header.ts
164
227
  var Header = () => header(
165
228
  {
@@ -207,20 +270,6 @@ var Header = () => header(
207
270
  )
208
271
  );
209
272
 
210
- // src/server/observe.ts
211
- var observe = (refs, update) => {
212
- const returnValue = {
213
- type: 2 /* OBSERVER */,
214
- initialValues: refs.map((ref) => ref.value),
215
- update,
216
- refs: refs.map((ref) => ({
217
- id: ref.id,
218
- bind: ref.bind
219
- }))
220
- };
221
- return returnValue;
222
- };
223
-
224
273
  // src/docs/utils/MEGALEXER.ts
225
274
  var tokenize = (input) => {
226
275
  const tokens = [];
@@ -420,7 +469,7 @@ var Toast = (bind) => {
420
469
  toastTimeoutId,
421
470
  isToastShowing
422
471
  ],
423
- fn: (state, toastTimeoutId2, isToastShowing2) => {
472
+ fn: (state2, toastTimeoutId2, isToastShowing2) => {
424
473
  return () => {
425
474
  clearTimeout(toastTimeoutId2.value);
426
475
  isToastShowing2.value = false;
@@ -476,7 +525,7 @@ var secondsSpentOnPage = createState(0, {
476
525
  createLoadHook({
477
526
  deps: [secondsSpentOnPage],
478
527
  bind: docsLayoutId,
479
- fn: (state, time) => {
528
+ fn: (state2, time) => {
480
529
  const storedTime = localStorage.getItem("time-on-page");
481
530
  if (storedTime) {
482
531
  time.value = parseInt(storedTime);