@norio-office/rich-text 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +971 -0
- package/dist/chunk-efA98nb6.js +13 -0
- package/dist/components/ColumnsBlockView.vue.d.ts +26 -0
- package/dist/components/CountdownBlockView.vue.d.ts +10 -0
- package/dist/components/EmojiPickerPanel.vue.d.ts +11 -0
- package/dist/components/FormulaBlockView.vue.d.ts +3 -0
- package/dist/components/FormulaEditorDialog.vue.d.ts +20 -0
- package/dist/components/HighlightBlockView.vue.d.ts +28 -0
- package/dist/components/ImageBlockView.vue.d.ts +6 -0
- package/dist/components/LinkBlockView.vue.d.ts +5 -0
- package/dist/components/LocalFileBlockView.vue.d.ts +3 -0
- package/dist/components/MentionInlineView.vue.d.ts +3 -0
- package/dist/components/OfficeCodeBlockView.vue.d.ts +28 -0
- package/dist/components/OfficeColorIcon.vue.d.ts +10 -0
- package/dist/components/OfficeIcon.vue.d.ts +12 -0
- package/dist/components/OutlinePanel.vue.d.ts +21 -0
- package/dist/components/RichTextEditor.vue.d.ts +69 -0
- package/dist/components/ScrollArea.vue.d.ts +24 -0
- package/dist/components/VideoBlockView.vue.d.ts +7 -0
- package/dist/composables/useRichTextEditor.d.ts +20 -0
- package/dist/data/emoji.d.ts +13 -0
- package/dist/extensions/columns-block.d.ts +2 -0
- package/dist/extensions/columns-column.d.ts +2 -0
- package/dist/extensions/countdown-block.d.ts +2 -0
- package/dist/extensions/formula-block.d.ts +2 -0
- package/dist/extensions/highlight-block.d.ts +2 -0
- package/dist/extensions/image-block.d.ts +2 -0
- package/dist/extensions/indent.d.ts +17 -0
- package/dist/extensions/link-block.d.ts +2 -0
- package/dist/extensions/local-file-block.d.ts +2 -0
- package/dist/extensions/mention.d.ts +2 -0
- package/dist/extensions/office-code-block.d.ts +1 -0
- package/dist/extensions/quote-style.d.ts +19 -0
- package/dist/extensions/text-align.d.ts +12 -0
- package/dist/extensions/video-block.d.ts +2 -0
- package/dist/html2canvas-Bwmq6fGh.js +4025 -0
- package/dist/icons.d.ts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.es-C4X5YiRo.js +5751 -0
- package/dist/index.js +38311 -0
- package/dist/index.umd.cjs +6652 -0
- package/dist/jspdf.es.min-ByaXFVjc.js +18214 -0
- package/dist/locales/editor.d.ts +5 -0
- package/dist/purify.es-B5ixYH9L.js +549 -0
- package/dist/style.css +2 -0
- package/dist/typeof-Dz1Ap89p.js +11 -0
- package/dist/types.d.ts +118 -0
- package/dist/utils/columns.d.ts +4 -0
- package/dist/utils/markdown-import.d.ts +2 -0
- package/dist/utils/mention.d.ts +6 -0
- package/docs/usage.md +95 -0
- package/package.json +73 -0
- package/yjs/README.md +63 -0
- package/yjs/index.js +224 -0
- package/yjs/package.json +14 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region ../../node_modules/@babel/runtime/helpers/esm/typeof.js
|
|
2
|
+
function e(t) {
|
|
3
|
+
"@babel/helpers - typeof";
|
|
4
|
+
return e = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(e) {
|
|
5
|
+
return typeof e;
|
|
6
|
+
} : function(e) {
|
|
7
|
+
return e && typeof Symbol == "function" && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
|
|
8
|
+
}, e(t);
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { e as t };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { JSONContent } from '@tiptap/core';
|
|
2
|
+
export type RichTextEditorProps = {
|
|
3
|
+
modelValue?: JSONContent | null;
|
|
4
|
+
editable?: boolean;
|
|
5
|
+
mode?: 'edit' | 'preview';
|
|
6
|
+
outlinePlacement?: 'left' | 'right';
|
|
7
|
+
messages?: RichTextEditorMessages | null;
|
|
8
|
+
enabledExportItems?: RichTextEditorExportItemKey[] | null;
|
|
9
|
+
enabledInsertMenuItems?: RichTextEditorInsertMenuItemKey[] | null;
|
|
10
|
+
enabledToolbarActions?: RichTextEditorToolbarActionKey[] | null;
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
collaboration?: RichTextEditorCollaborationOptions | null;
|
|
13
|
+
mentionProvider?: RichTextEditorMentionProvider | null;
|
|
14
|
+
onMentionSearch?: RichTextEditorMentionProvider | null;
|
|
15
|
+
};
|
|
16
|
+
export type RichTextEditorMessages = Partial<Record<string, string>>;
|
|
17
|
+
export type RichTextEditorExportItemKey = 'pdf' | 'html' | 'image' | 'print';
|
|
18
|
+
export type RichTextEditorInsertMenuItemKey = 'image' | 'video' | 'table' | 'local-file' | 'columns' | 'highlight-block' | 'date' | 'code-block' | 'formula' | 'blockquote' | 'emoji' | 'link' | 'divider' | 'countdown' | 'markdown-import';
|
|
19
|
+
export type RichTextEditorToolbarActionKey = 'blockquote';
|
|
20
|
+
export type RichTextEditorCollaborationDocument = {
|
|
21
|
+
getXmlFragment: (field: string) => unknown;
|
|
22
|
+
};
|
|
23
|
+
export type RichTextEditorCollaborationAwareness = {
|
|
24
|
+
states: Map<number, unknown>;
|
|
25
|
+
on: (event: 'update', callback: (...args: unknown[]) => void) => void;
|
|
26
|
+
setLocalStateField: (field: string, value: unknown) => void;
|
|
27
|
+
};
|
|
28
|
+
export type RichTextEditorCollaborationProvider = {
|
|
29
|
+
doc?: unknown;
|
|
30
|
+
awareness: RichTextEditorCollaborationAwareness;
|
|
31
|
+
};
|
|
32
|
+
export type RichTextEditorCollaborationUser = {
|
|
33
|
+
name: string;
|
|
34
|
+
color: string;
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
};
|
|
37
|
+
export type RichTextEditorCollaborationOptions = {
|
|
38
|
+
document: RichTextEditorCollaborationDocument;
|
|
39
|
+
field?: string;
|
|
40
|
+
provider?: RichTextEditorCollaborationProvider | null;
|
|
41
|
+
user?: RichTextEditorCollaborationUser | null;
|
|
42
|
+
};
|
|
43
|
+
export type RichTextEditorMentionType = 1 | 2;
|
|
44
|
+
export type RichTextEditorMentionItem = {
|
|
45
|
+
id: string;
|
|
46
|
+
name: string;
|
|
47
|
+
type: RichTextEditorMentionType;
|
|
48
|
+
avatar?: string;
|
|
49
|
+
icon?: string;
|
|
50
|
+
tag?: string;
|
|
51
|
+
updatedAt?: string;
|
|
52
|
+
[key: string]: unknown;
|
|
53
|
+
};
|
|
54
|
+
export type RichTextEditorMentionProviderPayload = {
|
|
55
|
+
query: string;
|
|
56
|
+
type?: RichTextEditorMentionType | 'all';
|
|
57
|
+
};
|
|
58
|
+
export type RichTextEditorMentionProvider = (payload: RichTextEditorMentionProviderPayload) => RichTextEditorMentionItem[] | Promise<RichTextEditorMentionItem[]>;
|
|
59
|
+
export type RichTextEditorAlign = 'left' | 'center' | 'right';
|
|
60
|
+
export type RichTextEditorImagePayload = {
|
|
61
|
+
src: string;
|
|
62
|
+
alt?: string;
|
|
63
|
+
name?: string;
|
|
64
|
+
description?: string;
|
|
65
|
+
link?: string;
|
|
66
|
+
rotation?: number;
|
|
67
|
+
};
|
|
68
|
+
export type RichTextEditorVideoPayload = {
|
|
69
|
+
src: string;
|
|
70
|
+
name?: string;
|
|
71
|
+
description?: string;
|
|
72
|
+
mimeType?: string;
|
|
73
|
+
align?: RichTextEditorAlign;
|
|
74
|
+
widthPercent?: number;
|
|
75
|
+
height?: number;
|
|
76
|
+
};
|
|
77
|
+
export type RichTextEditorFilePayload = {
|
|
78
|
+
url: string;
|
|
79
|
+
name: string;
|
|
80
|
+
displayMode?: 'text' | 'card' | 'preview';
|
|
81
|
+
align?: RichTextEditorAlign;
|
|
82
|
+
widthPercent?: number;
|
|
83
|
+
height?: number;
|
|
84
|
+
};
|
|
85
|
+
export type RichTextEditorLocalFilePayload = {
|
|
86
|
+
url: string;
|
|
87
|
+
name: string;
|
|
88
|
+
size?: number;
|
|
89
|
+
mimeType?: string;
|
|
90
|
+
};
|
|
91
|
+
export type RichTextEditorImageExportOptions = {
|
|
92
|
+
type?: 'image/png' | 'image/jpeg';
|
|
93
|
+
quality?: number;
|
|
94
|
+
};
|
|
95
|
+
export type RichTextEditorInstance = {
|
|
96
|
+
exportPdf: () => Promise<Blob | null>;
|
|
97
|
+
exportImage: (options?: RichTextEditorImageExportOptions) => Promise<Blob | null>;
|
|
98
|
+
exportHtml: () => string | null;
|
|
99
|
+
insertImage: (payload: RichTextEditorImagePayload | RichTextEditorImagePayload[]) => boolean;
|
|
100
|
+
insertVideo: (payload: RichTextEditorVideoPayload) => boolean;
|
|
101
|
+
insertFile: (payload: RichTextEditorFilePayload) => boolean;
|
|
102
|
+
insertLocalFile: (payload: RichTextEditorLocalFilePayload) => boolean;
|
|
103
|
+
insertMention: (payload: RichTextEditorMentionItem) => boolean;
|
|
104
|
+
openLocalFilePicker: () => void;
|
|
105
|
+
focus: () => void;
|
|
106
|
+
getJSON: () => JSONContent | null;
|
|
107
|
+
};
|
|
108
|
+
export type OfficeIconProps = {
|
|
109
|
+
name: string;
|
|
110
|
+
size?: number | string;
|
|
111
|
+
color?: string;
|
|
112
|
+
backgroundColor?: string;
|
|
113
|
+
};
|
|
114
|
+
export type OfficeColorIconProps = {
|
|
115
|
+
name: string;
|
|
116
|
+
size?: number | string;
|
|
117
|
+
backgroundColor?: string;
|
|
118
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const mentionAvatarColors: string[];
|
|
2
|
+
export declare function getMentionAvatarText(name: string): string;
|
|
3
|
+
export declare function getMentionAvatarColor(id: string): string;
|
|
4
|
+
export declare function getMentionAvatarStyle(id: string): {
|
|
5
|
+
backgroundColor: string;
|
|
6
|
+
};
|
package/docs/usage.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Package Usage
|
|
2
|
+
|
|
3
|
+
This file is shipped with the npm package so consumers can view the usage guide locally after installation.
|
|
4
|
+
|
|
5
|
+
See the package root `README.md` for the primary integration guide.
|
|
6
|
+
|
|
7
|
+
## Quick Start
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { RichTextEditor } from '@norio-office/rich-text'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Main Capabilities
|
|
14
|
+
|
|
15
|
+
- rich text editing with a built-in toolbar UI
|
|
16
|
+
- optional Yjs collaboration mode with the same component API
|
|
17
|
+
- async `@` mention candidates for people and documents
|
|
18
|
+
- export to PDF, image, and HTML through instance methods
|
|
19
|
+
- external upload integration for image, video, and file insertion
|
|
20
|
+
- typed public instance API for Vue `ref` usage
|
|
21
|
+
|
|
22
|
+
## Mention
|
|
23
|
+
|
|
24
|
+
Provide `@mention-search` to load people and document candidates when the user types `@`. The handler may return an array or a `Promise`.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import type { RichTextEditorMentionItem, RichTextEditorMentionProviderPayload } from '@norio-office/rich-text'
|
|
28
|
+
|
|
29
|
+
async function handleMentionSearch(payload: RichTextEditorMentionProviderPayload): Promise<RichTextEditorMentionItem[]> {
|
|
30
|
+
return [
|
|
31
|
+
{ id: 'u-1', name: '崔国强', type: 1, avatar: 'https://cdn.xxx.com/avatar.png' },
|
|
32
|
+
{ id: 'd-1', name: 'IT资产管理系统', type: 2, icon: 'https://cdn.xxx.com/doc.png', updatedAt: '2025年12月1日' },
|
|
33
|
+
].filter((item) => payload.type === 'all' || !payload.type || item.type === payload.type)
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```vue
|
|
38
|
+
<RichTextEditor
|
|
39
|
+
v-model="content"
|
|
40
|
+
@mention-search="handleMentionSearch"
|
|
41
|
+
@mention-item-click="handleMentionItemClick"
|
|
42
|
+
@mention-submit="handleMentionSubmit"
|
|
43
|
+
/>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`type: 1` means person and `type: 2` means document. The editor inserts a mention only after the user clicks `提及`.
|
|
47
|
+
|
|
48
|
+
## Collaboration
|
|
49
|
+
|
|
50
|
+
Use the optional `collaboration` prop to bind the editor to a Yjs document.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import * as Y from 'yjs'
|
|
54
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
55
|
+
|
|
56
|
+
const ydoc = new Y.Doc()
|
|
57
|
+
const provider = new WebsocketProvider('ws://localhost:1234', 'office-word-demo', ydoc)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```vue
|
|
61
|
+
<RichTextEditor
|
|
62
|
+
v-model="content"
|
|
63
|
+
:collaboration="{
|
|
64
|
+
document: ydoc,
|
|
65
|
+
field: 'content',
|
|
66
|
+
provider,
|
|
67
|
+
user: {
|
|
68
|
+
name: '张三',
|
|
69
|
+
color: '#3b82f6',
|
|
70
|
+
},
|
|
71
|
+
}"
|
|
72
|
+
/>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
When `collaboration` is omitted, the editor stays in normal single-user mode. When `provider + user` are passed, the editor also renders remote cursors and remote selections.
|
|
76
|
+
|
|
77
|
+
## Bundled Yjs Server
|
|
78
|
+
|
|
79
|
+
The npm package also ships a minimal websocket server example in `yjs/`.
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
cd node_modules/@norio-office/rich-text/yjs
|
|
83
|
+
npm install
|
|
84
|
+
npm run start
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Client side:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import * as Y from 'yjs'
|
|
91
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
92
|
+
|
|
93
|
+
const ydoc = new Y.Doc()
|
|
94
|
+
const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'office-word-demo', ydoc)
|
|
95
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@norio-office/rich-text",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A reusable Vue 3 rich text editor component built with Tiptap 3.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.umd.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.umd.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./style.css": "./dist/style.css"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md",
|
|
20
|
+
"docs",
|
|
21
|
+
"yjs",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": [
|
|
25
|
+
"*.css"
|
|
26
|
+
],
|
|
27
|
+
"keywords": [
|
|
28
|
+
"vue3",
|
|
29
|
+
"tiptap",
|
|
30
|
+
"rich-text-editor",
|
|
31
|
+
"component-library"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"homepage": "https://cgq001.github.io/office-docs/",
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public",
|
|
37
|
+
"registry": "https://registry.npmjs.org/"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@tiptap/core": "^3.22.1",
|
|
41
|
+
"@tiptap/extension-code-block": "^3.22.1",
|
|
42
|
+
"@tiptap/extension-code-block-lowlight": "^3.22.1",
|
|
43
|
+
"@tiptap/extension-collaboration": "^3.22.2",
|
|
44
|
+
"@tiptap/extension-collaboration-caret": "^3.22.2",
|
|
45
|
+
"@tiptap/extension-font-family": "^3.22.1",
|
|
46
|
+
"@tiptap/extension-placeholder": "^3.22.1",
|
|
47
|
+
"@tiptap/extension-subscript": "^3.22.1",
|
|
48
|
+
"@tiptap/extension-superscript": "^3.22.1",
|
|
49
|
+
"@tiptap/extension-table": "^3.22.1",
|
|
50
|
+
"@tiptap/extension-table-cell": "^3.22.1",
|
|
51
|
+
"@tiptap/extension-table-header": "^3.22.1",
|
|
52
|
+
"@tiptap/extension-table-row": "^3.22.1",
|
|
53
|
+
"@tiptap/extension-task-item": "^3.22.1",
|
|
54
|
+
"@tiptap/extension-task-list": "^3.22.1",
|
|
55
|
+
"@tiptap/extension-text-style": "^3.22.1",
|
|
56
|
+
"@tiptap/extension-underline": "^3.22.1",
|
|
57
|
+
"@tiptap/pm": "^3.22.1",
|
|
58
|
+
"@tiptap/starter-kit": "^3.22.1",
|
|
59
|
+
"@tiptap/vue-3": "^3.22.1",
|
|
60
|
+
"@tiptap/y-tiptap": "^3.0.2",
|
|
61
|
+
"lowlight": "^3.3.0",
|
|
62
|
+
"vue": "^3.5.0",
|
|
63
|
+
"y-prosemirror": "^1.2.6",
|
|
64
|
+
"yjs": "^13.6.30"
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"html2canvas": "^1.4.1",
|
|
68
|
+
"jspdf": "^4.2.1",
|
|
69
|
+
"katex": "^0.16.44",
|
|
70
|
+
"marked": "^17.0.5",
|
|
71
|
+
"plyr": "^3.8.4"
|
|
72
|
+
}
|
|
73
|
+
}
|
package/yjs/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Yjs Server Example
|
|
2
|
+
|
|
3
|
+
This folder is shipped with the npm package so consumers can run a minimal `y-websocket`-compatible Yjs server without adding another backend dependency.
|
|
4
|
+
|
|
5
|
+
## Files
|
|
6
|
+
|
|
7
|
+
- `index.js`: minimal websocket server compatible with `y-websocket`
|
|
8
|
+
- `package.json`: runtime dependencies and `npm run start`
|
|
9
|
+
|
|
10
|
+
## Start
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
cd yjs
|
|
14
|
+
npm install
|
|
15
|
+
npm run start
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
By default the server listens on `ws://0.0.0.0:1234`.
|
|
19
|
+
|
|
20
|
+
To override host or port:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
HOST=0.0.0.0 PORT=1234 npm run start
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
PowerShell:
|
|
27
|
+
|
|
28
|
+
```powershell
|
|
29
|
+
$env:HOST="0.0.0.0"
|
|
30
|
+
$env:PORT="1234"
|
|
31
|
+
npm run start
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Client Link Example
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import * as Y from 'yjs'
|
|
38
|
+
import { WebsocketProvider } from 'y-websocket'
|
|
39
|
+
|
|
40
|
+
const ydoc = new Y.Doc()
|
|
41
|
+
const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'office-word-demo', ydoc)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```vue
|
|
45
|
+
<RichTextEditor
|
|
46
|
+
v-model="content"
|
|
47
|
+
:collaboration="{
|
|
48
|
+
document: ydoc,
|
|
49
|
+
field: 'content',
|
|
50
|
+
provider,
|
|
51
|
+
user: {
|
|
52
|
+
name: '张三',
|
|
53
|
+
color: '#3b82f6',
|
|
54
|
+
},
|
|
55
|
+
}"
|
|
56
|
+
/>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Notes
|
|
60
|
+
|
|
61
|
+
- all collaborators must use the same websocket address and the same room name
|
|
62
|
+
- if clients are on different machines, do not use `127.0.0.1`; use the server's real LAN or public address
|
|
63
|
+
- this sample server is in-memory only; document state is lost after restart
|
package/yjs/index.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import http from 'node:http'
|
|
2
|
+
|
|
3
|
+
import * as decoding from 'lib0/decoding'
|
|
4
|
+
import * as encoding from 'lib0/encoding'
|
|
5
|
+
import * as awarenessProtocol from 'y-protocols/awareness'
|
|
6
|
+
import * as syncProtocol from 'y-protocols/sync'
|
|
7
|
+
import WebSocket, { WebSocketServer } from 'ws'
|
|
8
|
+
import * as Y from 'yjs'
|
|
9
|
+
|
|
10
|
+
const host = process.env.HOST || '0.0.0.0'
|
|
11
|
+
const port = Number(process.env.PORT || 1234)
|
|
12
|
+
|
|
13
|
+
const messageSync = 0
|
|
14
|
+
const messageAwareness = 1
|
|
15
|
+
const pingTimeout = 30000
|
|
16
|
+
|
|
17
|
+
const docs = new Map()
|
|
18
|
+
|
|
19
|
+
function getDoc(name) {
|
|
20
|
+
const existing = docs.get(name)
|
|
21
|
+
if (existing) {
|
|
22
|
+
return existing
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const doc = new Y.Doc()
|
|
26
|
+
const awareness = new awarenessProtocol.Awareness(doc)
|
|
27
|
+
const conns = new Map()
|
|
28
|
+
|
|
29
|
+
awareness.setLocalState(null)
|
|
30
|
+
|
|
31
|
+
doc.on('update', (update) => {
|
|
32
|
+
const encoder = encoding.createEncoder()
|
|
33
|
+
encoding.writeVarUint(encoder, messageSync)
|
|
34
|
+
syncProtocol.writeUpdate(encoder, update)
|
|
35
|
+
const message = encoding.toUint8Array(encoder)
|
|
36
|
+
|
|
37
|
+
conns.forEach((_, conn) => {
|
|
38
|
+
send(conn, message)
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
awareness.on('update', ({ added, updated, removed }, conn) => {
|
|
43
|
+
const changedClients = added.concat(updated, removed)
|
|
44
|
+
|
|
45
|
+
if (conn) {
|
|
46
|
+
const controlledIds = conns.get(conn)
|
|
47
|
+
if (controlledIds) {
|
|
48
|
+
added.forEach((id) => controlledIds.add(id))
|
|
49
|
+
removed.forEach((id) => controlledIds.delete(id))
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const encoder = encoding.createEncoder()
|
|
54
|
+
encoding.writeVarUint(encoder, messageAwareness)
|
|
55
|
+
encoding.writeVarUint8Array(
|
|
56
|
+
encoder,
|
|
57
|
+
awarenessProtocol.encodeAwarenessUpdate(awareness, changedClients),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const message = encoding.toUint8Array(encoder)
|
|
61
|
+
conns.forEach((_, ws) => {
|
|
62
|
+
send(ws, message)
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const entry = { name, doc, awareness, conns }
|
|
67
|
+
docs.set(name, entry)
|
|
68
|
+
return entry
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function removeDocIfEmpty(entry) {
|
|
72
|
+
if (entry.conns.size === 0) {
|
|
73
|
+
entry.doc.destroy()
|
|
74
|
+
docs.delete(entry.name)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function closeConn(entry, conn) {
|
|
79
|
+
const controlledIds = entry.conns.get(conn)
|
|
80
|
+
|
|
81
|
+
if (controlledIds) {
|
|
82
|
+
entry.conns.delete(conn)
|
|
83
|
+
awarenessProtocol.removeAwarenessStates(
|
|
84
|
+
entry.awareness,
|
|
85
|
+
Array.from(controlledIds),
|
|
86
|
+
null,
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
conn.close()
|
|
92
|
+
} catch {
|
|
93
|
+
// ignore close failures
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
removeDocIfEmpty(entry)
|
|
97
|
+
console.log(`[disconnect] room=${entry.name} clients=${entry.conns.size}`)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function send(conn, message) {
|
|
101
|
+
if (conn.readyState !== WebSocket.OPEN && conn.readyState !== WebSocket.CONNECTING) {
|
|
102
|
+
try {
|
|
103
|
+
conn.close()
|
|
104
|
+
} catch {
|
|
105
|
+
// ignore close failures
|
|
106
|
+
}
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
conn.send(message, (error) => {
|
|
111
|
+
if (error) {
|
|
112
|
+
try {
|
|
113
|
+
conn.close()
|
|
114
|
+
} catch {
|
|
115
|
+
// ignore close failures
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function setupWSConnection(conn, req) {
|
|
122
|
+
const docName = (req.url || '/').slice(1).split('?')[0] || 'default'
|
|
123
|
+
const entry = getDoc(docName)
|
|
124
|
+
|
|
125
|
+
entry.conns.set(conn, new Set())
|
|
126
|
+
conn.binaryType = 'arraybuffer'
|
|
127
|
+
|
|
128
|
+
conn.on('message', (message) => {
|
|
129
|
+
try {
|
|
130
|
+
const decoder = decoding.createDecoder(new Uint8Array(message))
|
|
131
|
+
const encoder = encoding.createEncoder()
|
|
132
|
+
const messageType = decoding.readVarUint(decoder)
|
|
133
|
+
|
|
134
|
+
switch (messageType) {
|
|
135
|
+
case messageSync:
|
|
136
|
+
encoding.writeVarUint(encoder, messageSync)
|
|
137
|
+
syncProtocol.readSyncMessage(decoder, encoder, entry.doc, conn)
|
|
138
|
+
if (encoding.length(encoder) > 1) {
|
|
139
|
+
send(conn, encoding.toUint8Array(encoder))
|
|
140
|
+
}
|
|
141
|
+
break
|
|
142
|
+
case messageAwareness:
|
|
143
|
+
awarenessProtocol.applyAwarenessUpdate(
|
|
144
|
+
entry.awareness,
|
|
145
|
+
decoding.readVarUint8Array(decoder),
|
|
146
|
+
conn,
|
|
147
|
+
)
|
|
148
|
+
break
|
|
149
|
+
default:
|
|
150
|
+
break
|
|
151
|
+
}
|
|
152
|
+
} catch (error) {
|
|
153
|
+
console.error('[message-error]', error)
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
let pongReceived = true
|
|
158
|
+
const pingInterval = setInterval(() => {
|
|
159
|
+
if (!pongReceived) {
|
|
160
|
+
clearInterval(pingInterval)
|
|
161
|
+
closeConn(entry, conn)
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
pongReceived = false
|
|
166
|
+
try {
|
|
167
|
+
conn.ping()
|
|
168
|
+
} catch {
|
|
169
|
+
clearInterval(pingInterval)
|
|
170
|
+
closeConn(entry, conn)
|
|
171
|
+
}
|
|
172
|
+
}, pingTimeout)
|
|
173
|
+
|
|
174
|
+
conn.on('pong', () => {
|
|
175
|
+
pongReceived = true
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
conn.on('close', () => {
|
|
179
|
+
clearInterval(pingInterval)
|
|
180
|
+
closeConn(entry, conn)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
conn.on('error', (error) => {
|
|
184
|
+
console.error('[socket-error]', error)
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
const syncEncoder = encoding.createEncoder()
|
|
188
|
+
encoding.writeVarUint(syncEncoder, messageSync)
|
|
189
|
+
syncProtocol.writeSyncStep1(syncEncoder, entry.doc)
|
|
190
|
+
send(conn, encoding.toUint8Array(syncEncoder))
|
|
191
|
+
|
|
192
|
+
const awarenessStates = entry.awareness.getStates()
|
|
193
|
+
if (awarenessStates.size > 0) {
|
|
194
|
+
const awarenessEncoder = encoding.createEncoder()
|
|
195
|
+
encoding.writeVarUint(awarenessEncoder, messageAwareness)
|
|
196
|
+
encoding.writeVarUint8Array(
|
|
197
|
+
awarenessEncoder,
|
|
198
|
+
awarenessProtocol.encodeAwarenessUpdate(
|
|
199
|
+
entry.awareness,
|
|
200
|
+
Array.from(awarenessStates.keys()),
|
|
201
|
+
),
|
|
202
|
+
)
|
|
203
|
+
send(conn, encoding.toUint8Array(awarenessEncoder))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
console.log(`[connect] room=${docName} clients=${entry.conns.size}`)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const server = http.createServer((_req, res) => {
|
|
210
|
+
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' })
|
|
211
|
+
res.end('ok')
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
const wss = new WebSocketServer({ noServer: true })
|
|
215
|
+
|
|
216
|
+
server.on('upgrade', (request, socket, head) => {
|
|
217
|
+
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
218
|
+
setupWSConnection(ws, request)
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
server.listen(port, host, () => {
|
|
223
|
+
console.log(`yjs websocket server running at ws://${host}:${port}`)
|
|
224
|
+
})
|
package/yjs/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@norio-office/rich-text-yjs-server",
|
|
3
|
+
"private": true,
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "node index.js"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"lib0": "^0.2.102",
|
|
10
|
+
"ws": "^8.18.0",
|
|
11
|
+
"y-protocols": "^1.0.6",
|
|
12
|
+
"yjs": "^13.6.30"
|
|
13
|
+
}
|
|
14
|
+
}
|