|
|
@ -1,6 +1,9 @@ |
|
|
|
import { useClipboard } from '#imports' |
|
|
|
import { Modal } from 'ant-design-vue' |
|
|
|
|
|
|
|
import { useClipboard, useI18n } from '#imports' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useCopy = (showDialogIfFailed = false) => { |
|
|
|
|
|
|
|
const { t } = useI18n() |
|
|
|
|
|
|
|
|
|
|
|
export const useCopy = () => { |
|
|
|
|
|
|
|
/** fallback for copy if clipboard api is not supported */ |
|
|
|
/** fallback for copy if clipboard api is not supported */ |
|
|
|
const copyFallback = async (text: string, retryCount = 0): Promise<boolean> => { |
|
|
|
const copyFallback = async (text: string, retryCount = 0): Promise<boolean> => { |
|
|
|
try { |
|
|
|
try { |
|
|
@ -12,7 +15,15 @@ export const useCopy = () => { |
|
|
|
document.body.removeChild(textAreaEl) |
|
|
|
document.body.removeChild(textAreaEl) |
|
|
|
if (!result && retryCount < 3) { |
|
|
|
if (!result && retryCount < 3) { |
|
|
|
// retry if copy failed
|
|
|
|
// retry if copy failed
|
|
|
|
return new Promise((resolve) => setTimeout(() => resolve(copyFallback(text, retryCount + 1)), 100)) |
|
|
|
return new Promise((resolve, reject) => |
|
|
|
|
|
|
|
setTimeout( |
|
|
|
|
|
|
|
() => |
|
|
|
|
|
|
|
copyFallback(text, retryCount + 1) |
|
|
|
|
|
|
|
.then(resolve) |
|
|
|
|
|
|
|
.catch(reject), |
|
|
|
|
|
|
|
100, |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!result) { |
|
|
|
if (!result) { |
|
|
@ -20,8 +31,15 @@ export const useCopy = () => { |
|
|
|
} |
|
|
|
} |
|
|
|
return result |
|
|
|
return result |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
throw new Error('Clipboard copy failed, please copy it from console log') |
|
|
|
if (!showDialogIfFailed) throw new Error(t('msg.error.copyToClipboardError')) |
|
|
|
console.log(text) |
|
|
|
|
|
|
|
|
|
|
|
Modal.info({ |
|
|
|
|
|
|
|
title: 'Copy failed, please manually copy it from here', |
|
|
|
|
|
|
|
content: text, |
|
|
|
|
|
|
|
class: 'nc-copy-failed-modal', |
|
|
|
|
|
|
|
width: '550px', |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -32,13 +50,11 @@ export const useCopy = () => { |
|
|
|
if (isSupported.value) { |
|
|
|
if (isSupported.value) { |
|
|
|
await _copy(text) |
|
|
|
await _copy(text) |
|
|
|
return true |
|
|
|
return true |
|
|
|
} else { |
|
|
|
|
|
|
|
return copyFallback(text) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} catch {} |
|
|
|
|
|
|
|
|
|
|
|
return copyFallback(text) |
|
|
|
return copyFallback(text) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { copy } |
|
|
|
return { copy } |
|
|
|
} |
|
|
|
} |
|
|
|