96 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-05-03 20:27:38 +02:00
import treeChangesService from "./branches.js";
import cloningService from "./cloning.js";
2019-10-20 10:00:18 +02:00
import toastService from "./toast.js";
import hoistedNoteService from "./hoisted_note.js";
2020-01-12 09:57:28 +01:00
import treeCache from "./tree_cache.js";
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
let clipboardBranchIds = [];
2019-05-03 20:27:38 +02:00
let clipboardMode = null;
2020-01-12 09:57:28 +01:00
async function pasteAfter(afterBranchId) {
2019-11-20 19:24:23 +01:00
if (isClipboardEmpty()) {
return;
}
2019-05-03 20:27:38 +02:00
if (clipboardMode === 'cut') {
2020-01-12 09:57:28 +01:00
await treeChangesService.moveAfterNode(clipboardBranchIds, afterBranchId);
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
clipboardBranchIds = [];
2019-05-03 20:27:38 +02:00
clipboardMode = null;
}
else if (clipboardMode === 'copy') {
2020-01-12 09:57:28 +01:00
const clipboardBranches = clipboardBranchIds.map(branchId => treeCache.getBranch(branchId));
2019-11-20 19:24:23 +01:00
2020-01-12 09:57:28 +01:00
for (const clipboardBranch of clipboardBranches) {
const clipboardNote = await clipboardBranch.getNote();
await cloningService.cloneNoteAfter(clipboardNote.noteId, afterBranchId);
2019-05-03 20:27:38 +02:00
}
2020-01-12 09:57:28 +01:00
// copy will keep clipboardBranchIds and clipboardMode so it's possible to paste into multiple places
2019-05-03 20:27:38 +02:00
}
else {
2019-10-20 10:00:18 +02:00
toastService.throwError("Unrecognized clipboard mode=" + clipboardMode);
2019-05-03 20:27:38 +02:00
}
}
2020-01-12 09:57:28 +01:00
async function pasteInto(parentNoteId) {
2019-11-20 19:24:23 +01:00
if (isClipboardEmpty()) {
return;
}
2019-05-03 20:27:38 +02:00
if (clipboardMode === 'cut') {
2020-01-12 09:57:28 +01:00
await treeChangesService.moveToNode(clipboardBranchIds, parentNoteId);
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
clipboardBranchIds = [];
2019-05-03 20:27:38 +02:00
clipboardMode = null;
}
else if (clipboardMode === 'copy') {
2020-01-12 09:57:28 +01:00
const clipboardBranches = clipboardBranchIds.map(branchId => treeCache.getBranch(branchId));
2019-11-20 19:24:23 +01:00
2020-01-12 09:57:28 +01:00
for (const clipboardBranch of clipboardBranches) {
const clipboardNote = await clipboardBranch.getNote();
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
await cloningService.cloneNoteTo(clipboardNote.noteId, parentNoteId);
}
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
// copy will keep clipboardBranchIds and clipboardMode so it's possible to paste into multiple places
2019-05-03 20:27:38 +02:00
}
else {
2019-10-20 10:00:18 +02:00
toastService.throwError("Unrecognized clipboard mode=" + mode);
2019-05-03 20:27:38 +02:00
}
}
function copy(nodes) {
2020-01-12 09:57:28 +01:00
clipboardBranchIds = nodes.map(node => node.data.branchId);
2019-05-03 20:27:38 +02:00
clipboardMode = 'copy';
2019-10-20 10:00:18 +02:00
toastService.showMessage("Note(s) have been copied into clipboard.");
2019-05-03 20:27:38 +02:00
}
function cut(nodes) {
2020-01-12 09:57:28 +01:00
clipboardBranchIds = nodes
.filter(node => node.data.noteId !== hoistedNoteService.getHoistedNoteNoPromise())
.filter(node => node.getParent().data.noteType !== 'search')
2019-11-20 19:24:23 +01:00
.map(node => node.key);
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
if (clipboardBranchIds.length > 0) {
clipboardMode = 'cut';
toastService.showMessage("Note(s) have been cut into clipboard.");
}
2019-05-03 20:27:38 +02:00
}
2019-11-20 19:24:23 +01:00
function isClipboardEmpty() {
2020-01-12 09:57:28 +01:00
clipboardBranchIds = clipboardBranchIds.filter(branchId => !!treeCache.getBranch(branchId));
2019-11-20 19:24:23 +01:00
2020-01-12 09:57:28 +01:00
return clipboardBranchIds.length === 0;
2019-05-03 20:27:38 +02:00
}
export default {
pasteAfter,
pasteInto,
cut,
copy,
2019-11-20 19:24:23 +01:00
isClipboardEmpty
2019-05-03 20:27:38 +02:00
}