mirror of
https://github.com/infinilabs/coco-app.git
synced 2026-09-01 19:51:52 +02:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import platformAdapter from "@/utils/platformAdapter";
|
|
import { useSearchStore } from "@/stores/searchStore";
|
|
import { useKeyPress } from "ahooks";
|
|
import { HISTORY_PANEL_ID } from "@/constants";
|
|
|
|
const useEscape = () => {
|
|
const visibleContextMenu = useSearchStore((state) => {
|
|
return state.visibleContextMenu;
|
|
});
|
|
const setVisibleContextMenu = useSearchStore((state) => {
|
|
return state.setVisibleContextMenu;
|
|
});
|
|
|
|
useKeyPress("esc", (event) => {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
|
|
if (
|
|
document.activeElement instanceof HTMLInputElement ||
|
|
document.activeElement instanceof HTMLTextAreaElement
|
|
) {
|
|
return document.activeElement.blur();
|
|
}
|
|
|
|
if (visibleContextMenu) {
|
|
return setVisibleContextMenu(false);
|
|
}
|
|
|
|
const historyPanel = document.getElementById(HISTORY_PANEL_ID);
|
|
|
|
if (historyPanel) {
|
|
const button = document.querySelector(
|
|
`[aria-controls="${HISTORY_PANEL_ID}"]`
|
|
);
|
|
|
|
return (button as HTMLElement).click();
|
|
}
|
|
|
|
platformAdapter.hideWindow();
|
|
});
|
|
};
|
|
|
|
export default useEscape;
|