给UMEditor 增加placeholder
工作中,遇到要使用富文本编辑器的场景,而UEditor(下称UE)又太庞大,于是采用UMEditor(下称UM)。
经过使用发现UE和UE均无placeholder功能,于是从网上找到UE的palceholder代码,经过查看UM源代码,发现也可移值到UM上。代码如下(适用UEditor时仅需将UM改为UE即可):
UM.Editor.prototype.placeholder = function(justPlainText) {
var _editor = this;
_editor.addListener("focus", function() {
var localHtml = _editor.getPlainTxt();
if($.trim(localHtml) === $.trim(justPlainText)) {
_editor.setContent(" ");
}
});
_editor.addListener("blur", function() {
var localHtml = _editor.getContent();
if(!localHtml) {
_editor.setContent(justPlainText);
}
});
_editor.ready(function() {
_editor.fireEvent("blur");
});
};
var um = UM.getEditor('myEditor');
um.placeholder("这里写你的初始化内容");
然而,在实际应用中,在拖拽图片到编辑器和上传图片时,placeholder也当作内容输入到编辑框里了,这显然不是我们想要的。
经过研究,在umeditor.js里修改如下两个地方,达到可用程度。
代码如下:
// line:5756
UM.commands['insertimage'] = {
execCommand:function (cmd, opt) {
//……
//……
//在这里添加这行代码
if(me.getPlainTxt() == "这里写你的初始化内容\n") me.setContent(" ");
me.execCommand('insertHtml', html.join(''), true);
}
};
// line:8020
UM.plugins['autoupload'] = function () {
var me = this;
me.setOpt('pasteImageEnabled', true);
me.setOpt('dropFileEnabled', true);
var sendAndInsertImage = function (file, editor) {
//模拟数据
var fd = new FormData();
fd.append(editor.options.imageFieldName || 'upfile', file, file.name || ('blob.' + file.type.substr('image/'.length)));
fd.append('type', 'ajax');
var xhr = new XMLHttpRequest();
xhr.open("post", me.options.imageUrl, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.addEventListener('load', function (e) {
try {
var json = eval('('+e.target.response+')'),
link = json.url,
picLink = me.options.imagePath + link;
//在这里添加这行代码
if(editor.getPlainTxt() == "这里写你的初始化内容\n") editor.setContent(" ");
editor.execCommand('insertimage', {
src: picLink,
_src: picLink
});
} catch (er) {
}
});
xhr.send(fd);
};
//……
//……
//……
}