mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-11-02 08:35:24 +00:00
fix(file-manager): restore Ctrl/Cmd+F file search (#1472)
* fix(ci): build plugin SDK before dependent checks * fix(mysql): keep plugin settings scope indexes within limits * test(mysql): refresh reviewed plugin upgrade SQL * fix(file-manager): focus file search with Ctrl or Cmd F
This commit is contained in:
1 parent
5e02e30d0f
commit
2770f14a25
2 files changed
+139
No files matched your search
@@ -232,6 +232,43 @@ export function FileManagerToolbar({
|
|||||||
sidebarOpen = true,
|
sidebarOpen = true,
|
||||||
onToggleSidebar,
|
onToggleSidebar,
|
||||||
}: FileManagerToolbarProps) {
|
}: FileManagerToolbarProps) {
|
||||||
|
const searchInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleSearchShortcut = (event: KeyboardEvent) => {
|
||||||
|
if (
|
||||||
|
event.defaultPrevented ||
|
||||||
|
!(event.ctrlKey || event.metaKey) ||
|
||||||
|
event.altKey ||
|
||||||
|
event.shiftKey ||
|
||||||
|
event.key.toLowerCase() !== "f"
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const input = searchInputRef.current;
|
||||||
|
if (!input) return;
|
||||||
|
const visible =
|
||||||
|
typeof input.checkVisibility === "function"
|
||||||
|
? input.checkVisibility({ visibilityProperty: true })
|
||||||
|
: input.offsetParent !== null;
|
||||||
|
if (!visible) return;
|
||||||
|
const active = document.activeElement;
|
||||||
|
if (
|
||||||
|
active !== input &&
|
||||||
|
active?.closest(
|
||||||
|
'input, textarea, [contenteditable="true"], [role="dialog"]',
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
input.focus();
|
||||||
|
input.select();
|
||||||
|
};
|
||||||
|
document.addEventListener("keydown", handleSearchShortcut);
|
||||||
|
return () => document.removeEventListener("keydown", handleSearchShortcut);
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col shrink-0 mx-3 mt-3 border border-border bg-card">
|
<div className="flex flex-col shrink-0 mx-3 mt-3 border border-border bg-card">
|
||||||
<div className="flex flex-row items-center justify-between px-3 py-2 gap-2">
|
<div className="flex flex-row items-center justify-between px-3 py-2 gap-2">
|
||||||
@@ -325,6 +362,7 @@ export function FileManagerToolbar({
|
|||||||
<div className="relative w-28 md:w-48">
|
<div className="relative w-28 md:w-48">
|
||||||
<Search className="absolute left-2.5 top-1/2 -translate-y-1/2 size-3.5 text-muted-foreground" />
|
<Search className="absolute left-2.5 top-1/2 -translate-y-1/2 size-3.5 text-muted-foreground" />
|
||||||
<Input
|
<Input
|
||||||
|
ref={searchInputRef}
|
||||||
placeholder={t("fileManager.searchFiles")}
|
placeholder={t("fileManager.searchFiles")}
|
||||||
value={searchQuery}
|
value={searchQuery}
|
||||||
onChange={(e) => setSearchQuery(e.target.value)}
|
onChange={(e) => setSearchQuery(e.target.value)}
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||||
|
import "@testing-library/jest-dom/vitest";
|
||||||
|
import { FileManagerToolbar } from "../../src/frontend/FileManagerToolbar";
|
||||||
|
|
||||||
|
const props: React.ComponentProps<typeof FileManagerToolbar> = {
|
||||||
|
t: (key) => key,
|
||||||
|
currentPath: "/home/alice",
|
||||||
|
navIndex: 0,
|
||||||
|
navHistoryLength: 1,
|
||||||
|
isLoading: false,
|
||||||
|
sshSessionId: "session",
|
||||||
|
selectedFiles: [],
|
||||||
|
searchQuery: "report",
|
||||||
|
setSearchQuery: vi.fn(),
|
||||||
|
viewMode: "list",
|
||||||
|
setViewMode: vi.fn(),
|
||||||
|
density: "comfortable",
|
||||||
|
setDensity: vi.fn(),
|
||||||
|
sortBy: "name",
|
||||||
|
setSortBy: vi.fn(),
|
||||||
|
sortOrder: "asc",
|
||||||
|
setSortOrder: vi.fn(),
|
||||||
|
setMobileSidebarOpen: vi.fn(),
|
||||||
|
goBack: vi.fn(),
|
||||||
|
goForward: vi.fn(),
|
||||||
|
goUp: vi.fn(),
|
||||||
|
navigateTo: vi.fn(),
|
||||||
|
handleRefreshDirectory: vi.fn(),
|
||||||
|
handleDeleteFiles: vi.fn(),
|
||||||
|
handleCopyFiles: vi.fn(),
|
||||||
|
handleFilesDropped: vi.fn(),
|
||||||
|
handleCreateNewFolder: vi.fn(),
|
||||||
|
handleCreateNewFile: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
function toolbar(visible = true) {
|
||||||
|
const view = render(<FileManagerToolbar {...props} />);
|
||||||
|
const input = view.getByPlaceholderText(
|
||||||
|
"fileManager.searchFiles",
|
||||||
|
) as HTMLInputElement;
|
||||||
|
Object.defineProperty(input, "checkVisibility", { value: () => visible });
|
||||||
|
return { ...view, input };
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
describe("file search shortcut", () => {
|
||||||
|
it.each([{ ctrlKey: true }, { metaKey: true }])(
|
||||||
|
"focuses and selects the search query with %j",
|
||||||
|
(modifier) => {
|
||||||
|
const { input } = toolbar();
|
||||||
|
expect(fireEvent.keyDown(document, { key: "f", ...modifier })).toBe(
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
expect(input).toHaveFocus();
|
||||||
|
expect([input.selectionStart, input.selectionEnd]).toEqual([0, 6]);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it("does not handle the shortcut in a hidden tab", () => {
|
||||||
|
const { input } = toolbar(false);
|
||||||
|
expect(fireEvent.keyDown(document, { key: "f", ctrlKey: true })).toBe(true);
|
||||||
|
expect(input).not.toHaveFocus();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves editor and dialog shortcuts alone", () => {
|
||||||
|
const { input } = toolbar();
|
||||||
|
render(
|
||||||
|
<>
|
||||||
|
<textarea aria-label="editor" />
|
||||||
|
<div role="dialog">
|
||||||
|
<button>Dialog action</button>
|
||||||
|
</div>
|
||||||
|
</>,
|
||||||
|
);
|
||||||
|
for (const target of [
|
||||||
|
screen.getByLabelText("editor"),
|
||||||
|
screen.getByText("Dialog action"),
|
||||||
|
]) {
|
||||||
|
target.focus();
|
||||||
|
expect(fireEvent.keyDown(target, { key: "f", ctrlKey: true })).toBe(true);
|
||||||
|
expect(target).toHaveFocus();
|
||||||
|
expect(input).not.toHaveFocus();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not intercept unmodified or already handled keys", () => {
|
||||||
|
const { input } = toolbar();
|
||||||
|
fireEvent.keyDown(document, { key: "f" });
|
||||||
|
const event = new KeyboardEvent("keydown", {
|
||||||
|
key: "f",
|
||||||
|
ctrlKey: true,
|
||||||
|
cancelable: true,
|
||||||
|
});
|
||||||
|
event.preventDefault();
|
||||||
|
document.dispatchEvent(event);
|
||||||
|
expect(input).not.toHaveFocus();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user