@lvce-editor/virtual-dom 6.2.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.d.ts +1 -1
- package/dist/index.js +116 -62
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -45,7 +45,7 @@ export interface TextPatch {
|
|
|
45
45
|
readonly value: string;
|
|
46
46
|
}
|
|
47
47
|
export type Patch = TextPatch | AttributePatch | ReplacePatch | RemoveAttributePatch | RemovePatch | AddPatch | NavigateChildPatch | NavigateParentPatch | RemoveChildPatch | NavigateSiblingPatch;
|
|
48
|
-
export declare const applyPatch: ($Element: Node, patches: readonly Patch[], eventMap?: Record<string, any
|
|
48
|
+
export declare const applyPatch: ($Element: Node, patches: readonly Patch[], eventMap?: Record<string, any>, id?: any) => void;
|
|
49
49
|
export declare const setComponentUid: ($Element: any, uid: any) => void;
|
|
50
50
|
export declare const getComponentUid: ($Element: any) => number;
|
|
51
51
|
export declare const getComponentUidFromEvent: (event: any) => number;
|
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
|
};
|
|
@@ -735,41 +743,6 @@ const NavigateParent = 8;
|
|
|
735
743
|
const RemoveChild = 9;
|
|
736
744
|
const NavigateSibling = 10;
|
|
737
745
|
|
|
738
|
-
const applyPatch = ($Element, patches, eventMap = {}) => {
|
|
739
|
-
let $Current = $Element;
|
|
740
|
-
for (const patch of patches) {
|
|
741
|
-
switch (patch.type) {
|
|
742
|
-
case Add:
|
|
743
|
-
add$1($Current, patch.nodes, eventMap);
|
|
744
|
-
break;
|
|
745
|
-
case NavigateChild:
|
|
746
|
-
$Current = $Current.childNodes[patch.index];
|
|
747
|
-
break;
|
|
748
|
-
case NavigateParent:
|
|
749
|
-
$Current = $Current.parentNode;
|
|
750
|
-
break;
|
|
751
|
-
case NavigateSibling:
|
|
752
|
-
$Current = $Current.parentNode.childNodes[patch.index];
|
|
753
|
-
break;
|
|
754
|
-
case RemoveAttribute:
|
|
755
|
-
removeAttribute($Current, patch.key);
|
|
756
|
-
break;
|
|
757
|
-
case RemoveChild:
|
|
758
|
-
removeChild($Current, patch.index);
|
|
759
|
-
break;
|
|
760
|
-
case Replace:
|
|
761
|
-
$Current = replace($Current, patch.nodes, eventMap);
|
|
762
|
-
break;
|
|
763
|
-
case SetAttribute:
|
|
764
|
-
setProp($Current, patch.key, patch.value, eventMap);
|
|
765
|
-
break;
|
|
766
|
-
case SetText:
|
|
767
|
-
setText($Current, patch.value);
|
|
768
|
-
break;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
};
|
|
772
|
-
|
|
773
746
|
const dragInfos = Object.create(null);
|
|
774
747
|
const setDragInfo = (id, data) => {
|
|
775
748
|
dragInfos[id] = data;
|
|
@@ -784,32 +757,6 @@ const isDragInfoOld = data => {
|
|
|
784
757
|
return Array.isArray(data);
|
|
785
758
|
};
|
|
786
759
|
|
|
787
|
-
let id = 0;
|
|
788
|
-
const create = () => {
|
|
789
|
-
return ++id;
|
|
790
|
-
};
|
|
791
|
-
|
|
792
|
-
const state = Object.create(null);
|
|
793
|
-
const acquire = id => {
|
|
794
|
-
const promise = state[id];
|
|
795
|
-
delete state[id];
|
|
796
|
-
return promise;
|
|
797
|
-
};
|
|
798
|
-
const getFileHandles = async ids => {
|
|
799
|
-
const promises = ids.map(acquire);
|
|
800
|
-
const handles = await Promise.all(promises);
|
|
801
|
-
return handles;
|
|
802
|
-
};
|
|
803
|
-
const add = promise => {
|
|
804
|
-
const id = create();
|
|
805
|
-
state[id] = promise;
|
|
806
|
-
return id;
|
|
807
|
-
};
|
|
808
|
-
const addFileHandle = fileHandle => {
|
|
809
|
-
const promise = Promise.resolve(fileHandle);
|
|
810
|
-
return add(promise);
|
|
811
|
-
};
|
|
812
|
-
|
|
813
760
|
const setDragImage = (dataTransfer, label) => {
|
|
814
761
|
const dragImage = document.createElement('div');
|
|
815
762
|
dragImage.className = 'DragImage';
|
|
@@ -862,6 +809,32 @@ const enabled = () => {
|
|
|
862
809
|
return ignore;
|
|
863
810
|
};
|
|
864
811
|
|
|
812
|
+
let id = 0;
|
|
813
|
+
const create = () => {
|
|
814
|
+
return ++id;
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
const state = Object.create(null);
|
|
818
|
+
const acquire = id => {
|
|
819
|
+
const promise = state[id];
|
|
820
|
+
delete state[id];
|
|
821
|
+
return promise;
|
|
822
|
+
};
|
|
823
|
+
const getFileHandles = async ids => {
|
|
824
|
+
const promises = ids.map(acquire);
|
|
825
|
+
const handles = await Promise.all(promises);
|
|
826
|
+
return handles;
|
|
827
|
+
};
|
|
828
|
+
const add = promise => {
|
|
829
|
+
const id = create();
|
|
830
|
+
state[id] = promise;
|
|
831
|
+
return id;
|
|
832
|
+
};
|
|
833
|
+
const addFileHandle = fileHandle => {
|
|
834
|
+
const promise = Promise.resolve(fileHandle);
|
|
835
|
+
return add(promise);
|
|
836
|
+
};
|
|
837
|
+
|
|
865
838
|
const unwrapItemString = async item => {
|
|
866
839
|
const {
|
|
867
840
|
resolve,
|
|
@@ -1056,6 +1029,87 @@ const getEventListenerMap = id => {
|
|
|
1056
1029
|
return map;
|
|
1057
1030
|
};
|
|
1058
1031
|
|
|
1032
|
+
const applyPatch = ($Element, patches, eventMap = {}, id = 0) => {
|
|
1033
|
+
const events = getEventListenerMap(id) || eventMap;
|
|
1034
|
+
let $Current = $Element;
|
|
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;
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
};
|
|
1112
|
+
|
|
1059
1113
|
const getActiveElementInside = $Viewlet => {
|
|
1060
1114
|
if (!$Viewlet) {
|
|
1061
1115
|
return undefined;
|