mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
allow cloning into notes, not just branches, fixes #2484
This commit is contained in:
parent
3128a7d62f
commit
d97e454463
@ -1114,7 +1114,7 @@ class Note extends AbstractEntity {
|
|||||||
|
|
||||||
const branch = this.becca.getNote(parentNoteId).getParentBranches()[0];
|
const branch = this.becca.getNote(parentNoteId).getParentBranches()[0];
|
||||||
|
|
||||||
return cloningService.cloneNoteToParent(this.noteId, branch.branchId);
|
return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);
|
||||||
}
|
}
|
||||||
|
|
||||||
decrypt() {
|
decrypt() {
|
||||||
|
@ -48,7 +48,7 @@ async function cloneNotesTo(notePath) {
|
|||||||
const targetBranchId = await froca.getBranchId(parentNoteId, noteId);
|
const targetBranchId = await froca.getBranchId(parentNoteId, noteId);
|
||||||
|
|
||||||
for (const cloneNoteId of clonedNoteIds) {
|
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 clonedNote = await froca.getNote(cloneNoteId);
|
||||||
const targetNote = await froca.getBranch(targetBranchId).getNote();
|
const targetNote = await froca.getBranch(targetBranchId).getNote();
|
||||||
|
@ -196,8 +196,18 @@ ws.subscribeToMessages(async message => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function cloneNoteTo(childNoteId, parentBranchId, prefix) {
|
async function cloneNoteToBranch(childNoteId, parentBranchId, prefix) {
|
||||||
const resp = await server.put(`notes/${childNoteId}/clone-to/${parentBranchId}`, {
|
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
|
prefix: prefix
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -222,5 +232,6 @@ export default {
|
|||||||
deleteNotes,
|
deleteNotes,
|
||||||
moveNodeUpInHierarchy,
|
moveNodeUpInHierarchy,
|
||||||
cloneNoteAfter,
|
cloneNoteAfter,
|
||||||
cloneNoteTo
|
cloneNoteToBranch,
|
||||||
|
cloneNoteToNote,
|
||||||
};
|
};
|
||||||
|
@ -51,7 +51,7 @@ async function pasteInto(parentBranchId) {
|
|||||||
for (const clipboardBranch of clipboardBranches) {
|
for (const clipboardBranch of clipboardBranches) {
|
||||||
const clipboardNote = await clipboardBranch.getNote();
|
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
|
// copy will keep clipboardBranchIds and clipboardMode so it's possible to paste into multiple places
|
||||||
|
@ -22,7 +22,7 @@ export default class SharedSwitchWidget extends SwitchWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switchOn() {
|
switchOn() {
|
||||||
branchService.cloneNoteTo(this.noteId, 'share');
|
branchService.cloneNoteToNote(this.noteId, 'share');
|
||||||
}
|
}
|
||||||
|
|
||||||
async switchOff() {
|
async switchOff() {
|
||||||
|
@ -2,11 +2,18 @@
|
|||||||
|
|
||||||
const cloningService = require('../../services/cloning');
|
const cloningService = require('../../services/cloning');
|
||||||
|
|
||||||
function cloneNoteToParent(req) {
|
function cloneNoteToBranch(req) {
|
||||||
const {noteId, parentBranchId} = req.params;
|
const {noteId, parentBranchId} = req.params;
|
||||||
const {prefix} = req.body;
|
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) {
|
function cloneNoteAfter(req) {
|
||||||
@ -16,6 +23,7 @@ function cloneNoteAfter(req) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
cloneNoteToParent,
|
cloneNoteToBranch,
|
||||||
|
cloneNoteToNote,
|
||||||
cloneNoteAfter
|
cloneNoteAfter
|
||||||
};
|
};
|
||||||
|
@ -229,7 +229,8 @@ function register(app) {
|
|||||||
|
|
||||||
apiRoute(GET, '/api/edited-notes/:date', noteRevisionsApiRoute.getEditedNotesOnDate);
|
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);
|
apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter);
|
||||||
|
|
||||||
route(GET, '/api/notes/:branchId/export/:type/:format/:version/:taskId', [auth.checkApiAuthOrElectron], exportRoute.exportBranch);
|
route(GET, '/api/notes/:branchId/export/:type/:format/:version/:taskId', [auth.checkApiAuthOrElectron], exportRoute.exportBranch);
|
||||||
|
@ -10,20 +10,18 @@ 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");
|
||||||
|
|
||||||
function cloneNoteToParent(noteId, parentBranchId, prefix) {
|
function cloneNoteToNote(noteId, parentNoteId, prefix) {
|
||||||
if (parentBranchId === 'share') {
|
if (parentNoteId === 'share') {
|
||||||
const specialNotesService = require('./special_notes');
|
const specialNotesService = require('./special_notes');
|
||||||
// share root note is created lazily
|
// share root note is created lazily
|
||||||
specialNotesService.getShareRoot();
|
specialNotesService.getShareRoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentBranch = becca.getBranch(parentBranchId);
|
if (isNoteDeleted(noteId) || isNoteDeleted(parentNoteId)) {
|
||||||
|
|
||||||
if (isNoteDeleted(noteId) || isNoteDeleted(parentBranch.noteId)) {
|
|
||||||
return { success: false, message: 'Note is deleted.' };
|
return { success: false, message: 'Note is deleted.' };
|
||||||
}
|
}
|
||||||
|
|
||||||
const validationResult = treeService.validateParentChild(parentBranch.noteId, noteId);
|
const validationResult = treeService.validateParentChild(parentNoteId, noteId);
|
||||||
|
|
||||||
if (!validationResult.success) {
|
if (!validationResult.success) {
|
||||||
return validationResult;
|
return validationResult;
|
||||||
@ -31,21 +29,33 @@ function cloneNoteToParent(noteId, parentBranchId, prefix) {
|
|||||||
|
|
||||||
const branch = new Branch({
|
const branch = new Branch({
|
||||||
noteId: noteId,
|
noteId: noteId,
|
||||||
parentNoteId: parentBranch.noteId,
|
parentNoteId: parentNoteId,
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
isExpanded: 0
|
isExpanded: 0
|
||||||
}).save();
|
}).save();
|
||||||
|
|
||||||
parentBranch.isExpanded = true; // the new target should be expanded so it immediately shows up to the user
|
|
||||||
parentBranch.save();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
branchId: branch.branchId,
|
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) {
|
function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) {
|
||||||
if (isNoteDeleted(noteId) || isNoteDeleted(parentNoteId)) {
|
if (isNoteDeleted(noteId) || isNoteDeleted(parentNoteId)) {
|
||||||
return { success: false, message: 'Note is deleted.' };
|
return { success: false, message: 'Note is deleted.' };
|
||||||
@ -121,7 +131,8 @@ function isNoteDeleted(noteId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
cloneNoteToParent,
|
cloneNoteToBranch,
|
||||||
|
cloneNoteToNote,
|
||||||
ensureNoteIsPresentInParent,
|
ensureNoteIsPresentInParent,
|
||||||
ensureNoteIsAbsentFromParent,
|
ensureNoteIsAbsentFromParent,
|
||||||
toggleNoteInParent,
|
toggleNoteInParent,
|
||||||
|
@ -61,7 +61,7 @@ async function start() {
|
|||||||
const parentNoteId = getRandomNoteId();
|
const parentNoteId = getRandomNoteId();
|
||||||
const prefix = Math.random() > 0.8 ? "prefix" : null;
|
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");
|
console.log(`Cloning ${i}:`, result.success ? "succeeded" : "FAILED");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user