|
|
@ -2,14 +2,27 @@ import { useClipboard } from '#imports' |
|
|
|
|
|
|
|
|
|
|
|
export const useCopy = () => { |
|
|
|
export const useCopy = () => { |
|
|
|
/** fallback for copy if clipboard api is not supported */ |
|
|
|
/** fallback for copy if clipboard api is not supported */ |
|
|
|
const copyFallback = (text: string) => { |
|
|
|
const copyFallback = async (text: string, retryCount = 0): Promise<boolean> => { |
|
|
|
const textAreaEl = document.createElement('textarea') |
|
|
|
try { |
|
|
|
textAreaEl.innerHTML = text |
|
|
|
const textAreaEl = document.createElement('textarea') |
|
|
|
document.body.appendChild(textAreaEl) |
|
|
|
textAreaEl.innerHTML = text |
|
|
|
textAreaEl.select() |
|
|
|
document.body.appendChild(textAreaEl) |
|
|
|
const result = document.execCommand('copy') |
|
|
|
textAreaEl.select() |
|
|
|
document.body.removeChild(textAreaEl) |
|
|
|
const result = document.execCommand('copy') |
|
|
|
return result |
|
|
|
document.body.removeChild(textAreaEl) |
|
|
|
|
|
|
|
if (!result && retryCount < 3) { |
|
|
|
|
|
|
|
// retry if copy failed
|
|
|
|
|
|
|
|
return new Promise((resolve) => setTimeout(() => resolve(copyFallback(text, retryCount + 1)), 100)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!result) { |
|
|
|
|
|
|
|
throw new Error('failed') |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
throw new Error('Clipboard copy failed, please copy it from console log') |
|
|
|
|
|
|
|
console.log(text) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const { copy: _copy, isSupported } = useClipboard() |
|
|
|
const { copy: _copy, isSupported } = useClipboard() |
|
|
@ -18,11 +31,12 @@ export const useCopy = () => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (isSupported.value) { |
|
|
|
if (isSupported.value) { |
|
|
|
await _copy(text) |
|
|
|
await _copy(text) |
|
|
|
|
|
|
|
return true |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
copyFallback(text) |
|
|
|
return copyFallback(text) |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
copyFallback(text) |
|
|
|
return copyFallback(text) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|