From 50aad138864551bf46c3a89593b4dc3c480f7f2d Mon Sep 17 00:00:00 2001
From: bytedream <bytedream@protonmail.com>
Date: Sat, 28 Dec 2024 02:20:37 +0100
Subject: [PATCH] fix scroll position resetting on admin panel

---
 .../PaginationTableBody.svelte                | 20 +++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/lib/components/PaginationTable/PaginationTableBody.svelte b/src/lib/components/PaginationTable/PaginationTableBody.svelte
index e6498d5..c24e199 100644
--- a/src/lib/components/PaginationTable/PaginationTableBody.svelte
+++ b/src/lib/components/PaginationTable/PaginationTableBody.svelte
@@ -6,6 +6,20 @@
 	let bodyElem: HTMLTableSectionElement;
 	let intersectionElem: HTMLElement;
 
+	async function onUpdateProxy() {
+		let scrollElem: HTMLElement | null = bodyElem;
+		while (scrollElem && scrollElem.scrollHeight <= scrollElem.clientHeight) {
+			scrollElem = scrollElem.parentElement;
+		}
+
+		const scrollTop = scrollElem?.scrollTop ?? 0;
+
+		await onUpdate();
+		await tick();
+
+		if (scrollElem) scrollElem.scrollTop = scrollTop;
+	}
+
 	async function getIntersectionElement(): Promise<HTMLElement> {
 		if (!bodyElem.lastElementChild) {
 			await new Promise<void>((resolve) => {
@@ -21,8 +35,7 @@
 	}
 
 	onMount(async () => {
-		await onUpdate();
-		await tick();
+		await onUpdateProxy();
 
 		if (!bodyElem) return;
 
@@ -33,8 +46,7 @@
 
 				const rows = bodyElem.rows.length;
 
-				await onUpdate();
-				await tick();
+				await onUpdateProxy();
 
 				if (rows === bodyElem.rows.length) return;
 				observer.observe((intersectionElem = await getIntersectionElement()));