vue cli4整合CKEditor5

最简单集成


npm install --save @ckeditor/ckeditor5-vue

  以下5选一即可不同样式的编辑器

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

             示例:https://ckeditor.com/docs/ckeditor5/latest/examples/builds/classic-editor.html

  • npm install --save @ckeditor/ckeditor5-build-inline

             示例:https://ckeditor.com/docs/ckeditor5/latest/examples/builds/inline-editor.html

  • npm install --save @ckeditor/ckeditor5-build-balloon

             示例:https://ckeditor.com/docs/ckeditor5/latest/examples/builds/balloon-block-editor.html

  • npm install --save @ckeditor/ckeditor5-build-balloon-block

             示例:https://ckeditor.com/docs/ckeditor5/latest/examples/builds/balloon-editor.html

  • npm install --save @ckeditor/ckeditor5-build-decoupled-document

             示例: https://ckeditor.com/docs/ckeditor5/latest/examples/builds/document-editor.html

注:大部分情况下ckeditor5-build-classic(精简版)和ckeditor5-build-decoupled-document(功能比较齐全)就可以满足需求

封装组件


  1. 以经典文档编辑器为例子
<template>
  <div>
    <ckeditor :editor="editor" v-model="content" :config="editorConfig" />
  </div>
</template>

<script>
import CKEditor from "@ckeditor/ckeditor5-vue";
import ClassicEditor from "@ckeditor/ckeditor5-build-classic";
import '@ckeditor/ckeditor5-build-classic/build/translations/zh-cn' 
export default {
  name: "Editor",
  components: {
    ckeditor: CKEditor.component
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    placeholder: {
      type: String,
      default: "请输入内容"
    }
  },
  created() {
    this.content = this.value;
  },
  watch: {
    value(val) {
      if (val && val !== this.content) {
        this.content = this.value;
      }
    },
    content(val) {
      if (val && val !== this.value) {
        this.$emit("change", val);
      }
    }
  },
  data() {
    return {
      editor: ClassicEditor,
      content: "",
      editorConfig: {
        language: 'zh-cn',  // 中文
        placeholder: this.placeholder
      }
    };
  }
};
</script>
<style scoped>
// 穿透 css,sass 使用 >>> less使用 /deep/
>>> .ck-content {
  min-height: 450px;
}
</style>

注: ckeditor5-build-classic编辑器不能添加其它插件例如图片(居右,居中,居左),代码块,office粘贴,缩进等。只能添加自定义文件上传插件。

2. 以文档编辑器为例子

<template>
  <div>
    <ckeditor :editor="editor" @ready="onReady" v-model="content" :config="editorConfig" />
  </div>
</template>

<script>
import CKEditor from "@ckeditor/ckeditor5-vue";
import DecoupledEditor from '@ckeditor/ckeditor5-build-decoupled-document';
import '@ckeditor/ckeditor5-build-decoupled-document/build/translations/zh-cn'; 
export default {
  name: "Editor",
  components: {
    ckeditor: CKEditor.component
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    placeholder: {
      type: String,
      default: "请输入内容"
    }
  },
  created() {
    this.content = this.value;
  },
  watch: {
    value(val) {
      if (val && val !== this.content) {
        this.content = this.value;
      }
    },
    content(val) {
      if (val && val !== this.value) {
        this.$emit("change", val);
      }
    }
  },
  data() {
    return {
      editor: DecoupledEditor ,
      content: "",
      editorConfig: {
        language: 'zh-cn',  // 中文
        placeholder: this.placeholder
      }
    };
  },
  methods: {
   onReady( editor )  {
      editor.ui.getEditableElement().parentElement.insertBefore(
       editor.ui.view.toolbar.element,
       editor.ui.getEditableElement()
      );
     }
   }
};
</script>
<style scoped>
// 穿透 css,sass 使用 >>> less使用 /deep/
>>> .ck-content {
  min-height: 450px;
}
</style>

   如果你还需添加其它功能(ckeditor5-build-decoupled-document默认带很多插件,但有些插件也没有例如控制图片大小)

   示例添加图片排版(居中,居右,居左)

   注:图片大小ckeditor5-build-decoupled-document不支持,参考后面的扩展插件编辑器

<template>
  <div>
    <ckeditor :editor="editor" @ready="onReady" v-model="content" :config="editorConfig" />
  </div>
</template>

