vue中使用tinymce富文本,以及如何使用图片上传

最近在写tinymce富文本,这里特别记录一下,以便于以后方便使用,因为也涉及到图片的上传,刚刚使用的时候图片上传这里只能输入地址保存,就想能手动上传,然后在网上看了好多方法,需要这个插件那个插件的感觉很复杂,后来阴差阳错的把手动上传配置出来了,操作很简单。tinymce中文文档

1.安装

npm install tinymce
npm install @tinymce/tinymce-vue

2.在官网下载语言包 https://www.tiny.cloud/get-tiny/language-packages/
在目录public下边新建一个tinymce文件夹,然后在node_modules里找到tinymce的skin包,复制到public/tinymce里,新建langs文件,将语言包放到下边。
在这里插入图片描述
3.使用,我是将富文本做成了一个组件,具体代码如下

<!-- 富文本编辑器 -->
<template>
	<div class="tinymce-editor">
		<editor v-model="myValue" :init="init" :disabled="disabled" @onClick="onClick" style="height:100%;">
		</editor>
	</div>
</template>

<script>
	import tinymce from 'tinymce/tinymce'
	import Editor from "@tinymce/tinymce-vue";
	import 'tinymce/themes/silver/theme'
	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' //文本颜色插件
	import 'tinymce/plugins/fullscreen' //全屏
	import 'tinymce/plugins/paste'
	export default {
		components: {
			Editor
		},
		props: {
			value: {
				type: String,
				required: false
			},
			disabled: {
				type: Boolean,
				default: false
			},
			plugins: {
				type: [String, Array],
				default: 'lists image imagetools link media table textcolor wordcount contextmenu fullscreen paste'
			},
			toolbar: {
				type: [String, Array],
				default: 'imagetools bold italic underline strikethrough blockquote | forecolor backcolor | formatselect | fontsizeselect | fontselect | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists link unlink image media table | removeformat | undo redo  | fullscreen',
				branding: false
			}
		},
		data() {
			return {
				init: {
					language_url: "/tinymce/langs/zh_CN.js",
					language: "zh_CN",
					skin_url: "/tinymce/skins/lightgray",
					plugins: this.plugins,
					toolbar: this.toolbar,
					branding: false, //隐藏品牌标识
					branding: false, //水印
					height: 500,
					menubar: false, // 最上方menu菜单的显示隐藏
					toolbar_drawer: true,
					statusbar: false, // 隐藏编辑器底部的状态栏
					elementpath: false, //禁用下角的当前标签路径
					// CONFIG: Paste
					paste_retain_style_properties: 'all',
					paste_word_valid_elements: '*[*]', //word需要它
					paste_data_images: true, // 粘贴的同时能把内容里的图片自动上传,非常强力的功能
					paste_convert_word_fake_lists: false, // 插入word文档需要该属性
					paste_webkit_styles: 'all',
					paste_merge_formats: true,
					nonbreaking_force_tab: false,
					paste_auto_cleanup_on_paste: false,
					//CONFIG: Font
					fontsize_formats: '10px 11px 12px 14px 16px 18px 20px 24px',
					images_upload_handler: (blobInfo, success) => {
						this.upload(blobInfo,(e)=>{
							success(e)
						})
					}
				},
				myValue: this.value,
			}
		},
		mounted() {
			tinymce.init({});
		},
		methods: {
			onClick(e) {
				this.$emit('onClick', e, tinymce)
			},
			//可以添加一些自己的自定义事件,如清空内容
			clear() {
				this.myValue = ''
			},
			upload(blobInfo,fn) {
				let formData = new FormData()
				formData.append('file', blobInfo.blob())
				//这里为自己的上传接口调用方法
				this.$api.product.fileUploading(formData).then(res => {
					if (res.code === 200) {
						fn && fn(res.result)
					} else {
						this.$message.error('图片上传失败')
						fn && fn('')
					}
				})
			}
		},
		watch: {
			value(newValue) {
				this.myValue = (newValue == null ? '' : newValue)
			},
			myValue(newValue) {
				if (this.triggerChange) {
					this.$emit('change', newValue)
				} else {
					this.$emit('input', newValue)
				}
			},
		}
	}
</script>

<style scoped>
	.tinymce-editor {
		width: 100%;
		height: 100%;
	}
</style>

4.另外 只要配置上images_upload_handler 就会出来手动上传,
如果需要粘贴图像进去,也是需要配置images_upload_handler的

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在tinymce/tinymce-vue富文本编辑器实现图片上传功能,你可以按照以下步骤操作: 1. 首先,确保你已经安装了tinymcetinymce-vue的依赖包。你可以去[TinyMCE官方网站](https://www.tiny.cloud/get-tiny/self-hosted/)下载最新的TinyMCE压缩包,并解压到你的项目目录。然后,安装tinymce-vue依赖包,可以使用npm或yarn进行安装。 2. 接下来,将TinyMCE的skins文件夹复制到你的项目的public文件夹下。这个文件夹包含了富文本编辑器的样式文件。 3. 然后,创建一个Vue组件来封装el-upload控件,并将其整合到tinymce-vue。你可以将这个组件放在你的项目的src/components文件夹下。具体的组件代码可以参考上述提供的链接。 通过以上步骤,你就可以在tinymce/tinymce-vue富文本编辑器实现图片上传功能了。请注意,这只是一种实现方式,具体的实现方式可能因项目需求的不同而有所差异。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vue3 tinymce+tinymce-vue 富文本编辑器使用](https://blog.csdn.net/oooosadas/article/details/131176384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [vue-tinymce 富文本编辑器自定义图片上传](https://download.csdn.net/download/hadues/13183093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值