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
allow cloning into notes, not just branches, fixes #2484
zadamd97e45446398
diff --git a/src/becca/entities/note.js b/src/becca/entities/note.jsindex be42556c18..bf4779bf0e 100644--- a/src/becca/entities/note.js+++ b/src/becca/entities/note.js@@ -1114,7 +1114,7 @@ class Note extends AbstractEntity {const branch = this.becca.getNote(parentNoteId).getParentBranches()[0];- return cloningService.cloneNoteToParent(this.noteId, branch.branchId);+ return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);}decrypt() {diff --git a/src/public/app/dialogs/clone_to.js b/src/public/app/dialogs/clone_to.jsindex e1ec951fa6..a992b1def8 100644--- a/src/public/app/dialogs/clone_to.js+++ b/src/public/app/dialogs/clone_to.js@@ -48,7 +48,7 @@ async function cloneNotesTo(notePath) {const targetBranchId = await froca.getBranchId(parentNoteId, noteId);for (const cloneNoteId of clonedNoteIds) {- await branchService.cloneNoteTo(cloneNoteId, targetBranchId, $clonePrefix.val());+ await branchService.cloneNoteToBranch(cloneNoteId, targetBranchId, $clonePrefix.val());const clonedNote = await froca.getNote(cloneNoteId);const targetNote = await froca.getBranch(targetBranchId).getNote();diff --git a/src/public/app/services/branches.js b/src/public/app/services/branches.jsindex 9a3a133fd3..7756c7a031 100644--- a/src/public/app/services/branches.js+++ b/src/public/app/services/branches.js@@ -196,8 +196,18 @@ ws.subscribeToMessages(async message => {}});-async function cloneNoteTo(childNoteId, parentBranchId, prefix) {- const resp = await server.put(`notes/${childNoteId}/clone-to/${parentBranchId}`, {+async function cloneNoteToBranch(childNoteId, parentBranchId, prefix) {+ const resp = await server.put(`notes/${childNoteId}/clone-to-branch/${parentBranchId}`, {+ prefix: prefix+ });++ if (!resp.success) {+ alert(resp.message);+ }+}++async function cloneNoteToNote(childNoteId, parentNoteId, prefix) {+ const resp = await server.put(`notes/${childNoteId}/clone-to-note/${parentNoteId}`, {prefix: prefix});@@ -222,5 +232,6 @@ export default {deleteNotes,moveNodeUpInHierarchy,cloneNoteAfter,- cloneNoteTo+ cloneNoteToBranch,+ cloneNoteToNote,};diff --git a/src/public/app/services/clipboard.js b/src/public/app/services/clipboard.jsindex 63d0d29cb2..235fe8381a 100644--- a/src/public/app/services/clipboard.js+++ b/src/public/app/services/clipboard.js@@ -51,7 +51,7 @@ async function pasteInto(parentBranchId) {for (const clipboardBranch of clipboardBranches) {const clipboardNote = await clipboardBranch.getNote();- await branchService.cloneNoteTo(clipboardNote.noteId, parentBranchId);+ await branchService.cloneNoteToBranch(clipboardNote.noteId, parentBranchId);}// copy will keep clipboardBranchIds and clipboardMode so it's possible to paste into multiple placesdiff --git a/src/public/app/widgets/shared_switch.js b/src/public/app/widgets/shared_switch.jsindex f9fbd000aa..46aee2cb3e 100644--- a/src/public/app/widgets/shared_switch.js+++ b/src/public/app/widgets/shared_switch.js@@ -22,7 +22,7 @@ export default class SharedSwitchWidget extends SwitchWidget {}switchOn() {- branchService.cloneNoteTo(this.noteId, 'share');+ branchService.cloneNoteToNote(this.noteId, 'share');}async switchOff() {diff --git a/src/routes/api/cloning.js b/src/routes/api/cloning.jsindex de8e1433de..f00343afdd 100644--- a/src/routes/api/cloning.js+++ b/src/routes/api/cloning.js@@ -2,11 +2,18 @@const cloningService = require('../../services/cloning');-function cloneNoteToParent(req) {+function cloneNoteToBranch(req) {const {noteId, parentBranchId} = req.params;const {prefix} = req.body;- return cloningService.cloneNoteToParent(noteId, parentBranchId, prefix);+ return cloningService.cloneNoteToBranch(noteId, parentBranchId, prefix);+}++function cloneNoteToNote(req) {+ const {noteId, parentNoteId} = req.params;+ const {prefix} = req.body;++ return cloningService.cloneNoteToNote(noteId, parentNoteId, prefix);}function cloneNoteAfter(req) {@@ -16,6 +23,7 @@ function cloneNoteAfter(req) {}module.exports = {- cloneNoteToParent,+ cloneNoteToBranch,+ cloneNoteToNote,cloneNoteAfter};diff --git a/src/routes/routes.js b/src/routes/routes.jsindex 923f9c078e..1b65257c47 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -229,7 +229,8 @@ function register(app) {apiRoute(GET, '/api/edited-notes/:date', noteRevisionsApiRoute.getEditedNotesOnDate);- apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentBranchId', cloningApiRoute.cloneNoteToParent);+ apiRoute(PUT, '/api/notes/:noteId/clone-to-branch/:parentBranchId', cloningApiRoute.cloneNoteToBranch);+ apiRoute(PUT, '/api/notes/:noteId/clone-to-note/:parentNoteId', cloningApiRoute.cloneNoteToNote);apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter);route(GET, '/api/notes/:branchId/export/:type/:format/:version/:taskId', [auth.checkApiAuthOrElectron], exportRoute.exportBranch);diff --git a/src/services/cloning.js b/src/services/cloning.jsindex 31aef0a527..0ffbd61ede 100644--- a/src/services/cloning.js+++ b/src/services/cloning.js@@ -10,20 +10,18 @@ const utils = require('./utils');const becca = require("../becca/becca");const beccaService = require("../becca/becca_service");-function cloneNoteToParent(noteId, parentBranchId, prefix) {- if (parentBranchId === 'share') {+function cloneNoteToNote(noteId, parentNoteId, prefix) {+ if (parentNoteId === 'share') {const specialNotesService = require('./special_notes');// share root note is created lazilyspecialNotesService.getShareRoot();}- const parentBranch = becca.getBranch(parentBranchId);-- if (isNoteDeleted(noteId) || isNoteDeleted(parentBranch.noteId)) {+ if (isNoteDeleted(noteId) || isNoteDeleted(parentNoteId)) {return { success: false, message: 'Note is deleted.' };}- const validationResult = treeService.validateParentChild(parentBranch.noteId, noteId);+ const validationResult = treeService.validateParentChild(parentNoteId, noteId);if (!validationResult.success) {return validationResult;@@ -31,21 +29,33 @@ function cloneNoteToParent(noteId, parentBranchId, prefix) {const branch = new Branch({noteId: noteId,- parentNoteId: parentBranch.noteId,+ parentNoteId: parentNoteId,prefix: prefix,isExpanded: 0}).save();- parentBranch.isExpanded = true; // the new target should be expanded so it immediately shows up to the user- parentBranch.save();-return {success: true,branchId: branch.branchId,- notePath: beccaService.getNotePath(parentBranch.noteId).path + "/" + noteId+ notePath: beccaService.getNotePath(parentNoteId).path + "/" + noteId};}+function cloneNoteToBranch(noteId, parentBranchId, prefix) {+ const parentBranch = becca.getBranch(parentBranchId);++ if (!parentBranch) {+ return { success: false, message: `Parent branch ${parentBranchId} does not exist.` };+ }++ const ret = cloneNoteToNote(noteId, parentBranch.noteId, prefix);++ parentBranch.isExpanded = true; // the new target should be expanded so it immediately shows up to the user+ parentBranch.save();++ return ret;+}+function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) {if (isNoteDeleted(noteId) || isNoteDeleted(parentNoteId)) {return { success: false, message: 'Note is deleted.' };@@ -121,7 +131,8 @@ function isNoteDeleted(noteId) {}module.exports = {- cloneNoteToParent,+ cloneNoteToBranch,+ cloneNoteToNote,ensureNoteIsPresentInParent,ensureNoteIsAbsentFromParent,toggleNoteInParent,diff --git a/src/tools/generate_document.js b/src/tools/generate_document.jsindex fb3869c83a..62af2f97a4 100644--- a/src/tools/generate_document.js+++ b/src/tools/generate_document.js@@ -61,7 +61,7 @@ async function start() {const parentNoteId = getRandomNoteId();const prefix = Math.random() > 0.8 ? "prefix" : null;- const result = await cloningService.cloneNoteToParent(noteIdToClone, parentNoteId, prefix);+ const result = await cloningService.cloneNoteToBranch(noteIdToClone, parentNoteId, prefix);console.log(`Cloning ${i}:`, result.success ? "succeeded" : "FAILED");}