@lvce-editor/virtual-dom 6.3.0 → 6.5.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.js +87 -32
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -709,8 +709,10 @@ const setText = ($Element, value) => {
|
|
|
709
709
|
$Element.nodeValue = value;
|
|
710
710
|
};
|
|
711
711
|
const removeChild = ($Element, index) => {
|
|
712
|
-
const $Child = $Element.
|
|
713
|
-
$Child
|
|
712
|
+
const $Child = $Element.childNodes[index];
|
|
713
|
+
if ($Child) {
|
|
714
|
+
$Child.remove();
|
|
715
|
+
}
|
|
714
716
|
};
|
|
715
717
|
const add$1 = ($Element, nodes, eventMap = {}) => {
|
|
716
718
|
renderInternal($Element, nodes, eventMap, eventMap);
|
|
@@ -720,7 +722,15 @@ const replace = ($Element, nodes, eventMap = {}) => {
|
|
|
720
722
|
const $Temp = document.createElement('div');
|
|
721
723
|
renderInternal($Temp, nodes, eventMap, eventMap);
|
|
722
724
|
// Replace the current element with the new ones
|
|
723
|
-
|
|
725
|
+
let $NewElement = $Temp.firstElementChild;
|
|
726
|
+
if (!$NewElement) {
|
|
727
|
+
// If no element was created (e.g., only text nodes), we need to create a wrapper
|
|
728
|
+
// In this case, we create a div and move all children to it
|
|
729
|
+
$NewElement = document.createElement('div');
|
|
730
|
+
while ($Temp.firstChild) {
|
|
731
|
+
$NewElement.append($Temp.firstChild);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
724
734
|
$Element.replaceWith($NewElement);
|
|
725
735
|
return $NewElement;
|
|
726
736
|
};
|
|
@@ -1024,35 +1034,80 @@ const getEventListenerMap = id => {
|
|
|
1024
1034
|
const applyPatch = ($Element, patches, eventMap = {}, id = 0) => {
|
|
1025
1035
|
const events = getEventListenerMap(id) || eventMap;
|
|
1026
1036
|
let $Current = $Element;
|
|
1027
|
-
for (
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1037
|
+
for (let patchIndex = 0; patchIndex < patches.length; patchIndex++) {
|
|
1038
|
+
const patch = patches[patchIndex];
|
|
1039
|
+
try {
|
|
1040
|
+
switch (patch.type) {
|
|
1041
|
+
case Add:
|
|
1042
|
+
add$1($Current, patch.nodes, events);
|
|
1043
|
+
break;
|
|
1044
|
+
case NavigateChild:
|
|
1045
|
+
{
|
|
1046
|
+
const $Child = $Current.childNodes[patch.index];
|
|
1047
|
+
if (!$Child) {
|
|
1048
|
+
console.error('Cannot navigate to child: child not found at index', {
|
|
1049
|
+
$Current,
|
|
1050
|
+
index: patch.index,
|
|
1051
|
+
childCount: $Current.childNodes.length
|
|
1052
|
+
});
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
$Current = $Child;
|
|
1056
|
+
break;
|
|
1057
|
+
}
|
|
1058
|
+
case NavigateParent:
|
|
1059
|
+
{
|
|
1060
|
+
const $Parent = $Current.parentNode;
|
|
1061
|
+
if (!$Parent) {
|
|
1062
|
+
console.error('Cannot navigate to parent: current node has no parent', {
|
|
1063
|
+
$Current
|
|
1064
|
+
});
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
$Current = $Parent;
|
|
1068
|
+
break;
|
|
1069
|
+
}
|
|
1070
|
+
case NavigateSibling:
|
|
1071
|
+
{
|
|
1072
|
+
const $Parent = $Current.parentNode;
|
|
1073
|
+
if (!$Parent) {
|
|
1074
|
+
console.error('Cannot navigate to sibling: current node has no parent', {
|
|
1075
|
+
patchIndex
|
|
1076
|
+
});
|
|
1077
|
+
return;
|
|
1078
|
+
}
|
|
1079
|
+
$Current = $Parent.childNodes[patch.index];
|
|
1080
|
+
if (!$Current) {
|
|
1081
|
+
console.error('Cannot navigate to sibling: sibling not found at index', {
|
|
1082
|
+
$Parent,
|
|
1083
|
+
index: patch.index,
|
|
1084
|
+
childCount: $Parent.childNodes.length
|
|
1085
|
+
});
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
break;
|
|
1089
|
+
}
|
|
1090
|
+
case RemoveAttribute:
|
|
1091
|
+
removeAttribute($Current, patch.key);
|
|
1092
|
+
break;
|
|
1093
|
+
case RemoveChild:
|
|
1094
|
+
removeChild($Current, patch.index);
|
|
1095
|
+
break;
|
|
1096
|
+
case Replace:
|
|
1097
|
+
$Current = replace($Current, patch.nodes, events);
|
|
1098
|
+
break;
|
|
1099
|
+
case SetAttribute:
|
|
1100
|
+
setProp($Current, patch.key, patch.value, events);
|
|
1101
|
+
break;
|
|
1102
|
+
case SetText:
|
|
1103
|
+
setText($Current, patch.value);
|
|
1104
|
+
break;
|
|
1105
|
+
default:
|
|
1106
|
+
break;
|
|
1107
|
+
}
|
|
1108
|
+
} catch (error) {
|
|
1109
|
+
console.error('Error applying patch at index ' + patchIndex, patch, error);
|
|
1110
|
+
throw error;
|
|
1056
1111
|
}
|
|
1057
1112
|
}
|
|
1058
1113
|
};
|