Skip to content

Commit

Permalink
Rename coordinator context
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 14, 2024
1 parent 792240a commit 24e473c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
12 changes: 7 additions & 5 deletions Sources/RichTextKit/Context/RichTextContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,6 +25,7 @@ public class RichTextContext: ObservableObject {
/// Create a new rich text context instance.
public init() {}


// MARK: - Not yet observable properties

/**
Expand All @@ -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
Expand All @@ -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<RichTextAction, Never> = .init()

// MARK: - Internal properties

// MARK: - Observable Properties

/// The current background color, if any.
@Published
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -77,31 +77,31 @@ private extension RichTextCoordinator {
}

func subscribeToAlignment() {
richTextContext.$textAlignment
context.$textAlignment
.sink { [weak self] in
self?.handle(.setAlignment($0))
}
.store(in: &cancellables)
}

func subscribeToFontName() {
richTextContext.$fontName
context.$fontName
.sink { [weak self] in
self?.textView.setRichTextFontName($0)
}
.store(in: &cancellables)
}

func subscribeToFontSize() {
richTextContext.$fontSize
context.$fontSize
.sink { [weak self] in
self?.textView.setRichTextFontSize($0)
}
.store(in: &cancellables)
}

func subscribeToIsEditingText() {
richTextContext.$isEditingText
context.$isEditingText
.sink { [weak self] in
self?.setIsEditing(to: $0)
}
Expand Down
88 changes: 44 additions & 44 deletions Sources/RichTextKit/Coordinator/RichTextCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<NSAttributedString>
Expand Down Expand Up @@ -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) {
Expand All @@ -97,7 +97,7 @@ open class RichTextCoordinator: NSObject {

open func textViewDidEndEditing(_ textView: UITextView) {
syncWithTextView()
richTextContext.isEditingText = false
context.isEditingText = false
}
#endif

Expand All @@ -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) {
Expand All @@ -118,7 +118,7 @@ open class RichTextCoordinator: NSObject {
}

open func textDidEndEditing(_ notification: Notification) {
richTextContext.isEditingText = false
context.isEditingText = false
}
#endif
}
Expand All @@ -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 }
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import SwiftUI

public extension RichTextCoordinator {

@available(*, deprecated, renamed: "context")
var richTextContext: RichTextContext {
context
}
}

0 comments on commit 24e473c

Please sign in to comment.