Skip to content

Commit

Permalink
Block lib: use RichText.isEmpty where forgotten (#56726)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Dec 1, 2023
1 parent 029f45c commit c2dfa29
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/block-caption/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The `BlockCaption` component renders block-level UI for adding and editing capti
Renders an editable caption field designed specifically for block-level use.

```jsx
import { BlockCaption } from '@wordpress/block-editor';
import { BlockCaption, RichText } from '@wordpress/block-editor';

const MyBlockCaption = (
clientId,
Expand All @@ -29,7 +29,7 @@ const MyBlockCaption = (
clientId={ clientId }
accessible={ true }
accessibilityLabelCreator={ ( caption ) =>
! caption
RichText.isEmpty( caption )
? /* translators: accessibility text. Empty caption. */
'Caption. Empty'
: sprintf(
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/audio/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
MediaPlaceholder,
MediaUpload,
MediaUploadProgress,
RichText,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __, _x, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -227,7 +228,7 @@ function AudioEdit( {
<BlockCaption
accessible={ true }
accessibilityLabelCreator={ ( caption ) =>
! caption
RichText.isEmpty( caption )
? /* translators: accessibility text. Empty Audio caption. */
__( 'Audio caption. Empty' )
: sprintf(
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function save( { attributes, className } ) {
width,
} = attributes;

if ( ! text ) {
if ( RichText.isEmpty( text ) ) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/embed/embed-preview.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import classnames from 'classnames/dedupe';
import { View } from '@wordpress/primitives';
import {
BlockCaption,
RichText,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -51,7 +52,7 @@ const EmbedPreview = ( {
styles[ `embed-preview__sandbox--align-${ align }` ];

function accessibilityLabelCreator( caption ) {
return ! caption
return RichText.isEmpty( caption )
? /* translators: accessibility text. Empty Embed caption. */
__( 'Embed caption. Empty' )
: sprintf(
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
revokeBlobURL( href );
}

if ( downloadButtonText === undefined ) {
if ( RichText.isEmpty( downloadButtonText ) ) {
setAttributes( {
downloadButtonText: _x( 'Download', 'button label' ),
} );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/file/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class FileEdit extends Component {
const { attributes, setAttributes } = this.props;
const { downloadButtonText } = attributes;

if ( downloadButtonText === undefined || downloadButtonText === '' ) {
if ( RichText.isEmpty( downloadButtonText ) ) {
setAttributes( {
downloadButtonText: _x( 'Download', 'button label' ),
} );
Expand Down
8 changes: 6 additions & 2 deletions packages/block-library/src/gallery/gallery.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import styles from './gallery-styles.scss';
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { BlockCaption, useInnerBlocksProps } from '@wordpress/block-editor';
import {
BlockCaption,
RichText,
useInnerBlocksProps,
} from '@wordpress/block-editor';
import { useState, useEffect } from '@wordpress/element';
import { mediaUploadSync } from '@wordpress/react-native-bridge';
import { WIDE_ALIGNMENTS } from '@wordpress/components';
Expand Down Expand Up @@ -99,7 +103,7 @@ export const Gallery = ( props ) => {
isSelected={ isCaptionSelected }
accessible={ true }
accessibilityLabelCreator={ ( caption ) =>
! caption
RichText.isEmpty( caption )
? /* translators: accessibility text. Empty gallery caption. */

'Gallery caption. Empty'
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/gallery/v1/gallery.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Tiles from './tiles';
import { __, sprintf } from '@wordpress/i18n';
import {
BlockCaption,
RichText,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useState, useEffect } from '@wordpress/element';
Expand Down Expand Up @@ -141,7 +142,7 @@ export const Gallery = ( props ) => {
isSelected={ isCaptionSelected }
accessible={ true }
accessibilityLabelCreator={ ( caption ) =>
! caption
RichText.isEmpty( caption )
? /* translators: accessibility text. Empty gallery caption. */
'Gallery caption. Empty'
: sprintf(
Expand Down
5 changes: 2 additions & 3 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
BlockStyles,
store as blockEditorStore,
blockSettingsScreens,
RichText,
} from '@wordpress/block-editor';
import { __, _x, sprintf } from '@wordpress/i18n';
import { getProtocol, hasQueryArg, isURL } from '@wordpress/url';
Expand Down Expand Up @@ -329,9 +330,7 @@ export class ImageEdit extends Component {

accessibilityLabelCreator( caption ) {
// Checks if caption is empty.
return ( typeof caption === 'string' && caption.trim().length === 0 ) ||
caption === undefined ||
caption === null
return RichText.isEmpty( caption )
? /* translators: accessibility text. Empty image caption. */
'Image caption. Empty'
: sprintf(
Expand Down
8 changes: 4 additions & 4 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ function ParagraphBlock( {
onReplace={ onReplace }
onRemove={ onRemove }
aria-label={
content
? __( 'Block: Paragraph' )
: __(
RichText.isEmpty( content )
? __(
'Empty block; start writing or type forward slash to choose a block'
)
: __( 'Block: Paragraph' )
}
data-empty={ content ? false : true }
data-empty={ RichText.isEmpty( content ) }
placeholder={ placeholder || __( 'Type / to choose a block' ) }
data-custom-placeholder={ placeholder ? true : undefined }
__unstableEmbedURLOnPaste
Expand Down
23 changes: 12 additions & 11 deletions packages/block-library/src/quote/transforms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';

const transforms = {
Expand Down Expand Up @@ -113,14 +114,14 @@ const transforms = {
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { citation }, innerBlocks ) =>
citation
? [
RichText.isEmpty( citation )
? innerBlocks
: [
...innerBlocks,
createBlock( 'core/paragraph', {
content: citation,
} ),
]
: innerBlocks,
],
},
{
type: 'block',
Expand All @@ -129,26 +130,26 @@ const transforms = {
createBlock(
'core/group',
{ anchor },
citation
? [
RichText.isEmpty( citation )
? innerBlocks
: [
...innerBlocks,
createBlock( 'core/paragraph', {
content: citation,
} ),
]
: innerBlocks
),
},
],
ungroup: ( { citation }, innerBlocks ) =>
citation
? [
RichText.isEmpty( citation )
? innerBlocks
: [
...innerBlocks,
createBlock( 'core/paragraph', {
content: citation,
} ),
]
: innerBlocks,
],
};

export default transforms;
4 changes: 2 additions & 2 deletions packages/block-library/src/utils/caption.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export function Caption( {
} ) {
const caption = attributes[ key ];
const prevCaption = usePrevious( caption );
const isCaptionEmpty = ! caption?.length;
const isPrevCaptionEmpty = ! prevCaption?.length;
const isCaptionEmpty = RichText.isEmpty( caption );
const isPrevCaptionEmpty = RichText.isEmpty( prevCaption );
const [ showCaption, setShowCaption ] = useState( ! isCaptionEmpty );

// We need to show the caption when changes come from
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/video/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
VIDEO_ASPECT_RATIO,
VideoPlayer,
InspectorControls,
RichText,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -366,7 +367,7 @@ class VideoEdit extends Component {
<BlockCaption
accessible={ true }
accessibilityLabelCreator={ ( caption ) =>
! caption
RichText.isEmpty( caption )
? /* translators: accessibility text. Empty video caption. */
__( 'Video caption. Empty' )
: sprintf(
Expand Down

0 comments on commit c2dfa29

Please sign in to comment.