2013-05-24 14:09:20 +04:00
|
|
|
// # Surrounds given text with Markdown syntax
|
2013-06-25 16:23:09 +04:00
|
|
|
|
2013-08-12 17:58:01 +04:00
|
|
|
/*global $, window, CodeMirror, Showdown, moment */
|
2013-06-25 16:23:09 +04:00
|
|
|
(function () {
|
2013-09-24 14:46:30 +04:00
|
|
|
'use strict';
|
2013-05-24 14:09:20 +04:00
|
|
|
var Markdown = {
|
|
|
|
init : function (options, elem) {
|
|
|
|
var self = this;
|
|
|
|
self.elem = elem;
|
|
|
|
|
|
|
|
self.style = (typeof options === 'string') ? options : options.style;
|
|
|
|
|
2013-05-28 16:54:55 +04:00
|
|
|
self.options = $.extend({}, CodeMirror.prototype.addMarkdown.options, options);
|
2013-05-24 14:09:20 +04:00
|
|
|
|
|
|
|
self.replace();
|
|
|
|
},
|
|
|
|
replace: function () {
|
2013-08-20 15:44:47 +04:00
|
|
|
var text = this.elem.getSelection(), pass = true, md, cursor, line, word, letterCount, converter;
|
2013-07-18 17:02:54 +04:00
|
|
|
switch (this.style) {
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'h1':
|
2013-08-12 17:58:01 +04:00
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
line = this.elem.getLine(cursor.line);
|
2013-09-24 14:46:30 +04:00
|
|
|
this.elem.setLine(cursor.line, '# ' + line);
|
2013-08-12 17:58:01 +04:00
|
|
|
this.elem.setCursor(cursor.line, cursor.ch + 2);
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'h2':
|
2013-08-12 17:58:01 +04:00
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
line = this.elem.getLine(cursor.line);
|
2013-09-24 14:46:30 +04:00
|
|
|
this.elem.setLine(cursor.line, '## ' + line);
|
2013-08-12 17:58:01 +04:00
|
|
|
this.elem.setCursor(cursor.line, cursor.ch + 3);
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'h3':
|
2013-08-12 17:58:01 +04:00
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
line = this.elem.getLine(cursor.line);
|
2013-09-24 14:46:30 +04:00
|
|
|
this.elem.setLine(cursor.line, '### ' + line);
|
2013-08-12 17:58:01 +04:00
|
|
|
this.elem.setCursor(cursor.line, cursor.ch + 4);
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'h4':
|
2013-08-12 17:58:01 +04:00
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
line = this.elem.getLine(cursor.line);
|
2013-09-24 14:46:30 +04:00
|
|
|
this.elem.setLine(cursor.line, '#### ' + line);
|
2013-08-12 17:58:01 +04:00
|
|
|
this.elem.setCursor(cursor.line, cursor.ch + 5);
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'h5':
|
2013-08-12 17:58:01 +04:00
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
line = this.elem.getLine(cursor.line);
|
2013-09-24 14:46:30 +04:00
|
|
|
this.elem.setLine(cursor.line, '##### ' + line);
|
2013-08-12 17:58:01 +04:00
|
|
|
this.elem.setCursor(cursor.line, cursor.ch + 6);
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'h6':
|
2013-08-12 17:58:01 +04:00
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
line = this.elem.getLine(cursor.line);
|
2013-09-24 14:46:30 +04:00
|
|
|
this.elem.setLine(cursor.line, '###### ' + line);
|
2013-08-12 17:58:01 +04:00
|
|
|
this.elem.setCursor(cursor.line, cursor.ch + 7);
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'link':
|
2013-07-18 17:02:54 +04:00
|
|
|
md = this.options.syntax.link.replace('$1', text);
|
2013-09-24 14:46:30 +04:00
|
|
|
this.elem.replaceSelection(md, 'end');
|
2013-07-18 17:02:54 +04:00
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
this.elem.setSelection({line: cursor.line, ch: cursor.ch - 8}, {line: cursor.line, ch: cursor.ch - 1});
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'image':
|
2013-09-16 18:18:33 +04:00
|
|
|
cursor = this.elem.getCursor();
|
2013-07-18 17:02:54 +04:00
|
|
|
md = this.options.syntax.image.replace('$1', text);
|
2013-09-24 14:46:30 +04:00
|
|
|
if (this.elem.getLine(cursor.line) !== '') {
|
2013-09-16 18:18:33 +04:00
|
|
|
md = "\n\n" + md;
|
|
|
|
}
|
2013-07-18 17:02:54 +04:00
|
|
|
this.elem.replaceSelection(md, "end");
|
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
this.elem.setSelection({line: cursor.line, ch: cursor.ch - 8}, {line: cursor.line, ch: cursor.ch - 1});
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'uppercase':
|
2013-07-18 17:02:54 +04:00
|
|
|
md = text.toLocaleUpperCase();
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'lowercase':
|
2013-07-18 17:02:54 +04:00
|
|
|
md = text.toLocaleLowerCase();
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'titlecase':
|
2013-07-18 17:42:16 +04:00
|
|
|
md = text.toTitleCase();
|
2013-07-18 17:02:54 +04:00
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'selectword':
|
2013-07-18 17:02:54 +04:00
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
word = this.elem.getTokenAt(cursor);
|
|
|
|
if (!/\w$/g.test(word.string)) {
|
|
|
|
this.elem.setSelection({line: cursor.line, ch: word.start}, {line: cursor.line, ch: word.end - 1});
|
|
|
|
} else {
|
|
|
|
this.elem.setSelection({line: cursor.line, ch: word.start}, {line: cursor.line, ch: word.end});
|
|
|
|
}
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'copyHTML':
|
2013-07-22 16:50:50 +04:00
|
|
|
converter = new Showdown.converter();
|
2013-08-20 15:44:47 +04:00
|
|
|
if (text) {
|
|
|
|
md = converter.makeHtml(text);
|
|
|
|
} else {
|
|
|
|
md = converter.makeHtml(this.elem.getValue());
|
|
|
|
}
|
|
|
|
|
2013-07-25 19:00:41 +04:00
|
|
|
$(".modal-copyToHTML-content").text(md).selectText();
|
2013-07-22 16:50:50 +04:00
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'list':
|
|
|
|
md = text.replace(/^(\s*)(\w\W*)/gm, '$1* $2');
|
|
|
|
this.elem.replaceSelection(md, 'end');
|
2013-07-18 17:02:54 +04:00
|
|
|
pass = false;
|
|
|
|
break;
|
2013-09-24 14:46:30 +04:00
|
|
|
case 'currentDate':
|
|
|
|
md = moment(new Date()).format('D MMMM YYYY');
|
|
|
|
this.elem.replaceSelection(md, 'end');
|
2013-09-23 19:23:05 +04:00
|
|
|
pass = false;
|
2013-08-12 17:58:01 +04:00
|
|
|
break;
|
2013-09-27 17:41:38 +04:00
|
|
|
case 'newLine':
|
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
if (this.elem.getLine(cursor.line) !== "") {
|
|
|
|
this.elem.setLine(cursor.line, this.elem.getLine(cursor.line) + "\n\n");
|
|
|
|
}
|
|
|
|
pass = false;
|
|
|
|
break;
|
2013-07-18 17:02:54 +04:00
|
|
|
default:
|
|
|
|
if (this.options.syntax[this.style]) {
|
|
|
|
md = this.options.syntax[this.style].replace('$1', text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pass && md) {
|
2013-09-24 14:46:30 +04:00
|
|
|
this.elem.replaceSelection(md, 'end');
|
2013-08-20 15:44:47 +04:00
|
|
|
if (!text) {
|
|
|
|
letterCount = md.length;
|
|
|
|
cursor = this.elem.getCursor();
|
|
|
|
this.elem.setCursor({line: cursor.line, ch: cursor.ch - (letterCount / 2)});
|
|
|
|
}
|
2013-05-24 14:09:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-28 16:54:55 +04:00
|
|
|
CodeMirror.prototype.addMarkdown = function (options) {
|
|
|
|
var markdown = Object.create(Markdown);
|
|
|
|
markdown.init(options, this);
|
2013-05-24 14:09:20 +04:00
|
|
|
};
|
|
|
|
|
2013-05-28 16:54:55 +04:00
|
|
|
CodeMirror.prototype.addMarkdown.options = {
|
2013-05-24 14:09:20 +04:00
|
|
|
style: null,
|
|
|
|
syntax: {
|
|
|
|
bold: "**$1**",
|
2013-09-07 19:38:04 +04:00
|
|
|
italic: "*$1*",
|
2013-05-24 14:09:20 +04:00
|
|
|
strike: "~~$1~~",
|
|
|
|
code: "`$1`",
|
|
|
|
link: "[$1](http://)",
|
2013-08-21 00:16:40 +04:00
|
|
|
image: "![$1](http://)",
|
2013-08-12 17:58:01 +04:00
|
|
|
blockquote: "> $1"
|
2013-05-24 14:09:20 +04:00
|
|
|
}
|
|
|
|
};
|
2013-06-25 16:23:09 +04:00
|
|
|
|
|
|
|
}());
|