mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 23:49:53 +08:00
feat: 添加防抖,优化日志页的自动滚动体验 (#654)
This commit is contained in:
@@ -1,22 +1,24 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import api from '$lib/api';
|
import api from '$lib/api';
|
||||||
import { setBreadcrumb } from '$lib/stores/breadcrumb';
|
import { setBreadcrumb } from '$lib/stores/breadcrumb';
|
||||||
import { onMount } from 'svelte';
|
import { onMount, tick } from 'svelte';
|
||||||
import { Badge } from '$lib/components/ui/badge';
|
import { Badge } from '$lib/components/ui/badge';
|
||||||
|
|
||||||
let unsubscribeLog: (() => void) | null = null;
|
let unsubscribeLog: (() => void) | null = null;
|
||||||
let logs: Array<{ timestamp: string; level: string; message: string }> = [];
|
let logs: Array<{ timestamp: string; level: string; message: string }> = [];
|
||||||
let shouldAutoScroll = true;
|
let shouldAutoScroll = true;
|
||||||
let main: HTMLElement | null = null;
|
let main: HTMLElement | null = null;
|
||||||
|
let scrollTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
function checkScrollPosition() {
|
function checkScrollPosition() {
|
||||||
if (main) {
|
if (main) {
|
||||||
const { scrollTop, scrollHeight, clientHeight } = main;
|
const { scrollTop, scrollHeight, clientHeight } = main;
|
||||||
shouldAutoScroll = scrollTop + clientHeight >= scrollHeight - 5;
|
shouldAutoScroll = scrollTop + clientHeight >= scrollHeight - 50;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function scrollToBottom() {
|
async function scrollToBottom() {
|
||||||
|
await tick();
|
||||||
if (shouldAutoScroll && main) {
|
if (shouldAutoScroll && main) {
|
||||||
main.scrollTop = main.scrollHeight;
|
main.scrollTop = main.scrollHeight;
|
||||||
}
|
}
|
||||||
@@ -28,9 +30,11 @@
|
|||||||
main?.addEventListener('scroll', checkScrollPosition);
|
main?.addEventListener('scroll', checkScrollPosition);
|
||||||
unsubscribeLog = api.subscribeToLogs((data: string) => {
|
unsubscribeLog = api.subscribeToLogs((data: string) => {
|
||||||
logs = [...logs.slice(-499), JSON.parse(data)];
|
logs = [...logs.slice(-499), JSON.parse(data)];
|
||||||
setTimeout(scrollToBottom, 0);
|
if (scrollTimer) clearTimeout(scrollTimer);
|
||||||
|
scrollTimer = setTimeout(scrollToBottom, 20);
|
||||||
});
|
});
|
||||||
return () => {
|
return () => {
|
||||||
|
if (scrollTimer) clearTimeout(scrollTimer);
|
||||||
main?.removeEventListener('scroll', checkScrollPosition);
|
main?.removeEventListener('scroll', checkScrollPosition);
|
||||||
if (unsubscribeLog) {
|
if (unsubscribeLog) {
|
||||||
unsubscribeLog();
|
unsubscribeLog();
|
||||||
|
|||||||
Reference in New Issue
Block a user