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

Shortcuts: add Ctrl+Y for redo on Windows #42627

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
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
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 ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@talldan I reused the export form the key codes package for these two other instances. As you can see we're always making this check in the context of key codes, so I think it makes sense in this package.

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 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not completely convinced about exporting this from keycodes. Maybe there should be a @wordpress/platform package or something.

It's a very minor thing though, so I'll leave it up to you to decide what to do 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was wondering also if it's a good idea. But I looked through all the apple checks we're making in Gutenberg, and they're always related to the keyboard, so it seems to make sense as part of this package.


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