diff --git a/modules/core/history.js b/modules/core/history.js index f7b50fcf9..e621c6d92 100644 --- a/modules/core/history.js +++ b/modules/core/history.js @@ -16,6 +16,10 @@ import { export function coreHistory(context) { var dispatch = d3_dispatch('change', 'merge', 'restore', 'undone', 'redone'); var lock = utilSessionMutex('lock'); + + // restorable if iD not open in another window/tab and a saved history exists in localStorage + var _hasUnresolvedRestorableChanges = lock.lock() && !!context.storage(getKey('saved_history')); + var duration = 150; var _imageryUsed = []; var _photoOverlaysUsed = []; @@ -635,15 +639,32 @@ export function coreHistory(context) { }, + lock: function() { + return lock.lock(); + }, + + + unlock: function() { + lock.unlock(); + }, + + save: function() { - if (lock.locked()) context.storage(getKey('saved_history'), history.toJSON() || null); + if (lock.locked() && + // don't overwrite existing, unresolved changes + !_hasUnresolvedRestorableChanges) { + + context.storage(getKey('saved_history'), history.toJSON() || null); + } return history; }, + // delete the history version saved in localStorage clearSaved: function() { context.debouncedSave.cancel(); if (lock.locked()) { + _hasUnresolvedRestorableChanges = false; context.storage(getKey('saved_history'), null); // clear the changeset metadata associated with the saved history @@ -655,29 +676,18 @@ export function coreHistory(context) { }, - lock: function() { - return lock.lock(); - }, - - - unlock: function() { - lock.unlock(); - }, - - - // is iD not open in another window and it detects that - // there's a history stored in localStorage that's recoverable? - restorableChanges: function() { - return lock.locked() && !!context.storage(getKey('saved_history')); + hasRestorableChanges: function() { + return _hasUnresolvedRestorableChanges; }, // load history from a version stored in localStorage restore: function() { - if (!lock.locked()) return; - - var json = context.storage(getKey('saved_history')); - if (json) history.fromJSON(json, true); + if (lock.locked()) { + _hasUnresolvedRestorableChanges = false; + var json = context.storage(getKey('saved_history')); + if (json) history.fromJSON(json, true); + } }, diff --git a/modules/ui/restore.js b/modules/ui/restore.js index ae15ee0f8..cb5a382ba 100644 --- a/modules/ui/restore.js +++ b/modules/ui/restore.js @@ -4,7 +4,7 @@ import { uiModal } from './modal'; export function uiRestore(context) { return function(selection) { - if (!context.history().lock() || !context.history().restorableChanges()) return; + if (!context.history().hasRestorableChanges()) return; let modalSelection = uiModal(selection, true); diff --git a/modules/ui/splash.js b/modules/ui/splash.js index 8807b4ed8..11e395eac 100644 --- a/modules/ui/splash.js +++ b/modules/ui/splash.js @@ -8,7 +8,7 @@ export function uiSplash(context) { // Exception - if there are restorable changes, skip this splash screen. // This is because we currently only support one `uiModal` at a time // and we need to show them `uiRestore`` instead of this one. - if (context.history().lock() && context.history().restorableChanges()) return; + if (context.history().hasRestorableChanges()) return; // If user has not seen this version of the privacy policy, show the splash again. let updateMessage = '';