一个全局快捷键的hook

2023-09-23 晚上前端 28 次浏览暂无评论
import Mousetrap from "mousetrap";

export function useKeyboard() {
  const [, setVisible] = useRecoilState(GlobalUserOpenState);
  const [, setOpen] = useRecoilState(GlobalDocSerachOpen);
  const [, setGlobalDocSearchOptions] = useRecoilState(GlobalDocSearchOptions);
  const [, setGlobalDocSerachCustomColumns] = useRecoilState(GlobalDocSerachCustomColumns);
  const [, setOpenSearch] = useRecoilState(GlobalSearchOpenState); // 全局搜索

  // 监听快捷键
  useEffect(() => {
    // 全局用户搜索
    Mousetrap.bind("u", function () {
      setVisible((value: boolean) => !value);
    });

    // 全局文档搜索
    Mousetrap.bind("s", function () {
      setOpen((value: boolean) => !value);
      setGlobalDocSerachCustomColumns(() => []);
      setGlobalDocSearchOptions(() => undefined);
    });

    // 大搜
    Mousetrap.bind(["command+k", "ctrl+k"], function () {
      setOpenSearch(true);
    });
  });
}


目录

ICP备案号:鲁ICP备2020040322号