Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Extend configuration #141

Merged
merged 9 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ extension RichTextCoordinator {
extension ColorRepresentable {

#if iOS || os(tvOS) || os(visionOS)
static var textColor: ColorRepresentable { .label }
public static var textColor: ColorRepresentable { .label }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can even keep this it is in dependent PR for themes (default text color etc.) Just a note 😅

#endif
}
#endif
34 changes: 3 additions & 31 deletions Sources/RichTextKit/RichTextView+Config.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
//
// RichTextView+Config.swift
// RichTextKit
//
// Created by Daniel Saidi on 2024-01-16.
// Copyright © 2024 Daniel Saidi. All rights reserved.
//
// Created by Dominik Bucher on 13.02.2024.
//

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

public extension RichTextView {

/**
This type can be used to configure a ``RichTextEditor``.
*/
struct Configuration {

/**
Create a custom configuration.

- Parameters:
- isScrollingEnabled: Whether or not the editor should scroll, by default `true`.
*/
public init(
isScrollingEnabled: Bool = true
) {
self.isScrollingEnabled = isScrollingEnabled
}

/// Whether or not the editor should scroll.
public var isScrollingEnabled: Bool
}
}
import Foundation

public extension RichTextView.Configuration {

/// Get a standard rich text editor configuration.
static var standard: Self { .init() }
}
#endif
36 changes: 36 additions & 0 deletions Sources/RichTextKit/RichTextView+Config_AppKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// RichTextView+Config_AppKit.swift
//
//
// Created by Dominik Bucher on 13.02.2024.
//

#if macOS
import Foundation

public extension RichTextView {

/**
This type can be used to configure a ``RichTextEditor``.
*/
struct Configuration {

/// Create a custom configuration
/// - Parameters:
/// - isScrollingEnabled: Whether or not the editor should scroll, by default `true`.
/// - isContinuousSpellCheckingEnabled: Whether the editor spell-checks in realtime. Defaults to `true`.
public init(
isScrollingEnabled: Bool = true,
isContinuousSpellCheckingEnabled: Bool = true
) {
self.isScrollingEnabled = isScrollingEnabled
self.isContinuousSpellCheckingEnabled = isContinuousSpellCheckingEnabled
}

/// Whether or not the editor should scroll.
public var isScrollingEnabled: Bool
/// Whether the editor spell-checks in realtime.
public var isContinuousSpellCheckingEnabled: Bool
}
}
#endif
50 changes: 50 additions & 0 deletions Sources/RichTextKit/RichTextView+Config_UIKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// RichTextView+Config_UIKit.swift
// RichTextKit
//
// Created by Daniel Saidi on 2024-01-16.
// Copyright © 2024 Daniel Saidi. All rights reserved.
//

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

public extension RichTextView {

/**
This type can be used to configure a ``RichTextEditor``.
*/
struct Configuration {

/**
Create a custom configuration.

- Parameters:
- isScrollingEnabled: Whether or not the editor should scroll, by default `true`.
- allowsEditingTextAttributes: If editor allows editing text attributes, by default `true`.
- autocapitalizationType: Type of Auto capitalization, default is to `.sentences`.
- spellCheckingType: Whether textView spell-Checks, default is `.no`.
*/
public init(
isScrollingEnabled: Bool = true,
allowsEditingTextAttributes: Bool = true,
autocapitalizationType: UITextAutocapitalizationType = .sentences,
spellCheckingType: UITextSpellCheckingType = .no
) {
self.isScrollingEnabled = isScrollingEnabled
self.allowsEditingTextAttributes = allowsEditingTextAttributes
self.autocapitalizationType = autocapitalizationType
self.spellCheckingType = spellCheckingType
}

/// Whether or not the editor should scroll.
public var isScrollingEnabled: Bool
/// Whether textView allows editting text attributes
public var allowsEditingTextAttributes: Bool
/// Kind of auto capitalization
public var autocapitalizationType: UITextAutocapitalizationType
/// If TextView spell-checks the text.
public var spellCheckingType: UITextSpellCheckingType
}
}
#endif
7 changes: 7 additions & 0 deletions Sources/RichTextKit/RichTextView_AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ open class RichTextView: NSTextView, RichTextViewComponent {
imageConfiguration = standardImageConfiguration(for: format)
layoutManager?.defaultAttachmentScaling = NSImageScaling.scaleProportionallyDown
setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
setupConfiguration()
}

// MARK: - Internal

func setupConfiguration() {
isContinuousSpellCheckingEnabled = configuration.isContinuousSpellCheckingEnabled
}

// MARK: - Open Functionality
Expand Down
8 changes: 4 additions & 4 deletions Sources/RichTextKit/RichTextView_UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ open class RichTextView: UITextView, RichTextViewComponent {
public var configuration: Configuration = .standard {
didSet {
isScrollEnabled = configuration.isScrollingEnabled
allowsEditingTextAttributes = configuration.allowsEditingTextAttributes
autocapitalizationType = configuration.autocapitalizationType
spellCheckingType = configuration.spellCheckingType
}
}

Expand Down Expand Up @@ -158,11 +161,8 @@ open class RichTextView: UITextView, RichTextViewComponent {
imageConfiguration = standardImageConfiguration(for: format)
text.autosizeImageAttachments(maxSize: imageAttachmentMaxSize)
attributedString = text
allowsEditingTextAttributes = false
autocapitalizationType = .sentences
backgroundColor = .clear
richTextDataFormat = format
spellCheckingType = .no
backgroundColor = .clear
trySetupInitialTextColor(for: text) {
textColor = .label
}
Expand Down
6 changes: 4 additions & 2 deletions Tests/RichTextKitTests/RichTextViewRepresentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ final class RichTextViewComponentTests: XCTestCase {
func testSettingUpWithEmptyTextWorks() {
let string = NSAttributedString(string: "")
view.setup(with: string, format: .rtf)
view.configuration = .standard
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This raises question if configuration shouldn't be passed to setup instead... This feels like API is all over the place, your thoughts @danielsaidi ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we should inject it into the environment in SwiftUI.

XCTAssertEqual(view.richText.string, "")
#if iOS || os(tvOS)
XCTAssertFalse(view.allowsEditingTextAttributes)
XCTAssertTrue(view.allowsEditingTextAttributes)
XCTAssertEqual(view.autocapitalizationType, .sentences)
#endif
XCTAssertEqual(view.backgroundColor, .clear)
Expand All @@ -63,9 +64,10 @@ final class RichTextViewComponentTests: XCTestCase {
func testSettingUpWithNonEmptyTextWorks() {
let string = NSAttributedString(string: "foo bar baz")
view.setup(with: string, format: .rtf)
view.configuration = .standard
XCTAssertEqual(view.richText.string, "foo bar baz")
#if iOS || os(tvOS)
XCTAssertFalse(view.allowsEditingTextAttributes)
XCTAssertTrue(view.allowsEditingTextAttributes)
XCTAssertEqual(view.autocapitalizationType, .sentences)
#endif
XCTAssertEqual(view.backgroundColor, .clear)
Expand Down
Loading