Skip to content

Commit

Permalink
Move RichTextAlignment views into enum namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Jan 17, 2024
1 parent 85b3b60 commit 7e3d4a6
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 93 deletions.
9 changes: 8 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ Until then, minor updates may remove deprecated features and introduce breaking

## 0.9.8

This release starts moving types into namespaces, to make the surface area smaller.
This release starts moving types and views that relate to other types into the type namespaces, to make the surface area of the library smaller.

### ✨ Features

* `RichTextAlignment.Picker` has a new style parameter.

### 🗑️ Deprecations

* `RichTextActionButton` has been renamed to `RichTextAction.Button`.
* `RichTextActionButtonGroup` has been renamed to `RichTextAction.ButtonGroup`.
* `RichTextActionButtonStack` has been renamed to `RichTextAction.ButtonStack`.
* `RichTextAlignmentPicker` has been renamed to `RichTextAlignment.Picker`.



Expand Down
6 changes: 3 additions & 3 deletions Sources/RichTextKit/Actions/RichTextAction+ButtonGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public extension RichTextAction {
Create a rich text action button stack.

- Parameters:
- context: The context to affect.
- actions: The actions to list, by default all non-size actions.
- greedy: Whether or not the group is horizontally greedy, by default `true`.
- context: The context to affect.
- actions: The actions to list, by default all non-size actions.
- greedy: Whether or not the group is horizontally greedy, by default `true`.
*/
public init(
context: RichTextContext,
Expand Down
13 changes: 7 additions & 6 deletions Sources/RichTextKit/Actions/RichTextAction+ButtonStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ public extension RichTextAction {
/**
This view lists ``RichTextAction`` buttons in a stack.

Since this view controls multiple values, it binds directly
to a ``RichTextContext`` instead of to individual values.
Since this view uses multiple values, it binds directly
to a ``RichTextContext`` instead of individual values.
*/
struct ButtonStack: View {

/**
Create a rich text action button stack.

- Parameters:
- context: The context to affect.
- actions: The actions to list, by default all non-size actions.
- spacing: The spacing to apply to stack items, by default `5`.
- context: The context to affect.
- actions: The actions to list, by default all non-size actions.
- spacing: The spacing to apply to stack items, by default `5`.
*/
public init(
context: RichTextContext,
Expand All @@ -49,7 +49,8 @@ public extension RichTextAction {
action: $0,
context: context,
fillVertically: true
).frame(maxHeight: .infinity)
)
.frame(maxHeight: .infinity)
}
}
.fixedSize(horizontal: false, vertical: true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// View+RichTextAction.swift
// RichTextAction+KeyboardShortcutModifier.swift
// RichTextKit
//
// Created by Daniel Saidi on 2022-12-13.
Expand All @@ -11,13 +11,11 @@ import SwiftUI
public extension RichTextAction {

/**
This view modifier can be used to apply keyboard action
shortcuts to any view.
This view modifier can apply keyboard shortcuts for any
``RichTextAction`` to any view.

You can also apply it with the `.keyboardShortcut(for:)`
view extension.

> Note: This modifier only has effect on some platform.
*/
struct KeyboardShortcutModifier: ViewModifier {

Expand All @@ -28,31 +26,31 @@ public extension RichTextAction {
private let action: RichTextAction

public func body(content: Content) -> some View {
#if iOS || macOS || os(visionOS)
switch action {
case .copy: content.keyboardShortcut("c", modifiers: .command)
case .dismissKeyboard: content
case .print: content.keyboardShortcut("p", modifiers: .command)
case .redoLatestChange: content.keyboardShortcut("z", modifiers: [.command, .shift])
case .setAlignment(let align): content.keyboardShortcut(for: align)
case .stepFontSize(let points): content.keyboardShortcut(points < 0 ? "-" : "+", modifiers: .command)
case .stepIndent(let steps): content.keyboardShortcut(steps < 0 ? "Ö" : "Ä", modifiers: .command)
case .stepSuperscript: content
case .toggleStyle(let style): content.keyboardShortcut(for: style)
case .undoLatestChange: content.keyboardShortcut("z", modifiers: .command)
}
#else
content
#endif
content.keyboardShortcut(for: action)
}
}
}

public extension View {

/// Apply a rich text action keyboard shortcut.
/// Apply a ``RichTextAction/KeyboardShortcutModifier``.
@ViewBuilder
func keyboardShortcut(for action: RichTextAction) -> some View {
modifier(RichTextAction.KeyboardShortcutModifier(action))
#if iOS || macOS || os(visionOS)
switch action {
case .copy: keyboardShortcut("c", modifiers: .command)
case .dismissKeyboard: self
case .print: keyboardShortcut("p", modifiers: .command)
case .redoLatestChange: keyboardShortcut("z", modifiers: [.command, .shift])
case .setAlignment(let align): keyboardShortcut(for: align)
case .stepFontSize(let points): keyboardShortcut(points < 0 ? "-" : "+", modifiers: .command)
case .stepIndent(let steps): keyboardShortcut(steps < 0 ? "Ö" : "Ä", modifiers: .command)
case .stepSuperscript: self
case .toggleStyle(let style): keyboardShortcut(for: style)
case .undoLatestChange: keyboardShortcut("z", modifiers: .command)
}
#else
self
#endif
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// View+RichTextAlignment.swift
// RichTextAlignment+KeyboardShortcutModifier.swift
// RichTextKit
//
// Created by Daniel Saidi on 2023-10-17.
Expand All @@ -8,6 +8,29 @@

import SwiftUI

public extension RichTextAlignment {

/**
This view modifier can apply keyboard shortcuts for any
``RichTextAlignment`` to any view.

You can also apply it with the `.keyboardShortcut(for:)`
view extension.
*/
struct KeyboardShortcutModifier: ViewModifier {

public init(_ alignment: RichTextAlignment) {
self.alignment = alignment
}

private let alignment: RichTextAlignment

public func body(content: Content) -> some View {
content.keyboardShortcut(for: alignment)
}
}
}

public extension View {

/**
Expand Down
97 changes: 97 additions & 0 deletions Sources/RichTextKit/Alignment/RichTextAlignment+Picker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// RichTextAlignment+Picker.swift
// RichTextKit
//
// Created by Daniel Saidi on 2022-05-30.
// Copyright © 2022-2024 Daniel Saidi. All rights reserved.
//

import SwiftUI

public extension RichTextAlignment {

/**
This picker can be used to pick rich text alignments.

The view returns a plain SwiftUI `Picker` view that can
be styled and configured with plain SwiftUI.
*/
struct Picker: View {

/**
Create a rich text alignment picker.

- Parameters:
- selection: The binding to update with the picker.
- style: The style to apply, by default `.standard`.
- values: The pickable alignments, by default `.allCases`.
*/
public init(
selection: Binding<RichTextAlignment>,
style: Style = .standard,
values: [RichTextAlignment] = RichTextAlignment.allCases
) {
self._selection = selection
self.style = style
self.values = values
}

let style: Style
let values: [RichTextAlignment]

@Binding
private var selection: RichTextAlignment

public var body: some View {
SwiftUI.Picker("", selection: $selection) {
ForEach(RichTextAlignment.allCases) { value in
value.icon
.foregroundColor(style.iconColor)
.accessibilityLabel(value.title)
.tag(value)
}
}
.accessibilityLabel(RTKL10n.textAlignment.text)
}
}
}

public extension RichTextAlignment.Picker {

/// This style can be used to style an alignment picker.
struct Style {

public init(
iconColor: Color = .primary
) {
self.iconColor = iconColor
}

public var iconColor: Color
}
}

public extension RichTextAlignment.Picker.Style {

static var standard = Self.init()
}

struct RichTextAlignment_Picker_Previews: PreviewProvider {

struct Preview: View {

@State
private var alignment = RichTextAlignment.left

var body: some View {
RichTextAlignment.Picker(
selection: $alignment,
values: .all
)
}
}

static var previews: some View {
Preview()
}
}
12 changes: 8 additions & 4 deletions Sources/RichTextKit/Alignment/RichTextAlignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
// RichTextKit
//
// Created by Daniel Saidi on 2022-05-28.
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
// Copyright © 2022-2024 Daniel Saidi. All rights reserved.
//

import SwiftUI

/**
This enum defines supported rich text alignments.

The enum makes the alignment type identifiable and diffable.
This enum defines supported rich text alignments, like left,
right, center, and justified.
*/
public enum RichTextAlignment: String, CaseIterable, Codable, Equatable, Identifiable {

Expand Down Expand Up @@ -44,6 +43,11 @@ public enum RichTextAlignment: String, CaseIterable, Codable, Equatable, Identif
case right
}

public extension Collection where Element == RichTextAlignment {

static var all: [Element] { RichTextAlignment.allCases }
}

public extension RichTextAlignment {

/// The unique ID of the alignment.
Expand Down
46 changes: 0 additions & 46 deletions Sources/RichTextKit/Alignment/RichTextAlignmentPicker.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/RichTextKit/Format/RichTextFormatSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private extension RichTextFormatSheet {

var paragraphRow: some View {
HStack {
RichTextAlignmentPicker(selection: $context.textAlignment)
RichTextAlignment.Picker(selection: $context.textAlignment)
.pickerStyle(.segmented)
Spacer()
indentButtons
Expand Down
2 changes: 1 addition & 1 deletion Sources/RichTextKit/Format/RichTextFormatSidebar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public struct RichTextFormatSidebar: View {
}

SidebarSection(title: nil) {
RichTextAlignmentPicker(selection: $context.textAlignment)
RichTextAlignment.Picker(selection: $context.textAlignment)
.pickerStyle(.segmented)
RichTextAction.ButtonGroup(
context: context,
Expand Down
2 changes: 1 addition & 1 deletion Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private extension RichTextKeyboardToolbar {

@ViewBuilder
var trailingViews: some View {
RichTextAlignmentPicker(selection: $context.textAlignment)
RichTextAlignment.Picker(selection: $context.textAlignment)
.pickerStyle(.segmented)
.frame(maxWidth: 200)
.keyboardShortcutsOnly(if: isCompact)
Expand Down
1 change: 0 additions & 1 deletion Sources/RichTextKit/RichTextKit.docc/RichTextKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ RichTextKit is available under the MIT license. See the [LICENSE][License] file
### Alignment

- ``RichTextAlignment``
- ``RichTextAlignmentPicker``

### Attributes

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import SwiftUI

@available(*, deprecated, renamed: "RichTextAlignment.Picker")
public typealias RichTextAlignmentPicker = RichTextAlignment.Picker
Loading

0 comments on commit 7e3d4a6

Please sign in to comment.