Skip to content

Commit

Permalink
Merge branch 'master' into 172-smooth-scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
nbelzer authored Aug 19, 2020
2 parents c36a93f + 6200b3d commit 835a56e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changelog
* 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)).
* Add smooth scrolling (based on sVim implementation).

### 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,9 +80,62 @@ var actionMap = {
function() { customScrollBy(0, document.body.scrollHeight); },

'goToPageTop':
function() { customScrollBy(0, -document.body.scrollHeight); }
function() { customScrollBy(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...
Mousetrap.prototype.stopCallback = function(e, element, combo) {
// Escape key is special, no need to stop. Vimari-specific.
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 @@ -18,6 +18,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 835a56e

Please sign in to comment.