From 0ce5198faeff2b9b454749958a9e55b2bc8bb9ea Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Tue, 28 Jan 2025 05:28:49 +0100 Subject: [PATCH] fix scrolling to highlighted lines on source view getClientRects returns an array, and its values are relative to the viewport. scrollIntoView is much easier to use for this. --- root/static/js/syntaxhighlighter.mjs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/root/static/js/syntaxhighlighter.mjs b/root/static/js/syntaxhighlighter.mjs index a224857eddf..e12807f99f3 100644 --- a/root/static/js/syntaxhighlighter.mjs +++ b/root/static/js/syntaxhighlighter.mjs @@ -379,12 +379,10 @@ if (source) { for (const toggle of document.querySelectorAll('.pod-toggle')) { toggle.classList.remove('pod-hidden'); } - window.scrollTo({ - top: lines[0].getClientRects().top, - left: 0, - behavior: 'smooth' - }); } + lines[0].scrollIntoView({ + behavior: 'smooth', + }); }); } @@ -395,10 +393,8 @@ const line_hash = document.location.hash.match(/^(#L\d+)(-|,|$)/); if (line_hash) { const el = document.querySelector(line_hash[1]); if (el) { - window.scrollTo({ - top: el.getClientRects().top, - left: 0, - behavior: 'smooth' + el.scrollIntoView({ + behavior: 'instant', }); } }