From mboxrd@z Thu Jan 1 00:00:00 1970 From: andy at warmcat.com (Andy Green) Date: Tue, 26 Jun 2018 19:25:34 +0800 Subject: [PATCH v5 4/6] cgit.js: line range highlight: improve vertical scroll logic In-Reply-To: <153001215740.12484.13344875445662026876.stgit@mail.warmcat.com> References: <153001215740.12484.13344875445662026876.stgit@mail.warmcat.com> Message-ID: <153001233481.12484.15834229108464485461.stgit@mail.warmcat.com> Instead of following the browser heuristic to put any matching id element to the top left of the browser window, compute the number of visible lines vertically in the window, and the middle of the highlit range, and try to centre the middle of the highlit range in the window. If the top of the range is no longer visible due to a range consisting of more lines than the window can show, fall back to placing the top of the range at the top of the window. Signed-off-by: Andy Green --- cgit.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cgit.js b/cgit.js index ce501bf..114b0c5 100644 --- a/cgit.js +++ b/cgit.js @@ -47,7 +47,7 @@ function cgit_line_range_highlight() if (l2 < l1) l2 = l1; - var lh, etable, etr, de, n; + var lh, etable, etr, de, n, hl, v; e = document.getElementById('n' + l1); if (!e) @@ -84,13 +84,25 @@ function cgit_line_range_highlight() while (n <= l2) document.getElementById('n' + n++).style.backgroundColor = "yellow"; - e.scrollIntoView(true); + hl = (window.innerHeight / (e.offsetHeight + 1)); + v = (l1 + ((l2 - l1) / 2)) - (hl / 2); + if (v > l1) + v = l1; + if (v < 1) + v = 1; + + t = document.getElementById('n' + Math.round(v)); + if (!t) + t = e; + + t.scrollIntoView(true); } /* we have to use load, because header images can push the layout vertically */ window.addEventListener("load", function() { cgit_line_range_highlight(); }, false); + window.addEventListener("hashchange", function() { cgit_line_range_highlight(); }, false);