From 9afea492db28020ddba5eaa57e92d38f527c24ca Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 8 May 2021 23:31:20 +0200 Subject: [PATCH] delete note fixes --- src/public/app/services/tree.js | 4 +- src/public/app/widgets/note_tree.js | 2 +- src/routes/api/recent_changes.js | 4 +- src/routes/api/script.js | 2 +- src/services/becca/becca_loader.js | 38 +++++++------------ .../becca/entities/abstract_entity.js | 14 +++---- src/services/becca/entities/note.js | 15 ++++++++ src/services/image.js | 2 +- src/services/notes.js | 10 ++--- 9 files changed, 46 insertions(+), 45 deletions(-) diff --git a/src/public/app/services/tree.js b/src/public/app/services/tree.js index 7c6107af7..f8d03e4db 100644 --- a/src/public/app/services/tree.js +++ b/src/public/app/services/tree.js @@ -113,13 +113,13 @@ function getSomeNotePathSegments(note, hoistedNotePath = 'root') { const notePaths = note.getSortedNotePaths(hoistedNotePath); - return notePaths[0].notePath; + return notePaths.length > 0 ? notePaths[0].notePath : null; } function getSomeNotePath(note, hoistedNotePath = 'root') { const notePath = getSomeNotePathSegments(note, hoistedNotePath); - return notePath.join('/'); + return notePath === null ? null : notePath.join('/'); } async function sortAlphabetically(noteId) { diff --git a/src/public/app/widgets/note_tree.js b/src/public/app/widgets/note_tree.js index c9e7de7d6..8ad63e95f 100644 --- a/src/public/app/widgets/note_tree.js +++ b/src/public/app/widgets/note_tree.js @@ -1166,7 +1166,7 @@ export default class NoteTreeWidget extends TabAwareWidget { const newActiveNode = this.getActiveNode(); // return focus if the previously active node was also focused - if (newActiveNode && activeNodeFocused) {console.log("FOCUSING!!!"); + if (newActiveNode && activeNodeFocused) { newActiveNode.setFocus(true); } }); diff --git a/src/routes/api/recent_changes.js b/src/routes/api/recent_changes.js index 5d7fe8044..750a4e23a 100644 --- a/src/routes/api/recent_changes.js +++ b/src/routes/api/recent_changes.js @@ -81,10 +81,10 @@ function getRecentChanges(req) { if (change.current_isDeleted) { const deleteId = change.current_deleteId; - const undeletedParentBranches = noteService.getUndeletedParentBranches(change.noteId, deleteId); + const undeletedParentBranchIds = noteService.getUndeletedParentBranchIds(change.noteId, deleteId); // note (and the subtree) can be undeleted if there's at least one undeleted parent (whose branch would be undeleted by this op) - change.canBeUndeleted = undeletedParentBranches.length > 0; + change.canBeUndeleted = undeletedParentBranchIds.length > 0; } } diff --git a/src/routes/api/script.js b/src/routes/api/script.js index 343169496..de673dd5b 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -2,7 +2,7 @@ const scriptService = require('../../services/script'); const attributeService = require('../../services/attributes'); -const repository = require('../../services/repository'); +const becca = require('../../services/becca/becca'); const syncService = require('../../services/sync'); async function exec(req) { diff --git a/src/services/becca/becca_loader.js b/src/services/becca/becca_loader.js index 29f5c301d..d33ef233e 100644 --- a/src/services/becca/becca_loader.js +++ b/src/services/becca/becca_loader.js @@ -59,32 +59,26 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_CHANGE_ } }); -eventService.subscribe([eventService.ENTITY_DELETED, eventService.ENTITY_DELETE_SYNCED], ({entityName, entityId}) => { +eventService.subscribe([eventService.ENTITY_DELETED, eventService.ENTITY_DELETE_SYNCED], ({entityName, entity}) => { if (!becca.loaded) { return; } if (entityName === 'notes') { - noteDeleted(entityId); + noteDeleted(entity); } else if (entityName === 'branches') { - branchDeleted(entityId); + branchDeleted(entity); } else if (entityName === 'attributes') { - attributeDeleted(entityId); + attributeDeleted(entity); } }); -function noteDeleted(noteId) { - delete becca.notes[noteId]; +function noteDeleted(note) { + delete becca.notes[note.noteId]; } -function branchDeleted(branchId) { - const branch = becca.branches[branchId]; - - if (!branch) { - return; - } - - const childNote = becca.notes[noteId]; +function branchDeleted(branch) { + const childNote = becca.notes[branch.noteId]; if (childNote) { childNote.parents = childNote.parents.filter(parent => parent.noteId !== branch.parentNoteId); @@ -115,13 +109,7 @@ function branchUpdated(branch) { } } -function attributeDeleted(attributeId) { - const attribute = becca.attributes[attributeId]; - - if (!attribute) { - return; - } - +function attributeDeleted(attribute) { const note = becca.notes[attribute.noteId]; if (note) { @@ -132,21 +120,21 @@ function attributeDeleted(attributeId) { note.invalidateThisCache(); } - note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attributeId); + note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attribute.attributeId); const targetNote = attribute.targetNote; if (targetNote) { - targetNote.targetRelations = targetNote.targetRelations.filter(rel => rel.attributeId !== attributeId); + targetNote.targetRelations = targetNote.targetRelations.filter(rel => rel.attributeId !== attribute.attributeId); } } - delete becca.attributes[attributeId]; + delete becca.attributes[attribute.attributeId]; const key = `${attribute.type}-${attribute.name.toLowerCase()}`; if (key in becca.attributeIndex) { - becca.attributeIndex[key] = becca.attributeIndex[key].filter(attr => attr.attributeId !== attributeId); + becca.attributeIndex[key] = becca.attributeIndex[key].filter(attr => attr.attributeId !== attribute.attributeId); } } diff --git a/src/services/becca/entities/abstract_entity.js b/src/services/becca/entities/abstract_entity.js index 7630904ff..be429b969 100644 --- a/src/services/becca/entities/abstract_entity.js +++ b/src/services/becca/entities/abstract_entity.js @@ -96,17 +96,15 @@ class AbstractEntity { } markAsDeleted(deleteId = null) { - sql.execute(`UPDATE ${this.constructor.entityName} SET isDeleted = 1, deleteId = ? WHERE ${this.constructor.primaryKeyName} = ?`, - [deleteId, this[this.constructor.primaryKeyName]]); + const entityId = this[this.constructor.primaryKeyName]; + const entityName = this.constructor.entityName; + + sql.execute(`UPDATE ${entityName} SET isDeleted = 1, deleteId = ? WHERE ${this.constructor.primaryKeyName} = ?`, + [deleteId, entityId]); this.addEntityChange(true); - const eventPayload = { - entityName: this.constructor.entityName, - entity: this - }; - - eventService.emit(eventService.ENTITY_DELETED, eventPayload); + eventService.emit(eventService.ENTITY_DELETED, { entityName, entity: this }); } } diff --git a/src/services/becca/entities/note.js b/src/services/becca/entities/note.js index 0c74292e4..d060d7b84 100644 --- a/src/services/becca/entities/note.js +++ b/src/services/becca/entities/note.js @@ -551,6 +551,17 @@ class Note extends AbstractEntity { } } + /** + * @returns {Attribute} attribute belonging to this specific note (excludes inherited attributes) + * + * This method can be significantly faster than the getAttribute() + */ + getOwnedAttribute(type, name) { + const attrs = this.getOwnedAttributes(type, name); + + return attrs.length > 0 ? attrs[0] : null; + } + get isArchived() { return this.hasAttribute('label', 'archived'); } @@ -773,6 +784,10 @@ class Note extends AbstractEntity { return this.ancestorCache; } + getTargetRelations() { + return this.targetRelations; + } + /** @return {Note[]} - returns only notes which are templated, does not include their subtrees * in effect returns notes which are influenced by note's non-inheritable attributes */ get templatedNotes() { diff --git a/src/services/image.js b/src/services/image.js index 4d2338e22..66edae160 100644 --- a/src/services/image.js +++ b/src/services/image.js @@ -1,6 +1,6 @@ "use strict"; -const repository = require('./repository'); +const becca = require('./becca/becca'); const log = require('./log'); const protectedSessionService = require('./protected_session'); const noteService = require('./notes'); diff --git a/src/services/notes.js b/src/services/notes.js index 5bfbae49c..690c4a869 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -523,7 +523,7 @@ function updateNote(noteId, noteUpdates) { function deleteBranch(branch, deleteId, taskContext) { taskContext.increaseProgressCount(); - if (!branch || branch.isDeleted) { + if (!branch) { return false; } @@ -531,7 +531,7 @@ function deleteBranch(branch, deleteId, taskContext) { || branch.noteId === 'root' || branch.noteId === cls.getHoistedNoteId()) { - throw new Error("Can't delete root branch/note"); + throw new Error("Can't delete root or hoisted branch/note"); } branch.markAsDeleted(deleteId); @@ -546,8 +546,6 @@ function deleteBranch(branch, deleteId, taskContext) { // first delete children and then parent - this will show up better in recent changes - note.markAsDeleted(deleteId); - log.info("Deleting note " + note.noteId); for (const attribute of note.getOwnedAttributes()) { @@ -558,6 +556,8 @@ function deleteBranch(branch, deleteId, taskContext) { relation.markAsDeleted(deleteId); } + note.markAsDeleted(deleteId); + return true; } else { @@ -874,7 +874,7 @@ module.exports = { scanForLinks, duplicateSubtree, duplicateSubtreeWithoutRoot, - getUndeletedParentBranches: getUndeletedParentBranchIds, + getUndeletedParentBranchIds, triggerNoteTitleChanged, eraseDeletedNotesNow };