Skip to content

Commit

Permalink
Adjust the rich text keyboard toolbar initializer and its new config …
Browse files Browse the repository at this point in the history
…and style components
  • Loading branch information
danielsaidi committed Mar 4, 2024
1 parent 7670cc8 commit 1e1cec4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 70 deletions.
18 changes: 18 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ Until then, minor updates may remove deprecated features and introduce breaking



## 1.0

This release removes all deprecated code and cleans up the library somewhat.

### ✨ Features

* The new `richTextKeyboardToolbarConfig` view extension can be used to configure a `RichTextKeyboardToolbar`.
* The new `richTextKeyboardToolbarStyle` view extension can be used to style a `RichTextKeyboardToolbar`.

### 💥 Breaking Changes

* All previously deprecated code has been deleted.

* `RichTextKeyboardToolbar`'s `style` and `config` is now applied with view extensions instead of with the initializer.
* `RichTextKeyboardToolbar`'s `leadingActions` and `trailingActions` are now specified with the configuration instead of the initializer.



## 0.9.9

This release adds a lot of new `RichTextAction` cases and adjusts the context and coordinator subscription.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,54 @@
#if iOS || macOS || os(visionOS)
import SwiftUI

/// This struct can be used to configure a `RichTextKeyboardToolbar`.
/// This struct can configure a ``RichTextKeyboardToolbar``.
public struct RichTextKeyboardToolbarConfiguration {

/**
- Parameters:
- alwaysDisplayToolbar: Should the toolbar always be shown, by default `false`.
*/
/// Create a custom toolbar configuration.
///
/// - Parameters:
/// - alwaysDisplayToolbar: Whether or not to always show the toolbar, by default `false`.
public init(
alwaysDisplayToolbar: Bool = false
) {
self.alwaysDisplayToolbar = alwaysDisplayToolbar
}

/// Should the toolbar always be shown.
var alwaysDisplayToolbar: Bool

/// Default value
static var standard: RichTextKeyboardToolbarConfiguration = .init()
/// Whether or not to always show the toolbar
public var alwaysDisplayToolbar: Bool
}

