[feature] key bindings: left & right arrow are always on

This commit is contained in:
Jinyuan Huang 2023-09-08 06:51:01 +08:00 committed by Markus Heiser
parent b189578b6b
commit e6b160da62
2 changed files with 47 additions and 15 deletions

View File

@ -55,13 +55,30 @@ searxng.ready(function () {
} }
}, true); }, true);
var vimKeys = { // these bindings are always on
var keyBindings = {
27: { 27: {
key: 'Escape', key: 'Escape',
fun: removeFocus, fun: removeFocus,
des: 'remove focus from the focused input', des: 'remove focus from the focused input',
cat: 'Control' cat: 'Control'
}, },
37: {
key: 'Left',
fun: ifDetailOpened(highlightResult('up')),
des: 'select previous search result',
cat: 'Results'
},
39: {
key: 'Right',
fun: ifDetailOpened(highlightResult('down')),
des: 'select next search result',
cat: 'Results'
}
}
// these bindings are enabled by user preferences
var vimKeys = {
73: { 73: {
key: 'i', key: 'i',
fun: searchInputFocus, fun: searchInputFocus,
@ -155,20 +172,31 @@ searxng.ready(function () {
}; };
if (searxng.settings.hotkeys) { if (searxng.settings.hotkeys) {
searxng.on(document, "keydown", function (e) { // To add Vim-like key bindings, merge the 'vimKeys' into 'keyBindings'.
// check for modifiers so we don't break browser's hotkeys Object.assign(keyBindings, vimKeys);
if (Object.prototype.hasOwnProperty.call(vimKeys, e.keyCode) && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) { }
var tagName = e.target.tagName.toLowerCase();
if (e.keyCode === 27) { searxng.on(document, "keydown", function (e) {
vimKeys[e.keyCode].fun(e); // check for modifiers so we don't break browser's hotkeys
} else { if (Object.prototype.hasOwnProperty.call(keyBindings, e.keyCode) && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {
if (e.target === document.body || tagName === 'a' || tagName === 'button') { var tagName = e.target.tagName.toLowerCase();
e.preventDefault(); if (e.keyCode === 27) {
vimKeys[e.keyCode].fun(); keyBindings[e.keyCode].fun(e);
} } else {
if (e.target === document.body || tagName === 'a' || tagName === 'button') {
e.preventDefault();
keyBindings[e.keyCode].fun();
} }
} }
}); }
});
function ifDetailOpened (f) {
return function () {
if (searxng.isDetailOpened()) {
f();
}
}
} }
function highlightResult (which) { function highlightResult (which) {
@ -347,8 +375,8 @@ searxng.ready(function () {
function initHelpContent (divElement) { function initHelpContent (divElement) {
var categories = {}; var categories = {};
for (var k in vimKeys) { for (var k in keyBindings) {
var key = vimKeys[k]; var key = keyBindings[k];
categories[key.cat] = categories[key.cat] || []; categories[key.cat] = categories[key.cat] || [];
categories[key.cat].push(key); categories[key.cat].push(key);
} }

View File

@ -74,6 +74,10 @@
searxng.scrollPageToSelected(); searxng.scrollPageToSelected();
} }
searxng.isDetailOpened = function () {
return d.getElementById('results').classList.contains('image-detail-open');
}
searxng.closeDetail = function (e) { searxng.closeDetail = function (e) {
d.getElementById('results').classList.remove('image-detail-open'); d.getElementById('results').classList.remove('image-detail-open');
searxng.scrollPageToSelected(); searxng.scrollPageToSelected();