diff --git a/README.md b/README.md index 85fc7464..779989fd 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ module.exports = { | searchResultContextMaxLength | number | `50` | Set the max length of characters of each search result to show. | | explicitSearchResultPath | boolean | false | Whether an explicit path to a heading should be presented on a suggestion template. | | ignoreFiles | string \| RegExp \| (string \| RegExp)[] | /**meta**\$/ | Set the match rules to ignore some files. | +| searchBarShortcut | boolean | `true` | Whether to enable shortcut for focusing in search bar. | ### I18N diff --git a/docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx b/docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx index 4d518937..31def95e 100644 --- a/docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx +++ b/docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx @@ -18,7 +18,7 @@ import { SearchSourceFactory } from "../../utils/SearchSourceFactory"; import { SuggestionTemplate } from "./SuggestionTemplate"; import { EmptyTemplate } from "./EmptyTemplate"; import { SearchResult } from "../../../shared/interfaces"; -import { searchResultLimits, Mark } from "../../utils/proxiedGenerated"; +import { searchResultLimits, Mark, searchBarShortcut } from "../../utils/proxiedGenerated"; import LoadingRing from "../LoadingRing/LoadingRing"; import styles from "./SearchBar.module.css"; @@ -237,14 +237,17 @@ export default function SearchBar({ : false; useEffect(() => { + if (!searchBarShortcut) { + return; + } // Add shortcuts command/ctrl + K - function handleShortcut(event: KeyboardEvent): void { + const handleShortcut = (event: KeyboardEvent): void => { if ((isMac ? event.metaKey : event.ctrlKey) && event.code === "KeyK") { event.preventDefault(); searchBarRef.current?.focus(); onInputFocus(); } - } + }; document.addEventListener("keydown", handleShortcut); return () => { @@ -291,7 +294,7 @@ export default function SearchBar({ value={inputValue} /> - {inputValue !== "" ? ( + {searchBarShortcut && (inputValue !== "" ? ( @@ -300,7 +303,7 @@ export default function SearchBar({ {isMac ? "⌘" : "ctrl"} K - )} + ))} ); } diff --git a/docusaurus-search-local/src/declarations.ts b/docusaurus-search-local/src/declarations.ts index b4493f60..90ec9d55 100644 --- a/docusaurus-search-local/src/declarations.ts +++ b/docusaurus-search-local/src/declarations.ts @@ -15,6 +15,7 @@ declare module "*/generated.js" { export const searchResultLimits: number; export const searchResultContextMaxLength: number; export const explicitSearchResultPath: boolean; + export const searchBarShortcut: boolean; // These below are for mocking only. export const __setLanguage: (value: string[]) => void; export const __setRemoveDefaultStopWordFilter: (value: boolean) => void; diff --git a/docusaurus-search-local/src/server/utils/generate.spec.ts b/docusaurus-search-local/src/server/utils/generate.spec.ts index 710a4fad..895830ed 100644 --- a/docusaurus-search-local/src/server/utils/generate.spec.ts +++ b/docusaurus-search-local/src/server/utils/generate.spec.ts @@ -27,6 +27,7 @@ describe("generate", () => { "export const searchResultLimits = 8;", "export const searchResultContextMaxLength = 50;", "export const explicitSearchResultPath = false;", + "export const searchBarShortcut = true;", ], ], [ @@ -45,6 +46,7 @@ describe("generate", () => { "export const searchResultLimits = 8;", "export const searchResultContextMaxLength = 50;", "export const explicitSearchResultPath = false;", + "export const searchBarShortcut = true;", ], ], [ @@ -65,6 +67,7 @@ describe("generate", () => { "export const searchResultLimits = 8;", "export const searchResultContextMaxLength = 50;", "export const explicitSearchResultPath = false;", + "export const searchBarShortcut = true;", ], ], [ @@ -88,6 +91,7 @@ describe("generate", () => { "export const searchResultLimits = 8;", "export const searchResultContextMaxLength = 50;", "export const explicitSearchResultPath = false;", + "export const searchBarShortcut = true;", ], ], [ @@ -109,6 +113,7 @@ describe("generate", () => { "export const searchResultLimits = 8;", "export const searchResultContextMaxLength = 50;", "export const explicitSearchResultPath = false;", + "export const searchBarShortcut = true;", ], ], [ @@ -133,6 +138,7 @@ describe("generate", () => { "export const searchResultLimits = 8;", "export const searchResultContextMaxLength = 50;", "export const explicitSearchResultPath = false;", + "export const searchBarShortcut = true;", ], ], ])("generate({ language: %j }, dir) should work", (language, contents) => { @@ -144,6 +150,7 @@ describe("generate", () => { searchResultLimits: 8, searchResultContextMaxLength: 50, explicitSearchResultPath: false, + searchBarShortcut: true, } as ProcessedPluginOptions, "/tmp" ); @@ -176,4 +183,22 @@ describe("generate", () => { expect.stringContaining("export { default as Mark } from") ); }); + + test("searchBarShortcut", () => { + generate( + { + language: ["en"], + removeDefaultStopWordFilter: false, + searchBarShortcut: false, + searchResultLimits: 8, + searchResultContextMaxLength: 50, + } as ProcessedPluginOptions, + "/tmp" + ); + + expect(mockWriteFileSync).toBeCalledWith( + "/tmp/generated.js", + expect.stringContaining("export const searchBarShortcut = false") + ); + }); }); diff --git a/docusaurus-search-local/src/server/utils/generate.ts b/docusaurus-search-local/src/server/utils/generate.ts index d8992182..4113c6f9 100644 --- a/docusaurus-search-local/src/server/utils/generate.ts +++ b/docusaurus-search-local/src/server/utils/generate.ts @@ -12,6 +12,7 @@ export function generate(config: ProcessedPluginOptions, dir: string): void { searchResultLimits, searchResultContextMaxLength, explicitSearchResultPath, + searchBarShortcut, } = config; const indexHash = getIndexHash(config); const contents: string[] = [ @@ -84,6 +85,9 @@ export function generate(config: ProcessedPluginOptions, dir: string): void { explicitSearchResultPath )};` ); + contents.push( + `export const searchBarShortcut = ${JSON.stringify(searchBarShortcut)};` + ); fs.writeFileSync(path.join(dir, "generated.js"), contents.join("\n")); } diff --git a/docusaurus-search-local/src/server/utils/validateOptions.spec.ts b/docusaurus-search-local/src/server/utils/validateOptions.spec.ts index e9b94c0d..ba724e2a 100644 --- a/docusaurus-search-local/src/server/utils/validateOptions.spec.ts +++ b/docusaurus-search-local/src/server/utils/validateOptions.spec.ts @@ -36,6 +36,7 @@ describe("validateOptions", () => { explicitSearchResultPath: false, searchResultContextMaxLength: 50, ignoreFiles: [], + searchBarShortcut: true, }, ], [ @@ -57,6 +58,7 @@ describe("validateOptions", () => { explicitSearchResultPath: false, searchResultContextMaxLength: 50, ignoreFiles: "file1", + searchBarShortcut: true, }, ], [ @@ -78,6 +80,7 @@ describe("validateOptions", () => { explicitSearchResultPath: false, searchResultContextMaxLength: 50, ignoreFiles: [/__meta__$/, "file1"], + searchBarShortcut: true, }, ], [ @@ -99,6 +102,7 @@ describe("validateOptions", () => { explicitSearchResultPath: false, searchResultContextMaxLength: 50, ignoreFiles: [], + searchBarShortcut: true, }, ], [ @@ -110,6 +114,7 @@ describe("validateOptions", () => { searchResultLimits: 5, explicitSearchResultPath: false, searchResultContextMaxLength: 30, + searchBarShortcut: false, }, { blogRouteBasePath: ["blog"], @@ -128,6 +133,7 @@ describe("validateOptions", () => { explicitSearchResultPath: false, searchResultContextMaxLength: 30, ignoreFiles: [], + searchBarShortcut: false, }, ], [ @@ -152,6 +158,7 @@ describe("validateOptions", () => { explicitSearchResultPath: false, searchResultContextMaxLength: 50, ignoreFiles: [], + searchBarShortcut: true, }, ], [ @@ -176,6 +183,7 @@ describe("validateOptions", () => { explicitSearchResultPath: false, searchResultContextMaxLength: 50, ignoreFiles: [], + searchBarShortcut: true, }, ], ])("validateOptions(...) should work", (options, config) => { diff --git a/docusaurus-search-local/src/server/utils/validateOptions.ts b/docusaurus-search-local/src/server/utils/validateOptions.ts index 2264b646..dd5bb247 100644 --- a/docusaurus-search-local/src/server/utils/validateOptions.ts +++ b/docusaurus-search-local/src/server/utils/validateOptions.ts @@ -34,6 +34,7 @@ const schema = Joi.object({ searchResultContextMaxLength: Joi.number().default(50), explicitSearchResultPath: Joi.boolean().default(false), ignoreFiles: isArrayOfStringsOrRegExpsOrStringOrRegExp.default([]), + searchBarShortcut: Joi.boolean().default(true), }); export function validateOptions({ diff --git a/docusaurus-search-local/src/shared/interfaces.ts b/docusaurus-search-local/src/shared/interfaces.ts index 934e8d5b..49d0290d 100644 --- a/docusaurus-search-local/src/shared/interfaces.ts +++ b/docusaurus-search-local/src/shared/interfaces.ts @@ -156,6 +156,8 @@ export interface PluginOptions { ignoreFiles?: string | RegExp | (string | RegExp)[]; + searchBarShortcut?: boolean; + // searchInputPlaceholder?: string; // searchNoResults?: string; // searchSeeAllResults?: string;