implove source-line-num performance.

and Stop scroll when click line number.
This commit is contained in:
nazoking
2015-03-04 03:14:22 +09:00
parent 51d7c43489
commit dc0aa0851e
2 changed files with 41 additions and 22 deletions

View File

@@ -104,35 +104,41 @@ $(window).load(function(){
updateHighlighting(); updateHighlighting();
}).hashchange(); }).hashchange();
var pre = $('pre.prettyprint');
function updateSourceLineNum(){ function updateSourceLineNum(){
$('.source-line-num').remove(); $('.source-line-num').remove();
var pre = $('pre.prettyprint'); var pos = pre.find('ol.linenums').position();
$('pre.prettyprint ol.linenums li').each(function(i, e){ $('<div class="source-line-num">').css({
var p = $(e).position(); height:pre.height(),
var left = pre.position().left width:'48px',
pre.append($('<div class="source-line-num">') cursor:'pointer',
.data('line', (i + 1)) position: 'absolute',
.css({ top : pos.top + 'px',
cursor : 'pointer', left : pos.left + 'px'
position: 'absolute', }).click(function(e){
top : p.top + 'px', $(window).hashchange(function(){})
width : (p.left - left) + 'px', var pos = $(this).data("pos");
left : left, if(!pos){
height : '16px' pos = $('ol.linenums li').map(function(){ return {id:$(this).attr("id"),top:$(this).position().top} }).toArray();
})); $(this).data("pos",pos);
}); }
$('div.source-line-num').click(function(e){ for(var i=0;i<pos.length-1;i++){
var line = $(e.target).data('line'); if(pos[i+1].top>e.pageY){
break;
}
}
var line = pos[i].id.replace(/^L/,'');
var hash = location.hash; var hash = location.hash;
if(e.shiftKey == true && hash.match(/#L\d+(-L\d+)?/)){ if(e.shiftKey == true && hash.match(/#L\d+(-L\d+)?/)){
var lines = hash.split('-'); var lines = hash.split('-');
location.hash = lines[0] + '-L' + line; location.hash = lines[0] + '-L' + line;
} else { } else {
var p = $("#L"+line).attr('id',"");
location.hash = '#L' + line; location.hash = '#L' + line;
p.attr('id','L'+line);
} }
}); }).appendTo(pre);
} }
updateSourceLineNum();
var repository = $('.blame-action').data('repository'); var repository = $('.blame-action').data('repository');
$('.blame-action').click(function(e){ $('.blame-action').click(function(e){
if(history.pushState && $('pre.prettyprint.no-renderable').length){ if(history.pushState && $('pre.prettyprint.no-renderable').length){
@@ -146,13 +152,13 @@ $(window).load(function(){
var m = /^\/(blame|blob)(\/.*)$/.exec(location.pathname.substring(repository.length)); var m = /^\/(blame|blob)(\/.*)$/.exec(location.pathname.substring(repository.length));
var mode = m[1]; var mode = m[1];
$('.blame-action').toggleClass("active", mode=='blame').attr('href', repository + (m[1]=='blame'?'/blob':'/blame')+m[2]); $('.blame-action').toggleClass("active", mode=='blame').attr('href', repository + (m[1]=='blame'?'/blob':'/blame')+m[2]);
var pre = $('pre.prettyprint');
if(pre.parents("td").find(".blame").length){ if(pre.parents("td").find(".blame").length){
pre.parents("div.container").toggleClass("blame-container", mode=='blame'); pre.parents("div.container").toggleClass("blame-container", mode=='blame');
updateSourceLineNum(); updateSourceLineNum();
return; return;
} }
if(mode=='blob'){ if(mode=='blob'){
updateSourceLineNum();
return; return;
} }
$('.blame-action').addClass("active"); $('.blame-action').addClass("active");
@@ -214,15 +220,20 @@ function updateHighlighting(){
var lines = hash.substr(1).split('-'); var lines = hash.substr(1).split('-');
if(lines.length == 1){ if(lines.length == 1){
$('#' + lines[0]).addClass('highlight'); $('#' + lines[0]).addClass('highlight');
$(window).scrollTop($('#' + lines[0]).offset().top - 40); if(!updateHighlighting.scrolling){
$(window).scrollTop($('#' + lines[0]).offset().top - 40);
}
} else if(lines.length > 1){ } else if(lines.length > 1){
var start = parseInt(lines[0].substr(1)); var start = parseInt(lines[0].substr(1));
var end = parseInt(lines[1].substr(1)); var end = parseInt(lines[1].substr(1));
for(var i = start; i <= end; i++){ for(var i = start; i <= end; i++){
$('#L' + i).addClass('highlight'); $('#L' + i).addClass('highlight');
} }
$(window).scrollTop($('#L' + start).offset().top - 40); if(!updateHighlighting.scrolling){
$(window).scrollTop($('#L' + start).offset().top - 40);
}
} }
updateHighlighting.scrolling = true;
} }
} }
</script> </script>

View File

@@ -1332,6 +1332,14 @@ h6 a.markdown-anchor-link {
/****************************************************************************/ /****************************************************************************/
/* blame */ /* blame */
/****************************************************************************/ /****************************************************************************/
.blobview pre.blob{
padding-left: 0;
}
.blobview ol.linenums{
margin-left: 0;
padding-left: 50px;
}
div.container.blame-container{ div.container.blame-container{
width:1270px; width:1270px;
} }