-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
160 lines (148 loc) · 3.76 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
/**
* External dependencies
*/
import { noop, get } from 'lodash';
/**
* 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';
import { DotTip } from '@wordpress/nux';
/**
* Internal dependencies
*/
import PublishButtonLabel from './label';
export class PostPublishButton extends Component {
constructor( props ) {
super( props );
this.buttonNode = createRef();
}
componentDidMount() {
if ( this.props.focusOnMount ) {
this.buttonNode.current.focus();
}
}
render() {
const {
forceIsDirty,
forceIsSaving,
hasPublishAction,
isBeingScheduled,
isOpen,
isPostSavingLocked,
isPublishable,
isPublished,
isSaveable,
isSaving,
isToggle,
onSave,
onStatusChange,
onSubmit = noop,
onToggle,
visibility,
} = 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 ( isBeingScheduled ) {
publishStatus = 'future';
} else if ( visibility === 'private' ) {
publishStatus = 'private';
} else {
publishStatus = 'publish';
}
const onClickButton = () => {
if ( isButtonDisabled ) {
return;
}
onSubmit();
onStatusChange( publishStatus );
onSave();
};
const onClickToggle = () => {
if ( isToggleDisabled ) {
return;
}
onToggle();
};
const buttonProps = {
'aria-disabled': isButtonDisabled,
className: 'editor-post-publish-button',
isBusy: isSaving && isPublished,
isLarge: true,
isPrimary: true,
onClick: onClickButton,
};
const toggleProps = {
'aria-disabled': isToggleDisabled,
'aria-expanded': isOpen,
className: 'editor-post-publish-panel__toggle',
isBusy: isSaving && isPublished,
isPrimary: true,
onClick: onClickToggle,
};
const toggleChildren = isBeingScheduled ? __( 'Schedule…' ) : __( 'Publish…' );
const buttonChildren = <PublishButtonLabel forceIsSaving={ forceIsSaving } />;
const componentProps = isToggle ? toggleProps : buttonProps;
const componentChildren = isToggle ? toggleChildren : buttonChildren;
return (
<Button
ref={ this.buttonNode }
{ ...componentProps }
>
{ componentChildren }
<DotTip tipId="core/editor.publish">
{ __( 'Finished writing? That’s great, let’s get this published right now. Just click “Publish” and you’re good to go.' ) }
</DotTip>
</Button>
);
}
}
export default compose( [
withSelect( ( select ) => {
const {
isSavingPost,
isEditedPostBeingScheduled,
getEditedPostVisibility,
isCurrentPostPublished,
isEditedPostSaveable,
isEditedPostPublishable,
isPostSavingLocked,
getCurrentPost,
getCurrentPostType,
} = select( 'core/editor' );
return {
isSaving: isSavingPost(),
isBeingScheduled: isEditedPostBeingScheduled(),
visibility: getEditedPostVisibility(),
isSaveable: isEditedPostSaveable(),
isPostSavingLocked: isPostSavingLocked(),
isPublishable: isEditedPostPublishable(),
isPublished: isCurrentPostPublished(),
hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ),
postType: getCurrentPostType(),
};
} ),
withDispatch( ( dispatch ) => {
const { editPost, savePost } = dispatch( 'core/editor' );
return {
onStatusChange: ( status ) => editPost( { status } ),
onSave: savePost,
};
} ),
] )( PostPublishButton );