Файли
openidea/src/ui/treeDirectoryContext.ts
T
l0ng-ai c5cb217c7c fix(search): scope Find in Files to the focused folder (#534)
Find in Files from the keyboard ignored the selected folder and reused the saved scope; only the context menu passed a directory.

Focused trees now publish the selected folder, or a file's parent, as an action context, and Find and Replace in Files use it when no explicit directory is given. This covers the Project view, Commit and Changes, Bookmarks and Scratches. Group rows and editor focus keep the previous behaviour.

Fixes #526
2026-10-31 12:43:42 +08:00

37 рядки
1.1 KiB
TypeScript

import { useLayoutEffect, type RefObject } from "react";
import { setContext } from "@/platform/context";
import { isVirtualUri } from "@/platform/virtualDocs";
let owner: HTMLElement | null = null;
/** Only the focused tree contributes its selected directory to actions. */
export function useTreeDirectoryContext(
ref: RefObject<HTMLElement | null>,
directory: string | null | undefined,
enabled: boolean,
) {
useLayoutEffect(() => {
if (!enabled) return;
const element = ref.current;
if (!element) return;
const clear = () => {
if (owner !== element) return;
owner = null;
setContext("focusedDirectory", undefined);
};
const publish = () => {
if (document.activeElement !== element) return;
owner = element;
setContext("focusedDirectory", directory && !isVirtualUri(directory) ? directory : undefined);
};
element.addEventListener("focus", publish);
element.addEventListener("blur", clear);
publish();
return () => {
element.removeEventListener("focus", publish);
element.removeEventListener("blur", clear);
clear();
};
}, [ref, directory, enabled]);
}