UNPKG

@wordpress/editor

Version:
81 lines (76 loc) 3.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = PostContentInformation; var _components = require("@wordpress/components"); var _data = require("@wordpress/data"); var _i18n = require("@wordpress/i18n"); var _wordcount = require("@wordpress/wordcount"); var _element = require("@wordpress/element"); var _coreData = require("@wordpress/core-data"); var _store = require("../../store"); var _constants = require("../../store/constants"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ // Taken from packages/editor/src/components/time-to-read/index.js. const AVERAGE_READING_RATE = 189; // This component renders the wordcount and reading time for the post. function PostContentInformation() { const { postContent } = (0, _data.useSelect)(select => { const { getEditedPostAttribute, getCurrentPostType, getCurrentPostId } = select(_store.store); const { canUser } = select(_coreData.store); const { getEntityRecord } = select(_coreData.store); const siteSettings = canUser('read', { kind: 'root', name: 'site' }) ? getEntityRecord('root', 'site') : undefined; const postType = getCurrentPostType(); const _id = getCurrentPostId(); const isPostsPage = +_id === siteSettings?.page_for_posts; const showPostContentInfo = !isPostsPage && ![_constants.TEMPLATE_POST_TYPE, _constants.TEMPLATE_PART_POST_TYPE].includes(postType); return { postContent: showPostContentInfo && 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 = (0, _i18n._x)('words', 'Word count type. Do not translate!'); const wordsCounted = (0, _element.useMemo)(() => postContent ? (0, _wordcount.count)(postContent, wordCountType) : 0, [postContent, wordCountType]); if (!wordsCounted) { return null; } const readingTime = Math.round(wordsCounted / AVERAGE_READING_RATE); const wordsCountText = (0, _i18n.sprintf)( // translators: %s: the number of words in the post. (0, _i18n._n)('%s word', '%s words', wordsCounted), wordsCounted.toLocaleString()); const minutesText = readingTime <= 1 ? (0, _i18n.__)('1 minute') : (0, _i18n.sprintf)( // translators: %s: the number of minutes to read the post. (0, _i18n._n)('%s minute', '%s minutes', readingTime), readingTime.toLocaleString()); return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: "editor-post-content-information", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, { children: (0, _i18n.sprintf)( /* translators: 1: How many words a post has. 2: the number of minutes to read the post (e.g. 130 words, 2 minutes read time.) */ (0, _i18n.__)('%1$s, %2$s read time.'), wordsCountText, minutesText) }) }); } //# sourceMappingURL=index.js.map