Skip to content

Commit

Permalink
Move style toggling from context to coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 14, 2024
1 parent 5db6897 commit 792240a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
6 changes: 4 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ This release adds a lot of new `RichTextAction` cases and adjusts the context an

### ✨ Features

* `RichTextKeyboardToolbar` has a new configuration to always be shown.
* `RichTextView` has a new theme concept that lets you define its style.
* `RichTextKeyboardToolbar` has a new config to always be shown.
* `RichTextView` has a new theme that lets you define its style.
* `RichTextViewComponent` has a new `hasRichTextStyle` function.
* `RichTextViewComponent` has a new `toggleRichTextStyle` function.

### 💡 Adjustments

Expand Down
17 changes: 15 additions & 2 deletions Sources/RichTextKit/Component/RichTextViewComponent+Styles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

public extension RichTextViewComponent {

/// Get all rich text styles at current range.
var richTextStyles: [RichTextStyle] {
let attributes = richTextAttributes
Expand All @@ -19,7 +19,12 @@ public extension RichTextViewComponent {
if attributes.isUnderlined { styles.append(.underlined) }
return styles
}


/// Whether or not the current range has a certain style.
func hasRichTextStyle(_ style: RichTextStyle) -> Bool {
richTextStyles.contains(style)
}

/// Set a certain rich text style at current range.
func setRichTextStyle(
_ style: RichTextStyle,
Expand All @@ -39,4 +44,12 @@ public extension RichTextViewComponent {
setRichTextAttribute(.strikethroughStyle, to: value)
}
}

/// Set a certain rich text style at current range.
func toggleRichTextStyle(
_ style: RichTextStyle
) {
let hasStyle = hasRichTextStyle(style)
setRichTextStyle(style, to: !hasStyle)
}
}
1 change: 0 additions & 1 deletion Sources/RichTextKit/Context/RichTextContext+Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public extension RichTextContext {
/// Handle a certain rich text action.
func handle(_ action: RichTextAction) {
switch action {
case .toggleStyle(let style): toggle(style)
default: userActionPublisher.send(action)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ private extension RichTextCoordinator {
textView.stepRichTextIndent(points: points)
case .stepSuperscript:
break
case .toggleStyle:
break
case .toggleStyle(let style):
textView.toggleRichTextStyle(style)
case .undoLatestChange:
textView.undoLatestChange()
syncContextWithTextView()
Expand Down

0 comments on commit 792240a

Please sign in to comment.