Skip to content

Commit 0675992

Browse files
authored
Fix strange attribute behaviour (#62)
1 parent c4cc566 commit 0675992

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Sources/RichEditorSwiftUI/BaseFoundation/RichTextCoordinator.swift

+14
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ open class RichTextCoordinator: NSObject {
150150
}
151151

152152
open func textViewDidChangeSelection(_ notification: Notification) {
153+
replaceCurrentAttributesIfNeeded()
153154
context.onTextViewEvent(
154155
.didChangeSelection(
155156
selectedRange: textView.selectedRange,
@@ -272,6 +273,19 @@ extension RichTextCoordinator {
272273
if textView.hasSelectedRange { return }
273274
let attributes = textView.richTextAttributes
274275
textView.setRichTextAttributes(attributes)
276+
#endif
277+
}
278+
279+
/**
280+
On macOS, we have to update the typingAttributes when we
281+
move the text input cursor and there's no selected text.
282+
So that the current attributes will set again for updated location.
283+
*/
284+
func replaceCurrentAttributesIfNeeded() {
285+
#if macOS
286+
if textView.hasSelectedRange { return }
287+
let attributes = textView.richTextAttributes
288+
textView.setNewRichTextAttributes(attributes)
275289
#endif
276290
}
277291
}

Sources/RichEditorSwiftUI/Components/RichTextViewComponent+Attributes.swift

+6
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,11 @@ public extension RichTextViewComponent {
5151
setRichTextAttribute(attribute, to: value)
5252
}
5353
}
54+
55+
func setNewRichTextAttributes(
56+
_ attributes: RichTextAttributes
57+
) {
58+
typingAttributes = attributes
59+
}
5460
}
5561

0 commit comments

Comments
 (0)