rajdeep / proton

Purely native and extensible rich text editor for iOS and macOS Catalyst apps
Other
1.25k stars 81 forks source link

Delegate method for didChangeSelectionAt not being called #322

Closed easiwriter closed 3 weeks ago

easiwriter commented 3 weeks ago
    func editor(_ editor: EditorView,
                didChangeSelectionAt range: NSRange,
                attributes: TypingAttributes,
                contentType: EditorContent) {

This is not being called. Other delegate methods are OK. I have checked out main.

easiwriter commented 3 weeks ago

Any chance of a resolution to this because I can't move forward without one.

rajdeep commented 3 weeks ago

@easiwriter, have you verified that the delegate is setup correctly? I have verified in ExampleApp and have also been using this in an active project without any issues.

I also noticed your method signature looks different from what is defined in the delegate. I am not sure what the type that you are using TypingAttributes maps to. The signature of function from delegate looks like this:

func editor(_ editor: EditorView, didChangeSelectionAt range: NSRange, attributes: [NSAttributedString.Key: Any], contentType: EditorContent.Name)

Please see the recording from ExampleApp from latest main:

https://github.com/rajdeep/proton/assets/266423/f688eca2-fe52-4b6b-871b-9ce9648b5b65

rajdeep commented 3 weeks ago

please feel free to reopen this issue should you continue to have problems even after trying aforementioned.

easiwriter commented 3 weeks ago

The type TypingAttributes is an alias for Attributes.

When I assign the delegate I am using the code:

    let editor = context.coordinator.setupTextView(with: size)
    editor.delegate = context.coordinator as EditorViewDelegate

The coordinator is defined as:

class Coordinator: EditorViewDelegate

When I inspect the delegate its type is given as

delegate    Proton.EditorViewDelegate?  0x000060000029f4e1

When I move the cursor it goes to line 53 of AggregateEditorViewDelegate it winds up at line 156 of the extension to EditorViewDelegate, which is a null method. It then carries on to the editorContextDelegate and returns but never calls my hook.

I’m guessing the delegate should be pointing at my code and that is the problem. If so then something must be going wrong with the compilation. I tried casting it to my code, but…

I’m mystified.

Keith

On 3 Jul 2024, at 11:29, Rajdeep Kwatra @.***> wrote:

@easiwriter, have you verified that the delegate is setup correctly? I have verified in ExampleApp and have also been using this in an active project without any issues. I also noticed your method signature looks different from what is defined in the delegate. I am not sure what the type that you are using TypingAttributes maps to. The signature of function from delegate looks like this: func editor(_ editor: EditorView, didChangeSelectionAt range: NSRange, attributes: [NSAttributedString.Key: Any], contentType: EditorContent.Name)

Please see the recording from ExampleApp from latest main: https://github.com/rajdeep/proton/assets/266423/f688eca2-fe52-4b6b-871b-9ce9648b5b65 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

easiwriter commented 3 weeks ago

I have attached a test too that shows the issue. If you put a break in the Coordinator method for handling selection changes you will see it never gets called. ProtonTest.zip

rajdeep commented 3 weeks ago

That's great. Having a failing test will help me diagnose the issue better. I'll try to find time tomorrow and have a look at it. Thank you.

rajdeep commented 3 weeks ago

@easiwriter, the issue in your code is that you are using incorrect delegate signature:

you have: func editor(_ editor: EditorView, didChangeSelectionAt range: NSRange, attributes: [NSAttributedString.Key: Any], contentType: EditorContent)

instead of: func editor(_ editor: EditorView, didChangeSelectionAt range: NSRange, attributes: [NSAttributedString.Key: Any], contentType: EditorContent.Name)

Since there is a default handler for all Editor delegate callbacks, it ends up going to default instead of your Coordinator as it does not implement this callback. If you correct the signature, you would see it coming through.

This is an issue that I have also realised in past and can take a lot of time to debug. In future, I plan to remove the default handlers to avoid such cases. However, as a best practice, I recommend always copying over the function signature from delegate itself, in case you are running into such issues.