2024-09-28 15:43:23 -04:00
|
|
|
import { ReceiptType, Unread, BufferType, receiptFromMessage } from "./state.js";
|
2020-07-23 03:58:05 -04:00
|
|
|
|
2021-05-31 13:21:54 -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);
|
2021-05-31 13:21:54 -04:00
|
|
|
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,
|
2021-08-25 02:47:35 -04:00
|
|
|
prevReadReceipt: null,
|
|
|
|
});
|
|
|
|
|
2022-02-12 04:05:58 -05:00
|
|
|
let receipts = {};
|
|
|
|
if (buf.messages.length > 0) {
|
|
|
|
let lastMsg = buf.messages[buf.messages.length - 1];
|
|
|
|
receipts[ReceiptType.READ] = receiptFromMessage(lastMsg);
|
|
|
|
}
|
|
|
|
|
2021-08-25 02:47:35 -04:00
|
|
|
let client = app.clients.get(buf.server);
|
|
|
|
app.bufferStore.put({
|
|
|
|
name: buf.name,
|
|
|
|
server: client.params,
|
|
|
|
unread: Unread.NONE,
|
2022-02-12 04:05:58 -05:00
|
|
|
receipts,
|
2020-07-23 03:58:05 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return { buffers };
|
2024-04-25 10:47:53 -04:00
|
|
|
}, () => {
|
|
|
|
app.updateDocumentTitle();
|
2020-07-23 03:58:05 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2020-07-23 04:12:57 -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()) {
|
2021-05-31 07:24:23 -04:00
|
|
|
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;
|
2020-07-23 04:12:57 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 07:24:23 -04:00
|
|
|
if (!target) {
|
|
|
|
target = firstServerBuffer;
|
|
|
|
}
|
|
|
|
if (target) {
|
|
|
|
app.switchBuffer(target);
|
|
|
|
}
|
2020-07-23 04:12:57 -04:00
|
|
|
},
|
|
|
|
},
|
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);
|
2021-05-31 13:21:54 -04:00
|
|
|
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);
|
2021-05-31 13:21:54 -04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|