UNPKG

@wordpress/block-library

Version:
165 lines (164 loc) • 6.76 kB
import clsx from "clsx"; import { useDispatch, useSelect } from "@wordpress/data"; import { useMemo, useRef } from "@wordpress/element"; import { store } from "@wordpress/core-data"; import { Placeholder, Spinner, ToolbarButton, ToolbarGroup } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { BlockControls, InnerBlocks, RecursionProvider, Warning, privateApis, store as store$1, useBlockProps, useHasRecursion, useInnerBlocksProps } from "@wordpress/block-editor"; import { privateApis as privateApis$1 } from "@wordpress/patterns"; import { getBlockBindingsSource, parse } from "@wordpress/blocks"; import { unlock } from "../lock-unlock.mjs"; import EditOriginalInspectorButton from "../utils/edit-original-inspector-button.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/block/edit.jsx const { useLayoutClasses } = unlock(privateApis); const { isOverridableBlock } = unlock(privateApis$1); const fullAlignments = [ "full", "wide", "left", "right" ]; const useInferredLayout = (blocks, parentLayout) => { const initialInferredAlignmentRef = useRef(); return useMemo(() => { if (!blocks?.length) return {}; let alignment = initialInferredAlignmentRef.current; if (alignment === void 0) { const isConstrained = parentLayout?.type === "constrained"; const hasFullAlignment = blocks.some((block) => fullAlignments.includes(block.attributes.align)); alignment = isConstrained && hasFullAlignment ? "full" : null; initialInferredAlignmentRef.current = alignment; } return { alignment, layout: alignment ? parentLayout : void 0 }; }, [blocks, parentLayout]); }; function RecursionWarning() { const blockProps = useBlockProps(); return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(Warning, { children: __("Block cannot be rendered inside itself.") }) }); } const NOOP = () => {}; function ReusableBlockEditRecursionWrapper(props) { const { ref } = props.attributes; if (useHasRecursion(ref)) return /* @__PURE__ */ jsx(RecursionWarning, {}); return /* @__PURE__ */ jsx(RecursionProvider, { uniqueId: ref, children: /* @__PURE__ */ jsx(ReusableBlockEdit, { ...props }) }); } function ReusableBlockControl({ recordId, canOverrideBlocks, hasContent, handleEditOriginal, resetContent }) { const canUserEdit = useSelect((select) => !!select(store).canUser("update", { kind: "postType", name: "wp_block", id: recordId }), [recordId]); return /* @__PURE__ */ jsxs(Fragment, { children: [canUserEdit && !!handleEditOriginal && /* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx(ToolbarButton, { onClick: handleEditOriginal, children: __("Edit original") }) }) }), canOverrideBlocks && /* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx(ToolbarButton, { onClick: resetContent, disabled: !hasContent, children: __("Reset") }) }) })] }); } const EMPTY_OBJECT = {}; const EMPTY_ARRAY = []; function ReusableBlockEdit({ name, clientId, attributes: { ref, content }, __unstableParentLayout: parentLayout, setAttributes }) { const { record, isResolving } = useSelect((select) => { if (select(store).hasEntityRecord("postType", "wp_block", ref)) return { record: select(store).getEditedEntityRecord("postType", "wp_block", ref), isResolving: false }; const args = [ "postType", "wp_block", ref, { context: "view" } ]; const viewRecord = select(store).getEntityRecord(...args); return { record: viewRecord, isResolving: !viewRecord && !select(store).hasFinishedResolution("getEntityRecord", args) }; }, [ref]); const patternContent = record?.content?.raw ?? record?.content; const blocks = useMemo(() => { if (record?.blocks) return record.blocks; return typeof patternContent === "string" && patternContent ? parse(patternContent) : EMPTY_ARRAY; }, [record?.blocks, patternContent]); const isMissing = !isResolving && !record; const { __unstableMarkLastChangeAsPersistent } = useDispatch(store$1); const { onNavigateToEntityRecord, hasPatternOverridesSource, supportedBlockTypesRaw } = useSelect((select) => { const { getSettings } = select(store$1); return { onNavigateToEntityRecord: getSettings().onNavigateToEntityRecord, hasPatternOverridesSource: !!getBlockBindingsSource("core/pattern-overrides"), supportedBlockTypesRaw: getSettings().__experimentalBlockBindingsSupportedAttributes || EMPTY_OBJECT }; }, []); const canOverrideBlocks = useMemo(() => { const supportedBlockTypes = Object.keys(supportedBlockTypesRaw); const hasOverridableBlocks = (_blocks) => _blocks?.some((block) => { if (supportedBlockTypes.includes(block.name) && isOverridableBlock(block)) return true; return hasOverridableBlocks(block.innerBlocks); }); return hasPatternOverridesSource && hasOverridableBlocks(blocks); }, [ hasPatternOverridesSource, blocks, supportedBlockTypesRaw ]); const { alignment, layout } = useInferredLayout(blocks, parentLayout); const layoutClasses = useLayoutClasses({ layout }, name); const blockProps = useBlockProps({ className: clsx("block-library-block__reusable-block-container", layout && layoutClasses, { [`align${alignment}`]: alignment }) }); const innerBlocksProps = useInnerBlocksProps(blockProps, { layout, value: blocks, onInput: NOOP, onChange: NOOP, renderAppender: blocks?.length ? void 0 : InnerBlocks.ButtonBlockAppender }); const handleEditOriginal = () => { onNavigateToEntityRecord({ postId: ref, postType: "wp_block" }); }; const resetContent = () => { if (content) { __unstableMarkLastChangeAsPersistent(); setAttributes({ content: void 0 }); } }; let children = null; if (isMissing) children = /* @__PURE__ */ jsx(Warning, { children: __("Block has been deleted or is unavailable.") }); if (isResolving) children = /* @__PURE__ */ jsx(Placeholder, { children: /* @__PURE__ */ jsx(Spinner, {}) }); return /* @__PURE__ */ jsxs(Fragment, { children: [!isResolving && !isMissing && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(EditOriginalInspectorButton, { clientId, postType: "wp_block", postId: ref }), /* @__PURE__ */ jsx(ReusableBlockControl, { recordId: ref, canOverrideBlocks, hasContent: !!content, handleEditOriginal: onNavigateToEntityRecord ? handleEditOriginal : void 0, resetContent })] }), children === null ? /* @__PURE__ */ jsx("div", { ...innerBlocksProps }) : /* @__PURE__ */ jsx("div", { ...blockProps, children })] }); } export { ReusableBlockEditRecursionWrapper as default }; //# sourceMappingURL=edit.mjs.map