Skip to content

Commit 2af091a

Browse files
committed
Implement scroll position saving with debouncing in dashboard, improving performance and user experience
1 parent 5093aa5 commit 2af091a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

internal/view/static/js/dashboard-aside-scroll.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ export function initDashboardAsideScroll () {
44

55
if (!el) return
66

7-
window.addEventListener('beforeunload', function () {
8-
const scrollPosition = el.scrollTop
9-
localStorage.setItem(key, scrollPosition)
10-
})
7+
const saveScrollPosition = window.debounce(
8+
() => {
9+
const scrollPosition = el.scrollTop
10+
localStorage.setItem(key, scrollPosition)
11+
console.log(scrollPosition)
12+
},
13+
200
14+
)
15+
el.addEventListener('scroll', saveScrollPosition)
1116

1217
document.addEventListener('DOMContentLoaded', function () {
1318
const scrollPosition = localStorage.getItem(key)

internal/view/static/js/init-helpers.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
export function initHelpers () {
2+
function debounce (fn, delayMilliseconds) {
3+
let timeoutInstance
4+
return function (...args) {
5+
clearTimeout(timeoutInstance)
6+
timeoutInstance = setTimeout(() => {
7+
fn.apply(this, args)
8+
}, delayMilliseconds)
9+
}
10+
}
11+
212
function copyToClipboard (textToCopy) {
313
const successMessage = 'Text copied'
414
const errorMessage = 'Error copying text'
@@ -58,6 +68,7 @@ export function initHelpers () {
5868
}
5969
}
6070

71+
window.debounce = debounce
6172
window.copyToClipboard = copyToClipboard
6273
window.textareaAutoGrow = textareaAutoGrow
6374
window.formatJson = formatJson

0 commit comments

Comments
 (0)