UNPKG

@wordpress/block-library

Version:
156 lines (155 loc) 4.77 kB
import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import clsx from "clsx"; import { __, _x, _n, sprintf } from "@wordpress/i18n"; import { useMemo } from "@wordpress/element"; import { AlignmentControl, BlockControls, InspectorControls, useBlockProps } from "@wordpress/block-editor"; import { ToggleControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem } from "@wordpress/components"; import { __unstableSerializeAndClean } from "@wordpress/blocks"; import { useEntityProp, useEntityBlockEditor } from "@wordpress/core-data"; import { count as wordCount } from "@wordpress/wordcount"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks"; function PostTimeToReadEdit({ attributes, setAttributes, context }) { const { textAlign, displayAsRange, displayMode, averageReadingSpeed } = attributes; const { postId, postType } = context; const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const [contentStructure] = useEntityProp( "postType", postType, "content", postId ); const [blocks] = useEntityBlockEditor("postType", postType, { id: postId }); const displayString = useMemo(() => { let content; if (contentStructure instanceof Function) { content = contentStructure({ blocks }); } else if (blocks) { content = __unstableSerializeAndClean(blocks); } else { content = contentStructure; } const wordCountType = _x( "words", "Word count type. Do not translate!" ); const totalWords = wordCount(content || "", wordCountType); if (displayMode === "time") { if (displayAsRange) { let maxMinutes = Math.max( 1, Math.round(totalWords / averageReadingSpeed * 1.2) ); const minMinutes = Math.max( 1, Math.round(totalWords / averageReadingSpeed * 0.8) ); if (minMinutes === maxMinutes) { maxMinutes = maxMinutes + 1; } const rangeLabel = _x( "%1$s\u2013%2$s minutes", "Range of minutes to read" ); return sprintf(rangeLabel, minMinutes, maxMinutes); } const minutesToRead = Math.max( 1, Math.round(totalWords / averageReadingSpeed) ); return sprintf( /* translators: %s: the number of minutes to read the post. */ _n("%s minute", "%s minutes", minutesToRead), minutesToRead ); } if (displayMode === "words") { return wordCountType === "words" ? sprintf( /* translators: %s: the number of words in the post. */ _n("%s word", "%s words", totalWords), totalWords.toLocaleString() ) : sprintf( /* translators: %s: the number of characters in the post. */ _n("%s character", "%s characters", totalWords), totalWords.toLocaleString() ); } }, [ contentStructure, blocks, displayAsRange, displayMode, averageReadingSpeed ]); 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 }); } } ) }), displayMode === "time" && /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx( ToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ displayAsRange: true }); }, dropdownMenuProps, children: /* @__PURE__ */ jsx( ToolsPanelItem, { isShownByDefault: true, label: _x( "Display as range", "Turns reading time range display on or off" ), hasValue: () => !displayAsRange, onDeselect: () => { setAttributes({ displayAsRange: true }); }, children: /* @__PURE__ */ jsx( ToggleControl, { __nextHasNoMarginBottom: true, label: __("Display as range"), checked: !!displayAsRange, onChange: () => setAttributes({ displayAsRange: !displayAsRange }) } ) } ) } ) }), /* @__PURE__ */ jsx("div", { ...blockProps, children: displayString }) ] }); } var edit_default = PostTimeToReadEdit; export { edit_default as default }; //# sourceMappingURL=edit.js.map