Add the replace selection text to the options.

patch-ionaru
Alex Canessa 9 years ago
parent e20b64c97a
commit 8bf55b1b34

@ -260,34 +260,34 @@ function toggleOrderedList(editor) {
_toggleLine(cm, "ordered-list"); _toggleLine(cm, "ordered-list");
} }
/** /**
* Action for drawing a link. * Action for drawing a link.
*/ */
function drawLink(editor) { function drawLink(editor) {
var cm = editor.codemirror; var cm = editor.codemirror;
var stat = getState(cm); var stat = getState(cm);
_replaceSelection(cm, stat.link, "[", "](http://)"); var options = editor.options;
_replaceSelection(cm, stat.link, options.replaceTexts.link);
} }
/** /**
* Action for drawing an img. * Action for drawing an img.
*/ */
function drawImage(editor) { function drawImage(editor) {
var cm = editor.codemirror; var cm = editor.codemirror;
var stat = getState(cm); var stat = getState(cm);
_replaceSelection(cm, stat.image, "![](http://", ")"); var options = editor.options;
_replaceSelection(cm, stat.image, options.replaceTexts.image);
} }
/** /**
* Action for drawing a horizontal rule. * Action for drawing a horizontal rule.
*/ */
function drawHorizontalRule(editor) { function drawHorizontalRule(editor) {
var cm = editor.codemirror; var cm = editor.codemirror;
var stat = getState(cm); var stat = getState(cm);
_replaceSelection(cm, stat.image, "", "\n\n-----\n\n"); var options = editor.options;
_replaceSelection(cm, stat.image, options.replaceTexts.horizontalRule);
} }
@ -399,11 +399,13 @@ function togglePreview(editor) {
toggleSideBySide(editor); toggleSideBySide(editor);
} }
function _replaceSelection(cm, active, start, end) { function _replaceSelection(cm, active, startEnd) {
if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className)) if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
return; return;
var text; var text;
var start = startEnd[0];
var end = startEnd[1];
var startPoint = cm.getCursor("start"); var startPoint = cm.getCursor("start");
var endPoint = cm.getCursor("end"); var endPoint = cm.getCursor("end");
if(active) { if(active) {
@ -604,6 +606,49 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
cm.focus(); cm.focus();
} }
/**
* Merge the properties of one object into another.
*
* @param {Object} target The object where the properties will be copied
* @param {Object} source The object whose properties will be copied
*
* @returns {Object}
*/
function _mergeProperties(target, source) {
for(var property in source) {
if (source.hasOwnProperty(property)) {
if (source[property] instanceof Array) {
target[property] = source[property].concat(target[property] instanceof Array ? target[property] : []);
} else if (
source[property] !== null &&
typeof source[property] === 'object' &&
source[property].constructor === Object
) {
target[property] = mergeProperties(target[property] || {}, source[property]);
} else {
target[property] = source[property];
}
}
}
return target;
}
/**
* Merge an arbitrary number of objects into one.
* This function modifies the <code>target</code> object but also returns it.
*
* @param {Object} target The target object of the merge
*
* @returns {Object}
*/
function extend(target) {
for(var i = 1; i < arguments.length; i++) {
target = _mergeProperties(target, arguments[i]);
}
return target;
}
/* The right word count in respect for CJK. */ /* The right word count in respect for CJK. */
function wordCount(data) { function wordCount(data) {
@ -745,6 +790,12 @@ var toolbarBuiltInButtons = {
} }
}; };
var replaceTexts = {
link: ["[", "](http://)"],
image: ["![](http://", ")"],
horizontalRule: ["", "\n\n-----\n\n"]
};
/** /**
* Interface of SimpleMDE. * Interface of SimpleMDE.
@ -816,6 +867,10 @@ function SimpleMDE(options) {
// Set default options for parsing config // Set default options for parsing config
options.parsingConfig = options.parsingConfig || {}; options.parsingConfig = options.parsingConfig || {};
// Merging the replaceTexts, with the given options
options.replaceTexts = extend({}, replaceTexts, options.replaceTexts || {});
// Update this options // Update this options
this.options = options; this.options = options;

Loading…
Cancel
Save