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

Rich text: reduce keyup handling #48384

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions packages/rich-text/src/component/use-input-and-selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ export function useInputAndSelection( props ) {
);
}

function removeSelectionAfterFocusHandlers() {
[ 'keyup', 'mouseup', 'touchend' ].forEach( ( eventName ) => {
element.removeEventListener(
eventName,
handleSelectionAfterFocusOnce
);
} );
element.removeEventListener(
'selectionchange',
removeSelectionAfterFocusHandlers
);
}

function handleSelectionAfterFocusOnce( event ) {
// Should only be handled once, so remove the listeners.
removeSelectionAfterFocusHandlers();
handleSelectionChange( event );
}

function onFocus() {
const { record, isSelected, onSelectionChange, applyRecord } =
propsRef.current;
Expand All @@ -287,6 +306,19 @@ export function useInputAndSelection( props ) {
end: index,
activeFormats: EMPTY_ACTIVE_FORMATS,
};

[ 'keyup', 'mouseup', 'touchend' ].forEach( ( eventName ) => {
element.addEventListener(
eventName,
handleSelectionAfterFocusOnce
);
} );
// If focus is not the result of a mouseup, touchend, or keyup
// event, we don't want to keep listening to those events.
element.addEventListener(
'selectionchange',
removeSelectionAfterFocusHandlers
);
} else {
applyRecord( record.current );
onSelectionChange( record.current.start, record.current.end );
Expand All @@ -303,13 +335,6 @@ export function useInputAndSelection( props ) {
element.addEventListener( 'compositionstart', onCompositionStart );
element.addEventListener( 'compositionend', onCompositionEnd );
element.addEventListener( 'focus', onFocus );
// Selection updates must be done at these events as they
// happen before the `selectionchange` event. In some cases,
// the `selectionchange` event may not even fire, for
// example when the window receives focus again on click.
element.addEventListener( 'keyup', handleSelectionChange );
element.addEventListener( 'mouseup', handleSelectionChange );
element.addEventListener( 'touchend', handleSelectionChange );
ownerDocument.addEventListener(
'selectionchange',
handleSelectionChange
Expand All @@ -322,9 +347,7 @@ export function useInputAndSelection( props ) {
);
element.removeEventListener( 'compositionend', onCompositionEnd );
element.removeEventListener( 'focus', onFocus );
element.removeEventListener( 'keyup', handleSelectionChange );
element.removeEventListener( 'mouseup', handleSelectionChange );
element.removeEventListener( 'touchend', handleSelectionChange );
removeSelectionAfterFocusHandlers();
ownerDocument.removeEventListener(
'selectionchange',
handleSelectionChange
Expand Down