UNPKG

@wordpress/block-library

Version:
137 lines (136 loc) • 4.99 kB
import { useDispatch, useSelect } from "@wordpress/data"; import { useInstanceId } from "@wordpress/compose"; import { useCallback, useEffect } from "@wordpress/element"; import { InspectorControls, privateApis, store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor"; import { __, sprintf } from "@wordpress/i18n"; import { store as store$1 } from "@wordpress/core-data"; import { store as store$2 } from "@wordpress/notices"; import EnhancedPaginationControl from "./inspector-controls/enhanced-pagination-control.mjs"; import { unlock } from "../../lock-unlock.mjs"; import QueryInspectorControls from "./inspector-controls/index.mjs"; import { getQueryContextFromTemplate, useUnsupportedBlocks } from "../utils.mjs"; import QueryToolbar from "./query-toolbar.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/query/edit/query-content.jsx const { HTMLElementControl } = unlock(privateApis); const DEFAULTS_POSTS_PER_PAGE = 3; function QueryContent({ attributes, setAttributes, clientId, context, name, isSelected }) { const { queryId, query, enhancedPagination, tagName: TagName = "div", query: { inherit } = {} } = attributes; const { templateSlug, postType } = context; const { isSingular, templateType } = getQueryContextFromTemplate(templateSlug); const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(store); const { createNotice } = useDispatch(store$2); const unsupportedBlocks = useUnsupportedBlocks(clientId); const instanceId = useInstanceId(QueryContent); const blockProps = useBlockProps(); const innerBlocksProps = useInnerBlocksProps(blockProps); const { postsPerPage } = useSelect((select) => { const { getSettings } = select(store); const { getEntityRecord, getEntityRecordEdits, canUser } = select(store$1); const settingPerPage = canUser("read", { kind: "root", name: "site" }) ? +getEntityRecord("root", "site")?.posts_per_page : +getSettings().postsPerPage; return { postsPerPage: +getEntityRecordEdits("root", "site")?.posts_per_page || settingPerPage || DEFAULTS_POSTS_PER_PAGE }; }, []); const shouldExcludeCurrentPost = isSingular && !inherit && (query.postType === postType || query.postType === templateType); const updateQuery = useCallback((newQuery) => setAttributes((prevAttributes) => ({ query: { ...prevAttributes.query, ...newQuery } })), [setAttributes]); useEffect(() => { const newQuery = {}; if (inherit && query.perPage !== postsPerPage) newQuery.perPage = postsPerPage; else if (!query.perPage && postsPerPage) newQuery.perPage = postsPerPage; if (!shouldExcludeCurrentPost && query.excludeCurrent !== void 0 && query.excludeCurrent !== null) newQuery.excludeCurrent = null; if (!!Object.keys(newQuery).length) { __unstableMarkNextChangeAsNotPersistent(); updateQuery(newQuery); } }, [ query.perPage, query.excludeCurrent, inherit, postsPerPage, shouldExcludeCurrentPost, __unstableMarkNextChangeAsNotPersistent, updateQuery ]); useEffect(() => { if (!Number.isFinite(queryId)) { __unstableMarkNextChangeAsNotPersistent(); setAttributes({ queryId: instanceId }); } }, [ queryId, instanceId, __unstableMarkNextChangeAsNotPersistent, setAttributes ]); useEffect(() => { if (enhancedPagination && unsupportedBlocks.length) { __unstableMarkNextChangeAsNotPersistent(); setAttributes({ enhancedPagination: false }); const message = sprintf(__(`"Reload full page" was enabled because some blocks inside the Query block aren't supported: %s.`), unsupportedBlocks.join(__(", "))); createNotice("info", message, { type: "snackbar", id: "query-enhanced-pagination-disabled" }); } }, [ enhancedPagination, unsupportedBlocks, __unstableMarkNextChangeAsNotPersistent, setAttributes, createNotice ]); return /* @__PURE__ */ jsxs(Fragment, { children: [ isSelected && /* @__PURE__ */ jsx(QueryToolbar, { clientId, attributes, hasInnerBlocks: true }), /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(QueryInspectorControls, { name, attributes, setQuery: updateQuery, setAttributes, clientId, isSingular, shouldExcludeCurrentPost }) }), /* @__PURE__ */ jsxs(InspectorControls, { group: "advanced", children: [/* @__PURE__ */ jsx(HTMLElementControl, { tagName: TagName, onChange: (value) => setAttributes({ tagName: value }), clientId, options: [ { label: __("Default (<div>)"), value: "div" }, { label: "<main>", value: "main" }, { label: "<section>", value: "section" }, { label: "<aside>", value: "aside" } ] }), /* @__PURE__ */ jsx(EnhancedPaginationControl, { enhancedPagination, setAttributes, clientId })] }), /* @__PURE__ */ jsx(TagName, { ...innerBlocksProps }) ] }); } export { QueryContent as default }; //# sourceMappingURL=query-content.mjs.map