trilium/src/public/javascripts/services/clipboard.js

96 lines
2.8 KiB
JavaScript
Raw Normal View History

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