UNPKG

@wordpress/block-library

Version:
179 lines (177 loc) 6.84 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-library/src/post-time-to-read/edit.js var edit_exports = {}; __export(edit_exports, { default: () => edit_default }); module.exports = __toCommonJS(edit_exports); var import_clsx = __toESM(require("clsx")); var import_i18n = require("@wordpress/i18n"); var import_element = require("@wordpress/element"); var import_block_editor = require("@wordpress/block-editor"); var import_components = require("@wordpress/components"); var import_blocks = require("@wordpress/blocks"); var import_core_data = require("@wordpress/core-data"); var import_wordcount = require("@wordpress/wordcount"); var import_hooks = require("../utils/hooks"); var import_jsx_runtime = require("react/jsx-runtime"); function PostTimeToReadEdit({ attributes, setAttributes, context }) { const { textAlign, displayAsRange, displayMode, averageReadingSpeed } = attributes; const { postId, postType } = context; const dropdownMenuProps = (0, import_hooks.useToolsPanelDropdownMenuProps)(); const [contentStructure] = (0, import_core_data.useEntityProp)( "postType", postType, "content", postId ); const [blocks] = (0, import_core_data.useEntityBlockEditor)("postType", postType, { id: postId }); const displayString = (0, import_element.useMemo)(() => { let content; if (contentStructure instanceof Function) { content = contentStructure({ blocks }); } else if (blocks) { content = (0, import_blocks.__unstableSerializeAndClean)(blocks); } else { content = contentStructure; } const wordCountType = (0, import_i18n._x)( "words", "Word count type. Do not translate!" ); const totalWords = (0, import_wordcount.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 * 0.8) ); if (minMinutes === maxMinutes) { maxMinutes = maxMinutes + 1; } const rangeLabel = (0, import_i18n._x)( "%1$s\u2013%2$s minutes", "Range of minutes to read" ); return (0, import_i18n.sprintf)(rangeLabel, minMinutes, maxMinutes); } const minutesToRead = Math.max( 1, Math.round(totalWords / averageReadingSpeed) ); return (0, import_i18n.sprintf)( /* translators: %s: the number of minutes to read the post. */ (0, import_i18n._n)("%s minute", "%s minutes", minutesToRead), minutesToRead ); } if (displayMode === "words") { return wordCountType === "words" ? (0, import_i18n.sprintf)( /* translators: %s: the number of words in the post. */ (0, import_i18n._n)("%s word", "%s words", totalWords), totalWords.toLocaleString() ) : (0, import_i18n.sprintf)( /* translators: %s: the number of characters in the post. */ (0, import_i18n._n)("%s character", "%s characters", totalWords), totalWords.toLocaleString() ); } }, [ contentStructure, blocks, displayAsRange, displayMode, averageReadingSpeed ]); const blockProps = (0, import_block_editor.useBlockProps)({ className: (0, import_clsx.default)({ [`has-text-align-${textAlign}`]: textAlign }) }); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.BlockControls, { group: "block", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_block_editor.AlignmentControl, { value: textAlign, onChange: (nextAlign) => { setAttributes({ textAlign: nextAlign }); } } ) }), displayMode === "time" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanel, { label: (0, import_i18n.__)("Settings"), resetAll: () => { setAttributes({ displayAsRange: true }); }, dropdownMenuProps, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanelItem, { isShownByDefault: true, label: (0, import_i18n._x)( "Display as range", "Turns reading time range display on or off" ), hasValue: () => !displayAsRange, onDeselect: () => { setAttributes({ displayAsRange: true }); }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.ToggleControl, { __nextHasNoMarginBottom: true, label: (0, import_i18n.__)("Display as range"), checked: !!displayAsRange, onChange: () => setAttributes({ displayAsRange: !displayAsRange }) } ) } ) } ) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...blockProps, children: displayString }) ] }); } var edit_default = PostTimeToReadEdit; //# sourceMappingURL=edit.js.map