veles 0.0.5 → 0.0.7
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/dist/{chunk-V3EV7UG6.js → chunk-ILNLS6QO.js} +14 -11
- package/dist/chunk-MH6DPZ3V.js +490 -0
- package/dist/chunk-X6QYYW56.js +15 -0
- package/dist/create-state-Bo6TT4qP.d.ts +33 -0
- package/dist/create-state-D1JASFVs.d.cts +33 -0
- package/dist/fragment-CU26z590.d.cts +9 -0
- package/dist/fragment-IVSEC7-Q.d.ts +9 -0
- package/dist/index.cjs +318 -235
- package/dist/index.d.cts +4 -45
- package/dist/index.d.ts +4 -45
- package/dist/index.js +7 -407
- package/dist/jsx-runtime.cjs +1 -1
- package/dist/jsx-runtime.d.cts +2 -2
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/jsx-runtime.js +4 -2
- package/dist/{fragment-CHmQ0MhU.d.cts → types.d-DgVBp6oa.d.cts} +1 -7
- package/dist/{fragment-CHmQ0MhU.d.ts → types.d-DgVBp6oa.d.ts} +1 -7
- package/dist/utils/index.cjs +814 -0
- package/dist/utils/index.d.cts +18 -0
- package/dist/utils/index.d.ts +18 -0
- package/dist/utils/index.js +37 -0
- package/package.json +6 -1
|
@@ -0,0 +1,814 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/utils/index.ts
|
|
20
|
+
var utils_exports = {};
|
|
21
|
+
__export(utils_exports, {
|
|
22
|
+
combine: () => combineState,
|
|
23
|
+
combineState: () => combineState,
|
|
24
|
+
select: () => selectState,
|
|
25
|
+
selectState: () => selectState
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(utils_exports);
|
|
28
|
+
|
|
29
|
+
// src/_utils.ts
|
|
30
|
+
function getComponentVelesNode(component) {
|
|
31
|
+
const componentsTree = [];
|
|
32
|
+
if ("velesStringElement" in component) {
|
|
33
|
+
return {
|
|
34
|
+
velesElementNode: component,
|
|
35
|
+
componentsTree: []
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
let childNode = component;
|
|
39
|
+
while ("velesComponent" in childNode) {
|
|
40
|
+
componentsTree.push(childNode);
|
|
41
|
+
if ("velesStringElement" in childNode.tree) {
|
|
42
|
+
return {
|
|
43
|
+
velesElementNode: childNode.tree,
|
|
44
|
+
componentsTree
|
|
45
|
+
};
|
|
46
|
+
} else {
|
|
47
|
+
childNode = childNode.tree;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { velesElementNode: childNode, componentsTree };
|
|
51
|
+
}
|
|
52
|
+
function callMountHandlers(component) {
|
|
53
|
+
if ("velesStringElement" in component) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if ("velesComponent" in component) {
|
|
57
|
+
component._privateMethods._callMountHandlers();
|
|
58
|
+
callMountHandlers(component.tree);
|
|
59
|
+
}
|
|
60
|
+
if ("velesNode" in component) {
|
|
61
|
+
component.childComponents.forEach(
|
|
62
|
+
(childComponent) => callMountHandlers(childComponent)
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function identity(value1, value2) {
|
|
67
|
+
return value1 === value2;
|
|
68
|
+
}
|
|
69
|
+
function unique(arr) {
|
|
70
|
+
const map = /* @__PURE__ */ new Map();
|
|
71
|
+
const resultArr = [];
|
|
72
|
+
arr.forEach((element) => {
|
|
73
|
+
if (map.has(element))
|
|
74
|
+
return;
|
|
75
|
+
map.set(element, true);
|
|
76
|
+
resultArr.push(element);
|
|
77
|
+
});
|
|
78
|
+
return resultArr;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/hooks/lifecycle.ts
|
|
82
|
+
var contextStack = [];
|
|
83
|
+
var currentContext = null;
|
|
84
|
+
function addContext(newContext) {
|
|
85
|
+
contextStack.push(newContext);
|
|
86
|
+
currentContext = newContext;
|
|
87
|
+
}
|
|
88
|
+
function popContext() {
|
|
89
|
+
contextStack.pop();
|
|
90
|
+
currentContext = contextStack[contextStack.length - 1];
|
|
91
|
+
}
|
|
92
|
+
function onMount(cb) {
|
|
93
|
+
if (currentContext) {
|
|
94
|
+
currentContext.onMount(cb);
|
|
95
|
+
} else {
|
|
96
|
+
console.error("missing current context");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function onUnmount(cb) {
|
|
100
|
+
if (currentContext) {
|
|
101
|
+
currentContext.onUnmount(cb);
|
|
102
|
+
} else {
|
|
103
|
+
console.error("missing current context");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/create-element/parse-children.ts
|
|
108
|
+
function parseChildren({
|
|
109
|
+
children,
|
|
110
|
+
htmlElement,
|
|
111
|
+
velesNode
|
|
112
|
+
}) {
|
|
113
|
+
const childComponents = [];
|
|
114
|
+
if (children === void 0 || children === null) {
|
|
115
|
+
return childComponents;
|
|
116
|
+
}
|
|
117
|
+
(Array.isArray(children) ? children : [children]).forEach(
|
|
118
|
+
(childComponent) => {
|
|
119
|
+
if (typeof childComponent === "string") {
|
|
120
|
+
const text = document.createTextNode(childComponent);
|
|
121
|
+
htmlElement.append(text);
|
|
122
|
+
} else if (typeof childComponent === "number") {
|
|
123
|
+
const text = document.createTextNode(String(childComponent));
|
|
124
|
+
htmlElement.append(text);
|
|
125
|
+
} else if (typeof childComponent === "object" && childComponent && "velesNode" in childComponent && (childComponent == null ? void 0 : childComponent.velesNode)) {
|
|
126
|
+
if (childComponent.phantom) {
|
|
127
|
+
childComponent.childComponents.forEach((childComponentofPhantom) => {
|
|
128
|
+
if ("velesNode" in childComponentofPhantom) {
|
|
129
|
+
htmlElement.append(childComponentofPhantom.html);
|
|
130
|
+
childComponentofPhantom.parentVelesElement = velesNode;
|
|
131
|
+
} else {
|
|
132
|
+
const { velesElementNode } = getComponentVelesNode(
|
|
133
|
+
childComponentofPhantom
|
|
134
|
+
);
|
|
135
|
+
if (!velesElementNode) {
|
|
136
|
+
console.error("can't find HTML tree in a component chain");
|
|
137
|
+
} else {
|
|
138
|
+
htmlElement.append(velesElementNode.html);
|
|
139
|
+
velesElementNode.parentVelesElement = velesNode;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
childComponent.parentVelesElement = velesNode;
|
|
144
|
+
childComponents.push(childComponent);
|
|
145
|
+
} else {
|
|
146
|
+
htmlElement.append(childComponent.html);
|
|
147
|
+
childComponent.parentVelesElement = velesNode;
|
|
148
|
+
childComponents.push(childComponent);
|
|
149
|
+
}
|
|
150
|
+
} else if (typeof childComponent === "object" && childComponent && "velesComponent" in childComponent && (childComponent == null ? void 0 : childComponent.velesComponent)) {
|
|
151
|
+
const { componentsTree, velesElementNode } = getComponentVelesNode(childComponent);
|
|
152
|
+
if (!velesElementNode) {
|
|
153
|
+
console.error("can't find HTML tree in a component chain");
|
|
154
|
+
} else {
|
|
155
|
+
if ("velesNode" in velesElementNode && velesElementNode.phantom) {
|
|
156
|
+
velesElementNode.childComponents.forEach(
|
|
157
|
+
(childComponentofPhantom) => {
|
|
158
|
+
if ("velesNode" in childComponentofPhantom) {
|
|
159
|
+
htmlElement.append(childComponentofPhantom.html);
|
|
160
|
+
childComponentofPhantom.parentVelesElement = velesNode;
|
|
161
|
+
} else {
|
|
162
|
+
const { componentsTree: componentsTree2, velesElementNode: velesElementNode2 } = getComponentVelesNode(childComponentofPhantom);
|
|
163
|
+
if (!velesElementNode2) {
|
|
164
|
+
console.error("can't find HTML tree in a component chain");
|
|
165
|
+
} else {
|
|
166
|
+
htmlElement.append(velesElementNode2.html);
|
|
167
|
+
velesElementNode2.parentVelesElement = velesNode;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
);
|
|
172
|
+
} else {
|
|
173
|
+
htmlElement.append(velesElementNode.html);
|
|
174
|
+
}
|
|
175
|
+
velesElementNode.parentVelesElement = velesNode;
|
|
176
|
+
childComponents.push(childComponent);
|
|
177
|
+
}
|
|
178
|
+
} else if (typeof childComponent === "object" && childComponent && "velesStringElement" in childComponent && (childComponent == null ? void 0 : childComponent.velesStringElement)) {
|
|
179
|
+
htmlElement.append(childComponent.html);
|
|
180
|
+
childComponent.parentVelesElement = velesNode;
|
|
181
|
+
childComponents.push(childComponent);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
return childComponents;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// src/create-element/assign-attributes.ts
|
|
189
|
+
function assignAttributes({
|
|
190
|
+
props,
|
|
191
|
+
htmlElement,
|
|
192
|
+
velesNode
|
|
193
|
+
}) {
|
|
194
|
+
Object.entries(props).forEach(([key, value]) => {
|
|
195
|
+
const isFunction = typeof value === "function";
|
|
196
|
+
if (isFunction && value.velesAttribute === true) {
|
|
197
|
+
const attributeValue = value(htmlElement, key, velesNode);
|
|
198
|
+
htmlElement.setAttribute(key, attributeValue);
|
|
199
|
+
} else if (
|
|
200
|
+
// basically, any form of `on` handlers, like `onClick`, `onCopy`, etc
|
|
201
|
+
isFunction && key.length > 2 && key.startsWith("on")
|
|
202
|
+
) {
|
|
203
|
+
htmlElement.addEventListener(
|
|
204
|
+
key[2].toLocaleLowerCase() + key.slice(3),
|
|
205
|
+
value
|
|
206
|
+
);
|
|
207
|
+
} else {
|
|
208
|
+
htmlElement.setAttribute(key, value);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/create-element/parse-component.ts
|
|
214
|
+
function parseComponent({
|
|
215
|
+
element,
|
|
216
|
+
props
|
|
217
|
+
}) {
|
|
218
|
+
const componentUnmountCbs = [];
|
|
219
|
+
const componentMountCbs = [];
|
|
220
|
+
const componentAPI = {
|
|
221
|
+
onMount: (cb) => {
|
|
222
|
+
componentMountCbs.push(cb);
|
|
223
|
+
},
|
|
224
|
+
onUnmount: (cb) => {
|
|
225
|
+
componentUnmountCbs.push(cb);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
addContext(componentAPI);
|
|
229
|
+
const _componentTree = element(props, componentAPI);
|
|
230
|
+
const componentTree = typeof _componentTree === "string" || !_componentTree ? {
|
|
231
|
+
velesStringElement: true,
|
|
232
|
+
html: document.createTextNode(
|
|
233
|
+
typeof _componentTree === "string" ? _componentTree : ""
|
|
234
|
+
)
|
|
235
|
+
} : _componentTree;
|
|
236
|
+
popContext();
|
|
237
|
+
const velesComponent = {
|
|
238
|
+
velesComponent: true,
|
|
239
|
+
tree: componentTree,
|
|
240
|
+
_privateMethods: {
|
|
241
|
+
_addUnmountHandler: (cb) => {
|
|
242
|
+
componentAPI.onUnmount(cb);
|
|
243
|
+
},
|
|
244
|
+
_callMountHandlers: () => {
|
|
245
|
+
componentMountCbs.forEach((cb) => {
|
|
246
|
+
const mountCbResult = cb();
|
|
247
|
+
if (typeof mountCbResult === "function") {
|
|
248
|
+
componentAPI.onUnmount(mountCbResult);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
},
|
|
252
|
+
_callUnmountHandlers: () => {
|
|
253
|
+
componentUnmountCbs.forEach((cb) => cb());
|
|
254
|
+
if ("_privateMethods" in velesComponent.tree) {
|
|
255
|
+
velesComponent.tree._privateMethods._callUnmountHandlers();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
return velesComponent;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// src/create-element/create-element.ts
|
|
264
|
+
function createElement(element, props = {}) {
|
|
265
|
+
if (typeof element === "string") {
|
|
266
|
+
const { children, ref, phantom = false, ...otherProps } = props;
|
|
267
|
+
const newElement = document.createElement(element);
|
|
268
|
+
const velesNode = {};
|
|
269
|
+
if (ref == null ? void 0 : ref.velesRef) {
|
|
270
|
+
ref.current = newElement;
|
|
271
|
+
}
|
|
272
|
+
const childComponents = parseChildren({
|
|
273
|
+
children,
|
|
274
|
+
htmlElement: newElement,
|
|
275
|
+
velesNode
|
|
276
|
+
});
|
|
277
|
+
let unmountHandlers = [];
|
|
278
|
+
const callUnmountHandlers = () => {
|
|
279
|
+
unmountHandlers.forEach((cb) => cb());
|
|
280
|
+
unmountHandlers = [];
|
|
281
|
+
childComponents.forEach((childComponent) => {
|
|
282
|
+
childComponent._privateMethods._callUnmountHandlers();
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
velesNode.html = newElement;
|
|
286
|
+
velesNode.velesNode = true;
|
|
287
|
+
velesNode.childComponents = childComponents;
|
|
288
|
+
velesNode.phantom = phantom;
|
|
289
|
+
velesNode._privateMethods = {
|
|
290
|
+
_addUnmountHandler: (cb) => {
|
|
291
|
+
unmountHandlers.push(cb);
|
|
292
|
+
},
|
|
293
|
+
_callUnmountHandlers: callUnmountHandlers
|
|
294
|
+
};
|
|
295
|
+
assignAttributes({ props: otherProps, htmlElement: newElement, velesNode });
|
|
296
|
+
return velesNode;
|
|
297
|
+
} else if (typeof element === "function") {
|
|
298
|
+
return parseComponent({ element, props });
|
|
299
|
+
}
|
|
300
|
+
throw new Error(
|
|
301
|
+
"Veles createElement expects a valid DOM string or another component"
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// src/create-element/create-text-element.ts
|
|
306
|
+
function createTextElement(text) {
|
|
307
|
+
const unmountHandlers = [];
|
|
308
|
+
return {
|
|
309
|
+
velesStringElement: true,
|
|
310
|
+
// in case there is no text, we create an empty Text node, so we still can
|
|
311
|
+
// have a reference to it, replace it, call lifecycle methods, etc
|
|
312
|
+
html: document.createTextNode(text || ""),
|
|
313
|
+
_privateMethods: {
|
|
314
|
+
_addUnmountHandler: (cb) => {
|
|
315
|
+
unmountHandlers.push(cb);
|
|
316
|
+
},
|
|
317
|
+
_callUnmountHandlers: () => {
|
|
318
|
+
unmountHandlers.forEach((cb) => cb());
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// src/hooks/create-state.ts
|
|
325
|
+
function createState(initialValue, subscribeCallback) {
|
|
326
|
+
let value = initialValue;
|
|
327
|
+
let previousValue = void 0;
|
|
328
|
+
let trackingEffects = [];
|
|
329
|
+
let trackingSelectorElements = [];
|
|
330
|
+
let trackingAttributes = [];
|
|
331
|
+
let trackingIterators = [];
|
|
332
|
+
const _triggerUpdates = () => {
|
|
333
|
+
const newTrackingSelectorElements = [];
|
|
334
|
+
trackingSelectorElements.forEach((selectorTrackingElement) => {
|
|
335
|
+
const { selectedValue, selector, cb, node, comparator } = selectorTrackingElement;
|
|
336
|
+
const newSelectedValue = selector ? selector(value) : value;
|
|
337
|
+
if (comparator(selectedValue, newSelectedValue)) {
|
|
338
|
+
newTrackingSelectorElements.push(selectorTrackingElement);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const returnednewNode = cb ? cb(newSelectedValue) : String(newSelectedValue);
|
|
342
|
+
const newNode = !returnednewNode || typeof returnednewNode === "string" ? createTextElement(returnednewNode) : returnednewNode;
|
|
343
|
+
const { velesElementNode: oldVelesElementNode } = getComponentVelesNode(node);
|
|
344
|
+
const { velesElementNode: newVelesElementNode } = getComponentVelesNode(newNode);
|
|
345
|
+
const parentVelesElement = oldVelesElementNode.parentVelesElement;
|
|
346
|
+
const newTrackingSelectorElement = {
|
|
347
|
+
selector,
|
|
348
|
+
selectedValue: newSelectedValue,
|
|
349
|
+
cb,
|
|
350
|
+
node: newNode,
|
|
351
|
+
comparator
|
|
352
|
+
};
|
|
353
|
+
if (parentVelesElement) {
|
|
354
|
+
newVelesElementNode.parentVelesElement = parentVelesElement;
|
|
355
|
+
if ("velesNode" in newVelesElementNode && newVelesElementNode.phantom) {
|
|
356
|
+
const insertAllPhantomChildren = (adjacentNode) => {
|
|
357
|
+
newVelesElementNode.childComponents.forEach(
|
|
358
|
+
(childComponentofPhantom) => {
|
|
359
|
+
if ("velesNode" in childComponentofPhantom) {
|
|
360
|
+
adjacentNode.html.before(childComponentofPhantom.html);
|
|
361
|
+
childComponentofPhantom.parentVelesElement = adjacentNode.parentVelesElement;
|
|
362
|
+
} else {
|
|
363
|
+
const { velesElementNode } = getComponentVelesNode(
|
|
364
|
+
childComponentofPhantom
|
|
365
|
+
);
|
|
366
|
+
if (!velesElementNode) {
|
|
367
|
+
console.error("can't find HTML tree in a component chain");
|
|
368
|
+
} else {
|
|
369
|
+
adjacentNode.html.before(velesElementNode.html);
|
|
370
|
+
velesElementNode.parentVelesElement = adjacentNode.parentVelesElement;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
);
|
|
375
|
+
};
|
|
376
|
+
if ("velesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
|
|
377
|
+
let isInserted = false;
|
|
378
|
+
oldVelesElementNode.childComponents.forEach(
|
|
379
|
+
(childComponentofPhantom) => {
|
|
380
|
+
if ("velesNode" in childComponentofPhantom) {
|
|
381
|
+
if (!isInserted) {
|
|
382
|
+
insertAllPhantomChildren(childComponentofPhantom);
|
|
383
|
+
isInserted = true;
|
|
384
|
+
}
|
|
385
|
+
childComponentofPhantom.html.remove();
|
|
386
|
+
} else {
|
|
387
|
+
const { velesElementNode } = getComponentVelesNode(
|
|
388
|
+
childComponentofPhantom
|
|
389
|
+
);
|
|
390
|
+
if (!velesElementNode) {
|
|
391
|
+
console.error("can't find HTML tree in a component chain");
|
|
392
|
+
} else {
|
|
393
|
+
if (!isInserted) {
|
|
394
|
+
insertAllPhantomChildren(velesElementNode);
|
|
395
|
+
isInserted = true;
|
|
396
|
+
}
|
|
397
|
+
velesElementNode.html.remove();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
);
|
|
402
|
+
} else {
|
|
403
|
+
insertAllPhantomChildren(oldVelesElementNode);
|
|
404
|
+
oldVelesElementNode.html.remove();
|
|
405
|
+
}
|
|
406
|
+
} else {
|
|
407
|
+
if ("velesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
|
|
408
|
+
let isInserted = false;
|
|
409
|
+
oldVelesElementNode.childComponents.forEach(
|
|
410
|
+
(childComponentofPhantom) => {
|
|
411
|
+
if ("velesNode" in childComponentofPhantom) {
|
|
412
|
+
if (!isInserted) {
|
|
413
|
+
childComponentofPhantom.html.before(
|
|
414
|
+
newVelesElementNode.html
|
|
415
|
+
);
|
|
416
|
+
isInserted = true;
|
|
417
|
+
}
|
|
418
|
+
childComponentofPhantom.html.remove();
|
|
419
|
+
} else {
|
|
420
|
+
const { velesElementNode } = getComponentVelesNode(
|
|
421
|
+
childComponentofPhantom
|
|
422
|
+
);
|
|
423
|
+
if (!velesElementNode) {
|
|
424
|
+
console.error("can't find HTML tree in a component chain");
|
|
425
|
+
} else {
|
|
426
|
+
if (!isInserted) {
|
|
427
|
+
velesElementNode.html.before(newVelesElementNode.html);
|
|
428
|
+
isInserted = true;
|
|
429
|
+
}
|
|
430
|
+
velesElementNode.html.remove();
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
);
|
|
435
|
+
} else {
|
|
436
|
+
parentVelesElement.html.replaceChild(
|
|
437
|
+
newVelesElementNode.html,
|
|
438
|
+
oldVelesElementNode.html
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
parentVelesElement.childComponents = parentVelesElement.childComponents.map(
|
|
443
|
+
(childComponent) => childComponent === node ? newNode : node
|
|
444
|
+
);
|
|
445
|
+
node._privateMethods._callUnmountHandlers();
|
|
446
|
+
callMountHandlers(newNode);
|
|
447
|
+
newNode._privateMethods._addUnmountHandler(() => {
|
|
448
|
+
trackingSelectorElements = trackingSelectorElements.filter(
|
|
449
|
+
(el) => el !== newTrackingSelectorElement
|
|
450
|
+
);
|
|
451
|
+
});
|
|
452
|
+
} else {
|
|
453
|
+
console.log("parent node was not found");
|
|
454
|
+
}
|
|
455
|
+
newTrackingSelectorElements.push(newTrackingSelectorElement);
|
|
456
|
+
});
|
|
457
|
+
trackingSelectorElements = unique(
|
|
458
|
+
trackingSelectorElements.concat(newTrackingSelectorElements)
|
|
459
|
+
);
|
|
460
|
+
trackingAttributes.forEach(({ cb, htmlElement, attributeName }) => {
|
|
461
|
+
const newAttributeValue = cb ? cb(value) : value;
|
|
462
|
+
htmlElement.setAttribute(attributeName, newAttributeValue);
|
|
463
|
+
});
|
|
464
|
+
trackingEffects.forEach((trackingEffect) => {
|
|
465
|
+
const { cb, selectedValue, selector, comparator } = trackingEffect;
|
|
466
|
+
const newSelectedValue = selector ? selector(value) : value;
|
|
467
|
+
if (comparator ? comparator(selectedValue, newSelectedValue) : selectedValue === newSelectedValue) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
cb(newSelectedValue);
|
|
471
|
+
trackingEffect.selectedValue = newSelectedValue;
|
|
472
|
+
});
|
|
473
|
+
trackingIterators.forEach((trackingIterator) => {
|
|
474
|
+
const {
|
|
475
|
+
cb,
|
|
476
|
+
key,
|
|
477
|
+
renderedElements,
|
|
478
|
+
elementsByKey,
|
|
479
|
+
wrapperComponent,
|
|
480
|
+
selector
|
|
481
|
+
} = trackingIterator;
|
|
482
|
+
if (!wrapperComponent) {
|
|
483
|
+
console.error("there is no wrapper component for the iterator");
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
const { velesElementNode: wrapperVelesElementNode } = getComponentVelesNode(wrapperComponent);
|
|
487
|
+
const parentVelesElement = wrapperVelesElementNode.parentVelesElement;
|
|
488
|
+
if (!parentVelesElement) {
|
|
489
|
+
console.error("there is no parent Veles node for the iterator wrapper");
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
const elements = selector ? selector(value) : value;
|
|
493
|
+
if (Array.isArray(elements)) {
|
|
494
|
+
const newRenderedElements = [];
|
|
495
|
+
const newElementsByKey = {};
|
|
496
|
+
const renderedExistingElements = {};
|
|
497
|
+
elements.forEach((element, index) => {
|
|
498
|
+
let calculatedKey = "";
|
|
499
|
+
if (typeof key === "string" && typeof element === "object" && element !== null && key in element) {
|
|
500
|
+
calculatedKey = element[key];
|
|
501
|
+
} else if (typeof key === "function") {
|
|
502
|
+
calculatedKey = key({ element, index });
|
|
503
|
+
} else {
|
|
504
|
+
}
|
|
505
|
+
if (!calculatedKey) {
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const existingElement = elementsByKey[calculatedKey];
|
|
509
|
+
if (existingElement) {
|
|
510
|
+
renderedExistingElements[calculatedKey] = true;
|
|
511
|
+
const currentValue = existingElement.elementState.getValue();
|
|
512
|
+
if (currentValue !== element) {
|
|
513
|
+
existingElement.elementState.setValue(element);
|
|
514
|
+
}
|
|
515
|
+
const currentIndex = existingElement.indexState.getValue();
|
|
516
|
+
if (currentIndex !== index) {
|
|
517
|
+
existingElement.indexState.setValue(index);
|
|
518
|
+
}
|
|
519
|
+
newRenderedElements.push([
|
|
520
|
+
existingElement.node,
|
|
521
|
+
calculatedKey,
|
|
522
|
+
existingElement.elementState
|
|
523
|
+
]);
|
|
524
|
+
newElementsByKey[calculatedKey] = {
|
|
525
|
+
elementState: existingElement.elementState,
|
|
526
|
+
indexState: existingElement.indexState,
|
|
527
|
+
indexValue: index,
|
|
528
|
+
node: existingElement.node
|
|
529
|
+
};
|
|
530
|
+
} else {
|
|
531
|
+
const elementState = createState(element);
|
|
532
|
+
const indexState = createState(index);
|
|
533
|
+
const node = cb({ elementState, indexState });
|
|
534
|
+
newRenderedElements.push([node, calculatedKey, elementState]);
|
|
535
|
+
newElementsByKey[calculatedKey] = {
|
|
536
|
+
elementState,
|
|
537
|
+
indexState,
|
|
538
|
+
indexValue: index,
|
|
539
|
+
node
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
const positioningOffset = {};
|
|
544
|
+
let newElementsCount = 0;
|
|
545
|
+
let offset = 0;
|
|
546
|
+
let currentElement = null;
|
|
547
|
+
newRenderedElements.forEach((newRenderedElement, index) => {
|
|
548
|
+
var _a, _b, _c;
|
|
549
|
+
if (positioningOffset[index]) {
|
|
550
|
+
offset = offset + positioningOffset[index];
|
|
551
|
+
}
|
|
552
|
+
const [newNode, calculatedKey, newState] = newRenderedElement;
|
|
553
|
+
const existingElement = elementsByKey[calculatedKey];
|
|
554
|
+
if (existingElement) {
|
|
555
|
+
const { velesElementNode: existingElementNode } = getComponentVelesNode(existingElement.node);
|
|
556
|
+
if (existingElement.indexValue + offset === index) {
|
|
557
|
+
currentElement = existingElementNode.html;
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
if (existingElement.indexValue + offset > index) {
|
|
561
|
+
if (currentElement) {
|
|
562
|
+
currentElement.after(existingElementNode.html);
|
|
563
|
+
positioningOffset[existingElement.indexValue + 1] = -1;
|
|
564
|
+
} else {
|
|
565
|
+
const firstRenderedElement = (_a = renderedElements[0]) == null ? void 0 : _a[0];
|
|
566
|
+
if (firstRenderedElement) {
|
|
567
|
+
const { velesElementNode: firstRenderedVelesNode } = getComponentVelesNode(firstRenderedElement);
|
|
568
|
+
firstRenderedVelesNode.html.before(existingElementNode.html);
|
|
569
|
+
} else {
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
currentElement = existingElementNode.html;
|
|
573
|
+
offset = offset + 1;
|
|
574
|
+
} else {
|
|
575
|
+
if (currentElement) {
|
|
576
|
+
currentElement.after(existingElementNode.html);
|
|
577
|
+
positioningOffset[existingElement.indexValue + 1] = 1;
|
|
578
|
+
} else {
|
|
579
|
+
const firstRenderedElement = (_b = renderedElements[0]) == null ? void 0 : _b[0];
|
|
580
|
+
if (firstRenderedElement) {
|
|
581
|
+
const { velesElementNode: firstRenderedVelesNode } = getComponentVelesNode(firstRenderedElement);
|
|
582
|
+
firstRenderedVelesNode.html.before(existingElementNode.html);
|
|
583
|
+
} else {
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
currentElement = existingElementNode.html;
|
|
587
|
+
offset = offset - 1;
|
|
588
|
+
}
|
|
589
|
+
} else {
|
|
590
|
+
const { velesElementNode: newNodeVelesElement } = getComponentVelesNode(newNode);
|
|
591
|
+
newNodeVelesElement.parentVelesElement = parentVelesElement;
|
|
592
|
+
if (currentElement) {
|
|
593
|
+
currentElement.after(newNodeVelesElement.html);
|
|
594
|
+
} else {
|
|
595
|
+
const firstRenderedElement = (_c = renderedElements[0]) == null ? void 0 : _c[0];
|
|
596
|
+
if (firstRenderedElement) {
|
|
597
|
+
const { velesElementNode: firstRenderedVelesNode } = getComponentVelesNode(firstRenderedElement);
|
|
598
|
+
firstRenderedVelesNode.html.before(newNodeVelesElement.html);
|
|
599
|
+
} else {
|
|
600
|
+
parentVelesElement.html.prepend(newNodeVelesElement.html);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
offset = offset + 1;
|
|
604
|
+
currentElement = newNodeVelesElement.html;
|
|
605
|
+
newElementsCount = newElementsCount + 1;
|
|
606
|
+
callMountHandlers(newNode);
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
if (renderedElements.length === newRenderedElements.length + newElementsCount) {
|
|
610
|
+
} else {
|
|
611
|
+
renderedElements.forEach(([oldNode, calculatedKey]) => {
|
|
612
|
+
if (renderedExistingElements[calculatedKey] === true) {
|
|
613
|
+
return;
|
|
614
|
+
} else {
|
|
615
|
+
const { velesElementNode: oldRenderedVelesNode } = getComponentVelesNode(oldNode);
|
|
616
|
+
oldRenderedVelesNode.html.remove();
|
|
617
|
+
oldNode._privateMethods._callUnmountHandlers();
|
|
618
|
+
if ("velesNode" in wrapperVelesElementNode) {
|
|
619
|
+
wrapperVelesElementNode.childComponents = wrapperVelesElementNode.childComponents.filter(
|
|
620
|
+
(childComponent) => childComponent !== oldNode
|
|
621
|
+
);
|
|
622
|
+
} else {
|
|
623
|
+
throw new Error("Wrapper iterator element is a string");
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
trackingIterator.renderedElements = newRenderedElements;
|
|
629
|
+
trackingIterator.elementsByKey = newElementsByKey;
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
};
|
|
633
|
+
const result = {
|
|
634
|
+
// supposed to be used at the component level
|
|
635
|
+
trackValue: (cb, options = {}) => {
|
|
636
|
+
result.trackValueSelector(void 0, cb, options);
|
|
637
|
+
},
|
|
638
|
+
trackValueSelector(selector, cb, options = {}) {
|
|
639
|
+
const trackedValue = selector ? selector(value) : value;
|
|
640
|
+
trackingEffects.push({
|
|
641
|
+
cb,
|
|
642
|
+
selector,
|
|
643
|
+
comparator: options.comparator,
|
|
644
|
+
selectedValue: trackedValue
|
|
645
|
+
});
|
|
646
|
+
if (!options.skipFirstCall) {
|
|
647
|
+
if (options.callOnMount) {
|
|
648
|
+
onMount(() => {
|
|
649
|
+
cb(trackedValue);
|
|
650
|
+
});
|
|
651
|
+
} else {
|
|
652
|
+
cb(trackedValue);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
onUnmount(() => {
|
|
656
|
+
trackingEffects = trackingEffects.filter(
|
|
657
|
+
(trackingCallback) => trackingCallback.cb !== cb
|
|
658
|
+
);
|
|
659
|
+
});
|
|
660
|
+
},
|
|
661
|
+
useValue: (cb, comparator) => {
|
|
662
|
+
return result.useValueSelector(void 0, cb, comparator);
|
|
663
|
+
},
|
|
664
|
+
useValueSelector(selector, cb, comparator = identity) {
|
|
665
|
+
const selectedValue = selector ? selector(value) : value;
|
|
666
|
+
const returnedNode = cb ? cb(selectedValue) : String(selectedValue);
|
|
667
|
+
const node = !returnedNode || typeof returnedNode === "string" ? createTextElement(returnedNode) : returnedNode;
|
|
668
|
+
const trackingSelectorElement = {
|
|
669
|
+
selector,
|
|
670
|
+
selectedValue,
|
|
671
|
+
cb,
|
|
672
|
+
node,
|
|
673
|
+
comparator
|
|
674
|
+
};
|
|
675
|
+
trackingSelectorElements.push(trackingSelectorElement);
|
|
676
|
+
node._privateMethods._addUnmountHandler(() => {
|
|
677
|
+
trackingSelectorElements = trackingSelectorElements.filter(
|
|
678
|
+
(el) => trackingSelectorElement !== el
|
|
679
|
+
);
|
|
680
|
+
});
|
|
681
|
+
return node;
|
|
682
|
+
},
|
|
683
|
+
useValueIterator(options, cb) {
|
|
684
|
+
const children = [];
|
|
685
|
+
const elementsByKey = {};
|
|
686
|
+
const elements = options.selector ? options.selector(value) : value;
|
|
687
|
+
if (!Array.isArray(elements)) {
|
|
688
|
+
console.error("useValueIterator received non-array value");
|
|
689
|
+
return null;
|
|
690
|
+
}
|
|
691
|
+
elements.forEach((element, index) => {
|
|
692
|
+
let calculatedKey = "";
|
|
693
|
+
if (typeof options.key === "string" && typeof element === "object" && element !== null && options.key in element) {
|
|
694
|
+
calculatedKey = element[options.key];
|
|
695
|
+
} else if (typeof options.key === "function") {
|
|
696
|
+
calculatedKey = options.key({ element, index });
|
|
697
|
+
} else {
|
|
698
|
+
}
|
|
699
|
+
const elementState = createState(element);
|
|
700
|
+
const indexState = createState(index);
|
|
701
|
+
if (!calculatedKey) {
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
let node = cb({ elementState, indexState });
|
|
705
|
+
elementsByKey[calculatedKey] = {
|
|
706
|
+
node,
|
|
707
|
+
indexState,
|
|
708
|
+
indexValue: index,
|
|
709
|
+
elementState
|
|
710
|
+
};
|
|
711
|
+
children.push([node, calculatedKey, elementState]);
|
|
712
|
+
});
|
|
713
|
+
const trackingParams = {};
|
|
714
|
+
trackingIterators.push(trackingParams);
|
|
715
|
+
const wrapperComponent = createElement(() => {
|
|
716
|
+
onUnmount(() => {
|
|
717
|
+
trackingIterators = trackingIterators.filter(
|
|
718
|
+
(currentTrackingParams) => currentTrackingParams !== trackingParams
|
|
719
|
+
);
|
|
720
|
+
});
|
|
721
|
+
return createElement("div", {
|
|
722
|
+
phantom: true,
|
|
723
|
+
children: children.map((child) => child[0])
|
|
724
|
+
});
|
|
725
|
+
});
|
|
726
|
+
trackingParams.cb = cb;
|
|
727
|
+
trackingParams.key = options.key;
|
|
728
|
+
trackingParams.elementsByKey = elementsByKey;
|
|
729
|
+
trackingParams.renderedElements = children;
|
|
730
|
+
trackingParams.wrapperComponent = wrapperComponent;
|
|
731
|
+
if (options.selector) {
|
|
732
|
+
trackingParams.selector = options.selector;
|
|
733
|
+
}
|
|
734
|
+
return wrapperComponent;
|
|
735
|
+
},
|
|
736
|
+
useAttribute: (cb) => {
|
|
737
|
+
const attributeValue = cb ? cb(value) : value;
|
|
738
|
+
const attributeHelper = (htmlElement, attributeName, node) => {
|
|
739
|
+
const trackingElement = { cb, htmlElement, attributeName };
|
|
740
|
+
trackingAttributes.push(trackingElement);
|
|
741
|
+
node._privateMethods._addUnmountHandler(() => {
|
|
742
|
+
trackingAttributes = trackingAttributes.filter(
|
|
743
|
+
(trackingAttribute) => trackingAttribute !== trackingElement
|
|
744
|
+
);
|
|
745
|
+
});
|
|
746
|
+
return attributeValue;
|
|
747
|
+
};
|
|
748
|
+
attributeHelper.velesAttribute = true;
|
|
749
|
+
return attributeHelper;
|
|
750
|
+
},
|
|
751
|
+
// useful for stuff like callbacks
|
|
752
|
+
getValue: () => {
|
|
753
|
+
return value;
|
|
754
|
+
},
|
|
755
|
+
getPreviousValue: () => {
|
|
756
|
+
return previousValue;
|
|
757
|
+
},
|
|
758
|
+
// set up new value only through the callback which
|
|
759
|
+
// gives the latest value to ensure no outdated data
|
|
760
|
+
// can be used for the state
|
|
761
|
+
setValue: (newValueCB) => {
|
|
762
|
+
const newValue = (
|
|
763
|
+
// @ts-expect-error
|
|
764
|
+
typeof newValueCB === "function" ? newValueCB(value) : newValueCB
|
|
765
|
+
);
|
|
766
|
+
if (newValue !== value) {
|
|
767
|
+
previousValue = value;
|
|
768
|
+
value = newValue;
|
|
769
|
+
_triggerUpdates();
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
if (subscribeCallback) {
|
|
774
|
+
const unsubscribe = subscribeCallback(result.setValue);
|
|
775
|
+
if (unsubscribe) {
|
|
776
|
+
onUnmount(unsubscribe);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
return result;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// src/utils/combine-state.ts
|
|
783
|
+
function combineState(...states) {
|
|
784
|
+
const initialValue = states.map((state) => state.getValue());
|
|
785
|
+
const combinedState = createState(initialValue);
|
|
786
|
+
states.forEach((state) => {
|
|
787
|
+
state.trackValue(() => {
|
|
788
|
+
const updatedValue = states.map((state2) => state2.getValue());
|
|
789
|
+
combinedState.setValue(updatedValue);
|
|
790
|
+
});
|
|
791
|
+
});
|
|
792
|
+
return combinedState;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
// src/utils/select-state.ts
|
|
796
|
+
function selectState(state, selector) {
|
|
797
|
+
const initialValue = selector(state.getValue());
|
|
798
|
+
const newState = createState(initialValue);
|
|
799
|
+
state.trackValueSelector(
|
|
800
|
+
selector,
|
|
801
|
+
(selectedState) => {
|
|
802
|
+
newState.setValue(selectedState);
|
|
803
|
+
},
|
|
804
|
+
{ skipFirstCall: true }
|
|
805
|
+
);
|
|
806
|
+
return newState;
|
|
807
|
+
}
|
|
808
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
809
|
+
0 && (module.exports = {
|
|
810
|
+
combine,
|
|
811
|
+
combineState,
|
|
812
|
+
select,
|
|
813
|
+
selectState
|
|
814
|
+
});
|