-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit.js
104 lines (95 loc) · 2.35 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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
Warning,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import CommentsForm from './form';
export default function PostCommentsFormEdit( {
attributes,
context,
setAttributes,
} ) {
const { textAlign } = attributes;
const { postId, postType } = context;
const [ commentStatus ] = useEntityProp(
'postType',
postType,
'comment_status',
postId
);
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
const isSiteEditor = postType === undefined || postId === undefined;
const { defaultCommentStatus } = useSelect(
( select ) =>
select( blockEditorStore ).getSettings()
.__experimentalDiscussionSettings
);
const postTypeSupportsComments = useSelect( ( select ) =>
postType
? !! select( coreStore ).getPostType( postType )?.supports.comments
: false
);
let warning = false;
let showPlaceholder = true;
if ( ! isSiteEditor && 'open' !== commentStatus ) {
if ( 'closed' === commentStatus ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments Form block: Comments on this %s are not allowed.'
),
postType
);
showPlaceholder = false;
} else if ( ! postTypeSupportsComments ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments Form block: Comments for this post type (%s) are not enabled.'
),
postType
);
showPlaceholder = false;
} else if ( 'open' !== defaultCommentStatus ) {
warning = __(
'Post Comments Form block: Comments are not enabled.'
);
showPlaceholder = false;
}
}
return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
<div { ...blockProps }>
{ warning && <Warning>{ warning }</Warning> }
{ showPlaceholder ? <CommentsForm /> : null }
</div>
</>
);
}