gamja/keybindings.js

133 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-09-28 15:43:23 -04:00
import { ReceiptType, Unread, BufferType, receiptFromMessage } from "./state.js";
2020-07-23 03:58:05 -04:00
function getSiblingBuffer(buffers, bufID, delta) {
2021-06-10 12:11:11 -04:00
let bufList = Array.from(buffers.values());
let i = bufList.findIndex((buf) => buf.id === bufID);
if (i < 0) {
return null;
}
i = (i + bufList.length + delta) % bufList.length;
return bufList[i];
}
2020-07-23 03:58:05 -04:00
export const keybindings = [
{
key: "h",
altKey: true,
description: "Mark all messages as read",
execute: (app) => {
app.setState((state) => {
2021-06-10 12:11:11 -04:00
let buffers = new Map();
2020-07-23 03:58:05 -04:00
state.buffers.forEach((buf) => {
2021-05-31 04:46:41 -04:00
buffers.set(buf.id, {
2020-07-23 03:58:05 -04:00
...buf,
unread: Unread.NONE,
prevReadReceipt: null,
});
let receipts = {};
if (buf.messages.length > 0) {
let lastMsg = buf.messages[buf.messages.length - 1];
receipts[ReceiptType.READ] = receiptFromMessage(lastMsg);
}
let client = app.clients.get(buf.server);
app.bufferStore.put({
name: buf.name,
server: client.params,
unread: Unread.NONE,
receipts,
2020-07-23 03:58:05 -04:00
});
});
return { buffers };
}, () => {
app.updateDocumentTitle();
2020-07-23 03:58:05 -04:00
});
},
},
{
key: "a",
altKey: true,
description: "Jump to next buffer with activity",
execute: (app) => {
2021-05-31 12:26:04 -04:00
// TODO: order by age if same priority
2021-06-10 12:11:11 -04:00
let firstServerBuffer = null;
let target = null;
for (let buf of app.state.buffers.values()) {
if (!firstServerBuffer && buf.type === BufferType.SERVER) {
firstServerBuffer = buf;
}
2021-05-31 12:26:04 -04:00
if (buf.unread === Unread.NONE) {
continue;
}
if (!target || Unread.compare(buf.unread, target.unread) > 0) {
2021-01-21 16:15:33 -05:00
target = buf;
}
}
if (!target) {
target = firstServerBuffer;
}
if (target) {
app.switchBuffer(target);
}
},
},
2020-08-03 09:49:30 -04:00
{
key: "ArrowUp",
altKey: true,
description: "Jump to the previous buffer",
execute: (app) => {
2021-06-10 12:11:11 -04:00
let prev = getSiblingBuffer(app.state.buffers, app.state.activeBuffer, -1);
if (prev) {
app.switchBuffer(prev);
2020-08-03 09:49:30 -04:00
}
},
},
{
key: "ArrowDown",
altKey: true,
description: "Jump to the next buffer",
execute: (app) => {
2021-06-10 12:11:11 -04:00
let next = getSiblingBuffer(app.state.buffers, app.state.activeBuffer, 1);
if (next) {
app.switchBuffer(next);
2020-08-03 09:49:30 -04:00
}
},
},
2023-06-08 09:07:28 -04:00
{
key: "k",
ctrlKey: true,
description: "Switch to a buffer",
execute: (app) => {
app.openDialog("switch");
},
},
2020-07-23 03:58:05 -04:00
];
export function setup(app) {
2021-06-10 12:11:11 -04:00
let byKey = {};
2020-07-23 03:58:05 -04:00
keybindings.forEach((binding) => {
if (!byKey[binding.key]) {
byKey[binding.key] = [];
}
byKey[binding.key].push(binding);
});
window.addEventListener("keydown", (event) => {
2021-06-10 12:11:11 -04:00
let candidates = byKey[event.key];
2020-07-23 03:58:05 -04:00
if (!candidates) {
return;
}
candidates = candidates.filter((binding) => {
2024-10-13 18:56:18 -04:00
return !!binding.altKey === event.altKey && !!binding.ctrlKey === event.ctrlKey;
2020-07-23 03:58:05 -04:00
});
2024-10-13 18:56:18 -04:00
if (candidates.length !== 1) {
2020-07-23 03:58:05 -04:00
return;
}
event.preventDefault();
candidates[0].execute(app);
});
}