carbon-tokenization / frontend /src /editor /extensions /collaboration-undo.ts
tfrere's picture
tfrere HF Staff
fix: resolve TipTap v3 collaboration crash
ade54e4
Raw
History Blame Contribute Delete
1.15 kB
import { Extension } from "@tiptap/core";
import { UndoManager } from "yjs";
import { yUndoPluginKey } from "@tiptap/y-tiptap";
export interface CollaborationUndoOptions {
onUndoManagerReady?: (manager: UndoManager) => void;
}
/**
* Thin extension that exposes the UndoManager created by @tiptap/extension-collaboration v3.
* The actual undo/redo commands and shortcuts are handled by the Collaboration extension itself.
* This extension only reads the UndoManager from the yUndoPlugin state and
* exposes it via onUndoManagerReady for external consumers (e.g. agent chat batching).
*/
export const CollaborationUndo = Extension.create<CollaborationUndoOptions>({
name: "collaborationUndoBridge",
addOptions() {
return {
onUndoManagerReady: undefined,
};
},
addStorage() {
return {
undoManager: null as UndoManager | null,
};
},
onCreate() {
const state = this.editor.state;
const undoState = yUndoPluginKey.getState(state);
if (undoState?.undoManager) {
this.storage.undoManager = undoState.undoManager;
this.options.onUndoManagerReady?.(undoState.undoManager);
}
},
});