From eba3e2537364174e809d363c32169575e5655773 Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Thu, 21 Sep 2023 15:45:43 +0200 Subject: [PATCH] Improve message edit with RTE (#1742) * Improv edit for RTE * Refine code block style in composer * Update matrix-wysiwyg-composer-swift to 2.13.0 * Remove workaround * Update ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/TextRoomTimelineItemContent.swift Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com> * Refactor rawFormattedBody -> formattedBodyHTMLString --------- Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com> --- ElementX.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 4 ++-- .../ComposerToolbar/ComposerToolbarViewModel.swift | 2 +- .../Screens/HomeScreen/View/HomeScreen.swift | 4 ++-- .../Screens/RoomScreen/RoomScreenCoordinator.swift | 4 ++-- .../Screens/RoomScreen/RoomScreenViewModel.swift | 14 +++++++++++++- .../Messages/TextRoomTimelineItemContent.swift | 2 ++ .../TimelineItems/RoomTimelineItemFactory.swift | 2 +- UnitTests/Sources/LocalizationTests.swift | 5 +++++ project.yml | 2 +- 10 files changed, 30 insertions(+), 11 deletions(-) diff --git a/ElementX.xcodeproj/project.pbxproj b/ElementX.xcodeproj/project.pbxproj index 479f56e96..096c49927 100644 --- a/ElementX.xcodeproj/project.pbxproj +++ b/ElementX.xcodeproj/project.pbxproj @@ -5754,7 +5754,7 @@ repositoryURL = "https://github.com/matrix-org/matrix-wysiwyg-composer-swift"; requirement = { kind = exactVersion; - version = 2.12.0; + version = 2.13.0; }; }; 96495DD8554E2F39D3954354 /* XCRemoteSwiftPackageReference "posthog-ios" */ = { diff --git a/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 88c4607b7..eede5be74 100644 --- a/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -138,8 +138,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/matrix-org/matrix-wysiwyg-composer-swift", "state" : { - "revision" : "b6c512d64ef9fd1fba9eb15b247c165377a1c325", - "version" : "2.12.0" + "revision" : "b30b7a0f6065907c42c56bd7a87ea4bc9e23a116", + "version" : "2.13.0" } }, { diff --git a/ElementX/Sources/Screens/ComposerToolbar/ComposerToolbarViewModel.swift b/ElementX/Sources/Screens/ComposerToolbar/ComposerToolbarViewModel.swift index a45ceb783..84b163973 100644 --- a/ElementX/Sources/Screens/ComposerToolbar/ComposerToolbarViewModel.swift +++ b/ElementX/Sources/Screens/ComposerToolbar/ComposerToolbarViewModel.swift @@ -149,7 +149,7 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool } private func set(text: String) { - wysiwygViewModel.setMarkdownContent(text) + wysiwygViewModel.setHtmlContent(text) } private func createLinkAlert() { diff --git a/ElementX/Sources/Screens/HomeScreen/View/HomeScreen.swift b/ElementX/Sources/Screens/HomeScreen/View/HomeScreen.swift index c7decd85a..154e2f359 100644 --- a/ElementX/Sources/Screens/HomeScreen/View/HomeScreen.swift +++ b/ElementX/Sources/Screens/HomeScreen/View/HomeScreen.swift @@ -324,13 +324,13 @@ struct HomeScreen_Previews: PreviewProvider, TestablePreview { HomeScreen(context: loadedViewModel.context) } .previewDisplayName("Loaded") - .snapshot(delay: 2.0) + .snapshot(delay: 4.0) NavigationStack { HomeScreen(context: emptyViewModel.context) } .previewDisplayName("Empty") - .snapshot(delay: 2.0) + .snapshot(delay: 4.0) } static func viewModel(_ state: MockRoomSummaryProviderState) -> HomeScreenViewModel { diff --git a/ElementX/Sources/Screens/RoomScreen/RoomScreenCoordinator.swift b/ElementX/Sources/Screens/RoomScreen/RoomScreenCoordinator.swift index a2fc31b16..18d276b75 100644 --- a/ElementX/Sources/Screens/RoomScreen/RoomScreenCoordinator.swift +++ b/ElementX/Sources/Screens/RoomScreen/RoomScreenCoordinator.swift @@ -141,8 +141,8 @@ private extension HTMLParserStyle { linkColor: UIColor.link, codeBlockStyle: BlockStyle(backgroundColor: UIColor(.compound._bgCodeBlock), borderColor: UIColor(.compound.borderInteractiveSecondary), - borderWidth: 0.0, - cornerRadius: 0.0, + borderWidth: 1.0, + cornerRadius: 2.0, padding: BlockStyle.Padding(horizontal: 10, vertical: 12), type: .background), quoteBlockStyle: BlockStyle(backgroundColor: UIColor(.compound.iconTertiary), diff --git a/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift b/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift index 39616b262..7327aa34d 100644 --- a/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift +++ b/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift @@ -578,7 +578,19 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol return } - actionsSubject.send(.composer(action: .setText(text: messageTimelineItem.body))) + let text: String + switch messageTimelineItem.contentType { + case .text(let textItem): + if ServiceLocator.shared.settings.richTextEditorEnabled, let formattedBodyHTMLString = textItem.formattedBodyHTMLString { + text = formattedBodyHTMLString + } else { + text = messageTimelineItem.body + } + default: + text = messageTimelineItem.body + } + + actionsSubject.send(.composer(action: .setText(text: text))) actionsSubject.send(.composer(action: .setMode(mode: .edit(originalItemId: messageTimelineItem.id)))) case .copyPermalink: do { diff --git a/ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/TextRoomTimelineItemContent.swift b/ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/TextRoomTimelineItemContent.swift index c7f541e97..fff09f07b 100644 --- a/ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/TextRoomTimelineItemContent.swift +++ b/ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/TextRoomTimelineItemContent.swift @@ -19,4 +19,6 @@ import UIKit struct TextRoomTimelineItemContent: Hashable { let body: String var formattedBody: AttributedString? + /// The original textual representation of the formatted body directly from the event (usually HTML code) + var formattedBodyHTMLString: String? } diff --git a/ElementX/Sources/Services/Timeline/TimelineItems/RoomTimelineItemFactory.swift b/ElementX/Sources/Services/Timeline/TimelineItems/RoomTimelineItemFactory.swift index 1dd99f2db..4793a39f6 100644 --- a/ElementX/Sources/Services/Timeline/TimelineItems/RoomTimelineItemFactory.swift +++ b/ElementX/Sources/Services/Timeline/TimelineItems/RoomTimelineItemFactory.swift @@ -424,7 +424,7 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol { let htmlBody = messageContent.formatted?.format == .html ? messageContent.formatted?.body : nil let formattedBody = (htmlBody != nil ? attributedStringBuilder.fromHTML(htmlBody) : attributedStringBuilder.fromPlain(messageContent.body)) - return .init(body: messageContent.body, formattedBody: formattedBody) + return .init(body: messageContent.body, formattedBody: formattedBody, formattedBodyHTMLString: htmlBody) } private func buildAudioTimelineItemContent(_ messageContent: AudioMessageContent) -> AudioRoomTimelineItemContent { diff --git a/UnitTests/Sources/LocalizationTests.swift b/UnitTests/Sources/LocalizationTests.swift index 7e0efcd9a..95a1cd820 100644 --- a/UnitTests/Sources/LocalizationTests.swift +++ b/UnitTests/Sources/LocalizationTests.swift @@ -18,6 +18,11 @@ import XCTest class LocalizationTests: XCTestCase { + override func tearDown() { + super.tearDown() + Bundle.overrideLocalizations = nil + } + /// Test ElementL10n considers app language changes func testAppLanguage() { // set app language to English diff --git a/project.yml b/project.yml index 5ef170764..d0abe0aef 100644 --- a/project.yml +++ b/project.yml @@ -114,4 +114,4 @@ packages: minorVersion: 2.0.0 WysiwygComposer: url: https://github.com/matrix-org/matrix-wysiwyg-composer-swift - exactVersion: 2.12.0 + exactVersion: 2.13.0