vue3使用TinyMCE富文本

news/2024/7/10 2:29:59 标签: javascript, vue

TinyMCE 介绍

TinyMCE 是一个功能强大的富文本编辑器,它允许您在网页应用程序中创建和编辑具有丰富格式的内容。官网    github项目地址   文档地址

下载tinymce文件

从网页下载最新版zip,也可以打开下面链接下载。 打开网页

tinymce.zip
zh-Hans

将下载的文件解压缩到Vue项目 public文件中

在这里插入图片描述

修改lang

将 zh-Hans 改为 zh_CN

在这里插入图片描述
在这里插入图片描述

组件

<template>
	<textarea :id="id" class="hidden"></textarea>
</template>
<script lang="ts" setup>
import { ref, watch, onMounted, onBeforeUnmount } from 'vue';
import type { EditorManager } from 'public/js/tinymce/tinymce';

const emit = defineEmits(['update:value']);

interface Props {
	value: string | undefined;
}
/** 设置默认值 */
const props = withDefaults(defineProps<Props>(), {
});

// 根据当前时间戳转16进制
function getId() {
	return Date.now().toString(16);
}
// id
const id = ref<string>('tinymce_' + getId());
// 内容
const value = ref<string>(props.value || '');
// tinymce对象
var tinymce: EditorManager;

/**
 * 加载Script
 * @param url Script地址
 */
function loadScript(url: string) : Promise<boolean> {
	if (window?.$tinymceLoad) {
		return window.$tinymceLoad;
	}
	window.$tinymceLoad = new Promise<boolean>((resolve, reject) => {
		try {
			const script = document.createElement('script');
			script.src = url;
			script.onload = function () {
				resolve(true);
			};
			script.onerror = function () {
				reject(false);
			};
			document.body.appendChild(script);
		} catch (error) {
			reject(false);
		}
	});
	return window?.$tinymceLoad;
}

/** Script  */
const tinymceLoad = loadScript('/js/tinymce/tinymce.min.js');

watch(() => props.value, (_nVal, _oVal) => {
	if (_nVal != value.value) {
		value.value = _nVal || '';
		if (tinymce.get(id.value)) {
			tinymce.get(id.value)?.setContent(value.value);
		}
	}
});

watch(value, (_nVal, _oVal) => {
	if (_nVal != _oVal)
		emit('update:value', _nVal);
});

// 挂载
onMounted(async () => {
	const isSuccess = await tinymceLoad;
	if (!isSuccess || !window.tinymce) {
		console.error('tinymce 加载失败!!!!')
		return;
	}
	if(value.value){
		const el = document.getElementById(id.value);
		if(el)
			el.innerText = value.value
	}
	tinymce = window.tinymce;
	tinymce._setBaseUrl('/js/tinymce/');
	tinymce.init({
		selector: `#${id.value}`,
		language: 'zh_CN',
		language_url: '/js/tinymce/langs/zh-Hans.js',
		// 编辑器的高度
		height: '100%',
		// 编辑器的宽度
		width: '100%',
		// 菜单栏
		menubar: false,
		// 去掉右上脚升级按钮
		promotion: false,
		// 底部状态栏
		statusbar: true,
		// 元素路径
		elementpath: true,
		// 去掉右下脚Logo图标
		branding: false,
		// 调整大小
		resize: false,
		// 工具栏
		toolbar: 'undo redo restoredraft |blocks fontfamily fontsize forecolor backcolor removeformat| bold italic underline strikethrough blockquote| alignleft aligncenter alignright alignjustify accordion| bullist numlist outdent indent | anchor link image media  |  codesample  visualblocks emoticons code preview searchreplace  pagebreak fullscreen  print  help ',
		// toobar 显示模式  floating  sliding scrolling  wrap
		toolbar_mode: 'wrap',
		// 插件
		plugins: 'advlist autolink lists link image charmap  preview anchor searchreplace visualblocks code fullscreen insertdatetime media table  code help wordcount pagebreak visualblocks emoticons codesample accordion ', 
		//contextmenu: 'undo redo | inserttable | cell row column deletetable | help',
		setup: function (editor) {
			editor.on('change', function () {
				const content = editor.getContent();
				value.value = content;
			});
		}
	}); 
})

