From 895ca1f6a7d7e492974ea55f693aecbeb1d5bbe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= <4710635+ellatrix@users.noreply.github.com> Date: Thu, 30 Jun 2022 13:27:48 +0200 Subject: [PATCH] Remove space handling (for BUTTON and SUMMARY) (#41977) --- packages/rich-text/src/component/index.js | 2 - packages/rich-text/src/component/use-space.js | 65 ------------------- 2 files changed, 67 deletions(-) delete mode 100644 packages/rich-text/src/component/use-space.js diff --git a/packages/rich-text/src/component/index.js b/packages/rich-text/src/component/index.js index 0cb718287c6849..26fab2cd8574bf 100644 --- a/packages/rich-text/src/component/index.js +++ b/packages/rich-text/src/component/index.js @@ -19,7 +19,6 @@ import { useSelectObject } from './use-select-object'; import { useIndentListItemOnSpace } from './use-indent-list-item-on-space'; import { useInputAndSelection } from './use-input-and-selection'; import { useDelete } from './use-delete'; -import { useSpace } from './use-space'; export function useRichText( { value = '', @@ -247,7 +246,6 @@ export function useRichText( { isSelected, onSelectionChange, } ), - useSpace(), useRefEffect( () => { applyFromProps(); didMount.current = true; diff --git a/packages/rich-text/src/component/use-space.js b/packages/rich-text/src/component/use-space.js deleted file mode 100644 index fcfb6e6cd989e1..00000000000000 --- a/packages/rich-text/src/component/use-space.js +++ /dev/null @@ -1,65 +0,0 @@ -/** - * WordPress dependencies - */ -import { useRefEffect } from '@wordpress/compose'; -import { SPACE } from '@wordpress/keycodes'; - -/** - * For some elements like BUTTON and SUMMARY, the space key doesn't insert a - * space character in some browsers even though the element is editable. We have - * to manually insert a space and prevent default behaviour. - * - * DO NOT limit this behaviour to specific tag names! It would mean that this - * behaviour is not widely tested. If there's ever any problems, we should find - * a different solution entirely or remove it entirely. - */ -export function useSpace() { - return useRefEffect( ( element ) => { - function onKeyDown( event ) { - // Don't insert a space if default behaviour is prevented. - if ( event.defaultPrevented ) { - return; - } - - const { keyCode, altKey, metaKey, ctrlKey, key } = event; - - // Only consider the space key without modifiers pressed. - if ( keyCode !== SPACE || altKey || metaKey || ctrlKey ) { - return; - } - - // Disregard character composition that involves the Space key. - // - // @see https://github.com/WordPress/gutenberg/issues/35086 - // - // For example, to input a standalone diacritic (like ´ or `) using a - // keyboard with dead keys, one must first press the dead key and then - // press the Space key. - // - // Many operating systems handle this in such a way that the second - // KeyboardEvent contains the property `keyCode: 229`. According to the - // spec, 229 allows the system to indicate that an Input Method Editor - // (IDE) is processing some key input. - // - // However, Windows doesn't use `keyCode: 229` for dead key composition, - // instead emitting an event with values `keyCode: SPACE` and `key: '´'`. - // That is why checking the `key` property for values other than `SPACE` - // is important. - // - // This should serve as a reminder that the `KeyboardEvent.keyCode` - // attribute is officially deprecated and that we should consider more - // consistent interfaces. - if ( key !== ' ' ) { - return; - } - - event.target.ownerDocument.execCommand( 'insertText', false, ' ' ); - event.preventDefault(); - } - - element.addEventListener( 'keydown', onKeyDown ); - return () => { - element.removeEventListener( 'keydown', onKeyDown ); - }; - }, [] ); -}