@norio-office/rich-text 0.3.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +610 -4
- package/dist/components/RichTextEditor.vue.d.ts +32 -1
- package/dist/composables/useRichTextEditor.d.ts +2 -1
- package/dist/extensions/comment-mark.d.ts +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6979 -5789
- package/dist/index.umd.cjs +964 -479
- package/dist/style.css +1 -1
- package/dist/types.d.ts +120 -0
- package/docs/usage.md +32 -1
- package/package.json +8 -2
- package/yjs/README.md +63 -0
- package/yjs/index.js +224 -0
- package/yjs/package.json +14 -0
- package/dist/index.es-D5dDjrL4.js +0 -5751
- package/dist/jspdf.es.min-B2a2-__Q.js +0 -18214
- package/dist/purify.es-jA1o6Tcu.js +0 -597
- package/dist/typeof-Dz1Ap89p.js +0 -11
- /package/dist/{html2canvas-CmV4ppBE.js → html2canvas-4hiP1s1t.js} +0 -0
package/dist/types.d.ts
CHANGED
|
@@ -13,10 +13,18 @@ export type RichTextEditorProps = {
|
|
|
13
13
|
enabledToolbarActions?: RichTextEditorToolbarActionKey[] | null;
|
|
14
14
|
placeholder?: string;
|
|
15
15
|
mention?: boolean;
|
|
16
|
+
collaboration?: RichTextEditorCollaborationOptions | null;
|
|
17
|
+
comments?: RichTextEditorCommentThread[] | null;
|
|
18
|
+
commentUser?: RichTextEditorCommentUser | null;
|
|
19
|
+
showComments?: boolean;
|
|
20
|
+
commentMention?: boolean;
|
|
16
21
|
uploadImage?: RichTextEditorUploadHook | null;
|
|
17
22
|
uploadVideo?: RichTextEditorUploadHook | null;
|
|
18
23
|
uploadFile?: RichTextEditorUploadHook | null;
|
|
24
|
+
uploadCommentImage?: RichTextEditorUploadHook | null;
|
|
19
25
|
onUploadError?: RichTextEditorUploadErrorHandler | null;
|
|
26
|
+
commentMentionProvider?: RichTextEditorCommentMentionProvider | null;
|
|
27
|
+
onCommentMentionSearch?: RichTextEditorCommentMentionProvider | null;
|
|
20
28
|
mentionProvider?: RichTextEditorMentionProvider | null;
|
|
21
29
|
onMentionSearch?: RichTextEditorMentionProvider | null;
|
|
22
30
|
};
|
|
@@ -31,6 +39,31 @@ export type RichTextEditorWatermarkOptions = {
|
|
|
31
39
|
export type RichTextEditorExportItemKey = 'pdf' | 'html' | 'image' | 'print';
|
|
32
40
|
export type RichTextEditorInsertMenuItemKey = 'image' | 'video' | 'table' | 'local-file' | 'columns' | 'highlight-block' | 'date' | 'code-block' | 'formula' | 'blockquote' | 'emoji' | 'link' | 'divider' | 'countdown' | 'markdown-import';
|
|
33
41
|
export type RichTextEditorToolbarActionKey = 'blockquote';
|
|
42
|
+
export type RichTextEditorCollaborationDocument = {
|
|
43
|
+
getXmlFragment: (field: string) => unknown;
|
|
44
|
+
};
|
|
45
|
+
export type RichTextEditorCollaborationAwareness = {
|
|
46
|
+
states: Map<number, unknown>;
|
|
47
|
+
on: (event: 'update', callback: (...args: unknown[]) => void) => void;
|
|
48
|
+
setLocalStateField: (field: string, value: unknown) => void;
|
|
49
|
+
};
|
|
50
|
+
export type RichTextEditorCollaborationProvider = {
|
|
51
|
+
doc?: unknown;
|
|
52
|
+
awareness: RichTextEditorCollaborationAwareness;
|
|
53
|
+
};
|
|
54
|
+
export type RichTextEditorCollaborationUser = {
|
|
55
|
+
name: string;
|
|
56
|
+
color: string;
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
};
|
|
59
|
+
export type RichTextEditorCollaborationOptions = {
|
|
60
|
+
document: RichTextEditorCollaborationDocument;
|
|
61
|
+
field?: string;
|
|
62
|
+
provider?: RichTextEditorCollaborationProvider | null;
|
|
63
|
+
user?: RichTextEditorCollaborationUser | null;
|
|
64
|
+
initializeContent?: boolean;
|
|
65
|
+
initialContent?: JSONContent | null;
|
|
66
|
+
};
|
|
34
67
|
export type RichTextEditorMentionType = 1 | 2;
|
|
35
68
|
export type RichTextEditorMentionItem = {
|
|
36
69
|
id: string;
|
|
@@ -68,6 +101,92 @@ export type RichTextEditorUploadErrorPayload = {
|
|
|
68
101
|
error: unknown;
|
|
69
102
|
};
|
|
70
103
|
export type RichTextEditorUploadErrorHandler = (payload: RichTextEditorUploadErrorPayload) => void;
|
|
104
|
+
export type RichTextEditorCommentUser = {
|
|
105
|
+
id: string;
|
|
106
|
+
name: string;
|
|
107
|
+
avatar?: string;
|
|
108
|
+
color?: string;
|
|
109
|
+
[key: string]: unknown;
|
|
110
|
+
};
|
|
111
|
+
export type RichTextEditorCommentImage = RichTextEditorUploadResult;
|
|
112
|
+
export type RichTextEditorCommentMentionItem = RichTextEditorMentionItem;
|
|
113
|
+
export type RichTextEditorCommentMentionProviderPayload = RichTextEditorMentionProviderPayload & {
|
|
114
|
+
threadId?: string;
|
|
115
|
+
commentId?: string;
|
|
116
|
+
parentId?: string;
|
|
117
|
+
content: string;
|
|
118
|
+
};
|
|
119
|
+
export type RichTextEditorCommentMentionProvider = (payload: RichTextEditorCommentMentionProviderPayload) => RichTextEditorCommentMentionItem[] | Promise<RichTextEditorCommentMentionItem[]>;
|
|
120
|
+
export type RichTextEditorCommentItem = {
|
|
121
|
+
id: string;
|
|
122
|
+
parentId?: string;
|
|
123
|
+
content: string;
|
|
124
|
+
author: RichTextEditorCommentUser;
|
|
125
|
+
createdAt: string;
|
|
126
|
+
updatedAt?: string;
|
|
127
|
+
images?: RichTextEditorCommentImage[];
|
|
128
|
+
mentions?: RichTextEditorCommentMentionItem[];
|
|
129
|
+
};
|
|
130
|
+
export type RichTextEditorCommentThread = {
|
|
131
|
+
id: string;
|
|
132
|
+
anchorText?: string;
|
|
133
|
+
status?: 'open' | 'resolved';
|
|
134
|
+
comments: RichTextEditorCommentItem[];
|
|
135
|
+
};
|
|
136
|
+
export type RichTextEditorCommentCreatePayload = {
|
|
137
|
+
threadId: string;
|
|
138
|
+
commentId: string;
|
|
139
|
+
anchorText: string;
|
|
140
|
+
content: string;
|
|
141
|
+
images: RichTextEditorCommentImage[];
|
|
142
|
+
mentions: RichTextEditorCommentMentionItem[];
|
|
143
|
+
author: RichTextEditorCommentUser;
|
|
144
|
+
createdAt: string;
|
|
145
|
+
};
|
|
146
|
+
export type RichTextEditorCommentReplyPayload = {
|
|
147
|
+
threadId: string;
|
|
148
|
+
commentId: string;
|
|
149
|
+
parentId?: string;
|
|
150
|
+
targetCommentId?: string;
|
|
151
|
+
targetUserId?: string;
|
|
152
|
+
targetUserName?: string;
|
|
153
|
+
content: string;
|
|
154
|
+
images: RichTextEditorCommentImage[];
|
|
155
|
+
mentions: RichTextEditorCommentMentionItem[];
|
|
156
|
+
author: RichTextEditorCommentUser;
|
|
157
|
+
createdAt: string;
|
|
158
|
+
};
|
|
159
|
+
export type RichTextEditorCommentUpdatePayload = {
|
|
160
|
+
threadId: string;
|
|
161
|
+
commentId: string;
|
|
162
|
+
content: string;
|
|
163
|
+
images?: RichTextEditorCommentImage[];
|
|
164
|
+
mentions: RichTextEditorCommentMentionItem[];
|
|
165
|
+
author?: RichTextEditorCommentUser;
|
|
166
|
+
updatedAt: string;
|
|
167
|
+
};
|
|
168
|
+
export type RichTextEditorCommentSubmitPayload = {
|
|
169
|
+
action: 'create' | 'reply' | 'update';
|
|
170
|
+
threadId: string;
|
|
171
|
+
commentId: string;
|
|
172
|
+
targetCommentId?: string;
|
|
173
|
+
targetUserId?: string;
|
|
174
|
+
targetUserName?: string;
|
|
175
|
+
content: string;
|
|
176
|
+
images?: RichTextEditorCommentImage[];
|
|
177
|
+
mentions: RichTextEditorCommentMentionItem[];
|
|
178
|
+
author?: RichTextEditorCommentUser;
|
|
179
|
+
createdAt?: string;
|
|
180
|
+
updatedAt?: string;
|
|
181
|
+
};
|
|
182
|
+
export type RichTextEditorCommentDeletePayload = {
|
|
183
|
+
threadId: string;
|
|
184
|
+
commentId?: string;
|
|
185
|
+
};
|
|
186
|
+
export type RichTextEditorCommentResolvePayload = {
|
|
187
|
+
threadId: string;
|
|
188
|
+
resolved: boolean;
|
|
189
|
+
};
|
|
71
190
|
export type RichTextEditorImagePayload = {
|
|
72
191
|
src: string;
|
|
73
192
|
alt?: string;
|
|
@@ -138,6 +257,7 @@ export type RichTextEditorInstance = {
|
|
|
138
257
|
getActiveOutlinePos: () => number | null;
|
|
139
258
|
focusOutlineItem: (pos: number) => boolean;
|
|
140
259
|
onOutlineChange: (handler: RichTextEditorOutlineChangeHandler) => () => void;
|
|
260
|
+
focusCommentThread: (threadId: string) => boolean;
|
|
141
261
|
};
|
|
142
262
|
export type OfficeIconProps = {
|
|
143
263
|
name: string;
|
package/docs/usage.md
CHANGED
|
@@ -13,6 +13,7 @@ import { RichTextEditor } from '@norio-office/rich-text'
|
|
|
13
13
|
## Main Capabilities
|
|
14
14
|
|
|
15
15
|
- rich text editing with a built-in toolbar UI
|
|
16
|
+
- optional Yjs collaboration mode with the same component API
|
|
16
17
|
- async `@` mention candidates for people and documents
|
|
17
18
|
- export to PDF, image, and HTML through instance methods
|
|
18
19
|
- external upload integration for image, video, and file insertion
|
|
@@ -44,8 +45,38 @@ async function handleMentionSearch(payload: RichTextEditorMentionProviderPayload
|
|
|
44
45
|
|
|
45
46
|
`type: 1` means person and `type: 2` means document. The editor inserts a mention only after the user clicks `提及`.
|
|
46
47
|
|
|
48
|
+
## Collaboration
|
|
47
49
|
|
|
48
|
-
|
|
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
|
+
In collaboration mode, the shared Yjs fragment is authoritative. The editor does not write `modelValue` into an empty collaborative fragment by default. If a brand-new room needs initial business content, the host must first confirm this client owns initialization, then pass `initializeContent: true` and `initialContent` inside the `collaboration` object. Do not use those flags when the backend already has a Yjs snapshot.
|
|
78
|
+
|
|
79
|
+
## Bundled Yjs Server
|
|
49
80
|
|
|
50
81
|
The npm package also ships a minimal websocket server example in `yjs/`.
|
|
51
82
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@norio-office/rich-text",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A reusable Vue 3 rich text editor component built with Tiptap 3.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.umd.cjs",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"dist",
|
|
19
19
|
"README.md",
|
|
20
20
|
"docs",
|
|
21
|
+
"yjs",
|
|
21
22
|
"LICENSE"
|
|
22
23
|
],
|
|
23
24
|
"sideEffects": [
|
|
@@ -39,6 +40,8 @@
|
|
|
39
40
|
"@tiptap/core": "^3.22.1",
|
|
40
41
|
"@tiptap/extension-code-block": "^3.22.1",
|
|
41
42
|
"@tiptap/extension-code-block-lowlight": "^3.22.1",
|
|
43
|
+
"@tiptap/extension-collaboration": "^3.22.2",
|
|
44
|
+
"@tiptap/extension-collaboration-caret": "^3.22.2",
|
|
42
45
|
"@tiptap/extension-font-family": "^3.22.1",
|
|
43
46
|
"@tiptap/extension-placeholder": "^3.22.1",
|
|
44
47
|
"@tiptap/extension-subscript": "^3.22.1",
|
|
@@ -54,8 +57,11 @@
|
|
|
54
57
|
"@tiptap/pm": "^3.22.1",
|
|
55
58
|
"@tiptap/starter-kit": "^3.22.1",
|
|
56
59
|
"@tiptap/vue-3": "^3.22.1",
|
|
60
|
+
"@tiptap/y-tiptap": "^3.0.2",
|
|
57
61
|
"lowlight": "^3.3.0",
|
|
58
|
-
"vue": "^3.5.0"
|
|
62
|
+
"vue": "^3.5.0",
|
|
63
|
+
"y-prosemirror": "^1.2.6",
|
|
64
|
+
"yjs": "^13.6.30"
|
|
59
65
|
},
|
|
60
66
|
"dependencies": {
|
|
61
67
|
"html2canvas": "^1.4.1",
|
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
|
+
}
|