aden-editor-vue
v0.1.17
Published
Vue 3 富文本编辑器组件,支持 v-model、自定义工具栏和业务上传接口
Maintainers
Readme
aden-editor-vue
Vue 3 富文本编辑器组件,安装后即可使用。 提供常用格式工具栏、图片/视频/附件、表格、公式、待办、撤销重做、字数统计和粘贴清洗。支持 v-model,并能通过配置连接你自己的上传与提及查询接口。
- Vue 3 项目专用的独立集成包
- 开箱即用的编辑器和内置工具栏
- 支持 TypeScript,暴露读取和设置内容的方法
- 上传地址、鉴权和服务端返回格式由业务项目自行控制
安装
npm install aden-editor-vue要求 Vue 3.3 或更高版本。如果当前项目还没有安装 Vue:
npm install vue aden-editor-vue本包将匹配版本的编辑器运行时代码一并打包,更新 aden-editor-vue 即可获得 Vue 插件和编辑器内核的修复。安装时会自动安装 aden-editor 作为公开 TypeScript 类型声明的依赖。React 项目请使用独立的 React 集成包(发布后提供),不要在 React 项目中安装本包。
最快开始
在 Vue 页面中直接使用组件:
<script setup lang="ts">
import { ref } from 'vue';
import { AdenEditor } from 'aden-editor-vue';
const content = ref('<p>欢迎使用 AdenEditor</p>');
</script>
<template>
<AdenEditor v-model="content" />
<pre>{{ content }}</pre>
</template>默认工具栏和样式会自动加载。未传 options 时,文字编辑、列表、引用、表格等内置功能仍可直接使用;无需单独导入 CSS,也无需自己处理创建、挂载和卸载。
带 HTML 格式的粘贴会以可编辑的富文本片段导入,尽量保留段落、表格、内联格式和可安全转换的样式;Word 内容不是嵌入的 Word 文档。编辑器会同时检查粘贴事件与异步 Clipboard API 提供的图片/RTF 数据,支持 RTF 十六进制及 \\binN 二进制图片负载,提取 PNG/JPEG,并将 EMF/WMF 转为 PNG 后交给 uploadImage。Word 中使用 Ctrl+A 全选复制时,剪贴板仍可能只提供图片引用、不含图片数据;若浏览器没有暴露 RTF 或图片文件,网页端无法访问 Word 的本地临时图片路径。
保存内容
v-model 的值是 HTML,适合接入常见的 HTML 内容字段。若需保存完整编辑器文档结构,可通过组件 ref 获取 JSON:
<script setup lang="ts">
import { ref } from 'vue';
import { AdenEditor } from 'aden-editor-vue';
import type { AdenEditorExpose } from 'aden-editor-vue';
const content = ref('<p>编辑内容</p>');
const editor = ref<AdenEditorExpose | null>(null);
async function save() {
const html = editor.value?.getHtml();
const doc = editor.value?.getJSON();
await fetch('/api/articles/123', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ html, doc }),
});
}
</script>
<template>
<AdenEditor ref="editor" v-model="content" />
<button type="button" @click="save">保存</button>
</template>组件 ref 提供:getHtml()、setHtml(html)、getJSON()、setJSON(doc)、getMarkdown()、getText()、focus() 和 clear()。
工具栏配置
可通过 options.toolbarItems 自选工具和排列顺序。每个内层数组代表一行:
<script setup lang="ts">
import { ref } from 'vue';
import { AdenEditor } from 'aden-editor-vue';
import type { AdenEditorOptions } from 'aden-editor-vue';
const content = ref('');
const options: AdenEditorOptions = {
placeholder: '写点什么…',
toolbarItems: [
['undo', 'redo', '|', 'heading', 'bold', 'italic', 'underline', 'link'],
['ul', 'ol', 'quote', 'codeblock', 'image', 'table', 'stat'],
],
};
</script>
<template>
<AdenEditor v-model="content" :options="options" />
</template>不设置 toolbarItems 使用完整默认工具栏;设 options.toolbar = false 可隐藏内置工具栏。工具栏项 ID 包括 bold、heading、fontSize、color、ul、quote、image、video、attachment、table、formula、fullscreen 等。可从 BuiltinToolbarItemId 获得完整 TypeScript 类型。
接入自己的上传接口
上传回调接收浏览器 File,需返回文件可访问的 URL。接口路径、登录态、鉴权头和返回 JSON 结构都在业务代码中处理:
const options: AdenEditorOptions = {
uploadImage: async (file) => {
const form = new FormData();
form.append('file', file);
const response = await fetch('/api/upload/image', {
method: 'POST',
headers: { Authorization: `Bearer ${getToken()}` },
body: form,
});
if (!response.ok) throw new Error(`图片上传失败:${response.status}`);
const result = await response.json() as { url: string };
return result.url;
},
};视频和附件可分别配置 uploadVideo、uploadAttachment。不配置上传回调时,编辑器不会猜测或请求你的业务接口。
内容变化与自动保存
组件的 change 事件会给出最新 JSON 和 HTML。适合在这里做防抖后自动保存:
<AdenEditor
v-model="content"
:options="options"
@change="({ doc, html }) => scheduleSave({ doc, html })"
/>提及、只读与字数限制
const options: AdenEditorOptions = {
maxLength: 5000,
onMaxLength: (length, max) => console.info(`当前 ${length}/${max}`),
mentionSearch: async (query) => {
const response = await fetch(`/api/users?q=${encodeURIComponent(query)}`);
if (!response.ok) throw new Error('用户查询失败');
return response.json(); // { id, label, description?, avatar? }[]
},
};只读状态可以通过 readonly 响应式属性动态切换:
<AdenEditor v-model="content" :readonly="isReadonly" />生命周期与 SSR
组件在 Vue 客户端挂载时初始化编辑器,在卸载时自动清理事件和弹层。使用 Nuxt 或其他 SSR 框架时,可将编辑器放在客户端组件边界内,例如 Nuxt 的 <ClientOnly>:
<ClientOnly>
<AdenEditor v-model="content" />
</ClientOnly>样式定制
默认样式随组件自动注入。可在项目 CSS 中覆盖编辑器变量:
.aden-editor {
--aden-accent: #e8452c;
--aden-radius: 4px;
--aden-font: 14px;
}当前支持范围
此包面向 Vue 3 浏览器项目,复用 aden-editor 的 Web 编辑能力。小程序端和协作编辑目前不包含在本 Vue 组件包内。
许可
MIT
