1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 15:02:51 +02:00

Make onDomSelectionChange triggered after onClick. (#4132)

* Delay onDomSelectionChange but not cause performance issue.

* Add changeset

* Update changeset

Changeset editor via GitHub's UI seems to violate our prettier rules 🤦🏼‍♂️

Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
This commit is contained in:
Ulion
2021-08-22 08:03:07 +08:00
committed by GitHub
parent 3e7ff3bb0d
commit 48b7129447
2 changed files with 17 additions and 3 deletions

View File

@@ -0,0 +1,6 @@
---
'slate-react': patch
'slate': patch
---
Make onDomSelectionChange trigger after onClick.

View File

@@ -511,6 +511,11 @@ export const Editable = (props: EditableProps) => {
[readOnly] [readOnly]
) )
const scheduleOnDOMSelectionChange = useCallback(
() => setTimeout(onDOMSelectionChange),
[onDOMSelectionChange]
)
// Attach a native DOM event handler for `selectionchange`, because React's // Attach a native DOM event handler for `selectionchange`, because React's
// built-in `onSelect` handler doesn't fire for all selection changes. It's a // built-in `onSelect` handler doesn't fire for all selection changes. It's a
// leaky polyfill that only fires on keypresses or clicks. Instead, we want to // leaky polyfill that only fires on keypresses or clicks. Instead, we want to
@@ -518,15 +523,18 @@ export const Editable = (props: EditableProps) => {
// https://github.com/facebook/react/issues/5785 // https://github.com/facebook/react/issues/5785
useIsomorphicLayoutEffect(() => { useIsomorphicLayoutEffect(() => {
const window = ReactEditor.getWindow(editor) const window = ReactEditor.getWindow(editor)
window.document.addEventListener('selectionchange', onDOMSelectionChange) window.document.addEventListener(
'selectionchange',
scheduleOnDOMSelectionChange
)
return () => { return () => {
window.document.removeEventListener( window.document.removeEventListener(
'selectionchange', 'selectionchange',
onDOMSelectionChange scheduleOnDOMSelectionChange
) )
} }
}, [onDOMSelectionChange]) }, [scheduleOnDOMSelectionChange])
const decorations = decorate([editor, []]) const decorations = decorate([editor, []])