-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
242 lines (223 loc) · 5.81 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
/**
* External dependencies
*/
import { noop, get, some } from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Button } from '@wordpress/components';
import { Component, createRef } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import PublishButtonLabel from './label';
import { store as editorStore } from '../../store';
export class PostPublishButton extends Component {
constructor( props ) {
super( props );
this.buttonNode = createRef();
this.createOnClick = this.createOnClick.bind( this );
this.closeEntitiesSavedStates = this.closeEntitiesSavedStates.bind(
this
);
this.state = {
entitiesSavedStatesCallback: false,
};
}
componentDidMount() {
if ( this.props.focusOnMount ) {
this.buttonNode.current.focus();
}
}
createOnClick( callback ) {
return ( ...args ) => {
const { hasNonPostEntityChanges } = this.props;
if ( hasNonPostEntityChanges ) {
// The modal for multiple entity saving will open,
// hold the callback for saving/publishing the post
// so that we can call it if the post entity is checked.
this.setState( {
entitiesSavedStatesCallback: () => callback( ...args ),
} );
// Open the save panel by setting its callback.
// To set a function on the useState hook, we must set it
// with another function (() => myFunction). Passing the
// function on its own will cause an error when called.
this.props.setEntitiesSavedStatesCallback(
() => this.closeEntitiesSavedStates
);
return noop;
}
return callback( ...args );
};
}
closeEntitiesSavedStates( savedEntities ) {
const { postType, postId } = this.props;
const { entitiesSavedStatesCallback } = this.state;
this.setState( { entitiesSavedStatesCallback: false }, () => {
if (
savedEntities &&
some(
savedEntities,
( elt ) =>
elt.kind === 'postType' &&
elt.name === postType &&
elt.key === postId
)
) {
// The post entity was checked, call the held callback from `createOnClick`.
entitiesSavedStatesCallback();
}
} );
}
render() {
const {
forceIsDirty,
forceIsSaving,
hasPublishAction,
isBeingScheduled,
isOpen,
isPostSavingLocked,
isPublishable,
isPublished,
isSaveable,
isSaving,
isAutoSaving,
isToggle,
onSave,
onStatusChange,
onSubmit = noop,
onToggle,
visibility,
hasNonPostEntityChanges,
} = this.props;
const isButtonDisabled =
isSaving ||
forceIsSaving ||
! isSaveable ||
isPostSavingLocked ||
( ! isPublishable && ! forceIsDirty );
const isToggleDisabled =
isPublished ||
isSaving ||
forceIsSaving ||
! isSaveable ||
( ! isPublishable && ! forceIsDirty );
let publishStatus;
if ( ! hasPublishAction ) {
publishStatus = 'pending';
} else if ( visibility === 'private' ) {
publishStatus = 'private';
} else if ( isBeingScheduled ) {
publishStatus = 'future';
} else {
publishStatus = 'publish';
}
const onClickButton = () => {
if ( isButtonDisabled ) {
return;
}
onSubmit();
onStatusChange( publishStatus );
onSave();
};
const onClickToggle = () => {
if ( isToggleDisabled ) {
return;
}
onToggle();
};
const buttonProps = {
'aria-disabled': isButtonDisabled && ! hasNonPostEntityChanges,
className: 'editor-post-publish-button',
isBusy: ! isAutoSaving && isSaving && isPublished,
variant: 'primary',
onClick: this.createOnClick( onClickButton ),
};
const toggleProps = {
'aria-disabled': isToggleDisabled && ! hasNonPostEntityChanges,
'aria-expanded': isOpen,
className: 'editor-post-publish-panel__toggle',
isBusy: isSaving && isPublished,
variant: 'primary',
onClick: this.createOnClick( onClickToggle ),
};
const toggleChildren = isBeingScheduled
? __( 'Schedule…' )
: __( 'Publish' );
const buttonChildren = (
<PublishButtonLabel
forceIsSaving={ forceIsSaving }
hasNonPostEntityChanges={ hasNonPostEntityChanges }
/>
);
const componentProps = isToggle ? toggleProps : buttonProps;
const componentChildren = isToggle ? toggleChildren : buttonChildren;
return (
<>
<Button
ref={ this.buttonNode }
{ ...componentProps }
className={ classnames(
componentProps.className,
'editor-post-publish-button__button',
{
'has-changes-dot': hasNonPostEntityChanges,
}
) }
>
{ componentChildren }
</Button>
</>
);
}
}
export default compose( [
withSelect( ( select ) => {
const {
isSavingPost,
isAutosavingPost,
isEditedPostBeingScheduled,
getEditedPostVisibility,
isCurrentPostPublished,
isEditedPostSaveable,
isEditedPostPublishable,
isPostSavingLocked,
getCurrentPost,
getCurrentPostType,
getCurrentPostId,
hasNonPostEntityChanges,
} = select( editorStore );
const _isAutoSaving = isAutosavingPost();
return {
isSaving: isSavingPost() || _isAutoSaving,
isAutoSaving: _isAutoSaving,
isBeingScheduled: isEditedPostBeingScheduled(),
visibility: getEditedPostVisibility(),
isSaveable: isEditedPostSaveable(),
isPostSavingLocked: isPostSavingLocked(),
isPublishable: isEditedPostPublishable(),
isPublished: isCurrentPostPublished(),
hasPublishAction: get(
getCurrentPost(),
[ '_links', 'wp:action-publish' ],
false
),
postType: getCurrentPostType(),
postId: getCurrentPostId(),
hasNonPostEntityChanges: hasNonPostEntityChanges(),
};
} ),
withDispatch( ( dispatch ) => {
const { editPost, savePost } = dispatch( editorStore );
return {
onStatusChange: ( status ) =>
editPost( { status }, { undoIgnore: true } ),
onSave: savePost,
};
} ),
] )( PostPublishButton );