Last active
August 29, 2015 14:00
-
-
Save dhensby/11214414 to your computer and use it in GitHub Desktop.
Scroll to the next diff in a PR summary window!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($, w, undefined) { | |
var $diffs = $('.diff-view .js-details-container'); | |
var $comments = $('.diff-view .js-comment'); | |
var $w = $(w); | |
var $a = $('<a/>', { | |
'class': 'js-next-diff', | |
'href': '#' | |
}).css({ | |
'position': 'fixed', | |
'font-size': 'large', | |
'display': 'block', | |
'padding': '1rem', | |
'text-transform': 'uppercase', | |
'top': 0, | |
'right': 0 | |
}).text('Next').on('click', function(ev) { | |
ev.preventDefault(); | |
goToNext($diffs); | |
}); | |
$(document.body).append($a).on('keydown', function(ev) { | |
//if the event came from an input box, we don't want to scroll | |
if ($(ev.target).is(':input')) { | |
return true; | |
} | |
if (ev.which == 74) { | |
goToNext($diffs); | |
} | |
else if (ev.which == 75) { | |
goToPrev($diffs); | |
} | |
else if (ev.which == 78) { | |
goToNext($comments); | |
} | |
else if (ev.which == 77) { | |
goToPrev($comments); | |
} | |
}); | |
function goToNext($els) { | |
var scrollTop = $w.scrollTop() + 5; | |
$els.each(function() { | |
var offsetTop = $(this).offset().top; | |
if (offsetTop > scrollTop) { | |
$('html,body').animate({scrollTop: offsetTop}); | |
return false; | |
} | |
}); | |
} | |
function goToPrev($els) { | |
var scrollTop = $w.scrollTop() - 5; | |
var prev; | |
$els.each(function() { | |
var offsetTop = $(this).offset().top; | |
if (offsetTop >= scrollTop) { | |
//don't animate here, because if we loop through all of them | |
// we want the last item to be animated to | |
return false; | |
} | |
prev = offsetTop; | |
}); | |
//animate to the last "prev" | |
$('html,body').animate({scrollTop: prev}); | |
} | |
})(jQuery, window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This bookmarklet appears to work: