Converted from Jquery plugin to Codemirror function

This commit is contained in:
Matthew Harrison-Jones 2013-05-28 13:54:55 +01:00
parent 27875b2849
commit d91c93f939
2 changed files with 28 additions and 60 deletions

View File

@ -90,22 +90,6 @@
}
}
function getSelectedText() {
if (window.getSelection) {
var sel = window.getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0).cloneRange();
range.surroundContents(span);
sel.removeAllRanges();
sel.addRange(range);
}
}
if (document.selection) {
return document.selection.createRange().text;
}
return '';
}
// ## Main Initialisation
$(document).ready(function () {
@ -170,90 +154,89 @@
$('body').toggleClass('zen');
});
var CMTextarea = $(".CodeMirror textarea");
// Bold text
shortcut.add("Ctrl+B", function () {
return CMTextarea.addMarkdown({style: "bold", target: editor});
return editor.addMarkdown({style: "bold"});
});
// Bold text
shortcut.add("Meta+B", function () {
return CMTextarea.addMarkdown({style: "bold", target: editor});
return editor.addMarkdown({style: "bold"});
});
// Italic text
shortcut.add("Ctrl+I", function () {
return CMTextarea.addMarkdown({style: "italic", target: editor});
return editor.addMarkdown({style: "italic"});
});
// Italic text
shortcut.add("Meta+I", function () {
return CMTextarea.addMarkdown({style: "italic", target: editor});
return editor.addMarkdown({style: "italic"});
});
// Strike through text
shortcut.add("Ctrl+Alt+U", function () {
return CMTextarea.addMarkdown({style: "strike", target: editor});
return editor.addMarkdown({style: "strike"});
});
// Inline Code
shortcut.add("Ctrl+Shift+K", function () {
return CMTextarea.addMarkdown({style: "code", target: editor});
return editor.addMarkdown({style: "code"});
});
// Inline Code
shortcut.add("Meta+K", function () {
return CMTextarea.addMarkdown({style: "code", target: editor});
return editor.addMarkdown({style: "code"});
});
// H1
shortcut.add("Alt+1", function () {
return CMTextarea.addMarkdown({style: "h1", target: editor});
return editor.addMarkdown({style: "h1"});
});
// H2
shortcut.add("Alt+2", function () {
return CMTextarea.addMarkdown({style: "h2", target: editor});
return editor.addMarkdown({style: "h2"});
});
// H3
shortcut.add("Alt+3", function () {
return CMTextarea.addMarkdown({style: "h3", target: editor});
return editor.addMarkdown({style: "h3"});
});
// H4
shortcut.add("Alt+4", function () {
return CMTextarea.addMarkdown({style: "h4", target: editor});
return editor.addMarkdown({style: "h4"});
});
// H5
shortcut.add("Alt+5", function () {
return CMTextarea.addMarkdown({style: "h5", target: editor});
return editor.addMarkdown({style: "h5"});
});
// H6
shortcut.add("Alt+6", function () {
return CMTextarea.addMarkdown({style: "h6", target: editor});
return editor.addMarkdown({style: "h6"});
});
// Link
shortcut.add("Ctrl+Shift+L", function () {
return CMTextarea.addMarkdown({style: "link", target: editor});
return editor.addMarkdown({style: "link"});
});
// Image
shortcut.add("Ctrl+Shift+I", function () {
return CMTextarea.addMarkdown({style: "image", target: editor});
return editor.addMarkdown({style: "image"});
});
// Blockquote
shortcut.add("Ctrl+Q", function () {
return CMTextarea.addMarkdown({style: "blockquote", target: editor});
return editor.addMarkdown({style: "blockquote"});
});
// Current Date
shortcut.add("Ctrl+Shift+1", function () {
return CMTextarea.addMarkdown({style: "currentDate", target: editor});
return editor.addMarkdown({style: "currentDate"});
});
});
}(jQuery, Showdown, CodeMirror, shortcut));

View File

@ -1,53 +1,38 @@
/*global window, document, console, jQuery*/
// Polyfill for Object.create
if (typeof Object.create !== 'function') {
Object.create = function (obj) {
"use strict";
function F() {}
F.prototype = obj;
return new F();
};
}
/*global console, jQuery, CodeMirror*/
// # Surrounds given text with Markdown syntax
(function ($, window, document) {
(function ($) {
"use strict";
var Markdown = {
init : function (options, elem) {
var self = this;
self.elem = elem;
self.$elem = $(elem);
self.style = (typeof options === 'string') ? options : options.style;
self.options = $.extend({}, $.fn.addMarkdown.options, options);
self.options = $.extend({}, CodeMirror.prototype.addMarkdown.options, options);
self.replace();
},
replace: function () {
var text = this.options.target.getSelection(), md;
var text = this.elem.getSelection(), md;
if (this.options.syntax[this.style]) {
md = this.options.syntax[this.style].replace('$1', text);
this.options.target.replaceSelection(md);
this.elem.replaceSelection(md);
} else {
console.log("Invalid style.");
}
}
};
$.fn.addMarkdown = function (options) {
return this.each(function () {
var markdown = Object.create(Markdown);
markdown.init(options, this);
});
CodeMirror.prototype.addMarkdown = function (options) {
var markdown = Object.create(Markdown);
markdown.init(options, this);
};
$.fn.addMarkdown.options = {
CodeMirror.prototype.addMarkdown.options = {
style: null,
target: null,
syntax: {
bold: "**$1**",
italic: "_$1_",
@ -60,9 +45,9 @@ if (typeof Object.create !== 'function') {
h5: "\n##### $1\n",
h6: "\n###### $1\n",
link: "[$1](http://)",
image: "![$1](http://)",
image: "!image[$1](http://)",
blockquote: "> $1",
currentDate: new Date().toLocaleString()
}
};
}(jQuery, window, document));
}(jQuery));