Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent Normal Mode #194

Merged
merged 8 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Changelog
-------------

### Unreleased
* Add `transparentBindings` setting that allows the use of non-bound keys in normal mode ([#188](https://github.com/televator-apps/vimari/issues/188)).
* Remove eager link hint triggering [#190](https://github.com/televator-apps/vimari/issues/190)
* Use `window.open` for `openNewTab` action [#189](https://github.com/televator-apps/vimari/issues/189)
* Add user customisation (based on the work of @nieldm [#163](https://github.com/televator-apps/vimari/pull/163)).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ for HTML elements having cursor style set to "pointer".

**Scroll Size** - How much each scroll will move on the page.

**Transparent Bindings** - Instead of fully isolating all keybindings in normal mode, the transparent bindings setting (when enabled) allows you to use all non-Vimari-bound keys to interact with the web page as if you were in insert mode.

### Keyboard Bindings

#### In-page navigation
Expand Down
33 changes: 31 additions & 2 deletions Vimari Extension/js/injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,41 @@ function unbindKeyCodes() {
document.removeEventListener("keydown", stopSitePropagation);
}

// Stops propagation of keyboard events in normal mode. Adding this
// Returns all keys bound in the settings.
function boundKeys() {
var bindings = Object.values(settings.bindings)
// Split multi-key bindings.
.flatMap(s => s.split(/\+| /i))

// Manually add the modifier, i, esc, and ctr+[.
bindings.push(settings.modifier)
bindings.push("i")
bindings.push("Escape")
bindings.push("Control")
bindings.push("[")

// Use a set to remove duplicates.
return new Set(bindings)
}

// Stops propagation of keyboard events in normal mode. Adding this
// callback to the document using the useCapture flag allows us to
// prevent custom key behaviour implemented by the underlying website.
function stopSitePropagation() {
return function (e) {
if (insertMode == false && !isActiveElementEditable()) {
if (insertMode) {
// Never stop propagation in insert mode.
return
}

if (settings.transparentBindings === true) {
if (boundKeys().has(e.key) && !isActiveElementEditable()) {
// If we are in normal mode with transparentBindings enabled we
// should only stop propagation in an editable element or if the
// key is bound to a Vimari action.
e.stopPropagation()
}
} else if (!isActiveElementEditable()) {
e.stopPropagation()
}
}
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 @@ -7,6 +7,7 @@
"modifier": "",
"smoothScroll": true,
"scrollDuration": 25,
"transparentBindings": true,
"bindings": {
"hintToggle": "f",
"newTabHintToggle": "shift+f",
Expand Down