diff --git a/index.html b/index.html index 11c1ae01e..7a75eed80 100755 --- a/index.html +++ b/index.html @@ -1326,6 +1326,9 @@ editor.on('storage:store', function(e) { console.log('STORE ', e); }) + editor.on('change:changesCount', function(e) { + console.log('update changesCount ', editor.getModel().get('changesCount')); + }) editor.on('styleManager:change:text-shadow', function(view) { var model = view.model; diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index 9c721b2e7..0614530b7 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -63,7 +63,7 @@ module.exports = Backbone.Model.extend({ this.initUndoManager(); this.on('change:selectedComponent', this.componentSelected, this); - this.on('change:changesCount', this.updateBeforeUnload, this); + this.on('change:changesCount', this.updateChanges, this); }, /** @@ -100,21 +100,28 @@ module.exports = Backbone.Model.extend({ } }, + /** * Set the alert before unload in case it's requested * and there are unsaved changes * @private */ - updateBeforeUnload() { - var changes = this.get('changesCount'); + updateChanges() { + const stm = this.get('StorageManager'); + const changes = this.get('changesCount'); if (this.config.noticeOnUnload && changes) { window.onbeforeunload = e => 1; } else { window.onbeforeunload = null; } + + if (stm.isAutosave() && changes >= stm.getStepsBeforeSave()) { + this.store(); + } }, + /** * Load generic module * @param {String} moduleName Module name @@ -199,17 +206,22 @@ module.exports = Backbone.Model.extend({ timedInterval && clearInterval(timedInterval); timedInterval = setTimeout(() => { + if (!opt.avoidStore) { + this.set('changesCount', this.get('changesCount') + 1, opt) + } + /* var count = this.get('changesCount') + 1; var stm = this.get('StorageManager'); - this.set('changesCount', count); if (!stm.isAutosave() || count < stm.getStepsBeforeSave()) { return; } if (!opt.avoidStore) { + this.set('changesCount', count) this.store(); } + */ }, 0); },