首页 > 其他分享 >vue中使用富文本编辑器

vue中使用富文本编辑器

时间:2024-09-12 20:51:47浏览次数:3  
标签:文本编辑 vue default 使用 tinymce 插件 param plugins import

        使用的是tinymce第三方插件

       npm install tinymce
       npm install @tinymce/tinymce-vue

封装组件

 components下新增editor目录

新增editor.vue文件

/**
 * 富文本编辑器组件
 * (c) 2024-02
 * @param {String} value 绑定的数据字段
 * @param {Boolean} disabled 是否禁用,默认值 false
 * @param {Boolean} menubar 是否显示菜单栏,默认值 false
 * @param {Boolean} statusbar 是否显示状态栏,默认值 true
 * @param {Boolean} resize 是否可以拖动高度,默认值 true
 * @param {Number} height 编辑器高度,默认值 400
 * @param {String | Array} plugins 编辑器插件
 * @param {String | Array} toolbar 编辑器工具栏
 * @example: <editor v-model="editorHtml" :height="300"></editor>
 */
<template>
  <div class="editor">
    <tinymce-editor v-model="myValue" :init="init" :disabled="disabled" @onBlur="onBlur" @onChange="onChange" @onClick="onClick" @onFocus="onFocus" @onInit="onInit"></tinymce-editor>
  </div>
</template>

<script>
  import tinymce from 'tinymce/tinymce';
  import tinymceEditor from '@tinymce/tinymce-vue';
  import 'tinymce/themes/silver/theme';
  import 'tinymce/models/dom' // 这里是个坑 一定要引入
  import 'tinymce/icons/default'; //引入编辑器图标icon,不引入则不显示对应图标

  // 编辑器插件plugins,更多插件参考:https://www.tiny.cloud/docs/plugins/
  import 'tinymce/plugins/link'; // 插入链接插件
  import 'tinymce/plugins/image'; // 插入图片插件
  import 'tinymce/plugins/media'; // 插入视频插件
  import 'tinymce/plugins/table'; // 插入表格插件
  import 'tinymce/plugins/lists'; // 列表插件
  import 'tinymce/plugins/code'; // 代码插件
  import 'tinymce/plugins/wordcount'; // 字数统计插件
  export default {
    name: 'editor',
    components: {
      tinymceEditor
    },
    props: {
      // 绑定的数据字段
      value: {
        type: String,
        default: ''
      },
      // 是否禁用
      disabled: {
        type: Boolean,
        default: false
      },
      // 是否显示菜单栏
      menubar: {
        type: Boolean,
        default: true
      },
      // 是否显示状态栏
      statusbar: {
        type: Boolean,
        default: true
      },
      // 是否可以拖动高度
      resize: {
        type: Boolean,
        default: true
      },
      // 编辑器高度
      height: {
        type: Number,
        default: 400
      },
      // 编辑器插件
      plugins: {
        type: [String, Array],
        default: 'link image media table lists code wordcount'
      },
      // 编辑器工具栏
      toolbar: {
        type: [String, Array],
        default: 'undo redo | fontsizeselect | bold italic underline forecolor backcolor | alignleft aligncenter alignright alignjustify | table link image media | code removeformat'
        // default: 'undo redo | formatselect | fontsizeselect | bold italic underline strikethrough superscript subscript forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link lists image media table | code wordcount removeformat'
      }
    },
    data() {
      return {
        myValue: this.value,
        init: {
          language_url: '/tinymce/langs/zh_CN.js',
          language: 'zh_CN',
          skin_url: '/tinymce/skins/ui/oxide',
					content_css: '/tinymce/skins/ui/oxide/content.min.css',
          height: this.height || 400,
          plugins: this.plugins,
          toolbar: this.toolbar || false,
          branding: false,
          menubar: this.menubar,
          statusbar: this.statusbar,
          resize: this.resize
        }
      };
    },
    mounted() {
      tinymce.init({});
    },
    methods: {
      onBlur(e) {
        this.$emit('onBlur', e, tinymce);
      },
      onChange(e) {
        this.$emit('onChange', e, tinymce);
      },
      onClick(e) {
        this.$emit('onClick', e, tinymce);
      },
      onFocus(e) {
        this.$emit('onFocus', e, tinymce);
      },
      onInit(e) {
        this.$emit('onInit', e, tinymce);
      }
    },
    watch: {
      value(val) {
        this.myValue = val;
      },
      myValue(val) {
        this.$emit('input', val);
      }
    }
  };
