@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
188 lines (187 loc) • 6.21 kB
JavaScript
// packages/editor/src/components/post-revisions-preview/revisions-slider.js
import { useSelect, useDispatch } from "@wordpress/data";
import { RangeControl, Spinner, Button } from "@wordpress/components";
import { store as coreStore } from "@wordpress/core-data";
import { __, sprintf } from "@wordpress/i18n";
import { dateI18n, getSettings as getDateSettings } from "@wordpress/date";
import { useCallback, useMemo, useRef } from "@wordpress/element";
import { chevronLeft, chevronRight } from "@wordpress/icons";
import { Stack } from "@wordpress/ui";
import { useFocusOnMount } from "@wordpress/compose";
import { store as editorStore } from "../../store/index.mjs";
import { unlock } from "../../lock-unlock.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
function ConnectedRevisionsSlider() {
const revisionData = useSelect((select) => {
const {
getCurrentRevisionId,
getRevisionPage,
getPageRevisions,
getRevisionsPerPage
} = unlock(select(editorStore));
const postType = select(editorStore).getCurrentPostType();
if (!postType) {
return {};
}
const entityConfig = select(coreStore).getEntityConfig(
"postType",
postType
);
const revisionKey = entityConfig?.revisionKey || "id";
const revisionPage = getRevisionPage();
return {
revisions: getPageRevisions(revisionPage),
perPage: getRevisionsPerPage(),
currentRevisionId: getCurrentRevisionId(),
revisionKey,
revisionPage,
totalRevisions: select(editorStore).getCurrentPostRevisionsCount()
};
}, []);
const revisionActions = unlock(useDispatch(editorStore));
return /* @__PURE__ */ jsx(
RevisionsSlider,
{
...revisionData,
setCurrentRevisionId: revisionActions.setCurrentRevisionId,
setRevisionPage: revisionActions.setRevisionPage
}
);
}
function RevisionsSlider({
revisions: rawRevisions,
perPage,
currentRevisionId,
revisionKey,
revisionPage,
totalRevisions,
setCurrentRevisionId,
setRevisionPage
}) {
const setFocusOnMountRef = useFocusOnMount(true);
const initialActiveElementRef = useRef();
const loadingRef = useCallback((node) => {
if (node && initialActiveElementRef.current === void 0) {
initialActiveElementRef.current = node.ownerDocument.activeElement;
}
}, []);
const focusOnMountRef = 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 = 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 = getDateSettings();
const renderTooltipContent = (index) => {
const revision = revisions?.[index];
if (!revision) {
return index;
}
return dateI18n(dateSettings.formats.datetime, revision.date);
};
const showPagination = totalPages > 1;
if (isLoading && !showPagination) {
return /* @__PURE__ */ jsx(Spinner, { ref: loadingRef });
}
if (!isLoading && !revisions?.length) {
return /* @__PURE__ */ jsx("span", { className: "editor-revisions-header__no-revisions", children: __("No revisions found.") });
}
if (totalRevisions <= 1) {
return /* @__PURE__ */ jsx("span", { className: "editor-revisions-header__no-revisions", children: __("Only one revision found.") });
}
const getPageRangeLabel = (page) => {
const end = totalRevisions - (page - 1) * perPage;
const start = Math.max(1, end - perPage + 1);
return sprintf(
/* translators: 1: first revision number, 2: last revision number */
__("Revisions %1$s–%2$s"),
start,
end
);
};
const sliderOrSpinner = isLoading || selectedIndex === -1 ? /* @__PURE__ */ jsx(Spinner, { ref: loadingRef }) : /* @__PURE__ */ jsx(
RangeControl,
{
ref: focusOnMountRef,
"aria-valuetext": renderTooltipContent(selectedIndex),
className: "editor-revisions-header__slider",
hideLabelFromVision: true,
label: __("Revision"),
max: revisions?.length - 1,
min: 0,
marks: true,
onChange: handleSliderChange,
renderTooltipContent,
value: selectedIndex,
withInputField: false
}
);
if (!showPagination) {
return sliderOrSpinner;
}
return /* @__PURE__ */ jsxs(Stack, { direction: "row", gap: "sm", align: "center", style: { flex: 1 }, children: [
/* @__PURE__ */ jsx(
Button,
{
icon: chevronLeft,
label: revisionPage < totalPages ? getPageRangeLabel(revisionPage + 1) : __("No older revisions"),
onClick: () => setRevisionPage(revisionPage + 1),
disabled: isLoading || revisionPage >= totalPages,
size: "compact",
accessibleWhenDisabled: true
}
),
/* @__PURE__ */ jsx(
"div",
{
style: {
flex: 1,
minWidth: 0,
display: "flex",
justifyContent: "center"
},
children: sliderOrSpinner
}
),
/* @__PURE__ */ jsx(
Button,
{
icon: chevronRight,
label: revisionPage > 1 ? getPageRangeLabel(revisionPage - 1) : __("No newer revisions"),
onClick: () => setRevisionPage(revisionPage - 1),
disabled: isLoading || revisionPage <= 1,
size: "compact",
accessibleWhenDisabled: true
}
)
] });
}
export {
RevisionsSlider,
ConnectedRevisionsSlider as default
};
//# sourceMappingURL=revisions-slider.mjs.map