From cb43c9e7f033c58497d961452c7d286a41f57fcb Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Thu, 12 Oct 2023 11:09:10 +0200 Subject: [PATCH] [iOS] expose the set at room mention function (#843) * expose the set at room mention function * code improvement * Unit Tests * more tests --- .../Components/ComposerModelWrapper.swift | 8 ++++ .../WysiwygComposerViewModel.swift | 12 ++++++ ...ygComposerViewModelTests+Suggestions.swift | 39 +++++++++++++++++++ .../WysiwygComposerTests+Suggestions.swift | 34 ++++++++++++++++ 4 files changed, 93 insertions(+) diff --git a/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/ComposerModelWrapper.swift b/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/ComposerModelWrapper.swift index 548741faa..039582f17 100644 --- a/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/ComposerModelWrapper.swift +++ b/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/ComposerModelWrapper.swift @@ -139,6 +139,14 @@ final class ComposerModelWrapper: ComposerModelWrapperProtocol { func insertMentionAtSuggestion(url: String, text: String, suggestion: SuggestionPattern, attributes: [Attribute]) -> ComposerUpdate { execute { try $0.insertMentionAtSuggestion(url: url, text: text, suggestion: suggestion, attributes: attributes) } } + + func insertAtRoomMention() -> ComposerUpdate { + execute { try $0.insertAtRoomMention() } + } + + func insertAtRoomMentionAtSuggestion(_ suggestion: SuggestionPattern) -> ComposerUpdate { + execute { try $0.insertAtRoomMentionAtSuggestion(suggestion: suggestion) } + } func removeLinks() -> ComposerUpdate { execute { try $0.removeLinks() } diff --git a/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/WysiwygComposerView/WysiwygComposerViewModel.swift b/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/WysiwygComposerView/WysiwygComposerViewModel.swift index b636f75f6..486ff80cd 100644 --- a/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/WysiwygComposerView/WysiwygComposerViewModel.swift +++ b/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/WysiwygComposerView/WysiwygComposerViewModel.swift @@ -251,6 +251,18 @@ public extension WysiwygComposerViewModel { applyUpdate(update) hasPendingFormats = true } + + /// Sets the @room mention at the suggestion position + func setAtRoomMention() { + let update: ComposerUpdate + if let suggestionPattern, suggestionPattern.key == .at { + update = model.insertAtRoomMentionAtSuggestion(suggestionPattern) + } else { + update = model.insertAtRoomMention() + } + applyUpdate(update) + hasPendingFormats = true + } /// Set a command with `Slash` pattern. /// diff --git a/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+Suggestions.swift b/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+Suggestions.swift index fc7454764..60266cf31 100644 --- a/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+Suggestions.swift +++ b/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+Suggestions.swift @@ -58,6 +58,17 @@ extension WysiwygComposerViewModelTests { """ ) } + + func testAtRoomSuggestionCanBeUsed() { + _ = viewModel.replaceText(range: .zero, replacementText: "@ro") + viewModel.setAtRoomMention() + XCTAssertEqual( + viewModel.content.html, + """ + @room\u{00A0} + """ + ) + } func testAtMentionWithNoSuggestion() { _ = viewModel.replaceText(range: .zero, replacementText: "Text") @@ -72,6 +83,21 @@ extension WysiwygComposerViewModelTests { """ ) } + + func testAtRoomMentionWithNoSuggestion() { + _ = viewModel.replaceText(range: .zero, replacementText: "Text") + viewModel.select(range: .init(location: 0, length: 4)) + viewModel.setAtRoomMention() + // Text is not removed, and the + // mention is added after the text + XCTAssertEqual( + viewModel.content.html, + """ + Text@room\u{00A0} + """ + ) + } + func testAtMentionWithNoSuggestionAtLeading() { _ = viewModel.replaceText(range: .zero, replacementText: "Text") @@ -85,6 +111,19 @@ extension WysiwygComposerViewModelTests { """ ) } + + func testAtRoomMentionWithNoSuggestionAtLeading() { + _ = viewModel.replaceText(range: .zero, replacementText: "Text") + viewModel.select(range: .init(location: 0, length: 0)) + viewModel.setAtRoomMention() + // Text is not removed, and the mention is added before the text + XCTAssertEqual( + viewModel.content.html, + """ + @roomText + """ + ) + } func testHashSuggestionCanBeUsed() { _ = viewModel.replaceText(range: .zero, replacementText: "#roo") diff --git a/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/WysiwygComposerTests+Suggestions.swift b/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/WysiwygComposerTests+Suggestions.swift index ab85e435e..818df4278 100644 --- a/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/WysiwygComposerTests+Suggestions.swift +++ b/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/WysiwygComposerTests+Suggestions.swift @@ -75,6 +75,40 @@ extension WysiwygComposerTests { ) .assertSelection(start: 8, end: 8) } + + func testSuggestionForAtRoomPattern() { + let model = ComposerModelWrapper() + let update = model.replaceText(newText: "@roo") + + guard case .suggestion(suggestionPattern: let suggestionPattern) = update.menuAction() else { + XCTFail("No user suggestion found") + return + } + + model + .action { + $0.insertAtRoomMentionAtSuggestion(suggestionPattern) + } + .assertHtml("@room ") + .assertSelection(start: 2, end: 2) + } + + func testForNonLeadingSuggestionForAtRoomPattern() { + let model = ComposerModelWrapper() + let update = model.replaceText(newText: "Hello @roo") + + guard case .suggestion(suggestionPattern: let suggestionPattern) = update.menuAction() else { + XCTFail("No user suggestion found") + return + } + + model + .action { + $0.insertAtRoomMentionAtSuggestion(suggestionPattern) + } + .assertHtml("Hello @room ") + .assertSelection(start: 8, end: 8) + } func testSuggestionForHashPattern() { let model = ComposerModelWrapper()