@@ -42,30 +42,16 @@ function getTitle(props) {
42
42
return id ? `p5.js Web Editor | ${ props . project . name } ` : 'p5.js Web Editor' ;
43
43
}
44
44
45
- function warnIfUnsavedChanges ( props ) {
46
- // eslint-disable-line
47
- const { route } = props . route ;
48
- if (
49
- route &&
50
- route . action === 'PUSH' &&
51
- ( route . pathname === '/login' || route . pathname === '/signup' )
52
- ) {
53
- // don't warn
54
- props . persistState ( ) ;
55
- window . onbeforeunload = null ;
56
- } else if (
57
- route &&
58
- ( props . location . pathname === '/login' ||
59
- props . location . pathname === '/signup' )
60
- ) {
61
- // don't warn
62
- props . persistState ( ) ;
63
- window . onbeforeunload = null ;
64
- } else if ( props . ide . unsavedChanges ) {
45
+ function warnIfUnsavedChanges ( props , nextLocation ) {
46
+ const toAuth = nextLocation &&
47
+ nextLocation . action === 'PUSH' &&
48
+ ( nextLocation . pathname === '/login' || nextLocation . pathname === '/signup' ) ;
49
+ const onAuth = nextLocation &&
50
+ ( props . location . pathname === '/login' || props . location . pathname === '/signup' ) ;
51
+ if ( props . ide . unsavedChanges && ( ! toAuth && ! onAuth ) ) {
65
52
if ( ! window . confirm ( props . t ( 'Nav.WarningUnsavedChanges' ) ) ) {
66
53
return false ;
67
54
}
68
- props . setUnsavedChanges ( false ) ;
69
55
return true ;
70
56
}
71
57
return true ;
@@ -103,7 +89,8 @@ class IDEView extends React.Component {
103
89
this . handleUnsavedChanges
104
90
) ;
105
91
106
- window . onbeforeunload = this . handleUnsavedChanges ;
92
+ // window.onbeforeunload = this.handleUnsavedChanges;
93
+ window . addEventListener ( 'beforeunload' , this . handleBeforeUnload ) ;
107
94
108
95
this . autosaveInterval = null ;
109
96
}
@@ -247,7 +234,16 @@ class IDEView extends React.Component {
247
234
}
248
235
}
249
236
250
- handleUnsavedChanges = ( ) => warnIfUnsavedChanges ( this . props ) ;
237
+ handleUnsavedChanges = nextLocation => warnIfUnsavedChanges ( this . props , nextLocation ) ;
238
+
239
+ handleBeforeUnload = ( e ) => {
240
+ const confirmationMessage = this . props . t ( 'Nav.WarningUnsavedChanges' ) ;
241
+ if ( this . props . ide . unsavedChanges ) {
242
+ ( e || window . event ) . returnValue = confirmationMessage ;
243
+ return confirmationMessage ;
244
+ }
245
+ return null ;
246
+ }
251
247
252
248
render ( ) {
253
249
return (
0 commit comments