Added dynamic line highlighter

fixes #138
This commit is contained in:
Claude 2014-09-03 18:10:48 +02:00
parent 02d3590322
commit ffd7b10dd2

View File

@ -50,10 +50,68 @@ ST.spamadmin = function() {
}
};
ST.line_highlighter = function() {
var org_href = window.location.href.replace(/(.*?)#(.*)/, '$1');
var first_line = false;
var second_line = false;
$('.text').on('mousedown', function() {
window.getSelection().removeAllRanges();
});
$('.text').on('click', 'li', function(ev) {
var $this = $(this);
var li_num = ($this.index() + 1);
if(ev.shiftKey == 1){
second_line = li_num;
} else {
first_line = li_num;
second_line = false;
}
if(second_line){
// determine mark
if(first_line < second_line) {
sel_start = first_line;
sel_end = second_line;
} else {
sel_start = second_line;
sel_end = first_line;
}
window.location.href = org_href + '#L' + sel_start + '-L' + sel_end;
} else {
window.location.href = org_href + '#L' + first_line;
}
ST.highlight_lines();
});
ST.highlight_lines();
}
ST.highlight_lines = function() {
var wloc = window.location.href;
if(wloc.indexOf('#') > -1) {
$('.text li').css('background', 'none');
var lines = wloc.split('#')[1];
if(lines.indexOf('-') > -1) {
var start_line = parseInt(lines.split('-')[0].replace('L', ''), 10);
var end_line = parseInt(lines.split('-')[1].replace('L', ''), 10);
for(var i=start_line; i<=end_line; i++) {
$('.text li:nth-child(' + i + ')').css('background', '#F8EEC7');
}
} else {
var marked_line = lines.replace('L', '');
$('.text li:nth-child(' + marked_line + ')').css('background', '#F8EEC7');
}
}
}
ST.init = function() {
ST.expand();
ST.show_embed();
ST.spamadmin();
ST.line_highlighter();
};
$(document).ready(function() {