Skip to content

Commit

Permalink
Replace a bunch of command views with new action group initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 15, 2024
1 parent a702cac commit 0d64a62
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 51 deletions.
10 changes: 8 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ By deprecating these functions, we can simplify the library in 1.0, and focus mo
### ✨ Features

* `FontRepresentable` has new extensions.
* `RichTextCommand.ActionButtonGroup` has many new initializers.
* `RichTextKeyboardToolbar` has a new config to always be shown.
* `RichTextView` has a new theme that lets you define its style.
* `RichTextViewComponent` has a new `hasRichTextStyle` function.
Expand All @@ -43,8 +44,13 @@ By deprecating these functions, we can simplify the library in 1.0, and focus mo

* `RichTextAttributeReader` has deprecated a lot of functions.
* `RichTextAttributeWriter` has deprecated a lot of functions.
* `RichTextCoordinator` functions that simply triggered `handle(_:)` have been deprecated.
* `RTKL10n.bundle` has been deprecated, since we can just use the `.module` bundle from now.
* `RichTextCommand.AlignmentOptionsGroup` has been deprecated.
* `RichTextCommand.FontSizeOptionsGroup` has been deprecated.
* `RichTextCommand.IndentOptionsGroup` has been deprecated.
* `RichTextCommand.StyleOptionsGroup` has been deprecated.
* `RichTextCommand.SuperscriptOptionsGroup` has been deprecated.
* `RichTextCoordinator` functions that called `handle(_:)` have been deprecated.
* `RTKL10n.bundle` has been deprecated since we can just use `.module` from now.

### 💥 Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public extension RichTextCommand {
struct ActionButtonGroup: View {

/**
Create a command button group.
Create a custom action button group.
- Parameters:
- actions: The actions to trigger.
Expand All @@ -40,3 +40,59 @@ public extension RichTextCommand {
}
}
}

public extension RichTextCommand.ActionButtonGroup {

/// Create a button group with alignments.
init(
alignments: [RichTextAlignment],
additionalActions: [RichTextAction] = []
) {
self.actions = RichTextAlignment.allCases.map {
.setAlignment($0)
} + additionalActions
}

/// Create a button group with font size steppers.
init(
fontSize: Bool,
additionalActions: [RichTextAction] = []
) {
self.actions = [
.increaseFontSize(),
.decreaseFontSize()
] + additionalActions
}

/// Create a button group with indent steppers.
init(
indent: Bool,
additionalActions: [RichTextAction] = []
) {
self.actions = [
.increaseIndent(),
.decreaseIndent()
] + additionalActions
}

/// Create a button group with style toggles.
init(
styles: [RichTextAlignment],
additionalActions: [RichTextAction] = []
) {
self.actions = RichTextStyle.allCases.map {
.toggleStyle($0)
} + additionalActions
}

/// Create a button group with superscript steppers.
init(
superscript: Bool,
additionalActions: [RichTextAction] = []
) {
self.actions = [
.increaseSuperscript(),
.decreaseSuperscript()
] + additionalActions
}
}
19 changes: 11 additions & 8 deletions Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import SwiftUI
public extension RichTextCommand {

/**
This menu view can add a list of text format options to
the main menu.
This menu adds standard rich text format options to the
main menu, using `CommandGroup`.
This view requires that a ``RichTextContext`` is set as
You can apply this to a `WindowGroup` or `DocumentGroup`
to make it appear in the app's main menu.
This menu requires that a ``RichTextContext`` is set as
a focused value, otherwise it will be disabled.
*/
struct FormatMenu: Commands {
Expand All @@ -30,18 +33,18 @@ public extension RichTextCommand {
CommandMenu(RTKL10n.menuFormat.text) {
Group {
Menu(RTKL10n.menuFont.text) {
StyleOptionsGroup()
ActionButtonGroup(styles: .all)
Divider()
FontSizeOptionsGroup()
ActionButtonGroup(fontSize: true)
}
Menu(RTKL10n.menuText.text) {
AlignmentOptionsGroup()
ActionButtonGroup(alignments: .all)
}
Menu(RTKL10n.menuIndent.text) {
IndentOptionsGroup()
ActionButtonGroup(indent: true)
}
Menu(RTKL10n.menuSuperscript.text) {
SuperscriptOptionsGroup()
ActionButtonGroup(superscript: true)
}
}
.disabled(context == nil)
Expand Down
6 changes: 3 additions & 3 deletions Sources/RichTextKit/Commands/RichTextCommand+ShareMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public extension RichTextCommand {
`isEnabled: false` to disable the menu, since you can't
add `.disabled` to the command menu.
Setting actions to `nil` removes a corresponding option
from the menu.
Setting actions to `nil` removes the related menu items.
*/
struct ShareMenu: Commands {

Expand Down Expand Up @@ -87,7 +86,8 @@ public extension RichTextCommand {
shareMenu
exportMenu
printButton
}.disabled(!isEnabled)
}
.disabled(!isEnabled)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/RichTextKit/Commands/RichTextCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Foundation

/**
This type is used as a namespace for command-specific types,
like views that are to be added to the main menu bar.
like command menus and buttons.
> Important: Most rich text commands require that there's a
focused ``RichTextContext`` value in the view hierarchy.
``RichTextContext`` set as a focused value.
*/
public struct RichTextCommand {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ import SwiftUI

public extension RichTextCommand {

/**
This view can add list of text alignment options to the
main menu, using an ``RichTextCommand/ActionButtonGroup``.
This view requires that a ``RichTextContext`` is set as
a focused value, otherwise it will be disabled.
*/
@available(*, deprecated, message: "Use RichTextCommand.ActionButtonGroup alignments initializer instead.")
struct AlignmentOptionsGroup: View {

public init() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ import SwiftUI

public extension RichTextCommand {

/**
This view can add list of font size options to the main
menu, using an ``RichTextCommand/ActionButtonGroup``.
This view requires that a ``RichTextContext`` is set as
a focused value, otherwise it will be disabled.
*/
@available(*, deprecated, message: "Use RichTextCommand.ActionButtonGroup fontSize initializer instead.")
struct FontSizeOptionsGroup: View {

public init() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ import SwiftUI

public extension RichTextCommand {

/**
This group can add a list of text indent options to the
main menu, using an ``RichTextCommand/ActionButtonGroup``.
This view requires that a ``RichTextContext`` is set as
a focused value, otherwise it will be disabled.
*/
@available(*, deprecated, message: "Use RichTextCommand.ActionButtonGroup indent initializer instead.")
struct IndentOptionsGroup: View {

public var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ import SwiftUI

public extension RichTextCommand {

/**
This view can add a list of styling options to the main
menu, using an ``RichTextCommand/ActionButtonGroup``.
This view requires that a ``RichTextContext`` is set as
a focused value, otherwise it will be disabled.
*/
@available(*, deprecated, message: "Use RichTextCommand.ActionButtonGroup styles initializer instead.")
struct StyleOptionsGroup: View {

public init() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ import SwiftUI

public extension RichTextCommand {

/**
This group can add a list of superscript options to the
main menu, using an ``RichTextCommand/ActionButtonGroup``.
This view requires that a ``RichTextContext`` is set as
a focused value, otherwise it will be disabled.
*/
@available(*, deprecated, message: "Use RichTextCommand.ActionButtonGroup superscript initializer instead.")
struct SuperscriptOptionsGroup: View {

public var body: some View {
Expand Down

0 comments on commit 0d64a62

Please sign in to comment.