Skip to content

Commit d967f91

Browse files
committed
add a input ref in viewmodel
1 parent 8ef705c commit d967f91

4 files changed

Lines changed: 23 additions & 42 deletions

File tree

frontend/app/element/search.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import "./search.scss";
1111

1212
type SearchProps = SearchAtoms & {
1313
anchorRef?: React.RefObject<HTMLElement>;
14+
searchInputRef?: React.RefObject<HTMLInputElement>;
1415
offsetX?: number;
1516
offsetY?: number;
1617
onSearch?: (search: string) => void;
1718
onNext?: () => void;
1819
onPrev?: () => void;
19-
blockId?: string;
2020
};
2121

2222
const SearchComponent = ({
@@ -28,13 +28,15 @@ const SearchComponent = ({
2828
wholeWord: wholeWordAtom,
2929
isOpen: isOpenAtom,
3030
anchorRef,
31+
searchInputRef: providedInputRef,
3132
offsetX = 10,
3233
offsetY = 10,
3334
onSearch,
3435
onNext,
3536
onPrev,
36-
blockId,
3737
}: SearchProps) => {
38+
const localInputRef = useRef<HTMLInputElement>(null);
39+
const inputRef = providedInputRef || localInputRef;
3840
const [isOpen, setIsOpen] = useAtom<boolean>(isOpenAtom);
3941
const [search, setSearch] = useAtom<string>(searchAtom);
4042
const [index, setIndex] = useAtom<number>(indexAtom);
@@ -146,8 +148,9 @@ const SearchComponent = ({
146148
<>
147149
{isOpen && (
148150
<FloatingPortal>
149-
<div className="search-container" style={{ ...floatingStyles }} ref={refs.setFloating} data-blockid={blockId}>
151+
<div className="search-container" style={{ ...floatingStyles }} ref={refs.setFloating}>
150152
<Input
153+
ref={inputRef}
151154
placeholder="Search"
152155
value={search}
153156
onChange={setSearch}
@@ -190,7 +193,6 @@ type SearchOptions = {
190193
regex?: boolean;
191194
caseSensitive?: boolean;
192195
wholeWord?: boolean;
193-
blockId?: string;
194196
};
195197

196198
export function useSearch(options?: SearchOptions): SearchProps {
@@ -207,15 +209,19 @@ export function useSearch(options?: SearchOptions): SearchProps {
207209
[]
208210
);
209211
const anchorRef = options?.anchorRef ?? useRef(null);
212+
const searchInputRef = useRef<HTMLInputElement>(null);
210213
useEffect(() => {
211214
if (options?.viewModel) {
212215
options.viewModel.searchAtoms = searchAtoms;
216+
// Store inputRef on viewModel for external access (e.g., keymodel.ts)
217+
(options.viewModel as any).searchInputRef = searchInputRef;
213218
return () => {
214219
options.viewModel.searchAtoms = undefined;
220+
(options.viewModel as any).searchInputRef = undefined;
215221
};
216222
}
217-
}, [options?.viewModel]);
218-
return { ...searchAtoms, anchorRef, blockId: options?.blockId };
223+
}, [options?.viewModel, searchAtoms]);
224+
return { ...searchAtoms, anchorRef, searchInputRef };
219225
}
220226

221227
const createToggleButtonDecl = (

frontend/app/store/keymodel.ts

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -653,48 +653,24 @@ function registerGlobalKeys() {
653653
return "";
654654
}
655655

656-
function focusSearchInput() {
657-
setTimeout(() => {
658-
const blockId = getFocusedBlockInStaticTab();
659-
if (!blockId) {
660-
return;
661-
}
662-
663-
// Directly find the search container by data-blockid attribute
664-
const searchContainer = document.querySelector(
665-
`.search-container[data-blockid="${blockId}"]`
666-
) as HTMLElement;
667-
if (searchContainer) {
668-
const searchInput = searchContainer.querySelector("input") as HTMLInputElement;
669-
if (searchInput) {
670-
searchInput.focus();
671-
searchInput.select();
672-
}
673-
}
674-
}, 0);
675-
}
676-
677656
function activateSearch(event: WaveKeyboardEvent): boolean {
678657
const bcm = getBlockComponentModel(getFocusedBlockInStaticTab());
679658
// Ctrl+f is reserved in most shells.
680659
if (event.control && bcm.viewModel.viewType == "term") {
681660
return false;
682661
}
683662
if (bcm.viewModel.searchAtoms) {
684-
const searchAtoms = bcm.viewModel.searchAtoms;
685-
const isOpen = globalStore.get(searchAtoms.isOpen);
686-
const selectedText = getSelectedText();
687-
688-
// Open search dialog if not already open
689-
if (!isOpen) {
690-
globalStore.set(searchAtoms.isOpen, true);
663+
let selectedText = getSelectedText();
664+
globalStore.set(bcm.viewModel.searchAtoms.isOpen, true);
665+
globalStore.set(bcm.viewModel.searchAtoms.searchValue, selectedText);
666+
667+
// Focus the search input using the exposed searchInputRef
668+
const searchInputRef = bcm.viewModel.searchInputRef;
669+
if (searchInputRef?.current) {
670+
setTimeout(() => {
671+
searchInputRef.current?.focus();
672+
}, 10);
691673
}
692-
// Set search value (use selected text if available, otherwise empty string)
693-
globalStore.set(searchAtoms.searchValue, selectedText || "");
694-
// Reset search results
695-
globalStore.set(searchAtoms.resultsIndex, 0);
696-
globalStore.set(searchAtoms.resultsCount, 0);
697-
focusSearchInput();
698674
return true;
699675
}
700676
return false;

frontend/app/view/term/term.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ const TerminalView = ({ blockId, model }: ViewComponentProps<TermViewModel>) =>
178178
caseSensitive: false,
179179
wholeWord: false,
180180
regex: false,
181-
blockId: blockId,
182181
});
183182
const searchIsOpen = jotai.useAtomValue<boolean>(searchProps.isOpen);
184183
const caseSensitive = useAtomValueSafe<boolean>(searchProps.caseSensitive);

frontend/app/view/webview/webview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ const WebView = memo(({ model, onFailLoad, blockRef, initialSrc }: WebViewProps)
821821
}
822822

823823
// Search
824-
const searchProps = useSearch({ anchorRef: model.webviewRef, viewModel: model, blockId: model.blockId });
824+
const searchProps = useSearch({ anchorRef: model.webviewRef, viewModel: model });
825825
const searchVal = useAtomValue<string>(searchProps.searchValue);
826826
const setSearchIndex = useSetAtom(searchProps.resultsIndex);
827827
const setNumSearchResults = useSetAtom(searchProps.resultsCount);

0 commit comments

Comments
 (0)