29 lines
978 B
TypeScript
29 lines
978 B
TypeScript
|
|
// apps/viewer/src/support/clipboard.ts —— 剪贴板写入兜底(UI-02 V02 起
|
||
|
|
// AlbumHeader 相册分享与 V04 大图 Share 共用):优先 navigator.clipboard
|
||
|
|
// (安全上下文),退回隐藏 textarea + execCommand('copy')(旧 WebView/
|
||
|
|
// 非安全上下文);均失败返回 false,由调用方展示可选中文本回退(§5.4)。
|
||
|
|
export async function copyText(text: string): Promise<boolean> {
|
||
|
|
try {
|
||
|
|
if (navigator.clipboard?.writeText !== undefined) {
|
||
|
|
await navigator.clipboard.writeText(text);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
} catch {
|
||
|
|
// 走传统复制兜底
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
const area = document.createElement('textarea');
|
||
|
|
area.value = text;
|
||
|
|
area.setAttribute('readonly', '');
|
||
|
|
area.style.position = 'fixed';
|
||
|
|
area.style.opacity = '0';
|
||
|
|
document.body.appendChild(area);
|
||
|
|
area.select();
|
||
|
|
const copied = document.execCommand('copy');
|
||
|
|
area.remove();
|
||
|
|
return copied;
|
||
|
|
} catch {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|