UNPKG

@wordpress/block-library

Version:
179 lines (178 loc) • 7.01 kB
import clsx from "clsx"; import { memo, useMemo, useState } from "@wordpress/element"; import { useSelect } from "@wordpress/data"; import { __, _x } from "@wordpress/i18n"; import { BlockContextProvider, BlockControls, __experimentalUseBlockPreview, store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor"; import { Spinner, ToolbarGroup } from "@wordpress/components"; import { store as store$1 } from "@wordpress/core-data"; import { grid, list } from "@wordpress/icons"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/post-template/edit.jsx const INNER_BLOCKS_LAYOUT = { type: "default" }; function PostTemplateInnerBlocks({ classList }) { const innerBlocksProps = useInnerBlocksProps({ className: clsx("wp-block-post", classList) }, { __unstableDisableLayoutClassNames: true, layout: INNER_BLOCKS_LAYOUT }); return /* @__PURE__ */ jsx("li", { ...innerBlocksProps }); } function PostTemplateBlockPreview({ blocks, blockContextId, classList, isHidden, setActiveBlockContextId }) { const blockPreviewProps = __experimentalUseBlockPreview({ blocks, props: { className: clsx("wp-block-post", classList) } }); const handleOnClick = () => { setActiveBlockContextId(blockContextId); }; const style = { display: isHidden ? "none" : void 0 }; return /* @__PURE__ */ jsx("li", { ...blockPreviewProps, tabIndex: 0, role: "button", onClick: handleOnClick, onKeyPress: handleOnClick, style }); } const MemoizedPostTemplateBlockPreview = memo(PostTemplateBlockPreview); function PostTemplateEdit({ setAttributes, clientId, context: { query: { perPage, offset = 0, postType = "post", order, orderBy, author, search, exclude, sticky, inherit, taxQuery, parents, pages, format, excludeCurrent, ...restQueryArgs } = {}, templateSlug, previewPostType, postId }, attributes: { layout }, __unstableLayoutClassNames }) { const { type: layoutType, columnCount = 3, minimumColumnWidth } = layout || {}; const [activeBlockContextId, setActiveBlockContextId] = useState(); const { posts, blocks } = useSelect((select) => { const { getEntityRecords, getTaxonomies } = select(store$1); const { getBlocks } = select(store); const templateCategory = inherit && templateSlug?.startsWith("category-") && getEntityRecords("taxonomy", "category", { context: "view", per_page: 1, _fields: ["id"], slug: templateSlug.replace("category-", "") }); const templateTag = inherit && templateSlug?.startsWith("tag-") && getEntityRecords("taxonomy", "post_tag", { context: "view", per_page: 1, _fields: ["id"], slug: templateSlug.replace("tag-", "") }); const query = { offset: offset || 0, order, orderby: orderBy }; if (taxQuery && !inherit) { const taxonomies = getTaxonomies({ type: postType, per_page: -1, context: "view" }); const buildTaxQuery = (terms, suffix = "") => { return Object.entries(terms || {}).reduce((accumulator, [taxonomySlug, termIds]) => { const taxonomy = taxonomies?.find(({ slug }) => slug === taxonomySlug); if (taxonomy?.rest_base && termIds?.length) accumulator[taxonomy.rest_base + suffix] = termIds; return accumulator; }, {}); }; const builtTaxQuery = buildTaxQuery(taxQuery.include); if (taxQuery.exclude) Object.assign(builtTaxQuery, buildTaxQuery(taxQuery.exclude, "_exclude")); if (!!Object.keys(builtTaxQuery).length) Object.assign(query, builtTaxQuery); } if (perPage) query.per_page = perPage; if (author) query.author = author; if (search) query.search = search; if (exclude?.length) query.exclude = exclude; if (parents?.length) query.parent = parents; if (format?.length) query.format = format; if (excludeCurrent && postId) { if (query.exclude) query.exclude = [...query.exclude, postId]; else query.exclude = [postId]; } if (["exclude", "only"].includes(sticky)) query.sticky = sticky === "only"; if (["", "ignore"].includes(sticky)) { delete query.sticky; query.ignore_sticky = sticky === "ignore"; } let currentPostType = postType; if (inherit) { if (templateSlug?.startsWith("archive-")) { query.postType = templateSlug.replace("archive-", ""); currentPostType = query.postType; } else if (templateCategory) query.categories = templateCategory[0]?.id; else if (templateTag) query.tags = templateTag[0]?.id; else if (templateSlug?.startsWith("taxonomy-post_format")) query.format = templateSlug.replace("taxonomy-post_format-post-format-", ""); } return { posts: getEntityRecords("postType", previewPostType || currentPostType, { ...query, ...restQueryArgs }), blocks: getBlocks(clientId) }; }, [ perPage, offset, order, orderBy, clientId, author, search, postType, exclude, sticky, inherit, templateSlug, taxQuery, parents, format, restQueryArgs, previewPostType ]); const blockContexts = useMemo(() => posts?.map((post) => ({ postType: post.type, postId: post.id, classList: post.class_list ?? "" })), [posts]); const blockProps = useBlockProps({ className: clsx(__unstableLayoutClassNames, { [`columns-${columnCount}`]: layoutType === "grid" && columnCount, "has-native-responsive-grid": layoutType === "grid" && columnCount && minimumColumnWidth }) }); if (!posts) return /* @__PURE__ */ jsx("p", { ...blockProps, children: /* @__PURE__ */ jsx(Spinner, {}) }); if (!posts.length) return /* @__PURE__ */ jsxs("p", { ...blockProps, children: [" ", __("No results found.")] }); const setDisplayLayout = (newDisplayLayout) => setAttributes({ layout: { ...layout, ...newDisplayLayout } }); const displayLayoutControls = [{ icon: list, title: _x("List view", "Post template block display setting"), onClick: () => setDisplayLayout({ type: "default" }), isActive: layoutType === "default" || layoutType === "constrained" }, { icon: grid, title: _x("Grid view", "Post template block display setting"), onClick: () => setDisplayLayout({ type: "grid", columnCount }), isActive: layoutType === "grid" }]; return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(ToolbarGroup, { controls: displayLayoutControls }) }), /* @__PURE__ */ jsx("ul", { ...blockProps, children: blockContexts && blockContexts.map((blockContext) => /* @__PURE__ */ jsxs(BlockContextProvider, { value: blockContext, children: [blockContext.postId === (activeBlockContextId || blockContexts[0]?.postId) ? /* @__PURE__ */ jsx(PostTemplateInnerBlocks, { classList: blockContext.classList }) : null, /* @__PURE__ */ jsx(MemoizedPostTemplateBlockPreview, { blocks, blockContextId: blockContext.postId, classList: blockContext.classList, setActiveBlockContextId, isHidden: blockContext.postId === (activeBlockContextId || blockContexts[0]?.postId) })] }, blockContext.postId)) })] }); } export { PostTemplateEdit as default }; //# sourceMappingURL=edit.mjs.map