vue +vue cli3 + tinymce 封装tinymce组件

news/2024/7/10 0:38:36 标签: vue, js, javascript, vue.js, html5

前言

给公安做了个项目,后台编辑文章 前台展示,一开始用的markdown编辑器,markdown不能段落缩进,格式也有些许问题,不是公安想要的效果,今天想换一个编辑器,选择了tinymac,好像达到了预期的效果,百度查找资料,做了一下午,从0开始封装,做个笔记。

vue_vue_cli3__tinymac_2">vue +vue cli3 + tinymac

vue_4">1.安装tinymce-vue

javascript">npm install @tinymce/tinymce-vue -S

1.安装安装tinymce

javascript">npm install tinymce -S

若无法完成安装,将下面的配置到package.json中执行npm install

javascript">"@tinymce/tinymce-vue": "^2.0.0",
"tinymce": "^5.0.3"

下载中文语言包

tinymce提供了很多的语言包,这里我们下载中文语言包

使用

2、在node_modules里面找到tinymce,将skins目录复制到public/tinymce里面
最终形成以下目录形式:    


在这里插入图片描述

初始化

引入基本文件

javascript">import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'

components中注册tinymce-vue才能使用

javascript"><editor id="tinymce" v-model="value" :init="init"></editor>
javascript">init: {
  language_url: '/tinymce/langs/zh_CN.js',// 语言包的路径
  language: 'zh_CN',//语言
  skin_url: '/tinymce/skins/ui/oxide',// skin路径
  height: 300,//编辑器高度
  branding: false,//是否禁用“Powered by TinyMCE”
  menubar: false,//顶部菜单栏显示
}

vue_59">完整代码tinymce-editor.vue

javascript"><template>
	<div class='tinymce'>
		<editor @change="editorChange" id='tinymce' v-model='tinymceHtml' :init='init'>

		</editor>
	</div>
</template>

<script>
	import tinymce from 'tinymce/tinymce.js'
	// import 'tinymce/themes/mobile/index.js'
	import 'tinymce/themes/silver/index.js'
	import Editor from '@tinymce/tinymce-vue'


	// // 更多插件参考:https://www.tiny.cloud/docs/plugins/
	import "tinymce/plugins/image"; // 插入上传图片插件
	// import "tinymce/plugins/link";
	// import "tinymce/plugins/code";
	import "tinymce/plugins/table"; // 插入表格插件
	// import "tinymce/plugins/lists"; // 列表插件
	// import "tinymce/plugins/contextmenu";
	// import "tinymce/plugins/wordcount"; // 字数统计插件
	// import "tinymce/plugins/colorpicker";
	// import "tinymce/plugins/textcolor";

	//vue-cli 3.x 需要在这引入中文包
	import "../../../../public/tinymce/langs/zh_CN.js";
	import 'tinymce/icons/default/icons.min.js' //图标

	import {
		postEditForm,
		uploadImg
	} from "network/edit.js"
	export default {
		name: 'tinymce',
		components: {
			Editor
		},
		props:{
			Html:{
				type:String,
				default:"请输入内容"
			}
		},
		data() {
			return {
				// imageArr:{
				// 	name: "",
					
				// },
				image: [],
				"tinymceHtml":"请输入内容",
				init: {

					language_url: '/tinymce/langs/zh_CN.js', // 语言包的路径
					language: 'zh_CN', //语言
					skin_url: '/tinymce/skins/ui/oxide', // skin路径
					content_css: `/tinymce/skins/content/default/content.css`, //
					height: 500, //编辑器高度
					// plugins: 'link lists image code table colorpicker textcolor wordcount contextmenu',
					plugins: 'image', //插件集合
					toolbar: 'bold italic underline strikethrough | fontsizeselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | link unlink image code | removeformat',
					branding: false, //是否禁用“Powered by TinyMCE”
					menubar: true, //顶部菜单栏显示
					images_upload_url: 'http://47.93.63.155:8081/',
					// 此处为图片上传处理函数,这个直接用了base64的图片形式上传图片,
					// 如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
					images_upload_handler: (blobInfo, success, failure) => {
						console.log(blobInfo)
						var file = blobInfo.blob(); //转化为易于理解的file对象
						// this.imageArr.name = file.name//将图片name赋值给imagearr并传递给父组件
						console.log(file)
						
						
						const image = 'data:image/jpeg;base64,' + blobInfo.base64()//获取图片64进制码
						
							uploadImg(image).then((res) => {
						
								console.log(res.data.data)
								const img = res.data.data
								
								success(img)
								// this.imageArr.image.push(img)
								this.image.push(img)
						
							})

						// let that = this

						// let reader = new FileReader(); //调用FileReader
						// reader.readAsDataURL(file); //将文件读取为 DataURL(base64)
						// reader.onload = function(evt) { //读取操作完成时触发。

						// 	evt = evt || window.evt

						// 	let image = evt.target.result
						// 	// that.firstImgInfo.image.push(image)//将文件存储
						// 	console.log(image)

						// 	uploadImg(image).then((res) => {

						// 		console.log(res.data.data)
						// 		// const img = 'data:image/jpeg;base64,' + blobInfo.base64()
						// 		const img = res.data.data
						// 		success(img)

						// 	}).catch(err => {
						// 		console.log(err);
						// 	})
						// }

					}
				},

			}
		},
		mounted() {
			tinymce.init({})
		},
		
		methods:{
			editorChange(){
				console.log(this.tinymceHtml)
			}
		},
		watch: {
		      tinymceHtml(newValue, oldValue) {
		        // console.log('新输入的值为:'+newValue); // 会输出新值
		        // console.log('原来的值为:'+oldValue); // 会输出旧值
				this.$emit("editorChange",newValue)//编辑器值改变 发射告诉父组件 让父组件更新content的值
		      },
			  Html(newValue, oldValue) {
			    this.tinymceHtml = newValue
			  },
			  
			  image(newValue, oldValue) {
				  this.$emit("addImg",newValue)
				  console.log(newValue)
			  },
		    },

	}
