UNPKG

@wordpress/editor

Version:
140 lines (139 loc) 4.37 kB
// packages/editor/src/components/post-revisions-panel/index.js import { PanelBody, Button, __experimentalHStack as HStack, __experimentalVStack as VStack, privateApis as componentsPrivateApis } from "@wordpress/components"; import { store as coreStore } from "@wordpress/core-data"; import { DataViews } from "@wordpress/dataviews"; import { dateI18n, getDate, humanTimeDiff, getSettings } from "@wordpress/date"; import { useSelect, useDispatch } from "@wordpress/data"; import { __ } from "@wordpress/i18n"; import { authorField } from "@wordpress/fields"; import PostLastRevisionCheck from "../post-last-revision/check.mjs"; import { store as editorStore } from "../../store/index.mjs"; import { unlock } from "../../lock-unlock.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; var { Badge } = unlock(componentsPrivateApis); var DAY_IN_MILLISECONDS = 864e5; var EMPTY_ARRAY = []; var REVISIONS_QUERY = { per_page: 3, orderby: "date", order: "desc", context: "embed", _fields: "id,date,author" }; var defaultLayouts = { activity: {} }; var view = { type: "activity", titleField: "date", fields: ["author"], layout: { density: "compact" } }; var fields = [ { id: "date", label: __("Date"), render: ({ item }) => { const dateNowInMs = getDate(null).getTime(); const date = getDate(item.date ?? null); const displayDate = dateNowInMs - date.getTime() > DAY_IN_MILLISECONDS ? dateI18n( getSettings().formats.datetimeAbbreviated, date ) : humanTimeDiff(date); return /* @__PURE__ */ jsx( "time", { className: "editor-post-revisions-panel__revision-date", dateTime: item.date, children: displayDate } ); }, enableSorting: false, enableHiding: false }, authorField ]; var noop = () => { }; var paginationInfo = {}; function PostRevisionsPanelContent() { const { setCurrentRevisionId } = unlock(useDispatch(editorStore)); const { revisionsCount, revisions, isLoading, lastRevisionId } = useSelect( (select) => { const { getCurrentPostId, getCurrentPostType } = select(editorStore); const { getCurrentPostRevisionsCount, getCurrentPostLastRevisionId } = select(editorStore); const { getRevisions, isResolving } = select(coreStore); const query = [ "postType", getCurrentPostType(), getCurrentPostId(), REVISIONS_QUERY ]; const _revisions = getRevisions(...query); return { revisionsCount: getCurrentPostRevisionsCount(), lastRevisionId: getCurrentPostLastRevisionId(), revisions: _revisions, isLoading: isResolving("getRevisions", query) }; }, [] ); return /* @__PURE__ */ jsx( PanelBody, { title: /* @__PURE__ */ jsxs(HStack, { justify: "space-between", align: "center", as: "span", children: [ /* @__PURE__ */ jsx("span", { children: __("Revisions") }), /* @__PURE__ */ jsx(Badge, { className: "editor-post-revisions-panel__revisions-count", children: revisionsCount }) ] }), initialOpen: false, children: /* @__PURE__ */ jsxs(VStack, { className: "editor-post-revisions-panel", children: [ /* @__PURE__ */ jsx( DataViews, { view, onChangeView: noop, fields, data: revisions || EMPTY_ARRAY, isLoading, paginationInfo, defaultLayouts, getItemId: (item) => item.id, isItemClickable: () => true, onClickItem: (item) => { setCurrentRevisionId(item.id); }, children: /* @__PURE__ */ jsx(DataViews.Layout, {}) } ), /* @__PURE__ */ jsx( Button, { className: "editor-post-revisions-panel__view-all", __next40pxDefaultSize: true, variant: "secondary", onClick: () => setCurrentRevisionId(lastRevisionId), children: __("View all revisions") } ) ] }) } ); } function PostRevisionsPanel() { return /* @__PURE__ */ jsx(PostLastRevisionCheck, { children: /* @__PURE__ */ jsx(PostRevisionsPanelContent, {}) }); } export { PostRevisionsPanel as default }; //# sourceMappingURL=index.mjs.map