diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..4d690b6612 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,5 @@ +## 2024-05-24 - Avoid global counters for ProseMirror widget keys + +**Learning:** Appending global generation counters to ProseMirror `Decoration.widget` keys (e.g., `g${gen}`) circumvents built-in DOM node recycling and forces continuous recreation of widgets, leading to layout thrashing. + +**Action:** When implementing ProseMirror decorations, always use the factory function pattern `() => HTMLElement` for `Decoration.widget` construction and derive the `key` string from underlying data properties (e.g., ID, position, and properties that dictate appearance) to enable DOM node recycling. diff --git a/pkgs/id/src/blob_gate.rs b/pkgs/id/src/blob_gate.rs index d8d207fd60..e64b3bdd34 100644 --- a/pkgs/id/src/blob_gate.rs +++ b/pkgs/id/src/blob_gate.rs @@ -125,10 +125,7 @@ pub struct FoldedWorlds { impl FoldedWorlds { /// Pairs the folded worlds with a reference index. pub const fn new(worlds: Worlds, references: HashMap>) -> Self { - Self { - worlds, - references, - } + Self { worlds, references } } } diff --git a/pkgs/id/web/src/search-panel.ts b/pkgs/id/web/src/search-panel.ts index 4313cda00e..3c83ba3ab4 100644 --- a/pkgs/id/web/src/search-panel.ts +++ b/pkgs/id/web/src/search-panel.ts @@ -14,6 +14,7 @@ import { keymap } from "prosemirror-keymap"; import { findNext, findPrev, + getMatchHighlights, getSearchState, replaceAll, replaceNext, @@ -54,15 +55,8 @@ function countMatches(view: EditorView): number { const ss = getSearchState(view.state); if (!ss || !ss.query.valid) return 0; - let count = 0; - let result = ss.query.findNext(view.state, 0); - const seen = new Set(); - while (result && !seen.has(result.from)) { - seen.add(result.from); - count++; - result = ss.query.findNext(view.state, result.to); - } - return count; + // Use pre-computed highlights instead of O(N) traversal + return getMatchHighlights(view.state).find().length; } /** Update match count label. */