Skip to content

Commit

Permalink
Make text views share common setup
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Mar 4, 2024
1 parent 2c16a9d commit 0339b75
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
29 changes: 29 additions & 0 deletions Sources/RichTextKit/Editor/RichTextView+Setup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// RichTextView+Setup.swift
// RichTextKit
//
// Created by Daniel Saidi on 2024-03-04.
// Copyright © 2024 Daniel Saidi. All rights reserved.
//

import SwiftUI

extension RichTextView {

func setupSharedBehavior(
with text: NSAttributedString,
_ format: RichTextDataFormat
) {
attributedString = .empty
imageConfiguration = standardImageConfiguration(for: format)
attributedString = text
setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
}

func setup(_ theme: RichTextView.Theme) {
guard richText.string.isEmpty else { return }
font = theme.font
textColor = theme.fontColor
backgroundColor = theme.backgroundColor
}
}
19 changes: 2 additions & 17 deletions Sources/RichTextKit/Editor/RichTextView_AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,12 @@ open class RichTextView: NSTextView, RichTextViewComponent {
with text: NSAttributedString,
format: RichTextDataFormat
) {
attributedString = .empty
attributedString = text
setupSharedBehavior(with: text, format)
allowsImageEditing = true
allowsUndo = true
imageConfiguration = standardImageConfiguration(for: format)
layoutManager?.defaultAttachmentScaling = NSImageScaling.scaleProportionallyDown
setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
setupConfiguration()
setupTheme()
}

// MARK: - Internal

func setupConfiguration() {
isContinuousSpellCheckingEnabled = configuration.isContinuousSpellCheckingEnabled
}

func setupTheme() {
font = theme.font
textColor = theme.fontColor
backgroundColor = theme.backgroundColor
setup(theme)
}

// MARK: - Open Functionality
Expand Down
17 changes: 2 additions & 15 deletions Sources/RichTextKit/Editor/RichTextView_UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,10 @@ open class RichTextView: UITextView, RichTextViewComponent {
with text: NSAttributedString,
format: RichTextDataFormat
) {
attributedString = .empty
imageConfiguration = standardImageConfiguration(for: format)
text.autosizeImageAttachments(maxSize: imageAttachmentMaxSize)
attributedString = text
setupSharedBehavior(with: text, format)
richTextDataFormat = format
setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
setupTheme()
}

// MARK: - Internal functionality

func setupTheme() {
if text.isEmpty {
font = theme.font
textColor = theme.fontColor
}
backgroundColor = theme.backgroundColor
setup(theme)
}

// MARK: - Open Functionality
Expand Down

0 comments on commit 0339b75

Please sign in to comment.