Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

**Learning:** Counting match highlights in ProseMirror search by repeatedly calling `findNext` performs an O(N) document traversal per match, which becomes a severe bottleneck (e.g. 1.7s for 10k matches) when document sizes scale or searches are frequent. `prosemirror-search` already computes a `DecorationSet` internally containing all matches.
**Action:** Always use `getMatchHighlights(view.state).find()` to retrieve search matches in O(1) time instead of iterating manually over the document with `findNext` or `findNextNoWrap`.

## 2025-02-24 - [ProseMirror Decoration Key Generation]

**Learning:** Avoid appending global generation counters or randomized values to ProseMirror `Decoration.widget` keys, as this circumvents built-in DOM node recycling and forces continuous recreation of widgets, leading to layout thrashing.
**Action:** Use stable keys for `Decoration.widget` to enable DOM node recycling, ensuring any properties that dynamically dictate the widget's appearance (such as opacity, connectionState, or color) are explicitly included in the key string. When implementing ProseMirror decorations, prefer the factory function pattern `() => HTMLElement` for `Decoration.widget` construction.
15 changes: 4 additions & 11 deletions pkgs/id/web/src/cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -655,7 +648,7 @@ function createCursorDecorations(
decorations.push(
Decoration.widget(ownCursor.head, ownCursorLine, {
side: 1,
key: `cursor-own-${ownCursor.clientID}-g${gen}`,
key: `cursor-own-${ownCursor.clientID}-c${ownCursor.color}-o${ownCursor.opacity}-s${connectionState}`,
}),
);
}
Expand Down Expand Up @@ -719,7 +712,7 @@ function createCursorDecorations(
decorations.push(
Decoration.widget(group.position, cursorLine, {
side: 1,
key: `cursor-cluster-${group.position}-${clusterKey}-g${gen}`,
key: `cursor-cluster-${group.position}-${clusterKey}-s${connectionState}`,
}),
);
});
Expand All @@ -742,7 +735,7 @@ function createCursorDecorations(
decorations.push(
Decoration.widget(cursor.head, cursorWidget, {
side: 1,
key: `cursor-${cursor.clientID}-g${gen}`,
key: `cursor-${cursor.clientID}-c${cursor.color}-o${cursor.opacity}-s${connectionState}`,
}),
);
}
Expand All @@ -765,7 +758,7 @@ function createCursorDecorations(
style: `background-color: ${cursor.color}${selectionOpacity};`,
},
{
key: `selection-${cursor.clientID}-g${gen}`,
key: `selection-${cursor.clientID}-c${cursor.color}-o${cursor.opacity}`,
},
),
);
Expand Down
Loading