@wordpress/block-library
Version:
Block library for the WordPress editor.
166 lines (165 loc) • 7.09 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_data = require("@wordpress/data");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_element = require("@wordpress/element");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_notices = require("@wordpress/notices");
let __inspector_controls_enhanced_pagination_control_jsx = require("./inspector-controls/enhanced-pagination-control.cjs");
__inspector_controls_enhanced_pagination_control_jsx = __toESM(__inspector_controls_enhanced_pagination_control_jsx);
let ______lock_unlock_ts = require("../../lock-unlock.cjs");
let __inspector_controls_index_jsx = require("./inspector-controls/index.cjs");
__inspector_controls_index_jsx = __toESM(__inspector_controls_index_jsx);
let ___utils_js = require("../utils.cjs");
let __query_toolbar_jsx = require("./query-toolbar.cjs");
__query_toolbar_jsx = __toESM(__query_toolbar_jsx);
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/query/edit/query-content.jsx
const { HTMLElementControl } = (0, ______lock_unlock_ts.unlock)(_wordpress_block_editor.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 } = (0, ___utils_js.getQueryContextFromTemplate)(templateSlug);
const { __unstableMarkNextChangeAsNotPersistent } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const { createNotice } = (0, _wordpress_data.useDispatch)(_wordpress_notices.store);
const unsupportedBlocks = (0, ___utils_js.useUnsupportedBlocks)(clientId);
const instanceId = (0, _wordpress_compose.useInstanceId)(QueryContent);
const blockProps = (0, _wordpress_block_editor.useBlockProps)();
const innerBlocksProps = (0, _wordpress_block_editor.useInnerBlocksProps)(blockProps);
const { postsPerPage } = (0, _wordpress_data.useSelect)((select) => {
const { getSettings } = select(_wordpress_block_editor.store);
const { getEntityRecord, getEntityRecordEdits, canUser } = select(_wordpress_core_data.store);
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 = (0, _wordpress_element.useCallback)((newQuery) => setAttributes((prevAttributes) => ({ query: {
...prevAttributes.query,
...newQuery
} })), [setAttributes]);
(0, _wordpress_element.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
]);
(0, _wordpress_element.useEffect)(() => {
if (!Number.isFinite(queryId)) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes({ queryId: instanceId });
}
}, [
queryId,
instanceId,
__unstableMarkNextChangeAsNotPersistent,
setAttributes
]);
(0, _wordpress_element.useEffect)(() => {
if (enhancedPagination && unsupportedBlocks.length) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes({ enhancedPagination: false });
const message = (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)(`"Reload full page" was enabled because some blocks inside the Query block aren't supported: %s.`), unsupportedBlocks.join((0, _wordpress_i18n.__)(", ")));
createNotice("info", message, {
type: "snackbar",
id: "query-enhanced-pagination-disabled"
});
}
}, [
enhancedPagination,
unsupportedBlocks,
__unstableMarkNextChangeAsNotPersistent,
setAttributes,
createNotice
]);
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
isSelected && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__query_toolbar_jsx.default, {
clientId,
attributes,
hasInnerBlocks: true
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__inspector_controls_index_jsx.default, {
name,
attributes,
setQuery: updateQuery,
setAttributes,
clientId,
isSingular,
shouldExcludeCurrentPost
}) }),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_block_editor.InspectorControls, {
group: "advanced",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(HTMLElementControl, {
tagName: TagName,
onChange: (value) => setAttributes({ tagName: value }),
clientId,
options: [
{
label: (0, _wordpress_i18n.__)("Default (<div>)"),
value: "div"
},
{
label: "<main>",
value: "main"
},
{
label: "<section>",
value: "section"
},
{
label: "<aside>",
value: "aside"
}
]
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__inspector_controls_enhanced_pagination_control_jsx.default, {
enhancedPagination,
setAttributes,
clientId
})]
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(TagName, { ...innerBlocksProps })
] });
}
exports.default = QueryContent;
//# sourceMappingURL=query-content.cjs.map