Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] 建议加入搜索快捷键绑定 #233

Open
lyj0309 opened this issue Oct 26, 2022 · 3 comments
Open

[FEATURE] 建议加入搜索快捷键绑定 #233

lyj0309 opened this issue Oct 26, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@lyj0309
Copy link

lyj0309 commented Oct 26, 2022

Describe the feature you want 描述你的功能需求

建议加入搜索快捷键绑定,如github,按 "/" 或者vuepress 按 “ctrl+k” 就能弹出搜索框

Useful reference 有价值的参考

https://v2.vuepress.vuejs.org/zh/reference/plugin/search.html#hotkeys

@lyj0309 lyj0309 added the enhancement New feature or request label Oct 26, 2022
@0x020B
Copy link
Contributor

0x020B commented Jan 10, 2023

该issue可扩展为自定义全局快捷键(如vim键位支持

@0x5c0f
Copy link

0x5c0f commented Dec 30, 2024

需求+1, 目前自实现(代码通过AI生成)

class KeybindCustom {
  static initKeyBind() {
    const SEARCH_TOGGLE_ID = "search-toggle-desktop";
    const SEARCH_INPUT_ID = "search-input-desktop";
    let isSearchActive = false;

    const handleKeyDown = (event) => {
      const searchToggle = document.getElementById(SEARCH_TOGGLE_ID);
      const searchInput = document.getElementById(SEARCH_INPUT_ID);

      if (event.key === "/") {
        if (document.activeElement !== searchInput 
          && document.activeElement.tagName !== 'TEXTAREA'
          && document.activeElement.tagName !== 'INPUT'
          && document.activeElement.tagName !== 'PRE'
        ) {
          event.preventDefault();

          if (searchToggle) {
            searchToggle.click();
            isSearchActive = true;
            searchInput.focus();
          }
        }
      } else if (event.key === "Escape" && isSearchActive) {
        const mask = document.getElementById("mask");
        if (mask) {
          mask.click();
          isSearchActive = false;
          searchInput.blur();
        }
      }
    };

    document.addEventListener("keydown", handleKeyDown);

    const searchInput = document.getElementById(SEARCH_INPUT_ID);
    if (searchInput) {
      const handleFocus = () => isSearchActive = true;
      const handleBlur = () => isSearchActive = false;

      searchInput.addEventListener("focus", handleFocus);
      searchInput.addEventListener("blur", handleBlur);
    }
  }
}
(() => {
  if (!window.ExtUtils.isMobile()) {
    KeybindCustom.initKeyBind();
  }
})();

@Lruihao
Copy link
Member

Lruihao commented Dec 30, 2024

这个需求迟迟没有动,是因为我想整体重构搜索框,像vuepress等那样。

@Lruihao Lruihao added the help wanted Extra attention is needed label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants