-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b615cf7
Add basic insert before and insert after buttons to block settings menu
talldan b662663
Add keyboard shortcuts for insert before and insert after
talldan 3fdf129
Change insert before/after shortcuts to meta+alt+t and meta+alt+y
talldan 0c75d3b
Add comment explaining use of preventDefault
talldan 22f6240
Add insert before/after shortcuts to keyboard help
talldan 200fc0c
Do not show Insert Before/After menu items when a block is template l…
talldan e5ae750
Disallow block settings actions that can be triggered through shortcu…
talldan e57f842
Tidy up comments
talldan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 { | ||
|
@@ -72,6 +80,8 @@ export class BlockSettingsMenu extends Component { | |
isHidden, | ||
onDuplicate, | ||
onRemove, | ||
onInsertBefore, | ||
onInsertAfter, | ||
canDuplicate, | ||
isLocked, | ||
} = this.props; | ||
|
@@ -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 | ||
|
@@ -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 && ( | ||
|
@@ -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 } | ||
|
@@ -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; | ||
|
||
const { | ||
insertBlocks, | ||
multiSelect, | ||
removeBlocks, | ||
selectBlock, | ||
insertDefaultBlock, | ||
} = dispatch( 'core/editor' ); | ||
|
||
return { | ||
onDuplicate() { | ||
|
@@ -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 ) { | ||
|
@@ -229,7 +282,19 @@ export default compose( [ | |
} | ||
}, | ||
onRemove() { | ||
removeBlocks( clientIds ); | ||
if ( ! isLocked ) { | ||
removeBlocks( clientIds ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.