Skip to content
Merged
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 @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/extensions/Collaboration/ForkYDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
93 changes: 47 additions & 46 deletions packages/core/src/extensions/Collaboration/YCursorPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ export const YCursorExtension = createExtension(
NonNullable<BlockNoteEditorOptions<any, any, any>["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"
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/extensions/Collaboration/YUndo.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Loading