UNPKG

@wordpress/block-library

Version:
70 lines (69 loc) • 3.34 kB
import { __, _n, _x, sprintf } from "@wordpress/i18n"; import { useMemo } from "@wordpress/element"; import { InspectorControls, useBlockProps } from "@wordpress/block-editor"; import { ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { __unstableSerializeAndClean } from "@wordpress/blocks"; import { useEntityBlockEditor, useEntityProp } from "@wordpress/core-data"; import { count } from "@wordpress/wordcount"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/post-time-to-read/edit.jsx function PostTimeToReadEdit({ attributes, setAttributes, context }) { const { 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 = count(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 * .8)); if (minMinutes === maxMinutes) maxMinutes = maxMinutes + 1; const rangeLabel = _x("%1$s–%2$s minutes", "Range of minutes to read"); return sprintf(rangeLabel, minMinutes, maxMinutes); } const minutesToRead = Math.max(1, Math.round(totalWords / averageReadingSpeed)); return sprintf(_n("%s minute", "%s minutes", minutesToRead), minutesToRead); } if (displayMode === "words") return wordCountType === "words" ? sprintf(_n("%s word", "%s words", totalWords), totalWords.toLocaleString()) : sprintf(_n("%s character", "%s characters", totalWords), totalWords.toLocaleString()); }, [ contentStructure, blocks, displayAsRange, displayMode, averageReadingSpeed ]); const blockProps = useBlockProps(); return /* @__PURE__ */ jsxs(Fragment, { children: [displayMode === "time" && /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ displayAsRange: true }); }, dropdownMenuProps, children: /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { 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, { label: __("Display as range"), checked: !!displayAsRange, onChange: () => setAttributes({ displayAsRange: !displayAsRange }) }) }) }) }), /* @__PURE__ */ jsx("div", { ...blockProps, children: displayString })] }); } export { PostTimeToReadEdit as default }; //# sourceMappingURL=edit.mjs.map