mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 09:58:32 +02:00
added "move note" search action
This commit is contained in:
parent
478eca47f4
commit
91e3dd022a
@ -23,6 +23,7 @@ import Limit from "../search_options/limit.js";
|
|||||||
import DeleteNoteRevisionsSearchAction from "../search_actions/delete_note_revisions.js";
|
import DeleteNoteRevisionsSearchAction from "../search_actions/delete_note_revisions.js";
|
||||||
import Debug from "../search_options/debug.js";
|
import Debug from "../search_options/debug.js";
|
||||||
import appContext from "../../services/app_context.js";
|
import appContext from "../../services/app_context.js";
|
||||||
|
import MoveNoteSearchAction from "../search_actions/move_note.js";
|
||||||
|
|
||||||
const TPL = `
|
const TPL = `
|
||||||
<div class="search-definition-widget">
|
<div class="search-definition-widget">
|
||||||
@ -127,10 +128,14 @@ const TPL = `
|
|||||||
action
|
action
|
||||||
</button>
|
</button>
|
||||||
<div class="dropdown-menu">
|
<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">
|
<a class="dropdown-item" href="#" data-action-add="deleteNote">
|
||||||
Delete note</a>
|
Delete note</a>
|
||||||
<a class="dropdown-item" href="#" data-action-add="deleteNoteRevisions">
|
<a class="dropdown-item" href="#" data-action-add="deleteNoteRevisions">
|
||||||
Delete note revisions</a>
|
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">
|
<a class="dropdown-item" href="#" data-action-add="deleteLabel">
|
||||||
Delete label</a>
|
Delete label</a>
|
||||||
<a class="dropdown-item" href="#" data-action-add="deleteRelation">
|
<a class="dropdown-item" href="#" data-action-add="deleteRelation">
|
||||||
@ -193,6 +198,7 @@ const OPTION_CLASSES = [
|
|||||||
const ACTION_CLASSES = {};
|
const ACTION_CLASSES = {};
|
||||||
|
|
||||||
for (const clazz of [
|
for (const clazz of [
|
||||||
|
MoveNoteSearchAction,
|
||||||
DeleteNoteSearchAction,
|
DeleteNoteSearchAction,
|
||||||
DeleteNoteRevisionsSearchAction,
|
DeleteNoteRevisionsSearchAction,
|
||||||
DeleteLabelSearchAction,
|
DeleteLabelSearchAction,
|
||||||
|
58
src/public/app/widgets/search_actions/move_note.js
Normal file
58
src/public/app/widgets/search_actions/move_note.js
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,8 @@ const treeService = require('../../services/tree');
|
|||||||
const noteService = require('../../services/notes');
|
const noteService = require('../../services/notes');
|
||||||
const becca = require('../../becca/becca');
|
const becca = require('../../becca/becca');
|
||||||
const TaskContext = require('../../services/task_context');
|
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
|
* 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`];
|
return [400, `One or both branches ${branchId}, ${parentBranchId} have not been found`];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (branchToMove.parentNoteId === parentBranch.noteId) {
|
return branchService.moveBranchToBranch(branchToMove, parentBranch, branchId);
|
||||||
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 };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveBranchBeforeNote(req) {
|
function moveBranchBeforeNote(req) {
|
||||||
@ -101,6 +81,8 @@ function moveBranchBeforeNote(req) {
|
|||||||
// if sorting is not needed then still the ordering might have changed above manually
|
// if sorting is not needed then still the ordering might have changed above manually
|
||||||
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
|
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
|
||||||
|
|
||||||
|
log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} before note ${beforeBranch.noteId}, branch ${beforeBranchId}`);
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,6 +132,8 @@ function moveBranchAfterNote(req) {
|
|||||||
// if sorting is not needed then still the ordering might have changed above manually
|
// if sorting is not needed then still the ordering might have changed above manually
|
||||||
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
|
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
|
||||||
|
|
||||||
|
log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} after note ${afterNote.noteId}, branch ${afterBranchId}`);
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ const log = require('../../services/log');
|
|||||||
const scriptService = require('../../services/script');
|
const scriptService = require('../../services/script');
|
||||||
const searchService = require('../../services/search/services/search');
|
const searchService = require('../../services/search/services/search');
|
||||||
const noteRevisionService = require("../../services/note_revisions");
|
const noteRevisionService = require("../../services/note_revisions");
|
||||||
|
const branchService = require("../../services/branches");
|
||||||
|
const cloningService = require("../../services/cloning");
|
||||||
const {formatAttrForSearch} = require("../../services/attribute_formatter");
|
const {formatAttrForSearch} = require("../../services/attribute_formatter");
|
||||||
|
|
||||||
async function searchFromNoteInt(note) {
|
async function searchFromNoteInt(note) {
|
||||||
@ -92,6 +94,26 @@ const ACTION_HANDLERS = {
|
|||||||
setRelationTarget: (action, note) => {
|
setRelationTarget: (action, note) => {
|
||||||
note.setRelation(action.relationName, action.targetNoteId);
|
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) => {
|
executeScript: (action, note) => {
|
||||||
if (!action.script || !action.script.trim()) {
|
if (!action.script || !action.script.trim()) {
|
||||||
log.info("Ignoring executeScript since the script is empty.")
|
log.info("Ignoring executeScript since the script is empty.")
|
||||||
|
46
src/services/branches.js
Normal file
46
src/services/branches.js
Normal file
@ -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
|
||||||
|
};
|
@ -9,6 +9,7 @@ const TaskContext = require("./task_context");
|
|||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const becca = require("../becca/becca");
|
const becca = require("../becca/becca");
|
||||||
const beccaService = require("../becca/becca_service");
|
const beccaService = require("../becca/becca_service");
|
||||||
|
const log = require("./log");
|
||||||
|
|
||||||
function cloneNoteToNote(noteId, parentNoteId, prefix) {
|
function cloneNoteToNote(noteId, parentNoteId, prefix) {
|
||||||
if (parentNoteId === 'share') {
|
if (parentNoteId === 'share') {
|
||||||
@ -34,6 +35,8 @@ function cloneNoteToNote(noteId, parentNoteId, prefix) {
|
|||||||
isExpanded: 0
|
isExpanded: 0
|
||||||
}).save();
|
}).save();
|
||||||
|
|
||||||
|
log.info(`Cloned note ${noteId} to new parent note ${parentNoteId} with prefix ${prefix}`);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
branchId: branch.branchId,
|
branchId: branch.branchId,
|
||||||
@ -73,6 +76,8 @@ function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) {
|
|||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
isExpanded: 0
|
isExpanded: 0
|
||||||
}).save();
|
}).save();
|
||||||
|
|
||||||
|
log.info(`Ensured note ${noteId} is in parent note ${parentNoteId} with prefix ${prefix}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {
|
function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {
|
||||||
@ -86,6 +91,8 @@ function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {
|
|||||||
|
|
||||||
const deleteId = utils.randomString(10);
|
const deleteId = utils.randomString(10);
|
||||||
noteService.deleteBranch(branch, deleteId, new TaskContext());
|
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
|
isExpanded: 0
|
||||||
}).save();
|
}).save();
|
||||||
|
|
||||||
|
log.info(`Cloned note ${noteId} into parent note ${afterNote.parentNoteId} after note ${afterNote.noteId}, branch ${afterBranchId}`);
|
||||||
|
|
||||||
return { success: true, branchId: branch.branchId };
|
return { success: true, branchId: branch.branchId };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user