fix: preserve scroll position during fallback copy

Signed-off-by: suyua9 <1521777066@qq.com>
This commit is contained in:
suyua9 2026-04-03 17:48:21 +08:00
parent c40ea7f29d
commit b9952ec288

View file

@ -511,16 +511,24 @@ export const copyToClipboard = async (text, html = null, formatted = false) => {
} else {
let result = false;
if (!navigator.clipboard) {
const activeElement = document.activeElement instanceof HTMLElement ? document.activeElement : null;
const scrollX = window.scrollX;
const scrollY = window.scrollY;
const textArea = document.createElement('textarea');
textArea.value = text;
// Avoid scrolling to bottom
// Keep the fallback copy target off-screen without stealing the viewport.
textArea.style.top = '0';
textArea.style.left = '0';
textArea.style.position = 'fixed';
textArea.style.opacity = '0';
document.body.appendChild(textArea);
textArea.focus();
try {
textArea.focus({ preventScroll: true });
} catch {
textArea.focus();
}
textArea.select();
try {
@ -533,6 +541,14 @@ export const copyToClipboard = async (text, html = null, formatted = false) => {
}
document.body.removeChild(textArea);
window.scrollTo(scrollX, scrollY);
if (activeElement && document.contains(activeElement)) {
try {
activeElement.focus({ preventScroll: true });
} catch {
activeElement.focus();
}
}
return result;
}