Compare commits

...
+40 -2
View File
@@ -1,5 +1,8 @@
import { emitter } from 'shared/helpers/mitt';
/**
* Writes a text string to the system clipboard.
* @deprecated This will be removed in the future. Use `https://vueuse.org/core/useClipboard/` instead.
*
* @async
* @param {string} text text to be written to the clipboard
@@ -7,8 +10,43 @@
*/
export const copyTextToClipboard = async text => {
try {
await navigator.clipboard.writeText(text);
const permission = await navigator.permissions.query({
name: 'clipboard-write',
});
if (permission.state === 'granted' || permission.state === 'prompt') {
await navigator.clipboard.writeText(text);
} else {
throw new Error('Clipboard write permission denied');
}
} catch (error) {
throw new Error(`Unable to copy text to clipboard: ${error.message}`);
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.opacity = '0';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
const successful = document.execCommand('copy');
if (successful) {
emitter.emit(
'newToastMessage',
'Text successfully copied to clipboard.'
);
} else {
throw new Error('Fallback method failed');
}
} catch (execCommandError) {
emitter.emit(
'newToastMessage',
'Network Error: Unable to copy text to clipboard.'
);
throw new Error(
`Unable to copy text to clipboard: ${execCommandError.message}`
);
} finally {
document.body.removeChild(textArea);
}
}
};