@norio-office/rich-text 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +971 -0
  3. package/dist/chunk-efA98nb6.js +13 -0
  4. package/dist/components/ColumnsBlockView.vue.d.ts +26 -0
  5. package/dist/components/CountdownBlockView.vue.d.ts +10 -0
  6. package/dist/components/EmojiPickerPanel.vue.d.ts +11 -0
  7. package/dist/components/FormulaBlockView.vue.d.ts +3 -0
  8. package/dist/components/FormulaEditorDialog.vue.d.ts +20 -0
  9. package/dist/components/HighlightBlockView.vue.d.ts +28 -0
  10. package/dist/components/ImageBlockView.vue.d.ts +6 -0
  11. package/dist/components/LinkBlockView.vue.d.ts +5 -0
  12. package/dist/components/LocalFileBlockView.vue.d.ts +3 -0
  13. package/dist/components/MentionInlineView.vue.d.ts +3 -0
  14. package/dist/components/OfficeCodeBlockView.vue.d.ts +28 -0
  15. package/dist/components/OfficeColorIcon.vue.d.ts +10 -0
  16. package/dist/components/OfficeIcon.vue.d.ts +12 -0
  17. package/dist/components/OutlinePanel.vue.d.ts +21 -0
  18. package/dist/components/RichTextEditor.vue.d.ts +69 -0
  19. package/dist/components/ScrollArea.vue.d.ts +24 -0
  20. package/dist/components/VideoBlockView.vue.d.ts +7 -0
  21. package/dist/composables/useRichTextEditor.d.ts +20 -0
  22. package/dist/data/emoji.d.ts +13 -0
  23. package/dist/extensions/columns-block.d.ts +2 -0
  24. package/dist/extensions/columns-column.d.ts +2 -0
  25. package/dist/extensions/countdown-block.d.ts +2 -0
  26. package/dist/extensions/formula-block.d.ts +2 -0
  27. package/dist/extensions/highlight-block.d.ts +2 -0
  28. package/dist/extensions/image-block.d.ts +2 -0
  29. package/dist/extensions/indent.d.ts +17 -0
  30. package/dist/extensions/link-block.d.ts +2 -0
  31. package/dist/extensions/local-file-block.d.ts +2 -0
  32. package/dist/extensions/mention.d.ts +2 -0
  33. package/dist/extensions/office-code-block.d.ts +1 -0
  34. package/dist/extensions/quote-style.d.ts +19 -0
  35. package/dist/extensions/text-align.d.ts +12 -0
  36. package/dist/extensions/video-block.d.ts +2 -0
  37. package/dist/html2canvas-Bwmq6fGh.js +4025 -0
  38. package/dist/icons.d.ts +8 -0
  39. package/dist/index.d.ts +8 -0
  40. package/dist/index.es-C4X5YiRo.js +5751 -0
  41. package/dist/index.js +38311 -0
  42. package/dist/index.umd.cjs +6652 -0
  43. package/dist/jspdf.es.min-ByaXFVjc.js +18214 -0
  44. package/dist/locales/editor.d.ts +5 -0
  45. package/dist/purify.es-B5ixYH9L.js +549 -0
  46. package/dist/style.css +2 -0
  47. package/dist/typeof-Dz1Ap89p.js +11 -0
  48. package/dist/types.d.ts +118 -0
  49. package/dist/utils/columns.d.ts +4 -0
  50. package/dist/utils/markdown-import.d.ts +2 -0
  51. package/dist/utils/mention.d.ts +6 -0
  52. package/docs/usage.md +95 -0
  53. package/package.json +73 -0
  54. package/yjs/README.md +63 -0
  55. package/yjs/index.js +224 -0
  56. package/yjs/package.json +14 -0
