-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
edit.js
147 lines (141 loc) · 3.55 KB
/
edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* WordPress dependencies
*/
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import {
Icon,
ToggleControl,
PanelBody,
withNotices,
} from '@wordpress/components';
import {
InspectorControls,
BlockControls,
MediaPlaceholder,
MediaReplaceFlow,
BlockIcon,
useBlockProps,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { postFeaturedImage } from '@wordpress/icons';
/**
* Internal dependencies
*/
import DimensionControls from './dimension-controls';
const ALLOWED_MEDIA_TYPES = [ 'image' ];
const placeholderChip = (
<div className="post-featured-image_placeholder">
<Icon icon={ postFeaturedImage } />
<p> { __( 'Featured Image' ) }</p>
</div>
);
function PostFeaturedImageDisplay( {
attributes,
setAttributes,
context: { postId, postType, queryId },
noticeUI,
noticeOperations,
} ) {
const isDescendentOfQueryLoop = !! queryId;
const { isLink, height, width, scale } = attributes;
const [ featuredImage, setFeaturedImage ] = useEntityProp(
'postType',
postType,
'featured_media',
postId
);
const media = useSelect(
( select ) =>
featuredImage &&
select( coreStore ).getMedia( featuredImage, { context: 'view' } ),
[ featuredImage ]
);
const blockProps = useBlockProps( {
style: { width },
} );
const onSelectImage = ( value ) => {
if ( value?.id ) {
setFeaturedImage( value.id );
}
};
function onUploadError( message ) {
noticeOperations.removeAllNotices();
noticeOperations.createErrorNotice( message );
}
let image;
if ( ! featuredImage && isDescendentOfQueryLoop ) {
return <div { ...blockProps }>{ placeholderChip }</div>;
}
if ( ! featuredImage ) {
image = (
<MediaPlaceholder
icon={ <BlockIcon icon={ postFeaturedImage } /> }
onSelect={ onSelectImage }
notices={ noticeUI }
onError={ onUploadError }
accept="image/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
labels={ {
title: __( 'Featured image' ),
instructions: __(
'Upload a media file or pick one from your media library.'
),
} }
/>
);
} else {
// We have a Featured image so show a Placeholder if is loading.
image = ! media ? (
placeholderChip
) : (
<img
src={ media.source_url }
alt={ media.alt_text || __( 'Featured image' ) }
style={ { height, objectFit: height && scale } }
/>
);
}
return (
<>
<InspectorControls>
<DimensionControls
attributes={ attributes }
setAttributes={ setAttributes }
/>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
postType
) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
</PanelBody>
</InspectorControls>
{ !! media && ! isDescendentOfQueryLoop && (
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ featuredImage }
mediaURL={ media.source_url }
allowedTypes={ ALLOWED_MEDIA_TYPES }
accept="image/*"
onSelect={ onSelectImage }
onError={ onUploadError }
/>
</BlockControls>
) }
<figure { ...blockProps }>{ image }</figure>
</>
);
}
const PostFeaturedImageWithNotices = withNotices( PostFeaturedImageDisplay );
export default function PostFeaturedImageEdit( props ) {
const blockProps = useBlockProps();
if ( ! props.context?.postId ) {
return <div { ...blockProps }>{ placeholderChip }</div>;
}
return <PostFeaturedImageWithNotices { ...props } />;
}