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