Skip to content

Commit

Permalink
Restore but only selectively
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 24, 2023
1 parent 51d5525 commit 0381c93
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useCopyHandler } from './use-copy-handler';
import { useFormatBoundaries } from './use-format-boundaries';
import { useSelectObject } from './use-select-object';
import { useInputAndSelection } from './use-input-and-selection';
import { useSelectionChangeCompat } from './use-selection-change-compat';
import { useDelete } from './use-delete';

export function useRichText( {
Expand Down Expand Up @@ -240,6 +241,7 @@ export function useRichText( {
isSelected,
onSelectionChange,
} ),
useSelectionChangeCompat(),
useRefEffect( () => {
applyFromProps();
didMount.current = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function useInputAndSelection( props ) {

/**
* Syncs the selection to local state. A callback for the
* `selectionchange`, `keyup`, `mouseup` and `touchend` events.
* `selectionchange` native events.
*
* @param {Event} event
*/
Expand Down
48 changes: 48 additions & 0 deletions packages/rich-text/src/component/use-selection-change-compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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;

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();
ownerDocument.dispatchEvent( new Event( 'selectionchange' ) );
}

ownerDocument.addEventListener( type, onUp );
ownerDocument.addEventListener( 'selectionchange', onCancel );
ownerDocument.addEventListener( 'input', onCancel );
}

element.addEventListener( 'pointerdown', onDown );
element.addEventListener( 'keydown', onDown );
return () => {
element.removeEventListener( 'pointerdown', onDown );
element.removeEventListener( 'keydown', onDown );
};
}, [] );
}

0 comments on commit 0381c93

Please sign in to comment.