wangEdiotr 编辑器
Vue3 使用
安装
bash
pnpm install @wangeditor/editor --save
pnpm install @wangeditor/editor-for-vue@next --save
流程
注意
两个组件 Toolbar 和 Editor 一个是工具 栏 一个是编辑器
defaultConfig 是配置项 Toolbar 和 Editor 都需要配置
mode 是模式.分成 simple 和 default 两个模式 ,default 就是全的 simple 就是简单版本
v-model 就是获取到最后的用户 HTML
css 文件必须引入
获取示例
:editor="editorRef"
只能使用 shallowRef
快速入门
vue
<template>
<div id="editor—wrapper">
<div id="toolbar-container">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editorRef"
:defaultConfig="toolbarConfig"
:mode="mode"
/>
</div>
<div style="width: 100%; height: 40px; background: #ccc;"></div>
<div id="editor-container" style="border: 1px solid #ccc">
<Editor
style="height: 500px; overflow-y: hidden"
v-model="valueHtml"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="handleCreated"
@onChange="handleChange"
@onDestroyed="handleDestroyed"
@onFocus="handleFocus"
@onBlur="handleBlur"
@customAlert="customAlert"
@customPaste="customPaste"
/>
</div>
</div>
<button @click="getcontent">提交</button>
</template>
<script setup lang="ts">
import "@wangeditor/editor/dist/css/style.css"; // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted } from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
// 编辑器实例,必须用shallowRef包裹
const editorRef = shallowRef();
// 内容HTML
const valueHtml = ref("<p>hello</p>");
// 编辑器模式
const mode = ref("simple"); // simple或者default;
// 模拟ajax异步获取数据内容
onMounted(() => {
setTimeout(() => {
valueHtml.value = "<p>模拟 Ajax 异步设置内容</p>";
}, 1500);
});
// 工具栏配置
const toolbarConfig = {};
// 编辑器配置
const editorConfig = { placeholder: "请输入内容..." };
// 组件销毁的时候,也即时 销毁编辑器
onBeforeUnmount(() => {
const editor = editorRef.value;
if (editor == null) return;
editor.destroy();
});
// 编辑器示例
const handleCreated = (editor: any) => {
editorRef.value = editor; // 记录editor实例
};
// 获取到编辑器内容
const getcontent = () => {
window.alert(valueHtml.value);
};
</script>
<style scoped lang="scss"></style>
特别注意
在编辑器配置中 onxxx
格式的生命周期函数 ,必须通过 Vue 事件来传递.不可以放在配置文件中
bash
<Editor
@onCreated="onCreated"
@onChange="onChange"
@onDestroyed="onDestroyed"
@onMaxLength="onMaxLength"
@onFocus="onFocus"
@onBlur="onBlur"
@customAlert="customAlert"
@customPaste="customPaste"
/>
事件
ts
// 组件销毁的时候,也即时 销毁编辑器
onBeforeUnmount(() => {
const editor = editorRef.value;
if (editor == null) return;
editor.destroy();
});
// 编辑器示例
const handleCreated = (editor: any) => {
editorRef.value = editor; // 记录editor实例
};
// 改变
const handleChange = (editor) => {
console.log("change:", editor.children);
};
// 销毁
const handleDestroyed = (editor) => {
console.log("destroyed", editor);
};
// 获取焦点
const handleFocus = (editor) => {
console.log("focus", editor);
};
// 失去焦点
const handleBlur = (editor) => {
console.log("blur", editor);
};
// 自定义提示
const customAlert = (info, type) => {
alert(`【自定义提示】${type} - ${info}`);
};
// 自定义粘贴
const customPaste = (editor, event, callback) => {
console.log("ClipboardEvent 粘贴事件对象", event);
const html = event.clipboardData.getData("text/html"); // 获取粘贴的 html
const text = event.clipboardData.getData("text/plain"); // 获取粘贴的纯文本
const rtf = event.clipboardData.getData("text/rtf"); // 获取 rtf 数据(如从 word wsp 复制粘贴)
console.log(rtf);
console.log(html);
console.log(text);
// 自定义插入内容
editor.insertText(rft);
// 返回 false ,阻止默认粘贴行为
event.preventDefault();
callback(false); // 返回值(注意,vue 事件的返回值,不能用 return)
// 返回 true ,继续默认的粘贴行为
// callback(true)
};
// 获取到编辑器内容
const getcontent = () => {
window.alert(valueHtml.value);
};
调用 API
- 通过实例调取 API
js
editor.insertText(rft);