package/README.md ADDED
@@ -0,0 +1,971 @@
1
+ # @norio-office/rich-text
2
+
3
+ 个人开发,未全方位测试,用于生产请多测试一下
4
+
5
+ 中文 | [English](#english)
6
+
7
+ ## 中文
8
+
9
+ `@norio-office/rich-text` 是一个基于 Vue 3 和 Tiptap 3 的富文本编辑器组件,内置文档式编辑界面、图片/视频/文件块、表格、公式、倒计时、高亮块、大纲、预览模式、导出能力,以及可选的 Yjs 协同编辑。
10
+
11
+ ### 安装
12
+
13
+ ```bash
14
+ npm install @norio-office/rich-text
15
+ ```
16
+
17
+ 组件样式会作为独立 CSS 文件发布。建议在应用入口显式引入:
18
+
19
+ ```ts
20
+ import '@norio-office/rich-text/style.css'
21
+ ```
22
+
23
+ 如果你的包管理器没有自动安装 peer dependencies,请同时安装运行时 peer 包:
24
+
25
+ ```bash
26
+ npm install vue @tiptap/core @tiptap/vue-3 @tiptap/pm @tiptap/starter-kit @tiptap/extension-placeholder @tiptap/extension-code-block @tiptap/extension-code-block-lowlight @tiptap/extension-collaboration @tiptap/extension-collaboration-caret @tiptap/extension-font-family @tiptap/extension-subscript @tiptap/extension-superscript @tiptap/extension-table @tiptap/extension-table-cell @tiptap/extension-table-header @tiptap/extension-table-row @tiptap/extension-task-item @tiptap/extension-task-list @tiptap/extension-text-style @tiptap/extension-underline @tiptap/y-tiptap lowlight y-prosemirror yjs
27
+ ```
28
+
29
+ `html2canvas`、`jspdf`、`katex`、`marked`、`plyr` 已作为普通 dependencies 随包安装。
30
+
31
+ ### 快速开始
32
+
33
+ ```vue
34
+ <script setup lang="ts">
35
+ import { ref } from 'vue'
36
+ import type { JSONContent } from '@tiptap/core'
37
+ import { RichTextEditor } from '@norio-office/rich-text'
38
+ import '@norio-office/rich-text/style.css'
39
+
40
+ const content = ref<JSONContent | null>(null)
41
+ </script>
42
+
43
+ <template>
44
+ <RichTextEditor v-model="content" />
45
+ </template>
46
+ ```
47
+
48
+ 也可以使用默认导出:
49
+
50
+ ```ts
51
+ import RichTextEditor from '@norio-office/rich-text'
52
+ ```
53
+
54
+ ### Props
55
+
56
+ | Prop | 类型 | 默认值 | 说明 |
57
+ | --- | --- | --- | --- |
58
+ | `modelValue` | `JSONContent \| null` | `null` | 编辑器内容,支持 `v-model`。 |
59
+ | `editable` | `boolean` | `true` | 是否允许编辑。 |
60
+ | `mode` | `'edit' \| 'preview'` | `'edit'` | 编辑模式或预览模式。 |
61
+ | `outlinePlacement` | `'left' \| 'right'` | `'right'` | 大纲面板位置。 |
62
+ | `messages` | `RichTextEditorMessages \| null` | `null` | 覆盖内置 UI 文案。 |
63
+ | `enabledExportItems` | `RichTextEditorExportItemKey[] \| null` | `null` | 导出菜单白名单。 |
64
+ | `enabledInsertMenuItems` | `RichTextEditorInsertMenuItemKey[] \| null` | `null` | 插入菜单白名单。 |
65
+ | `enabledToolbarActions` | `RichTextEditorToolbarActionKey[] \| null` | `null` | 工具栏能力白名单。 |
66
+ | `placeholder` | `string` | `''` | 空内容占位文案。 |
67
+ | `collaboration` | `RichTextEditorCollaborationOptions \| null` | `null` | Yjs 协同配置。 |
68
+ | `mentionProvider` | `RichTextEditorMentionProvider \| null` | `null` | `@` 提及候选数据提供函数,兼容用法。 |
69
+ | `onMentionSearch` | `RichTextEditorMentionProvider \| null` | `null` | `@mention-search` 函数式事件对应的异步候选加载函数。 |
70
+
71
+ 白名单 props 不传时表示启用全部内置项;传入数组后,仅数组里的项目会显示并可用。
72
+
73
+ ```vue
74
+ <RichTextEditor
75
+ v-model="content"
76
+ :enabled-export-items="['html', 'image']"
77
+ :enabled-insert-menu-items="['image', 'local-file', 'blockquote']"
78
+ :enabled-toolbar-actions="['blockquote']"
79
+ />
80
+ ```
81
+
82
+ ### 可用 Key
83
+
84
+ 导出菜单:
85
+
86
+ ```ts
87
+ type RichTextEditorExportItemKey = 'pdf' | 'html' | 'image' | 'print'
88
+ ```
89
+
90
+ 插入菜单:
91
+
92
+ ```ts
93
+ type RichTextEditorInsertMenuItemKey =
94
+ | 'image'
95
+ | 'video'
96
+ | 'table'
97
+ | 'local-file'
98
+ | 'columns'
99
+ | 'highlight-block'
100
+ | 'date'
101
+ | 'code-block'
102
+ | 'formula'
103
+ | 'blockquote'
104
+ | 'emoji'
105
+ | 'link'
106
+ | 'divider'
107
+ | 'countdown'
108
+ | 'markdown-import'
109
+ ```
110
+
111
+ 工具栏:
112
+
113
+ ```ts
114
+ type RichTextEditorToolbarActionKey = 'blockquote'
115
+ ```
116
+
117
+ ### 事件
118
+
119
+ | 事件 | Payload | 说明 |
120
+ | --- | --- | --- |
121
+ | `update:modelValue` | `JSONContent` | 内容更新,用于 `v-model`。 |
122
+ | `change` | `JSONContent` | 内容更新事件。 |
123
+ | `local-file-upload` | `RichTextEditorLocalFilePayload` | 用户通过本地文件选择器插入文件后触发。 |
124
+ | `local-file-download` | `RichTextEditorLocalFilePayload` | 用户点击本地文件卡片下载按钮时触发。 |
125
+ | `mention-search` | `RichTextEditorMentionProviderPayload` | 输入 `@` 后加载候选数据,可返回数组或 `Promise`。 |
126
+ | `mention-item-click` | `RichTextEditorMentionItem` | 点击弹窗候选项或已插入的提及节点时触发。 |
127
+ | `mention-submit` | `RichTextEditorMentionItem` | 点击弹窗 `提及` 按钮并插入提及节点后触发。 |
128
+
129
+ ### @ 提及
130
+
131
+ 输入 `@` 会打开候选弹窗。宿主项目通过 `@mention-search` 异步返回候选数据,组件不会内置默认人员或文档数据。推荐使用 `@mention-search`,`mentionProvider` 仅作为函数 prop 兼容保留。
132
+
133
+ `type: 1` 表示人,`type: 2` 表示文档。人和文档共用 `id`、`name`、`type`、`avatar`、`icon`、`tag`、`updatedAt` 等字段;文档传入 `tag` 时会在标题旁显示标签文字,不传则不显示;人没有头像时会用名字最后两个字和固定亮色颜色池生成头像。
134
+
135
+ ```vue
136
+ <script setup lang="ts">
137
+ import { ref } from 'vue'
138
+ import type { JSONContent } from '@tiptap/core'
139
+ import { RichTextEditor } from '@norio-office/rich-text'
140
+ import type {
141
+ RichTextEditorMentionItem,
142
+ RichTextEditorMentionProviderPayload,
143
+ } from '@norio-office/rich-text'
144
+
145
+ const content = ref<JSONContent | null>(null)
146
+
147
+ const mentionItems: RichTextEditorMentionItem[] = [
148
+ { id: 'user-1001', name: '崔国强', type: 1 },
149
+ { id: 'user-1002', name: '刘佳宁', type: 1 },
150
+ { id: 'doc-2001', name: 'IT资产管理系统', type: 2, tag: '外部', updatedAt: '2025年12月1日' },
151
+ { id: 'doc-2002', name: '客户满意度调研表', type: 2, updatedAt: '2025年12月4日' },
152
+ ]
153
+
154
+ async function handleMentionSearch(payload: RichTextEditorMentionProviderPayload) {
155
+ const query = payload.query.trim().toLowerCase()
156
+ await new Promise((resolve) => window.setTimeout(resolve, 120))
157
+
158
+ return mentionItems.filter((item) => {
159
+ const matchedType = payload.type === 'all' || item.type === payload.type
160
+ const matchedKeyword = !query || item.name.toLowerCase().includes(query)
161
+ return matchedType && matchedKeyword
162
+ })
163
+ }
164
+
165
+ function handleMentionItemClick(item: RichTextEditorMentionItem) {
166
+ console.log('clicked mention item:', item)
167
+ }
168
+
169
+ function handleMentionSubmit(item: RichTextEditorMentionItem) {
170
+ console.log('inserted mention:', item)
171
+ }
172
+ </script>
173
+
174
+ <template>
175
+ <RichTextEditor
176
+ v-model="content"
177
+ @mention-search="handleMentionSearch"
178
+ @mention-item-click="handleMentionItemClick"
179
+ @mention-submit="handleMentionSubmit"
180
+ />
181
+ </template>
182
+ ```
183
+
184
+ 点击候选项只会触发 `mention-item-click` 并选中当前项;点击底部 `提及` 按钮才会插入结构化提及节点并触发 `mention-submit`。关闭弹窗或不点击 `提及` 时,编辑器会保留普通 `@` 文本。
185
+
186
+ ### 自定义文案
187
+
188
+ 编辑器默认 UI 文案为中文。你可以用 `messages` 覆盖任意 key:
189
+
190
+ ```vue
191
+ <RichTextEditor
192
+ v-model="content"
193
+ :messages="{
194
+ 'insert.localFile': '附件',
195
+ 'export.label': '下载',
196
+ }"
197
+ />
198
+ ```
199
+
200
+ 常用文案 key:
201
+
202
+ | Key | 默认中文 |
203
+ | --- | --- |
204
+ | `insert.label` | 插入 |
205
+ | `insert.section.general` | 通用 |
206
+ | `insert.section.apps` | 小应用 |
207
+ | `insert.section.external` | 外部内容 |
208
+ | `insert.image` | 图片 |
209
+ | `insert.video` | 视频 |
210
+ | `insert.table` | 表格 |
211
+ | `insert.localFile` | 本地文件 |
212
+ | `insert.columns` | 分栏 |
213
+ | `insert.highlightBlock` | 高亮块 |
214
+ | `insert.date` | 日期 |
215
+ | `insert.codeBlock` | 代码块 |
216
+ | `insert.formula` | 公式 |
217
+ | `insert.blockquote` | 引用 |
218
+ | `insert.emoji` | 表情符号 |
219
+ | `insert.link` | 超链接 |
220
+ | `insert.divider` | 分隔线 |
221
+ | `insert.countdown` | 倒计时 |
222
+ | `insert.markdownImport` | Markdown 导入 |
223
+ | `export.label` | 导出 |
224
+ | `export.pdf` | 导出 PDF |
225
+ | `export.pdf.loading` | 导出 PDF 中... |
226
+ | `export.html` | 导出 HTML |
227
+ | `export.html.loading` | 导出 HTML 中... |
228
+ | `export.image` | 导出图片 |
229
+ | `export.image.loading` | 导出图片中... |
230
+ | `print.label` | 打印 |
231
+ | `print.loading` | 打印中... |
232
+ | `quote.apply` | 应用引用 |
233
+ | `quote.cancel` | 取消引用 |
234
+ | `quote.borderColor` | 边框颜色 |
235
+ | `quote.backgroundColor` | 背景颜色 |
236
+ | `outline.label` | 大纲 |
237
+ | `outline.collapse` | 收起大纲 |
238
+ | `outline.empty.description` | 对文档内容应用“标题”样式,即可自动生成大纲。 |
239
+ | `outline.empty.tip` | 点击左下角“大纲”按钮可以随时展开或收起。 |
240
+ | `status.wordCountUnit` | 个字 |
241
+ | `status.presentation.enter` | 演示 |
242
+ | `status.presentation.exit` | 退出演示 |
243
+ | `status.fullscreen.enter` | 全屏 |
244
+ | `status.fullscreen.exit` | 退出全屏 |
245
+ | `countdown.selectTime` | 请选择时间 |
246
+ | `countdown.settingsTitle` | 倒计时设置 |
247
+ | `formula.insertTitle` | 插入 LaTeX 公式 |
248
+
249
+ ### 预览模式
250
+
251
+ `mode="preview"` 会切换到只读预览外壳。预览模式下工具栏隐藏,编辑禁用,大纲仍可放在左侧或右侧。
252
+
253
+ ```vue
254
+ <RichTextEditor
255
+ v-model="content"
256
+ mode="preview"
257
+ outline-placement="right"
258
+ />
259
+ ```
260
+
261
+ 窄屏下预览模式会自动缩放页面画布,让文档在手机上保持可读。
262
+
263
+ ### 协同编辑
264
+
265
+ 组件支持普通单人编辑,也支持可选的 Yjs 协同编辑。
266
+
267
+ ```vue
268
+ <script setup lang="ts">
269
+ import { ref } from 'vue'
270
+ import * as Y from 'yjs'
271
+ import { WebsocketProvider } from 'y-websocket'
272
+ import type { JSONContent } from '@tiptap/core'
273
+ import { RichTextEditor } from '@norio-office/rich-text'
274
+ import '@norio-office/rich-text/style.css'
275
+
276
+ const ydoc = new Y.Doc()
277
+ const provider = new WebsocketProvider('ws://localhost:1234', 'office-word-demo', ydoc)
278
+ const content = ref<JSONContent | null>(null)
279
+ </script>
280
+
281
+ <template>
282
+ <RichTextEditor
283
+ v-model="content"
284
+ :collaboration="{
285
+ document: ydoc,
286
+ field: 'content',
287
+ provider,
288
+ user: {
289
+ name: '张三',
290
+ color: '#3b82f6',
291
+ },
292
+ }"
293
+ />
294
+ </template>
295
+ ```
296
+
297
+ 协同模式下,Yjs fragment 是内容的事实来源。组件仍会 emit JSON 更新,但外部 `modelValue` 的变化不会再反向写入编辑器。如果需要远程光标,请在宿主项目安装 `y-websocket`,并传入 `provider` 和 `user`。
298
+
299
+ 不要把 `modelValue` 当作协同房间的初始化种子。如果房间需要默认内容,请在挂载编辑器之前先初始化 Yjs 文档。
300
+
301
+ ### 内置 Yjs 服务示例
302
+
303
+ 发布包内包含一个最小 Yjs WebSocket 服务示例:
304
+
305
+ ```bash
306
+ cd node_modules/@norio-office/rich-text/yjs
307
+ npm install
308
+ npm run start
309
+ ```
310
+
311
+ 默认监听地址为 `ws://0.0.0.0:1234`。客户端需要使用相同 WebSocket 地址和房间名:
312
+
313
+ ```ts
314
+ import * as Y from 'yjs'
315
+ import { WebsocketProvider } from 'y-websocket'
316
+
317
+ const ydoc = new Y.Doc()
318
+ const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'office-word-demo', ydoc)
319
+ ```
320
+
321
+ ### 实例 API
322
+
323
+ 通过 Vue `ref` 获取组件实例方法:
324
+
325
+ ```vue
326
+ <script setup lang="ts">
327
+ import { ref } from 'vue'
328
+ import { RichTextEditor } from '@norio-office/rich-text'
329
+ import type { RichTextEditorInstance } from '@norio-office/rich-text'
330
+
331
+ const editorRef = ref<RichTextEditorInstance | null>(null)
332
+ </script>
333
+
334
+ <template>
335
+ <RichTextEditor ref="editorRef" />
336
+ </template>
337
+ ```
338
+
339
+ 可用方法:
340
+
341
+ | 方法 | 返回值 | 说明 |
342
+ | --- | --- | --- |
343
+ | `exportPdf()` | `Promise<Blob \| null>` | 导出 PDF。 |
344
+ | `exportImage(options?)` | `Promise<Blob \| null>` | 导出图片,支持 PNG/JPEG。 |
345
+ | `exportHtml()` | `string \| null` | 导出 HTML 字符串。 |
346
+ | `insertImage(payload)` | `boolean` | 插入 1 到 4 张图片。 |
347
+ | `insertVideo(payload)` | `boolean` | 插入视频块。 |
348
+ | `insertFile(payload)` | `boolean` | 插入链接/文件预览块。 |
349
+ | `insertLocalFile(payload)` | `boolean` | 插入本地文件卡片。 |
350
+ | `insertMention(payload)` | `boolean` | 插入一个结构化行内提及节点。 |
351
+ | `openLocalFilePicker()` | `void` | 打开本地文件选择器。 |
352
+ | `focus()` | `void` | 聚焦编辑器。 |
353
+ | `getJSON()` | `JSONContent \| null` | 获取当前 JSON 内容。 |
354
+
355
+ ### 上传并插入
356
+
357
+ 推荐集成流程:
358
+
359
+ 1. 在业务层上传文件。
360
+ 2. 等待接口返回可访问 URL。
361
+ 3. 调用组件实例方法插入返回内容。
362
+
363
+ 插入图片:
364
+
365
+ ```ts
366
+ async function uploadImage(file: File) {
367
+ const imageUrl = await yourUploadApi(file)
368
+
369
+ editorRef.value?.insertImage({
370
+ src: imageUrl,
371
+ name: file.name,
372
+ alt: file.name,
373
+ description: '',
374
+ })
375
+ }
376
+ ```
377
+
378
+ 一次最多插入 4 张图片:
379
+
380
+ ```ts
381
+ editorRef.value?.insertImage([
382
+ { src: 'https://cdn.example.com/a.png', name: 'a.png' },
383
+ { src: 'https://cdn.example.com/b.png', name: 'b.png' },
384
+ ])
385
+ ```
386
+
387
+ 插入视频:
388
+
389
+ ```ts
390
+ async function uploadVideo(file: File) {
391
+ const videoUrl = await yourUploadApi(file)
392
+
393
+ editorRef.value?.insertVideo({
394
+ src: videoUrl,
395
+ name: file.name,
396
+ mimeType: file.type || 'video/mp4',
397
+ description: 'video description',
398
+ })
399
+ }
400
+ ```
401
+
402
+ 插入文件链接/预览块:
403
+
404
+ ```ts
405
+ async function uploadFile(file: File) {
406
+ const fileUrl = await yourUploadApi(file)
407
+
408
+ editorRef.value?.insertFile({
409
+ url: fileUrl,
410
+ name: file.name,
411
+ displayMode: 'text',
412
+ })
413
+ }
414
+ ```
415
+
416
+ 插入本地文件卡片:
417
+
418
+ ```ts
419
+ editorRef.value?.insertLocalFile({
420
+ url: 'https://cdn.example.com/files/demo.txt',
421
+ name: 'demo.txt',
422
+ size: 2048,
423
+ mimeType: 'text/plain',
424
+ })
425
+ ```
426
+
427
+ ### 导出示例
428
+
429
+ ```ts
430
+ async function handleExportPdf() {
431
+ const blob = await editorRef.value?.exportPdf()
432
+ if (!blob) return
433
+
434
+ const url = URL.createObjectURL(blob)
435
+ const link = document.createElement('a')
436
+ link.href = url
437
+ link.download = 'demo.pdf'
438
+ link.click()
439
+ URL.revokeObjectURL(url)
440
+ }
441
+
442
+ async function handleExportImage() {
443
+ const blob = await editorRef.value?.exportImage({
444
+ type: 'image/png',
445
+ })
446
+ if (!blob) return
447
+
448
+ const url = URL.createObjectURL(blob)
449
+ window.open(url, '_blank')
450
+ }
451
+
452
+ function handleExportHtml() {
453
+ const html = editorRef.value?.exportHtml()
454
+ if (!html) return
455
+
456
+ console.log(html)
457
+ }
458
+ ```
459
+
460
+ ### 类型导入
461
+
462
+ ```ts
463
+ import type {
464
+ RichTextEditorCollaborationProvider,
465
+ RichTextEditorCollaborationUser,
466
+ RichTextEditorCollaborationOptions,
467
+ RichTextEditorExportItemKey,
468
+ RichTextEditorFilePayload,
469
+ RichTextEditorImageExportOptions,
470
+ RichTextEditorImagePayload,
471
+ RichTextEditorInsertMenuItemKey,
472
+ RichTextEditorInstance,
473
+ RichTextEditorLocalFilePayload,
474
+ RichTextEditorMentionItem,
475
+ RichTextEditorMentionProvider,
476
+ RichTextEditorMentionProviderPayload,
477
+ RichTextEditorMentionType,
478
+ RichTextEditorMessages,
479
+ RichTextEditorProps,
480
+ RichTextEditorToolbarActionKey,
481
+ RichTextEditorVideoPayload,
482
+ } from '@norio-office/rich-text'
483
+ ```
484
+
485
+ ### 包内文档
486
+
487
+ - 使用指南:`docs/usage.md`
488
+ - Yjs 服务示例:`yjs/README.md`
489
+
490
+ ## English
491
+
492
+ `@norio-office/rich-text` is a reusable rich text editor component for Vue 3 and Tiptap 3. It ships with a document-style editing UI, image/video/file blocks, tables, formulas, countdown blocks, highlight blocks, outline navigation, preview mode, export APIs, and optional Yjs collaboration.
493
+
494
+ ### Installation
495
+
496
+ ```bash
497
+ npm install @norio-office/rich-text
498
+ ```
499
+
500
+ The component styles are published as a separate CSS file. Import it in your application entry:
501
+
502
+ ```ts
503
+ import '@norio-office/rich-text/style.css'
504
+ ```
505
+
506
+ If your package manager does not auto-install peer dependencies, install the runtime peer packages as well:
507
+
508
+ ```bash
509
+ npm install vue @tiptap/core @tiptap/vue-3 @tiptap/pm @tiptap/starter-kit @tiptap/extension-placeholder @tiptap/extension-code-block @tiptap/extension-code-block-lowlight @tiptap/extension-collaboration @tiptap/extension-collaboration-caret @tiptap/extension-font-family @tiptap/extension-subscript @tiptap/extension-superscript @tiptap/extension-table @tiptap/extension-table-cell @tiptap/extension-table-header @tiptap/extension-table-row @tiptap/extension-task-item @tiptap/extension-task-list @tiptap/extension-text-style @tiptap/extension-underline @tiptap/y-tiptap lowlight y-prosemirror yjs
510
+ ```
511
+
512
+ `html2canvas`, `jspdf`, `katex`, `marked`, and `plyr` are regular dependencies and are installed with the package.
513
+
514
+ ### Quick Start
515
+
516
+ ```vue
517
+ <script setup lang="ts">
518
+ import { ref } from 'vue'
519
+ import type { JSONContent } from '@tiptap/core'
520
+ import { RichTextEditor } from '@norio-office/rich-text'
521
+ import '@norio-office/rich-text/style.css'
522
+
523
+ const content = ref<JSONContent | null>(null)
524
+ </script>
525
+
526
+ <template>
527
+ <RichTextEditor v-model="content" />
528
+ </template>
529
+ ```
530
+
531
+ Default import is also supported:
532
+
533
+ ```ts
534
+ import RichTextEditor from '@norio-office/rich-text'
535
+ ```
536
+
537
+ ### Props
538
+
539
+ | Prop | Type | Default | Description |
540
+ | --- | --- | --- | --- |
541
+ | `modelValue` | `JSONContent \| null` | `null` | Editor content. Supports `v-model`. |
542
+ | `editable` | `boolean` | `true` | Enables or disables editing. |
543
+ | `mode` | `'edit' \| 'preview'` | `'edit'` | Edit mode or read-only preview mode. |
544
+ | `outlinePlacement` | `'left' \| 'right'` | `'right'` | Outline panel placement. |
545
+ | `messages` | `RichTextEditorMessages \| null` | `null` | Overrides built-in UI labels. |
546
+ | `enabledExportItems` | `RichTextEditorExportItemKey[] \| null` | `null` | Export menu whitelist. |
547
+ | `enabledInsertMenuItems` | `RichTextEditorInsertMenuItemKey[] \| null` | `null` | Insert menu whitelist. |
548
+ | `enabledToolbarActions` | `RichTextEditorToolbarActionKey[] \| null` | `null` | Toolbar action whitelist. |
549
+ | `placeholder` | `string` | `''` | Placeholder text for empty content. |
550
+ | `collaboration` | `RichTextEditorCollaborationOptions \| null` | `null` | Yjs collaboration options. |
551
+ | `mentionProvider` | `RichTextEditorMentionProvider \| null` | `null` | Compatibility function prop for loading `@` mention candidates. |
552
+ | `onMentionSearch` | `RichTextEditorMentionProvider \| null` | `null` | Async candidate loader used by the function-style `@mention-search` event. |
553
+
554
+ When whitelist props are omitted, all built-in options remain enabled. Once a list is passed, only listed items are visible and usable.
555
+
556
+ ```vue
557
+ <RichTextEditor
558
+ v-model="content"
559
+ :enabled-export-items="['html', 'image']"
560
+ :enabled-insert-menu-items="['image', 'local-file', 'blockquote']"
561
+ :enabled-toolbar-actions="['blockquote']"
562
+ />
563
+ ```
564
+
565
+ ### Available Keys
566
+
567
+ Export menu:
568
+
569
+ ```ts
570
+ type RichTextEditorExportItemKey = 'pdf' | 'html' | 'image' | 'print'
571
+ ```
572
+
573
+ Insert menu:
574
+
575
+ ```ts
576
+ type RichTextEditorInsertMenuItemKey =
577
+ | 'image'
578
+ | 'video'
579
+ | 'table'
580
+ | 'local-file'
581
+ | 'columns'
582
+ | 'highlight-block'
583
+ | 'date'
584
+ | 'code-block'
585
+ | 'formula'
586
+ | 'blockquote'
587
+ | 'emoji'
588
+ | 'link'
589
+ | 'divider'
590
+ | 'countdown'
591
+ | 'markdown-import'
592
+ ```
593
+
594
+ Toolbar:
595
+
596
+ ```ts
597
+ type RichTextEditorToolbarActionKey = 'blockquote'
598
+ ```
599
+
600
+ ### Events
601
+
602
+ | Event | Payload | Description |
603
+ | --- | --- | --- |
604
+ | `update:modelValue` | `JSONContent` | Content update event used by `v-model`. |
605
+ | `change` | `JSONContent` | Content update event. |
606
+ | `local-file-upload` | `RichTextEditorLocalFilePayload` | Emitted after a local file is picked and inserted. |
607
+ | `local-file-download` | `RichTextEditorLocalFilePayload` | Emitted when the local file card download button is clicked. |
608
+ | `mention-search` | `RichTextEditorMentionProviderPayload` | Loads candidates after the user types `@`; may return an array or a `Promise`. |
609
+ | `mention-item-click` | `RichTextEditorMentionItem` | Emitted when a popup candidate or inserted mention node is clicked. |
610
+ | `mention-submit` | `RichTextEditorMentionItem` | Emitted after the popup `提及` button inserts the selected mention. |
611
+
612
+ ### @ Mention
613
+
614
+ Typing `@` opens the mention popup. The host application provides candidates with async `@mention-search`; the reusable editor does not ship built-in people or document data. Prefer `@mention-search`; `mentionProvider` remains available as a compatibility function prop.
615
+
616
+ `type: 1` means person and `type: 2` means document. People and documents share the same shape: `id`, `name`, `type`, `avatar`, `icon`, `tag`, `updatedAt`, and related metadata. When a document has `tag`, the popup displays that label next to the title; when it is omitted, no label is shown. When a person has no avatar, the editor displays the last two characters of the name on a stable color selected from a bright 20-color pool.
617
+
618
+ ```vue
619
+ <script setup lang="ts">
620
+ import { ref } from 'vue'
621
+ import type { JSONContent } from '@tiptap/core'
622
+ import { RichTextEditor } from '@norio-office/rich-text'
623
+ import type {
624
+ RichTextEditorMentionItem,
625
+ RichTextEditorMentionProviderPayload,
626
+ } from '@norio-office/rich-text'
627
+
628
+ const content = ref<JSONContent | null>(null)
629
+
630
+ const mentionItems: RichTextEditorMentionItem[] = [
631
+ { id: 'user-1001', name: 'Cui Guoqiang', type: 1 },
632
+ { id: 'user-1002', name: 'Liu Jianing', type: 1 },
633
+ { id: 'doc-2001', name: 'IT Asset Management System', type: 2, tag: 'External', updatedAt: '2025-12-01' },
634
+ { id: 'doc-2002', name: 'Customer Satisfaction Survey', type: 2, updatedAt: '2025-12-04' },
635
+ ]
636
+
637
+ async function handleMentionSearch(payload: RichTextEditorMentionProviderPayload) {
638
+ const query = payload.query.trim().toLowerCase()
639
+ await new Promise((resolve) => window.setTimeout(resolve, 120))
640
+
641
+ return mentionItems.filter((item) => {
642
+ const matchedType = payload.type === 'all' || item.type === payload.type
643
+ const matchedKeyword = !query || item.name.toLowerCase().includes(query)
644
+ return matchedType && matchedKeyword
645
+ })
646
+ }
647
+
648
+ function handleMentionItemClick(item: RichTextEditorMentionItem) {
649
+ console.log('clicked mention item:', item)
650
+ }
651
+
652
+ function handleMentionSubmit(item: RichTextEditorMentionItem) {
653
+ console.log('inserted mention:', item)
654
+ }
655
+ </script>
656
+
657
+ <template>
658
+ <RichTextEditor
659
+ v-model="content"
660
+ @mention-search="handleMentionSearch"
661
+ @mention-item-click="handleMentionItemClick"
662
+ @mention-submit="handleMentionSubmit"
663
+ />
664
+ </template>
665
+ ```
666
+
667
+ Clicking a candidate only selects it and emits `mention-item-click`; clicking the bottom `提及` button inserts the structured inline mention and emits `mention-submit`. Closing the popup without submitting keeps the typed `@` as normal text.
668
+
669
+ ### Custom Labels
670
+
671
+ The default UI labels are Chinese. Use `messages` to override only the keys you need:
672
+
673
+ ```vue
674
+ <RichTextEditor
675
+ v-model="content"
676
+ :messages="{
677
+ 'insert.localFile': 'Attachment',
678
+ 'export.label': 'Download',
679
+ }"
680
+ />
681
+ ```
682
+
683
+ Common message keys:
684
+
685
+ | Key | Default Chinese | English meaning |
686
+ | --- | --- | --- |
687
+ | `insert.label` | 插入 | Insert |
688
+ | `insert.section.general` | 通用 | General |
689
+ | `insert.section.apps` | 小应用 | Apps |
690
+ | `insert.section.external` | 外部内容 | External content |
691
+ | `insert.image` | 图片 | Image |
692
+ | `insert.video` | 视频 | Video |
693
+ | `insert.table` | 表格 | Table |
694
+ | `insert.localFile` | 本地文件 | Local file |
695
+ | `insert.columns` | 分栏 | Columns |
696
+ | `insert.highlightBlock` | 高亮块 | Highlight block |
697
+ | `insert.date` | 日期 | Date |
698
+ | `insert.codeBlock` | 代码块 | Code block |
699
+ | `insert.formula` | 公式 | Formula |
700
+ | `insert.blockquote` | 引用 | Quote |
701
+ | `insert.emoji` | 表情符号 | Emoji |
702
+ | `insert.link` | 超链接 | Link |
703
+ | `insert.divider` | 分隔线 | Divider |
704
+ | `insert.countdown` | 倒计时 | Countdown |
705
+ | `insert.markdownImport` | Markdown 导入 | Markdown import |
706
+ | `export.label` | 导出 | Export |
707
+ | `export.pdf` | 导出 PDF | Export PDF |
708
+ | `export.pdf.loading` | 导出 PDF 中... | Exporting PDF... |
709
+ | `export.html` | 导出 HTML | Export HTML |
710
+ | `export.html.loading` | 导出 HTML 中... | Exporting HTML... |
711
+ | `export.image` | 导出图片 | Export image |
712
+ | `export.image.loading` | 导出图片中... | Exporting image... |
713
+ | `print.label` | 打印 | Print |
714
+ | `print.loading` | 打印中... | Printing... |
715
+ | `quote.apply` | 应用引用 | Apply quote |
716
+ | `quote.cancel` | 取消引用 | Cancel quote |
717
+ | `quote.borderColor` | 边框颜色 | Border color |
718
+ | `quote.backgroundColor` | 背景颜色 | Background color |
719
+ | `outline.label` | 大纲 | Outline |
720
+ | `outline.collapse` | 收起大纲 | Collapse outline |
721
+ | `outline.empty.description` | 对文档内容应用“标题”样式,即可自动生成大纲。 | Apply heading styles to generate an outline. |
722
+ | `outline.empty.tip` | 点击左下角“大纲”按钮可以随时展开或收起。 | Use the Outline button to show or hide the outline. |
723
+ | `status.wordCountUnit` | 个字 | characters |
724
+ | `status.presentation.enter` | 演示 | Present |
725
+ | `status.presentation.exit` | 退出演示 | Exit presentation |
726
+ | `status.fullscreen.enter` | 全屏 | Fullscreen |
727
+ | `status.fullscreen.exit` | 退出全屏 | Exit fullscreen |
728
+ | `countdown.selectTime` | 请选择时间 | Select time |
729
+ | `countdown.settingsTitle` | 倒计时设置 | Countdown settings |
730
+ | `formula.insertTitle` | 插入 LaTeX 公式 | Insert LaTeX formula |
731
+
732
+ ### Preview Mode
733
+
734
+ Use `mode="preview"` to switch the component into a read-only preview shell. In preview mode, the toolbar is hidden, editing is disabled, and the outline can be placed on either side.
735
+
736
+ ```vue
737
+ <RichTextEditor
738
+ v-model="content"
739
+ mode="preview"
740
+ outline-placement="right"
741
+ />
742
+ ```
743
+
744
+ On narrow screens, preview mode automatically scales the page canvas so the document stays readable on phones.
745
+
746
+ ### Collaboration
747
+
748
+ The component supports both normal single-user editing and optional Yjs collaboration.
749
+
750
+ ```vue
751
+ <script setup lang="ts">
752
+ import { ref } from 'vue'
753
+ import * as Y from 'yjs'
754
+ import { WebsocketProvider } from 'y-websocket'
755
+ import type { JSONContent } from '@tiptap/core'
756
+ import { RichTextEditor } from '@norio-office/rich-text'
757
+ import '@norio-office/rich-text/style.css'
758
+
759
+ const ydoc = new Y.Doc()
760
+ const provider = new WebsocketProvider('ws://localhost:1234', 'office-word-demo', ydoc)
761
+ const content = ref<JSONContent | null>(null)
762
+ </script>
763
+
764
+ <template>
765
+ <RichTextEditor
766
+ v-model="content"
767
+ :collaboration="{
768
+ document: ydoc,
769
+ field: 'content',
770
+ provider,
771
+ user: {
772
+ name: 'Zhang San',
773
+ color: '#3b82f6',
774
+ },
775
+ }"
776
+ />
777
+ </template>
778
+ ```
779
+
780
+ In collaboration mode, the shared Yjs fragment becomes the source of truth. The component still emits JSON updates, but external `modelValue` changes are not pushed back into the editor. If you want remote cursors, install `y-websocket` in the host project and pass `provider` plus `user`.
781
+
782
+ Do not use `modelValue` as the initial seed source for a collaborative room. Seed the Yjs document before mounting the editor if the room needs default content.
783
+
784
+ ### Bundled Yjs Server Example
785
+
786
+ The published package includes a minimal Yjs WebSocket server example under `yjs/`.
787
+
788
+ ```bash
789
+ cd node_modules/@norio-office/rich-text/yjs
790
+ npm install
791
+ npm run start
792
+ ```
793
+
794
+ The default address is `ws://0.0.0.0:1234`. Use the same WebSocket address and room name on every client:
795
+
796
+ ```ts
797
+ import * as Y from 'yjs'
798
+ import { WebsocketProvider } from 'y-websocket'
799
+
800
+ const ydoc = new Y.Doc()
801
+ const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'office-word-demo', ydoc)
802
+ ```
803
+
804
+ ### Instance API
805
+
806
+ Use a Vue `ref` to access the editor instance methods:
807
+
808
+ ```vue
809
+ <script setup lang="ts">
810
+ import { ref } from 'vue'
811
+ import { RichTextEditor } from '@norio-office/rich-text'
812
+ import type { RichTextEditorInstance } from '@norio-office/rich-text'
813
+
814
+ const editorRef = ref<RichTextEditorInstance | null>(null)
815
+ </script>
816
+
817
+ <template>
818
+ <RichTextEditor ref="editorRef" />
819
+ </template>
820
+ ```
821
+
822
+ Available methods:
823
+
824
+ | Method | Return | Description |
825
+ | --- | --- | --- |
826
+ | `exportPdf()` | `Promise<Blob \| null>` | Exports a PDF. |
827
+ | `exportImage(options?)` | `Promise<Blob \| null>` | Exports an image as PNG or JPEG. |
828
+ | `exportHtml()` | `string \| null` | Exports an HTML string. |
829
+ | `insertImage(payload)` | `boolean` | Inserts 1 to 4 images. |
830
+ | `insertVideo(payload)` | `boolean` | Inserts a video block. |
831
+ | `insertFile(payload)` | `boolean` | Inserts a link/file preview block. |
832
+ | `insertLocalFile(payload)` | `boolean` | Inserts a local file card. |
833
+ | `insertMention(payload)` | `boolean` | Inserts one structured inline mention node. |
834
+ | `openLocalFilePicker()` | `void` | Opens the local file picker. |
835
+ | `focus()` | `void` | Focuses the editor. |
836
+ | `getJSON()` | `JSONContent \| null` | Returns the current JSON document. |
837
+
838
+ ### Upload And Insert
839
+
840
+ Recommended integration flow:
841
+
842
+ 1. Upload the asset from your own business layer.
843
+ 2. Wait for the API to return the final accessible URL.
844
+ 3. Call the editor instance method to insert the returned content.
845
+
846
+ Insert image:
847
+
848
+ ```ts
849
+ async function uploadImage(file: File) {
850
+ const imageUrl = await yourUploadApi(file)
851
+
852
+ editorRef.value?.insertImage({
853
+ src: imageUrl,
854
+ name: file.name,
855
+ alt: file.name,
856
+ description: '',
857
+ })
858
+ }
859
+ ```
860
+
861
+ You can insert up to 4 images in one call:
862
+
863
+ ```ts
864
+ editorRef.value?.insertImage([
865
+ { src: 'https://cdn.example.com/a.png', name: 'a.png' },
866
+ { src: 'https://cdn.example.com/b.png', name: 'b.png' },
867
+ ])
868
+ ```
869
+
870
+ Insert video:
871
+
872
+ ```ts
873
+ async function uploadVideo(file: File) {
874
+ const videoUrl = await yourUploadApi(file)
875
+
876
+ editorRef.value?.insertVideo({
877
+ src: videoUrl,
878
+ name: file.name,
879
+ mimeType: file.type || 'video/mp4',
880
+ description: 'video description',
881
+ })
882
+ }
883
+ ```
884
+
885
+ Insert file link or preview:
886
+
887
+ ```ts
888
+ async function uploadFile(file: File) {
889
+ const fileUrl = await yourUploadApi(file)
890
+
891
+ editorRef.value?.insertFile({
892
+ url: fileUrl,
893
+ name: file.name,
894
+ displayMode: 'text',
895
+ })
896
+ }
897
+ ```
898
+
899
+ Insert a local file card:
900
+
901
+ ```ts
902
+ editorRef.value?.insertLocalFile({
903
+ url: 'https://cdn.example.com/files/demo.txt',
904
+ name: 'demo.txt',
905
+ size: 2048,
906
+ mimeType: 'text/plain',
907
+ })
908
+ ```
909
+
910
+ ### Export Example
911
+
912
+ ```ts
913
+ async function handleExportPdf() {
914
+ const blob = await editorRef.value?.exportPdf()
915
+ if (!blob) return
916
+
917
+ const url = URL.createObjectURL(blob)
918
+ const link = document.createElement('a')
919
+ link.href = url
920
+ link.download = 'demo.pdf'
921
+ link.click()
922
+ URL.revokeObjectURL(url)
923
+ }
924
+
925
+ async function handleExportImage() {
926
+ const blob = await editorRef.value?.exportImage({
927
+ type: 'image/png',
928
+ })
929
+ if (!blob) return
930
+
931
+ const url = URL.createObjectURL(blob)
932
+ window.open(url, '_blank')
933
+ }
934
+
935
+ function handleExportHtml() {
936
+ const html = editorRef.value?.exportHtml()
937
+ if (!html) return
938
+
939
+ console.log(html)
940
+ }
941
+ ```
942
+
943
+ ### Type Imports
944
+
945
+ ```ts
946
+ import type {
947
+ RichTextEditorCollaborationProvider,
948
+ RichTextEditorCollaborationUser,
949
+ RichTextEditorCollaborationOptions,
950
+ RichTextEditorExportItemKey,
951
+ RichTextEditorFilePayload,
952
+ RichTextEditorImageExportOptions,
953
+ RichTextEditorImagePayload,
954
+ RichTextEditorInsertMenuItemKey,
955
+ RichTextEditorInstance,
956
+ RichTextEditorLocalFilePayload,
957
+ RichTextEditorMentionItem,
958
+ RichTextEditorMentionProvider,
959
+ RichTextEditorMentionProviderPayload,
960
+ RichTextEditorMentionType,
961
+ RichTextEditorMessages,
962
+ RichTextEditorProps,
963
+ RichTextEditorToolbarActionKey,
964
+ RichTextEditorVideoPayload,
965
+ } from '@norio-office/rich-text'
966
+ ```
967
+
968
+ ### Package Docs
969
+
970
+ - Usage guide: `docs/usage.md`
971
+ - Bundled Yjs server example: `yjs/README.md`