-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
185 lines (173 loc) · 4.91 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* WordPress dependencies
*/
import { useState, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import {
BlockContextProvider,
BlockPreview,
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Spinner } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { convertToTree } from './util';
const TEMPLATE = [
[ 'core/comment-author-avatar' ],
[ 'core/comment-author-name' ],
[ 'core/comment-date' ],
[ 'core/comment-content' ],
[ 'core/comment-reply-link' ],
[ 'core/comment-edit-link' ],
];
/**
* Component which renders the inner blocks of the Comment Template.
*
* @param {Object} props Component props.
* @param {Array} [props.comment] - A comment object.
* @param {Array} [props.activeComment] - The block that is currently active.
* @param {Array} [props.setActiveComment] - The setter for activeComment.
* @param {Array} [props.firstBlock] - First comment in the array.
* @param {Array} [props.blocks] - Array of blocks returned from
* getBlocks() in parent .
* @return {WPElement} Inner blocks of the Comment Template
*/
function CommentTemplateInnerBlocks( {
comment,
activeComment,
setActiveComment,
firstBlock,
blocks,
} ) {
const { children, ...innerBlocksProps } = useInnerBlocksProps(
{},
{ template: TEMPLATE }
);
return (
<li { ...innerBlocksProps }>
{ comment === ( activeComment || firstBlock ) ? (
children
) : (
<BlockPreview
blocks={ blocks }
__experimentalLive
__experimentalOnClick={ () => setActiveComment( comment ) }
/>
) }
{ comment?.children?.length > 0 ? (
<CommentsList
comments={ comment.children }
activeComment={ activeComment }
setActiveComment={ setActiveComment }
blocks={ blocks }
/>
) : null }
</li>
);
}
/**
* Component that renders a list of (nested) comments. It is called recursively.
*
* @param {Object} props Component props.
* @param {Array} [props.comments] - Array of comment objects.
* @param {Array} [props.blockProps] - Props from parent's `useBlockProps()`.
* @param {Array} [props.activeComment] - The block that is currently active.
* @param {Array} [props.setActiveComment] - The setter for activeComment.
* @param {Array} [props.blocks] - Array of blocks returned from
* getBlocks() in parent .
* @return {WPElement} List of comments.
*/
const CommentsList = ( {
comments,
blockProps,
activeComment,
setActiveComment,
blocks,
} ) => (
<ol { ...blockProps }>
{ comments &&
comments.map( ( comment ) => (
<BlockContextProvider
key={ comment.commentId }
value={ comment }
>
<CommentTemplateInnerBlocks
comment={ comment }
activeComment={ activeComment }
setActiveComment={ setActiveComment }
blocks={ blocks }
firstBlock={ comments[ 0 ] }
/>
</BlockContextProvider>
) ) }
</ol>
);
export default function CommentTemplateEdit( {
clientId,
context: { postId, 'comments/perPage': perPage, 'comments/order': order },
} ) {
const blockProps = useBlockProps();
const [ activeComment, setActiveComment ] = useState();
const { commentOrder, commentsPerPage } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return getSettings().__experimentalDiscussionSettings;
} );
const { rawComments, blocks } = useSelect(
( select ) => {
const { getEntityRecords } = select( coreStore );
const { getBlocks } = select( blockEditorStore );
const commentQuery = {
post: postId,
status: 'approve',
context: 'embed',
order: order || commentOrder,
};
if ( order ) {
commentQuery.order = order;
}
return {
rawComments: getEntityRecords(
'root',
'comment',
commentQuery
),
blocks: getBlocks( clientId ),
};
},
[ postId, clientId, order ]
);
// TODO: Replicate the logic used on the server.
perPage = perPage || commentsPerPage;
// We convert the flat list of comments to tree.
// Then, we show only a maximum of `perPage` number of comments.
// This is because passing `per_page` to `getEntityRecords()` does not
// take into account nested comments.
const comments = useMemo(
() => convertToTree( rawComments ).slice( 0, perPage ),
[ rawComments, perPage ]
);
if ( ! rawComments ) {
return (
<p { ...blockProps }>
<Spinner />
</p>
);
}
if ( ! comments.length ) {
return <p { ...blockProps }> { __( 'No results found.' ) }</p>;
}
return (
<CommentsList
comments={ comments }
blockProps={ blockProps }
blocks={ blocks }
activeComment={ activeComment }
setActiveComment={ setActiveComment }
/>
);
}