mirror of
https://github.com/giuseqpe/vimium-for-obsidian.git
synced 2026-07-22 08:33:35 +00:00
29 lines
1.4 KiB
JavaScript
29 lines
1.4 KiB
JavaScript
export const DEFAULT_BINDINGS = Object.freeze({
|
|
j: "scrollDown", k: "scrollUp", h: "scrollLeft", l: "scrollRight",
|
|
d: "halfPageDown", u: "halfPageUp", gg: "top", G: "bottom",
|
|
f: "hints", F: "hintsNewLeaf", i: "insert", gi: "focusInput",
|
|
"/": "find", n: "findNext", N: "findPrevious", o: "omnibar",
|
|
O: "omnibarNewLeaf", b: "files", B: "filesNewLeaf", T: "leaves",
|
|
H: "historyBack", L: "historyForward", t: "newLeaf", J: "leafLeft",
|
|
K: "leafRight", g0: "firstLeaf", "g$": "lastLeaf", yt: "duplicateLeaf",
|
|
x: "closeLeaf", X: "reopenLeaf", yy: "copyLink", p: "openClipboard",
|
|
P: "openClipboardNewLeaf", r: "reload", "?": "help", Escape: "normal"
|
|
});
|
|
|
|
export class KeyEngine {
|
|
constructor(bindings = DEFAULT_BINDINGS, timeout = 700) {
|
|
this.bindings = bindings; this.timeout = timeout; this.pending = ""; this.at = 0;
|
|
}
|
|
reset() { this.pending = ""; this.at = 0; }
|
|
feed(key, now = Date.now()) {
|
|
if (this.pending && now - this.at > this.timeout) this.reset();
|
|
const candidate = this.pending + key;
|
|
if (this.bindings[candidate]) { const action = this.bindings[candidate]; this.reset(); return { action }; }
|
|
if (Object.keys(this.bindings).some(k => k.startsWith(candidate))) {
|
|
this.pending = candidate; this.at = now; return { pending: candidate };
|
|
}
|
|
this.reset();
|
|
if (this.bindings[key]) return { action: this.bindings[key] };
|
|
return {};
|
|
}
|
|
}
|