-
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.
Abstract keyboard shorcuts for heading to paragraph transform and vic…
…e-versa.
- Loading branch information
Showing
12 changed files
with
113 additions
and
286 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
94 changes: 94 additions & 0 deletions
94
packages/block-editor/src/components/block-keyboard-shortcuts/index.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,94 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
useShortcut, | ||
store as keyboardShortcutsStore, | ||
} from '@wordpress/keyboard-shortcuts'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
function BlockKeyboardShortcuts() { | ||
const { registerShortcut } = useDispatch( keyboardShortcutsStore ); | ||
const { replaceBlocks } = useDispatch( blockEditorStore ); | ||
const { getBlockName, getSelectedBlockClientId, getBlockAttributes } = | ||
useSelect( blockEditorStore ); | ||
|
||
const handleTransformHeadingAndParagraph = ( event, level ) => { | ||
event.preventDefault(); | ||
const destinationBlockName = | ||
level === 0 ? 'core/paragraph' : 'core/heading'; | ||
const currentClientId = getSelectedBlockClientId(); | ||
if ( currentClientId === null ) { | ||
return; | ||
} | ||
const blockName = getBlockName( currentClientId ); | ||
if ( blockName !== 'core/paragraph' && blockName !== 'core/heading' ) { | ||
return; | ||
} | ||
const attributes = getBlockAttributes( currentClientId ); | ||
const textAlign = | ||
blockName === 'core/paragraph' ? 'align' : 'textAlign'; | ||
const destinationTextAlign = | ||
destinationBlockName === 'core/paragraph' ? 'align' : 'textAlign'; | ||
|
||
replaceBlocks( | ||
currentClientId, | ||
createBlock( destinationBlockName, { | ||
level, | ||
content: attributes.content, | ||
...{ [ destinationTextAlign ]: attributes[ textAlign ] }, | ||
} ) | ||
); | ||
}; | ||
|
||
useEffect( () => { | ||
registerShortcut( { | ||
name: 'core/block-editor/transform-heading-to-paragraph', | ||
category: 'block-library', | ||
description: __( 'Transform heading to paragraph.' ), | ||
keyCombination: { | ||
modifier: 'access', | ||
character: `0`, | ||
}, | ||
} ); | ||
|
||
[ 1, 2, 3, 4, 5, 6 ].forEach( ( level ) => { | ||
registerShortcut( { | ||
name: `core/block-editor/transform-paragraph-to-heading-${ level }`, | ||
category: 'block-library', | ||
description: __( 'Transform paragraph to heading.' ), | ||
keyCombination: { | ||
modifier: 'access', | ||
character: `${ level }`, | ||
}, | ||
} ); | ||
} ); | ||
}, [] ); | ||
|
||
useShortcut( | ||
'core/block-editor/transform-heading-to-paragraph', | ||
( event ) => handleTransformHeadingAndParagraph( event, 0 ) | ||
); | ||
|
||
[ 1, 2, 3, 4, 5, 6 ].forEach( ( level ) => { | ||
//the loop is based off on a constant therefore | ||
//the hook will execute the same way every time | ||
//eslint-disable-next-line react-hooks/rules-of-hooks | ||
useShortcut( | ||
`core/block-editor/transform-paragraph-to-heading-${ level }`, | ||
( event ) => handleTransformHeadingAndParagraph( event, level ) | ||
); | ||
} ); | ||
|
||
return null; | ||
} | ||
|
||
export default BlockKeyboardShortcuts; |
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
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
Oops, something went wrong.