vue中TinyMCE编辑器的使用

2023-10-29 01:50

本文主要是介绍vue中TinyMCE编辑器的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

vue中TinyMCE编辑器的使用

  • TinyMCE是一个轻量级的基于浏览器的所见即所得编辑器。富文本编辑器
  • UEditor:百度前端的开源项目,功能强大,基于 jQuery,但已经没有再维护,而且限定了后端代码,修改起来比较费劲
  • bootstrap-wysiwyg:微型,易用,小而美,只是 Bootstrap + jQuery…
  • 所以项目中用到的是TinyMCE,可前后端分离。
  • 可购买 tinymce 的服务,可以参考 tinymce-vue 的说明,通过 api-key 直接使用 tinymce。也可以下载 tinymce来使用。
  • 安装与使用:
    1. 安装:npm install tinymce -S
    2. 安装之后,在 node_modules 中找到 tinymce/skins 目录,然后将 skins 目录拷贝到 static 目录下。tinymce 默认是英文界面,所以还需要下载一个中文语言包zh_CN.js
    3. 下载中文语言包,附上地址下载链接
      在这里插入图片描述
    4. 扩展插件:
      添加plugins.js插件和toolbar.js工具栏插件
plugins.js
// Any plugins you want to use has to be imported
// Detail plugins list see https://www.tinymce.com/docs/plugins/
// Custom builds see https://www.tinymce.com/download/custom-builds/const plugins = ['advlist anchor autolink autosave code codesample colorpicker colorpicker contextmenu directionality emoticons fullscreen hr image imagetools importcss insertdatetime legacyoutput link lists media nonbreaking noneditable pagebreak paste preview print save searchreplace spellchecker tabfocus table template textcolor textpattern visualblocks visualchars wordcount']export default plugins
toolbar.js
// Here is a list of the toolbar
// Detail list see https://www.tinymce.com/docs/advanced/editor-control-identifiers/#toolbarcontrolsconst toolbar = ['bold italic underline strikethrough alignleft aligncenter alignright outdent indent undo redo removeformat code preview fullscreen', 'link charmap insertdatetime media table forecolor backcolor']export default toolbar

在这里插入图片描述
项目中使用:

组件:
<template><div class="tinymce-container editor-container"><textarea class="tinymce-textarea" :id="tinymceId"></textarea></div>
</template><script>import plugins from './plugins'import toolbar from './toolbar'export default {name: 'tinymce',components: {},props: {id: {type: String},value: {type: String,default: ''},toolbar: {type: Array,required: false,default() {return []}},menubar: {default: 'view format'},height: {type: Number,required: false,default: 360}},data() {return {hasChange: false,hasInit: false,tinymceId: this.id || 'vue-tinymce-' + +new Date()}},watch: {value(val) {if (!this.hasChange && this.hasInit) {this.$nextTick(() => window.tinymce.get(this.tinymceId).setContent(val))}}},mounted() {this.initTinymce()},activated() {this.initTinymce()},deactivated() {this.destroyTinymce()},methods: {initTinymce() {const _this = thiswindow.tinymce.init({selector: `#${this.tinymceId}`,//操作的对象// selector: `textarea`,//操作的对象schema: 'html5',//模式height: this.height,body_class: 'panel-body ',object_resizing: true,//是否允许编辑图片toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,//工具栏menubar: this.menubar,//菜单栏plugins: plugins,//插件end_container_on_empty_block: true,//是否允许回车powerpaste_word_import: 'clean',code_dialog_height: 450,code_dialog_width: 1000,paste_auto_cleanup_on_paste: false,//是否自动清理复制粘贴过来的文本样式advlist_bullet_styles: 'square',//默认的无序列表标记advlist_number_styles: 'default',//默认的有序列表标记// imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],//图片跨域请求用的default_link_target: '_blank',//链接的默认打开形式link_title: false,language: "zh_CN",//语言init_instance_callback: editor => {if (_this.value) {editor.setContent(_this.value)}_this.hasInit = trueeditor.on('NodeChange Change KeyUp SetContent', () => {// console.log(window.tinymce.get(this.tinymceId))this.hasChange = true// console.log(editor.getContent({format: 'html'}));this.$emit('input', editor.getContent({format: 'html'}))})},insertimage_callback: cb => {console.log(cb)}})},destroyTinymce() {if (window.tinymce.get(this.tinymceId)) {window.tinymce.get(this.tinymceId).destroy()}},setContent(value) {window.tinymce.get(this.tinymceId).setContent(value)},getContent() {window.tinymce.get(this.tinymceId).getContent()},imageSuccessCBK(arr) {const _this = this;arr = arr.split(",");arr.forEach(v => {window.tinymce.get(_this.tinymceId).insertContent(`<img class="wscnph" src="${v}" >`);})}},destroyed() {this.destroyTinymce()}}
</script>
在页面中使用:<el-card><p class="content_title">参数</p><tinymce :height="300" v-model="content1"></tinymce></el-card>
import Tinymce from '@/components/Tinymce'components: { Tinymce},

在这里插入图片描述

这篇关于vue中TinyMCE编辑器的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/297450

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完