Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
history scroll by shortcut key (Redmine view customize plugin)
$(function() {
var KEYCODE_J = 74;
var KEYCODE_K = 75;
var EXCEPT_TAG_NAMES = ['input', 'textarea', 'select'];
var targetElements = $('#history > div');
var currentIndex = -1;
document.addEventListener(
'keydown',
function(event) {
if (EXCEPT_TAG_NAMES.indexOf(event.target.tagName.toLowerCase()) != -1) {
return;
}
if (event.keyCode != KEYCODE_J && event.keyCode != KEYCODE_K) {
return;
}
var nextIndex =
currentIndex + ((event.keyCode == KEYCODE_J) ? 1 : -1);
if (nextIndex < 0) {
nextIndex = 0;
} else if (nextIndex >= targetElements.length) {
nextIndex = targetElements.length - 1;
}
targetElements.css('border', '');
targetElements[nextIndex].scrollIntoView();
targetElements.eq(nextIndex).css('border', '3px solid #d7d7d7');
currentIndex = nextIndex;
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.