/** 卸载 */
onBeforeUnmount(() => {
	tinymce?.remove('#' + id.value);
})

</script>

使用

<template>
	<div class="m-8px">
		<tiny-mce	v-model:value="text"></tiny-mce>
	</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';

const text =ref<string>('Hello Word');
</script>

效果

在这里插入图片描述


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

相关文章

【转】ORB-SLAM2调用OAK-D双目摄像头进行点云建图

编辑&#xff1a;OAK中国 首发&#xff1a;A. hyhCSDN 喜欢的话&#xff0c;请多多&#x1f44d;⭐️✍ 内容来自用户的分享&#xff0c;如有疑问请与原作者交流&#xff01; ▌前言 Hello&#xff0c;大家好&#xff0c;这里是OAK中国&#xff0c;我是助手君。 近期在CSDN刷…

Spring Bean教程

Spring Bean教程 Spring框架是一个轻量级的Java开发框架&#xff0c;它提供了一套完整的解决方案&#xff0c;用于简化企业级应用程序的开发。其中&#xff0c;Spring Bean是Spring框架的核心概念之一&#xff0c;它代表了Spring容器中的一个对象实例。本教程将详细介绍Spring…

亥姆霍兹线圈的组成

亥姆霍兹线圈是由两个半径、匝数、电流★全相同的线圈&#xff0c;距离为半径长度&#xff0c;运行电流方向相同组成。 亥姆霍兹线圈是一种产生均匀磁场的线圈&#xff0c;其磁场特点是在内部产生均匀度较高的磁场&#xff0c;一般长螺线管的均匀度要优于赫姆霍兹线圈&#xf…

ISO20000认证实施意义

ISO20000认证已经成为越来越多企业关注的焦点。ISO2000作为服务管理体系的标准&#xff0c;其实施意义究竟在哪里呢&#xff1f;本文将围绕这个问题&#xff0c;进行详细的解答。我们也将探讨ISO20000的具体含义、实施ISO认证的条件以及ISO20000的认证流程。 一、ISO20000认证…

Linux操作系统使用及C高级编程-D15D16内存管理和动态内存使用

内存分区 使用size查看内存使用 动态内存使用 不能返回局部变量的引用&#xff0c;局部变量存放在栈区&#xff0c;空间随着函数结束自动释放 动态申请内存 内存泄漏和内存溢出

华北高温背后的隐忧,双碳最大的障碍之一藏在这里

高温天气带来的庞大用电负荷&#xff0c;再一次让人们想到能源紧张问题和节能减碳的必要性。 然而&#xff0c;在日常企业经营中&#xff0c;由于员工缺乏对公共财产的成本意识&#xff0c;往往造成电力资源的巨大浪费。节能减碳&#xff0c;必须与精细化管理相结合&#xff0c…

请停止在简历上写: 精通Python, 会害了你

离了个大谱&#xff01; 瑞银暑期实习生都要求精通Python? 你以为能用Python演示“hello world" 就是精通Python了么&#xff1f; too yang too天真 一、不会Python的我们不要 1、瑞士银行 瑞士银行的暑期实习岗位JD中要求应聘者精通编程语言&#xff0c;特别是C或…

c语言通过前序遍历构建二叉树

前言&#xff1a; 在链式二叉树中&#xff0c;我们一般都是通过一个建立好的二叉树从而算出他的前序遍历&#xff0c;那么如何通过一个前序遍历来创建一个二叉树呢&#xff0c;本文将详细解读前序遍历每一个步骤是如何创建二叉树的。 1、分析前序遍历&#xff0c;构建出二叉树…