-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Remove text align control for paragraph, heading, and button in contentOnly editing mode #57906
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
Merged
talldan
merged 3 commits into
trunk
from
remove/text-align-control-from-content-only-mode-for-a-few-blocks
Jan 17, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5c70648
Remove text align control for paragraphs and buttons in contentOnly e…
talldan f8fa12f
Remove heading level and text align on content only mode heading blocks
talldan b522b8e
Add a react native edit file for the heading block that does not use …
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
Prev
Previous commit
Add a react native edit file for the heading block that does not use …
…useBlockEditingMode
- Loading branch information
commit b522b8e541594833b6900ad5f64b09386ad28390
There are no files selected for viewing
This file contains hidden or 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,144 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import classnames from 'classnames'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useEffect, Platform } from '@wordpress/element'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
| import { createBlock, getDefaultBlockName } from '@wordpress/blocks'; | ||
| import { | ||
| AlignmentControl, | ||
| BlockControls, | ||
| RichText, | ||
| useBlockProps, | ||
| store as blockEditorStore, | ||
| HeadingLevelDropdown, | ||
| } from '@wordpress/block-editor'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { generateAnchor, setAnchor } from './autogenerate-anchors'; | ||
|
|
||
| function HeadingEdit( { | ||
| attributes, | ||
| setAttributes, | ||
| mergeBlocks, | ||
| onReplace, | ||
| style, | ||
| clientId, | ||
| } ) { | ||
| const { textAlign, content, level, placeholder, anchor } = attributes; | ||
| const tagName = 'h' + level; | ||
| const blockProps = useBlockProps( { | ||
| className: classnames( { | ||
| [ `has-text-align-${ textAlign }` ]: textAlign, | ||
| } ), | ||
| style, | ||
| } ); | ||
|
|
||
| const { canGenerateAnchors } = useSelect( ( select ) => { | ||
| const { getGlobalBlockCount, getSettings } = select( blockEditorStore ); | ||
| const settings = getSettings(); | ||
|
|
||
| return { | ||
| canGenerateAnchors: | ||
| !! settings.generateAnchors || | ||
| getGlobalBlockCount( 'core/table-of-contents' ) > 0, | ||
| }; | ||
| }, [] ); | ||
|
|
||
| const { __unstableMarkNextChangeAsNotPersistent } = | ||
| useDispatch( blockEditorStore ); | ||
|
|
||
| // Initially set anchor for headings that have content but no anchor set. | ||
| // This is used when transforming a block to heading, or for legacy anchors. | ||
| useEffect( () => { | ||
| if ( ! canGenerateAnchors ) { | ||
| return; | ||
| } | ||
|
|
||
| if ( ! anchor && content ) { | ||
| // This side-effect should not create an undo level. | ||
| __unstableMarkNextChangeAsNotPersistent(); | ||
| setAttributes( { | ||
| anchor: generateAnchor( clientId, content ), | ||
| } ); | ||
| } | ||
| setAnchor( clientId, anchor ); | ||
|
|
||
| // Remove anchor map when block unmounts. | ||
| return () => setAnchor( clientId, null ); | ||
| }, [ anchor, content, clientId, canGenerateAnchors ] ); | ||
|
|
||
| const onContentChange = ( value ) => { | ||
| const newAttrs = { content: value }; | ||
| if ( | ||
| canGenerateAnchors && | ||
| ( ! anchor || | ||
| ! value || | ||
| generateAnchor( clientId, content ) === anchor ) | ||
| ) { | ||
| newAttrs.anchor = generateAnchor( clientId, value ); | ||
| } | ||
| setAttributes( newAttrs ); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <BlockControls group="block"> | ||
| <HeadingLevelDropdown | ||
| value={ level } | ||
| onChange={ ( newLevel ) => | ||
| setAttributes( { level: newLevel } ) | ||
| } | ||
| /> | ||
| <AlignmentControl | ||
| value={ textAlign } | ||
| onChange={ ( nextAlign ) => { | ||
| setAttributes( { textAlign: nextAlign } ); | ||
| } } | ||
| /> | ||
| </BlockControls> | ||
| <RichText | ||
| identifier="content" | ||
| tagName={ tagName } | ||
| value={ content } | ||
| onChange={ onContentChange } | ||
| onMerge={ mergeBlocks } | ||
| onSplit={ ( value, isOriginal ) => { | ||
| let block; | ||
|
|
||
| if ( isOriginal || value ) { | ||
| block = createBlock( 'core/heading', { | ||
| ...attributes, | ||
| content: value, | ||
| } ); | ||
| } else { | ||
| block = createBlock( | ||
| getDefaultBlockName() ?? 'core/heading' | ||
| ); | ||
| } | ||
|
|
||
| if ( isOriginal ) { | ||
| block.clientId = clientId; | ||
| } | ||
|
|
||
| return block; | ||
| } } | ||
| onReplace={ onReplace } | ||
| onRemove={ () => onReplace( [] ) } | ||
| placeholder={ placeholder || __( 'Heading' ) } | ||
| textAlign={ textAlign } | ||
| { ...( Platform.isNative && { deleteEnter: true } ) } // setup RichText on native mobile to delete the "Enter" key as it's handled by the JS/RN side | ||
| { ...blockProps } | ||
| /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default HeadingEdit; | ||
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.
Hm, why was this duplicated?
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.
Native code doesn't support
useBlockEditingMode