added new runOnNoteDeletion, runOnBranchCreation, runOnBranchDeletion, #2898

This commit is contained in:
zadam 2022-06-05 14:58:19 +02:00
parent b5214e6cea
commit f587e0dfd9
6 changed files with 40 additions and 7 deletions

View File

@ -5,9 +5,9 @@ const AbstractEntity = require("./abstract_entity");
const sql = require("../../services/sql"); const sql = require("../../services/sql");
const dateUtils = require("../../services/date_utils"); const dateUtils = require("../../services/date_utils");
const utils = require("../../services/utils.js"); const utils = require("../../services/utils.js");
const TaskContext = require("../../services/task_context.js"); const TaskContext = require("../../services/task_context");
const cls = require("../../services/cls.js"); const cls = require("../../services/cls");
const log = require("../../services/log.js"); const log = require("../../services/log");
/** /**
* Branch represents a relationship between a child note and its parent note. Trilium allows a note to have multiple * Branch represents a relationship between a child note and its parent note. Trilium allows a note to have multiple
@ -137,6 +137,18 @@ class Branch extends AbstractEntity {
taskContext.increaseProgressCount(); taskContext.increaseProgressCount();
const note = this.getNote();
if (!taskContext.noteDeletionHandlerTriggered) {
const parentBranches = note.getParentBranches();
if (parentBranches.length === 1 && parentBranches[0] === this) {
// needs to be run before branches and attributes are deleted and thus attached relations disappear
const handlers = require("../../services/handlers");
handlers.runAttachedRelations(note, 'runOnNoteDeletion', note);
}
}
if (this.branchId === 'root' if (this.branchId === 'root'
|| this.noteId === 'root' || this.noteId === 'root'
|| this.noteId === cls.getHoistedNoteId()) { || this.noteId === cls.getHoistedNoteId()) {
@ -146,7 +158,6 @@ class Branch extends AbstractEntity {
this.markAsDeleted(deleteId); this.markAsDeleted(deleteId);
const note = this.getNote();
const notDeletedBranches = note.getParentBranches(); const notDeletedBranches = note.getParentBranches();
if (notDeletedBranches.length === 0) { if (notDeletedBranches.length === 0) {

View File

@ -8,9 +8,8 @@ const dateUtils = require('../../services/date_utils');
const entityChangesService = require('../../services/entity_changes'); const entityChangesService = require('../../services/entity_changes');
const AbstractEntity = require("./abstract_entity"); const AbstractEntity = require("./abstract_entity");
const NoteRevision = require("./note_revision"); const NoteRevision = require("./note_revision");
const TaskContext = require("../../services/task_context.js"); const TaskContext = require("../../services/task_context");
const optionService = require("../../services/options.js"); const handlers = require("../../services/handlers");
const noteRevisionService = require("../../services/note_revisions.js");
const LABEL = 'label'; const LABEL = 'label';
const RELATION = 'relation'; const RELATION = 'relation';
@ -1143,6 +1142,10 @@ class Note extends AbstractEntity {
taskContext = new TaskContext('no-progress-reporting'); taskContext = new TaskContext('no-progress-reporting');
} }
// needs to be run before branches and attributes are deleted and thus attached relations disappear
handlers.runAttachedRelations(this, 'runOnNoteDeletion', this);
taskContext.noteDeletionHandlerTriggered = true;
for (const branch of this.getParentBranches()) { for (const branch of this.getParentBranches()) {
branch.deleteBranch(deleteId, taskContext); branch.deleteBranch(deleteId, taskContext);
} }

View File

@ -233,6 +233,9 @@ const ATTR_HELP = {
"runOnNoteCreation": "executes when note is created on backend", "runOnNoteCreation": "executes when note is created on backend",
"runOnNoteTitleChange": "executes when note title is changed (includes note creation as well)", "runOnNoteTitleChange": "executes when note title is changed (includes note creation as well)",
"runOnNoteChange": "executes when note is changed (includes note creation as well)", "runOnNoteChange": "executes when note is changed (includes note creation as well)",
"runOnNoteDeletion": "executes when note is being deleted",
"runOnBranchCreation": "executes when a branch is created. Branch is a link between parent note and child note and is created e.g. when cloning or moving note.",
"runOnBranchDeletion": "executes when a branch is delete. Branch is a link between parent note and child note and is deleted e.g. when moving note (old branch/link is deleted).",
"runOnChildNoteCreation": "executes when new note is created under this note", "runOnChildNoteCreation": "executes when new note is created under this note",
"runOnAttributeCreation": "executes when new attribute is created under this note", "runOnAttributeCreation": "executes when new attribute is created under this note",
"runOnAttributeChange": "executes when attribute is changed under this note", "runOnAttributeChange": "executes when attribute is changed under this note",

View File

@ -60,6 +60,9 @@ module.exports = [
{ type: 'relation', name: 'runOnNoteCreation', isDangerous: true }, { type: 'relation', name: 'runOnNoteCreation', isDangerous: true },
{ type: 'relation', name: 'runOnNoteTitleChange', isDangerous: true }, { type: 'relation', name: 'runOnNoteTitleChange', isDangerous: true },
{ type: 'relation', name: 'runOnNoteChange', isDangerous: true }, { type: 'relation', name: 'runOnNoteChange', isDangerous: true },
{ type: 'relation', name: 'runOnNoteDeletion', isDangerous: true },
{ type: 'relation', name: 'runOnBranchCreation', isDangerous: true },
{ type: 'relation', name: 'runOnBranchDeletion', isDangerous: true },
{ type: 'relation', name: 'runOnChildNoteCreation', isDangerous: true }, { type: 'relation', name: 'runOnChildNoteCreation', isDangerous: true },
{ type: 'relation', name: 'runOnAttributeCreation', isDangerous: true }, { type: 'relation', name: 'runOnAttributeCreation', isDangerous: true },
{ type: 'relation', name: 'runOnAttributeChange', isDangerous: true }, { type: 'relation', name: 'runOnAttributeChange', isDangerous: true },

View File

@ -49,6 +49,7 @@ eventService.subscribe([ eventService.ENTITY_CHANGED, eventService.ENTITY_DELETE
} }
} }
else if (entityName === 'notes') { else if (entityName === 'notes') {
// ENTITY_DELETED won't trigger anything since all branches/attributes are already deleted at this point
runAttachedRelations(entity, 'runOnNoteChange', entity); runAttachedRelations(entity, 'runOnNoteChange', entity);
} }
}); });
@ -94,6 +95,9 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
handleSortedAttribute(entity); handleSortedAttribute(entity);
} }
} }
else if (entityName === 'branches') {
runAttachedRelations(entity.getNote(), 'runOnBranchCreation', entity);
}
else if (entityName === 'notes') { else if (entityName === 'notes') {
runAttachedRelations(entity, 'runOnNoteCreation', entity); runAttachedRelations(entity, 'runOnNoteCreation', entity);
} }
@ -167,4 +171,12 @@ eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) =>
} }
} }
}); });
if (entityName === 'branches') {
runAttachedRelations(entity.getNote(), 'runOnBranchDeletion', entity);
}
}); });
module.exports = {
runAttachedRelations
};

View File

@ -10,6 +10,7 @@ class TaskContext {
this.taskId = taskId; this.taskId = taskId;
this.taskType = taskType; this.taskType = taskType;
this.data = data; this.data = data;
this.noteDeletionHandlerTriggered = false;
// progressCount is meant to represent just some progress - to indicate the task is not stuck // progressCount is meant to represent just some progress - to indicate the task is not stuck
this.progressCount = -1; // we're incrementing immediatelly this.progressCount = -1; // we're incrementing immediatelly