From 009be5d613b6b4cfbca93833bb88a873506df6b8 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Mon, 4 Mar 2024 15:57:49 +0100 Subject: [PATCH] Add font size picker config environment support --- .../Commands/RichTextCommand+FormatMenu.swift | 2 +- .../Editor/RichTextEditor+Config.swift | 1 + .../Editor/RichTextEditor+Style.swift | 1 + .../Fonts/RichTextFont+PickerConfig.swift | 7 +- .../Fonts/RichTextFont+SizePicker.swift | 28 +++++--- .../Fonts/RichTextFont+SizePickerConfig.swift | 64 +++++++++++++++++++ .../Fonts/RichTextFont+SizePickerStack.swift | 28 +++++--- .../Format/RichTextFormatToolbar+Style.swift | 2 +- .../RichTextKeyboardToolbar+Config.swift | 1 + .../RichTextKeyboardToolbar+Style.swift | 1 + .../Line/RichTextLine+SpacingPicker.swift | 13 ++-- .../RichTextKit/Localization/RTKL10n.swift | 7 +- .../Resources/da.lproj/Localizable.strings | 7 +- .../Resources/de.lproj/Localizable.strings | 11 ++-- .../Resources/en.lproj/Localizable.strings | 7 +- .../Resources/nb.lproj/Localizable.strings | 7 +- .../Resources/sv.lproj/Localizable.strings | 7 +- 17 files changed, 141 insertions(+), 53 deletions(-) create mode 100644 Sources/RichTextKit/Fonts/RichTextFont+SizePickerConfig.swift diff --git a/Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift b/Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift index ba63ac811..4a026d84f 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift +++ b/Sources/RichTextKit/Commands/RichTextCommand+FormatMenu.swift @@ -83,7 +83,7 @@ public extension RichTextCommand.FormatMenu { Group(indent: true) } case .lineSpacing: - Menu(RTKL10n.menuLineSpacing.text) { + Menu(RTKL10n.lineSpacing.text) { Group(lineSpacing: true) } case .superscript: diff --git a/Sources/RichTextKit/Editor/RichTextEditor+Config.swift b/Sources/RichTextKit/Editor/RichTextEditor+Config.swift index fffdb927b..fda27da40 100644 --- a/Sources/RichTextKit/Editor/RichTextEditor+Config.swift +++ b/Sources/RichTextKit/Editor/RichTextEditor+Config.swift @@ -32,6 +32,7 @@ private extension RichTextEditorConfig { public extension EnvironmentValues { + /// This value can bind to a rich text editor config. var richTextEditorConfig: RichTextEditorConfig { get { self [RichTextEditorConfig.Key.self] } set { self [RichTextEditorConfig.Key.self] = newValue } diff --git a/Sources/RichTextKit/Editor/RichTextEditor+Style.swift b/Sources/RichTextKit/Editor/RichTextEditor+Style.swift index 2c5543f62..70fdad7b4 100644 --- a/Sources/RichTextKit/Editor/RichTextEditor+Style.swift +++ b/Sources/RichTextKit/Editor/RichTextEditor+Style.swift @@ -32,6 +32,7 @@ private extension RichTextEditorStyle { public extension EnvironmentValues { + /// This value can bind to a rich text editor style. var richTextEditorStyle: RichTextEditorStyle { get { self [RichTextEditorStyle.Key.self] } set { self [RichTextEditorStyle.Key.self] = newValue } diff --git a/Sources/RichTextKit/Fonts/RichTextFont+PickerConfig.swift b/Sources/RichTextKit/Fonts/RichTextFont+PickerConfig.swift index 84fd51030..19b4f67c7 100644 --- a/Sources/RichTextKit/Fonts/RichTextFont+PickerConfig.swift +++ b/Sources/RichTextKit/Fonts/RichTextFont+PickerConfig.swift @@ -10,14 +10,14 @@ import SwiftUI public extension RichTextFont { - /// This struct can configure a ``RichTextFont/Picker``. + /// This type can configure a ``RichTextFont/Picker``. /// /// This configuration contains configuration properties /// for many different font pickers types. Some of these /// properties are not used in some pickers. struct PickerConfig { - /// Create a custom rich text font picker config. + /// Create a custom font picker config. /// /// - Parameters: /// - fonts: The fonts to display in the list, by default `all`. @@ -52,7 +52,7 @@ public extension RichTextFont { public extension RichTextFont.PickerConfig { - /// The standard rich text font picker configuration. + /// The standard font picker configuration. /// /// You can set a new value to change the global default. static var standard = Self() @@ -78,6 +78,7 @@ private extension RichTextFont.PickerConfig { public extension EnvironmentValues { + /// This value can bind to a font picker config. var richTextFontPickerConfig: RichTextFont.PickerConfig { get { self [RichTextFont.PickerConfig.Key.self] } set { self [RichTextFont.PickerConfig.Key.self] = newValue } diff --git a/Sources/RichTextKit/Fonts/RichTextFont+SizePicker.swift b/Sources/RichTextKit/Fonts/RichTextFont+SizePicker.swift index 533c22016..0a7ddc41e 100644 --- a/Sources/RichTextKit/Fonts/RichTextFont+SizePicker.swift +++ b/Sources/RichTextKit/Fonts/RichTextFont+SizePicker.swift @@ -15,6 +15,17 @@ public extension RichTextFont { The view returns a plain SwiftUI `Picker` view that can be styled and configured with plain SwiftUI. + + You can configure this picker by applying a config view + modifier to your view hierarchy: + + ```swift + VStack { + RichTextFont.SizePicker(...) + ... + } + .richTextFontSizePickerConfig(...) + ``` */ struct SizePicker: View { @@ -23,23 +34,25 @@ public extension RichTextFont { - Parameters: - selection: The selected font size. - - values: The values to display in the list. */ public init( - selection: Binding, - values: [CGFloat] = standardValues + selection: Binding ) { self._selection = selection + self.values = [] self.values = Self.values( - for: values, + for: config.values, selection: selection.wrappedValue ) } - private let values: [CGFloat] + private var values: [CGFloat] @Binding private var selection: CGFloat + + @Environment(\.richTextFontSizePickerConfig) + private var config public var body: some View { SwiftUI.Picker(RTKL10n.fontSize.text, selection: $selection) { @@ -54,11 +67,6 @@ public extension RichTextFont { public extension RichTextFont.SizePicker { - /// The standard picker values. - static var standardValues: [CGFloat] { - [10, 12, 14, 18, 20, 22, 24, 28, 36, 48, 64, 72, 96, 144] - } - /// Get a list of values for a certain selection. static func values( for values: [CGFloat], diff --git a/Sources/RichTextKit/Fonts/RichTextFont+SizePickerConfig.swift b/Sources/RichTextKit/Fonts/RichTextFont+SizePickerConfig.swift new file mode 100644 index 000000000..a18593c35 --- /dev/null +++ b/Sources/RichTextKit/Fonts/RichTextFont+SizePickerConfig.swift @@ -0,0 +1,64 @@ +// +// RichTextFont+SizePickerConfig.swift +// RichTextKit +// +// Created by Daniel Saidi on 2024-03-04. +// Copyright © 2024 Daniel Saidi. All rights reserved. +// + +import SwiftUI + +public extension RichTextFont { + + /// This type can configure a ``RichTextFont/SizePicker``. + struct SizePickerConfig { + + /// Create a custom font size picker config. + /// + /// - Parameters: + /// - values: The values to display in the list, by default a standard list. + public init( + values: [CGFloat] = [10, 12, 14, 18, 20, 22, 24, 28, 36, 48, 64, 72, 96, 144] + ) { + self.values = values + } + + /// The values to display in the list. + public var values: [CGFloat] + } +} + +public extension RichTextFont.SizePickerConfig { + + /// The standard font size picker configuration. + /// + /// You can set a new value to change the global default. + static var standard = Self() +} + +public extension View { + + /// Apply a ``RichTextFont`` size picker configuration. + func richTextFontSizePickerConfig( + _ config: RichTextFont.SizePickerConfig + ) -> some View { + self.environment(\.richTextFontSizePickerConfig, config) + } +} + +private extension RichTextFont.SizePickerConfig { + + struct Key: EnvironmentKey { + + public static var defaultValue: RichTextFont.SizePickerConfig = .standard + } +} + +public extension EnvironmentValues { + + /// This value can bind to a font size picker config. + var richTextFontSizePickerConfig: RichTextFont.SizePickerConfig { + get { self [RichTextFont.SizePickerConfig.Key.self] } + set { self [RichTextFont.SizePickerConfig.Key.self] = newValue } + } +} diff --git a/Sources/RichTextKit/Fonts/RichTextFont+SizePickerStack.swift b/Sources/RichTextKit/Fonts/RichTextFont+SizePickerStack.swift index a1cd36653..2e6b4dc03 100644 --- a/Sources/RichTextKit/Fonts/RichTextFont+SizePickerStack.swift +++ b/Sources/RichTextKit/Fonts/RichTextFont+SizePickerStack.swift @@ -14,6 +14,17 @@ public extension RichTextFont { /** This view uses a ``RichTextFont/SizePicker`` and button steppers to increment and a decrement the font size. + + You can configure this picker by applying a config view + modifier to your view hierarchy: + + ```swift + VStack { + RichTextFont.SizePickerStack(...) + ... + } + .richTextFontSizePickerConfig(...) + ``` */ struct SizePickerStack: View { @@ -22,18 +33,13 @@ public extension RichTextFont { - Parameters: - context: The context to affect. - - values: The values to display, by default ``RichTextFont/SizePicker/standardValues``. */ public init( - context: RichTextContext, - values: [CGFloat] = RichTextFont.SizePicker.standardValues + context: RichTextContext ) { self._context = ObservedObject(wrappedValue: context) - self.values = values } - private let values: [CGFloat] - private let step = 1 @ObservedObject @@ -80,14 +86,16 @@ private extension RichTextFont.SizePickerStack { var picker: some View { RichTextFont.SizePicker( - selection: $context.fontSize, - values: values + selection: $context.fontSize ) } var stepper: some View { - Stepper("", onIncrement: increment, onDecrement: decrement) - .labelsHidden() + Stepper( + RTKL10n.fontSize.text, + onIncrement: increment, + onDecrement: decrement + ) } func decrement() { diff --git a/Sources/RichTextKit/Format/RichTextFormatToolbar+Style.swift b/Sources/RichTextKit/Format/RichTextFormatToolbar+Style.swift index c98070f6a..bcef9f559 100644 --- a/Sources/RichTextKit/Format/RichTextFormatToolbar+Style.swift +++ b/Sources/RichTextKit/Format/RichTextFormatToolbar+Style.swift @@ -45,7 +45,7 @@ public extension View { public extension EnvironmentValues { - /// This environment value defines format toolbar styles. + /// This value can bind to a format toolbar style. var richTextFormatToolbarStyle: RichTextFormatToolbar.Style { get { self [RichTextFormatToolbar.StyleKey.self] } set { self [RichTextFormatToolbar.StyleKey.self] = newValue } diff --git a/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Config.swift b/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Config.swift index 29e2c5f17..382a299f5 100644 --- a/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Config.swift +++ b/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Config.swift @@ -66,6 +66,7 @@ private extension RichTextKeyboardToolbarConfig { public extension EnvironmentValues { + /// This value can bind to a keyboard toolbar config. var richTextKeyboardToolbarConfig: RichTextKeyboardToolbarConfig { get { self [RichTextKeyboardToolbarConfig.Key.self] } set { self [RichTextKeyboardToolbarConfig.Key.self] = newValue } diff --git a/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Style.swift b/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Style.swift index 34806e8a3..d7f2a3ba1 100644 --- a/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Style.swift +++ b/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar+Style.swift @@ -72,6 +72,7 @@ private extension RichTextKeyboardToolbarStyle { public extension EnvironmentValues { + /// This value can bind to a keyboard toolbar style. var richTextKeyboardToolbarStyle: RichTextKeyboardToolbarStyle { get { self [RichTextKeyboardToolbarStyle.Key.self] } set { self [RichTextKeyboardToolbarStyle.Key.self] = newValue } diff --git a/Sources/RichTextKit/Line/RichTextLine+SpacingPicker.swift b/Sources/RichTextKit/Line/RichTextLine+SpacingPicker.swift index 6c5850f11..213d75a7b 100644 --- a/Sources/RichTextKit/Line/RichTextLine+SpacingPicker.swift +++ b/Sources/RichTextKit/Line/RichTextLine+SpacingPicker.swift @@ -42,7 +42,7 @@ public extension RichTextLine { private var selection: CGFloat public var body: some View { - SwiftUI.Picker("", selection: $selection) { + SwiftUI.Picker(RTKL10n.lineSpacing.text, selection: $selection) { ForEach(values, id: \.self) { text(for: $0) .tag($0) @@ -89,13 +89,10 @@ struct RichTextFont_SpacingPicker_Previews: PreviewProvider { private var selection: CGFloat = 3.0 var body: some View { - List { - HStack { - RichTextLine.SpacingPicker( - selection: $selection - ) - } - } + RichTextLine.SpacingPicker( + selection: $selection + ) + .withPreviewPickerStyles() } } diff --git a/Sources/RichTextKit/Localization/RTKL10n.swift b/Sources/RichTextKit/Localization/RTKL10n.swift index 52ba4b837..923299b4e 100644 --- a/Sources/RichTextKit/Localization/RTKL10n.swift +++ b/Sources/RichTextKit/Localization/RTKL10n.swift @@ -43,6 +43,10 @@ public enum RTKL10n: String, CaseIterable, Identifiable { fileFormatPdf, fileFormatRtf, fileFormatTxt, + + lineSpacing, + lineSpacingIncrease, + lineSpacingDecrease, menuExport, menuExportAs, @@ -51,9 +55,6 @@ public enum RTKL10n: String, CaseIterable, Identifiable { menuIndent, menuIndentIncrease, menuIndentDecrease, - menuLineSpacing, - menuLineSpacingIncrease, - menuLineSpacingDecrease, menuPrint, menuSave, menuSaveAs, diff --git a/Sources/RichTextKit/Resources/da.lproj/Localizable.strings b/Sources/RichTextKit/Resources/da.lproj/Localizable.strings index e2316a505..8acc8ff4b 100644 --- a/Sources/RichTextKit/Resources/da.lproj/Localizable.strings +++ b/Sources/RichTextKit/Resources/da.lproj/Localizable.strings @@ -40,6 +40,10 @@ "fileFormatRtf" = "Formateret tekst (.rtf)"; "fileFormatTxt" = "Ren tekst (.txt)"; +"lineSpacing" = "Linjeafstand"; +"lineSpacingIncrease" = "Increase"; +"lineSpacingDecrease" = "Decrease"; + "menuExport" = "Eksportere"; "menuExportAs" = "Eksportere som..."; "menuFont" = "Skrifttype"; @@ -49,9 +53,6 @@ "menuIndent" = "Indrag"; "menuIndentIncrease" = "Øge"; "menuIndentDecrease" = "Mindske"; -"menuLineSpacing" = "Linjeafstand"; -"menuLineSpacingIncrease" = "Increase"; -"menuLineSpacingDecrease" = "Decrease"; "menuPrint" = "Udskriv..."; "menuSave" = "Spare"; "menuSaveAs" = "Spare som..."; diff --git a/Sources/RichTextKit/Resources/de.lproj/Localizable.strings b/Sources/RichTextKit/Resources/de.lproj/Localizable.strings index ceabd2f44..a654dc57c 100644 --- a/Sources/RichTextKit/Resources/de.lproj/Localizable.strings +++ b/Sources/RichTextKit/Resources/de.lproj/Localizable.strings @@ -27,8 +27,8 @@ "actionFontSizeDecrease" = "Schriftgröße verkleinern"; "actionIndentIncrease" = "Indent vergrößern"; "actionIndentDecrease" = "Indent verkleinern"; -"actionLineSpacingIncrease" = "Zeilenabstand vergrößern"; -"actionLineSpacingDecrease" = "Zeilenabstand verkleinern"; +"actionLineSpacingIncrease" = "Zeilenabstand Vergrößern"; +"actionLineSpacingDecrease" = "Zeilenabstand Verkleinern"; "actionPrint" = "Drucken"; "actionRedoLatestChange" = "Rückgängig"; "actionSuperscriptIncrease" = "Increase Superscript"; @@ -40,6 +40,10 @@ "fileFormatRtf" = "Formatierter Text (.rtf)"; "fileFormatTxt" = "Klartext (.txt)"; +"lineSpacing" = "Zeilenabstand"; +"lineSpacingIncrease" = "Vergrößern"; +"lineSpacingDecrease" = "Verkleinern"; + "menuExport" = "Exportieren"; "menuExportAs" = "Exportieren als..."; "menuFont" = "Schriftart"; @@ -49,9 +53,6 @@ "menuIndent" = "Indent"; "menuIndentIncrease" = "Vergrößern"; "menuIndentDecrease" = "Verkleinern"; -"menuLineSpacing" = "Zeilenabstand"; -"menuLineSpacingIncrease" = "Vergrößern"; -"menuLineSpacingDecrease" = "Verkleinern"; "menuPrint" = "Drucken..."; "menuSave" = "Speichern"; "menuSaveAs" = "Speichern als..."; diff --git a/Sources/RichTextKit/Resources/en.lproj/Localizable.strings b/Sources/RichTextKit/Resources/en.lproj/Localizable.strings index caf3f2b00..a68e8b1a2 100644 --- a/Sources/RichTextKit/Resources/en.lproj/Localizable.strings +++ b/Sources/RichTextKit/Resources/en.lproj/Localizable.strings @@ -40,6 +40,10 @@ "fileFormatRtf" = "Rich Text (.rtf)"; "fileFormatTxt" = "Plain Text (.txt)"; +"lineSpacing" = "Line Spacing"; +"lineSpacingIncrease" = "Increase"; +"lineSpacingDecrease" = "Decrease"; + "menuExport" = "Export"; "menuExportAs" = "Export as..."; "menuFont" = "Font"; @@ -49,9 +53,6 @@ "menuIndent" = "Indent"; "menuIndentIncrease" = "Increase"; "menuIndentDecrease" = "Decrease"; -"menuLineSpacing" = "LineSpacing"; -"menuLineSpacingIncrease" = "Increase"; -"menuLineSpacingDecrease" = "Decrease"; "menuPrint" = "Print..."; "menuSave" = "Save"; "menuSaveAs" = "Save as..."; diff --git a/Sources/RichTextKit/Resources/nb.lproj/Localizable.strings b/Sources/RichTextKit/Resources/nb.lproj/Localizable.strings index cc7f1cff9..05c8fb879 100644 --- a/Sources/RichTextKit/Resources/nb.lproj/Localizable.strings +++ b/Sources/RichTextKit/Resources/nb.lproj/Localizable.strings @@ -40,6 +40,10 @@ "fileFormatRtf" = "Formatert tekst (.rtf)"; "fileFormatTxt" = "Ren tekst (.txt)"; +"lineSpacing" = "Linjeavstand"; +"lineSpacingIncrease" = "Øk"; +"lineSpacingDecrease" = "Reduser"; + "menuExport" = "Eksporterer"; "menuExportAs" = "Eksporterer som..."; "menuFont" = "Skrifttype"; @@ -49,9 +53,6 @@ "menuIndent" = "Indrag"; "menuIndentIncrease" = "Øk"; "menuIndentDecrease" = "Reduser"; -"menuLineSpacing" = "Linjeavstand"; -"menuLineSpacingIncrease" = "Increase"; -"menuLineSpacingDecrease" = "Decrease"; "menuPrint" = "Skrive ut..."; "menuSave" = "Lagre"; "menuSaveAs" = "Lagre som..."; diff --git a/Sources/RichTextKit/Resources/sv.lproj/Localizable.strings b/Sources/RichTextKit/Resources/sv.lproj/Localizable.strings index e25068769..73a4c2a2d 100644 --- a/Sources/RichTextKit/Resources/sv.lproj/Localizable.strings +++ b/Sources/RichTextKit/Resources/sv.lproj/Localizable.strings @@ -40,6 +40,10 @@ "fileFormatRtf" = "Formaterad Text (.rtf)"; "fileFormatTxt" = "Ren Text (.txt)"; +"lineSpacing" = "Radavstånd"; +"lineSpacingIncrease" = "Öka"; +"lineSpacingDecrease" = "Minska"; + "menuExport" = "Exportera"; "menuExportAs" = "Exportera som..."; "menuFont" = "Typsnitt"; @@ -49,9 +53,6 @@ "menuIndent" = "Indrag"; "menuIndentIncrease" = "Öka"; "menuIndentDecrease" = "Minska"; -"menuLineSpacing" = "Radavstånd"; -"menuLineSpacingIncrease" = "Öka"; -"menuLineSpacingDecrease" = "Minska"; "menuPrint" = "Skriv ut..."; "menuSave" = "Spara"; "menuSaveAs" = "Spara som...";