0
\$\begingroup\$
try {
 var isSupported = !! new Blob();
} catch (e) {
 document.body.innerHTML = "<h1>Sorry your browser isn\'t supported :(</h1>";
}
var textarea = document.getElementById("textarea"),
 inputFile = document.getElementById("input-file"),
 appname = "notepad",
 untitled = "untitled.txt",
 isModified = false,
 filename;
window.onload = function() {
 if (localStorage.getItem("txt")) { // Load localStorage
 newNote(localStorage.getItem("txt"), localStorage.getItem("name"));
 } else {
 newNote();
 }
};
window.onunload = function() {
 if (isModified) { // Save localStorage
 localStorage.setItem("txt", textarea.value);
 localStorage.setItem("name", filename);
 } else {
 localStorage.clear();
 }
};
textarea.addEventListener("input", function() {
 isModified = true;
});
function changeDocTitle(newFilename) { // Change doc title
 filename = newFilename;
 document.title = filename + " - " + appname;
}
function dontSave() { // Confirm dont save
 if (confirm("You have unsaved changes that will be lost.")) {
 isModified = false;
 return true;
 }
}
function newNote(txt, name) { // New
 if (!isModified || dontSave()) {
 textarea.value = txt || "";
 changeDocTitle(name || untitled);
 if (textarea.value) {
 isModified = true;
 }
 }
 textarea.focus();
}
function openNote() { // Open
 if (!isModified || dontSave()) {
 inputFile.click();
 }
 textarea.focus();
}
inputFile.addEventListener("change", function() { // Load file
 var file = inputFile.files[0],
 reader = new FileReader();
 reader.onload = function() {
 newNote(reader.result, file.name);
 };
 reader.readAsText(file);
});
function rename() { // Rename
 var newFilename = prompt("Name this note:", filename);
 if (newFilename !== null) {
 if (newFilename === "") {
 changeDocTitle(untitled);
 } else {
 changeDocTitle(newFilename.lastIndexOf(".txt") == -1 ? newFilename + ".txt" : newFilename);
 }
 return true;
 }
}
function saveNote() { // Save
 if (rename()) {
 var blob = new Blob([textarea.value.replace(/\n/g, "\r\n")], {
 type: "text/plain;charset=utf-8"
 });
 saveAs(blob, filename);
 isModified = false;
 }
 textarea.focus();
}
function getStats() { // Stats
 var txt = textarea.value,
 txtStats = {};
 txtStats.chars = txt.length;
 txtStats.words = txt.split(/\S+/g).length - 1;
 txtStats.lines = txt.replace(/[^\n]/g, "").length + 1;
 return txtStats.lines + " lines, " + txtStats.words + " words, " + txtStats.chars + " chars";
}
document.addEventListener("keydown", function(e) { // Shortcuts
 var key = e.keyCode || e.which;
 if (e.altKey && e.shiftKey && key == 78) { // Alt+Shift+N
 e.preventDefault();
 newNote();
 }
 if (e.ctrlKey) { // Ctrl+
 switch (key) {
 case 79: // O
 e.preventDefault();
 openNote();
 break;
 case 83: // S
 e.preventDefault();
 saveNote();
 break;
 case 75: // K
 e.preventDefault();
 alert(getStats());
 break;
 case 191: // /
 e.preventDefault();
 alert("Help note for " + appname + " will be added soon!");
 break;
 }
 }
 if (key == 9) { // Tab
 e.preventDefault();
 var sStart = textarea.selectionStart,
 txt = textarea.value;
 textarea.value = txt.substring(0, sStart) + "\t" + txt.substring(textarea.selectionEnd);
 textarea.selectionEnd = sStart + 1;
 }
});
konijn
34.2k5 gold badges70 silver badges267 bronze badges
asked Oct 24, 2013 at 6:50
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

From a once over:

  • You do not need the \ in "<h1>Sorry your browser isn\'t supported :(</h1>" because your string is surrounded with double quotes
  • I would check immediately whether textarea and inputFile found something and give a proper error message when those elements are missing in the HTML
  • You are doing both proper event handling with textarea.addEventListener and old skool event handling with window.onload, stick to proper event handling
  • "txt" and "name" are the keys to interact with localStorage, these should be named constants
  • Personally, I would try also to store the cursor position when storing the text in localStorage, this would look nice
  • dontSave() { // Confirm dont save -> perhaps the function should be named differently ;)
  • saveAs is missing ?

All in all a promising start for a cool editor. It helped me to build a jsbin for this : http://jsbin.com/jetuh/1/edit

answered Apr 15, 2014 at 19:36
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.