Take a buffer ID in switchBuffer

This commit is contained in:
Simon Ser 2021-01-21 22:15:33 +01:00
parent dd35f1df95
commit ffed801b0e
3 changed files with 39 additions and 27 deletions

View file

@ -72,10 +72,13 @@ export default {
},
"buffer": (app, args) => {
var name = args[0];
if (!app.state.buffers.has(name)) {
throw new Error("Unknown buffer");
for (var buf of app.state.buffers.values()) {
if (buf.name === name) {
app.switchBuffer(buf);
return;
}
app.switchBuffer(name);
}
throw new Error("Unknown buffer");
},
"topic": (app, args) => {
var channel = app.state.activeBuffer;

View file

@ -104,6 +104,10 @@ function getBuffer(state, id) {
case "number":
return state.buffers.get(id);
case "object":
if (id.id) {
return state.buffers.get(id.id);
}
var network = id.network, name = id.name;
if (!network) {
network = state.activeNetwork;
@ -284,30 +288,35 @@ export default class App extends Component {
});
}
switchBuffer(name) {
var lastReadReceipt = this.getReceipt(name, ReceiptType.READ);
// TODO: only mark as read if user scrolled at the bottom
this.setBufferState({ name }, {
unread: Unread.NONE,
lastReadReceipt,
});
switchBuffer(id) {
var buf;
this.setState((state) => {
buf = getBuffer(state, { name });
buf = getBuffer(state, id);
if (!buf) {
return;
}
return { activeBuffer: buf.id };
}, () => {
if (!buf) {
return;
}
var lastReadReceipt = this.getReceipt(buf.name, ReceiptType.READ);
// TODO: only mark as read if user scrolled at the bottom
this.setBufferState(buf.id, {
unread: Unread.NONE,
lastReadReceipt,
});
if (this.composer.current) {
this.composer.current.focus();
}
if (!buf || buf.messages.length == 0) {
if (buf.messages.length == 0) {
return;
}
var lastMsg = buf.messages[buf.messages.length - 1];
this.setReceipt(name, ReceiptType.READ, lastMsg);
this.setReceipt(buf.name, ReceiptType.READ, lastMsg);
});
}
@ -387,7 +396,7 @@ export default class App extends Component {
});
notif.addEventListener("click", () => {
// TODO: scroll to message
this.switchBuffer(target);
this.switchBuffer({ network: netID, name: target });
});
}
}
@ -450,7 +459,7 @@ export default class App extends Component {
});
this.createBuffer(netID, SERVER_BUFFER);
this.switchBuffer(SERVER_BUFFER);
this.switchBuffer({ network: netID, name: SERVER_BUFFER });
}
handleClose(netID) {
@ -584,7 +593,7 @@ export default class App extends Component {
}
if (channel == this.state.connectParams.autojoin[0]) {
// TODO: only switch once right after connect
this.switchBuffer(channel);
this.switchBuffer({ network: netID, name: channel });
}
var receipt = this.getReceipt(channel, ReceiptType.READ);
@ -624,7 +633,7 @@ export default class App extends Component {
var members = new Map(buf.members);
members.delete(msg.prefix.name);
var offline = buf.name == msg.prefix.name;
buffers.set(buf.name, { ...buf, members, offline });
buffers.set(buf.id, { ...buf, members, offline });
affectedBuffers.push(buf.name);
});
return { buffers };
@ -644,7 +653,7 @@ export default class App extends Component {
var members = new Map(buf.members);
members.set(newNick, members.get(msg.prefix.name));
members.delete(msg.prefix.name);
buffers.set(buf.name, { ...buf, members });
buffers.set(buf.id, { ...buf, members });
affectedBuffers.push(buf.name);
});
return { buffers };
@ -707,7 +716,7 @@ export default class App extends Component {
this.client.send({ command: "WHO", params: [target] });
}
this.createBuffer(this.state.activeNetwork, target);
this.switchBuffer(target);
this.switchBuffer({ name: target });
}
close(target) {
@ -724,7 +733,7 @@ export default class App extends Component {
this.client.send({ command: "PART", params: [target] });
}
this.switchBuffer(SERVER_BUFFER);
this.switchBuffer({ name: SERVER_BUFFER });
this.setState((state) => {
var buffers = new Map(state.buffers);
buffers.delete(target);
@ -789,7 +798,7 @@ export default class App extends Component {
}
handleBufferListClick(name) {
this.switchBuffer(name);
this.switchBuffer({ name });
}
handleJoinClick(event) {

View file

@ -28,10 +28,10 @@ export const keybindings = [
description: "Jump to next buffer with activity",
execute: (app) => {
// TODO: order by priority, then by age
var target = SERVER_BUFFER;
var target = { name: SERVER_BUFFER };
for (var buf of app.state.buffers.values()) {
if (buf.unread != Unread.NONE) {
target = buf.name;
target = buf;
break;
}
}
@ -45,9 +45,9 @@ export const keybindings = [
execute: (app) => {
var prev = null;
for (var buf of app.state.buffers.values()) {
if (app.state.activeBuffer == buf.name) {
if (app.state.activeBuffer == buf.id) {
if (prev) {
app.switchBuffer(prev.name);
app.switchBuffer(prev);
}
break;
}
@ -63,9 +63,9 @@ export const keybindings = [
var found = false;
for (var buf of app.state.buffers.values()) {
if (found) {
app.switchBuffer(buf.name);
app.switchBuffer(buf);
break;
} else if (app.state.activeBuffer == buf.name) {
} else if (app.state.activeBuffer == buf.id) {
found = true;
}
}