mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	refactoring of note deletion
This commit is contained in:
		| @@ -2,11 +2,16 @@ | ||||
|  | ||||
| const Entity = require('./entity'); | ||||
| const utils = require('../services/utils'); | ||||
| const repository = require('../services/repository'); | ||||
|  | ||||
| class Branch extends Entity { | ||||
|     static get tableName() { return "branches"; } | ||||
|     static get primaryKeyName() { return "branchId"; } | ||||
|  | ||||
|     async getNote() { | ||||
|         return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); | ||||
|     } | ||||
|  | ||||
|     beforeSaving() { | ||||
|         this.dateModified = utils.nowDate() | ||||
|     } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| "use strict"; | ||||
|  | ||||
| const utils = require('../services/utils'); | ||||
| const repository = require('../services/repository'); | ||||
|  | ||||
| class Entity { | ||||
|     constructor(row) { | ||||
| @@ -10,6 +11,10 @@ class Entity { | ||||
|             this[key] = row[key]; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     async save() { | ||||
|         await repository.updateEntity(this); | ||||
|     } | ||||
| } | ||||
|  | ||||
| module.exports = Entity; | ||||
| @@ -84,11 +84,11 @@ class Note extends Entity { | ||||
|         return await repository.getEntities("SELECT * FROM note_images WHERE noteId = ? AND isDeleted = 0", [this.noteId]); | ||||
|     } | ||||
|  | ||||
|     async getTrees() { | ||||
|     async getBranches() { | ||||
|         return await repository.getEntities("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ?", [this.noteId]); | ||||
|     } | ||||
|  | ||||
|     async getChild(name) { | ||||
|     async getChildNote(name) { | ||||
|         return await repository.getEntity(` | ||||
|           SELECT notes.*  | ||||
|           FROM branches  | ||||
| @@ -99,7 +99,7 @@ class Note extends Entity { | ||||
|                 AND notes.title = ?`, [this.noteId, name]); | ||||
|     } | ||||
|  | ||||
|     async getChildren() { | ||||
|     async getChildNotes() { | ||||
|         return await repository.getEntities(` | ||||
|           SELECT notes.*  | ||||
|           FROM branches  | ||||
| @@ -110,7 +110,16 @@ class Note extends Entity { | ||||
|           ORDER BY branches.notePosition`, [this.noteId]); | ||||
|     } | ||||
|  | ||||
|     async getParents() { | ||||
|     async getChildBranches() { | ||||
|         return await repository.getEntities(` | ||||
|           SELECT branches.*  | ||||
|           FROM branches  | ||||
|           WHERE branches.isDeleted = 0 | ||||
|                 AND branches.parentNoteId = ? | ||||
|           ORDER BY branches.notePosition`, [this.noteId]); | ||||
|     } | ||||
|  | ||||
|     async getParentNotes() { | ||||
|         return await repository.getEntities(` | ||||
|           SELECT parent_notes.*  | ||||
|           FROM  | ||||
| @@ -121,16 +130,6 @@ class Note extends Entity { | ||||
|                 AND parent_notes.isDeleted = 0`, [this.noteId]); | ||||
|     } | ||||
|  | ||||
|     async getBranch() { | ||||
|         return await repository.getEntities(` | ||||
|           SELECT branches.*  | ||||
|           FROM branches  | ||||
|             JOIN notes USING(noteId)  | ||||
|           WHERE notes.isDeleted = 0 | ||||
|                 AND branches.isDeleted = 0 | ||||
|                 AND branches.noteId = ?`, [this.noteId]); | ||||
|     } | ||||
|  | ||||
|     beforeSaving() { | ||||
|         if (this.isJson()) { | ||||
|             this.content = JSON.stringify(this.jsonContent, null, '\t'); | ||||
|   | ||||
| @@ -17,7 +17,7 @@ class NoteShort { | ||||
|         const branches = []; | ||||
|  | ||||
|         for (const parent of this.treeCache.parents[this.noteId]) { | ||||
|             branches.push(await this.treeCache.getBranchByChildParent(this.noteId, p.noteId)); | ||||
|             branches.push(await this.treeCache.getBranchByChildParent(this.noteId, parent.noteId)); | ||||
|         } | ||||
|  | ||||
|         return branches; | ||||
|   | ||||
| @@ -7,7 +7,7 @@ import editTreePrefixDialog from "../dialogs/edit_tree_prefix.js"; | ||||
|  | ||||
| const keyBindings = { | ||||
|     "del": node => { | ||||
|         treeChangesService.deleteNodes(getSelectedNodes(true)); | ||||
|         treeChangesService.deleteNodes(treeService.getSelectedNodes(true)); | ||||
|     }, | ||||
|     "ctrl+up": node => { | ||||
|         const beforeNode = node.getPrevSibling(); | ||||
|   | ||||
| @@ -5,6 +5,7 @@ const utils = require('../../services/utils'); | ||||
| const sync_table = require('../../services/sync_table'); | ||||
| const tree = require('../../services/tree'); | ||||
| const notes = require('../../services/notes'); | ||||
| const repository = require('../../services/repository'); | ||||
|  | ||||
| /** | ||||
|  * Code in this file deals with moving and cloning note tree rows. Relationship between note and parent note is unique | ||||
| @@ -101,7 +102,9 @@ async function setExpanded(req) { | ||||
| } | ||||
|  | ||||
| async function deleteBranch(req) { | ||||
|     await notes.deleteNote(req.params.branchId); | ||||
|     const branch = await repository.getBranch(req.params.branchId); | ||||
|  | ||||
|     await notes.deleteNote(branch); | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   | ||||
| @@ -115,7 +115,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {}) | ||||
| async function protectNoteRecursively(note, protect) { | ||||
|     await protectNote(note, protect); | ||||
|  | ||||
|     for (const child of await note.getChildren()) { | ||||
|     for (const child of await note.getChildNotes()) { | ||||
|         await protectNoteRecursively(child, protect); | ||||
|     } | ||||
| } | ||||
| @@ -229,30 +229,23 @@ async function updateNote(noteId, noteUpdates) { | ||||
|     await protectNoteRevisions(note); | ||||
| } | ||||
|  | ||||
| async function deleteNote(branchId) { | ||||
|     const branch = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [branchId]); | ||||
|  | ||||
| async function deleteNote(branch) { | ||||
|     if (!branch || branch.isDeleted === 1) { | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     const now = utils.nowDate(); | ||||
|     branch.isDeleted = true; | ||||
|     await branch.save(); | ||||
|  | ||||
|     await sql.execute("UPDATE branches SET isDeleted = 1, dateModified = ? WHERE branchId = ?", [now, branchId]); | ||||
|     await sync_table.addBranchSync(branchId); | ||||
|     const note = await branch.getNote(); | ||||
|     const notDeletedBranches = await note.getBranches(); | ||||
|  | ||||
|     const noteId = await sql.getValue("SELECT noteId FROM branches WHERE branchId = ?", [branchId]); | ||||
|     if (notDeletedBranches.length === 0) { | ||||
|         note.isDeleted = true; | ||||
|         await note.save(); | ||||
|  | ||||
|     const notDeletedBranchsCount = await sql.getValue("SELECT COUNT(*) FROM branches WHERE noteId = ? AND isDeleted = 0", [noteId]); | ||||
|  | ||||
|     if (!notDeletedBranchsCount) { | ||||
|         await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]); | ||||
|         await sync_table.addNoteSync(noteId); | ||||
|  | ||||
|         const children = await sql.getRows("SELECT branchId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); | ||||
|  | ||||
|         for (const child of children) { | ||||
|             await deleteNote(child.branchId); | ||||
|         for (const childBranch of await note.getChildBranches()) { | ||||
|             await deleteNote(childBranch); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -29,6 +29,10 @@ async function getNote(noteId) { | ||||
|     return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||
| } | ||||
|  | ||||
| async function getBranch(branchId) { | ||||
|     return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]); | ||||
| } | ||||
|  | ||||
| async function updateEntity(entity) { | ||||
|     if (entity.beforeSaving) { | ||||
|         entity.beforeSaving(); | ||||
| @@ -49,6 +53,7 @@ module.exports = { | ||||
|     getEntities, | ||||
|     getEntity, | ||||
|     getNote, | ||||
|     getBranch, | ||||
|     updateEntity, | ||||
|     setEntityConstructor | ||||
| }; | ||||
| @@ -99,7 +99,7 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNote | ||||
|  | ||||
|     const modules = []; | ||||
|  | ||||
|     for (const child of await note.getChildren()) { | ||||
|     for (const child of await note.getChildNotes()) { | ||||
|         const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds); | ||||
|  | ||||
|         if (childBundle) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user