Skip to content

Commit 835a56e

Browse files
authored
Merge branch 'master' into 172-smooth-scrolling
2 parents c36a93f + 6200b3d commit 835a56e

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Changelog
88
* Normal mode now isolates keybindings from the underlying website, this means that to interact with the underlying website you need to enter insert mode.
99
* You can enter insert mode by pressing <kbd>i</kbd> and exit the mode by pressing <kbd>esc</kbd>.
1010
* 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)).
1112
* Add smooth scrolling (based on sVim implementation).
1213

1314
### 2.0.3 (2019-09-26)

Vimari Extension/js/injected.js

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

8282
'goToPageTop':
83-
function() { customScrollBy(0, -document.body.scrollHeight); }
83+
function() { customScrollBy(0, -document.body.scrollHeight); },
84+
85+
'goToFirstInput':
86+
function() { goToFirstInput(); }
8487
};
8588

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+
}
137+
}
138+
86139
// Meant to be overridden, but still has to be copy/pasted from the original...
87140
Mousetrap.prototype.stopCallback = function(e, element, combo) {
88141
// Escape key is special, no need to stop. Vimari-specific.

Vimari Extension/json/defaultSettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"scrollDownHalfPage": "d",
1919
"goToPageTop": "g g",
2020
"goToPageBottom": "shift+g",
21+
"goToFirstInput": "g i",
2122
"goBack": "shift+h",
2223
"goForward": "shift+l",
2324
"reload": "r",

0 commit comments

Comments
 (0)