首页 > 其他分享 >vue富文本(5版本)组件

vue富文本(5版本)组件

时间:2022-09-21 11:02:18浏览次数:69  
标签:src vue return res html important editor 组件 文本

<template>
  <div>
    <div style="border: 1px solid #ccc; width: 500px">
      <!-- 工具栏 -->
      <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" />
      <!-- 编辑器 -->
      <Editor style="height: 400px; overflow-y: hidden" :defaultConfig="editorConfig" v-model="html" @onChange="onChange" @onCreated="onCreated" />
    </div>
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import Cookie from 'js-cookie';

export default {
  name: 'MyEditor',
  components: { Editor, Toolbar },
  props: {
    destDir: {
      type: String,
    },
  },
  data() {
    return {
      editor: null,
      html: '',
      toolbarConfig: {
        // toolbarKeys: [ /* 显示哪些菜单,如何排序、分组 */ ],
        // excludeKeys: [ /* 隐藏哪些菜单 */ ],
      },
      editorConfig: {
        placeholder: '请输入内容...',
        // autoFocus: false,

        // 所有的菜单配置,都要在 MENU_CONF 属性下
        MENU_CONF: {},
      },
    };
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否则会报错
      console.log('Created');
    },
    onChange(editor) {
      // console.log('onChange', editor.getHtml()); // onChange 时获取编辑器最新内容
    },

    loadHtml(html) {
      this.html = html;
    },
    insertTextHandler(html) {
      const editor = this.editor;
      if (editor == null) return;
      console.log(html);
      editor.insertText(html);
    },
    printEditorHtml() {
      const editor = this.editor;
      if (editor == null) return;
      console.log(editor.getHtml());
    },
    disableHandler() {
      const editor = this.editor;
      if (editor == null) return;
      editor.disable();
    },
    // 自定义校验图片
    customCheckImageFn(src, alt, url) {
      if (!src) {
        return;
      }
      if (src.indexOf('http') !== 0) {
        return '图片网址必须以 http/https 开头';
      }
      return true;

      // 返回值有三种选择:
      // 1. 返回 true ,说明检查通过,编辑器将正常插入图片
      // 2. 返回一个字符串,说明检查未通过,编辑器会阻止插入。会 alert 出错误信息(即返回的字符串)
      // 3. 返回 undefined(即没有任何返回),说明检查未通过,编辑器会阻止插入。但不会提示任何信息
    },

    // 转换图片链接
    customParseImageSrc(src) {
      // JS 语法
      if (src.indexOf('http') !== 0) {
        return `${this.pictureUrl}${src}`;
      }
      return src;
    },
  },
  created() {
    let that = this;
    this.editorConfig.MENU_CONF['uploadImage'] = {
      server: process.env.VUE_APP_API_MANAGE + '/uploadFileController/editUpload.do',
      fieldName: 'file',
      // 继续写其他配置...
      meta: {
        destDir: that.destDir,
      },
      headers: {
        adminToken: Cookie.get('adminToken'),
      },
      onSuccess(file, res) {
        // JS 语法
        console.log(`${file.name} 上传成功`, res);
      },
      // customInsert(res: any, insertFn: InsertFnType) {
      // TS 语法
      customInsert(res, insertFn) {
        // JS 语法
        // res 即服务端的返回结果
        // 从 res 中找到 url alt href ,然后插入图片
        insertFn(process.env.VUE_APP_SERVER + 'imageServer' + res.data.url, res.data.alt, res.data.href);
      },
    };

    // 插入图片
    this.editorConfig.MENU_CONF['insertImage'] = {
      onInsertedImage(imageNode) {
        // JS 语法
        if (imageNode == null) return;

        const { src, alt, url, href } = imageNode;
        console.log('inserted image', src, alt, url, href);
      },
      checkImage: that.customCheckImageFn, // 也支持 async 函数
      parseImageSrc: that.customParseImageSrc, // 也支持 async 函数
    };
  },
  mounted() {
    // // 模拟 ajax 请求,异步渲染编辑器
    // setTimeout(() => {
    //   this.html = '<p>Ajax 异步设置内容 HTML</p>';
    // }, 1500);
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时,及时销毁 editor ,重要!!!
  },
};
</script>
<style>
.w-e-full-screen-container {
  bottom: 0 !important;
  display: flex !important;
  flex-direction: column !important;
  height: 100% !important;
  left: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
  position: fixed;
  right: 0 !important;
  top: 0 !important;
  width: 100% !important;
  z-index: 99999;
}
</style>
<style src="@wangeditor/editor/dist/css/style.css"></style>

 

 

 

标签:src,vue,return,res,html,important,editor,组件,文本
From: https://www.cnblogs.com/book-student/p/16714841.html

相关文章

  • vue中设置class多种方式
    class可以绑定对象数组和函数等<!--第一种:数组直接传递一个数组,注意:这里的class需要使用v-moddel做数据绑定-->33<h1:class="['red','thin']">第一种:数组了......
  • vue +iview Select省市区联动
    因为需要保存的表里只有City_id一个字段,所以这边只保存"区"的值<Rowtype="flex"justify="start"class="code-row-bg"v-show="loginName=='admin'"><Cols......
  • vue下载图片
       asyncworks(obj){   awaitthis.axios({    method:'get',    url:`entryFormController/downloadWork.do`,    param......
  • React 面向组件编程 之 函数式组件
    简单组件和复杂组件的概念如果组件有state(状态)属性,就是复杂组件。如果没有state(状态)属性,就是简单组件。state、props、refs是组件实例的三大核心属性,在之后会逐一进......
  • vue导出文件
    /**导出*/asynctoExcel(){//letresult=awaitthis.axios({//method:'get',//url:`issdc-manage/gameController/export.do`,......
  • vue上传证书
       //队伍证书上传getFile(){varthat=this;////1创建formDataletformData=newFormData();////2添加数据,key可以......
  • 即用型UI组件Kendo UI,助力惠普缩短40%的应用开发时长
    惠普解决方案架构师BenjaminThatcher:"我们HP有自己的设备策略,内部使用的应用程序需要支持几乎所有主要的移动操作系统,包括WindowsPhone、Android、iOS系统及这些平台的......
  • 小程序自定义组件
    1.创建自定义组件一个组件也类似于一个页面,由json wxml wxss js 4个文件组成可以在一个新文件夹上右键新建component,可以直接生成这四个文件其次在json配置文件中......
  • 来看界面组件DevExpress WinForm是如何实现地图搜索的
    DevExpressWinForm拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office......
  • vue多图片上传组件
         <template><!--上传控件用法:<upload-widgetv-model="imgUrl"></upload-widget>--><divclass="clearfix"><a-upload......