-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate the block editor shortcuts from the post editor shortcuts (#…
- Loading branch information
1 parent
fc92e93
commit 17d509b
Showing
3 changed files
with
174 additions
and
140 deletions.
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
158 changes: 158 additions & 0 deletions
158
packages/editor/src/components/global-keyboard-shortcuts/block-editor-shortcuts.js
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 |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { first, last, some, flow } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, Fragment } from '@wordpress/element'; | ||
import { KeyboardShortcuts } from '@wordpress/components'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { rawShortcut, displayShortcut } from '@wordpress/keycodes'; | ||
import { compose } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockActions from '../block-actions'; | ||
|
||
const preventDefault = ( event ) => { | ||
event.preventDefault(); | ||
return event; | ||
}; | ||
|
||
export const shortcuts = { | ||
duplicate: { | ||
raw: rawShortcut.primaryShift( 'd' ), | ||
display: displayShortcut.primaryShift( 'd' ), | ||
}, | ||
removeBlock: { | ||
raw: rawShortcut.access( 'z' ), | ||
display: displayShortcut.access( 'z' ), | ||
}, | ||
insertBefore: { | ||
raw: rawShortcut.primaryAlt( 't' ), | ||
display: displayShortcut.primaryAlt( 't' ), | ||
}, | ||
insertAfter: { | ||
raw: rawShortcut.primaryAlt( 'y' ), | ||
display: displayShortcut.primaryAlt( 'y' ), | ||
}, | ||
}; | ||
|
||
class BlockEditorKeyboardShortcuts extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.selectAll = this.selectAll.bind( this ); | ||
this.deleteSelectedBlocks = this.deleteSelectedBlocks.bind( this ); | ||
this.clearMultiSelection = this.clearMultiSelection.bind( this ); | ||
} | ||
|
||
selectAll( event ) { | ||
const { rootBlocksClientIds, onMultiSelect } = this.props; | ||
event.preventDefault(); | ||
onMultiSelect( first( rootBlocksClientIds ), last( rootBlocksClientIds ) ); | ||
} | ||
|
||
deleteSelectedBlocks( event ) { | ||
const { selectedBlockClientIds, hasMultiSelection, onRemove, isLocked } = this.props; | ||
if ( hasMultiSelection ) { | ||
event.preventDefault(); | ||
if ( ! isLocked ) { | ||
onRemove( selectedBlockClientIds ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Clears current multi-selection, if one exists. | ||
*/ | ||
clearMultiSelection() { | ||
const { hasMultiSelection, clearSelectedBlock } = this.props; | ||
if ( hasMultiSelection ) { | ||
clearSelectedBlock(); | ||
window.getSelection().removeAllRanges(); | ||
} | ||
} | ||
|
||
render() { | ||
const { selectedBlockClientIds } = this.props; | ||
return ( | ||
<Fragment> | ||
<KeyboardShortcuts | ||
shortcuts={ { | ||
[ rawShortcut.primary( 'a' ) ]: this.selectAll, | ||
backspace: this.deleteSelectedBlocks, | ||
del: this.deleteSelectedBlocks, | ||
escape: this.clearMultiSelection, | ||
} } | ||
/> | ||
{ selectedBlockClientIds.length > 0 && ( | ||
<BlockActions clientIds={ selectedBlockClientIds }> | ||
{ ( { onDuplicate, onRemove, onInsertAfter, onInsertBefore } ) => ( | ||
<KeyboardShortcuts | ||
bindGlobal | ||
shortcuts={ { | ||
// Prevents bookmark all Tabs shortcut in Chrome when devtools are closed. | ||
// Prevents reposition Chrome devtools pane shortcut when devtools are open. | ||
[ 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. | ||
[ 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 ), | ||
} } | ||
/> | ||
) } | ||
</BlockActions> | ||
) } | ||
</Fragment> | ||
); | ||
} | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
const { | ||
getBlockOrder, | ||
getMultiSelectedBlockClientIds, | ||
hasMultiSelection, | ||
getBlockRootClientId, | ||
getTemplateLock, | ||
getSelectedBlockClientId, | ||
} = select( 'core/block-editor' ); | ||
const selectedBlockClientId = getSelectedBlockClientId(); | ||
const selectedBlockClientIds = selectedBlockClientId ? [ selectedBlockClientId ] : getMultiSelectedBlockClientIds(); | ||
|
||
return { | ||
rootBlocksClientIds: getBlockOrder(), | ||
hasMultiSelection: hasMultiSelection(), | ||
isLocked: some( | ||
selectedBlockClientIds, | ||
( clientId ) => !! getTemplateLock( getBlockRootClientId( clientId ) ) | ||
), | ||
selectedBlockClientIds, | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => { | ||
const { | ||
clearSelectedBlock, | ||
multiSelect, | ||
removeBlocks, | ||
} = dispatch( 'core/block-editor' ); | ||
|
||
return { | ||
clearSelectedBlock, | ||
onMultiSelect: multiSelect, | ||
onRemove: removeBlocks, | ||
}; | ||
} ), | ||
] )( BlockEditorKeyboardShortcuts ); |
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