All files / webview/search useSearch.ts

95.18% Statements 79/83
83.33% Branches 25/30
92% Functions 23/25
95.83% Lines 69/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                                                                        63x                   63x 63x 63x   63x 17x   11x 10x     1x       63x         17x 17x 6x 6x 6x 6x     11x 17x   17x       17x 17x   11x             63x   63x 23x 23x                       63x 14x 14x     63x 1x           1x 1x     63x 10x 10x             63x 3x 3x 3x         3x       63x 2x 2x 2x         2x       63x 2x 2x 2x         2x       63x 4x 4x 3x 3x 3x 3x 3x       63x 2x 2x 1x 1x 1x 1x 1x       63x                     63x    
import { useState, useCallback, useRef, useEffect } from 'react';
import type { EditorMode } from '../Toolbar';
import {
  findMatches,
  applyHighlights,
  clearHighlights,
  scrollToMatch,
  type MatchRange,
  type SearchOptions,
} from './domHighlighter';
 
export interface SearchState {
  isOpen: boolean;
  query: string;
  caseSensitive: boolean;
  wholeWord: boolean;
  useRegex: boolean;
  matchCount: number;
  currentIndex: number;
}
 
export interface SearchActions {
  open: () => void;
  close: () => void;
  setQuery: (q: string) => void;
  toggleCaseSensitive: () => void;
  toggleWholeWord: () => void;
  toggleRegex: () => void;
  findNext: () => void;
  findPrev: () => void;
}
 
export function useSearch(
  mode: EditorMode,
  contentContainerRef: React.RefObject<HTMLDivElement | null>,
) {
  const [state, setState] = useState<SearchState>({
    isOpen: false,
    query: '',
    caseSensitive: false,
    wholeWord: false,
    useRegex: false,
    matchCount: 0,
    currentIndex: -1,
  });
 
  const domMatchesRef = useRef<MatchRange[]>([]);
  const searchInputRef = useRef<HTMLInputElement | null>(null);
  const searchParamsRef = useRef(state);
 
  const getSearchContainer = useCallback((): Element | null => {
    if (!contentContainerRef.current) return null;
 
    if (mode === 'preview') {
      return contentContainerRef.current.querySelector('.markdown-section') ??
             contentContainerRef.current;
    }
    return contentContainerRef.current.querySelector('.ProseMirror') ??
           contentContainerRef.current;
  }, [mode, contentContainerRef]);
 
  const executeSearch = useCallback((
    query: string,
    options: SearchOptions,
    newIndex?: number,
  ) => {
    const container = getSearchContainer();
    if (!container) {
      domMatchesRef.current = [];
      clearHighlights();
      setState(prev => ({ ...prev, matchCount: 0, currentIndex: -1 }));
      return;
    }
 
    const matches = query ? findMatches(container, query, options) : [];
    domMatchesRef.current = matches;
 
    const idx = matches.length > 0
      ? Math.min(newIndex ?? 0, matches.length - 1)
      : -1;
 
    applyHighlights(matches, idx);
    if (idx >= 0) scrollToMatch(matches[idx]);
 
    setState(prev => ({
      ...prev,
      matchCount: matches.length,
      currentIndex: idx,
    }));
  }, [getSearchContainer]);
 
  searchParamsRef.current = state;
 
  useEffect(() => {
    const params = searchParamsRef.current;
    Iif (params.isOpen && params.query) {
      const timer = setTimeout(() => {
        executeSearch(params.query, {
          caseSensitive: params.caseSensitive,
          wholeWord: params.wholeWord,
          useRegex: params.useRegex,
        });
      }, 100);
      return () => clearTimeout(timer);
    }
  }, [mode, executeSearch]);
 
  const open = useCallback(() => {
    setState(prev => ({ ...prev, isOpen: true }));
    requestAnimationFrame(() => searchInputRef.current?.focus());
  }, []);
 
  const close = useCallback(() => {
    setState(prev => ({
      ...prev,
      isOpen: false,
      matchCount: 0,
      currentIndex: -1,
    }));
    domMatchesRef.current = [];
    clearHighlights();
  }, []);
 
  const setQueryText = useCallback((q: string) => {
    setState(prev => ({ ...prev, query: q }));
    executeSearch(q, {
      caseSensitive: state.caseSensitive,
      wholeWord: state.wholeWord,
      useRegex: state.useRegex,
    });
  }, [executeSearch, state.caseSensitive, state.wholeWord, state.useRegex]);
 
  const toggleCaseSensitive = useCallback(() => {
    setState(prev => {
      const next = { ...prev, caseSensitive: !prev.caseSensitive };
      executeSearch(prev.query, {
        caseSensitive: next.caseSensitive,
        wholeWord: prev.wholeWord,
        useRegex: prev.useRegex,
      });
      return next;
    });
  }, [executeSearch]);
 
  const toggleWholeWord = useCallback(() => {
    setState(prev => {
      const next = { ...prev, wholeWord: !prev.wholeWord };
      executeSearch(prev.query, {
        caseSensitive: prev.caseSensitive,
        wholeWord: next.wholeWord,
        useRegex: prev.useRegex,
      });
      return next;
    });
  }, [executeSearch]);
 
  const toggleRegex = useCallback(() => {
    setState(prev => {
      const next = { ...prev, useRegex: !prev.useRegex };
      executeSearch(prev.query, {
        caseSensitive: prev.caseSensitive,
        wholeWord: prev.wholeWord,
        useRegex: next.useRegex,
      });
      return next;
    });
  }, [executeSearch]);
 
  const findNext = useCallback(() => {
    const matches = domMatchesRef.current;
    if (matches.length === 0) return;
    setState(prev => {
      const newIdx = prev.currentIndex + 1 >= matches.length ? 0 : prev.currentIndex + 1;
      applyHighlights(matches, newIdx);
      scrollToMatch(matches[newIdx]);
      return { ...prev, currentIndex: newIdx };
    });
  }, []);
 
  const findPrev = useCallback(() => {
    const matches = domMatchesRef.current;
    if (matches.length === 0) return;
    setState(prev => {
      const newIdx = prev.currentIndex - 1 < 0 ? matches.length - 1 : prev.currentIndex - 1;
      applyHighlights(matches, newIdx);
      scrollToMatch(matches[newIdx]);
      return { ...prev, currentIndex: newIdx };
    });
  }, []);
 
  const actions: SearchActions = {
    open,
    close,
    setQuery: setQueryText,
    toggleCaseSensitive,
    toggleWholeWord,
    toggleRegex,
    findNext,
    findPrev,
  };
 
  return { state, actions, searchInputRef };
}