Skip to content

Commit

Permalink
Implement scroll position saving with debouncing in dashboard, improv…
Browse files Browse the repository at this point in the history
…ing performance and user experience
  • Loading branch information
eduardolat committed Sep 5, 2024
1 parent 5093aa5 commit 2af091a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/view/static/js/dashboard-aside-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ export function initDashboardAsideScroll () {

if (!el) return

window.addEventListener('beforeunload', function () {
const scrollPosition = el.scrollTop
localStorage.setItem(key, scrollPosition)
})
const saveScrollPosition = window.debounce(
() => {
const scrollPosition = el.scrollTop
localStorage.setItem(key, scrollPosition)
console.log(scrollPosition)
},
200
)
el.addEventListener('scroll', saveScrollPosition)

document.addEventListener('DOMContentLoaded', function () {
const scrollPosition = localStorage.getItem(key)
Expand Down
11 changes: 11 additions & 0 deletions internal/view/static/js/init-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
export function initHelpers () {
function debounce (fn, delayMilliseconds) {
let timeoutInstance
return function (...args) {
clearTimeout(timeoutInstance)
timeoutInstance = setTimeout(() => {
fn.apply(this, args)
}, delayMilliseconds)
}
}

function copyToClipboard (textToCopy) {
const successMessage = 'Text copied'
const errorMessage = 'Error copying text'
Expand Down Expand Up @@ -58,6 +68,7 @@ export function initHelpers () {
}
}

window.debounce = debounce
window.copyToClipboard = copyToClipboard
window.textareaAutoGrow = textareaAutoGrow
window.formatJson = formatJson
Expand Down

0 comments on commit 2af091a

Please sign in to comment.