diff --git a/gui/src/components/find/FindWidget.tsx b/gui/src/components/find/FindWidget.tsx
index 36adc1f50..c3fb37ef9 100644
--- a/gui/src/components/find/FindWidget.tsx
+++ b/gui/src/components/find/FindWidget.tsx
@@ -1,9 +1,9 @@
-import { useRef, useEffect, useState, RefObject, useCallback, useMemo } from 'react';
-import { RootState } from '../../redux/store';
-import { useSelector } from 'react-redux';
-import { HeaderButton, Input } from '..';
-import ButtonWithTooltip from '../ButtonWithTooltip';
-import { ArrowDownIcon, ArrowUpIcon, XMarkIcon } from '@heroicons/react/24/outline';
+import { useRef, useEffect, useState, RefObject, useCallback, useMemo } from "react";
+import { RootState } from "../../redux/store";
+import { useSelector } from "react-redux";
+import { HeaderButton, Input } from "..";
+import ButtonWithTooltip from "../ButtonWithTooltip";
+import { ArrowDownIcon, ArrowUpIcon, XMarkIcon } from "@heroicons/react/24/outline";
interface SearchMatch {
index: number;
@@ -11,7 +11,7 @@ interface SearchMatch {
overlayRectangle: Rectangle;
}
-type ScrollToMatchOption = 'closest' | 'first' | 'none'
+type ScrollToMatchOption = "closest" | "first" | "none"
const SEARCH_DEBOUNCE = 300
const RESIZE_DEBOUNCE = 200
@@ -23,7 +23,6 @@ interface Rectangle {
height: number;
}
-
interface HighlightOverlayProps extends Rectangle {
isCurrent: boolean;
}
@@ -32,15 +31,15 @@ const HighlightOverlay = (props: HighlightOverlayProps) => {
const { isCurrent, top, left, width, height } = props;
return (
@@ -82,7 +81,7 @@ export const useFindWidget = (searchRef: RefObject) => {
searchRef.current.scrollTo({
top: match.overlayRectangle.top - searchRef.current.clientHeight / 2,
left: match.overlayRectangle.left - searchRef.current.clientWidth / 2,
- behavior: 'smooth'
+ behavior: "smooth"
})
}, [searchRef.current])
@@ -103,16 +102,16 @@ export const useFindWidget = (searchRef: RefObject) => {
// Handle keyboard shortcuts for navigation
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
- if (event.metaKey && event.key.toLowerCase() === 'f') {
+ if (event.metaKey && event.key.toLowerCase() === "f") {
event.preventDefault();
event.stopPropagation();
openWidget();
} else if (document.activeElement === inputRef.current) {
- if (event.key === 'Escape') {
+ if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
setOpen(false);
- } else if (event.key === 'Enter') {
+ } else if (event.key === "Enter") {
event.preventDefault();
event.stopPropagation();
if (event.shiftKey) previousMatch()
@@ -121,9 +120,9 @@ export const useFindWidget = (searchRef: RefObject) => {
}
}
- document.addEventListener('keydown', handleKeyDown);
+ document.addEventListener("keydown", handleKeyDown);
return () => {
- document.removeEventListener('keydown', handleKeyDown);
+ document.removeEventListener("keydown", handleKeyDown);
};
}, [inputRef, matches, nextMatch]);
@@ -153,7 +152,7 @@ export const useFindWidget = (searchRef: RefObject) => {
}, [searchRef.current]);
// Main function for finding matches and generating highlight overlays
- const refreshSearch = useCallback((scrollTo: ScrollToMatchOption = 'none', clearFirst = false) => {
+ const refreshSearch = useCallback((scrollTo: ScrollToMatchOption = "none", clearFirst = false) => {
if (clearFirst) setMatches([])
const _query = debouncedInput.current; // trimStart - decided no because spaces should be fully searchable
@@ -164,7 +163,7 @@ export const useFindWidget = (searchRef: RefObject) => {
const query = caseSensitive ? _query : _query.toLowerCase()
// First grab all text nodes
- // Skips any elements with the 'find-widget-skip' class
+ // Skips any elements with the "find-widget-skip" class
const textNodes: Text[] = [];
const walker = document.createTreeWalker(
searchRef.current,
@@ -172,7 +171,7 @@ export const useFindWidget = (searchRef: RefObject) => {
{
acceptNode: (node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
- if ((node as Element).classList.contains('find-widget-skip')) return NodeFilter.FILTER_REJECT;
+ if ((node as Element).classList.contains("find-widget-skip")) return NodeFilter.FILTER_REJECT;
return NodeFilter.FILTER_ACCEPT;
} else if (node.nodeType === Node.TEXT_NODE) {
if (!node.nodeValue) return NodeFilter.FILTER_REJECT;
@@ -189,17 +188,12 @@ export const useFindWidget = (searchRef: RefObject) => {
textNodes.push(walker.currentNode as Text);
}
- // Keep track of the match closest match to the middle of the view
- const verticalMiddle = searchRef.current.scrollTop + searchRef.current.clientHeight / 2
- let closestDist = Infinity
- let closestMatchToMiddle: SearchMatch | null = null
-
// Now walk through each node match and extract search results
// One node can have several matches
const newMatches: SearchMatch[] = [];
textNodes.forEach((textNode, idx) => {
// Hacky way to detect code blocks that be wider than client and cause absolute positioning to fail
- const highlightFullLine = textNode.parentElement.className.includes('hljs')
+ const highlightFullLine = textNode.parentElement.className.includes("hljs")
let nodeTextValue = caseSensitive ? textNode.nodeValue : textNode.nodeValue.toLowerCase();
let startIndex = 0;
@@ -229,13 +223,6 @@ export const useFindWidget = (searchRef: RefObject) => {
}
newMatches.push(newMatch);
- // Keep track of the match closest match to the middle of the view
- const dist = Math.abs(verticalMiddle - top);
- if (dist < closestDist) {
- closestDist = dist;
- closestMatchToMiddle = newMatch
- }
-
if (highlightFullLine) {
break; // Since highlighting full line no need for multiple overlays, will cause darker highlight
}
@@ -247,16 +234,34 @@ export const useFindWidget = (searchRef: RefObject) => {
const matchHash = Object.fromEntries(newMatches.map(match => [JSON.stringify(match.overlayRectangle), match]))
const filteredMatches = Object.values(matchHash).map((match, index) => ({ ...match, index }));
+ // Find match closest to the middle of the view
+ const verticalMiddle = searchRef.current.scrollTop + searchRef.current.clientHeight / 2
+ let closestDist = Infinity
+ let closestMatchToMiddle: SearchMatch | null = null
+ filteredMatches.forEach((match) => {
+ const dist = Math.abs(verticalMiddle - match.overlayRectangle.top);
+ if (dist < closestDist) {
+ closestDist = dist;
+ closestMatchToMiddle = match;
+ }
+ })
+
// Update matches and scroll to the closest or first match
setMatches(filteredMatches);
if (query.length > 1 && filteredMatches.length) {
- // console.log(scrollTo)
- if (scrollTo === 'first') {
+ if (scrollTo === "first") {
scrollToMatch(filteredMatches[0]);
}
- if (scrollTo === 'closest' && closestMatchToMiddle) {
+ if (scrollTo === "closest") {
scrollToMatch(closestMatchToMiddle)
}
+ if (scrollTo === "none") {
+ if (closestMatchToMiddle) {
+ setCurrentMatch(closestMatchToMiddle)
+ } else {
+ setCurrentMatch(filteredMatches[0])
+ }
+ }
}
}, [searchRef.current, debouncedInput, scrollToMatch, caseSensitive, useRegex])
@@ -267,7 +272,7 @@ export const useFindWidget = (searchRef: RefObject) => {
const debounce = () => {
debouncedInput.current = input
lastUpdateRef.current = Date.now();
- refreshSearch('closest');
+ refreshSearch("closest");
}
const timeSinceLastUpdate = Date.now() - lastUpdateRef.current;
if (timeSinceLastUpdate >= SEARCH_DEBOUNCE) {
@@ -282,41 +287,44 @@ export const useFindWidget = (searchRef: RefObject) => {
}
}, [refreshSearch, input]);
+ // Could consider doing any window click but I only handled search div here
+ // Since usually only clicks in search div will cause content changes in search div
useEffect(() => {
- if (!open) return
- const handleWindowClick = () => {
- refreshSearch('none');
+ if (!open || !searchRef.current) return
+ const handleSearchRefClick = () => {
+ refreshSearch("none");
}
- window.addEventListener('click', handleWindowClick)
+ searchRef.current.addEventListener("click", handleSearchRefClick)
return () => {
- window.removeEventListener('click', handleWindowClick);
+ searchRef.current.removeEventListener("click", handleSearchRefClick);
};
- }, [refreshSearch, open]);
+ }, [searchRef.current, refreshSearch, open]);
// Triggers that should cause results to temporarily disappear and then reload
// Active = LLM is generating, etc.
const active = useSelector((state: RootState) => state.state.active);
useEffect(() => {
if (active || isResizing) setMatches([])
- else refreshSearch('none')
+ else refreshSearch("none")
}, [refreshSearch, active]);
useEffect(() => {
if (!open) setMatches([])
- else refreshSearch('closest')
+ else refreshSearch("closest")
}, [refreshSearch, open]);
useEffect(() => {
- refreshSearch('closest')
+ refreshSearch("closest")
}, [refreshSearch, caseSensitive, useRegex])
// Find widget component
const widget = useMemo(() => {
return (
) => {
}}
placeholder="Search..."
/>
-
+
{matches.length === 0 ? "No results" : `${(currentMatch?.index ?? 0) + 1} of ${matches.length}`}
-
+
{
e.stopPropagation()
- // e.preventDefault()
previousMatch()
}}
- className='h-4 w-4 focus:ring focus:ring-1'
- disabled={matches.length < 2}
+ className="h-4 w-4 focus:ring focus:ring-1"
+ disabled={matches.length < 2 || active}
>
-
+
{
e.stopPropagation()
- // e.preventDefault()
nextMatch()
}}
- className='h-4 w-4 focus:ring focus:ring-1'
- disabled={matches.length < 2}
+ className="h-4 w-4 focus:ring focus:ring-1"
+ disabled={matches.length < 2 || active}
>
-
+
{
e.stopPropagation()
- // e.preventDefault()
setCaseSensitive(curr => !curr)
}}
- className='h-5 w-6 text-xs focus:ring focus:ring-1 rounded-full border focus:outline-none'
+ className="h-5 w-6 text-xs focus:ring focus:ring-1 rounded-full border focus:outline-none"
>
Aa
@@ -373,9 +379,9 @@ export const useFindWidget = (searchRef: RefObject
) => {
setOpen(false)}
- className='focus:ring focus:ring-1'
+ className="focus:ring focus:ring-1"
>
-
+
)