From 0675992ecacbcb06d662600764f230522c173f2f Mon Sep 17 00:00:00 2001 From: Divyesh Canopas <83937721+cp-divyesh-v@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:16:58 +0530 Subject: [PATCH] Fix strange attribute behaviour (#62) --- .../BaseFoundation/RichTextCoordinator.swift | 14 ++++++++++++++ .../RichTextViewComponent+Attributes.swift | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/Sources/RichEditorSwiftUI/BaseFoundation/RichTextCoordinator.swift b/Sources/RichEditorSwiftUI/BaseFoundation/RichTextCoordinator.swift index 541e72b..3bb4060 100644 --- a/Sources/RichEditorSwiftUI/BaseFoundation/RichTextCoordinator.swift +++ b/Sources/RichEditorSwiftUI/BaseFoundation/RichTextCoordinator.swift @@ -150,6 +150,7 @@ open class RichTextCoordinator: NSObject { } open func textViewDidChangeSelection(_ notification: Notification) { + replaceCurrentAttributesIfNeeded() context.onTextViewEvent( .didChangeSelection( selectedRange: textView.selectedRange, @@ -272,6 +273,19 @@ extension RichTextCoordinator { if textView.hasSelectedRange { return } let attributes = textView.richTextAttributes textView.setRichTextAttributes(attributes) +#endif + } + + /** + On macOS, we have to update the typingAttributes when we + move the text input cursor and there's no selected text. + So that the current attributes will set again for updated location. + */ + func replaceCurrentAttributesIfNeeded() { +#if macOS + if textView.hasSelectedRange { return } + let attributes = textView.richTextAttributes + textView.setNewRichTextAttributes(attributes) #endif } } diff --git a/Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Attributes.swift b/Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Attributes.swift index 7bccaf8..ea0c0a7 100644 --- a/Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Attributes.swift +++ b/Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Attributes.swift @@ -51,5 +51,11 @@ public extension RichTextViewComponent { setRichTextAttribute(attribute, to: value) } } + + func setNewRichTextAttributes( + _ attributes: RichTextAttributes + ) { + typingAttributes = attributes + } }