Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 73 additions & 33 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import {
withColors,
ColorPalette,
useBlockProps,
useSettings,
useInnerBlocksProps,
__experimentalUseGradient,
store as blockEditorStore,
useBlockEditingMode,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { createBlocksFromInnerBlocksTemplate } from '@wordpress/blocks';
import { isBlobURL } from '@wordpress/blob';
import { store as noticesStore } from '@wordpress/notices';

Expand Down Expand Up @@ -134,8 +134,9 @@ function CoverEdit( {
[]
);

const { __unstableMarkNextChangeAsNotPersistent } =
const { __unstableMarkNextChangeAsNotPersistent, replaceInnerBlocks } =
useDispatch( blockEditorStore );
const registry = useRegistry();

// Ref to access latest values after async operations (e.g. getMediaColor),
// avoiding stale values that could overwrite concurrent remote changes.
Expand Down Expand Up @@ -225,6 +226,35 @@ function CoverEdit( {
const { createErrorNotice } = useDispatch( noticesStore );
const { gradientClass, gradientValue } = __experimentalUseGradient();

// Create the initial inner paragraph when the block gains a background
// and has no content yet.
const scaffoldInnerBlocks = () => {
const {
getBlocks,
isBlockSelected,
getSelectedBlocksInitialCaretPosition,
} = registry.select( blockEditorStore );
if ( getBlocks( clientId ).length > 0 ) {
return;
}
// Check for fontSize support before we pass a fontSize attribute to
// the innerBlocks.
const [ fontSizes ] = unlock(
registry.select( blockEditorStore )
).getBlockSettings( clientId, 'typography.fontSizes' );
const hasFontSizes = fontSizes?.length > 0;
replaceInnerBlocks(
clientId,
createBlocksFromInnerBlocksTemplate(
getInnerBlocksTemplate( {
fontSize: hasFontSizes ? 'large' : undefined,
} )
),
isBlockSelected( clientId ),
getSelectedBlocksInitialCaretPosition()
);
};

const onSelectMedia = async ( newMedia ) => {
const mediaAttributes = attributesFromMedia( newMedia );
const isImage = [ newMedia?.type, newMedia?.media_type ].includes(
Expand Down Expand Up @@ -290,13 +320,17 @@ function CoverEdit( {
}
}

setAttributes( {
...mediaAttributes,
focalPoint: undefined,
useFeaturedImage: undefined,
dimRatio: newDimRatio,
isDark: newIsDark,
isUserOverlayColor: currentAttrs.isUserOverlayColor || false,
registry.batch( () => {
setAttributes( {
...mediaAttributes,
focalPoint: undefined,
useFeaturedImage: undefined,
dimRatio: newDimRatio,
isDark: newIsDark,
isUserOverlayColor: currentAttrs.isUserOverlayColor || false,
} );

scaffoldInnerBlocks();
} );
};

Expand Down Expand Up @@ -345,9 +379,17 @@ function CoverEdit( {
// Make undo revert the next setAttributes and the previous setOverlayColor.
__unstableMarkNextChangeAsNotPersistent();

setAttributes( {
isUserOverlayColor: true,
isDark: newIsDark,
registry.batch( () => {
setAttributes( {
isUserOverlayColor: true,
isDark: newIsDark,
} );

// Skip when the color is cleared: that returns the block to its
// placeholder, which only renders while there is no content.
if ( newOverlayColor ) {
scaffoldInnerBlocks();
}
} );
};

Expand Down Expand Up @@ -477,22 +519,11 @@ function CoverEdit( {
const ref = useRef();
const blockProps = useBlockProps( { ref } );

// Check for fontSize support before we pass a fontSize attribute to the innerBlocks.
const [ fontSizes ] = useSettings( 'typography.fontSizes' );
const hasFontSizes = fontSizes?.length > 0;
const innerBlocksTemplate = getInnerBlocksTemplate( {
fontSize: hasFontSizes ? 'large' : undefined,
} );

const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-cover__inner-container',
},
{
// Avoid template sync when the `templateLock` value is `all` or `contentOnly`.
// See: https://github.com/WordPress/gutenberg/pull/45632
template: ! hasInnerBlocks ? innerBlocksTemplate : undefined,
templateInsertUpdatesSelection: true,
allowedBlocks,
templateLock,
dropZoneElement: ref.current,
Expand Down Expand Up @@ -617,15 +648,24 @@ function CoverEdit( {
averageBackgroundColor
);

setAttributes( {
id: undefined,
url: undefined,
useFeaturedImage: newUseFeaturedImage,
dimRatio: newDimRatio,
backgroundType: useFeaturedImage
? IMAGE_BACKGROUND_TYPE
: undefined,
isDark: newIsDark,
registry.batch( () => {
setAttributes( {
id: undefined,
url: undefined,
useFeaturedImage: newUseFeaturedImage,
dimRatio: newDimRatio,
backgroundType: useFeaturedImage
? IMAGE_BACKGROUND_TYPE
: undefined,
isDark: newIsDark,
} );

// Skip when the featured image is disabled: that can return the
// block to its placeholder, which only renders while there is no
// content.
if ( newUseFeaturedImage ) {
scaffoldInnerBlocks();
}
} );
};

Expand Down
Loading