From 381842486039a1e6aa3aebb56690e448991f101c Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Wed, 8 Jun 2022 23:56:13 +0100 Subject: [PATCH 1/5] Add TimeToRead component --- .../src/components/time-to-read/index.js | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 packages/editor/src/components/time-to-read/index.js diff --git a/packages/editor/src/components/time-to-read/index.js b/packages/editor/src/components/time-to-read/index.js new file mode 100644 index 00000000000000..e1a7491daef79a --- /dev/null +++ b/packages/editor/src/components/time-to-read/index.js @@ -0,0 +1,39 @@ +/** + * WordPress dependencies + */ +import { useSelect } from '@wordpress/data'; +import { _x, _n } from '@wordpress/i18n'; +import { count as wordCount } from '@wordpress/wordcount'; + +/** + * Internal dependencies + */ +import { store as editorStore } from '../../store'; + +const AVERAGE_READING_RATE = 200; + +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 + ); + + return ( + + + { minutesToRead }{ ' ' } + + { _n( 'minute', 'minutes', minutesToRead ) } + + ); +} From 8def320dd43c8e02cc41d9247a327c9f829606f1 Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Wed, 8 Jun 2022 23:56:58 +0100 Subject: [PATCH 2/5] Add TimeToRead to the table of contents panel --- packages/editor/src/components/table-of-contents/panel.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/editor/src/components/table-of-contents/panel.js b/packages/editor/src/components/table-of-contents/panel.js index 48ca98c2fd04a6..08f6b23186995f 100644 --- a/packages/editor/src/components/table-of-contents/panel.js +++ b/packages/editor/src/components/table-of-contents/panel.js @@ -9,6 +9,7 @@ import { store as blockEditorStore } from '@wordpress/block-editor'; * Internal dependencies */ import WordCount from '../word-count'; +import TimeToRead from '../time-to-read'; import DocumentOutline from '../document-outline'; import CharacterCount from '../character-count'; @@ -66,6 +67,10 @@ function TableOfContentsPanel( { hasOutlineItemsDisabled, onRequestClose } ) { { numberOfBlocks } +
  • + { __( 'Time to read' ) } + +
  • { headingCount > 0 && ( From 8c2372b19677651c4572b480306dc66ae6230b68 Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Thu, 9 Jun 2022 00:00:48 +0100 Subject: [PATCH 3/5] Adjust average reading rate --- packages/editor/src/components/time-to-read/index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/time-to-read/index.js b/packages/editor/src/components/time-to-read/index.js index e1a7491daef79a..f2fe16c3c4cf1f 100644 --- a/packages/editor/src/components/time-to-read/index.js +++ b/packages/editor/src/components/time-to-read/index.js @@ -10,7 +10,14 @@ import { count as wordCount } from '@wordpress/wordcount'; */ import { store as editorStore } from '../../store'; -const AVERAGE_READING_RATE = 200; +/** + * 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( From 17dae9365ee4ffe88d01d8a53c57b101878428ac Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Fri, 17 Jun 2022 15:16:26 +0100 Subject: [PATCH 4/5] Use createInterpolateElement to show the minutes to read --- .../src/components/time-to-read/index.js | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/editor/src/components/time-to-read/index.js b/packages/editor/src/components/time-to-read/index.js index f2fe16c3c4cf1f..0ff85b2e717d0d 100644 --- a/packages/editor/src/components/time-to-read/index.js +++ b/packages/editor/src/components/time-to-read/index.js @@ -2,8 +2,9 @@ * WordPress dependencies */ import { useSelect } from '@wordpress/data'; -import { _x, _n } from '@wordpress/i18n'; +import { _x, _n, __, sprintf } from '@wordpress/i18n'; import { count as wordCount } from '@wordpress/wordcount'; +import { createInterpolateElement } from '@wordpress/element'; /** * Internal dependencies @@ -34,13 +35,25 @@ export default function TimeToRead() { const minutesToRead = Math.round( wordCount( content, wordCountType ) / AVERAGE_READING_RATE ); + const minutesToReadString = + minutesToRead === 0 + ? createInterpolateElement( __( '< 1 minute' ), { + span: , + } ) + : createInterpolateElement( + sprintf( + /* translators: %s is the number of minutes the post will take to read. */ + _n( + '%d minute', + '%d minutes', + minutesToRead + ), + minutesToRead + ), + { + span: , + } + ); - return ( - - - { minutesToRead }{ ' ' } - - { _n( 'minute', 'minutes', minutesToRead ) } - - ); + return { minutesToReadString }; } From d171a8435285c2c26b85af0483efa10489da5712 Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Sat, 2 Jul 2022 11:02:37 -0700 Subject: [PATCH 5/5] Re-order information in toc panel --- .../editor/src/components/table-of-contents/panel.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/editor/src/components/table-of-contents/panel.js b/packages/editor/src/components/table-of-contents/panel.js index 08f6b23186995f..7e1b9ed5c75124 100644 --- a/packages/editor/src/components/table-of-contents/panel.js +++ b/packages/editor/src/components/table-of-contents/panel.js @@ -39,6 +39,10 @@ function TableOfContentsPanel( { hasOutlineItemsDisabled, onRequestClose } ) { tabIndex="0" >
      +
    • + { __( 'Words' ) } + +
    • { __( 'Characters' ) } @@ -46,8 +50,8 @@ function TableOfContentsPanel( { hasOutlineItemsDisabled, onRequestClose } ) {
    • - { __( 'Words' ) } - + { __( 'Time to read' ) } +
    • { __( 'Headings' ) } @@ -67,10 +71,6 @@ function TableOfContentsPanel( { hasOutlineItemsDisabled, onRequestClose } ) { { numberOfBlocks }
    • -
    • - { __( 'Time to read' ) } - -
    { headingCount > 0 && (