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

[iOS] Add "shouldReplaceText" API #857

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public class WysiwygComposerViewModel: WysiwygComposerViewModelProtocol, Observa
}
}

/// Whether the composer should take any keyboard input.
/// When set to `false`, `replaceText(range:replacementText:)` returns `false` as well.
public var shouldReplaceText = true

/// Published value for the composer plain text mode.
@Published public var plainTextMode = false {
didSet {
Expand Down Expand Up @@ -289,6 +293,10 @@ public extension WysiwygComposerViewModel {
}

func replaceText(range: NSRange, replacementText: String) -> Bool {
guard shouldReplaceText else {
return false
}

guard !plainTextMode else {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ import Combine
import XCTest

final class WysiwygComposerViewModelTests: XCTestCase {
let viewModel = WysiwygComposerViewModel()
var viewModel: WysiwygComposerViewModel!

override func setUpWithError() throws {
viewModel = WysiwygComposerViewModel()
viewModel.clearContent()
}

override func tearDownWithError() throws {
viewModel.plainTextMode = false
}

func testIsContentEmpty() throws {
XCTAssertTrue(viewModel.isContentEmpty)

Expand All @@ -51,6 +48,13 @@ final class WysiwygComposerViewModelTests: XCTestCase {
XCTAssertTrue(shouldChange)
}

func testSimpleTextInputIsNotAccepted() throws {
viewModel.shouldReplaceText = false
let shouldChange = viewModel.replaceText(range: .zero,
replacementText: "A")
XCTAssertFalse(shouldChange)
}

func testNewlineIsNotAccepted() throws {
let shouldChange = viewModel.replaceText(range: .zero,
replacementText: "\n")
Expand Down
Loading