@wordpress/block-editor
Version:
68 lines (67 loc) • 2.66 kB
JavaScript
// packages/block-editor/src/hooks/list-view.js
import { __ } from "@wordpress/i18n";
import { PanelBody } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { store as blocksStore, hasBlockSupport } from "@wordpress/blocks";
import { useContext } from "@wordpress/element";
import { store as blockEditorStore } from "../store/index.mjs";
import { PrivateListView } from "../components/list-view/index.mjs";
import InspectorControls from "../components/inspector-controls/fill.mjs";
import { PrivateBlockContext } from "../components/block-list/private-block-context.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
var LIST_VIEW_SUPPORT_KEY = "listView";
function hasListViewSupport(nameOrType) {
return hasBlockSupport(nameOrType, LIST_VIEW_SUPPORT_KEY);
}
function ListViewPanel({ clientId, name }) {
const { isSelectionWithinCurrentSection } = useContext(PrivateBlockContext);
const isEnabled = hasListViewSupport(name);
const { hasChildren, blockTitle, isNestedListView } = useSelect(
(select) => {
const { getBlockCount, getBlockParents, getBlockName } = select(blockEditorStore);
let _isNestedListView = false;
if (isSelectionWithinCurrentSection) {
const parents = getBlockParents(clientId, true);
_isNestedListView = parents.find((parentId) => {
const parentName = getBlockName(parentId);
return parentName === "core/navigation" || hasBlockSupport(parentName, "listView");
});
}
return {
hasChildren: !!getBlockCount(clientId),
blockTitle: select(blocksStore).getBlockType(name)?.title,
isNestedListView: _isNestedListView
};
},
[clientId, name, isSelectionWithinCurrentSection]
);
if (!isEnabled || isNestedListView) {
return null;
}
const showBlockTitle = isSelectionWithinCurrentSection;
return /* @__PURE__ */ jsx(InspectorControls, { group: "list", children: /* @__PURE__ */ jsxs(PanelBody, { title: showBlockTitle ? blockTitle : void 0, children: [
!hasChildren && /* @__PURE__ */ jsx("p", { className: "block-editor-block-inspector__no-blocks", children: __("No items yet.") }),
/* @__PURE__ */ jsx(
PrivateListView,
{
rootClientId: clientId,
isExpanded: true,
description: blockTitle,
showAppender: true
}
)
] }) });
}
var list_view_default = {
edit: ListViewPanel,
hasSupport: hasListViewSupport,
attributeKeys: [],
supportsPatternEditing: true
};
export {
LIST_VIEW_SUPPORT_KEY,
ListViewPanel,
list_view_default as default,
hasListViewSupport
};
//# sourceMappingURL=list-view.mjs.map