[PATCH v2 1/2] cgit.js: make line range highlight responsive to url changes
Andy Green
andy at warmcat.com
Sat Jun 23 01:01:52 CEST 2018
Default interpretation of the # part of the URL is to try to
match it to an element ID one time at page load.
Subsequent modifications in the URL bar are ignored, even if
you hit enter there.
This patch makes the line range highlight able to clean up
after itself and be reapplied, and has the highlight function
listen for hashchange events.
Now if you edit the URL and press enter, any existing highlight
is removed, the # part reinterpreted and the new highlight
applied automatically.
This is particularly useful if you edit the # link with the
intention to show a range before copying the URL.
Signed-off-by: Andy Green <andy at warmcat.com>
---
cgit.js | 78 ++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 52 insertions(+), 26 deletions(-)
diff --git a/cgit.js b/cgit.js
index 6e3473c..5430c43 100644
--- a/cgit.js
+++ b/cgit.js
@@ -1,43 +1,69 @@
+var cgit_line_range_depth;
+
function cgit_line_range_highlight(depth)
{
var h = window.location.hash, l1 = 0, l2 = 0, e, t;
+ cgit_line_range_depth = depth;
+
+ e = document.getElementById("cgit_line_range");
+ if (e) {
+ l1 = e.l1;
+ while (l1 <= e.l2) {
+ var e1;
+ e1 = document.getElementById('n' + l1++);
+ e1.style.backgroundColor = null;
+ }
+
+ e.remove();
+ }
+
l1 = parseInt(h.substring(2));
+ if (!l1)
+ return;
+
t = h.indexOf("-");
- if (t >= 1)
+ l2 = l1;
+ if (t >= 1) {
l2 = parseInt(h.substring(t + 1));
- else
- l2 = l1;
-
- if (l1) {
- var lh, t = 0, e1, de;
+ if (l2 < l1)
+ l2 = l1;
+ }
- e1 = e = document.getElementById('n' + l1);
- if (e) {
+ var lh, t = 0, e1, de;
- while (e1) { if (e1.offsetTop) t += e1.offsetTop; e1 = e1.offsetParent; }
+ e1 = e = document.getElementById('n' + l1);
+ if (!e)
+ return;
- de = document.createElement("DIV");
+ while (e1) { if (e1.offsetTop) t += e1.offsetTop; e1 = e1.offsetParent; }
- de.className = "line_range";
- de.style.bottom = e.style.bottom;
- de.style.top = t + 'px';
- if (!depth)
- de.style.width = e.parentElement.parentElement.parentNode.offsetWidth + 'px';
- else
- de.style.width = e.parentElement.parentElement.parentNode.parentNode.offsetWidth + 'px';
- de.style.height = ((l2 - l1 + 1) * e.offsetHeight) + 'px';
+ de = document.createElement("DIV");
- e.parentElement.parentElement.insertBefore(de, e.parentElement.parentElement.firstChild);
+ de.className = "line_range";
+ de.style.bottom = e.style.bottom;
+ de.style.top = t + 'px';
+ de.id = "cgit_line_range";
+ de.l1 = l1;
+ de.l2 = l2;
+ if (!depth)
+ de.style.width = e.parentElement.parentElement.parentNode.offsetWidth + 'px';
+ else
+ de.style.width = e.parentElement.parentElement.parentNode.parentNode.offsetWidth + 'px';
+ de.style.height = ((l2 - l1 + 1) * e.offsetHeight) + 'px';
- while (l1 <= l2) {
- var e1;
- e1 = document.getElementById('n' + l1++);
- e1.style.backgroundColor = "yellow";
- }
+ e.parentElement.parentElement.insertBefore(de, e.parentElement.parentElement.firstChild);
- e.scrollIntoView(true);
- }
+ while (l1 <= l2) {
+ var e1;
+ e1 = document.getElementById('n' + l1++);
+ e1.style.backgroundColor = "yellow";
}
+
+ window.addEventListener("hashchange", function() {
+ cgit_line_range_highlight(cgit_line_range_depth);
+ }, false);
+
+ e.scrollIntoView(true);
}
More information about the CGit
mailing list