@docusaurus/theme-common 2.0.0-beta.1 → 2.0.0-beta.10
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/copyUntypedFiles.js +20 -0
- package/lib/.tsbuildinfo +1 -1
- package/lib/components/Collapsible/index.d.ts +35 -0
- package/lib/components/Collapsible/index.js +139 -0
- package/lib/components/Details/index.d.ts +12 -0
- package/lib/components/Details/index.js +64 -0
- package/lib/components/Details/styles.module.css +58 -0
- package/lib/index.d.ts +24 -4
- package/lib/index.js +18 -3
- package/lib/utils/ThemeClassNames.d.ts +36 -12
- package/lib/utils/ThemeClassNames.js +36 -3
- package/lib/utils/announcementBarUtils.d.ts +17 -0
- package/lib/utils/announcementBarUtils.js +69 -0
- package/lib/utils/codeBlockUtils.d.ts +10 -0
- package/lib/utils/codeBlockUtils.js +119 -0
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +2 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +2 -2
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +1 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +11 -3
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
- package/lib/utils/docsUtils.d.ts +20 -0
- package/lib/utils/docsUtils.js +106 -0
- package/lib/utils/generalUtils.d.ts +6 -0
- package/lib/utils/generalUtils.js +2 -2
- package/lib/utils/historyUtils.d.ts +11 -0
- package/lib/utils/historyUtils.js +39 -0
- package/lib/utils/jsUtils.d.ts +19 -0
- package/lib/utils/jsUtils.js +25 -0
- package/lib/utils/mobileSecondaryMenu.d.ts +20 -0
- package/lib/utils/mobileSecondaryMenu.js +49 -0
- package/lib/utils/pathUtils.js +1 -3
- package/lib/utils/reactUtils.d.ts +9 -0
- package/lib/utils/reactUtils.js +26 -0
- package/lib/utils/regexpUtils.d.ts +10 -0
- package/lib/utils/regexpUtils.js +16 -0
- package/lib/utils/scrollUtils.d.ts +52 -0
- package/lib/utils/scrollUtils.js +135 -0
- package/lib/utils/storageUtils.d.ts +4 -0
- package/lib/utils/storageUtils.js +29 -3
- package/lib/utils/tagsUtils.d.ts +18 -0
- package/lib/utils/tagsUtils.js +33 -0
- package/lib/utils/tocUtils.d.ts +15 -0
- package/lib/utils/tocUtils.js +34 -0
- package/lib/utils/useContextualSearchFilters.d.ts +11 -0
- package/lib/utils/useContextualSearchFilters.js +36 -0
- package/lib/utils/{useChangeRoute.d.ts → useLocalPathname.d.ts} +1 -1
- package/lib/utils/useLocalPathname.js +16 -0
- package/lib/utils/useLocationChange.d.ts +14 -0
- package/lib/utils/useLocationChange.js +23 -0
- package/lib/utils/usePluralForm.js +1 -3
- package/{src/utils/docsUtils.ts → lib/utils/usePrevious.d.ts} +1 -5
- package/lib/utils/usePrevious.js +15 -0
- package/lib/utils/useTOCHighlight.d.ts +14 -0
- package/lib/utils/useTOCHighlight.js +124 -0
- package/lib/utils/useThemeConfig.d.ts +21 -3
- package/package.json +18 -12
- package/src/components/Collapsible/index.tsx +242 -0
- package/src/components/Details/index.tsx +94 -0
- package/src/components/Details/styles.module.css +58 -0
- package/src/index.ts +73 -3
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +42 -4
- package/src/utils/__tests__/codeBlockUtils.test.ts +2 -2
- package/src/utils/__tests__/docsUtils.test.tsx +331 -0
- package/src/utils/__tests__/jsUtils.test.ts +33 -0
- package/src/utils/__tests__/tagUtils.test.ts +66 -0
- package/src/utils/__tests__/tocUtils.test.ts +197 -0
- package/src/utils/announcementBarUtils.tsx +120 -0
- package/src/utils/codeBlockUtils.ts +151 -0
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +7 -6
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +2 -3
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +14 -14
- package/src/utils/docsUtils.tsx +185 -0
- package/src/utils/generalUtils.ts +3 -2
- package/src/utils/historyUtils.ts +50 -0
- package/src/utils/jsUtils.ts +33 -0
- package/src/utils/mobileSecondaryMenu.tsx +114 -0
- package/src/utils/pathUtils.ts +2 -3
- package/src/utils/reactUtils.tsx +34 -0
- package/src/utils/regexpUtils.ts +23 -0
- package/src/utils/scrollUtils.tsx +237 -0
- package/src/utils/storageUtils.ts +27 -4
- package/src/utils/tagsUtils.ts +48 -0
- package/src/utils/tocUtils.ts +55 -0
- package/src/utils/useAlternatePageUtils.ts +9 -1
- package/src/utils/useContextualSearchFilters.ts +50 -0
- package/src/utils/useLocalPathname.ts +20 -0
- package/src/utils/useLocationChange.ts +35 -0
- package/src/utils/usePluralForm.ts +5 -4
- package/src/utils/usePrevious.ts +19 -0
- package/src/utils/useTOCHighlight.ts +179 -0
- package/src/utils/useThemeConfig.ts +20 -3
- package/lib/utils/useChangeRoute.js +0 -18
- package/src/utils/useChangeRoute.ts +0 -21
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// A replacement of lodash in client code
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Gets the duplicate values in an array.
|
|
12
|
+
* @param arr The array.
|
|
13
|
+
* @param comparator Compares two values and returns `true` if they are equal (duplicated).
|
|
14
|
+
* @returns Value of the elements `v` that have a preceding element `u` where `comparator(u, v) === true`. Values within the returned array are not guaranteed to be unique.
|
|
15
|
+
*/
|
|
16
|
+
export function duplicates<T>(
|
|
17
|
+
arr: readonly T[],
|
|
18
|
+
comparator: (a: T, b: T) => boolean = (a, b) => a === b,
|
|
19
|
+
): T[] {
|
|
20
|
+
return arr.filter(
|
|
21
|
+
(v, vIndex) => arr.findIndex((u) => comparator(u, v)) !== vIndex,
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Remove duplicate array items (similar to _.uniq)
|
|
27
|
+
* @param arr The array.
|
|
28
|
+
* @returns An array with duplicate elements removed by reference comparison.
|
|
29
|
+
*/
|
|
30
|
+
export function uniq<T>(arr: T[]): T[] {
|
|
31
|
+
// Note: had problems with [...new Set()]: https://github.com/facebook/docusaurus/issues/4972#issuecomment-863895061
|
|
32
|
+
return Array.from(new Set(arr));
|
|
33
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React, {
|
|
9
|
+
useState,
|
|
10
|
+
ReactNode,
|
|
11
|
+
useContext,
|
|
12
|
+
createContext,
|
|
13
|
+
useEffect,
|
|
14
|
+
ComponentType,
|
|
15
|
+
useMemo,
|
|
16
|
+
} from 'react';
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
The idea behind all this is that a specific component must be able to fill a placeholder in the generic layout
|
|
20
|
+
The doc page should be able to fill the secondary menu of the main mobile navbar.
|
|
21
|
+
This permits to reduce coupling between the main layout and the specific page.
|
|
22
|
+
|
|
23
|
+
This kind of feature is often called portal/teleport/gateway... various unmaintained React libs exist
|
|
24
|
+
Most up-to-date one: https://github.com/gregberge/react-teleporter
|
|
25
|
+
Not sure any of those is safe regarding concurrent mode.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
type ExtraProps = {
|
|
29
|
+
toggleSidebar: () => void;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type MobileSecondaryMenuComponent<Props> = ComponentType<
|
|
33
|
+
Props & ExtraProps
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
type State = {
|
|
37
|
+
component: MobileSecondaryMenuComponent<unknown>;
|
|
38
|
+
props: unknown;
|
|
39
|
+
} | null;
|
|
40
|
+
|
|
41
|
+
function useContextValue() {
|
|
42
|
+
return useState<State>(null);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type ContextValue = ReturnType<typeof useContextValue>;
|
|
46
|
+
|
|
47
|
+
const Context = createContext<ContextValue | null>(null);
|
|
48
|
+
|
|
49
|
+
export function MobileSecondaryMenuProvider({
|
|
50
|
+
children,
|
|
51
|
+
}: {
|
|
52
|
+
children: ReactNode;
|
|
53
|
+
}): JSX.Element {
|
|
54
|
+
return (
|
|
55
|
+
<Context.Provider value={useContextValue()}>{children}</Context.Provider>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function useMobileSecondaryMenuContext(): ContextValue {
|
|
60
|
+
const value = useContext(Context);
|
|
61
|
+
if (value === null) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
'MobileSecondaryMenuProvider was not used correctly, context value is null',
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function useMobileSecondaryMenuRenderer(): (
|
|
70
|
+
extraProps: ExtraProps,
|
|
71
|
+
) => ReactNode | undefined {
|
|
72
|
+
const [state] = useMobileSecondaryMenuContext();
|
|
73
|
+
if (state) {
|
|
74
|
+
const Comp = state.component;
|
|
75
|
+
return function render(extraProps) {
|
|
76
|
+
return <Comp {...state.props} {...extraProps} />;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return () => undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function useShallowMemoizedObject<O extends Record<string, unknown>>(obj: O) {
|
|
83
|
+
return useMemo(
|
|
84
|
+
() => obj,
|
|
85
|
+
// Is this safe?
|
|
86
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
87
|
+
[...Object.keys(obj), ...Object.values(obj)],
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Fill the secondary menu placeholder with some real content
|
|
92
|
+
export function MobileSecondaryMenuFiller<
|
|
93
|
+
Props extends Record<string, unknown>,
|
|
94
|
+
>({
|
|
95
|
+
component,
|
|
96
|
+
props,
|
|
97
|
+
}: {
|
|
98
|
+
component: MobileSecondaryMenuComponent<Props & ExtraProps>;
|
|
99
|
+
props: Props;
|
|
100
|
+
}): JSX.Element | null {
|
|
101
|
+
const [, setState] = useMobileSecondaryMenuContext();
|
|
102
|
+
|
|
103
|
+
// To avoid useless context re-renders, props are memoized shallowly
|
|
104
|
+
const memoizedProps = useShallowMemoizedObject(props);
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
// @ts-expect-error: context is not 100% typesafe but it's ok
|
|
108
|
+
setState({component, props: memoizedProps});
|
|
109
|
+
}, [setState, component, memoizedProps]);
|
|
110
|
+
|
|
111
|
+
useEffect(() => () => setState(null), [setState]);
|
|
112
|
+
|
|
113
|
+
return null;
|
|
114
|
+
}
|
package/src/utils/pathUtils.ts
CHANGED
|
@@ -10,8 +10,7 @@ export const isSamePath = (
|
|
|
10
10
|
path1: string | undefined,
|
|
11
11
|
path2: string | undefined,
|
|
12
12
|
): boolean => {
|
|
13
|
-
const normalize = (pathname: string | undefined) =>
|
|
14
|
-
|
|
15
|
-
};
|
|
13
|
+
const normalize = (pathname: string | undefined) =>
|
|
14
|
+
!pathname || pathname?.endsWith('/') ? pathname : `${pathname}/`;
|
|
16
15
|
return normalize(path1) === normalize(path2);
|
|
17
16
|
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {useCallback, useEffect, useLayoutEffect, useRef} from 'react';
|
|
9
|
+
|
|
10
|
+
// This hook is like useLayoutEffect, but without the SSR warning
|
|
11
|
+
// It seems hacky but it's used in many React libs (Redux, Formik...)
|
|
12
|
+
// Also mentioned here: https://github.com/facebook/react/issues/16956
|
|
13
|
+
// It is useful when you need to update a ref as soon as possible after a React render (before useEffect)
|
|
14
|
+
export const useIsomorphicLayoutEffect =
|
|
15
|
+
typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
16
|
+
|
|
17
|
+
// Permits to transform an unstable callback (like an arrow function provided as props)
|
|
18
|
+
// to a "stable" callback that is safe to use in a useEffect dependency array
|
|
19
|
+
// Useful to avoid React stale closure problems + avoid useless effect re-executions
|
|
20
|
+
//
|
|
21
|
+
// Workaround until the React team recommends a good solution, see https://github.com/facebook/react/issues/16956
|
|
22
|
+
// This generally works has some potential drawbacks, such as https://github.com/facebook/react/issues/16956#issuecomment-536636418
|
|
23
|
+
export function useDynamicCallback<T extends (...args: never[]) => unknown>(
|
|
24
|
+
callback: T,
|
|
25
|
+
): T {
|
|
26
|
+
const ref = useRef<T>(callback);
|
|
27
|
+
|
|
28
|
+
useIsomorphicLayoutEffect(() => {
|
|
29
|
+
ref.current = callback;
|
|
30
|
+
}, [callback]);
|
|
31
|
+
|
|
32
|
+
// @ts-expect-error: TODO, not sure how to fix this TS error
|
|
33
|
+
return useCallback<T>((...args) => ref.current(...args), []);
|
|
34
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Utility to convert an optional string into a Regex case sensitive and global
|
|
10
|
+
*/
|
|
11
|
+
export function isRegexpStringMatch(
|
|
12
|
+
regexAsString?: string,
|
|
13
|
+
valueToTest?: string,
|
|
14
|
+
): boolean {
|
|
15
|
+
if (
|
|
16
|
+
typeof regexAsString === 'undefined' ||
|
|
17
|
+
typeof valueToTest === 'undefined'
|
|
18
|
+
) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return new RegExp(regexAsString, 'gi').test(valueToTest);
|
|
23
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import React, {
|
|
9
|
+
createContext,
|
|
10
|
+
ReactNode,
|
|
11
|
+
useCallback,
|
|
12
|
+
useContext,
|
|
13
|
+
useEffect,
|
|
14
|
+
useLayoutEffect,
|
|
15
|
+
useMemo,
|
|
16
|
+
useRef,
|
|
17
|
+
} from 'react';
|
|
18
|
+
import {useDynamicCallback} from './reactUtils';
|
|
19
|
+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* We need a way to update the scroll position while ignoring scroll events
|
|
23
|
+
* without affecting Navbar/BackToTop visibility
|
|
24
|
+
*
|
|
25
|
+
* This API permits to temporarily disable/ignore scroll events
|
|
26
|
+
* Motivated by https://github.com/facebook/docusaurus/pull/5618
|
|
27
|
+
*/
|
|
28
|
+
type ScrollController = {
|
|
29
|
+
/**
|
|
30
|
+
* A boolean ref tracking whether scroll events are enabled
|
|
31
|
+
*/
|
|
32
|
+
scrollEventsEnabledRef: React.MutableRefObject<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Enables scroll events in `useScrollPosition`
|
|
35
|
+
*/
|
|
36
|
+
enableScrollEvents: () => void;
|
|
37
|
+
/**
|
|
38
|
+
* Disables scroll events in `useScrollPosition`
|
|
39
|
+
*/
|
|
40
|
+
disableScrollEvents: () => void;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function useScrollControllerContextValue(): ScrollController {
|
|
44
|
+
const scrollEventsEnabledRef = useRef(true);
|
|
45
|
+
|
|
46
|
+
return useMemo(
|
|
47
|
+
() => ({
|
|
48
|
+
scrollEventsEnabledRef,
|
|
49
|
+
enableScrollEvents: () => {
|
|
50
|
+
scrollEventsEnabledRef.current = true;
|
|
51
|
+
},
|
|
52
|
+
disableScrollEvents: () => {
|
|
53
|
+
scrollEventsEnabledRef.current = false;
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
[],
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const ScrollMonitorContext = createContext<ScrollController | undefined>(
|
|
61
|
+
undefined,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
export function ScrollControllerProvider({
|
|
65
|
+
children,
|
|
66
|
+
}: {
|
|
67
|
+
children: ReactNode;
|
|
68
|
+
}): JSX.Element {
|
|
69
|
+
return (
|
|
70
|
+
<ScrollMonitorContext.Provider value={useScrollControllerContextValue()}>
|
|
71
|
+
{children}
|
|
72
|
+
</ScrollMonitorContext.Provider>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function useScrollController(): ScrollController {
|
|
77
|
+
const context = useContext(ScrollMonitorContext);
|
|
78
|
+
if (context == null) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
'"useScrollController" is used but no context provider was found in the React tree.',
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return context;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const getScrollPosition = (): ScrollPosition | null =>
|
|
87
|
+
ExecutionEnvironment.canUseDOM
|
|
88
|
+
? {
|
|
89
|
+
scrollX: window.pageXOffset,
|
|
90
|
+
scrollY: window.pageYOffset,
|
|
91
|
+
}
|
|
92
|
+
: null;
|
|
93
|
+
|
|
94
|
+
type ScrollPosition = {scrollX: number; scrollY: number};
|
|
95
|
+
|
|
96
|
+
export function useScrollPosition(
|
|
97
|
+
effect: (
|
|
98
|
+
position: ScrollPosition,
|
|
99
|
+
lastPosition: ScrollPosition | null,
|
|
100
|
+
) => void,
|
|
101
|
+
deps: unknown[] = [],
|
|
102
|
+
): void {
|
|
103
|
+
const {scrollEventsEnabledRef} = useScrollController();
|
|
104
|
+
const lastPositionRef = useRef<ScrollPosition | null>(getScrollPosition());
|
|
105
|
+
|
|
106
|
+
const dynamicEffect = useDynamicCallback(effect);
|
|
107
|
+
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
const handleScroll = () => {
|
|
110
|
+
if (!scrollEventsEnabledRef.current) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const currentPosition = getScrollPosition()!;
|
|
114
|
+
|
|
115
|
+
if (dynamicEffect) {
|
|
116
|
+
dynamicEffect(currentPosition, lastPositionRef.current);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
lastPositionRef.current = currentPosition;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const opts: AddEventListenerOptions & EventListenerOptions = {
|
|
123
|
+
passive: true,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
handleScroll();
|
|
127
|
+
window.addEventListener('scroll', handleScroll, opts);
|
|
128
|
+
|
|
129
|
+
return () => window.removeEventListener('scroll', handleScroll, opts);
|
|
130
|
+
}, [
|
|
131
|
+
dynamicEffect,
|
|
132
|
+
scrollEventsEnabledRef,
|
|
133
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
134
|
+
...deps,
|
|
135
|
+
]);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type UseScrollPositionSaver = {
|
|
139
|
+
/**
|
|
140
|
+
* Measure the top of an element, and store the details
|
|
141
|
+
*/
|
|
142
|
+
save: (elem: HTMLElement) => void;
|
|
143
|
+
/**
|
|
144
|
+
* Restore the page position to keep the stored element's position from
|
|
145
|
+
* the top of the viewport, and remove the stored details
|
|
146
|
+
*/
|
|
147
|
+
restore: () => {restored: boolean};
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
function useScrollPositionSaver(): UseScrollPositionSaver {
|
|
151
|
+
const lastElementRef = useRef<{elem: HTMLElement | null; top: number}>({
|
|
152
|
+
elem: null,
|
|
153
|
+
top: 0,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const save = useCallback((elem: HTMLElement) => {
|
|
157
|
+
lastElementRef.current = {
|
|
158
|
+
elem,
|
|
159
|
+
top: elem.getBoundingClientRect().top,
|
|
160
|
+
};
|
|
161
|
+
}, []);
|
|
162
|
+
|
|
163
|
+
const restore = useCallback(() => {
|
|
164
|
+
const {
|
|
165
|
+
current: {elem, top},
|
|
166
|
+
} = lastElementRef;
|
|
167
|
+
if (!elem) {
|
|
168
|
+
return {restored: false};
|
|
169
|
+
}
|
|
170
|
+
const newTop = elem.getBoundingClientRect().top;
|
|
171
|
+
const heightDiff = newTop - top;
|
|
172
|
+
if (heightDiff) {
|
|
173
|
+
window.scrollBy({left: 0, top: heightDiff});
|
|
174
|
+
}
|
|
175
|
+
lastElementRef.current = {elem: null, top: 0};
|
|
176
|
+
|
|
177
|
+
return {restored: heightDiff !== 0};
|
|
178
|
+
}, []);
|
|
179
|
+
|
|
180
|
+
return useMemo(() => ({save, restore}), [restore, save]);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
type UseScrollPositionBlockerReturn = {
|
|
184
|
+
blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* This hook permits to "block" the scroll position of a dom element
|
|
189
|
+
* The idea is that we should be able to update DOM content above this element
|
|
190
|
+
* but the screen position of this element should not change
|
|
191
|
+
*
|
|
192
|
+
* Feature motivated by the Tabs groups:
|
|
193
|
+
* clicking on a tab may affect tabs of the same group upper in the tree
|
|
194
|
+
* Yet to avoid a bad UX, the clicked tab must remain under the user mouse!
|
|
195
|
+
* See GIF here: https://github.com/facebook/docusaurus/pull/5618
|
|
196
|
+
*/
|
|
197
|
+
export function useScrollPositionBlocker(): UseScrollPositionBlockerReturn {
|
|
198
|
+
const scrollController = useScrollController();
|
|
199
|
+
const scrollPositionSaver = useScrollPositionSaver();
|
|
200
|
+
|
|
201
|
+
const nextLayoutEffectCallbackRef = useRef<(() => void) | undefined>(
|
|
202
|
+
undefined,
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const blockElementScrollPositionUntilNextRender = useCallback(
|
|
206
|
+
(el: HTMLElement) => {
|
|
207
|
+
scrollPositionSaver.save(el);
|
|
208
|
+
scrollController.disableScrollEvents();
|
|
209
|
+
nextLayoutEffectCallbackRef.current = () => {
|
|
210
|
+
const {restored} = scrollPositionSaver.restore();
|
|
211
|
+
nextLayoutEffectCallbackRef.current = undefined;
|
|
212
|
+
|
|
213
|
+
// Restoring the former scroll position will trigger a scroll event
|
|
214
|
+
// We need to wait for next scroll event to happen
|
|
215
|
+
// before enabling again the scrollController events
|
|
216
|
+
if (restored) {
|
|
217
|
+
const handleScrollRestoreEvent = () => {
|
|
218
|
+
scrollController.enableScrollEvents();
|
|
219
|
+
window.removeEventListener('scroll', handleScrollRestoreEvent);
|
|
220
|
+
};
|
|
221
|
+
window.addEventListener('scroll', handleScrollRestoreEvent);
|
|
222
|
+
} else {
|
|
223
|
+
scrollController.enableScrollEvents();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
},
|
|
227
|
+
[scrollController, scrollPositionSaver],
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
useLayoutEffect(() => {
|
|
231
|
+
nextLayoutEffectCallbackRef.current?.();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
return {
|
|
235
|
+
blockElementScrollPositionUntilNextRender,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
@@ -27,7 +27,7 @@ function getBrowserStorage(
|
|
|
27
27
|
try {
|
|
28
28
|
return window[storageType];
|
|
29
29
|
} catch (e) {
|
|
30
|
-
logOnceBrowserStorageNotAvailableWarning(e);
|
|
30
|
+
logOnceBrowserStorageNotAvailableWarning(e as Error);
|
|
31
31
|
return null;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -79,6 +79,10 @@ Please only call storage APIs in effects and event handlers.`);
|
|
|
79
79
|
|
|
80
80
|
/**
|
|
81
81
|
* Creates an object for accessing a particular key in localStorage.
|
|
82
|
+
* The API is fail-safe, and usage of browser storage should be considered unreliable
|
|
83
|
+
* Local storage might simply be unavailable (iframe + browser security) or operations might fail individually
|
|
84
|
+
* Please assume that using this API can be a NO-OP
|
|
85
|
+
* See also https://github.com/facebook/docusaurus/issues/6036
|
|
82
86
|
*/
|
|
83
87
|
export const createStorageSlot = (
|
|
84
88
|
key: string,
|
|
@@ -92,9 +96,28 @@ export const createStorageSlot = (
|
|
|
92
96
|
return NoopStorageSlot;
|
|
93
97
|
}
|
|
94
98
|
return {
|
|
95
|
-
get: () =>
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
get: () => {
|
|
100
|
+
try {
|
|
101
|
+
return browserStorage.getItem(key);
|
|
102
|
+
} catch (e) {
|
|
103
|
+
console.error(`Docusaurus storage error, can't get key=${key}`, e);
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
set: (value) => {
|
|
108
|
+
try {
|
|
109
|
+
browserStorage.setItem(key, value);
|
|
110
|
+
} catch (e) {
|
|
111
|
+
console.error(`Docusaurus storage error, can't set ${key}=${value}`, e);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
del: () => {
|
|
115
|
+
try {
|
|
116
|
+
browserStorage.removeItem(key);
|
|
117
|
+
} catch (e) {
|
|
118
|
+
console.error(`Docusaurus storage error, can't delete key=${key}`, e);
|
|
119
|
+
}
|
|
120
|
+
},
|
|
98
121
|
};
|
|
99
122
|
};
|
|
100
123
|
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {translate} from '@docusaurus/Translate';
|
|
9
|
+
|
|
10
|
+
export const translateTagsPageTitle = (): string =>
|
|
11
|
+
translate({
|
|
12
|
+
id: 'theme.tags.tagsPageTitle',
|
|
13
|
+
message: 'Tags',
|
|
14
|
+
description: 'The title of the tag list page',
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
type TagsListItem = Readonly<{name: string; permalink: string; count: number}>; // TODO remove duplicated type :s
|
|
18
|
+
|
|
19
|
+
export type TagLetterEntry = Readonly<{letter: string; tags: TagsListItem[]}>;
|
|
20
|
+
|
|
21
|
+
function getTagLetter(tag: string): string {
|
|
22
|
+
return tag[0].toUpperCase();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function listTagsByLetters(
|
|
26
|
+
tags: readonly TagsListItem[],
|
|
27
|
+
): TagLetterEntry[] {
|
|
28
|
+
// Group by letters
|
|
29
|
+
const groups: Record<string, TagsListItem[]> = {};
|
|
30
|
+
Object.values(tags).forEach((tag) => {
|
|
31
|
+
const letter = getTagLetter(tag.name);
|
|
32
|
+
groups[letter] = groups[letter] ?? [];
|
|
33
|
+
groups[letter].push(tag);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
Object.entries(groups)
|
|
38
|
+
// Sort letters
|
|
39
|
+
.sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
|
|
40
|
+
.map(([letter, letterTags]) => {
|
|
41
|
+
// Sort tags inside a letter
|
|
42
|
+
const sortedTags = letterTags.sort((tag1, tag2) =>
|
|
43
|
+
tag1.name.localeCompare(tag2.name),
|
|
44
|
+
);
|
|
45
|
+
return {letter, tags: sortedTags};
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {useMemo} from 'react';
|
|
9
|
+
import {TOCItem} from '@docusaurus/types';
|
|
10
|
+
|
|
11
|
+
type FilterTOCParam = {
|
|
12
|
+
toc: readonly TOCItem[];
|
|
13
|
+
minHeadingLevel: number;
|
|
14
|
+
maxHeadingLevel: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function filterTOC({
|
|
18
|
+
toc,
|
|
19
|
+
minHeadingLevel,
|
|
20
|
+
maxHeadingLevel,
|
|
21
|
+
}: FilterTOCParam): TOCItem[] {
|
|
22
|
+
function isValid(item: TOCItem) {
|
|
23
|
+
return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return toc.flatMap((item) => {
|
|
27
|
+
const filteredChildren = filterTOC({
|
|
28
|
+
toc: item.children,
|
|
29
|
+
minHeadingLevel,
|
|
30
|
+
maxHeadingLevel,
|
|
31
|
+
});
|
|
32
|
+
if (isValid(item)) {
|
|
33
|
+
return [
|
|
34
|
+
{
|
|
35
|
+
...item,
|
|
36
|
+
children: filteredChildren,
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
} else {
|
|
40
|
+
return filteredChildren;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Memoize potentially expensive filtering logic
|
|
46
|
+
export function useTOCFilter({
|
|
47
|
+
toc,
|
|
48
|
+
minHeadingLevel,
|
|
49
|
+
maxHeadingLevel,
|
|
50
|
+
}: FilterTOCParam): readonly TOCItem[] {
|
|
51
|
+
return useMemo(
|
|
52
|
+
() => filterTOC({toc, minHeadingLevel, maxHeadingLevel}),
|
|
53
|
+
[toc, minHeadingLevel, maxHeadingLevel],
|
|
54
|
+
);
|
|
55
|
+
}
|
|
@@ -11,7 +11,15 @@ import {useLocation} from '@docusaurus/router';
|
|
|
11
11
|
// Permits to obtain the url of the current page in another locale
|
|
12
12
|
// Useful to generate hreflang meta headers etc...
|
|
13
13
|
// See https://developers.google.com/search/docs/advanced/crawling/localized-versions
|
|
14
|
-
export function useAlternatePageUtils() {
|
|
14
|
+
export function useAlternatePageUtils(): {
|
|
15
|
+
createUrl: ({
|
|
16
|
+
locale,
|
|
17
|
+
fullyQualified,
|
|
18
|
+
}: {
|
|
19
|
+
locale: string;
|
|
20
|
+
fullyQualified: boolean;
|
|
21
|
+
}) => string;
|
|
22
|
+
} {
|
|
15
23
|
const {
|
|
16
24
|
siteConfig: {baseUrl, url},
|
|
17
25
|
i18n: {defaultLocale, currentLocale},
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {useAllDocsData, useActivePluginAndVersion} from '@theme/hooks/useDocs';
|
|
9
|
+
import {useDocsPreferredVersionByPluginId} from './docsPreferredVersion/useDocsPreferredVersion';
|
|
10
|
+
import {docVersionSearchTag, DEFAULT_SEARCH_TAG} from './searchUtils';
|
|
11
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
12
|
+
|
|
13
|
+
export type useContextualSearchFiltersReturns = {
|
|
14
|
+
locale: string;
|
|
15
|
+
tags: string[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// We may want to support multiple search engines, don't couple that to Algolia/DocSearch
|
|
19
|
+
// Maybe users will want to use its own search engine solution
|
|
20
|
+
export function useContextualSearchFilters(): useContextualSearchFiltersReturns {
|
|
21
|
+
const {i18n} = useDocusaurusContext();
|
|
22
|
+
const allDocsData = useAllDocsData();
|
|
23
|
+
const activePluginAndVersion = useActivePluginAndVersion();
|
|
24
|
+
const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
|
|
25
|
+
|
|
26
|
+
function getDocPluginTags(pluginId: string) {
|
|
27
|
+
const activeVersion =
|
|
28
|
+
activePluginAndVersion?.activePlugin?.pluginId === pluginId
|
|
29
|
+
? activePluginAndVersion.activeVersion
|
|
30
|
+
: undefined;
|
|
31
|
+
|
|
32
|
+
const preferredVersion = docsPreferredVersionByPluginId[pluginId];
|
|
33
|
+
|
|
34
|
+
const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast)!;
|
|
35
|
+
|
|
36
|
+
const version = activeVersion ?? preferredVersion ?? latestVersion;
|
|
37
|
+
|
|
38
|
+
return docVersionSearchTag(pluginId, version.name);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const tags = [
|
|
42
|
+
DEFAULT_SEARCH_TAG,
|
|
43
|
+
...Object.keys(allDocsData).map(getDocPluginTags),
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
locale: i18n.currentLocale,
|
|
48
|
+
tags,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
9
|
+
import {useLocation} from '@docusaurus/router';
|
|
10
|
+
|
|
11
|
+
// Get the pathname of current route, without the optional site baseUrl
|
|
12
|
+
// - /docs/myDoc => /docs/myDoc
|
|
13
|
+
// - /baseUrl/docs/myDoc => /docs/myDoc
|
|
14
|
+
export function useLocalPathname(): string {
|
|
15
|
+
const {
|
|
16
|
+
siteConfig: {baseUrl},
|
|
17
|
+
} = useDocusaurusContext();
|
|
18
|
+
const {pathname} = useLocation();
|
|
19
|
+
return pathname.replace(baseUrl, '/');
|
|
20
|
+
}
|