-
-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onChange(of:perform)
not being called for bound NSAttributedString
when the editor is typed into
#64
Comments
Not sure if this helps, but it looks like the onChange modifier is called before the bound text is updated. I found this out by troubleshooting an issue I had today with the editor. What property are you trying to observe? You might look into adding a didSet { } somewhere on the property depending on your setup. |
Yeah, that's always the case with Tried |
Thanks for reporting @bee-uh-joe-knee - I will take a look at this. |
struct EditorScreen: View {
init() {
// RichTextEditor.standardRichTextFontSize = 100
}
@State
private var text = NSAttributedString.empty
@StateObject
var context = RichTextContext()
var body: some View {
VStack {
editor.padding()
toolbar
}
.background(Color.primary.opacity(0.15))
.navigationTitle("RichTextKit")
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) {
MainMenu()
}
}
.viewDebug()
.onChange(of: text) { newValue in
print(newValue)
}
}
} Typing in the text field prints every change. I'm closing this. Feel free to reopen if you still don't get it to work. |
I might be nuts, but I'm encountering this issue as well. On iOS it seems to work fine, but not on macOS. I've also tried to isolate this issue by creating a new |
yeah, it's been a while and haven't touched the project that was originally using this, but i too was building for macOS when running into this issue |
This bug sucks! :( makes RichTextKit unusable for MacOS. I can reproduce using the demo project. |
Hi all! I can confirm that I can also see that the print doesn't work on macOS. It's very strange, because the string does in fact change. I tried adding this to the demo app, instead of the code above: struct EditorScreen: View {
....
var body: some View {
VStack(spacing: 0) {
Divider()
HStack(spacing: 0) {
editor
Divider()
toolbar
}
}
.toolbar {
ToolbarItem(placement: .automatic) {
RichTextAction.ButtonStack(
context: context,
actions: [.undo, .redo, .copy]
)
}
}
.viewDebug()
.onChange(of: context.selectedRange) { _ in <--- HERE
print(text.string)
}
}
} Subscribing to the selected range DOES work, and printing the text correctly prints the current text. @asabhaney @biajoeknee @chrisdmacrae Can you confirm if this works for you? I have pushed this change to the main branch, so you can test it without having to make any modifications. |
I can confirm onChange doesn't detect anything on macOS. I got to make it work by using
It'll be called much more times than needed, but it works. |
I do use .onChange with RTK successfully, for syntax coloring. My observer is set up like so:
edit: -with both macOS and iOS |
I'm trying to get an action to occur whenever the text of the editor changes however when I add an
onChange(of:perform)
callback to the editor view it never gets called. The only time it is actually called is if the@State
field is mutated directly from within the view that defines it.The text was updated successfully, but these errors were encountered: