refactor keymap generation to lib/keyseq

This commit is contained in:
Torsten Schmits 2020-05-08 20:19:51 +02:00
parent 3f70e1886b
commit 7a15db4f98
3 changed files with 19 additions and 30 deletions

View file

@ -23,6 +23,7 @@
/** */
import { filter, find, izip } from "@src/lib/itertools"
import { Parser } from "@src/lib/nearley_utils"
import * as config from "@src/lib/config"
import grammar from "@src/grammars/.bracketexpr.generated"
const bracketexpr_grammar = grammar
const bracketexpr_parser = new Parser(bracketexpr_grammar)
@ -345,6 +346,21 @@ export function mapstrMapToKeyMap(mapstrMap: Map<string, MapTarget>): KeyMap {
return newKeyMap
}
export function keyMap(conf, keys): KeyMap {
let maps: any = config.get(conf)
if (maps === undefined) throw new Error("No binds defined for this mode. Reload page with <C-r> and add binds, e.g. :bind --mode=[mode] <Esc> mode normal")
// If so configured, translate keys using the key translation map
if (config.get("keytranslatemodes")[conf] === "true") {
const translationmap = config.get("keytranslatemap")
translateKeysUsingKeyTranslateMap(keys, translationmap)
}
// Convert to KeyMap
maps = new Map(Object.entries(maps))
return mapstrMapToKeyMap(maps)
}
// }}}
// {{{ Utility functions for dealing with KeyboardEvents

View file

@ -1,21 +1,8 @@
/** Tridactyl helper mode */
import * as config from "@src/lib/config"
import * as keyseq from "@src/lib/keyseq"
export function parser(conf, keys): keyseq.ParserResponse {
let maps: any = config.get(conf)
if (maps === undefined) throw new Error("No binds defined for this mode. Reload page with <C-r> and add binds, e.g. :bind --mode=[mode] <Esc> mode normal")
// If so configured, translate keys using the key translation map
if (config.get("keytranslatemodes")[conf] === "true") {
const translationmap = config.get("keytranslatemap")
keyseq.translateKeysUsingKeyTranslateMap(keys, translationmap)
}
// Convert to KeyMap
maps = new Map(Object.entries(maps))
maps = keyseq.mapstrMapToKeyMap(maps)
const maps = keyseq.keyMap(conf, keys)
return keyseq.parse(keys, maps)
}

View file

@ -4,7 +4,7 @@ import { contentState } from "@src/content/state_content"
import * as config from "@src/lib/config"
import * as keyseq from "@src/lib/keyseq"
/** Simple container for the gobble state. */
/** Simple container for the nmode state. */
class NModeState {
public numCommands = 1
public curCommands = 0
@ -35,22 +35,8 @@ const configs = {
/** Receive keypress. If applicable, execute a command. */
export function parser(keys: KeyboardEvent[]) {
// Borrowed from genericmode.ts
const conf = configs[modeState.mode] || modeState.mode + "maps"
let maps: any = config.get(conf)
if (maps === undefined) throw new Error("No binds defined for this mode. Reload page with <C-r> and add binds, e.g. :bind --mode=[mode] <Esc> mode normal")
// If so configured, translate keys using the key translation map
if (config.get("keytranslatemodes")[conf] === "true") {
const translationmap = config.get("keytranslatemap")
keyseq.translateKeysUsingKeyTranslateMap(keys, translationmap)
}
// Convert to KeyMap
maps = new Map(Object.entries(maps))
maps = keyseq.mapstrMapToKeyMap(maps)
// genericmode.ts borrowing ends
const maps: any = keyseq.keyMap(conf, keys)
const key = keys[0].key
if (key === "Escape") {