-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Abstract keyboard shorcuts for heading to paragraph transform and vice-versa #60606
Merged
afercia
merged 5 commits into
trunk
from
fix/abstract-heading-paragraph-transform-keyboard-shorcuts
Apr 22, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d953aaa
Abstract keyboard shorcuts for heading to paragraph transform and vic…
afercia e6e2648
Add alias to transform heading to paragraph for consistency with clas…
afercia ed27957
Move code to block-library.
afercia e3f79ec
Make BlockKeyboardShortcuts private.
afercia ce27aeb
Adjust after rebase.
afercia 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
96 changes: 96 additions & 0 deletions
96
packages/block-library/src/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,96 @@ | ||
/** | ||
* 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'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
||
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', | ||
}, | ||
aliases: [ | ||
{ | ||
modifier: 'access', | ||
character: '7', | ||
}, | ||
], | ||
} ); | ||
|
||
[ 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { default as BlockKeyboardShortcuts } from './block-keyboard-shortcuts'; | ||
import { lock } from './lock-unlock'; | ||
|
||
/** | ||
* @private | ||
*/ | ||
export const privateApis = {}; | ||
lock( privateApis, { | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
In this component registration and usage is merged. They were in different components before. The reason is that in some contexts, we want to register the shortcuts (show them in the keyboard shortcuts modal) but "disable" them in some contexts (view mode in the site editor for instance).
I'm not sure whether this creates issues for this PR, it needs to be tested.
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.
It seems the
Block
prefix is redundant here as well, we're already in the block library package.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.
I can rename it but then I'd have to import it 'as' a different name you know, because other components are already named KeyboardShortcuts.
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.
yeah, we do that already for other things. I don't care much though, it's a private API anyway.
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.
I think they were separated only in edit-site, where the
use
was here? Not sure that makes a difference, because they are within a check for isEditMode?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.
yes, for this PR it probably doesn't change much.