UNPKG

@wordpress/global-styles-ui

Version:
299 lines (297 loc) • 11.8 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/global-styles-ui/src/screen-block-list.tsx var screen_block_list_exports = {}; __export(screen_block_list_exports, { default: () => screen_block_list_default, hasUserStylesForBlock: () => hasUserStylesForBlock, useBlockHasGlobalStyles: () => useBlockHasGlobalStyles }); module.exports = __toCommonJS(screen_block_list_exports); var import_blocks = require("@wordpress/blocks"); var import_i18n = require("@wordpress/i18n"); var import_components = require("@wordpress/components"); var import_ui = require("@wordpress/ui"); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_block_editor = require("@wordpress/block-editor"); var import_compose = require("@wordpress/compose"); var import_a11y = require("@wordpress/a11y"); var import_icons = require("@wordpress/icons"); var import_variations_panel = require("./variations/variations-panel.cjs"); var import_screen_header = require("./screen-header.cjs"); var import_navigation_button = require("./navigation-button.cjs"); var import_hooks = require("./hooks.cjs"); var import_lock_unlock = require("./lock-unlock.cjs"); var import_context = require("./context.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); var { useHasDimensionsPanel, useHasTypographyPanel, useHasBorderPanel, useSettingsForBlockElement, useHasColorPanel, useHasBackgroundPanel } = (0, import_lock_unlock.unlock)(import_block_editor.privateApis); var { Menu } = (0, import_lock_unlock.unlock)(import_components.privateApis); function hasAnyValue(value) { if (value === void 0 || value === null) { return false; } if (Array.isArray(value)) { return value.length > 0; } if (typeof value === "object") { return Object.values(value).some(hasAnyValue); } return true; } function hasUserStylesForBlock(user, blockName) { return hasAnyValue(user?.styles?.blocks?.[blockName]) || hasAnyValue(user?.settings?.blocks?.[blockName]); } function useSortedBlockTypes() { const blockItems = (0, import_data.useSelect)( (select) => select(import_blocks.store).getBlockTypes(), [] ); const groupByType = (blocks, block) => { const { core, noncore } = blocks; const type = block.name.startsWith("core/") ? core : noncore; type.push(block); return blocks; }; const { core: coreItems, noncore: nonCoreItems } = blockItems.reduce( groupByType, { core: [], noncore: [] } ); return [...coreItems, ...nonCoreItems]; } function useBlockHasGlobalStyles(blockName) { const [rawSettings] = (0, import_hooks.useSetting)("", blockName); const settings = useSettingsForBlockElement(rawSettings, blockName); const hasTypographyPanel = useHasTypographyPanel(settings); const hasColorPanel = useHasColorPanel(settings); const hasBackgroundPanel = useHasBackgroundPanel(settings); const hasBorderPanel = useHasBorderPanel(settings); const hasDimensionsPanel = useHasDimensionsPanel(settings); const hasLayoutPanel = hasBorderPanel || hasDimensionsPanel; const hasVariationsPanel = !!(0, import_variations_panel.useBlockVariations)(blockName)?.length; const hasGlobalStyles = hasTypographyPanel || hasColorPanel || hasBackgroundPanel || hasLayoutPanel || hasVariationsPanel; return hasGlobalStyles; } function BlockMenuItem({ block, isCustomized }) { const hasBlockMenuItem = useBlockHasGlobalStyles(block.name); if (!hasBlockMenuItem) { return null; } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_navigation_button.NavigationButtonAsItem, { path: "/blocks/" + encodeURIComponent(block.name), children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { justify: "flex-start", spacing: 2, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_block_editor.BlockIcon, { className: "global-styles-ui-block-types-item__icon", icon: block.icon } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.FlexItem, { children: block.title }), isCustomized && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ui.VisuallyHidden, { children: (0, import_i18n.__)("Has custom styles") }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "span", { "aria-hidden": "true", className: "global-styles-ui-block-types-item__indicator" } ) ] }) ] }) } ); } function EmptyBlockList({ filterValue, styleFilter }) { const label = "customized" === styleFilter && !filterValue ? (0, import_i18n.__)("You haven't customized any blocks yet.") : (0, import_i18n.__)("No blocks found."); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalText, { align: "center", as: "p", className: "global-styles-ui-block-types-item-list__no-results", children: label } ); } function BlockList({ filterValue, styleFilter }) { const sortedBlockTypes = useSortedBlockTypes(); const debouncedSpeak = (0, import_compose.useDebounce)(import_a11y.speak, 500); const { isMatchingSearchTerm } = (0, import_data.useSelect)(import_blocks.store); const { user } = (0, import_element.useContext)(import_context.GlobalStylesContext); const customizedBlockNames = (0, import_element.useMemo)(() => { const names = /* @__PURE__ */ new Set(); const blockNames = [ ...Object.keys(user?.styles?.blocks ?? {}), ...Object.keys(user?.settings?.blocks ?? {}) ]; blockNames.forEach((blockName) => { if (hasUserStylesForBlock(user, blockName)) { names.add(blockName); } }); return names; }, [user]); const searchedBlockTypes = !filterValue ? sortedBlockTypes : sortedBlockTypes.filter( (blockType) => isMatchingSearchTerm(blockType, filterValue) ); const filteredBlockTypes = styleFilter === "customized" ? searchedBlockTypes.filter( (blockType) => customizedBlockNames.has(blockType.name) ) : searchedBlockTypes; const blockTypesListRef = (0, import_element.useRef)(null); const hasResults = filteredBlockTypes.length > 0; (0, import_element.useEffect)(() => { if (!filterValue && styleFilter === "all") { return; } const count = hasResults ? blockTypesListRef.current?.childElementCount || 0 : 0; const resultsFoundMessage = (0, import_i18n.sprintf)( /* translators: %d: number of results. */ (0, import_i18n._n)("%d result found.", "%d results found.", count), count ); debouncedSpeak(resultsFoundMessage, "polite"); }, [filterValue, styleFilter, hasResults, debouncedSpeak]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "div", { ref: blockTypesListRef, className: "global-styles-ui-block-types-item-list", role: "list", children: filteredBlockTypes.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)( EmptyBlockList, { filterValue, styleFilter } ) : filteredBlockTypes.map((block) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)( BlockMenuItem, { block, isCustomized: customizedBlockNames.has(block.name) }, "menu-itemblock-" + block.name )) } ); } var MemoizedBlockList = (0, import_element.memo)(BlockList); function ScreenBlockList() { const [filterValue, setFilterValue] = (0, import_element.useState)(""); const [styleFilter, setStyleFilter] = (0, import_element.useState)("all"); const deferredFilterValue = (0, import_element.useDeferredValue)(filterValue); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_screen_header.ScreenHeader, { title: (0, import_i18n.__)("Blocks"), description: (0, import_i18n.__)( "Customize the appearance of specific blocks and for the whole site." ) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( import_components.__experimentalHStack, { className: "global-styles-ui-block-types-filter", alignment: "center", spacing: 2, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.SearchControl, { className: "global-styles-ui-block-types-search", onChange: setFilterValue, value: filterValue, label: (0, import_i18n.__)("Search"), placeholder: (0, import_i18n.__)("Search"), size: "compact" } ), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Menu, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( Menu.TriggerButton, { render: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Button, { size: "compact", icon: import_icons.funnel, label: (0, import_i18n.__)("Filter blocks"), isPressed: styleFilter !== "all" } ) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Menu.Popover, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( Menu.RadioItem, { name: "global-styles-block-filter", value: "all", checked: styleFilter === "all", onChange: () => setStyleFilter("all"), hideOnClick: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Menu.ItemLabel, { children: (0, import_i18n.__)("All blocks") }) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( Menu.RadioItem, { name: "global-styles-block-filter", value: "customized", checked: styleFilter === "customized", onChange: () => setStyleFilter("customized"), hideOnClick: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Menu.ItemLabel, { children: (0, import_i18n.__)("Customized") }) } ) ] }) ] }) ] } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( MemoizedBlockList, { filterValue: deferredFilterValue, styleFilter } ) ] }); } var screen_block_list_default = ScreenBlockList; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { hasUserStylesForBlock, useBlockHasGlobalStyles }); //# sourceMappingURL=screen-block-list.cjs.map