Skip to content

Commit

Permalink
添加setMD、setHTML和添加编辑器初始值value
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 3, 2015
1 parent 04e4cbd commit 760deea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion build/MDEditor.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@
id: "#mdeditor",
minheight: 200,
maxheight: 500,
value: "",
themes: ""
};
self.obj = _$("#" + opts.id ? opts.id : "mdeditor");
Expand All @@ -891,6 +892,7 @@
var self = this, box = self.obj[0], index = 0;
if (self.obj.length > 0) box.appendChild(self.preview), box.appendChild(self.tools),
box.appendChild(self.textarea);
self.setMD();
addEvent(self.btn_preview, "click", function(event) {
index = parseInt(self.preview.style.zIndex);
if (index === 9) {
Expand All @@ -900,7 +902,7 @@
self.preview.style.zIndex = 9;
this.innerHTML = "编辑";
}
if (marked) self.preview.innerHTML = marked(self.getMD());
self.setHTML(self.getMD());
css(self.preview, {
height: self.textarea.offsetHeight + "px"
});
Expand Down Expand Up @@ -928,9 +930,17 @@
MDEditor.prototype.getMD = function() {
return this.textarea.value;
};
MDEditor.prototype.setMD = function(val) {
if (marked) this.textarea.value = val ? val : this.opts.value;
return this;
};
MDEditor.prototype.getHTML = function() {
if (marked) return marked(this.getMD()); else return this.preview.innerHTML;
};
MDEditor.prototype.setHTML = function(val) {
if (marked) this.preview.innerHTML = marked(val ? val : this.opts.value);
return this;
};
if (typeof window.define === "function" && window.define.amd) {
window.define(function() {
return MDEditor;
Expand Down
22 changes: 21 additions & 1 deletion lib/MDEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
id:"#mdeditor",
minheight:200,
maxheight:500,
value:"",
themes:""
};

Expand Down Expand Up @@ -146,6 +147,8 @@
box.appendChild(self.tools),
box.appendChild(self.textarea);

self.setMD();

addEvent(self.btn_preview,'click', function(event) {
index = parseInt(self.preview.style.zIndex)
if(index === 9){
Expand All @@ -155,7 +158,7 @@
self.preview.style.zIndex = 9
this.innerHTML = "编辑"
}
if(marked) self.preview.innerHTML = marked(self.getMD())
self.setHTML(self.getMD())
css(self.preview,{'height':self.textarea.offsetHeight + "px"});
});

Expand All @@ -182,6 +185,15 @@
MDEditor.prototype.getMD =function(){
return this.textarea.value
}

/**
* [setMD 设置markdown的字符串]
*/
MDEditor.prototype.setMD =function(val){
if(marked) this.textarea.value = val?val:this.opts.value;
return this;
}

/**
* [getHTML 获取生成的HTML]
* @return {[type]} [返回HTML字符串]
Expand All @@ -191,6 +203,14 @@
else return this.preview.innerHTML
}

/**
* [setHTML 设置预览的HTML]
*/
MDEditor.prototype.setHTML =function(val){
if(marked) this.preview.innerHTML = marked(val?val:this.opts.value);
return this;
}

if (typeof window.define === 'function' && window.define.amd) {
window.define(function () { return MDEditor; });
} else {
Expand Down

0 comments on commit 760deea

Please sign in to comment.