UNPKG

@wordpress/block-editor

Version:
224 lines (189 loc) 8.27 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.useInnerBlocksProps = useInnerBlocksProps; var _element = require("@wordpress/element"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _classnames = _interopRequireDefault(require("classnames")); var _compose = require("@wordpress/compose"); var _data = require("@wordpress/data"); var _blocks = require("@wordpress/blocks"); var _buttonBlockAppender = _interopRequireDefault(require("./button-block-appender")); var _defaultBlockAppender = _interopRequireDefault(require("./default-block-appender")); var _useNestedSettingsUpdate = _interopRequireDefault(require("./use-nested-settings-update")); var _useInnerBlockTemplateSync = _interopRequireDefault(require("./use-inner-block-template-sync")); var _useBlockContext = _interopRequireDefault(require("./use-block-context")); var _blockList = require("../block-list"); var _blockContext = require("../block-context"); var _context = require("../block-edit/context"); var _useBlockSync = _interopRequireDefault(require("../provider/use-block-sync")); var _store = require("../../store"); var _useBlockDropZone = _interopRequireDefault(require("../use-block-drop-zone")); var _useSetting = _interopRequireDefault(require("../use-setting")); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ const EMPTY_OBJECT = {}; /** * InnerBlocks is a component which allows a single block to have multiple blocks * as children. The UncontrolledInnerBlocks component is used whenever the inner * blocks are not controlled by another entity. In other words, it is normally * used for inner blocks in the post editor * * @param {Object} props The component props. */ function UncontrolledInnerBlocks(props) { const { clientId, allowedBlocks, prioritizedInserterBlocks, __experimentalDefaultBlock, __experimentalDirectInsert, template, templateLock, wrapperRef, templateInsertUpdatesSelection, __experimentalCaptureToolbars: captureToolbars, __experimentalAppenderTagName, renderAppender, orientation, placeholder, layout } = props; (0, _useNestedSettingsUpdate.default)(clientId, allowedBlocks, prioritizedInserterBlocks, __experimentalDefaultBlock, __experimentalDirectInsert, templateLock, captureToolbars, orientation, layout); (0, _useInnerBlockTemplateSync.default)(clientId, template, templateLock, templateInsertUpdatesSelection); const context = (0, _useBlockContext.default)(clientId); const name = (0, _data.useSelect)(select => { return select(_store.store).getBlock(clientId)?.name; }, [clientId]); const defaultLayoutBlockSupport = (0, _blocks.getBlockSupport)(name, '__experimentalLayout') || EMPTY_OBJECT; const { allowSizingOnChildren = false } = defaultLayoutBlockSupport; const defaultLayout = (0, _useSetting.default)('layout') || EMPTY_OBJECT; const usedLayout = layout || defaultLayoutBlockSupport; const memoedLayout = (0, _element.useMemo)(() => ({ // Default layout will know about any content/wide size defined by the theme. ...defaultLayout, ...usedLayout, ...(allowSizingOnChildren && { allowSizingOnChildren: true }) }), [defaultLayout, usedLayout, allowSizingOnChildren]); // This component needs to always be synchronous as it's the one changing // the async mode depending on the block selection. return (0, _element.createElement)(_blockContext.BlockContextProvider, { value: context }, (0, _element.createElement)(_blockList.BlockListItems, { rootClientId: clientId, renderAppender: renderAppender, __experimentalAppenderTagName: __experimentalAppenderTagName, __experimentalLayout: memoedLayout, wrapperRef: wrapperRef, placeholder: placeholder })); } /** * The controlled inner blocks component wraps the uncontrolled inner blocks * component with the blockSync hook. This keeps the innerBlocks of the block in * the block-editor store in sync with the blocks of the controlling entity. An * example of an inner block controller is a template part block, which provides * its own blocks from the template part entity data source. * * @param {Object} props The component props. */ function ControlledInnerBlocks(props) { (0, _useBlockSync.default)(props); return (0, _element.createElement)(UncontrolledInnerBlocks, props); } const ForwardedInnerBlocks = (0, _element.forwardRef)((props, ref) => { const innerBlocksProps = useInnerBlocksProps({ ref }, props); return (0, _element.createElement)("div", { className: "block-editor-inner-blocks" }, (0, _element.createElement)("div", innerBlocksProps)); }); /** * This hook is used to lightly mark an element as an inner blocks wrapper * element. Call this hook and pass the returned props to the element to mark as * an inner blocks wrapper, automatically rendering inner blocks as children. If * you define a ref for the element, it is important to pass the ref to this * hook, which the hook in turn will pass to the component through the props it * returns. Optionally, you can also pass any other props through this hook, and * they will be merged and returned. * * @param {Object} props Optional. Props to pass to the element. Must contain * the ref if one is defined. * @param {Object} options Optional. Inner blocks options. * * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/inner-blocks/README.md */ function useInnerBlocksProps(props = {}, options = {}) { const { __unstableDisableLayoutClassNames, __unstableDisableDropZone } = options; const { clientId, layout = null, __unstableLayoutClassNames: layoutClassNames = '' } = (0, _context.useBlockEditContext)(); const isSmallScreen = (0, _compose.useViewportMatch)('medium', '<'); const { __experimentalCaptureToolbars, hasOverlay } = (0, _data.useSelect)(select => { if (!clientId) { return {}; } const { getBlockName, isBlockSelected, hasSelectedInnerBlock, __unstableGetEditorMode } = select(_store.store); const blockName = getBlockName(clientId); const enableClickThrough = __unstableGetEditorMode() === 'navigation' || isSmallScreen; return { __experimentalCaptureToolbars: select(_blocks.store).hasBlockSupport(blockName, '__experimentalExposeControlsToChildren', false), hasOverlay: blockName !== 'core/template' && !isBlockSelected(clientId) && !hasSelectedInnerBlock(clientId, true) && enableClickThrough }; }, [clientId, isSmallScreen]); const blockDropZoneRef = (0, _useBlockDropZone.default)({ rootClientId: clientId }); const ref = (0, _compose.useMergeRefs)([props.ref, __unstableDisableDropZone ? null : blockDropZoneRef]); const innerBlocksProps = { __experimentalCaptureToolbars, layout, ...options }; const InnerBlocks = innerBlocksProps.value && innerBlocksProps.onChange ? ControlledInnerBlocks : UncontrolledInnerBlocks; return { ...props, ref, className: (0, _classnames.default)(props.className, 'block-editor-block-list__layout', __unstableDisableLayoutClassNames ? '' : layoutClassNames, { 'has-overlay': hasOverlay }), children: clientId ? (0, _element.createElement)(InnerBlocks, (0, _extends2.default)({}, innerBlocksProps, { clientId: clientId })) : (0, _element.createElement)(_blockList.BlockListItems, options) }; } useInnerBlocksProps.save = _blocks.__unstableGetInnerBlocksProps; // Expose default appender placeholders as components. ForwardedInnerBlocks.DefaultBlockAppender = _defaultBlockAppender.default; ForwardedInnerBlocks.ButtonBlockAppender = _buttonBlockAppender.default; ForwardedInnerBlocks.Content = () => useInnerBlocksProps.save().children; /** * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/inner-blocks/README.md */ var _default = ForwardedInnerBlocks; exports.default = _default; //# sourceMappingURL=index.js.map