Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ describe('EditorHistorySnapshotAdapter — Yjs-backed editors', () => {
});
});

describe('EditorHistorySnapshotAdapter — late collaboration upgrade', () => {
it('switches to Yjs history after a local editor is upgraded in place', () => {
mockUndoDepth.mockReturnValue(0);
mockRedoDepth.mockReturnValue(0);
mockGetPluginState.mockReturnValue({
undoManager: { undoStack: [1], redoStack: [] },
});

const editor = buildEditor();
const adapter = new EditorHistorySnapshotAdapter(editor as Editor);

editor.options.collaborationProvider = {};
editor.options.ydoc = {};

expect(adapter.getSnapshot()).toEqual({ undoDepth: 1, redoDepth: 0 });
expect(mockUndoDepth).not.toHaveBeenCalled();
expect(mockRedoDepth).not.toHaveBeenCalled();
});
});

describe('EditorHistorySnapshotAdapter — command delegation', () => {
it('undo() / redo() delegate to the shared history helpers', () => {
mockRunEditorUndo.mockReturnValue(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Snapshot adapter that works for both PM-history-backed and Yjs-backed editors.
*
* The backend type is determined once per editor by whether it was created
* with a `collaborationProvider` + `ydoc` pair. We keep one adapter class
* instead of two because the surface difference is small — read depth from
* the right stack, delegate undo/redo to `runEditorUndo` / `runEditorRedo`.
* The backend type is determined from the editor's current options whenever
* a snapshot is read. This allows an adapter created for a local editor to
* switch to Yjs history after `upgradeToCollaboration()` attaches a provider
* and ydoc in place.
*/

import { undoDepth, redoDepth } from 'prosemirror-history';
Expand Down Expand Up @@ -65,16 +65,14 @@ export const readEditorHistorySnapshot = (editor: Editor): ParticipantHistorySna
*/
export class EditorHistorySnapshotAdapter implements HistorySnapshotAdapter {
readonly #editor: Editor;
readonly #collaborative: boolean;
#pendingChangeKind: ParticipantHistoryChangeKind = 'unknown';

constructor(editor: Editor) {
this.#editor = editor;
this.#collaborative = isYjsBacked(editor);
}

getSnapshot(): ParticipantHistorySnapshot {
if (this.#collaborative) {
if (isYjsBacked(this.#editor)) {
return readYjsDepths(this.#editor);
}
return readPmDepths(this.#editor);
Expand Down
Loading