|
| 1 | +import { Plugin } from "prosemirror-state"; |
| 2 | +import { defaultSelectionBuilder, yCursorPlugin } from "y-prosemirror"; |
| 3 | +import { Awareness } from "y-protocols/awareness.js"; |
| 4 | +import * as Y from "yjs"; |
| 5 | + |
| 6 | +export type CollaborationUser = { |
| 7 | + name: string; |
| 8 | + color: string; |
| 9 | + [key: string]: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export class CursorPlugin { |
| 13 | + public plugin: Plugin; |
| 14 | + private provider: { awareness: Awareness }; |
| 15 | + private recentlyUpdatedCursors: Map< |
| 16 | + number, |
| 17 | + { element: HTMLElement; hideTimeout: NodeJS.Timeout | undefined } |
| 18 | + >; |
| 19 | + constructor( |
| 20 | + private collaboration: { |
| 21 | + fragment: Y.XmlFragment; |
| 22 | + user: CollaborationUser; |
| 23 | + provider: { awareness: Awareness }; |
| 24 | + renderCursor?: (user: CollaborationUser) => HTMLElement; |
| 25 | + showCursorLabels?: "always" | "activity"; |
| 26 | + } |
| 27 | + ) { |
| 28 | + this.provider = collaboration.provider; |
| 29 | + this.recentlyUpdatedCursors = new Map(); |
| 30 | + |
| 31 | + this.provider.awareness.setLocalStateField("user", collaboration.user); |
| 32 | + |
| 33 | + if (collaboration.showCursorLabels !== "always") { |
| 34 | + this.provider.awareness.on( |
| 35 | + "change", |
| 36 | + ({ |
| 37 | + updated, |
| 38 | + }: { |
| 39 | + added: Array<number>; |
| 40 | + updated: Array<number>; |
| 41 | + removed: Array<number>; |
| 42 | + }) => { |
| 43 | + for (const clientID of updated) { |
| 44 | + const cursor = this.recentlyUpdatedCursors.get(clientID); |
| 45 | + |
| 46 | + if (cursor) { |
| 47 | + cursor.element.setAttribute("data-active", ""); |
| 48 | + |
| 49 | + if (cursor.hideTimeout) { |
| 50 | + clearTimeout(cursor.hideTimeout); |
| 51 | + } |
| 52 | + |
| 53 | + this.recentlyUpdatedCursors.set(clientID, { |
| 54 | + element: cursor.element, |
| 55 | + hideTimeout: setTimeout(() => { |
| 56 | + cursor.element.removeAttribute("data-active"); |
| 57 | + }, 2000), |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + this.plugin = yCursorPlugin(this.provider.awareness, { |
| 66 | + selectionBuilder: defaultSelectionBuilder, |
| 67 | + cursorBuilder: this.renderCursor, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + public get priority() { |
| 72 | + return 999; |
| 73 | + } |
| 74 | + |
| 75 | + private renderCursor = (user: CollaborationUser, clientID: number) => { |
| 76 | + let cursorData = this.recentlyUpdatedCursors.get(clientID); |
| 77 | + |
| 78 | + if (!cursorData) { |
| 79 | + const cursorElement = ( |
| 80 | + this.collaboration.renderCursor ?? CursorPlugin.defaultCursorRender |
| 81 | + )(user); |
| 82 | + |
| 83 | + if (this.collaboration.showCursorLabels !== "always") { |
| 84 | + cursorElement.addEventListener("mouseenter", () => { |
| 85 | + const cursor = this.recentlyUpdatedCursors.get(clientID)!; |
| 86 | + cursor.element.setAttribute("data-active", ""); |
| 87 | + |
| 88 | + if (cursor.hideTimeout) { |
| 89 | + clearTimeout(cursor.hideTimeout); |
| 90 | + this.recentlyUpdatedCursors.set(clientID, { |
| 91 | + element: cursor.element, |
| 92 | + hideTimeout: undefined, |
| 93 | + }); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + cursorElement.addEventListener("mouseleave", () => { |
| 98 | + const cursor = this.recentlyUpdatedCursors.get(clientID)!; |
| 99 | + |
| 100 | + this.recentlyUpdatedCursors.set(clientID, { |
| 101 | + element: cursor.element, |
| 102 | + hideTimeout: setTimeout(() => { |
| 103 | + cursor.element.removeAttribute("data-active"); |
| 104 | + }, 2000), |
| 105 | + }); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + cursorData = { |
| 110 | + element: cursorElement, |
| 111 | + hideTimeout: undefined, |
| 112 | + }; |
| 113 | + |
| 114 | + this.recentlyUpdatedCursors.set(clientID, cursorData); |
| 115 | + } |
| 116 | + |
| 117 | + return cursorData.element; |
| 118 | + }; |
| 119 | + |
| 120 | + public updateUser = (user: { |
| 121 | + name: string; |
| 122 | + color: string; |
| 123 | + [key: string]: string; |
| 124 | + }) => { |
| 125 | + this.provider.awareness.setLocalStateField("user", user); |
| 126 | + }; |
| 127 | + |
| 128 | + public static defaultCursorRender = (user: CollaborationUser) => { |
| 129 | + const cursorElement = document.createElement("span"); |
| 130 | + |
| 131 | + cursorElement.classList.add("bn-collaboration-cursor__base"); |
| 132 | + |
| 133 | + const caretElement = document.createElement("span"); |
| 134 | + caretElement.setAttribute("contentedEditable", "false"); |
| 135 | + caretElement.classList.add("bn-collaboration-cursor__caret"); |
| 136 | + caretElement.setAttribute("style", `background-color: ${user.color}`); |
| 137 | + |
| 138 | + const labelElement = document.createElement("span"); |
| 139 | + |
| 140 | + labelElement.classList.add("bn-collaboration-cursor__label"); |
| 141 | + labelElement.setAttribute("style", `background-color: ${user.color}`); |
| 142 | + labelElement.insertBefore(document.createTextNode(user.name), null); |
| 143 | + |
| 144 | + caretElement.insertBefore(labelElement, null); |
| 145 | + |
| 146 | + cursorElement.insertBefore(document.createTextNode("\u2060"), null); // Non-breaking space |
| 147 | + cursorElement.insertBefore(caretElement, null); |
| 148 | + cursorElement.insertBefore(document.createTextNode("\u2060"), null); // Non-breaking space |
| 149 | + |
| 150 | + return cursorElement; |
| 151 | + }; |
| 152 | +} |
0 commit comments