diff --git a/CHANGELOG.md b/CHANGELOG.md index aa754fe..db7dba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Support for Node.js 14. - Preview without fullscreen (Thanks to [@nick-denry], [#196]). +- `autosave.saveOnChangeText` option to save the text only when modifying the content of the easymde instance (Thanks to [@firm1], [#181]). ### Fixed - Fix cursor displayed position on activity ([#183]). diff --git a/README.md b/README.md index 451c3c0..c05404e 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ easyMDE.value('New input for **EasyMDE**'); - **enabled**: If set to `true`, saves the text automatically. Defaults to `false`. - **delay**: Delay between saves, in milliseconds. Defaults to `10000` (10s). - **submit_delay**: Delay before assuming that submit of the form failed and saving the text, in milliseconds. Defaults to `autosave.delay` or `10000` (10s). + - **saveOnChangeText**: Saves the text only when the content has been changed. Without this parameter, the text will be saved periodically according to the `autosave.delay` parameter. - **uniqueId**: You must set a unique string identifier so that EasyMDE can autosave. Something that separates this from other instances of EasyMDE elsewhere on your website. - **timeFormat**: Set DateTimeFormat. More information see [DateTimeFormat instances](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat). Default `locale: en-US, format: hour:minute`. - **text**: Set text for autosave. @@ -210,6 +211,7 @@ var editor = new EasyMDE({ uniqueId: "MyUniqueID", delay: 1000, submit_delay: 5000, + saveOnChangeText: true, timeFormat: { locale: 'en-US', format: { diff --git a/src/js/easymde.js b/src/js/easymde.js index e5bba82..0ab7e46 100644 --- a/src/js/easymde.js +++ b/src/js/easymde.js @@ -2035,7 +2035,16 @@ EasyMDE.prototype.render = function (el) { this.gui.statusbar = this.createStatusbar(); } if (options.autosave != undefined && options.autosave.enabled === true) { - this.autosave(); + if(this.options.autosave.saveOnChangeText !== undefined && this.options.autosave.saveOnChangeText === true) { + this.autosave(false); // use to load localstorage content + this.codemirror.on('change', function () { + setTimeout(function () { + self.autosave(false); + }, self.options.autosave.submit_delay || self.options.autosave.delay || 1000); + }); + } else { + this.autosave(true); + } } this.gui.sideBySide = this.createSideBySide(); @@ -2066,7 +2075,7 @@ function isLocalStorageAvailable() { return true; } -EasyMDE.prototype.autosave = function () { +EasyMDE.prototype.autosave = function (repeatSaving) { if (isLocalStorageAvailable()) { var easyMDE = this; @@ -2084,9 +2093,11 @@ EasyMDE.prototype.autosave = function () { localStorage.removeItem('smde_' + easyMDE.options.autosave.uniqueId); // Restart autosaving in case the submit will be cancelled down the line - setTimeout(function () { - easyMDE.autosave(); - }, easyMDE.options.autosave.submit_delay || easyMDE.options.autosave.delay || 10000); + if (repeatSaving === true) { + setTimeout(function () { + easyMDE.autosave(repeatSaving); + }, easyMDE.options.autosave.submit_delay || easyMDE.options.autosave.delay || 10000); + } }); } @@ -2118,9 +2129,11 @@ EasyMDE.prototype.autosave = function () { el.innerHTML = save + dd; } - this.autosaveTimeoutId = setTimeout(function () { - easyMDE.autosave(); - }, this.options.autosave.delay || 10000); + if (repeatSaving === true) { + this.autosaveTimeoutId = setTimeout(function () { + easyMDE.autosave(repeatSaving); + }, this.options.autosave.delay || 10000); + } } else { console.log('EasyMDE: localStorage not available, cannot autosave'); }