Closed WheelaStudio closed 2 years ago
I was able to fix the error in my case by adding an empty onSelectionChange listener to my editor. The default listener has a bug!
My fix:
.onSelectionChange{ _ in }
Hey, thank you for posting this. Couple questions:
struct ContentView: View {
@State var text: String = "first line\n\nsecond\n\nthird\n\nfourth"
var body: some View {
VStack {
HighlightedTextEditor(text: $text, highlightRules: .markdown)
}
}
}
I haven't been able to reproduce this bug yet.
Anywhere i clicked, and in the end, cursor moves to top of editor.In my case I was able to expect default behavior by adding empty listener. I'll try reproduce this bug.
This is a sample of the bug. You can reproduce this bug, if you will delete the empty listener onSelectedChange on line 133 here: https://github.com/WheelaStudio/Text-Editor-IOS/blob/main/TextEditor/TextEditorMain/ContentView.swift
Thank you for the video. Unfortunately, that doesn't help narrow it down.
In my previous reply, I asked if you could modify the sample below so it causes the error to happen. We need to do that in order to fix this bug.
struct ContentView: View {
@State var text: String = "first line\n\nsecond\n\nthird\n\nfourth"
var body: some View {
VStack {
HighlightedTextEditor(text: $text, highlightRules: .markdown)
}
}
}
I was able to reproeuce this issue with the following code
import SwiftUI
import HighlightedTextEditor
struct ContentView: View {
@State private var text = ""
@FocusState private var focused
var body: some View {
List {
Button("Done") { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) }
HighlightedTextEditor(text: self.$text, highlightRules: .markdown)
.focused(self.$focused)
/// This
.onChange(of: self.focused) { print($0) }
/// Or this
.toolbar {
ToolbarItem(placement: .primaryAction) {
if self.focused { }
}
}
}
}
}
It seems that if .focused(FocusState)
is used on the HighlightedTextEditor
and the FocusState
is used somewhere else, even if it's just read and not modified, the issue occurs.
Just to clarify, the issue is that the caret doesn't go to where the tap happened. Instead, it goes to either the beginning or the end in different situations for me.
And as mentioned in https://github.com/kyle-n/HighlightedTextEditor/issues/51#issuecomment-974717542, .onSelectionChange{ _ in }
does fix the issue.
Current behavior https://user-images.githubusercontent.com/92257240/142736982-ce38758d-dc19-42e3-b2ed-169657549c02.MP4 Expected behavior Like this https://user-images.githubusercontent.com/92257240/142736991-5fe03420-9d36-4086-ac75-ee45c11ea868.MP4 Code
HighlightedTextEditor(text: $document.text, highlightRules: getHighlightRules(pattern: searchText.trimmingCharacters(in: .whitespacesAndNewlines))).onTextChange { text in undoManager?.registerUndo(withTarget: empty, handler: { _ in let oldText = text document.text = oldText }) }.focused($editorFocus).onTapGesture { dissmisKeyboard() }.onChange(of: document.text, perform: {_ in updateWordsCount() if(isSearching) { updateFoundedWordsCount() } })
Environment Based on SwiftUI app, which running on IOS 15.1