-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
105 lines (95 loc) · 2.44 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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Dashicon, IconButton, withSafeTimeout } from '@wordpress/components';
import { Component, compose } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { keycodes } from '@wordpress/utils';
/**
* Internal dependencies
*/
import './style.scss';
import PostSwitchToDraftButton from '../post-switch-to-draft-button';
const { displayShortcut } = keycodes;
/**
* Component showing whether the post is saved or not and displaying save links.
*
* @param {Object} Props Component Props.
*/
export class PostSavedState extends Component {
constructor() {
super( ...arguments );
this.state = {
forceSavedMessage: false,
};
}
componentDidUpdate( prevProps ) {
if ( prevProps.isSaving && ! this.props.isSaving ) {
this.setState( { forceSavedMessage: true } );
this.props.setTimeout( () => {
this.setState( { forceSavedMessage: false } );
}, 1000 );
}
}
render() {
const { isNew, isPublished, isDirty, isSaving, isSaveable, onSave } = this.props;
const { forceSavedMessage } = this.state;
if ( isSaving ) {
return (
<span className="editor-post-saved-state is-saving">
<Dashicon icon="cloud" />
{ __( 'Saving' ) }
</span>
);
}
if ( isPublished ) {
return <PostSwitchToDraftButton />;
}
if ( ! isSaveable ) {
return null;
}
if ( forceSavedMessage || ( ! isNew && ! isDirty ) ) {
return (
<span className="editor-post-saved-state is-saved">
<Dashicon icon="saved" />
{ __( 'Saved' ) }
</span>
);
}
return (
<IconButton
className="editor-post-save-draft"
onClick={ onSave }
icon="cloud-upload"
shortcut={ displayShortcut.primary( 's' ) }
>
{ __( 'Save Draft' ) }
</IconButton>
);
}
}
export default compose( [
withSelect( ( select, { forceIsDirty, forceIsSaving } ) => {
const {
isEditedPostNew,
isCurrentPostPublished,
isEditedPostDirty,
isSavingPost,
isEditedPostSaveable,
getCurrentPost,
} = select( 'core/editor' );
return {
post: getCurrentPost(),
isNew: isEditedPostNew(),
isPublished: isCurrentPostPublished(),
isDirty: forceIsDirty || isEditedPostDirty(),
isSaving: forceIsSaving || isSavingPost(),
isSaveable: isEditedPostSaveable(),
};
} ),
withDispatch( ( dispatch ) => ( {
onSave: dispatch( 'core/editor' ).savePost,
} ) ),
withSafeTimeout,
] )( PostSavedState );