Verified Commit f4fdd124 authored by Camil Staps's avatar Camil Staps 🙂

Allow selection of multiple lines in the library browser

parent 9d3600c7
...@@ -18,6 +18,10 @@ table.source-code { ...@@ -18,6 +18,10 @@ table.source-code {
white-space: pre; white-space: pre;
} }
table.source-code tr td {
padding: 0;
}
table.source-code tr:first-child td { table.source-code tr:first-child td {
padding-top: 1em; padding-top: 1em;
} }
...@@ -48,5 +52,16 @@ table.source-code tr td:last-child { ...@@ -48,5 +52,16 @@ table.source-code tr td:last-child {
.hll { .hll {
background-color: #eef; background-color: #eef;
outline: 1px solid #dbdee3; }
table.source-code tr.hll-first {
box-shadow: 0 -1px 0 #dbdee3;
}
table.source-code tr.hll-last {
box-shadow: 0 1px 0 #dbdee3;
}
table.source-code tr.hll-first.hll-last {
box-shadow: 0 -1px 0 #dbdee3, 0 1px 0 #dbdee3;
} }
...@@ -22,27 +22,73 @@ function shareButtonClick() { ...@@ -22,27 +22,73 @@ function shareButtonClick() {
}); });
} }
function selectLine(n) { function selectLines(n) {
browser.state.line = n; var firstline = null;
browser.state.lines = n;
if (!isNaN(n)) {
var hll = document.getElementsByClassName('hll'); var hll = document.getElementsByClassName('hll');
for (var i = 0; i < hll.length; i++) while (hll.length > 0)
hll[i].classList.remove('hll'); hll[0].classList.remove('hll', 'hll-first', 'hll-last');
var linespan = document.getElementById('line-' + browser.state.line); if (!isNaN(n)) {
firstline = n;
var linespan = document.getElementById('line-' + n);
if (linespan != null) if (linespan != null)
linespan.classList.add('hll', 'hll-first', 'hll-last');
} else if (typeof n == 'string') {
var match = n.match(/^(\d+)-(\d+)$/);
if (match != null) {
firstline = match[1];
var first = true;
for (var i = firstline; i < match[2]; i++) {
var linespan = document.getElementById('line-' + i);
if (linespan != null) {
linespan.classList.add('hll'); linespan.classList.add('hll');
if (first) {
linespan.classList.add('hll-first');
first = false;
}
}
}
var linespan = document.getElementById('line-' + match[2]);
if (linespan != null)
linespan.classList.add('hll', 'hll-last');
}
} }
browser.triggerChange(false); browser.triggerChange(false);
return firstline;
}
function selectLinesByEvent(e, n) {
if (e.shiftKey) {
if (!isNaN(browser.state.lines)) {
if (browser.state.lines < n)
selectLines(browser.state.lines + '-' + n);
else
selectLines(n + '-' + browser.state.lines);
return;
} else if (typeof n == 'string') {
var match = n.match(/^(\d+)-(\d+)$/);
if (match != null) {
if (n < match[1])
selectLines(n + '-' + match[2]);
else
selectLines(match[1] + '-' + n);
return;
}
}
}
selectLines(n);
} }
var tableLineNo = null; var tableLineNo = null;
function tableHighlightCallback(span, cls, str) { function tableHighlightCallback(span, cls, str) {
var html = ''; var html = '';
if (tableLineNo == null) { if (tableLineNo == null) {
html = '<tr id="line-1"><td onclick="selectLine(1);">1</td><td>'; html = '<tr id="line-1"><td onclick="selectLinesByEvent(event,1);">1</td><td>';
tableLineNo = 1; tableLineNo = 1;
} }
var lines = str.split('\n'); var lines = str.split('\n');
...@@ -51,7 +97,7 @@ function tableHighlightCallback(span, cls, str) { ...@@ -51,7 +97,7 @@ function tableHighlightCallback(span, cls, str) {
html += highlightCallback( html += highlightCallback(
'<span class="' + cls + '">' + escapeHTML(lines[i]) + '</span>', '<span class="' + cls + '">' + escapeHTML(lines[i]) + '</span>',
cls, lines[i]) + cls, lines[i]) +
'</td></tr><tr id="line-' + tableLineNo + '"><td onclick="selectLine(' + tableLineNo + ');">' '</td></tr><tr id="line-' + tableLineNo + '"><td onclick="selectLinesByEvent(event,' + tableLineNo + ');">'
+ tableLineNo + '</td><td>'; + tableLineNo + '</td><td>';
} }
html += highlightCallback( html += highlightCallback(
...@@ -69,7 +115,7 @@ window.onload = function() { ...@@ -69,7 +115,7 @@ window.onload = function() {
browser = document.getElementsByClassName('browser')[0].browser({ browser = document.getElementsByClassName('browser')[0].browser({
newPath: function (path) { newPath: function (path) {
this.state.mod = path.join('/'); this.state.mod = path.join('/');
this.state.line = null; this.state.lines = null;
this.newState(); this.newState();
}, },
newHash: function (hash) { newHash: function (hash) {
...@@ -77,16 +123,16 @@ window.onload = function() { ...@@ -77,16 +123,16 @@ window.onload = function() {
var update = this.state.mod != hashelems[0]; var update = this.state.mod != hashelems[0];
this.state.mod = hashelems[0]; this.state.mod = hashelems[0];
this.state.icl = false; this.state.icl = false;
this.state.line = null; this.state.lines = null;
for (var i = 1; i < hashelems.length; i++) { for (var i = 1; i < hashelems.length; i++) {
if (hashelems[i] == 'icl') if (hashelems[i] == 'icl')
icl.checked = this.state.icl = true; icl.checked = this.state.icl = true;
else if (hashelems[i].substring(0,5) == 'line=') else if (hashelems[i].substring(0,5) == 'line=')
this.state.line = hashelems[i].substring(5); this.state.lines = hashelems[i].substring(5);
} }
if (this.state.line != null) if (this.state.lines != null)
selectLine(this.state.line); selectLines(this.state.lines);
browser.openPath(this.state.mod.split('/')); browser.openPath(this.state.mod.split('/'));
browser.triggerChange(update); browser.triggerChange(update);
...@@ -95,8 +141,8 @@ window.onload = function() { ...@@ -95,8 +141,8 @@ window.onload = function() {
var hash = this.state.mod; var hash = this.state.mod;
if (this.state.icl) if (this.state.icl)
hash += ';icl'; hash += ';icl';
if (this.state.line != null) if (this.state.lines != null)
hash += ';line=' + this.state.line; hash += ';line=' + this.state.lines;
browser.setHash(hash); browser.setHash(hash);
}, },
...@@ -104,8 +150,8 @@ window.onload = function() { ...@@ -104,8 +150,8 @@ window.onload = function() {
var url = 'src.php?mod=' + this.state.mod; var url = 'src.php?mod=' + this.state.mod;
if (this.state.icl) if (this.state.icl)
url += '&icl'; url += '&icl';
if (this.state.line != null) if (this.state.lines != null)
url += '&line=' + this.state.line; url += '&line=' + this.state.lines;
return url; return url;
}, },
viewer: viewer, viewer: viewer,
...@@ -115,9 +161,10 @@ window.onload = function() { ...@@ -115,9 +161,10 @@ window.onload = function() {
highlightClean(text, tableHighlightCallback) + highlightClean(text, tableHighlightCallback) +
'</table>'; '</table>';
viewer.scrollLeft = 0; viewer.scrollLeft = 0;
if (state.line != null) { if (state.lines != null) {
selectLine(state.line); var firstline = selectLines(state.lines);
browser.scrollTo(document.getElementById('line-' + state.line)); if (firstline != null)
browser.scrollTo(document.getElementById('line-' + firstline));
} else { } else {
browser.scrollTo(); browser.scrollTo();
} }
...@@ -138,7 +185,7 @@ window.onload = function() { ...@@ -138,7 +185,7 @@ window.onload = function() {
browser.state.icl = icl.checked; browser.state.icl = icl.checked;
icl.onchange = function() { icl.onchange = function() {
browser.state.icl = this.checked; browser.state.icl = this.checked;
browser.state.line = null; browser.state.lines = null;
browser.triggerChange(); browser.triggerChange();
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment