From f54571715fff0486ccfc2ebc290a7c4c7ce8034c Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 17 Jan 2024 16:33:52 +0800 Subject: [PATCH] Remove text align control for paragraph, heading, and button in contentOnly 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 --- packages/block-library/src/button/edit.js | 16 +- packages/block-library/src/heading/edit.js | 32 ++-- .../block-library/src/heading/edit.native.js | 144 ++++++++++++++++++ packages/block-library/src/paragraph/edit.js | 42 ++--- 4 files changed, 195 insertions(+), 39 deletions(-) create mode 100644 packages/block-library/src/heading/edit.native.js diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index d41d917d5b8fe1..b46e145d760ad5 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -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'; @@ -189,6 +190,7 @@ function ButtonEdit( props ) { ref: useMergeRefs( [ setPopoverAnchor, ref ] ), onKeyDown, } ); + const blockEditingMode = useBlockEditingMode(); const [ isEditingURL, setIsEditingURL ] = useState( false ); const isURLSet = !! url; @@ -277,12 +279,14 @@ function ButtonEdit( props ) { /> - { - setAttributes( { textAlign: nextAlign } ); - } } - /> + { blockEditingMode === 'default' && ( + { + setAttributes( { textAlign: nextAlign } ); + } } + /> + ) } { ! isURLSet && isLinkTag && ( { const { getGlobalBlockCount, getSettings } = select( blockEditorStore ); @@ -90,20 +92,22 @@ function HeadingEdit( { return ( <> - - - setAttributes( { level: newLevel } ) - } - /> - { - setAttributes( { textAlign: nextAlign } ); - } } - /> - + { blockEditingMode === 'default' && ( + + + setAttributes( { level: newLevel } ) + } + /> + { + setAttributes( { textAlign: nextAlign } ); + } } + /> + + ) } { + 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 ( + <> + + + setAttributes( { level: newLevel } ) + } + /> + { + setAttributes( { textAlign: nextAlign } ); + } } + /> + + { + 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; diff --git a/packages/block-library/src/paragraph/edit.js b/packages/block-library/src/paragraph/edit.js index ac766f69dd846a..cecfdce474c51a 100644 --- a/packages/block-library/src/paragraph/edit.js +++ b/packages/block-library/src/paragraph/edit.js @@ -19,6 +19,7 @@ import { RichText, useBlockProps, useSettings, + useBlockEditingMode, } from '@wordpress/block-editor'; import { createBlock } from '@wordpress/blocks'; import { formatLtr } from '@wordpress/icons'; @@ -108,28 +109,31 @@ function ParagraphBlock( { } ), style: { direction }, } ); + const blockEditingMode = useBlockEditingMode(); return ( <> - - - setAttributes( { - align: newAlign, - dropCap: hasDropCapDisabled( newAlign ) - ? false - : dropCap, - } ) - } - /> - - setAttributes( { direction: newDirection } ) - } - /> - + { blockEditingMode === 'default' && ( + + + setAttributes( { + align: newAlign, + dropCap: hasDropCapDisabled( newAlign ) + ? false + : dropCap, + } ) + } + /> + + setAttributes( { direction: newDirection } ) + } + /> + + ) }