diff --git a/index.js b/index.js index 873179a..1d84a47 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ emojify.setConfig({ var md = markdownit({ html: true, - highlight: function(code, lang) { + highlight: function (code, lang) { if (languageOverrides[lang]) lang = languageOverrides[lang]; if (lang && hljs.getLanguage(lang)) { try { @@ -52,7 +52,7 @@ function update(e) { } function setOutput(val) { - val = val.replace(/((.*?\n)*?.*?)<\/equation>/ig, function(a, b) { + val = val.replace(/((.*?\n)*?.*?)<\/equation>/ig, function (a, b) { return ''; }); @@ -93,11 +93,11 @@ var editor = CodeMirror.fromTextArea(document.getElementById('code'), { editor.on('change', update); -function selectionChanger(selection,operator,endoperator){ - if(selection == ""){ +function selectionChanger(selection, operator, endoperator) { + if (selection == "") { return operator; } - if(!endoperator){ + if (!endoperator) { endoperator = operator } var isApplied = selection.slice(0, 2) === operator && seisAppliedection.slice(-2) === endoperator; @@ -107,29 +107,129 @@ function selectionChanger(selection,operator,endoperator){ editor.addKeyMap({ // bold - 'Ctrl-B': function(cm) { - cm.replaceSelection(selectionChanger(cm.getSelection(),'**')); + 'Ctrl-B': function (cm) { + var selection = cm.getSelection(); + cm.replaceSelection(selectionChanger(cm.getSelection(), '**')); }, // italic - 'Ctrl-I': function(cm) { - cm.replaceSelection(selectionChanger(cm.getSelection(),'_')); + '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) { - cm.replaceSelection(selectionChanger(cm.getSelection(),'`')); + '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(),'','')); - } + '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) { +document.addEventListener('drop', function (e) { e.preventDefault(); e.stopPropagation(); var reader = new FileReader(); - reader.onload = function(e) { + reader.onload = function (e) { editor.setValue(e.target.result); }; @@ -146,12 +246,12 @@ function saveAsHtml() { 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() { +document.getElementById('saveas-html').addEventListener('click', function () { saveAsHtml(); hideMenu(); }); @@ -193,7 +293,7 @@ function openFile(evt) { var files = evt.target.files; console.log(files); var reader = new FileReader(); - reader.onload = function(file) { + reader.onload = function (file) { console.log(file.target.result); editor.setValue(file.target.result); return true; @@ -205,13 +305,13 @@ function openFile(evt) { } } -document.getElementById('close-menu').addEventListener('click', function() { +document.getElementById('close-menu').addEventListener('click', function () { hideMenu(); }); -document.addEventListener('keydown', function(e) { +document.addEventListener('keydown', function (e) { if (e.keyCode == 83 && (e.ctrlKey || e.metaKey)) { - if ( localStorage.getItem('content') == editor.getValue() ) { + if (localStorage.getItem('content') == editor.getValue()) { e.preventDefault(); return false; } @@ -245,7 +345,7 @@ function saveInBrowser() { confirmButtonText: "Yes, overwrite!", closeOnConfirm: false }, - function() { + function () { localStorage.setItem('content', text); swal("Saved", "Your Document has been saved.", "success"); }); @@ -289,7 +389,7 @@ function processQueryParams() { } if (params) { var obj = {}; - params.split('&').forEach(function(elem) { + params.split('&').forEach(function (elem) { obj[elem.split('=')[0]] = elem.split('=')[1]; }); if (obj.reading === 'false') { @@ -328,8 +428,8 @@ function start() { } 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.'; + 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. });