/// This environment key defines a `RichTextKeyboardToolbar` configuration.
private struct RichTextKeyboardToolbarConfigurationKey: EnvironmentKey {
public static var defaultValue: RichTextKeyboardToolbarConfiguration = .standard
public extension RichTextKeyboardToolbarConfiguration {

/// A standard rich text keyboard toolbar configuration.
///
/// You can override this to change the global default.
static var standard = RichTextKeyboardToolbarConfiguration()
}

public extension View {

/// Apply a `RichTextKeyboardToolbar` configuration.
/// Apply a ``RichTextKeyboardToolbar`` configuration.
func richTextKeyboardToolbarConfiguration(
_ configuration: RichTextKeyboardToolbarConfiguration
) -> some View {
self.environment(\.richTextKeyboardToolbarConfiguration, configuration)
}
}

extension RichTextKeyboardToolbarConfiguration {

struct Key: EnvironmentKey {

public static var defaultValue: RichTextKeyboardToolbarConfiguration = .standard
}
}

public extension EnvironmentValues {

/// This environment value defines `RichTextKeyboardToolbar` configurations.
var richTextKeyboardToolbarConfiguration: RichTextKeyboardToolbarConfiguration {
get { self [RichTextKeyboardToolbarConfigurationKey.self] }
set { self [RichTextKeyboardToolbarConfigurationKey.self] = newValue }
get { self [RichTextKeyboardToolbarConfiguration.Key.self] }
set { self [RichTextKeyboardToolbarConfiguration.Key.self] = newValue }
}
}
#endif
43 changes: 25 additions & 18 deletions Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
#if iOS || macOS || os(visionOS)
import SwiftUI

/// This struct can be used to style a `RichTextKeyboardToolbar`.
/// This struct can style a ``RichTextKeyboardToolbar``.
public struct RichTextKeyboardToolbarStyle {

/**
- Parameters:
- toolbarHeight: The height of the toolbar, by default `50`.
- itemSpacing: The spacing between toolbar items, by default `15`.
- shadowColor: The toolbar's shadow color, by default transparent black.
- shadowRadius: The toolbar's shadow radius, by default `3`.
*/
/// Create a custom toolbar style
///
/// - Parameters:
/// - toolbarHeight: The height of the toolbar, by default `50`.
/// - itemSpacing: The spacing between toolbar items, by default `15`.
/// - shadowColor: The toolbar's shadow color, by default transparent black.
/// - shadowRadius: The toolbar's shadow radius, by default `3`.
public init(
toolbarHeight: Double = 50,
itemSpacing: Double = 15,
Expand All @@ -42,32 +42,39 @@ public struct RichTextKeyboardToolbarStyle {

/// The toolbar's shadow radius.
public var shadowRadius: Double

/// Default value
static var standard: RichTextKeyboardToolbarStyle = .init()
}

/// This environment key defines a `RichTextKeyboardToolbar` style.
private struct RichTextKeyboardToolbarStyleKey: EnvironmentKey {
public static var defaultValue: RichTextKeyboardToolbarStyle = .standard
public extension RichTextKeyboardToolbarStyle {

/// A standard rich text keyboard toolbar style.
///
/// You can override this to change the global default.
static var standard = RichTextKeyboardToolbarStyle()
}

public extension View {

/// Apply a `RichTextKeyboardToolbar` style.
/// Apply a ``RichTextKeyboardToolbar`` style.
func richTextKeyboardToolbarStyle(
_ style: RichTextKeyboardToolbarStyle
) -> some View {
self.environment(\.richTextKeyboardToolbarStyle, style)
}
}

extension RichTextKeyboardToolbarStyle {

struct Key: EnvironmentKey {

static var defaultValue: RichTextKeyboardToolbarStyle = .standard
}
}

public extension EnvironmentValues {

/// This environment value defines `RichTextKeyboardToolbar` styles.
var richTextKeyboardToolbarStyle: RichTextKeyboardToolbarStyle {
get { self [RichTextKeyboardToolbarStyleKey.self] }
set { self [RichTextKeyboardToolbarStyleKey.self] = newValue }
get { self [RichTextKeyboardToolbarStyle.Key.self] }
set { self [RichTextKeyboardToolbarStyle.Key.self] = newValue }
}
}

Expand Down
43 changes: 8 additions & 35 deletions Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,58 +46,30 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
- trailingActions: The trailing actions, by default `.dismissKeyboard`.
- leadingButtons: The leading buttons to place after the leading actions.
- trailingButtons: The trailing buttons to place before the trailing actions.
- richTextFormatSheet: The rich text format sheet to use, given the default ``RichTextFormatSheet``.
- formatSheet: The rich text format sheet to use, given the default ``RichTextFormatSheet``.
*/
public init(
context: RichTextContext,
leadingActions: [RichTextAction] = [.undo, .redo, .copy],
leadingActions: [RichTextAction] = [.undo, .redo],
trailingActions: [RichTextAction] = [.dismissKeyboard],
@ViewBuilder leadingButtons: @escaping () -> LeadingButtons,
@ViewBuilder trailingButtons: @escaping () -> TrailingButtons,
@ViewBuilder richTextFormatSheet: @escaping (RichTextFormatSheet) -> FormatSheet
@ViewBuilder formatSheet: @escaping (RichTextFormatSheet) -> FormatSheet
) {
self._context = ObservedObject(wrappedValue: context)
self.leadingActions = leadingActions
self.trailingActions = trailingActions
self.leadingButtons = leadingButtons
self.trailingButtons = trailingButtons
self.richTextFormatSheet = richTextFormatSheet
}

/**
Create a rich text keyboard toolbar.

- Parameters:
- context: The context to affect.
- leadingActions: The leading actions, by default `.undo`, `.redo` and `.copy`.
- trailingActions: The trailing actions, by default `.dismissKeyboard`.
- leadingButtons: The leading buttons to place after the leading actions.
- trailingButtons: The trailing buttons to place after the trailing actions.
- richTextFormatSheet: The rich text format sheet to use, given the default ``RichTextFormatSheet``.
*/
public init(
context: RichTextContext,
leadingActions: [RichTextAction] = [.undo, .redo, .copy],
trailingActions: [RichTextAction] = [.dismissKeyboard],
@ViewBuilder leadingButtons: @escaping () -> LeadingButtons,
@ViewBuilder trailingButtons: @escaping () -> TrailingButtons
) where FormatSheet == RichTextFormatSheet {
self.init(
context: context,
leadingActions: leadingActions,
trailingActions: trailingActions,
leadingButtons: leadingButtons,
trailingButtons: trailingButtons,
richTextFormatSheet: { $0 }
)
self.formatSheet = formatSheet
}

private let leadingActions: [RichTextAction]
private let trailingActions: [RichTextAction]

private let leadingButtons: () -> LeadingButtons
private let trailingButtons: () -> TrailingButtons
private let richTextFormatSheet: (RichTextFormatSheet) -> FormatSheet
private let formatSheet: (RichTextFormatSheet) -> FormatSheet

@ObservedObject
private var context: RichTextContext
Expand Down Expand Up @@ -136,7 +108,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
.offset(y: shouldDisplayToolbar ? 0 : style.toolbarHeight)
.frame(height: shouldDisplayToolbar ? nil : 0)
.sheet(isPresented: $isFormatSheetPresented) {
richTextFormatSheet(
formatSheet(
RichTextFormatSheet(context: context)
).prefersMediumSize()
}
Expand Down Expand Up @@ -261,7 +233,8 @@ struct RichTextKeyboardToolbar_Previews: PreviewProvider {
RichTextKeyboardToolbar(
context: context,
leadingButtons: {},
trailingButtons: {}
trailingButtons: {},
formatSheet: { $0 }
)
}
}
Expand Down

0 comments on commit 1e1cec4

Please sign in to comment.