mirror of
https://codeberg.org/emersion/gamja.git
synced 2024-11-14 19:05:01 -05:00
Add keybinding infrastructure
This commit is contained in:
parent
4d3a1548fe
commit
2951c7810f
3 changed files with 59 additions and 6 deletions
|
@ -8,17 +8,13 @@ import Connect from "/components/connect.js";
|
|||
import Composer from "/components/composer.js";
|
||||
import ScrollManager from "/components/scroll-manager.js";
|
||||
import { html, Component, createRef } from "/lib/index.js";
|
||||
import { SERVER_BUFFER, BufferType, Status, Unread } from "/state.js";
|
||||
import { SERVER_BUFFER, BufferType, ReceiptType, Status, Unread } from "/state.js";
|
||||
import commands from "/commands.js";
|
||||
import { setup as setupKeybindings } from "/keybindings.js";
|
||||
|
||||
const CHATHISTORY_PAGE_SIZE = 100;
|
||||
const CHATHISTORY_MAX_SIZE = 4000;
|
||||
|
||||
const ReceiptType = {
|
||||
DELIVERED: "delivered",
|
||||
READ: "read",
|
||||
};
|
||||
|
||||
var messagesCount = 0;
|
||||
|
||||
function parseQueryString() {
|
||||
|
@ -758,6 +754,8 @@ export default class App extends Component {
|
|||
if (this.state.connectParams.autoconnect) {
|
||||
this.connect(this.state.connectParams);
|
||||
}
|
||||
|
||||
setupKeybindings(this);
|
||||
}
|
||||
|
||||
render() {
|
||||
|
|
50
keybindings.js
Normal file
50
keybindings.js
Normal 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);
|
||||
});
|
||||
}
|
5
state.js
5
state.js
|
@ -27,6 +27,11 @@ export const Unread = {
|
|||
},
|
||||
};
|
||||
|
||||
export const ReceiptType = {
|
||||
DELIVERED: "delivered",
|
||||
READ: "read",
|
||||
};
|
||||
|
||||
export function getNickURL(nick) {
|
||||
return "irc:///" + encodeURIComponent(nick) + ",isnick";
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue