2021-03-02 16:46:48 -05:00
|
|
|
import { ReceiptType, Unread, SERVER_BUFFER } from "./state.js";
|
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) => {
|
|
|
|
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);
|
|
|
|
}
|
2021-05-31 04:46:41 -04:00
|
|
|
buffers.set(buf.id, {
|
2020-07-23 03:58:05 -04:00
|
|
|
...buf,
|
|
|
|
unread: Unread.NONE,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return { buffers };
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2020-07-23 04:12:57 -04:00
|
|
|
{
|
|
|
|
key: "a",
|
|
|
|
altKey: true,
|
|
|
|
description: "Jump to next buffer with activity",
|
|
|
|
execute: (app) => {
|
|
|
|
// TODO: order by priority, then by age
|
2021-01-21 16:15:33 -05:00
|
|
|
var target = { name: SERVER_BUFFER };
|
2020-07-23 04:12:57 -04:00
|
|
|
for (var buf of app.state.buffers.values()) {
|
|
|
|
if (buf.unread != Unread.NONE) {
|
2021-01-21 16:15:33 -05:00
|
|
|
target = buf;
|
2020-07-23 04:12:57 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 09:41:38 -04:00
|
|
|
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) => {
|
|
|
|
var prev = null;
|
|
|
|
for (var buf of app.state.buffers.values()) {
|
2021-01-21 16:15:33 -05:00
|
|
|
if (app.state.activeBuffer == buf.id) {
|
2020-08-03 09:49:30 -04:00
|
|
|
if (prev) {
|
2021-01-21 16:15:33 -05:00
|
|
|
app.switchBuffer(prev);
|
2020-08-03 09:49:30 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = buf;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "ArrowDown",
|
|
|
|
altKey: true,
|
|
|
|
description: "Jump to the next buffer",
|
|
|
|
execute: (app) => {
|
|
|
|
var found = false;
|
|
|
|
for (var buf of app.state.buffers.values()) {
|
|
|
|
if (found) {
|
2021-01-21 16:15:33 -05:00
|
|
|
app.switchBuffer(buf);
|
2020-08-03 09:49:30 -04:00
|
|
|
break;
|
2021-01-21 16:15:33 -05:00
|
|
|
} else if (app.state.activeBuffer == buf.id) {
|
2020-08-03 09:49:30 -04:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-07-23 03:58:05 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|