diff --git a/apps/client/src/services/clipboard.ts b/apps/client/src/services/clipboard.ts index feffee065..af898e296 100644 --- a/apps/client/src/services/clipboard.ts +++ b/apps/client/src/services/clipboard.ts @@ -4,6 +4,7 @@ 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"; let clipboardBranchIds: string[] = []; let clipboardMode: string | null = null; @@ -108,6 +109,21 @@ function isClipboardEmpty() { return clipboardBranchIds.length === 0; } +export function copyText(text: string) { + if (!text) { + return; + } + + try { + if (navigator.clipboard) { + navigator.clipboard.writeText(text); + toast.showMessage(t("code_block.copy_success")); + } + } catch (e) { + toast.showError(t("code_block.copy_failed")); + } +} + export default { pasteAfter, pasteInto, diff --git a/apps/client/src/services/syntax_highlight.ts b/apps/client/src/services/syntax_highlight.ts index 6780f8130..48969a906 100644 --- a/apps/client/src/services/syntax_highlight.ts +++ b/apps/client/src/services/syntax_highlight.ts @@ -1,8 +1,8 @@ import { ensureMimeTypes, highlight, highlightAuto, loadTheme, Themes, type AutoHighlightResult, type HighlightResult, type Theme } from "@triliumnext/highlightjs"; import mime_types from "./mime_types.js"; import options from "./options.js"; -import toast from "./toast.js"; import { t } from "./i18n.js"; +import { copyText } from "./clipboard.js"; let highlightingLoaded = false; @@ -37,16 +37,7 @@ export function applyCopyToClipboardButton($codeBlock: JQuery) { const $copyButton = $("