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

93 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-02-18 02:42:52 +08:00
import branchService from "./branches.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-02-18 02:42:52 +08:00
await branchService.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();
2020-02-18 02:42:52 +08:00
await branchService.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
}
}
async function pasteInto(parentBranchId) {
2019-11-21 02:24:23 +08:00
if (isClipboardEmpty()) {
return;
}
2019-05-04 02:27:38 +08:00
if (clipboardMode === 'cut') {
await branchService.moveToParentNote(clipboardBranchIds, parentBranchId);
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
await branchService.cloneNoteTo(clipboardNote.noteId, parentBranchId);
2020-01-12 16:57:28 +08:00
}
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
}
}
2020-02-18 05:38:46 +08:00
function copy(branchIds) {
clipboardBranchIds = branchIds;
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
}
2020-02-18 05:38:46 +08:00
function cut(branchIds) {
clipboardBranchIds = branchIds;
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
}