From f1acb4712c052bd74894119103c425c748ae2e06 Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Fri, 14 Oct 2022 17:15:12 +0200 Subject: [PATCH] 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 Co-authored-by: Benoit Marty --- changelog.d/7359.bugfix | 1 + changelog.d/7359.sdk | 1 + .../api/session/room/timeline/TimelineEvent.kt | 16 ++++++---------- .../detail/composer/MessageComposerViewModel.kt | 5 +++-- 4 files changed, 11 insertions(+), 12 deletions(-) create mode 100644 changelog.d/7359.bugfix create mode 100644 changelog.d/7359.sdk diff --git a/changelog.d/7359.bugfix b/changelog.d/7359.bugfix new file mode 100644 index 0000000000..98e29fb697 --- /dev/null +++ b/changelog.d/7359.bugfix @@ -0,0 +1 @@ +Fix editing formatted messages with plain text editor diff --git a/changelog.d/7359.sdk b/changelog.d/7359.sdk new file mode 100644 index 0000000000..c78c591d67 --- /dev/null +++ b/changelog.d/7359.sdk @@ -0,0 +1 @@ +Allow getting the formatted or plain text body of a message for the fun `TimelineEvent.getTextEditableContent()`. diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineEvent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineEvent.kt index 7341fd922e..223acd1b9c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineEvent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineEvent.kt @@ -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 - } -} diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt index b877c2979b..5d3465ab2e 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt @@ -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 {