Skip to content

Commit

Permalink
Shortcuts: add Ctrl+Y for redo on Windows (#42627)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jul 27, 2022
1 parent 8d36703 commit 6da2f27
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
18 changes: 1 addition & 17 deletions packages/block-editor/src/components/inserter-list-item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';
import { __experimentalTruncate as Truncate } from '@wordpress/components';
import { ENTER } from '@wordpress/keycodes';
import { ENTER, isAppleOS } from '@wordpress/keycodes';

/**
* Internal dependencies
Expand All @@ -21,22 +21,6 @@ import BlockIcon from '../block-icon';
import { InserterListboxItem } from '../inserter-listbox';
import InserterDraggableBlocks from '../inserter-draggable-blocks';

/**
* Return true if platform is MacOS.
*
* @param {Object} _window window object by default; used for DI testing.
*
* @return {boolean} True if MacOS; false otherwise.
*/
function isAppleOS( _window = window ) {
const { platform } = _window.navigator;

return (
platform.indexOf( 'Mac' ) !== -1 ||
[ 'iPad', 'iPhone' ].includes( platform )
);
}

function InserterListItem( {
className,
isFirst,
Expand Down
19 changes: 2 additions & 17 deletions packages/compose/src/hooks/use-keyboard-shortcut/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
*/
import Mousetrap from 'mousetrap';
import 'mousetrap/plugins/global-bind/mousetrap-global-bind';
import { includes, castArray } from 'lodash';
import { castArray } from 'lodash';

/**
* WordPress dependencies
*/
import { useEffect, useRef } from '@wordpress/element';
import { isAppleOS } from '@wordpress/keycodes';

/**
* A block selection object.
Expand All @@ -21,22 +22,6 @@ import { useEffect, useRef } from '@wordpress/element';
* @property {import('react').RefObject<HTMLElement>} [target] React reference to the DOM element used to catch the keyboard event.
*/

/**
* Return true if platform is MacOS.
*
* @param {Window} [_window] window object by default; used for DI testing.
*
* @return {boolean} True if MacOS; false otherwise.
*/
function isAppleOS( _window = window ) {
const { platform } = _window.navigator;

return (
platform.indexOf( 'Mac' ) !== -1 ||
includes( [ 'iPad', 'iPhone' ], platform )
);
}

/* eslint-disable jsdoc/valid-types */
/**
* Attach a keyboard shortcut handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { BlockEditorKeyboardShortcuts } from '@wordpress/block-editor';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import { isAppleOS } from '@wordpress/keycodes';

function EditorKeyboardShortcutsRegister() {
// Registering the shortcuts.
Expand Down Expand Up @@ -39,6 +40,18 @@ function EditorKeyboardShortcutsRegister() {
modifier: 'primaryShift',
character: 'z',
},
// Disable on Apple OS because it conflicts with the browser's
// history shortcut. It's a fine alias for both Windows and Linux.
// Since there's no conflict for Ctrl+Shift+Z on both Windows and
// Linux, we keep it as the default for consistency.
aliases: isAppleOS()
? []
: [
{
modifier: 'primary',
character: 'y',
},
],
} );
}, [ registerShortcut ] );

Expand Down
12 changes: 12 additions & 0 deletions packages/keycodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ Keycode for F10 key.

Keycode for HOME key.

### isAppleOS

Return true if platform is MacOS.

_Parameters_

- _\_window_ `Window?`: window object by default; used for DI testing.

_Returns_

- `boolean`: True if MacOS; false otherwise.

### isKeyboardEvent

An object that contains functions to check if a keyboard event matches a
Expand Down
2 changes: 2 additions & 0 deletions packages/keycodes/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export const SHIFT = 'shift';
*/
export const ZERO = 48;

export { isAppleOS };

/**
* Object that contains functions that return the available modifier
* depending on platform.
Expand Down

0 comments on commit 6da2f27

Please sign in to comment.