-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
45 lines (38 loc) · 1.1 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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
class UnsavedChangesWarning extends Component {
constructor() {
super( ...arguments );
this.warnIfUnsavedChanges = this.warnIfUnsavedChanges.bind( this );
}
componentDidMount() {
window.addEventListener( 'beforeunload', this.warnIfUnsavedChanges );
}
componentWillUnmount() {
window.removeEventListener( 'beforeunload', this.warnIfUnsavedChanges );
}
/**
* Warns the user if there are unsaved changes before leaving the editor.
*
* @param {Event} event `beforeunload` event.
*
* @return {?string} Warning prompt message, if unsaved changes exist.
*/
warnIfUnsavedChanges( event ) {
const { isDirty } = this.props;
if ( isDirty ) {
event.returnValue = __( 'You have unsaved changes. If you proceed, they will be lost.' );
return event.returnValue;
}
}
render() {
return null;
}
}
export default withSelect( ( select ) => ( {
isDirty: select( 'core/editor' ).isEditedPostDirty(),
} ) )( UnsavedChangesWarning );