vue2 使用 tinymce富文本编辑器

news2024/9/27 19:22:49

注意:
在vue2中使用tinymce有版本限制的,最新版都是支持v3的,官方也说明了;
vue2中不能使用@tinymce/tinymce-vue 为4以上的版本;

使用步骤:

1、vue项目中安装 tinymce;
npm install tinymce@5.1.0 -S
npm install @tinymce/tinymce-vue@3.0.1 -S

2、将富文本编译器封装成组件;
在src/components目录下新建一个tinymce文件夹(每个文件的代码在文章末尾提供)
在这里插入图片描述

3、将组件引入到页面中进行使用;
哪个页面使用富文本编译器 就在哪个页面引入组件;

<tinymce 
  v-model="addContent"
  @input="(info) => tinymceInput(info)"
   id="tinymceId"
></tinymce>

import tinymce from '@/components/tinymce/index.vue' // 注意引入路径
components: { tinymce },

data () {
  return {
    addContent: '',
  }
},
methods: {
	tinymceInput (info) {
       console.log(info,'info------------');
     },
},

注意:可能会遇到的问题
import “tinymce/icons/default” 路径找不到需要升级tinymce版本
解决方法:
终端执行:npm i tinymce -S

代码:

index.vue

<template>
  <div class="tinymce-editor">
    <editor
      :id="id"
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick"
    ></editor>
  </div>
</template>

<script>
// 引入组件
import tinymce from 'tinymce'
import editor from '@tinymce/tinymce-vue'

// 引入富文本编辑器主题的js和css
import 'tinymce/skins/content/default/content.css'
import 'tinymce/themes/silver'
import 'tinymce/icons/default/icons' // 解决了icons.js 报错问题,不用定制icon可以忽略

// 编辑器扩展插件plugins
import 'tinymce/plugins/paste' // 粘贴插件
import 'tinymce/plugins/image' // 上传图片插件
import 'tinymce/plugins/media'// 插入视频插件
import 'tinymce/plugins/link' // 超链接
import 'tinymce/plugins/code' // 源码
import 'tinymce/plugins/table' // 插入表格插件
import 'tinymce/plugins/lists' // 列表插件
import 'tinymce/plugins/wordcount' // 字数统计插件
import 'tinymce/plugins/preview' // 预览

// import 'tinymce/plugins/table'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/colorpicker'
import 'tinymce/plugins/textcolor'
import "tinymce/plugins/code";
import "tinymce/plugins/link";
import "tinymce/plugins/advlist";
import "tinymce/plugins/anchor";
import "tinymce/plugins/codesample";
import "tinymce/plugins/hr";
import "tinymce/plugins/fullscreen";
import "tinymce/plugins/textpattern";
import "tinymce/plugins/searchreplace";
import "tinymce/plugins/autolink";
import "tinymce/plugins/directionality";
import "tinymce/plugins/visualblocks";
import "tinymce/plugins/visualchars";
import "tinymce/plugins/template";
import "tinymce/plugins/charmap";
import "tinymce/plugins/nonbreaking";
import "tinymce/plugins/insertdatetime";
import "tinymce/plugins/autoresize"; 
import "tinymce/plugins/pagebreak";
import "tinymce/plugins/print";
import "tinymce/plugins/save";
import "tinymce/plugins/tabfocus";

// import 'tinymce/plugins/contextmenu'
// import "tinymce/plugins/imagetools";
// import "tinymce/plugins/autosave";
// import "tinymce/plugins/emoticons";
// import "tinymce/plugins/spellchecker";


import plugins from './plugins'
import toolbar from './toolbar'

