在实际生产过程中,在粘贴插入图片或者说是把内容传给后台,后台再传递回来时,被压缩的base64字符串将会以长字符串的显示,妨碍阅读,状况如下
在查询完解决方案,并参考了官方issues的更改:https://github.com/Tencent/cherry-markdown/pull/727https://github.com/Tencent/cherry-markdown/pull/727
在issues的更改基础上,我们需要在if语句里面也加上 this.editor.dealSpecialWords();
更改如下,假如你引用的是cherry-markdown.js,需要对setValue函数进行更改,行数大概在71034行(见截图):
function setValue(content) {
var keepCursor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
this.editor.storeDocumentScroll();
// keepCursor 用于确定在设置文本后是否需要保持光标的当前位置
if (keepCursor === false) {
// 在这里也需要进行base64的处理
this.editor.dealSpecialWords(); // 添加这一句
return this.editor.editor.setValue(content);
}
var codemirror = this.editor.editor;
var old = this.getValue();
var pos = codemirror.getDoc().indexFromPos(codemirror.getCursor());
var newPos = getPosBydiffs(pos, old, content);
codemirror.setValue(content);
var cursor = codemirror.getDoc().posFromIndex(newPos);
codemirror.setCursor(cursor);
this.editor.dealSpecialWords();
}
如果你引用的是cherry-markdown.min.js,需要对定位到更改的地方, 建议使用 return this.editor.editor.setValue( 来进行定位,然后进行代码修改,修改为 {this.editor.dealSpecialWords();return this.editor.editor.setValue(t);} 。详见截图和代码:
// 注意这一段代码需要加上{},因为没改之前if语句后面只有一句话,现在有两句
{this.editor.dealSpecialWords();return this.editor.editor.setValue(t);}
以下是解决完的效果图:
愿能帮到大家少走弯路,如果有更好的解决方法也可以私我一下,哈哈哈