@wordpress/block-library
Version:
Block library for the WordPress editor.
75 lines (74 loc) • 4.05 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_element = require("@wordpress/element");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_components = require("@wordpress/components");
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_wordcount = require("@wordpress/wordcount");
let ___utils_hooks_js = require("../utils/hooks.cjs");
let react_jsx_runtime = require("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 = (0, ___utils_hooks_js.useToolsPanelDropdownMenuProps)();
const [contentStructure] = (0, _wordpress_core_data.useEntityProp)("postType", postType, "content", postId);
const [blocks] = (0, _wordpress_core_data.useEntityBlockEditor)("postType", postType, { id: postId });
const displayString = (0, _wordpress_element.useMemo)(() => {
let content;
if (contentStructure instanceof Function) content = contentStructure({ blocks });
else if (blocks) content = (0, _wordpress_blocks.__unstableSerializeAndClean)(blocks);
else content = contentStructure;
const wordCountType = (0, _wordpress_i18n._x)("words", "Word count type. Do not translate!");
const totalWords = (0, _wordpress_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 * .8));
if (minMinutes === maxMinutes) maxMinutes = maxMinutes + 1;
const rangeLabel = (0, _wordpress_i18n._x)("%1$s–%2$s minutes", "Range of minutes to read");
return (0, _wordpress_i18n.sprintf)(rangeLabel, minMinutes, maxMinutes);
}
const minutesToRead = Math.max(1, Math.round(totalWords / averageReadingSpeed));
return (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n._n)("%s minute", "%s minutes", minutesToRead), minutesToRead);
}
if (displayMode === "words") return wordCountType === "words" ? (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n._n)("%s word", "%s words", totalWords), totalWords.toLocaleString()) : (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n._n)("%s character", "%s characters", totalWords), totalWords.toLocaleString());
}, [
contentStructure,
blocks,
displayAsRange,
displayMode,
averageReadingSpeed
]);
const blockProps = (0, _wordpress_block_editor.useBlockProps)();
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [displayMode === "time" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanel, {
label: (0, _wordpress_i18n.__)("Settings"),
resetAll: () => {
setAttributes({ displayAsRange: true });
},
dropdownMenuProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
isShownByDefault: true,
label: (0, _wordpress_i18n._x)("Display as range", "Turns reading time range display on or off"),
hasValue: () => !displayAsRange,
onDeselect: () => {
setAttributes({ displayAsRange: true });
},
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, {
label: (0, _wordpress_i18n.__)("Display as range"),
checked: !!displayAsRange,
onChange: () => setAttributes({ displayAsRange: !displayAsRange })
})
})
}) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: displayString
})] });
}
exports.default = PostTimeToReadEdit;
//# sourceMappingURL=edit.cjs.map