Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows for monitoring textViewSouldChangeTextInRange #167

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 25 additions & 2 deletions Sources/RichTextKit/Coordinator/RichTextCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ open class RichTextCoordinator: NSObject {
public init(
text: Binding<NSAttributedString>,
textView: RichTextView,
richTextContext: RichTextContext
richTextContext: RichTextContext,
textViewSouldChangeTextInRange: @escaping RichTextEditor.TextViewSouldChangeTextInRange = { (_, _) in return true },
textViewDidChangeSelection: @escaping RichTextEditor.TextViewDidChangeSelection = { }
) {
textView.attributedString = text.wrappedValue
self.text = text
self.textView = textView
self.context = richTextContext
self.textViewSouldChangeTextInRange = textViewSouldChangeTextInRange
self.textViewDidChangeSelection = textViewDidChangeSelection
super.init()
self.textView.delegate = self
subscribeToUserActions()
Expand All @@ -58,6 +62,10 @@ open class RichTextCoordinator: NSObject {
/// The text view for which the coordinator is used.
public private(set) var textView: RichTextView

/// Allows for observing text changes
private var textViewSouldChangeTextInRange: RichTextEditor.TextViewSouldChangeTextInRange
private var textViewDidChangeSelection: RichTextEditor.TextViewDidChangeSelection

/// This set is used to store context observations.
public var cancellables = Set<AnyCancellable>()

Expand All @@ -81,7 +89,13 @@ open class RichTextCoordinator: NSObject {
#if canImport(UIKit)

// MARK: - UITextViewDelegate


open func textView(_ textView: UITextView,
shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool {
return textViewSouldChangeTextInRange(range, text)
}

open func textViewDidBeginEditing(_ textView: UITextView) {
context.isEditingText = true
}
Expand All @@ -92,6 +106,7 @@ open class RichTextCoordinator: NSObject {

open func textViewDidChangeSelection(_ textView: UITextView) {
syncWithTextView()
textViewDidChangeSelection()
}

open func textViewDidEndEditing(_ textView: UITextView) {
Expand All @@ -104,6 +119,12 @@ open class RichTextCoordinator: NSObject {

// MARK: - NSTextViewDelegate

open func textView(_ textView: NSTextView,
shouldChangeTextIn affectedCharRange: NSRange,
replacementString: String?) -> Bool {
return textViewSouldChangeTextInRange(affectedCharRange, replacementString)
}

open func textDidBeginEditing(_ notification: Notification) {
context.isEditingText = true
}
Expand All @@ -114,11 +135,13 @@ open class RichTextCoordinator: NSObject {

open func textViewDidChangeSelection(_ notification: Notification) {
syncWithTextView()
textViewDidChangeSelection()
}

open func textDidEndEditing(_ notification: Notification) {
context.isEditingText = false
}

#endif
}

Expand Down
14 changes: 12 additions & 2 deletions Sources/RichTextKit/Editor/RichTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,30 @@ public struct RichTextEditor: ViewRepresentable {
text: Binding<NSAttributedString>,
context: RichTextContext,
format: RichTextDataFormat = .archivedData,
viewConfiguration: @escaping ViewConfiguration = { _ in }
viewConfiguration: @escaping ViewConfiguration = { _ in },
textViewSouldChangeTextInRange: @escaping TextViewSouldChangeTextInRange = { (_, _) in return true },
textViewDidChangeSelection: @escaping TextViewDidChangeSelection = { }
) {
self.text = text
self._context = ObservedObject(wrappedValue: context)
self.format = format
self.viewConfiguration = viewConfiguration
self.textViewSouldChangeTextInRange = textViewSouldChangeTextInRange
self.textViewDidChangeSelection = textViewDidChangeSelection
}

public typealias ViewConfiguration = (RichTextViewComponent) -> Void
public typealias TextViewSouldChangeTextInRange = (NSRange, String?) -> Bool
public typealias TextViewDidChangeSelection = () -> Void

@ObservedObject
private var context: RichTextContext

private var text: Binding<NSAttributedString>
private var format: RichTextDataFormat
private var viewConfiguration: ViewConfiguration
private var textViewSouldChangeTextInRange: TextViewSouldChangeTextInRange
private var textViewDidChangeSelection: TextViewDidChangeSelection

@Environment(\.richTextEditorConfig)
private var config
Expand All @@ -105,7 +113,9 @@ public struct RichTextEditor: ViewRepresentable {
RichTextCoordinator(
text: text,
textView: textView,
richTextContext: context
richTextContext: context,
textViewSouldChangeTextInRange: textViewSouldChangeTextInRange,
textViewDidChangeSelection: textViewDidChangeSelection
)
}

Expand Down