tridactyl/src/keydown_content.ts

64 lines
1.9 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'
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 (! (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 = ['/']
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 (! ke.ctrlKey && ! ke.metaKey && ! ke.altKey && ! 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
case "hint":
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 {}