Get rid of intermediate view model

This commit is contained in:
Danila Fedorin 2021-07-21 11:47:52 -07:00
parent d33d55376a
commit 7adb0e5ddc
2 changed files with 27 additions and 34 deletions

View file

@ -155,7 +155,7 @@ export class RoomViewModel extends ViewModel {
this._room.join(); this._room.join();
} }
async _sendMessage(message, replyingTo) { async _sendMessage(message, reply) {
if (!this._room.isArchived && message) { if (!this._room.isArchived && message) {
try { try {
let msgtype = "m.text"; let msgtype = "m.text";
@ -164,8 +164,8 @@ export class RoomViewModel extends ViewModel {
msgtype = "m.emote"; msgtype = "m.emote";
} }
const content = {msgtype, body: message}; const content = {msgtype, body: message};
if (replyingTo) { if (reply) {
content["m.relates_to"] = replyingTo.reply(); content["m.relates_to"] = reply;
} }
await this._room.sendEvent("m.room.message", content); await this._room.sendEvent("m.room.message", content);
} catch (err) { } catch (err) {
@ -302,7 +302,7 @@ export class RoomViewModel extends ViewModel {
startReply(entry) { startReply(entry) {
if (!this._room.isArchived) { if (!this._room.isArchived) {
this._composerVM.startReply(entry); this._composerVM.setReplyingTo(entry);
} }
} }
} }
@ -311,11 +311,24 @@ class ComposerViewModel extends ViewModel {
constructor(roomVM) { constructor(roomVM) {
super(); super();
this._roomVM = roomVM; this._roomVM = roomVM;
this._replyVM = new ReplyViewModel(roomVM); this._isEmpty = true;
this._replyVM = null;
} }
startReply(entry) { setReplyingTo(tile) {
this._replyVM.setReplyingTo(entry); const changed = this._replyVM !== tile;
this._replyVM = tile;
if (changed) {
this.emitChange("replyViewModel");
}
}
clearReplyingTo() {
this.setReplyingTo(null);
}
get replyViewModel() {
return this._replyVM;
} }
get isEncrypted() { get isEncrypted() {
@ -323,11 +336,11 @@ class ComposerViewModel extends ViewModel {
} }
sendMessage(message) { sendMessage(message) {
const success = this._roomVM._sendMessage(message, this._replyVM.replyingTo); const success = this._roomVM._sendMessage(message, this._replyVM?.reply());
if (success) { if (success) {
this._isEmpty = true; this._isEmpty = true;
this.emitChange("canSend"); this.emitChange("canSend");
this._replyVM.clearReply(); this.clearReplyingTo();
} }
return success; 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) { function imageToInfo(image) {
return { return {
w: image.width, w: image.width,

View file

@ -107,7 +107,11 @@ export class BaseMessageTile extends SimpleTile {
} }
startReply() { startReply() {
this._roomVM.startReply(this._entry); this._roomVM.startReply(this);
}
reply() {
return this._entry.reply();
} }
redact(reason, log) { redact(reason, log) {