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

Show PostFeaturedImage in editor #25412

Merged
merged 3 commits into from
Sep 29, 2020
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
1 change: 1 addition & 0 deletions packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
@import "./widget-area/editor.scss";
@import "./query-loop/editor.scss";
@import "./query/editor.scss";
@import "./post-featured-image/editor.scss";

/**
* Import styles from internal editor components used by the blocks.
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "core/post-featured-image",
"category": "design",
"usesContext": [
"postId"
"postId",
"postType"
],
"supports": {
"html": false
Expand Down
35 changes: 23 additions & 12 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
/**
* WordPress dependencies
*/
import { useEntityProp, useEntityId } from '@wordpress/core-data';
import { useEntityProp } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { ResponsiveWrapper } from '@wordpress/components';
import { ResponsiveWrapper, Icon } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { postFeaturedImage as icon } from '@wordpress/icons';

function PostFeaturedImageDisplay() {
function PostFeaturedImageDisplay( { context: { postId, postType } } ) {
const [ featuredImage ] = useEntityProp(
'postType',
'post',
'featured_media'
postType,
'featured_media',
postId
);
const media = useSelect(
( select ) =>
featuredImage && select( 'core' ).getMedia( featuredImage ),
[ featuredImage ]
);
return media ? (
if ( ! media ) {
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
return (
<div className="post-featured-image_placeholder">
<Icon icon={ icon } />
<p> { __( 'Featured Image' ) }</p>
</div>
);
}
const alt = media.alt_text || __( 'No alternative text set' );
return (
<ResponsiveWrapper
naturalWidth={ media.media_details.width }
naturalHeight={ media.media_details.height }
>
<img src={ media.source_url } alt="Post Featured Media" />
<img src={ media.source_url } alt={ alt } />
</ResponsiveWrapper>
) : null;
);
}

export default function PostFeaturedImageEdit() {
if ( ! useEntityId( 'postType', 'post' ) ) {
return __( 'Post Featured Image' );
export default function PostFeaturedImageEdit( props ) {
if ( ! props.context?.postId ) {
return __( 'Featured Image' );
}
return <PostFeaturedImageDisplay />;
return <PostFeaturedImageDisplay { ...props } />;
}
19 changes: 19 additions & 0 deletions packages/block-library/src/post-featured-image/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
div[data-type="core/post-featured-image"] {
.post-featured-image_placeholder {
display: flex;
flex-direction: row;
align-items: flex-start;
border-radius: $radius-block-ui;
background-color: $white;
box-shadow: inset 0 0 0 $border-width $gray-900;
padding: $grid-unit-15;
svg {
margin-right: $grid-unit-15;
}
p {
font-family: $default-font;
font-size: $default-font-size;
margin: 0;
}
}
}
4 changes: 4 additions & 0 deletions packages/components/src/responsive-wrapper/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
& > span {
display: block;
}
&,
& > img {
width: auto;
}
}

.components-responsive-wrapper__content {
Expand Down