-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rich text: only selectively handle keyup/pointerup (#48385)
- Loading branch information
Showing
3 changed files
with
65 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/rich-text/src/component/use-selection-change-compat.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRefEffect } from '@wordpress/compose'; | ||
|
||
/** | ||
* Sometimes some browsers are not firing a `selectionchange` event when | ||
* changing the selection by mouse or keyboard. This hook makes sure that, if we | ||
* detect no `selectionchange` or `input` event between the up and down events, | ||
* we fire a `selectionchange` event. | ||
* | ||
* @return {import('@wordpress/compose').RefEffect} A ref effect attaching the | ||
* listeners. | ||
*/ | ||
export function useSelectionChangeCompat() { | ||
return useRefEffect( ( element ) => { | ||
const { ownerDocument } = element; | ||
const { defaultView } = ownerDocument; | ||
const selection = defaultView.getSelection(); | ||
|
||
let range; | ||
|
||
function getRange() { | ||
return selection.rangeCount ? selection.getRangeAt( 0 ) : null; | ||
} | ||
|
||
function onDown( event ) { | ||
const type = event.type === 'keydown' ? 'keyup' : 'pointerup'; | ||
|
||
function onCancel() { | ||
ownerDocument.removeEventListener( type, onUp ); | ||
ownerDocument.removeEventListener( | ||
'selectionchange', | ||
onCancel | ||
); | ||
ownerDocument.removeEventListener( 'input', onCancel ); | ||
} | ||
|
||
function onUp() { | ||
onCancel(); | ||
if ( range === getRange() ) return; | ||
ownerDocument.dispatchEvent( new Event( 'selectionchange' ) ); | ||
} | ||
|
||
ownerDocument.addEventListener( type, onUp ); | ||
ownerDocument.addEventListener( 'selectionchange', onCancel ); | ||
ownerDocument.addEventListener( 'input', onCancel ); | ||
|
||
range = getRange(); | ||
} | ||
|
||
element.addEventListener( 'pointerdown', onDown ); | ||
element.addEventListener( 'keydown', onDown ); | ||
return () => { | ||
element.removeEventListener( 'pointerdown', onDown ); | ||
element.removeEventListener( 'keydown', onDown ); | ||
}; | ||
}, [] ); | ||
} |