mirror of
https://github.com/sparksuite/simplemde-markdown-editor.git
synced 2024-06-08 08:52:11 +02:00
Autosave improvements (see description)
Checks that localStorage is available for all features, logs error if not Added function to clear the autosaved value Prefixed the autosave localStorage key with “smde_”
This commit is contained in:
parent
a70b4978cb
commit
a64f07e1c3
11
README.md
11
README.md
|
@ -221,14 +221,15 @@ simplemde.codemirror.on("change", function(){
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
## State methods
|
## Useful methods
|
||||||
The following methods will let you check on the state of the editor.
|
The following self-explanatory methods may be of use while developing with SimpleMDE.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var simplemde = new SimpleMDE();
|
var simplemde = new SimpleMDE();
|
||||||
simplemde.isPreviewActive();
|
simplemde.isPreviewActive(); // returns boolean
|
||||||
simplemde.isSideBySideActive();
|
simplemde.isSideBySideActive(); // returns boolean
|
||||||
simplemde.isFullscreenActive();
|
simplemde.isFullscreenActive(); // returns boolean
|
||||||
|
simplemde.clearAutosavedValue(); // no returned value
|
||||||
```
|
```
|
||||||
|
|
||||||
## How it works
|
## How it works
|
||||||
|
|
|
@ -999,52 +999,67 @@ SimpleMDE.prototype.render = function(el) {
|
||||||
};
|
};
|
||||||
|
|
||||||
SimpleMDE.prototype.autosave = function() {
|
SimpleMDE.prototype.autosave = function() {
|
||||||
var simplemde = this;
|
|
||||||
|
|
||||||
if(this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") {
|
|
||||||
console.log("SimpleMDE: You must set a uniqueId to use the autosave feature");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(simplemde.element.form != null && simplemde.element.form != undefined) {
|
|
||||||
simplemde.element.form.addEventListener("submit", function() {
|
|
||||||
localStorage.setItem(simplemde.options.autosave.uniqueId, "");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if(this.options.autosave.loaded !== true) {
|
|
||||||
if(typeof localStorage.getItem(this.options.autosave.uniqueId) == "string" && localStorage.getItem(this.options.autosave.uniqueId) != "")
|
|
||||||
this.codemirror.setValue(localStorage.getItem(this.options.autosave.uniqueId));
|
|
||||||
|
|
||||||
this.options.autosave.loaded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(localStorage) {
|
if(localStorage) {
|
||||||
localStorage.setItem(this.options.autosave.uniqueId, simplemde.value());
|
var simplemde = this;
|
||||||
}
|
|
||||||
|
|
||||||
var el = document.getElementById("autosaved");
|
if(this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") {
|
||||||
if(el != null && el != undefined && el != "") {
|
console.log("SimpleMDE: You must set a uniqueId to use the autosave feature");
|
||||||
var d = new Date();
|
return;
|
||||||
var hh = d.getHours();
|
|
||||||
var m = d.getMinutes();
|
|
||||||
var dd = "am";
|
|
||||||
var h = hh;
|
|
||||||
if(h >= 12) {
|
|
||||||
h = hh - 12;
|
|
||||||
dd = "pm";
|
|
||||||
}
|
}
|
||||||
if(h == 0) {
|
|
||||||
h = 12;
|
if(simplemde.element.form != null && simplemde.element.form != undefined) {
|
||||||
|
simplemde.element.form.addEventListener("submit", function() {
|
||||||
|
localStorage.removeItem("smde_" + simplemde.options.autosave.uniqueId);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
m = m < 10 ? "0" + m : m;
|
|
||||||
|
|
||||||
el.innerHTML = "Autosaved: " + h + ":" + m + " " + dd;
|
if(this.options.autosave.loaded !== true) {
|
||||||
|
if(typeof localStorage.getItem("smde_" + this.options.autosave.uniqueId) == "string" && localStorage.getItem("smde_" + this.options.autosave.uniqueId) != "")
|
||||||
|
this.codemirror.setValue(localStorage.getItem("smde_" + this.options.autosave.uniqueId));
|
||||||
|
|
||||||
|
this.options.autosave.loaded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage.setItem("smde_" + this.options.autosave.uniqueId, simplemde.value());
|
||||||
|
|
||||||
|
var el = document.getElementById("autosaved");
|
||||||
|
if(el != null && el != undefined && el != "") {
|
||||||
|
var d = new Date();
|
||||||
|
var hh = d.getHours();
|
||||||
|
var m = d.getMinutes();
|
||||||
|
var dd = "am";
|
||||||
|
var h = hh;
|
||||||
|
if(h >= 12) {
|
||||||
|
h = hh - 12;
|
||||||
|
dd = "pm";
|
||||||
|
}
|
||||||
|
if(h == 0) {
|
||||||
|
h = 12;
|
||||||
|
}
|
||||||
|
m = m < 10 ? "0" + m : m;
|
||||||
|
|
||||||
|
el.innerHTML = "Autosaved: " + h + ":" + m + " " + dd;
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
simplemde.autosave();
|
||||||
|
}, this.options.autosave.delay || 10000);
|
||||||
|
} else {
|
||||||
|
console.log("SimpleMDE: localStorage not available, cannot autosave");
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
setTimeout(function() {
|
SimpleMDE.prototype.clearAutosavedValue = function() {
|
||||||
simplemde.autosave();
|
if(localStorage) {
|
||||||
}, this.options.autosave.delay || 10000);
|
if(this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") {
|
||||||
|
console.log("SimpleMDE: You must set a uniqueId to use the autosave feature");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage.removeItem("smde_" + this.options.autosave.uniqueId);
|
||||||
|
} else {
|
||||||
|
console.log("SimpleMDE: localStorage not available, cannot autosave");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
SimpleMDE.prototype.createSideBySide = function() {
|
SimpleMDE.prototype.createSideBySide = function() {
|
||||||
|
|
Loading…
Reference in a new issue