From a666e26194e41a927b5e23a58d8c999b7954f1db Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 26 May 2025 20:02:32 +0300 Subject: [PATCH] feat(client): display message when copying code block in text note --- apps/client/src/services/clipboard.ts | 16 +++++++++++++++ apps/client/src/services/syntax_highlight.ts | 13 ++---------- .../widgets/type_widgets/ckeditor/config.ts | 7 ++++++- packages/ckeditor5/src/augmentation.ts | 13 ++++++++++++ .../src/plugins/copy_to_clipboard_button.ts | 20 ++++++++++++++----- 5 files changed, 52 insertions(+), 17 deletions(-) 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 = $("