check added nodes from removed nodes and dropped nodes

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent aa3e2f02ed
commit 7c35cb2f49
5 changed files with 937 additions and 12 deletions

View File

@@ -47,7 +47,7 @@ function initMutationObserver(cb: mutationCallBack): MutationObserver {
const observer = new MutationObserver(mutations => {
const texts: textCursor[] = [];
const attributes: attributeCursor[] = [];
const removes: removedNodeMutation[] = [];
let removes: removedNodeMutation[] = [];
const adds: addedNodeMutation[] = [];
const dropped: Node[] = [];
@@ -123,8 +123,21 @@ function initMutationObserver(cb: mutationCallBack): MutationObserver {
}
});
removes = removes.map(remove => {
if (remove.parentNode) {
remove.parentId = mirror.getId(remove.parentNode as INode);
delete remove.parentNode;
}
return remove;
});
Array.from(addsSet).forEach(n => {
if (n.parentNode && dropped.indexOf(n.parentNode) < 0) {
const parentId = mirror.getId(n.parentNode as INode);
if (
parentId &&
!dropped.some(d => d === n.parentNode) &&
!removes.some(r => r.id === parentId)
) {
adds.push({
parentId: mirror.getId(n.parentNode as INode),
previousId: !n.previousSibling
@@ -149,13 +162,7 @@ function initMutationObserver(cb: mutationCallBack): MutationObserver {
id: mirror.getId(attribute.node as INode),
attributes: attribute.attributes,
})),
removes: removes.map(remove => {
if (remove.parentNode) {
remove.parentId = mirror.getId(remove.parentNode as INode);
delete remove.parentNode;
}
return remove;
}),
removes,
adds,
});
});