Delete key removes message when focused

This commit is contained in:
2024-08-30 23:28:05 +02:00
parent ce895a6716
commit cd2f32ae02
4 changed files with 47 additions and 40 deletions

View File

@@ -85,37 +85,37 @@ export class MainView extends View {
public onCreate(): void {
this.settingsButton = new Button("Settings")
.setPosition(0, 0, 10, 10)
.onClick(() => this.openSettingsDialog());
.setPosition(0, 0, 10, 10)
.onClick(() => this.openSettingsDialog());
this.channelSwitcher = new Dropdown("Channel", [])
.setPosition(30, 10, 30, 10);
.setPosition(30, 10, 30, 10);
this.channelInfoButton = new Button("Channel info")
.setPosition(60, 10, 30, 10);
.setPosition(60, 10, 30, 10);
this.searchButton = new Button("Search")
.setPosition(90, 10, 10, 10)
.onClick(async () => this.openSearchDialog());
.setPosition(90, 10, 10, 10)
.onClick(async () => this.openSearchDialog());
this.fileInput = new FileInput("Upload file")
.setPosition(0, 90, 15, 10);
.setPosition(0, 90, 15, 10);
this.imageInput = new Button("Image")
.setPosition(15, 90, 15, 10);
.setPosition(15, 90, 15, 10);
this.messageInput = new MultilineInput("New message")
.setPosition(30, 90, 60, 10);
.setPosition(30, 90, 60, 10);
this.messageInput.getElement().autofocus = true;
this.voiceMessageInput = new Button("Voice message")
.setPosition(70, 90, 30, 10);
.setPosition(70, 90, 30, 10);
this.messageList = new List("Messages")
.setPosition(30, 30, 60, 50);
.setPosition(30, 30, 60, 50);
this.window.add(this.settingsButton)
.add(this.channelSwitcher)
.add(this.channelInfoButton)
.add(this.searchButton)
.add(this.messageList)
.add(this.messageInput)
.add(this.fileInput)
.add(this.imageInput)
.add(this.voiceMessageInput);
.add(this.channelSwitcher)
.add(this.channelInfoButton)
.add(this.searchButton)
.add(this.messageList)
.add(this.messageInput)
.add(this.fileInput)
.add(this.imageInput)
.add(this.voiceMessageInput);
this.channelSwitcher.getElement().addEventListener("change", (e) => this.handleChannelSwitcher(e));
this.voiceMessageInput.onClick(async () => this.handleVoiceMessageButton());
@@ -228,6 +228,11 @@ export class MainView extends View {
itm.onClick(() => {
this.openMessageDialog(message);
})
itm.onKeyDown((key: string, alt: boolean | undefined, shift: boolean | undefined, ctrl: boolean | undefined) => {
if (key === "Delete") {
this.removeMessage(message.id);
}
});
return itm;
}
@@ -563,7 +568,7 @@ export class MainView extends View {
}
state.save();
this.updateChannelList();
});
});
}
private async handleImageButton() {
@@ -579,7 +584,7 @@ export class MainView extends View {
this.hotkeyMap.set("v", () => this.handleVoiceMessageButton());
this.hotkeyMap.set(" ", () => this.messageInput.focus());
}
private handleHotkey(e: KeyboardEvent) {
if (e.ctrlKey && e.shiftKey) {
const action = this.hotkeyMap.get(e.key.toLowerCase());
@@ -593,7 +598,6 @@ export class MainView extends View {
private async openMessageDialog(message: IMessage) {
const d = new MessageDialog(message);
const msg = await d.open();
console.log(msg);
if (!msg || msg === null) {
state.currentChannel?.removeMessage(message.id);
const node = this.messageElementMap.get(message.id);
@@ -604,4 +608,17 @@ export class MainView extends View {
state.save();
}
}
private async removeMessage(id: number) {
if (state.currentChannel) {
await API.deleteMessage(state.currentChannel.id.toString(), id.toString());
state.currentChannel.removeMessage(id);
const node = this.messageElementMap.get(id);
if (node) {
this.messageList.remove(node);
this.messageElementMap.delete(id);
state.save();
}
}
}
}