Skip to content

Commit

Permalink
Rename the context action publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 15, 2024
1 parent 0dfe91c commit a07a54a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
5 changes: 3 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions Sources/RichTextKit/Actions/RichTextAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import SwiftUI
import Combine

/**
This enum defines rich text actions that can be executed on
Expand Down Expand Up @@ -76,6 +77,8 @@ public enum RichTextAction: Identifiable, Equatable, RichTextLabelValue {
}

public extension RichTextAction {

typealias Publisher = PassthroughSubject<Self, Never>

/// The action's unique identifier.
var id: String { title }
Expand Down
4 changes: 2 additions & 2 deletions Sources/RichTextKit/Context/RichTextContext+Actions.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 @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/RichTextKit/Context/RichTextContext+Colors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public extension RichTextContext {
func binding(for color: RichTextColor) -> Binding<Color> {
Binding(
get: { Color(self.color(for: color) ?? .clear) },
set: { self.setColor(color, to: ColorRepresentable($0)) }
set: { self.setColor(color, to: .init($0)) }
)
}

Expand All @@ -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)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/RichTextKit/Context/RichTextContext+Styles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
17 changes: 10 additions & 7 deletions Sources/RichTextKit/Context/RichTextContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<RichTextAction, Never> = .init()


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

@Published
@available(*, deprecated, renamed: "colors")
Expand Down Expand Up @@ -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
}

Expand All @@ -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.
Expand All @@ -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`.
Expand Down
10 changes: 7 additions & 3 deletions Sources/RichTextKit/_Deprecated/RichTextContext+Deprecated.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import SwiftUI
import Combine

public extension RichTextContext {

@available(*, deprecated, renamed: "actionPublisher")
var userActionPublisher: PassthroughSubject<RichTextAction, Never> { actionPublisher }

@available(*, deprecated, message: "Use handle(_:) instead")
func decrementFontSize(points: UInt = 1) {
Expand Down Expand Up @@ -39,7 +43,7 @@ public extension RichTextContext {
moveCursorToPastedContent: Bool = false
) {
let index = index ?? selectedRange.location
userActionPublisher.send(
actionPublisher.send(
.pasteImage(
RichTextInsertion(
content: image,
Expand All @@ -57,7 +61,7 @@ public extension RichTextContext {
moveCursorToPastedContent: Bool = false
) {
let index = index ?? selectedRange.location
userActionPublisher.send(
actionPublisher.send(
.pasteImages(
RichTextInsertion(
content: images,
Expand All @@ -75,7 +79,7 @@ public extension RichTextContext {
moveCursorToPastedContent: Bool = false
) {
let index = index ?? selectedRange.location
userActionPublisher.send(
actionPublisher.send(
.pasteText(
RichTextInsertion(
content: text,
Expand Down

0 comments on commit a07a54a

Please sign in to comment.