Skip to content

Commit

Permalink
Start with featured image in media placeholder (#41722)
Browse files Browse the repository at this point in the history
* adds the option to start with featured image from the media placeholder

* makes the media placeholder aware if the post has a featured image set

* fix rebase problem

* revert to using illustration if no featured image is set for post
  • Loading branch information
draganescu authored Jul 6, 2022
1 parent af7da80 commit 6cdd6ae
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
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,
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

0 comments on commit 6cdd6ae

Please sign in to comment.