export default {
  name: 'tinymce',
  components: { editor },
  props: {
    value: {
      // 默认的富文本内容
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: ''
    },
    disabled: {
      // 禁用
      type: Boolean,
      default: false
    },
  },
  data () {
    return {
      init: {
        language_url: '/tinymce/langs/zh_CN.js', // 语言包路径
        language: 'zh_CN', // 语言
        skin_url: '/tinymce/skins/ui/oxide', // 主题路径
        content_css: '/tinymce/skins/ui/oxide/content.css', // 为编辑区指定css文件,该参数的值是你的css文件路径,可使用绝对路径或相对路径。
        min_height: 80,
        // height: 500,
        // width: this.calcWidth(),
        plugins: plugins, // 指定需加载的插件
        toolbar: toolbar, // 自定义工具栏
        toolbar_mode: 'sliding', // 工具栏模式
        // paste_data_images: true, // 允许粘贴图片
        branding: false, // 不显示富文本支持方
        statusbar: false, // 隐藏状态栏
        menubar: false, // 禁用菜单栏
        theme: 'silver', // 默认主题
        placeholder: '请输入',
        fontsize_formats: "12px 14px 16px 18px 20px 22px 24px 36px 48px 56px 72px",
        font_formats: "宋体='宋体';仿宋='仿宋';微软雅黑='微软雅黑';楷体='楷体';隶书='隶书';幼圆='幼圆';Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Helvetica=helvetica;",
        content_style: `
        body { font-family:宋体,Arial,sans-serif; font-size:14px;line-height: 1.5; }` + 
        `table { width: 100% !important;}` + `td { border: 1px solid #ccc !important; }`,
        zIndex: 1101,
        style_formats_merge: false, //是否将style_formats设置中的样式附加到默认样式格式还是完全替换它们。true为附加
        style_formats_autohide: true, // 隐藏当前不可用的样式列表
        object_resizing: false, //禁用表格内联样式拖拽拉伸
        table_resize_bars: false,//禁用表格单元格拖拽拉伸
        forced_root_block: '', // 删除在tinymce中自动添加的p标签
        force_br_newlines : true,
        force_p_newlines : false,

        // style_formats: [
        //   // {
        //   //   title: '首行缩进',
        //   //   block: 'p',
        //   //   styles: { 'text-indent': '2em' }
        //   // },
        //   {
        //     title: 'Headings',
        //     items: [
        //       { title: 'Heading 1', format: 'h1' },
        //       { title: 'Heading 2', format: 'h2' },
        //       { title: 'Heading 3', format: 'h3' },
        //       { title: 'Heading 4', format: 'h4' },
        //       { title: 'Heading 5', format: 'h5' },
        //       { title: 'Heading 6', format: 'h6' }
        //     ]
        //   },
        //   // {
        //   //   title: 'Inline',
        //   //   items: [
        //   //     { title: 'Bold', format: 'bold' },
        //   //     { title: 'Italic', format: 'italic' },
        //   //     { title: 'Underline', format: 'underline' },
        //   //     { title: 'Strikethrough', format: 'strikethrough' },
        //   //     { title: 'Superscript', format: 'superscript' },
        //   //     { title: 'Subscript', format: 'subscript' },
        //   //     { title: 'Code', format: 'code' }
        //   //   ]
        //   // },
        //   {
        //     title: 'Blocks',
        //     items: [
        //       { title: 'Paragraph', format: 'p' },
        //       { title: 'Div', format: 'div' },
        //       // { title: 'Blockquote', format: 'blockquote' },
        //       // { title: 'Pre', format: 'pre' }
        //     ]
        //   },
        //   {
        //     title: 'Align',
        //     items: [
        //       { title: 'Left', format: 'alignleft' },
        //       { title: 'Center', format: 'aligncenter' },
        //       { title: 'Right', format: 'alignright' },
        //       { title: 'Justify', format: 'alignjustify' }
        //     ]
        //   },
        //   // {
        //   //   title: "行高",
        //   //   items: [
        //   //     { title: "1", block: "p", styles: { "line-height": "1.0" } },

        //   //     { title: "1.5", block: "p", styles: { "line-height": "1.5" } },

        //   //     { title: "1.75", block: "p", styles: { "line-height": "1.75" } },

        //   //     { title: "2", block: "p", styles: { "line-height": "2" } },

        //   //     { title: "3", block: "p", styles: { "line-height": "3" } },

        //   //     { title: "4", block: "p", styles: { "line-height": "4" } },

        //   //     { title: "5", block: "p", styles: { "line-height": "5" } },
        //   //   ],
        //   // },
        // ],

        // init_instance_callback: (editor) => {
        //   console.log(editor,'editor');
        //     // 更改元素为Div
        //     editor.execCommand('mceInsertContent', false, '<p></p>')
        // },

        // images_upload_handler: (blobInfo, success, failure) => {
          // console.log(blobInfo, success, failure,'blobInfo');
          // console.log(success, 'success');
          // console.log(failure,'failure');
          // const img = "data:image/jpeg;base64," + blobInfo.base64();
          // success(img);
        // },

      },
      myValue: this.value,

    }
  },
  beforeDestroy () {
    tinymce.remove()
  },
  created () {},
  mounted () {
    tinymce.init({})
  },
  computed: {},
  watch: {
    value(newValue) {
      this.myValue = newValue
    },
    myValue(newValue) {
      this.$emit('input', newValue)
    },
    
  },
  methods: {
    onClick(e) {
      this.$emit('onClick', e, tinymce)
    },
    
    // clear() {
    //   this.myValue = ''
    // },

    // calcWidth() {
    //   return document.body.clientWidth / 2 + 'px'
    // },
  },
}
</script>

