delete notes are now in cache as well which allows simplified update of the tree after deletion

This commit is contained in:
zadam 2019-10-26 22:50:46 +02:00
parent f82e99b5ed
commit 86a330c8c3
6 changed files with 50 additions and 62 deletions

File diff suppressed because one or more lines are too long

View File

@ -32,6 +32,8 @@ class NoteShort {
/** @param {string} content-type, e.g. "application/json" */
this.mime = row.mime;
/** @param {boolean} */
this.isDeleted = row.isDeleted;
/** @param {boolean} */
this.archived = row.archived;
/** @param {string} */
this.cssClass = row.cssClass;

View File

@ -80,6 +80,22 @@ async function moveToNode(nodesToMove, toNode) {
}
}
async function getNextNode(nodes) {
// following code assumes that nodes contain only top-most selected nodes - getSelectedNodes has been
// called with stopOnParent=true
let next = nodes[nodes.length - 1].getNextSibling();
if (!next) {
next = nodes[0].getPrevSibling();
}
if (!next && !await hoistedNoteService.isRootNode(nodes[0])) {
next = nodes[0].getParent();
}
return treeUtils.getNotePath(next);
}
async function deleteNodes(nodes) {
nodes = await filterRootNote(nodes);
@ -130,47 +146,11 @@ async function deleteNodes(nodes) {
}
}
if (deleteClones) {
// if clones are also deleted we give up with targeted cleanup of the tree
treeService.reload();
return true;
}
const nextNotePath = await getNextNode(nodes);
// following code assumes that nodes contain only top-most selected nodes - getSelectedNodes has been
// called with stopOnParent=true
let next = nodes[nodes.length - 1].getNextSibling();
const noteIds = Array.from(new Set(nodes.map(node => node.data.noteId)));
if (!next) {
next = nodes[0].getPrevSibling();
}
if (!next && !await hoistedNoteService.isRootNode(nodes[0])) {
next = nodes[0].getParent();
}
let activeNotePath = null;
if (next) {
activeNotePath = await treeUtils.getNotePath(next);
}
await treeService.loadTreeCache();
const parentNoteIds = Array.from(new Set(nodes.map(node => node.getParent().data.noteId)));
for (const node of nodes) {
node.remove();
}
await treeService.reloadNotes(parentNoteIds);
// activate after all the reloading
if (activeNotePath) {
treeService.focusTree();
const node = await treeService.activateNote(activeNotePath);
node.setFocus(true);
}
await treeService.reloadNotes(noteIds, nextNotePath);
return true;
}

View File

@ -559,14 +559,10 @@ function getHashValueFromAddress() {
return str.split("-");
}
async function loadTreeCache() {
async function loadTree() {
const resp = await server.get('tree');
treeCache.load(resp.notes, resp.branches);
}
async function loadTree() {
await loadTreeCache();
return await treeBuilder.prepareTree();
}
@ -828,25 +824,34 @@ async function checkFolderStatus(node) {
node.renderTitle();
}
async function reloadNotes(noteIds) {
async function reloadNotes(noteIds, activateNotePath = null) {
if (noteIds.length === 0) {
return;
}
await treeCache.reloadNotes(noteIds);
const activeNotePath = noteDetailService.getActiveTabNotePath();
if (!activateNotePath) {
activateNotePath = noteDetailService.getActiveTabNotePath();
}
for (const noteId of noteIds) {
for (const node of getNodesByNoteId(noteId)) {
await node.load(true);
const branch = treeCache.getBranch(node.data.branchId, true);
await checkFolderStatus(node);
if (!branch) {
node.remove();
}
else {
await node.load(true);
await checkFolderStatus(node);
}
}
}
if (activeNotePath) {
const node = await getNodeFromPath(activeNotePath);
if (activateNotePath) {
const node = await getNodeFromPath(activateNotePath);
if (node) {
await node.setActive(true, {noEvents: true}); // this node has been already active so no need to fire events again
@ -926,7 +931,6 @@ export default {
getNodesByNoteId,
checkFolderStatus,
reloadNotes,
loadTreeCache,
expandToNote,
getNodeFromPath,
resolveNotePath,

View File

@ -77,11 +77,11 @@ class TreeCache {
}
}
for (const branch of branchesByNotes[noteId]) {
for (const branch of branchesByNotes[noteId] || []) { // can be empty for deleted notes
this.addBranch(branch);
}
const note = new NoteShort(this, noteRow, branchesByNotes[noteId]);
const note = new NoteShort(this, noteRow, branchesByNotes[noteId] || []);
this.notes[note.noteId] = note;
@ -158,9 +158,11 @@ class TreeCache {
}
/** @return {Branch} */
getBranch(branchId) {
getBranch(branchId, silentNotFoundError = false) {
if (!(branchId in this.branches)) {
console.error(`Not existing branch ${branchId}`);
if (!silentNotFoundError) {
console.error(`Not existing branch ${branchId}`);
}
}
else {
return this.branches[branchId];

View File

@ -6,17 +6,17 @@ const protectedSessionService = require('../../services/protected_session');
const noteCacheService = require('../../services/note_cache');
async function getNotes(noteIds) {
// we return also deleted notes which have been specifically asked for
const notes = await sql.getManyRows(`
SELECT
noteId,
title,
isProtected,
type,
mime
FROM
notes
WHERE isDeleted = 0
AND noteId IN (???)`, noteIds);
mime,
isDeleted
FROM notes
WHERE noteId IN (???)`, noteIds);
const cssClassLabels = await sql.getManyRows(`
SELECT noteId, value FROM attributes WHERE isDeleted = 0 AND type = 'label'