Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
release cb43c9e7
Browse files Browse the repository at this point in the history
  • Loading branch information
runner authored and runner committed Oct 12, 2023
1 parent 6e567f6 commit bbe32f0
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 9 deletions.
18 changes: 18 additions & 0 deletions Sources/WysiwygComposer/Components/ComposerModelWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ protocol ComposerModelWrapperProtocol {
func setContentFromHtml(html: String) -> ComposerUpdate
func setContentFromMarkdown(markdown: String) -> ComposerUpdate
func getContentAsHtml() -> String
func getContentAsMessageHtml() -> String
func getContentAsMarkdown() -> String
func getContentAsMessageMarkdown() -> String
func getContentAsPlainText() -> String
func clear() -> ComposerUpdate
func select(startUtf16Codeunit: UInt32, endUtf16Codeunit: UInt32) -> ComposerUpdate
Expand Down Expand Up @@ -78,10 +80,18 @@ final class ComposerModelWrapper: ComposerModelWrapperProtocol {
model.getContentAsHtml()
}

func getContentAsMessageHtml() -> String {
model.getContentAsMessageHtml()
}

func getContentAsMarkdown() -> String {
model.getContentAsMarkdown()
}

func getContentAsMessageMarkdown() -> String {
model.getContentAsMessageMarkdown()
}

func getContentAsPlainText() -> String {
model.getContentAsPlainText()
}
Expand Down Expand Up @@ -129,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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public class WysiwygComposerViewModel: WysiwygComposerViewModelProtocol, Observa
if plainTextMode {
_ = model.setContentFromMarkdown(markdown: computeMarkdownContent())
}
return WysiwygComposerContent(markdown: model.getContentAsMarkdown(),
html: model.getContentAsHtml())
return WysiwygComposerContent(markdown: model.getContentAsMessageMarkdown(),
html: model.getContentAsMessageHtml())
}

// MARK: - Private
Expand Down Expand Up @@ -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.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protocol WysiwygTextViewDelegate: AnyObject {
}

/// A markdown protocol used to provide additional context to the text view when displaying mentions through the text attachment provider
public protocol MentionDisplayHelper {}
public protocol MentionDisplayHelper { }

public class WysiwygTextView: UITextView {
/// Internal delegate for the text view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ extension WysiwygComposerViewModelTests {
XCTAssertEqual(
viewModel.content.html,
"""
<a data-mention-type="user" href="https://matrix.to/#/@alice:matrix.org" contenteditable="false">Alice</a>\u{00A0}
<a href="https://matrix.to/#/@alice:matrix.org">Alice</a>\u{00A0}
"""
)
}

func testAtRoomSuggestionCanBeUsed() {
_ = viewModel.replaceText(range: .zero, replacementText: "@ro")
viewModel.setAtRoomMention()
XCTAssertEqual(
viewModel.content.html,
"""
@room\u{00A0}
"""
)
}
Expand All @@ -68,10 +79,25 @@ extension WysiwygComposerViewModelTests {
XCTAssertEqual(
viewModel.content.html,
"""
Text<a data-mention-type="user" href="https://matrix.to/#/@alice:matrix.org" contenteditable="false">Alice</a>\u{00A0}
Text<a href="https://matrix.to/#/@alice:matrix.org">Alice</a>\u{00A0}
"""
)
}

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")
Expand All @@ -81,7 +107,20 @@ extension WysiwygComposerViewModelTests {
XCTAssertEqual(
viewModel.content.html,
"""
<a data-mention-type="user" href="https://matrix.to/#/@alice:matrix.org" contenteditable="false">Alice</a>Text
<a href="https://matrix.to/#/@alice:matrix.org">Alice</a>Text
"""
)
}

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
"""
)
}
Expand All @@ -92,7 +131,7 @@ extension WysiwygComposerViewModelTests {
XCTAssertEqual(
viewModel.content.html,
"""
<a data-mention-type="room" href="https://matrix.to/#/#room1:matrix.org" contenteditable="false">Room 1</a>\u{00A0}
<a href="https://matrix.to/#/#room1:matrix.org">#room1:matrix.org</a>\u{00A0}
"""
)
}
Expand All @@ -104,7 +143,7 @@ extension WysiwygComposerViewModelTests {
XCTAssertEqual(
viewModel.content.html,
"""
Text<a data-mention-type="room" href="https://matrix.to/#/#room1:matrix.org" contenteditable="false">Room 1</a>\u{00A0}
Text<a href="https://matrix.to/#/#room1:matrix.org">#room1:matrix.org</a>\u{00A0}
"""
)
}
Expand All @@ -116,7 +155,7 @@ extension WysiwygComposerViewModelTests {
XCTAssertEqual(
viewModel.content.html,
"""
<a data-mention-type="room" href="https://matrix.to/#/#room1:matrix.org" contenteditable="false">Room 1</a>Text
<a href="https://matrix.to/#/#room1:matrix.org">#room1:matrix.org</a>Text
"""
)
}
Expand Down
34 changes: 34 additions & 0 deletions Tests/WysiwygComposerTests/WysiwygComposerTests+Suggestions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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("<a data-mention-type=\"at-room\" href=\"#\" contenteditable=\"false\">@room</a> ")
.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 <a data-mention-type=\"at-room\" href=\"#\" contenteditable=\"false\">@room</a> ")
.assertSelection(start: 8, end: 8)
}

func testSuggestionForHashPattern() {
let model = ComposerModelWrapper()
Expand Down

0 comments on commit bbe32f0

Please sign in to comment.