From 24e473cb361f62bfa9ee61a8b3168c05b9dbb334 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Wed, 14 Feb 2024 13:57:32 +0100 Subject: [PATCH] Rename coordinator context --- .../RichTextViewComponent+Superscript.swift | 2 +- .../RichTextKit/Context/RichTextContext.swift | 12 +-- .../RichTextCoordinator+Subscriptions.swift | 10 +-- .../Coordinator/RichTextCoordinator.swift | 88 +++++++++---------- .../RichTextCoordinator+Deprecated.swift | 9 ++ 5 files changed, 66 insertions(+), 55 deletions(-) create mode 100644 Sources/RichTextKit/_Deprecated/RichTextCoordinator+Deprecated.swift diff --git a/Sources/RichTextKit/Component/RichTextViewComponent+Superscript.swift b/Sources/RichTextKit/Component/RichTextViewComponent+Superscript.swift index b4acad39b..8e1f967fb 100644 --- a/Sources/RichTextKit/Component/RichTextViewComponent+Superscript.swift +++ b/Sources/RichTextKit/Component/RichTextViewComponent+Superscript.swift @@ -17,7 +17,7 @@ public extension RichTextViewComponent { } /// Set the rich text superscript level at current range. - func setSuperscript(to val: Int) { + func setRichTextSuperscriptLevel(to val: Int) { setRichTextAttribute(.superscript, to: val) } } diff --git a/Sources/RichTextKit/Context/RichTextContext.swift b/Sources/RichTextKit/Context/RichTextContext.swift index ceb921491..8ceba836b 100644 --- a/Sources/RichTextKit/Context/RichTextContext.swift +++ b/Sources/RichTextKit/Context/RichTextContext.swift @@ -15,8 +15,7 @@ import Combine Use ``handle(_:)`` to trigger a change, e.g. to change font, text style, alignment, select a range, etc. You can observe - the various published properties to keep your UI updated as - the context changes. + the various published properties to keep your UI updated. The SwiftUI ``RichTextEditor`` uses this context as well as a ``RichTextCoordinator`` to keep itself updated. @@ -26,6 +25,7 @@ public class RichTextContext: ObservableObject { /// Create a new rich text context instance. public init() {} + // MARK: - Not yet observable properties /** @@ -46,7 +46,8 @@ public class RichTextContext: ObservableObject { /// The currently selected range, if any. public internal(set) var selectedRange = NSRange() - // MARK: - Public properies + + // MARK: - Bindable Properies /// Whether or not the text is currently being edited. @Published @@ -67,10 +68,11 @@ public class RichTextContext: ObservableObject { /// The currently highlighted range, if any. public var highlightedRange: NSRange? - /// Use this Publisher to emit any attribute changes to textView. + /// This publisher can emit actions to the coordinator. public let userActionPublisher: PassthroughSubject = .init() - // MARK: - Internal properties + + // MARK: - Observable Properties /// The current background color, if any. @Published diff --git a/Sources/RichTextKit/Coordinator/RichTextCoordinator+Subscriptions.swift b/Sources/RichTextKit/Coordinator/RichTextCoordinator+Subscriptions.swift index c509858c1..14764d357 100644 --- a/Sources/RichTextKit/Coordinator/RichTextCoordinator+Subscriptions.swift +++ b/Sources/RichTextKit/Coordinator/RichTextCoordinator+Subscriptions.swift @@ -17,7 +17,7 @@ extension RichTextCoordinator { /// by various buttons via the context, but also to some /// context value that are changed through view bindings. func subscribeToUserActions() { - richTextContext.userActionPublisher.sink { [weak self] action in + context.userActionPublisher.sink { [weak self] action in self?.handle(action) } .store(in: &cancellables) @@ -77,7 +77,7 @@ private extension RichTextCoordinator { } func subscribeToAlignment() { - richTextContext.$textAlignment + context.$textAlignment .sink { [weak self] in self?.handle(.setAlignment($0)) } @@ -85,7 +85,7 @@ private extension RichTextCoordinator { } func subscribeToFontName() { - richTextContext.$fontName + context.$fontName .sink { [weak self] in self?.textView.setRichTextFontName($0) } @@ -93,7 +93,7 @@ private extension RichTextCoordinator { } func subscribeToFontSize() { - richTextContext.$fontSize + context.$fontSize .sink { [weak self] in self?.textView.setRichTextFontSize($0) } @@ -101,7 +101,7 @@ private extension RichTextCoordinator { } func subscribeToIsEditingText() { - richTextContext.$isEditingText + context.$isEditingText .sink { [weak self] in self?.setIsEditing(to: $0) } diff --git a/Sources/RichTextKit/Coordinator/RichTextCoordinator.swift b/Sources/RichTextKit/Coordinator/RichTextCoordinator.swift index 687149a86..ee604aab1 100644 --- a/Sources/RichTextKit/Coordinator/RichTextCoordinator.swift +++ b/Sources/RichTextKit/Coordinator/RichTextCoordinator.swift @@ -42,7 +42,7 @@ open class RichTextCoordinator: NSObject { textView.attributedString = text.wrappedValue self.text = text self.textView = textView - self.richTextContext = richTextContext + self.context = richTextContext super.init() self.textView.delegate = self subscribeToUserActions() @@ -51,7 +51,7 @@ open class RichTextCoordinator: NSObject { // MARK: - Properties /// The rich text context to coordinate with. - public let richTextContext: RichTextContext + public let context: RichTextContext /// The rich text to edit. public var text: Binding @@ -84,7 +84,7 @@ open class RichTextCoordinator: NSObject { // MARK: - UITextViewDelegate open func textViewDidBeginEditing(_ textView: UITextView) { - richTextContext.isEditingText = true + context.isEditingText = true } open func textViewDidChange(_ textView: UITextView) { @@ -97,7 +97,7 @@ open class RichTextCoordinator: NSObject { open func textViewDidEndEditing(_ textView: UITextView) { syncWithTextView() - richTextContext.isEditingText = false + context.isEditingText = false } #endif @@ -106,7 +106,7 @@ open class RichTextCoordinator: NSObject { // MARK: - NSTextViewDelegate open func textDidBeginEditing(_ notification: Notification) { - richTextContext.isEditingText = true + context.isEditingText = true } open func textDidChange(_ notification: Notification) { @@ -118,7 +118,7 @@ open class RichTextCoordinator: NSObject { } open func textDidEndEditing(_ notification: Notification) { - richTextContext.isEditingText = false + context.isEditingText = false } #endif } @@ -141,7 +141,7 @@ public extension RichTextCoordinator { /// Reset appearance for the currently highlighted range. func resetHighlightedRangeAppearance() { guard - let range = richTextContext.highlightedRange, + let range = context.highlightedRange, let background = highlightedRangeOriginalBackgroundColor, let foreground = highlightedRangeOriginalForegroundColor else { return } @@ -180,100 +180,100 @@ extension RichTextCoordinator { let styles = textView.richTextStyles let string = textView.attributedString - if richTextContext.attributedString != string { - richTextContext.attributedString = string + if context.attributedString != string { + context.attributedString = string } let range = textView.selectedRange - if richTextContext.selectedRange != range { - richTextContext.selectedRange = range + if context.selectedRange != range { + context.selectedRange = range } RichTextColor.allCases.forEach { if let color = textView.richTextColor($0) { - richTextContext.setColor($0, to: color) + context.setColor($0, to: color) } } let foreground = textView.richTextColor(.foreground) - if richTextContext.foregroundColor != foreground { - richTextContext.foregroundColor = foreground + if context.foregroundColor != foreground { + context.foregroundColor = foreground } let background = textView.richTextColor(.background) - if richTextContext.backgroundColor != background { - richTextContext.backgroundColor = background + if context.backgroundColor != background { + context.backgroundColor = background } let stroke = textView.richTextColor(.stroke) - if richTextContext.strokeColor != stroke { - richTextContext.strokeColor = stroke + if context.strokeColor != stroke { + context.strokeColor = stroke } let strikethrough = textView.richTextColor(.strikethrough) - if richTextContext.strikethroughColor != strikethrough { - richTextContext.strikethroughColor = strikethrough + if context.strikethroughColor != strikethrough { + context.strikethroughColor = strikethrough } let underline = textView.richTextColor(.underline) - if richTextContext.underlineColor != underline { - richTextContext.underlineColor = underline + if context.underlineColor != underline { + context.underlineColor = underline } let hasRange = textView.hasSelectedRange - if richTextContext.canCopy != hasRange { - richTextContext.canCopy = hasRange + if context.canCopy != hasRange { + context.canCopy = hasRange } let canRedo = textView.undoManager?.canRedo ?? false - if richTextContext.canRedoLatestChange != canRedo { - richTextContext.canRedoLatestChange = canRedo + if context.canRedoLatestChange != canRedo { + context.canRedoLatestChange = canRedo } let canUndo = textView.undoManager?.canUndo ?? false - if richTextContext.canUndoLatestChange != canUndo { - richTextContext.canUndoLatestChange = canUndo + if context.canUndoLatestChange != canUndo { + context.canUndoLatestChange = canUndo } if let fontName = textView.richTextFont?.fontName, !fontName.isEmpty, - richTextContext.fontName != fontName { - richTextContext.fontName = fontName + context.fontName != fontName { + context.fontName = fontName } let fontSize = textView.richTextFont?.pointSize ?? .standardRichTextFontSize - if richTextContext.fontSize != fontSize { - richTextContext.fontSize = fontSize + if context.fontSize != fontSize { + context.fontSize = fontSize } let isBold = styles.hasStyle(.bold) - if richTextContext.isBold != isBold { - richTextContext.isBold = isBold + if context.isBold != isBold { + context.isBold = isBold } let isItalic = styles.hasStyle(.italic) - if richTextContext.isItalic != isItalic { - richTextContext.isItalic = isItalic + if context.isItalic != isItalic { + context.isItalic = isItalic } let isStrikethrough = styles.hasStyle(.strikethrough) - if richTextContext.isStrikethrough != isStrikethrough { - richTextContext.isStrikethrough = isStrikethrough + if context.isStrikethrough != isStrikethrough { + context.isStrikethrough = isStrikethrough } let isUnderlined = styles.hasStyle(.underlined) - if richTextContext.isUnderlined != isUnderlined { - richTextContext.isUnderlined = isUnderlined + if context.isUnderlined != isUnderlined { + context.isUnderlined = isUnderlined } let isEditingText = textView.isFirstResponder - if richTextContext.isEditingText != isEditingText { - richTextContext.isEditingText = isEditingText + if context.isEditingText != isEditingText { + context.isEditingText = isEditingText } if let alignment = textView.richTextAlignment, - richTextContext.textAlignment != alignment { - richTextContext.textAlignment = alignment + context.textAlignment != alignment { + context.textAlignment = alignment } updateTextViewAttributesIfNeeded() diff --git a/Sources/RichTextKit/_Deprecated/RichTextCoordinator+Deprecated.swift b/Sources/RichTextKit/_Deprecated/RichTextCoordinator+Deprecated.swift new file mode 100644 index 000000000..db45a85f5 --- /dev/null +++ b/Sources/RichTextKit/_Deprecated/RichTextCoordinator+Deprecated.swift @@ -0,0 +1,9 @@ +import SwiftUI + +public extension RichTextCoordinator { + + @available(*, deprecated, renamed: "context") + var richTextContext: RichTextContext { + context + } +}