diff --git a/packages/core/src/editor/managers/ExtensionManager/extensions.ts b/packages/core/src/editor/managers/ExtensionManager/extensions.ts index 7f3b010187..5af4f625fb 100644 --- a/packages/core/src/editor/managers/ExtensionManager/extensions.ts +++ b/packages/core/src/editor/managers/ExtensionManager/extensions.ts @@ -191,11 +191,9 @@ export function getDefaultExtensions( if (options.collaboration) { extensions.push(ForkYDocExtension(options.collaboration)); - if (options.collaboration.provider?.awareness) { - extensions.push(YCursorExtension(options.collaboration)); - } + extensions.push(YCursorExtension(options.collaboration)); extensions.push(YSyncExtension(options.collaboration)); - extensions.push(YUndoExtension(options.collaboration)); + extensions.push(YUndoExtension()); extensions.push(SchemaMigration(options.collaboration)); } else { // YUndo is not compatible with ProseMirror's history plugin diff --git a/packages/core/src/extensions/Collaboration/ForkYDoc.ts b/packages/core/src/extensions/Collaboration/ForkYDoc.ts index 63897a6344..b2958c56f1 100644 --- a/packages/core/src/extensions/Collaboration/ForkYDoc.ts +++ b/packages/core/src/extensions/Collaboration/ForkYDoc.ts @@ -107,7 +107,7 @@ export const ForkYDocExtension = createExtension( editor.registerExtension([ YSyncExtension(newOptions), // No need to register the cursor plugin again, it's a local fork - YUndoExtension({}), + YUndoExtension(), ]); // Tell the store that the editor is now forked @@ -131,7 +131,7 @@ export const ForkYDocExtension = createExtension( editor.registerExtension([ YSyncExtension(options), YCursorExtension(options), - YUndoExtension({}), + YUndoExtension(), ]); // Reset the undo stack to the original undo stack diff --git a/packages/core/src/extensions/Collaboration/YCursorPlugin.ts b/packages/core/src/extensions/Collaboration/YCursorPlugin.ts index 98ac192f23..3bf17f574e 100644 --- a/packages/core/src/extensions/Collaboration/YCursorPlugin.ts +++ b/packages/core/src/extensions/Collaboration/YCursorPlugin.ts @@ -73,12 +73,11 @@ export const YCursorExtension = createExtension( NonNullable["collaboration"]> >) => { const recentlyUpdatedCursors = new Map(); - - if ( + const hasAwareness = options.provider && "awareness" in options.provider && - typeof options.provider.awareness === "object" - ) { + typeof options.provider.awareness === "object"; + if (hasAwareness) { if ( "setLocalStateField" in options.provider.awareness && typeof options.provider.awareness.setLocalStateField === "function" @@ -126,54 +125,56 @@ export const YCursorExtension = createExtension( return { key: "yCursor", prosemirrorPlugins: [ - yCursorPlugin(options.provider.awareness, { - selectionBuilder: defaultSelectionBuilder, - cursorBuilder(user: CollaborationUser, clientID: number) { - let cursorData = recentlyUpdatedCursors.get(clientID); - - if (!cursorData) { - const cursorElement = ( - options.renderCursor ?? defaultCursorRender - )(user); - - if (options.showCursorLabels !== "always") { - cursorElement.addEventListener("mouseenter", () => { - const cursor = recentlyUpdatedCursors.get(clientID)!; - cursor.element.setAttribute("data-active", ""); - - if (cursor.hideTimeout) { - clearTimeout(cursor.hideTimeout); - recentlyUpdatedCursors.set(clientID, { - element: cursor.element, - hideTimeout: undefined, + hasAwareness + ? yCursorPlugin(options.provider.awareness, { + selectionBuilder: defaultSelectionBuilder, + cursorBuilder(user: CollaborationUser, clientID: number) { + let cursorData = recentlyUpdatedCursors.get(clientID); + + if (!cursorData) { + const cursorElement = ( + options.renderCursor ?? defaultCursorRender + )(user); + + if (options.showCursorLabels !== "always") { + cursorElement.addEventListener("mouseenter", () => { + const cursor = recentlyUpdatedCursors.get(clientID)!; + cursor.element.setAttribute("data-active", ""); + + if (cursor.hideTimeout) { + clearTimeout(cursor.hideTimeout); + recentlyUpdatedCursors.set(clientID, { + element: cursor.element, + hideTimeout: undefined, + }); + } }); - } - }); - cursorElement.addEventListener("mouseleave", () => { - const cursor = recentlyUpdatedCursors.get(clientID)!; + cursorElement.addEventListener("mouseleave", () => { + const cursor = recentlyUpdatedCursors.get(clientID)!; - recentlyUpdatedCursors.set(clientID, { - element: cursor.element, - hideTimeout: setTimeout(() => { - cursor.element.removeAttribute("data-active"); - }, 2000), - }); - }); - } + recentlyUpdatedCursors.set(clientID, { + element: cursor.element, + hideTimeout: setTimeout(() => { + cursor.element.removeAttribute("data-active"); + }, 2000), + }); + }); + } - cursorData = { - element: cursorElement, - hideTimeout: undefined, - }; + cursorData = { + element: cursorElement, + hideTimeout: undefined, + }; - recentlyUpdatedCursors.set(clientID, cursorData); - } + recentlyUpdatedCursors.set(clientID, cursorData); + } - return cursorData.element; - }, - }), - ], + return cursorData.element; + }, + }) + : undefined, + ].filter(Boolean), dependsOn: ["ySync"], updateUser(user: { name: string; color: string; [key: string]: string }) { options.provider.awareness.setLocalStateField("user", user); diff --git a/packages/core/src/extensions/Collaboration/YUndo.ts b/packages/core/src/extensions/Collaboration/YUndo.ts index 0bc84c902b..c3b65a55bd 100644 --- a/packages/core/src/extensions/Collaboration/YUndo.ts +++ b/packages/core/src/extensions/Collaboration/YUndo.ts @@ -1,10 +1,10 @@ import { redoCommand, undoCommand, yUndoPlugin } from "y-prosemirror"; import { createExtension } from "../../editor/BlockNoteExtension.js"; -export const YUndoExtension = createExtension(({ editor }) => { +export const YUndoExtension = createExtension(() => { return { key: "yUndo", - prosemirrorPlugins: [yUndoPlugin({ trackedOrigins: [editor] })], + prosemirrorPlugins: [yUndoPlugin()], dependsOn: ["yCursor", "ySync"], undoCommand: undoCommand, redoCommand: redoCommand,