Skip to content

Commit

Permalink
Refactor context colors to use a single published property
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 15, 2024
1 parent 035c5d6 commit 0dfe91c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 59 deletions.
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ By deprecating these functions, we can simplify the library in 1.0, and focus mo
* `RichTextCommand.IndentOptionsGroup` has been deprecated.
* `RichTextCommand.StyleOptionsGroup` has been deprecated.
* `RichTextCommand.SuperscriptOptionsGroup` has been deprecated.
* `RichTextCoordinator` functions that called `handle(_:)` have 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.

### 💥 Breaking Changes
Expand Down
29 changes: 14 additions & 15 deletions Sources/RichTextKit/Context/RichTextContext+Colors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,13 +20,7 @@ public extension RichTextContext {

/// Get the value for a certain color.
func color(for color: RichTextColor) -> ColorRepresentable? {
switch color {
case .foreground: foregroundColor
case .background: backgroundColor
case .strikethrough: strikethroughColor
case .stroke: strokeColor
case .underline: underlineColor
}
colors[color]
}

/// Set the value for a certain color.
Expand All @@ -36,13 +30,18 @@ public extension RichTextContext {
) {
guard self.color(for: color) != val else { return }
userActionPublisher.send(.setColor(color, val))
setColorInternal(color, to: val)
}
}

extension RichTextContext {

switch color {
case .foreground: foregroundColor = val
case .background: backgroundColor = val
case .strikethrough: strikethroughColor = val
case .stroke: strokeColor = val
case .underline: underlineColor = val
}
/// Set the value for a certain color, or remove it.
func setColorInternal(
_ color: RichTextColor,
to val: ColorRepresentable?
) {
guard let val else { return colors[color] = nil }
colors[color] = val
}
}
2 changes: 1 addition & 1 deletion Sources/RichTextKit/Context/RichTextContext+Styles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 32 additions & 26 deletions Sources/RichTextKit/Context/RichTextContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import Combine
This observable context can be used to affect and observe a
``RichTextEditor`` and its content.
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.
The SwiftUI ``RichTextEditor`` uses this context as well as
a ``RichTextCoordinator`` to keep itself updated.
Use ``handle(_:)`` to trigger actions, e.g. to change fonts,
text styles, text alignments, select a text range, etc.
You can observe the various published properties to keep an
app up to date with the current state. The ``RichTextEditor``
uses this with a ``RichTextCoordinator`` to keep it updated.
*/
public class RichTextContext: ObservableObject {

Expand Down Expand Up @@ -70,19 +70,38 @@ public class RichTextContext: ObservableObject {

/// This publisher can emit actions to the coordinator.
public let userActionPublisher: PassthroughSubject<RichTextAction, Never> = .init()


// MARK: - Observable Properties

/// The current background color, if any.

// MARK: - Soft Deprecations (to avoid library warnings)

@Published
@available(*, deprecated, renamed: "colors")
public internal(set) var backgroundColor: ColorRepresentable?

@Published
@available(*, deprecated, renamed: "colors")
public internal(set) var foregroundColor: ColorRepresentable?

@Published
@available(*, deprecated, renamed: "colors")
public internal(set) var strikethroughColor: ColorRepresentable?

@Published
@available(*, deprecated, renamed: "colors")
public internal(set) var strokeColor: ColorRepresentable?

@Published
@available(*, deprecated, renamed: "colors")
public internal(set) var underlineColor: ColorRepresentable?


// MARK: - Observable Properties

/// Whether or not the current rich text can be copied.
@Published
public internal(set) var canCopy = false

/// Whether or not the latest undone change can be redone.
/// Whether or not the latest undo can be redone.
@Published
public internal(set) var canRedoLatestChange = false

Expand All @@ -97,10 +116,9 @@ public class RichTextContext: ObservableObject {
/// Whether or not the indent level can be increased.
@Published
public internal(set) var canIncreaseIndent = true

/// The current foreground color, if any.

@Published
public internal(set) var foregroundColor: ColorRepresentable?
public internal(set) var colors = [RichTextColor: ColorRepresentable]()

/// The style to apply when highlighting a range.
@Published
Expand All @@ -121,18 +139,6 @@ public class RichTextContext: ObservableObject {
/// Whether or not the current text is underlined.
@Published
public internal(set) var isUnderlined = false

/// The current strikethrough color, if any.
@Published
public internal(set) var strikethroughColor: ColorRepresentable?

/// The current stroke color, if any.
@Published
public internal(set) var strokeColor: ColorRepresentable?

/// The current underline color, if any.
@Published
public internal(set) var underlineColor: ColorRepresentable?
}

public extension RichTextContext {
Expand Down
22 changes: 6 additions & 16 deletions Sources/RichTextKit/Coordinator/RichTextCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,29 +196,19 @@ extension RichTextCoordinator {
}

let foreground = textView.richTextColor(.foreground)
if context.foregroundColor != foreground {
context.foregroundColor = foreground
}
context.setColorInternal(.foreground, to: foreground)

let background = textView.richTextColor(.background)
if context.backgroundColor != background {
context.backgroundColor = background
}
context.setColorInternal(.background, to: background)

let stroke = textView.richTextColor(.stroke)
if context.strokeColor != stroke {
context.strokeColor = stroke
}
context.setColorInternal(.stroke, to: stroke)

let strikethrough = textView.richTextColor(.strikethrough)
if context.strikethroughColor != strikethrough {
context.strikethroughColor = strikethrough
}

context.setColorInternal(.strikethrough, to: strikethrough)

let underline = textView.richTextColor(.underline)
if context.underlineColor != underline {
context.underlineColor = underline
}
context.setColorInternal(.underline, to: underline)

let hasRange = textView.hasSelectedRange
if context.canCopy != hasRange {
Expand Down

0 comments on commit 0dfe91c

Please sign in to comment.