From 0ca2c9e116cefba0b45d642c7a90a20363d22140 Mon Sep 17 00:00:00 2001 From: MOHAN M U Date: Sun, 31 Mar 2019 00:11:50 +0530 Subject: [PATCH 1/8] added KeyBindings --- index.js | 591 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 372 insertions(+), 219 deletions(-) diff --git a/index.js b/index.js index 873179a..d56ab57 100644 --- a/index.js +++ b/index.js @@ -1,337 +1,490 @@ var URL = window.URL || window.webkitURL || window.mozURL || window.msURL; -navigator.saveBlob = navigator.saveBlob || navigator.msSaveBlob || navigator.mozSaveBlob || navigator.webkitSaveBlob; -window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs; +navigator.saveBlob = + navigator.saveBlob || + navigator.msSaveBlob || + navigator.mozSaveBlob || + navigator.webkitSaveBlob; +window.saveAs = + window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs; // Because highlight.js is a bit awkward at times var languageOverrides = { - js: 'javascript', - html: 'xml' + js: 'javascript', + html: 'xml', }; var livestyles; emojify.setConfig({ - img_dir: 'emoji' + img_dir: 'emoji', }); var md = markdownit({ - html: true, - highlight: function(code, lang) { - if (languageOverrides[lang]) lang = languageOverrides[lang]; - if (lang && hljs.getLanguage(lang)) { - try { - return hljs.highlight(lang, code).value; - } catch (e) {} - } - return ''; - } - }) - .use(markdownitFootnote); + html: true, + highlight: function(code, lang) { + if (languageOverrides[lang]) lang = languageOverrides[lang]; + if (lang && hljs.getLanguage(lang)) { + try { + return hljs.highlight(lang, code).value; + } catch (e) {} + } + return ''; + }, +}).use(markdownitFootnote); var hashto; function update(e) { - setOutput(e.getValue()); - - //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; - } else { - title = 'Markdown Editor' - } - - //To avoid to much title changing we check if is not the same as before - oldTitle = document.title; - if (oldTitle != title) { - oldTitle = title; - document.title = title; - } - //clearTimeout(hashto); - //hashto = setTimeout(updateHash, 1000); + setOutput(e.getValue()); + + //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; + } else { + title = 'Markdown Editor'; + } + + //To avoid to much title changing we check if is not the same as before + oldTitle = document.title; + if (oldTitle != title) { + oldTitle = title; + document.title = title; + } + //clearTimeout(hashto); + //hashto = setTimeout(updateHash, 1000); } function setOutput(val) { - val = val.replace(/((.*?\n)*?.*?)<\/equation>/ig, function(a, b) { - return ''; - }); + val = val.replace(/((.*?\n)*?.*?)<\/equation>/gi, function(a, b) { + return ( + '' + ); + }); - var out = document.getElementById('out'); - var old = out.cloneNode(true); - out.innerHTML = md.render(val); - emojify.run(out); + var out = document.getElementById('out'); + var old = out.cloneNode(true); + out.innerHTML = md.render(val); + emojify.run(out); - var allold = old.getElementsByTagName("*"); - if (allold === undefined) return; + var allold = old.getElementsByTagName('*'); + if (allold === undefined) return; - var allnew = out.getElementsByTagName("*"); - if (allnew === undefined) return; + var allnew = out.getElementsByTagName('*'); + if (allnew === undefined) return; - for (var i = 0, max = Math.min(allold.length, allnew.length); i < max; i++) { - if (!allold[i].isEqualNode(allnew[i])) { - out.scrollTop = allnew[i].offsetTop; - return; - } + for (var i = 0, max = Math.min(allold.length, allnew.length); i < max; i++) { + if (!allold[i].isEqualNode(allnew[i])) { + out.scrollTop = allnew[i].offsetTop; + return; } + } } CodeMirrorSpellChecker({ - codeMirrorInstance: CodeMirror, + codeMirrorInstance: CodeMirror, }); var editor = CodeMirror.fromTextArea(document.getElementById('code'), { - mode: "spell-checker", - backdrop: "gfm", - lineNumbers: false, - matchBrackets: true, - lineWrapping: true, - theme: 'base16-light', - extraKeys: { - "Enter": "newlineAndIndentContinueMarkdownList" - } + mode: 'spell-checker', + backdrop: 'gfm', + lineNumbers: false, + matchBrackets: true, + lineWrapping: true, + theme: 'base16-light', + extraKeys: { + Enter: 'newlineAndIndentContinueMarkdownList', + }, }); editor.on('change', update); -function selectionChanger(selection,operator,endoperator){ - if(selection == ""){ - return operator; - } - if(!endoperator){ - endoperator = operator - } - var isApplied = selection.slice(0, 2) === operator && seisAppliedection.slice(-2) === endoperator; - var finaltext = isApplied ? selection.slice(2, -2) : operator + selection + endoperator; - return finaltext; +function selectionChanger(selection, operator, endoperator) { + if (selection == '') { + return operator; + } + if (!endoperator) { + endoperator = operator; + } + var isApplied = + selection.slice(0, 2) === operator && + seisAppliedection.slice(-2) === endoperator; + var finaltext = isApplied + ? selection.slice(2, -2) + : operator + selection + endoperator; + return finaltext; } editor.addKeyMap({ - // bold - 'Ctrl-B': function(cm) { - cm.replaceSelection(selectionChanger(cm.getSelection(),'**')); - }, - // italic - 'Ctrl-I': function(cm) { - cm.replaceSelection(selectionChanger(cm.getSelection(),'_')); - }, - // code - 'Ctrl-K': function(cm) { - cm.replaceSelection(selectionChanger(cm.getSelection(),'`')); - }, - // keyboard shortcut - 'Ctrl-L': function(cm) { - cm.replaceSelection(selectionChanger(cm.getSelection(),'','')); + // bold + 'Ctrl-B': function(cm) { + var selection = cm.getSelection(); + cm.replaceSelection('**' + selection + '**'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line, cursorPos.ch - 2); + } + }, + // italic + 'Ctrl-I': function(cm) { + var selection = cm.getSelection(); + cm.replaceSelection('_' + selection + '_'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // code + 'Ctrl-K': function(cm) { + var selection = cm.getSelection(); + cm.replaceSelection('`' + selection + '`'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // keyboard shortcut + 'Ctrl-L': function(cm) { + cm.replaceSelection(selectionChanger(cm.getSelection(), '', '')); + }, + //Heading 1 + 'Ctrl-Alt-1': function(cm) { + cm.replaceSelection('# ' + cm.getSelection()); + }, + //Heading 2 + 'Ctrl-Alt-2': function(cm) { + cm.replaceSelection('## ' + cm.getSelection()); + }, + //Heading 3 + 'Ctrl-Alt-3': function(cm) { + cm.replaceSelection('### ' + cm.getSelection()); + }, + //Heading 4 + 'Ctrl-Alt-4': function(cm) { + cm.replaceSelection('#### ' + cm.getSelection()); + }, + //Heading 5 + 'Ctrl-Alt-5': function(cm) { + cm.replaceSelection('##### ' + cm.getSelection()); + }, + //Heading 6 + 'Ctrl-Alt-6': function(cm) { + cm.replaceSelection('###### ' + cm.getSelection()); + }, + // Links + 'Shift-Ctrl-L': function(cm) { + var selection = cm.getSelection(); + var text = ''; + var link = ''; + + if (selection.match(/^https?:\/\//)) { + link = selection; + } else { + text = selection; + } + cm.replaceSelection('[' + text + '](' + link + ')'); + + var cursorPos = cm.getCursor(); + if (!selection) { + cm.setCursor(cursorPos.line, cursorPos.ch - 3); + } else if (link) { + cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); + } else { + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // Insert Image + 'Shift-Ctrl-I': function(cm) { + var selection = cm.getSelection(); + var text = ''; + var link = ''; + + if (selection.match(/^https?:\/\//)) { + link = selection; + } else { + text = selection; + } + cm.replaceSelection('![' + text + '](' + link + ')'); + + var cursorPos = cm.getCursor(); + if (!selection) { + cm.setCursor(cursorPos.line, cursorPos.ch - 3); + } else if (link) { + cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); + } else { + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + //Unordered list + 'Shift-U': function(cm) { + cm.replaceSelection('* ' + cm.getSelection()); + }, + //Ordered list + 'Shift-O': function(cm) { + cm.replaceSelection('1. ' + cm.getSelection()); + }, + //Blockquote + 'Shift-Ctrl-.': function(cm) { + cm.replaceSelection('> ' + cm.getSelection()); + }, + //codeblock + "Shift-Ctrl-'": function(cm) { + var selection = cm.getSelection(); + cm.replaceSelection('```' + selection + '```'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line, cursorPos.ch - 3); } + }, }); -document.addEventListener('drop', function(e) { +document.addEventListener( + 'drop', + function(e) { e.preventDefault(); e.stopPropagation(); var reader = new FileReader(); reader.onload = function(e) { - editor.setValue(e.target.result); + editor.setValue(e.target.result); }; reader.readAsText(e.dataTransfer.files[0]); -}, false); + }, + false, +); //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() { +document + .getElementById('saveas-markdown') + .addEventListener('click', function() { saveAsMarkdown(); hideMenu(); -}); + }); document.getElementById('saveas-html').addEventListener('click', function() { - saveAsHtml(); - hideMenu(); + saveAsHtml(); + hideMenu(); }); function save(code, name) { - var blob = new Blob([code], { - type: 'text/plain' - }); - if (window.saveAs) { - window.saveAs(blob, name); - } else if (navigator.saveBlob) { - navigator.saveBlob(blob, name); - } else { - url = URL.createObjectURL(blob); - var link = document.createElement("a"); - link.setAttribute("href", url); - link.setAttribute("download", name); - var event = document.createEvent('MouseEvents'); - event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); - link.dispatchEvent(event); - } + var blob = new Blob([code], { + type: 'text/plain', + }); + if (window.saveAs) { + window.saveAs(blob, name); + } else if (navigator.saveBlob) { + navigator.saveBlob(blob, name); + } else { + url = URL.createObjectURL(blob); + var link = document.createElement('a'); + link.setAttribute('href', url); + link.setAttribute('download', name); + var event = document.createEvent('MouseEvents'); + event.initMouseEvent( + 'click', + true, + true, + window, + 1, + 0, + 0, + 0, + 0, + false, + false, + false, + false, + 0, + null, + ); + link.dispatchEvent(event); + } } var menuVisible = false; var menu = document.getElementById('menu'); function showMenu() { - menuVisible = true; - menu.style.display = 'block'; + menuVisible = true; + menu.style.display = 'block'; } function hideMenu() { - menuVisible = false; - menu.style.display = 'none'; + menuVisible = false; + menu.style.display = 'none'; } function openFile(evt) { - if (window.File && window.FileReader && window.FileList && window.Blob) { - var files = evt.target.files; - console.log(files); - var reader = new FileReader(); - reader.onload = function(file) { - console.log(file.target.result); - editor.setValue(file.target.result); - return true; - }; - reader.readAsText(files[0]); - - } else { - alert('The File APIs are not fully supported in this browser.'); - } + if (window.File && window.FileReader && window.FileList && window.Blob) { + var files = evt.target.files; + console.log(files); + var reader = new FileReader(); + reader.onload = function(file) { + console.log(file.target.result); + editor.setValue(file.target.result); + return true; + }; + reader.readAsText(files[0]); + } else { + alert('The File APIs are not fully supported in this browser.'); + } } document.getElementById('close-menu').addEventListener('click', function() { - hideMenu(); + hideMenu(); }); document.addEventListener('keydown', function(e) { - if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { - if ( localStorage.getItem('content') == editor.getValue() ) { - e.preventDefault(); - return false; - } - e.shiftKey ? showMenu() : saveInBrowser(); - - e.preventDefault(); - return false; + if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { + if (localStorage.getItem('content') == editor.getValue()) { + e.preventDefault(); + return false; } + e.shiftKey ? showMenu() : saveInBrowser(); - if (e.keyCode === 27 && menuVisible) { - hideMenu(); + e.preventDefault(); + return false; + } - e.preventDefault(); - return false; - } + if (e.keyCode === 27 && menuVisible) { + hideMenu(); + + e.preventDefault(); + return false; + } }); function clearEditor() { - editor.setValue(""); + editor.setValue(''); } 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 { + 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"); - } - console.log("Saved"); + 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 toggleNightMode(button) { - button.classList.toggle('selected'); - document.getElementById('toplevel').classList.toggle('nightmode'); + button.classList.toggle('selected'); + document.getElementById('toplevel').classList.toggle('nightmode'); } function toggleReadMode(button) { - button.classList.toggle('selected'); - document.getElementById('out').classList.toggle('focused'); - document.getElementById('in').classList.toggle('hidden'); + button.classList.toggle('selected'); + document.getElementById('out').classList.toggle('focused'); + document.getElementById('in').classList.toggle('hidden'); } function toggleSpellCheck(button) { - button.classList.toggle('selected'); - document.body.classList.toggle('no-spellcheck'); + button.classList.toggle('selected'); + document.body.classList.toggle('no-spellcheck'); } function updateHash() { - window.location.hash = btoa( // base64 so url-safe - RawDeflate.deflate( // gzip - unescape(encodeURIComponent( // convert to utf8 - editor.getValue() - )) - ) - ); + window.location.hash = btoa( + // base64 so url-safe + RawDeflate.deflate( + // gzip + unescape( + encodeURIComponent( + // convert to utf8 + editor.getValue(), + ), + ), + ), + ); } function processQueryParams() { - var params = window.location.search.split('?')[1]; - if (window.location.hash) { - document.getElementById('readbutton').click(); // Show reading view + var params = window.location.search.split('?')[1]; + if (window.location.hash) { + document.getElementById('readbutton').click(); // Show reading view + } + if (params) { + var obj = {}; + params.split('&').forEach(function(elem) { + obj[elem.split('=')[0]] = elem.split('=')[1]; + }); + if (obj.reading === 'false') { + document.getElementById('readbutton').click(); // Hide reading view } - if (params) { - var obj = {}; - params.split('&').forEach(function(elem) { - obj[elem.split('=')[0]] = elem.split('=')[1]; - }); - if (obj.reading === 'false') { - document.getElementById('readbutton').click(); // Hide reading view - } - if (obj.dark === 'true') { - document.getElementById('nightbutton').click(); // Show night view - } + if (obj.dark === 'true') { + document.getElementById('nightbutton').click(); // Show night view } + } } function start() { - processQueryParams(); - if (window.location.hash) { - var h = window.location.hash.replace(/^#/, ''); - if (h.slice(0, 5) == 'view:') { - setOutput(decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5)))))); - document.body.className = 'view'; - } else { - editor.setValue( - decodeURIComponent(escape( - RawDeflate.inflate( - atob( - h - ) - ) - )) - ); - } - } else if (localStorage.getItem('content')) { - editor.setValue(localStorage.getItem('content')); + processQueryParams(); + if (window.location.hash) { + var h = window.location.hash.replace(/^#/, ''); + if (h.slice(0, 5) == 'view:') { + setOutput( + decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5))))), + ); + document.body.className = 'view'; + } else { + editor.setValue(decodeURIComponent(escape(RawDeflate.inflate(atob(h))))); } - update(editor); - editor.focus(); - document.getElementById('fileInput').addEventListener('change', openFile, false); + } else if (localStorage.getItem('content')) { + editor.setValue(localStorage.getItem('content')); + } + update(editor); + editor.focus(); + document + .getElementById('fileInput') + .addEventListener('change', openFile, false); } -window.addEventListener("beforeunload", function (e) { - var confirmationMessage = 'It looks like you have been editing something. ' - + 'If you leave before saving, your changes will be lost.'; - (e || window.event).returnValue = confirmationMessage; //Gecko + IE - return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc. +window.addEventListener('beforeunload', function(e) { + var confirmationMessage = + 'It looks like you have been editing something. ' + + 'If you leave before saving, your changes will be lost.'; + (e || window.event).returnValue = confirmationMessage; //Gecko + IE + return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc. }); start(); From 172c8a04e634daa6870e8daba671731db51557a5 Mon Sep 17 00:00:00 2001 From: MOHAN M U Date: Sun, 31 Mar 2019 00:31:27 +0530 Subject: [PATCH 2/8] Update index.js --- index.js | 457 +++++++++++++++++++++++++------------------------------ 1 file changed, 204 insertions(+), 253 deletions(-) diff --git a/index.js b/index.js index d56ab57..6103f95 100644 --- a/index.js +++ b/index.js @@ -1,120 +1,108 @@ var URL = window.URL || window.webkitURL || window.mozURL || window.msURL; -navigator.saveBlob = - navigator.saveBlob || - navigator.msSaveBlob || - navigator.mozSaveBlob || - navigator.webkitSaveBlob; -window.saveAs = - window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs; +navigator.saveBlob = navigator.saveBlob || navigator.msSaveBlob || navigator.mozSaveBlob || navigator.webkitSaveBlob; +window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs; // Because highlight.js is a bit awkward at times var languageOverrides = { - js: 'javascript', - html: 'xml', + js: 'javascript', + html: 'xml' }; var livestyles; emojify.setConfig({ - img_dir: 'emoji', + img_dir: 'emoji' }); var md = markdownit({ - html: true, - highlight: function(code, lang) { - if (languageOverrides[lang]) lang = languageOverrides[lang]; - if (lang && hljs.getLanguage(lang)) { - try { - return hljs.highlight(lang, code).value; - } catch (e) {} - } - return ''; - }, -}).use(markdownitFootnote); + html: true, + highlight: function(code, lang) { + if (languageOverrides[lang]) lang = languageOverrides[lang]; + if (lang && hljs.getLanguage(lang)) { + try { + return hljs.highlight(lang, code).value; + } catch (e) {} + } + return ''; + } + }) + .use(markdownitFootnote); var hashto; function update(e) { - setOutput(e.getValue()); - - //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; - } else { - title = 'Markdown Editor'; - } - - //To avoid to much title changing we check if is not the same as before - oldTitle = document.title; - if (oldTitle != title) { - oldTitle = title; - document.title = title; - } - //clearTimeout(hashto); - //hashto = setTimeout(updateHash, 1000); + setOutput(e.getValue()); + + //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; + } else { + title = 'Markdown Editor' + } + + //To avoid to much title changing we check if is not the same as before + oldTitle = document.title; + if (oldTitle != title) { + oldTitle = title; + document.title = title; + } + //clearTimeout(hashto); + //hashto = setTimeout(updateHash, 1000); } function setOutput(val) { - val = val.replace(/((.*?\n)*?.*?)<\/equation>/gi, function(a, b) { - return ( - '' - ); - }); + val = val.replace(/((.*?\n)*?.*?)<\/equation>/ig, function(a, b) { + return ''; + }); - var out = document.getElementById('out'); - var old = out.cloneNode(true); - out.innerHTML = md.render(val); - emojify.run(out); + var out = document.getElementById('out'); + var old = out.cloneNode(true); + out.innerHTML = md.render(val); + emojify.run(out); - var allold = old.getElementsByTagName('*'); - if (allold === undefined) return; + var allold = old.getElementsByTagName("*"); + if (allold === undefined) return; - var allnew = out.getElementsByTagName('*'); - if (allnew === undefined) return; + var allnew = out.getElementsByTagName("*"); + if (allnew === undefined) return; - for (var i = 0, max = Math.min(allold.length, allnew.length); i < max; i++) { - if (!allold[i].isEqualNode(allnew[i])) { - out.scrollTop = allnew[i].offsetTop; - return; + for (var i = 0, max = Math.min(allold.length, allnew.length); i < max; i++) { + if (!allold[i].isEqualNode(allnew[i])) { + out.scrollTop = allnew[i].offsetTop; + return; + } } - } } CodeMirrorSpellChecker({ - codeMirrorInstance: CodeMirror, + codeMirrorInstance: CodeMirror, }); var editor = CodeMirror.fromTextArea(document.getElementById('code'), { - mode: 'spell-checker', - backdrop: 'gfm', - lineNumbers: false, - matchBrackets: true, - lineWrapping: true, - theme: 'base16-light', - extraKeys: { - Enter: 'newlineAndIndentContinueMarkdownList', - }, + mode: "spell-checker", + backdrop: "gfm", + lineNumbers: false, + matchBrackets: true, + lineWrapping: true, + theme: 'base16-light', + extraKeys: { + "Enter": "newlineAndIndentContinueMarkdownList" + } }); editor.on('change', update); -function selectionChanger(selection, operator, endoperator) { - if (selection == '') { - return operator; - } - if (!endoperator) { - endoperator = operator; - } - var isApplied = - selection.slice(0, 2) === operator && - seisAppliedection.slice(-2) === endoperator; - var finaltext = isApplied - ? selection.slice(2, -2) - : operator + selection + endoperator; - return finaltext; +function selectionChanger(selection,operator,endoperator){ + if(selection == ""){ + return operator; + } + if(!endoperator){ + endoperator = operator + } + var isApplied = selection.slice(0, 2) === operator && seisAppliedection.slice(-2) === endoperator; + var finaltext = isApplied ? selection.slice(2, -2) : operator + selection + endoperator; + return finaltext; } editor.addKeyMap({ @@ -240,251 +228,214 @@ editor.addKeyMap({ }, }); -document.addEventListener( - 'drop', - function(e) { +document.addEventListener('drop', function(e) { e.preventDefault(); e.stopPropagation(); var reader = new FileReader(); reader.onload = function(e) { - editor.setValue(e.target.result); + editor.setValue(e.target.result); }; reader.readAsText(e.dataTransfer.files[0]); - }, - false, -); +}, false); //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() { +document.getElementById('saveas-markdown').addEventListener('click', function() { saveAsMarkdown(); hideMenu(); - }); +}); document.getElementById('saveas-html').addEventListener('click', function() { - saveAsHtml(); - hideMenu(); + saveAsHtml(); + hideMenu(); }); function save(code, name) { - var blob = new Blob([code], { - type: 'text/plain', - }); - if (window.saveAs) { - window.saveAs(blob, name); - } else if (navigator.saveBlob) { - navigator.saveBlob(blob, name); - } else { - url = URL.createObjectURL(blob); - var link = document.createElement('a'); - link.setAttribute('href', url); - link.setAttribute('download', name); - var event = document.createEvent('MouseEvents'); - event.initMouseEvent( - 'click', - true, - true, - window, - 1, - 0, - 0, - 0, - 0, - false, - false, - false, - false, - 0, - null, - ); - link.dispatchEvent(event); - } + var blob = new Blob([code], { + type: 'text/plain' + }); + if (window.saveAs) { + window.saveAs(blob, name); + } else if (navigator.saveBlob) { + navigator.saveBlob(blob, name); + } else { + url = URL.createObjectURL(blob); + var link = document.createElement("a"); + link.setAttribute("href", url); + link.setAttribute("download", name); + var event = document.createEvent('MouseEvents'); + event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); + link.dispatchEvent(event); + } } var menuVisible = false; var menu = document.getElementById('menu'); function showMenu() { - menuVisible = true; - menu.style.display = 'block'; + menuVisible = true; + menu.style.display = 'block'; } function hideMenu() { - menuVisible = false; - menu.style.display = 'none'; + menuVisible = false; + menu.style.display = 'none'; } function openFile(evt) { - if (window.File && window.FileReader && window.FileList && window.Blob) { - var files = evt.target.files; - console.log(files); - var reader = new FileReader(); - reader.onload = function(file) { - console.log(file.target.result); - editor.setValue(file.target.result); - return true; - }; - reader.readAsText(files[0]); - } else { - alert('The File APIs are not fully supported in this browser.'); - } + if (window.File && window.FileReader && window.FileList && window.Blob) { + var files = evt.target.files; + console.log(files); + var reader = new FileReader(); + reader.onload = function(file) { + console.log(file.target.result); + editor.setValue(file.target.result); + return true; + }; + reader.readAsText(files[0]); + + } else { + alert('The File APIs are not fully supported in this browser.'); + } } document.getElementById('close-menu').addEventListener('click', function() { - hideMenu(); + hideMenu(); }); document.addEventListener('keydown', function(e) { - if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { - if (localStorage.getItem('content') == editor.getValue()) { - e.preventDefault(); - return false; + if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { + if ( localStorage.getItem('content') == editor.getValue() ) { + e.preventDefault(); + return false; + } + e.shiftKey ? showMenu() : saveInBrowser(); + + e.preventDefault(); + return false; } - e.shiftKey ? showMenu() : saveInBrowser(); - - e.preventDefault(); - return false; - } - if (e.keyCode === 27 && menuVisible) { - hideMenu(); + if (e.keyCode === 27 && menuVisible) { + hideMenu(); - e.preventDefault(); - return false; - } + e.preventDefault(); + return false; + } }); function clearEditor() { - editor.setValue(''); + editor.setValue(""); } 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() { + 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'); - }, - ); - } else { - localStorage.setItem('content', text); - swal('Saved', 'Your Document has been saved.', 'success'); - } - console.log('Saved'); + swal("Saved", "Your Document has been saved.", "success"); + } + console.log("Saved"); } function toggleNightMode(button) { - button.classList.toggle('selected'); - document.getElementById('toplevel').classList.toggle('nightmode'); + button.classList.toggle('selected'); + document.getElementById('toplevel').classList.toggle('nightmode'); } function toggleReadMode(button) { - button.classList.toggle('selected'); - document.getElementById('out').classList.toggle('focused'); - document.getElementById('in').classList.toggle('hidden'); + button.classList.toggle('selected'); + document.getElementById('out').classList.toggle('focused'); + document.getElementById('in').classList.toggle('hidden'); } function toggleSpellCheck(button) { - button.classList.toggle('selected'); - document.body.classList.toggle('no-spellcheck'); + button.classList.toggle('selected'); + document.body.classList.toggle('no-spellcheck'); } function updateHash() { - window.location.hash = btoa( - // base64 so url-safe - RawDeflate.deflate( - // gzip - unescape( - encodeURIComponent( - // convert to utf8 - editor.getValue(), - ), - ), - ), - ); + window.location.hash = btoa( // base64 so url-safe + RawDeflate.deflate( // gzip + unescape(encodeURIComponent( // convert to utf8 + editor.getValue() + )) + ) + ); } function processQueryParams() { - var params = window.location.search.split('?')[1]; - if (window.location.hash) { - document.getElementById('readbutton').click(); // Show reading view - } - if (params) { - var obj = {}; - params.split('&').forEach(function(elem) { - obj[elem.split('=')[0]] = elem.split('=')[1]; - }); - if (obj.reading === 'false') { - document.getElementById('readbutton').click(); // Hide reading view + var params = window.location.search.split('?')[1]; + if (window.location.hash) { + document.getElementById('readbutton').click(); // Show reading view } - if (obj.dark === 'true') { - document.getElementById('nightbutton').click(); // Show night view + if (params) { + var obj = {}; + params.split('&').forEach(function(elem) { + obj[elem.split('=')[0]] = elem.split('=')[1]; + }); + if (obj.reading === 'false') { + document.getElementById('readbutton').click(); // Hide reading view + } + if (obj.dark === 'true') { + document.getElementById('nightbutton').click(); // Show night view + } } - } } function start() { - processQueryParams(); - if (window.location.hash) { - var h = window.location.hash.replace(/^#/, ''); - if (h.slice(0, 5) == 'view:') { - setOutput( - decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5))))), - ); - document.body.className = 'view'; - } else { - editor.setValue(decodeURIComponent(escape(RawDeflate.inflate(atob(h))))); + processQueryParams(); + if (window.location.hash) { + var h = window.location.hash.replace(/^#/, ''); + if (h.slice(0, 5) == 'view:') { + setOutput(decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5)))))); + document.body.className = 'view'; + } else { + editor.setValue( + decodeURIComponent(escape( + RawDeflate.inflate( + atob( + h + ) + ) + )) + ); + } + } else if (localStorage.getItem('content')) { + editor.setValue(localStorage.getItem('content')); } - } else if (localStorage.getItem('content')) { - editor.setValue(localStorage.getItem('content')); - } - update(editor); - editor.focus(); - document - .getElementById('fileInput') - .addEventListener('change', openFile, false); + update(editor); + editor.focus(); + document.getElementById('fileInput').addEventListener('change', openFile, false); } -window.addEventListener('beforeunload', function(e) { - var confirmationMessage = - 'It looks like you have been editing something. ' + - 'If you leave before saving, your changes will be lost.'; - (e || window.event).returnValue = confirmationMessage; //Gecko + IE - return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc. +window.addEventListener("beforeunload", function (e) { + var confirmationMessage = 'It looks like you have been editing something. ' + + 'If you leave before saving, your changes will be lost.'; + (e || window.event).returnValue = confirmationMessage; //Gecko + IE + return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc. }); start(); From 56ae789940cc929967eea6ab28a5dee537982642 Mon Sep 17 00:00:00 2001 From: MOHAN M U Date: Wed, 3 Apr 2019 10:04:52 +0530 Subject: [PATCH 3/8] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 6103f95..193b09a 100644 --- a/index.js +++ b/index.js @@ -220,7 +220,7 @@ editor.addKeyMap({ //codeblock "Shift-Ctrl-'": function(cm) { var selection = cm.getSelection(); - cm.replaceSelection('```' + selection + '```'); + cm.replaceSelection('```javasctipt' + selection + '\n'+'```'); if (!selection) { var cursorPos = cm.getCursor(); cm.setCursor(cursorPos.line, cursorPos.ch - 3); From 53012bed34ce1d0004e3cc2afb1e111f537d74be Mon Sep 17 00:00:00 2001 From: MOHAN M U Date: Wed, 3 Apr 2019 10:06:12 +0530 Subject: [PATCH 4/8] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 193b09a..f4b3005 100644 --- a/index.js +++ b/index.js @@ -220,10 +220,10 @@ editor.addKeyMap({ //codeblock "Shift-Ctrl-'": function(cm) { var selection = cm.getSelection(); - cm.replaceSelection('```javasctipt' + selection + '\n'+'```'); + cm.replaceSelection('```javascript' + selection + '\n'+'\n' +'```'); if (!selection) { var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line, cursorPos.ch - 3); + cm.setCursor(cursorPos.line, cursorPos.ch - 4); } }, }); From b005a3bbd25410e6bd02327b63ab535ee0020b5a Mon Sep 17 00:00:00 2001 From: MOHAN M U Date: Wed, 3 Apr 2019 10:19:49 +0530 Subject: [PATCH 5/8] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f4b3005..f9a3157 100644 --- a/index.js +++ b/index.js @@ -223,7 +223,7 @@ editor.addKeyMap({ cm.replaceSelection('```javascript' + selection + '\n'+'\n' +'```'); if (!selection) { var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line, cursorPos.ch - 4); + cm.setCursor(cursorPos.line -1, cursorPos.ch); } }, }); From 048ca27f31e785c679f17dccc9364c2414adfcc0 Mon Sep 17 00:00:00 2001 From: MOHAN M U Date: Thu, 4 Apr 2019 07:50:38 +0530 Subject: [PATCH 6/8] new line fix after selection --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f9a3157..26f6866 100644 --- a/index.js +++ b/index.js @@ -220,7 +220,7 @@ editor.addKeyMap({ //codeblock "Shift-Ctrl-'": function(cm) { var selection = cm.getSelection(); - cm.replaceSelection('```javascript' + selection + '\n'+'\n' +'```'); + cm.replaceSelection('```javascript' + '\n' + selection+'\n' +'```'); if (!selection) { var cursorPos = cm.getCursor(); cm.setCursor(cursorPos.line -1, cursorPos.ch); From 8c6058c2069374f0d186fbbf620a6fc9a14a845e Mon Sep 17 00:00:00 2001 From: MOHAN M U Date: Sat, 2 Nov 2019 11:44:17 +0530 Subject: [PATCH 7/8] Update index.js --- index.js | 674 +++++++++++++++++++++++++++---------------------------- 1 file changed, 335 insertions(+), 339 deletions(-) diff --git a/index.js b/index.js index 26f6866..06a0e5f 100644 --- a/index.js +++ b/index.js @@ -4,438 +4,434 @@ window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || wind // Because highlight.js is a bit awkward at times var languageOverrides = { - js: 'javascript', - html: 'xml' + js: 'javascript', + html: 'xml' }; var livestyles; emojify.setConfig({ - img_dir: 'emoji' + img_dir: 'emoji' }); var md = markdownit({ - html: true, - highlight: function(code, lang) { - if (languageOverrides[lang]) lang = languageOverrides[lang]; - if (lang && hljs.getLanguage(lang)) { - try { - return hljs.highlight(lang, code).value; - } catch (e) {} - } - return ''; - } - }) - .use(markdownitFootnote); + html: true, + highlight: function (code, lang) { + if (languageOverrides[lang]) lang = languageOverrides[lang]; + if (lang && hljs.getLanguage(lang)) { + try { + return hljs.highlight(lang, code).value; + } catch (e) {} + } + return ''; + } + }) + .use(markdownitFootnote); var hashto; function update(e) { - setOutput(e.getValue()); - - //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; - } else { - title = 'Markdown Editor' - } - - //To avoid to much title changing we check if is not the same as before - oldTitle = document.title; - if (oldTitle != title) { - oldTitle = title; - document.title = title; - } - //clearTimeout(hashto); - //hashto = setTimeout(updateHash, 1000); + setOutput(e.getValue()); + + //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; + } else { + title = 'Markdown Editor' + } + + //To avoid to much title changing we check if is not the same as before + oldTitle = document.title; + if (oldTitle != title) { + oldTitle = title; + document.title = title; + } + //clearTimeout(hashto); + //hashto = setTimeout(updateHash, 1000); } function setOutput(val) { - val = val.replace(/((.*?\n)*?.*?)<\/equation>/ig, function(a, b) { - return ''; - }); - - var out = document.getElementById('out'); - var old = out.cloneNode(true); - out.innerHTML = md.render(val); - emojify.run(out); - - var allold = old.getElementsByTagName("*"); - if (allold === undefined) return; - - var allnew = out.getElementsByTagName("*"); - if (allnew === undefined) return; - - for (var i = 0, max = Math.min(allold.length, allnew.length); i < max; i++) { - if (!allold[i].isEqualNode(allnew[i])) { - out.scrollTop = allnew[i].offsetTop; - return; - } - } + val = val.replace(/((.*?\n)*?.*?)<\/equation>/ig, function (a, b) { + return ''; + }); + + var out = document.getElementById('out'); + var old = out.cloneNode(true); + out.innerHTML = md.render(val); + emojify.run(out); + + var allold = old.getElementsByTagName("*"); + if (allold === undefined) return; + + var allnew = out.getElementsByTagName("*"); + if (allnew === undefined) return; + + for (var i = 0, max = Math.min(allold.length, allnew.length); i < max; i++) { + if (!allold[i].isEqualNode(allnew[i])) { + out.scrollTop = allnew[i].offsetTop; + return; + } + } } CodeMirrorSpellChecker({ - codeMirrorInstance: CodeMirror, + codeMirrorInstance: CodeMirror, }); var editor = CodeMirror.fromTextArea(document.getElementById('code'), { - mode: "spell-checker", - backdrop: "gfm", - lineNumbers: false, - matchBrackets: true, - lineWrapping: true, - theme: 'base16-light', - extraKeys: { - "Enter": "newlineAndIndentContinueMarkdownList" - } + mode: "spell-checker", + backdrop: "gfm", + lineNumbers: false, + matchBrackets: true, + lineWrapping: true, + theme: 'base16-light', + extraKeys: { + "Enter": "newlineAndIndentContinueMarkdownList" + } }); editor.on('change', update); -function selectionChanger(selection,operator,endoperator){ - if(selection == ""){ - return operator; - } - if(!endoperator){ - endoperator = operator - } - var isApplied = selection.slice(0, 2) === operator && seisAppliedection.slice(-2) === endoperator; - var finaltext = isApplied ? selection.slice(2, -2) : operator + selection + endoperator; - return finaltext; +function selectionChanger(selection, operator, endoperator) { + if (selection == "") { + return operator; + } + if (!endoperator) { + endoperator = operator + } + var isApplied = selection.slice(0, 2) === operator && seisAppliedection.slice(-2) === endoperator; + var finaltext = isApplied ? selection.slice(2, -2) : operator + selection + endoperator; + return finaltext; } editor.addKeyMap({ - // bold - 'Ctrl-B': function(cm) { - var selection = cm.getSelection(); - cm.replaceSelection('**' + selection + '**'); - if (!selection) { - var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line, cursorPos.ch - 2); - } - }, - // italic - 'Ctrl-I': function(cm) { - var selection = cm.getSelection(); - cm.replaceSelection('_' + selection + '_'); - if (!selection) { - var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line, cursorPos.ch - 1); - } - }, - // code - 'Ctrl-K': function(cm) { - var selection = cm.getSelection(); - cm.replaceSelection('`' + selection + '`'); - if (!selection) { - var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line, cursorPos.ch - 1); - } - }, - // keyboard shortcut - 'Ctrl-L': function(cm) { - cm.replaceSelection(selectionChanger(cm.getSelection(), '', '')); - }, - //Heading 1 - 'Ctrl-Alt-1': function(cm) { - cm.replaceSelection('# ' + cm.getSelection()); - }, - //Heading 2 - 'Ctrl-Alt-2': function(cm) { - cm.replaceSelection('## ' + cm.getSelection()); - }, - //Heading 3 - 'Ctrl-Alt-3': function(cm) { - cm.replaceSelection('### ' + cm.getSelection()); - }, - //Heading 4 - 'Ctrl-Alt-4': function(cm) { - cm.replaceSelection('#### ' + cm.getSelection()); - }, - //Heading 5 - 'Ctrl-Alt-5': function(cm) { - cm.replaceSelection('##### ' + cm.getSelection()); - }, - //Heading 6 - 'Ctrl-Alt-6': function(cm) { - cm.replaceSelection('###### ' + cm.getSelection()); - }, - // Links - 'Shift-Ctrl-L': function(cm) { - var selection = cm.getSelection(); - var text = ''; - var link = ''; - - if (selection.match(/^https?:\/\//)) { - link = selection; - } else { - text = selection; - } - cm.replaceSelection('[' + text + '](' + link + ')'); - - var cursorPos = cm.getCursor(); - if (!selection) { - cm.setCursor(cursorPos.line, cursorPos.ch - 3); - } else if (link) { - cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); - } else { - cm.setCursor(cursorPos.line, cursorPos.ch - 1); - } - }, - // Insert Image - 'Shift-Ctrl-I': function(cm) { - var selection = cm.getSelection(); - var text = ''; - var link = ''; - - if (selection.match(/^https?:\/\//)) { - link = selection; - } else { - text = selection; - } - cm.replaceSelection('![' + text + '](' + link + ')'); - - var cursorPos = cm.getCursor(); - if (!selection) { - cm.setCursor(cursorPos.line, cursorPos.ch - 3); - } else if (link) { - cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); - } else { - cm.setCursor(cursorPos.line, cursorPos.ch - 1); - } - }, - //Unordered list - 'Shift-U': function(cm) { - cm.replaceSelection('* ' + cm.getSelection()); - }, - //Ordered list - 'Shift-O': function(cm) { - cm.replaceSelection('1. ' + cm.getSelection()); - }, - //Blockquote - 'Shift-Ctrl-.': function(cm) { - cm.replaceSelection('> ' + cm.getSelection()); - }, - //codeblock - "Shift-Ctrl-'": function(cm) { - var selection = cm.getSelection(); - cm.replaceSelection('```javascript' + '\n' + selection+'\n' +'```'); - if (!selection) { - var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line -1, cursorPos.ch); - } - }, + // bold + 'Ctrl-B': function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection(selectionChanger(cm.getSelection(), '**')); + }, + // italic + 'Ctrl-I': function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection('_' + selection + '_'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // code + 'Ctrl-K': function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection('`' + selection + '`'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // keyboard shortcut + 'Ctrl-L': function (cm) { + cm.replaceSelection(selectionChanger(cm.getSelection(), '', '')); + }, + //Heading 1 + 'Ctrl-Alt-1': function (cm) { + cm.replaceSelection('# ' + cm.getSelection()); + }, + //Heading 2 + 'Ctrl-Alt-2': function (cm) { + cm.replaceSelection('## ' + cm.getSelection()); + }, + //Heading 3 + 'Ctrl-Alt-3': function (cm) { + cm.replaceSelection('### ' + cm.getSelection()); + }, + //Heading 4 + 'Ctrl-Alt-4': function (cm) { + cm.replaceSelection('#### ' + cm.getSelection()); + }, + //Heading 5 + 'Ctrl-Alt-5': function (cm) { + cm.replaceSelection('##### ' + cm.getSelection()); + }, + //Heading 6 + 'Ctrl-Alt-6': function (cm) { + cm.replaceSelection('###### ' + cm.getSelection()); + }, + // Links + 'Shift-Ctrl-L': function (cm) { + var selection = cm.getSelection(); + var text = ''; + var link = ''; + + if (selection.match(/^https?:\/\//)) { + link = selection; + } else { + text = selection; + } + cm.replaceSelection('[' + text + '](' + link + ')'); + + var cursorPos = cm.getCursor(); + if (!selection) { + cm.setCursor(cursorPos.line, cursorPos.ch - 3); + } else if (link) { + cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); + } else { + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // Insert Image + 'Shift-Ctrl-I': function (cm) { + var selection = cm.getSelection(); + var text = ''; + var link = ''; + + if (selection.match(/^https?:\/\//)) { + link = selection; + } else { + text = selection; + } + cm.replaceSelection('![' + text + '](' + link + ')'); + + var cursorPos = cm.getCursor(); + if (!selection) { + cm.setCursor(cursorPos.line, cursorPos.ch - 3); + } else if (link) { + cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); + } else { + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + //Unordered list + 'Shift-U': function (cm) { + cm.replaceSelection('* ' + cm.getSelection()); + }, + //Ordered list + 'Shift-O': function (cm) { + cm.replaceSelection('1. ' + cm.getSelection()); + }, + //Blockquote + 'Shift-Ctrl-.': function (cm) { + cm.replaceSelection('> ' + cm.getSelection()); + }, + //codeblock + "Shift-Ctrl-'": function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection('```javascript' + '\n' + selection + '\n' + '```'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line - 1, cursorPos.ch); + } + }, }); -document.addEventListener('drop', function(e) { - e.preventDefault(); - e.stopPropagation(); +document.addEventListener('drop', function (e) { + e.preventDefault(); + e.stopPropagation(); - var reader = new FileReader(); - reader.onload = function(e) { - editor.setValue(e.target.result); - }; + var reader = new FileReader(); + reader.onload = function (e) { + editor.setValue(e.target.result); + }; - reader.readAsText(e.dataTransfer.files[0]); + reader.readAsText(e.dataTransfer.files[0]); }, false); //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() { - saveAsMarkdown(); - hideMenu(); +document.getElementById('saveas-markdown').addEventListener('click', function () { + saveAsMarkdown(); + hideMenu(); }); -document.getElementById('saveas-html').addEventListener('click', function() { - saveAsHtml(); - hideMenu(); +document.getElementById('saveas-html').addEventListener('click', function () { + saveAsHtml(); + hideMenu(); }); function save(code, name) { - var blob = new Blob([code], { - type: 'text/plain' - }); - if (window.saveAs) { - window.saveAs(blob, name); - } else if (navigator.saveBlob) { - navigator.saveBlob(blob, name); - } else { - url = URL.createObjectURL(blob); - var link = document.createElement("a"); - link.setAttribute("href", url); - link.setAttribute("download", name); - var event = document.createEvent('MouseEvents'); - event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); - link.dispatchEvent(event); - } + var blob = new Blob([code], { + type: 'text/plain' + }); + if (window.saveAs) { + window.saveAs(blob, name); + } else if (navigator.saveBlob) { + navigator.saveBlob(blob, name); + } else { + url = URL.createObjectURL(blob); + var link = document.createElement("a"); + link.setAttribute("href", url); + link.setAttribute("download", name); + var event = document.createEvent('MouseEvents'); + event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); + link.dispatchEvent(event); + } } var menuVisible = false; var menu = document.getElementById('menu'); function showMenu() { - menuVisible = true; - menu.style.display = 'block'; + menuVisible = true; + menu.style.display = 'block'; } function hideMenu() { - menuVisible = false; - menu.style.display = 'none'; + menuVisible = false; + menu.style.display = 'none'; } function openFile(evt) { - if (window.File && window.FileReader && window.FileList && window.Blob) { - var files = evt.target.files; - console.log(files); - var reader = new FileReader(); - reader.onload = function(file) { - console.log(file.target.result); - editor.setValue(file.target.result); - return true; - }; - reader.readAsText(files[0]); - - } else { - alert('The File APIs are not fully supported in this browser.'); - } + if (window.File && window.FileReader && window.FileList && window.Blob) { + var files = evt.target.files; + console.log(files); + var reader = new FileReader(); + reader.onload = function (file) { + console.log(file.target.result); + editor.setValue(file.target.result); + return true; + }; + reader.readAsText(files[0]); + + } else { + alert('The File APIs are not fully supported in this browser.'); + } } -document.getElementById('close-menu').addEventListener('click', function() { - hideMenu(); +document.getElementById('close-menu').addEventListener('click', function () { + hideMenu(); }); -document.addEventListener('keydown', function(e) { - if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { - if ( localStorage.getItem('content') == editor.getValue() ) { - e.preventDefault(); - return false; - } - e.shiftKey ? showMenu() : saveInBrowser(); +document.addEventListener('keydown', function (e) { + if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { + if (localStorage.getItem('content') == editor.getValue()) { + e.preventDefault(); + return false; + } + e.shiftKey ? showMenu() : saveInBrowser(); - e.preventDefault(); - return false; - } + e.preventDefault(); + return false; + } - if (e.keyCode === 27 && menuVisible) { - hideMenu(); + if (e.keyCode === 27 && menuVisible) { + hideMenu(); - e.preventDefault(); - return false; - } + e.preventDefault(); + return false; + } }); function clearEditor() { - editor.setValue(""); + editor.setValue(""); } 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"); + 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 toggleNightMode(button) { - button.classList.toggle('selected'); - document.getElementById('toplevel').classList.toggle('nightmode'); + button.classList.toggle('selected'); + document.getElementById('toplevel').classList.toggle('nightmode'); } function toggleReadMode(button) { - button.classList.toggle('selected'); - document.getElementById('out').classList.toggle('focused'); - document.getElementById('in').classList.toggle('hidden'); + button.classList.toggle('selected'); + document.getElementById('out').classList.toggle('focused'); + document.getElementById('in').classList.toggle('hidden'); } function toggleSpellCheck(button) { - button.classList.toggle('selected'); - document.body.classList.toggle('no-spellcheck'); + button.classList.toggle('selected'); + document.body.classList.toggle('no-spellcheck'); } function updateHash() { - window.location.hash = btoa( // base64 so url-safe - RawDeflate.deflate( // gzip - unescape(encodeURIComponent( // convert to utf8 - editor.getValue() - )) - ) - ); + window.location.hash = btoa( // base64 so url-safe + RawDeflate.deflate( // gzip + unescape(encodeURIComponent( // convert to utf8 + editor.getValue() + )) + ) + ); } function processQueryParams() { - var params = window.location.search.split('?')[1]; - if (window.location.hash) { - document.getElementById('readbutton').click(); // Show reading view - } - if (params) { - var obj = {}; - params.split('&').forEach(function(elem) { - obj[elem.split('=')[0]] = elem.split('=')[1]; - }); - if (obj.reading === 'false') { - document.getElementById('readbutton').click(); // Hide reading view - } - if (obj.dark === 'true') { - document.getElementById('nightbutton').click(); // Show night view - } - } + var params = window.location.search.split('?')[1]; + if (window.location.hash) { + document.getElementById('readbutton').click(); // Show reading view + } + if (params) { + var obj = {}; + params.split('&').forEach(function (elem) { + obj[elem.split('=')[0]] = elem.split('=')[1]; + }); + if (obj.reading === 'false') { + document.getElementById('readbutton').click(); // Hide reading view + } + if (obj.dark === 'true') { + document.getElementById('nightbutton').click(); // Show night view + } + } } function start() { - processQueryParams(); - if (window.location.hash) { - var h = window.location.hash.replace(/^#/, ''); - if (h.slice(0, 5) == 'view:') { - setOutput(decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5)))))); - document.body.className = 'view'; - } else { - editor.setValue( - decodeURIComponent(escape( - RawDeflate.inflate( - atob( - h - ) - ) - )) - ); - } - } else if (localStorage.getItem('content')) { - editor.setValue(localStorage.getItem('content')); - } - update(editor); - editor.focus(); - document.getElementById('fileInput').addEventListener('change', openFile, false); + processQueryParams(); + if (window.location.hash) { + var h = window.location.hash.replace(/^#/, ''); + if (h.slice(0, 5) == 'view:') { + setOutput(decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5)))))); + document.body.className = 'view'; + } else { + editor.setValue( + decodeURIComponent(escape( + RawDeflate.inflate( + atob( + h + ) + ) + )) + ); + } + } else if (localStorage.getItem('content')) { + editor.setValue(localStorage.getItem('content')); + } + update(editor); + editor.focus(); + document.getElementById('fileInput').addEventListener('change', openFile, false); } window.addEventListener("beforeunload", function (e) { - var confirmationMessage = 'It looks like you have been editing something. ' - + 'If you leave before saving, your changes will be lost.'; - (e || window.event).returnValue = confirmationMessage; //Gecko + IE - return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc. + var confirmationMessage = 'It looks like you have been editing something. ' + + 'If you leave before saving, your changes will be lost.'; + (e || window.event).returnValue = confirmationMessage; //Gecko + IE + return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc. }); start(); From 24de85df2b328e1993e610ab9a3f10583f4cc592 Mon Sep 17 00:00:00 2001 From: MOHAN M U Date: Sat, 2 Nov 2019 11:45:27 +0530 Subject: [PATCH 8/8] Update index.js --- index.js | 664 +++++++++++++++++++++++++++---------------------------- 1 file changed, 332 insertions(+), 332 deletions(-) diff --git a/index.js b/index.js index 06a0e5f..1d84a47 100644 --- a/index.js +++ b/index.js @@ -4,434 +4,434 @@ window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || wind // Because highlight.js is a bit awkward at times var languageOverrides = { - js: 'javascript', - html: 'xml' + js: 'javascript', + html: 'xml' }; var livestyles; emojify.setConfig({ - img_dir: 'emoji' + img_dir: 'emoji' }); var md = markdownit({ - html: true, - highlight: function (code, lang) { - if (languageOverrides[lang]) lang = languageOverrides[lang]; - if (lang && hljs.getLanguage(lang)) { - try { - return hljs.highlight(lang, code).value; - } catch (e) {} - } - return ''; - } - }) - .use(markdownitFootnote); + html: true, + highlight: function (code, lang) { + if (languageOverrides[lang]) lang = languageOverrides[lang]; + if (lang && hljs.getLanguage(lang)) { + try { + return hljs.highlight(lang, code).value; + } catch (e) {} + } + return ''; + } + }) + .use(markdownitFootnote); var hashto; function update(e) { - setOutput(e.getValue()); - - //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; - } else { - title = 'Markdown Editor' - } - - //To avoid to much title changing we check if is not the same as before - oldTitle = document.title; - if (oldTitle != title) { - oldTitle = title; - document.title = title; - } - //clearTimeout(hashto); - //hashto = setTimeout(updateHash, 1000); + setOutput(e.getValue()); + + //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; + } else { + title = 'Markdown Editor' + } + + //To avoid to much title changing we check if is not the same as before + oldTitle = document.title; + if (oldTitle != title) { + oldTitle = title; + document.title = title; + } + //clearTimeout(hashto); + //hashto = setTimeout(updateHash, 1000); } function setOutput(val) { - val = val.replace(/((.*?\n)*?.*?)<\/equation>/ig, function (a, b) { - return ''; - }); - - var out = document.getElementById('out'); - var old = out.cloneNode(true); - out.innerHTML = md.render(val); - emojify.run(out); - - var allold = old.getElementsByTagName("*"); - if (allold === undefined) return; - - var allnew = out.getElementsByTagName("*"); - if (allnew === undefined) return; - - for (var i = 0, max = Math.min(allold.length, allnew.length); i < max; i++) { - if (!allold[i].isEqualNode(allnew[i])) { - out.scrollTop = allnew[i].offsetTop; - return; - } - } + val = val.replace(/((.*?\n)*?.*?)<\/equation>/ig, function (a, b) { + return ''; + }); + + var out = document.getElementById('out'); + var old = out.cloneNode(true); + out.innerHTML = md.render(val); + emojify.run(out); + + var allold = old.getElementsByTagName("*"); + if (allold === undefined) return; + + var allnew = out.getElementsByTagName("*"); + if (allnew === undefined) return; + + for (var i = 0, max = Math.min(allold.length, allnew.length); i < max; i++) { + if (!allold[i].isEqualNode(allnew[i])) { + out.scrollTop = allnew[i].offsetTop; + return; + } + } } CodeMirrorSpellChecker({ - codeMirrorInstance: CodeMirror, + codeMirrorInstance: CodeMirror, }); var editor = CodeMirror.fromTextArea(document.getElementById('code'), { - mode: "spell-checker", - backdrop: "gfm", - lineNumbers: false, - matchBrackets: true, - lineWrapping: true, - theme: 'base16-light', - extraKeys: { - "Enter": "newlineAndIndentContinueMarkdownList" - } + mode: "spell-checker", + backdrop: "gfm", + lineNumbers: false, + matchBrackets: true, + lineWrapping: true, + theme: 'base16-light', + extraKeys: { + "Enter": "newlineAndIndentContinueMarkdownList" + } }); editor.on('change', update); function selectionChanger(selection, operator, endoperator) { - if (selection == "") { - return operator; - } - if (!endoperator) { - endoperator = operator - } - var isApplied = selection.slice(0, 2) === operator && seisAppliedection.slice(-2) === endoperator; - var finaltext = isApplied ? selection.slice(2, -2) : operator + selection + endoperator; - return finaltext; + if (selection == "") { + return operator; + } + if (!endoperator) { + endoperator = operator + } + var isApplied = selection.slice(0, 2) === operator && seisAppliedection.slice(-2) === endoperator; + var finaltext = isApplied ? selection.slice(2, -2) : operator + selection + endoperator; + return finaltext; } editor.addKeyMap({ - // bold - 'Ctrl-B': function (cm) { - var selection = cm.getSelection(); - cm.replaceSelection(selectionChanger(cm.getSelection(), '**')); - }, - // italic - 'Ctrl-I': function (cm) { - var selection = cm.getSelection(); - cm.replaceSelection('_' + selection + '_'); - if (!selection) { - var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line, cursorPos.ch - 1); - } - }, - // code - 'Ctrl-K': function (cm) { - var selection = cm.getSelection(); - cm.replaceSelection('`' + selection + '`'); - if (!selection) { - var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line, cursorPos.ch - 1); - } - }, - // keyboard shortcut - 'Ctrl-L': function (cm) { - cm.replaceSelection(selectionChanger(cm.getSelection(), '', '')); - }, - //Heading 1 - 'Ctrl-Alt-1': function (cm) { - cm.replaceSelection('# ' + cm.getSelection()); - }, - //Heading 2 - 'Ctrl-Alt-2': function (cm) { - cm.replaceSelection('## ' + cm.getSelection()); - }, - //Heading 3 - 'Ctrl-Alt-3': function (cm) { - cm.replaceSelection('### ' + cm.getSelection()); - }, - //Heading 4 - 'Ctrl-Alt-4': function (cm) { - cm.replaceSelection('#### ' + cm.getSelection()); - }, - //Heading 5 - 'Ctrl-Alt-5': function (cm) { - cm.replaceSelection('##### ' + cm.getSelection()); - }, - //Heading 6 - 'Ctrl-Alt-6': function (cm) { - cm.replaceSelection('###### ' + cm.getSelection()); - }, - // Links - 'Shift-Ctrl-L': function (cm) { - var selection = cm.getSelection(); - var text = ''; - var link = ''; - - if (selection.match(/^https?:\/\//)) { - link = selection; - } else { - text = selection; - } - cm.replaceSelection('[' + text + '](' + link + ')'); - - var cursorPos = cm.getCursor(); - if (!selection) { - cm.setCursor(cursorPos.line, cursorPos.ch - 3); - } else if (link) { - cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); - } else { - cm.setCursor(cursorPos.line, cursorPos.ch - 1); - } - }, - // Insert Image - 'Shift-Ctrl-I': function (cm) { - var selection = cm.getSelection(); - var text = ''; - var link = ''; - - if (selection.match(/^https?:\/\//)) { - link = selection; - } else { - text = selection; - } - cm.replaceSelection('![' + text + '](' + link + ')'); - - var cursorPos = cm.getCursor(); - if (!selection) { - cm.setCursor(cursorPos.line, cursorPos.ch - 3); - } else if (link) { - cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); - } else { - cm.setCursor(cursorPos.line, cursorPos.ch - 1); - } - }, - //Unordered list - 'Shift-U': function (cm) { - cm.replaceSelection('* ' + cm.getSelection()); - }, - //Ordered list - 'Shift-O': function (cm) { - cm.replaceSelection('1. ' + cm.getSelection()); - }, - //Blockquote - 'Shift-Ctrl-.': function (cm) { - cm.replaceSelection('> ' + cm.getSelection()); - }, - //codeblock - "Shift-Ctrl-'": function (cm) { - var selection = cm.getSelection(); - cm.replaceSelection('```javascript' + '\n' + selection + '\n' + '```'); - if (!selection) { - var cursorPos = cm.getCursor(); - cm.setCursor(cursorPos.line - 1, cursorPos.ch); - } - }, + // bold + 'Ctrl-B': function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection(selectionChanger(cm.getSelection(), '**')); + }, + // italic + 'Ctrl-I': function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection('_' + selection + '_'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // code + 'Ctrl-K': function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection('`' + selection + '`'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // keyboard shortcut + 'Ctrl-L': function (cm) { + cm.replaceSelection(selectionChanger(cm.getSelection(), '', '')); + }, + //Heading 1 + 'Ctrl-Alt-1': function (cm) { + cm.replaceSelection('# ' + cm.getSelection()); + }, + //Heading 2 + 'Ctrl-Alt-2': function (cm) { + cm.replaceSelection('## ' + cm.getSelection()); + }, + //Heading 3 + 'Ctrl-Alt-3': function (cm) { + cm.replaceSelection('### ' + cm.getSelection()); + }, + //Heading 4 + 'Ctrl-Alt-4': function (cm) { + cm.replaceSelection('#### ' + cm.getSelection()); + }, + //Heading 5 + 'Ctrl-Alt-5': function (cm) { + cm.replaceSelection('##### ' + cm.getSelection()); + }, + //Heading 6 + 'Ctrl-Alt-6': function (cm) { + cm.replaceSelection('###### ' + cm.getSelection()); + }, + // Links + 'Shift-Ctrl-L': function (cm) { + var selection = cm.getSelection(); + var text = ''; + var link = ''; + + if (selection.match(/^https?:\/\//)) { + link = selection; + } else { + text = selection; + } + cm.replaceSelection('[' + text + '](' + link + ')'); + + var cursorPos = cm.getCursor(); + if (!selection) { + cm.setCursor(cursorPos.line, cursorPos.ch - 3); + } else if (link) { + cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); + } else { + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + // Insert Image + 'Shift-Ctrl-I': function (cm) { + var selection = cm.getSelection(); + var text = ''; + var link = ''; + + if (selection.match(/^https?:\/\//)) { + link = selection; + } else { + text = selection; + } + cm.replaceSelection('![' + text + '](' + link + ')'); + + var cursorPos = cm.getCursor(); + if (!selection) { + cm.setCursor(cursorPos.line, cursorPos.ch - 3); + } else if (link) { + cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length)); + } else { + cm.setCursor(cursorPos.line, cursorPos.ch - 1); + } + }, + //Unordered list + 'Shift-U': function (cm) { + cm.replaceSelection('* ' + cm.getSelection()); + }, + //Ordered list + 'Shift-O': function (cm) { + cm.replaceSelection('1. ' + cm.getSelection()); + }, + //Blockquote + 'Shift-Ctrl-.': function (cm) { + cm.replaceSelection('> ' + cm.getSelection()); + }, + //codeblock + "Shift-Ctrl-'": function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection('```javascript' + '\n' + selection + '\n' + '```'); + if (!selection) { + var cursorPos = cm.getCursor(); + cm.setCursor(cursorPos.line - 1, cursorPos.ch); + } + }, }); document.addEventListener('drop', function (e) { - e.preventDefault(); - e.stopPropagation(); + e.preventDefault(); + e.stopPropagation(); - var reader = new FileReader(); - reader.onload = function (e) { - editor.setValue(e.target.result); - }; + var reader = new FileReader(); + reader.onload = function (e) { + editor.setValue(e.target.result); + }; - reader.readAsText(e.dataTransfer.files[0]); + reader.readAsText(e.dataTransfer.files[0]); }, false); //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 () { - saveAsMarkdown(); - hideMenu(); + saveAsMarkdown(); + hideMenu(); }); document.getElementById('saveas-html').addEventListener('click', function () { - saveAsHtml(); - hideMenu(); + saveAsHtml(); + hideMenu(); }); function save(code, name) { - var blob = new Blob([code], { - type: 'text/plain' - }); - if (window.saveAs) { - window.saveAs(blob, name); - } else if (navigator.saveBlob) { - navigator.saveBlob(blob, name); - } else { - url = URL.createObjectURL(blob); - var link = document.createElement("a"); - link.setAttribute("href", url); - link.setAttribute("download", name); - var event = document.createEvent('MouseEvents'); - event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); - link.dispatchEvent(event); - } + var blob = new Blob([code], { + type: 'text/plain' + }); + if (window.saveAs) { + window.saveAs(blob, name); + } else if (navigator.saveBlob) { + navigator.saveBlob(blob, name); + } else { + url = URL.createObjectURL(blob); + var link = document.createElement("a"); + link.setAttribute("href", url); + link.setAttribute("download", name); + var event = document.createEvent('MouseEvents'); + event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); + link.dispatchEvent(event); + } } var menuVisible = false; var menu = document.getElementById('menu'); function showMenu() { - menuVisible = true; - menu.style.display = 'block'; + menuVisible = true; + menu.style.display = 'block'; } function hideMenu() { - menuVisible = false; - menu.style.display = 'none'; + menuVisible = false; + menu.style.display = 'none'; } function openFile(evt) { - if (window.File && window.FileReader && window.FileList && window.Blob) { - var files = evt.target.files; - console.log(files); - var reader = new FileReader(); - reader.onload = function (file) { - console.log(file.target.result); - editor.setValue(file.target.result); - return true; - }; - reader.readAsText(files[0]); - - } else { - alert('The File APIs are not fully supported in this browser.'); - } + if (window.File && window.FileReader && window.FileList && window.Blob) { + var files = evt.target.files; + console.log(files); + var reader = new FileReader(); + reader.onload = function (file) { + console.log(file.target.result); + editor.setValue(file.target.result); + return true; + }; + reader.readAsText(files[0]); + + } else { + alert('The File APIs are not fully supported in this browser.'); + } } document.getElementById('close-menu').addEventListener('click', function () { - hideMenu(); + hideMenu(); }); document.addEventListener('keydown', function (e) { - if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { - if (localStorage.getItem('content') == editor.getValue()) { - e.preventDefault(); - return false; - } - e.shiftKey ? showMenu() : saveInBrowser(); - - e.preventDefault(); - return false; - } - - if (e.keyCode === 27 && menuVisible) { - hideMenu(); - - e.preventDefault(); - return false; - } + if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { + if (localStorage.getItem('content') == editor.getValue()) { + e.preventDefault(); + return false; + } + e.shiftKey ? showMenu() : saveInBrowser(); + + e.preventDefault(); + return false; + } + + if (e.keyCode === 27 && menuVisible) { + hideMenu(); + + e.preventDefault(); + return false; + } }); function clearEditor() { - editor.setValue(""); + editor.setValue(""); } 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"); + 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 toggleNightMode(button) { - button.classList.toggle('selected'); - document.getElementById('toplevel').classList.toggle('nightmode'); + button.classList.toggle('selected'); + document.getElementById('toplevel').classList.toggle('nightmode'); } function toggleReadMode(button) { - button.classList.toggle('selected'); - document.getElementById('out').classList.toggle('focused'); - document.getElementById('in').classList.toggle('hidden'); + button.classList.toggle('selected'); + document.getElementById('out').classList.toggle('focused'); + document.getElementById('in').classList.toggle('hidden'); } function toggleSpellCheck(button) { - button.classList.toggle('selected'); - document.body.classList.toggle('no-spellcheck'); + button.classList.toggle('selected'); + document.body.classList.toggle('no-spellcheck'); } function updateHash() { - window.location.hash = btoa( // base64 so url-safe - RawDeflate.deflate( // gzip - unescape(encodeURIComponent( // convert to utf8 - editor.getValue() - )) - ) - ); + window.location.hash = btoa( // base64 so url-safe + RawDeflate.deflate( // gzip + unescape(encodeURIComponent( // convert to utf8 + editor.getValue() + )) + ) + ); } function processQueryParams() { - var params = window.location.search.split('?')[1]; - if (window.location.hash) { - document.getElementById('readbutton').click(); // Show reading view - } - if (params) { - var obj = {}; - params.split('&').forEach(function (elem) { - obj[elem.split('=')[0]] = elem.split('=')[1]; - }); - if (obj.reading === 'false') { - document.getElementById('readbutton').click(); // Hide reading view - } - if (obj.dark === 'true') { - document.getElementById('nightbutton').click(); // Show night view - } - } + var params = window.location.search.split('?')[1]; + if (window.location.hash) { + document.getElementById('readbutton').click(); // Show reading view + } + if (params) { + var obj = {}; + params.split('&').forEach(function (elem) { + obj[elem.split('=')[0]] = elem.split('=')[1]; + }); + if (obj.reading === 'false') { + document.getElementById('readbutton').click(); // Hide reading view + } + if (obj.dark === 'true') { + document.getElementById('nightbutton').click(); // Show night view + } + } } function start() { - processQueryParams(); - if (window.location.hash) { - var h = window.location.hash.replace(/^#/, ''); - if (h.slice(0, 5) == 'view:') { - setOutput(decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5)))))); - document.body.className = 'view'; - } else { - editor.setValue( - decodeURIComponent(escape( - RawDeflate.inflate( - atob( - h - ) - ) - )) - ); - } - } else if (localStorage.getItem('content')) { - editor.setValue(localStorage.getItem('content')); - } - update(editor); - editor.focus(); - document.getElementById('fileInput').addEventListener('change', openFile, false); + processQueryParams(); + if (window.location.hash) { + var h = window.location.hash.replace(/^#/, ''); + if (h.slice(0, 5) == 'view:') { + setOutput(decodeURIComponent(escape(RawDeflate.inflate(atob(h.slice(5)))))); + document.body.className = 'view'; + } else { + editor.setValue( + decodeURIComponent(escape( + RawDeflate.inflate( + atob( + h + ) + ) + )) + ); + } + } else if (localStorage.getItem('content')) { + editor.setValue(localStorage.getItem('content')); + } + update(editor); + editor.focus(); + document.getElementById('fileInput').addEventListener('change', openFile, false); } window.addEventListener("beforeunload", function (e) { - var confirmationMessage = 'It looks like you have been editing something. ' + - 'If you leave before saving, your changes will be lost.'; - (e || window.event).returnValue = confirmationMessage; //Gecko + IE - return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc. + var confirmationMessage = 'It looks like you have been editing something. ' + + 'If you leave before saving, your changes will be lost.'; + (e || window.event).returnValue = confirmationMessage; //Gecko + IE + return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc. }); start();