@norio-office/rich-text 0.1.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 +1753 -971
- package/dist/{chunk-efA98nb6.js → chunk-C_CI6nMA.js} +1 -1
- package/dist/components/OutlinePanel.vue.d.ts +22 -11
- package/dist/components/RichTextEditor.vue.d.ts +56 -1
- package/dist/components/RichTextOutline.vue.d.ts +30 -0
- package/dist/composables/useRichTextEditor.d.ts +7 -1
- package/dist/extensions/comment-mark.d.ts +2 -0
- package/dist/extensions/image-block.d.ts +7 -1
- package/dist/extensions/video-block.d.ts +7 -1
- package/dist/{html2canvas-Bwmq6fGh.js → html2canvas-4hiP1s1t.js} +2 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +7163 -5226
- package/dist/index.umd.cjs +1472 -705
- package/dist/style.css +1 -1
- package/dist/types.d.ts +154 -0
- package/dist/utils/url.d.ts +2 -0
- package/docs/usage.md +94 -92
- package/package.json +1 -1
- package/yjs/README.md +63 -63
- package/yjs/index.js +224 -224
- package/yjs/package.json +14 -14
- package/dist/index.es-C4X5YiRo.js +0 -5751
- package/dist/jspdf.es.min-ByaXFVjc.js +0 -18214
- package/dist/purify.es-B5ixYH9L.js +0 -549
- package/dist/typeof-Dz1Ap89p.js +0 -11
package/README.md
CHANGED
|
@@ -1,971 +1,1753 @@
|
|
|
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
|
-
| `
|
|
60
|
-
| `
|
|
61
|
-
| `
|
|
62
|
-
| `
|
|
63
|
-
| `
|
|
64
|
-
| `
|
|
65
|
-
| `
|
|
66
|
-
| `
|
|
67
|
-
| `
|
|
68
|
-
| `
|
|
69
|
-
| `
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
|
243
|
-
|
|
|
244
|
-
| `
|
|
245
|
-
| `
|
|
246
|
-
| `
|
|
247
|
-
| `
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
`
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
npm
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
```
|
|
314
|
-
|
|
315
|
-
import {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
```
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
```
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
<
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
```
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
import
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
|
832
|
-
|
|
|
833
|
-
| `
|
|
834
|
-
| `
|
|
835
|
-
| `
|
|
836
|
-
| `
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
1
|
+
# @norio-office/rich-text
|
|
2
|
+
|
|
3
|
+
个人开发,未全方位测试,用于生产请多测试一下 有BUG 请发邮件至:norio-office@qq.com
|
|
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
|
+
| `documentName` | `string` | `''` | 内置导出下载的文件名基础名;组件会自动追加 `.pdf`、`.png` 或 `.html`。 |
|
|
60
|
+
| `editable` | `boolean` | `true` | 是否允许编辑。 |
|
|
61
|
+
| `mode` | `'edit' \| 'preview'` | `'edit'` | 编辑模式或预览模式。 |
|
|
62
|
+
| `watermark` | `RichTextEditorWatermarkOptions \| null` | `null` | 文档页面文本水印配置;不传或 `text` 为空时不显示水印。 |
|
|
63
|
+
| `showOutline` | `boolean` | `true` | 是否显示编辑器内置大纲。设为 `false` 后可以配合外部 `RichTextOutline` 组件自行放置目录。 |
|
|
64
|
+
| `outlinePlacement` | `'left' \| 'right'` | `'right'` | 大纲面板位置。 |
|
|
65
|
+
| `messages` | `RichTextEditorMessages \| null` | `null` | 覆盖内置 UI 文案。 |
|
|
66
|
+
| `enabledExportItems` | `RichTextEditorExportItemKey[] \| null` | `null` | 导出菜单白名单。 |
|
|
67
|
+
| `enabledInsertMenuItems` | `RichTextEditorInsertMenuItemKey[] \| null` | `null` | 插入菜单白名单。 |
|
|
68
|
+
| `enabledToolbarActions` | `RichTextEditorToolbarActionKey[] \| null` | `null` | 工具栏能力白名单。 |
|
|
69
|
+
| `placeholder` | `string` | `''` | 空内容占位文案。 |
|
|
70
|
+
| `mention` | `boolean` | `false` | 是否启用 `@` 提及功能;只有设为 `true` 时输入 `@` 才会打开候选面板,`insertMention()` 才会插入提及节点。 |
|
|
71
|
+
| `collaboration` | `RichTextEditorCollaborationOptions \| null` | `null` | Yjs 协同配置。 |
|
|
72
|
+
| `uploadImage` | `RichTextEditorUploadHook \| null` | `null` | 内置图片选择、图片块上传时调用;返回最终可访问 URL 后组件插入图片。 |
|
|
73
|
+
| `uploadVideo` | `RichTextEditorUploadHook \| null` | `null` | 内置视频选择、视频块上传时调用;返回最终可访问 URL 后组件插入视频。 |
|
|
74
|
+
| `uploadFile` | `RichTextEditorUploadHook \| null` | `null` | 本地文件选择器上传时调用;返回最终可访问 URL 后组件插入文件卡片。 |
|
|
75
|
+
| `onUploadError` | `RichTextEditorUploadErrorHandler \| null` | `null` | 上传失败回调;同时也会触发 `upload-error` 事件。 |
|
|
76
|
+
| `mentionProvider` | `RichTextEditorMentionProvider \| null` | `null` | `@` 提及候选数据提供函数,兼容用法。 |
|
|
77
|
+
| `onMentionSearch` | `RichTextEditorMentionProvider \| null` | `null` | `@mention-search` 函数式事件对应的异步候选加载函数。 |
|
|
78
|
+
|
|
79
|
+
白名单 props 不传时表示启用全部内置项;传入数组后,仅数组里的项目会显示并可用。
|
|
80
|
+
|
|
81
|
+
### 水印
|
|
82
|
+
|
|
83
|
+
`watermark` 只作为页面视觉层渲染,不写入编辑器 JSON,也不会影响 `getText()` 的纯文本结果。PDF、图片和打印导出会保留水印。
|
|
84
|
+
|
|
85
|
+
```vue
|
|
86
|
+
<RichTextEditor
|
|
87
|
+
v-model="content"
|
|
88
|
+
:watermark="{
|
|
89
|
+
text: '内部资料',
|
|
90
|
+
color: 'rgba(37, 99, 235, 0.12)',
|
|
91
|
+
fontSize: 20,
|
|
92
|
+
rotate: -24,
|
|
93
|
+
showInEdit: true,
|
|
94
|
+
}"
|
|
95
|
+
/>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
| 字段 | 类型 | 默认值 | 说明 |
|
|
99
|
+
| --- | --- | --- | --- |
|
|
100
|
+
| `text` | `string` | - | 水印文字,必填。 |
|
|
101
|
+
| `color` | `string` | `'rgba(15, 23, 42, 0.12)'` | 水印文字颜色。 |
|
|
102
|
+
| `fontSize` | `number` | `18` | 水印字号,单位 px。 |
|
|
103
|
+
| `rotate` | `number` | `-24` | 水印倾斜角度,单位 deg。 |
|
|
104
|
+
| `showInEdit` | `boolean` | `true` | 编辑状态下是否显示水印;预览状态始终按配置显示。 |
|
|
105
|
+
|
|
106
|
+
```vue
|
|
107
|
+
<RichTextEditor
|
|
108
|
+
v-model="content"
|
|
109
|
+
:enabled-export-items="['html', 'image']"
|
|
110
|
+
:enabled-insert-menu-items="['image', 'local-file', 'blockquote']"
|
|
111
|
+
:enabled-toolbar-actions="['blockquote']"
|
|
112
|
+
/>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 可用 Key
|
|
116
|
+
|
|
117
|
+
导出菜单:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
type RichTextEditorExportItemKey = 'pdf' | 'html' | 'image' | 'print'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
插入菜单:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
type RichTextEditorInsertMenuItemKey =
|
|
127
|
+
| 'image'
|
|
128
|
+
| 'video'
|
|
129
|
+
| 'table'
|
|
130
|
+
| 'local-file'
|
|
131
|
+
| 'columns'
|
|
132
|
+
| 'highlight-block'
|
|
133
|
+
| 'date'
|
|
134
|
+
| 'code-block'
|
|
135
|
+
| 'formula'
|
|
136
|
+
| 'blockquote'
|
|
137
|
+
| 'emoji'
|
|
138
|
+
| 'link'
|
|
139
|
+
| 'divider'
|
|
140
|
+
| 'countdown'
|
|
141
|
+
| 'markdown-import'
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
工具栏:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
type RichTextEditorToolbarActionKey = 'blockquote'
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### 事件
|
|
151
|
+
|
|
152
|
+
| 事件 | Payload | 说明 |
|
|
153
|
+
| --- | --- | --- |
|
|
154
|
+
| `update:modelValue` | `JSONContent` | 内容更新,用于 `v-model`。 |
|
|
155
|
+
| `change` | `JSONContent` | 内容更新事件。 |
|
|
156
|
+
| `local-file-upload` | `RichTextEditorLocalFilePayload` | 用户通过本地文件选择器插入文件后触发。 |
|
|
157
|
+
| `local-file-click` | `RichTextEditorLocalFilePayload` | 用户点击本地文件卡片非下载按钮区域时触发。 |
|
|
158
|
+
| `local-file-download` | `RichTextEditorLocalFilePayload` | 用户点击本地文件卡片下载按钮时触发。 |
|
|
159
|
+
| `upload-error` | `RichTextEditorUploadErrorPayload` | 图片、视频或文件上传 hook 失败时触发。 |
|
|
160
|
+
| `mention-search` | `RichTextEditorMentionProviderPayload` | 函数式监听写法,对应 `onMentionSearch` prop;输入 `@` 后加载候选数据,可返回数组或 `Promise`。 |
|
|
161
|
+
| `mention-item-click` | `RichTextEditorMentionItem` | 点击弹窗候选项或已插入的提及节点时触发。 |
|
|
162
|
+
| `mention-submit` | `RichTextEditorMentionItem` | 点击弹窗 `提及` 按钮并插入提及节点后触发。 |
|
|
163
|
+
|
|
164
|
+
`mention-search` 没有出现在组件的 `defineEmits` 中;Vue 模板里的 `@mention-search="handler"` 会作为 `onMentionSearch` 函数式 prop 传入组件。
|
|
165
|
+
|
|
166
|
+
### @ 提及
|
|
167
|
+
|
|
168
|
+
`mention` 默认关闭。需要提及能力时请显式传入 `:mention="true"`;未开启时输入 `@` 只会保留普通文本,`@mention-search` 不会被调用,`insertMention()` 会返回 `false`。
|
|
169
|
+
|
|
170
|
+
输入 `@` 会打开候选弹窗。宿主项目通过 `@mention-search` 异步返回候选数据,组件不会内置默认人员或文档数据。推荐使用 `@mention-search`,`mentionProvider` 仅作为函数 prop 兼容保留。
|
|
171
|
+
|
|
172
|
+
`type: 1` 表示人,`type: 2` 表示文档。人和文档共用 `id`、`name`、`type`、`avatar`、`icon`、`tag`、`updatedAt` 等字段;文档传入 `tag` 时会在标题旁显示标签文字,不传则不显示;人没有头像时会用名字最后两个字和固定亮色颜色池生成头像。
|
|
173
|
+
|
|
174
|
+
```vue
|
|
175
|
+
<script setup lang="ts">
|
|
176
|
+
import { ref } from 'vue'
|
|
177
|
+
import type { JSONContent } from '@tiptap/core'
|
|
178
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
179
|
+
import type {
|
|
180
|
+
RichTextEditorMentionItem,
|
|
181
|
+
RichTextEditorMentionProviderPayload,
|
|
182
|
+
} from '@norio-office/rich-text'
|
|
183
|
+
|
|
184
|
+
const content = ref<JSONContent | null>(null)
|
|
185
|
+
|
|
186
|
+
const mentionItems: RichTextEditorMentionItem[] = [
|
|
187
|
+
{ id: 'user-1001', name: '崔国强', type: 1 },
|
|
188
|
+
{ id: 'user-1002', name: '刘佳宁', type: 1 },
|
|
189
|
+
{ id: 'doc-2001', name: 'IT资产管理系统', type: 2, tag: '外部', updatedAt: '2025年12月1日' },
|
|
190
|
+
{ id: 'doc-2002', name: '客户满意度调研表', type: 2, updatedAt: '2025年12月4日' },
|
|
191
|
+
]
|
|
192
|
+
|
|
193
|
+
async function handleMentionSearch(payload: RichTextEditorMentionProviderPayload) {
|
|
194
|
+
const query = payload.query.trim().toLowerCase()
|
|
195
|
+
await new Promise((resolve) => window.setTimeout(resolve, 120))
|
|
196
|
+
|
|
197
|
+
return mentionItems.filter((item) => {
|
|
198
|
+
const matchedType = payload.type === 'all' || item.type === payload.type
|
|
199
|
+
const matchedKeyword = !query || item.name.toLowerCase().includes(query)
|
|
200
|
+
return matchedType && matchedKeyword
|
|
201
|
+
})
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function handleMentionItemClick(item: RichTextEditorMentionItem) {
|
|
205
|
+
console.log('clicked mention item:', item)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function handleMentionSubmit(item: RichTextEditorMentionItem) {
|
|
209
|
+
console.log('inserted mention:', item)
|
|
210
|
+
}
|
|
211
|
+
</script>
|
|
212
|
+
|
|
213
|
+
<template>
|
|
214
|
+
<RichTextEditor
|
|
215
|
+
v-model="content"
|
|
216
|
+
:mention="true"
|
|
217
|
+
@mention-search="handleMentionSearch"
|
|
218
|
+
@mention-item-click="handleMentionItemClick"
|
|
219
|
+
@mention-submit="handleMentionSubmit"
|
|
220
|
+
/>
|
|
221
|
+
</template>
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
点击候选项只会触发 `mention-item-click` 并选中当前项;点击底部 `提及` 按钮才会插入结构化提及节点并触发 `mention-submit`。关闭弹窗或不点击 `提及` 时,编辑器会保留普通 `@` 文本。
|
|
225
|
+
|
|
226
|
+
### 自定义文案
|
|
227
|
+
|
|
228
|
+
编辑器默认 UI 文案为中文。你可以用 `messages` 覆盖任意 key:
|
|
229
|
+
|
|
230
|
+
```vue
|
|
231
|
+
<RichTextEditor
|
|
232
|
+
v-model="content"
|
|
233
|
+
:messages="{
|
|
234
|
+
'insert.localFile': '附件',
|
|
235
|
+
'export.label': '下载',
|
|
236
|
+
}"
|
|
237
|
+
/>
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
常用文案 key:
|
|
241
|
+
|
|
242
|
+
| Key | 默认中文 |
|
|
243
|
+
| --- | --- |
|
|
244
|
+
| `insert.label` | 插入 |
|
|
245
|
+
| `insert.section.general` | 通用 |
|
|
246
|
+
| `insert.section.apps` | 小应用 |
|
|
247
|
+
| `insert.section.external` | 外部内容 |
|
|
248
|
+
| `insert.image` | 图片 |
|
|
249
|
+
| `insert.video` | 视频 |
|
|
250
|
+
| `insert.table` | 表格 |
|
|
251
|
+
| `insert.localFile` | 本地文件 |
|
|
252
|
+
| `insert.columns` | 分栏 |
|
|
253
|
+
| `insert.highlightBlock` | 高亮块 |
|
|
254
|
+
| `insert.date` | 日期 |
|
|
255
|
+
| `insert.codeBlock` | 代码块 |
|
|
256
|
+
| `insert.formula` | 公式 |
|
|
257
|
+
| `insert.blockquote` | 引用 |
|
|
258
|
+
| `insert.emoji` | 表情符号 |
|
|
259
|
+
| `insert.link` | 超链接 |
|
|
260
|
+
| `insert.divider` | 分隔线 |
|
|
261
|
+
| `insert.countdown` | 倒计时 |
|
|
262
|
+
| `insert.markdownImport` | Markdown 导入 |
|
|
263
|
+
| `export.label` | 导出 |
|
|
264
|
+
| `export.pdf` | 导出 PDF |
|
|
265
|
+
| `export.pdf.loading` | 导出 PDF 中... |
|
|
266
|
+
| `export.html` | 导出 HTML |
|
|
267
|
+
| `export.html.loading` | 导出 HTML 中... |
|
|
268
|
+
| `export.image` | 导出图片 |
|
|
269
|
+
| `export.image.loading` | 导出图片中... |
|
|
270
|
+
| `print.label` | 打印 |
|
|
271
|
+
| `print.loading` | 打印中... |
|
|
272
|
+
| `quote.apply` | 应用引用 |
|
|
273
|
+
| `quote.cancel` | 取消引用 |
|
|
274
|
+
| `quote.borderColor` | 边框颜色 |
|
|
275
|
+
| `quote.backgroundColor` | 背景颜色 |
|
|
276
|
+
| `outline.label` | 大纲 |
|
|
277
|
+
| `outline.collapse` | 收起大纲 |
|
|
278
|
+
| `outline.empty.description` | 对文档内容应用“标题”样式,即可自动生成大纲。 |
|
|
279
|
+
| `outline.empty.tip` | 点击左下角“大纲”按钮可以随时展开或收起。 |
|
|
280
|
+
| `status.wordCountUnit` | 个字 |
|
|
281
|
+
| `status.presentation.enter` | 演示 |
|
|
282
|
+
| `status.presentation.exit` | 退出演示 |
|
|
283
|
+
| `status.fullscreen.enter` | 全屏 |
|
|
284
|
+
| `status.fullscreen.exit` | 退出全屏 |
|
|
285
|
+
| `countdown.selectTime` | 请选择时间 |
|
|
286
|
+
| `countdown.settingsTitle` | 倒计时设置 |
|
|
287
|
+
| `formula.insertTitle` | 插入 LaTeX 公式 |
|
|
288
|
+
|
|
289
|
+
### 预览模式
|
|
290
|
+
|
|
291
|
+
`mode="preview"` 会切换到只读预览外壳。预览模式下工具栏隐藏,编辑禁用,大纲仍可放在左侧或右侧。
|
|
292
|
+
|
|
293
|
+
```vue
|
|
294
|
+
<RichTextEditor
|
|
295
|
+
v-model="content"
|
|
296
|
+
mode="preview"
|
|
297
|
+
outline-placement="right"
|
|
298
|
+
/>
|
|
299
|
+
```
|
|
300
|
+
|
|
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
|
+
```
|
|
461
|
+
|
|
462
|
+
### 实例 API
|
|
463
|
+
|
|
464
|
+
通过 Vue `ref` 获取组件实例方法:
|
|
465
|
+
|
|
466
|
+
```vue
|
|
467
|
+
<script setup lang="ts">
|
|
468
|
+
import { ref } from 'vue'
|
|
469
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
470
|
+
import type { RichTextEditorInstance } from '@norio-office/rich-text'
|
|
471
|
+
|
|
472
|
+
const editorRef = ref<RichTextEditorInstance | null>(null)
|
|
473
|
+
</script>
|
|
474
|
+
|
|
475
|
+
<template>
|
|
476
|
+
<RichTextEditor ref="editorRef" />
|
|
477
|
+
</template>
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
可用方法:
|
|
481
|
+
|
|
482
|
+
| 方法 | 返回值 | 说明 |
|
|
483
|
+
| --- | --- | --- |
|
|
484
|
+
| `exportPdf()` | `Promise<Blob \| null>` | 导出 PDF。 |
|
|
485
|
+
| `exportImage(options?)` | `Promise<Blob \| null>` | 导出图片,支持 PNG/JPEG。 |
|
|
486
|
+
| `exportHtml()` | `string \| null` | 导出 HTML 字符串。 |
|
|
487
|
+
| `insertImage(payload)` | `boolean` | 插入 1 到 4 张图片。 |
|
|
488
|
+
| `insertVideo(payload)` | `boolean` | 插入视频块。 |
|
|
489
|
+
| `insertFile(payload)` | `boolean` | 插入链接/文件预览块。 |
|
|
490
|
+
| `insertLocalFile(payload)` | `boolean` | 插入本地文件卡片。 |
|
|
491
|
+
| `insertMention(payload)` | `boolean` | 插入一个结构化行内提及节点;`mention` 未开启时返回 `false`。 |
|
|
492
|
+
| `openLocalFilePicker()` | `void` | 打开本地文件选择器。 |
|
|
493
|
+
| `focus()` | `void` | 聚焦编辑器。 |
|
|
494
|
+
| `getJSON()` | `JSONContent \| null` | 获取当前 JSON 内容。 |
|
|
495
|
+
| `getText()` | `string` | 获取当前纯文本内容,不包含 HTML 标签。 |
|
|
496
|
+
| `getImages()` | `RichTextEditorImagePayload[]` | 获取文档内所有图片信息。 |
|
|
497
|
+
| `getVideos()` | `RichTextEditorVideoPayload[]` | 获取文档内所有视频信息。 |
|
|
498
|
+
| `getFiles()` | `RichTextEditorCollectedFilePayload[]` | 获取文档内所有文件信息;`kind: 'file'` 表示链接/文件预览块,`kind: 'local-file'` 表示本地文件卡片。 |
|
|
499
|
+
| `getOutlineItems()` | `RichTextEditorOutlineItem[]` | 获取当前目录项。 |
|
|
500
|
+
| `getActiveOutlinePos()` | `number \| null` | 获取当前激活目录项的文档位置。 |
|
|
501
|
+
| `focusOutlineItem(pos)` | `boolean` | 聚焦并跳转到指定目录项。 |
|
|
502
|
+
| `onOutlineChange(handler)` | `() => void` | 监听目录变化,返回取消监听函数。 |
|
|
503
|
+
|
|
504
|
+
### 外置目录组件
|
|
505
|
+
|
|
506
|
+
内置目录默认跟随编辑器一起渲染。如果需要把目录放到任意侧栏、抽屉或自定义布局里,可以关闭内置目录,并把编辑器实例传给 `RichTextOutline`。
|
|
507
|
+
|
|
508
|
+
```vue
|
|
509
|
+
<script setup lang="ts">
|
|
510
|
+
import { ref } from 'vue'
|
|
511
|
+
import { RichTextEditor, RichTextOutline } from '@norio-office/rich-text'
|
|
512
|
+
import type { RichTextEditorInstance } from '@norio-office/rich-text'
|
|
513
|
+
|
|
514
|
+
const editorRef = ref<RichTextEditorInstance | null>(null)
|
|
515
|
+
</script>
|
|
516
|
+
|
|
517
|
+
<template>
|
|
518
|
+
<RichTextEditor ref="editorRef" v-model="content" :show-outline="false" />
|
|
519
|
+
<aside class="outline-sidebar">
|
|
520
|
+
<RichTextOutline :editor="editorRef" />
|
|
521
|
+
</aside>
|
|
522
|
+
</template>
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
`RichTextOutline` 会自动订阅编辑器目录状态,点击条目时会调用 `focusOutlineItem` 跳转到对应标题。组件自身最小高度为 `200px`,默认根据内容自适应增高;如果需要固定高度或内部滚动,请在外层套一个容器控制高度。可选 props:`open`、`placement`、`title`、`collapseTitle`、`emptyDescription`、`emptyTip`、`showCollapse`;事件:`select`、`toggle`、`change`。
|
|
526
|
+
|
|
527
|
+
### 上传并插入
|
|
528
|
+
|
|
529
|
+
推荐集成流程:
|
|
530
|
+
|
|
531
|
+
1. 在业务层上传文件。
|
|
532
|
+
2. 等待接口返回可访问 URL。
|
|
533
|
+
3. 调用组件实例方法插入返回内容。
|
|
534
|
+
|
|
535
|
+
如果使用编辑器内置的图片、视频、本地文件选择器或页面拖拽上传,请传入上传 hook。编辑器只负责把用户选中的 `File` 交给业务侧上传,并在 hook 返回 URL 后插入内容;组件不会内置业务上传接口,也不会自动回退为 base64/blob URL。
|
|
536
|
+
|
|
537
|
+
页面拖拽上传会按文件类型分流:`image/*` 走图片上传,`video/*` 走视频上传,其他格式全部走 `uploadFile` 并插入本地文件卡片。
|
|
538
|
+
|
|
539
|
+
```vue
|
|
540
|
+
<script setup lang="ts">
|
|
541
|
+
import type {
|
|
542
|
+
RichTextEditorUploadInput,
|
|
543
|
+
RichTextEditorUploadResult,
|
|
544
|
+
RichTextEditorUploadErrorPayload,
|
|
545
|
+
} from '@norio-office/rich-text'
|
|
546
|
+
|
|
547
|
+
async function uploadToServer(payload: RichTextEditorUploadInput): Promise<RichTextEditorUploadResult> {
|
|
548
|
+
const form = new FormData()
|
|
549
|
+
form.append('file', payload.file)
|
|
550
|
+
form.append('kind', payload.kind)
|
|
551
|
+
|
|
552
|
+
const response = await fetch('/api/upload', {
|
|
553
|
+
method: 'POST',
|
|
554
|
+
body: form,
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
if (!response.ok) {
|
|
558
|
+
throw new Error('上传失败')
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
return await response.json()
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function handleUploadError(payload: RichTextEditorUploadErrorPayload) {
|
|
565
|
+
console.error(payload.kind, payload.fileName, payload.error)
|
|
566
|
+
}
|
|
567
|
+
</script>
|
|
568
|
+
|
|
569
|
+
<template>
|
|
570
|
+
<RichTextEditor
|
|
571
|
+
v-model="content"
|
|
572
|
+
:upload-image="uploadToServer"
|
|
573
|
+
:upload-video="uploadToServer"
|
|
574
|
+
:upload-file="uploadToServer"
|
|
575
|
+
@local-file-click="(file) => console.log('file clicked', file)"
|
|
576
|
+
@upload-error="handleUploadError"
|
|
577
|
+
/>
|
|
578
|
+
</template>
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
`onUploadError` callback prop 和 `@upload-error` 事件都能接收上传失败信息。通常选择一种接入方式即可,避免业务层重复处理。
|
|
582
|
+
|
|
583
|
+
插入图片:
|
|
584
|
+
|
|
585
|
+
```ts
|
|
586
|
+
async function uploadImage(file: File) {
|
|
587
|
+
const imageUrl = await yourUploadApi(file)
|
|
588
|
+
|
|
589
|
+
editorRef.value?.insertImage({
|
|
590
|
+
src: imageUrl,
|
|
591
|
+
name: file.name,
|
|
592
|
+
alt: file.name,
|
|
593
|
+
description: '',
|
|
594
|
+
})
|
|
595
|
+
}
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
一次最多插入 4 张图片:
|
|
599
|
+
|
|
600
|
+
```ts
|
|
601
|
+
editorRef.value?.insertImage([
|
|
602
|
+
{ src: 'https://cdn.example.com/a.png', name: 'a.png' },
|
|
603
|
+
{ src: 'https://cdn.example.com/b.png', name: 'b.png' },
|
|
604
|
+
])
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
插入视频:
|
|
608
|
+
|
|
609
|
+
```ts
|
|
610
|
+
async function uploadVideo(file: File) {
|
|
611
|
+
const videoUrl = await yourUploadApi(file)
|
|
612
|
+
|
|
613
|
+
editorRef.value?.insertVideo({
|
|
614
|
+
src: videoUrl,
|
|
615
|
+
name: file.name,
|
|
616
|
+
mimeType: file.type || 'video/mp4',
|
|
617
|
+
description: 'video description',
|
|
618
|
+
})
|
|
619
|
+
}
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
插入文件链接/预览块:
|
|
623
|
+
|
|
624
|
+
```ts
|
|
625
|
+
async function uploadFile(file: File) {
|
|
626
|
+
const fileUrl = await yourUploadApi(file)
|
|
627
|
+
|
|
628
|
+
editorRef.value?.insertFile({
|
|
629
|
+
url: fileUrl,
|
|
630
|
+
name: file.name,
|
|
631
|
+
displayMode: 'text',
|
|
632
|
+
})
|
|
633
|
+
}
|
|
634
|
+
```
|
|
635
|
+
|
|
636
|
+
插入本地文件卡片:
|
|
637
|
+
|
|
638
|
+
```ts
|
|
639
|
+
editorRef.value?.insertLocalFile({
|
|
640
|
+
url: 'https://cdn.example.com/files/demo.txt',
|
|
641
|
+
name: 'demo.txt',
|
|
642
|
+
size: 2048,
|
|
643
|
+
mimeType: 'text/plain',
|
|
644
|
+
})
|
|
645
|
+
```
|
|
646
|
+
|
|
647
|
+
### 导出示例
|
|
648
|
+
|
|
649
|
+
```ts
|
|
650
|
+
async function handleExportPdf() {
|
|
651
|
+
const blob = await editorRef.value?.exportPdf()
|
|
652
|
+
if (!blob) return
|
|
653
|
+
|
|
654
|
+
const url = URL.createObjectURL(blob)
|
|
655
|
+
const link = document.createElement('a')
|
|
656
|
+
link.href = url
|
|
657
|
+
link.download = 'demo.pdf'
|
|
658
|
+
link.click()
|
|
659
|
+
URL.revokeObjectURL(url)
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
async function handleExportImage() {
|
|
663
|
+
const blob = await editorRef.value?.exportImage({
|
|
664
|
+
type: 'image/png',
|
|
665
|
+
})
|
|
666
|
+
if (!blob) return
|
|
667
|
+
|
|
668
|
+
const url = URL.createObjectURL(blob)
|
|
669
|
+
window.open(url, '_blank')
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function handleExportHtml() {
|
|
673
|
+
const html = editorRef.value?.exportHtml()
|
|
674
|
+
if (!html) return
|
|
675
|
+
|
|
676
|
+
console.log(html)
|
|
677
|
+
}
|
|
678
|
+
```
|
|
679
|
+
|
|
680
|
+
### 类型导入
|
|
681
|
+
|
|
682
|
+
```ts
|
|
683
|
+
import type {
|
|
684
|
+
OfficeColorIconProps,
|
|
685
|
+
OfficeIconProps,
|
|
686
|
+
RichTextEditorAlign,
|
|
687
|
+
RichTextEditorCollaborationAwareness,
|
|
688
|
+
RichTextEditorCollaborationDocument,
|
|
689
|
+
RichTextEditorCollaborationProvider,
|
|
690
|
+
RichTextEditorCollaborationUser,
|
|
691
|
+
RichTextEditorCollaborationOptions,
|
|
692
|
+
RichTextEditorCollectedFilePayload,
|
|
693
|
+
RichTextEditorExportItemKey,
|
|
694
|
+
RichTextEditorFilePayload,
|
|
695
|
+
RichTextEditorImageExportOptions,
|
|
696
|
+
RichTextEditorImagePayload,
|
|
697
|
+
RichTextEditorInsertMenuItemKey,
|
|
698
|
+
RichTextEditorInstance,
|
|
699
|
+
RichTextEditorLocalFilePayload,
|
|
700
|
+
RichTextEditorMentionItem,
|
|
701
|
+
RichTextEditorMentionProvider,
|
|
702
|
+
RichTextEditorMentionProviderPayload,
|
|
703
|
+
RichTextEditorMentionType,
|
|
704
|
+
RichTextEditorMessages,
|
|
705
|
+
RichTextEditorOutlineChangeHandler,
|
|
706
|
+
RichTextEditorOutlineItem,
|
|
707
|
+
RichTextEditorOutlineState,
|
|
708
|
+
RichTextEditorProps,
|
|
709
|
+
RichTextEditorToolbarActionKey,
|
|
710
|
+
RichTextEditorUploadErrorHandler,
|
|
711
|
+
RichTextEditorUploadErrorPayload,
|
|
712
|
+
RichTextEditorUploadHook,
|
|
713
|
+
RichTextEditorUploadInput,
|
|
714
|
+
RichTextEditorUploadKind,
|
|
715
|
+
RichTextEditorUploadResult,
|
|
716
|
+
RichTextEditorWatermarkOptions,
|
|
717
|
+
RichTextEditorVideoPayload,
|
|
718
|
+
} from '@norio-office/rich-text'
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
### 包导出
|
|
722
|
+
|
|
723
|
+
```ts
|
|
724
|
+
import RichTextEditor, {
|
|
725
|
+
RichTextEditor as NamedRichTextEditor,
|
|
726
|
+
RichTextOutline,
|
|
727
|
+
OfficeIcon,
|
|
728
|
+
OfficeColorIcon,
|
|
729
|
+
ScrollArea,
|
|
730
|
+
monoIconNames,
|
|
731
|
+
colorIconNames,
|
|
732
|
+
} from '@norio-office/rich-text'
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
### 包内文档
|
|
736
|
+
|
|
737
|
+
- 使用指南:`docs/usage.md`
|
|
738
|
+
- Yjs 服务示例:`yjs/README.md`
|
|
739
|
+
|
|
740
|
+
## English
|
|
741
|
+
|
|
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.
|
|
743
|
+
|
|
744
|
+
### Installation
|
|
745
|
+
|
|
746
|
+
```bash
|
|
747
|
+
npm install @norio-office/rich-text
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
The component styles are published as a separate CSS file. Import it in your application entry:
|
|
751
|
+
|
|
752
|
+
```ts
|
|
753
|
+
import '@norio-office/rich-text/style.css'
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
If your package manager does not auto-install peer dependencies, install the runtime peer packages as well:
|
|
757
|
+
|
|
758
|
+
```bash
|
|
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
|
|
760
|
+
```
|
|
761
|
+
|
|
762
|
+
`html2canvas`, `jspdf`, `katex`, `marked`, and `plyr` are regular dependencies and are installed with the package.
|
|
763
|
+
|
|
764
|
+
### Quick Start
|
|
765
|
+
|
|
766
|
+
```vue
|
|
767
|
+
<script setup lang="ts">
|
|
768
|
+
import { ref } from 'vue'
|
|
769
|
+
import type { JSONContent } from '@tiptap/core'
|
|
770
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
771
|
+
import '@norio-office/rich-text/style.css'
|
|
772
|
+
|
|
773
|
+
const content = ref<JSONContent | null>(null)
|
|
774
|
+
</script>
|
|
775
|
+
|
|
776
|
+
<template>
|
|
777
|
+
<RichTextEditor v-model="content" />
|
|
778
|
+
</template>
|
|
779
|
+
```
|
|
780
|
+
|
|
781
|
+
Default import is also supported:
|
|
782
|
+
|
|
783
|
+
```ts
|
|
784
|
+
import RichTextEditor from '@norio-office/rich-text'
|
|
785
|
+
```
|
|
786
|
+
|
|
787
|
+
### Props
|
|
788
|
+
|
|
789
|
+
| Prop | Type | Default | Description |
|
|
790
|
+
| --- | --- | --- | --- |
|
|
791
|
+
| `modelValue` | `JSONContent \| null` | `null` | Editor content. Supports `v-model`. |
|
|
792
|
+
| `documentName` | `string` | `''` | Base filename for built-in downloads. The component appends `.pdf`, `.png`, or `.html`. |
|
|
793
|
+
| `editable` | `boolean` | `true` | Enables or disables editing. |
|
|
794
|
+
| `mode` | `'edit' \| 'preview'` | `'edit'` | Edit mode or read-only preview mode. |
|
|
795
|
+
| `watermark` | `RichTextEditorWatermarkOptions \| null` | `null` | Text watermark configuration for the document page. No watermark is rendered when omitted or when `text` is empty. |
|
|
796
|
+
| `showOutline` | `boolean` | `true` | Shows the built-in outline panel. Set it to `false` when placing an external `RichTextOutline` component yourself. |
|
|
797
|
+
| `outlinePlacement` | `'left' \| 'right'` | `'right'` | Outline panel placement. |
|
|
798
|
+
| `messages` | `RichTextEditorMessages \| null` | `null` | Overrides built-in UI labels. |
|
|
799
|
+
| `enabledExportItems` | `RichTextEditorExportItemKey[] \| null` | `null` | Export menu whitelist. |
|
|
800
|
+
| `enabledInsertMenuItems` | `RichTextEditorInsertMenuItemKey[] \| null` | `null` | Insert menu whitelist. |
|
|
801
|
+
| `enabledToolbarActions` | `RichTextEditorToolbarActionKey[] \| null` | `null` | Toolbar action whitelist. |
|
|
802
|
+
| `placeholder` | `string` | `''` | Placeholder text for empty content. |
|
|
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. |
|
|
805
|
+
| `uploadImage` | `RichTextEditorUploadHook \| null` | `null` | Upload hook required by built-in image pickers and image drops. The editor inserts the returned URL. |
|
|
806
|
+
| `uploadVideo` | `RichTextEditorUploadHook \| null` | `null` | Upload hook required by built-in video pickers and video drops. The editor inserts the returned URL. |
|
|
807
|
+
| `uploadFile` | `RichTextEditorUploadHook \| null` | `null` | Upload hook required by the local file card picker. The editor inserts the returned URL. |
|
|
808
|
+
| `onUploadError` | `RichTextEditorUploadErrorHandler \| null` | `null` | Optional callback for upload errors. |
|
|
809
|
+
| `mentionProvider` | `RichTextEditorMentionProvider \| null` | `null` | Compatibility function prop for loading `@` mention candidates. |
|
|
810
|
+
| `onMentionSearch` | `RichTextEditorMentionProvider \| null` | `null` | Async candidate loader used by the function-style `@mention-search` event. |
|
|
811
|
+
|
|
812
|
+
When whitelist props are omitted, all built-in options remain enabled. Once a list is passed, only listed items are visible and usable.
|
|
813
|
+
|
|
814
|
+
### Watermark
|
|
815
|
+
|
|
816
|
+
`watermark` is rendered as a visual page layer only. It is not written into the editor JSON and does not affect `getText()`. PDF, image, and print exports keep the watermark.
|
|
817
|
+
|
|
818
|
+
```vue
|
|
819
|
+
<RichTextEditor
|
|
820
|
+
v-model="content"
|
|
821
|
+
:watermark="{
|
|
822
|
+
text: 'Internal',
|
|
823
|
+
color: 'rgba(37, 99, 235, 0.12)',
|
|
824
|
+
fontSize: 20,
|
|
825
|
+
rotate: -24,
|
|
826
|
+
showInEdit: true,
|
|
827
|
+
}"
|
|
828
|
+
/>
|
|
829
|
+
```
|
|
830
|
+
|
|
831
|
+
| Field | Type | Default | Description |
|
|
832
|
+
| --- | --- | --- | --- |
|
|
833
|
+
| `text` | `string` | - | Watermark text. Required. |
|
|
834
|
+
| `color` | `string` | `'rgba(15, 23, 42, 0.12)'` | Watermark text color. |
|
|
835
|
+
| `fontSize` | `number` | `18` | Watermark font size in px. |
|
|
836
|
+
| `rotate` | `number` | `-24` | Watermark rotation in degrees. |
|
|
837
|
+
| `showInEdit` | `boolean` | `true` | Whether to show the watermark while editing. Preview mode still renders it when configured. |
|
|
838
|
+
|
|
839
|
+
```vue
|
|
840
|
+
<RichTextEditor
|
|
841
|
+
v-model="content"
|
|
842
|
+
:enabled-export-items="['html', 'image']"
|
|
843
|
+
:enabled-insert-menu-items="['image', 'local-file', 'blockquote']"
|
|
844
|
+
:enabled-toolbar-actions="['blockquote']"
|
|
845
|
+
/>
|
|
846
|
+
```
|
|
847
|
+
|
|
848
|
+
### Available Keys
|
|
849
|
+
|
|
850
|
+
Export menu:
|
|
851
|
+
|
|
852
|
+
```ts
|
|
853
|
+
type RichTextEditorExportItemKey = 'pdf' | 'html' | 'image' | 'print'
|
|
854
|
+
```
|
|
855
|
+
|
|
856
|
+
Insert menu:
|
|
857
|
+
|
|
858
|
+
```ts
|
|
859
|
+
type RichTextEditorInsertMenuItemKey =
|
|
860
|
+
| 'image'
|
|
861
|
+
| 'video'
|
|
862
|
+
| 'table'
|
|
863
|
+
| 'local-file'
|
|
864
|
+
| 'columns'
|
|
865
|
+
| 'highlight-block'
|
|
866
|
+
| 'date'
|
|
867
|
+
| 'code-block'
|
|
868
|
+
| 'formula'
|
|
869
|
+
| 'blockquote'
|
|
870
|
+
| 'emoji'
|
|
871
|
+
| 'link'
|
|
872
|
+
| 'divider'
|
|
873
|
+
| 'countdown'
|
|
874
|
+
| 'markdown-import'
|
|
875
|
+
```
|
|
876
|
+
|
|
877
|
+
Toolbar:
|
|
878
|
+
|
|
879
|
+
```ts
|
|
880
|
+
type RichTextEditorToolbarActionKey = 'blockquote'
|
|
881
|
+
```
|
|
882
|
+
|
|
883
|
+
### Events
|
|
884
|
+
|
|
885
|
+
| Event | Payload | Description |
|
|
886
|
+
| --- | --- | --- |
|
|
887
|
+
| `update:modelValue` | `JSONContent` | Content update event used by `v-model`. |
|
|
888
|
+
| `change` | `JSONContent` | Content update event. |
|
|
889
|
+
| `local-file-upload` | `RichTextEditorLocalFilePayload` | Emitted after a local file is picked and inserted. |
|
|
890
|
+
| `local-file-click` | `RichTextEditorLocalFilePayload` | Emitted when the user clicks the non-download area of a local file card. |
|
|
891
|
+
| `local-file-download` | `RichTextEditorLocalFilePayload` | Emitted when the local file card download button is clicked. |
|
|
892
|
+
| `upload-error` | `RichTextEditorUploadErrorPayload` | Emitted when an image, video, or file upload hook fails. |
|
|
893
|
+
| `mention-search` | `RichTextEditorMentionProviderPayload` | Function-style listener syntax for the `onMentionSearch` prop; loads candidates after the user types `@`; may return an array or a `Promise`. |
|
|
894
|
+
| `mention-item-click` | `RichTextEditorMentionItem` | Emitted when a popup candidate or inserted mention node is clicked. |
|
|
895
|
+
| `mention-submit` | `RichTextEditorMentionItem` | Emitted after the popup `提及` button inserts the selected mention. |
|
|
896
|
+
|
|
897
|
+
`mention-search` is not declared in the component's `defineEmits`. In Vue templates, `@mention-search="handler"` is passed to the component as the `onMentionSearch` function prop.
|
|
898
|
+
|
|
899
|
+
### @ Mention
|
|
900
|
+
|
|
901
|
+
`mention` is disabled by default. Pass `:mention="true"` to enable it. When it is disabled, typing `@` stays as plain text, `@mention-search` is not called, and `insertMention()` returns `false`.
|
|
902
|
+
|
|
903
|
+
Typing `@` opens the mention popup when `mention` is enabled. 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.
|
|
904
|
+
|
|
905
|
+
`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.
|
|
906
|
+
|
|
907
|
+
```vue
|
|
908
|
+
<script setup lang="ts">
|
|
909
|
+
import { ref } from 'vue'
|
|
910
|
+
import type { JSONContent } from '@tiptap/core'
|
|
911
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
912
|
+
import type {
|
|
913
|
+
RichTextEditorMentionItem,
|
|
914
|
+
RichTextEditorMentionProviderPayload,
|
|
915
|
+
} from '@norio-office/rich-text'
|
|
916
|
+
|
|
917
|
+
const content = ref<JSONContent | null>(null)
|
|
918
|
+
|
|
919
|
+
const mentionItems: RichTextEditorMentionItem[] = [
|
|
920
|
+
{ id: 'user-1001', name: 'Cui Guoqiang', type: 1 },
|
|
921
|
+
{ id: 'user-1002', name: 'Liu Jianing', type: 1 },
|
|
922
|
+
{ id: 'doc-2001', name: 'IT Asset Management System', type: 2, tag: 'External', updatedAt: '2025-12-01' },
|
|
923
|
+
{ id: 'doc-2002', name: 'Customer Satisfaction Survey', type: 2, updatedAt: '2025-12-04' },
|
|
924
|
+
]
|
|
925
|
+
|
|
926
|
+
async function handleMentionSearch(payload: RichTextEditorMentionProviderPayload) {
|
|
927
|
+
const query = payload.query.trim().toLowerCase()
|
|
928
|
+
await new Promise((resolve) => window.setTimeout(resolve, 120))
|
|
929
|
+
|
|
930
|
+
return mentionItems.filter((item) => {
|
|
931
|
+
const matchedType = payload.type === 'all' || item.type === payload.type
|
|
932
|
+
const matchedKeyword = !query || item.name.toLowerCase().includes(query)
|
|
933
|
+
return matchedType && matchedKeyword
|
|
934
|
+
})
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
function handleMentionItemClick(item: RichTextEditorMentionItem) {
|
|
938
|
+
console.log('clicked mention item:', item)
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
function handleMentionSubmit(item: RichTextEditorMentionItem) {
|
|
942
|
+
console.log('inserted mention:', item)
|
|
943
|
+
}
|
|
944
|
+
</script>
|
|
945
|
+
|
|
946
|
+
<template>
|
|
947
|
+
<RichTextEditor
|
|
948
|
+
v-model="content"
|
|
949
|
+
:mention="true"
|
|
950
|
+
@mention-search="handleMentionSearch"
|
|
951
|
+
@mention-item-click="handleMentionItemClick"
|
|
952
|
+
@mention-submit="handleMentionSubmit"
|
|
953
|
+
/>
|
|
954
|
+
</template>
|
|
955
|
+
```
|
|
956
|
+
|
|
957
|
+
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.
|
|
958
|
+
|
|
959
|
+
### Custom Labels
|
|
960
|
+
|
|
961
|
+
The default UI labels are Chinese. Use `messages` to override only the keys you need:
|
|
962
|
+
|
|
963
|
+
```vue
|
|
964
|
+
<RichTextEditor
|
|
965
|
+
v-model="content"
|
|
966
|
+
:messages="{
|
|
967
|
+
'insert.localFile': 'Attachment',
|
|
968
|
+
'export.label': 'Download',
|
|
969
|
+
}"
|
|
970
|
+
/>
|
|
971
|
+
```
|
|
972
|
+
|
|
973
|
+
Common message keys:
|
|
974
|
+
|
|
975
|
+
| Key | Default Chinese | English meaning |
|
|
976
|
+
| --- | --- | --- |
|
|
977
|
+
| `insert.label` | 插入 | Insert |
|
|
978
|
+
| `insert.section.general` | 通用 | General |
|
|
979
|
+
| `insert.section.apps` | 小应用 | Apps |
|
|
980
|
+
| `insert.section.external` | 外部内容 | External content |
|
|
981
|
+
| `insert.image` | 图片 | Image |
|
|
982
|
+
| `insert.video` | 视频 | Video |
|
|
983
|
+
| `insert.table` | 表格 | Table |
|
|
984
|
+
| `insert.localFile` | 本地文件 | Local file |
|
|
985
|
+
| `insert.columns` | 分栏 | Columns |
|
|
986
|
+
| `insert.highlightBlock` | 高亮块 | Highlight block |
|
|
987
|
+
| `insert.date` | 日期 | Date |
|
|
988
|
+
| `insert.codeBlock` | 代码块 | Code block |
|
|
989
|
+
| `insert.formula` | 公式 | Formula |
|
|
990
|
+
| `insert.blockquote` | 引用 | Quote |
|
|
991
|
+
| `insert.emoji` | 表情符号 | Emoji |
|
|
992
|
+
| `insert.link` | 超链接 | Link |
|
|
993
|
+
| `insert.divider` | 分隔线 | Divider |
|
|
994
|
+
| `insert.countdown` | 倒计时 | Countdown |
|
|
995
|
+
| `insert.markdownImport` | Markdown 导入 | Markdown import |
|
|
996
|
+
| `export.label` | 导出 | Export |
|
|
997
|
+
| `export.pdf` | 导出 PDF | Export PDF |
|
|
998
|
+
| `export.pdf.loading` | 导出 PDF 中... | Exporting PDF... |
|
|
999
|
+
| `export.html` | 导出 HTML | Export HTML |
|
|
1000
|
+
| `export.html.loading` | 导出 HTML 中... | Exporting HTML... |
|
|
1001
|
+
| `export.image` | 导出图片 | Export image |
|
|
1002
|
+
| `export.image.loading` | 导出图片中... | Exporting image... |
|
|
1003
|
+
| `print.label` | 打印 | Print |
|
|
1004
|
+
| `print.loading` | 打印中... | Printing... |
|
|
1005
|
+
| `quote.apply` | 应用引用 | Apply quote |
|
|
1006
|
+
| `quote.cancel` | 取消引用 | Cancel quote |
|
|
1007
|
+
| `quote.borderColor` | 边框颜色 | Border color |
|
|
1008
|
+
| `quote.backgroundColor` | 背景颜色 | Background color |
|
|
1009
|
+
| `outline.label` | 大纲 | Outline |
|
|
1010
|
+
| `outline.collapse` | 收起大纲 | Collapse outline |
|
|
1011
|
+
| `outline.empty.description` | 对文档内容应用“标题”样式,即可自动生成大纲。 | Apply heading styles to generate an outline. |
|
|
1012
|
+
| `outline.empty.tip` | 点击左下角“大纲”按钮可以随时展开或收起。 | Use the Outline button to show or hide the outline. |
|
|
1013
|
+
| `status.wordCountUnit` | 个字 | characters |
|
|
1014
|
+
| `status.presentation.enter` | 演示 | Present |
|
|
1015
|
+
| `status.presentation.exit` | 退出演示 | Exit presentation |
|
|
1016
|
+
| `status.fullscreen.enter` | 全屏 | Fullscreen |
|
|
1017
|
+
| `status.fullscreen.exit` | 退出全屏 | Exit fullscreen |
|
|
1018
|
+
| `countdown.selectTime` | 请选择时间 | Select time |
|
|
1019
|
+
| `countdown.settingsTitle` | 倒计时设置 | Countdown settings |
|
|
1020
|
+
| `formula.insertTitle` | 插入 LaTeX 公式 | Insert LaTeX formula |
|
|
1021
|
+
|
|
1022
|
+
### Preview Mode
|
|
1023
|
+
|
|
1024
|
+
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.
|
|
1025
|
+
|
|
1026
|
+
```vue
|
|
1027
|
+
<RichTextEditor
|
|
1028
|
+
v-model="content"
|
|
1029
|
+
mode="preview"
|
|
1030
|
+
outline-placement="right"
|
|
1031
|
+
/>
|
|
1032
|
+
```
|
|
1033
|
+
|
|
1034
|
+
On narrow screens, preview mode automatically scales the page canvas so the document stays readable on phones.
|
|
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
|
+
```
|
|
1194
|
+
|
|
1195
|
+
### Instance API
|
|
1196
|
+
|
|
1197
|
+
Use a Vue `ref` to access the editor instance methods:
|
|
1198
|
+
|
|
1199
|
+
```vue
|
|
1200
|
+
<script setup lang="ts">
|
|
1201
|
+
import { ref } from 'vue'
|
|
1202
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
1203
|
+
import type { RichTextEditorInstance } from '@norio-office/rich-text'
|
|
1204
|
+
|
|
1205
|
+
const editorRef = ref<RichTextEditorInstance | null>(null)
|
|
1206
|
+
</script>
|
|
1207
|
+
|
|
1208
|
+
<template>
|
|
1209
|
+
<RichTextEditor ref="editorRef" />
|
|
1210
|
+
</template>
|
|
1211
|
+
```
|
|
1212
|
+
|
|
1213
|
+
Available methods:
|
|
1214
|
+
|
|
1215
|
+
| Method | Return | Description |
|
|
1216
|
+
| --- | --- | --- |
|
|
1217
|
+
| `exportPdf()` | `Promise<Blob \| null>` | Exports a PDF. |
|
|
1218
|
+
| `exportImage(options?)` | `Promise<Blob \| null>` | Exports an image as PNG or JPEG. |
|
|
1219
|
+
| `exportHtml()` | `string \| null` | Exports an HTML string. |
|
|
1220
|
+
| `insertImage(payload)` | `boolean` | Inserts 1 to 4 images. |
|
|
1221
|
+
| `insertVideo(payload)` | `boolean` | Inserts a video block. |
|
|
1222
|
+
| `insertFile(payload)` | `boolean` | Inserts a link/file preview block. |
|
|
1223
|
+
| `insertLocalFile(payload)` | `boolean` | Inserts a local file card. |
|
|
1224
|
+
| `insertMention(payload)` | `boolean` | Inserts one structured inline mention node; returns `false` when `mention` is disabled. |
|
|
1225
|
+
| `openLocalFilePicker()` | `void` | Opens the local file picker. |
|
|
1226
|
+
| `focus()` | `void` | Focuses the editor. |
|
|
1227
|
+
| `getJSON()` | `JSONContent \| null` | Returns the current JSON document. |
|
|
1228
|
+
| `getText()` | `string` | Returns the current plain text content without HTML tags. |
|
|
1229
|
+
| `getImages()` | `RichTextEditorImagePayload[]` | Returns all images in the document. |
|
|
1230
|
+
| `getVideos()` | `RichTextEditorVideoPayload[]` | Returns all videos in the document. |
|
|
1231
|
+
| `getFiles()` | `RichTextEditorCollectedFilePayload[]` | Returns all files in the document. `kind: 'file'` means a link/file preview block, and `kind: 'local-file'` means a local file card. |
|
|
1232
|
+
| `getOutlineItems()` | `RichTextEditorOutlineItem[]` | Returns the current outline items. |
|
|
1233
|
+
| `getActiveOutlinePos()` | `number \| null` | Returns the document position of the active outline item. |
|
|
1234
|
+
| `focusOutlineItem(pos)` | `boolean` | Focuses and scrolls to an outline item. |
|
|
1235
|
+
| `onOutlineChange(handler)` | `() => void` | Subscribes to outline changes and returns an unsubscribe function. |
|
|
1236
|
+
|
|
1237
|
+
### External Outline Component
|
|
1238
|
+
|
|
1239
|
+
The editor renders its built-in outline by default. To place the outline in any sidebar, drawer, or custom layout, hide the built-in panel and pass the editor instance to `RichTextOutline`.
|
|
1240
|
+
|
|
1241
|
+
```vue
|
|
1242
|
+
<script setup lang="ts">
|
|
1243
|
+
import { ref } from 'vue'
|
|
1244
|
+
import { RichTextEditor, RichTextOutline } from '@norio-office/rich-text'
|
|
1245
|
+
import type { RichTextEditorInstance } from '@norio-office/rich-text'
|
|
1246
|
+
|
|
1247
|
+
const editorRef = ref<RichTextEditorInstance | null>(null)
|
|
1248
|
+
</script>
|
|
1249
|
+
|
|
1250
|
+
<template>
|
|
1251
|
+
<RichTextEditor ref="editorRef" v-model="content" :show-outline="false" />
|
|
1252
|
+
<aside class="outline-sidebar">
|
|
1253
|
+
<RichTextOutline :editor="editorRef" />
|
|
1254
|
+
</aside>
|
|
1255
|
+
</template>
|
|
1256
|
+
```
|
|
1257
|
+
|
|
1258
|
+
`RichTextOutline` subscribes to the editor outline state automatically. Clicking an item calls `focusOutlineItem` on the editor instance. The component has a `200px` minimum height and grows with its content by default; wrap it with your own container when you need a fixed height or internal scrolling. Optional props: `open`, `placement`, `title`, `collapseTitle`, `emptyDescription`, `emptyTip`, and `showCollapse`. Events: `select`, `toggle`, and `change`.
|
|
1259
|
+
|
|
1260
|
+
### Upload And Insert
|
|
1261
|
+
|
|
1262
|
+
Recommended integration flow:
|
|
1263
|
+
|
|
1264
|
+
1. Upload the asset from your own business layer.
|
|
1265
|
+
2. Wait for the API to return the final accessible URL.
|
|
1266
|
+
3. Call the editor instance method to insert the returned content.
|
|
1267
|
+
|
|
1268
|
+
The built-in image, video, and local-file pickers, as well as page-level drag
|
|
1269
|
+
upload, follow the same boundary: the editor only passes the selected `File` to
|
|
1270
|
+
`uploadImage`, `uploadVideo`, or `uploadFile`, then inserts the URL returned by
|
|
1271
|
+
that hook. The package does not upload to a business API by itself and does not
|
|
1272
|
+
fall back to base64/blob URLs.
|
|
1273
|
+
|
|
1274
|
+
Page-level drag upload routes files by MIME type: `image/*` uses image upload,
|
|
1275
|
+
`video/*` uses video upload, and every other file type uses `uploadFile` and is
|
|
1276
|
+
inserted as a local file card.
|
|
1277
|
+
|
|
1278
|
+
```vue
|
|
1279
|
+
<script setup lang="ts">
|
|
1280
|
+
import type {
|
|
1281
|
+
RichTextEditorUploadInput,
|
|
1282
|
+
RichTextEditorUploadResult,
|
|
1283
|
+
RichTextEditorUploadErrorPayload,
|
|
1284
|
+
} from '@norio-office/rich-text'
|
|
1285
|
+
|
|
1286
|
+
async function uploadToServer(payload: RichTextEditorUploadInput): Promise<RichTextEditorUploadResult> {
|
|
1287
|
+
const form = new FormData()
|
|
1288
|
+
form.append('file', payload.file)
|
|
1289
|
+
form.append('kind', payload.kind)
|
|
1290
|
+
|
|
1291
|
+
const response = await fetch('/api/upload', {
|
|
1292
|
+
method: 'POST',
|
|
1293
|
+
body: form,
|
|
1294
|
+
})
|
|
1295
|
+
|
|
1296
|
+
if (!response.ok) {
|
|
1297
|
+
throw new Error('Upload failed')
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
return await response.json()
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
function handleUploadError(payload: RichTextEditorUploadErrorPayload) {
|
|
1304
|
+
console.error(payload.kind, payload.fileName, payload.error)
|
|
1305
|
+
}
|
|
1306
|
+
</script>
|
|
1307
|
+
|
|
1308
|
+
<template>
|
|
1309
|
+
<RichTextEditor
|
|
1310
|
+
v-model="content"
|
|
1311
|
+
:upload-image="uploadToServer"
|
|
1312
|
+
:upload-video="uploadToServer"
|
|
1313
|
+
:upload-file="uploadToServer"
|
|
1314
|
+
@local-file-click="(file) => console.log('file clicked', file)"
|
|
1315
|
+
@upload-error="handleUploadError"
|
|
1316
|
+
/>
|
|
1317
|
+
</template>
|
|
1318
|
+
```
|
|
1319
|
+
|
|
1320
|
+
Both the `onUploadError` callback prop and the `@upload-error` event can receive upload failures. In most apps, choose one style to avoid duplicate business handling.
|
|
1321
|
+
|
|
1322
|
+
Insert image:
|
|
1323
|
+
|
|
1324
|
+
```ts
|
|
1325
|
+
async function uploadImage(file: File) {
|
|
1326
|
+
const imageUrl = await yourUploadApi(file)
|
|
1327
|
+
|
|
1328
|
+
editorRef.value?.insertImage({
|
|
1329
|
+
src: imageUrl,
|
|
1330
|
+
name: file.name,
|
|
1331
|
+
alt: file.name,
|
|
1332
|
+
description: '',
|
|
1333
|
+
})
|
|
1334
|
+
}
|
|
1335
|
+
```
|
|
1336
|
+
|
|
1337
|
+
You can insert up to 4 images in one call:
|
|
1338
|
+
|
|
1339
|
+
```ts
|
|
1340
|
+
editorRef.value?.insertImage([
|
|
1341
|
+
{ src: 'https://cdn.example.com/a.png', name: 'a.png' },
|
|
1342
|
+
{ src: 'https://cdn.example.com/b.png', name: 'b.png' },
|
|
1343
|
+
])
|
|
1344
|
+
```
|
|
1345
|
+
|
|
1346
|
+
Insert video:
|
|
1347
|
+
|
|
1348
|
+
```ts
|
|
1349
|
+
async function uploadVideo(file: File) {
|
|
1350
|
+
const videoUrl = await yourUploadApi(file)
|
|
1351
|
+
|
|
1352
|
+
editorRef.value?.insertVideo({
|
|
1353
|
+
src: videoUrl,
|
|
1354
|
+
name: file.name,
|
|
1355
|
+
mimeType: file.type || 'video/mp4',
|
|
1356
|
+
description: 'video description',
|
|
1357
|
+
})
|
|
1358
|
+
}
|
|
1359
|
+
```
|
|
1360
|
+
|
|
1361
|
+
Insert file link or preview:
|
|
1362
|
+
|
|
1363
|
+
```ts
|
|
1364
|
+
async function uploadFile(file: File) {
|
|
1365
|
+
const fileUrl = await yourUploadApi(file)
|
|
1366
|
+
|
|
1367
|
+
editorRef.value?.insertFile({
|
|
1368
|
+
url: fileUrl,
|
|
1369
|
+
name: file.name,
|
|
1370
|
+
displayMode: 'text',
|
|
1371
|
+
})
|
|
1372
|
+
}
|
|
1373
|
+
```
|
|
1374
|
+
|
|
1375
|
+
Insert a local file card:
|
|
1376
|
+
|
|
1377
|
+
```ts
|
|
1378
|
+
editorRef.value?.insertLocalFile({
|
|
1379
|
+
url: 'https://cdn.example.com/files/demo.txt',
|
|
1380
|
+
name: 'demo.txt',
|
|
1381
|
+
size: 2048,
|
|
1382
|
+
mimeType: 'text/plain',
|
|
1383
|
+
})
|
|
1384
|
+
```
|
|
1385
|
+
|
|
1386
|
+
### Export Example
|
|
1387
|
+
|
|
1388
|
+
```ts
|
|
1389
|
+
async function handleExportPdf() {
|
|
1390
|
+
const blob = await editorRef.value?.exportPdf()
|
|
1391
|
+
if (!blob) return
|
|
1392
|
+
|
|
1393
|
+
const url = URL.createObjectURL(blob)
|
|
1394
|
+
const link = document.createElement('a')
|
|
1395
|
+
link.href = url
|
|
1396
|
+
link.download = 'demo.pdf'
|
|
1397
|
+
link.click()
|
|
1398
|
+
URL.revokeObjectURL(url)
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
async function handleExportImage() {
|
|
1402
|
+
const blob = await editorRef.value?.exportImage({
|
|
1403
|
+
type: 'image/png',
|
|
1404
|
+
})
|
|
1405
|
+
if (!blob) return
|
|
1406
|
+
|
|
1407
|
+
const url = URL.createObjectURL(blob)
|
|
1408
|
+
window.open(url, '_blank')
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
function handleExportHtml() {
|
|
1412
|
+
const html = editorRef.value?.exportHtml()
|
|
1413
|
+
if (!html) return
|
|
1414
|
+
|
|
1415
|
+
console.log(html)
|
|
1416
|
+
}
|
|
1417
|
+
```
|
|
1418
|
+
|
|
1419
|
+
### Type Imports
|
|
1420
|
+
|
|
1421
|
+
```ts
|
|
1422
|
+
import type {
|
|
1423
|
+
OfficeColorIconProps,
|
|
1424
|
+
OfficeIconProps,
|
|
1425
|
+
RichTextEditorAlign,
|
|
1426
|
+
RichTextEditorCollaborationAwareness,
|
|
1427
|
+
RichTextEditorCollaborationDocument,
|
|
1428
|
+
RichTextEditorCollaborationProvider,
|
|
1429
|
+
RichTextEditorCollaborationUser,
|
|
1430
|
+
RichTextEditorCollaborationOptions,
|
|
1431
|
+
RichTextEditorCollectedFilePayload,
|
|
1432
|
+
RichTextEditorExportItemKey,
|
|
1433
|
+
RichTextEditorFilePayload,
|
|
1434
|
+
RichTextEditorImageExportOptions,
|
|
1435
|
+
RichTextEditorImagePayload,
|
|
1436
|
+
RichTextEditorInsertMenuItemKey,
|
|
1437
|
+
RichTextEditorInstance,
|
|
1438
|
+
RichTextEditorLocalFilePayload,
|
|
1439
|
+
RichTextEditorMentionItem,
|
|
1440
|
+
RichTextEditorMentionProvider,
|
|
1441
|
+
RichTextEditorMentionProviderPayload,
|
|
1442
|
+
RichTextEditorMentionType,
|
|
1443
|
+
RichTextEditorMessages,
|
|
1444
|
+
RichTextEditorOutlineChangeHandler,
|
|
1445
|
+
RichTextEditorOutlineItem,
|
|
1446
|
+
RichTextEditorOutlineState,
|
|
1447
|
+
RichTextEditorProps,
|
|
1448
|
+
RichTextEditorToolbarActionKey,
|
|
1449
|
+
RichTextEditorUploadErrorHandler,
|
|
1450
|
+
RichTextEditorUploadErrorPayload,
|
|
1451
|
+
RichTextEditorUploadHook,
|
|
1452
|
+
RichTextEditorUploadInput,
|
|
1453
|
+
RichTextEditorUploadKind,
|
|
1454
|
+
RichTextEditorUploadResult,
|
|
1455
|
+
RichTextEditorWatermarkOptions,
|
|
1456
|
+
RichTextEditorVideoPayload,
|
|
1457
|
+
} from '@norio-office/rich-text'
|
|
1458
|
+
```
|
|
1459
|
+
|
|
1460
|
+
### Package Exports
|
|
1461
|
+
|
|
1462
|
+
```ts
|
|
1463
|
+
import RichTextEditor, {
|
|
1464
|
+
RichTextEditor as NamedRichTextEditor,
|
|
1465
|
+
RichTextOutline,
|
|
1466
|
+
OfficeIcon,
|
|
1467
|
+
OfficeColorIcon,
|
|
1468
|
+
ScrollArea,
|
|
1469
|
+
monoIconNames,
|
|
1470
|
+
colorIconNames,
|
|
1471
|
+
} from '@norio-office/rich-text'
|
|
1472
|
+
```
|
|
1473
|
+
|
|
1474
|
+
### Package Docs
|
|
1475
|
+
|
|
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` 可从外部定位到指定评论锚点。
|