Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
map cmd+[left,right] to navigation on non-editable elements
Browse files Browse the repository at this point in the history
Fix #378. TODO: map ctrl+[left, right] on Windows.

Auditors: @bbondy
  • Loading branch information
diracdeltas committed Jan 30, 2016
1 parent fe282da commit 735a60a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
41 changes: 36 additions & 5 deletions app/content/webviewPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ function hasSelection (node) {
return false
}

/**
* Whether an element is editable or can be typed into.
* @param {Element} elem
* @return {boolean}
*/
function isEditable (elem) {
// TODO: find other node types that are editable
return (elem.contentEditable === 'true' ||
elem.nodeName === 'INPUT' ||
elem.nodeName === 'TEXTAREA')
}

document.addEventListener('contextmenu', (e) => {
var name = e.target.nodeName.toUpperCase()
var nodeProps = {
Expand All @@ -211,6 +223,7 @@ document.addEventListener('contextmenu', (e) => {
}, false)

var shiftDown = false
var cmdDown = false
document.onkeydown = (e) => {
switch (e.keyCode) {
case KeyCodes.ESC:
Expand All @@ -219,24 +232,42 @@ document.onkeydown = (e) => {
break
case KeyCodes.BACKSPACE:
const msg = shiftDown ? messages.GO_FORWARD : messages.GO_BACK
const elem = document.activeElement
if (elem.contentEditable !== 'true' &&
elem.nodeName !== 'INPUT' &&
elem.nodeName !== 'TEXTAREA') {
// TODO: find other node types where this shortcut should be disabled
if (!isEditable(document.activeElement)) {
ipc.send(msg)
}
break
case KeyCodes.SHIFT:
shiftDown = true
break
case KeyCodes.CMD1:

This comment has been minimized.

Copy link
@bbondy

bbondy Jan 30, 2016

Member

Should this be OSX only and command on windows?

This comment has been minimized.

Copy link
@bbondy

bbondy Jan 30, 2016

Member

Fairly sure on Windows the Control variant only jumps by a word at a time though.

This comment has been minimized.

Copy link
@diracdeltas

diracdeltas Jan 30, 2016

Author Member

I think so, i don't have a windows machine to test what happens in Chrome on windows. I'll fix to make it mac-only for now.

cmdDown = true
break
case KeyCodes.CMD2:
cmdDown = true
break

This comment has been minimized.

Copy link
@bbondy

bbondy Jan 30, 2016

Member

nit: Could just do

case KeyCodes.CMD1:
case KeyCodes.CMD2:
  cmdDown = true
break

Maybe the linter warns about it though?

This comment has been minimized.

Copy link
@diracdeltas

diracdeltas Jan 30, 2016

Author Member

will do

case KeyCodes.LEFT:
if (cmdDown && !isEditable(document.activeElement)) {
ipc.send(messages.GO_BACK)
}
break
case KeyCodes.RIGHT:
if (cmdDown && !isEditable(document.activeElement)) {
ipc.send(messages.GO_FORWARD)
}
break
}
}
document.onkeyup = (e) => {
switch (e.keyCode) {
case KeyCodes.SHIFT:
shiftDown = false
break
case KeyCodes.CMD1:
cmdDown = false
break
case KeyCodes.CMD2:
cmdDown = false
break
}
}

Expand Down
7 changes: 6 additions & 1 deletion js/constants/keyCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const KeyCodes = {
UP: 38,
DOWN: 40,
SHIFT: 16,
BACKSPACE: 8
BACKSPACE: 8,
CTRL: 17,
CMD1: 91,
CMD2: 93,
LEFT: 37,
RIGHT: 39
}

module.exports = KeyCodes

3 comments on commit 735a60a

@bbondy
Copy link
Member

@bbondy bbondy commented on 735a60a Jan 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ great stuff
Can we do this for the chrome inputs too? (urlbar and findbar, but we'll have others later like in preferences)

@diracdeltas
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbondy cmd+left,right are already mapped to jump to start/end of line in chrome inputs (by the operating system)

@bbondy
Copy link
Member

@bbondy bbondy commented on 735a60a Jan 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbondy cmd+left,right are already mapped to jump to start/end of line in chrome inputs (by the operating system)

Sorry if that's the case, I have a VM so hard to tell because they rewrite those.

Please sign in to comment.