Skip to content

Commit 6200b3d

Browse files
committed
Merge branch 'isundaylee-go-to-first-input' into master
2 parents 71da0b2 + 7018a3e commit 6200b3d

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ Changelog
55
* Add user customisation (based on the work of @nieldm [#163](https://github.com/televator-apps/vimari/pull/163)).
66
* Update Vimari interface to allow users access to their configuration.
77
* Remove `closeTabReverse` action.
8-
98
* Normal mode now isolates keybindings from the underlying website, this means that to interact with the underlying website you need to enter insert mode.
109
* You can enter insert mode by pressing <kbd>i</kbd> and exit the mode by pressing <kbd>esc</kbd>.
1110
* 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.
11+
* Add `goToFirstInput` action on <kbd>g i</kbd> by default (by [isundaylee](https://github.com/isundaylee)).
1212

1313
### 2.0.3 (2019-09-26)
1414

Vimari Extension/js/injected.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,60 @@ var actionMap = {
8080
function() { window.scrollBy(0, document.body.scrollHeight); },
8181

8282
'goToPageTop':
83-
function() { window.scrollBy(0, -document.body.scrollHeight); }
83+
function() { window.scrollBy(0, -document.body.scrollHeight); },
84+
85+
'goToFirstInput':
86+
function() { goToFirstInput(); },
87+
};
88+
89+
// Inspiration and general algorithm taken from sVim.
90+
function goToFirstInput() {
91+
var inputs = document.querySelectorAll('input,textarea');
92+
93+
var bestInput = null;
94+
var bestInViewInput = null;
95+
96+
inputs.forEach(function(input) {
97+
// Skip if hidden or disabled
98+
if ((input.offsetParent === null) ||
99+
input.disabled ||
100+
(input.getAttribute('type') === 'hidden') ||
101+
(getComputedStyle(input).visibility === 'hidden') ||
102+
(input.getAttribute('display') === 'none')) {
103+
return;
104+
}
105+
106+
// Skip things that are not actual inputs
107+
if ((input.localName !== 'textarea') &&
108+
(input.localName !== 'input') &&
109+
(input.getAttribute('contenteditable') !== 'true')) {
110+
return;
111+
}
112+
113+
// Skip non-text inputs
114+
if (/button|radio|file|image|checkbox|submit/i.test(input.getAttribute('type'))) {
115+
return;
116+
}
117+
118+
var inputRect = input.getClientRects()[0];
119+
var isInView = (inputRect.top >= -inputRect.height) &&
120+
(inputRect.top <= window.innerHeight) &&
121+
(inputRect.left >= -inputRect.width) &&
122+
(inputRect.left <= window.innerWidth);
123+
124+
if (bestInput === null) {
125+
bestInput = input;
126+
}
127+
128+
if (isInView && (bestInViewInput === null)) {
129+
bestInViewInput = input;
130+
}
131+
});
132+
133+
var inputToFocus = bestInViewInput || bestInput;
134+
if (inputToFocus !== null) {
135+
inputToFocus.focus();
136+
}
84137
};
85138

86139
// Meant to be overridden, but still has to be copy/pasted from the original...

Vimari Extension/json/defaultSettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"scrollDownHalfPage": "d",
1717
"goToPageTop": "g g",
1818
"goToPageBottom": "shift+g",
19+
"goToFirstInput": "g i",
1920
"goBack": "shift+h",
2021
"goForward": "shift+l",
2122
"reload": "r",

0 commit comments

Comments
 (0)