<style lang="scss" scoped>
  ::v-deep .tox .tox-tbtn--select {
    width: 100px;
  }
  ::v-deep .tox .tox-sidebar-wrap {
    padding: 5px;
  }
 </style>

plugins.js

// autoresize 编辑器高度自适应
// autosave 添加此插件跳跳转url时会弹出一个提示框(提示信息:重新加载此网站? 系统可能不会保存您所做的更改。)

const plugins = ['advlist anchor autolink  code codesample colorpicker directionality fullscreen hr image insertdatetime link lists media nonbreaking pagebreak paste preview print save searchreplace tabfocus table template textcolor textpattern visualblocks visualchars wordcount autoresize']

export default plugins

toolbar.js

// Here is a list of the toolbar
// Detail list see https://www.tinymce.com/docs/advanced/editor-control-identifiers/#toolbarcontrols

// const toolbar = ['searchreplace bold italic underline strikethrough alignleft aligncenter alignright outdent indent  blockquote undo redo removeformat subscript superscript code codesample', 'hr bullist numlist link image charmap preview anchor pagebreak insertdatetime media table emoticons forecolor backcolor fullscreen']

// styleselect 行高
// table 表格
const toolbar = ['bold italic underline | fontselect | fontsizeselect | forecolor backcolor | removeformat']

export default toolbar

展示效果图:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1975386.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

用TensorFlow训练自己的第一个模型

现在学AI的一个优势就是&#xff1a;前人栽树后人乘凉&#xff0c;很多资料都已完善&#xff0c;而且有很多很棒的开源作品可以学习&#xff0c;感谢大佬们 项目 项目源码地址 视频教程地址 我在大佬的基础上基于此模型还加上了根据特征值缓存进行快速识别的方法&#xff0c;…

【教程】Python语言的地球科学常见数据——全球大气再分析数据

a、多年数据的读取 b、趋势分析 c、多时间尺度统计。 ECMWF 中心推出的 ERA5 全球大气再分析数据提供了大量大气、陆地和海洋气候变量的逐小时数据。这些数据在 30km 网格上覆盖了全球&#xff0c;在时间跨度上从 1979 至今。该数据能够提供全球范围的格点气象数据。 将针对该…

react-native从入门到实战系列教程一Switch组件和StatusBar的运用

跨平台通用的组件。这是一个受控组件&#xff0c;你必须使用onValueChange回调来更新value属性以响应用户的操作。如果不更新value属性&#xff0c;组件只会按一开始给定的value值来渲染且保持不变&#xff0c;看上去就像完全点不动。 实现效果 代码实现 import {View, Text,…

力扣hot100-二叉树

文章目录 概要二叉树的基本概念常见的二叉树类型常用的二叉树遍历二叉树的常用技巧 题目&#xff1a;二叉树的中序遍历方法1--递归遍历方法2--使用栈 概要 二叉树&#xff08;Binary Tree&#xff09;是一种树形数据结构&#xff0c;其中每个节点最多有两个子节点&#xff0c;…

DC-2靶机试试看 继续打靶!!冲冲冲!!

要更改一下自己的host&#xff0c;这样才可以正确的访问我们的靶机的页面。 下面看看我的思路吧&#xff1a; 前面还是老样子&#xff0c;先发现靶机的ip地址以及收集他开放了哪些端口等等的信息 查看相对应的cms 我用了一些msf的模块没有打下来这个站点 收集相关的信息&…

【计算机人接私活】手把手教你上手挖到第一个漏洞,从底薪3k到月入过万,只有一步之遥!

计算机人想接靠谱的私活&#xff1f;看这篇&#xff01; 暑假想做兼职赚生活费&#xff1f;看这篇&#xff01; 挖漏洞找不到门路&#xff1f;看这篇&#xff01; 挖漏洞必备工具 Up入行网安多年&#xff0c;一直在探索副业项目。 从最初的月薪5k&#xff0c;到现在一个漏…

[米联客-安路飞龙DR1-FPSOC] UDP通信篇连载-01 以太网协议介绍

软件版本&#xff1a;Anlogic -TD5.9.1-DR1_ES1.1 操作系统&#xff1a;WIN10 64bit 硬件平台&#xff1a;适用安路(Anlogic)FPGA 实验平台&#xff1a;米联客-MLK-L1-CZ06-DR1M90G开发板 板卡获取平台&#xff1a;https://milianke.tmall.com/ 登录“米联客”FPGA社区 ht…

SpringBoot 3的两种SPI加载方式

从spring boot 2.7.0发布后&#xff0c; 自动配置类的加载方式就发生了改变&#xff0c;原来从META-INF/spring.factories文件中加载&#xff0c;变为了从META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中加载&#xff0c;对应的加载实…

