@wordpress/block-library
Version:
Block library for the WordPress editor.
306 lines (305 loc) • 12.9 kB
JavaScript
import { Notice, SelectControl, TextControl, ToggleControl, __experimentalToggleGroupControl, __experimentalToggleGroupControlOption, __experimentalToolsPanel, __experimentalToolsPanelItem, __experimentalVStack } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { store } from "@wordpress/core-data";
import { __, _x, sprintf } from "@wordpress/i18n";
import { debounce } from "@wordpress/compose";
import { useMemo, useState } from "@wordpress/element";
import OrderControl from "./order-control.mjs";
import AuthorControl from "./author-control.mjs";
import ParentControl from "./parent-control.mjs";
import { TaxonomyControls } from "./taxonomy-controls.mjs";
import FormatControls from "./format-controls.mjs";
import StickyControl from "./sticky-control.mjs";
import PerPageControl from "./per-page-control.mjs";
import OffsetControl from "./offset-controls.mjs";
import PagesControl from "./pages-control.mjs";
import { isControlAllowed, useAllowedControls, useIsPostTypeHierarchical, useOrderByOptions, usePostTypes, useTaxonomies } from "../../utils.mjs";
import { useToolsPanelDropdownMenuProps } from "../../../utils/hooks.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/query/edit/inspector-controls/index.jsx
function QueryInspectorControls(props) {
const { attributes, setQuery, isSingular, shouldExcludeCurrentPost } = props;
const { query } = attributes;
const { order, orderBy, author: authorIds, pages, postType = "post", perPage, offset, sticky, inherit, taxQuery, parents, format, excludeCurrent } = query;
const allowedControls = useAllowedControls(attributes);
const showSticky = postType === "post";
const { postTypesTaxonomiesMap, postTypesSelectOptions, postTypeFormatSupportMap } = usePostTypes();
const taxonomies = useTaxonomies(postType);
const isPostTypeHierarchical = useIsPostTypeHierarchical(postType);
const onPostTypeChange = (newValue) => {
const updateQuery = { postType: newValue };
const supportedTaxonomies = postTypesTaxonomiesMap[newValue];
if (!!supportedTaxonomies?.length && !!taxQuery) {
const buildTaxQuery = (_taxQuery) => {
return Object.entries(_taxQuery || {}).reduce((accumulator, [taxonomy, terms]) => {
if (supportedTaxonomies.includes(taxonomy)) accumulator[taxonomy] = terms;
return accumulator;
}, {});
};
const updatedTaxQuery = {};
const builtIncludeTaxQuery = buildTaxQuery(taxQuery.include);
if (!!Object.keys(builtIncludeTaxQuery).length) updatedTaxQuery.include = builtIncludeTaxQuery;
const builtExcludeTaxQuery = buildTaxQuery(taxQuery.exclude);
if (!!Object.keys(builtExcludeTaxQuery).length) updatedTaxQuery.exclude = builtExcludeTaxQuery;
updateQuery.taxQuery = !!Object.keys(updatedTaxQuery).length ? updatedTaxQuery : void 0;
}
if (newValue !== "post") updateQuery.sticky = "";
updateQuery.parents = [];
if (!postTypeFormatSupportMap[newValue]) updateQuery.format = [];
setQuery(updateQuery);
};
const [querySearch, setQuerySearch] = useState(query.search);
const debouncedQuerySearch = useMemo(() => {
return debounce((newQuerySearch) => {
setQuery({ search: newQuerySearch });
}, 250);
}, [setQuery]);
const orderByOptions = useOrderByOptions(postType);
const showInheritControl = isControlAllowed(allowedControls, "inherit");
const showPostTypeControl = !inherit && isControlAllowed(allowedControls, "postType");
const postTypeControlLabel = __("Post type");
const postTypeControlHelp = __("Select the type of content to display: posts, pages, or custom post types.");
const showOrderControl = !inherit && isControlAllowed(allowedControls, "order");
const showStickyControl = !inherit && showSticky && isControlAllowed(allowedControls, "sticky");
const showSettingsPanel = showInheritControl || showPostTypeControl || showOrderControl || showStickyControl;
const showTaxControl = !!taxonomies?.length && isControlAllowed(allowedControls, "taxQuery");
const showAuthorControl = isControlAllowed(allowedControls, "author");
const showSearchControl = isControlAllowed(allowedControls, "search");
const showParentControl = isControlAllowed(allowedControls, "parents") && isPostTypeHierarchical;
const postTypeHasFormatSupport = postTypeFormatSupportMap[postType];
const showFormatControl = useSelect((select) => {
if (!postTypeHasFormatSupport || !isControlAllowed(allowedControls, "format")) return false;
const themeSupports = select(store).getThemeSupports();
return themeSupports.formats && themeSupports.formats.length > 0 && themeSupports.formats.some((type) => type !== "standard");
}, [allowedControls, postTypeHasFormatSupport]);
const showExcludeCurrentControl = shouldExcludeCurrentPost && isControlAllowed(allowedControls, "excludeCurrent");
const postTypeSingularName = useSelect((select) => select(store).getPostType(postType)?.labels?.singular_name, [postType]);
const showFiltersPanel = showTaxControl || showAuthorControl || showSearchControl || showParentControl || showFormatControl || showExcludeCurrentControl;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const showPostCountControl = isControlAllowed(allowedControls, "postCount");
const showOffSetControl = isControlAllowed(allowedControls, "offset");
const showPagesControl = isControlAllowed(allowedControls, "pages");
const showDisplayPanel = showPostCountControl || showOffSetControl || showPagesControl;
const hasInheritanceWarning = isSingular && inherit;
return /* @__PURE__ */ jsxs(Fragment, { children: [
showSettingsPanel && /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setQuery({
postType: "post",
order: "desc",
orderBy: "date",
sticky: "",
inherit: true
});
},
dropdownMenuProps,
children: [
showInheritControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !inherit,
label: __("Query type"),
onDeselect: () => setQuery({ inherit: true }),
isShownByDefault: true,
children: /* @__PURE__ */ jsxs(__experimentalVStack, {
spacing: 4,
children: [/* @__PURE__ */ jsxs(__experimentalToggleGroupControl, {
label: __("Query type"),
isBlock: true,
onChange: (value) => {
setQuery({ inherit: value === "default" });
},
help: inherit ? __("Display a list of posts or custom post types based on the current template.") : __("Display a list of posts or custom post types based on specific criteria."),
value: !!inherit ? "default" : "custom",
children: [/* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
value: "default",
label: __("Default")
}), /* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
value: "custom",
label: __("Custom")
})]
}), hasInheritanceWarning && /* @__PURE__ */ jsx(Notice, {
status: "warning",
isDismissible: false,
children: __("Cannot inherit the current template query when placed inside the singular content (e.g., post, page, 404, blank).")
})]
})
}),
showPostTypeControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => postType !== "post",
label: postTypeControlLabel,
onDeselect: () => onPostTypeChange("post"),
isShownByDefault: true,
children: postTypesSelectOptions.length > 2 ? /* @__PURE__ */ jsx(SelectControl, {
options: postTypesSelectOptions,
value: postType,
label: postTypeControlLabel,
onChange: onPostTypeChange,
help: postTypeControlHelp
}) : /* @__PURE__ */ jsx(__experimentalToggleGroupControl, {
isBlock: true,
value: postType,
label: postTypeControlLabel,
onChange: onPostTypeChange,
help: postTypeControlHelp,
children: postTypesSelectOptions.map((option) => /* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
value: option.value,
label: option.label
}, option.value))
})
}),
showOrderControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => order !== "desc" || orderBy !== "date",
label: __("Order by"),
onDeselect: () => setQuery({
order: "desc",
orderBy: "date"
}),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(OrderControl, {
order,
orderBy,
orderByOptions,
onChange: setQuery
})
}),
showStickyControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!sticky,
label: __("Sticky posts"),
onDeselect: () => setQuery({ sticky: "" }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(StickyControl, {
value: sticky,
onChange: (value) => setQuery({ sticky: value })
})
})
]
}),
!inherit && showDisplayPanel && /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
className: "block-library-query-toolspanel__display",
label: __("Display"),
resetAll: () => {
setQuery({
offset: 0,
pages: 0
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Items per page"),
hasValue: () => perPage > 0,
children: /* @__PURE__ */ jsx(PerPageControl, {
perPage,
offset,
onChange: setQuery
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: _x("Offset", "Number of posts to skip in a query"),
hasValue: () => offset > 0,
onDeselect: () => setQuery({ offset: 0 }),
children: /* @__PURE__ */ jsx(OffsetControl, {
offset,
onChange: setQuery
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Max pages to show"),
hasValue: () => pages > 0,
onDeselect: () => setQuery({ pages: 0 }),
children: /* @__PURE__ */ jsx(PagesControl, {
pages,
onChange: setQuery
})
})
]
}),
!inherit && showFiltersPanel && /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
className: "block-library-query-toolspanel__filters",
label: __("Filters"),
resetAll: () => {
setQuery({
author: "",
parents: [],
search: "",
taxQuery: null,
format: [],
excludeCurrent: null
});
setQuerySearch("");
},
dropdownMenuProps,
children: [
showTaxControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Taxonomies"),
hasValue: () => Object.values(taxQuery || {}).some((value) => Object.values(value || {}).some((termIds) => !!termIds?.length)),
onDeselect: () => setQuery({ taxQuery: null }),
children: /* @__PURE__ */ jsx(TaxonomyControls, {
onChange: setQuery,
query
})
}),
showAuthorControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!authorIds,
label: __("Authors"),
onDeselect: () => setQuery({ author: "" }),
children: /* @__PURE__ */ jsx(AuthorControl, {
value: authorIds,
onChange: setQuery
})
}),
showSearchControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!querySearch,
label: __("Search terms"),
onDeselect: () => {
setQuery({ search: "" });
setQuerySearch("");
},
children: /* @__PURE__ */ jsx(TextControl, {
label: __("Search terms"),
help: __("Filter the query by keywords that appear in the content, title, or excerpt."),
value: querySearch,
onChange: (newQuerySearch) => {
debouncedQuerySearch(newQuerySearch);
setQuerySearch(newQuerySearch);
}
})
}),
showParentControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!parents?.length,
label: __("Parents"),
onDeselect: () => setQuery({ parents: [] }),
children: /* @__PURE__ */ jsx(ParentControl, {
parents,
postType,
onChange: setQuery
})
}),
showFormatControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!format?.length,
label: __("Formats"),
onDeselect: () => setQuery({ format: [] }),
children: /* @__PURE__ */ jsx(FormatControls, {
onChange: setQuery,
query
})
}),
showExcludeCurrentControl && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Exclude"),
hasValue: () => excludeCurrent !== void 0 && excludeCurrent !== null,
onDeselect: () => setQuery({ excludeCurrent: null }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Exclude current"),
help: sprintf(__("Exclude the current %s from the query."), postTypeSingularName),
checked: !!excludeCurrent,
onChange: (value) => {
setQuery({ excludeCurrent: !!value });
}
})
})
]
})
] });
}
export { QueryInspectorControls as default };
//# sourceMappingURL=index.mjs.map