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

Add new keyboard shortcuts help modal #8316

Merged
merged 40 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1da0ece
Create a simple modal for showing keyboard shortcuts that displays wh…
talldan Jul 31, 2018
a6a0061
Add more shortcuts and place them in sections with a title and descri…
talldan Jul 31, 2018
7175829
Add styles for shortcut modal
talldan Jul 31, 2018
dbe8e48
Add missing shortctuts
talldan Jul 31, 2018
634cccf
Remove unecessary shortcut section descriptions
talldan Aug 1, 2018
374acbb
Display shortcut keys in a kbd element
talldan Aug 1, 2018
b611aa4
Styling changes
talldan Aug 1, 2018
9c2e6c8
Regroup shortcuts
talldan Aug 1, 2018
be364fc
Fix invalid style rule
talldan Aug 1, 2018
a1c19c5
Change copy for slash inserter shortcut
talldan Aug 1, 2018
22b776c
Map over config to produce ShortcutSections
talldan Aug 1, 2018
9a92615
Change where keyboard shortcut help modal is rendered
talldan Aug 1, 2018
6f8158a
Move keyboard-shortcut-help-modal to edit-post from editor package
talldan Aug 2, 2018
d2cdc0f
Add some border between rows in the keyboard shortcuts list
talldan Aug 2, 2018
845e68a
Add reduxy things for opening and closing a modal
talldan Aug 2, 2018
acf1851
Use actions to open/close modal and selector to determine current state
talldan Aug 2, 2018
542567b
Migrate the activeModal state from preferences to normal state so tha…
talldan Aug 2, 2018
7384e36
Add a menu item tot he more menu for toggling keyboard shortcut help
talldan Aug 2, 2018
5f9d83e
Modify some shortcuts and description text
talldan Aug 2, 2018
09b655e
Add snapshot tests for help modal
talldan Aug 2, 2018
a08466e
Destructure props
talldan Aug 2, 2018
f7928bd
Modify header styling
talldan Aug 2, 2018
8cd1a3d
Rename class name to reflect component is now in edit-post, and reord…
talldan Aug 2, 2018
941771b
Display shortcut for more menu menu item
talldan Aug 3, 2018
886f147
Disable WP_Help TinyMCE command for classic block
talldan Aug 7, 2018
2f0bd3d
Change keyboard shortcut for triggering help to access + h, the same …
talldan Aug 7, 2018
bb9be30
Add utility to keycodes for returning key combinations in an array
talldan Aug 7, 2018
282b3af
Avoid ugly regular expression to split key combination into array
talldan Aug 7, 2018
79d4a58
Add e2e test for shortcut help modal
talldan Aug 9, 2018
cd508ba
Vertically center shortcut text (if adjacent label wraps onto multipl…
talldan Aug 9, 2018
1e8c7b4
Close more menu when keyboard shortcuts modal is opened using the men…
talldan Aug 9, 2018
2e5fdce
Use castArray over lengthy ternary
talldan Aug 9, 2018
99ac0ca
Styling linter fixes
talldan Aug 9, 2018
0fe9a1c
JSDoc fixes
talldan Aug 9, 2018
223718b
Group activeModal unit tests in a describe
talldan Aug 9, 2018
f3c8056
Add test for initial activeModal state
talldan Aug 9, 2018
6020c68
Fix incorrect state used in test
talldan Aug 9, 2018
3374379
Update list of key shortcuts for the help modal
talldan Aug 9, 2018
d7aedc2
Remove unecessary return
talldan Aug 10, 2018
52aa738
Make KeyboardShortcutsHelpMenuItem only open the help modal
talldan Aug 10, 2018
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
7 changes: 7 additions & 0 deletions core-blocks/freeform/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ export default class FreeformEdit extends Component {

this.editor = editor;

// Disable TinyMCE's keyboard shortcut help.
editor.on( 'BeforeExecCommand', ( event ) => {
if ( event.command === 'WP_Help' ) {
event.preventDefault();
}
} );
Copy link
Member

Choose a reason for hiding this comment

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

Wondering if editor.shortcuts.remove( 'ctrl+alt+h' ) might be a nicer way of doing this? Not sure.

https://www.tiny.cloud/docs/api/tinymce/tinymce.shortcuts/

Copy link
Contributor Author

@talldan talldan Aug 9, 2018

Choose a reason for hiding this comment

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

Doesn't seem to work unfortunately. I also thought editor.editorCommands.addCommand( 'WP_Help', noop ) might also work but it doesn't. It suggests a race condition - that the WP_Help command is added after freeform block's setupEditor runs.

Copy link
Member

Choose a reason for hiding this comment

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

Does what we're doing in RichText help?

this.editor.shortcuts.remove( 'meta+y', '', 'Redo' );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly not. Guessing it works there because Redo is already bound by TinyMCE at this point.


if ( content ) {
editor.on( 'loadContent', () => editor.setContent( content ) );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress Dependencies
*/
import { withDispatch } from '@wordpress/data';
import { displayShortcut } from '@wordpress/keycodes';

/**
* WordPress Dependencies
*/
import { __ } from '@wordpress/i18n';
import { MenuItem } from '@wordpress/components';

export function KeyboardShortcutsHelpMenuItem( { openModal, onSelect } ) {
return (
<MenuItem
onClick={ () => {
onSelect();
openModal( 'edit-post/keyboard-shortcut-help' );
} }
shortcut={ displayShortcut.access( 'h' ) }
>
{ __( 'Keyboard Shortcuts' ) }
</MenuItem>
);
}

export default withDispatch( ( dispatch, ) => {
const {
openModal,
} = dispatch( 'core/edit-post' );

return {
openModal,
};
} )( KeyboardShortcutsHelpMenuItem );
5 changes: 4 additions & 1 deletion edit-post/components/header/more-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ModeSwitcher from '../mode-switcher';
import FixedToolbarToggle from '../fixed-toolbar-toggle';
import PluginMoreMenuGroup from '../plugins-more-menu-group';
import TipsToggle from '../tips-toggle';
import KeyboardShortcutsHelpMenuItem from '../keyboard-shortcuts-help-menu-item';

const MoreMenu = () => (
<Dropdown
Expand Down Expand Up @@ -39,7 +40,9 @@ const MoreMenu = () => (
<MenuGroup
label={ __( 'Tools' ) }
filterName="editPost.MoreMenu.tools"
/>
>
<KeyboardShortcutsHelpMenuItem onSelect={ onClose } />
</MenuGroup>
</div>
) }
/>
Expand Down
134 changes: 134 additions & 0 deletions edit-post/components/keyboard-shortcut-help-modal/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
Copy link
Member

Choose a reason for hiding this comment

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

Having this all in a config.js is a nice idea 🙂🌈

* WordPress dependencies
*/
import { displayShortcutList } from '@wordpress/keycodes';
import { __ } from '@wordpress/i18n';

const {
// Cmd+<key> on a mac, Ctrl+<key> elsewhere
primary,
// Shift+Cmd+<key> on a mac, Ctrl+Shift+<key> elsewhere
primaryShift,
// Shift+Alt+Cmd+<key> on a mac, Ctrl+Shift+Akt+<key> elsewhere
secondary,
// Ctrl+Alt+<key> on a mac, Shift+Alt+<key> elsewhere
access,
ctrl,
ctrlShift,
shiftAlt,
} = displayShortcutList;

const globalShortcuts = {
title: __( 'Global shortcuts' ),
shortcuts: [
{
keyCombination: access( 'h' ),
description: __( 'Display this help.' ),
},
{
keyCombination: primary( 's' ),
description: __( 'Save your changes.' ),
},
{
keyCombination: primary( 'z' ),
description: __( 'Undo your last changes.' ),
},
{
keyCombination: primaryShift( 'z' ),
description: __( 'Redo your last undo.' ),
},
{
keyCombination: primaryShift( ',' ),
description: __( 'Show or hide the settings sidebar.' ),
},
{
keyCombination: ctrl( '`' ),
description: __( 'Navigate to a the next part of the editor.' ),
},
{
keyCombination: ctrlShift( '`' ),
description: __( 'Navigate to the previous part of the editor.' ),
},
{
keyCombination: shiftAlt( 'n' ),
description: __( 'Navigate to a the next part of the editor (alternative).' ),
},
{
keyCombination: shiftAlt( 'p' ),
description: __( 'Navigate to the previous part of the editor (alternative).' ),
},
{
keyCombination: secondary( 'm' ),
description: __( 'Switch between Visual Editor and Code Editor.' ),
},
],
};

const selectionShortcuts = {
title: __( 'Selection shortcuts' ),
shortcuts: [
{
keyCombination: primary( 'a' ),
description: __( 'Select all text when typing. Press again to select all blocks.' ),
},
{
keyCombination: 'Esc',
description: __( 'Clear selection.' ),
},
],
};

const blockShortcuts = {
title: __( 'Block shortcuts' ),
shortcuts: [
{
keyCombination: primaryShift( 'd' ),
description: __( 'Duplicate the selected block(s).' ),
},
{
keyCombination: '/',
description: __( `Change the block type after adding a new paragraph.` ),
},
],
};

const textFormattingShortcuts = {
title: __( 'Text formatting' ),
shortcuts: [
{
keyCombination: primary( 'b' ),
description: __( 'Make the selected text bold.' ),
},
{
keyCombination: primary( 'i' ),
description: __( 'Make the selected text italic.' ),
},
{
keyCombination: primary( 'u' ),
description: __( 'Underline the selected text.' ),
},
{
keyCombination: primary( 'k' ),
description: __( 'Convert the selected text into a link.' ),
},
{
keyCombination: access( 's' ),
description: __( 'Remove a link.' ),
},
{
keyCombination: access( 'd' ),
description: __( 'Add a strikethrough to the selected text.' ),
},
{
keyCombination: access( 'x' ),
description: __( 'Display the selected text in a monospaced font.' ),
},
],
};

export default [
globalShortcuts,
selectionShortcuts,
blockShortcuts,
textFormattingShortcuts,
];
119 changes: 119 additions & 0 deletions edit-post/components/keyboard-shortcut-help-modal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';

/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { Modal, KeyboardShortcuts } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { rawShortcut } from '@wordpress/keycodes';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import shortcutConfig from './config';
import './style.scss';

const MODAL_NAME = 'edit-post/keyboard-shortcut-help';

const mapKeyCombination = ( keyCombination ) => keyCombination.map( ( character, index ) => {
if ( character === '+' ) {
return (
<Fragment key={ index }>
{ character }
</Fragment>
);
}

return (
<kbd
key={ index }
className="edit-post-keyboard-shortcut-help__shortcut-key"
>
{ character }
</kbd>
);
} );

const ShortcutList = ( { shortcuts } ) => (
<dl className="edit-post-keyboard-shortcut-help__shortcut-list">
{ shortcuts.map( ( { keyCombination, description }, index ) => (
<div
className="edit-post-keyboard-shortcut-help__shortcut"
key={ index }
>
<dt className="edit-post-keyboard-shortcut-help__shortcut-term">
<kbd className="edit-post-keyboard-shortcut-help__shortcut-key-combination">
{ mapKeyCombination( castArray( keyCombination ) ) }
</kbd>
</dt>
<dd className="edit-post-keyboard-shortcut-help__shortcut-description">
{ description }
</dd>
</div>
) ) }
</dl>
);

const ShortcutSection = ( { title, shortcuts } ) => (
<section className="edit-post-keyboard-shortcut-help__section">
<h2 className="edit-post-keyboard-shortcut-help__section-title">
{ title }
</h2>
<ShortcutList shortcuts={ shortcuts } />
</section>
);

export function KeyboardShortcutHelpModal( { isModalActive, toggleModal } ) {
const title = (
<span className="edit-post-keyboard-shortcut-help__title">
{ __( 'Keyboard Shortcuts' ) }
</span>
);

return (
<Fragment>
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.access( 'h' ) ]: toggleModal,
} }
/>
{ isModalActive && (
<Modal
className="edit-post-keyboard-shortcut-help"
title={ title }
closeLabel={ __( 'Close' ) }
onRequestClose={ toggleModal }
>

{ shortcutConfig.map( ( config, index ) => (
<ShortcutSection key={ index } { ...config } />
) ) }

</Modal>
) }
</Fragment>
);
}

