UNPKG

@wordpress/block-library

Version:
178 lines (177 loc) 5.82 kB
// packages/block-library/src/query/edit/query-content.jsx import { useSelect, useDispatch } from "@wordpress/data"; import { useInstanceId } from "@wordpress/compose"; import { useEffect, useCallback } from "@wordpress/element"; import { InspectorControls, useBlockProps, store as blockEditorStore, useInnerBlocksProps, privateApis as blockEditorPrivateApis } from "@wordpress/block-editor"; import { __, sprintf } from "@wordpress/i18n"; import { store as coreStore } from "@wordpress/core-data"; import { store as noticesStore } 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"; var { HTMLElementControl } = unlock(blockEditorPrivateApis); var 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(blockEditorStore); const { createNotice } = useDispatch(noticesStore); const unsupportedBlocks = useUnsupportedBlocks(clientId); const instanceId = useInstanceId(QueryContent); const blockProps = useBlockProps(); const innerBlocksProps = useInnerBlocksProps(blockProps); const { postsPerPage } = useSelect((select) => { const { getSettings } = select(blockEditorStore); const { getEntityRecord, getEntityRecordEdits, canUser } = select(coreStore); const settingPerPage = canUser("read", { kind: "root", name: "site" }) ? +getEntityRecord("root", "site")?.posts_per_page : +getSettings().postsPerPage; const editedSettingPerPage = +getEntityRecordEdits("root", "site")?.posts_per_page; return { postsPerPage: editedSettingPerPage || 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( /* translators: %s: A list of block titles. */ __( `"Reload full page" was enabled because some blocks inside the Query block aren't supported: %s.` ), unsupportedBlocks.join( /* translators: Used between list items, there is a space after the comma. */ __(", ") // eslint-disable-line @wordpress/i18n-no-flanking-whitespace ) ); 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