@lvce-editor/virtual-dom 5.2.0 → 6.1.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/dist/index.d.ts +2 -3
- package/dist/index.js +68 -6
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -37,8 +37,7 @@ export interface RemovePatch {
|
|
|
37
37
|
readonly type: 5;
|
|
38
38
|
}
|
|
39
39
|
export interface ReplacePatch {
|
|
40
|
-
readonly
|
|
41
|
-
readonly node: VirtualDomNode;
|
|
40
|
+
readonly nodes: readonly VirtualDomNode[];
|
|
42
41
|
readonly type: 2;
|
|
43
42
|
}
|
|
44
43
|
export interface TextPatch {
|
|
@@ -46,7 +45,7 @@ export interface TextPatch {
|
|
|
46
45
|
readonly value: string;
|
|
47
46
|
}
|
|
48
47
|
export type Patch = TextPatch | AttributePatch | ReplacePatch | RemoveAttributePatch | RemovePatch | AddPatch | NavigateChildPatch | NavigateParentPatch | RemoveChildPatch | NavigateSiblingPatch;
|
|
49
|
-
export declare const applyPatch: ($Element: Node, patches: readonly Patch[]) => void;
|
|
48
|
+
export declare const applyPatch: ($Element: Node, patches: readonly Patch[], eventMap?: Record<string, any>) => void;
|
|
50
49
|
export declare const setComponentUid: ($Element: any, uid: any) => void;
|
|
51
50
|
export declare const getComponentUid: ($Element: any) => number;
|
|
52
51
|
export declare const getComponentUidFromEvent: (event: any) => number;
|
package/dist/index.js
CHANGED
|
@@ -682,11 +682,60 @@ const renderInternal = ($Parent, elements, eventMap, newEventMap) => {
|
|
|
682
682
|
$Parent.append(...stack);
|
|
683
683
|
};
|
|
684
684
|
|
|
685
|
+
// Map of property names to attribute names for cases where they differ
|
|
686
|
+
const propertyToAttribute = {
|
|
687
|
+
className: 'class',
|
|
688
|
+
htmlFor: 'for',
|
|
689
|
+
ariaActivedescendant: 'aria-activedescendant',
|
|
690
|
+
ariaControls: 'aria-controls',
|
|
691
|
+
ariaLabelledBy: 'aria-labelledby',
|
|
692
|
+
ariaOwns: 'aria-owns',
|
|
693
|
+
inputType: 'type'
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
// Style properties that need to be set on element.style
|
|
697
|
+
const styleProperties = new Set(['width', 'height', 'top', 'left', 'marginTop', 'paddingLeft', 'paddingRight']);
|
|
698
|
+
|
|
699
|
+
// TODO merge this with the setProp function
|
|
700
|
+
|
|
685
701
|
const setAttribute = ($Element, key, value) => {
|
|
686
|
-
|
|
702
|
+
// Handle width/height for images (set as attributes, not style)
|
|
703
|
+
if ((key === 'width' || key === 'height') && $Element instanceof HTMLImageElement) {
|
|
704
|
+
// @ts-ignore - dynamic property access
|
|
705
|
+
$Element[key] = value;
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
// Handle style properties
|
|
710
|
+
if (styleProperties.has(key)) {
|
|
711
|
+
// @ts-ignore - dynamic style property access
|
|
712
|
+
$Element.style[key] = typeof value === 'number' ? `${value}px` : value;
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// Handle aria attributes - map camelCase to hyphenated form
|
|
717
|
+
if (key in propertyToAttribute) {
|
|
718
|
+
$Element.setAttribute(propertyToAttribute[key], value);
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// Use property assignment for known DOM properties, attribute for others
|
|
723
|
+
if (key in $Element) {
|
|
724
|
+
// @ts-ignore - dynamic property access
|
|
725
|
+
$Element[key] = value;
|
|
726
|
+
} else {
|
|
727
|
+
$Element.setAttribute(key, value);
|
|
728
|
+
}
|
|
687
729
|
};
|
|
688
730
|
const removeAttribute = ($Element, key) => {
|
|
689
|
-
|
|
731
|
+
// Handle style properties
|
|
732
|
+
if (styleProperties.has(key)) {
|
|
733
|
+
// @ts-ignore - dynamic style property access
|
|
734
|
+
$Element.style[key] = '';
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
const attributeName = propertyToAttribute[key] || key;
|
|
738
|
+
$Element.removeAttribute(attributeName);
|
|
690
739
|
};
|
|
691
740
|
const setText = ($Element, value) => {
|
|
692
741
|
$Element.nodeValue = value;
|
|
@@ -695,11 +744,21 @@ const removeChild = ($Element, index) => {
|
|
|
695
744
|
const $Child = $Element.children[index];
|
|
696
745
|
$Child.remove();
|
|
697
746
|
};
|
|
698
|
-
const add$1 = ($Element, nodes) => {
|
|
699
|
-
renderInternal($Element, nodes,
|
|
747
|
+
const add$1 = ($Element, nodes, eventMap = {}) => {
|
|
748
|
+
renderInternal($Element, nodes, eventMap, eventMap);
|
|
749
|
+
};
|
|
750
|
+
const replace = ($Element, nodes, eventMap = {}) => {
|
|
751
|
+
// Create a temporary container to render the new nodes
|
|
752
|
+
const $Temp = document.createElement('div');
|
|
753
|
+
renderInternal($Temp, nodes, eventMap, eventMap);
|
|
754
|
+
// Replace the current element with the new ones
|
|
755
|
+
const $NewElement = $Temp.firstElementChild;
|
|
756
|
+
$Element.replaceWith($NewElement);
|
|
757
|
+
return $NewElement;
|
|
700
758
|
};
|
|
701
759
|
|
|
702
760
|
const SetText = 1;
|
|
761
|
+
const Replace = 2;
|
|
703
762
|
const SetAttribute = 3;
|
|
704
763
|
const RemoveAttribute = 4;
|
|
705
764
|
const Add = 6;
|
|
@@ -708,12 +767,12 @@ const NavigateParent = 8;
|
|
|
708
767
|
const RemoveChild = 9;
|
|
709
768
|
const NavigateSibling = 10;
|
|
710
769
|
|
|
711
|
-
const applyPatch = ($Element, patches) => {
|
|
770
|
+
const applyPatch = ($Element, patches, eventMap = {}) => {
|
|
712
771
|
let $Current = $Element;
|
|
713
772
|
for (const patch of patches) {
|
|
714
773
|
switch (patch.type) {
|
|
715
774
|
case Add:
|
|
716
|
-
add$1($Current, patch.nodes);
|
|
775
|
+
add$1($Current, patch.nodes, eventMap);
|
|
717
776
|
break;
|
|
718
777
|
case NavigateChild:
|
|
719
778
|
$Current = $Current.childNodes[patch.index];
|
|
@@ -730,6 +789,9 @@ const applyPatch = ($Element, patches) => {
|
|
|
730
789
|
case RemoveChild:
|
|
731
790
|
removeChild($Current, patch.index);
|
|
732
791
|
break;
|
|
792
|
+
case Replace:
|
|
793
|
+
$Current = replace($Current, patch.nodes, eventMap);
|
|
794
|
+
break;
|
|
733
795
|
case SetAttribute:
|
|
734
796
|
setAttribute($Current, patch.key, patch.value);
|
|
735
797
|
break;
|