UNPKG

@wordpress/editor

Version:
213 lines (211 loc) 7.93 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/editor/src/components/post-revisions-preview/revisions-slider.js var revisions_slider_exports = {}; __export(revisions_slider_exports, { RevisionsSlider: () => RevisionsSlider, default: () => ConnectedRevisionsSlider }); module.exports = __toCommonJS(revisions_slider_exports); var import_data = require("@wordpress/data"); var import_components = require("@wordpress/components"); var import_core_data = require("@wordpress/core-data"); var import_i18n = require("@wordpress/i18n"); var import_date = require("@wordpress/date"); var import_element = require("@wordpress/element"); var import_icons = require("@wordpress/icons"); var import_ui = require("@wordpress/ui"); var import_compose = require("@wordpress/compose"); var import_store = require("../../store/index.cjs"); var import_lock_unlock = require("../../lock-unlock.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); function ConnectedRevisionsSlider() { const revisionData = (0, import_data.useSelect)((select) => { const { getCurrentRevisionId, getRevisionPage, getPageRevisions, getRevisionsPerPage } = (0, import_lock_unlock.unlock)(select(import_store.store)); const postType = select(import_store.store).getCurrentPostType(); if (!postType) { return {}; } const entityConfig = select(import_core_data.store).getEntityConfig( "postType", postType ); const revisionKey = entityConfig?.revisionKey || "id"; const revisionPage = getRevisionPage(); return { revisions: getPageRevisions(revisionPage), perPage: getRevisionsPerPage(), currentRevisionId: getCurrentRevisionId(), revisionKey, revisionPage, totalRevisions: select(import_store.store).getCurrentPostRevisionsCount() }; }, []); const revisionActions = (0, import_lock_unlock.unlock)((0, import_data.useDispatch)(import_store.store)); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( RevisionsSlider, { ...revisionData, setCurrentRevisionId: revisionActions.setCurrentRevisionId, setRevisionPage: revisionActions.setRevisionPage } ); } function RevisionsSlider({ revisions: rawRevisions, perPage, currentRevisionId, revisionKey, revisionPage, totalRevisions, setCurrentRevisionId, setRevisionPage }) { const setFocusOnMountRef = (0, import_compose.useFocusOnMount)(true); const initialActiveElementRef = (0, import_element.useRef)(); const loadingRef = (0, import_element.useCallback)((node) => { if (node && initialActiveElementRef.current === void 0) { initialActiveElementRef.current = node.ownerDocument.activeElement; } }, []); const focusOnMountRef = (0, import_element.useCallback)( (node) => { if (!node) { setFocusOnMountRef(null); return; } const { activeElement, body, documentElement } = node.ownerDocument; const initialActiveElement = initialActiveElementRef.current ?? activeElement; const focusHasNotMoved = activeElement === initialActiveElement || activeElement === body || activeElement === documentElement; if (focusHasNotMoved) { setFocusOnMountRef(node); } }, [setFocusOnMountRef] ); const isLoading = !rawRevisions; const totalPages = Math.ceil(totalRevisions / perPage) || 1; const revisions = (0, import_element.useMemo)( () => rawRevisions && [...rawRevisions].reverse(), [rawRevisions] ); const selectedIndex = revisions?.findIndex( (r) => r[revisionKey] === currentRevisionId ); const handleSliderChange = (index) => { const revision = revisions?.[index]; if (revision) { setCurrentRevisionId(revision[revisionKey]); } }; const dateSettings = (0, import_date.getSettings)(); const renderTooltipContent = (index) => { const revision = revisions?.[index]; if (!revision) { return index; } return (0, import_date.dateI18n)(dateSettings.formats.datetime, revision.date); }; const showPagination = totalPages > 1; if (isLoading && !showPagination) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, { ref: loadingRef }); } if (!isLoading && !revisions?.length) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "editor-revisions-header__no-revisions", children: (0, import_i18n.__)("No revisions found.") }); } if (totalRevisions <= 1) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "editor-revisions-header__no-revisions", children: (0, import_i18n.__)("Only one revision found.") }); } const getPageRangeLabel = (page) => { const end = totalRevisions - (page - 1) * perPage; const start = Math.max(1, end - perPage + 1); return (0, import_i18n.sprintf)( /* translators: 1: first revision number, 2: last revision number */ (0, import_i18n.__)("Revisions %1$s–%2$s"), start, end ); }; const sliderOrSpinner = isLoading || selectedIndex === -1 ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, { ref: loadingRef }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.RangeControl, { ref: focusOnMountRef, "aria-valuetext": renderTooltipContent(selectedIndex), className: "editor-revisions-header__slider", hideLabelFromVision: true, label: (0, import_i18n.__)("Revision"), max: revisions?.length - 1, min: 0, marks: true, onChange: handleSliderChange, renderTooltipContent, value: selectedIndex, withInputField: false } ); if (!showPagination) { return sliderOrSpinner; } return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.Stack, { direction: "row", gap: "sm", align: "center", style: { flex: 1 }, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Button, { icon: import_icons.chevronLeft, label: revisionPage < totalPages ? getPageRangeLabel(revisionPage + 1) : (0, import_i18n.__)("No older revisions"), onClick: () => setRevisionPage(revisionPage + 1), disabled: isLoading || revisionPage >= totalPages, size: "compact", accessibleWhenDisabled: true } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "div", { style: { flex: 1, minWidth: 0, display: "flex", justifyContent: "center" }, children: sliderOrSpinner } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Button, { icon: import_icons.chevronRight, label: revisionPage > 1 ? getPageRangeLabel(revisionPage - 1) : (0, import_i18n.__)("No newer revisions"), onClick: () => setRevisionPage(revisionPage - 1), disabled: isLoading || revisionPage <= 1, size: "compact", accessibleWhenDisabled: true } ) ] }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RevisionsSlider }); //# sourceMappingURL=revisions-slider.cjs.map