Advertisement
Guest User

notes to localstorage

a guest
Jan 5th, 2014
1,748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.59 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="utf-8">
  4. <link rel="shortcut icon" href="http://g.etfv.co/http://www.sublimetext.com"/><!-- Add favicon from sublimetext -->
  5. </head>
  6. <title>Local Text Editor</title>
  7. <style>
  8.     body { background:#E8E8E8 }
  9.     .note { background:#FFF; border:1px solid #DADADA; box-shadow: inset 1px 1px 0 rgba(0, 0, 0, 0.1),inset 0 -1px 0 rgba(0, 0, 0, 0.07); color:#666; font-family:helvetica; font-size:1rem; line-height:1.4; margin:3rem auto; padding:0.4rem 2.5rem 2rem; position:relative; width:45rem; }
  10.     .note span { background:#E8E8E8; color:#fff; cursor:pointer; display:inline-block; font-weight:bold; height:25px; left:0; position:absolute; text-align:center; top:0; width:25px; }
  11.     .note span + span { bottom:0px; top:auto; }
  12.     .note span + span + span { bottom:auto; left:auto; right:0; top:0px; }
  13. </style>
  14. <script>
  15.     var noteHTML = '<div class="note"><span onClick="addNewNoteBefore(this)">+</span><span onClick="addNewNoteAfter(this)">+</span><span onClick="deleteNote(this)">x</span><h2 contenteditable spellcheck="false">New note</h2><div contenteditable spellcheck="false">Your note</div></div>';
  16.     // Load notes from localstorage on load
  17.     window.onload=function(){
  18.         a=window.localStorage['notes'];
  19.         if(a) document.body.innerHTML=a;
  20.         // localStorage.clear();
  21.     };
  22.  
  23.     document.addEventListener("keydown", function(e) {
  24.     if(e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) { // Change page title and body background color on CTRL+S (CMD + S on mac) and save the text to local storage
  25.      e.preventDefault();
  26.             window.localStorage['notes']=document.body.innerHTML;
  27.             document.title = 'Saved';
  28.             document.body.style.backgroundColor = "#E8E8E8";
  29.     }
  30.     else { // Change page title and body background color on key pres
  31.             document.title = 'Unsaved';
  32.             document.body.style.backgroundColor = "#F6E8E8";
  33.     }
  34.   }, false);
  35.     function deleteNote(e) {
  36.       e.parentNode.parentNode.removeChild(e.parentNode);
  37.     }
  38.     function addNewNoteBefore(e) {
  39.     var div = e.parentNode;
  40.         div.insertAdjacentHTML('beforeBegin', noteHTML);
  41.   }
  42.     function addNewNoteAfter(e) {
  43.     var div = e.parentNode;
  44.         div.insertAdjacentHTML('afterEnd', noteHTML);
  45.   }
  46. </script>
  47. <body>
  48.     <div class="note">
  49.         <span onClick="addNewNoteBefore(this)">+</span>
  50.         <span onClick="addNewNoteAfter(this)">+</span>
  51.         <span onClick="deleteNote(this)">x</span>
  52.         <h2 contenteditable spellcheck="false">Note Title</h2>
  53.         <div contenteditable spellcheck="false">
  54.             Your note... <br /><br />
  55.             (Press CTRL+S or CMD+S on mac to save to localstorage)
  56.         </div>
  57.     </div>
  58. </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement