-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
277 lines (267 loc) · 7.01 KB
/
index.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* WordPress dependencies
*/
import {
Button,
CheckboxControl,
Dropdown,
__experimentalVStack as VStack,
TextControl,
RadioControl,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { useState, useMemo } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { __experimentalInspectorPopoverHeader as InspectorPopoverHeader } from '@wordpress/block-editor';
import { useInstanceId } from '@wordpress/compose';
import {
drafts,
published,
scheduled,
pending,
notAllowed,
} from '@wordpress/icons';
/**
* Internal dependencies
*/
import {
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
PATTERN_POST_TYPE,
NAVIGATION_POST_TYPE,
} from '../../store/constants';
import PostPanelRow from '../post-panel-row';
import PostSticky from '../post-sticky';
import { PrivatePostSchedule } from '../post-schedule';
import { store as editorStore } from '../../store';
const postStatusesInfo = {
'auto-draft': { label: __( 'Draft' ), icon: drafts },
draft: { label: __( 'Draft' ), icon: drafts },
pending: { label: __( 'Pending' ), icon: pending },
private: { label: __( 'Private' ), icon: notAllowed },
future: { label: __( 'Scheduled' ), icon: scheduled },
publish: { label: __( 'Published' ), icon: published },
};
export const STATUS_OPTIONS = [
{
label: __( 'Draft' ),
value: 'draft',
description: __( 'Not ready to publish.' ),
},
{
label: __( 'Pending' ),
value: 'pending',
description: __( 'Waiting for review before publishing.' ),
},
{
label: __( 'Private' ),
value: 'private',
description: __( 'Only visible to site admins and editors.' ),
},
{
label: __( 'Scheduled' ),
value: 'future',
description: __( 'Publish automatically on a chosen date.' ),
},
{
label: __( 'Published' ),
value: 'publish',
description: __( 'Visible to everyone.' ),
},
];
const DESIGN_POST_TYPES = [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
PATTERN_POST_TYPE,
NAVIGATION_POST_TYPE,
];
export default function PostStatus() {
const { status, date, password, postId, postType, canEdit } = useSelect(
( select ) => {
const {
getEditedPostAttribute,
getCurrentPostId,
getCurrentPostType,
getCurrentPost,
} = select( editorStore );
return {
status: getEditedPostAttribute( 'status' ),
date: getEditedPostAttribute( 'date' ),
password: getEditedPostAttribute( 'password' ),
postId: getCurrentPostId(),
postType: getCurrentPostType(),
canEdit:
getCurrentPost()._links?.[ 'wp:action-publish' ] ?? false,
};
},
[]
);
const [ showPassword, setShowPassword ] = useState( !! password );
const passwordInputId = useInstanceId(
PostStatus,
'editor-change-status__password-input'
);
const { editEntityRecord } = useDispatch( coreStore );
const [ popoverAnchor, setPopoverAnchor ] = useState( null );
// Memoize popoverProps to avoid returning a new object every time.
const popoverProps = useMemo(
() => ( {
// Anchor the popover to the middle of the entire row so that it doesn't
// move around when the label changes.
anchor: popoverAnchor,
'aria-label': __( 'Status & visibility' ),
headerTitle: __( 'Status & visibility' ),
placement: 'left-start',
offset: 36,
shift: true,
} ),
[ popoverAnchor ]
);
if ( DESIGN_POST_TYPES.includes( postType ) ) {
return null;
}
const updatePost = ( {
status: newStatus = status,
password: newPassword = password,
date: newDate = date,
} ) => {
editEntityRecord( 'postType', postType, postId, {
status: newStatus,
date: newDate,
password: newPassword,
} );
};
const handleTogglePassword = ( value ) => {
setShowPassword( value );
if ( ! value ) {
updatePost( { password: '' } );
}
};
const handleStatus = ( value ) => {
let newDate = date;
let newPassword = password;
if ( status === 'future' && new Date( date ) > new Date() ) {
newDate = null;
}
if ( value === 'private' && password ) {
newPassword = '';
}
updatePost( {
status: value,
date: newDate,
password: newPassword,
} );
};
return (
<PostPanelRow label={ __( 'Status' ) } ref={ setPopoverAnchor }>
{ canEdit ? (
<Dropdown
className="editor-post-status"
contentClassName="editor-change-status__content"
popoverProps={ popoverProps }
focusOnMount
renderToggle={ ( { onToggle, isOpen } ) => (
<Button
variant="tertiary"
size="compact"
onClick={ onToggle }
icon={ postStatusesInfo[ status ]?.icon }
aria-label={ sprintf(
// translators: %s: Current post status.
__( 'Change status: %s' ),
postStatusesInfo[ status ]?.label
) }
aria-expanded={ isOpen }
>
{ postStatusesInfo[ status ]?.label }
</Button>
) }
renderContent={ ( { onClose } ) => (
<>
<InspectorPopoverHeader
title={ __( 'Status & visibility' ) }
onClose={ onClose }
/>
<form>
<VStack spacing={ 4 }>
<RadioControl
className="editor-change-status__options"
hideLabelFromVision
label={ __( 'Status' ) }
options={ STATUS_OPTIONS }
onChange={ handleStatus }
selected={
status === 'auto-draft'
? 'draft'
: status
}
/>
{ status === 'future' && (
<div className="editor-change-status__publish-date-wrapper">
<PrivatePostSchedule
showPopoverHeaderActions={
false
}
isCompact
/>
</div>
) }
{ status !== 'private' && (
<VStack
as="fieldset"
spacing={ 4 }
className="editor-change-status__password-fieldset"
>
<CheckboxControl
__nextHasNoMarginBottom
label={ __(
'Password protected'
) }
help={ __(
'Only visible to those who know the password'
) }
checked={ showPassword }
onChange={
handleTogglePassword
}
/>
{ showPassword && (
<div className="editor-change-status__password-input">
<TextControl
label={ __(
'Password'
) }
onChange={ ( value ) =>
updatePost( {
password: value,
} )
}
value={ password }
placeholder={ __(
'Use a secure password'
) }
type="text"
id={ passwordInputId }
__next40pxDefaultSize
__nextHasNoMarginBottom
maxLength={ 255 }
/>
</div>
) }
</VStack>
) }
<PostSticky />
</VStack>
</form>
</>
) }
/>
) : (
<div className="editor-post-status is-read-only">
{ postStatusesInfo[ status ]?.label }
</div>
) }
</PostPanelRow>
);
}