-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@wordpress/editor
: Add estimated time to read to table of contents …
…in editor (#41611) * Add TimeToRead component * Add TimeToRead to the table of contents panel * Adjust average reading rate * Use createInterpolateElement to show the minutes to read * Re-order information in toc panel
- Loading branch information
Showing
2 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { _x, _n, __, sprintf } from '@wordpress/i18n'; | ||
import { count as wordCount } from '@wordpress/wordcount'; | ||
import { createInterpolateElement } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
|
||
/** | ||
* Average reading rate - based on average taken from | ||
* https://irisreading.com/average-reading-speed-in-various-languages/ | ||
* (Characters/minute used for Chinese rather than words). | ||
* | ||
* @type {number} A rough estimate of the average reading rate across multiple languages. | ||
*/ | ||
const AVERAGE_READING_RATE = 189; | ||
|
||
export default function TimeToRead() { | ||
const content = useSelect( | ||
( select ) => select( editorStore ).getEditedPostAttribute( 'content' ), | ||
[] | ||
); | ||
|
||
/* | ||
* 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 minutesToRead = Math.round( | ||
wordCount( content, wordCountType ) / AVERAGE_READING_RATE | ||
); | ||
const minutesToReadString = | ||
minutesToRead === 0 | ||
? createInterpolateElement( __( '<span>< 1</span> minute' ), { | ||
span: <span className="table-of-contents__number" />, | ||
} ) | ||
: createInterpolateElement( | ||
sprintf( | ||
/* translators: %s is the number of minutes the post will take to read. */ | ||
_n( | ||
'<span>%d</span> minute', | ||
'<span>%d</span> minutes', | ||
minutesToRead | ||
), | ||
minutesToRead | ||
), | ||
{ | ||
span: <span className="table-of-contents__number" />, | ||
} | ||
); | ||
|
||
return <span className="time-to-read">{ minutesToReadString }</span>; | ||
} |