UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 2.82 kB
{ "version": 3, "sources": ["../../../src/components/time-to-read/index.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { _x, _n, __, sprintf } from '@wordpress/i18n';\nimport { count as wordCount } from '@wordpress/wordcount';\nimport { createInterpolateElement } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { store as editorStore } from '../../store';\n\n/**\n * Average reading rate - based on average taken from\n * https://irisreading.com/average-reading-speed-in-various-languages/\n * (Characters/minute used for Chinese rather than words).\n *\n * @type {number} A rough estimate of the average reading rate across multiple languages.\n */\nconst AVERAGE_READING_RATE = 189;\n\n/**\n * Component for showing Time To Read in Content.\n *\n * @return {React.ReactNode} The rendered TimeToRead component.\n */\nexport default function TimeToRead() {\n\tconst content = useSelect(\n\t\t( select ) => select( editorStore ).getEditedPostAttribute( 'content' ),\n\t\t[]\n\t);\n\n\t/*\n\t * translators: If your word count is based on single characters (e.g. East Asian characters),\n\t * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.\n\t * Do not translate into your own language.\n\t */\n\tconst wordCountType = _x( 'words', 'Word count type. Do not translate!' );\n\tconst minutesToRead = Math.round(\n\t\twordCount( content, wordCountType ) / AVERAGE_READING_RATE\n\t);\n\tconst minutesToReadString =\n\t\tminutesToRead === 0\n\t\t\t? createInterpolateElement( __( '<span>< 1</span> minute' ), {\n\t\t\t\t\tspan: <span />,\n\t\t\t } )\n\t\t\t: createInterpolateElement(\n\t\t\t\t\tsprintf(\n\t\t\t\t\t\t/* translators: %s: the number of minutes to read the post. */\n\t\t\t\t\t\t_n(\n\t\t\t\t\t\t\t'<span>%s</span> minute',\n\t\t\t\t\t\t\t'<span>%s</span> minutes',\n\t\t\t\t\t\t\tminutesToRead\n\t\t\t\t\t\t),\n\t\t\t\t\t\tminutesToRead\n\t\t\t\t\t),\n\t\t\t\t\t{\n\t\t\t\t\t\tspan: <span />,\n\t\t\t\t\t}\n\t\t\t );\n\n\treturn <span className=\"time-to-read\">{ minutesToReadString }</span>;\n}\n"], "mappings": ";AAGA,SAAS,iBAAiB;AAC1B,SAAS,IAAI,IAAI,IAAI,eAAe;AACpC,SAAS,SAAS,iBAAiB;AACnC,SAAS,gCAAgC;AAKzC,SAAS,SAAS,mBAAmB;AAkC1B;AAzBX,IAAM,uBAAuB;AAOd,SAAR,aAA8B;AACpC,QAAM,UAAU;AAAA,IACf,CAAE,WAAY,OAAQ,WAAY,EAAE,uBAAwB,SAAU;AAAA,IACtE,CAAC;AAAA,EACF;AAOA,QAAM,gBAAgB,GAAI,SAAS,oCAAqC;AACxE,QAAM,gBAAgB,KAAK;AAAA,IAC1B,UAAW,SAAS,aAAc,IAAI;AAAA,EACvC;AACA,QAAM,sBACL,kBAAkB,IACf,yBAA0B,GAAI,yBAA0B,GAAG;AAAA,IAC3D,MAAM,oBAAC,UAAK;AAAA,EACZ,CAAE,IACF;AAAA,IACA;AAAA;AAAA,MAEC;AAAA,QACC;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAAA,MACA;AAAA,IACD;AAAA,IACA;AAAA,MACC,MAAM,oBAAC,UAAK;AAAA,IACb;AAAA,EACA;AAEJ,SAAO,oBAAC,UAAK,WAAU,gBAAiB,+BAAqB;AAC9D;", "names": [] }