Skip to content

fix: byo hocuspocus provider does not work #3834

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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.
27 changes: 21 additions & 6 deletions packages/yjs/src/lib/BaseYjsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type YjsConfig = PluginConfig<
{
isConnected: boolean;
isSynced: boolean;
provider: HocuspocusProvider;
provider?: HocuspocusProvider;
/** WithCursors options */
cursorOptions?: WithCursorsOptions;
disableCursors?: boolean;
Expand All @@ -34,11 +34,26 @@ 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.on('connect', () => {
setOption('isConnected', true);
});
provider.on('disconnect', () => {
setOption('isConnected', false);
setOption('isSynced', false);
});
provider.on('synced', () => {
setOption('isSynced', true);
});

return {
options: { provider },
};
}
if (!hocuspocusProviderOptions) {
throw new Error('HocuspocusProvider configuration is required');
}
Expand All @@ -48,9 +63,9 @@ 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() {},
onAwarenessChange() { },
onConnect() {
setOption('isConnected', true);
hocuspocusProviderOptions.onConnect?.();
Expand All @@ -67,6 +82,6 @@ export const BaseYjsPlugin = createTSlatePlugin<YjsConfig>({
});

return {
options: { provider },
options: { provider: defaultProvider },
};
});
3 changes: 3 additions & 0 deletions packages/yjs/src/lib/withPlateYjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const withPlateYjs: ExtendEditor<YjsConfig> = ({
// not reactive
const { cursorOptions, disableCursors, provider, yjsOptions } = getOptions();

if (!provider)
throw new Error('HocuspocusProvider configuration is required');

const sharedType = provider.document.get(
'content',
Y.XmlText
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6360,7 +6360,7 @@ __metadata:
resolution: "@udecode/plate-ai@workspace:packages/ai"
dependencies:
"@udecode/plate-combobox": "npm:44.0.0"
"@udecode/plate-markdown": "npm:46.0.3"
"@udecode/plate-markdown": "npm:46.0.8"
"@udecode/plate-selection": "npm:46.0.4"
ai: "npm:^3.4.33"
lodash: "npm:^4.17.21"
Expand Down Expand Up @@ -6832,7 +6832,7 @@ __metadata:
languageName: unknown
linkType: soft

"@udecode/plate-markdown@npm:46.0.3, @udecode/plate-markdown@workspace:^, @udecode/plate-markdown@workspace:packages/markdown":
"@udecode/plate-markdown@npm:46.0.8, @udecode/plate-markdown@workspace:^, @udecode/plate-markdown@workspace:packages/markdown":
version: 0.0.0-use.local
resolution: "@udecode/plate-markdown@workspace:packages/markdown"
dependencies:
Expand Down
Loading