-
Notifications
You must be signed in to change notification settings - Fork 4k
/
edit.js
258 lines (243 loc) · 7.19 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* External dependencies
*/
import { isUndefined, pickBy } from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Component, RawHTML } from '@wordpress/element';
import {
PanelBody,
Placeholder,
QueryControls,
RangeControl,
Spinner,
ToggleControl,
Toolbar,
RadioControl,
} from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import { __ } from '@wordpress/i18n';
import { dateI18n, format, __experimentalGetSettings } from '@wordpress/date';
import {
InspectorControls,
BlockControls,
} from '@wordpress/block-editor';
import { withSelect } from '@wordpress/data';
/**
* Module Constants
*/
const CATEGORIES_LIST_QUERY = {
per_page: -1,
};
const MAX_POSTS_COLUMNS = 6;
class LatestPostsEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
categoriesList: [],
};
}
componentDidMount() {
this.isStillMounted = true;
this.fetchRequest = apiFetch( {
path: addQueryArgs( `/wp/v2/categories`, CATEGORIES_LIST_QUERY ),
} ).then(
( categoriesList ) => {
if ( this.isStillMounted ) {
this.setState( { categoriesList } );
}
}
).catch(
() => {
if ( this.isStillMounted ) {
this.setState( { categoriesList: [] } );
}
}
);
}
componentWillUnmount() {
this.isStillMounted = false;
}
render() {
const { attributes, setAttributes, latestPosts } = this.props;
const { categoriesList } = this.state;
const { displayPostContentRadio, displayPostContent, displayPostDate, postLayout, columns, order, orderBy, categories, postsToShow, excerptLength } = attributes;
const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Post Content Settings' ) }>
<ToggleControl
label={ __( 'Post Content' ) }
checked={ displayPostContent }
onChange={ ( value ) => setAttributes( { displayPostContent: value } ) }
/>
{ displayPostContent &&
<RadioControl
label="Show:"
selected={ displayPostContentRadio }
options={ [
{ label: 'Excerpt', value: 'excerpt' },
{ label: 'Full Post', value: 'full_post' },
] }
onChange={ ( value ) => setAttributes( { displayPostContentRadio: value } ) }
/>
}
{ displayPostContent && displayPostContentRadio === 'excerpt' &&
<RangeControl
label={ __( 'Max number of words in excerpt' ) }
value={ excerptLength }
onChange={ ( value ) => setAttributes( { excerptLength: value } ) }
min={ 10 }
max={ 100 }
/>
}
</PanelBody>
<PanelBody title={ __( 'Post Meta Settings' ) }>
<ToggleControl
label={ __( 'Display post date' ) }
checked={ displayPostDate }
onChange={ ( value ) => setAttributes( { displayPostDate: value } ) }
/>
</PanelBody>
<PanelBody title={ __( 'Sorting and Filtering' ) }>
<QueryControls
{ ...{ order, orderBy } }
numberOfItems={ postsToShow }
categoriesList={ categoriesList }
selectedCategoryId={ categories }
onOrderChange={ ( value ) => setAttributes( { order: value } ) }
onOrderByChange={ ( value ) => setAttributes( { orderBy: value } ) }
onCategoryChange={ ( value ) => setAttributes( { categories: '' !== value ? value : undefined } ) }
onNumberOfItemsChange={ ( value ) => setAttributes( { postsToShow: value } ) }
/>
{ postLayout === 'grid' &&
<RangeControl
label={ __( 'Columns' ) }
value={ columns }
onChange={ ( value ) => setAttributes( { columns: value } ) }
min={ 2 }
max={ ! hasPosts ? MAX_POSTS_COLUMNS : Math.min( MAX_POSTS_COLUMNS, latestPosts.length ) }
required
/>
}
</PanelBody>
</InspectorControls>
);
const hasPosts = Array.isArray( latestPosts ) && latestPosts.length;
if ( ! hasPosts ) {
return (
<>
{ inspectorControls }
<Placeholder
icon="admin-post"
label={ __( 'Latest Posts' ) }
>
{ ! Array.isArray( latestPosts ) ?
<Spinner /> :
__( 'No posts found.' )
}
</Placeholder>
</>
);
}
// Removing posts from display should be instant.
const displayPosts = latestPosts.length > postsToShow ?
latestPosts.slice( 0, postsToShow ) :
latestPosts;
const layoutControls = [
{
icon: 'list-view',
title: __( 'List view' ),
onClick: () => setAttributes( { postLayout: 'list' } ),
isActive: postLayout === 'list',
},
{
icon: 'grid-view',
title: __( 'Grid view' ),
onClick: () => setAttributes( { postLayout: 'grid' } ),
isActive: postLayout === 'grid',
},
];
const dateFormat = __experimentalGetSettings().formats.date;
return (
<>
{ inspectorControls }
<BlockControls>
<Toolbar controls={ layoutControls } />
</BlockControls>
<ul
className={ classnames( this.props.className, {
'wp-block-latest-posts__list': true,
'is-grid': postLayout === 'grid',
'has-dates': displayPostDate,
[ `columns-${ columns }` ]: postLayout === 'grid',
} ) }
>
{ displayPosts.map( ( post, i ) => {
const titleTrimmed = post.title.rendered.trim();
let excerpt = post.excerpt.rendered;
if ( post.excerpt.raw === '' ) {
excerpt = post.content.raw;
}
const excerptElement = document.createElement( 'div' );
excerptElement.innerHTML = excerpt;
excerpt = excerptElement.textContent || excerptElement.innerText || '';
return (
<li key={ i }>
<a href={ post.link } target="_blank" rel="noreferrer noopener">
{ titleTrimmed ? (
<RawHTML>
{ titleTrimmed }
</RawHTML>
) :
__( '(no title)' )
}
</a>
{ displayPostDate && post.date_gmt &&
<time dateTime={ format( 'c', post.date_gmt ) } className="wp-block-latest-posts__post-date">
{ dateI18n( dateFormat, post.date_gmt ) }
</time>
}
{ displayPostContent && displayPostContentRadio === 'excerpt' &&
<div className="wp-block-latest-posts__post-excerpt">
<RawHTML
key="html"
>
{ excerptLength < excerpt.trim().split( ' ' ).length ?
excerpt.trim().split( ' ', excerptLength ).join( ' ' ) + ' ... <a href=' + post.link + 'target="_blank" rel="noopener noreferrer">' + __( 'Read more' ) + '</a>' :
excerpt.trim().split( ' ', excerptLength ).join( ' ' ) }
</RawHTML>
</div>
}
{ displayPostContent && displayPostContentRadio === 'full_post' &&
<div className="wp-block-latest-posts__post-full-content">
<RawHTML
key="html"
>
{ post.content.raw.trim() }
</RawHTML>
</div>
}
</li>
);
} ) }
</ul>
</>
);
}
}
export default withSelect( ( select, props ) => {
const { postsToShow, order, orderBy, categories } = props.attributes;
const { getEntityRecords } = select( 'core' );
const latestPostsQuery = pickBy( {
categories,
order,
orderby: orderBy,
per_page: postsToShow,
}, ( value ) => ! isUndefined( value ) );
return {
latestPosts: getEntityRecords( 'postType', 'post', latestPostsQuery ),
};
} )( LatestPostsEdit );