From 0d64a627db729ca0255df5f1e0248ed1fc7bb393 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Thu, 15 Feb 2024 07:52:18 +0100 Subject: [PATCH] Replace a bunch of command views with new action group initializers --- RELEASE_NOTES.md | 10 +++- .../RichTextCommand+ActionButtonGroup.swift | 58 ++++++++++++++++++- .../Commands/RichTextCommand+FormatMenu.swift | 19 +++--- .../Commands/RichTextCommand+ShareMenu.swift | 6 +- .../Commands/RichTextCommand.swift | 4 +- ...ichTextCommand+AlignmentOptionsGroup.swift | 8 +-- ...RichTextCommand+FontSizeOptionsGroup.swift | 8 +-- .../RichTextCommand+IndentOptionsGroup.swift | 8 +-- .../RichTextCommand+StyleOptionsGroup.swift | 8 +-- ...hTextCommand+SuperscriptOptionsGroup.swift | 8 +-- 10 files changed, 86 insertions(+), 51 deletions(-) rename Sources/RichTextKit/{Commands => _Deprecated}/RichTextCommand+AlignmentOptionsGroup.swift (66%) rename Sources/RichTextKit/{Commands => _Deprecated}/RichTextCommand+FontSizeOptionsGroup.swift (67%) rename Sources/RichTextKit/{Commands => _Deprecated}/RichTextCommand+IndentOptionsGroup.swift (65%) rename Sources/RichTextKit/{Commands => _Deprecated}/RichTextCommand+StyleOptionsGroup.swift (66%) rename Sources/RichTextKit/{Commands => _Deprecated}/RichTextCommand+SuperscriptOptionsGroup.swift (65%) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 35e7eaa7f..897e32b99 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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. @@ -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 diff --git a/Sources/RichTextKit/Commands/RichTextCommand+ActionButtonGroup.swift b/Sources/RichTextKit/Commands/RichTextCommand+ActionButtonGroup.swift index 568ca1b08..9ad313f8b 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+ActionButtonGroup.swift +++ b/Sources/RichTextKit/Commands/RichTextCommand+ActionButtonGroup.swift @@ -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. @@ -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 + } +} diff --git a/Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift b/Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift index 7819c4ba0..f58278c06 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift +++ b/Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift @@ -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 { @@ -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) diff --git a/Sources/RichTextKit/Commands/RichTextCommand+ShareMenu.swift b/Sources/RichTextKit/Commands/RichTextCommand+ShareMenu.swift index 00fe74edd..d42e93d6e 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+ShareMenu.swift +++ b/Sources/RichTextKit/Commands/RichTextCommand+ShareMenu.swift @@ -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 { @@ -87,7 +86,8 @@ public extension RichTextCommand { shareMenu exportMenu printButton - }.disabled(!isEnabled) + } + .disabled(!isEnabled) } } } diff --git a/Sources/RichTextKit/Commands/RichTextCommand.swift b/Sources/RichTextKit/Commands/RichTextCommand.swift index da825ab59..beb419bb5 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand.swift +++ b/Sources/RichTextKit/Commands/RichTextCommand.swift @@ -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 {} diff --git a/Sources/RichTextKit/Commands/RichTextCommand+AlignmentOptionsGroup.swift b/Sources/RichTextKit/_Deprecated/RichTextCommand+AlignmentOptionsGroup.swift similarity index 66% rename from Sources/RichTextKit/Commands/RichTextCommand+AlignmentOptionsGroup.swift rename to Sources/RichTextKit/_Deprecated/RichTextCommand+AlignmentOptionsGroup.swift index ce49b295a..d629df989 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+AlignmentOptionsGroup.swift +++ b/Sources/RichTextKit/_Deprecated/RichTextCommand+AlignmentOptionsGroup.swift @@ -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() {} diff --git a/Sources/RichTextKit/Commands/RichTextCommand+FontSizeOptionsGroup.swift b/Sources/RichTextKit/_Deprecated/RichTextCommand+FontSizeOptionsGroup.swift similarity index 67% rename from Sources/RichTextKit/Commands/RichTextCommand+FontSizeOptionsGroup.swift rename to Sources/RichTextKit/_Deprecated/RichTextCommand+FontSizeOptionsGroup.swift index edc252575..966fb65e1 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+FontSizeOptionsGroup.swift +++ b/Sources/RichTextKit/_Deprecated/RichTextCommand+FontSizeOptionsGroup.swift @@ -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() {} diff --git a/Sources/RichTextKit/Commands/RichTextCommand+IndentOptionsGroup.swift b/Sources/RichTextKit/_Deprecated/RichTextCommand+IndentOptionsGroup.swift similarity index 65% rename from Sources/RichTextKit/Commands/RichTextCommand+IndentOptionsGroup.swift rename to Sources/RichTextKit/_Deprecated/RichTextCommand+IndentOptionsGroup.swift index ad347802c..88f750919 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+IndentOptionsGroup.swift +++ b/Sources/RichTextKit/_Deprecated/RichTextCommand+IndentOptionsGroup.swift @@ -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 { diff --git a/Sources/RichTextKit/Commands/RichTextCommand+StyleOptionsGroup.swift b/Sources/RichTextKit/_Deprecated/RichTextCommand+StyleOptionsGroup.swift similarity index 66% rename from Sources/RichTextKit/Commands/RichTextCommand+StyleOptionsGroup.swift rename to Sources/RichTextKit/_Deprecated/RichTextCommand+StyleOptionsGroup.swift index fbcbedc0e..d46e7fa88 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+StyleOptionsGroup.swift +++ b/Sources/RichTextKit/_Deprecated/RichTextCommand+StyleOptionsGroup.swift @@ -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() {} diff --git a/Sources/RichTextKit/Commands/RichTextCommand+SuperscriptOptionsGroup.swift b/Sources/RichTextKit/_Deprecated/RichTextCommand+SuperscriptOptionsGroup.swift similarity index 65% rename from Sources/RichTextKit/Commands/RichTextCommand+SuperscriptOptionsGroup.swift rename to Sources/RichTextKit/_Deprecated/RichTextCommand+SuperscriptOptionsGroup.swift index ef6ed1382..75f5c0713 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+SuperscriptOptionsGroup.swift +++ b/Sources/RichTextKit/_Deprecated/RichTextCommand+SuperscriptOptionsGroup.swift @@ -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 {