mirror of
https://github.com/zadam/trilium.git
synced 2025-01-18 04:59:56 +08:00
refactored moving note in the tree
This commit is contained in:
parent
68bba623b6
commit
57d19f3302
3 changed files with 53 additions and 31 deletions
|
@ -72,25 +72,6 @@ async function setNodeTitleWithPrefix(node) {
|
|||
node.setTitle(utils.escapeHtml(title));
|
||||
}
|
||||
|
||||
function removeParentChildRelation(parentNoteId, childNoteId) {
|
||||
utils.assertArguments(parentNoteId, childNoteId);
|
||||
|
||||
treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p.noteId !== parentNoteId);
|
||||
treeCache.children[parentNoteId] = treeCache.children[parentNoteId].filter(ch => ch.noteId !== childNoteId);
|
||||
|
||||
delete treeCache.childParentToBranch[childNoteId + '-' + parentNoteId];
|
||||
}
|
||||
|
||||
async function setParentChildRelation(branchId, parentNoteId, childNoteId) {
|
||||
treeCache.parents[childNoteId] = treeCache.parents[childNoteId] || [];
|
||||
treeCache.parents[childNoteId].push(await treeCache.getNote(parentNoteId));
|
||||
|
||||
treeCache.children[parentNoteId] = treeCache.children[parentNoteId] || [];
|
||||
treeCache.children[parentNoteId].push(await treeCache.getNote(childNoteId));
|
||||
|
||||
treeCache.childParentToBranch[childNoteId + '-' + parentNoteId] = await treeCache.getBranch(branchId);
|
||||
}
|
||||
|
||||
async function prepareBranch(noteRows, branchRows) {
|
||||
utils.assertArguments(noteRows);
|
||||
|
||||
|
@ -641,8 +622,6 @@ function collapseTree(node = null) {
|
|||
node.visit(node => node.setExpanded(false));
|
||||
}
|
||||
|
||||
$(document).bind('keydown', 'alt+c', () => collapseTree()); // don't use shortened form since collapseTree() accepts argument
|
||||
|
||||
function scrollToCurrentNote() {
|
||||
const node = getCurrentNode();
|
||||
|
||||
|
@ -796,6 +775,8 @@ $(window).bind('hashchange', function() {
|
|||
}
|
||||
});
|
||||
|
||||
utils.bindShortcut('alt+c', () => collapseTree()); // don't use shortened form since collapseTree() accepts argument
|
||||
|
||||
$createTopLevelNoteButton.click(createNewTopLevelNote);
|
||||
$collapseTreeButton.click(collapseTree);
|
||||
$scrollToCurrentNoteButton.click(scrollToCurrentNote);
|
||||
|
@ -815,8 +796,6 @@ export default {
|
|||
setPrefix,
|
||||
createNewTopLevelNote,
|
||||
createNote,
|
||||
removeParentChildRelation,
|
||||
setParentChildRelation,
|
||||
getSelectedNodes,
|
||||
sortAlphabetically
|
||||
};
|
|
@ -65,6 +65,47 @@ class TreeCache {
|
|||
|
||||
return branch;
|
||||
}
|
||||
|
||||
/* Move note from one parent to another. */
|
||||
async moveNote(childNoteId, oldParentNoteId, newParentNoteId) {
|
||||
utils.assertArguments(childNoteId, oldParentNoteId, newParentNoteId);
|
||||
|
||||
if (oldParentNoteId === newParentNoteId) {
|
||||
return;
|
||||
}
|
||||
|
||||
treeCache.childParentToBranch[childNoteId + '-' + newParentNoteId] = treeCache.childParentToBranch[childNoteId + '-' + oldParentNoteId];
|
||||
delete treeCache.childParentToBranch[childNoteId + '-' + oldParentNoteId]; // this is correct because we know that oldParentId isn't same as newParentId
|
||||
|
||||
// remove old associations
|
||||
treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p.noteId !== oldParentNoteId);
|
||||
treeCache.children[oldParentNoteId] = treeCache.children[oldParentNoteId].filter(ch => ch.noteId !== childNoteId);
|
||||
|
||||
// add new associations
|
||||
treeCache.parents[childNoteId].push(await treeCache.getNote(newParentNoteId));
|
||||
|
||||
treeCache.children[newParentNoteId] = treeCache.children[newParentNoteId] || []; // this might be first child
|
||||
treeCache.children[newParentNoteId].push(await treeCache.getNote(childNoteId));
|
||||
}
|
||||
|
||||
removeParentChildRelation(parentNoteId, childNoteId) {
|
||||
utils.assertArguments(parentNoteId, childNoteId);
|
||||
|
||||
treeCache.parents[childNoteId] = treeCache.parents[childNoteId].filter(p => p.noteId !== parentNoteId);
|
||||
treeCache.children[parentNoteId] = treeCache.children[parentNoteId].filter(ch => ch.noteId !== childNoteId);
|
||||
|
||||
delete treeCache.childParentToBranch[childNoteId + '-' + parentNoteId];
|
||||
}
|
||||
|
||||
async setParentChildRelation(branchId, parentNoteId, childNoteId) {
|
||||
treeCache.parents[childNoteId] = treeCache.parents[childNoteId] || [];
|
||||
treeCache.parents[childNoteId].push(await treeCache.getNote(parentNoteId));
|
||||
|
||||
treeCache.children[parentNoteId] = treeCache.children[parentNoteId] || [];
|
||||
treeCache.children[parentNoteId].push(await treeCache.getNote(childNoteId));
|
||||
|
||||
treeCache.childParentToBranch[childNoteId + '-' + parentNoteId] = await treeCache.getBranch(branchId);
|
||||
}
|
||||
}
|
||||
|
||||
const treeCache = new TreeCache();
|
||||
|
|
|
@ -2,6 +2,7 @@ import treeService from './tree.js';
|
|||
import utils from './utils.js';
|
||||
import server from './server.js';
|
||||
import infoService from "./info.js";
|
||||
import treeCache from "./tree_cache.js";
|
||||
|
||||
async function moveBeforeNode(nodesToMove, beforeNode) {
|
||||
for (const nodeToMove of nodesToMove) {
|
||||
|
@ -12,7 +13,7 @@ async function moveBeforeNode(nodesToMove, beforeNode) {
|
|||
return;
|
||||
}
|
||||
|
||||
changeNode(nodeToMove, node => node.moveTo(beforeNode, 'before'));
|
||||
await changeNode(nodeToMove, node => node.moveTo(beforeNode, 'before'));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,7 +28,7 @@ async function moveAfterNode(nodesToMove, afterNode) {
|
|||
return;
|
||||
}
|
||||
|
||||
changeNode(nodeToMove, node => node.moveTo(afterNode, 'after'));
|
||||
await changeNode(nodeToMove, node => node.moveTo(afterNode, 'after'));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,7 +41,7 @@ async function moveToNode(nodesToMove, toNode) {
|
|||
return;
|
||||
}
|
||||
|
||||
changeNode(nodeToMove, node => {
|
||||
await changeNode(nodeToMove, node => {
|
||||
// first expand which will force lazy load and only then move the node
|
||||
// if this is not expanded before moving, then lazy load won't happen because it already contains node
|
||||
// this doesn't work if this isn't a folder yet, that's why we expand second time below
|
||||
|
@ -107,19 +108,20 @@ async function moveNodeUpInHierarchy(node) {
|
|||
node.getParent().renderTitle();
|
||||
}
|
||||
|
||||
changeNode(node, node => node.moveTo(node.getParent(), 'after'));
|
||||
await changeNode(node, node => node.moveTo(node.getParent(), 'after'));
|
||||
}
|
||||
|
||||
function changeNode(node, func) {
|
||||
async function changeNode(node, func) {
|
||||
utils.assertArguments(node.data.parentNoteId, node.data.noteId);
|
||||
|
||||
treeService.removeParentChildRelation(node.data.parentNoteId, node.data.noteId);
|
||||
const childNoteId = node.data.noteId;
|
||||
const oldParentNoteId = node.data.parentNoteId;
|
||||
|
||||
func(node);
|
||||
|
||||
node.data.parentNoteId = utils.isTopLevelNode(node) ? 'root' : node.getParent().data.noteId;
|
||||
const newParentNoteId = node.data.parentNoteId = utils.isTopLevelNode(node) ? 'root' : node.getParent().data.noteId;
|
||||
|
||||
treeService.setParentChildRelation(node.data.branchId, node.data.parentNoteId, node.data.noteId);
|
||||
await treeCache.moveNote(childNoteId, oldParentNoteId, newParentNoteId);
|
||||
|
||||
treeService.setCurrentNotePathToHash(node);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue