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-11-05 05:41:06 +08:00
|
|
|
import hoistedNoteService from "./hoisted_note.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) {
|
2019-11-05 05:41:06 +08:00
|
|
|
clipboardIds = nodes
|
|
|
|
.filter(node => node.data.noteId !== hoistedNoteService.getHoistedNoteNoPromise())
|
|
|
|
.filter(node => node.getParent().data.noteType !== 'search')
|
|
|
|
.map(node => node.data.noteId);
|
2019-05-04 02:27:38 +08:00
|
|
|
|
2019-11-05 05:41:06 +08:00
|
|
|
if (clipboardIds.length > 0) {
|
|
|
|
clipboardMode = 'cut';
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|