Skip to content

Commit

Permalink
Use view extensions to configure and style the text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Mar 4, 2024
1 parent 4d3cb44 commit 2ba2b51
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 22 deletions.
9 changes: 5 additions & 4 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ Until then, minor updates may remove deprecated features and introduce breaking

## 1.0

This release removes all deprecated code and cleans up the library somewhat.
This release removes all deprecated code and cleans up the library.

### ✨ Features

* The new `richTextKeyboardToolbarConfig` view extension can be used to configure the `RichTextKeyboardToolbar`.
* The new `richTextKeyboardToolbarStyle` view extension can be used to style the `RichTextKeyboardToolbar`.
* There are new `richTextEditor` config and style view extensions.
* There are new `richTextKeyboardToolbar` config and style view extensions.

### 💥 Breaking Changes

* All previously deprecated code has been deleted.

* `RichTextKeyboardToolbar` is now configured with view extensions instead of with the initializer.
* `RichTextEditor` is configured with view extensions instead of the initializer.
* `RichTextKeyboardToolbar` is configured with view extensions instead of the initializer.



Expand Down
40 changes: 40 additions & 0 deletions Sources/RichTextKit/Editor/RichTextEditor+Config.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// RichTextEditor+Config.swift
// RichTextKit
//
// Created by Daniel Saidi on 2024-02-24.
// Copyright © 2024 Daniel Saidi. All rights reserved.
//

#if iOS || macOS || os(visionOS)
import SwiftUI

/// This struct be used to configure a ``RichTextEditor``.
public typealias RichTextEditorConfig = RichTextView.Configuration

public extension View {

/// Apply a ``RichTextEditor`` configuration.
func richTextEditorConfig(
_ config: RichTextEditorConfig
) -> some View {
self.environment(\.richTextEditorConfig, config)
}
}

extension RichTextEditorConfig {

struct Key: EnvironmentKey {

public static var defaultValue: RichTextEditorConfig = .standard
}
}

public extension EnvironmentValues {

var richTextEditorConfig: RichTextEditorConfig {
get { self [RichTextEditorConfig.Key.self] }
set { self [RichTextEditorConfig.Key.self] = newValue }
}
}
#endif
41 changes: 41 additions & 0 deletions Sources/RichTextKit/Editor/RichTextEditor+Style.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// RichTextEditor+Style.swift
// RichTextKit
//
// Created by Ryan Jarvis on 2024-02-24.
//


#if iOS || macOS || os(visionOS)
import SwiftUI

/// This struct can be used to style a ``RichTextEditor``.
public typealias RichTextEditorStyle = RichTextView.Theme

public extension View {

/// Apply a ``RichTextEditor`` style.
func richTextEditorStyle(
_ style: RichTextEditorStyle
) -> some View {
self.environment(\.richTextEditorStyle, style)
}
}

extension RichTextEditorStyle {

struct Key: EnvironmentKey {

static var defaultValue: RichTextEditorStyle = .standard
}
}

public extension EnvironmentValues {

var richTextEditorStyle: RichTextEditorStyle {
get { self [RichTextEditorStyle.Key.self] }
set { self [RichTextEditorStyle.Key.self] = newValue }
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import SwiftUI

/**
This SwiftUI editor can be used to view and edit rich text.
This view can be used to view and edit rich text in SwiftUI.
The view uses a platform-specific ``RichTextView`` together
with a ``RichTextContext`` and a ``RichTextCoordinator`` to
make view and context changes sync correctly.
keep the view and context in sync.
You can use the provided context to trigger and observe any
changes to the text editor. Note that changing the value of
Expand All @@ -34,12 +34,25 @@ import SwiftUI
}
```
This code will not show anything when you start to edit the
text. To work around this use a ``RichTextKeyboardToolbar``.
This will not show anything. To work around this limitation,
use a ``RichTextKeyboardToolbar`` instead.
You may have noticed that `updateUIView/updateNSView` don't
contain any code. This is because having updates there will
update this view, which in turn makes typing very slow.
You can configure and style the view by applying config and
style view extensions to your view hierarchy:
```swift
VStack {
RichTextEditor(...)
...
}
.richTextEditorStyle(...)
.richTextEditorConfig(...)
```
For more information, see ``RichTextKeyboardToolbarConfig``
and ``RichTextKeyboardToolbarStyle``.
*/
public struct RichTextEditor: ViewRepresentable {

Expand All @@ -56,14 +69,10 @@ public struct RichTextEditor: ViewRepresentable {
public init(
text: Binding<NSAttributedString>,
context: RichTextContext,
config: RichTextView.Configuration = .standard,
theme: RichTextView.Theme = .standard,
format: RichTextDataFormat = .archivedData,
viewConfiguration: @escaping ViewConfiguration = { _ in }
) {
self.text = text
self.config = config
self.theme = theme
self._context = ObservedObject(wrappedValue: context)
self.format = format
self.viewConfiguration = viewConfiguration
Expand All @@ -75,10 +84,14 @@ public struct RichTextEditor: ViewRepresentable {
private var context: RichTextContext

private var text: Binding<NSAttributedString>
private let config: RichTextView.Configuration
private let theme: RichTextView.Theme
private var format: RichTextDataFormat
private var viewConfiguration: ViewConfiguration

@Environment(\.richTextEditorConfig)
private var config

@Environment(\.richTextEditorStyle)
private var style

#if iOS || os(tvOS) || os(visionOS)
public let textView = RichTextView()
Expand All @@ -104,7 +117,7 @@ public struct RichTextEditor: ViewRepresentable {
public func makeUIView(context: Context) -> some UIView {
textView.setup(with: text.wrappedValue, format: format)
textView.configuration = config
textView.theme = theme
textView.theme = style
viewConfiguration(textView)
return textView
}
Expand All @@ -116,7 +129,7 @@ public struct RichTextEditor: ViewRepresentable {
public func makeNSView(context: Context) -> some NSView {
textView.setup(with: text.wrappedValue, format: format)
textView.configuration = config
textView.theme = theme
textView.theme = style
viewConfiguration(textView)
return scrollView
}
Expand Down Expand Up @@ -154,5 +167,4 @@ public extension RichTextEditor {
textView.mutableAttributedString
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// RichTextKit
//
// Created by Ryan Jarvis on 2024-02-24.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

#if iOS || macOS || os(visionOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// RichTextKit
//
// Created by Ryan Jarvis on 2024-02-24.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//


#if iOS || macOS || os(visionOS)
import SwiftUI

Expand Down
7 changes: 6 additions & 1 deletion Sources/RichTextKit/RichTextKit.docc/RichTextKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ RichTextKit is available under the MIT license. See the [LICENSE][License] file
- ``RichTextDataFormat``
- ``RichTextDataReader``

### Editor

- ``RichTextEditorConfig``
- ``RichTextEditorStyle``

### Export

- ``RichTextExportError``
Expand Down Expand Up @@ -135,7 +140,7 @@ RichTextKit is available under the MIT license. See the [LICENSE][License] file
### Keyboard

- ``RichTextKeyboardToolbar``
- ``RichTextKeyboardToolbarConfiguration``
- ``RichTextKeyboardToolbarConfig``
- ``RichTextKeyboardToolbarMenu``
- ``RichTextKeyboardToolbarStyle``

Expand Down

0 comments on commit 2ba2b51

Please sign in to comment.