插件地址
官方地址:https://pandao.github.io/editor.md/
使用示例:http://editor.md.ipandao.com/examples/index.html
github地址:https://github.com/pandao/editor.md
引入依赖
在入口htm文件中引入相关依赖,设置div
<script src="jquery.min.js"></script><script src="./plug/editormd/editormd.min.js"></script><link rel="stylesheet" href="./plug/editormd/css/editormd.min.css" /><div id="test-editor"><textarea style="display:none;">### 关于 Editor.md</textarea></div>
初始化编辑器
如果是使用react或vue等框架,因为editormd已经挂载到全局,使用window.editormd获取对象
<script type="text/javascript">$(function() {var editor = editormd("test-editor", {// width : "100%",// height : "100%",path : "editormd/lib/", // 必须设置,编辑器相关模块markdown: 'hello world', // 初始内容emoji: true, // 表情saveHTMLToTextarea: true,// 工具栏设置toolbarIcons: function () {// Or return editormd.toolbarModes[name]; // full, simple, mini// Using "||" set icons align right.return ["undo", "redo", "|", "bold", "hr", "|", "preview", "watch", "|", "fullscreen", "info", "testIcon", "testIcon2", "file", "faicon", "||", "watch", "fullscreen", "preview", "testIcon"]},toolbarIconsClass: { // 指定一个FontAawsome的图标类testIcon: "fa-gears"},toolbarIconTexts: { // 如果没有图标,则可以这样直接插入内容,可以是字符串或HTML标签testIcon2: "测试按钮"},toolbarCustomIcons: { // 用于增加自定义工具栏的功能,可以直接插入HTML标签,不使用默认的元素创建图标(所有默认事件失效)file: `<input type="file" accept=".md" />`,faicon: `<i class="fa fa- star" onclick="alert('faicon'); "></i>`},toolbarHandlers: { // 自定义工具栏按钮的事件处理/*** @param {Object} cm CodeMirror对象* @param {Object} icon 图标按钮jQuery元素对象* @param {Object} cursor CodeMirror的光标对象,可获取光标所在行和位置* @param {String} selection 编辑器选中的文本*/testIcon: function (cm, icon, cursor, selection) {//var cursor = cm.getCursor(); //获取当前光标对象,同cursor参数//var selection = cm.getSelection(); //获取当前选中的文本,同selection参数// 替换选中文本,如果没有选中文本,则直接插入cm.replaceSelection("[" + selection + ":testIcon]");// 如果当前没有选中的文本,将光标移到要输入的位置if (selection === "") {cm.setCursor(cursor.line, cursor.ch + 1);}// this == 当前editormd实例console.log("testIcon =>", this, cm, icon, cursor, selection);},testIcon2: function (cm, icon, cursor, selection) {cm.replaceSelection("[" + selection + ":testIcon2](" + icon.html() + ")");console.log("testIcon2 =>", this, icon.html());}},lang: { // 自定义按钮的提示文本,即title属性toolbar: {file: "上传文件",testIcon: "自定义按钮testIcon",testIcon2: "自定义按钮testIcon2",undo: "撤销 (Ctrl+Z)"}},// 本地图片上传(跨域请参考官网)imageUpload: true, // 设置本地上传按钮imageFormats: ['jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG'] // 虽然计算机不区分大小写imageUploadURL: 'upload/file' // 上传地址// 编辑器加载完成或图片上传完成onload: function () {$("[type=\"file\"]").bind("change", function () {alert($(this).val());testEditor.cm.replaceSelection($(this).val());console.log($(this).val(), testEditor);});}});});</script>
注意事项
1、获取编辑器中所有图片地址:应该用正则匹配到这东西就可以了”“
参考资料
https://blog.csdn.net/zhulier1124/article/details/80242300
https://www.jianshu.com/p/176a446e5b64
https://juejin.im/post/5ac2ecbd518825558c479d0e
https://blog.csdn.net/cnhome/article/details/82855271
https://zhidao.baidu.com/question/521582839.html
https://blog.csdn.net/JontyHua/article/details/90142999
