From fb1f6f91a77cb57860410f031ffc0dbe4d1076d4 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Wed, 14 Feb 2024 10:07:33 +0100 Subject: [PATCH] Make coordinator properly handle indentation --- .../RichTextAttributeWriter+Alignment.swift | 29 ++------------ .../RichTextAttributeWriter+Paragraph.swift | 38 +++++++++++++++++++ .../RichTextCommand+ActionButton.swift | 2 +- .../RichTextViewComponent+Alignment.swift | 3 ++ .../Context/RichTextContext+Actions.swift | 2 +- .../Keyboard/RichTextKeyboardToolbar.swift | 15 +++++--- .../ReadersWriters+Deprecated.swift | 6 +-- 7 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 Sources/RichTextKit/Attributes/RichTextAttributeWriter+Paragraph.swift diff --git a/Sources/RichTextKit/Attributes/RichTextAttributeWriter+Alignment.swift b/Sources/RichTextKit/Attributes/RichTextAttributeWriter+Alignment.swift index 6c469ee5d..16b11248b 100644 --- a/Sources/RichTextKit/Attributes/RichTextAttributeWriter+Alignment.swift +++ b/Sources/RichTextKit/Attributes/RichTextAttributeWriter+Alignment.swift @@ -19,35 +19,12 @@ import AppKit public extension RichTextAttributeWriter { /// Set the rich text alignment at a certain range. - /// - /// > Todo: Something's currently off with alignment. It - /// spils over to other paragraphs when moving the input - /// cursor and inserting new text. func setRichTextAlignment( _ alignment: RichTextAlignment, at range: NSRange ) { - let text = richText.string - let length = range.length - let location = range.location - let ulocation = UInt(location) - let uindex = text.findIndexOfCurrentParagraph(from: ulocation) - let index = Int(uindex) - let diff = location - index - let newLength = max(length + diff, 1) - let newRange = NSRange(location: index, length: newLength) - let alignment = alignment.nativeAlignment - let safeRange = safeRange(for: newRange) - #if os(macOS) - mutableRichText?.setAlignment(alignment, range: safeRange) - #else - let paragraph = richTextParagraphStyle(at: safeRange) ?? .init() - paragraph.alignment = alignment - setRichTextAttribute( - .paragraphStyle, - to: paragraph, - at: safeRange - ) - #endif + let paragraph = richTextParagraphStyle(at: range) ?? .init() + paragraph.alignment = alignment.nativeAlignment + setRichTextParagraphStyle(to: paragraph, at: range) } } diff --git a/Sources/RichTextKit/Attributes/RichTextAttributeWriter+Paragraph.swift b/Sources/RichTextKit/Attributes/RichTextAttributeWriter+Paragraph.swift new file mode 100644 index 000000000..ad1dab770 --- /dev/null +++ b/Sources/RichTextKit/Attributes/RichTextAttributeWriter+Paragraph.swift @@ -0,0 +1,38 @@ +// +// RichTextAttributeWriter+Paragraph.swift +// RichTextKit +// +// Created by Daniel Saidi on 2024-02-14. +// Copyright © 2023-2024 Daniel Saidi. All rights reserved. +// + +import Foundation + +#if canImport(UIKit) +import UIKit +#endif + +#if canImport(AppKit) && !targetEnvironment(macCatalyst) +import AppKit +#endif + +public extension RichTextAttributeWriter { + + /// Set the rich text paragraph style at a certain range. + func setRichTextParagraphStyle( + to style: NSParagraphStyle, + at range: NSRange + ) { + let text = richText.string + let length = range.length + let location = range.location + let ulocation = UInt(location) + let uindex = text.findIndexOfCurrentParagraph(from: ulocation) + let index = Int(uindex) + let diff = location - index + let newLength = max(length + diff, 1) + let newRange = NSRange(location: index, length: newLength) + let safeRange = safeRange(for: newRange) + setRichTextAttribute(.paragraphStyle, to: style, at: safeRange) + } +} diff --git a/Sources/RichTextKit/Commands/RichTextCommand+ActionButton.swift b/Sources/RichTextKit/Commands/RichTextCommand+ActionButton.swift index b7ac5b50f..f37984787 100644 --- a/Sources/RichTextKit/Commands/RichTextCommand+ActionButton.swift +++ b/Sources/RichTextKit/Commands/RichTextCommand+ActionButton.swift @@ -38,7 +38,7 @@ public extension RichTextCommand { private var context: RichTextContext? public var body: some View { - SwiftUI.Button(action.menuTitle) { + Button(action.menuTitle) { context?.handle(action) } .disabled(!canHandle) diff --git a/Sources/RichTextKit/Component/RichTextViewComponent+Alignment.swift b/Sources/RichTextKit/Component/RichTextViewComponent+Alignment.swift index 24c0cc148..cb00a83ab 100644 --- a/Sources/RichTextKit/Component/RichTextViewComponent+Alignment.swift +++ b/Sources/RichTextKit/Component/RichTextViewComponent+Alignment.swift @@ -24,6 +24,9 @@ public extension RichTextViewComponent { /// Set the rich text alignment at current range. /// + /// This function does not use ``RichTextAttributeWriter`` + /// since the text views require affecting text storage. + /// /// > Important: This function will affect the next line /// of text if we grab `richTextParagraphStyle` and make /// the alignment change to it, instead of creating this diff --git a/Sources/RichTextKit/Context/RichTextContext+Actions.swift b/Sources/RichTextKit/Context/RichTextContext+Actions.swift index 9097451dd..5e8cc5718 100644 --- a/Sources/RichTextKit/Context/RichTextContext+Actions.swift +++ b/Sources/RichTextKit/Context/RichTextContext+Actions.swift @@ -16,7 +16,7 @@ public extension RichTextContext { case .setAlignment(let align): textAlignment = align case .stepFontSize(let points): fontSize += CGFloat(points) case .toggleStyle(let style): toggle(style) - default: break + default: userActionPublisher.send(action) } } diff --git a/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift b/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift index 913f32e6c..cfca1675d 100644 --- a/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift +++ b/Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift @@ -125,9 +125,7 @@ public struct RichTextKeyboardToolbar