Skip to content

Commit 04352ad

Browse files
authored
Merge pull request #1649 from processing/bug/unsaved-changes
[#1633] Update beforeunload and route change handlers
2 parents a55505d + eadce50 commit 04352ad

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

client/modules/IDE/pages/IDEView.jsx

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,16 @@ function getTitle(props) {
4242
return id ? `p5.js Web Editor | ${props.project.name}` : 'p5.js Web Editor';
4343
}
4444

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)) {
6552
if (!window.confirm(props.t('Nav.WarningUnsavedChanges'))) {
6653
return false;
6754
}
68-
props.setUnsavedChanges(false);
6955
return true;
7056
}
7157
return true;
@@ -103,7 +89,8 @@ class IDEView extends React.Component {
10389
this.handleUnsavedChanges
10490
);
10591

106-
window.onbeforeunload = this.handleUnsavedChanges;
92+
// window.onbeforeunload = this.handleUnsavedChanges;
93+
window.addEventListener('beforeunload', this.handleBeforeUnload);
10794

10895
this.autosaveInterval = null;
10996
}
@@ -247,7 +234,16 @@ class IDEView extends React.Component {
247234
}
248235
}
249236

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+
}
251247

252248
render() {
253249
return (

0 commit comments

Comments
 (0)