`g j` and `g k` for an easy top, bottom jump

This commit is contained in:
Dawid Ciężarkiewicz 2020-09-05 23:23:48 -07:00
parent 635a5fe7a8
commit 440a0d1601
No known key found for this signature in database
GPG Key ID: E1DD596D71BDC261
1 changed files with 34 additions and 9 deletions

View File

@ -50,25 +50,42 @@ function smartFocusEditById(id) {
}
}
function findFocusableElements(parentArea) {
const focussableElements = 'a:not([disabled])';
return Array.prototype.filter.call(parentArea.querySelectorAll(focussableElements),
function (element) {
return true;
}
);
}
// Modified: https://stackoverflow.com/a/35173443/12271202
// dir: 1 for down, -1 for up
function indexFocusSwitch(dir) {
const parentArea = document.getElementById('page-content');
if (parentArea) {
var focussableElements = 'a:not([disabled])';
var focussable = Array.prototype.filter.call(parentArea.querySelectorAll(focussableElements),
function (element) {
return true;
}
);
const focussable = findFocusableElements(parentArea);
var index = focussable.indexOf(document.activeElement);
if(focussable.length > 0) {
if (focussable.length > 0) {
var nextElement = focussable[(index + dir + focussable.length) % focussable.length] || focussable[0];
nextElement.focus();
}
}
}
// dir: -1 start, 1 end
function indexFocusStartOrEnd(dir) {
const parentArea = document.getElementById('page-content');
if (parentArea) {
const focussable = findFocusableElements(parentArea);
var index = focussable.indexOf(document.activeElement);
if (focussable.length > 0) {
var nextElement = focussable[dir === -1 ? 0 : focussable.length - 1] || focussable[0];
nextElement.focus();
}
}
}
Mousetrap.bind('n', function() {
// window.location.href = '?edit=true';
reliableClickById('new-button');
@ -120,12 +137,20 @@ Mousetrap.bind(['ctrl+right', 'l'], function() {
}
});
Mousetrap.bind(['ctrl+up', 'k'], function() {
indexFocusSwitch(-1);
});
Mousetrap.bind(['ctrl+down', 'j'], function() {
indexFocusSwitch(1);
});
Mousetrap.bind(['ctrl+up', 'k'], function() {
indexFocusSwitch(-1);
Mousetrap.bindGlobal(['g k'], function() {
indexFocusStartOrEnd(-1);
});
Mousetrap.bindGlobal(['g j'], function() {
indexFocusStartOrEnd(1);
});
Mousetrap.bind(['/'], function() {