Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
introduced bulk action groups
zadamf9bee7cd4e14
diff --git a/src/public/app/dialogs/bulk_assign_attributes.js b/src/public/app/dialogs/bulk_assign_attributes.jsindex 21aab712ff..d6a31a4dd5 100644--- a/src/public/app/dialogs/bulk_assign_attributes.js+++ b/src/public/app/dialogs/bulk_assign_attributes.js@@ -14,12 +14,21 @@ $dialog.on('click', '[data-action-add]', async event => {await refresh();});-for (const action of bulkActionService.ACTION_CLASSES) {- $availableActionList.append(- $('<button class="btn btn-sm">')- .attr('data-action-add', action.actionName)- .text(action.actionTitle)- );+for (const actionGroup of bulkActionService.ACTION_GROUPS) {+ const $actionGroupList = $("<td>");+ const $actionGroup = $("<tr>")+ .append($("<td>").text(actionGroup.title + ": "))+ .append($actionGroupList);++ for (const action of actionGroup.actions) {+ $actionGroupList.append(+ $('<button class="btn btn-sm">')+ .attr('data-action-add', action.actionName)+ .text(action.actionTitle)+ );+ }++ $availableActionList.append($actionGroup);}async function refresh() {diff --git a/src/public/app/services/bulk_action.js b/src/public/app/services/bulk_action.jsindex b2de0c1306..48ca92ef2b 100644--- a/src/public/app/services/bulk_action.js+++ b/src/public/app/services/bulk_action.js@@ -13,6 +13,25 @@ import ExecuteScriptBulkAction from "../widgets/bulk_actions/execute_script.js";import AddLabelBulkAction from "../widgets/bulk_actions/label/add_label.js";import AddRelationBulkAction from "../widgets/bulk_actions/relation/add_relation.js";+const ACTION_GROUPS = [+ {+ title: 'Labels',+ actions: [AddLabelBulkAction, UpdateLabelValueBulkAction, RenameLabelBulkAction, DeleteLabelBulkAction]+ },+ {+ title: 'Relations',+ actions: [AddRelationBulkAction, UpdateRelationTargetBulkAction, RenameRelationBulkAction, DeleteRelationBulkAction]+ },+ {+ title: 'Notes',+ actions: [DeleteNoteBulkAction, DeleteNoteRevisionsBulkAction, MoveNoteBulkAction],+ },+ {+ title: 'Other',+ actions: [ExecuteScriptBulkAction]+ }+];+const ACTION_CLASSES = [MoveNoteBulkAction,DeleteNoteBulkAction,@@ -68,5 +87,6 @@ function parseActions(note) {export default {addAction,parseActions,- ACTION_CLASSES+ ACTION_CLASSES,+ ACTION_GROUPS};diff --git a/src/public/app/widgets/bulk_actions/label/add_label.js b/src/public/app/widgets/bulk_actions/label/add_label.jsindex e4267c88db..85a026d3e2 100644--- a/src/public/app/widgets/bulk_actions/label/add_label.js+++ b/src/public/app/widgets/bulk_actions/label/add_label.js@@ -5,7 +5,7 @@ const TPL = `<tr><td colspan="2"><div style="display: flex; align-items: center">- <div style="margin-right: 10px;" class="text-nowrap">Set label</div>+ <div style="margin-right: 10px;" class="text-nowrap">Add label</div><input type="text"class="form-control label-name"diff --git a/src/public/app/widgets/bulk_actions/relation/add_relation.js b/src/public/app/widgets/bulk_actions/relation/add_relation.jsindex 40ba0ab085..846aa870eb 100644--- a/src/public/app/widgets/bulk_actions/relation/add_relation.js+++ b/src/public/app/widgets/bulk_actions/relation/add_relation.js@@ -6,8 +6,8 @@ const TPL = `<tr><td colspan="2"><div style="display: flex; align-items: center">- <div style="margin-right: 10px;" class="text-nowrap">Set relation</div>-+ <div style="margin-right: 10px;" class="text-nowrap">Add relation</div>+<input type="text"class="form-control relation-name"placeholder="relation name"diff --git a/src/public/app/widgets/ribbon_widgets/search_definition.js b/src/public/app/widgets/ribbon_widgets/search_definition.jsindex c90d2e6678..014af8602d 100644--- a/src/public/app/widgets/ribbon_widgets/search_definition.js+++ b/src/public/app/widgets/ribbon_widgets/search_definition.js@@ -64,6 +64,10 @@ const TPL = `.add-search-option button {margin-top: 5px; /* to give some spacing when buttons overflow on the next line */}++ .dropdown-header {+ background-color: var(--accented-background-color);+ }</style><div class="search-settings">@@ -183,12 +187,16 @@ export default class SearchDefinitionWidget extends NoteContextAwareWidget {this.$component = this.$widget.find('.search-definition-widget');this.$actionList = this.$widget.find('.action-list');- for (const action of bulkActionService.ACTION_CLASSES) {- this.$actionList.append(- $('<a class="dropdown-item" href="#">')- .attr('data-action-add', action.actionName)- .text(action.actionTitle)- );+ for (const actionGroup of bulkActionService.ACTION_GROUPS) {+ this.$actionList.append($('<h6 class="dropdown-header">').append(actionGroup.title));++ for (const action of actionGroup.actions) {+ this.$actionList.append(+ $('<a class="dropdown-item" href="#">')+ .attr('data-action-add', action.actionName)+ .text(action.actionTitle)+ );+ }}this.$widget.on('click', '[data-search-option-add]', async event => {diff --git a/src/routes/api/search.js b/src/routes/api/search.jsindex a929d357e5..0c020e7468 100644--- a/src/routes/api/search.js+++ b/src/routes/api/search.js@@ -5,9 +5,7 @@ const SearchContext = require('../../services/search/search_context');const log = require('../../services/log');const scriptService = require('../../services/script');const searchService = require('../../services/search/services/search');-const noteRevisionService = require("../../services/note_revisions");-const branchService = require("../../services/branches");-const cloningService = require("../../services/cloning");+const bulkActionService = require("../../services/bulk_actions");const {formatAttrForSearch} = require("../../services/attribute_formatter");async function searchFromNoteInt(note) {@@ -59,108 +57,6 @@ async function searchFromNote(req) {return await searchFromNoteInt(note);}-const ACTION_HANDLERS = {- deleteNote: (action, note) => {- note.markAsDeleted();- },- deleteNoteRevisions: (action, note) => {- noteRevisionService.eraseNoteRevisions(note.getNoteRevisions().map(rev => rev.noteRevisionId));- },- deleteLabel: (action, note) => {- for (const label of note.getOwnedLabels(action.labelName)) {- label.markAsDeleted();- }- },- deleteRelation: (action, note) => {- for (const relation of note.getOwnedRelations(action.relationName)) {- relation.markAsDeleted();- }- },- renameLabel: (action, note) => {- for (const label of note.getOwnedLabels(action.oldLabelName)) {- // attribute name is immutable, renaming means delete old + create new- const newLabel = label.createClone('label', action.newLabelName, label.value);-- newLabel.save();- label.markAsDeleted();- }- },- renameRelation: (action, note) => {- for (const relation of note.getOwnedRelations(action.oldRelationName)) {- // attribute name is immutable, renaming means delete old + create new- const newRelation = relation.createClone('relation', action.newRelationName, relation.value);-- newRelation.save();- relation.markAsDeleted();- }- },- updateLabelValue: (action, note) => {- for (const label of note.getOwnedLabels(action.labelName)) {- label.value = action.labelValue;- label.save();- }- },- updateRelationTarget: (action, note) => {- for (const relation of note.getOwnedLabels(action.relationName)) {- relation.value = action.targetNoteId;- relation.save();- }- },- moveNote: (action, note) => {- const targetParentNote = becca.getNote(action.targetParentNoteId);-- if (!targetParentNote) {- return;- }-- let res;-- if (note.getParentBranches().length > 1) {- res = cloningService.cloneNoteToNote(note.noteId, action.targetParentNoteId);- }- else {- res = branchService.moveBranchToNote(note.getParentBranches()[0], action.targetParentNoteId);- }-- if (!res.success) {- log.info(`Moving/cloning note ${note.noteId} to ${action.targetParentNoteId} failed with error ${JSON.stringify(res)}`);- }- },- executeScript: (action, note) => {- if (!action.script || !action.script.trim()) {- log.info("Ignoring executeScript since the script is empty.")- return;- }-- const scriptFunc = new Function("note", action.script);- scriptFunc(note);-- note.save();- }-};--function getActions(note) {- return note.getLabels('action')- .map(actionLabel => {- let action;-- try {- action = JSON.parse(actionLabel.value);- } catch (e) {- log.error(`Cannot parse '${actionLabel.value}' into search action, skipping.`);- return null;- }-- if (!(action.name in ACTION_HANDLERS)) {- log.error(`Cannot find '${action.name}' search action handler, skipping.`);- return null;- }-- return action;- })- .filter(a => !!a);-}-async function searchAndExecute(req) {const note = becca.getNote(req.params.noteId);@@ -179,26 +75,7 @@ async function searchAndExecute(req) {const searchResultNoteIds = await searchFromNoteInt(note);- const actions = getActions(note);-- for (const resultNoteId of searchResultNoteIds) {- const resultNote = becca.getNote(resultNoteId);-- if (!resultNote || resultNote.isDeleted) {- continue;- }-- for (const action of actions) {- try {- log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`);-- ACTION_HANDLERS[action.name](action, resultNote);- }- catch (e) {- log.error(`ExecuteScript search action failed with ${e.message}`);- }- }- }+ bulkActionService.executeActions(note, searchResultNoteIds);}function searchFromRelation(note, relationName) {diff --git a/src/services/bulk_actions.js b/src/services/bulk_actions.jsnew file mode 100644index 0000000000..85d24a24bd--- /dev/null+++ b/src/services/bulk_actions.js@@ -0,0 +1,139 @@+const log = require("./log.js");+const noteRevisionService = require("./note_revisions.js");+const becca = require("../becca/becca.js");+const cloningService = require("./cloning.js");+const branchService = require("./branches.js");++const ACTION_HANDLERS = {+ addLabel: (action, note) => {+ note.addLabel(action.labelName, action.labelValue);+ },+ addRelation: (action, note) => {+ note.addRelation(action.relationName, action.targetNoteId);+ },+ deleteNote: (action, note) => {+ note.markAsDeleted();+ },+ deleteNoteRevisions: (action, note) => {+ noteRevisionService.eraseNoteRevisions(note.getNoteRevisions().map(rev => rev.noteRevisionId));+ },+ deleteLabel: (action, note) => {+ for (const label of note.getOwnedLabels(action.labelName)) {+ label.markAsDeleted();+ }+ },+ deleteRelation: (action, note) => {+ for (const relation of note.getOwnedRelations(action.relationName)) {+ relation.markAsDeleted();+ }+ },+ renameLabel: (action, note) => {+ for (const label of note.getOwnedLabels(action.oldLabelName)) {+ // attribute name is immutable, renaming means delete old + create new+ const newLabel = label.createClone('label', action.newLabelName, label.value);++ newLabel.save();+ label.markAsDeleted();+ }+ },+ renameRelation: (action, note) => {+ for (const relation of note.getOwnedRelations(action.oldRelationName)) {+ // attribute name is immutable, renaming means delete old + create new+ const newRelation = relation.createClone('relation', action.newRelationName, relation.value);++ newRelation.save();+ relation.markAsDeleted();+ }+ },+ updateLabelValue: (action, note) => {+ for (const label of note.getOwnedLabels(action.labelName)) {+ label.value = action.labelValue;+ label.save();+ }+ },+ updateRelationTarget: (action, note) => {+ for (const relation of note.getOwnedLabels(action.relationName)) {+ relation.value = action.targetNoteId;+ relation.save();+ }+ },+ moveNote: (action, note) => {+ const targetParentNote = becca.getNote(action.targetParentNoteId);++ if (!targetParentNote) {+ return;+ }++ let res;++ if (note.getParentBranches().length > 1) {+ res = cloningService.cloneNoteToNote(note.noteId, action.targetParentNoteId);+ }+ else {+ res = branchService.moveBranchToNote(note.getParentBranches()[0], action.targetParentNoteId);+ }++ if (!res.success) {+ log.info(`Moving/cloning note ${note.noteId} to ${action.targetParentNoteId} failed with error ${JSON.stringify(res)}`);+ }+ },+ executeScript: (action, note) => {+ if (!action.script || !action.script.trim()) {+ log.info("Ignoring executeScript since the script is empty.")+ return;+ }++ const scriptFunc = new Function("note", action.script);+ scriptFunc(note);++ note.save();+ }+};++function getActions(note) {+ return note.getLabels('action')+ .map(actionLabel => {+ let action;++ try {+ action = JSON.parse(actionLabel.value);+ } catch (e) {+ log.error(`Cannot parse '${actionLabel.value}' into search action, skipping.`);+ return null;+ }++ if (!(action.name in ACTION_HANDLERS)) {+ log.error(`Cannot find '${action.name}' search action handler, skipping.`);+ return null;+ }++ return action;+ })+ .filter(a => !!a);+}++function executeActions(note, searchResultNoteIds) {+ const actions = getActions(note);++ for (const resultNoteId of searchResultNoteIds) {+ const resultNote = becca.getNote(resultNoteId);++ if (!resultNote || resultNote.isDeleted) {+ continue;+ }++ for (const action of actions) {+ try {+ log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`);++ ACTION_HANDLERS[action.name](action, resultNote);+ } catch (e) {+ log.error(`ExecuteScript search action failed with ${e.message}`);+ }+ }+ }+}++module.exports = {+ executeActions+};diff --git a/src/views/dialogs/bulk_assign_attributes.ejs b/src/views/dialogs/bulk_assign_attributes.ejsindex 556e3cd5a7..13e74fcb93 100644--- a/src/views/dialogs/bulk_assign_attributes.ejs+++ b/src/views/dialogs/bulk_assign_attributes.ejs@@ -16,27 +16,25 @@<span aria-hidden="true">×</span></button></div>- <form id="clone-to-form">- <div class="modal-body">- Affected notes: <span id="affected-note-count">0</span>+ <div class="modal-body">+ Affected notes: <span id="affected-note-count">0</span>- <div class="form-check">- <input class="form-check-input" type="checkbox" value="" id="include-descendants">- <label class="form-check-label" for="include-descendants">- Include descendant notes- </label>- </div>+ <div class="form-check">+ <input class="form-check-input" type="checkbox" value="" id="include-descendants">+ <label class="form-check-label" for="include-descendants">+ Include descendant notes+ </label>+ </div>- Available actions:+ Available actions:- <div id="bulk-available-action-list"></div>+ <table id="bulk-available-action-list"></table>- <div id="bulk-existing-action-list"></div>- </div>- <div class="modal-footer">- <button type="submit" class="btn btn-primary">Execute bulk actions</button>- </div>- </form>+ <div id="bulk-existing-action-list"></div>+ </div>+ <div class="modal-footer">+ <button type="submit" class="btn btn-primary">Execute bulk actions</button>+ </div></div></div></div>