Skip to content

Commit

Permalink
Add rich text format namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Mar 4, 2024
1 parent 5ab1087 commit cef30d8
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 95 deletions.
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This release removes all deprecated code and cleans up the library.

* There are new `richTextEditor` config and style view modifiers.
* There are new `richTextKeyboardToolbar` config and style view modifiers.
* There is a new `RichTextFormat` namespace that groups format types and views.

### 💥 Breaking Changes

Expand All @@ -27,6 +28,7 @@ This release removes all deprecated code and cleans up the library.
* `RichTextFormat` components are now configured with a shared modifier instead of the init.
* `RichTextFormatSheet` has been renamed to `RichTextFormat.Sheet` to trim SDK surface area.
* `RichTextFormatSidebar` has been renamed to `RichTextFormat.Sidebar` to trim SDK surface area.
* `RichTextFormatToolbar` has been renamed to `RichTextFormat.Toolbar` to trim SDK surface area.
* `RichTextLine.SpacingPicker` is now configured with a shared view modifier instead of the init.
* `RichTextKeyboardToolbar` is now configured and styled with two view modifiers instead of the init.

Expand Down
6 changes: 3 additions & 3 deletions Sources/RichTextKit/Format/RichTextFormat+Sheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public extension RichTextFormat {
self._context = ObservedObject(wrappedValue: context)
}

public typealias Config = RichTextFormatToolbar.Config
public typealias Style = RichTextFormatToolbar.Style
public typealias Config = RichTextFormat.ToolbarConfig
public typealias Style = RichTextFormat.ToolbarStyle

