fix moving/cloning notes broken in 0.42.4, closes #1066

This commit is contained in:
zadam 2020-05-31 22:33:02 +02:00
parent 4d22959e28
commit 8c88ce6f65
8 changed files with 34 additions and 29 deletions

View File

@ -39,13 +39,14 @@ export async function showDialog(noteIds) {
} }
async function cloneNotesTo(notePath) { async function cloneNotesTo(notePath) {
const targetNoteId = treeService.getNoteIdFromNotePath(notePath); const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
const targetBranchId = await treeCache.getBranchId(parentNoteId, noteId);
for (const cloneNoteId of clonedNoteIds) { for (const cloneNoteId of clonedNoteIds) {
await branchService.cloneNoteTo(cloneNoteId, targetNoteId, $clonePrefix.val()); await branchService.cloneNoteTo(cloneNoteId, targetBranchId, $clonePrefix.val());
const clonedNote = await treeCache.getNote(cloneNoteId); const clonedNote = await treeCache.getNote(cloneNoteId);
const targetNote = await treeCache.getNote(targetNoteId); const targetNote = await treeCache.getBranch(targetBranchId).getNote();
toastService.showMessage(`Note "${clonedNote.title}" has been cloned into ${targetNote.title}`); toastService.showMessage(`Note "${clonedNote.title}" has been cloned into ${targetNote.title}`);
} }

View File

@ -32,10 +32,11 @@ export async function showDialog(branchIds) {
noteAutocompleteService.showRecentNotes($noteAutoComplete); noteAutocompleteService.showRecentNotes($noteAutoComplete);
} }
async function moveNotesTo(parentNoteId) { async function moveNotesTo(parentBranchId) {
await branchService.moveToParentNote(movedBranchIds, parentNoteId); await branchService.moveToParentNote(movedBranchIds, parentBranchId);
const parentNote = await treeCache.getNote(parentNoteId); const parentBranch = treeCache.getBranch(parentBranchId);
const parentNote = await parentBranch.getNote();
toastService.showMessage(`Selected notes have been moved into ${parentNote.title}`); toastService.showMessage(`Selected notes have been moved into ${parentNote.title}`);
} }
@ -46,9 +47,8 @@ $form.on('submit', () => {
if (notePath) { if (notePath) {
$dialog.modal('hide'); $dialog.modal('hide');
const noteId = treeService.getNoteIdFromNotePath(notePath); const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
treeCache.getBranchId(parentNoteId, noteId).then(branchId => moveNotesTo(branchId));
moveNotesTo(noteId);
} }
else { else {
console.error("No path to move to."); console.error("No path to move to.");

View File

@ -198,8 +198,8 @@ ws.subscribeToMessages(async message => {
} }
}); });
async function cloneNoteTo(childNoteId, parentNoteId, prefix) { async function cloneNoteTo(childNoteId, parentBranchId, prefix) {
const resp = await server.put('notes/' + childNoteId + '/clone-to/' + parentNoteId, { const resp = await server.put(`notes/${childNoteId}/clone-to/${parentBranchId}`, {
prefix: prefix prefix: prefix
}); });

View File

@ -33,13 +33,13 @@ async function pasteAfter(afterBranchId) {
} }
} }
async function pasteInto(parentNoteId) { async function pasteInto(parentBranchId) {
if (isClipboardEmpty()) { if (isClipboardEmpty()) {
return; return;
} }
if (clipboardMode === 'cut') { if (clipboardMode === 'cut') {
await branchService.moveToParentNote(clipboardBranchIds, parentNoteId); await branchService.moveToParentNote(clipboardBranchIds, parentBranchId);
clipboardBranchIds = []; clipboardBranchIds = [];
clipboardMode = null; clipboardMode = null;
@ -50,7 +50,7 @@ async function pasteInto(parentNoteId) {
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, parentNoteId); await branchService.cloneNoteTo(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

View File

@ -746,6 +746,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
node.icon = this.getIcon(note, isFolder); node.icon = this.getIcon(note, isFolder);
node.extraClasses = this.getExtraClasses(note); node.extraClasses = this.getExtraClasses(note);
node.title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title; node.title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title;
node.setExpanded(branch.isExpanded, {noEvents:true});
node.renderTitle(); node.renderTitle();
} }
@ -1079,7 +1080,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
const toNode = node.getPrevSibling(); const toNode = node.getPrevSibling();
if (toNode !== null) { if (toNode !== null) {
branchService.moveToParentNote([node.data.branchId], toNode.data.noteId); branchService.moveToParentNote([node.data.branchId], toNode.data.branchId);
} }
} }
@ -1164,7 +1165,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
} }
pasteNotesFromClipboardCommand({node}) { pasteNotesFromClipboardCommand({node}) {
clipboard.pasteInto(node.data.noteId); clipboard.pasteInto(node.data.branchId);
} }
pasteNotesAfterFromClipboard({node}) { pasteNotesAfterFromClipboard({node}) {

View File

@ -3,10 +3,10 @@
const cloningService = require('../../services/cloning'); const cloningService = require('../../services/cloning');
async function cloneNoteToParent(req) { async function cloneNoteToParent(req) {
const {noteId, parentNoteId} = req.params; const {noteId, parentBranchId} = req.params;
const {prefix} = req.body; const {prefix} = req.body;
return await cloningService.cloneNoteToParent(noteId, parentNoteId, prefix); return await cloningService.cloneNoteToParent(noteId, parentBranchId, prefix);
} }
async function cloneNoteAfter(req) { async function cloneNoteAfter(req) {

View File

@ -152,7 +152,7 @@ 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/:parentNoteId', cloningApiRoute.cloneNoteToParent); apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentBranchId', cloningApiRoute.cloneNoteToParent);
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);

View File

@ -9,12 +9,14 @@ const Branch = require('../entities/branch');
const TaskContext = require("./task_context.js"); const TaskContext = require("./task_context.js");
const utils = require('./utils'); const utils = require('./utils');
async function cloneNoteToParent(noteId, parentNoteId, prefix) { async function cloneNoteToParent(noteId, parentBranchId, prefix) {
if (await isNoteDeleted(noteId) || await isNoteDeleted(parentNoteId)) { const parentBranch = await repository.getBranch(parentBranchId);
if (await isNoteDeleted(noteId) || await isNoteDeleted(parentBranch.noteId)) {
return { success: false, message: 'Note is deleted.' }; return { success: false, message: 'Note is deleted.' };
} }
const validationResult = await treeService.validateParentChild(parentNoteId, noteId); const validationResult = await treeService.validateParentChild(parentBranch.noteId, noteId);
if (!validationResult.success) { if (!validationResult.success) {
return validationResult; return validationResult;
@ -22,12 +24,13 @@ async function cloneNoteToParent(noteId, parentNoteId, prefix) {
const branch = await new Branch({ const branch = await new Branch({
noteId: noteId, noteId: noteId,
parentNoteId: parentNoteId, parentNoteId: parentBranch.noteId,
prefix: prefix, prefix: prefix,
isExpanded: 0 isExpanded: 0
}).save(); }).save();
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]); parentBranch.isExpanded = true; // the new target should be expanded so it immediately shows up to the user
await parentBranch.save();
return { success: true, branchId: branch.branchId }; return { success: true, branchId: branch.branchId };
} }