mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 09:58:32 +02:00
added new runOnNoteDeletion, runOnBranchCreation, runOnBranchDeletion, #2898
This commit is contained in:
parent
b5214e6cea
commit
f587e0dfd9
@ -5,9 +5,9 @@ const AbstractEntity = require("./abstract_entity");
|
||||
const sql = require("../../services/sql");
|
||||
const dateUtils = require("../../services/date_utils");
|
||||
const utils = require("../../services/utils.js");
|
||||
const TaskContext = require("../../services/task_context.js");
|
||||
const cls = require("../../services/cls.js");
|
||||
const log = require("../../services/log.js");
|
||||
const TaskContext = require("../../services/task_context");
|
||||
const cls = require("../../services/cls");
|
||||
const log = require("../../services/log");
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
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'
|
||||
|| this.noteId === 'root'
|
||||
|| this.noteId === cls.getHoistedNoteId()) {
|
||||
@ -146,7 +158,6 @@ class Branch extends AbstractEntity {
|
||||
|
||||
this.markAsDeleted(deleteId);
|
||||
|
||||
const note = this.getNote();
|
||||
const notDeletedBranches = note.getParentBranches();
|
||||
|
||||
if (notDeletedBranches.length === 0) {
|
||||
|
@ -8,9 +8,8 @@ const dateUtils = require('../../services/date_utils');
|
||||
const entityChangesService = require('../../services/entity_changes');
|
||||
const AbstractEntity = require("./abstract_entity");
|
||||
const NoteRevision = require("./note_revision");
|
||||
const TaskContext = require("../../services/task_context.js");
|
||||
const optionService = require("../../services/options.js");
|
||||
const noteRevisionService = require("../../services/note_revisions.js");
|
||||
const TaskContext = require("../../services/task_context");
|
||||
const handlers = require("../../services/handlers");
|
||||
|
||||
const LABEL = 'label';
|
||||
const RELATION = 'relation';
|
||||
@ -1143,6 +1142,10 @@ class Note extends AbstractEntity {
|
||||
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()) {
|
||||
branch.deleteBranch(deleteId, taskContext);
|
||||
}
|
||||
|
@ -233,6 +233,9 @@ const ATTR_HELP = {
|
||||
"runOnNoteCreation": "executes when note is created on backend",
|
||||
"runOnNoteTitleChange": "executes when note title 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",
|
||||
"runOnAttributeCreation": "executes when new attribute is created under this note",
|
||||
"runOnAttributeChange": "executes when attribute is changed under this note",
|
||||
|
@ -60,6 +60,9 @@ module.exports = [
|
||||
{ type: 'relation', name: 'runOnNoteCreation', isDangerous: true },
|
||||
{ type: 'relation', name: 'runOnNoteTitleChange', 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: 'runOnAttributeCreation', isDangerous: true },
|
||||
{ type: 'relation', name: 'runOnAttributeChange', isDangerous: true },
|
||||
|
@ -49,6 +49,7 @@ eventService.subscribe([ eventService.ENTITY_CHANGED, eventService.ENTITY_DELETE
|
||||
}
|
||||
}
|
||||
else if (entityName === 'notes') {
|
||||
// ENTITY_DELETED won't trigger anything since all branches/attributes are already deleted at this point
|
||||
runAttachedRelations(entity, 'runOnNoteChange', entity);
|
||||
}
|
||||
});
|
||||
@ -94,6 +95,9 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
|
||||
handleSortedAttribute(entity);
|
||||
}
|
||||
}
|
||||
else if (entityName === 'branches') {
|
||||
runAttachedRelations(entity.getNote(), 'runOnBranchCreation', entity);
|
||||
}
|
||||
else if (entityName === 'notes') {
|
||||
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
|
||||
};
|
||||
|
@ -10,6 +10,7 @@ class TaskContext {
|
||||
this.taskId = taskId;
|
||||
this.taskType = taskType;
|
||||
this.data = data;
|
||||
this.noteDeletionHandlerTriggered = false;
|
||||
|
||||
// progressCount is meant to represent just some progress - to indicate the task is not stuck
|
||||
this.progressCount = -1; // we're incrementing immediatelly
|
||||
|
Loading…
x
Reference in New Issue
Block a user