diff --git a/app/editor/components/FindAndReplace.tsx b/app/editor/components/FindAndReplace.tsx index a8acb22e01..5c574e60c6 100644 --- a/app/editor/components/FindAndReplace.tsx +++ b/app/editor/components/FindAndReplace.tsx @@ -225,7 +225,7 @@ export default function FindAndReplace({ }) => { editor.commands.find(attrs); }, - 250 + 100 ), [editor.commands] ); diff --git a/app/editor/extensions/FindAndReplace.tsx b/app/editor/extensions/FindAndReplace.tsx index 47f69ee6f3..0c9078ec56 100644 --- a/app/editor/extensions/FindAndReplace.tsx +++ b/app/editor/extensions/FindAndReplace.tsx @@ -490,6 +490,7 @@ export default class FindAndReplaceExtension extends Extension + !range.startContainer.isConnected || !range.endContainer.isConnected + ); + } + private handleEscape = () => { const params = new URLSearchParams(window.location.search); if (params.has("q")) { @@ -543,6 +564,8 @@ export default class FindAndReplaceExtension extends Extension { const generation = pluginKey.getState(view.state) as number; - // Rebuild highlights when the results change (generation bump) or, - // while a search is active, on any view update. The CSS Custom - // Highlight API relies on static DOM ranges that become detached - // when the editor re-renders its DOM — e.g. content settling after - // sync when navigating from search results, collaboration cursors, - // or node views mounting — none of which bump the generation. This - // keeps the highlights tracking the live DOM, as decorations do. - if (generation !== lastGeneration || this.searchTerm) { + // The results changed (search ran, doc changed, fold toggled), so + // always rebuild. + if (generation !== lastGeneration) { lastGeneration = generation; this.updateHighlights(); + return; + } + // The results are unchanged, but the CSS Custom Highlight API relies + // on static DOM ranges that become detached when the editor + // re-renders its DOM without bumping the generation — e.g. content + // settling after sync when navigating from search results, + // collaboration cursors, or node views mounting. Only pay for an + // O(n) domAtPos rebuild when the ranges have actually gone stale; + // otherwise the live highlights are still valid and we skip the work + // (cheap isConnected checks only, no layout). + if (this.searchTerm && this.highlightsStale()) { + this.updateHighlights(); } }, destroy: () => {