From a6e9a24798ac4e8ca954ebdf762a614a2c4df7ff Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Thu, 29 Sep 2022 07:38:43 +0200 Subject: [PATCH] Disable drop cap control if text is aligned (#42326) * Disable drop cap control if text is aligned --- .../block-library/src/paragraph/deprecated.js | 36 +++++++++++++++++++ packages/block-library/src/paragraph/edit.js | 31 +++++++++++----- packages/block-library/src/paragraph/save.js | 6 +++- .../block-library/src/paragraph/style.scss | 5 +++ 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/paragraph/deprecated.js b/packages/block-library/src/paragraph/deprecated.js index 3d3505b9f132ec..b5b62992aab769 100644 --- a/packages/block-library/src/paragraph/deprecated.js +++ b/packages/block-library/src/paragraph/deprecated.js @@ -11,8 +11,11 @@ import { getColorClassName, getFontSizeClass, RichText, + useBlockProps, } from '@wordpress/block-editor'; +import { isRTL } from '@wordpress/i18n'; + const supports = { className: false, }; @@ -90,6 +93,39 @@ const migrateCustomColorsAndFontSizes = ( attributes ) => { const { style, ...restBlockAttributes } = blockAttributes; const deprecated = [ + // Version without drop cap on aligned text. + { + supports, + attributes: { + ...restBlockAttributes, + customTextColor: { + type: 'string', + }, + customBackgroundColor: { + type: 'string', + }, + customFontSize: { + type: 'number', + }, + }, + save( { attributes } ) { + const { align, content, dropCap, direction } = attributes; + const className = classnames( { + 'has-drop-cap': + align === ( isRTL() ? 'left' : 'right' ) || + align === 'center' + ? false + : dropCap, + [ `has-text-align-${ align }` ]: align, + } ); + + return ( +

+ +

+ ); + }, + }, { supports, attributes: { diff --git a/packages/block-library/src/paragraph/edit.js b/packages/block-library/src/paragraph/edit.js index 799d2b9d5c462c..c049b0b72bd249 100644 --- a/packages/block-library/src/paragraph/edit.js +++ b/packages/block-library/src/paragraph/edit.js @@ -48,6 +48,10 @@ function ParagraphRTLControl( { direction, setDirection } ) { ); } +function hasDropCapDisabled( align ) { + return align === ( isRTL() ? 'left' : 'right' ) || align === 'center'; +} + function ParagraphBlock( { attributes, mergeBlocks, @@ -65,19 +69,33 @@ function ParagraphBlock( { setParagraphElement, ] ), className: classnames( { - 'has-drop-cap': dropCap, + 'has-drop-cap': hasDropCapDisabled( align ) ? false : dropCap, [ `has-text-align-${ align }` ]: align, } ), style: { direction }, } ); + let helpText; + if ( hasDropCapDisabled( align ) ) { + helpText = __( 'Not available for aligned text.' ); + } else if ( dropCap ) { + helpText = __( 'Showing large initial letter.' ); + } else { + helpText = __( 'Toggle to show a large initial letter.' ); + } + return ( <> - setAttributes( { align: newAlign } ) + setAttributes( { + align: newAlign, + dropCap: hasDropCapDisabled( newAlign ) + ? false + : dropCap, + } ) } /> setAttributes( { dropCap: ! dropCap } ) } - help={ - dropCap - ? __( 'Showing large initial letter.' ) - : __( - 'Toggle to show a large initial letter.' - ) + help={ helpText } + disabled={ + hasDropCapDisabled( align ) ? true : false } /> diff --git a/packages/block-library/src/paragraph/save.js b/packages/block-library/src/paragraph/save.js index 9bc9921e31334a..5f451225ff2237 100644 --- a/packages/block-library/src/paragraph/save.js +++ b/packages/block-library/src/paragraph/save.js @@ -7,11 +7,15 @@ import classnames from 'classnames'; * WordPress dependencies */ import { RichText, useBlockProps } from '@wordpress/block-editor'; +import { isRTL } from '@wordpress/i18n'; export default function save( { attributes } ) { const { align, content, dropCap, direction } = attributes; const className = classnames( { - 'has-drop-cap': dropCap, + 'has-drop-cap': + align === ( isRTL() ? 'left' : 'right' ) || align === 'center' + ? false + : dropCap, [ `has-text-align-${ align }` ]: align, } ); diff --git a/packages/block-library/src/paragraph/style.scss b/packages/block-library/src/paragraph/style.scss index 810787c64692ac..f3bb9f8c5aee86 100644 --- a/packages/block-library/src/paragraph/style.scss +++ b/packages/block-library/src/paragraph/style.scss @@ -28,6 +28,11 @@ font-style: normal; } +body.rtl .has-drop-cap:not(:focus)::first-letter { + float: initial; + margin-left: 0.1em; +} + // Prevent the dropcap from breaking out of the box when a background is applied. p.has-drop-cap.has-background { overflow: hidden;