tridactyl/src/keydown_content.ts

91 lines
2.5 KiB
TypeScript
Raw Normal View History

/** Shim for the keyboard API because it won't hit in FF57. */
import * as Messaging from './messaging'
2017-10-06 03:28:14 +01:00
import * as msgsafe from './msgsafe'
import {isTextEditable} from './dom'
import {isSimpleKey} from './keyseq'
function keyeventHandler(ke: KeyboardEvent) {
// Ignore JS-generated events for security reasons.
if (! ke.isTrusted) return
// Bad workaround: never suppress events in an editable field
// and never suppress keys pressed with modifiers
if (state.mode === 'input' || ! (isTextEditable(ke.target as Node) || ke.ctrlKey || ke.altKey)) {
suppressKey(ke)
}
Messaging.message("keydown_background", "recvEvent", [msgsafe.KeyboardEvent(ke)])
}
/** Choose to suppress a key or not */
function suppressKey(ke: KeyboardEvent) {
// Mode specific suppression
2017-11-19 02:41:01 +00:00
TerribleModeSpecificSuppression(ke)
}
2017-11-19 02:41:01 +00:00
// {{{ Shitty key suppression workaround.
import state from './state'
// Keys not to suppress in normal mode.
const normalmodewhitelist = [
2018-01-28 19:51:13 +00:00
// '/',
"'",
2017-11-19 16:22:00 +00:00
' ',
]
const hintmodewhitelist = [
'F3',
'F5',
'F12',
]
2017-11-19 02:41:01 +00:00
function TerribleModeSpecificSuppression(ke: KeyboardEvent) {
switch (state.mode) {
case "normal":
// StartsWith happens to work for our maps so far. Obviously won't in the future.
/* if (Object.getOwnPropertyNames(nmaps).find((map) => map.startsWith(ke.key))) { */
if (isSimpleKey(ke) && ! normalmodewhitelist.includes(ke.key)) {
2017-11-19 02:41:01 +00:00
ke.preventDefault()
ke.stopImmediatePropagation()
2017-11-19 02:41:01 +00:00
}
break
// Hintmode can't clean up after itself yet, so it needs to block more FF shortcuts.
2017-11-19 02:41:01 +00:00
case "hint":
case "find":
if (! hintmodewhitelist.includes(ke.key)) {
ke.preventDefault()
ke.stopImmediatePropagation()
}
2017-11-19 02:41:01 +00:00
break;
case "gobble":
if (isSimpleKey(ke) || ke.key === "Escape") {
ke.preventDefault()
ke.stopImmediatePropagation()
}
break
case "input":
if (ke.key === "Tab") {
ke.preventDefault()
ke.stopImmediatePropagation()
}
break
case "ignore":
break;
case "insert":
break;
2017-11-19 02:41:01 +00:00
}
}
// }}}
// Add listeners
window.addEventListener("keydown", keyeventHandler, true)
import * as SELF from './keydown_content'
Messaging.addListener('keydown_content', Messaging.attributeCaller(SELF))
// Dummy export so that TS treats this as a module.
export {}