From e8aa17f2a0aadebec5145f49e40a90afcda11961 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Mon, 4 Mar 2024 15:00:28 +0100 Subject: [PATCH] Clean up code --- RELEASE_NOTES.md | 9 ++-- ...hTextAction+KeyboardShortcutModifier.swift | 2 +- ...xtAlignment+KeyboardShortcutModifier.swift | 2 +- .../Alignment/RichTextAlignment+Picker.swift | 13 ++---- .../RichTextKit/Context/RichTextContext.swift | 9 ++-- .../RichTextContextFocusedValueKey.swift | 43 +++++++++++++++++ .../Data/RichTextDataFormat+Menu.swift | 14 +++--- .../Editor/RichTextEditor+Config.swift | 2 +- .../Editor/RichTextEditor+Style.swift | 3 +- .../RichTextKit/Editor/RichTextEditor.swift | 4 +- .../{ => Editor}/RichTextView+Config.swift | 0 .../RichTextView+Config_AppKit.swift | 4 +- .../RichTextView+Config_UIKit.swift | 0 .../{ => Editor}/RichTextView+Theme.swift | 0 .../{ => Editor}/RichTextView_AppKit.swift | 0 .../{ => Editor}/RichTextView_UIKit.swift | 0 .../RichTextContextFocusedValueKey.swift | 46 ------------------- .../Fonts/RichTextFont+Picker.swift | 42 ++++++++++++++--- .../Fonts/RichTextFont+PickerFont.swift | 4 +- .../Keyboard/RichTextKeyboardToolbar.swift | 4 +- .../Bundle+Locale.swift} | 2 +- .../RichTextKit.docc/RichTextKit.md | 4 -- 22 files changed, 113 insertions(+), 94 deletions(-) create mode 100644 Sources/RichTextKit/Context/RichTextContextFocusedValueKey.swift rename Sources/RichTextKit/{ => Editor}/RichTextView+Config.swift (100%) rename Sources/RichTextKit/{ => Editor}/RichTextView+Config_AppKit.swift (89%) rename Sources/RichTextKit/{ => Editor}/RichTextView+Config_UIKit.swift (100%) rename Sources/RichTextKit/{ => Editor}/RichTextView+Theme.swift (100%) rename Sources/RichTextKit/{ => Editor}/RichTextView_AppKit.swift (100%) rename Sources/RichTextKit/{ => Editor}/RichTextView_UIKit.swift (100%) delete mode 100644 Sources/RichTextKit/Focus/RichTextContextFocusedValueKey.swift rename Sources/RichTextKit/{Bundle/Bundle+RichTextKit.swift => Localization/Bundle+Locale.swift} (96%) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 76193663e..194ad276f 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -12,15 +12,16 @@ This release removes all deprecated code and cleans up the library. ### ✨ Features -* There are new `richTextEditor` config and style view extensions. -* There are new `richTextKeyboardToolbar` config and style view extensions. +* There are new `richTextEditor` config and style view modifiers. +* There are new `richTextKeyboardToolbar` config and style view modifiers. ### 💥 Breaking Changes * All previously deprecated code has been deleted. -* `RichTextEditor` is configured with view extensions instead of the initializer. -* `RichTextKeyboardToolbar` is configured with view extensions instead of the initializer. +* `RichTextContextFocusedValueKey` is renamed to `RichTextContext.FocusedValueKey` +* `RichTextEditor` is configured with view modifiers instead of the initializer. +* `RichTextKeyboardToolbar` is configured with view modifiers instead of the initializer. diff --git a/Sources/RichTextKit/Actions/RichTextAction+KeyboardShortcutModifier.swift b/Sources/RichTextKit/Actions/RichTextAction+KeyboardShortcutModifier.swift index 7fc29555c..a2cfde709 100644 --- a/Sources/RichTextKit/Actions/RichTextAction+KeyboardShortcutModifier.swift +++ b/Sources/RichTextKit/Actions/RichTextAction+KeyboardShortcutModifier.swift @@ -15,7 +15,7 @@ public extension RichTextAction { ``RichTextAction`` to any view. You can also apply it with the `.keyboardShortcut(for:)` - view extension. + view modifier. */ struct KeyboardShortcutModifier: ViewModifier { diff --git a/Sources/RichTextKit/Alignment/RichTextAlignment+KeyboardShortcutModifier.swift b/Sources/RichTextKit/Alignment/RichTextAlignment+KeyboardShortcutModifier.swift index f7cd2f4de..836f26f5f 100644 --- a/Sources/RichTextKit/Alignment/RichTextAlignment+KeyboardShortcutModifier.swift +++ b/Sources/RichTextKit/Alignment/RichTextAlignment+KeyboardShortcutModifier.swift @@ -15,7 +15,7 @@ public extension RichTextAlignment { ``RichTextAlignment`` to any view. You can also apply it with the `.keyboardShortcut(for:)` - view extension. + view modifier. */ struct KeyboardShortcutModifier: ViewModifier { diff --git a/Sources/RichTextKit/Alignment/RichTextAlignment+Picker.swift b/Sources/RichTextKit/Alignment/RichTextAlignment+Picker.swift index 5c0a554a0..5f62258be 100644 --- a/Sources/RichTextKit/Alignment/RichTextAlignment+Picker.swift +++ b/Sources/RichTextKit/Alignment/RichTextAlignment+Picker.swift @@ -59,14 +59,11 @@ struct RichTextAlignment_Picker_Previews: PreviewProvider { private var alignment = RichTextAlignment.left var body: some View { - VStack(spacing: 10) { - RichTextAlignment.Picker( - selection: $alignment, - values: .all - ) - .withPreviewPickerStyles() - } - .padding() + RichTextAlignment.Picker( + selection: $alignment, + values: .all + ) + .withPreviewPickerStyles() } } diff --git a/Sources/RichTextKit/Context/RichTextContext.swift b/Sources/RichTextKit/Context/RichTextContext.swift index 4fd106eaa..1435bfe92 100644 --- a/Sources/RichTextKit/Context/RichTextContext.swift +++ b/Sources/RichTextKit/Context/RichTextContext.swift @@ -11,14 +11,13 @@ import Combine /** This observable context can be used to affect and observe a - ``RichTextEditor`` and its content. + ``RichTextEditor`` and its native text view. 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. + + You can use ``RichTextContext/FocusedValueKey`` to handle a + context with focus in a multi-windowed app. */ public class RichTextContext: ObservableObject { diff --git a/Sources/RichTextKit/Context/RichTextContextFocusedValueKey.swift b/Sources/RichTextKit/Context/RichTextContextFocusedValueKey.swift new file mode 100644 index 000000000..be5e22fdd --- /dev/null +++ b/Sources/RichTextKit/Context/RichTextContextFocusedValueKey.swift @@ -0,0 +1,43 @@ +// +// RichTextContextFocusedValueKey.swift +// RichTextKit +// +// Created by Daniel Saidi on 2022-12-19. +// Copyright © 2022-2024 Daniel Saidi. All rights reserved. +// + +import SwiftUI + +public extension RichTextContext { + + /// This key can be used to keep track of a context in a + /// multi-windowed app. + struct FocusedValueKey: SwiftUI.FocusedValueKey { + + public typealias Value = RichTextContext + } +} + +public extension FocusedValues { + + /// This value can be used to keep track of a context in + /// a multi-windowed app. + /// + /// You can bind a context to a view with `focusedValue`: + /// + /// ```swift + /// RichTextEditor(...) + /// .focusedValue(\.richTextContext, richTextContext) + /// ``` + /// + /// You can then access the context as a `@FocusedValue`: + /// + /// ```swift + /// @FocusedValue(\.richTextContext) + /// var richTextContext: RichTextContext? + /// ``` + var richTextContext: RichTextContext.FocusedValueKey.Value? { + get { self[RichTextContext.FocusedValueKey.self] } + set { self[RichTextContext.FocusedValueKey.self] = newValue } + } +} diff --git a/Sources/RichTextKit/Data/RichTextDataFormat+Menu.swift b/Sources/RichTextKit/Data/RichTextDataFormat+Menu.swift index 955873d39..07605c1f3 100644 --- a/Sources/RichTextKit/Data/RichTextDataFormat+Menu.swift +++ b/Sources/RichTextKit/Data/RichTextDataFormat+Menu.swift @@ -68,14 +68,12 @@ public extension RichTextDataFormat { struct RichTextData_FormatMenu_Previews: PreviewProvider { static var previews: some View { - VStack { - RichTextDataFormat.Menu( - title: "Export...", - icon: .richTextExport, - formatAction: { _ in }, - pdfAction: {} - ) - } + RichTextDataFormat.Menu( + title: "Export...", + icon: .richTextExport, + formatAction: { _ in }, + pdfAction: {} + ) } } #endif diff --git a/Sources/RichTextKit/Editor/RichTextEditor+Config.swift b/Sources/RichTextKit/Editor/RichTextEditor+Config.swift index 974c713a0..34cbc199d 100644 --- a/Sources/RichTextKit/Editor/RichTextEditor+Config.swift +++ b/Sources/RichTextKit/Editor/RichTextEditor+Config.swift @@ -6,7 +6,7 @@ // Copyright © 2024 Daniel Saidi. All rights reserved. // -#if iOS || macOS || os(visionOS) +#if iOS || macOS || os(tvOS) || os(visionOS) import SwiftUI /// This struct be used to configure a ``RichTextEditor``. diff --git a/Sources/RichTextKit/Editor/RichTextEditor+Style.swift b/Sources/RichTextKit/Editor/RichTextEditor+Style.swift index 07981104a..da3c9c214 100644 --- a/Sources/RichTextKit/Editor/RichTextEditor+Style.swift +++ b/Sources/RichTextKit/Editor/RichTextEditor+Style.swift @@ -6,7 +6,7 @@ // -#if iOS || macOS || os(visionOS) +#if iOS || macOS || os(tvOS) || os(visionOS) import SwiftUI /// This struct can be used to style a ``RichTextEditor``. @@ -37,5 +37,4 @@ public extension EnvironmentValues { set { self [RichTextEditorStyle.Key.self] = newValue } } } - #endif diff --git a/Sources/RichTextKit/Editor/RichTextEditor.swift b/Sources/RichTextKit/Editor/RichTextEditor.swift index 195b52932..e1f7dad08 100644 --- a/Sources/RichTextKit/Editor/RichTextEditor.swift +++ b/Sources/RichTextKit/Editor/RichTextEditor.swift @@ -37,8 +37,8 @@ import SwiftUI This will not show anything. To work around this limitation, use a ``RichTextKeyboardToolbar`` instead. - You can configure and style the view by applying config and - style view extensions to your view hierarchy: + You can configure and style the view by applying its config + and style view modifiers to your view hierarchy: ```swift VStack { diff --git a/Sources/RichTextKit/RichTextView+Config.swift b/Sources/RichTextKit/Editor/RichTextView+Config.swift similarity index 100% rename from Sources/RichTextKit/RichTextView+Config.swift rename to Sources/RichTextKit/Editor/RichTextView+Config.swift diff --git a/Sources/RichTextKit/RichTextView+Config_AppKit.swift b/Sources/RichTextKit/Editor/RichTextView+Config_AppKit.swift similarity index 89% rename from Sources/RichTextKit/RichTextView+Config_AppKit.swift rename to Sources/RichTextKit/Editor/RichTextView+Config_AppKit.swift index 8e9b6d364..babc9b9b1 100644 --- a/Sources/RichTextKit/RichTextView+Config_AppKit.swift +++ b/Sources/RichTextKit/Editor/RichTextView+Config_AppKit.swift @@ -1,7 +1,9 @@ // // RichTextView+Config_AppKit.swift +// RichTextKit // -// Created by Dominik Bucher on 13.02.2024. +// Created by Daniel Saidi on 2024-01-16. +// Copyright © 2024 Daniel Saidi. All rights reserved. // #if macOS diff --git a/Sources/RichTextKit/RichTextView+Config_UIKit.swift b/Sources/RichTextKit/Editor/RichTextView+Config_UIKit.swift similarity index 100% rename from Sources/RichTextKit/RichTextView+Config_UIKit.swift rename to Sources/RichTextKit/Editor/RichTextView+Config_UIKit.swift diff --git a/Sources/RichTextKit/RichTextView+Theme.swift b/Sources/RichTextKit/Editor/RichTextView+Theme.swift similarity index 100% rename from Sources/RichTextKit/RichTextView+Theme.swift rename to Sources/RichTextKit/Editor/RichTextView+Theme.swift diff --git a/Sources/RichTextKit/RichTextView_AppKit.swift b/Sources/RichTextKit/Editor/RichTextView_AppKit.swift similarity index 100% rename from Sources/RichTextKit/RichTextView_AppKit.swift rename to Sources/RichTextKit/Editor/RichTextView_AppKit.swift diff --git a/Sources/RichTextKit/RichTextView_UIKit.swift b/Sources/RichTextKit/Editor/RichTextView_UIKit.swift similarity index 100% rename from Sources/RichTextKit/RichTextView_UIKit.swift rename to Sources/RichTextKit/Editor/RichTextView_UIKit.swift diff --git a/Sources/RichTextKit/Focus/RichTextContextFocusedValueKey.swift b/Sources/RichTextKit/Focus/RichTextContextFocusedValueKey.swift deleted file mode 100644 index dee1de07e..000000000 --- a/Sources/RichTextKit/Focus/RichTextContextFocusedValueKey.swift +++ /dev/null @@ -1,46 +0,0 @@ -// -// RichTextContextFocusedValueKey.swift -// RichTextKit -// -// Created by Daniel Saidi on 2022-12-19. -// Copyright © 2022-2024 Daniel Saidi. All rights reserved. -// - -import SwiftUI - -/** - This key can be used to keep track of the currently focused - ``RichTextContext`` in a multi-window app. - */ -public struct RichTextContextFocusedValueKey: FocusedValueKey { - - public typealias Value = RichTextContext -} - -public extension FocusedValues { - - /** - This value can be used to bind a rich text context to a - view, using the `focusedValue` view modifier: - - ```swift - RichTextEditor(...) - .focusedValue(\.richTextContext, richTextContext) - ``` - - You can then access the currently focused context using - the `@FocusedValue` property wrapper: - - ```swift - @FocusedValue(\.richTextContext) - var richTextContext: RichTextContext? - ``` - - This is needed for e.g. main menu commands to determine - which context to affect, if any. - */ - var richTextContext: RichTextContextFocusedValueKey.Value? { - get { self[RichTextContextFocusedValueKey.self] } - set { self[RichTextContextFocusedValueKey.self] = newValue } - } -} diff --git a/Sources/RichTextKit/Fonts/RichTextFont+Picker.swift b/Sources/RichTextKit/Fonts/RichTextFont+Picker.swift index ea8c4fe85..9f2ac32ef 100644 --- a/Sources/RichTextKit/Fonts/RichTextFont+Picker.swift +++ b/Sources/RichTextKit/Fonts/RichTextFont+Picker.swift @@ -117,14 +117,42 @@ struct RichTextFont_Picker_Previews: PreviewProvider { extension View { func withPreviewPickerStyles() -> some View { - VStack(spacing: 10) { + NavigationView { + VStack(spacing: 10) { + self.label("Default") + self.pickerStyle(.automatic).label(".automatic") + self.pickerStyle(.inline).label(".inline") + #if iOS || macOS + self.pickerStyle(.menu).label(".menu") + #endif + if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { + pickerStyle(.navigationLink).label(".navigationLink") + } + #if iOS || macOS + if #available(iOS 17.0, macOS 14.0, watchOS 10.0, *) { + pickerStyle(.palette).label(".palette") + } + #endif + #if iOS || macOS || os(tvOS) || os(visionOS) + self.pickerStyle(.segmented).label(".segmented") + #endif + #if iOS || macOS + pickerStyle(.wheel).label(".wheel") + #endif + } + } + } +} + +private extension View { + + func label(_ title: String) -> some View { + VStack { + Text(title) + .font(.footnote) + .foregroundStyle(.secondary) self - self.pickerStyle(.inline) - #if iOS || macOS - self.pickerStyle(.menu) - #elseif iOS || macOS || os(tvOS) || os(visionOS) - self.pickerStyle(.segmented) - #endif + Divider() } } } diff --git a/Sources/RichTextKit/Fonts/RichTextFont+PickerFont.swift b/Sources/RichTextKit/Fonts/RichTextFont+PickerFont.swift index 98fe11a8c..ad8b1259b 100644 --- a/Sources/RichTextKit/Fonts/RichTextFont+PickerFont.swift +++ b/Sources/RichTextKit/Fonts/RichTextFont+PickerFont.swift @@ -37,7 +37,9 @@ public extension RichTextFont { */ struct PickerFont: Identifiable, Equatable { - public init(fontName: String) { + public init( + fontName: String + ) { let fontName = fontName.capitalized self.fontName = fontName self.fontDisplayName = "" diff --git a/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift b/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift index 8cb17ce0c..9f6edfe8e 100644 --- a/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift +++ b/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift @@ -48,8 +48,8 @@ import SwiftUI `$0` to use these standard views, or return any custom view that you want to use instead. - You can configure and style the view by applying config and - style view extensions to your view hierarchy: + You can configure and style the view by applying its config + and style view modifiers to your view hierarchy: ```swift VStack { diff --git a/Sources/RichTextKit/Bundle/Bundle+RichTextKit.swift b/Sources/RichTextKit/Localization/Bundle+Locale.swift similarity index 96% rename from Sources/RichTextKit/Bundle/Bundle+RichTextKit.swift rename to Sources/RichTextKit/Localization/Bundle+Locale.swift index 9d33b9765..d20dcf69b 100644 --- a/Sources/RichTextKit/Bundle/Bundle+RichTextKit.swift +++ b/Sources/RichTextKit/Localization/Bundle+Locale.swift @@ -1,5 +1,5 @@ // -// Bundle+RichTextKit.swift +// Bundle+Locale.swift // RichTextKit // // Created by Daniel Saidi on 2022-08-21. diff --git a/Sources/RichTextKit/RichTextKit.docc/RichTextKit.md b/Sources/RichTextKit/RichTextKit.docc/RichTextKit.md index a84ee6830..a0d2cc4f1 100644 --- a/Sources/RichTextKit/RichTextKit.docc/RichTextKit.md +++ b/Sources/RichTextKit/RichTextKit.docc/RichTextKit.md @@ -110,10 +110,6 @@ RichTextKit is available under the MIT license. See the [LICENSE][License] file - ``StandardRichTextExportService`` - ``StandardRichTextExportUrlResolver`` -### Focus - -- ``RichTextContextFocusedValueKey`` - ### Fonts - ``FontRepresentable``