From ad8ec684439fffd8d9b437753548cd3883653042 Mon Sep 17 00:00:00 2001 From: baiyongjie <407221377@qq.com> Date: Thu, 13 Apr 2023 10:28:14 +0800 Subject: [PATCH] fix: correct active tree node after moving an clone node --- src/public/app/widgets/note_tree.js | 65 ++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/src/public/app/widgets/note_tree.js b/src/public/app/widgets/note_tree.js index 7837672d2..d438bba4b 100644 --- a/src/public/app/widgets/note_tree.js +++ b/src/public/app/widgets/note_tree.js @@ -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()); @@ -251,12 +251,12 @@ export default class NoteTreeWidget extends NoteContextAwareWidget { // code inspired by https://gist.github.com/jtsternberg/c272d7de5b967cec2d3d const isEnclosing = ($container, $sub) => { - const conOffset = $container.offset(); - const conDistanceFromTop = conOffset.top + $container.outerHeight(true); + const conOffset = $container.offset(); + const conDistanceFromTop = conOffset.top + $container.outerHeight(true); const conDistanceFromLeft = conOffset.left + $container.outerWidth(true); - const subOffset = $sub.offset(); - const subDistanceFromTop = subOffset.top + $sub.outerHeight(true); + const subOffset = $sub.offset(); + const subDistanceFromTop = subOffset.top + $sub.outerHeight(true); const subDistanceFromLeft = subOffset.left + $sub.outerWidth(true); return conDistanceFromTop > subDistanceFromTop @@ -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) { - await parentNode.setExpanded(true, {noAnimation: true}); + 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,12 +1164,16 @@ export default class NoteTreeWidget extends NoteContextAwareWidget { for (const node of this.getNodesByBranch(ecBranch)) { if (ecBranch.isDeleted) { if (node.isActive()) { - const newActiveNode = node.getNextSibling() - || node.getPrevSibling() - || node.getParent(); + if (allBranchesDeleted) { + const newActiveNode = node.getNextSibling() + || node.getPrevSibling() + || node.getParent(); - if (newActiveNode) { - newActiveNode.setActive(true, {noEvents: true, noFocus: true}); + if (newActiveNode) { + newActiveNode.setActive(true, {noEvents: true, noFocus: true}); + } + } else { + movedActiveNode = node; } } @@ -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 + } } }