喜乐人 發表於 2019-7-29 10:58:00

Vue使用vue-html5-editor的 富文本编辑器

<p>最近由于工作的需求,需要在后台管理界面中,添加一个富文本编辑器。</p>
<p>&nbsp;</p>
<p>富文本编辑器,我们之前用过百度的ueditor,在传统模式开发的时候还是很方便的,可以很便利的进行内容编辑,然后获取到编辑内容的信息</p>
<p>百度的ueditor地址:</p>
<p>https://ueditor.baidu.com/website/</p>
<p>&nbsp;</p>
<p>但是这次是要整合一个vue的,还没有这么搞过,就试试吧。</p>
<p>富文本编辑器的核心原理其实蛮简单的,但是自己写还是很麻烦,没有那个时间。</p>
<p>1.考虑方式</p>
<p>有两种方式,一种是把ueditor进行vue的移植改造。把传统模式改成webpack可用的模式就可以了。</p>
<p>但是,我觉得ueditor已经很久没有维护了,界面也太丑了,所以试试别的能用的。</p>
<p>&nbsp;</p>
<p>2.踩坑&nbsp;froala editor...</p>
<p>搞了很久,按这位老兄说的</p>
<p>https://www.jianshu.com/p/cb782b957a13</p>
<p>也许是因为这个本身是收费的,也许是我哪个地方有问题,没搞出来。</p>
<p>&nbsp;</p>
<p>3.改换用vue-html5-editor</p>
<p>改换了以后就成功了,上图看看</p>
<p><img src="https://img2018.cnblogs.com/blog/939469/201907/939469-20190729103812751-385025779.png"></p>
<p>&nbsp;</p>
<p>先安装</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:false;">npm install vue-html5-editor --save</pre>
</div>
<p>安装完毕之后引入到项目中</p>
<p>这个是编辑的main.js,按照官方说明&nbsp;https://gitee.com/tai/vue-html5-editor&nbsp;以全局引入的方式引入的</p>
<div class="cnblogs_code">
<pre>import VueHtml5Editor from 'vue-html5-editor'<span style="color: rgba(0, 0, 0, 1)">

