From 735eee7d596ecbb747521935b12a23d4bbe667db Mon Sep 17 00:00:00 2001 From: Hetarth Shah <80420115+Hetarth02@users.noreply.github.com> Date: Mon, 13 Feb 2023 00:14:36 +0530 Subject: [PATCH] Add keyboard shortcut for search box, close #1536 (#2027) --- .gitignore | 2 ++ CHANGELOG.md | 4 +++- assets/html/js/shortcut.js | 18 ++++++++++++++++++ src/html/HTMLWriter.jl | 2 +- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 assets/html/js/shortcut.js diff --git a/.gitignore b/.gitignore index 6eea0ebc6f..653ccabec7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.vscode + *.jl.cov *.jl.*.cov *.jl.mem diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e3de6e299..52253e7f2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * The `target` keyword of `deploydocs` is now required to point to a subdirectory of `root` (usually the directory where `make.jl` is located). (#2019) +* Added keyboard shortcuts for search box (`Ctrl + /` or `Cmd + /` to focus into the search box, `Esc` to focus out of it). (#1536), (#2027) + ### Fixed * Documenter now generates the correct source URLs for docstrings from other packages when the `repo` argument to `makedocs` is set (note: the source links to such docstrings only work if the external package is cloned from GitHub and added as a dev-dependency). However, this change **breaks** the case where the `repo` argument is used to override the main package/repository URL, assuming the repository is cloned from GitHub. (#1808) @@ -1076,4 +1078,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * The at-blocks that execute code can now handle `include` statements. (#793), (#794) -* At-docs blocks no longer give an error when containing empty lines. (#823), (#824) +* At-docs blocks no longer give an error when containing empty lines. (#823), (#824) \ No newline at end of file diff --git a/assets/html/js/shortcut.js b/assets/html/js/shortcut.js new file mode 100644 index 0000000000..ea7b6feb74 --- /dev/null +++ b/assets/html/js/shortcut.js @@ -0,0 +1,18 @@ +let searchbox = document.querySelector("#documenter-search-query"); +let sidebar = document.querySelector(".docs-sidebar"); + +document.addEventListener("keydown", (event) => { + if ((event.ctrlKey || event.metaKey) && event.key === "/") { + if (!sidebar.classList.contains("visible")) { + sidebar.classList.add("visible"); + } + searchbox.focus(); + return false; + } else if (event.key === "Escape") { + if (sidebar.classList.contains("visible")) { + sidebar.classList.remove("visible"); + } + searchbox.blur(); + return false; + } +}); diff --git a/src/html/HTMLWriter.jl b/src/html/HTMLWriter.jl index 905c0d7381..adffa020f2 100644 --- a/src/html/HTMLWriter.jl +++ b/src/html/HTMLWriter.jl @@ -1058,7 +1058,7 @@ function render_sidebar(ctx, navnode) "#documenter-search-query.docs-search-query", :name => "q", :type => "text", - :placeholder => "Search docs", + :placeholder => "Search docs (Ctrl + /)", ], ) )