diff --git a/locales/en.yml b/locales/en.yml index 0fcc3995..88e09acd 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -1222,7 +1222,7 @@ tools: label-loan-term: Loan Term label-interest-rate: Interest Rate label-displayed-currency: Displayed Currency - label-end-of-year: "End of Year {year}" + label-end-of-year: End of Year {year} label-year: Year label-month: Month label-interest: Interest @@ -1408,6 +1408,7 @@ tools: tag-: ''', ''' tag-copy-html-formatted: Copy HTML Formatted tag-copy-code-text: Copy Code Text + tag-copy-html-word: Copy to HTML/Rich text in Word coin-flipper: title: Coin Flipper description: Flip a coin diff --git a/src/composable/copy.ts b/src/composable/copy.ts index 265977e5..fe870729 100644 --- a/src/composable/copy.ts +++ b/src/composable/copy.ts @@ -59,3 +59,82 @@ export function useCopyClipboardItems({ source, text = 'Copied to the clipboard' }, }; } + +export function useCopyHtml({ sourceHtml, fallbackText, toastText = 'Copied to the clipboard', createToast = true }: { sourceHtml?: MaybeRefOrGetter; fallbackText?: MaybeRefOrGetter; toastText?: string; createToast?: boolean } = {}) { + const message = useMessage(); + + const copied = ref(false); + const error = ref(''); + + function legacyCopy(text: string) { + const textarea = document.createElement('textarea'); + textarea.value = text; + + textarea.style.position = 'fixed'; + textarea.style.opacity = '0'; + textarea.style.pointerEvents = 'none'; + + document.body.appendChild(textarea); + textarea.focus(); + textarea.select(); + + try { + document.execCommand('copy'); + } + finally { + document.body.removeChild(textarea); + } + } + + async function copyHtml(html: string, fallbackText?: string) { + copied.value = false; + error.value = ''; + + try { + const ClipboardItemCtor = (window as any).ClipboardItem; + + if (navigator.clipboard && ClipboardItemCtor) { + // Modern HTML clipboard support + const blobHtml = new Blob([html], { type: 'text/html' }); + const blobText = new Blob([fallbackText || html], { type: 'text/plain' }); + const item = new ClipboardItemCtor({ 'text/plain': blobText, 'text/html': blobHtml }); + await navigator.clipboard.write([item]); + } + else if (navigator.clipboard?.writeText) { + // Async text fallback + await navigator.clipboard.writeText(fallbackText || html); + } + else { + // Legacy execCommand fallback + legacyCopy(fallbackText || html); + } + + copied.value = true; + } + catch (e: any) { + error.value = e.toString(); + } + } + + return { + isJustCopied: copied, + async copy(html?: string, text?: string, { notificationMessage }: { notificationMessage?: string } = {}) { + if (sourceHtml) { + await copyHtml(toValue(sourceHtml), toValue(fallbackText)); + } + else if (html) { + await copyHtml(html, text); + } + + if (createToast) { + if (error.value) { + message.error(error.value); + } + else { + message.success(notificationMessage ?? toastText); + } + } + }, + error, + }; +} diff --git a/src/tools/code-highlighter/code-highlighter.vue b/src/tools/code-highlighter/code-highlighter.vue index 21e447ea..412ebb69 100644 --- a/src/tools/code-highlighter/code-highlighter.vue +++ b/src/tools/code-highlighter/code-highlighter.vue @@ -1,10 +1,9 @@ @@ -118,7 +113,7 @@ const { copy: copyText } = useCopy({ source: code }); - {{ t('tools.code-highlighter.texts.tag-copy-html-formatted') }} + {{ t('tools.code-highlighter.texts.tag-copy-html-word') }} {{ t('tools.code-highlighter.texts.tag-copy-code-text') }}