Skip to content

Commit

Permalink
Add font size picker config environment support
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Mar 4, 2024
1 parent ee3134b commit 009be5d
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions Sources/RichTextKit/Editor/RichTextEditor+Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions Sources/RichTextKit/Editor/RichTextEditor+Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
7 changes: 4 additions & 3 deletions Sources/RichTextKit/Fonts/RichTextFont+PickerConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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()
Expand All @@ -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 }
Expand Down
28 changes: 18 additions & 10 deletions Sources/RichTextKit/Fonts/RichTextFont+SizePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<CGFloat>,
values: [CGFloat] = standardValues
selection: Binding<CGFloat>
) {
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) {
Expand All @@ -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],
Expand Down
64 changes: 64 additions & 0 deletions Sources/RichTextKit/Fonts/RichTextFont+SizePickerConfig.swift
Original file line number Diff line number Diff line change
@@ -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 }
}
}
28 changes: 18 additions & 10 deletions Sources/RichTextKit/Fonts/RichTextFont+SizePickerStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
13 changes: 5 additions & 8 deletions Sources/RichTextKit/Line/RichTextLine+SpacingPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}

Expand Down
7 changes: 4 additions & 3 deletions Sources/RichTextKit/Localization/RTKL10n.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public enum RTKL10n: String, CaseIterable, Identifiable {
fileFormatPdf,
fileFormatRtf,
fileFormatTxt,

lineSpacing,
lineSpacingIncrease,
lineSpacingDecrease,

menuExport,
menuExportAs,
Expand All @@ -51,9 +55,6 @@ public enum RTKL10n: String, CaseIterable, Identifiable {
menuIndent,
menuIndentIncrease,
menuIndentDecrease,
menuLineSpacing,
menuLineSpacingIncrease,
menuLineSpacingDecrease,
menuPrint,
menuSave,
menuSaveAs,
Expand Down
7 changes: 4 additions & 3 deletions Sources/RichTextKit/Resources/da.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -49,9 +53,6 @@
"menuIndent" = "Indrag";
"menuIndentIncrease" = "Øge";
"menuIndentDecrease" = "Mindske";
"menuLineSpacing" = "Linjeafstand";
"menuLineSpacingIncrease" = "Increase";
"menuLineSpacingDecrease" = "Decrease";
"menuPrint" = "Udskriv...";
"menuSave" = "Spare";
"menuSaveAs" = "Spare som...";
Expand Down
11 changes: 6 additions & 5 deletions Sources/RichTextKit/Resources/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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...";
Expand Down
7 changes: 4 additions & 3 deletions Sources/RichTextKit/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -49,9 +53,6 @@
"menuIndent" = "Indent";
"menuIndentIncrease" = "Increase";
"menuIndentDecrease" = "Decrease";
"menuLineSpacing" = "LineSpacing";
"menuLineSpacingIncrease" = "Increase";
"menuLineSpacingDecrease" = "Decrease";
"menuPrint" = "Print...";
"menuSave" = "Save";
"menuSaveAs" = "Save as...";
Expand Down
Loading

0 comments on commit 009be5d

Please sign in to comment.