Vue.use(VueHtml5Editor, {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 全局组件名称,使用new VueHtml5Editor(options)时该选项无效</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> global component name</span>
name: 'vue-html5-editor'<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否显示模块名称,开启的话会在工具栏的图标后台直接显示名称</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> if set true,will append module name to toolbar after icon</span>
showModuleName: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义各个图标的class,默认使用的是font-awesome提供的图标</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> custom icon class of built-in modules,default using font-awesome</span>
<span style="color: rgba(0, 0, 0, 1)">icons: {
    text: </span>'fa fa-pencil'<span style="color: rgba(0, 0, 0, 1)">,
    color: </span>'fa fa-paint-brush'<span style="color: rgba(0, 0, 0, 1)">,
    font: </span>'fa fa-font'<span style="color: rgba(0, 0, 0, 1)">,
    align: </span>'fa fa-align-justify'<span style="color: rgba(0, 0, 0, 1)">,
    list: </span>'fa fa-list'<span style="color: rgba(0, 0, 0, 1)">,
    link: </span>'fa fa-chain'<span style="color: rgba(0, 0, 0, 1)">,
    unlink: </span>'fa fa-chain-broken'<span style="color: rgba(0, 0, 0, 1)">,
    tabulation: </span>'fa fa-table'<span style="color: rgba(0, 0, 0, 1)">,
    image: </span>'fa fa-file-image-o'<span style="color: rgba(0, 0, 0, 1)">,
    hr: </span>'fa fa-minus'<span style="color: rgba(0, 0, 0, 1)">,
    eraser: </span>'fa fa-eraser'<span style="color: rgba(0, 0, 0, 1)">,
    undo: </span>'fa-undo fa'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'full-screen': 'fa fa-arrows-alt'<span style="color: rgba(0, 0, 0, 1)">,
    info: </span>'fa fa-info'<span style="color: rgba(0, 0, 0, 1)">
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 配置图片模块</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> config image module</span>
<span style="color: rgba(0, 0, 0, 1)">image: {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件最大体积,单位字节max file size</span>
    <span style="color: rgba(0, 128, 0, 1)">sizeLimit: 512 * 1024,</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 上传参数,默认把图片转为base64而不上传</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> upload config,default null and convert image to base64</span>
<span style="color: rgba(0, 0, 0, 1)">    upload: {
      url: </span>'http://localhost:8080/files/upload'<span style="color: rgba(0, 0, 0, 1)">,
      headers: {},
      params: {},
      fieldName: </span>'file'<span style="color: rgba(0, 0, 0, 1)">
    },
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 压缩参数,默认使用localResizeIMG进行压缩,设置为null禁止压缩</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> compression config,default resize image by localResizeIMG (https://github.com/think2011/localResizeIMG)</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> set null to disable compression</span>
    compress: <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 响应数据处理,最终返回图片链接</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> handle response data,return image url</span>
<span style="color: rgba(0, 0, 0, 1)">    uploadHandler (responseText) {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> default accept json data like{ok:false,msg:"unexpected"} or {ok:true,data:"image url"}</span>
      <span style="color: rgba(0, 0, 255, 1)">var</span> json =<span style="color: rgba(0, 0, 0, 1)"> JSON.parse(responseText)
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 0, 1)">json.data.fileurl
    }
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 语言,内建的有英文(en-us)和中文(zh-cn)</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> default en-us, en-us and zh-cn are built-in</span>
language: 'zh-cn'<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义语言</span>
<span style="color: rgba(0, 0, 0, 1)">i18n: {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> specify your language here</span>
    'zh-cn'<span style="color: rgba(0, 0, 0, 1)">: {
      </span>'align': '对齐方式'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'image': '图片'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'list': '列表'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'link': '链接'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'unlink': '去除链接'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'table': '表格'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'font': '文字'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'full screen': '全屏'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'text': '排版'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'eraser': '格式清除'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'info': '关于'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'color': '颜色'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'please enter a url': '请输入地址'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'create link': '创建链接'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'bold': '加粗'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'italic': '倾斜'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'underline': '下划线'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'strike through': '删除线'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'subscript': '上标'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'superscript': '下标'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'heading': '标题'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'font name': '字体'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'font size': '文字大小'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'left justify': '左对齐'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'center justify': '居中'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'right justify': '右对齐'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'ordered list': '有序列表'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'unordered list': '无序列表'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'fore color': '前景色'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'background color': '背景色'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'row count': '行数'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'column count': '列数'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'save': '确定'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'upload': '上传'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'progress': '进度'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'unknown': '未知'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'please wait': '请稍等'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'error': '错误'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'abort': '中断'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'reset': '重置'<span style="color: rgba(0, 0, 0, 1)">
    }
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 隐藏不想要显示出来的模块</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> the modules you don't want</span>
<span style="color: rgba(0, 0, 0, 1)">hiddenModules: [
    </span>'info'<span style="color: rgba(0, 0, 0, 1)">
],
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义要显示的模块,并控制顺序</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> keep only the modules you want and customize the order.</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> can be used with hiddenModules together</span>
<span style="color: rgba(0, 0, 0, 1)">visibleModules: [
    </span>'text'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'color'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'font'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'align'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'list'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'link'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'unlink'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'tabulation'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'image'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'hr'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'eraser'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'undo'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'full-screen'<span style="color: rgba(0, 0, 0, 1)">,
    </span>'info'</pre>
<pre><span style="color: rgba(0, 0, 0, 1)">], </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 扩展模块,具体可以参考examples或查看源码</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> extended modules</span> <span style="color: rgba(0, 0, 0, 1)"> modules: { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> omit,reference to source code of build-in modules</span> <span style="color: rgba(0, 0, 0, 1)"> } })</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;在模块中引入</p>
<p>模块 contents.vue</p>
<div class="cnblogs_code">
<pre>&lt;template&gt;
&lt;div id="contents"&gt;
    &lt;h1&gt;添加内容&lt;/h1&gt;
    &lt;br&gt;
    &lt;vue-html5-editor :content="content" @change="updateData" :height="500" :z-index="1000" :auto-height="true" :show-module-name="false"&gt;&lt;/vue-html5-editor&gt;
&lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
name: </span>'Contents'<span style="color: rgba(0, 0, 0, 1)">,
data () {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
      id: </span>''<span style="color: rgba(0, 0, 0, 1)">,
      content: </span>''<span style="color: rgba(0, 0, 0, 1)">
    }
},</span><span style="color: rgba(0, 0, 0, 1)">
methods: {
    updateData (e) {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.content =<span style="color: rgba(0, 0, 0, 1)"> e
    }
}
}
</span>&lt;/script&gt;

&lt;!-- Add "scoped" attribute to limit CSS to <span style="color: rgba(0, 0, 255, 1)">this</span> component only --&gt;
&lt;style scoped&gt;

&lt;/style&gt;</pre>
</div>
<p>&nbsp;</p>
<p>嘿嘿,运行起来以后,你会发现,有框,但按钮不在,因为这是个坑。 按钮是个开源组件 font-awesome</p>
<p>这个开源组件就是个图标库,有多种引入方式,我采用的是css全局引入的方式,</p>
<p>先下载了http://www.fontawesome.com.cn/download/font-awesome-4.7.0.zip</p>
<p>解压放入 src/assets,再到App.vue的style引入</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">&lt;style&gt;
@import 'assets/font-awesome-4.7.0/css/font-awesome.min.css';
&lt;/style&gt;</pre>
</div>
<p>&nbsp;到此,引入就告一段落。</p>
<p>&nbsp;</p>
<p>添加文件上传的部分。</p>
<p>文件上传我有两个部分</p>
<p>图片文件上传: http://localhost:8080/files/upload</p>
<p>这个上传要求请求方式为POST,form表单的文件部分字段名为file</p>
<p>返回如下</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
    status: </span>200<span style="color: rgba(0, 0, 0, 1)">,
    data:{
      filename: </span>"2019072935563.png"<span style="color: rgba(0, 0, 0, 1)">,
      fileurl: </span>"http://localhost:8080/files/2019072935563.png"<span style="color: rgba(0, 0, 0, 1)">
    },
    message: </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
}   
  </span></pre>
</div>
<p>&nbsp;</p>
<p>图片文件获取: http://localhost:8080/files/{imgname}</p>
<p>这样就完成了所有功能</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/enroban/p/11262716.html
頁: [1]
查看完整版本: Vue使用vue-html5-editor的 富文本编辑器