ckeditor5-vue自定义图片上传函数

简介: ckeditor5-vue自定义图片上传函数

文档:https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/vuejs.html


共同的依赖

cnpm i -S @ckeditor/ckeditor5-vue  axios

自定义上传图片插件


MyUploadAdapter.js


import axios from "axios";
/**
 * 自定义上传图片插件
 */
class MyUploadAdapter {
  constructor(loader) {
    this.loader = loader;
  }
  async upload() {
    const data = new FormData();
    data.append("file", await this.loader.file);
    const res = await axios({
      url: process.env.VUE_APP_BASE_URL + `/upload`,
      method: "POST",
      data,
      withCredentials: true, // true 为不允许带 token, false 为允许
    });
    console.log(res.data);
    // 后台返回数据:
    // {"code":0,"msg":"success","data":{"url":"/upload/struts2.jpeg"}}
    // 方法返回数据格式: {default: "url"}
    return {
      default: process.env.VUE_APP_TARGET_URL + res.data.data.url,
    };
  }
}
export default MyUploadAdapter;

ClassicEditor

安装依赖


cnpm i -S @ckeditor/ckeditor5-build-classic

使用示例


<template>
  <div id="ck-editer">
    <ckeditor :editor="editor"
      @ready="onReady"
      v-model="editorData"
      :config="editorConfig">
    </ckeditor>
  </div>
</template>
<script>
import ClassicEditor from "@ckeditor/ckeditor5-build-classic";
import CKEditor from "@ckeditor/ckeditor5-vue";
import "@ckeditor/ckeditor5-build-classic/build/translations/zh-cn";
import MyUploadAdapter from "./MyUploadAdapter.js";
export default {
  components: {
    ckeditor: CKEditor.component
  },
  data() {
    return {
      editor: ClassicEditor,
      editorData: "<div>Content of the editor.</div>",
      editorConfig: {
        // The configuration of the editor.
        language: "zh-cn"
        // ckfinder: {
        //   // 后端处理上传逻辑返回json数据, 包括uploaded 上传的字节数 和url两个字段
        //   uploadUrl: process.env.VUE_APP_BASE_URL + `/upload`
        // }
      }
    };
  },
  methods: {
    onReady(editor) {
      // 自定义上传图片插件
      editor.plugins.get("FileRepository").createUploadAdapter = loader => {
        return new MyUploadAdapter(loader);
      };
    }
  }
};
</script>
<style lang="scss">
/* 全局修改生效 */
#ck-editer {
  .ck.ck-editor__editable_inline {
    p {
      line-height: 1.5;
    }
    min-height: 400px;
  }
}
</style>

image.png


参考文章


自定义图片上传

Rich text editor component for Vue.js

参数配置

Document editor

依赖


npm i - S @ckeditor/ckeditor5-build-decoupled-document
<template>
  <div id="ck-editer">
    <ckeditor :editor="editor"
      @ready="onReady"
      v-model="editorData"
      :config="editorConfig">
    </ckeditor>
  </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";
import MyUploadAdapter from "./MyUploadAdapter.js";
export default {
  name: "",
  props: {
    // v-model
    value: {
      type: String,
      default: "",
    },
  },
  components: {
    ckeditor: CKEditor.component,
  },
  data() {
    return {
      editor: DecoupledEditor,
      editorConfig: {
        // The configuration of the editor.
        language: "zh-cn",
      },
    };
  },
  computed: {
    editorData: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      },
    },
  },
  methods: {
    onReady(editor) {
      // Insert the toolbar before the editable area.
      editor.ui.getEditableElement().parentElement.insertBefore(
        editor.ui.view.toolbar.element,
        editor.ui.getEditableElement()
      );
      // 自定义上传图片插件
      editor.plugins.get("FileRepository").createUploadAdapter = (loader) => {
        return new MyUploadAdapter(loader);
      };
    },
  },
  created() {},
};
</script>
<style lang="scss">
/* 全局修改生效 */
#ck-editer {
  // 编辑器高度,边框
  .ck-content {
    min-height: 400px;
    border: 1px solid #ccc !important;
    background-color: white;
  }
}
</style>



参考


文档: https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/vuejs.html#using-the-document-editor-build

CKEdior5 文档编辑器构建步骤 vue富文本编辑器

相关文章
|
1月前
|
设计模式 JavaScript 前端开发
深入理解 Vue3 中的 setup 函数
深入理解 Vue3 中的 setup 函数
|
3月前
|
JavaScript 开发者
Vue组件生命周期钩子函数有哪些?它们分别在什么时候触发?
Vue组件生命周期钩子函数有哪些?它们分别在什么时候触发?
46 0
|
3月前
|
JavaScript API
uniapp使用Vue3挂载函数到全局
uniapp使用Vue3挂载函数到全局
106 0
|
3月前
|
JavaScript
后端类似于VUE的钩子函数页面加载
后端类似于VUE的钩子函数页面加载
16 0
|
2月前
|
JavaScript
vue3中reactive和ref函数及对比
vue3中reactive和ref函数及对比
|
1月前
|
JavaScript
vue组件中data为什么必须是一个函数
Vue.js 组件的 data 选项必须是一个函数,这是为了确保每个组件实例都有自己的独立数据副本,从而避免数据污染和确保组件的复用性和可预测性
|
3月前
|
JavaScript
vue组件中data为什么必须是一个函数?
vue组件中data为什么必须是一个函数?
20 1
|
3月前
|
JavaScript
VUE组件:Vue组件中的data为什么必须是一个函数而不是一个对象?
VUE组件:Vue组件中的data为什么必须是一个函数而不是一个对象?
30 2
|
2月前
|
JavaScript
在Vue中,什么是Watcher?它和生命周期钩子函数有什么关系?
在Vue中,什么是Watcher?它和生命周期钩子函数有什么关系?
11 1
|
2月前
|
JavaScript
在Vue中,如何使用生命周期钩子函数?
在Vue中,如何使用生命周期钩子函数?
9 1