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

85 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-05-04 02:27:38 +08:00
import treeUtils from "./tree_utils.js";
import treeChangesService from "./branches.js";
import cloningService from "./cloning.js";
2019-10-20 16:00:18 +08:00
import toastService from "./toast.js";
2019-05-04 02:27:38 +08:00
let clipboardIds = [];
let clipboardMode = null;
async function pasteAfter(node) {
if (clipboardMode === 'cut') {
const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey));
await treeChangesService.moveAfterNode(nodes, node);
clipboardIds = [];
clipboardMode = null;
}
else if (clipboardMode === 'copy') {
for (const noteId of clipboardIds) {
await cloningService.cloneNoteAfter(noteId, node.data.branchId);
}
// copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
}
else if (clipboardIds.length === 0) {
// just do nothing
}
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(node) {
if (clipboardMode === 'cut') {
const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey));
await treeChangesService.moveToNode(nodes, node);
await node.setExpanded(true);
clipboardIds = [];
clipboardMode = null;
}
else if (clipboardMode === 'copy') {
for (const noteId of clipboardIds) {
await cloningService.cloneNoteTo(noteId, node.data.noteId);
}
await node.setExpanded(true);
// copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
}
else if (clipboardIds.length === 0) {
// just do nothing
}
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) {
clipboardIds = nodes.map(node => node.data.noteId);
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) {
clipboardIds = nodes.map(node => node.key);
clipboardMode = 'cut';
2019-10-20 16:00:18 +08:00
toastService.showMessage("Note(s) have been cut into clipboard.");
2019-05-04 02:27:38 +08:00
}
function isEmpty() {
return clipboardIds.length === 0;
}
export default {
pasteAfter,
pasteInto,
cut,
copy,
isEmpty
}