From a07a54ad99553833c5ab8f64d430da4bc2a61e43 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Thu, 15 Feb 2024 11:06:50 +0100 Subject: [PATCH] Rename the context action publisher --- RELEASE_NOTES.md | 5 +++-- .../RichTextKit/Actions/RichTextAction.swift | 3 +++ .../Context/RichTextContext+Actions.swift | 4 ++-- .../Context/RichTextContext+Colors.swift | 4 ++-- .../Context/RichTextContext+Styles.swift | 8 ++++---- .../RichTextKit/Context/RichTextContext.swift | 17 ++++++++++------- .../RichTextContext+Deprecated.swift | 10 +++++++--- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index b955fa31b..9e969d3f1 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -51,8 +51,9 @@ By deprecating these functions, we can simplify the library in 1.0, and focus mo * `RichTextCommand.StyleOptionsGroup` has been deprecated. * `RichTextCommand.SuperscriptOptionsGroup` has been deprecated. * `RichTextContext` replaces individual colors with a single `colors`. -* `RichTextCoordinator` functions that call `handle(_:)` have been deprecated. -* `RTKL10n.bundle` has been deprecated since we can just use `.module` from now. +* `RichTextContext` `userActionPublisher` is renamed to `actionPublisher`. +* `RichTextCoordinator` functions calling `handle(_:)` have been deprecated. +* `RTKL10n.bundle` has been deprecated since we can use the `.module` bundle. ### 💥 Breaking Changes diff --git a/Sources/RichTextKit/Actions/RichTextAction.swift b/Sources/RichTextKit/Actions/RichTextAction.swift index 5f0fcbc44..575612fbc 100644 --- a/Sources/RichTextKit/Actions/RichTextAction.swift +++ b/Sources/RichTextKit/Actions/RichTextAction.swift @@ -7,6 +7,7 @@ // import SwiftUI +import Combine /** This enum defines rich text actions that can be executed on @@ -76,6 +77,8 @@ public enum RichTextAction: Identifiable, Equatable, RichTextLabelValue { } public extension RichTextAction { + + typealias Publisher = PassthroughSubject /// The action's unique identifier. var id: String { title } diff --git a/Sources/RichTextKit/Context/RichTextContext+Actions.swift b/Sources/RichTextKit/Context/RichTextContext+Actions.swift index 9c4e716f2..7c68ff1c5 100644 --- a/Sources/RichTextKit/Context/RichTextContext+Actions.swift +++ b/Sources/RichTextKit/Context/RichTextContext+Actions.swift @@ -3,7 +3,7 @@ // RichTextKit // // Created by Daniel Saidi on 2022-12-08. -// Copyright © 2022-2023 Daniel Saidi. All rights reserved. +// Copyright © 2022-2024 Daniel Saidi. All rights reserved. // import SwiftUI @@ -13,7 +13,7 @@ public extension RichTextContext { /// Handle a certain rich text action. func handle(_ action: RichTextAction) { switch action { - default: userActionPublisher.send(action) + default: actionPublisher.send(action) } } diff --git a/Sources/RichTextKit/Context/RichTextContext+Colors.swift b/Sources/RichTextKit/Context/RichTextContext+Colors.swift index 5e52412c8..6368b8a3a 100644 --- a/Sources/RichTextKit/Context/RichTextContext+Colors.swift +++ b/Sources/RichTextKit/Context/RichTextContext+Colors.swift @@ -14,7 +14,7 @@ public extension RichTextContext { func binding(for color: RichTextColor) -> Binding { Binding( get: { Color(self.color(for: color) ?? .clear) }, - set: { self.setColor(color, to: ColorRepresentable($0)) } + set: { self.setColor(color, to: .init($0)) } ) } @@ -29,7 +29,7 @@ public extension RichTextContext { to val: ColorRepresentable ) { guard self.color(for: color) != val else { return } - userActionPublisher.send(.setColor(color, val)) + actionPublisher.send(.setColor(color, val)) setColorInternal(color, to: val) } } diff --git a/Sources/RichTextKit/Context/RichTextContext+Styles.swift b/Sources/RichTextKit/Context/RichTextContext+Styles.swift index bb3fc0559..ed0351391 100644 --- a/Sources/RichTextKit/Context/RichTextContext+Styles.swift +++ b/Sources/RichTextKit/Context/RichTextContext+Styles.swift @@ -34,10 +34,10 @@ public extension RichTextContext { to val: Bool ) { switch style { - case .bold: userActionPublisher.send(.setStyle(.bold, val)); isBold = val - case .italic: userActionPublisher.send(.setStyle(.italic, val)); isItalic = val - case .underlined: userActionPublisher.send(.setStyle(.underlined, val)); isUnderlined = val - case .strikethrough: userActionPublisher.send(.setStyle(.strikethrough, val)); isStrikethrough = val + case .bold: actionPublisher.send(.setStyle(.bold, val)); isBold = val + case .italic: actionPublisher.send(.setStyle(.italic, val)); isItalic = val + case .underlined: actionPublisher.send(.setStyle(.underlined, val)); isUnderlined = val + case .strikethrough: actionPublisher.send(.setStyle(.strikethrough, val)); isStrikethrough = val } } diff --git a/Sources/RichTextKit/Context/RichTextContext.swift b/Sources/RichTextKit/Context/RichTextContext.swift index 1883e6bd7..3c9ea7ae3 100644 --- a/Sources/RichTextKit/Context/RichTextContext.swift +++ b/Sources/RichTextKit/Context/RichTextContext.swift @@ -64,15 +64,18 @@ public class RichTextContext: ObservableObject { /// The current font size. @Published public var fontSize = CGFloat.standardRichTextFontSize + + + // MARK: - Properties + + /// This publisher can emit actions to the coordinator. + public let actionPublisher = RichTextAction.Publisher() /// The currently highlighted range, if any. public var highlightedRange: NSRange? - - /// This publisher can emit actions to the coordinator. - public let userActionPublisher: PassthroughSubject = .init() - // MARK: - Soft Deprecations (to avoid library warnings) + // MARK: - Deprecations (to avoid library warnings) @Published @available(*, deprecated, renamed: "colors") @@ -158,7 +161,7 @@ public extension RichTextContext { /// Set ``highlightedRange`` to a new, optional range. func highlightRange(_ range: NSRange?) { - userActionPublisher.send(.setHighlightedRange(range)) + actionPublisher.send(.setHighlightedRange(range)) highlightedRange = range } @@ -181,7 +184,7 @@ public extension RichTextContext { /// Set a new range and start editing. func selectRange(_ range: NSRange) { isEditingText = true - userActionPublisher.send(.selectRange(range)) + actionPublisher.send(.selectRange(range)) } /// Set the attributed string to a new plain text. @@ -192,7 +195,7 @@ public extension RichTextContext { /// Set the attributed string to a new rich text. func setAttributedString(to string: NSAttributedString) { let mutable = NSMutableAttributedString(attributedString: string) - userActionPublisher.send(.setAttributedString(mutable)) + actionPublisher.send(.setAttributedString(mutable)) } /// Set ``isEditingText`` to `false`. diff --git a/Sources/RichTextKit/_Deprecated/RichTextContext+Deprecated.swift b/Sources/RichTextKit/_Deprecated/RichTextContext+Deprecated.swift index f149eb239..ddccb8b3f 100644 --- a/Sources/RichTextKit/_Deprecated/RichTextContext+Deprecated.swift +++ b/Sources/RichTextKit/_Deprecated/RichTextContext+Deprecated.swift @@ -1,6 +1,10 @@ import SwiftUI +import Combine public extension RichTextContext { + + @available(*, deprecated, renamed: "actionPublisher") + var userActionPublisher: PassthroughSubject { actionPublisher } @available(*, deprecated, message: "Use handle(_:) instead") func decrementFontSize(points: UInt = 1) { @@ -39,7 +43,7 @@ public extension RichTextContext { moveCursorToPastedContent: Bool = false ) { let index = index ?? selectedRange.location - userActionPublisher.send( + actionPublisher.send( .pasteImage( RichTextInsertion( content: image, @@ -57,7 +61,7 @@ public extension RichTextContext { moveCursorToPastedContent: Bool = false ) { let index = index ?? selectedRange.location - userActionPublisher.send( + actionPublisher.send( .pasteImages( RichTextInsertion( content: images, @@ -75,7 +79,7 @@ public extension RichTextContext { moveCursorToPastedContent: Bool = false ) { let index = index ?? selectedRange.location - userActionPublisher.send( + actionPublisher.send( .pasteText( RichTextInsertion( content: text,