Add keybinding infrastructure

This commit is contained in:
Simon Ser 2020-07-23 09:58:05 +02:00
parent 4d3a1548fe
commit 2951c7810f
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 59 additions and 6 deletions

50
keybindings.js Normal file
View file

@ -0,0 +1,50 @@
import { ReceiptType, Unread } from "/state.js";
export const keybindings = [
{
key: "h",
altKey: true,
description: "Mark all messages as read",
execute: (app) => {
app.setState((state) => {
var buffers = new Map();
state.buffers.forEach((buf) => {
if (buf.messages.length > 0) {
var lastMsg = buf.messages[buf.messages.length - 1];
app.setReceipt(buf.name, ReceiptType.READ, lastMsg);
}
buffers.set(buf.name, {
...buf,
unread: Unread.NONE,
});
});
return { buffers };
});
},
},
];
export function setup(app) {
var byKey = {};
keybindings.forEach((binding) => {
if (!byKey[binding.key]) {
byKey[binding.key] = [];
}
byKey[binding.key].push(binding);
});
window.addEventListener("keydown", (event) => {
var candidates = byKey[event.key];
if (!candidates) {
return;
}
candidates = candidates.filter((binding) => {
return !!binding.altKey == event.altKey && !!binding.ctrlKey == event.ctrlKey;
});
if (candidates.length != 1) {
return;
}
event.preventDefault();
candidates[0].execute(app);
});
}