import treeUtils from "./tree_utils.js"; import treeChangesService from "./branches.js"; import cloningService from "./cloning.js"; import toastService from "./toast.js"; import hoistedNoteService from "./hoisted_note.js"; 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 { toastService.throwError("Unrecognized clipboard mode=" + clipboardMode); } } 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 { toastService.throwError("Unrecognized clipboard mode=" + mode); } } function copy(nodes) { clipboardIds = nodes.map(node => node.data.noteId); clipboardMode = 'copy'; toastService.showMessage("Note(s) have been copied into clipboard."); } function cut(nodes) { clipboardIds = nodes .filter(node => node.data.noteId !== hoistedNoteService.getHoistedNoteNoPromise()) .filter(node => node.getParent().data.noteType !== 'search') .map(node => node.data.noteId); if (clipboardIds.length > 0) { clipboardMode = 'cut'; toastService.showMessage("Note(s) have been cut into clipboard."); } } function isEmpty() { return clipboardIds.length === 0; } export default { pasteAfter, pasteInto, cut, copy, isEmpty }