From mboxrd@z Thu Jan 1 00:00:00 1970 From: andy at warmcat.com (Andy Green) Date: Tue, 26 Jun 2018 19:25:39 +0800 Subject: [PATCH v5 5/6] line-range-highlight: onclick handler and range selection In-Reply-To: <153001215740.12484.13344875445662026876.stgit@mail.warmcat.com> References: <153001215740.12484.13344875445662026876.stgit@mail.warmcat.com> Message-ID: <153001233989.12484.14002171757728106827.stgit@mail.warmcat.com> This allows the user to select line ranges simply by clicking on the line number links. - No selected highlit line, or a range already selected, causes the click to highlight just the clicked line as usual. - Clicking on a second line number link when a single line was already highlit creates a line range highlight, between the lowest and highest line numbers of the already-selected and newly-selected line number links. The order of the clicks is unimportant, you can click the higher line number link first and then the lower to define the range of lines equally well. The implementation is slightly complicated by our single parent onclick handler not being able to interrupt the already ongoing processing of the a href #n change from the link click itself. Rather than bloat every linenumber link with its own onclick handler defeating this default we simply do it with a single parent onclick event and apply any computed range url in the existing hashchange event handler. Signed-off-by: Andy Green --- cgit.js | 32 ++++++++++++++++++++++++++++++++ ui-blame.c | 2 +- ui-tree.c | 2 +- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/cgit.js b/cgit.js index 114b0c5..ebc4e5b 100644 --- a/cgit.js +++ b/cgit.js @@ -98,11 +98,43 @@ function cgit_line_range_highlight() t.scrollIntoView(true); } +function cgit_line_range_click(e) { + var t, m, n = window.location.href.length - window.location.hash.length; + + /* disable passthru to stop needless scrolling by default browser #URL handler */ + e.stopPropagation(); + e.preventDefault(); + + if (!window.location.hash || + window.location.hash.indexOf("-") >= 0) + t = window.location.href.substring(0, n) + + '#n' + e.target.id.substring(1); + else { + if (parseInt(window.location.hash.substring(2)) < + parseInt(e.target.id.substring(1))) /* forwards */ + t = window.location + '-' + e.target.id.substring(1); + else + t = window.location.href.substring(0, n) + + '#n' + e.target.id.substring(1) + '-' + + window.location.href.substring(n + 2); + } + + window.history.replaceState(null, null, t); + + cgit_line_range_highlight(); +} + /* we have to use load, because header images can push the layout vertically */ window.addEventListener("load", function() { cgit_line_range_highlight(); }, false); +document.addEventListener("DOMContentLoaded", function() { + /* event listener cannot override default #URL browser processing, + * requires onclick */ + document.getElementById("linenumbers").onclick = cgit_line_range_click; +}, false); + window.addEventListener("hashchange", function() { cgit_line_range_highlight(); }, false); diff --git a/ui-blame.c b/ui-blame.c index 50d0580..c9cca18 100644 --- a/ui-blame.c +++ b/ui-blame.c @@ -170,7 +170,7 @@ static void print_object(const struct object_id *oid, const char *path, /* Line numbers */ if (ctx.cfg.enable_tree_linenumbers) { - html(""); + html(""); for (ent = sb.ent; ent; ent = ent->next) { html("
");
 			emit_blame_entry_linenumber(ent);
diff --git a/ui-tree.c b/ui-tree.c
index e4cb558..f96b845 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -28,7 +28,7 @@ static void print_text_buffer(const char *name, char *buf, unsigned long size)
 	html("\n");
 
 	if (ctx.cfg.enable_tree_linenumbers) {
-		html("
");
+		html("
");
 		idx = 0;
 		lineno = 0;