File size: 1,013 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { dialog } = require( 'electron' );

/**
 * Handles client responses to the beforeunload event. Electron doesn't have a default implementation
 * like browsers do, so we need to provide our own.
 */
module.exports = function ( { view, window } ) {
	view.webContents.on( 'will-prevent-unload', ( event ) => {
		const choice = dialog.showMessageBoxSync( window, {
			type: 'question',
			buttons: [ 'Stay', 'Leave' ],
			defaultId: 1,
			cancelId: 0,
			title: 'WordPress.com',
			// The beforeunload handler can set a custom message
			// e.g. https://github.com/WordPress/gutenberg/blob/4b60f9ececa96ba51d9cae4d6d7a9a1311d8b9d8/packages/editor/src/components/unsaved-changes-warning/index.js#L34
			// Unfortunately Electron doesn't give us access to that value.
			message: 'You have unsaved changes. If you proceed, they will be lost.',
		} );
		if ( choice === 1 ) {
			// preventDefault() ignores the beforeunload event handler and allow the page to be unloaded.
			event.preventDefault();
		}
	} );
};