mirror of https://github.com/dpc/tagwiki
`g j` and `g k` for an easy top, bottom jump
This commit is contained in:
parent
635a5fe7a8
commit
440a0d1601
|
@ -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
|
// Modified: https://stackoverflow.com/a/35173443/12271202
|
||||||
// dir: 1 for down, -1 for up
|
// dir: 1 for down, -1 for up
|
||||||
function indexFocusSwitch(dir) {
|
function indexFocusSwitch(dir) {
|
||||||
const parentArea = document.getElementById('page-content');
|
const parentArea = document.getElementById('page-content');
|
||||||
if (parentArea) {
|
if (parentArea) {
|
||||||
var focussableElements = 'a:not([disabled])';
|
const focussable = findFocusableElements(parentArea);
|
||||||
var focussable = Array.prototype.filter.call(parentArea.querySelectorAll(focussableElements),
|
|
||||||
function (element) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
var index = focussable.indexOf(document.activeElement);
|
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];
|
var nextElement = focussable[(index + dir + focussable.length) % focussable.length] || focussable[0];
|
||||||
nextElement.focus();
|
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() {
|
Mousetrap.bind('n', function() {
|
||||||
// window.location.href = '?edit=true';
|
// window.location.href = '?edit=true';
|
||||||
reliableClickById('new-button');
|
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() {
|
Mousetrap.bind(['ctrl+down', 'j'], function() {
|
||||||
indexFocusSwitch(1);
|
indexFocusSwitch(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
Mousetrap.bind(['ctrl+up', 'k'], function() {
|
Mousetrap.bindGlobal(['g k'], function() {
|
||||||
indexFocusSwitch(-1);
|
indexFocusStartOrEnd(-1);
|
||||||
|
});
|
||||||
|
|
||||||
|
Mousetrap.bindGlobal(['g j'], function() {
|
||||||
|
indexFocusStartOrEnd(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
Mousetrap.bind(['/'], function() {
|
Mousetrap.bind(['/'], function() {
|
||||||
|
|
Loading…
Reference in New Issue