Skip to content

Commit ab573fc

Browse files
authored
fix: always include the cursor extension #2244 (#2260)
1 parent 6273b83 commit ab573fc

File tree

4 files changed

+53
-54
lines changed

4 files changed

+53
-54
lines changed

packages/core/src/editor/managers/ExtensionManager/extensions.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,9 @@ export function getDefaultExtensions(
191191

192192
if (options.collaboration) {
193193
extensions.push(ForkYDocExtension(options.collaboration));
194-
if (options.collaboration.provider?.awareness) {
195-
extensions.push(YCursorExtension(options.collaboration));
196-
}
194+
extensions.push(YCursorExtension(options.collaboration));
197195
extensions.push(YSyncExtension(options.collaboration));
198-
extensions.push(YUndoExtension(options.collaboration));
196+
extensions.push(YUndoExtension());
199197
extensions.push(SchemaMigration(options.collaboration));
200198
} else {
201199
// YUndo is not compatible with ProseMirror's history plugin

packages/core/src/extensions/Collaboration/ForkYDoc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const ForkYDocExtension = createExtension(
107107
editor.registerExtension([
108108
YSyncExtension(newOptions),
109109
// No need to register the cursor plugin again, it's a local fork
110-
YUndoExtension({}),
110+
YUndoExtension(),
111111
]);
112112

113113
// Tell the store that the editor is now forked
@@ -131,7 +131,7 @@ export const ForkYDocExtension = createExtension(
131131
editor.registerExtension([
132132
YSyncExtension(options),
133133
YCursorExtension(options),
134-
YUndoExtension({}),
134+
YUndoExtension(),
135135
]);
136136

137137
// Reset the undo stack to the original undo stack

packages/core/src/extensions/Collaboration/YCursorPlugin.ts

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,11 @@ export const YCursorExtension = createExtension(
7373
NonNullable<BlockNoteEditorOptions<any, any, any>["collaboration"]>
7474
>) => {
7575
const recentlyUpdatedCursors = new Map();
76-
77-
if (
76+
const hasAwareness =
7877
options.provider &&
7978
"awareness" in options.provider &&
80-
typeof options.provider.awareness === "object"
81-
) {
79+
typeof options.provider.awareness === "object";
80+
if (hasAwareness) {
8281
if (
8382
"setLocalStateField" in options.provider.awareness &&
8483
typeof options.provider.awareness.setLocalStateField === "function"
@@ -126,54 +125,56 @@ export const YCursorExtension = createExtension(
126125
return {
127126
key: "yCursor",
128127
prosemirrorPlugins: [
129-
yCursorPlugin(options.provider.awareness, {
130-
selectionBuilder: defaultSelectionBuilder,
131-
cursorBuilder(user: CollaborationUser, clientID: number) {
132-
let cursorData = recentlyUpdatedCursors.get(clientID);
133-
134-
if (!cursorData) {
135-
const cursorElement = (
136-
options.renderCursor ?? defaultCursorRender
137-
)(user);
138-
139-
if (options.showCursorLabels !== "always") {
140-
cursorElement.addEventListener("mouseenter", () => {
141-
const cursor = recentlyUpdatedCursors.get(clientID)!;
142-
cursor.element.setAttribute("data-active", "");
143-
144-
if (cursor.hideTimeout) {
145-
clearTimeout(cursor.hideTimeout);
146-
recentlyUpdatedCursors.set(clientID, {
147-
element: cursor.element,
148-
hideTimeout: undefined,
128+
hasAwareness
129+
? yCursorPlugin(options.provider.awareness, {
130+
selectionBuilder: defaultSelectionBuilder,
131+
cursorBuilder(user: CollaborationUser, clientID: number) {
132+
let cursorData = recentlyUpdatedCursors.get(clientID);
133+
134+
if (!cursorData) {
135+
const cursorElement = (
136+
options.renderCursor ?? defaultCursorRender
137+
)(user);
138+
139+
if (options.showCursorLabels !== "always") {
140+
cursorElement.addEventListener("mouseenter", () => {
141+
const cursor = recentlyUpdatedCursors.get(clientID)!;
142+
cursor.element.setAttribute("data-active", "");
143+
144+
if (cursor.hideTimeout) {
145+
clearTimeout(cursor.hideTimeout);
146+
recentlyUpdatedCursors.set(clientID, {
147+
element: cursor.element,
148+
hideTimeout: undefined,
149+
});
150+
}
149151
});
150-
}
151-
});
152152

153-
cursorElement.addEventListener("mouseleave", () => {
154-
const cursor = recentlyUpdatedCursors.get(clientID)!;
153+
cursorElement.addEventListener("mouseleave", () => {
154+
const cursor = recentlyUpdatedCursors.get(clientID)!;
155155

156-
recentlyUpdatedCursors.set(clientID, {
157-
element: cursor.element,
158-
hideTimeout: setTimeout(() => {
159-
cursor.element.removeAttribute("data-active");
160-
}, 2000),
161-
});
162-
});
163-
}
156+
recentlyUpdatedCursors.set(clientID, {
157+
element: cursor.element,
158+
hideTimeout: setTimeout(() => {
159+
cursor.element.removeAttribute("data-active");
160+
}, 2000),
161+
});
162+
});
163+
}
164164

165-
cursorData = {
166-
element: cursorElement,
167-
hideTimeout: undefined,
168-
};
165+
cursorData = {
166+
element: cursorElement,
167+
hideTimeout: undefined,
168+
};
169169

170-
recentlyUpdatedCursors.set(clientID, cursorData);
171-
}
170+
recentlyUpdatedCursors.set(clientID, cursorData);
171+
}
172172

173-
return cursorData.element;
174-
},
175-
}),
176-
],
173+
return cursorData.element;
174+
},
175+
})
176+
: undefined,
177+
].filter(Boolean),
177178
dependsOn: ["ySync"],
178179
updateUser(user: { name: string; color: string; [key: string]: string }) {
179180
options.provider.awareness.setLocalStateField("user", user);

packages/core/src/extensions/Collaboration/YUndo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { redoCommand, undoCommand, yUndoPlugin } from "y-prosemirror";
22
import { createExtension } from "../../editor/BlockNoteExtension.js";
33

4-
export const YUndoExtension = createExtension(({ editor }) => {
4+
export const YUndoExtension = createExtension(() => {
55
return {
66
key: "yUndo",
7-
prosemirrorPlugins: [yUndoPlugin({ trackedOrigins: [editor] })],
7+
prosemirrorPlugins: [yUndoPlugin()],
88
dependsOn: ["yCursor", "ySync"],
99
undoCommand: undoCommand,
1010
redoCommand: redoCommand,

0 commit comments

Comments
 (0)