export default compose( [
withSelect( ( select ) => ( {
isModalActive: select( 'core/edit-post' ).isModalActive( MODAL_NAME ),
} ) ),
withDispatch( ( dispatch, { isModalActive } ) => {
const {
openModal,
closeModal,
} = dispatch( 'core/edit-post' );

return {
toggleModal: () => isModalActive ? closeModal() : openModal( MODAL_NAME ),
};
} ),
] )( KeyboardShortcutHelpModal );
56 changes: 56 additions & 0 deletions edit-post/components/keyboard-shortcut-help-modal/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.edit-post-keyboard-shortcut-help {
&__title {
font-size: 1rem;
font-weight: bold;
}

&__section {
margin: 0 0 2rem 0;
}


&__section-title {
font-size: 0.9rem;
font-weight: bold;
}

&__shortcut {
display: flex;
align-items: center;
padding: 0.6rem 0;
border-top: 1px solid $light-gray-500;

&:last-child {
border-bottom: 1px solid $light-gray-500;
}
}

&__shortcut-term {
flex: 1;
order: 1;
text-align: right;
font-weight: bold;
margin: 0 0 0 1rem;
}

&__shortcut-description {
order: 0;
margin: 0;
}

&__shortcut-key-combination {
background: none;
margin: 0;
padding: 0;
}

&__shortcut-key {
padding: 0.25rem 0.5rem;
border-radius: 8%;
margin: 0 0.2rem 0 0.2rem;

&:last-child {
margin: 0 0 0 0.2rem;
}
}
}
Loading