|
| 1 | +import * as Y from "yjs"; |
| 2 | + |
| 3 | +import { |
| 4 | + yCursorPluginKey, |
| 5 | + ySyncPluginKey, |
| 6 | + yUndoPluginKey, |
| 7 | +} from "y-prosemirror"; |
| 8 | +import { CursorPlugin } from "./CursorPlugin.js"; |
| 9 | +import { SyncPlugin } from "./SyncPlugin.js"; |
| 10 | +import { UndoPlugin } from "./UndoPlugin.js"; |
| 11 | + |
| 12 | +import { |
| 13 | + BlockNoteEditor, |
| 14 | + BlockNoteEditorOptions, |
| 15 | +} from "../../editor/BlockNoteEditor.js"; |
| 16 | +import { BlockNoteExtension } from "../../editor/BlockNoteExtension.js"; |
| 17 | + |
| 18 | +export class ForkYDocPlugin extends BlockNoteExtension<{ |
| 19 | + forked: boolean; |
| 20 | +}> { |
| 21 | + public static name() { |
| 22 | + return "ForkYDocPlugin"; |
| 23 | + } |
| 24 | + |
| 25 | + private editor: BlockNoteEditor<any, any, any>; |
| 26 | + private collaboration: BlockNoteEditorOptions<any, any, any>["collaboration"]; |
| 27 | + |
| 28 | + constructor({ |
| 29 | + editor, |
| 30 | + collaboration, |
| 31 | + }: { |
| 32 | + editor: BlockNoteEditor<any, any, any>; |
| 33 | + collaboration: BlockNoteEditorOptions<any, any, any>["collaboration"]; |
| 34 | + }) { |
| 35 | + super(editor); |
| 36 | + this.editor = editor; |
| 37 | + this.collaboration = collaboration; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * To find a fragment in another ydoc, we need to search for it. |
| 42 | + */ |
| 43 | + private findTypeInOtherYdoc<T extends Y.AbstractType<any>>( |
| 44 | + ytype: T, |
| 45 | + otherYdoc: Y.Doc, |
| 46 | + ): T { |
| 47 | + const ydoc = ytype.doc!; |
| 48 | + if (ytype._item === null) { |
| 49 | + /** |
| 50 | + * If is a root type, we need to find the root key in the original ydoc |
| 51 | + * and use it to get the type in the other ydoc. |
| 52 | + */ |
| 53 | + const rootKey = Array.from(ydoc.share.keys()).find( |
| 54 | + (key) => ydoc.share.get(key) === ytype, |
| 55 | + ); |
| 56 | + if (rootKey == null) { |
| 57 | + throw new Error("type does not exist in other ydoc"); |
| 58 | + } |
| 59 | + return otherYdoc.get(rootKey, ytype.constructor as new () => T) as T; |
| 60 | + } else { |
| 61 | + /** |
| 62 | + * If it is a sub type, we use the item id to find the history type. |
| 63 | + */ |
| 64 | + const ytypeItem = ytype._item; |
| 65 | + const otherStructs = |
| 66 | + otherYdoc.store.clients.get(ytypeItem.id.client) ?? []; |
| 67 | + const itemIndex = Y.findIndexSS(otherStructs, ytypeItem.id.clock); |
| 68 | + const otherItem = otherStructs[itemIndex] as Y.Item; |
| 69 | + const otherContent = otherItem.content as Y.ContentType; |
| 70 | + return otherContent.type as T; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Whether the editor is editing a forked document, |
| 76 | + * preserving a reference to the original document and the forked document. |
| 77 | + */ |
| 78 | + public get isForkedFromRemote() { |
| 79 | + return this.forkedState !== undefined; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Stores whether the editor is editing a forked document, |
| 84 | + * preserving a reference to the original document and the forked document. |
| 85 | + */ |
| 86 | + private forkedState: |
| 87 | + | { |
| 88 | + originalFragment: Y.XmlFragment; |
| 89 | + forkedFragment: Y.XmlFragment; |
| 90 | + } |
| 91 | + | undefined; |
| 92 | + |
| 93 | + /** |
| 94 | + * Fork the Y.js document from syncing to the remote, |
| 95 | + * allowing modifications to the document without affecting the remote. |
| 96 | + * These changes can later be rolled back or applied to the remote. |
| 97 | + */ |
| 98 | + public forkYjsSync() { |
| 99 | + if (this.forkedState) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const originalFragment = this.collaboration.fragment; |
| 104 | + |
| 105 | + if (!originalFragment) { |
| 106 | + throw new Error("No fragment to fork from"); |
| 107 | + } |
| 108 | + |
| 109 | + const doc = new Y.Doc(); |
| 110 | + // Copy the original document to a new Yjs document |
| 111 | + Y.applyUpdate(doc, Y.encodeStateAsUpdate(originalFragment.doc!)); |
| 112 | + |
| 113 | + // Find the forked fragment in the new Yjs document |
| 114 | + const forkedFragment = this.findTypeInOtherYdoc(originalFragment, doc); |
| 115 | + |
| 116 | + this.forkedState = { |
| 117 | + originalFragment, |
| 118 | + forkedFragment, |
| 119 | + }; |
| 120 | + |
| 121 | + // Need to reset all the yjs plugins |
| 122 | + this.editor._tiptapEditor.unregisterPlugin([ |
| 123 | + yCursorPluginKey, |
| 124 | + yUndoPluginKey, |
| 125 | + ySyncPluginKey, |
| 126 | + ]); |
| 127 | + // Register them again, based on the new forked fragment |
| 128 | + this.editor._tiptapEditor.registerPlugin( |
| 129 | + new SyncPlugin(forkedFragment).plugins[0], |
| 130 | + ); |
| 131 | + this.editor._tiptapEditor.registerPlugin(new UndoPlugin().plugins[0]); |
| 132 | + // No need to register the cursor plugin again, it's a local fork |
| 133 | + this.emit("forked", true); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Resume syncing the Y.js document to the remote |
| 138 | + * If `keepChanges` is true, any changes that have been made to the forked document will be applied to the original document. |
| 139 | + * Otherwise, the original document will be restored and the changes will be discarded. |
| 140 | + */ |
| 141 | + public resumeYjsSync(keepChanges = false) { |
| 142 | + if (!this.forkedState) { |
| 143 | + return; |
| 144 | + } |
| 145 | + // Remove the forked fragment's plugins |
| 146 | + this.editor._tiptapEditor.unregisterPlugin(ySyncPluginKey); |
| 147 | + this.editor._tiptapEditor.unregisterPlugin(yUndoPluginKey); |
| 148 | + |
| 149 | + const { originalFragment, forkedFragment } = this.forkedState!; |
| 150 | + if (keepChanges) { |
| 151 | + // Apply any changes that have been made to the fork, onto the original doc |
| 152 | + const update = Y.encodeStateAsUpdate(forkedFragment.doc!); |
| 153 | + Y.applyUpdate(originalFragment.doc!, update); |
| 154 | + } |
| 155 | + this.editor.extensions["ySyncPlugin"] = new SyncPlugin(originalFragment); |
| 156 | + this.editor.extensions["yCursorPlugin"] = new CursorPlugin( |
| 157 | + this.collaboration!, |
| 158 | + ); |
| 159 | + this.editor.extensions["yUndoPlugin"] = new UndoPlugin(); |
| 160 | + // Register the plugins again, based on the original fragment |
| 161 | + this.editor._tiptapEditor.registerPlugin( |
| 162 | + this.editor.extensions["ySyncPlugin"].plugins[0], |
| 163 | + ); |
| 164 | + this.editor._tiptapEditor.registerPlugin( |
| 165 | + this.editor.extensions["yCursorPlugin"].plugins[0], |
| 166 | + ); |
| 167 | + this.editor._tiptapEditor.registerPlugin( |
| 168 | + this.editor.extensions["yUndoPlugin"].plugins[0], |
| 169 | + ); |
| 170 | + // Reset the forked state |
| 171 | + this.forkedState = undefined; |
| 172 | + this.emit("forked", false); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +// export const forkYDocPlugin = ( |
| 177 | +// editor: BlockNoteEditor<any, any, any>, |
| 178 | +// { |
| 179 | +// collaboration, |
| 180 | +// }: { |
| 181 | +// collaboration: BlockNoteEditorOptions<any, any, any>["collaboration"]; |
| 182 | +// }, |
| 183 | +// ): BlockNoteExtension<{ forked: boolean }> => { |
| 184 | +// return new ForkYDocPlugin({ editor, collaboration }); |
| 185 | +// }; |
0 commit comments