Skip to content

Commit

Permalink
Update edit.js
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinan committed Dec 16, 2022
1 parent ef266d5 commit daa5b05
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';
import { useMemo, useEffect } from '@wordpress/element';
import {
AlignmentToolbar,
BlockControls,
Expand All @@ -17,7 +17,7 @@ import {
useBlockProps,
} from '@wordpress/block-editor';
import { PanelBody, ToggleControl, RangeControl } from '@wordpress/components';
import { sprintf, __, _x } from '@wordpress/i18n';
import { sprintf, __, _n, _x } from '@wordpress/i18n';
import { count as wordCount } from '@wordpress/wordcount';
import { speak } from '@wordpress/a11y';
import { useDebounce } from '@wordpress/compose';
Expand All @@ -36,7 +36,7 @@ export default function PostExcerptEditor( {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const userCanEdit = useCanEditEntity( 'postType', postType, postId );
const isEditable = userCanEdit && ! isDescendentOfQueryLoop;
const debouncedSpeak = useDebounce( speak, 3000 );
const debouncedSpeak = useDebounce( speak, 500 );
const [
rawExcerpt,
setExcerpt,
Expand All @@ -47,6 +47,22 @@ export default function PostExcerptEditor( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

/**
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
const wordCountType = _x( 'words', 'Word count type. Do not translate!' );
const currentWordCount = wordCount( rawExcerpt, wordCountType );

// Use speak() to announce the word count status when the current word count is updated.
useEffect( () => {
if ( !! speakWordCountMessage ) {
debouncedSpeak( speakWordCountMessage );
}
}, [ currentWordCount ] );

/**
* When excerpt is editable, strip the html tags from
* rendered excerpt. This will be used if the entity's
Expand Down Expand Up @@ -104,15 +120,6 @@ export default function PostExcerptEditor( {
'is-inline': ! showMoreOnNewLine,
} );

/**
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
const wordCountType = _x( 'words', 'Word count type. Do not translate!' );

const currentWordCount = wordCount( rawExcerpt, wordCountType );

/**
* The excerpt length setting needs to be applied to both
* the raw and the rendered excerpt depending on which is being used.
Expand Down Expand Up @@ -157,7 +164,8 @@ export default function PostExcerptEditor( {
* Show a warning if the word count is same as,
* 5 words lower, or larger than the excerpt length value.
*/
let wordCountMessage = null;
let wordCountMessage,
speakWordCountMessage = null;
if (
excerptLength === currentWordCount ||
( excerptLength > currentWordCount &&
Expand All @@ -169,11 +177,26 @@ export default function PostExcerptEditor( {
currentWordCount,
excerptLength
);
speakWordCountMessage = sprintf(
/* translators: 1: Number of words entered, 2: Number of words allowed. */
__( 'The excerpt uses %1$s out of %2$s words' ),
currentWordCount,
excerptLength
);
} else if ( currentWordCount > excerptLength ) {
// If the word count exceeds the excerpt length, show a warning with a negative value.
// Convert the value to a string so that it can be used in speak().
wordCountMessage = String( excerptLength - currentWordCount );
speakWordCountMessage = sprintf(
/* translators: %s: Number of words that exceed the excerpt length limit */
_n(
'The excerpt is %s word longer than allowed.',
'The excerpt is %s words longer than allowed.',
currentWordCount - excerptLength
),
String( currentWordCount - excerptLength )
);
}

const excerptContent = isEditable ? (
<RichText
className={ excerptClassName }
Expand Down Expand Up @@ -228,9 +251,6 @@ export default function PostExcerptEditor( {
{ isSelected && wordCountMessage && (
<Warning>{ wordCountMessage }</Warning>
) }
{ isSelected &&
wordCountMessage &&
debouncedSpeak( wordCountMessage ) }
{ ! showMoreOnNewLine && ' ' }
{ showMoreOnNewLine ? (
<p className="wp-block-post-excerpt__more-text">
Expand Down

1 comment on commit daa5b05

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3712517229
📝 Reported issues:

Please sign in to comment.