Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 126 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -52,7 +52,7 @@ function update(e) {
}

function setOutput(val) {
val = val.replace(/<equation>((.*?\n)*?.*?)<\/equation>/ig, function(a, b) {
val = val.replace(/<equation>((.*?\n)*?.*?)<\/equation>/ig, function (a, b) {
return '<img src="http://latex.codecogs.com/png.latex?' + encodeURIComponent(b) + '" />';
});

Expand Down Expand Up @@ -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;
Expand All @@ -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(),'<kbd>','</kbd>'));
}
'Ctrl-L': function (cm) {
cm.replaceSelection(selectionChanger(cm.getSelection(), '<kbd>', '</kbd>'));
},
//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);
};

Expand All @@ -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();
});
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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");
});
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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.
});
Expand Down