-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
82 lines (71 loc) · 2.02 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
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
export class AutosaveMonitor extends Component {
componentDidUpdate( prevProps ) {
const { isDirty, editsReference, isAutosaveable, isAutosaving } = this.props;
// The edits reference is held for comparison to avoid scheduling an
// autosave if an edit has not been made since the last autosave
// completion. This is assigned when the autosave completes, and reset
// when an edit occurs.
//
// See: https://github.com/WordPress/gutenberg/issues/12318
if ( editsReference !== prevProps.editsReference ) {
this.didAutosaveForEditsReference = false;
}
if ( ! isAutosaving && prevProps.isAutosaving ) {
this.didAutosaveForEditsReference = true;
}
if (
prevProps.isDirty !== isDirty ||
prevProps.isAutosaveable !== isAutosaveable ||
prevProps.editsReference !== editsReference
) {
this.toggleTimer(
isDirty &&
isAutosaveable &&
! this.didAutosaveForEditsReference
);
}
}
componentWillUnmount() {
this.toggleTimer( false );
}
toggleTimer( isPendingSave ) {
clearTimeout( this.pendingSave );
const { autosaveInterval } = this.props;
if ( isPendingSave ) {
this.pendingSave = setTimeout(
() => this.props.autosave(),
autosaveInterval * 1000
);
}
}
render() {
return null;
}
}
export default compose( [
withSelect( ( select ) => {
const {
isEditedPostDirty,
isEditedPostAutosaveable,
getReferenceByDistinctEdits,
isAutosavingPost,
} = select( 'core/editor' );
const { autosaveInterval } = select( 'core/editor' ).getEditorSettings();
return {
isDirty: isEditedPostDirty(),
isAutosaveable: isEditedPostAutosaveable(),
editsReference: getReferenceByDistinctEdits(),
isAutosaving: isAutosavingPost(),
autosaveInterval,
};
} ),
withDispatch( ( dispatch ) => ( {
autosave: dispatch( 'core/editor' ).autosave,
} ) ),
] )( AutosaveMonitor );