Skip to content

Commit

Permalink
Revert update view code, since it made the editor unperformant
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Jun 2, 2023
1 parent 0d1c6ed commit 5abf055
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Until then, deprecated features may be removed in the next minor version.



## 0.6.1

This update reverts a `RichTextEditor` view update that made the editor less performant.



## 0.6

### ✨ Features
Expand Down
8 changes: 8 additions & 0 deletions Sources/RichTextKit/Context/RichTextContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ public class RichTextContext: ObservableObject {

public extension RichTextContext {

/**
Whether or not the context has a selected range.
*/
var hasHighlightedRange: Bool {
highlightedRange != nil
}

/**
Whether or not the context has a selected range.
*/
Expand Down Expand Up @@ -346,6 +353,7 @@ public extension RichTextContext {
Reset the ``highlightedRange``.
*/
func resetHighlightedRange() {
guard hasHighlightedRange else { return }
highlightedRange = nil
}

Expand Down
25 changes: 7 additions & 18 deletions Sources/RichTextKit/RichTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ import SwiftUI
The above code will simply not show anything when you start
to edit text. To work around this limitation, you can use a
``RichTextKeyboardToolbar`` instead.

You may have noticed that `updateUIView/updateNSView` don't
contain any code. This is because having updates there will
update this view, which in turn makes typing very slow. Use
the rich text context to modify the text instead.
*/
public struct RichTextEditor: ViewRepresentable {

Expand Down Expand Up @@ -103,10 +108,7 @@ public struct RichTextEditor: ViewRepresentable {
return textView
}

public func updateUIView(_ view: UIViewType, context: Context) {
textView.attributedString = text.wrappedValue
resetHighlightedRange()
}
public func updateUIView(_ view: UIViewType, context: Context) {}
#endif

#if os(macOS)
Expand All @@ -116,23 +118,10 @@ public struct RichTextEditor: ViewRepresentable {
return scrollView
}

public func updateNSView(_ view: NSViewType, context: Context) {
// textView.attributedString = text.wrappedValue
// resetHighlightedRange()
}
public func updateNSView(_ view: NSViewType, context: Context) {}
#endif
}

@MainActor
private extension RichTextEditor {

func resetHighlightedRange() {
DispatchQueue.main.async {
richTextContext.highlightedRange = nil
}
}
}


// MARK: RichTextPresenter

Expand Down

0 comments on commit 5abf055

Please sign in to comment.