1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Add approximate reading time to note statistics (#3373)

* First stab at a reading time calculator

* Use ceil instead of floor for rounding

* Fix variable names to camelCase

* Up wpm to 250, add source
This commit is contained in:
cadolphs 2020-07-06 16:39:36 -07:00 committed by GitHub
parent 77a03599de
commit 4ade1bf65f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,6 +29,14 @@ function countElements(text:string, wordSetter:Function, characterSetter:Functio
text === '' ? lineSetter(0) : lineSetter(text.split('\n').length);
}
function formatReadTime(readTimeMinutes: number) {
if (readTimeMinutes < 1) {
return '< 1';
}
return Math.ceil(readTimeMinutes).toString();
}
export default function NoteContentPropertiesDialog(props:NoteContentPropertiesDialogProps) {
const theme = themeStyle(props.theme);
const tableBodyComps: JSX.Element[] = [];
@ -42,6 +50,10 @@ export default function NoteContentPropertiesDialog(props:NoteContentPropertiesD
const [strippedWords, setStrippedWords] = useState<number>(0);
const [strippedCharacters, setStrippedCharacters] = useState<number>(0);
const [strippedCharactersNoSpace, setStrippedCharactersNoSpace] = useState<number>(0);
const [strippedReadTime, setStrippedReadTime] = useState<number>(0);
// This amount based on the following paper:
// https://www.researchgate.net/publication/332380784_How_many_words_do_we_read_per_minute_A_review_and_meta-analysis_of_reading_rate
const wordsPerMinute = 250;
useEffect(() => {
countElements(props.text, setWords, setCharacters, setCharactersNoSpace, setLines);
@ -52,6 +64,11 @@ export default function NoteContentPropertiesDialog(props:NoteContentPropertiesD
countElements(strippedText, setStrippedWords, setStrippedCharacters, setStrippedCharactersNoSpace, setStrippedLines);
}, [props.text]);
useEffect(() => {
const readTimeMinutes: number = strippedWords / wordsPerMinute;
setStrippedReadTime(readTimeMinutes);
}, [strippedWords]);
const textProperties: TextPropertiesMap = {
lines: lines,
words: words,
@ -133,6 +150,9 @@ export default function NoteContentPropertiesDialog(props:NoteContentPropertiesD
{tableBodyComps}
</tbody>
</table>
<div style={labelCompStyle}>
{_('Read time: %s min', formatReadTime(strippedReadTime))}
</div>
<DialogButtonRow theme={props.theme} onClick={buttonRow_click} okButtonShow={false} cancelButtonLabel={_('Close')}/>
</div>
</div>