UNPKG

@wordpress/block-editor

Version:
240 lines (238 loc) 9.75 kB
"use strict"; 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 __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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/block-patterns-list/index.js var block_patterns_list_exports = {}; __export(block_patterns_list_exports, { default: () => block_patterns_list_default }); module.exports = __toCommonJS(block_patterns_list_exports); var import_clsx = __toESM(require("clsx")); var import_blocks = require("@wordpress/blocks"); var import_element = require("@wordpress/element"); var import_components = require("@wordpress/components"); var import_compose = require("@wordpress/compose"); var import_i18n = require("@wordpress/i18n"); var import_icons = require("@wordpress/icons"); var import_block_preview = __toESM(require("../block-preview")); var import_inserter_draggable_blocks = __toESM(require("../inserter-draggable-blocks")); var import_block_patterns_paging = __toESM(require("../block-patterns-paging")); var import_utils = require("../inserter/block-patterns-tab/utils"); var import_jsx_runtime = require("react/jsx-runtime"); var WithToolTip = ({ showTooltip, title, children }) => { if (showTooltip) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Tooltip, { text: title, children }); } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children }); }; function BlockPattern({ id, isDraggable, pattern, onClick, onHover, showTitlesAsTooltip, category, isSelected }) { const [isDragging, setIsDragging] = (0, import_element.useState)(false); const { blocks, viewportWidth } = pattern; const instanceId = (0, import_compose.useInstanceId)(BlockPattern); const descriptionId = `block-editor-block-patterns-list__item-description-${instanceId}`; const isUserPattern = pattern.type === import_utils.INSERTER_PATTERN_TYPES.user; const patternBlocks = (0, import_element.useMemo)(() => { if (!category || !isDraggable) { return blocks; } return (blocks ?? []).map((block) => { const clonedBlock = (0, import_blocks.cloneBlock)(block); if (clonedBlock.attributes.metadata?.categories?.includes( category )) { clonedBlock.attributes.metadata.categories = [category]; } return clonedBlock; }); }, [blocks, isDraggable, category]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_inserter_draggable_blocks.default, { isEnabled: isDraggable, blocks: patternBlocks, pattern, children: ({ draggable, onDragStart, onDragEnd }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "div", { className: "block-editor-block-patterns-list__list-item", draggable, onDragStart: (event) => { setIsDragging(true); if (onDragStart) { onHover?.(null); onDragStart(event); } }, onDragEnd: (event) => { setIsDragging(false); if (onDragEnd) { onDragEnd(event); } }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( WithToolTip, { showTooltip: showTitlesAsTooltip && !isUserPattern, title: pattern.title, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( import_components.Composite.Item, { render: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "div", { role: "option", "aria-label": pattern.title, "aria-describedby": pattern.description ? descriptionId : void 0, className: (0, import_clsx.default)( "block-editor-block-patterns-list__item", { "block-editor-block-patterns-list__list-item-synced": pattern.type === import_utils.INSERTER_PATTERN_TYPES.user && !pattern.syncStatus, "is-selected": isSelected } ) } ), id, onClick: () => { onClick(pattern, blocks); onHover?.(null); }, onMouseEnter: () => { if (isDragging) { return; } onHover?.(pattern); }, onMouseLeave: () => onHover?.(null), children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_block_preview.default.Async, { placeholder: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockPatternPlaceholder, {}), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_block_preview.default, { blocks, viewportWidth } ) } ), (!showTitlesAsTooltip || isUserPattern) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( import_components.__experimentalHStack, { className: "block-editor-patterns__pattern-details", spacing: 2, children: [ isUserPattern && !pattern.syncStatus && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-patterns__pattern-icon-wrapper", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_icons.Icon, { className: "block-editor-patterns__pattern-icon", icon: import_icons.symbol } ) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-block-patterns-list__item-title", children: pattern.title }) ] } ), !!pattern.description && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.VisuallyHidden, { id: descriptionId, children: pattern.description }) ] } ) } ) } ) } ); } function BlockPatternPlaceholder() { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-block-patterns-list__item is-placeholder" }); } function BlockPatternsList({ isDraggable, blockPatterns, onHover, onClickPattern, orientation, label = (0, import_i18n.__)("Block patterns"), category, showTitlesAsTooltip, pagingProps }, ref) { const [activeCompositeId, setActiveCompositeId] = (0, import_element.useState)(void 0); const [activePattern, setActivePattern] = (0, import_element.useState)(null); (0, import_element.useEffect)(() => { const firstCompositeItemId = blockPatterns[0]?.name; setActiveCompositeId(firstCompositeItemId); }, [blockPatterns]); const handleClickPattern = (pattern, blocks) => { setActivePattern(pattern.name); onClickPattern(pattern, blocks); }; return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( import_components.Composite, { orientation, activeId: activeCompositeId, setActiveId: setActiveCompositeId, role: "listbox", className: "block-editor-block-patterns-list", "aria-label": label, ref, children: [ blockPatterns.map((pattern) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)( BlockPattern, { id: pattern.name, pattern, onClick: handleClickPattern, onHover, isDraggable, showTitlesAsTooltip, category, isSelected: !!activePattern && activePattern === pattern.name }, pattern.name )), pagingProps && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_patterns_paging.default, { ...pagingProps }) ] } ); } var block_patterns_list_default = (0, import_element.forwardRef)(BlockPatternsList); //# sourceMappingURL=index.js.map