From 7adb0e5ddcbafba7e424ccdb09862e90f678d014 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 21 Jul 2021 11:47:52 -0700 Subject: [PATCH] Get rid of intermediate view model --- src/domain/session/room/RoomViewModel.js | 55 ++++++++----------- .../room/timeline/tiles/BaseMessageTile.js | 6 +- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/domain/session/room/RoomViewModel.js b/src/domain/session/room/RoomViewModel.js index 3b0d61e1..52613002 100644 --- a/src/domain/session/room/RoomViewModel.js +++ b/src/domain/session/room/RoomViewModel.js @@ -155,7 +155,7 @@ export class RoomViewModel extends ViewModel { this._room.join(); } - async _sendMessage(message, replyingTo) { + async _sendMessage(message, reply) { if (!this._room.isArchived && message) { try { let msgtype = "m.text"; @@ -164,8 +164,8 @@ export class RoomViewModel extends ViewModel { msgtype = "m.emote"; } const content = {msgtype, body: message}; - if (replyingTo) { - content["m.relates_to"] = replyingTo.reply(); + if (reply) { + content["m.relates_to"] = reply; } await this._room.sendEvent("m.room.message", content); } catch (err) { @@ -302,7 +302,7 @@ export class RoomViewModel extends ViewModel { startReply(entry) { if (!this._room.isArchived) { - this._composerVM.startReply(entry); + this._composerVM.setReplyingTo(entry); } } } @@ -311,11 +311,24 @@ class ComposerViewModel extends ViewModel { constructor(roomVM) { super(); this._roomVM = roomVM; - this._replyVM = new ReplyViewModel(roomVM); + this._isEmpty = true; + this._replyVM = null; } - startReply(entry) { - this._replyVM.setReplyingTo(entry); + setReplyingTo(tile) { + const changed = this._replyVM !== tile; + this._replyVM = tile; + if (changed) { + this.emitChange("replyViewModel"); + } + } + + clearReplyingTo() { + this.setReplyingTo(null); + } + + get replyViewModel() { + return this._replyVM; } get isEncrypted() { @@ -323,11 +336,11 @@ class ComposerViewModel extends ViewModel { } sendMessage(message) { - const success = this._roomVM._sendMessage(message, this._replyVM.replyingTo); + const success = this._roomVM._sendMessage(message, this._replyVM?.reply()); if (success) { this._isEmpty = true; this.emitChange("canSend"); - this._replyVM.clearReply(); + this.clearReplyingTo(); } return success; } @@ -364,30 +377,6 @@ class ComposerViewModel extends ViewModel { } } -class ReplyViewModel extends ViewModel { - constructor(roomVM) { - super(); - this._roomVM = roomVM; - this._replyingTo = null; - } - - setReplyingTo(entry) { - const changed = this._replyingTo !== entry; - this._replyingTo = entry; - if (changed) { - this.emitChange("replyingTo"); - } - } - - clearReply() { - this.setReplyingTo(null); - } - - get replyingTo() { - return this._replyingTo; - } -} - function imageToInfo(image) { return { w: image.width, diff --git a/src/domain/session/room/timeline/tiles/BaseMessageTile.js b/src/domain/session/room/timeline/tiles/BaseMessageTile.js index 01d32a9c..0cff2793 100644 --- a/src/domain/session/room/timeline/tiles/BaseMessageTile.js +++ b/src/domain/session/room/timeline/tiles/BaseMessageTile.js @@ -107,7 +107,11 @@ export class BaseMessageTile extends SimpleTile { } startReply() { - this._roomVM.startReply(this._entry); + this._roomVM.startReply(this); + } + + reply() { + return this._entry.reply(); } redact(reason, log) {