UNPKG

@wordpress/block-library

Version:
88 lines (85 loc) 2.95 kB
/** * External dependencies */ import clsx from 'clsx'; /** * WordPress dependencies */ import { _x, _n, sprintf } from '@wordpress/i18n'; import { useMemo } from '@wordpress/element'; import { AlignmentControl, BlockControls, useBlockProps } from '@wordpress/block-editor'; import { __unstableSerializeAndClean } from '@wordpress/blocks'; import { useEntityProp, useEntityBlockEditor } from '@wordpress/core-data'; import { count as wordCount } from '@wordpress/wordcount'; /** * 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). */ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; const AVERAGE_READING_RATE = 189; function PostTimeToReadEdit({ attributes, setAttributes, context }) { const { textAlign } = attributes; const { postId, postType } = context; const [contentStructure] = useEntityProp('postType', postType, 'content', postId); const [blocks] = useEntityBlockEditor('postType', postType, { id: postId }); const minutesToReadString = useMemo(() => { // Replicates the logic found in getEditedPostContent(). let content; if (contentStructure instanceof Function) { content = contentStructure({ blocks }); } else if (blocks) { // If we have parsed blocks already, they should be our source of truth. // Parsing applies block deprecations and legacy block conversions that // unparsed content will not have. content = __unstableSerializeAndClean(blocks); } else { content = contentStructure; } /* * 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.max(1, Math.round(wordCount(content || '', wordCountType) / AVERAGE_READING_RATE)); return sprintf(/* translators: %s: the number of minutes to read the post. */ _n('%s minute', '%s minutes', minutesToRead), minutesToRead); }, [contentStructure, blocks]); const blockProps = useBlockProps({ className: clsx({ [`has-text-align-${textAlign}`]: textAlign }) }); return /*#__PURE__*/_jsxs(_Fragment, { children: [/*#__PURE__*/_jsx(BlockControls, { group: "block", children: /*#__PURE__*/_jsx(AlignmentControl, { value: textAlign, onChange: nextAlign => { setAttributes({ textAlign: nextAlign }); } }) }), /*#__PURE__*/_jsx("div", { ...blockProps, children: minutesToReadString })] }); } export default PostTimeToReadEdit; //# sourceMappingURL=edit.js.map