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 Insert Before/After menu items and shortcuts #8909

Merged
merged 8 commits into from
Aug 15, 2018
18 changes: 13 additions & 5 deletions edit-post/components/keyboard-shortcut-help-modal/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { displayShortcutList } from '@wordpress/keycodes';
import { __ } from '@wordpress/i18n';

const {
// Cmd+<key> on a mac, Ctrl+<key> elsewhere
// Cmd+<key> on a mac, Ctrl+<key> elsewhere.
primary,
// Shift+Cmd+<key> on a mac, Ctrl+Shift+<key> elsewhere
// Shift+Cmd+<key> on a mac, Ctrl+Shift+<key> elsewhere.
primaryShift,
// Alt+Cmd+<key> on a mac, Ctrl+Alt+<key> elsewhere
// Option+Cmd+<key> on a mac, Ctrl+Alt+<key> elsewhere.
primaryAlt,
// Shift+Alt+Cmd+<key> on a mac, Ctrl+Shift+Akt+<key> elsewhere
// Shift+Alt+Cmd+<key> on a mac, Ctrl+Shift+Akt+<key> elsewhere.
secondary,
// Ctrl+Alt+<key> on a mac, Shift+Alt+<key> elsewhere
// Ctrl+Alt+<key> on a mac, Shift+Alt+<key> elsewhere.
access,
ctrl,
ctrlShift,
Expand Down Expand Up @@ -91,6 +91,14 @@ const blockShortcuts = {
keyCombination: primaryAlt( 'backspace' ),
description: __( 'Remove the selected block(s).' ),
},
{
keyCombination: primaryAlt( 't' ),
description: __( 'Insert a new block before the selected block(s).' ),
},
{
keyCombination: primaryAlt( 'y' ),
description: __( 'Insert a new block after the selected block(s).' ),
},
{
keyCombination: '/',
description: __( `Change the block type after adding a new paragraph.` ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ exports[`KeyboardShortcutHelpModal should match snapshot when the modal is activ
"Backspace",
],
},
Object {
"description": "Insert a new block before the selected block(s).",
"keyCombination": Array [
"Ctrl",
"+",
"Alt",
"+",
"T",
],
},
Object {
"description": "Insert a new block after the selected block(s).",
"keyCombination": Array [
"Ctrl",
"+",
"Alt",
"+",
"Y",
],
},
Object {
"description": "Change the block type after adding a new paragraph.",
"keyCombination": "/",
Expand Down
81 changes: 73 additions & 8 deletions packages/editor/src/components/block-settings-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { castArray, first, last, every, flow } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { Component, Fragment } from '@wordpress/element';
import { IconButton, Dropdown, NavigableMenu, MenuItem, KeyboardShortcuts } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
Expand Down Expand Up @@ -40,6 +40,14 @@ const shortcuts = {
raw: rawShortcut.primaryAlt( 'backspace' ),
display: displayShortcut.primaryAlt( 'bksp' ),
},
insertBefore: {
raw: rawShortcut.primaryAlt( 't' ),
display: displayShortcut.primaryAlt( 't' ),
},
insertAfter: {
raw: rawShortcut.primaryAlt( 'y' ),
display: displayShortcut.primaryAlt( 'y' ),
},
};

export class BlockSettingsMenu extends Component {
Expand Down Expand Up @@ -72,6 +80,8 @@ export class BlockSettingsMenu extends Component {
isHidden,
onDuplicate,
onRemove,
onInsertBefore,
onInsertAfter,
canDuplicate,
isLocked,
} = this.props;
Expand All @@ -90,8 +100,15 @@ export class BlockSettingsMenu extends Component {
[ shortcuts.duplicate.raw ]: flow( preventDefault, onDuplicate ),

// Does not clash with any known browser/native shortcuts, but preventDefault
// is used to prevent any obscure unknown shortcuts from triggering
// is used to prevent any obscure unknown shortcuts from triggering.
[ shortcuts.removeBlock.raw ]: flow( preventDefault, onRemove ),

// Prevent 'view recently closed tabs' in Opera using preventDefault.
[ shortcuts.insertBefore.raw ]: flow( preventDefault, onInsertBefore ),

// Does not clash with any known browser/native shortcuts, but preventDefault
// is used to prevent any obscure unknown shortcuts from triggering.
[ shortcuts.insertAfter.raw ]: flow( preventDefault, onInsertAfter ),
} }
/>
<Dropdown
Expand Down Expand Up @@ -123,7 +140,6 @@ export class BlockSettingsMenu extends Component {
);
} }
renderContent={ ( { onClose } ) => (
// Should this just use a DropdownMenu instead of a DropDown ?
<NavigableMenu className="editor-block-settings-menu__content">
<_BlockSettingsMenuFirstItem.Slot fillProps={ { onClose } } />
{ count === 1 && (
Expand All @@ -146,6 +162,26 @@ export class BlockSettingsMenu extends Component {
{ __( 'Duplicate' ) }
</MenuItem>
) }
{ ! isLocked && (
<Fragment>
<MenuItem
className="editor-block-settings-menu__control"
onClick={ onInsertBefore }
icon="insert-before"
shortcut={ shortcuts.insertBefore.display }
>
{ __( 'Insert Before' ) }
</MenuItem>
<MenuItem
className="editor-block-settings-menu__control"
onClick={ onInsertAfter }
icon="insert-after"
shortcut={ shortcuts.insertAfter.display }
>
{ __( 'Insert After' ) }
</MenuItem>
</Fragment>
) }
{ count === 1 && (
<BlockModeToggle
clientId={ firstBlockClientId }
Expand Down Expand Up @@ -199,15 +235,32 @@ export default compose( [
} );

return {
index: getBlockIndex( last( castArray( clientIds ) ), rootClientId ),
firstSelectedIndex: getBlockIndex( first( castArray( clientIds ) ), rootClientId ),
lastSelectedIndex: getBlockIndex( last( castArray( clientIds ) ), rootClientId ),
isLocked: !! getTemplateLock( rootClientId ),
blocks,
canDuplicate,
shortcuts,
};
} ),
withDispatch( ( dispatch, { clientIds, rootClientId, blocks, index, isLocked, canDuplicate } ) => {
const { insertBlocks, multiSelect, removeBlocks, selectBlock } = dispatch( 'core/editor' );
withDispatch( ( dispatch, props ) => {
const {
clientIds,
rootClientId,
blocks,
firstSelectedIndex,
lastSelectedIndex,
isLocked,
canDuplicate,
} = props;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: you could put newlines into these function arguments if you don't want props in the scope.

withDispatch( ( dispatch, {
	clientIds,
	rootClientId,
	// etc.
} ) ) {
	// etc.
}


const {
insertBlocks,
multiSelect,
removeBlocks,
selectBlock,
insertDefaultBlock,
} = dispatch( 'core/editor' );

return {
onDuplicate() {
Expand All @@ -218,7 +271,7 @@ export default compose( [
const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
insertBlocks(
clonedBlocks,
index + 1,
lastSelectedIndex + 1,
rootClientId
);
if ( clonedBlocks.length > 1 ) {
Expand All @@ -229,7 +282,19 @@ export default compose( [
}
},
onRemove() {
removeBlocks( clientIds );
if ( ! isLocked ) {
removeBlocks( clientIds );
}
Copy link
Member

Choose a reason for hiding this comment

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

Nice, a sneaky bug fix!

},
onInsertBefore() {
if ( ! isLocked ) {
insertDefaultBlock( {}, rootClientId, firstSelectedIndex );
}
},
onInsertAfter() {
if ( ! isLocked ) {
insertDefaultBlock( {}, rootClientId, lastSelectedIndex + 1 );
}
},
onSelect( clientId ) {
selectBlock( clientId );
Expand Down