Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions packages/block-library/src/cover/edit/block-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { MenuItem } from '@wordpress/components';
import { link } from '@wordpress/icons';
import { MenuItem, ToolbarButton } from '@wordpress/components';
import { crop, link } from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -31,6 +31,9 @@ export default function CoverBlockControls( {
toggleUseFeaturedImage,
onClearMedia,
onSelectEmbedUrl,
onEditMedia,
editMediaButtonRef,
showEditMediaButton,
blockEditingMode,
} ) {
const {
Expand Down Expand Up @@ -106,6 +109,15 @@ export default function CoverBlockControls( {
onToggle={ toggleMinFullHeight }
isDisabled={ ! hasInnerBlocks }
/>
{ showEditMediaButton && (
<ToolbarButton
ref={ editMediaButtonRef }
icon={ crop }
label={ __( 'Crop background image' ) }
Comment thread
andrewserong marked this conversation as resolved.
onClick={ onEditMedia }
aria-haspopup="dialog"
/>
) }
</BlockControls>
) }
<BlockControls group="other">
Expand Down
86 changes: 86 additions & 0 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import clsx from 'clsx';
*/
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
Expand All @@ -24,6 +25,7 @@ import {
__experimentalUseGradient,
store as blockEditorStore,
useBlockEditingMode,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -55,6 +57,9 @@ import {
} from './color-utils';
import { DEFAULT_MEDIA_SIZE_SLUG } from '../constants';
import { getBackgroundEmbedHtml } from '../embed-video-utils';
import { unlock } from '../../lock-unlock';

const { openMediaEditorModalKey } = unlock( blockEditorPrivateApis );

function getInnerBlocksTemplate( attributes ) {
return [
Expand Down Expand Up @@ -123,6 +128,11 @@ function CoverEdit( {
postId
);
const { getSettings } = useSelect( blockEditorStore );
const openMediaEditorModal = useSelect(
( select ) =>
select( blockEditorStore ).getSettings()[ openMediaEditorModalKey ],
[]
);

const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
Expand Down Expand Up @@ -490,6 +500,7 @@ function CoverEdit( {
);

const mediaElement = useRef();
const editMediaButtonRef = useRef();
const currentSettings = {
isVideoBackground,
isImageBackground,
Expand All @@ -500,6 +511,78 @@ function CoverEdit( {
overlayColor,
};

const openCoverMediaEditorModal = useCallback( () => {
if ( ! id || ! openMediaEditorModal ) {
return;
}

openMediaEditorModal( {
id,
onClose: () => {
editMediaButtonRef.current?.focus();
},
onUpdate: async ( { id: newId, url: newUrl } ) => {
if ( typeof newId !== 'number' ) {
return;
}

const nextAttributes = {
id: newId,
backgroundType: IMAGE_BACKGROUND_TYPE,
...( newUrl ? { url: newUrl } : {} ),
...( newId !== id
? { sizeSlug: DEFAULT_MEDIA_SIZE_SLUG }
: {} ),
};

if ( newUrl ) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the change targeted to the new media editor update path rather than refactoring the existing Cover media-selection logic. I tried the latter first, but wasn't confident about it affecting existing flows.

Maybe a follow up to unify, but it'd be a behavioral refactor so I'm not sure I'd want to make this PR more complicated.

There's a fair bit going on, e.g., overlay color, isDark, isUserOverlayColor, undo persistence, and sometimes dim ratio so any shared helper could trigger unintended side effects.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping it contained to this flow sounds wise, then 👍

const averageBackgroundColor =
await getMediaColor( newUrl );

// Read latest values after await to avoid stale closures.
const {
attributes: currentAttrs,
overlayColor: currentOverlay,
} = propsRef.current;

let newOverlayColor = currentOverlay.color;
if ( ! currentAttrs.isUserOverlayColor ) {
newOverlayColor = averageBackgroundColor;
setOverlayColor( newOverlayColor );

// Make undo revert the next setAttributes and the previous setOverlayColor.
__unstableMarkNextChangeAsNotPersistent();
}

nextAttributes.isDark = compositeIsDark(
currentAttrs.dimRatio,
newOverlayColor,
averageBackgroundColor
);
nextAttributes.isUserOverlayColor =
currentAttrs.isUserOverlayColor || false;
}

setAttributes( nextAttributes );
},
} );
}, [
id,
openMediaEditorModal,
setAttributes,
setOverlayColor,
__unstableMarkNextChangeAsNotPersistent,
] );

const showEditMediaButton =
hasNonContentControls &&
! useFeaturedImage &&
isImageBackground &&
!! id &&
!! url &&
! isUploadingMedia &&
!! openMediaEditorModal;

const toggleUseFeaturedImage = async () => {
const newUseFeaturedImage = ! useFeaturedImage;

Expand Down Expand Up @@ -556,6 +639,9 @@ function CoverEdit( {
toggleUseFeaturedImage={ toggleUseFeaturedImage }
onClearMedia={ onClearMedia }
blockEditingMode={ blockEditingMode }
onEditMedia={ openCoverMediaEditorModal }
editMediaButtonRef={ editMediaButtonRef }
showEditMediaButton={ showEditMediaButton }
/>
);

Expand Down
Loading