@rspress/core 0.0.13 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +90 -50
- package/dist/index.js.map +1 -1
- package/dist/loader.d.ts +3 -0
- package/dist/loader.js +28 -72
- package/dist/loader.js.map +1 -1
- package/dist/runtime/App.d.ts +8 -0
- package/dist/runtime/App.js +1 -0
- package/dist/runtime/Content.d.ts +6 -0
- package/dist/runtime/Content.js +7 -2
- package/dist/runtime/NoSSR.d.ts +4 -0
- package/dist/runtime/clientEntry.d.ts +1 -0
- package/dist/runtime/hooks.d.ts +29 -0
- package/dist/runtime/hooks.js +23 -1
- package/dist/runtime/index.d.ts +6 -0
- package/dist/runtime/ssrEntry.d.ts +6 -0
- package/dist/runtime/tsconfig.json +9 -0
- package/dist/runtime/utils.d.ts +6 -0
- package/dist/theme/bundle.css +94 -53
- package/dist/theme/bundle.d.ts +12 -1
- package/dist/theme/bundle.js +527 -489
- package/dist/theme/bundle.js.map +1 -1
- package/package.json +14 -14
- package/src/runtime/App.tsx +1 -0
- package/src/runtime/Content.tsx +11 -2
- package/src/runtime/global.d.ts +49 -0
- package/src/runtime/hooks.ts +47 -1
- package/src/runtime/tsconfig.json +9 -0
- package/src/theme-default/components/LocalSideBar/{index.module.scss → index.scss} +6 -5
- package/src/theme-default/components/LocalSideBar/index.tsx +4 -4
- package/src/theme-default/components/Nav/index.tsx +19 -5
- package/src/theme-default/components/Search/SearchPanel.tsx +339 -0
- package/src/theme-default/components/Search/index.tsx +6 -326
- package/src/theme-default/index.ts +3 -1
- package/src/theme-default/layout/DocLayout/docComponents/link.tsx +8 -23
- package/src/theme-default/layout/DocLayout/index.module.scss +52 -3
- package/src/theme-default/layout/DocLayout/index.tsx +9 -7
- package/src/theme-default/logic/index.ts +1 -0
- package/src/theme-default/logic/useAppearance.ts +0 -1
- package/src/theme-default/logic/usePathUtils.ts +72 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rspress/core",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The Rspress Documentation Framework",
|
|
5
5
|
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
|
6
6
|
"repository": {
|
|
@@ -40,10 +40,10 @@
|
|
|
40
40
|
"@mdx-js/loader": "2.2.1",
|
|
41
41
|
"@mdx-js/mdx": "2.2.1",
|
|
42
42
|
"@mdx-js/react": "2.2.1",
|
|
43
|
-
"@modern-js/builder": "2.35.
|
|
44
|
-
"@modern-js/builder-rspack-provider": "2.35.
|
|
45
|
-
"@
|
|
46
|
-
"@modern-js/utils": "2.35.
|
|
43
|
+
"@modern-js/builder": "2.35.1",
|
|
44
|
+
"@modern-js/builder-rspack-provider": "2.35.1",
|
|
45
|
+
"@rspress/mdx-rs": "0.3.2",
|
|
46
|
+
"@modern-js/utils": "2.35.1",
|
|
47
47
|
"@types/compression": "^1.7.2",
|
|
48
48
|
"@types/polka": "^0.5.4",
|
|
49
49
|
"autoprefixer": "10.4.13",
|
|
@@ -89,20 +89,20 @@
|
|
|
89
89
|
"unist-util-visit-children": "^2.0.1",
|
|
90
90
|
"yaml-front-matter": "^4.1.1",
|
|
91
91
|
"rspack-plugin-virtual-module": "0.1.12",
|
|
92
|
-
"@rspress/shared": "0.0
|
|
93
|
-
"@rspress/plugin-medium-zoom": "0.0
|
|
94
|
-
"@rspress/plugin-last-updated": "0.0
|
|
95
|
-
"@rspress/plugin-auto-nav-sidebar": "0.0
|
|
96
|
-
"@rspress/plugin-container-syntax": "0.0
|
|
92
|
+
"@rspress/shared": "0.2.0",
|
|
93
|
+
"@rspress/plugin-medium-zoom": "0.2.0",
|
|
94
|
+
"@rspress/plugin-last-updated": "0.2.0",
|
|
95
|
+
"@rspress/plugin-auto-nav-sidebar": "0.2.0",
|
|
96
|
+
"@rspress/plugin-container-syntax": "0.2.0"
|
|
97
97
|
},
|
|
98
98
|
"peerDependencies": {
|
|
99
99
|
"react": ">=17"
|
|
100
100
|
},
|
|
101
101
|
"devDependencies": {
|
|
102
|
-
"@modern-js/plugin-tailwindcss": "2.35.
|
|
103
|
-
"@modern-js/plugin-testing": "2.35.
|
|
104
|
-
"@modern-js/tsconfig": "2.35.
|
|
105
|
-
"@modern-js/types": "2.35.
|
|
102
|
+
"@modern-js/plugin-tailwindcss": "2.35.1",
|
|
103
|
+
"@modern-js/plugin-testing": "2.35.1",
|
|
104
|
+
"@modern-js/tsconfig": "2.35.1",
|
|
105
|
+
"@modern-js/types": "2.35.1",
|
|
106
106
|
"@types/body-scroll-lock": "^3.1.0",
|
|
107
107
|
"@types/fs-extra": "^9.0.13",
|
|
108
108
|
"@types/hast": "^2.3.4",
|
package/src/runtime/App.tsx
CHANGED
package/src/runtime/Content.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ReactNode, Suspense } from 'react';
|
|
2
2
|
import { matchRoutes, useLocation } from 'react-router-dom';
|
|
3
3
|
import { normalizeRoutePath } from './utils';
|
|
4
|
+
import { usePageData, useViewTransition } from './hooks';
|
|
4
5
|
|
|
5
6
|
const { routes } = process.env.__SSR__
|
|
6
7
|
? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
|
|
@@ -13,9 +14,17 @@ export const Content = ({ fallback = <></> }: { fallback?: ReactNode }) => {
|
|
|
13
14
|
return <div></div>;
|
|
14
15
|
}
|
|
15
16
|
const routesElement = matched[0].route.element;
|
|
17
|
+
/**
|
|
18
|
+
* useLayoutEffect to flush elements animation
|
|
19
|
+
*/
|
|
20
|
+
let element = routesElement;
|
|
21
|
+
if (usePageData().siteData.themeConfig.enableContentAnimation) {
|
|
22
|
+
element = useViewTransition(routesElement);
|
|
23
|
+
}
|
|
24
|
+
|
|
16
25
|
// React 17 Suspense SSR is not supported
|
|
17
26
|
if (!process.env.__IS_REACT_18__ && process.env.__SSR__) {
|
|
18
|
-
return
|
|
27
|
+
return element;
|
|
19
28
|
}
|
|
20
|
-
return <Suspense fallback={fallback}>{
|
|
29
|
+
return <Suspense fallback={fallback}>{element}</Suspense>;
|
|
21
30
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
declare module 'virtual-routes' {
|
|
2
|
+
export { Route } from 'node/route/RouteService';
|
|
3
|
+
|
|
4
|
+
export const routes: Route[];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare module 'virtual-routes-ssr' {
|
|
8
|
+
export { Route } from 'node/route/RouteService';
|
|
9
|
+
|
|
10
|
+
export const routes: Route[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare module 'virtual-site-data' {
|
|
14
|
+
import { SiteData } from 'shared/types';
|
|
15
|
+
import ThemeConfig from 'shared/types/defaultTheme';
|
|
16
|
+
|
|
17
|
+
const data: SiteData<ThemeConfig>;
|
|
18
|
+
export default data;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare module 'virtual-search-index-hash' {
|
|
22
|
+
const hash: string;
|
|
23
|
+
export default hash;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
declare module 'virtual-global-components' {
|
|
27
|
+
import { ComponentType } from 'react';
|
|
28
|
+
|
|
29
|
+
const components: (ComponentType | [ComponentType, any])[];
|
|
30
|
+
export default components;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare module 'virtual-global-styles';
|
|
34
|
+
|
|
35
|
+
declare module 'virtual-i18n-text';
|
|
36
|
+
|
|
37
|
+
declare module 'virtual-search-hooks' {
|
|
38
|
+
export const onSearch: (query: string) => void | Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare module '*.module.scss' {
|
|
42
|
+
const classes: { [key: string]: string };
|
|
43
|
+
export default classes;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
declare module '*.svg' {
|
|
47
|
+
export const ReactComponent: React.SFC<React.SVGProps<SVGSVGElement>>;
|
|
48
|
+
export default ReactComponent;
|
|
49
|
+
}
|
package/src/runtime/hooks.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { createContext, useContext } from 'react';
|
|
1
|
+
import { createContext, useContext, useLayoutEffect, useState } from 'react';
|
|
2
2
|
import { PageData } from '@rspress/shared';
|
|
3
3
|
import i18nTextData from 'virtual-i18n-text';
|
|
4
|
+
import { flushSync } from 'react-dom';
|
|
4
5
|
|
|
5
6
|
// Type shim for window.__EDEN_PAGE_DATA__
|
|
6
7
|
declare global {
|
|
@@ -32,6 +33,11 @@ export function useLang(): string {
|
|
|
32
33
|
return ctx.data.page.lang || '';
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
export function useVersion(): string {
|
|
37
|
+
const ctx = useContext(DataContext);
|
|
38
|
+
return ctx.data.page.version || '';
|
|
39
|
+
}
|
|
40
|
+
|
|
35
41
|
export function useDark() {
|
|
36
42
|
const ctx = useContext(ThemeContext);
|
|
37
43
|
return ctx.theme === 'dark';
|
|
@@ -42,3 +48,43 @@ export function useI18n<T = Record<string, Record<string, string>>>() {
|
|
|
42
48
|
|
|
43
49
|
return (key: keyof T) => i18nTextData[key][lang];
|
|
44
50
|
}
|
|
51
|
+
|
|
52
|
+
declare global {
|
|
53
|
+
interface Document {
|
|
54
|
+
startViewTransition: (callback: () => void) => void;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function useViewTransition(dom) {
|
|
59
|
+
/**
|
|
60
|
+
* use a pesudo element to hold the actual JSX element so we can schedule the
|
|
61
|
+
* update later in sync
|
|
62
|
+
*/
|
|
63
|
+
const [element, setElement] = useState(dom);
|
|
64
|
+
|
|
65
|
+
useLayoutEffect(() => {
|
|
66
|
+
if (document.startViewTransition) {
|
|
67
|
+
/**
|
|
68
|
+
* the browser will take a screenshot here
|
|
69
|
+
*/
|
|
70
|
+
document.startViewTransition(() => {
|
|
71
|
+
/**
|
|
72
|
+
* react will batch all the updates in callback and flush it sync
|
|
73
|
+
*/
|
|
74
|
+
flushSync(() => {
|
|
75
|
+
setElement(dom);
|
|
76
|
+
});
|
|
77
|
+
/**
|
|
78
|
+
* react flushed the dom to browser
|
|
79
|
+
* and the browser will start the animation
|
|
80
|
+
*/
|
|
81
|
+
});
|
|
82
|
+
} else {
|
|
83
|
+
setElement(dom);
|
|
84
|
+
}
|
|
85
|
+
}, [dom]);
|
|
86
|
+
/**
|
|
87
|
+
* take this element to the actual VDOM tree
|
|
88
|
+
*/
|
|
89
|
+
return element;
|
|
90
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
// Only appear on mobile
|
|
2
|
+
.rspress-sidebar-menu {
|
|
2
3
|
position: sticky;
|
|
3
4
|
top: 0;
|
|
4
5
|
left: 0;
|
|
@@ -11,7 +12,7 @@
|
|
|
11
12
|
color: var(--rp-c-text-1);
|
|
12
13
|
background-color: var(--rp-c-bg);
|
|
13
14
|
padding-left: 10px;
|
|
14
|
-
|
|
15
|
+
> button {
|
|
15
16
|
padding: 12px;
|
|
16
17
|
transition: color 0.5s;
|
|
17
18
|
&:hover {
|
|
@@ -20,15 +21,15 @@
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
@media (min-width: 960px) {
|
|
23
|
-
.
|
|
24
|
+
.rspress-sidebar-menu {
|
|
24
25
|
display: none;
|
|
25
26
|
}
|
|
26
|
-
.
|
|
27
|
+
.rspress-sidebar-back-drop {
|
|
27
28
|
display: none;
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
.
|
|
32
|
+
.rspress-sidebar-back-drop {
|
|
32
33
|
position: fixed;
|
|
33
34
|
top: 0;
|
|
34
35
|
right: 0;
|
|
@@ -2,7 +2,7 @@ import { Fragment, useEffect, useState } from 'react';
|
|
|
2
2
|
import { NormalizedSidebarGroup, SidebarItem } from '@rspress/shared';
|
|
3
3
|
import { SideBar } from '../Sidebar';
|
|
4
4
|
import MenuIcon from '../../assets/menu.svg';
|
|
5
|
-
import
|
|
5
|
+
import './index.scss';
|
|
6
6
|
|
|
7
7
|
interface Props {
|
|
8
8
|
pathname: string;
|
|
@@ -27,8 +27,8 @@ export function SideMenu(props: Props) {
|
|
|
27
27
|
|
|
28
28
|
return (
|
|
29
29
|
<Fragment>
|
|
30
|
-
<div className=
|
|
31
|
-
<button onClick={openSidebar} className=
|
|
30
|
+
<div className="rspress-sidebar-menu">
|
|
31
|
+
<button onClick={openSidebar} className="flex-center">
|
|
32
32
|
<div className="text-md mr-2">
|
|
33
33
|
<MenuIcon />
|
|
34
34
|
</div>
|
|
@@ -42,7 +42,7 @@ export function SideMenu(props: Props) {
|
|
|
42
42
|
isSidebarOpen={isSidebarOpen}
|
|
43
43
|
/>
|
|
44
44
|
{isSidebarOpen ? (
|
|
45
|
-
<div onClick={closeSidebar} className=
|
|
45
|
+
<div onClick={closeSidebar} className="rspress-sidebar-back-drop" />
|
|
46
46
|
) : null}
|
|
47
47
|
</Fragment>
|
|
48
48
|
);
|
|
@@ -9,7 +9,7 @@ import { SwitchAppearance } from '../SwitchAppearance';
|
|
|
9
9
|
import { NavMenuGroup, NavMenuGroupItem } from './NavMenuGroup';
|
|
10
10
|
import { NavMenuSingleItem } from './NavMenuSingleItem';
|
|
11
11
|
import styles from './index.module.scss';
|
|
12
|
-
import { ThemeContext, usePageData, withBase } from '@/runtime';
|
|
12
|
+
import { ThemeContext, usePageData, useVersion, withBase } from '@/runtime';
|
|
13
13
|
|
|
14
14
|
export interface NavProps {
|
|
15
15
|
beforeNav?: React.ReactNode;
|
|
@@ -70,21 +70,35 @@ export function Nav(props: NavProps) {
|
|
|
70
70
|
const { pathname } = useLocation();
|
|
71
71
|
const { theme } = useContext(ThemeContext);
|
|
72
72
|
const localeData = useLocaleSiteData();
|
|
73
|
+
const currentVersion = useVersion();
|
|
73
74
|
const [isMobile, setIsMobile] = useState(false);
|
|
74
75
|
const localeLanguages = Object.values(siteData.themeConfig.locales || {});
|
|
75
76
|
const hasMultiLanguage = localeLanguages.length > 1;
|
|
76
77
|
const socialLinks = siteData.themeConfig.socialLinks || [];
|
|
77
78
|
const hasSocialLinks = socialLinks.length > 0;
|
|
78
|
-
const defaultLang = siteData.lang || '
|
|
79
|
+
const defaultLang = siteData.lang || '';
|
|
80
|
+
const defaultVersion = siteData.multiVersion.default || '';
|
|
79
81
|
const { lang } = page;
|
|
80
|
-
const langs = localeLanguages.map(item => item.lang || '
|
|
82
|
+
const langs = localeLanguages.map(item => item.lang || '') || [];
|
|
81
83
|
const [logo, setLogo] = useState(getLogoUrl(rawLogo, theme));
|
|
82
84
|
|
|
83
85
|
const translationMenuData = hasMultiLanguage
|
|
84
86
|
? {
|
|
85
87
|
items: localeLanguages.map(item => ({
|
|
86
88
|
text: item?.label,
|
|
87
|
-
link: replaceLang(
|
|
89
|
+
link: replaceLang(
|
|
90
|
+
pathname,
|
|
91
|
+
{
|
|
92
|
+
current: lang,
|
|
93
|
+
target: item.lang,
|
|
94
|
+
default: defaultLang,
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
current: currentVersion,
|
|
98
|
+
default: defaultVersion,
|
|
99
|
+
},
|
|
100
|
+
base,
|
|
101
|
+
),
|
|
88
102
|
})),
|
|
89
103
|
activeValue: localeLanguages.find(item => lang === item.lang)?.label,
|
|
90
104
|
}
|
|
@@ -173,7 +187,7 @@ export function Nav(props: NavProps) {
|
|
|
173
187
|
};
|
|
174
188
|
return (
|
|
175
189
|
<header
|
|
176
|
-
className=
|
|
190
|
+
className="top-0 left-0 md:fixed w-full"
|
|
177
191
|
style={{
|
|
178
192
|
zIndex: 'var(--rp-z-index-nav)',
|
|
179
193
|
background: 'var(--rp-c-bg)',
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { createPortal } from 'react-dom';
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { groupBy, debounce } from 'lodash-es';
|
|
4
|
+
import { onSearch } from 'virtual-search-hooks';
|
|
5
|
+
import { SearchOptions } from '@rspress/shared';
|
|
6
|
+
import { getSidebarGroupData } from '../../logic/useSidebarData';
|
|
7
|
+
import { useLocaleSiteData } from '../../logic/useLocaleSiteData';
|
|
8
|
+
import { Tabs, Tab } from '../Tabs';
|
|
9
|
+
import styles from './index.module.scss';
|
|
10
|
+
import SearchSvg from './assets/search.svg';
|
|
11
|
+
import LoadingSvg from './assets/loading.svg';
|
|
12
|
+
import CloseSvg from './assets/close.svg';
|
|
13
|
+
import { MatchResult, MatchResultItem, PageSearcher } from './logic/search';
|
|
14
|
+
import { SuggestItem } from './SuggestItem';
|
|
15
|
+
import { normalizeSearchIndexes, removeDomain } from './logic/util';
|
|
16
|
+
import { NoSearchResult } from './NoSearchResult';
|
|
17
|
+
import { isProduction, usePageData } from '@/runtime';
|
|
18
|
+
|
|
19
|
+
const KEY_CODE = {
|
|
20
|
+
ARROW_UP: 'ArrowUp',
|
|
21
|
+
ARROW_DOWN: 'ArrowDown',
|
|
22
|
+
ENTER: 'Enter',
|
|
23
|
+
SEARCH: 'KeyK',
|
|
24
|
+
ESC: 'Escape',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const RECOMMEND_WORD: Record<string, string> = {
|
|
28
|
+
zh: '其它站点推荐结果',
|
|
29
|
+
en: 'Other Site Search Results',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export interface SearchPanelProps {
|
|
33
|
+
focused: boolean;
|
|
34
|
+
setFocused: (focused: boolean) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function SearchPanel({ focused, setFocused }: SearchPanelProps) {
|
|
38
|
+
const [query, setQuery] = useState('');
|
|
39
|
+
const [searchResult, setSearchResult] = useState<MatchResult>({
|
|
40
|
+
current: [],
|
|
41
|
+
others: [],
|
|
42
|
+
});
|
|
43
|
+
const searchInputRef = useRef<HTMLInputElement | null>(null);
|
|
44
|
+
const [initing, setIniting] = useState(true);
|
|
45
|
+
const [currentSuggestionIndex, setCurrentSuggestionIndex] = useState(0);
|
|
46
|
+
const pageSearcherRef = useRef<PageSearcher | null>(null);
|
|
47
|
+
const {
|
|
48
|
+
siteData,
|
|
49
|
+
page: { lang },
|
|
50
|
+
} = usePageData();
|
|
51
|
+
const { sidebar } = useLocaleSiteData();
|
|
52
|
+
const { search } = siteData;
|
|
53
|
+
|
|
54
|
+
const suggestions = [
|
|
55
|
+
...searchResult.current,
|
|
56
|
+
...searchResult.others.map(item => item.items),
|
|
57
|
+
].flat();
|
|
58
|
+
|
|
59
|
+
// We need to extract the group name by the link so that we can divide the search result into different groups.
|
|
60
|
+
const extractGroupName = (link: string) =>
|
|
61
|
+
getSidebarGroupData(sidebar, link).group;
|
|
62
|
+
|
|
63
|
+
async function initPageSearcher() {
|
|
64
|
+
if (search === false) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const pageSearcher = new PageSearcher({
|
|
68
|
+
...search,
|
|
69
|
+
currentLang: lang,
|
|
70
|
+
extractGroupName,
|
|
71
|
+
});
|
|
72
|
+
pageSearcherRef.current = pageSearcher;
|
|
73
|
+
await pageSearcherRef.current.init();
|
|
74
|
+
setIniting(false);
|
|
75
|
+
const query = searchInputRef.current?.value;
|
|
76
|
+
if (query) {
|
|
77
|
+
const matched = await pageSearcherRef.current?.match(query);
|
|
78
|
+
setSearchResult(matched || { current: [], others: [] });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
84
|
+
switch (e.code) {
|
|
85
|
+
case KEY_CODE.SEARCH:
|
|
86
|
+
if (e.ctrlKey || e.metaKey) {
|
|
87
|
+
e.preventDefault();
|
|
88
|
+
setFocused(!focused);
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
case KEY_CODE.ARROW_DOWN:
|
|
92
|
+
if (focused) {
|
|
93
|
+
e.preventDefault();
|
|
94
|
+
setCurrentSuggestionIndex(
|
|
95
|
+
(currentSuggestionIndex + 1) % suggestions.length,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
case KEY_CODE.ARROW_UP:
|
|
100
|
+
if (focused) {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
setCurrentSuggestionIndex(
|
|
103
|
+
(currentSuggestionIndex - 1 + suggestions.length) %
|
|
104
|
+
suggestions.length,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
case KEY_CODE.ENTER:
|
|
109
|
+
if (currentSuggestionIndex >= 0) {
|
|
110
|
+
const suggestion = suggestions[currentSuggestionIndex];
|
|
111
|
+
const isCurrent =
|
|
112
|
+
currentSuggestionIndex < searchResult.current.length;
|
|
113
|
+
if (isCurrent) {
|
|
114
|
+
window.location.href = isProduction()
|
|
115
|
+
? suggestion.link
|
|
116
|
+
: removeDomain(suggestion.link);
|
|
117
|
+
setFocused(false);
|
|
118
|
+
} else {
|
|
119
|
+
window.open(suggestion.link);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
case KEY_CODE.ESC:
|
|
124
|
+
setFocused(false);
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
document.addEventListener('keydown', onKeyDown);
|
|
131
|
+
return () => {
|
|
132
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
133
|
+
};
|
|
134
|
+
}, [
|
|
135
|
+
setCurrentSuggestionIndex,
|
|
136
|
+
setFocused,
|
|
137
|
+
suggestions,
|
|
138
|
+
currentSuggestionIndex,
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
if (focused) {
|
|
143
|
+
setSearchResult({ current: [], others: [] });
|
|
144
|
+
if (!pageSearcherRef.current) {
|
|
145
|
+
initPageSearcher();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}, [focused]);
|
|
149
|
+
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
!initing && initPageSearcher();
|
|
152
|
+
// init pageSearcher again when lang changed
|
|
153
|
+
}, [lang]);
|
|
154
|
+
|
|
155
|
+
const onQueryChanged = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
156
|
+
const newQuery = e.target.value;
|
|
157
|
+
setQuery(newQuery);
|
|
158
|
+
if (newQuery) {
|
|
159
|
+
await onSearch(newQuery);
|
|
160
|
+
const matched = await pageSearcherRef.current?.match(newQuery);
|
|
161
|
+
setSearchResult(matched || { current: [], others: [] });
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const normalizeSuggestions = (suggestions: MatchResultItem[]) =>
|
|
166
|
+
groupBy(suggestions, 'group');
|
|
167
|
+
|
|
168
|
+
// accumulateIndex is used to calculate the index of the suggestion in the whole list.
|
|
169
|
+
let accumulateIndex = -1;
|
|
170
|
+
|
|
171
|
+
const renderSearchResult = (
|
|
172
|
+
result: MatchResult,
|
|
173
|
+
searchOptions: SearchOptions,
|
|
174
|
+
) => {
|
|
175
|
+
if (!suggestions.length && !initing) {
|
|
176
|
+
return <NoSearchResult query={query} />;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const hasOtherResult =
|
|
180
|
+
searchResult.others.map(item => item.items).flat().length > 0;
|
|
181
|
+
|
|
182
|
+
const tabValues = result.others.map(item => {
|
|
183
|
+
if (!searchOptions || searchOptions.mode !== 'remote') {
|
|
184
|
+
return item;
|
|
185
|
+
}
|
|
186
|
+
const indexItem = normalizeSearchIndexes(
|
|
187
|
+
searchOptions.searchIndexes || [],
|
|
188
|
+
).find(indexInfo => indexInfo.value === item.index);
|
|
189
|
+
return indexItem.label;
|
|
190
|
+
}) as string[];
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<div>
|
|
194
|
+
{/* current index */}
|
|
195
|
+
{renderSearchResultItem(result.current)}
|
|
196
|
+
{/* other indexes */}
|
|
197
|
+
{hasOtherResult && (
|
|
198
|
+
<h2 className={styles.groupTitle}>{RECOMMEND_WORD[lang]}</h2>
|
|
199
|
+
)}
|
|
200
|
+
<div style={{ marginTop: '-12px' }}>
|
|
201
|
+
<Tabs values={tabValues} tabContainerClassName={styles.tabClassName}>
|
|
202
|
+
{result.others.map(item => (
|
|
203
|
+
<Tab key={item.index}>
|
|
204
|
+
{renderSearchResultItem(item.items, false)}
|
|
205
|
+
</Tab>
|
|
206
|
+
))}
|
|
207
|
+
</Tabs>
|
|
208
|
+
</div>
|
|
209
|
+
</div>
|
|
210
|
+
);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const renderSearchResultItem = (
|
|
214
|
+
suggestionList: MatchResultItem[],
|
|
215
|
+
isCurrent = true,
|
|
216
|
+
) => {
|
|
217
|
+
// if no result, show no result
|
|
218
|
+
if (suggestionList.length === 0 && !initing) {
|
|
219
|
+
return (
|
|
220
|
+
<div className="mt-4 flex-center">
|
|
221
|
+
<div
|
|
222
|
+
className="p-2 font-bold text-md"
|
|
223
|
+
style={{
|
|
224
|
+
color: '#2c3e50',
|
|
225
|
+
}}
|
|
226
|
+
>
|
|
227
|
+
No results found
|
|
228
|
+
</div>
|
|
229
|
+
</div>
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
const normalizedSuggestions = normalizeSuggestions(suggestionList);
|
|
233
|
+
return (
|
|
234
|
+
<ul className={styles.suggestList}>
|
|
235
|
+
{Object.keys(normalizedSuggestions).map(group => {
|
|
236
|
+
const groupSuggestions = normalizedSuggestions[group] || [];
|
|
237
|
+
return (
|
|
238
|
+
<li key={group}>
|
|
239
|
+
{isCurrent && <h2 className={styles.groupTitle}>{group}</h2>}
|
|
240
|
+
<ul className="pb-2">
|
|
241
|
+
{groupSuggestions.map(suggestion => {
|
|
242
|
+
accumulateIndex++;
|
|
243
|
+
const suggestionIndex = accumulateIndex;
|
|
244
|
+
return (
|
|
245
|
+
<SuggestItem
|
|
246
|
+
key={`${suggestion.title}-${suggestionIndex}`}
|
|
247
|
+
suggestion={suggestion}
|
|
248
|
+
isCurrent={suggestionIndex === currentSuggestionIndex}
|
|
249
|
+
setCurrentSuggestionIndex={() => {
|
|
250
|
+
setCurrentSuggestionIndex(suggestionIndex);
|
|
251
|
+
}}
|
|
252
|
+
closeSearch={() => setFocused(false)}
|
|
253
|
+
inCurrentDocIndex={
|
|
254
|
+
suggestionIndex < searchResult.current.length
|
|
255
|
+
}
|
|
256
|
+
/>
|
|
257
|
+
);
|
|
258
|
+
})}
|
|
259
|
+
</ul>
|
|
260
|
+
</li>
|
|
261
|
+
);
|
|
262
|
+
})}
|
|
263
|
+
</ul>
|
|
264
|
+
);
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
return (
|
|
268
|
+
<>
|
|
269
|
+
{focused &&
|
|
270
|
+
createPortal(
|
|
271
|
+
<div className={styles.mask} onClick={() => setFocused(false)}>
|
|
272
|
+
<div
|
|
273
|
+
className={`${styles.modal}`}
|
|
274
|
+
onClick={e => {
|
|
275
|
+
setFocused(true);
|
|
276
|
+
e.stopPropagation();
|
|
277
|
+
}}
|
|
278
|
+
>
|
|
279
|
+
<div className="flex items-center">
|
|
280
|
+
<div className={styles.inputForm}>
|
|
281
|
+
<label>
|
|
282
|
+
<SearchSvg />
|
|
283
|
+
</label>
|
|
284
|
+
<input
|
|
285
|
+
className={styles.input}
|
|
286
|
+
ref={searchInputRef}
|
|
287
|
+
placeholder="Search Docs"
|
|
288
|
+
aria-label="Search"
|
|
289
|
+
autoComplete="off"
|
|
290
|
+
autoFocus
|
|
291
|
+
onChange={debounce(onQueryChanged, 150)}
|
|
292
|
+
/>
|
|
293
|
+
<label>
|
|
294
|
+
<CloseSvg
|
|
295
|
+
className={styles.close}
|
|
296
|
+
onClick={e => {
|
|
297
|
+
if (searchInputRef.current) {
|
|
298
|
+
e.stopPropagation();
|
|
299
|
+
if (!query) {
|
|
300
|
+
setFocused(false);
|
|
301
|
+
} else {
|
|
302
|
+
searchInputRef.current.value = '';
|
|
303
|
+
setQuery('');
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}}
|
|
307
|
+
/>
|
|
308
|
+
</label>
|
|
309
|
+
</div>
|
|
310
|
+
<h2
|
|
311
|
+
className="text-brand ml-2 sm:hidden cursor-pointer"
|
|
312
|
+
onClick={e => {
|
|
313
|
+
e.stopPropagation();
|
|
314
|
+
setFocused(false);
|
|
315
|
+
}}
|
|
316
|
+
>
|
|
317
|
+
Cancel
|
|
318
|
+
</h2>
|
|
319
|
+
</div>
|
|
320
|
+
|
|
321
|
+
{query ? (
|
|
322
|
+
<div className={`${styles.searchHits} rspress-scrollbar`}>
|
|
323
|
+
{renderSearchResult(searchResult, search)}
|
|
324
|
+
</div>
|
|
325
|
+
) : null}
|
|
326
|
+
{initing && (
|
|
327
|
+
<div className="flex-center">
|
|
328
|
+
<div className="p-2 text-sm">
|
|
329
|
+
<LoadingSvg />
|
|
330
|
+
</div>
|
|
331
|
+
</div>
|
|
332
|
+
)}
|
|
333
|
+
</div>
|
|
334
|
+
</div>,
|
|
335
|
+
document.getElementById('search-container')!,
|
|
336
|
+
)}
|
|
337
|
+
</>
|
|
338
|
+
);
|
|
339
|
+
}
|