From d97e45446398d335e555b85a0b6b6c0125943996 Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 27 Dec 2021 23:39:46 +0100 Subject: [PATCH] allow cloning into notes, not just branches, fixes #2484 --- src/becca/entities/note.js | 2 +- src/public/app/dialogs/clone_to.js | 2 +- src/public/app/services/branches.js | 17 +++++++++--- src/public/app/services/clipboard.js | 2 +- src/public/app/widgets/shared_switch.js | 2 +- src/routes/api/cloning.js | 14 +++++++--- src/routes/routes.js | 3 ++- src/services/cloning.js | 35 ++++++++++++++++--------- src/tools/generate_document.js | 2 +- 9 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/becca/entities/note.js b/src/becca/entities/note.js index be42556c1..bf4779bf0 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.js index e1ec951fa..a992b1def 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.js index 9a3a133fd..7756c7a03 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.js index 63d0d29cb..235fe8381 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 places diff --git a/src/public/app/widgets/shared_switch.js b/src/public/app/widgets/shared_switch.js index f9fbd000a..46aee2cb3 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.js index de8e1433d..f00343afd 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.js index 923f9c078..1b65257c4 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.js index 31aef0a52..0ffbd61ed 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 lazily specialNotesService.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.js index fb3869c83..62af2f97a 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"); }