Notes/apps/client/src/services/clipboard.ts

119 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-02-17 19:42:52 +01:00
import branchService from "./branches.js";
2019-10-20 10:00:18 +02:00
import toastService from "./toast.js";
2021-04-16 23:01:56 +02:00
import froca from "./froca.js";
import linkService from "./link.js";
import utils from "./utils.js";
import { t } from "./i18n.js";
import toast from "./toast.js";
2019-05-03 20:27:38 +02:00
let clipboardBranchIds: string[] = [];
let clipboardMode: string | null = null;
2019-05-03 20:27:38 +02:00
async function pasteAfter(afterBranchId: string) {
2019-11-20 19:24:23 +01:00
if (isClipboardEmpty()) {
return;
}
2025-01-09 18:07:02 +02:00
if (clipboardMode === "cut") {
2020-02-17 19:42:52 +01:00
await branchService.moveAfterBranch(clipboardBranchIds, afterBranchId);
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
clipboardBranchIds = [];
2019-05-03 20:27:38 +02:00
clipboardMode = null;
2025-01-09 18:07:02 +02:00
} else if (clipboardMode === "copy") {
const clipboardBranches = clipboardBranchIds.map((branchId) => froca.getBranch(branchId));
2019-11-20 19:24:23 +01:00
2020-01-12 09:57:28 +01:00
for (const clipboardBranch of clipboardBranches) {
if (!clipboardBranch) {
continue;
}
2020-01-12 09:57:28 +01:00
const clipboardNote = await clipboardBranch.getNote();
if (!clipboardNote) {
continue;
}
2020-01-12 09:57:28 +01:00
2020-02-17 19:42:52 +01:00
await branchService.cloneNoteAfter(clipboardNote.noteId, afterBranchId);
2019-05-03 20:27:38 +02:00
}
// copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places
2025-01-09 18:07:02 +02:00
} else {
toastService.throwError(`Unrecognized clipboard mode=${clipboardMode}`);
2019-05-03 20:27:38 +02:00
}
}
async function pasteInto(parentBranchId: string) {
2019-11-20 19:24:23 +01:00
if (isClipboardEmpty()) {
return;
}
2025-01-09 18:07:02 +02:00
if (clipboardMode === "cut") {
await branchService.moveToParentNote(clipboardBranchIds, parentBranchId);
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
clipboardBranchIds = [];
2019-05-03 20:27:38 +02:00
clipboardMode = null;
2025-01-09 18:07:02 +02:00
} else if (clipboardMode === "copy") {
const clipboardBranches = clipboardBranchIds.map((branchId) => froca.getBranch(branchId));
2019-11-20 19:24:23 +01:00
2020-01-12 09:57:28 +01:00
for (const clipboardBranch of clipboardBranches) {
if (!clipboardBranch) {
continue;
}
2020-01-12 09:57:28 +01:00
const clipboardNote = await clipboardBranch.getNote();
if (!clipboardNote) {
continue;
}
2019-05-03 20:27:38 +02:00
await branchService.cloneNoteToBranch(clipboardNote.noteId, parentBranchId);
2020-01-12 09:57:28 +01:00
}
2019-05-03 20:27:38 +02:00
2022-08-08 23:13:31 +02:00
// copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places
2025-01-09 18:07:02 +02:00
} else {
toastService.throwError(`Unrecognized clipboard mode=${clipboardMode}`);
2019-05-03 20:27:38 +02:00
}
}
async function copy(branchIds: string[]) {
2020-02-17 22:38:46 +01:00
clipboardBranchIds = branchIds;
2025-01-09 18:07:02 +02:00
clipboardMode = "copy";
2019-05-03 20:27:38 +02:00
if (utils.isElectron()) {
// https://github.com/zadam/trilium/issues/2401
2025-01-09 18:07:02 +02:00
const { clipboard } = require("electron");
2025-05-28 20:42:21 +03:00
const links: string[] = [];
for (const branch of froca.getBranches(clipboardBranchIds)) {
2023-05-29 00:19:54 +02:00
const $link = await linkService.createLink(`${branch.parentNoteId}/${branch.noteId}`, { referenceLink: true });
links.push($link[0].outerHTML);
}
2025-01-09 18:07:02 +02:00
clipboard.writeHTML(links.join(", "));
}
2024-10-20 02:06:08 +03:00
toastService.showMessage(t("clipboard.copied"));
2019-05-03 20:27:38 +02:00
}
function cut(branchIds: string[]) {
2020-02-17 22:38:46 +01:00
clipboardBranchIds = branchIds;
2019-05-03 20:27:38 +02:00
2020-01-12 09:57:28 +01:00
if (clipboardBranchIds.length > 0) {
2025-01-09 18:07:02 +02:00
clipboardMode = "cut";
2024-10-20 02:06:08 +03:00
toastService.showMessage(t("clipboard.cut"));
}
2019-05-03 20:27:38 +02:00
}
2019-11-20 19:24:23 +01:00
function isClipboardEmpty() {
2025-01-09 18:07:02 +02:00
clipboardBranchIds = clipboardBranchIds.filter((branchId) => !!froca.getBranch(branchId));
2019-11-20 19:24:23 +01:00
2020-01-12 09:57:28 +01:00
return clipboardBranchIds.length === 0;
2019-05-03 20:27:38 +02:00
}
export default {
pasteAfter,
pasteInto,
cut,
copy,
2019-11-20 19:24:23 +01:00
isClipboardEmpty
2025-01-09 18:07:02 +02:00
};