Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start with featured image in media placeholder #41722

Merged
merged 4 commits into from
Jul 6, 2022
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
19 changes: 19 additions & 0 deletions packages/block-editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function MediaPlaceholder( {
onSelect,
onCancel,
onSelectURL,
onToggleFeaturedImage,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure about this name. Would onSelectFeaturedImage be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think not, because we're not selecting, we're toggling the use of the featured image as a source.

onDoubleClick,
onFilesPreUpload = noop,
onHTMLDrop = noop,
Expand Down Expand Up @@ -307,6 +308,22 @@ export function MediaPlaceholder( {
);
};

const renderFeaturedImageToggle = () => {
return (
onToggleFeaturedImage && (
<div className="block-editor-media-placeholder__url-input-container">
<Button
className="block-editor-media-placeholder__button"
onClick={ onToggleFeaturedImage }
variant="tertiary"
>
{ __( 'Use featured image' ) }
</Button>
</div>
)
);
};

const renderMediaUploadChecked = () => {
const defaultButton = ( { open } ) => {
return (
Expand Down Expand Up @@ -361,6 +378,7 @@ export function MediaPlaceholder( {
</Button>
{ uploadMediaLibraryButton }
{ renderUrlSelectionUI() }
{ renderFeaturedImageToggle() }
{ renderCancelLink() }
</>
);
Expand Down Expand Up @@ -389,6 +407,7 @@ export function MediaPlaceholder( {
</FormFileUpload>
{ uploadMediaLibraryButton }
{ renderUrlSelectionUI() }
{ renderFeaturedImageToggle() }
{ renderCancelLink() }
</>
);
Expand Down
24 changes: 4 additions & 20 deletions packages/block-library/src/cover/edit/block-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,17 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { ALLOWED_MEDIA_TYPES, IMAGE_BACKGROUND_TYPE } from '../shared';
import { ALLOWED_MEDIA_TYPES } from '../shared';

export default function CoverBlockControls( {
attributes,
setAttributes,
onSelectMedia,
currentSettings,
toggleUseFeaturedImage,
} ) {
const {
contentPosition,
id,
useFeaturedImage,
dimRatio,
minHeight,
minHeightUnit,
} = attributes;
const { contentPosition, id, useFeaturedImage, minHeight, minHeightUnit } =
attributes;
const { hasInnerBlocks, url } = currentSettings;

const [ prevMinHeightValue, setPrevMinHeightValue ] = useState( minHeight );
Expand Down Expand Up @@ -63,17 +58,6 @@ export default function CoverBlockControls( {
} );
};

const toggleUseFeaturedImage = () => {
setAttributes( {
id: undefined,
url: undefined,
useFeaturedImage: ! useFeaturedImage,
dimRatio: dimRatio === 100 ? 50 : dimRatio,
backgroundType: useFeaturedImage
? IMAGE_BACKGROUND_TYPE
: undefined,
} );
};
return (
<>
<BlockControls group="block">
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/cover/edit/cover-placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function CoverPlaceholder( {
onSelectMedia,
onError,
style,
toggleUseFeaturedImage,
} ) {
return (
<MediaPlaceholder
Expand All @@ -30,6 +31,7 @@ export default function CoverPlaceholder( {
accept="image/*,video/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
disableMediaButtons={ disableMediaButtons }
onToggleFeaturedImage={ toggleUseFeaturedImage }
onError={ onError }
style={ style }
>
Expand Down
16 changes: 16 additions & 0 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,25 @@ function CoverEdit( {
overlayColor,
};

const toggleUseFeaturedImage = () => {
setAttributes( {
id: undefined,
url: undefined,
useFeaturedImage: ! useFeaturedImage,
dimRatio: dimRatio === 100 ? 50 : dimRatio,
backgroundType: useFeaturedImage
? IMAGE_BACKGROUND_TYPE
: undefined,
} );
};

const blockControls = (
<CoverBlockControls
attributes={ attributes }
setAttributes={ setAttributes }
onSelectMedia={ onSelectMedia }
currentSettings={ currentSettings }
toggleUseFeaturedImage={ toggleUseFeaturedImage }
/>
);

Expand All @@ -228,6 +241,7 @@ function CoverEdit( {
setOverlayColor={ setOverlayColor }
coverRef={ ref }
currentSettings={ currentSettings }
toggleUseFeaturedImage={ toggleUseFeaturedImage }
/>
);

Expand All @@ -249,6 +263,7 @@ function CoverEdit( {
style={ {
minHeight: minHeightWithUnit || undefined,
} }
toggleUseFeaturedImage={ toggleUseFeaturedImage }
>
<div className="wp-block-cover__placeholder-background-options">
<ColorPalette
Expand Down Expand Up @@ -384,6 +399,7 @@ function CoverEdit( {
disableMediaButtons
onSelectMedia={ onSelectMedia }
onError={ onUploadError }
toggleUseFeaturedImage={ toggleUseFeaturedImage }
/>
<div { ...innerBlocksProps } />
</div>
Expand Down