Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: byo hocuspocus provider does not work #3834

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-swans-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-yjs': patch
---

Allow BYO provider instead of plate hocuspocus setup.
7 changes: 5 additions & 2 deletions apps/www/content/docs/collaboration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ const editor = createPlateEditor({
YjsPlugin.configure({
options: {
hocuspocusProviderOptions: {
url: "https://hocuspocus.test/hocuspocus",
name: "test",
url: 'https://hocuspocus.test/hocuspocus',
name: 'test',
},
},
}),
],
});
```

As an alternative to allowing plate to create the provider, you may want to pass it yourself.
This can be passed as the `provider` field to `options`.

## Backend

Follow the backend instructions in [Hocuspocus docs](https://tiptap.dev/hocuspocus/getting-started).
Expand Down
35 changes: 30 additions & 5 deletions packages/yjs/src/lib/BaseYjsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { HocuspocusProviderConfiguration } from '@hocuspocus/provider';
import type {
HocuspocusProviderConfiguration,
onDisconnectParameters,
onSyncedParameters,
} from '@hocuspocus/provider';
import type { WithCursorsOptions } from '@slate-yjs/core';

import { HocuspocusProvider } from '@hocuspocus/provider';
Expand Down Expand Up @@ -44,11 +48,32 @@ export const BaseYjsPlugin = createTSlatePlugin<YjsConfig>({
options: {
isConnected: false,
isSynced: false,
provider: {} as any,
},
}).extend(({ getOptions, setOption }) => {
const { hocuspocusProviderOptions } = getOptions();
const { hocuspocusProviderOptions, provider } = getOptions();

if (provider) {
provider.setConfiguration({
onAwarenessChange() {},
onConnect() {
setOption('isConnected', true);
provider.onConnect();
},
onDisconnect(data: onDisconnectParameters) {
setOption('isConnected', false);
setOption('isSynced', false);
provider.onDisconnect(data);
},
onSynced(data: onSyncedParameters) {
setOption('isSynced', true);
provider.onSynced(data);
},
});

return {
options: { provider },
};
}
if (!hocuspocusProviderOptions) {
throw new Error('HocuspocusProvider configuration is required');
}
Expand All @@ -58,7 +83,7 @@ export const BaseYjsPlugin = createTSlatePlugin<YjsConfig>({
* connected ydoc, is not destroyed, the changes will be synced to other
* clients via the connected server.
*/
const provider = new HocuspocusProvider({
const defaultProvider = new HocuspocusProvider({
...hocuspocusProviderOptions,
onAwarenessChange() {},
onConnect() {
Expand All @@ -77,6 +102,6 @@ export const BaseYjsPlugin = createTSlatePlugin<YjsConfig>({
});

return {
options: { provider },
options: { provider: defaultProvider },
};
});
Loading