From 2af091aca500c7b48060d86a9fb154daeb25a136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Jer=C3=A9z=20Gir=C3=B3n?= Date: Thu, 5 Sep 2024 00:54:31 -0600 Subject: [PATCH] Implement scroll position saving with debouncing in dashboard, improving performance and user experience --- internal/view/static/js/dashboard-aside-scroll.js | 13 +++++++++---- internal/view/static/js/init-helpers.js | 11 +++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/internal/view/static/js/dashboard-aside-scroll.js b/internal/view/static/js/dashboard-aside-scroll.js index ad1b5b0..cc962fa 100644 --- a/internal/view/static/js/dashboard-aside-scroll.js +++ b/internal/view/static/js/dashboard-aside-scroll.js @@ -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) diff --git a/internal/view/static/js/init-helpers.js b/internal/view/static/js/init-helpers.js index 44a3364..787b63c 100644 --- a/internal/view/static/js/init-helpers.js +++ b/internal/view/static/js/init-helpers.js @@ -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' @@ -58,6 +68,7 @@ export function initHelpers () { } } + window.debounce = debounce window.copyToClipboard = copyToClipboard window.textareaAutoGrow = textareaAutoGrow window.formatJson = formatJson