@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
114 lines (113 loc) • 3.76 kB
JavaScript
// packages/editor/src/components/post-revisions-preview/revisions-slider.js
import { useMemo } from "@wordpress/element";
import { useSelect, useDispatch } from "@wordpress/data";
import { RangeControl, Spinner } from "@wordpress/components";
import { store as coreStore } from "@wordpress/core-data";
import { __ } from "@wordpress/i18n";
import { dateI18n, getSettings as getDateSettings } from "@wordpress/date";
import { store as editorStore } from "../../store/index.mjs";
import { unlock } from "../../lock-unlock.mjs";
import { jsx } from "react/jsx-runtime";
function RevisionsSlider() {
const { revisions, isLoading, currentRevisionId, revisionKey } = useSelect(
(select) => {
const { getCurrentPostId, getCurrentPostType } = select(editorStore);
const { getRevisions, isResolving, getEntityConfig } = select(coreStore);
const postId = getCurrentPostId();
const postType = getCurrentPostType();
if (!postId || !postType) {
return {};
}
const entityConfig = getEntityConfig("postType", postType);
const _revisionKey = entityConfig?.revisionKey || "id";
const query = {
per_page: -1,
context: "edit",
_fields: [
.../* @__PURE__ */ new Set([
"id",
"date",
"modified",
"author",
"meta",
"title.raw",
"excerpt.raw",
"content.raw",
_revisionKey
])
].join()
};
return {
revisions: getRevisions("postType", postType, postId, query),
isLoading: isResolving("getRevisions", [
"postType",
postType,
postId,
query
]),
currentRevisionId: unlock(
select(editorStore)
).getCurrentRevisionId(),
revisionKey: _revisionKey
};
},
[]
);
const { setCurrentRevisionId } = unlock(useDispatch(editorStore));
const revisionDateField = revisionKey === "wp_id" ? "modified" : "date";
const sortedRevisions = useMemo(() => {
return revisions?.slice().sort(
(a, b) => new Date(a[revisionDateField]) - new Date(b[revisionDateField])
) ?? [];
}, [revisions, revisionDateField]);
const selectedIndex = sortedRevisions.findIndex(
(r) => r[revisionKey] === currentRevisionId
);
const handleSliderChange = (index) => {
const revision = sortedRevisions[index];
if (revision) {
setCurrentRevisionId(revision[revisionKey]);
}
};
const dateSettings = getDateSettings();
const renderTooltipContent = (index) => {
const revision = sortedRevisions[index];
if (!revision) {
return index;
}
return dateI18n(
dateSettings.formats.datetime,
revision[revisionDateField]
);
};
if (isLoading) {
return /* @__PURE__ */ jsx(Spinner, {});
}
if (!sortedRevisions.length) {
return /* @__PURE__ */ jsx("span", { className: "editor-revisions-header__no-revisions", children: __("No revisions found.") });
}
if (sortedRevisions.length === 1) {
return /* @__PURE__ */ jsx("span", { className: "editor-revisions-header__no-revisions", children: __("Only one revision found.") });
}
return /* @__PURE__ */ jsx(
RangeControl,
{
__next40pxDefaultSize: true,
className: "editor-revisions-header__slider",
hideLabelFromVision: true,
label: __("Revision"),
max: sortedRevisions.length - 1,
min: 0,
marks: true,
onChange: handleSliderChange,
renderTooltipContent,
value: selectedIndex,
withInputField: false
}
);
}
var revisions_slider_default = RevisionsSlider;
export {
revisions_slider_default as default
};
//# sourceMappingURL=revisions-slider.mjs.map