Skip to content

Commit

Permalink
Add editable toggle to rich text context
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Mar 4, 2024
1 parent 7e40f0e commit f3ed533
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
6 changes: 5 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ This release removes all deprecated code and cleans up the library.
* There are new `richTextKeyboardToolbar` config and style view modifiers.
* There is a new `RichTextFormat` namespace that groups format types and views.

### 🐛 Bug Fixes

* `RichTextView` no longer resets font and styles when setting up the macOS text view.

### 💥 Breaking Changes

* All previously deprecated code has been deleted.
* Many localization keys have been renamed to be easier to use.

* `RichTextContextFocusedValueKey` has been renamed to `RichTextContext.FocusedValueKey`.
* `RichTextContextFocusedValueKey` is renamed to `RichTextContext.FocusedValueKey`.
* `RichTextEditor` is now configured and styled with modifiers instead of the init.
* `RichTextFont` pickers are now configured with a shared modifier instead of the init.
* `RichTextFont` size picker is now configured with a shared modifier instead of the init.
Expand Down
6 changes: 6 additions & 0 deletions Sources/RichTextKit/Context/RichTextContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class RichTextContext: ObservableObject {


// MARK: - Bindable Properies

/// Whether or not the rich text editor is editable.
@Published
public var isEditable = true

/// Whether or not the text is currently being edited.
@Published
Expand Down Expand Up @@ -99,13 +103,15 @@ public class RichTextContext: ObservableObject {
@Published
public internal(set) var canIncreaseIndent = true

/// The current color values.
@Published
public internal(set) var colors = [RichTextColor: ColorRepresentable]()

/// The style to apply when highlighting a range.
@Published
public internal(set) var highlightingStyle = RichTextHighlightingStyle.standard

/// The current rich text styles.
@Published
public internal(set) var styles = [RichTextStyle: Bool]()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ extension RichTextCoordinator {
textView.setRichTextColor(.background, to: background, at: range)
textView.setRichTextColor(.foreground, to: foreground, at: range)
}

func setIsEditable(to newValue: Bool) {
if newValue == textView.isEditable { return }
textView.isEditable = newValue
}

func setIsEditing(to newValue: Bool) {
if newValue == textView.isFirstResponder { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extension RichTextCoordinator {
subscribeToAlignment()
subscribeToFontName()
subscribeToFontSize()
subscribeToIsEditable()
subscribeToIsEditingText()
subscribeToLineSpacing()
}
Expand Down Expand Up @@ -58,7 +59,13 @@ private extension RichTextCoordinator {
self?.textView.setRichTextFontSize($0)
}
}


func subscribeToIsEditable() {
subscribe(to: context.$isEditable) { [weak self] in
self?.setIsEditable(to: $0)
}
}

func subscribeToIsEditingText() {
subscribe(to: context.$isEditingText) { [weak self] in
self?.setIsEditing(to: $0)
Expand Down
4 changes: 3 additions & 1 deletion Sources/RichTextKit/Editor/RichTextView_AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ open class RichTextView: NSTextView, RichTextViewComponent {
public var configuration: Configuration = .standard

/// The theme for coloring and setting style to text view.
public var theme: Theme = .standard
public var theme: Theme = .standard {
didSet { setup(theme) }
}

/// The style to use when highlighting text in the view.
public var highlightingStyle: RichTextHighlightingStyle = .standard
Expand Down
4 changes: 1 addition & 3 deletions Sources/RichTextKit/Editor/RichTextView_UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ open class RichTextView: UITextView, RichTextViewComponent {
}

public var theme: Theme = .standard {
didSet {
setupTheme()
}
didSet { setup(theme) }
}

/// The style to use when highlighting text in the view.
Expand Down

0 comments on commit f3ed533

Please sign in to comment.