Repository navigation
Fix dead-key and input method typing in Safari shadow roots #6362
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
33b81c2
4d79f05
cc72371
7b0a70f
87914ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'slate-react': patch | ||
| --- | ||
|
|
||
| In Safari, when the editor is inside a shadow root, typing with a dead key or an input method no longer loses text, and a click during a composition now moves the caret |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -329,7 +329,12 @@ | |
| const el = ReactEditor.toDOMNode(editor, editor) | ||
| const root = el.getRootNode() | ||
|
|
||
| if (!processing.current && IS_WEBKIT && root instanceof ShadowRoot) { | ||
| if ( | ||
| !processing.current && | ||
| IS_WEBKIT && | ||
| root instanceof ShadowRoot && | ||
| !ReactEditor.isComposing(editor) | ||
| ) { | ||
| processing.current = true | ||
|
|
||
| const active = getActiveElement() | ||
|
|
@@ -1427,7 +1432,7 @@ | |
| ) | ||
| } | ||
| }, | ||
| [ | ||
| readOnly, | ||
| attributes.onInput, | ||
| editor, | ||
|
|
@@ -1509,6 +1514,15 @@ | |
| if (IS_WEBKIT) { | ||
| const domSelection = getSelection(root) | ||
| domSelection?.removeAllRanges() | ||
|
|
||
| // COMPAT: Removing the selection while an input method is | ||
| // composing makes Safari drop the composition without a | ||
| // `compositionend` event. Clear the composing state, or the | ||
| // editor ignores selection changes until the next key press. | ||
| if (ReactEditor.isComposing(editor)) { | ||
| IS_COMPOSING.set(editor, false) | ||
| setIsComposing(false) | ||
| } | ||
| } | ||
|
|
||
| IS_FOCUSED.delete(editor) | ||
|
|
@@ -1521,6 +1535,27 @@ | |
| flushDOMSelectionChange, | ||
| ] | ||
| )} | ||
| onMouseDown={useCallback( | ||
| (event: React.MouseEvent<HTMLDivElement>) => { | ||
| // COMPAT: WebKit ends an open composition right after | ||
| // `mousedown`. In a shadow root, the re-render that | ||
| // `compositionend` triggers can come after the click has | ||
| // moved the caret, and moves it back to where the composition | ||
| // started. Clear the composing state now, so the re-render | ||
| // happens before the caret moves. | ||
| if ( | ||
| IS_WEBKIT && | ||
| ReactEditor.isComposing(editor) && | ||
| ReactEditor.hasTarget(editor, event.target) | ||
| ) { | ||
| IS_COMPOSING.set(editor, false) | ||
| setIsComposing(false) | ||
| } | ||
|
|
||
| isEventHandled(event, attributes.onMouseDown) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not intended, good catch. Safari keeps the composition open when |
||
| }, | ||
| [editor, attributes.onMouseDown] | ||
| )} | ||
| onClick={useCallback( | ||
| (event: React.MouseEvent<HTMLDivElement>) => { | ||
| if (ReactEditor.hasTarget(editor, event.target)) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IS_WEBKITalso matches iOS Safari and every iOS browser, since they all report AppleWebKit without Chrome!IS_IOSThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it on iPhone and iPad simulators with a Japanese keyboard: a tap never gets this far mid-composition. Only macOS WebKit ends a composition on
mousedownanyway, so it's gated now, with unit tests.IS_IOSmisses iPad Safari (it reports a Mac user agent), so there's a touch check as well.