UNPKG

@wordpress/global-styles-ui

Version:
291 lines (290 loc) • 9.45 kB
// packages/global-styles-ui/src/screen-block-list.tsx import { store as blocksStore } from "@wordpress/blocks"; import { __, sprintf, _n } from "@wordpress/i18n"; import { Button, FlexItem, privateApis as componentsPrivateApis, SearchControl, __experimentalHStack as HStack, __experimentalText as WCText } from "@wordpress/components"; import { VisuallyHidden } from "@wordpress/ui"; import { useSelect } from "@wordpress/data"; import { useState, useContext, useEffect, useMemo, useRef, useDeferredValue, memo } from "@wordpress/element"; import { BlockIcon, privateApis as blockEditorPrivateApis } from "@wordpress/block-editor"; import { useDebounce } from "@wordpress/compose"; import { speak } from "@wordpress/a11y"; import { funnel } from "@wordpress/icons"; import { useBlockVariations } from "./variations/variations-panel.mjs"; import { ScreenHeader } from "./screen-header.mjs"; import { NavigationButtonAsItem } from "./navigation-button.mjs"; import { useSetting } from "./hooks.mjs"; import { unlock } from "./lock-unlock.mjs"; import { GlobalStylesContext } from "./context.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; var { useHasDimensionsPanel, useHasTypographyPanel, useHasBorderPanel, useSettingsForBlockElement, useHasColorPanel, useHasBackgroundPanel } = unlock(blockEditorPrivateApis); var { Menu } = unlock(componentsPrivateApis); 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 = useSelect( (select) => select(blocksStore).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] = 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 = !!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__ */ jsx( NavigationButtonAsItem, { path: "/blocks/" + encodeURIComponent(block.name), children: /* @__PURE__ */ jsxs(HStack, { justify: "flex-start", spacing: 2, children: [ /* @__PURE__ */ jsx( BlockIcon, { className: "global-styles-ui-block-types-item__icon", icon: block.icon } ), /* @__PURE__ */ jsx(FlexItem, { children: block.title }), isCustomized && /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(VisuallyHidden, { children: __("Has custom styles") }), /* @__PURE__ */ jsx( "span", { "aria-hidden": "true", className: "global-styles-ui-block-types-item__indicator" } ) ] }) ] }) } ); } function EmptyBlockList({ filterValue, styleFilter }) { const label = "customized" === styleFilter && !filterValue ? __("You haven't customized any blocks yet.") : __("No blocks found."); return /* @__PURE__ */ jsx( WCText, { 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 = useDebounce(speak, 500); const { isMatchingSearchTerm } = useSelect(blocksStore); const { user } = useContext(GlobalStylesContext); const customizedBlockNames = 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 = useRef(null); const hasResults = filteredBlockTypes.length > 0; useEffect(() => { if (!filterValue && styleFilter === "all") { return; } const count = hasResults ? blockTypesListRef.current?.childElementCount || 0 : 0; const resultsFoundMessage = sprintf( /* translators: %d: number of results. */ _n("%d result found.", "%d results found.", count), count ); debouncedSpeak(resultsFoundMessage, "polite"); }, [filterValue, styleFilter, hasResults, debouncedSpeak]); return /* @__PURE__ */ jsx( "div", { ref: blockTypesListRef, className: "global-styles-ui-block-types-item-list", role: "list", children: filteredBlockTypes.length === 0 ? /* @__PURE__ */ jsx( EmptyBlockList, { filterValue, styleFilter } ) : filteredBlockTypes.map((block) => /* @__PURE__ */ jsx( BlockMenuItem, { block, isCustomized: customizedBlockNames.has(block.name) }, "menu-itemblock-" + block.name )) } ); } var MemoizedBlockList = memo(BlockList); function ScreenBlockList() { const [filterValue, setFilterValue] = useState(""); const [styleFilter, setStyleFilter] = useState("all"); const deferredFilterValue = useDeferredValue(filterValue); return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx( ScreenHeader, { title: __("Blocks"), description: __( "Customize the appearance of specific blocks and for the whole site." ) } ), /* @__PURE__ */ jsxs( HStack, { className: "global-styles-ui-block-types-filter", alignment: "center", spacing: 2, children: [ /* @__PURE__ */ jsx( SearchControl, { className: "global-styles-ui-block-types-search", onChange: setFilterValue, value: filterValue, label: __("Search"), placeholder: __("Search"), size: "compact" } ), /* @__PURE__ */ jsxs(Menu, { children: [ /* @__PURE__ */ jsx( Menu.TriggerButton, { render: /* @__PURE__ */ jsx( Button, { size: "compact", icon: funnel, label: __("Filter blocks"), isPressed: styleFilter !== "all" } ) } ), /* @__PURE__ */ jsxs(Menu.Popover, { children: [ /* @__PURE__ */ jsx( Menu.RadioItem, { name: "global-styles-block-filter", value: "all", checked: styleFilter === "all", onChange: () => setStyleFilter("all"), hideOnClick: true, children: /* @__PURE__ */ jsx(Menu.ItemLabel, { children: __("All blocks") }) } ), /* @__PURE__ */ jsx( Menu.RadioItem, { name: "global-styles-block-filter", value: "customized", checked: styleFilter === "customized", onChange: () => setStyleFilter("customized"), hideOnClick: true, children: /* @__PURE__ */ jsx(Menu.ItemLabel, { children: __("Customized") }) } ) ] }) ] }) ] } ), /* @__PURE__ */ jsx( MemoizedBlockList, { filterValue: deferredFilterValue, styleFilter } ) ] }); } var screen_block_list_default = ScreenBlockList; export { screen_block_list_default as default, hasUserStylesForBlock, useBlockHasGlobalStyles }; //# sourceMappingURL=screen-block-list.mjs.map