@lvce-editor/virtual-dom 5.2.0 → 6.0.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 +1 -2
- package/dist/index.js +64 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
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;
|
|
@@ -698,8 +747,18 @@ const removeChild = ($Element, index) => {
|
|
|
698
747
|
const add$1 = ($Element, nodes) => {
|
|
699
748
|
renderInternal($Element, nodes, {}, {});
|
|
700
749
|
};
|
|
750
|
+
const replace = ($Element, nodes) => {
|
|
751
|
+
// Create a temporary container to render the new nodes
|
|
752
|
+
const $Temp = document.createElement('div');
|
|
753
|
+
renderInternal($Temp, nodes, {}, {});
|
|
754
|
+
// Replace the current element with the new ones
|
|
755
|
+
const $NewElement = $Temp.firstElementChild;
|
|
756
|
+
$Element.replaceWith($NewElement);
|
|
757
|
+
return $NewElement;
|
|
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;
|
|
@@ -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);
|
|
794
|
+
break;
|
|
733
795
|
case SetAttribute:
|
|
734
796
|
setAttribute($Current, patch.key, patch.value);
|
|
735
797
|
break;
|