@wordpress/block-library
Version:
Block library for the WordPress editor.
68 lines (67 loc) • 2.26 kB
JavaScript
// packages/block-library/src/calendar/edit.js
import memoize from "memize";
import { calendar as icon } from "@wordpress/icons";
import { Disabled, Placeholder, Spinner } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import ServerSideRender from "@wordpress/server-side-render";
import { useBlockProps } from "@wordpress/block-editor";
import { store as coreStore } from "@wordpress/core-data";
import { __ } from "@wordpress/i18n";
import { jsx } from "react/jsx-runtime";
var getYearMonth = memoize((date) => {
if (!date) {
return {};
}
const dateObj = new Date(date);
return {
year: dateObj.getFullYear(),
month: dateObj.getMonth() + 1
};
});
function CalendarEdit({ attributes }) {
const blockProps = useBlockProps();
const { date, hasPosts, hasPostsResolved } = useSelect((select) => {
const { getEntityRecords, hasFinishedResolution } = select(coreStore);
const singlePublishedPostQuery = {
status: "publish",
per_page: 1
};
const posts = getEntityRecords(
"postType",
"post",
singlePublishedPostQuery
);
const postsResolved = hasFinishedResolution("getEntityRecords", [
"postType",
"post",
singlePublishedPostQuery
]);
let _date;
const editorSelectors = select("core/editor");
if (editorSelectors) {
const postType = editorSelectors.getEditedPostAttribute("type");
if (postType === "post") {
_date = editorSelectors.getEditedPostAttribute("date");
}
}
return {
date: _date,
hasPostsResolved: postsResolved,
hasPosts: postsResolved && posts?.length === 1
};
}, []);
if (!hasPosts) {
return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(Placeholder, { icon, label: __("Calendar"), children: !hasPostsResolved ? /* @__PURE__ */ jsx(Spinner, {}) : __("No published posts found.") }) });
}
return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(Disabled, { children: /* @__PURE__ */ jsx(
ServerSideRender,
{
block: "core/calendar",
attributes: { ...attributes, ...getYearMonth(date) }
}
) }) });
}
export {
CalendarEdit as default
};
//# sourceMappingURL=edit.js.map