fix: correct active tree node after moving an clone node

This commit is contained in:
baiyongjie 2023-04-13 10:28:14 +08:00
parent 5e88e24693
commit ad8ec68443

View file

@ -223,7 +223,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
return false;
});
this.$treeSettingsPopup.on("click", e => { e.stopPropagation(); });
this.$treeSettingsPopup.on("click", e => {e.stopPropagation();});
$(document).on('click', () => this.$treeSettingsPopup.hide());
@ -673,7 +673,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
return noteList;
}
updateNode(node) {
async updateNode(node) {
const note = froca.getNoteFromCache(node.data.noteId);
const branch = froca.getBranch(node.data.branchId);
@ -697,7 +697,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
node.title = utils.escapeHtml(title);
if (node.isExpanded() !== branch.isExpanded) {
node.setExpanded(branch.isExpanded, {noEvents: true, noAnimation: true});
await node.setExpanded(branch.isExpanded, {noEvents: true, noAnimation: true});
}
node.renderTitle();
@ -849,7 +849,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
await this.setExpandedStatusForSubtree(node, false);
}
collapseTreeEvent() { this.collapseTree(); }
collapseTreeEvent() {this.collapseTree();}
/**
* @returns {FancytreeNode|null}
@ -920,7 +920,9 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
}
if (expand) {
if (!parentNode.isExpanded()) {
await parentNode.setExpanded(true, {noAnimation: true});
}
// although previous line should set the expanded status, it seems to happen asynchronously,
// so we need to make sure it is set properly before calling updateNode which uses this flag
@ -928,7 +930,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
branch.isExpanded = true;
}
this.updateNode(parentNode);
await this.updateNode(parentNode);
let foundChildNode = this.findChildNode(parentNode, childNoteId);
@ -1096,10 +1098,10 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
const activeNode = this.getActiveNode();
const activeNodeFocused = activeNode && activeNode.hasFocus();
const nextNode = activeNode ? (activeNode.getNextSibling() || activeNode.getPrevSibling() || activeNode.getParent()) : null;
const activeNotePath = activeNode ? treeService.getNotePath(activeNode) : null;
let activeNotePath = activeNode ? treeService.getNotePath(activeNode) : null;
const nextNotePath = nextNode ? treeService.getNotePath(nextNode) : null;
const activeNoteId = activeNode ? activeNode.data.noteId : null;
let activeNoteId = activeNode ? activeNode.data.noteId : null;
const noteIdsToUpdate = new Set();
const noteIdsToReload = new Set();
@ -1142,7 +1144,14 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
}
}
for (const ecBranch of loadResults.getBranches()) {
// activeNode is supposed to be moved when we find out activeNode is deleted but not all branches are deleted. save it for fixing activeNodePath after all nodes loaded.
let movedActiveNode = null;
let parentsOfAddedNodes = [];
const allBranches = loadResults.getBranches();
const allBranchesDeleted = allBranches.every(branch => !!branch.isDeleted);
for (const ecBranch of allBranches) {
if (ecBranch.parentNoteId === '_share') {
// all shared notes have a sign in the tree, even the descendants of shared notes
noteIdsToReload.add(ecBranch.noteId);
@ -1155,6 +1164,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
for (const node of this.getNodesByBranch(ecBranch)) {
if (ecBranch.isDeleted) {
if (node.isActive()) {
if (allBranchesDeleted) {
const newActiveNode = node.getNextSibling()
|| node.getPrevSibling()
|| node.getParent();
@ -1162,6 +1172,9 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
if (newActiveNode) {
newActiveNode.setActive(true, {noEvents: true, noFocus: true});
}
} else {
movedActiveNode = node;
}
}
if (node.getParent()) {
@ -1174,12 +1187,13 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
if (!ecBranch.isDeleted) {
for (const parentNode of this.getNodesByNoteId(ecBranch.parentNoteId)) {
parentsOfAddedNodes.push(parentNode)
if (parentNode.isFolder() && !parentNode.isLoaded()) {
continue;
}
const found = (parentNode.getChildren() || []).find(child => child.data.noteId === ecBranch.noteId);
if (!found) {
// make sure it's loaded
await froca.getNote(ecBranch.noteId);
@ -1222,7 +1236,18 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
// for some reason node update cannot be in the batchUpdate() block (node is not re-rendered)
for (const noteId of noteIdsToUpdate) {
for (const node of this.getNodesByNoteId(noteId)) {
this.updateNode(node);
await this.updateNode(node);
}
}
if (movedActiveNode) {
for (const parentNode of parentsOfAddedNodes) {
const found = (parentNode.getChildren() || []).find(child => child.data.noteId === movedActiveNode.data.noteId);
if (found) {
activeNotePath = treeService.getNotePath(found);
activeNoteId = found.data.noteId;
break
}
}
}