ckeditor5,ckeditor4富文本编辑器在vue中的使用

先介绍下ckeditor5的使用方式:javascript

安装依赖:vue

npm install --save @ckeditor/ckeditor5-vue @ckeditor/ckeditor5-build-classic

要建立编辑器实例,必须首先将编辑器构建和组件模块导入应用程序的根文件中(例如,main.js在由Vue CLI生成时)。而后,使用如下Vue.use()方法启用组件java

import Vue from 'vue';
import CKEditor from '@ckeditor/ckeditor5-vue';

Vue.use( CKEditor );

在组件中的具体使用方式以下:webpack

<template>
    <div id="app">
        <ckeditor :editor="editor" v-model="editorData" :config="editorConfig"></ckeditor>
    </div>
</template>

<script>
    import ClassicEditor from '@ckeditor/ckeditor5-build-classic';

    export default {
        name: 'app',
        data() {
            return {
                editor: ClassicEditor,
                editorData: '<p>Content of the editor.</p>',
                editorConfig: {
                    // The configuration of the editor.
                }
            };
        }
    }
</script>

若是须要自定义配置,则能够配置以下:web

editorConfig: {
     toolbar: [
'heading', 'fontSize', 'highlight', 'highlight:yellowMarker', 'highlight:greenMarker', 'highlight:pinkMarker', 'highlight:blueMarker', 'fontFamily', 'alignment', 'imageUpload', 'bold', 'italic', 'underline', 'imageStyle:full', 'imageStyle:alignLeft', 'imageStyle:alignRight', 'link', 'undo', 'redo'], fontSize: { options: [8, 9, 10, 11, 12, 'default', 14, 16, 18, 20, 22, 24, 26, 28, 36, 44, 48, 72], },      highlight: {       options: [         { model: 'blackPen', class: 'pen-black', title: '黑色', color: 'var(--ck-highlight-pen-black)', type: 'pen' },         { model: 'redPenPen', class: 'pen-red', title: '红色', color: 'var(--ck-highlight-pen-red)', type: 'pen' },      }     },     ckfinder: {       uploadUrl: `${store.getters.currentStack.baseURL}/ckeditor/upload`,       // 后端处理上传逻辑返回json数据,包括uploaded 上传的字节数 和url两个字段      }, }language: 'zh-cn',

 这里要说明的是上传接口必须返回的有uploaded和url这两个字段才能够,以下图:npm

 

设置ckeditor5输入区域的高度:json

<style>
.ck-editor__editable { min-height: 100px; }
</style>

效果图以下:后端

至此,就大功告成了!!!!!!api

在此要说明一点,ckeditor5在ie11下是不兼容的,要想在ie11下正常运行,可能须要较低版本的ckeditor,,,在解决兼容性的时候,发现ckeditor4是兼容ie11的。app

下面咱们介绍一下ckeditor4的使用方式:

从CKEditor 网站:https://ckeditor.com/ckeditor-4/download/下载CKEditor,这个是我项目中下载好的包,已经上传到个人百度网盘

连接:https://pan.baidu.com/s/1_fskJGVMedK_7ghvK8Q4qg
提取码:yals
复制这段内容后打开百度网盘手机App,操做更方便哦

引用ckeditor,而且须要在webpack.base.config.js中配置以下:

 

<script type="text/javascript" src="static/ckeditor/ckeditor.js"></script>
<textarea id="editor" rows="10" cols="80"></textarea>
import CKEDITOR from "CKEDITOR"

mounted: function () { CKEDITOR.replace("editor", {height: "300px", width: "100%", toolbar: "Full"}); this.editor = CKEDITOR.instances.editor; console.log(this.editor.getData()); },

效果以下图:

在此说一下关于图片上传的配置:

修改ckeditor文件夹下config.js:

 config.removeDialogTabs = 'image:advanced;image:Link';//隐藏超连接和高级选项
  config.image_previewText = ' ';//设置图片预览说明为空
//上传图片窗口用到的接口
  config.filebrowserImageUploadUrl = "http://192.168.12.160:8090/gdt_information/messageFeedBack/uploadImgEdit";
  /*config.filebrowserUploadUrl = "http://192.168.12.160:8090/gdt_information/messageFeedBack/uploadImgEdit";

  // 使上传图片弹窗出现对应的“上传”tab标签
  config.removeDialogTabs = 'image:advanced;link:advanced';

  //粘贴图片时用获得
  config.extraPlugins = 'uploadimage';
  config.uploadUrl = "http://192.168.12.160:8090/gdt_information/messageFeedBack/uploadImgEdit";*/

这样就能实现图片的上传啦,

效果图以下:

 在项目中发现,ckeditor的setData('1111111')方法很差用,总是设置不上数据,因而我本身也是百度了各类资料都不大好使,后来用dom添加的方式给实现了,

我是这样写的

 1 this.$http
 2         .get(
 3           `${global.postUrl}/gdt_information/newsManage/queryNewsById?newsId=${this.newsId}`
 4         )
 5         .then(res => {
 6           this.editForm = res.data.data;
 7           this.editor.setData(this.editForm.newsContent);
 8           let body = (document.getElementById('editor-textarea').getElementsByTagName('iframe')[0]).contentDocument.getElementsByTagName('body');
 9           console.log(body);
10           setTimeout(() => {
11             if(body.length ==1){
12               console.log(body[0]);
13               body[0].innerHTML  = this.editForm.newsContent;
14             }
15           },500);
16 
17         })
18         .catch(error => {
19           console.log(error);
20         });

在textarea的外层声明了一个div,

也不知道还有没有更好的解决办法,但愿有更好办法的小伙伴多多分享!!!