-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@wordpress/editor
: Add estimated time to read to table of contents in editor
#41611
Conversation
return ( | ||
<span className="time-to-read"> | ||
<span className="table-of-contents__number"> | ||
{ minutesToRead }{ ' ' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to leave a space between the number and the text, is CSS more appropriate for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe using sprintf
from @wordpress/i18n
would be the way to go. It would also cover those languages that would put the word minutes after the number.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also createInterpolateElement
from @wordpress/element
that can cover using React components:
Example usage:
gutenberg/packages/block-editor/src/components/link-control/search-create-button.js
Lines 32 to 39 in 862ec61
text = createInterpolateElement( | |
sprintf( | |
/* translators: %s: search term. */ | |
__( 'Create: <mark>%s</mark>' ), | |
searchTerm | |
), | |
{ mark: <mark /> } | |
); |
@opr I'd like to accessibility review this PR but have no idea where the table of contents is. What is the aria-label on the button? Thanks. |
Hi @alexstine thank you - sure the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@opr Accessibility is good and I think the spacing reflects how other items are listed. I only checked this for keyboard navigation and general usability/code quality, I am unable to do visual testing. No idea how it looks. A sighted review would probably be good for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent addition. I left some minor feedback, but overall this looks great 💯
return ( | ||
<span className="time-to-read"> | ||
<span className="table-of-contents__number"> | ||
{ minutesToRead }{ ' ' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe using sprintf
from @wordpress/i18n
would be the way to go. It would also cover those languages that would put the word minutes after the number.
* Do not translate into your own language. | ||
*/ | ||
const wordCountType = _x( 'words', 'Word count type. Do not translate!' ); | ||
const minutesToRead = Math.round( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a special case for minutes to read less than 1 (or 0.5)? In that case, we could display: Less than a minute
. Otherwise, we might see 0 minutes
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gziolo I went with < 1 minute
because without there being a number there it looks kind of weird.
Compared to:
@gziolo awesome thanks for the reminder about |
By the way, @richtabor created a WordPress plugin with a Post Reading Time block that is available at https://github.com/richtabor/post-reading-time-block. It could be a good addition to the core as well. |
Your suggestion looks good to me. |
@gziolo and @jameskoster thanks for your input. I've made the changes you suggested and re-ordered the panel. As for the time to read block, I think it would be better to bring that in in a separate PR, do you agree? |
Surely, it's outside of the scope of this PR. We want to keep it small. It's a good opportunity to discuss further work though before we jump into opening issues.
Looks good to me! I re-trigger one of the CI job to see if the test (unrelated to this PR) passes. |
What?
Adds an estimated time to read to the table of contents in the post editor.
Why?
It would be a useful tool for editors to know how long their article will take (the average) reader to read.
Closes #38593.
How?
I took an average of the reading rate (words per minute, but characters per minute for Chinese) from this source: https://irisreading.com/average-reading-speed-in-various-languages/. A lot of other sources cite 200 wpm is the average reading rate but I think they didn't account for other languages. Using the average of 189 should get us a pretty good estimate.
Using this, I created a
TimeToRead
function, which usescount
from@wordpress/wordcount
to calculate how many minutes to display.The number is styled larger than the text. Please guide me on styling if we should do something different.
Testing Instructions
i
above the editor).Screenshots or screencast
Additional thoughts
If #41598 gets merged then we could possibly use
humanTimeDiff
to get the locale specific time between now and the estimated minutes to read. This displays time differences including froma few seconds
,x minutes
,x hours
,x days
,x months
andx years
.The challenge here would be identifying how to split the resulting string into its locale-correct constituent parts (number of units of time, and string describing the unit of time) and styling the number and string differently. We also need to consider the case of the text only option, where the words are such that the post will take a single unit of time to read (i.e.
a minute
) and how we would stylea minute
where no number is present vs2 minutes
where the number being larger than the text looks alright.