You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

79 lines
2.0 KiB

import * as actions from '../shared/actions';
const DEFAULT_KEYMAP = {
':': [ actions.CMD_OPEN ],
'o': [ actions.CMD_TABS_OPEN, false ],
'O': [ actions.CMD_TABS_OPEN, true ],
'k': [ actions.SCROLL_LINES, -1 ],
'j': [ actions.SCROLL_LINES, 1 ],
'<C-e>': [ actions.SCROLL_LINES, -1 ],
'<C-y>': [ actions.SCROLL_LINES, 1 ],
'<C-u>': [ actions.SCROLL_PAGES, -0.5 ],
'<C-d>': [ actions.SCROLL_PAGES, 0.5 ],
'<C-b>': [ actions.SCROLL_PAGES, -1 ],
'<C-f>': [ actions.SCROLL_PAGES, 1 ],
'gg': [ actions.SCROLL_TOP ],
'G': [ actions.SCROLL_BOTTOM ],
'd': [ actions.TABS_CLOSE ],
'u': [ actions.TABS_REOPEN],
'h': [ actions.TABS_PREV, 1 ],
'l': [ actions.TABS_NEXT, 1 ],
'r': [ actions.TABS_RELOAD, false ],
'R': [ actions.TABS_RELOAD, true ],
'zi': [ actions.ZOOM_IN ],
'zo': [ actions.ZOOM_OUT ],
'zz': [ actions.ZOOM_NEUTRAL],
'f': [ actions.FOLLOW_START, false ],
'F': [ actions.FOLLOW_START, true ],
'H': [ actions.HISTORY_PREV ],
'L': [ actions.HISTORY_NEXT ],
}
export default class KeyQueue {
constructor(keymap = DEFAULT_KEYMAP) {
this.data = [];
this.keymap = keymap;
}
push(key) {
this.data.push(key);
let current = this.asKeymapChars();
let filtered = Object.keys(this.keymap).filter((keys) => {
return keys.startsWith(current);
});
if (filtered.length == 0) {
this.data = [];
return null;
} else if (filtered.length === 1 && current === filtered[0]) {
let action = this.keymap[filtered[0]];
this.data = [];
return action;
}
return null;
}
asKeymapChars() {
return this.data.map((k) => {
let c = String.fromCharCode(k.code);
if (k.ctrl) {
return '<C-' + c.toUpperCase() + '>';
} else {
return c
}
}).join('');
}
asCaretChars() {
return this.data.map((k) => {
let c = String.fromCharCode(k.code);
if (k.ctrl) {
return '^' + c.toUpperCase();
} else {
return c;
}
}).join('');
}
}