mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
fix(Code Highlighter): copy HTML for Word (text/html) and for code editors (text/plain)
This commit is contained in:
1 parent
4fd5d89c3a
commit
0715b2862a
3 files changed
+84
-9
No files matched your search
+2
-1
@@ -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
|
||||
|
||||
@@ -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<string>; fallbackText?: MaybeRefOrGetter<string>; 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,
|
||||
};
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { computed, ref } from 'vue';
|
||||
import { bundledLanguagesInfo, createHighlighter } from 'shiki/bundle/full';
|
||||
import { bundledThemesInfo } from 'shiki/themes';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
import { useCopy, useCopyClipboardItems } from '@/composable/copy';
|
||||
import { useCopy, useCopyHtml } from '@/composable/copy';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
@@ -75,11 +74,7 @@ const formattedCodeHtml = computedAsync(async () => {
|
||||
],
|
||||
});
|
||||
});
|
||||
const htmlClipboardItems = computed(() => [{
|
||||
mime: 'text/html',
|
||||
content: formattedCodeHtml.value,
|
||||
}]);
|
||||
const { copy: copyHtml } = useCopyClipboardItems({ source: htmlClipboardItems });
|
||||
const { copy: copyHtml } = useCopyHtml({ sourceHtml: formattedCodeHtml });
|
||||
const { copy: copyText } = useCopy({ source: code });
|
||||
</script>
|
||||
|
||||
@@ -118,7 +113,7 @@ const { copy: copyText } = useCopy({ source: code });
|
||||
<n-switch v-model:value="showLineNumbers" />
|
||||
</n-form-item>
|
||||
<c-button @click="copyHtml()">
|
||||
{{ t('tools.code-highlighter.texts.tag-copy-html-formatted') }}
|
||||
{{ t('tools.code-highlighter.texts.tag-copy-html-word') }}
|
||||
</c-button>
|
||||
<c-button @click="copyText()">
|
||||
{{ t('tools.code-highlighter.texts.tag-copy-code-text') }}
|
||||
|
||||
Reference in new issue
Block a user