simonbs / Runestone

📝 Performant plain text editor for iOS with syntax highlighting, line numbers, invisible characters and much more.
MIT License
2.66k stars 148 forks source link

Checking for user changes does not work #381

Open hooczech opened 2 weeks ago

hooczech commented 2 weeks ago

What happened?

Checking whether user did any changes to the text does not work, onChange function does not fire

What are the steps to reproduce?

We want to check if there is any text addition to the editor by user. We use SwiftUI.

We have simple wrapper as:

import Runestone
import SwiftUI

struct RunestoneTextViewWrapper: UIViewRepresentable {
    @Binding var textView: TextView

    func makeUIView(context: Context) -> TextView {
        textView
    }

    func updateUIView(_ uiView: TextView, context: Context) {
        if uiView.text != textView.text {
            uiView.text = textView.text
        }
    }
}

We have follwing way to try to check for user input/changes, however this is never called when user interacts with the text

ZStack(alignment: .topTrailing) {
    ScrollView {
        RunestoneTextViewWrapper(textView: $textView)
            .onAppear {
             //...
            }
            .onChange(of: textView.text) { oldValue, newValue in
                self.hasChanges = true
                self.updateThingsToDoCount(text: newValue)
            }
    }
}

What is the expected behavior?

fire the onChange function

vvisionnn commented 2 weeks ago

just have quick look and didn't try on my computer

so you are passing the TextView as a binding which is a reference type that is not the recommend way to monitoring the change possible a solution: try make the text string as a binding not the TextView, then you can monitor the change of text instead of textView.text