From af62b241fd7cc02ad1151feaed02d6eecea3f37b Mon Sep 17 00:00:00 2001 From: Hossein Ahmadian-Yazdi Date: Sat, 13 Mar 2021 00:48:52 -0500 Subject: [PATCH 1/4] local doc search --- .../src/components/layout/TopNav.tsx | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/packages/typescriptlang-org/src/components/layout/TopNav.tsx b/packages/typescriptlang-org/src/components/layout/TopNav.tsx index ffe2085b26dc..2ac5ed073e34 100644 --- a/packages/typescriptlang-org/src/components/layout/TopNav.tsx +++ b/packages/typescriptlang-org/src/components/layout/TopNav.tsx @@ -17,22 +17,30 @@ import { OpenInMyLangQuickJump } from "./LanguageRecommendation"; export const SiteNav = (props: Props) => { const i = createInternational(useIntl()) const IntlLink = createIntlLink(props.lang) - + const loadDocSearch = () => { + // @ts-ignore - this comes from the script above + docsearch({ + apiKey: '3c2db2aef0c7ff26e8911267474a9b2c', + indexName: 'typescriptlang', + inputSelector: '.search input', + handleSelected: function (input, event, suggestion, datasetNumber, context) { + let urlToOpen = suggestion.url; + if (window.location.href.includes("localhost:8000")) { + urlToOpen = suggestion.url.replace("www.typescriptlang.org", "localhost:8000").replace("https", "http") + } + window.open(urlToOpen); + }, + }); + } // This extra bit of mis-direction ensures that non-essential code runs after // the page is loaded useEffect(() => { setupStickyNavigation() - // @ts-ignore - this could come from a previous page load - if (typeof docsearch !== 'undefined') { - // @ts-ignore - just been validate - docsearch({ - apiKey: '3c2db2aef0c7ff26e8911267474a9b2c', - indexName: 'typescriptlang', - inputSelector: '.search input', - }); + // @ts-ignore - this comes from the script above + if (window.docsearch) { + loadDocSearch(); } - if (document.getElementById("algolia-search")) return const searchScript = document.createElement('script'); @@ -43,13 +51,8 @@ export const SiteNav = (props: Props) => { searchScript.async = true; searchScript.onload = () => { // @ts-ignore - this comes from the script above - if (typeof docsearch !== 'undefined') { - // @ts-ignore - this comes from the script above - docsearch({ - apiKey: '3c2db2aef0c7ff26e8911267474a9b2c', - indexName: 'typescriptlang', - inputSelector: '.search input' - }); + if (window.docsearch) { + loadDocSearch(); searchCSS.rel = 'stylesheet'; searchCSS.href = withPrefix('/css/docsearch.css'); From 424d0622e1018a4e3119013562619951d6de7636 Mon Sep 17 00:00:00 2001 From: Hossein Ahmadian-Yazdi Date: Sat, 13 Mar 2021 00:56:08 -0500 Subject: [PATCH 2/4] es6 --- packages/typescriptlang-org/src/components/layout/TopNav.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescriptlang-org/src/components/layout/TopNav.tsx b/packages/typescriptlang-org/src/components/layout/TopNav.tsx index 2ac5ed073e34..a4959682ebbd 100644 --- a/packages/typescriptlang-org/src/components/layout/TopNav.tsx +++ b/packages/typescriptlang-org/src/components/layout/TopNav.tsx @@ -23,7 +23,7 @@ export const SiteNav = (props: Props) => { apiKey: '3c2db2aef0c7ff26e8911267474a9b2c', indexName: 'typescriptlang', inputSelector: '.search input', - handleSelected: function (input, event, suggestion, datasetNumber, context) { + handleSelected: (input, event, suggestion, datasetNumber, context) => { let urlToOpen = suggestion.url; if (window.location.href.includes("localhost:8000")) { urlToOpen = suggestion.url.replace("www.typescriptlang.org", "localhost:8000").replace("https", "http") From 7378ac3da90679ae30cf1f505ef8dc4bd99d43af Mon Sep 17 00:00:00 2001 From: Hossein Ahmadian-Yazdi Date: Mon, 22 Mar 2021 23:14:52 -0400 Subject: [PATCH 3/4] only run in dev --- .../src/components/layout/TopNav.tsx | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/typescriptlang-org/src/components/layout/TopNav.tsx b/packages/typescriptlang-org/src/components/layout/TopNav.tsx index a4959682ebbd..b48dd1871ad6 100644 --- a/packages/typescriptlang-org/src/components/layout/TopNav.tsx +++ b/packages/typescriptlang-org/src/components/layout/TopNav.tsx @@ -18,18 +18,23 @@ export const SiteNav = (props: Props) => { const i = createInternational(useIntl()) const IntlLink = createIntlLink(props.lang) const loadDocSearch = () => { + const isDev = document.location.host.includes('localhost') + let customHandleSelected; + + if (isDev) { + customHandleSelected = (input, event, suggestion, datasetNumber, context) => { + const urlToOpen = suggestion.url.replace("www.typescriptlang.org", "localhost:8000").replace("https", "http") + window.open(urlToOpen); + } + } + + // @ts-ignore - this comes from the script above docsearch({ apiKey: '3c2db2aef0c7ff26e8911267474a9b2c', indexName: 'typescriptlang', inputSelector: '.search input', - handleSelected: (input, event, suggestion, datasetNumber, context) => { - let urlToOpen = suggestion.url; - if (window.location.href.includes("localhost:8000")) { - urlToOpen = suggestion.url.replace("www.typescriptlang.org", "localhost:8000").replace("https", "http") - } - window.open(urlToOpen); - }, + handleSelected: customHandleSelected, }); } // This extra bit of mis-direction ensures that non-essential code runs after From b511dd7cd2c6f5ad1a001c036e042f4cc99392f2 Mon Sep 17 00:00:00 2001 From: Hossein Ahmadian-Yazdi Date: Mon, 22 Mar 2021 23:15:18 -0400 Subject: [PATCH 4/4] remove semicolon --- packages/typescriptlang-org/src/components/layout/TopNav.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescriptlang-org/src/components/layout/TopNav.tsx b/packages/typescriptlang-org/src/components/layout/TopNav.tsx index b48dd1871ad6..d783b9f19012 100644 --- a/packages/typescriptlang-org/src/components/layout/TopNav.tsx +++ b/packages/typescriptlang-org/src/components/layout/TopNav.tsx @@ -24,7 +24,7 @@ export const SiteNav = (props: Props) => { if (isDev) { customHandleSelected = (input, event, suggestion, datasetNumber, context) => { const urlToOpen = suggestion.url.replace("www.typescriptlang.org", "localhost:8000").replace("https", "http") - window.open(urlToOpen); + window.open(urlToOpen) } }