Skip to content

Commit

Permalink
Merge branch 'isundaylee-go-to-first-input' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
nbelzer committed Aug 12, 2020
2 parents 71da0b2 + 7018a3e commit 6200b3d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Changelog
* Add user customisation (based on the work of @nieldm [#163](https://github.com/televator-apps/vimari/pull/163)).
* Update Vimari interface to allow users access to their configuration.
* Remove `closeTabReverse` action.

* Normal mode now isolates keybindings from the underlying website, this means that to interact with the underlying website you need to enter insert mode.
* You can enter insert mode by pressing <kbd>i</kbd> and exit the mode by pressing <kbd>esc</kbd>.
* In insert mode Vimari keybindings are disabled (except for <kbd>esc</kbd> which brings you back to normal mode) allowing you to interact with the underlying website.
* Add `goToFirstInput` action on <kbd>g i</kbd> by default (by [isundaylee](https://github.com/isundaylee)).

### 2.0.3 (2019-09-26)

Expand Down
55 changes: 54 additions & 1 deletion Vimari Extension/js/injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,60 @@ var actionMap = {
function() { window.scrollBy(0, document.body.scrollHeight); },

'goToPageTop':
function() { window.scrollBy(0, -document.body.scrollHeight); }
function() { window.scrollBy(0, -document.body.scrollHeight); },

'goToFirstInput':
function() { goToFirstInput(); },
};

// Inspiration and general algorithm taken from sVim.
function goToFirstInput() {
var inputs = document.querySelectorAll('input,textarea');

var bestInput = null;
var bestInViewInput = null;

inputs.forEach(function(input) {
// Skip if hidden or disabled
if ((input.offsetParent === null) ||
input.disabled ||
(input.getAttribute('type') === 'hidden') ||
(getComputedStyle(input).visibility === 'hidden') ||
(input.getAttribute('display') === 'none')) {
return;
}

// Skip things that are not actual inputs
if ((input.localName !== 'textarea') &&
(input.localName !== 'input') &&
(input.getAttribute('contenteditable') !== 'true')) {
return;
}

// Skip non-text inputs
if (/button|radio|file|image|checkbox|submit/i.test(input.getAttribute('type'))) {
return;
}

var inputRect = input.getClientRects()[0];
var isInView = (inputRect.top >= -inputRect.height) &&
(inputRect.top <= window.innerHeight) &&
(inputRect.left >= -inputRect.width) &&
(inputRect.left <= window.innerWidth);

if (bestInput === null) {
bestInput = input;
}

if (isInView && (bestInViewInput === null)) {
bestInViewInput = input;
}
});

var inputToFocus = bestInViewInput || bestInput;
if (inputToFocus !== null) {
inputToFocus.focus();
}
};

// Meant to be overridden, but still has to be copy/pasted from the original...
Expand Down
1 change: 1 addition & 0 deletions Vimari Extension/json/defaultSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"scrollDownHalfPage": "d",
"goToPageTop": "g g",
"goToPageBottom": "shift+g",
"goToFirstInput": "g i",
"goBack": "shift+h",
"goForward": "shift+l",
"reload": "r",
Expand Down

0 comments on commit 6200b3d

Please sign in to comment.