diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..f1050a4d14 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-02-14 - ProseMirror DOM Node Recycling +**Learning:** ProseMirror has built-in DOM node recycling for widget decorations (`Decoration.widget`). However, this is completely broken if you attach a global generation counter or randomized value to the `key` of the decoration (e.g. `g${gen}`). This forces ProseMirror to consider every single decoration as a completely new widget on every transaction, causing it to destroy and recreate the DOM nodes leading to severe layout thrashing and garbage collection pressure. +**Action:** When implementing ProseMirror decorations, always use the factory function pattern `() => HTMLElement` for `Decoration.widget` construction and derive the `key` string solely from underlying dynamic data properties (e.g., ID, position, opacity, connection state) to enable DOM node recycling. Only recreate the DOM node when its visual state fundamentally changes. diff --git a/pkgs/id/web/bun.lock b/pkgs/id/web/bun.lock index f3308c7d9f..ce75c61a43 100644 --- a/pkgs/id/web/bun.lock +++ b/pkgs/id/web/bun.lock @@ -1,6 +1,5 @@ { "lockfileVersion": 1, - "configVersion": 1, "workspaces": { "": { "name": "id-web", diff --git a/pkgs/id/web/src/cursors.ts b/pkgs/id/web/src/cursors.ts index b127290321..a0463af118 100644 --- a/pkgs/id/web/src/cursors.ts +++ b/pkgs/id/web/src/cursors.ts @@ -40,9 +40,6 @@ const RECONNECT_CLEANUP_DELAY_MS = 3_000; // Wait 3s after Init before cleanup ( const HOVER_RESTORE_DELAY_MS = 1_000; // 1s delay before restoring after hover const PENDING_REMOVAL_MS = 5_000; // Show fading cursor for 5s after disconnect -// Global counter to force widget recreation on every decoration call -let decorationGeneration = 0; - export interface CursorInfo { clientID: string | number; head: number; @@ -457,10 +454,6 @@ function createCursorDecorations( const now = Date.now(); const localHead = state.selection.head; - // Increment generation to force widget recreation - decorationGeneration++; - const gen = decorationGeneration; - // Normalize myClientID to number for comparison (handles string/number mismatch) const myClientIDNum = myClientID !== null ? Number(myClientID) : null; @@ -647,15 +640,18 @@ function createCursorDecorations( // Create own cursor decoration if present in cluster if (clusterHasOwnCursor && ownCursor) { - const ownCursorLine = document.createElement("span"); - ownCursorLine.className = "collab-cursor collab-cursor-own"; - ownCursorLine.style.borderColor = ownCursor.color; - ownCursorLine.setAttribute("data-client-id", String(ownCursor.clientID)); + const widgetFactory = () => { + const ownCursorLine = document.createElement("span"); + ownCursorLine.className = "collab-cursor collab-cursor-own"; + ownCursorLine.style.borderColor = ownCursor.color; + ownCursorLine.setAttribute("data-client-id", String(ownCursor.clientID)); + return ownCursorLine; + }; decorations.push( - Decoration.widget(ownCursor.head, ownCursorLine, { + Decoration.widget(ownCursor.head, widgetFactory, { side: 1, - key: `cursor-own-${ownCursor.clientID}-g${gen}`, + key: `cursor-own-${ownCursor.clientID}-${ownCursor.color}`, }), ); } @@ -686,63 +682,64 @@ function createCursorDecorations( // Create cursor lines for each position group (no attached labels) groupsForBar.forEach((group) => { - const firstCursor = group.cursors[0]; - const allClientIDs = group.cursors.map((c) => c.clientID); - const cursorLine = createCursorLine(firstCursor, true, connectionState, allClientIDs); - cursorLine.style.borderColor = group.mostRecentColor; - - // For merged cursor lines with bars, use full opacity on the line - // (the bar segments handle their own individual opacities) - if (group === leftmostGroup) { - cursorLine.style.opacity = "1"; - cursorLine.style.setProperty("--base-opacity", "1"); - cursorLine.style.animation = "none"; // No strobe on bar container - } - - // Track the leftmost cursor line for bar attachment - if (group === leftmostGroup) { - leftmostCursorLine = cursorLine; - } + const groupOpacities = group.cursors.map((c) => c.opacity).join("-"); + const stableKey = `cursor-cluster-${group.position}-${clusterKey}-c${connectionState}-o${groupOpacities}-co${group.mostRecentColor}`; + + const widgetFactory = () => { + const firstCursor = group.cursors[0]; + const allClientIDs = group.cursors.map((c) => c.clientID); + const cursorLine = createCursorLine(firstCursor, true, connectionState, allClientIDs); + cursorLine.style.borderColor = group.mostRecentColor; + + // For merged cursor lines with bars, use full opacity on the line + // (the bar segments handle their own individual opacities) + if (group === leftmostGroup) { + cursorLine.style.opacity = "1"; + cursorLine.style.setProperty("--base-opacity", "1"); + cursorLine.style.animation = "none"; // No strobe on bar container + + // Create merged tooltip bar and attach to leftmost cursor line + const mergedBar = createMergedBar(groupsForBar, handleMouseEnter, handleMouseLeave, connectionState); + cursorLine.appendChild(mergedBar); + } - // Register all cursors in this group for strobe tracking - if (viewState) { - group.cursors.forEach((cursor) => { - viewState.strobeInfos.set(cursor.clientID, { - element: cursorLine, - baseOpacity: cursor.opacity, - paused: false, + // Register all cursors in this group for strobe tracking + if (viewState) { + group.cursors.forEach((cursor) => { + viewState.strobeInfos.set(cursor.clientID, { + element: cursorLine, + baseOpacity: cursor.opacity, + paused: false, + }); }); - }); - } + } + + return cursorLine; + }; - // Use cluster-wide key + generation to force recreation on every update decorations.push( - Decoration.widget(group.position, cursorLine, { + Decoration.widget(group.position, widgetFactory, { side: 1, - key: `cursor-cluster-${group.position}-${clusterKey}-g${gen}`, + key: stableKey, }), ); }); - - // Create merged tooltip bar and attach to leftmost cursor line - // (appending to cursor line ensures proper positioning via position: relative) - const mergedBar = createMergedBar(groupsForBar, handleMouseEnter, handleMouseLeave, connectionState); - - if (leftmostCursorLine) { - (leftmostCursorLine as HTMLElement).appendChild(mergedBar); - } } else { // Single remote cursor, no overlap - simple standalone cursor const group = groupsForBar[0]; const cursor = group.cursors[0]; - const cursorWidget = viewState - ? createStandaloneCursor(cursor, connectionState, viewState, handleMouseEnter, handleMouseLeave) - : createCursorLine(cursor, false, connectionState); + const stableKey = `cursor-${cursor.clientID}-c${connectionState}-o${cursor.opacity}-co${cursor.color}`; + + const widgetFactory = () => { + return viewState + ? createStandaloneCursor(cursor, connectionState, viewState, handleMouseEnter, handleMouseLeave) + : createCursorLine(cursor, false, connectionState); + }; decorations.push( - Decoration.widget(cursor.head, cursorWidget, { + Decoration.widget(cursor.head, widgetFactory, { side: 1, - key: `cursor-${cursor.clientID}-g${gen}`, + key: stableKey, }), ); } @@ -765,7 +762,7 @@ function createCursorDecorations( style: `background-color: ${cursor.color}${selectionOpacity};`, }, { - key: `selection-${cursor.clientID}-g${gen}`, + key: `selection-${cursor.clientID}-o${cursor.opacity}-co${cursor.color}`, }, ), );