Skip to content

Commit

Permalink
save: Add document saving to local storage. Closes jbt#7.
Browse files Browse the repository at this point in the history
  • Loading branch information
aero31aero committed Feb 4, 2017
1 parent 035544c commit 8bd0a00
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<p id="openbutton" class="navbutton left" onclick="document.getElementById('fileInput').click();">Open</p>
<input id="fileInput" type="file" class="hidden" accept=".md,.mdown,.txt,.markdown"/>
<p id="savebutton" class="navbutton left" onclick="showMenu()">Save</p>
<p id="browsersavebutton" class="navbutton left" onclick="saveInBrowser()">Browser Save(Experimental)</p>
<p id="sharebutton" class="navbutton left" onclick="updateHash()">Generate Shareable URL</p>
<p id="nightbutton" class="navbutton left" onclick="toggleNightMode(this)">Night Mode</p>
<p id="readbutton" class="navbutton left" onclick="toggleReadMode(this)">Reading Mode</p>
Expand Down
51 changes: 37 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ function update(e) {
//If a title is added to the document it will be the new document.title, otherwise use default
var headerElements = document.querySelectorAll('h1');
if (headerElements.length > 0 && headerElements[0].textContent.length > 0) {
title = headerElements[0].textContent;
title = headerElements[0].textContent;
} else {
title = 'Markdown Editor'
title = 'Markdown Editor'
}

//To avoid to much title changing we check if is not the same as before
oldTitle = document.title;
if(oldTitle != title){
if (oldTitle != title) {
oldTitle = title;
document.title = title;
}
Expand Down Expand Up @@ -107,12 +107,12 @@ document.addEventListener('drop', function(e) {

//Print the document named as the document title encoded to avoid strange chars and spaces
function saveAsMarkdown() {
save(editor.getValue(), document.title.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/\s]/gi, '')+".md");
save(editor.getValue(), document.title.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/\s]/gi, '') + ".md");
}

//Print the document named as the document title encoded to avoid strange chars and spaces
function saveAsHtml() {
save(document.getElementById('out').innerHTML, document.title.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/\s]/gi, '')+".html");
save(document.getElementById('out').innerHTML, document.title.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/\s]/gi, '') + ".html");
}

document.getElementById('saveas-markdown').addEventListener('click', function() {
Expand Down Expand Up @@ -232,9 +232,34 @@ function manageSpiltView(mq) {
}
}

function saveInBrowser() {
var text = editor.getValue();
if (localStorage.getItem('content')) {
swal({
title: "Existing Data Detected",
text: "You will overwrite the data previously saved!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, overwrite!",
closeOnConfirm: false
},
function() {
localStorage.setItem('content', text);
swal("Saved", "Your Document has been saved.", "success");
});
} else {
localStorage.setItem('content', text);
swal("Saved", "Your Document has been saved.", "success");
}
console.log("Saved");
}

function toggleEditorView(button) {
button.classList.toggle('selected');
manageSpiltView({ matches: true });
manageSpiltView({
matches: true
});
}

function toggleNightMode(button) {
Expand Down Expand Up @@ -274,16 +299,14 @@ function start() {
)
))
);
update(editor);
editor.focus();
}
} else {
update(editor);
editor.focus();
} else if (localStorage.getItem('content')) {
editor.setValue(localStorage.getItem('content'));
}

update(editor);
editor.focus();
initEditor();
document.getElementById('fileInput').addEventListener('change', openFile, false);
}

start();
start();

0 comments on commit 8bd0a00

Please sign in to comment.