Ubuntu下python3.12安装, 分布式 LLM 推理 exo 安装调试过程, 运行自己的 AI 集群

创作不易 只因热爱!! 热衷分享&#xff0c;一起成长! “你的鼓励就是我努力付出的动力” —调试有点废,文章有点长,希望大家用心看完,肯定能学废,感谢. 1. Ubuntu下python3.12安装 1.1 导入 Python 的稳定版 PPA,不用编译 sudo add-apt-repository ppa:deadsnakes/ppa sudo…

82.WEB渗透测试-信息收集-框架组件识别利用(6)

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 内容参考于&#xff1a; 易锦网校会员专享课 上一个内容&#xff1a;81.WEB渗透测试-信息收集-框架组件识别利用&#xff08;5&#xff09; log4j/log4j2&…

《Excelize权威指南》新书发布

在数据洪流涌动的数字化时代&#xff0c;数据处理与分析已跃升为解锁无限洞察力的金钥匙&#xff0c;赋能商业智慧、重塑医疗健康版图、驱动教育科研创新。然而&#xff0c;当数据量级爆炸式增长&#xff0c;传统工具如 Excel 虽被誉为数据处理领域的常青树&#xff0c;其手动操…

modelsim仿真quartus IP

开发环境&#xff1a;quartus prime pro 20&#xff1b;modelsim se-64 10.6d 1. 生成Altera的IP库 使用quartus生成IP库&#xff0c;需要使用Simulation Library Compiler&#xff08;Tools->Launch Simulation Library Compiler&#xff09; 如下图操作&#xff0c;选择…

车载音频开发(一):从看懂wav开始

背景介绍&#xff1a;随着电车的发展势头迅猛&#xff0c;国内车载音频也成为电车火热宣称的势头&#xff0c;要想深入了解车载音频&#xff0c;那首先还是得从最为普通的音频文件WAV开始。 我们都知道&#xff0c;计算机只能存储数字&#xff0c;声音确实靠不同频率的波组成&a…

RabbitMQ的快速入门

目录 前言 1. 安装RabbitMQ 2.基本结构 3. RabbitMQ消息模型 ​​​​​​4. 入门案例 4.1 publisher实现 4.2 consumer实现 4.3 总结 前言 RabbitMQ是一套开源&#xff08;MPL&#xff09;的消息队列服务软件&#xff0c;是由 LShift 提供的一个 Advanced Message Q…

达梦数据库的系统视图v$cachesql

达梦数据库的系统视图v$cachesql 达梦数据库的系统视图V$CACHESQL的主要作用是提供缓冲区中SQL语句的信息&#xff0c;在 ini 参数 USE_PLN_POOL !0 时才统计。通过查询这个视图&#xff0c;用户可以了解SQL语句在缓冲区中的执行情况&#xff0c;包括SQL节点的类型、进入次数、…

滚珠丝杆与丝杆支撑座:稳定性与精度的双重保障

丝杆支撑座是连接滚珠丝杆与电机的轴承&#xff0c;采用优质的轴承能确保支撑座与滚珠丝杆之间的刚性平衡。那么&#xff0c;滚珠丝杆搭连接杆支撑座有哪些优缺点呢&#xff1f; 正常情况下&#xff0c;丝杆支撑座能够提供稳定的支撑力&#xff0c;确保滚珠丝杆在复杂工况下保持…

使用PasteSpider实现类似Jenkins的功能,让你的2G服务器也可以飞起

获取你接触过Jenkins&#xff0c;在我理解就是拉取源码&#xff0c;然后构建成镜像&#xff0c;最后启动容器&#xff01; 这个步骤你在PasteSpider上也可以实现&#xff0c;以下案例使用svn作为源码管理 如果你使用git作为源码管理&#xff0c;道理差不多 以我的代码为例 …

假期BUUCTF小练习3

文章目录 [极客大挑战 2019]BuyFlag[BJDCTF2020]Easy MD5[HCTF 2018]admin第一种方法 直接登录第二种方法 flack session伪造第三种方法Unicode欺骗 [MRCTF2020]你传你&#x1f40e;呢[护网杯 2018]easy_tornadoSSTI注入 [ZJCTF 2019]NiZhuanSiWei [极客大挑战 2019]BuyFlag 一…

好用的AI智能写作助手,创作者必备

随着科技的不断发展&#xff0c;人工智能&#xff08;AI&#xff09;在各个领域都起到了革命性的作用。在写作领域&#xff0c;AI智能写作助手已经成为了创作者们的必备工具。这些智能助手通过强大的自然语言处理能力和深度学习算法&#xff0c;能够帮助创作者们提高写作效率、…