Skip to content

Commit

Permalink
Adjust documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Oct 8, 2024
1 parent eaa2a02 commit 1a466a0
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 222 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/danielsaidi/MockingKit.git", .upToNextMajor(from: "1.5.0"))
.package(
url: "https://github.com/danielsaidi/MockingKit.git",
.upToNextMajor(from: "1.5.0")
)
],
targets: [
.target(
Expand Down
23 changes: 10 additions & 13 deletions Sources/RichTextKit/Actions/RichTextAction+Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ import SwiftUI

public extension RichTextAction {

/**
This button can be used to trigger a ``RichTextAction``.
This renders a plain `Button`, which means that you can
use and configure it as a normal button.
*/
/// This view can be used to trigger a ``RichTextAction``.
///
/// It renders a plain `Button`, which means you can use
/// and configure it as a normal button.
struct Button: View {
/**
Create a rich text action button.

- Parameters:
- action: The action to trigger.
- context: The context to affect.
- fillVertically: WhetherP or not fill up vertical space, by default `false`.
*/
/// Create a rich text action button.
///
/// - Parameters:
/// - action: The action to trigger.
/// - context: The context to affect.
/// - fillVertically: Whether to fill up vertical space, by default `false`.
public init(
action: RichTextAction,
context: RichTextContext,
Expand Down
24 changes: 10 additions & 14 deletions Sources/RichTextKit/Actions/RichTextAction+ButtonGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ import SwiftUI

public extension RichTextAction {

/**
This view lists ``RichTextAction`` buttons in a group.
Since this view uses multiple values, it binds directly
to a ``RichTextContext`` instead of individual values.
*/
/// This view adds ``RichTextAction`` buttons to a group.
///
/// Since the view will bind to multiple values, it uses
/// a ``RichTextContext`` instead of individual values.
struct ButtonGroup: 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.
- greedy: Whether or not the group is horizontally greedy, by default `true`.
*/
/// 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`.
public init(
context: RichTextContext,
actions: [RichTextAction],
Expand Down
24 changes: 10 additions & 14 deletions Sources/RichTextKit/Actions/RichTextAction+ButtonStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ import SwiftUI

public extension RichTextAction {

/**
This view lists ``RichTextAction`` buttons in a stack.
Since this view uses multiple values, it binds directly
to a ``RichTextContext`` instead of individual values.
*/
/// This view adds ``RichTextAction`` buttons to a stack.
///
/// Since the view will bind to multiple values, it uses
/// 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`.
*/
/// 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`.
public init(
context: RichTextContext,
actions: [RichTextAction],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import SwiftUI

public extension RichTextAction {

/**
This view modifier can apply keyboard shortcuts for any
``RichTextAction`` to any view.
You can also apply it with the `.keyboardShortcut(for:)`
view modifier.
*/
/// Apply keyboard shortcuts for a ``RichTextAction`` to
/// the view.
///
/// You can also use ``SwiftUICore/View/keyboardShortcut(for:)-9i7js``.
struct KeyboardShortcutModifier: ViewModifier {

public init(_ action: RichTextAction) {
Expand All @@ -33,21 +30,22 @@ public extension RichTextAction {

public extension View {

/// Apply a ``RichTextAction/KeyboardShortcutModifier``.
/// Apply keyboard shortcuts for a ``RichTextAction`` to
/// the view.
@ViewBuilder
func keyboardShortcut(for action: RichTextAction) -> some View {
#if iOS || macOS || os(visionOS)
switch action {
case .copy: keyboardShortcut("c", modifiers: .command)
case .copy: self.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 .print: self.keyboardShortcut("p", modifiers: .command)
case .redoLatestChange: self.keyboardShortcut("z", modifiers: [.command, .shift])
case .setAlignment(let align): self.keyboardShortcut(for: align)
case .stepFontSize(let points): self.keyboardShortcut(points < 0 ? "-" : "+", modifiers: .command)
case .stepIndent(let steps): self.keyboardShortcut(steps < 0 ? "Ö" : "Ä", modifiers: .command)
case .stepSuperscript: self
case .toggleStyle(let style): keyboardShortcut(for: style)
case .undoLatestChange: keyboardShortcut("z", modifiers: .command)
case .toggleStyle(let style): self.keyboardShortcut(for: style)
case .undoLatestChange: self.keyboardShortcut("z", modifiers: .command)
default: self // TODO: Probably not defined, object to discuss.
}
#else
Expand Down
12 changes: 5 additions & 7 deletions Sources/RichTextKit/Actions/RichTextAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
import SwiftUI
import Combine

/**
This enum defines rich text actions that can be executed on
a rich text editor.
This type also serves as a type namespace for other related
types and views, like ``RichTextAction/Button``.
*/
/// This enum defines rich text actions that can be executed
/// on a rich text editor.
///
/// It's also a namespace for action-related types and views,
/// like ``RichTextAction/Button``.
public enum RichTextAction: Identifiable, Equatable, RichTextLabelValue {

/// Copy the currently selected text, if any.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ 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 modifier.
*/
/// Apply keyboard shortcuts for a ``RichTextAlignment``
/// to the view.
///
/// You can also use ``SwiftUICore/View/keyboardShortcut(for:)-22ksm``.
struct KeyboardShortcutModifier: ViewModifier {

public init(_ alignment: RichTextAlignment) {
Expand All @@ -33,19 +30,15 @@ public extension RichTextAlignment {

public extension View {

/**
Add a keyboard shortcut that toggles a certain style.
This modifier only has effect on platforms that support
keyboard shortcuts.
*/
/// Apply keyboard shortcuts for a ``RichTextAlignment``
/// to the view.
@ViewBuilder
func keyboardShortcut(for alignment: RichTextAlignment) -> some View {
#if iOS || macOS || os(visionOS)
switch alignment {
case .left: keyboardShortcut("Ö", modifiers: [.command, .shift])
case .center: keyboardShortcut("*", modifiers: [.command])
case .right: keyboardShortcut("Ä", modifiers: [.command, .shift])
case .left: self.keyboardShortcut("Ö", modifiers: [.command, .shift])
case .center: self.keyboardShortcut("*", modifiers: [.command])
case .right: self.keyboardShortcut("Ä", modifiers: [.command, .shift])
case .justified: self
}
#else
Expand Down
22 changes: 9 additions & 13 deletions Sources/RichTextKit/Alignment/RichTextAlignment+Picker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@ import SwiftUI

public extension RichTextAlignment {

/**
This picker can be used to pick a rich text alignment.
The view returns a plain SwiftUI `Picker` view that can
be styled and configured with plain SwiftUI.
*/
/// This picker can be used to pick a text alignment.
///
/// This view returns a plain SwiftUI `Picker` view that
/// can be styled and configured with a `PickerStyle`.
struct Picker: View {

/**
Create a rich text alignment picker.
- Parameters:
- selection: The binding to update with the picker.
- values: The pickable alignments, by default `.allCases`.
*/
/// Create a rich text alignment picker.
///
/// - Parameters:
/// - selection: The binding to update with the picker.
/// - values: The pickable alignments, by default `.allCases`.
public init(
selection: Binding<RichTextAlignment>,
values: [RichTextAlignment] = RichTextAlignment.allCases
Expand Down
10 changes: 4 additions & 6 deletions Sources/RichTextKit/Alignment/RichTextAlignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import SwiftUI
*/
public enum RichTextAlignment: String, CaseIterable, Codable, Equatable, Identifiable, RichTextLabelValue {

/**
Initialize a rich text alignment with a native alignment.
- Parameters:
- alignment: The native alignment to use.
*/
/// Create a rich text alignment with a native alignment.
///
/// - Parameters:
/// - alignment: The native alignment to use.
public init(_ alignment: NSTextAlignment) {
switch alignment {
case .left: self = .left
Expand Down
70 changes: 29 additions & 41 deletions Sources/RichTextKit/_Foundation/RichTextCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,24 @@
import Combine
import SwiftUI

/**
This coordinator is used to keep a ``RichTextView`` in sync
with a ``RichTextContext``.
This is used by ``RichTextEditor`` to coordinate changes in
its context and the underlying text view.
The coordinator sets itself as the text view's delegate. It
updates the context when things change in the text view and
syncs to context changes to the text view.
*/
/// This class is used to keep ``RichTextView`` in sync with
/// a ``RichTextContext``.
///
/// This is used by ``RichTextEditor`` to coordinate changes
/// in its context and the underlying text view. It will set
/// itself as the text view delegate, and update the context
/// when things change in the text view, and vice versa.
@preconcurrency @MainActor
open class RichTextCoordinator: NSObject {

// MARK: - Initialization

/**
Create a rich text coordinator.
- Parameters:
- text: The rich text to edit.
- textView: The rich text view to keep in sync.
- richTextContext: The context to keep in sync.
*/
/// Create a rich text coordinator.
///
/// - Parameters:
/// - text: The rich text to edit.
/// - textView: The rich text view to keep in sync.
/// - richTextContext: The context to keep in sync.
public init(
text: Binding<NSAttributedString>,
textView: RichTextView,
Expand Down Expand Up @@ -67,16 +61,13 @@ open class RichTextCoordinator: NSObject {

// MARK: - Internal Properties

/**
The background color that was used before the currently
highlighted range was set.
*/
/// The background color that was set before any current
/// highlighted range was set.
var highlightedRangeOriginalBackgroundColor: ColorRepresentable?

/**
The foreground color that was used before the currently
highlighted range was set.
*/

/// The foreground color that was set before any current
/// highlighted range was set.
var highlightedRangeOriginalForegroundColor: ColorRepresentable?

#if canImport(UIKit)
Expand Down Expand Up @@ -213,20 +204,17 @@ extension RichTextCoordinator {
}
}

/**
On macOS, we have to update the font and colors when we
move the text input cursor and there's no selected text.
The code looks very strange, but setting current values
to the current values will reset the text view in a way
that is otherwise not done correctly.
To try out the incorrect behavior, comment out the code
below, then change font size, colors etc. for a part of
the text then move the input cursor around. When you do,
the presented information will be correct, but when you
type, the last selected font, colors etc. will be used.
*/
/// On macOS, we have to update the font and colors when
/// we move the text input cursor with no selected text.
///
/// The code may look strange, but setting values resets
/// the text view in a way that is otherwise not correct.
///
/// To try out the incorrect behavior, disable this code,
/// then change font size, colors etc. for a part of the
/// text, then move the input cursor around. When you do,
/// the information will show correctly, but as you type,
/// the last selected font, colors etc. will be used.
func updateTextViewAttributesIfNeeded() {
#if macOS
if textView.hasSelectedRange { return }
Expand Down
10 changes: 2 additions & 8 deletions Sources/RichTextKit/_Foundation/RichTextPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@

import Foundation

/**
This protocol can be implemented any types that can present
a rich text and provide a ``selectedRange``.
This protocol is implemented by ``RichTextEditor`` since it
can both present and select text. It is also implemented by
the platform-specific ``RichTextView`` components.
*/
/// This protocol can be implemented by any type that can be
/// used to present rich text and provide a ``selectedRange``.
public protocol RichTextPresenter: RichTextReader {

/// Get the currently selected range.
Expand Down
Loading

0 comments on commit 1a466a0

Please sign in to comment.