From 6e80cf77b59c5638c121de49bea04c9bb20f2645 Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Sun, 12 May 2024 11:55:23 +1000 Subject: [PATCH] fix(scroller): handle repeated 'today' button clicks --- src/extensions/scroller/client.mjs | 33 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/extensions/scroller/client.mjs b/src/extensions/scroller/client.mjs index f9e8c91..96f4be9 100644 --- a/src/extensions/scroller/client.mjs +++ b/src/extensions/scroller/client.mjs @@ -19,7 +19,7 @@ export default async (api, db) => { scrollBehavior = (await db.get("smoothScrolling")) ? "smooth" : "auto", scroller = ".notion-frame .notion-scroller"; - let $scroller, $todayButton; + let $scroller, $today; const scrollTo = (top, behavior) => $scroller?.scroll({ top, behavior }), $scrollToBottom = html`<${FloatingButton} onclick="${() => scrollTo($scroller.scrollHeight, scrollBehavior)}" @@ -50,27 +50,32 @@ export default async (api, db) => { } else removeFloatingButton($scrollToTop); }, onTodayLoaded = () => { - const $bubble = document.querySelector(todayBubble); - if (!$bubble) return; // calendar will jump anyway when pinning the current month, // so ignore smooth scroll setting and jump direct to week + const $bubble = document.querySelector(todayBubble); + if (!$bubble) return; scrollTo($bubble.offsetParent.offsetParent.offsetTop + 57, "auto"); removeMutationListener(onTodayLoaded); }, onTodayClicked = () => { - removeMutationListener(onTodayLoaded); - addMutationListener(todayBubble, onTodayLoaded); + // if calendar does not refresh: jump direct to current week + // if calendar does refresh: wait, then jump to current week + requestIdleCallback(() => { + onTodayLoaded(); + addMutationListener(todayBubble, onTodayLoaded); + }); }, setup = () => { - if (document.contains($scroller)) return; - $scroller = document.querySelector(scroller); - $scroller?.removeEventListener("scroll", onPageScrolled); - $scroller?.addEventListener("scroll", onPageScrolled); - onPageScrolled(); - if (jumpToWeek) { - $todayButton = document.querySelector(todayButton); - $todayButton?.removeEventListener("click", onTodayClicked); - $todayButton?.addEventListener("click", onTodayClicked); + if (!document.contains($scroller)) { + $scroller = document.querySelector(scroller); + $scroller?.removeEventListener("scroll", onPageScrolled); + $scroller?.addEventListener("scroll", onPageScrolled); + onPageScrolled(); + } + if (jumpToWeek && !document.contains($today)) { + $today = document.querySelector(todayButton); + $today?.removeEventListener("click", onTodayClicked); + $today?.addEventListener("click", onTodayClicked); } }; addMutationListener(scroller, setup, { subtree: false });