[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cgit
Subject:    [PATCH v6 4/7] cgit.js: line range highlight: improve vertical scroll logic
From:       Andy Green <andy () warmcat ! com>
Date:       2018-06-29 1:40:34
Message-ID: 153023643461.27756.10380225630206174134.stgit () mail ! warmcat ! com
[Download RAW message or body]

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 <andy@warmcat.com>
---
 cgit.js |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/cgit.js b/cgit.js
index 6cc27c1..d99c980 100644
--- a/cgit.js
+++ b/cgit.js
@@ -57,7 +57,7 @@ function 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)
@@ -94,7 +94,18 @@ function 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 */

_______________________________________________
CGit mailing list
CGit@lists.zx2c4.com
https://lists.zx2c4.com/mailman/listinfo/cgit
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic