@farris/ui-editor-vue3
v0.2.2
Published
Farris HTML/富文本编辑器组件库,基于 Vue 3 与 Tiptap
Readme
@farris/ui-editor-vue3
基于 Vue 3 + Tiptap 的 Farris 富文本编辑器组件库(rich-text-editor),内置富文本编辑、表格、图片上传与缩放、代码块高亮、Monaco 源码编辑器、全屏等功能,并深度集成 Farris 设计器(Designer)体系。
特性
- 基于 Tiptap(ProseMirror)构建,扩展丰富、渲染稳定
- 开箱即用的工具栏:字体、字号、颜色、对齐、列表、缩进、超链接、图片、表格、分隔线等
- 支持 表格 的插入、行列增删、单元格合并/拆分(气泡菜单操作)
- 图片:粘贴即上传、等比缩放、上传进度展示,支持
base64与file(后端存储)两种保存模式 - 代码块:基于
lowlight的高亮;通过 Monaco Editor 提供源码视图编辑 - 富文本能力扩展:全屏、气泡菜单、placeholder、选中态模拟等
- i18n:内置中文简/繁、英文语言包,依赖
vue-i18n - 组件与 Farris Designer 无缝集成(
FHtmlEditorDesigner、schema、props/配置解析器) - 随包发布运行时资源(
assets/:工具栏图标、主题样式、Monaco 依赖),开箱即用
技术栈
| 依赖 | 用途 |
| --- | --- |
| vue ^3.5 | UI 框架 |
| @farris/ui-vue | Farris 基础组件库 / 设计器宿主 |
| @tiptap/* | 编辑器内核及扩展 |
| lowlight | 代码块语法高亮 |
| monaco-editor(运行时加载) | 源码视图编辑器 |
| vue-i18n | 国际化 |
安装
npm install @farris/ui-editor-vue3
# 或
yarn add @farris/ui-editor-vue3需同时安装
peerDependencies声明的依赖:vue@^3.5、@farris/ui-vue。vue-i18n需在宿主应用中预先安装并启用,用于组件内部文案国际化。
快速开始
1. 注册组件
// main.ts
import { createApp } from 'vue';
import App from './App.vue';
import FHtmlEditor from '@farris/ui-editor-vue3';
import '@farris/ui-editor-vue3/assets/farris-editor/themes/default/editor.css';
const app = createApp(App);
app.use(FHtmlEditor); // 全局注册
app.mount('#app');也支持按需引入:
import { FHtmlEditor, FHtmlEditorDesigner } from '@farris/ui-editor-vue3';2. 页面中使用
<template>
<FHtmlEditor
id="editor1"
v-model="content"
:min-height="400"
:enable-bubble-menu="true"
:image-save-mode="'file'"
:uploader="uploadImage"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const content = ref('<p>Hello <strong>Farris</strong>!</p>');
// imageSaveMode = 'file' 时必填,返回图片地址
const uploadImage = async (file: File): Promise<string> => {
const formData = new FormData();
formData.append('file', file);
const res = await fetch('/api/upload', { method: 'POST', body: formData });
const data = await res.json();
return data.url;
};
</script>API
Props
| 属性 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| id | string | —(必填) | 组件唯一标识 |
| modelValue / v-model | string | '' | HTML 内容(双向绑定) |
| placeholder | string | '请输入内容' | 空内容时的占位提示 |
| width | string \| number | '100%' | 编辑器宽度 |
| height | string \| number | '300px' | 编辑器高度 |
| autoHeight | boolean | true | 高度随内容自适应 |
| minHeight | string \| number | 300 | 最小高度 |
| maxHeight | string \| number | — | 最大高度(autoHeight=false 时生效) |
| disabled | boolean | false | 禁用编辑 |
| readonly | boolean | false | 只读 |
| enableToolbar | boolean | true | 是否显示工具栏 |
| toolbar | string[] | 见下 | 工具栏项配置 |
| enableBubbleMenu | boolean | false | 启用选中文字后的气泡菜单 |
| assetsPath | string | 'assets/farris-editor/' | 组件资源(图标/主题)地址前缀 |
| vsPath | string | '/platform/common/web/monaco/vs' | Monaco 编辑器 vs 目录地址 |
| editSourceCode | boolean | true | 是否显示"源码"工具栏按钮 |
| enableContextMenu | boolean | true | 源码视图中是否启用右键菜单 |
| allowImageUrl | boolean | true | 是否允许通过 URL 插入图片 |
| imageSaveMode | 'base64' \| 'file' | 'base64' | 图片保存方式 |
| uploader | (file: File) => Promise<string> | — | 图片上传函数(file 模式必填) |
| serverConfigId | string | '' | 服务端配置标识 |
| dataId | string | '' | 数据标识(配合上传/回显) |
| uploadedImages | { id: string; url: string }[] | [] | 已上传图片列表(回显用) |
工具栏项(toolbar)
可用的项:undo redo bold italic underline strike heading font fontSize textColor bgColor alignLeft alignCenter alignRight alignJustify subscript superscript bulletList orderedList outdent indent horizontalRule link image code table sourceCode fullscreen;使用 '|' 作为分隔符。
默认值:
[
'bold', 'italic', 'underline', 'strike', 'textColor', 'bgColor', '|',
'heading', 'font', 'fontSize', '|', 'link', '|',
'alignLeft', 'alignCenter', 'alignRight', 'bulletList', 'orderedList', '|',
'image', 'table', 'horizontalRule', '|',
'sourceCode', 'fullscreen'
]Events
| 事件 | 参数 | 说明 |
| --- | --- | --- |
| update:modelValue | (html: string) | 内容变化时触发 |
| update:uploadedImages | (images: { id: string; url: string }[]) | 上传图片集合变化时触发 |
运行时资源说明
组件依赖随包发布的 assets/ 目录(工具栏图标、主题样式、Monaco 文件)。请在宿主项目中保证以下资源可被访问:
主题样式(推荐直接引入):
import '@farris/ui-editor-vue3/assets/farris-editor/themes/default/editor.css';assetsPath:默认assets/farris-editor/,为相对运行时页面的路径。若资源被拷贝到静态目录或 CDN,可通过 prop 或全局配置调整。Monaco(
vsPath):默认/platform/common/web/monaco/vs。若未部署平台 Monaco,可改为任意可访问的monaco-editor/min/vs路径(例如 CDN:https://cdn.jsdelivr.net/npm/[email protected]/min/vs),或直接使用包内assets/monaco/vs。
Farris 设计器集成
组件库提供设计器组件与注册能力,便于在 Farris Designer 中使用:
import { FHtmlEditor, FHtmlEditorDesigner } from '@farris/ui-editor-vue3';
// 注册为 'rich-text-editor' 控件(运行态/设计态)
FHtmlEditor.register(componentMap, propsResolverMap, configResolverMap, resolverMap);
FHtmlEditor.registerDesigner(componentMap, propsResolverMap, configResolverMap);目录结构
packages/
├── index.ts # 库入口(install / 导出)
└── ui-editor-vue3/
├── package.json # 发布配置(npm 包元数据权威源)
└── src/
├── html-editor.component.tsx # 富文本编辑器主组件
├── designer/ # Farris 设计器相关
├── components/ # 工具栏、气泡菜单、源码编辑等
├── composition/ # 组合式函数(状态/上传/命令/上传服务等)
├── extensions/ # Tiptap 扩展(缩进、图片缩放、粘贴上传、全屏等)
├── locales/ # 语言包(zh-CHS / zh-CHT / en)
└── schema/ # 设计器 schema 映射/解析开发与构建
npm install # 安装依赖
npm run dev # 本地开发
npm run build # 类型检查 + 库构建(输出到 dist/@farris/ui-editor-vue3)
npm run build:dev # 构建 system 格式产物(低版本浏览器兼容)
npm run build:prod# 构建 system 格式产物(生产/压缩)
npm run build:css # 单独编译主题 scss -> editor.css构建产物(dist/@farris/ui-editor-vue3/):
├── ui-editor-vue3.es.js # ESM
├── ui-editor-vue3.umd.js # UMD
├── ui-editor-vue3.schema.json # 设计器 schema
├── types/ # TypeScript 类型声明
└── assets/ # 运行时资源(图标/主题/monaco)发布
npm run publish:lib该命令依次执行:build(构建库)→ 将 packages/ui-editor-vue3/package.json(发布配置权威源)同步到 dist/@farris/ui-editor-vue3/ → 发布到 npm registry。
发布前请先在
packages/ui-editor-vue3/package.json中递增version(npm 不允许重复发布相同版本)。
许可证
MIT
English
@farris/ui-editor-vue3 is a rich-text editor component (type rich-text-editor) for Vue 3 built on Tiptap, part of the Farris design system. It ships with a full toolbar, table support, image upload/resize, code-block highlighting, Monaco-based source-code editing, fullscreen, i18n (vue-i18n) and deep integration with the Farris Designer (FHtmlEditorDesigner).
<template>
<FHtmlEditor id="editor1" v-model="content" />
</template>
<script setup lang="ts">
import { ref } from 'vue';
const content = ref('<p>Hello <strong>Farris</strong>!</p>');
</script>import { createApp } from 'vue';
import App from './App.vue';
import FHtmlEditor from '@farris/ui-editor-vue3';
import '@farris/ui-editor-vue3/assets/farris-editor/themes/default/editor.css';
createApp(App).use(FHtmlEditor).mount('#app');All component props follow camelCase in TS / kebab-case in templates (see the Chinese API table above). Runtime assets (assets/) are published with the package.
