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
added "move note" search action
zadam91e3dd022a84
diff --git a/src/public/app/widgets/ribbon_widgets/search_definition.js b/src/public/app/widgets/ribbon_widgets/search_definition.jsindex 5ffdeb9188..c6872877db 100644--- a/src/public/app/widgets/ribbon_widgets/search_definition.js+++ b/src/public/app/widgets/ribbon_widgets/search_definition.js@@ -23,6 +23,7 @@ import Limit from "../search_options/limit.js";import DeleteNoteRevisionsSearchAction from "../search_actions/delete_note_revisions.js";import Debug from "../search_options/debug.js";import appContext from "../../services/app_context.js";+import MoveNoteSearchAction from "../search_actions/move_note.js";const TPL = `<div class="search-definition-widget">@@ -127,10 +128,14 @@ const TPL = `action</button><div class="dropdown-menu">+ <a class="dropdown-item" href="#" data-action-add="moveNote">+ Move note</a><a class="dropdown-item" href="#" data-action-add="deleteNote">Delete note</a><a class="dropdown-item" href="#" data-action-add="deleteNoteRevisions">Delete note revisions</a>+ <a class="dropdown-item" href="#" data-action-add="moveNote">+ Delete note revisions</a><a class="dropdown-item" href="#" data-action-add="deleteLabel">Delete label</a><a class="dropdown-item" href="#" data-action-add="deleteRelation">@@ -193,6 +198,7 @@ const OPTION_CLASSES = [const ACTION_CLASSES = {};for (const clazz of [+ MoveNoteSearchAction,DeleteNoteSearchAction,DeleteNoteRevisionsSearchAction,DeleteLabelSearchAction,diff --git a/src/public/app/widgets/search_actions/move_note.js b/src/public/app/widgets/search_actions/move_note.jsnew file mode 100644index 0000000000..3d7fcf1396--- /dev/null+++ b/src/public/app/widgets/search_actions/move_note.js@@ -0,0 +1,58 @@+import SpacedUpdate from "../../services/spaced_update.js";+import AbstractSearchAction from "./abstract_search_action.js";+import noteAutocompleteService from "../../services/note_autocomplete.js";++const TPL = `+<tr>+ <td colspan="2">+ <div style="display: flex; align-items: center">+ <div style="margin-right: 10px;" class="text-nowrap">Move note</div>++ <div style="margin-right: 10px;" class="text-nowrap">to</div>++ <div class="input-group">+ <input type="text" class="form-control target-parent-note" placeholder="target parent note"/>+ </div>+ </div>+ </td>+ <td class="button-column">+ <div class="dropdown help-dropdown">+ <span class="bx bx-help-circle icon-action" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>+ <div class="dropdown-menu dropdown-menu-right p-4">+ <p>On all matched notes:</p>++ <ul>+ <li>move note to the new parent if note has only one parent (i.e. the old placement is removed and new placement into the new parent is created)</li>+ <li>clone note to the new parent if note has multiple clones/placements (it's not clear which placement should be removed)</li>+ <li>nothing will happen if note cannot be moved to the target note (i.e. this would create a tree cycle)</li>+ </ul>+ </div>+ </div>++ <span class="bx bx-x icon-action action-conf-del"></span>+ </td>+</tr>`;++export default class MoveNoteSearchAction extends AbstractSearchAction {+ static get actionName() { return "moveNote"; }++ doRender() {+ const $action = $(TPL);++ const $targetParentNote = $action.find('.target-parent-note');+ noteAutocompleteService.initNoteAutocomplete($targetParentNote);+ $targetParentNote.setNote(this.actionDef.targetParentNoteId);++ $targetParentNote.on('autocomplete:closed', () => spacedUpdate.scheduleUpdate());++ const spacedUpdate = new SpacedUpdate(async () => {+ await this.saveAction({+ targetParentNoteId: $targetParentNote.getSelectedNoteId()+ });+ }, 1000)++ $targetParentNote.on('input', () => spacedUpdate.scheduleUpdate());++ return $action;+ }+}diff --git a/src/routes/api/branches.js b/src/routes/api/branches.jsindex 0a33d2680d..f05a6e0c3e 100644--- a/src/routes/api/branches.js+++ b/src/routes/api/branches.js@@ -7,6 +7,8 @@ const treeService = require('../../services/tree');const noteService = require('../../services/notes');const becca = require('../../becca/becca');const TaskContext = require('../../services/task_context');+const branchService = require("../../services/branches");+const log = require("../../services/log.js");/*** Code in this file deals with moving and cloning branches. Relationship between note and parent note is unique@@ -23,29 +25,7 @@ function moveBranchToParent(req) {return [400, `One or both branches ${branchId}, ${parentBranchId} have not been found`];}- if (branchToMove.parentNoteId === parentBranch.noteId) {- return { success: true }; // no-op- }-- const validationResult = treeService.validateParentChild(parentBranch.noteId, branchToMove.noteId, branchId);-- if (!validationResult.success) {- return [200, validationResult];- }-- const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentBranch.noteId]);- const newNotePos = maxNotePos === null ? 0 : maxNotePos + 10;-- // expanding so that the new placement of the branch is immediately visible- parentBranch.isExpanded = true;- parentBranch.save();-- const newBranch = branchToMove.createClone(parentBranch.noteId, newNotePos);- newBranch.save();-- branchToMove.markAsDeleted();-- return { success: true };+ return branchService.moveBranchToBranch(branchToMove, parentBranch, branchId);}function moveBranchBeforeNote(req) {@@ -101,6 +81,8 @@ function moveBranchBeforeNote(req) {// if sorting is not needed then still the ordering might have changed above manuallyentityChangesService.addNoteReorderingEntityChange(parentNote.noteId);+ log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} before note ${beforeBranch.noteId}, branch ${beforeBranchId}`);+return { success: true };}@@ -150,6 +132,8 @@ function moveBranchAfterNote(req) {// if sorting is not needed then still the ordering might have changed above manuallyentityChangesService.addNoteReorderingEntityChange(parentNote.noteId);+ log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} after note ${afterNote.noteId}, branch ${afterBranchId}`);+return { success: true };}diff --git a/src/routes/api/search.js b/src/routes/api/search.jsindex aaafe27870..ca64f24fed 100644--- a/src/routes/api/search.js+++ b/src/routes/api/search.js@@ -6,6 +6,8 @@ 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 {formatAttrForSearch} = require("../../services/attribute_formatter");async function searchFromNoteInt(note) {@@ -92,6 +94,26 @@ const ACTION_HANDLERS = {setRelationTarget: (action, note) => {note.setRelation(action.relationName, action.targetNoteId);},+ 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.")diff --git a/src/services/branches.js b/src/services/branches.jsnew file mode 100644index 0000000000..c18ee8b32f--- /dev/null+++ b/src/services/branches.js@@ -0,0 +1,46 @@+const treeService = require("./tree.js");+const sql = require("./sql.js");++function moveBranchToNote(sourceBranch, targetParentNoteId) {+ if (sourceBranch.parentNoteId === targetParentNoteId) {+ return {success: true}; // no-op+ }++ const validationResult = treeService.validateParentChild(targetParentNoteId, sourceBranch.noteId, sourceBranch.branchId);++ if (!validationResult.success) {+ return [200, validationResult];+ }++ const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [targetParentNoteId]);+ const newNotePos = maxNotePos === null ? 0 : maxNotePos + 10;++ const newBranch = sourceBranch.createClone(targetParentNoteId, newNotePos);+ newBranch.save();++ sourceBranch.markAsDeleted();++ return {+ success: true,+ branch: newBranch+ };+}++function moveBranchToBranch(sourceBranch, targetParentBranch) {+ const res = moveBranchToNote(sourceBranch, targetParentBranch.noteId);++ if (!res.success) {+ return res;+ }++ // expanding so that the new placement of the branch is immediately visible+ targetParentBranch.isExpanded = true;+ targetParentBranch.save();++ return res;+}++module.exports = {+ moveBranchToBranch,+ moveBranchToNote+};diff --git a/src/services/cloning.js b/src/services/cloning.jsindex 9fce5234c5..6fbd3adf16 100644--- a/src/services/cloning.js+++ b/src/services/cloning.js@@ -9,6 +9,7 @@ const TaskContext = require("./task_context");const utils = require('./utils');const becca = require("../becca/becca");const beccaService = require("../becca/becca_service");+const log = require("./log");function cloneNoteToNote(noteId, parentNoteId, prefix) {if (parentNoteId === 'share') {@@ -34,6 +35,8 @@ function cloneNoteToNote(noteId, parentNoteId, prefix) {isExpanded: 0}).save();+ log.info(`Cloned note ${noteId} to new parent note ${parentNoteId} with prefix ${prefix}`);+return {success: true,branchId: branch.branchId,@@ -73,6 +76,8 @@ function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) {prefix: prefix,isExpanded: 0}).save();++ log.info(`Ensured note ${noteId} is in parent note ${parentNoteId} with prefix ${prefix}`);}function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {@@ -86,6 +91,8 @@ function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {const deleteId = utils.randomString(10);noteService.deleteBranch(branch, deleteId, new TaskContext());++ log.info(`Ensured note ${noteId} is NOT in parent note ${parentNoteId}`);}}@@ -125,6 +132,8 @@ function cloneNoteAfter(noteId, afterBranchId) {isExpanded: 0}).save();+ log.info(`Cloned note ${noteId} into parent note ${afterNote.parentNoteId} after note ${afterNote.noteId}, branch ${afterBranchId}`);+return { success: true, branchId: branch.branchId };}