From 792240ac8d87a497e6a8a5911089633e95caee21 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Wed, 14 Feb 2024 13:51:27 +0100 Subject: [PATCH] Move style toggling from context to coordinator --- RELEASE_NOTES.md | 6 ++++-- .../RichTextViewComponent+Styles.swift | 17 +++++++++++++++-- .../Context/RichTextContext+Actions.swift | 1 - .../RichTextCoordinator+Subscriptions.swift | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index dbf05ba66..820400834 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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 diff --git a/Sources/RichTextKit/Component/RichTextViewComponent+Styles.swift b/Sources/RichTextKit/Component/RichTextViewComponent+Styles.swift index 6708bcaa9..9d5f378a7 100644 --- a/Sources/RichTextKit/Component/RichTextViewComponent+Styles.swift +++ b/Sources/RichTextKit/Component/RichTextViewComponent+Styles.swift @@ -9,7 +9,7 @@ import Foundation public extension RichTextViewComponent { - + /// Get all rich text styles at current range. var richTextStyles: [RichTextStyle] { let attributes = richTextAttributes @@ -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, @@ -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) + } } diff --git a/Sources/RichTextKit/Context/RichTextContext+Actions.swift b/Sources/RichTextKit/Context/RichTextContext+Actions.swift index e3e92c0ef..6ee2cfb3d 100644 --- a/Sources/RichTextKit/Context/RichTextContext+Actions.swift +++ b/Sources/RichTextKit/Context/RichTextContext+Actions.swift @@ -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) } } diff --git a/Sources/RichTextKit/Coordinator/RichTextCoordinator+Subscriptions.swift b/Sources/RichTextKit/Coordinator/RichTextCoordinator+Subscriptions.swift index e76d6cf83..c509858c1 100644 --- a/Sources/RichTextKit/Coordinator/RichTextCoordinator+Subscriptions.swift +++ b/Sources/RichTextKit/Coordinator/RichTextCoordinator+Subscriptions.swift @@ -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()