2014-06-21 22:58:06 +04:00
|
|
|
/* global CodeMirror, moment */
|
|
|
|
/** Set up a shortcut function to be called via router actions.
|
|
|
|
* See editor-route-base
|
|
|
|
*/
|
|
|
|
|
2014-06-24 05:17:57 +04:00
|
|
|
function init() {
|
|
|
|
//Used for simple, noncomputational replace-and-go! shortcuts.
|
|
|
|
// See default case in shortcut function below.
|
|
|
|
CodeMirror.prototype.simpleShortcutSyntax = {
|
|
|
|
bold: '**$1**',
|
|
|
|
italic: '*$1*',
|
|
|
|
strike: '~~$1~~',
|
|
|
|
code: '`$1`',
|
|
|
|
link: '[$1](http://)',
|
|
|
|
image: '![$1](http://)',
|
|
|
|
blockquote: '> $1'
|
|
|
|
};
|
|
|
|
CodeMirror.prototype.shortcut = function (type) {
|
|
|
|
var text = this.getSelection(),
|
|
|
|
cursor = this.getCursor(),
|
|
|
|
line = this.getLine(cursor.line),
|
|
|
|
fromLineStart = {line: cursor.line, ch: 0},
|
|
|
|
md, letterCount, textIndex, position;
|
|
|
|
switch (type) {
|
|
|
|
case 'h1':
|
|
|
|
this.replaceRange('# ' + line, fromLineStart);
|
|
|
|
this.setCursor(cursor.line, cursor.ch + 2);
|
|
|
|
return;
|
|
|
|
case 'h2':
|
|
|
|
this.replaceRange('## ' + line, fromLineStart);
|
|
|
|
this.setCursor(cursor.line, cursor.ch + 3);
|
|
|
|
return;
|
|
|
|
case 'h3':
|
|
|
|
this.replaceRange('### ' + line, fromLineStart);
|
|
|
|
this.setCursor(cursor.line, cursor.ch + 4);
|
|
|
|
return;
|
|
|
|
case 'h4':
|
|
|
|
this.replaceRange('#### ' + line, fromLineStart);
|
|
|
|
this.setCursor(cursor.line, cursor.ch + 5);
|
|
|
|
return;
|
|
|
|
case 'h5':
|
|
|
|
this.replaceRange('##### ' + line, fromLineStart);
|
|
|
|
this.setCursor(cursor.line, cursor.ch + 6);
|
|
|
|
return;
|
|
|
|
case 'h6':
|
|
|
|
this.replaceRange('###### ' + line, fromLineStart);
|
|
|
|
this.setCursor(cursor.line, cursor.ch + 7);
|
|
|
|
return;
|
|
|
|
case 'link':
|
|
|
|
md = this.simpleShortcutSyntax.link.replace('$1', text);
|
|
|
|
this.replaceSelection(md, 'end');
|
|
|
|
if (!text) {
|
|
|
|
this.setCursor(cursor.line, cursor.ch + 1);
|
|
|
|
} else {
|
|
|
|
textIndex = line.indexOf(text, cursor.ch - text.length);
|
|
|
|
position = textIndex + md.length - 1;
|
|
|
|
this.setSelection({
|
|
|
|
line: cursor.line,
|
|
|
|
ch: position - 7
|
|
|
|
}, {
|
|
|
|
line: cursor.line,
|
|
|
|
ch: position
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case 'image':
|
|
|
|
md = this.simpleShortcutSyntax.image.replace('$1', text);
|
|
|
|
if (line !== '') {
|
|
|
|
md = '\n\n' + md;
|
|
|
|
}
|
|
|
|
this.replaceSelection(md, 'end');
|
|
|
|
cursor = this.getCursor();
|
|
|
|
this.setSelection({line: cursor.line, ch: cursor.ch - 8}, {line: cursor.line, ch: cursor.ch - 1});
|
|
|
|
return;
|
|
|
|
case 'list':
|
|
|
|
md = text.replace(/^(\s*)(\w\W*)/gm, '$1* $2');
|
|
|
|
this.replaceSelection(md, 'end');
|
|
|
|
return;
|
|
|
|
case 'currentDate':
|
|
|
|
md = moment(new Date()).format('D MMMM YYYY');
|
|
|
|
this.replaceSelection(md, 'end');
|
|
|
|
return;
|
|
|
|
/** @TODO
|
|
|
|
case 'uppercase':
|
|
|
|
md = text.toLocaleUpperCase();
|
|
|
|
break;
|
|
|
|
case 'lowercase':
|
|
|
|
md = text.toLocaleLowerCase();
|
|
|
|
break;
|
|
|
|
case 'titlecase':
|
|
|
|
md = text.toTitleCase();
|
|
|
|
break;
|
|
|
|
case 'copyHTML':
|
|
|
|
converter = new Showdown.converter();
|
|
|
|
if (text) {
|
|
|
|
md = converter.makeHtml(text);
|
|
|
|
} else {
|
|
|
|
md = converter.makeHtml(this.getValue());
|
|
|
|
}
|
2014-06-21 22:58:06 +04:00
|
|
|
|
2014-06-24 05:17:57 +04:00
|
|
|
$(".modal-copyToHTML-content").text(md).selectText();
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
if (this.simpleShortcutSyntax[type]) {
|
|
|
|
md = this.simpleShortcutSyntax[type].replace('$1', text);
|
|
|
|
}
|
2014-06-21 22:58:06 +04:00
|
|
|
}
|
2014-06-24 05:17:57 +04:00
|
|
|
if (md) {
|
|
|
|
this.replaceSelection(md, 'end');
|
|
|
|
if (!text) {
|
|
|
|
letterCount = md.length;
|
|
|
|
this.setCursor({
|
|
|
|
line: cursor.line,
|
|
|
|
ch: cursor.ch + (letterCount / 2)
|
|
|
|
});
|
|
|
|
}
|
2014-06-21 22:58:06 +04:00
|
|
|
}
|
2014-06-24 05:17:57 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
init: init
|
2014-07-31 07:18:00 +04:00
|
|
|
};
|