From fee8a87c06a732fcad3dd2ba4da484222d0729fe Mon Sep 17 00:00:00 2001 From: Alex Canessa Date: Mon, 2 Nov 2015 11:53:23 +0000 Subject: [PATCH 1/4] Add extend and _mergeProperties function, to be able to merge a given option with a default --- src/js/simplemde.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/js/simplemde.js b/src/js/simplemde.js index 09efa05..e0151f4 100644 --- a/src/js/simplemde.js +++ b/src/js/simplemde.js @@ -604,6 +604,50 @@ 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) { From 07196a532f6cb704c4dceabb4a7dc6f4a81c2b69 Mon Sep 17 00:00:00 2001 From: Alex Canessa Date: Mon, 2 Nov 2015 12:00:24 +0000 Subject: [PATCH 2/4] Changed the _replaceSelection startEnd arg to be an array; Create the replaceTexts var; Extending the replaceText with the options (using the merging functions); Use the replaceText option for the drawLink and drawImage functions --- src/js/simplemde.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/js/simplemde.js b/src/js/simplemde.js index e0151f4..fb2daf1 100644 --- a/src/js/simplemde.js +++ b/src/js/simplemde.js @@ -260,27 +260,26 @@ 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. */ @@ -399,11 +398,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) { @@ -789,6 +790,10 @@ var toolbarBuiltInButtons = { } }; +var replaceTexts = { + link: ["[", "](http://)"], + image: ["![](http://", ")"] +}; /** * Interface of SimpleMDE. @@ -861,6 +866,10 @@ function SimpleMDE(options) { options.parsingConfig = options.parsingConfig || {}; + // Merging the replaceTexts, with the given options + options.replaceTexts = extend({}, replaceTexts, options.replaceTexts || {}); + + // Update this options this.options = options; From f8fc4d2b12c0fe8c0a65ddc3594c1fb099f8a7dd Mon Sep 17 00:00:00 2001 From: Alex Canessa Date: Mon, 2 Nov 2015 12:06:51 +0000 Subject: [PATCH 3/4] Indenting; Add horizontalRule to the replaceTexts option --- src/js/simplemde.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/js/simplemde.js b/src/js/simplemde.js index fb2daf1..ef0dd2f 100644 --- a/src/js/simplemde.js +++ b/src/js/simplemde.js @@ -266,7 +266,7 @@ function toggleOrderedList(editor) { function drawLink(editor) { var cm = editor.codemirror; var stat = getState(cm); - var options = editor.options; + var options = editor.options; _replaceSelection(cm, stat.link, options.replaceTexts.link); } @@ -276,8 +276,8 @@ function drawLink(editor) { function drawImage(editor) { var cm = editor.codemirror; var stat = getState(cm); - var options = editor.options; - _replaceSelection(cm, stat.image, options.replaceTexts.image); + var options = editor.options; + _replaceSelection(cm, stat.image, options.replaceTexts.image); } /** @@ -286,8 +286,8 @@ function drawImage(editor) { 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);} /** @@ -792,7 +792,8 @@ var toolbarBuiltInButtons = { var replaceTexts = { link: ["[", "](http://)"], - image: ["![](http://", ")"] + image: ["![](http://", ")"], + horizontalRule: ["", "\n\n-----\n\n"] }; /** From b9d43cec8f43f6fee56dbb374dc39d71033813c2 Mon Sep 17 00:00:00 2001 From: Alex Canessa Date: Mon, 2 Nov 2015 12:11:09 +0000 Subject: [PATCH 4/4] Variable name fix --- src/js/simplemde.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/simplemde.js b/src/js/simplemde.js index ef0dd2f..eeb3f3a 100644 --- a/src/js/simplemde.js +++ b/src/js/simplemde.js @@ -287,7 +287,7 @@ function drawHorizontalRule(editor) { var cm = editor.codemirror; var stat = getState(cm); var options = editor.options; - _replaceSelection(cm, stat.image, options.replaceTexts.HorizontalRule);} + _replaceSelection(cm, stat.image, options.replaceTexts.horizontalRule);} /**