</script>

 同目录下新增导出文件index.js

export { default } from './editor.vue';

需要注意!!!

有几个样式文件和语言文件需要单独引入到public目录下

最后就可以直接导入使用了

<template>
	<div class="app-container">
        <editor :value="editorHtml" @input="editorInput" :height="300"></editor>
		<el-button type="primary" @click="save">保存</el-button>
	</div>
</template>
<script lang="ts" setup>
import {reactive, ref} from "vue";
import Editor from "@/components/editor/index";
const editorHtml = ref('')

const editorInput = (e: any)=>{
	editorHtml.value = e
}

const save = () => {
	console.log('editorHtml', editorHtml.value);
}
</script>

标签:文本编辑,vue,default,使用,tinymce,插件,param,plugins,import
From: https://blog.csdn.net/m0_56344834/article/details/142090239

相关文章

  • 异步实例化预制体Object.InstantiateAsync配合Async/Await使用
    Unity2022.3.20之后,可以使用异步克隆,正如前面一篇文章《Unity2022.3.20f1新功能,异步实例化预制体Object.InstantiateAsync》说明的那样,常规的使用携程方式异步克隆,但如今await/async写法如此简单方便,肯定就不想放IEnumerator里头去实现了,怎么办呢?实现一个,代码如下:publicGame......
  • AES加密解密全流程演示__api基础使用
    publicstaticvoidmain(String[]args)throwsException{//共同约定秘钥和加密算法Stringcontent="你好,世界";//AES密钥长度通常为128、192或256位Stringkey="1234567812345678";//16字节*8Stringalgorithm="AE......
  • dotnet 测试在 UOS Linux 上使用 Process Start 打开文件的行为
    本文记录我在UOSLinux系统上使用Process.Start打开文件的行为使用UseShellExecute打开文本文件我放入了名为Test.txt的文件,然后使用下面代码尝试打开文件。实际测试可以正常打开usingSystem.Diagnostics;varfilePath="../Test.txt";Console.WriteLine($"文件......
  • vue3 判断浏览器打开窗口页签变化
       场景:当需要同时打开两个页签,需要在切换页签的时候,重新获取数据    根据document.visibilityState结果判断。如果为visible则证明回到当前页签, 如果为hidden则证明当前页面未显示(前往了其他页签)import{onMounted,onUnmounted}from'vue';consthan......
  • 实际工作中使用Git
    在实际的工作开发中,面向的是团队协作的开发环境,跟自己独立开发还是有很大的不同的,接下来主要介绍在实际的工作中怎么使用git来进行协作开发1.git迭代开发流程介绍各个企业的迭代开发流程可能不一致,但大体上是类似的。首先是主干分支main,main分支应该是这里面最全的代码分支,......
  • [基于 Vue CLI 5 + Vue 3 + Ant Design Vue 4 搭建项目] 05 创建一个 web 项目
    1.通过vuecreate项目名命令创建项目这里创建一个项目名叫做web的项目vuecreateweb选择Manuallyselectfeatures然后摁回车选择了Router和Vuex然后摁回车选择3.x然后摁回车是否使用历史模式,这里y然后摁回车历史模式和非历史模式(Hash模式)主要......
  • 【开题报告】基于django+vue校园二手交易平台(论文+程序)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着数字化时代的到来,校园生活的便捷性与资源循环利用意识日益增强。在各大高校中,学生群体对二手物品的需求与供给均呈现出显著增长态势。......
  • [基于 Vue CLI 5 + Vue 3 + Ant Design Vue 4 搭建项目] 03 使用 npm 安装依赖出现 ce
    文章目录问题描述解决问题1.修改镜像源2.清理缓存3.临时禁用SSL证书验证4.再次安装依赖问题描述在使用npminstall进行按安装依赖的时候出现了下面错误npmerrorcodeCERT_HAS_EXPIREDnpmerrorerrnoCERT_HAS_EXPIREDnpmerrorrequesttohttps://registr......
  • 基于SpringBoot+Vue+uniapp的农产品质量安全检测网站(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • 基于SpringBoot+Vue+uniapp的物资物流系统(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......