mirror of
https://github.com/open-webui/open-webui.git
synced 2026-09-10 22:43:29 +00:00
fix: preserve scroll position during fallback copy
Signed-off-by: suyua9 <1521777066@qq.com>
This commit is contained in:
parent
c40ea7f29d
commit
b9952ec288
1 changed files with 18 additions and 2 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue