@norio-office/rich-text 0.3.0 → 0.6.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.
- package/README.md +610 -4
- package/dist/components/RichTextEditor.vue.d.ts +32 -1
- package/dist/composables/useRichTextEditor.d.ts +2 -1
- package/dist/extensions/comment-mark.d.ts +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6979 -5789
- package/dist/index.umd.cjs +964 -479
- package/dist/style.css +1 -1
- package/dist/types.d.ts +120 -0
- package/docs/usage.md +32 -1
- package/package.json +8 -2
- package/yjs/README.md +63 -0
- package/yjs/index.js +224 -0
- package/yjs/package.json +14 -0
- package/dist/index.es-D5dDjrL4.js +0 -5751
- package/dist/jspdf.es.min-B2a2-__Q.js +0 -18214
- package/dist/purify.es-jA1o6Tcu.js +0 -597
- package/dist/typeof-Dz1Ap89p.js +0 -11
- /package/dist/{html2canvas-CmV4ppBE.js → html2canvas-4hiP1s1t.js} +0 -0
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
## 中文
|
|
8
8
|
|
|
9
|
-
`@norio-office/rich-text` 是一个基于 Vue 3 和 Tiptap 3
|
|
9
|
+
`@norio-office/rich-text` 是一个基于 Vue 3 和 Tiptap 3 的富文本编辑器组件,内置文档式编辑界面、图片/视频/文件块、表格、公式、倒计时、高亮块、大纲、预览模式、导出能力,以及可选的 Yjs 协同编辑。
|
|
10
10
|
|
|
11
11
|
### 安装
|
|
12
12
|
|
|
@@ -23,7 +23,7 @@ import '@norio-office/rich-text/style.css'
|
|
|
23
23
|
如果你的包管理器没有自动安装 peer dependencies,请同时安装运行时 peer 包:
|
|
24
24
|
|
|
25
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-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 lowlight
|
|
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
27
|
```
|
|
28
28
|
|
|
29
29
|
`html2canvas`、`jspdf`、`katex`、`marked`、`plyr` 已作为普通 dependencies 随包安装。
|
|
@@ -68,6 +68,7 @@ import RichTextEditor from '@norio-office/rich-text'
|
|
|
68
68
|
| `enabledToolbarActions` | `RichTextEditorToolbarActionKey[] \| null` | `null` | 工具栏能力白名单。 |
|
|
69
69
|
| `placeholder` | `string` | `''` | 空内容占位文案。 |
|
|
70
70
|
| `mention` | `boolean` | `false` | 是否启用 `@` 提及功能;只有设为 `true` 时输入 `@` 才会打开候选面板,`insertMention()` 才会插入提及节点。 |
|
|
71
|
+
| `collaboration` | `RichTextEditorCollaborationOptions \| null` | `null` | Yjs 协同配置。 |
|
|
71
72
|
| `uploadImage` | `RichTextEditorUploadHook \| null` | `null` | 内置图片选择、图片块上传时调用;返回最终可访问 URL 后组件插入图片。 |
|
|
72
73
|
| `uploadVideo` | `RichTextEditorUploadHook \| null` | `null` | 内置视频选择、视频块上传时调用;返回最终可访问 URL 后组件插入视频。 |
|
|
73
74
|
| `uploadFile` | `RichTextEditorUploadHook \| null` | `null` | 本地文件选择器上传时调用;返回最终可访问 URL 后组件插入文件卡片。 |
|
|
@@ -299,6 +300,164 @@ function handleMentionSubmit(item: RichTextEditorMentionItem) {
|
|
|
299
300
|
|
|
300
301
|
窄屏下预览模式会自动缩放页面画布,让文档在手机上保持可读。
|
|
301
302
|
|
|
303
|
+
### 协同编辑
|
|
304
|
+
|
|
305
|
+
推荐使用统一协同服务接入当前富文本组件。当前富文本组件的类型是 `rich`,不要把它作为未来 Word 组件的 `documentType=word` 接入。
|
|
306
|
+
|
|
307
|
+
```bash
|
|
308
|
+
npm install yjs y-websocket
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
最小接入示例:
|
|
312
|
+
|
|
313
|
+
```vue
|
|
314
|
+
<script setup lang="ts">
|
|
315
|
+
import { computed, ref } from 'vue'
|
|
316
|
+
import * as Y from 'yjs'
|
|
317
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
318
|
+
import type { JSONContent } from '@tiptap/core'
|
|
319
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
320
|
+
import '@norio-office/rich-text/style.css'
|
|
321
|
+
|
|
322
|
+
const fileId = 'rich-demo-file-001'
|
|
323
|
+
const content = ref<JSONContent | null>(null)
|
|
324
|
+
const ydoc = new Y.Doc()
|
|
325
|
+
|
|
326
|
+
const user = {
|
|
327
|
+
userId: 'user_001',
|
|
328
|
+
userName: 'Alice',
|
|
329
|
+
clientUniqueCode: 'browser-tab-001',
|
|
330
|
+
color: '#3b82f6',
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const provider = new WebsocketProvider(
|
|
334
|
+
'ws://127.0.0.1:1234/collaboration',
|
|
335
|
+
fileId,
|
|
336
|
+
ydoc,
|
|
337
|
+
{
|
|
338
|
+
params: {
|
|
339
|
+
roomId: fileId,
|
|
340
|
+
token: 'dev-token',
|
|
341
|
+
userId: user.userId,
|
|
342
|
+
userName: user.userName,
|
|
343
|
+
clientType: 'web',
|
|
344
|
+
clientUniqueCode: user.clientUniqueCode,
|
|
345
|
+
documentType: 'rich',
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
const collaboration = computed(() => ({
|
|
351
|
+
document: ydoc,
|
|
352
|
+
field: 'content',
|
|
353
|
+
provider,
|
|
354
|
+
user: {
|
|
355
|
+
name: user.userName,
|
|
356
|
+
color: user.color,
|
|
357
|
+
userId: user.userId,
|
|
358
|
+
clientUniqueCode: user.clientUniqueCode,
|
|
359
|
+
},
|
|
360
|
+
}))
|
|
361
|
+
</script>
|
|
362
|
+
|
|
363
|
+
<template>
|
|
364
|
+
<RichTextEditor
|
|
365
|
+
v-model="content"
|
|
366
|
+
:collaboration="collaboration"
|
|
367
|
+
/>
|
|
368
|
+
</template>
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
关键规则:
|
|
372
|
+
|
|
373
|
+
- `WebsocketProvider` 的 room name 和 query 里的 `roomId` 建议保持同一个值。
|
|
374
|
+
- 同一个协同房间里的所有客户端必须使用同一个 `field`,普通一文档一编辑器场景固定用 `content`。
|
|
375
|
+
- 同一个用户开两个浏览器窗口时,`clientUniqueCode` 必须不同;后端不会按 `userId` 去重在线客户端。
|
|
376
|
+
- `provider + user` 都传入时,组件才会显示远程光标和远程选区。
|
|
377
|
+
- 协同模式下 Y.Doc/Yjs fragment 是内容事实来源,外部 `modelValue` 不会再作为远程内容的权威写入源。
|
|
378
|
+
|
|
379
|
+
初始化规则:
|
|
380
|
+
|
|
381
|
+
- 默认情况下,组件不会把 `modelValue` 写入空的协同 fragment。
|
|
382
|
+
- 如果后端已有 Yjs 快照,必须以快照为准,不要传 `initializeContent: true`。
|
|
383
|
+
- 如果是全新的空房间,宿主业务必须先确认当前客户端拥有初始化权,再传 `initializeContent: true` 和 `initialContent`。
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
const collaboration = computed(() => ({
|
|
387
|
+
document: ydoc,
|
|
388
|
+
field: 'content',
|
|
389
|
+
provider,
|
|
390
|
+
user,
|
|
391
|
+
initializeContent: ownsInitialContent,
|
|
392
|
+
initialContent: ownsInitialContent ? businessContent : null,
|
|
393
|
+
}))
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
两窗口测试时,保持相同 `roomId`,但使用不同 `userId` 和 `clientUniqueCode`。只有第一个窗口应该带初始化权:
|
|
397
|
+
|
|
398
|
+
```text
|
|
399
|
+
http://127.0.0.1:5177/?collab=1&room=rich-demo-file-001&server=ws://127.0.0.1:1234/collaboration&token=dev-token&userId=user_001&name=Alice&clientUniqueCode=browser-tab-001&init=1
|
|
400
|
+
|
|
401
|
+
http://127.0.0.1:5177/?collab=1&room=rich-demo-file-001&server=ws://127.0.0.1:1234/collaboration&token=dev-token&userId=user_002&name=Bob&clientUniqueCode=browser-tab-002
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
> 本包内置的 `yjs/` 服务示例只适合快速本地验证。生产或完整联调建议使用统一协同后端,例如 `ws://127.0.0.1:1234/collaboration`。
|
|
405
|
+
|
|
406
|
+
组件支持普通单人编辑,也支持可选的 Yjs 协同编辑。
|
|
407
|
+
|
|
408
|
+
```vue
|
|
409
|
+
<script setup lang="ts">
|
|
410
|
+
import { ref } from 'vue'
|
|
411
|
+
import * as Y from 'yjs'
|
|
412
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
413
|
+
import type { JSONContent } from '@tiptap/core'
|
|
414
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
415
|
+
import '@norio-office/rich-text/style.css'
|
|
416
|
+
|
|
417
|
+
const ydoc = new Y.Doc()
|
|
418
|
+
const provider = new WebsocketProvider('ws://localhost:1234', 'office-word-demo', ydoc)
|
|
419
|
+
const content = ref<JSONContent | null>(null)
|
|
420
|
+
</script>
|
|
421
|
+
|
|
422
|
+
<template>
|
|
423
|
+
<RichTextEditor
|
|
424
|
+
v-model="content"
|
|
425
|
+
:collaboration="{
|
|
426
|
+
document: ydoc,
|
|
427
|
+
field: 'content',
|
|
428
|
+
provider,
|
|
429
|
+
user: {
|
|
430
|
+
name: '张三',
|
|
431
|
+
color: '#3b82f6',
|
|
432
|
+
},
|
|
433
|
+
}"
|
|
434
|
+
/>
|
|
435
|
+
</template>
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
协同模式下,Yjs fragment 是内容的事实来源。组件仍会 emit JSON 更新,但外部 `modelValue` 的变化不会再反向写入编辑器。如果需要远程光标,请在宿主项目安装 `y-websocket`,并传入 `provider` 和 `user`。
|
|
439
|
+
|
|
440
|
+
不要把 `modelValue` 当作协同房间的初始化种子。默认情况下,组件不会把 `modelValue` 写入空的协同 fragment;如果全新房间需要默认内容,请先确认当前客户端拥有初始化权,再在 `collaboration` 里传入 `initializeContent: true` 和 `initialContent`。如果后端已经有 Yjs 快照,不要传初始化开关。
|
|
441
|
+
|
|
442
|
+
### 内置 Yjs 服务示例
|
|
443
|
+
|
|
444
|
+
发布包内包含一个最小 Yjs WebSocket 服务示例:
|
|
445
|
+
|
|
446
|
+
```bash
|
|
447
|
+
cd node_modules/@norio-office/rich-text/yjs
|
|
448
|
+
npm install
|
|
449
|
+
npm run start
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
默认监听地址为 `ws://0.0.0.0:1234`。客户端需要使用相同 WebSocket 地址和房间名:
|
|
453
|
+
|
|
454
|
+
```ts
|
|
455
|
+
import * as Y from 'yjs'
|
|
456
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
457
|
+
|
|
458
|
+
const ydoc = new Y.Doc()
|
|
459
|
+
const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'office-word-demo', ydoc)
|
|
460
|
+
```
|
|
302
461
|
|
|
303
462
|
### 实例 API
|
|
304
463
|
|
|
@@ -525,6 +684,11 @@ import type {
|
|
|
525
684
|
OfficeColorIconProps,
|
|
526
685
|
OfficeIconProps,
|
|
527
686
|
RichTextEditorAlign,
|
|
687
|
+
RichTextEditorCollaborationAwareness,
|
|
688
|
+
RichTextEditorCollaborationDocument,
|
|
689
|
+
RichTextEditorCollaborationProvider,
|
|
690
|
+
RichTextEditorCollaborationUser,
|
|
691
|
+
RichTextEditorCollaborationOptions,
|
|
528
692
|
RichTextEditorCollectedFilePayload,
|
|
529
693
|
RichTextEditorExportItemKey,
|
|
530
694
|
RichTextEditorFilePayload,
|
|
@@ -571,10 +735,11 @@ import RichTextEditor, {
|
|
|
571
735
|
### 包内文档
|
|
572
736
|
|
|
573
737
|
- 使用指南:`docs/usage.md`
|
|
738
|
+
- Yjs 服务示例:`yjs/README.md`
|
|
574
739
|
|
|
575
740
|
## English
|
|
576
741
|
|
|
577
|
-
`@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.
|
|
742
|
+
`@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.
|
|
578
743
|
|
|
579
744
|
### Installation
|
|
580
745
|
|
|
@@ -591,7 +756,7 @@ import '@norio-office/rich-text/style.css'
|
|
|
591
756
|
If your package manager does not auto-install peer dependencies, install the runtime peer packages as well:
|
|
592
757
|
|
|
593
758
|
```bash
|
|
594
|
-
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-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 lowlight
|
|
759
|
+
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
|
|
595
760
|
```
|
|
596
761
|
|
|
597
762
|
`html2canvas`, `jspdf`, `katex`, `marked`, and `plyr` are regular dependencies and are installed with the package.
|
|
@@ -636,6 +801,7 @@ import RichTextEditor from '@norio-office/rich-text'
|
|
|
636
801
|
| `enabledToolbarActions` | `RichTextEditorToolbarActionKey[] \| null` | `null` | Toolbar action whitelist. |
|
|
637
802
|
| `placeholder` | `string` | `''` | Placeholder text for empty content. |
|
|
638
803
|
| `mention` | `boolean` | `false` | Enables `@` mention. Only when `true`, typing `@` opens the candidate popup and `insertMention()` can insert a mention node. |
|
|
804
|
+
| `collaboration` | `RichTextEditorCollaborationOptions \| null` | `null` | Yjs collaboration options. |
|
|
639
805
|
| `uploadImage` | `RichTextEditorUploadHook \| null` | `null` | Upload hook required by built-in image pickers and image drops. The editor inserts the returned URL. |
|
|
640
806
|
| `uploadVideo` | `RichTextEditorUploadHook \| null` | `null` | Upload hook required by built-in video pickers and video drops. The editor inserts the returned URL. |
|
|
641
807
|
| `uploadFile` | `RichTextEditorUploadHook \| null` | `null` | Upload hook required by the local file card picker. The editor inserts the returned URL. |
|
|
@@ -867,6 +1033,164 @@ Use `mode="preview"` to switch the component into a read-only preview shell. In
|
|
|
867
1033
|
|
|
868
1034
|
On narrow screens, preview mode automatically scales the page canvas so the document stays readable on phones.
|
|
869
1035
|
|
|
1036
|
+
### Collaboration
|
|
1037
|
+
|
|
1038
|
+
Use the unified collaboration service for the current rich text editor. The current component type is `rich`; `word` is reserved for a future dedicated Word component.
|
|
1039
|
+
|
|
1040
|
+
```bash
|
|
1041
|
+
npm install yjs y-websocket
|
|
1042
|
+
```
|
|
1043
|
+
|
|
1044
|
+
Minimal integration:
|
|
1045
|
+
|
|
1046
|
+
```vue
|
|
1047
|
+
<script setup lang="ts">
|
|
1048
|
+
import { computed, ref } from 'vue'
|
|
1049
|
+
import * as Y from 'yjs'
|
|
1050
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
1051
|
+
import type { JSONContent } from '@tiptap/core'
|
|
1052
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
1053
|
+
import '@norio-office/rich-text/style.css'
|
|
1054
|
+
|
|
1055
|
+
const fileId = 'rich-demo-file-001'
|
|
1056
|
+
const content = ref<JSONContent | null>(null)
|
|
1057
|
+
const ydoc = new Y.Doc()
|
|
1058
|
+
|
|
1059
|
+
const user = {
|
|
1060
|
+
userId: 'user_001',
|
|
1061
|
+
userName: 'Alice',
|
|
1062
|
+
clientUniqueCode: 'browser-tab-001',
|
|
1063
|
+
color: '#3b82f6',
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
const provider = new WebsocketProvider(
|
|
1067
|
+
'ws://127.0.0.1:1234/collaboration',
|
|
1068
|
+
fileId,
|
|
1069
|
+
ydoc,
|
|
1070
|
+
{
|
|
1071
|
+
params: {
|
|
1072
|
+
roomId: fileId,
|
|
1073
|
+
token: 'dev-token',
|
|
1074
|
+
userId: user.userId,
|
|
1075
|
+
userName: user.userName,
|
|
1076
|
+
clientType: 'web',
|
|
1077
|
+
clientUniqueCode: user.clientUniqueCode,
|
|
1078
|
+
documentType: 'rich',
|
|
1079
|
+
},
|
|
1080
|
+
},
|
|
1081
|
+
)
|
|
1082
|
+
|
|
1083
|
+
const collaboration = computed(() => ({
|
|
1084
|
+
document: ydoc,
|
|
1085
|
+
field: 'content',
|
|
1086
|
+
provider,
|
|
1087
|
+
user: {
|
|
1088
|
+
name: user.userName,
|
|
1089
|
+
color: user.color,
|
|
1090
|
+
userId: user.userId,
|
|
1091
|
+
clientUniqueCode: user.clientUniqueCode,
|
|
1092
|
+
},
|
|
1093
|
+
}))
|
|
1094
|
+
</script>
|
|
1095
|
+
|
|
1096
|
+
<template>
|
|
1097
|
+
<RichTextEditor
|
|
1098
|
+
v-model="content"
|
|
1099
|
+
:collaboration="collaboration"
|
|
1100
|
+
/>
|
|
1101
|
+
</template>
|
|
1102
|
+
```
|
|
1103
|
+
|
|
1104
|
+
Rules:
|
|
1105
|
+
|
|
1106
|
+
- Keep the `WebsocketProvider` room name and query `roomId` the same.
|
|
1107
|
+
- All clients in the same room must use the same `field`; for normal one-file-one-editor use, keep it as `content`.
|
|
1108
|
+
- If the same user opens two browser windows, each window must use a different `clientUniqueCode`.
|
|
1109
|
+
- Remote cursors and selections require both `provider` and `user`.
|
|
1110
|
+
- In collaboration mode, the Y.Doc/Yjs fragment is authoritative. External `modelValue` is only an emitted snapshot and must not continuously drive remote collaborative content.
|
|
1111
|
+
|
|
1112
|
+
Initialization:
|
|
1113
|
+
|
|
1114
|
+
- By default, the editor does not write `modelValue` into an empty collaborative fragment.
|
|
1115
|
+
- If the backend already has a Yjs snapshot, that snapshot must win. Do not pass `initializeContent: true`.
|
|
1116
|
+
- For a brand-new empty room, the host application must first confirm that this client owns initialization, then pass `initializeContent: true` and `initialContent`.
|
|
1117
|
+
|
|
1118
|
+
```ts
|
|
1119
|
+
const collaboration = computed(() => ({
|
|
1120
|
+
document: ydoc,
|
|
1121
|
+
field: 'content',
|
|
1122
|
+
provider,
|
|
1123
|
+
user,
|
|
1124
|
+
initializeContent: ownsInitialContent,
|
|
1125
|
+
initialContent: ownsInitialContent ? businessContent : null,
|
|
1126
|
+
}))
|
|
1127
|
+
```
|
|
1128
|
+
|
|
1129
|
+
For two-window testing, use the same `roomId`, different `userId` values, and different `clientUniqueCode` values. Only the first window should own initialization:
|
|
1130
|
+
|
|
1131
|
+
```text
|
|
1132
|
+
http://127.0.0.1:5177/?collab=1&room=rich-demo-file-001&server=ws://127.0.0.1:1234/collaboration&token=dev-token&userId=user_001&name=Alice&clientUniqueCode=browser-tab-001&init=1
|
|
1133
|
+
|
|
1134
|
+
http://127.0.0.1:5177/?collab=1&room=rich-demo-file-001&server=ws://127.0.0.1:1234/collaboration&token=dev-token&userId=user_002&name=Bob&clientUniqueCode=browser-tab-002
|
|
1135
|
+
```
|
|
1136
|
+
|
|
1137
|
+
The bundled `yjs/` server is useful for quick local checks. For full integration, prefer the unified collaboration backend, for example `ws://127.0.0.1:1234/collaboration`.
|
|
1138
|
+
|
|
1139
|
+
The component supports both normal single-user editing and optional Yjs collaboration.
|
|
1140
|
+
|
|
1141
|
+
```vue
|
|
1142
|
+
<script setup lang="ts">
|
|
1143
|
+
import { ref } from 'vue'
|
|
1144
|
+
import * as Y from 'yjs'
|
|
1145
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
1146
|
+
import type { JSONContent } from '@tiptap/core'
|
|
1147
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
1148
|
+
import '@norio-office/rich-text/style.css'
|
|
1149
|
+
|
|
1150
|
+
const ydoc = new Y.Doc()
|
|
1151
|
+
const provider = new WebsocketProvider('ws://localhost:1234', 'office-word-demo', ydoc)
|
|
1152
|
+
const content = ref<JSONContent | null>(null)
|
|
1153
|
+
</script>
|
|
1154
|
+
|
|
1155
|
+
<template>
|
|
1156
|
+
<RichTextEditor
|
|
1157
|
+
v-model="content"
|
|
1158
|
+
:collaboration="{
|
|
1159
|
+
document: ydoc,
|
|
1160
|
+
field: 'content',
|
|
1161
|
+
provider,
|
|
1162
|
+
user: {
|
|
1163
|
+
name: 'Zhang San',
|
|
1164
|
+
color: '#3b82f6',
|
|
1165
|
+
},
|
|
1166
|
+
}"
|
|
1167
|
+
/>
|
|
1168
|
+
</template>
|
|
1169
|
+
```
|
|
1170
|
+
|
|
1171
|
+
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`.
|
|
1172
|
+
|
|
1173
|
+
Do not use `modelValue` as the initial seed source for a collaborative room. The editor does not write `modelValue` into an empty collaborative fragment by default. If a brand-new room needs initial business content, first confirm this client owns initialization, then pass `initializeContent: true` and `initialContent` inside `collaboration`. Do not set those flags when a backend Yjs snapshot already exists.
|
|
1174
|
+
|
|
1175
|
+
### Bundled Yjs Server Example
|
|
1176
|
+
|
|
1177
|
+
The published package includes a minimal Yjs WebSocket server example under `yjs/`.
|
|
1178
|
+
|
|
1179
|
+
```bash
|
|
1180
|
+
cd node_modules/@norio-office/rich-text/yjs
|
|
1181
|
+
npm install
|
|
1182
|
+
npm run start
|
|
1183
|
+
```
|
|
1184
|
+
|
|
1185
|
+
The default address is `ws://0.0.0.0:1234`. Use the same WebSocket address and room name on every client:
|
|
1186
|
+
|
|
1187
|
+
```ts
|
|
1188
|
+
import * as Y from 'yjs'
|
|
1189
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
1190
|
+
|
|
1191
|
+
const ydoc = new Y.Doc()
|
|
1192
|
+
const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'office-word-demo', ydoc)
|
|
1193
|
+
```
|
|
870
1194
|
|
|
871
1195
|
### Instance API
|
|
872
1196
|
|
|
@@ -1099,6 +1423,11 @@ import type {
|
|
|
1099
1423
|
OfficeColorIconProps,
|
|
1100
1424
|
OfficeIconProps,
|
|
1101
1425
|
RichTextEditorAlign,
|
|
1426
|
+
RichTextEditorCollaborationAwareness,
|
|
1427
|
+
RichTextEditorCollaborationDocument,
|
|
1428
|
+
RichTextEditorCollaborationProvider,
|
|
1429
|
+
RichTextEditorCollaborationUser,
|
|
1430
|
+
RichTextEditorCollaborationOptions,
|
|
1102
1431
|
RichTextEditorCollectedFilePayload,
|
|
1103
1432
|
RichTextEditorExportItemKey,
|
|
1104
1433
|
RichTextEditorFilePayload,
|
|
@@ -1145,3 +1474,280 @@ import RichTextEditor, {
|
|
|
1145
1474
|
### Package Docs
|
|
1146
1475
|
|
|
1147
1476
|
- Usage guide: `docs/usage.md`
|
|
1477
|
+
- Bundled Yjs server example: `yjs/README.md`
|
|
1478
|
+
|
|
1479
|
+
## 评论功能对接
|
|
1480
|
+
|
|
1481
|
+
评论功能由组件负责右侧栏 UI、正文锚点标注、回复交互、图片预览和评论输入框里的 `@` 弹层;评论数据本身由业务系统保存。对接时可以把它理解成:组件抛事件,业务保存数据,然后把新的 `comments` 数组再传回组件。
|
|
1482
|
+
|
|
1483
|
+
下面是一个完整示例,包含:
|
|
1484
|
+
|
|
1485
|
+
- 当前评论用户假数据
|
|
1486
|
+
- 已有评论线程假数据
|
|
1487
|
+
- 评论图片上传 mock
|
|
1488
|
+
- 评论里的 `@用户` / `@文档` 查询假数据
|
|
1489
|
+
- 新建、回复、编辑、删除、完成评论的本地更新逻辑
|
|
1490
|
+
- `comment-submit` 统一通知事件,用来拿到“评论了谁、@了谁、评论内容”等信息
|
|
1491
|
+
|
|
1492
|
+
```vue
|
|
1493
|
+
<script setup lang="ts">
|
|
1494
|
+
import { ref } from 'vue'
|
|
1495
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
1496
|
+
import type {
|
|
1497
|
+
RichTextEditorCommentCreatePayload,
|
|
1498
|
+
RichTextEditorCommentDeletePayload,
|
|
1499
|
+
RichTextEditorCommentMentionProviderPayload,
|
|
1500
|
+
RichTextEditorCommentReplyPayload,
|
|
1501
|
+
RichTextEditorCommentResolvePayload,
|
|
1502
|
+
RichTextEditorCommentSubmitPayload,
|
|
1503
|
+
RichTextEditorCommentThread,
|
|
1504
|
+
RichTextEditorCommentUpdatePayload,
|
|
1505
|
+
RichTextEditorCommentUser,
|
|
1506
|
+
RichTextEditorMentionItem,
|
|
1507
|
+
RichTextEditorUploadInput,
|
|
1508
|
+
RichTextEditorUploadResult,
|
|
1509
|
+
} from '@norio-office/rich-text'
|
|
1510
|
+
import '@norio-office/rich-text/style.css'
|
|
1511
|
+
|
|
1512
|
+
const content = ref({
|
|
1513
|
+
type: 'doc',
|
|
1514
|
+
content: [
|
|
1515
|
+
{
|
|
1516
|
+
type: 'heading',
|
|
1517
|
+
attrs: { level: 1 },
|
|
1518
|
+
content: [{ type: 'text', text: '评论对接示例文档' }],
|
|
1519
|
+
},
|
|
1520
|
+
{
|
|
1521
|
+
type: 'paragraph',
|
|
1522
|
+
content: [{ type: 'text', text: '选中这段文字后点击工具栏评论按钮,就可以新建评论。' }],
|
|
1523
|
+
},
|
|
1524
|
+
{
|
|
1525
|
+
type: 'paragraph',
|
|
1526
|
+
content: [
|
|
1527
|
+
{
|
|
1528
|
+
type: 'text',
|
|
1529
|
+
text: '评论输入框里输入 @',
|
|
1530
|
+
marks: [{ type: 'commentMark', attrs: { threadId: 'thread-demo-1' } }],
|
|
1531
|
+
},
|
|
1532
|
+
{ type: 'text', text: ' 可以选择用户或文档。' },
|
|
1533
|
+
],
|
|
1534
|
+
},
|
|
1535
|
+
],
|
|
1536
|
+
})
|
|
1537
|
+
|
|
1538
|
+
const commentUser: RichTextEditorCommentUser = {
|
|
1539
|
+
id: 'user-current',
|
|
1540
|
+
name: '当前用户',
|
|
1541
|
+
color: '#2563eb',
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
const mentionOptions: RichTextEditorMentionItem[] = [
|
|
1545
|
+
{ id: 'user-001', name: '崔国强', type: 1 },
|
|
1546
|
+
{ id: 'user-002', name: '张晓明', type: 1 },
|
|
1547
|
+
{ id: 'user-003', name: '李思雨', type: 1 },
|
|
1548
|
+
{ id: 'doc-001', name: '产品需求文档', type: 2, tag: '文档', updatedAt: '今天 10:30' },
|
|
1549
|
+
{ id: 'doc-002', name: '协同功能设计稿', type: 2, tag: '外部', updatedAt: '昨天 19:17' },
|
|
1550
|
+
{ id: 'doc-003', name: '上线检查清单', type: 2, tag: '模板', updatedAt: '2026-05-17' },
|
|
1551
|
+
]
|
|
1552
|
+
|
|
1553
|
+
const comments = ref<RichTextEditorCommentThread[]>([
|
|
1554
|
+
{
|
|
1555
|
+
id: 'thread-demo-1',
|
|
1556
|
+
anchorText: '评论输入框里输入 @',
|
|
1557
|
+
status: 'open',
|
|
1558
|
+
comments: [
|
|
1559
|
+
{
|
|
1560
|
+
id: 'comment-demo-1',
|
|
1561
|
+
content: '这里可以 @张晓明 看一下评论提及。',
|
|
1562
|
+
mentions: [{ id: 'user-002', name: '张晓明', type: 1 }],
|
|
1563
|
+
author: { id: 'user-001', name: '崔国强', color: '#4c7dff' },
|
|
1564
|
+
createdAt: new Date(Date.now() - 10 * 60 * 1000).toISOString(),
|
|
1565
|
+
},
|
|
1566
|
+
{
|
|
1567
|
+
id: 'comment-demo-2',
|
|
1568
|
+
parentId: 'comment-demo-1',
|
|
1569
|
+
content: '收到,也可以关联 @产品需求文档。',
|
|
1570
|
+
mentions: [{ id: 'doc-001', name: '产品需求文档', type: 2, tag: '文档' }],
|
|
1571
|
+
author: { id: 'user-002', name: '张晓明', color: '#16a34a' },
|
|
1572
|
+
createdAt: new Date(Date.now() - 5 * 60 * 1000).toISOString(),
|
|
1573
|
+
},
|
|
1574
|
+
],
|
|
1575
|
+
},
|
|
1576
|
+
])
|
|
1577
|
+
|
|
1578
|
+
async function uploadCommentImage(payload: RichTextEditorUploadInput): Promise<RichTextEditorUploadResult> {
|
|
1579
|
+
// 真实项目里改成上传到文件服务,然后返回 url/name/size/mimeType。
|
|
1580
|
+
return {
|
|
1581
|
+
url: URL.createObjectURL(payload.file),
|
|
1582
|
+
name: payload.file.name,
|
|
1583
|
+
size: payload.file.size,
|
|
1584
|
+
mimeType: payload.file.type,
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
async function handleCommentMentionSearch(payload: RichTextEditorCommentMentionProviderPayload): Promise<RichTextEditorMentionItem[]> {
|
|
1589
|
+
// 真实项目里按 payload.query、payload.type、payload.threadId 等调用接口。
|
|
1590
|
+
const keyword = payload.query.trim().toLowerCase()
|
|
1591
|
+
|
|
1592
|
+
return mentionOptions.filter((item) => {
|
|
1593
|
+
const typeMatched = payload.type === 'all' || !payload.type || item.type === payload.type
|
|
1594
|
+
const keywordMatched = !keyword || item.name.toLowerCase().includes(keyword) || item.id.toLowerCase().includes(keyword)
|
|
1595
|
+
return typeMatched && keywordMatched
|
|
1596
|
+
})
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
function handleCommentCreate(payload: RichTextEditorCommentCreatePayload) {
|
|
1600
|
+
comments.value = [
|
|
1601
|
+
...comments.value,
|
|
1602
|
+
{
|
|
1603
|
+
id: payload.threadId,
|
|
1604
|
+
anchorText: payload.anchorText,
|
|
1605
|
+
status: 'open',
|
|
1606
|
+
comments: [
|
|
1607
|
+
{
|
|
1608
|
+
id: payload.commentId,
|
|
1609
|
+
content: payload.content,
|
|
1610
|
+
images: payload.images,
|
|
1611
|
+
mentions: payload.mentions,
|
|
1612
|
+
author: payload.author,
|
|
1613
|
+
createdAt: payload.createdAt,
|
|
1614
|
+
},
|
|
1615
|
+
],
|
|
1616
|
+
},
|
|
1617
|
+
]
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
function handleCommentReply(payload: RichTextEditorCommentReplyPayload) {
|
|
1621
|
+
comments.value = comments.value.map((thread) =>
|
|
1622
|
+
thread.id === payload.threadId
|
|
1623
|
+
? {
|
|
1624
|
+
...thread,
|
|
1625
|
+
comments: [
|
|
1626
|
+
...thread.comments,
|
|
1627
|
+
{
|
|
1628
|
+
id: payload.commentId,
|
|
1629
|
+
parentId: payload.parentId,
|
|
1630
|
+
content: payload.content,
|
|
1631
|
+
images: payload.images,
|
|
1632
|
+
mentions: payload.mentions,
|
|
1633
|
+
author: payload.author,
|
|
1634
|
+
createdAt: payload.createdAt,
|
|
1635
|
+
},
|
|
1636
|
+
],
|
|
1637
|
+
}
|
|
1638
|
+
: thread,
|
|
1639
|
+
)
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
function handleCommentUpdate(payload: RichTextEditorCommentUpdatePayload) {
|
|
1643
|
+
comments.value = comments.value.map((thread) =>
|
|
1644
|
+
thread.id === payload.threadId
|
|
1645
|
+
? {
|
|
1646
|
+
...thread,
|
|
1647
|
+
comments: thread.comments.map((item) =>
|
|
1648
|
+
item.id === payload.commentId
|
|
1649
|
+
? {
|
|
1650
|
+
...item,
|
|
1651
|
+
content: payload.content,
|
|
1652
|
+
images: payload.images ?? item.images,
|
|
1653
|
+
mentions: payload.mentions,
|
|
1654
|
+
updatedAt: payload.updatedAt,
|
|
1655
|
+
}
|
|
1656
|
+
: item,
|
|
1657
|
+
),
|
|
1658
|
+
}
|
|
1659
|
+
: thread,
|
|
1660
|
+
)
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
function handleCommentDelete(payload: RichTextEditorCommentDeletePayload) {
|
|
1664
|
+
if (!payload.commentId) {
|
|
1665
|
+
comments.value = comments.value.filter((thread) => thread.id !== payload.threadId)
|
|
1666
|
+
return
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
comments.value = comments.value
|
|
1670
|
+
.map((thread) =>
|
|
1671
|
+
thread.id === payload.threadId
|
|
1672
|
+
? {
|
|
1673
|
+
...thread,
|
|
1674
|
+
comments: thread.comments.filter((item) => item.id !== payload.commentId && item.parentId !== payload.commentId),
|
|
1675
|
+
}
|
|
1676
|
+
: thread,
|
|
1677
|
+
)
|
|
1678
|
+
.filter((thread) => thread.comments.length > 0)
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
function handleCommentResolve(payload: RichTextEditorCommentResolvePayload) {
|
|
1682
|
+
comments.value = comments.value.map((thread) =>
|
|
1683
|
+
thread.id === payload.threadId
|
|
1684
|
+
? { ...thread, status: payload.resolved ? 'resolved' : 'open' }
|
|
1685
|
+
: thread,
|
|
1686
|
+
)
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
function handleCommentSubmit(payload: RichTextEditorCommentSubmitPayload) {
|
|
1690
|
+
// 这个事件适合做通知、消息流、审计日志。
|
|
1691
|
+
// payload.content:评论内容
|
|
1692
|
+
// payload.mentions:本次评论 @ 的用户/文档
|
|
1693
|
+
// payload.targetUserId / targetCommentId:回复别人时,被回复用户和评论 ID
|
|
1694
|
+
console.log('comment submit:', payload)
|
|
1695
|
+
}
|
|
1696
|
+
</script>
|
|
1697
|
+
|
|
1698
|
+
<template>
|
|
1699
|
+
<RichTextEditor
|
|
1700
|
+
v-model="content"
|
|
1701
|
+
:comments="comments"
|
|
1702
|
+
:comment-user="commentUser"
|
|
1703
|
+
:show-comments="true"
|
|
1704
|
+
:upload-comment-image="uploadCommentImage"
|
|
1705
|
+
:comment-mention-provider="handleCommentMentionSearch"
|
|
1706
|
+
@comment-create="handleCommentCreate"
|
|
1707
|
+
@comment-reply="handleCommentReply"
|
|
1708
|
+
@comment-update="handleCommentUpdate"
|
|
1709
|
+
@comment-delete="handleCommentDelete"
|
|
1710
|
+
@comment-resolve="handleCommentResolve"
|
|
1711
|
+
@comment-submit="handleCommentSubmit"
|
|
1712
|
+
/>
|
|
1713
|
+
</template>
|
|
1714
|
+
```
|
|
1715
|
+
|
|
1716
|
+
### 评论 Props
|
|
1717
|
+
|
|
1718
|
+
| Prop | 类型 | 说明 |
|
|
1719
|
+
| --- | --- | --- |
|
|
1720
|
+
| `comments` | `RichTextEditorCommentThread[] \| null` | 评论线程列表,业务方持久化并回传。 |
|
|
1721
|
+
| `commentUser` | `RichTextEditorCommentUser \| null` | 当前评论用户;不传时不能新建评论或回复。 |
|
|
1722
|
+
| `showComments` | `boolean` | 是否显示评论功能;为 `true` 时显示工具栏评论按钮和右侧评论栏,为 `false` 或不传时隐藏评论入口和评论栏。 |
|
|
1723
|
+
| `uploadCommentImage` | `RichTextEditorUploadHook \| null` | 评论图片上传 hook,返回结构同 `uploadImage`;未传时会回退使用 `uploadImage`。 |
|
|
1724
|
+
| `commentMention` | `boolean` | 是否启用评论输入框里的 `@` 提及,默认启用。 |
|
|
1725
|
+
| `commentMentionProvider` | `RichTextEditorCommentMentionProvider \| null` | 评论 `@` 查询数据源,独立于正文 `mentionProvider`。 |
|
|
1726
|
+
| `onCommentMentionSearch` | `RichTextEditorCommentMentionProvider \| null` | 评论 `@` 查询函数的另一种传入方式,独立于正文 `onMentionSearch`。 |
|
|
1727
|
+
|
|
1728
|
+
### 评论事件
|
|
1729
|
+
|
|
1730
|
+
| 事件 | Payload | 说明 |
|
|
1731
|
+
| --- | --- | --- |
|
|
1732
|
+
| `comment-create` | `RichTextEditorCommentCreatePayload` | 用户选中文字并发布第一条评论时触发。 |
|
|
1733
|
+
| `comment-reply` | `RichTextEditorCommentReplyPayload` | 用户在已有评论线程下回复时触发;回复某人时会带 `parentId`、`targetUserId`、`targetCommentId`。 |
|
|
1734
|
+
| `comment-update` | `RichTextEditorCommentUpdatePayload` | 用户编辑评论内容时触发。 |
|
|
1735
|
+
| `comment-delete` | `RichTextEditorCommentDeletePayload` | 删除单条评论或整个评论线程时触发。 |
|
|
1736
|
+
| `comment-resolve` | `RichTextEditorCommentResolvePayload` | 用户点击完成评论线程时触发。 |
|
|
1737
|
+
| `comment-select` | `RichTextEditorCommentThread` | 用户点击正文标注或侧边栏评论卡片时触发。 |
|
|
1738
|
+
| `comment-submit` | `RichTextEditorCommentSubmitPayload` | 新建、回复、编辑评论后都会触发;会带 `content`、`mentions`、`targetUserId`、`targetCommentId` 等通知所需信息。 |
|
|
1739
|
+
| `comment-mention-search` | `RichTextEditorCommentMentionProviderPayload` | 评论输入框触发 `@` 查询时通知外部;实际返回数据请使用 `commentMentionProvider` 或 `onCommentMentionSearch`。 |
|
|
1740
|
+
| `comment-mention-item-click` | `RichTextEditorCommentMentionItem` | 用户点击评论 `@` 弹层里的某个用户/文档时触发。 |
|
|
1741
|
+
| `comment-mention-submit` | `RichTextEditorCommentMentionItem` | 用户确认选择评论 `@` 用户/文档时触发。 |
|
|
1742
|
+
|
|
1743
|
+
### 数据保存建议
|
|
1744
|
+
|
|
1745
|
+
- 评论锚点是正文内容的一部分,会随 `v-model` JSON 或 Yjs 协同内容保存。
|
|
1746
|
+
- 评论列表不写入正文 JSON,建议后端按 `documentId + threadId` 独立保存。
|
|
1747
|
+
- 新建评论时,组件会先给选区写入 `threadId` 标注;业务方收到 `comment-create` 后应保存同一个 `threadId` 并回传到 `comments`。
|
|
1748
|
+
- 回复某一条评论时,业务方保存回复项时保留 `parentId`,组件会把它展示在被回复评论下面。
|
|
1749
|
+
- 评论内容里的 `@用户` / `@文档` 以普通文本插入到 `content`,同时会通过 `mentions` 数组把 `{ id, name, type }` 等结构化信息抛出,业务方建议一起保存。
|
|
1750
|
+
- 评论 `@` 的查询通道与正文提及通道完全独立,接入评论提及时不要复用 `mention-search` 事件。
|
|
1751
|
+
- 多人协同时,正文锚点跟随 Yjs 同步;评论数据需要业务后端自行广播或刷新。
|
|
1752
|
+
- `commentUser` 可以不传;组件会优先使用 `commentUser`,没有时回退使用 `collaboration.user`。如果两者都没有,新建评论和回复按钮会禁用。
|
|
1753
|
+
- 实例方法 `focusCommentThread(threadId): boolean` 可从外部定位到指定评论锚点。
|