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

106 lines
3 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";
import hoistedNoteService from "./hoisted_note.js";
2019-05-04 02:27:38 +08:00
2019-11-21 02:24:23 +08:00
/*
* Clipboard contains node keys which are not stable. If a (part of the) tree is reloaded,
* node keys in the clipboard might not exist anymore. Code here should then be ready to deal
* with this.
*/
let clipboardNodeKeys = [];
2019-05-04 02:27:38 +08:00
let clipboardMode = null;
2019-11-21 02:24:23 +08:00
async function pasteAfter(afterNode) {
if (isClipboardEmpty()) {
return;
}
2019-05-04 02:27:38 +08:00
if (clipboardMode === 'cut') {
2019-11-21 02:24:23 +08:00
const nodes = clipboardNodeKeys.map(nodeKey => treeUtils.getNodeByKey(nodeKey));
2019-05-04 02:27:38 +08:00
2019-11-21 02:24:23 +08:00
await treeChangesService.moveAfterNode(nodes, afterNode);
2019-05-04 02:27:38 +08:00
2019-11-21 02:24:23 +08:00
clipboardNodeKeys = [];
2019-05-04 02:27:38 +08:00
clipboardMode = null;
}
else if (clipboardMode === 'copy') {
2019-11-21 02:24:23 +08:00
for (const nodeKey of clipboardNodeKeys) {
const clipNode = treeUtils.getNodeByKey(nodeKey);
await cloningService.cloneNoteAfter(clipNode.data.noteId, afterNode.data.branchId);
2019-05-04 02:27:38 +08:00
}
// copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
}
else {
2019-10-20 16:00:18 +08:00
toastService.throwError("Unrecognized clipboard mode=" + clipboardMode);
2019-05-04 02:27:38 +08:00
}
}
2019-11-21 02:24:23 +08:00
async function pasteInto(parentNode) {
if (isClipboardEmpty()) {
return;
}
2019-05-04 02:27:38 +08:00
if (clipboardMode === 'cut') {
2019-11-21 02:24:23 +08:00
const nodes = clipboardNodeKeys.map(nodeKey => treeUtils.getNodeByKey(nodeKey));
2019-05-04 02:27:38 +08:00
2019-11-21 02:24:23 +08:00
await treeChangesService.moveToNode(nodes, parentNode);
2019-05-04 02:27:38 +08:00
2019-11-21 02:24:23 +08:00
await parentNode.setExpanded(true);
2019-05-04 02:27:38 +08:00
2019-11-21 02:24:23 +08:00
clipboardNodeKeys = [];
2019-05-04 02:27:38 +08:00
clipboardMode = null;
}
else if (clipboardMode === 'copy') {
2019-11-21 02:24:23 +08:00
for (const nodeKey of clipboardNodeKeys) {
const clipNode = treeUtils.getNodeByKey(nodeKey);
await cloningService.cloneNoteTo(clipNode.data.noteId, parentNode.data.noteId);
2019-05-04 02:27:38 +08:00
}
2019-11-21 02:24:23 +08:00
await parentNode.setExpanded(true);
2019-05-04 02:27:38 +08:00
// copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
}
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) {
2019-11-21 02:24:23 +08:00
clipboardNodeKeys = nodes.map(node => node.key);
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) {
2019-11-21 02:24:23 +08:00
clipboardNodeKeys = 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
2019-11-21 02:24:23 +08:00
if (clipboardNodeKeys.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() {
clipboardNodeKeys = clipboardNodeKeys.filter(key => !!treeUtils.getNodeByKey(key));
return clipboardNodeKeys.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
}