-
Notifications
You must be signed in to change notification settings - Fork 63
fix: headless yjs #1913
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
Open
caio-pizzol
wants to merge
1
commit into
main
Choose a base branch
from
caio/sd-1717-support-yjs-ysyncplugin-in-headless-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+533
−5
Open
fix: headless yjs #1913
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
...ages/super-editor/src/extensions/collaboration/collaboration-headless.integration.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /** | ||
| * Headless Y.js Collaboration Integration Test | ||
| * | ||
| * Tests that a headless Editor properly initializes Y.js binding. | ||
| * The actual sync behavior depends on y-prosemirror internals and is better | ||
| * tested end-to-end with a real collaboration server. | ||
| */ | ||
|
|
||
| import { describe, it, expect, beforeEach, afterEach } from 'vitest'; | ||
| import { Doc as YDoc } from 'yjs'; | ||
| import { Editor } from '@core/Editor.js'; | ||
| import { getStarterExtensions } from '@extensions/index.js'; | ||
| import { ySyncPluginKey } from 'y-prosemirror'; | ||
|
|
||
| describe('Headless Y.js Collaboration Integration', () => { | ||
| let ydoc; | ||
| let editor; | ||
|
|
||
| beforeEach(() => { | ||
| ydoc = new YDoc({ gc: false }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (editor) { | ||
| editor.destroy(); | ||
| editor = null; | ||
| } | ||
| if (ydoc) { | ||
| ydoc.destroy(); | ||
| ydoc = null; | ||
| } | ||
| }); | ||
|
|
||
| it('initializes Y.js binding in headless mode', () => { | ||
| editor = new Editor({ | ||
| isHeadless: true, | ||
| mode: 'docx', | ||
| documentId: 'test-headless-binding', | ||
| extensions: getStarterExtensions(), | ||
| ydoc, | ||
| content: [], | ||
| mediaFiles: {}, | ||
| fonts: {}, | ||
| }); | ||
|
|
||
| // Get the sync plugin state | ||
| const syncState = ySyncPluginKey.getState(editor.state); | ||
|
|
||
| // Verify binding was initialized | ||
| expect(syncState).toBeDefined(); | ||
| expect(syncState.binding).toBeDefined(); | ||
| expect(syncState.binding.prosemirrorView).toBeDefined(); | ||
| }); | ||
|
|
||
| it('does not create infinite sync loop when making edits', async () => { | ||
| editor = new Editor({ | ||
| isHeadless: true, | ||
| mode: 'docx', | ||
| documentId: 'test-no-loop', | ||
| extensions: getStarterExtensions(), | ||
| ydoc, | ||
| content: [], | ||
| mediaFiles: {}, | ||
| fonts: {}, | ||
| }); | ||
|
|
||
| let transactionCount = 0; | ||
| const originalDispatch = editor.dispatch.bind(editor); | ||
| editor.dispatch = (tr) => { | ||
| transactionCount++; | ||
| return originalDispatch(tr); | ||
| }; | ||
|
|
||
| // Make an edit | ||
| editor.commands.insertContent({ | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Test' }], | ||
| }); | ||
|
|
||
| // Wait for any potential sync loops | ||
| await new Promise((resolve) => setTimeout(resolve, 500)); | ||
|
|
||
| // Should have very few transactions (1 for insert, maybe 1-2 for sync) | ||
| // If there's a loop, this would be hundreds or thousands | ||
| expect(transactionCount).toBeLessThan(10); | ||
| }); | ||
|
|
||
| it('allows making edits in headless mode with Y.js', () => { | ||
| editor = new Editor({ | ||
| isHeadless: true, | ||
| mode: 'docx', | ||
| documentId: 'test-headless-edits', | ||
| extensions: getStarterExtensions(), | ||
| ydoc, | ||
| content: [], | ||
| mediaFiles: {}, | ||
| fonts: {}, | ||
| }); | ||
|
|
||
| const initialContent = editor.state.doc.textContent; | ||
|
|
||
| // Make edits - this should not throw | ||
| editor.commands.insertContent({ | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Hello from headless!' }], | ||
| }); | ||
|
|
||
| // Verify edit was applied to editor | ||
| expect(editor.state.doc.textContent).toContain('Hello from headless'); | ||
| expect(editor.state.doc.textContent).not.toBe(initialContent); | ||
| }); | ||
|
|
||
| it('works without collaborationProvider (local-only Y.js)', () => { | ||
| // This simulates the customer's use case where they manage their own provider | ||
| editor = new Editor({ | ||
| isHeadless: true, | ||
| mode: 'docx', | ||
| documentId: 'test-local-ydoc', | ||
| extensions: getStarterExtensions(), | ||
| ydoc, // Y.js doc without provider | ||
| // No collaborationProvider - user manages it externally | ||
| content: [], | ||
| mediaFiles: {}, | ||
| fonts: {}, | ||
| }); | ||
|
|
||
| const syncState = ySyncPluginKey.getState(editor.state); | ||
| expect(syncState.binding).toBeDefined(); | ||
| expect(syncState.binding.prosemirrorView).toBeDefined(); | ||
|
|
||
| // Should still be able to make edits | ||
| editor.commands.insertContent({ | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Local Y.js test' }], | ||
| }); | ||
|
|
||
| expect(editor.state.doc.textContent).toContain('Local Y.js test'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The headless transaction handler calls
binding._prosemirrorChanged(editor.state.doc)directly for every local transaction, but it never propagates the ProseMirroraddToHistorymeta into the Yjs transaction the way the normalySyncPluginview.update path does (see the bundled y-prosemirror inexamples/integrations/chrome-extension/chrome-extension/lib/superdoc.umd.js, where it wraps_prosemirrorChangedwithtr.meta.set("addToHistory", pluginState.addToHistory)). Because Yjs undo capture usestr.meta.get("addToHistory")(same file) and defaults to true, headless transactions that should be excluded from history (e.g., selection-only or programmatic updates) will still be recorded. That can pollute undo/redo stacks or make non-history changes undoable in headless collaboration. Consider mirroring the view.update meta handling before calling_prosemirrorChanged.Useful? React with 👍 / 👎.