@ObservedObject
private var context: RichTextContext
Expand All @@ -64,7 +64,7 @@ public extension RichTextFormat {
selection: $context.fontName
)
Divider()
RichTextFormatToolbar(
RichTextFormat.Toolbar(
context: context
)
.richTextFormatToolbarConfig(config)
Expand Down
4 changes: 2 additions & 2 deletions Sources/RichTextKit/Format/RichTextFormat+Sidebar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public extension RichTextFormat {
self._context = ObservedObject(wrappedValue: context)
}

public typealias Config = RichTextFormatToolbar.Config
public typealias Style = RichTextFormatToolbar.Style
public typealias Config = RichTextFormat.ToolbarConfig
public typealias Style = RichTextFormat.ToolbarStyle

@ObservedObject
private var context: RichTextContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// RichTextFormatToolbar.swift
// RichTextFormat+Toolbar.swift
// RichTextKit
//
// Created by Daniel Saidi on 2022-12-13.
Expand All @@ -9,74 +9,73 @@
#if iOS || macOS || os(visionOS)
import SwiftUI

/**
This horizontal toolbar provides text format controls.

This toolbar adapts the layout based on the horizontal size
class. The control row will be split in two in compact size,
while macOS and regular sizes get a single row.

You can configure and style the view by applying its config
and style view modifiers to your view hierarchy:

```swift
VStack {
...
}
.richTextFormatToolbarStyle(...)
.richTextFormatToolbarConfig(...)
```

You can provide custom configurations to adjust the toolbar
and style it by applying a `.richTextFormatToolbarStyle` to
the view hierarchy.
*/
public struct RichTextFormatToolbar: RichTextFormatToolbarBase {

public extension RichTextFormat {

/**
Create a rich text format sheet.

- Parameters:
- context: The context to apply changes to.
This horizontal toolbar provides text format controls.

This toolbar adapts the layout based on horizontal size
class. The control row is split in two for compact size,
while macOS and regular sizes get a single row.

You can configure and style the view by applying config
and style view modifiers to your view hierarchy:

```swift
VStack {
...
}
.richTextFormatToolbarStyle(...)
.richTextFormatToolbarConfig(...)
```
*/
public init(
context: RichTextContext
) {
self._context = ObservedObject(wrappedValue: context)
}

@ObservedObject
private var context: RichTextContext

@Environment(\.richTextFormatToolbarConfig)
var config

@Environment(\.richTextFormatToolbarStyle)
var style

@Environment(\.horizontalSizeClass)
private var horizontalSizeClass

public var body: some View {
VStack(spacing: style.spacing) {
controls
if hasColorPickers {
Divider()
colorPickers(for: context)
struct Toolbar: RichTextFormatToolbarBase {

/**
Create a rich text format sheet.

- Parameters:
- context: The context to apply changes to.
*/
public init(
context: RichTextContext
) {
self._context = ObservedObject(wrappedValue: context)
}

@ObservedObject
private var context: RichTextContext

@Environment(\.richTextFormatToolbarConfig)
var config

@Environment(\.richTextFormatToolbarStyle)
var style

@Environment(\.horizontalSizeClass)
private var horizontalSizeClass

public var body: some View {
VStack(spacing: style.spacing) {
controls
if hasColorPickers {
Divider()
colorPickers(for: context)
}
}
.padding(.vertical, style.padding)
.environment(\.sizeCategory, .medium)
.background(background)
#if macOS
.frame(minWidth: 650)
#endif
}
.padding(.vertical, style.padding)
.environment(\.sizeCategory, .medium)
.background(background)
#if macOS
.frame(minWidth: 650)
#endif
}
}

// MARK: - Views

private extension RichTextFormatToolbar {
private extension RichTextFormat.Toolbar {

var useSingleLine: Bool {
#if macOS
Expand All @@ -87,7 +86,7 @@ private extension RichTextFormatToolbar {
}
}

private extension RichTextFormatToolbar {
private extension RichTextFormat.Toolbar {

var background: some View {
Color.clear
Expand Down Expand Up @@ -134,15 +133,15 @@ private extension RichTextFormatToolbar {
}
}

struct RichTextFormatToolbar_Previews: PreviewProvider {
struct RichTextFormat_Toolbar_Previews: PreviewProvider {

struct Preview: View {

@StateObject
private var context = RichTextContext()

var toolbar: some View {
RichTextFormatToolbar(
RichTextFormat.Toolbar(
context: context
)
.richTextFormatToolbarConfig(.init(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// RichTextFormatToolbar+Config.swift
// RichTextFormat+ToolbarConfig.swift
// RichTextKit
//
// Created by Daniel Saidi on 2024-02-16.
Expand All @@ -9,10 +9,10 @@
#if iOS || macOS || os(visionOS)
import SwiftUI

public extension RichTextFormatToolbar {
public extension RichTextFormat {

/// This struct can be used to configure a format sheet.
struct Config {
/// This type can be used to configure a format toolbar.
struct ToolbarConfig {

public init(
alignments: [RichTextAlignment] = .all,
Expand Down Expand Up @@ -52,7 +52,7 @@ public extension RichTextFormatToolbar {
}
}

public extension RichTextFormatToolbar.Config {
public extension RichTextFormat.ToolbarConfig {

/// The standard rich text format toolbar configuration.
///
Expand All @@ -64,26 +64,26 @@ public extension View {

/// Apply a rich text format toolbar style.
func richTextFormatToolbarConfig(
_ value: RichTextFormatToolbar.Config
_ value: RichTextFormat.ToolbarConfig
) -> some View {
self.environment(\.richTextFormatToolbarConfig, value)
}
}

private extension RichTextFormatToolbar.Config {
private extension RichTextFormat.ToolbarConfig {

struct Key: EnvironmentKey {

public static let defaultValue = RichTextFormatToolbar.Config()
public static let defaultValue = RichTextFormat.ToolbarConfig()
}
}

public extension EnvironmentValues {

/// This value can bind to a format toolbar config.
var richTextFormatToolbarConfig: RichTextFormatToolbar.Config {
get { self [RichTextFormatToolbar.Config.Key.self] }
set { self [RichTextFormatToolbar.Config.Key.self] = newValue }
var richTextFormatToolbarConfig: RichTextFormat.ToolbarConfig {
get { self [RichTextFormat.ToolbarConfig.Key.self] }
set { self [RichTextFormat.ToolbarConfig.Key.self] = newValue }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// RichTextFormatToolbar+Style.swift
// RichTextFormat+ToolbarStyle.swift
// RichTextKit
//
// Created by Daniel Saidi on 2024-02-16.
Expand All @@ -9,10 +9,10 @@
#if iOS || macOS || os(visionOS)
import SwiftUI

public extension RichTextFormatToolbar {
public extension RichTextFormat {

/// This struct can be used to style a format sheet.
struct Style {
/// This type can be used to style a format toolbar.
struct ToolbarStyle {

public init(
padding: Double = 10,
Expand All @@ -31,26 +31,26 @@ public extension View {

/// Apply a rich text format toolbar style.
func richTextFormatToolbarStyle(
_ style: RichTextFormatToolbar.Style
_ style: RichTextFormat.ToolbarStyle
) -> some View {
self.environment(\.richTextFormatToolbarStyle, style)
}
}

private extension RichTextFormatToolbar.Style {
private extension RichTextFormat.ToolbarStyle {

struct Key: EnvironmentKey {

public static let defaultValue = RichTextFormatToolbar.Style()
public static let defaultValue = RichTextFormat.ToolbarStyle()
}
}

public extension EnvironmentValues {

/// This value can bind to a format toolbar style.
var richTextFormatToolbarStyle: RichTextFormatToolbar.Style {
get { self [RichTextFormatToolbar.Style.Key.self] }
set { self [RichTextFormatToolbar.Style.Key.self] = newValue }
var richTextFormatToolbarStyle: RichTextFormat.ToolbarStyle {
get { self [RichTextFormat.ToolbarStyle.Key.self] }
set { self [RichTextFormat.ToolbarStyle.Key.self] = newValue }
}
}
#endif
4 changes: 2 additions & 2 deletions Sources/RichTextKit/Format/RichTextFormatToolbarBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import SwiftUI
/// two toolbars, which should eventually become one.
protocol RichTextFormatToolbarBase: View {

var config: RichTextFormatToolbar.Config { get }
var style: RichTextFormatToolbar.Style { get }
var config: RichTextFormat.ToolbarConfig { get }
var style: RichTextFormat.ToolbarStyle { get }
}

extension RichTextFormatToolbarBase {
Expand Down
6 changes: 2 additions & 4 deletions Sources/RichTextKit/RichTextKit.docc/RichTextKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ RichTextKit is available under the MIT license. See the [LICENSE][License] file
- ``RichTextEditor``
- ``RichTextView``
- ``RichTextContext``
- ``RichTextCoordinator``

### Foundation

- ``RichTextCoordinator``
- ``RichTextPresenter``
- ``RichTextReader``
- ``RichTextWriter``
Expand Down Expand Up @@ -120,9 +120,7 @@ RichTextKit is available under the MIT license. See the [LICENSE][License] file

### Format

- ``RichTextFormatSheet``
- ``RichTextFormatSidebar``
- ``RichTextFormatToolbar``
- ``RichTextFormat``

### Images

Expand Down

0 comments on commit cef30d8

Please sign in to comment.