From 8bf55b1b3432e19668499b1c13c89327c889e52f Mon Sep 17 00:00:00 2001 From: Alex Canessa Date: Tue, 3 Nov 2015 10:36:24 +0000 Subject: [PATCH] Add the replace selection text to the options. --- src/js/simplemde.js | 69 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/src/js/simplemde.js b/src/js/simplemde.js index c0bc34e..1069863 100644 --- a/src/js/simplemde.js +++ b/src/js/simplemde.js @@ -260,34 +260,34 @@ function toggleOrderedList(editor) { _toggleLine(cm, "ordered-list"); } - /** * Action for drawing a link. */ function drawLink(editor) { var cm = editor.codemirror; 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. */ function drawImage(editor) { var cm = editor.codemirror; 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. */ function drawHorizontalRule(editor) { var cm = editor.codemirror; 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); } -function _replaceSelection(cm, active, start, end) { +function _replaceSelection(cm, active, startEnd) { if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className)) return; var text; + var start = startEnd[0]; + var end = startEnd[1]; var startPoint = cm.getCursor("start"); var endPoint = cm.getCursor("end"); if(active) { @@ -604,6 +606,49 @@ function _toggleBlock(editor, type, start_chars, end_chars) { 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 target 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. */ function wordCount(data) { @@ -745,6 +790,12 @@ var toolbarBuiltInButtons = { } }; +var replaceTexts = { + link: ["[", "](http://)"], + image: ["![](http://", ")"], + horizontalRule: ["", "\n\n-----\n\n"] +}; + /** * Interface of SimpleMDE. @@ -816,6 +867,10 @@ function SimpleMDE(options) { // Set default options for parsing config options.parsingConfig = options.parsingConfig || {}; + + // Merging the replaceTexts, with the given options + options.replaceTexts = extend({}, replaceTexts, options.replaceTexts || {}); + // Update this options this.options = options;