</script>

参考地址

https://liubing.me/vue-tinymce-5.html
http://tinymce.ax-z.cn/general/basic-setup.php


http://www.niftyadmin.cn/n/936654.html

相关文章

图片查看器

这几天一直在学习Java&#xff0c;在看《Java核心技术》这本书&#xff0c;书中有一个查看动态图片的小程序&#xff0c;写了一下&#xff0c;感觉还挺好使。正好自己电脑没有gif查看器&#xff0c;所以收藏了。 代码如下&#xff1a; 1 import java.awt.*;2 import java.io.*;…

虚拟化工具之VMware Workstation安装虚拟机篇

前言&#xff1a;开源商用云端平台OpenStack的掘起&#xff0c;俨然威胁虚拟化大将VMware的地位。而VMware和OpenStack之间的关系既是竞争&#xff0c;又是合作。学计算机的应该都知道VMware Workstation这个软件&#xff0c;很多人都把它当做一个虚拟机&#xff0c;一个供自己…

vue学习笔记--vue cli3 初始化项目

1. 安装vue-cli npm install -g vue/cli-service-global2. 创建项目 vue create 项目名称(项目名称不能有大写字母)选择手动配置&#xff08;如果喜欢使用eslint可以选择默认&#xff09; 创建完成 启动 npm run serve

【转】asp.net中的cookie使用介绍

来源&#xff1a;http://www.jb51.net/article/30398.htm 一.cookie导读&#xff0c;理解什么是cookie 1.什么是cookie&#xff1a;cookie是一种能够让网站服务器把少量数据&#xff08;4kb左右&#xff09;存储到客户端的硬盘或内存。并且读可以取出来的一种技术。 2.当你浏览…

核心游记之 page_address_init

lock_kernel()仅仅虚晃一枪就过去了&#xff0e; 紧接着来的是page_address_init include/linux/mm.h #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) #define HASHED_PAGE_VIRTUAL #endif 宏CONFIG_HIGHMEM 在系统中是没有定义的 宏WANT_PAGE_VIRTUAL…

安装引入axios

安装axios npm install axios$ bower install axioscdn引入: <script src"https://unpkg.com/axios/dist/axios.min.js"></script>引入 import axios from "axios" //导入axios例子 const axios require(axios);// Make a request for a …

数论结论 nefu 702

Given a prime p (p<108),you are to find min{x2y2},where x and y belongs to positive integer, so that x2y20 (mod p).打表可以看出结论: x4k3则没有平方和会等于P 转载于:https://www.cnblogs.com/acvc/p/3751702.html

SecureCRT恢复默认字体

1\要想永久的改变的就跟我来吧&#xff0c;选项--全局选项--常规--默认会话--编辑默认设置--外观--当前颜色方案--选择自己喜欢的方案就行啦&#xff0c;然后选择保存就OK啦方案二&#xff1a; 自从装了Win7之后&#xff0c;觉得securecrt里面可用的字体太少了。所以专门抽了一…