fix: 传递正确的 removeIndex 给到 subtreeModified 钩子

This commit is contained in:
力皓 2020-12-19 21:54:56 +08:00
parent c97b0a39c2
commit 822b2fd797

View File

@ -128,6 +128,8 @@ export class NodeChildren {
slotNode.remove(useMutator, purge); slotNode.remove(useMutator, purge);
}, (iterable, idx) => (iterable as [])[idx]); }, (iterable, idx) => (iterable as [])[idx]);
} }
// 需要在从 children 中删除 node 前记录下 indexinternalSetParent 中会执行删除(unlink)操作
const i = this.children.indexOf(node);
if (purge) { if (purge) {
// should set parent null // should set parent null
node.internalSetParent(null, useMutator); node.internalSetParent(null, useMutator);
@ -142,14 +144,13 @@ export class NodeChildren {
document.selection.remove(node.id); document.selection.remove(node.id);
document.destroyNode(node); document.destroyNode(node);
this.emitter.emit('change'); this.emitter.emit('change');
const i = this.children.indexOf(node);
if (useMutator) { if (useMutator) {
this.reportModified(node, this.owner, { type: 'remove', removeIndex: i, removeNode: node }); this.reportModified(node, this.owner, { type: 'remove', removeIndex: i, removeNode: node });
} }
if (i < 0) { // purge 为 true 时,已在 internalSetParent 中删除了子节点
return false; if (i > -1 && !purge) {
this.children.splice(i, 1);
} }
this.children.splice(i, 1);
return false; return false;
} }