<script>
import CKEditor from "@ckeditor/ckeditor5-vue";
import DecoupledEditor from '@ckeditor/ckeditor5-build-decoupled-document';
import '@ckeditor/ckeditor5-build-decoupled-document/build/translations/zh-cn'; 
export default {
  name: "Editor",
  components: {
    ckeditor: CKEditor.component
  },
  model: {
    prop: "value",
    event: "change"
  },
  props: {
    value: {
      type: String,
      default: ""
    },
    placeholder: {
      type: String,
      default: "请输入内容"
    }
  },
  created() {
    this.content = this.value;
  },
  watch: {
    value(val) {
      if (val && val !== this.content) {
        this.content = this.value;
      }
    },
    content(val) {
      if (val && val !== this.value) {
        this.$emit("change", val);
      }
    }
  },
  data() {
    return {
      editor: DecoupledEditor ,
      content: "",
      editorConfig: {
        language: 'zh-cn',  // 中文
        placeholder: this.placeholder,
        image: {
          toolbar: [
            "imageTextAlternative",
            "|",
            "imageStyle:alignLeft",
            "imageStyle:full",
            "imageStyle:alignRight"
          ],
          styles: ["full", "alignLeft", "alignRight"]
        },
        toolbar: [
          "heading",
          "|",
          "alignment",
          "bold",
          "italic",
          "link",
          "imageUpload",
          "undo",
          "redo"
        ]
      }
      }
    };
  },
  methods: {
   onReady( editor )  {
      editor.ui.getEditableElement().parentElement.insertBefore(
       editor.ui.view.toolbar.element,
       editor.ui.getEditableElement()
      );
     }
   }
};
</script>
<style scoped>
// 穿透 css,sass 使用 >>> less使用 /deep/
>>> .ck-content {
  min-height: 450px;
}
</style>

   还可以添加字体,大小等功能,其它功能参考官方示例https://ckeditor.com/docs/ckeditor5/latest/features/index.html

  注意:ckeditor5-build-decoupled-document是不需要额外任何npm下载插件,无法使用就是不支持此功能(例如图片大小),      大部分支功能持。如果想使用参考后面的扩展插件编辑器,ckeditor5-build-decoupled-document是无法使用的。

 例如再添加一个水平线功能 https://ckeditor.com/docs/ckeditor5/latest/features/horizontal-line.html

 horizontalLine 功能添加到toolbar即可

export default {
  data() {
    return {
      editor: DecoupledEditor ,
      content: "",
      editorConfig: {
        language: 'zh-cn',  // 中文
        placeholder: this.placeholder,
        image: {
          toolbar: [
            "imageTextAlternative",
            "|",
            "imageStyle:alignLeft",
            "imageStyle:full",
            "imageStyle:alignRight"
          ],
          styles: ["full", "alignLeft", "alignRight"]
        },
        toolbar: [
          "heading",
          "|",
          "alignment",
          "bold",
          "italic",
          "link",
          "imageUpload",
          "undo",
          "redo",
          "horizontalLine"
        ]
      }
      }
    };
  }
};

使用 


<template>
  <div>
  <Editor  v-model="richText"></Editor>
  </div>
</template>
<script>
//导入封装的组件
import Editor from "./Editor";
export default {
  components: {
    Editor
  },
  data() {
    return {
      richText: ""
    };
  }
}
</script>

文件上传


  1.新建js文件CustomUpload.js

   注:如果有header或token添加到axios中

import axios from "axios";
export default class CustomUploadAdapter {
  constructor(loader) {
    this.loader = loader;
  }
  upload() {
    return this.loader.file.then(file => {
      const formData = new FormData();
      formData.append("file", file);
      return axios.post("/upload", formData).then(response => {
        if (response.status === 200) {
          const data = response.data;
           // data值为:{ default: 'http://example.com/images/image.png' }
          return Promise.resolve(data);
        }
      });
    });
  }
}

 后端返回 json 格式  { default: 'http://example.com/images/image.png' }

  2.新建CustomUploadPlugin.js

//导入上面js文件
import CustomUploadAdapter from "./CustomUpload";
const CustomUploadPlugin = editor => {
  editor.plugins.get("FileRepository").createUploadAdapter = loader => {
    return new CustomUploadAdapter(loader);
  };
};
export default CustomUploadPlugin;

3.添加插件

  注:CKEditor任何编辑器都支持此文件上传

  base64使用 参考 Base64 upload adapter - CKEditor 5 Documentation

import CustomUploadPlugin from "./CustomUploadPlugin";
export default {
  data() {
    return {
      editor: Editor,
      content: "",
      editorConfig: {
        language: 'zh-cn',  // 中文
        placeholder: this.placeholder,
        extraPlugins: [ CustomUploadPlugin ]
        }
      }
    };
  }
};

扩展插件编辑器(可自定义功能)


参考:https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/vuejs.html#configuring-vueconfigjs

按照官方配置即可(写了一个小时,肝不动了)

请注意包不同,使用的是@ckeditor/ckeditor5-editor-classic ,不是@ckeditor/ckeditor5-build-classic

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: Vue CLI是一个基于Vue.js进行快速开发的完整系统,而axios是一个基于Promise的HTTP客户端,用于发送HTTP请求。将Vue CLI与axios整合可以方便地在Vue项目中进行网络请求。整合的步骤如下: 1. 创建Vue CLI项目。 2. 在项目中封装axios,可以在项目的src目录下创建一个util文件夹,然后在util文件夹中创建一个http.js文件,用来封装axios。 3. 在main.js中引入封装好的axios,并将其添加到Vue的原型对象中,这样在其他组件中就可以通过this.$http来使用axios进行网络请求。 4. 如果需要解决跨域问题,在开发环境中可以设置代理来解决,可以在配置文件中进行相应的配置。 5. 在项目中使用axios时,可以根据需要选择是否拦截请求或响应。如果需要拦截,可以使用封装好的axios实例来发起异步请求;如果不需要拦截,可以直接使用没有封装的axios来发起异步请求。 通过以上步骤,就可以实现Vue CLI与axios的整合,方便在Vue项目中进行网络请求。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [Vue总结(二)—— 整合axios](https://blog.csdn.net/qq_43776195/article/details/123307777)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue-cli整合axios的几种方法](https://blog.csdn.net/weixin_30845171/article/details/96808354)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值