@veltdev/draftjs-crdt 1.0.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 ADDED
@@ -0,0 +1,65 @@
1
+ # Velt Draft.js CRDT
2
+
3
+ Add Velt-powered realtime collaboration to an existing Draft.js editor.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @veltdev/draftjs-crdt @veltdev/crdt @veltdev/client draft-js immutable yjs y-protocols react
9
+ ```
10
+
11
+ Using the React hook? Also install the Velt React SDK.
12
+
13
+ ```bash
14
+ npm install @veltdev/react
15
+ ```
16
+
17
+ ## Usage (vanilla manager)
18
+
19
+ Create a `CollaborationManager`, pass your controlled Draft.js editor state accessors, and call `manager.handleChange(nextState)` from Draft's `onChange`.
20
+
21
+ ```ts
22
+ import { createCollaboration } from '@veltdev/draftjs-crdt';
23
+
24
+ const manager = await createCollaboration({
25
+ editorId: 'document-1',
26
+ veltClient,
27
+ getEditorState: () => editorState,
28
+ setEditorState,
29
+ initialContent: 'Hello Draft.js CRDT!',
30
+ });
31
+ ```
32
+
33
+ The manager exposes the Velt CRDT `Store`, `SyncProvider`, Yjs document, XML fragment, awareness, undo manager, status subscriptions, version APIs, and cleanup.
34
+
35
+ ## React hook
36
+
37
+ `useCollaboration` (alias `useDraftJsCollaboration`) wraps the manager with Velt-context gating and lifecycle cleanup. Draft.js is a controlled editor, so pass your state accessors and route every `onChange` state through the returned `handleChange`.
38
+
39
+ ```tsx
40
+ import { useState } from 'react';
41
+ import { Editor, EditorState } from 'draft-js';
42
+ import { useCollaboration } from '@veltdev/draftjs-crdt';
43
+
44
+ function CollaborativeEditor() {
45
+ const [editorState, setEditorState] = useState(() => EditorState.createEmpty());
46
+
47
+ const { handleChange, isLoading, status } = useCollaboration({
48
+ editorId: 'my-document-id',
49
+ getEditorState: () => editorState,
50
+ setEditorState,
51
+ initialContent: 'Hello Draft.js CRDT!',
52
+ });
53
+
54
+ return (
55
+ <Editor
56
+ editorState={editorState}
57
+ onChange={(next) => setEditorState(handleChange(next))}
58
+ />
59
+ );
60
+ }
61
+ ```
62
+
63
+ The hook returns `handleChange`, `manager`, `isLoading`, `synced`, `isSynced`, `status`, `error`, `versions`, `saveVersion`, `restoreVersion`, `refreshVersions`, and `destroy`. It initializes after the Velt client (from `@veltdev/react` context, or an optional `veltClient` config override) is ready and a user is identified, and it destroys the manager on unmount or `editorId` change.
64
+
65
+ Initialize Velt with your own key, for example `initVelt('YOUR_API_KEY')`, before mounting collaborative editors.