Skip to content

Commit 8bf55b1

Browse files
committed
Add the replace selection text to the options.
1 parent e20b64c commit 8bf55b1

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

src/js/simplemde.js

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,34 +260,34 @@ function toggleOrderedList(editor) {
260260
_toggleLine(cm, "ordered-list");
261261
}
262262

263-
264263
/**
265264
* Action for drawing a link.
266265
*/
267266
function drawLink(editor) {
268267
var cm = editor.codemirror;
269268
var stat = getState(cm);
270-
_replaceSelection(cm, stat.link, "[", "](http://)");
269+
var options = editor.options;
270+
_replaceSelection(cm, stat.link, options.replaceTexts.link);
271271
}
272272

273-
274273
/**
275274
* Action for drawing an img.
276275
*/
277276
function drawImage(editor) {
278277
var cm = editor.codemirror;
279278
var stat = getState(cm);
280-
_replaceSelection(cm, stat.image, "![](http://", ")");
279+
var options = editor.options;
280+
_replaceSelection(cm, stat.image, options.replaceTexts.image);
281281
}
282282

283-
284283
/**
285284
* Action for drawing a horizontal rule.
286285
*/
287286
function drawHorizontalRule(editor) {
288287
var cm = editor.codemirror;
289288
var stat = getState(cm);
290-
_replaceSelection(cm, stat.image, "", "\n\n-----\n\n");
289+
var options = editor.options;
290+
_replaceSelection(cm, stat.image, options.replaceTexts.horizontalRule);
291291
}
292292

293293

@@ -399,11 +399,13 @@ function togglePreview(editor) {
399399
toggleSideBySide(editor);
400400
}
401401

402-
function _replaceSelection(cm, active, start, end) {
402+
function _replaceSelection(cm, active, startEnd) {
403403
if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
404404
return;
405405

406406
var text;
407+
var start = startEnd[0];
408+
var end = startEnd[1];
407409
var startPoint = cm.getCursor("start");
408410
var endPoint = cm.getCursor("end");
409411
if(active) {
@@ -604,6 +606,49 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
604606
cm.focus();
605607
}
606608

609+
/**
610+
* Merge the properties of one object into another.
611+
*
612+
* @param {Object} target The object where the properties will be copied
613+
* @param {Object} source The object whose properties will be copied
614+
*
615+
* @returns {Object}
616+
*/
617+
function _mergeProperties(target, source) {
618+
for(var property in source) {
619+
if (source.hasOwnProperty(property)) {
620+
if (source[property] instanceof Array) {
621+
target[property] = source[property].concat(target[property] instanceof Array ? target[property] : []);
622+
} else if (
623+
source[property] !== null &&
624+
typeof source[property] === 'object' &&
625+
source[property].constructor === Object
626+
) {
627+
target[property] = mergeProperties(target[property] || {}, source[property]);
628+
} else {
629+
target[property] = source[property];
630+
}
631+
}
632+
}
633+
634+
return target;
635+
}
636+
637+
/**
638+
* Merge an arbitrary number of objects into one.
639+
* This function modifies the <code>target</code> object but also returns it.
640+
*
641+
* @param {Object} target The target object of the merge
642+
*
643+
* @returns {Object}
644+
*/
645+
function extend(target) {
646+
for(var i = 1; i < arguments.length; i++) {
647+
target = _mergeProperties(target, arguments[i]);
648+
}
649+
650+
return target;
651+
}
607652

608653
/* The right word count in respect for CJK. */
609654
function wordCount(data) {
@@ -745,6 +790,12 @@ var toolbarBuiltInButtons = {
745790
}
746791
};
747792

793+
var replaceTexts = {
794+
link: ["[", "](http://)"],
795+
image: ["![](http://", ")"],
796+
horizontalRule: ["", "\n\n-----\n\n"]
797+
};
798+
748799

749800
/**
750801
* Interface of SimpleMDE.
@@ -816,6 +867,10 @@ function SimpleMDE(options) {
816867
// Set default options for parsing config
817868
options.parsingConfig = options.parsingConfig || {};
818869

870+
871+
// Merging the replaceTexts, with the given options
872+
options.replaceTexts = extend({}, replaceTexts, options.replaceTexts || {});
873+
819874

820875
// Update this options
821876
this.options = options;

0 commit comments

Comments
 (0)