Skip to content

Commit

Permalink
Implement normal mode separation
Browse files Browse the repository at this point in the history
  • Loading branch information
nbelzer committed Jul 25, 2020
1 parent abb69bf commit 4cbd1ca
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions Vimari Extension/js/injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,27 @@ function executeAction(actionName) {

function unbindKeyCodes() {
Mousetrap.reset();
document.removeEventListener("keydown", stopSitePropagation);
}

// 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()) {
e.stopPropagation()
}
}
}

// Check whether the current active element is editable.
function isActiveElementEditable() {
const el = document.activeElement;
return (el != null && isEditable(el))
}


// Adds an optional modifier to the configured key code for the action
function getKeyCode(actionName) {
var keyCode = '';
Expand Down Expand Up @@ -225,7 +244,7 @@ function handleMessage(msg) {
*/
function setSettings(msg) {
settings = msg;
bindKeyCodesToActions();
activateExtension();
}

/*
Expand All @@ -234,12 +253,18 @@ function setSettings(msg) {
function setActive(msg) {
extensionActive = msg;
if(msg) {
bindKeyCodesToActions();
activateExtension();
} else {
unbindKeyCodes();
}
}

function activateExtension() {
// Stop keydown propagation
document.addEventListener("keydown", stopSitePropagation(), true);
bindKeyCodesToActions();
}

function isExcludedUrl(storedExcludedUrls, currentUrl) {
if (!storedExcludedUrls.length) {
return false;
Expand Down

0 comments on commit 4cbd1ca

Please sign in to comment.