@ryupold/vode 0.12.2 → 0.13.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.
- package/.github/workflows/npm-publish.yml +2 -1
- package/package.json +1 -1
- package/src/vode.ts +33 -39
- package/vode.mjs +23 -35
package/package.json
CHANGED
package/src/vode.ts
CHANGED
|
@@ -62,8 +62,10 @@ export type EventsMap =
|
|
|
62
62
|
& { [K in keyof SVGElementEventMap as `on${K}`]: SVGElementEventMap[K] }
|
|
63
63
|
& { onsearch: Event };
|
|
64
64
|
|
|
65
|
-
export type PropertyValue<S> =
|
|
66
|
-
|
|
65
|
+
export type PropertyValue<S> =
|
|
66
|
+
| string | boolean | null | undefined | void
|
|
67
|
+
| StyleProp | ClassProp
|
|
68
|
+
| Patch<S>;
|
|
67
69
|
|
|
68
70
|
export type Dispatch<S> = (action: Patch<S>) => void;
|
|
69
71
|
export type PatchableState<S> = S & { patch: Dispatch<Patch<S>> };
|
|
@@ -104,7 +106,7 @@ export function createPatch<S extends object | unknown>(p: DeepPartial<S> | Effe
|
|
|
104
106
|
* - identity: `vode(["div", ["span", "bar"]])` => `["div", ["span", "bar"]]` --*rendered*-> `<div><span>bar</span></div>`
|
|
105
107
|
*/
|
|
106
108
|
export function vode<S extends object | unknown>(tag: Tag | Vode<S>, props?: Props<S> | ChildVode<S>, ...children: ChildVode<S>[]): Vode<S> {
|
|
107
|
-
if(!tag) throw new Error("tag must be a string or vode");
|
|
109
|
+
if (!tag) throw new Error("tag must be a string or vode");
|
|
108
110
|
if (Array.isArray(tag)) return tag;
|
|
109
111
|
else if (props) return [tag, props as Props<S>, ...children];
|
|
110
112
|
else return [tag, ...children];
|
|
@@ -319,16 +321,6 @@ export function childrenStart<S>(vode: ChildVode<S> | AttachedVode<S>): number {
|
|
|
319
321
|
return props(vode) ? 2 : 1;
|
|
320
322
|
}
|
|
321
323
|
|
|
322
|
-
/** @returns multiple merged objects as one, applying from left to right ({}, first, ...p) */
|
|
323
|
-
export function merge(first?: object | unknown, ...p: (object | unknown)[]): object {
|
|
324
|
-
first = mergeState({}, first);
|
|
325
|
-
for (const pp of p) {
|
|
326
|
-
if (!pp) continue;
|
|
327
|
-
first = mergeState(first, pp);
|
|
328
|
-
}
|
|
329
|
-
return first!;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
324
|
function mergeState(target: any, source: any) {
|
|
333
325
|
if (!source) return target;
|
|
334
326
|
|
|
@@ -564,31 +556,34 @@ function unwrap<S>(c: Component<S> | ChildVode<S>, s: S): ChildVode<S> {
|
|
|
564
556
|
|
|
565
557
|
function patchProperties<S>(patch: Dispatch<S>, node: ChildNode, oldProps?: Props<S>, newProps?: Props<S>, isSvg?: boolean) {
|
|
566
558
|
if (!newProps && !oldProps) return;
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
}
|
|
572
|
-
} else if (newProps) { // clear old props and set new in one loop
|
|
573
|
-
const combinedKeys = new Set([...Object.keys(oldProps), ...Object.keys(newProps)]);
|
|
574
|
-
for (const key of combinedKeys) {
|
|
559
|
+
|
|
560
|
+
// match existing properties
|
|
561
|
+
if (oldProps) {
|
|
562
|
+
for (const key in oldProps) {
|
|
575
563
|
const oldValue = oldProps[key as keyof Props<S>] as PropertyValue<S>;
|
|
576
|
-
const newValue = newProps[key as keyof Props<S>] as PropertyValue<S>;
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
if (
|
|
580
|
-
|
|
581
|
-
}
|
|
582
|
-
(<any>node)["__" + key] = newValue;
|
|
564
|
+
const newValue = newProps?.[key as keyof Props<S>] as PropertyValue<S>;
|
|
565
|
+
|
|
566
|
+
if (oldValue !== newValue) {
|
|
567
|
+
if (newProps) newProps[key as keyof Props<S>] = patchProperty(patch, node, key, oldValue, newValue, isSvg);
|
|
568
|
+
else patchProperty(patch, node, key, oldValue, undefined, isSvg);
|
|
583
569
|
}
|
|
584
|
-
|
|
585
|
-
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
//new properties that weren't in oldProps
|
|
574
|
+
if (newProps && oldProps) {
|
|
575
|
+
for (const key in newProps) {
|
|
576
|
+
if (!(key in oldProps)) {
|
|
577
|
+
const newValue = newProps[key as keyof Props<S>] as PropertyValue<S>;
|
|
578
|
+
newProps[key as keyof Props<S>] = patchProperty(patch, <Element>node, key, undefined, newValue, isSvg);
|
|
586
579
|
}
|
|
587
580
|
}
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
581
|
+
}
|
|
582
|
+
// only new props
|
|
583
|
+
else if (newProps) {
|
|
584
|
+
for (const key in newProps) {
|
|
585
|
+
const newValue = newProps[key as keyof Props<S>] as PropertyValue<S>;
|
|
586
|
+
newProps[key as keyof Props<S>] = patchProperty(patch, <Element>node, key, undefined, newValue, isSvg);
|
|
592
587
|
}
|
|
593
588
|
}
|
|
594
589
|
}
|
|
@@ -660,13 +655,12 @@ function patchProperty<S>(patch: Dispatch<S>, node: ChildNode, key: string | key
|
|
|
660
655
|
}
|
|
661
656
|
|
|
662
657
|
function classString(classProp: ClassProp): string {
|
|
663
|
-
if (typeof classProp === "string")
|
|
658
|
+
if (typeof classProp === "string")
|
|
664
659
|
return classProp;
|
|
665
|
-
|
|
660
|
+
else if (Array.isArray(classProp))
|
|
666
661
|
return classProp.map(classString).join(" ");
|
|
667
|
-
|
|
662
|
+
else if (typeof classProp === "object")
|
|
668
663
|
return Object.keys(classProp!).filter(k => classProp![k]).join(" ");
|
|
669
|
-
|
|
664
|
+
else
|
|
670
665
|
return "";
|
|
671
|
-
}
|
|
672
666
|
}
|
package/vode.mjs
CHANGED
|
@@ -187,15 +187,6 @@ function child(vode2, index) {
|
|
|
187
187
|
function childrenStart(vode2) {
|
|
188
188
|
return props(vode2) ? 2 : 1;
|
|
189
189
|
}
|
|
190
|
-
function merge(first, ...p) {
|
|
191
|
-
first = mergeState({}, first);
|
|
192
|
-
for (const pp of p) {
|
|
193
|
-
if (!pp)
|
|
194
|
-
continue;
|
|
195
|
-
first = mergeState(first, pp);
|
|
196
|
-
}
|
|
197
|
-
return first;
|
|
198
|
-
}
|
|
199
190
|
function mergeState(target, source) {
|
|
200
191
|
if (!source)
|
|
201
192
|
return target;
|
|
@@ -390,30 +381,29 @@ function unwrap(c, s) {
|
|
|
390
381
|
function patchProperties(patch, node, oldProps, newProps, isSvg) {
|
|
391
382
|
if (!newProps && !oldProps)
|
|
392
383
|
return;
|
|
393
|
-
if (
|
|
394
|
-
for (const key in
|
|
395
|
-
const newValue = newProps[key];
|
|
396
|
-
newProps[key] = patchProperty(patch, node, key, undefined, newValue, isSvg);
|
|
397
|
-
}
|
|
398
|
-
} else if (newProps) {
|
|
399
|
-
const combinedKeys = new Set([...Object.keys(oldProps), ...Object.keys(newProps)]);
|
|
400
|
-
for (const key of combinedKeys) {
|
|
384
|
+
if (oldProps) {
|
|
385
|
+
for (const key in oldProps) {
|
|
401
386
|
const oldValue = oldProps[key];
|
|
402
|
-
const newValue = newProps[key];
|
|
403
|
-
if (
|
|
404
|
-
|
|
405
|
-
if (oldEvent && oldEvent !== newValue || !oldEvent && oldValue !== newValue) {
|
|
387
|
+
const newValue = newProps?.[key];
|
|
388
|
+
if (oldValue !== newValue) {
|
|
389
|
+
if (newProps)
|
|
406
390
|
newProps[key] = patchProperty(patch, node, key, oldValue, newValue, isSvg);
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
} else if (oldValue !== newValue) {
|
|
410
|
-
newProps[key] = patchProperty(patch, node, key, oldValue, newValue, isSvg);
|
|
391
|
+
else
|
|
392
|
+
patchProperty(patch, node, key, oldValue, undefined, isSvg);
|
|
411
393
|
}
|
|
412
394
|
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
395
|
+
}
|
|
396
|
+
if (newProps && oldProps) {
|
|
397
|
+
for (const key in newProps) {
|
|
398
|
+
if (!(key in oldProps)) {
|
|
399
|
+
const newValue = newProps[key];
|
|
400
|
+
newProps[key] = patchProperty(patch, node, key, undefined, newValue, isSvg);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
} else if (newProps) {
|
|
404
|
+
for (const key in newProps) {
|
|
405
|
+
const newValue = newProps[key];
|
|
406
|
+
newProps[key] = patchProperty(patch, node, key, undefined, newValue, isSvg);
|
|
417
407
|
}
|
|
418
408
|
}
|
|
419
409
|
}
|
|
@@ -479,15 +469,14 @@ function patchProperty(patch, node, key, oldValue, newValue, isSvg) {
|
|
|
479
469
|
return newValue;
|
|
480
470
|
}
|
|
481
471
|
function classString(classProp) {
|
|
482
|
-
if (typeof classProp === "string")
|
|
472
|
+
if (typeof classProp === "string")
|
|
483
473
|
return classProp;
|
|
484
|
-
|
|
474
|
+
else if (Array.isArray(classProp))
|
|
485
475
|
return classProp.map(classString).join(" ");
|
|
486
|
-
|
|
476
|
+
else if (typeof classProp === "object")
|
|
487
477
|
return Object.keys(classProp).filter((k) => classProp[k]).join(" ");
|
|
488
|
-
|
|
478
|
+
else
|
|
489
479
|
return "";
|
|
490
|
-
}
|
|
491
480
|
}
|
|
492
481
|
// src/vode-tags.ts
|
|
493
482
|
var A = "a";
|
|
@@ -728,7 +717,6 @@ export {
|
|
|
728
717
|
tag,
|
|
729
718
|
props,
|
|
730
719
|
mergeClass,
|
|
731
|
-
merge,
|
|
732
720
|
memo,
|
|
733
721
|
htmlToVode,
|
|
734
722
|
createState,
|