Skip to content

Commit

Permalink
Remove text align control for paragraph, heading, and button in conte…
Browse files Browse the repository at this point in the history
…ntOnly editing mode (#57906)

* Remove text align control for paragraphs and buttons in contentOnly editing mode

* Remove heading level and text align on content only mode heading blocks

* Add a react native edit file for the heading block that does not use useBlockEditingMode
  • Loading branch information
talldan authored Jan 17, 2024
1 parent 2881d64 commit aec6947
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 39 deletions.
16 changes: 10 additions & 6 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
__experimentalLinkControl as LinkControl,
__experimentalGetElementClassName,
store as blockEditorStore,
useBlockEditingMode,
} from '@wordpress/block-editor';
import { displayShortcut, isKeyboardEvent, ENTER } from '@wordpress/keycodes';
import { link, linkOff } from '@wordpress/icons';
Expand Down Expand Up @@ -189,6 +190,7 @@ function ButtonEdit( props ) {
ref: useMergeRefs( [ setPopoverAnchor, ref ] ),
onKeyDown,
} );
const blockEditingMode = useBlockEditingMode();

const [ isEditingURL, setIsEditingURL ] = useState( false );
const isURLSet = !! url;
Expand Down Expand Up @@ -277,12 +279,14 @@ function ButtonEdit( props ) {
/>
</div>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
{ blockEditingMode === 'default' && (
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
) }
{ ! isURLSet && isLinkTag && (
<ToolbarButton
name="link"
Expand Down
32 changes: 18 additions & 14 deletions packages/block-library/src/heading/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useBlockProps,
store as blockEditorStore,
HeadingLevelDropdown,
useBlockEditingMode,
} from '@wordpress/block-editor';

/**
Expand All @@ -40,6 +41,7 @@ function HeadingEdit( {
} ),
style,
} );
const blockEditingMode = useBlockEditingMode();

const { canGenerateAnchors } = useSelect( ( select ) => {
const { getGlobalBlockCount, getSettings } = select( blockEditorStore );
Expand Down Expand Up @@ -90,20 +92,22 @@ function HeadingEdit( {

return (
<>
<BlockControls group="block">
<HeadingLevelDropdown
value={ level }
onChange={ ( newLevel ) =>
setAttributes( { level: newLevel } )
}
/>
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
{ blockEditingMode === 'default' && (
<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 }
Expand Down
144 changes: 144 additions & 0 deletions packages/block-library/src/heading/edit.native.js
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;
42 changes: 23 additions & 19 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
RichText,
useBlockProps,
useSettings,
useBlockEditingMode,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { formatLtr } from '@wordpress/icons';
Expand Down Expand Up @@ -108,28 +109,31 @@ function ParagraphBlock( {
} ),
style: { direction },
} );
const blockEditingMode = useBlockEditingMode();

return (
<>
<BlockControls group="block">
<AlignmentControl
value={ align }
onChange={ ( newAlign ) =>
setAttributes( {
align: newAlign,
dropCap: hasDropCapDisabled( newAlign )
? false
: dropCap,
} )
}
/>
<ParagraphRTLControl
direction={ direction }
setDirection={ ( newDirection ) =>
setAttributes( { direction: newDirection } )
}
/>
</BlockControls>
{ blockEditingMode === 'default' && (
<BlockControls group="block">
<AlignmentControl
value={ align }
onChange={ ( newAlign ) =>
setAttributes( {
align: newAlign,
dropCap: hasDropCapDisabled( newAlign )
? false
: dropCap,
} )
}
/>
<ParagraphRTLControl
direction={ direction }
setDirection={ ( newDirection ) =>
setAttributes( { direction: newDirection } )
}
/>
</BlockControls>
) }
<InspectorControls group="typography">
<DropCapControl
clientId={ clientId }
Expand Down

0 comments on commit aec6947

Please sign in to comment.