UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 13.6 kB
{ "version": 3, "sources": ["../../../src/components/inner-blocks/index.js"], "sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\n\n/**\n * WordPress dependencies\n */\nimport { useMergeRefs } from '@wordpress/compose';\nimport { forwardRef, useMemo, memo } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\nimport {\n\tgetBlockSupport,\n\tstore as blocksStore,\n\t__unstableGetInnerBlocksProps as getInnerBlocksProps,\n} from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport ButtonBlockAppender from './button-block-appender';\nimport DefaultBlockAppender from './default-block-appender';\nimport useNestedSettingsUpdate from './use-nested-settings-update';\nimport useInnerBlockTemplateSync from './use-inner-block-template-sync';\nimport useBlockContext from './use-block-context';\nimport { BlockListItems } from '../block-list';\nimport { BlockContextProvider } from '../block-context';\nimport { useBlockEditContext } from '../block-edit/context';\nimport useBlockSync from '../provider/use-block-sync';\nimport { store as blockEditorStore } from '../../store';\nimport useBlockDropZone from '../use-block-drop-zone';\nimport { unlock } from '../../lock-unlock';\n\nconst EMPTY_OBJECT = {};\n\nfunction BlockContext( { children, clientId } ) {\n\tconst context = useBlockContext( clientId );\n\treturn (\n\t\t<BlockContextProvider value={ context }>\n\t\t\t{ children }\n\t\t</BlockContextProvider>\n\t);\n}\n\nconst BlockListItemsMemo = memo( BlockListItems );\n\n/**\n * InnerBlocks is a component which allows a single block to have multiple blocks\n * as children. The UncontrolledInnerBlocks component is used whenever the inner\n * blocks are not controlled by another entity. In other words, it is normally\n * used for inner blocks in the post editor\n *\n * @param {Object} props The component props.\n */\nfunction UncontrolledInnerBlocks( props ) {\n\tconst {\n\t\tclientId,\n\t\tallowedBlocks,\n\t\tprioritizedInserterBlocks,\n\t\tdefaultBlock,\n\t\tdirectInsert,\n\t\t__experimentalDefaultBlock,\n\t\t__experimentalDirectInsert,\n\t\ttemplate,\n\t\ttemplateLock,\n\t\twrapperRef,\n\t\ttemplateInsertUpdatesSelection,\n\t\t__experimentalCaptureToolbars: captureToolbars,\n\t\t__experimentalAppenderTagName,\n\t\trenderAppender,\n\t\torientation,\n\t\tplaceholder,\n\t\tlayout,\n\t\tname,\n\t\tblockType,\n\t\tparentLock,\n\t\tdefaultLayout,\n\t} = props;\n\n\tuseNestedSettingsUpdate(\n\t\tclientId,\n\t\tparentLock,\n\t\tallowedBlocks,\n\t\tprioritizedInserterBlocks,\n\t\tdefaultBlock,\n\t\tdirectInsert,\n\t\t__experimentalDefaultBlock,\n\t\t__experimentalDirectInsert,\n\t\ttemplateLock,\n\t\tcaptureToolbars,\n\t\torientation,\n\t\tlayout\n\t);\n\n\tuseInnerBlockTemplateSync(\n\t\tclientId,\n\t\ttemplate,\n\t\ttemplateLock,\n\t\ttemplateInsertUpdatesSelection\n\t);\n\n\tconst defaultLayoutBlockSupport =\n\t\tgetBlockSupport( name, 'layout' ) ||\n\t\tgetBlockSupport( name, '__experimentalLayout' ) ||\n\t\tEMPTY_OBJECT;\n\n\tconst { allowSizingOnChildren = false } = defaultLayoutBlockSupport;\n\tconst usedLayout = layout || defaultLayoutBlockSupport;\n\n\tconst memoedLayout = useMemo(\n\t\t() => ( {\n\t\t\t// Default layout will know about any content/wide size defined by the theme.\n\t\t\t...defaultLayout,\n\t\t\t...usedLayout,\n\t\t\t...( allowSizingOnChildren && {\n\t\t\t\tallowSizingOnChildren: true,\n\t\t\t} ),\n\t\t} ),\n\t\t[ defaultLayout, usedLayout, allowSizingOnChildren ]\n\t);\n\n\t// For controlled inner blocks, we don't want a change in blocks to\n\t// re-render the blocks list.\n\tconst items = (\n\t\t<BlockListItemsMemo\n\t\t\trootClientId={ clientId }\n\t\t\trenderAppender={ renderAppender }\n\t\t\t__experimentalAppenderTagName={ __experimentalAppenderTagName }\n\t\t\tlayout={ memoedLayout }\n\t\t\twrapperRef={ wrapperRef }\n\t\t\tplaceholder={ placeholder }\n\t\t/>\n\t);\n\n\tif (\n\t\t! blockType?.providesContext ||\n\t\tObject.keys( blockType.providesContext ).length === 0\n\t) {\n\t\treturn items;\n\t}\n\n\treturn <BlockContext clientId={ clientId }>{ items }</BlockContext>;\n}\n\n/**\n * The controlled inner blocks component wraps the uncontrolled inner blocks\n * component with the blockSync hook. This keeps the innerBlocks of the block in\n * the block-editor store in sync with the blocks of the controlling entity. An\n * example of an inner block controller is a template part block, which provides\n * its own blocks from the template part entity data source.\n *\n * @param {Object} props The component props.\n */\nfunction ControlledInnerBlocks( props ) {\n\tuseBlockSync( props );\n\treturn <UncontrolledInnerBlocks { ...props } />;\n}\n\nconst ForwardedInnerBlocks = forwardRef( ( props, ref ) => {\n\tconst innerBlocksProps = useInnerBlocksProps( { ref }, props );\n\treturn (\n\t\t<div className=\"block-editor-inner-blocks\">\n\t\t\t<div { ...innerBlocksProps } />\n\t\t</div>\n\t);\n} );\n\n/**\n * This hook is used to lightly mark an element as an inner blocks wrapper\n * element. Call this hook and pass the returned props to the element to mark as\n * an inner blocks wrapper, automatically rendering inner blocks as children. If\n * you define a ref for the element, it is important to pass the ref to this\n * hook, which the hook in turn will pass to the component through the props it\n * returns. Optionally, you can also pass any other props through this hook, and\n * they will be merged and returned.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/inner-blocks/README.md\n *\n * @param {Object} props Optional. Props to pass to the element. Must contain\n * the ref if one is defined.\n * @param {Object} options Optional. Inner blocks options.\n */\nexport function useInnerBlocksProps( props = {}, options = {} ) {\n\tconst {\n\t\t__unstableDisableLayoutClassNames,\n\t\t__unstableDisableDropZone,\n\t\tdropZoneElement,\n\t} = options;\n\tconst {\n\t\tclientId,\n\t\tlayout = null,\n\t\t__unstableLayoutClassNames: layoutClassNames = '',\n\t} = useBlockEditContext();\n\tconst selected = useSelect(\n\t\t( select ) => {\n\t\t\tconst {\n\t\t\t\tgetBlockName,\n\t\t\t\tisZoomOut,\n\t\t\t\tgetTemplateLock,\n\t\t\t\tgetBlockRootClientId,\n\t\t\t\tgetBlockEditingMode,\n\t\t\t\tgetBlockSettings,\n\t\t\t\tgetSectionRootClientId,\n\t\t\t} = unlock( select( blockEditorStore ) );\n\n\t\t\tif ( ! clientId ) {\n\t\t\t\tconst sectionRootClientId = getSectionRootClientId();\n\t\t\t\t// Disable the root drop zone when zoomed out and the section root client id\n\t\t\t\t// is not the root block list (represented by an empty string).\n\t\t\t\t// This avoids drag handling bugs caused by having two block lists acting as\n\t\t\t\t// drop zones - the actual 'root' block list and the section root.\n\t\t\t\treturn {\n\t\t\t\t\tisDropZoneDisabled:\n\t\t\t\t\t\tisZoomOut() && sectionRootClientId !== '',\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst { hasBlockSupport, getBlockType } = select( blocksStore );\n\t\t\tconst blockName = getBlockName( clientId );\n\t\t\tconst blockEditingMode = getBlockEditingMode( clientId );\n\t\t\tconst parentClientId = getBlockRootClientId( clientId );\n\t\t\tconst [ defaultLayout ] = getBlockSettings( clientId, 'layout' );\n\n\t\t\tlet _isDropZoneDisabled = blockEditingMode === 'disabled';\n\n\t\t\tif ( isZoomOut() ) {\n\t\t\t\t// In zoom out mode, we want to disable the drop zone for the sections.\n\t\t\t\t// The inner blocks belonging to the section drop zone is\n\t\t\t\t// already disabled by the blocks themselves being disabled.\n\t\t\t\tconst sectionRootClientId = getSectionRootClientId();\n\t\t\t\t_isDropZoneDisabled = clientId !== sectionRootClientId;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t__experimentalCaptureToolbars: hasBlockSupport(\n\t\t\t\t\tblockName,\n\t\t\t\t\t'__experimentalExposeControlsToChildren',\n\t\t\t\t\tfalse\n\t\t\t\t),\n\t\t\t\tname: blockName,\n\t\t\t\tblockType: getBlockType( blockName ),\n\t\t\t\tparentLock: getTemplateLock( parentClientId ),\n\t\t\t\tparentClientId,\n\t\t\t\tisDropZoneDisabled: _isDropZoneDisabled,\n\t\t\t\tdefaultLayout,\n\t\t\t};\n\t\t},\n\t\t[ clientId ]\n\t);\n\tconst {\n\t\t__experimentalCaptureToolbars,\n\t\tname,\n\t\tblockType,\n\t\tparentLock,\n\t\tparentClientId,\n\t\tisDropZoneDisabled,\n\t\tdefaultLayout,\n\t} = selected;\n\n\tconst blockDropZoneRef = useBlockDropZone( {\n\t\tdropZoneElement,\n\t\trootClientId: clientId,\n\t\tparentClientId,\n\t} );\n\n\tconst ref = useMergeRefs( [\n\t\tprops.ref,\n\t\t__unstableDisableDropZone ||\n\t\tisDropZoneDisabled ||\n\t\t( layout?.isManualPlacement &&\n\t\t\twindow.__experimentalEnableGridInteractivity )\n\t\t\t? null\n\t\t\t: blockDropZoneRef,\n\t] );\n\n\tconst innerBlocksProps = {\n\t\t__experimentalCaptureToolbars,\n\t\tlayout,\n\t\tname,\n\t\tblockType,\n\t\tparentLock,\n\t\tdefaultLayout,\n\t\t...options,\n\t};\n\tconst InnerBlocks =\n\t\tinnerBlocksProps.value && innerBlocksProps.onChange\n\t\t\t? ControlledInnerBlocks\n\t\t\t: UncontrolledInnerBlocks;\n\n\treturn {\n\t\t...props,\n\t\tref,\n\t\tclassName: clsx(\n\t\t\tprops.className,\n\t\t\t'block-editor-block-list__layout',\n\t\t\t__unstableDisableLayoutClassNames ? '' : layoutClassNames\n\t\t),\n\t\tchildren: clientId ? (\n\t\t\t<InnerBlocks { ...innerBlocksProps } clientId={ clientId } />\n\t\t) : (\n\t\t\t<BlockListItems { ...options } />\n\t\t),\n\t};\n}\n\nuseInnerBlocksProps.save = getInnerBlocksProps;\n\n// Expose default appender placeholders as components.\nForwardedInnerBlocks.DefaultBlockAppender = DefaultBlockAppender;\nForwardedInnerBlocks.ButtonBlockAppender = ButtonBlockAppender;\n\nForwardedInnerBlocks.Content = () => useInnerBlocksProps.save().children;\n\n/**\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/inner-blocks/README.md\n */\nexport default ForwardedInnerBlocks;\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;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,oCAAiB,GAAG,SAAU;AAAA,EAEjC;AACD;AAEA,oBAAoB,OAAO,cAAAC;AAG3B,qBAAqB,uBAAuB,8BAAAC;AAC5C,qBAAqB,sBAAsB,6BAAAC;AAE3C,qBAAqB,UAAU,MAAM,oBAAoB,KAAK,EAAE;AAKhE,IAAO,uBAAQ;", "names": ["useBlockContext", "useNestedSettingsUpdate", "useInnerBlockTemplateSync", "useBlockSync", "blockEditorStore", "blocksStore", "parentClientId", "defaultLayout", "useBlockDropZone", "clsx", "getInnerBlocksProps", "DefaultBlockAppender", "ButtonBlockAppender"] }