UNPKG

@wordpress/block-library

Version:
156 lines (154 loc) 4.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = TemplatePartInnerBlocks; var _coreData = require("@wordpress/core-data"); var _blockEditor = require("@wordpress/block-editor"); var _data = require("@wordpress/data"); var _element = require("@wordpress/element"); var _blocks = require("@wordpress/blocks"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ function useRenderAppender(hasInnerBlocks) { const blockEditingMode = (0, _blockEditor.useBlockEditingMode)(); // Disable appending when the editing mode is 'contentOnly'. This is so that the user can't // append into a template part when editing a page in the site editor. See // DisableNonPageContentBlocks. Ideally instead of (mis)using editing mode there would be a // block editor API for achieving this. if (blockEditingMode === 'contentOnly') { return false; } if (!hasInnerBlocks) { return _blockEditor.InnerBlocks.ButtonBlockAppender; } } function useLayout(layout) { const themeSupportsLayout = (0, _data.useSelect)(select => { const { getSettings } = select(_blockEditor.store); return getSettings()?.supportsLayout; }, []); const [defaultLayout] = (0, _blockEditor.useSettings)('layout'); if (themeSupportsLayout) { return layout?.inherit ? defaultLayout || {} : layout; } } function NonEditableTemplatePartPreview({ postId: id, layout, tagName: TagName, blockProps }) { (0, _blockEditor.useBlockEditingMode)('disabled'); const { content, editedBlocks } = (0, _data.useSelect)(select => { if (!id) { return {}; } const { getEditedEntityRecord } = select(_coreData.store); const editedRecord = getEditedEntityRecord('postType', 'wp_template_part', id, { context: 'view' }); return { editedBlocks: editedRecord.blocks, content: editedRecord.content }; }, [id]); const blocks = (0, _element.useMemo)(() => { if (!id) { return undefined; } if (editedBlocks) { return editedBlocks; } if (!content || typeof content !== 'string') { return []; } return (0, _blocks.parse)(content); }, [id, editedBlocks, content]); const innerBlocksProps = (0, _blockEditor.useInnerBlocksProps)(blockProps, { value: blocks, onInput: () => {}, onChange: () => {}, renderAppender: false, layout: useLayout(layout) }); return /*#__PURE__*/(0, _jsxRuntime.jsx)(TagName, { ...innerBlocksProps }); } function EditableTemplatePartInnerBlocks({ postId: id, hasInnerBlocks, layout, tagName: TagName, blockProps }) { const onNavigateToEntityRecord = (0, _data.useSelect)(select => select(_blockEditor.store).getSettings().onNavigateToEntityRecord, []); const [blocks, onInput, onChange] = (0, _coreData.useEntityBlockEditor)('postType', 'wp_template_part', { id }); const innerBlocksProps = (0, _blockEditor.useInnerBlocksProps)(blockProps, { value: blocks, onInput, onChange, renderAppender: useRenderAppender(hasInnerBlocks), layout: useLayout(layout) }); const blockEditingMode = (0, _blockEditor.useBlockEditingMode)(); const customProps = blockEditingMode === 'contentOnly' && onNavigateToEntityRecord ? { onDoubleClick: () => onNavigateToEntityRecord({ postId: id, postType: 'wp_template_part' }) } : {}; return /*#__PURE__*/(0, _jsxRuntime.jsx)(TagName, { ...innerBlocksProps, ...customProps }); } function TemplatePartInnerBlocks({ postId: id, hasInnerBlocks, layout, tagName: TagName, blockProps }) { const { canViewTemplatePart, canEditTemplatePart } = (0, _data.useSelect)(select => { return { canViewTemplatePart: !!select(_coreData.store).canUser('read', { kind: 'postType', name: 'wp_template_part', id }), canEditTemplatePart: !!select(_coreData.store).canUser('update', { kind: 'postType', name: 'wp_template_part', id }) }; }, [id]); if (!canViewTemplatePart) { return null; } const TemplatePartInnerBlocksComponent = canEditTemplatePart ? EditableTemplatePartInnerBlocks : NonEditableTemplatePartPreview; return /*#__PURE__*/(0, _jsxRuntime.jsx)(TemplatePartInnerBlocksComponent, { postId: id, hasInnerBlocks: hasInnerBlocks, layout: layout, tagName: TagName, blockProps: blockProps }); } //# sourceMappingURL=inner-blocks.js.map