-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve collab cursor code (#1405)
* improve collab cursor logic * Extracted collaboration extension code to new file --------- Co-authored-by: matthewlipski <[email protected]>
- Loading branch information
1 parent
d386300
commit 7ca714c
Showing
2 changed files
with
155 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
packages/core/src/extensions/Collaboration/createCollaborationExtensions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import Collaboration from "@tiptap/extension-collaboration"; | ||
import { Awareness } from "y-protocols/awareness"; | ||
import CollaborationCursor from "@tiptap/extension-collaboration-cursor"; | ||
import * as Y from "yjs"; | ||
|
||
export const createCollaborationExtensions = (collaboration: { | ||
fragment: Y.XmlFragment; | ||
user: { | ||
name: string; | ||
color: string; | ||
[key: string]: string; | ||
}; | ||
provider: any; | ||
renderCursor?: (user: any) => HTMLElement; | ||
showCursorLabels?: "always" | "activity"; | ||
}) => { | ||
const tiptapExtensions = []; | ||
|
||
tiptapExtensions.push( | ||
Collaboration.configure({ | ||
fragment: collaboration.fragment, | ||
}) | ||
); | ||
|
||
const awareness = collaboration.provider?.awareness as Awareness | undefined; | ||
|
||
if (awareness) { | ||
const cursors = new Map< | ||
number, | ||
{ element: HTMLElement; hideTimeout: NodeJS.Timeout | undefined } | ||
>(); | ||
|
||
if (collaboration.showCursorLabels !== "always") { | ||
awareness.on( | ||
"change", | ||
({ | ||
updated, | ||
}: { | ||
added: Array<number>; | ||
updated: Array<number>; | ||
removed: Array<number>; | ||
}) => { | ||
for (const clientID of updated) { | ||
const cursor = cursors.get(clientID); | ||
|
||
if (cursor) { | ||
cursor.element.setAttribute("data-active", ""); | ||
|
||
if (cursor.hideTimeout) { | ||
clearTimeout(cursor.hideTimeout); | ||
} | ||
|
||
cursors.set(clientID, { | ||
element: cursor.element, | ||
hideTimeout: setTimeout(() => { | ||
cursor.element.removeAttribute("data-active"); | ||
}, 2000), | ||
}); | ||
} | ||
} | ||
} | ||
); | ||
} | ||
|
||
const renderCursor = (user: { name: string; color: string }) => { | ||
const cursorElement = document.createElement("span"); | ||
|
||
cursorElement.classList.add("collaboration-cursor__caret"); | ||
cursorElement.setAttribute("style", `border-color: ${user.color}`); | ||
if (collaboration?.showCursorLabels === "always") { | ||
cursorElement.setAttribute("data-active", ""); | ||
} | ||
|
||
const labelElement = document.createElement("span"); | ||
|
||
labelElement.classList.add("collaboration-cursor__label"); | ||
labelElement.setAttribute("style", `background-color: ${user.color}`); | ||
labelElement.insertBefore(document.createTextNode(user.name), null); | ||
|
||
cursorElement.insertBefore(document.createTextNode("\u2060"), null); // Non-breaking space | ||
cursorElement.insertBefore(labelElement, null); | ||
cursorElement.insertBefore(document.createTextNode("\u2060"), null); // Non-breaking space | ||
|
||
return cursorElement; | ||
}; | ||
|
||
const render = (user: { color: string; name: string }) => { | ||
const clientState = [...awareness.getStates().entries()].find( | ||
(state) => state[1].user === user | ||
); | ||
|
||
if (!clientState) { | ||
throw new Error("Could not find client state for user"); | ||
} | ||
|
||
const clientID = clientState[0]; | ||
|
||
let cursorData = cursors.get(clientID); | ||
|
||
if (!cursorData) { | ||
const cursorElement = | ||
collaboration?.renderCursor?.(user) || renderCursor(user); | ||
|
||
if (collaboration?.showCursorLabels !== "always") { | ||
cursorElement.addEventListener("mouseenter", () => { | ||
const cursor = cursors.get(clientID)!; | ||
cursor.element.setAttribute("data-active", ""); | ||
|
||
if (cursor.hideTimeout) { | ||
clearTimeout(cursor.hideTimeout); | ||
cursors.set(clientID, { | ||
element: cursor.element, | ||
hideTimeout: undefined, | ||
}); | ||
} | ||
}); | ||
|
||
cursorElement.addEventListener("mouseleave", () => { | ||
const cursor = cursors.get(clientID)!; | ||
|
||
cursors.set(clientID, { | ||
element: cursor.element, | ||
hideTimeout: setTimeout(() => { | ||
cursor.element.removeAttribute("data-active"); | ||
}, 2000), | ||
}); | ||
}); | ||
} | ||
|
||
cursorData = { | ||
element: cursorElement, | ||
hideTimeout: undefined, | ||
}; | ||
|
||
cursors.set(clientID, cursorData); | ||
} | ||
|
||
return cursorData.element; | ||
}; | ||
|
||
tiptapExtensions.push( | ||
CollaborationCursor.configure({ | ||
user: collaboration.user, | ||
render, | ||
provider: collaboration.provider, | ||
}) | ||
); | ||
} | ||
|
||
return tiptapExtensions; | ||
}; |