Skip to content
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

cursorCleared: remove asyncFocus #2772

Conversation

ethan-james
Copy link
Collaborator

Fixes #2705

Calling asyncFocus() after clearThought triggers the editable's onBlur which dispatches cursorCleared({ value: false }), which undoes the whole clearThought operation.

Screenshot 2025-01-15 at 10 23 33

Without asyncFocus(), clearThought can proceed as intended.

Screenshot 2025-01-15 at 10 24 26

asyncFocus() was triggered on any clearThought, and the reason why it's failing in this specific case is because selection.isText() return true on a basic clearThought, and false after cursorForward. The reason for this is because the native selection sets focusNode to the text node within the editable, while cursorForward is setting focusNode to the editable div.

restore focus for iOS Safari in useEditMode
@@ -51,6 +51,7 @@ const useEditMode = ({
selection.clear()
} else {
selection.set(contentRef.current, { offset: editingCursorOffset || 0 })
if (isTouch && isSafari()) contentRef.current?.focus()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is the same fix from #2752 that is already captured in #2753.

In this case, the steps to reproduce that bad focus behavior are:

  1. clearThought gesture on the active cursor
  2. Close the keyboard
  3. clearThought gesture again
  4. Keyboard doesn't open

Copy link
Contributor

@raineorshine raineorshine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for the clear explanation. It looks like I have been a bit too liberal with asyncFocus! It works great when there is no focus, but causes problems if the editable already has the focus, and it definitely doesn't work well with clearThought.

The provided solution works great.

@raineorshine raineorshine merged commit 650c3d2 into cybersemics:main Jan 15, 2025
3 checks passed
@ethan-james
Copy link
Collaborator Author

Right on, I tested it when there is no focus and it seemed OK. I know asyncFocus is in there to solve a problem, but I couldn't find any case that would break without it here.

I'd still like to figure out why the selection.isText() thing is breaking, but like I say, I didn't see any example where leaving asyncFocus in would improve things.

@raineorshine
Copy link
Contributor

Yep. It's possible we might be able to implement a more general fix, but I've opened #2753 for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clearThought does not work after cursorForward
2 participants