Fix editing formatted messages in plain text editor (#7360)

* Fix editing formatted messages in plain text editor

* Update changelog.d/7359.sdk

Co-authored-by: Benoit Marty <benoitm@matrix.org>

Co-authored-by: Benoit Marty <benoitm@matrix.org>
This commit is contained in:
Jorge Martin Espinosa 2022-10-14 17:15:12 +02:00 committed by GitHub
parent 612d2e51e5
commit f1acb4712c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 12 deletions

1
changelog.d/7359.bugfix Normal file
View File

@ -0,0 +1 @@
Fix editing formatted messages with plain text editor

1
changelog.d/7359.sdk Normal file
View File

@ -0,0 +1 @@
Allow getting the formatted or plain text body of a message for the fun `TimelineEvent.getTextEditableContent()`.

View File

@ -181,9 +181,13 @@ fun TimelineEvent.isRootThread(): Boolean {
/**
* Get the latest message body, after a possible edition, stripping the reply prefix if necessary.
*/
fun TimelineEvent.getTextEditableContent(): String {
fun TimelineEvent.getTextEditableContent(formatted: Boolean): String {
val lastMessageContent = getLastMessageContent()
val lastContentBody = lastMessageContent.getFormattedBody() ?: return ""
val lastContentBody = if (formatted && lastMessageContent is MessageContentWithFormattedBody) {
lastMessageContent.formattedBody
} else {
lastMessageContent?.body
} ?: return ""
return if (isReply()) {
extractUsefulTextFromReply(lastContentBody)
} else {
@ -201,11 +205,3 @@ fun MessageContent.getTextDisplayableContent(): String {
?: (this as MessageTextContent?)?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it) }
?: body
}
fun MessageContent?.getFormattedBody(): String? {
return if (this is MessageContentWithFormattedBody) {
formattedBody
} else {
this?.body
}
}

View File

@ -151,7 +151,8 @@ class MessageComposerViewModel @AssistedInject constructor(
private fun handleEnterEditMode(action: MessageComposerAction.EnterEditMode) {
room.getTimelineEvent(action.eventId)?.let { timelineEvent ->
setState { copy(sendMode = SendMode.Edit(timelineEvent, timelineEvent.getTextEditableContent())) }
val formatted = vectorPreferences.isRichTextEditorEnabled()
setState { copy(sendMode = SendMode.Edit(timelineEvent, timelineEvent.getTextEditableContent(formatted))) }
}
}
@ -552,7 +553,7 @@ class MessageComposerViewModel @AssistedInject constructor(
state.sendMode.timelineEvent,
messageContent?.msgType ?: MessageType.MSGTYPE_TEXT,
action.text,
(messageContent as? MessageContentWithFormattedBody)?.formattedBody,
action.formattedText,
action.autoMarkdown
)
} else {