|
| 1 | +--- |
| 2 | +title: Upgrade to Collaboration |
| 3 | +sidebarTitle: Upgrade Later |
| 4 | +keywords: "upgrade to collaboration, runtime collaboration, late collaboration, yjs room promotion, superdoc upgradeToCollaboration" |
| 5 | +--- |
| 6 | + |
| 7 | +Use `superdoc.upgradeToCollaboration()` when you want to render a document locally first and turn that same SuperDoc instance into a collaborative room later. |
| 8 | + |
| 9 | +This is useful for flows like: |
| 10 | + |
| 11 | +- A user opens a document privately, then clicks `Share` |
| 12 | +- You only provision collaboration infrastructure when someone requests it |
| 13 | +- You want the document visible immediately, then enable real-time editing on demand |
| 14 | + |
| 15 | +<Warning> |
| 16 | + `upgradeToCollaboration()` promotes the current local document into the target |
| 17 | + room. It seeds the room with the current document state, comments, and lock |
| 18 | + state. Use it to create a new shared room from local state, not to join an |
| 19 | + existing room you need to preserve as-is. |
| 20 | +</Warning> |
| 21 | + |
| 22 | +## Before you start |
| 23 | + |
| 24 | +- Wait until the SuperDoc instance is ready |
| 25 | +- Call it only on a local instance that is not already collaborative |
| 26 | +- The current implementation supports a single DOCX document |
| 27 | +- Provide a matching `{ ydoc, provider }` pair for the room you want to create |
| 28 | +- You do not need to wait for provider sync yourself; SuperDoc waits internally |
| 29 | + |
| 30 | +## Example |
| 31 | + |
| 32 | +This example starts with a local editor and upgrades it when the user clicks a button. |
| 33 | + |
| 34 | +```javascript |
| 35 | +import * as Y from 'yjs'; |
| 36 | +import { WebsocketProvider } from 'y-websocket'; |
| 37 | +import { SuperDoc } from 'superdoc'; |
| 38 | +import 'superdoc/style.css'; |
| 39 | + |
| 40 | +const superdoc = new SuperDoc({ |
| 41 | + selector: '#editor', |
| 42 | + document: file, |
| 43 | + user: { |
| 44 | + name: 'John Smith', |
| 45 | + email: 'john@example.com', |
| 46 | + }, |
| 47 | +}); |
| 48 | + |
| 49 | +superdoc.on('ready', () => { |
| 50 | + document.querySelector('#share-button').addEventListener('click', async () => { |
| 51 | + const roomId = `document-${crypto.randomUUID()}`; |
| 52 | + const ydoc = new Y.Doc(); |
| 53 | + const provider = new WebsocketProvider('wss://collab.example.com', roomId, ydoc); |
| 54 | + |
| 55 | + try { |
| 56 | + await superdoc.upgradeToCollaboration({ ydoc, provider }); |
| 57 | + console.log('Collaboration enabled:', roomId); |
| 58 | + } catch (error) { |
| 59 | + provider.destroy(); |
| 60 | + ydoc.destroy(); |
| 61 | + throw error; |
| 62 | + } |
| 63 | + }); |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +## What happens during the upgrade |
| 68 | + |
| 69 | +1. SuperDoc waits for the collaboration provider to connect and sync |
| 70 | +2. The current local document state is written into the target room |
| 71 | +3. Comments and lock state are copied into the room |
| 72 | +4. The editor runtime is rebuilt in collaboration mode |
| 73 | +5. The same `superdoc` instance continues running, now backed by Yjs |
| 74 | + |
| 75 | +## Constructor-time collaboration vs late upgrade |
| 76 | + |
| 77 | +<CardGroup cols={2}> |
| 78 | + <Card title="Start collaborative immediately" href="/modules/collaboration/quickstart"> |
| 79 | + Create the provider first, then pass `{ ydoc, provider }` in |
| 80 | + `modules.collaboration` when you construct `SuperDoc`. |
| 81 | + </Card> |
| 82 | + <Card title="Start local, upgrade later" href="/core/superdoc/methods"> |
| 83 | + Create a local `SuperDoc` first, then call |
| 84 | + `superdoc.upgradeToCollaboration({ ydoc, provider })` when you want to |
| 85 | + create a shared room. |
| 86 | + </Card> |
| 87 | +</CardGroup> |
| 88 | + |
| 89 | +## Related docs |
| 90 | + |
| 91 | +- [Collaboration quickstart](/modules/collaboration/quickstart) |
| 92 | +- [Collaboration configuration](/modules/collaboration/configuration) |
| 93 | +- [SuperDoc methods](/core/superdoc/methods) |
| 94 | +- [Liveblocks guide](/guides/collaboration/liveblocks) |
| 95 | +- [SuperDoc Yjs guide](/guides/collaboration/superdoc-yjs) |
0 commit comments