0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

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 // ## Main Initialisation
$(document).ready(function () { $(document).ready(function () {
@ -170,90 +154,89 @@
$('body').toggleClass('zen'); $('body').toggleClass('zen');
}); });
var CMTextarea = $(".CodeMirror textarea");
// Bold text // Bold text
shortcut.add("Ctrl+B", function () { shortcut.add("Ctrl+B", function () {
return CMTextarea.addMarkdown({style: "bold", target: editor}); return editor.addMarkdown({style: "bold"});
}); });
// Bold text // Bold text
shortcut.add("Meta+B", function () { shortcut.add("Meta+B", function () {
return CMTextarea.addMarkdown({style: "bold", target: editor}); return editor.addMarkdown({style: "bold"});
}); });
// Italic text // Italic text
shortcut.add("Ctrl+I", function () { shortcut.add("Ctrl+I", function () {
return CMTextarea.addMarkdown({style: "italic", target: editor}); return editor.addMarkdown({style: "italic"});
}); });
// Italic text // Italic text
shortcut.add("Meta+I", function () { shortcut.add("Meta+I", function () {
return CMTextarea.addMarkdown({style: "italic", target: editor}); return editor.addMarkdown({style: "italic"});
}); });
// Strike through text // Strike through text
shortcut.add("Ctrl+Alt+U", function () { shortcut.add("Ctrl+Alt+U", function () {
return CMTextarea.addMarkdown({style: "strike", target: editor}); return editor.addMarkdown({style: "strike"});
}); });
// Inline Code // Inline Code
shortcut.add("Ctrl+Shift+K", function () { shortcut.add("Ctrl+Shift+K", function () {
return CMTextarea.addMarkdown({style: "code", target: editor}); return editor.addMarkdown({style: "code"});
}); });
// Inline Code // Inline Code
shortcut.add("Meta+K", function () { shortcut.add("Meta+K", function () {
return CMTextarea.addMarkdown({style: "code", target: editor}); return editor.addMarkdown({style: "code"});
}); });
// H1 // H1
shortcut.add("Alt+1", function () { shortcut.add("Alt+1", function () {
return CMTextarea.addMarkdown({style: "h1", target: editor}); return editor.addMarkdown({style: "h1"});
}); });
// H2 // H2
shortcut.add("Alt+2", function () { shortcut.add("Alt+2", function () {
return CMTextarea.addMarkdown({style: "h2", target: editor}); return editor.addMarkdown({style: "h2"});
}); });
// H3 // H3
shortcut.add("Alt+3", function () { shortcut.add("Alt+3", function () {
return CMTextarea.addMarkdown({style: "h3", target: editor}); return editor.addMarkdown({style: "h3"});
}); });
// H4 // H4
shortcut.add("Alt+4", function () { shortcut.add("Alt+4", function () {
return CMTextarea.addMarkdown({style: "h4", target: editor}); return editor.addMarkdown({style: "h4"});
}); });
// H5 // H5
shortcut.add("Alt+5", function () { shortcut.add("Alt+5", function () {
return CMTextarea.addMarkdown({style: "h5", target: editor}); return editor.addMarkdown({style: "h5"});
}); });
// H6 // H6
shortcut.add("Alt+6", function () { shortcut.add("Alt+6", function () {
return CMTextarea.addMarkdown({style: "h6", target: editor}); return editor.addMarkdown({style: "h6"});
}); });
// Link // Link
shortcut.add("Ctrl+Shift+L", function () { shortcut.add("Ctrl+Shift+L", function () {
return CMTextarea.addMarkdown({style: "link", target: editor}); return editor.addMarkdown({style: "link"});
}); });
// Image // Image
shortcut.add("Ctrl+Shift+I", function () { shortcut.add("Ctrl+Shift+I", function () {
return CMTextarea.addMarkdown({style: "image", target: editor}); return editor.addMarkdown({style: "image"});
}); });
// Blockquote // Blockquote
shortcut.add("Ctrl+Q", function () { shortcut.add("Ctrl+Q", function () {
return CMTextarea.addMarkdown({style: "blockquote", target: editor}); return editor.addMarkdown({style: "blockquote"});
}); });
// Current Date // Current Date
shortcut.add("Ctrl+Shift+1", function () { shortcut.add("Ctrl+Shift+1", function () {
return CMTextarea.addMarkdown({style: "currentDate", target: editor}); return editor.addMarkdown({style: "currentDate"});
}); });
}); });
}(jQuery, Showdown, CodeMirror, shortcut)); }(jQuery, Showdown, CodeMirror, shortcut));

View file

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