@docusaurus/theme-common 2.0.0-beta.ff31de0ff → 2.0.0-rc.1
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/lib/components/Collapsible/index.d.ts +64 -0
- package/lib/components/Collapsible/index.d.ts.map +1 -0
- package/lib/components/Collapsible/index.js +150 -0
- package/lib/components/Collapsible/index.js.map +1 -0
- package/lib/components/Details/index.d.ts +17 -0
- package/lib/components/Details/index.d.ts.map +1 -0
- package/lib/components/Details/index.js +76 -0
- package/lib/components/Details/index.js.map +1 -0
- package/lib/components/Details/styles.module.css +62 -0
- package/lib/contexts/announcementBar.d.ts +22 -0
- package/lib/contexts/announcementBar.d.ts.map +1 -0
- package/lib/contexts/announcementBar.js +72 -0
- package/lib/contexts/announcementBar.js.map +1 -0
- package/lib/contexts/blogPost.d.ts +33 -0
- package/lib/contexts/blogPost.d.ts.map +1 -0
- package/lib/contexts/blogPost.js +46 -0
- package/lib/contexts/blogPost.js.map +1 -0
- package/lib/contexts/colorMode.d.ts +27 -0
- package/lib/contexts/colorMode.d.ts.map +1 -0
- package/lib/contexts/colorMode.js +132 -0
- package/lib/contexts/colorMode.js.map +1 -0
- package/lib/contexts/doc.d.ts +30 -0
- package/lib/contexts/doc.d.ts.map +1 -0
- package/lib/contexts/doc.js +48 -0
- package/lib/contexts/doc.js.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts +31 -0
- package/lib/contexts/docSidebarItemsExpandedState.d.ts.map +1 -0
- package/lib/contexts/docSidebarItemsExpandedState.js +28 -0
- package/lib/contexts/docSidebarItemsExpandedState.js.map +1 -0
- package/lib/contexts/docsPreferredVersion.d.ts +30 -0
- package/lib/contexts/docsPreferredVersion.d.ts.map +1 -0
- package/lib/contexts/docsPreferredVersion.js +130 -0
- package/lib/contexts/docsPreferredVersion.js.map +1 -0
- package/lib/contexts/docsSidebar.d.ts +26 -0
- package/lib/contexts/docsSidebar.d.ts.map +1 -0
- package/lib/contexts/docsSidebar.js +30 -0
- package/lib/contexts/docsSidebar.js.map +1 -0
- package/lib/contexts/docsVersion.d.ts +20 -0
- package/lib/contexts/docsVersion.d.ts.map +1 -0
- package/lib/contexts/docsVersion.js +26 -0
- package/lib/contexts/docsVersion.js.map +1 -0
- package/lib/contexts/navbarMobileSidebar.d.ts +31 -0
- package/lib/contexts/navbarMobileSidebar.d.ts.map +1 -0
- package/lib/contexts/navbarMobileSidebar.js +56 -0
- package/lib/contexts/navbarMobileSidebar.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts +37 -0
- package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/content.js +50 -0
- package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
- package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
- package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
- package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
- package/lib/contexts/tabGroupChoice.d.ts +21 -0
- package/lib/contexts/tabGroupChoice.d.ts.map +1 -0
- package/lib/contexts/tabGroupChoice.js +49 -0
- package/lib/contexts/tabGroupChoice.js.map +1 -0
- package/lib/{utils/useChangeRoute.d.ts → hooks/styles.css} +4 -1
- package/lib/hooks/useBackToTopButton.d.ts +27 -0
- package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
- package/lib/hooks/useBackToTopButton.js +50 -0
- package/lib/hooks/useBackToTopButton.js.map +1 -0
- package/lib/hooks/useCodeWordWrap.d.ts +14 -0
- package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
- package/lib/hooks/useCodeWordWrap.js +67 -0
- package/lib/hooks/useCodeWordWrap.js.map +1 -0
- package/lib/hooks/useHideableNavbar.d.ts +17 -0
- package/lib/hooks/useHideableNavbar.d.ts.map +1 -0
- package/lib/hooks/useHideableNavbar.js +60 -0
- package/lib/hooks/useHideableNavbar.js.map +1 -0
- package/lib/hooks/useKeyboardNavigation.d.ts +20 -0
- package/lib/hooks/useKeyboardNavigation.d.ts.map +1 -0
- package/lib/hooks/useKeyboardNavigation.js +39 -0
- package/lib/hooks/useKeyboardNavigation.js.map +1 -0
- package/lib/hooks/useLockBodyScroll.d.ts +12 -0
- package/lib/hooks/useLockBodyScroll.d.ts.map +1 -0
- package/lib/hooks/useLockBodyScroll.js +20 -0
- package/lib/hooks/useLockBodyScroll.js.map +1 -0
- package/lib/hooks/useMutationObserver.d.ts +4 -0
- package/lib/hooks/useMutationObserver.d.ts.map +1 -0
- package/lib/hooks/useMutationObserver.js +29 -0
- package/lib/hooks/useMutationObserver.js.map +1 -0
- package/lib/hooks/usePrismTheme.d.ts +13 -0
- package/lib/hooks/usePrismTheme.d.ts.map +1 -0
- package/lib/hooks/usePrismTheme.js +21 -0
- package/lib/hooks/usePrismTheme.js.map +1 -0
- package/lib/hooks/useSearchPage.d.ts +25 -0
- package/lib/hooks/useSearchPage.d.ts.map +1 -0
- package/lib/hooks/useSearchPage.js +43 -0
- package/lib/hooks/useSearchPage.js.map +1 -0
- package/lib/hooks/useSkipToContent.d.ts +25 -0
- package/lib/hooks/useSkipToContent.d.ts.map +1 -0
- package/lib/hooks/useSkipToContent.js +35 -0
- package/lib/hooks/useSkipToContent.js.map +1 -0
- package/lib/hooks/useTOCHighlight.d.ts +25 -0
- package/lib/hooks/useTOCHighlight.d.ts.map +1 -0
- package/lib/hooks/useTOCHighlight.js +130 -0
- package/lib/hooks/useTOCHighlight.js.map +1 -0
- package/lib/hooks/useWindowSize.d.ts +28 -0
- package/lib/hooks/useWindowSize.d.ts.map +1 -0
- package/lib/hooks/useWindowSize.js +59 -0
- package/lib/hooks/useWindowSize.js.map +1 -0
- package/lib/index.d.ts +16 -11
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +23 -10
- package/lib/index.js.map +1 -0
- package/lib/internal.d.ts +41 -0
- package/lib/internal.d.ts.map +1 -0
- package/lib/internal.js +52 -0
- package/lib/internal.js.map +1 -0
- package/lib/utils/ThemeClassNames.d.ts +49 -12
- package/lib/utils/ThemeClassNames.d.ts.map +1 -0
- package/lib/utils/ThemeClassNames.js +47 -4
- package/lib/utils/ThemeClassNames.js.map +1 -0
- package/lib/utils/codeBlockUtils.d.ts +63 -0
- package/lib/utils/codeBlockUtils.d.ts.map +1 -0
- package/lib/utils/codeBlockUtils.js +159 -3
- package/lib/utils/codeBlockUtils.js.map +1 -0
- package/lib/utils/docsUtils.d.ts +91 -0
- package/lib/utils/docsUtils.d.ts.map +1 -0
- package/lib/utils/docsUtils.js +217 -1
- package/lib/utils/docsUtils.js.map +1 -0
- package/lib/utils/footerUtils.d.ts +13 -0
- package/lib/utils/footerUtils.d.ts.map +1 -0
- package/lib/utils/footerUtils.js +14 -0
- package/lib/utils/footerUtils.js.map +1 -0
- package/lib/utils/generalUtils.d.ts +11 -1
- package/lib/utils/generalUtils.d.ts.map +1 -0
- package/lib/utils/generalUtils.js +9 -5
- package/lib/utils/generalUtils.js.map +1 -0
- package/lib/utils/historyUtils.d.ts +17 -0
- package/lib/utils/historyUtils.d.ts.map +1 -0
- package/lib/utils/historyUtils.js +38 -0
- package/lib/utils/historyUtils.js.map +1 -0
- package/lib/utils/jsUtils.d.ts +23 -0
- package/lib/utils/jsUtils.d.ts.map +1 -0
- package/lib/utils/jsUtils.js +29 -0
- package/lib/utils/jsUtils.js.map +1 -0
- package/lib/utils/metadataUtils.d.ts +38 -0
- package/lib/utils/metadataUtils.d.ts.map +1 -0
- package/lib/utils/metadataUtils.js +70 -0
- package/lib/utils/metadataUtils.js.map +1 -0
- package/lib/utils/navbarUtils.d.ts +21 -0
- package/lib/utils/navbarUtils.d.ts.map +1 -0
- package/lib/utils/navbarUtils.js +36 -0
- package/lib/utils/navbarUtils.js.map +1 -0
- package/lib/utils/reactUtils.d.ts +69 -0
- package/lib/utils/reactUtils.d.ts.map +1 -0
- package/lib/utils/reactUtils.js +98 -0
- package/lib/utils/reactUtils.js.map +1 -0
- package/lib/utils/regexpUtils.d.ts +12 -0
- package/lib/utils/regexpUtils.d.ts.map +1 -0
- package/lib/utils/regexpUtils.js +18 -0
- package/lib/utils/regexpUtils.js.map +1 -0
- package/lib/utils/routesUtils.d.ts +26 -0
- package/lib/utils/routesUtils.d.ts.map +1 -0
- package/lib/utils/routesUtils.js +54 -0
- package/lib/utils/routesUtils.js.map +1 -0
- package/lib/utils/scrollUtils.d.ts +83 -0
- package/lib/utils/scrollUtils.d.ts.map +1 -0
- package/lib/utils/scrollUtils.js +200 -0
- package/lib/utils/scrollUtils.js.map +1 -0
- package/lib/utils/searchUtils.d.ts +13 -0
- package/lib/utils/searchUtils.d.ts.map +1 -0
- package/lib/utils/searchUtils.js +37 -0
- package/lib/utils/searchUtils.js.map +1 -0
- package/lib/utils/storageUtils.d.ts +15 -7
- package/lib/utils/storageUtils.d.ts.map +1 -0
- package/lib/utils/storageUtils.js +58 -25
- package/lib/utils/storageUtils.js.map +1 -0
- package/lib/utils/tagsUtils.d.ts +18 -0
- package/lib/utils/tagsUtils.d.ts.map +1 -0
- package/lib/utils/tagsUtils.js +36 -0
- package/lib/utils/tagsUtils.js.map +1 -0
- package/lib/utils/tocUtils.d.ts +36 -0
- package/lib/utils/tocUtils.d.ts.map +1 -0
- package/lib/utils/tocUtils.js +84 -0
- package/lib/utils/tocUtils.js.map +1 -0
- package/lib/utils/useAlternatePageUtils.d.ts +21 -1
- package/lib/utils/useAlternatePageUtils.d.ts.map +1 -0
- package/lib/utils/useAlternatePageUtils.js +9 -4
- package/lib/utils/useAlternatePageUtils.js.map +1 -0
- package/lib/utils/useLocalPathname.d.ts +13 -0
- package/lib/utils/useLocalPathname.d.ts.map +1 -0
- package/lib/utils/useLocalPathname.js +19 -0
- package/lib/utils/useLocalPathname.js.map +1 -0
- package/lib/utils/useLocationChange.d.ts +17 -0
- package/lib/utils/useLocationChange.d.ts.map +1 -0
- package/lib/utils/useLocationChange.js +31 -0
- package/lib/utils/useLocationChange.js.map +1 -0
- package/lib/utils/usePluralForm.d.ts +12 -0
- package/lib/utils/usePluralForm.d.ts.map +1 -0
- package/lib/utils/usePluralForm.js +36 -37
- package/lib/utils/usePluralForm.js.map +1 -0
- package/lib/utils/useThemeConfig.d.ts +56 -27
- package/lib/utils/useThemeConfig.d.ts.map +1 -0
- package/lib/utils/useThemeConfig.js +4 -0
- package/lib/utils/useThemeConfig.js.map +1 -0
- package/package.json +35 -13
- package/src/components/Collapsible/index.tsx +263 -0
- package/src/components/Details/index.tsx +109 -0
- package/src/components/Details/styles.module.css +62 -0
- package/src/contexts/announcementBar.tsx +119 -0
- package/src/contexts/blogPost.tsx +80 -0
- package/src/contexts/colorMode.tsx +198 -0
- package/src/contexts/doc.tsx +71 -0
- package/src/contexts/docSidebarItemsExpandedState.tsx +55 -0
- package/src/contexts/docsPreferredVersion.tsx +251 -0
- package/src/contexts/docsSidebar.tsx +50 -0
- package/src/contexts/docsVersion.tsx +36 -0
- package/src/contexts/navbarMobileSidebar.tsx +99 -0
- package/src/contexts/navbarSecondaryMenu/content.tsx +100 -0
- package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
- package/src/contexts/tabGroupChoice.tsx +85 -0
- package/{lib/utils/pathUtils.d.ts → src/hooks/styles.css} +4 -1
- package/src/hooks/useBackToTopButton.ts +73 -0
- package/src/hooks/useCodeWordWrap.ts +105 -0
- package/src/hooks/useHideableNavbar.ts +75 -0
- package/src/hooks/useKeyboardNavigation.ts +45 -0
- package/src/hooks/useLockBodyScroll.ts +21 -0
- package/src/hooks/useMutationObserver.ts +38 -0
- package/src/hooks/usePrismTheme.ts +24 -0
- package/src/hooks/useSearchPage.ts +79 -0
- package/src/hooks/useSkipToContent.ts +58 -0
- package/src/hooks/useTOCHighlight.ts +189 -0
- package/src/hooks/useWindowSize.ts +72 -0
- package/src/index.ts +55 -23
- package/src/internal.ts +122 -0
- package/src/types.d.ts +0 -2
- package/src/utils/ThemeClassNames.ts +54 -5
- package/src/utils/codeBlockUtils.ts +241 -2
- package/src/utils/docsUtils.tsx +334 -0
- package/src/utils/footerUtils.ts +18 -0
- package/src/utils/generalUtils.ts +9 -5
- package/src/utils/historyUtils.ts +45 -0
- package/src/utils/jsUtils.ts +36 -0
- package/src/utils/metadataUtils.tsx +115 -0
- package/src/utils/navbarUtils.tsx +45 -0
- package/src/utils/reactUtils.tsx +129 -0
- package/src/utils/regexpUtils.ts +24 -0
- package/src/utils/routesUtils.ts +75 -0
- package/src/utils/scrollUtils.tsx +304 -0
- package/src/utils/searchUtils.ts +51 -0
- package/src/utils/storageUtils.ts +59 -26
- package/src/utils/tagsUtils.ts +50 -0
- package/src/utils/tocUtils.ts +119 -0
- package/src/utils/useAlternatePageUtils.ts +28 -7
- package/src/utils/useLocalPathname.ts +22 -0
- package/src/utils/useLocationChange.ts +41 -0
- package/src/utils/usePluralForm.ts +53 -39
- package/src/utils/useThemeConfig.ts +55 -29
- package/lib/.tsbuildinfo +0 -4115
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.d.ts +0 -21
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +0 -94
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.d.ts +0 -13
- package/lib/utils/docsPreferredVersion/DocsPreferredVersionStorage.js +0 -20
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +0 -5
- package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +0 -41
- package/lib/utils/pathUtils.js +0 -13
- package/lib/utils/useChangeRoute.js +0 -18
- package/src/utils/__tests__/codeBlockUtils.test.ts +0 -54
- package/src/utils/__tests__/pathUtils.test.ts +0 -32
- package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +0 -165
- package/src/utils/docsPreferredVersion/DocsPreferredVersionStorage.ts +0 -34
- package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +0 -66
- package/src/utils/docsUtils.ts +0 -11
- package/src/utils/pathUtils.ts +0 -17
- package/src/utils/useChangeRoute.ts +0 -21
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,200 @@
|
|
|
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
|
+
import React, { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, } from 'react';
|
|
8
|
+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
9
|
+
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
10
|
+
import { useEvent, ReactContextError } from './reactUtils';
|
|
11
|
+
function useScrollControllerContextValue() {
|
|
12
|
+
const scrollEventsEnabledRef = useRef(true);
|
|
13
|
+
return useMemo(() => ({
|
|
14
|
+
scrollEventsEnabledRef,
|
|
15
|
+
enableScrollEvents: () => {
|
|
16
|
+
scrollEventsEnabledRef.current = true;
|
|
17
|
+
},
|
|
18
|
+
disableScrollEvents: () => {
|
|
19
|
+
scrollEventsEnabledRef.current = false;
|
|
20
|
+
},
|
|
21
|
+
}), []);
|
|
22
|
+
}
|
|
23
|
+
const ScrollMonitorContext = React.createContext(undefined);
|
|
24
|
+
export function ScrollControllerProvider({ children, }) {
|
|
25
|
+
const value = useScrollControllerContextValue();
|
|
26
|
+
return (<ScrollMonitorContext.Provider value={value}>
|
|
27
|
+
{children}
|
|
28
|
+
</ScrollMonitorContext.Provider>);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* We need a way to update the scroll position while ignoring scroll events
|
|
32
|
+
* so as not to toggle Navbar/BackToTop visibility.
|
|
33
|
+
*
|
|
34
|
+
* This API permits to temporarily disable/ignore scroll events. Motivated by
|
|
35
|
+
* https://github.com/facebook/docusaurus/pull/5618
|
|
36
|
+
*/
|
|
37
|
+
export function useScrollController() {
|
|
38
|
+
const context = useContext(ScrollMonitorContext);
|
|
39
|
+
if (context == null) {
|
|
40
|
+
throw new ReactContextError('ScrollControllerProvider');
|
|
41
|
+
}
|
|
42
|
+
return context;
|
|
43
|
+
}
|
|
44
|
+
const getScrollPosition = () => ExecutionEnvironment.canUseDOM
|
|
45
|
+
? {
|
|
46
|
+
scrollX: window.pageXOffset,
|
|
47
|
+
scrollY: window.pageYOffset,
|
|
48
|
+
}
|
|
49
|
+
: null;
|
|
50
|
+
/**
|
|
51
|
+
* This hook fires an effect when the scroll position changes. The effect will
|
|
52
|
+
* be provided with the before/after scroll positions. Note that the effect may
|
|
53
|
+
* not be always run: if scrolling is disabled through `useScrollController`, it
|
|
54
|
+
* will be a no-op.
|
|
55
|
+
*
|
|
56
|
+
* @see {@link useScrollController}
|
|
57
|
+
*/
|
|
58
|
+
export function useScrollPosition(effect, deps = []) {
|
|
59
|
+
const { scrollEventsEnabledRef } = useScrollController();
|
|
60
|
+
const lastPositionRef = useRef(getScrollPosition());
|
|
61
|
+
const dynamicEffect = useEvent(effect);
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
const handleScroll = () => {
|
|
64
|
+
if (!scrollEventsEnabledRef.current) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const currentPosition = getScrollPosition();
|
|
68
|
+
dynamicEffect(currentPosition, lastPositionRef.current);
|
|
69
|
+
lastPositionRef.current = currentPosition;
|
|
70
|
+
};
|
|
71
|
+
const opts = {
|
|
72
|
+
passive: true,
|
|
73
|
+
};
|
|
74
|
+
handleScroll();
|
|
75
|
+
window.addEventListener('scroll', handleScroll, opts);
|
|
76
|
+
return () => window.removeEventListener('scroll', handleScroll, opts);
|
|
77
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
78
|
+
}, [dynamicEffect, scrollEventsEnabledRef, ...deps]);
|
|
79
|
+
}
|
|
80
|
+
function useScrollPositionSaver() {
|
|
81
|
+
const lastElementRef = useRef({
|
|
82
|
+
elem: null,
|
|
83
|
+
top: 0,
|
|
84
|
+
});
|
|
85
|
+
const save = useCallback((elem) => {
|
|
86
|
+
lastElementRef.current = {
|
|
87
|
+
elem,
|
|
88
|
+
top: elem.getBoundingClientRect().top,
|
|
89
|
+
};
|
|
90
|
+
}, []);
|
|
91
|
+
const restore = useCallback(() => {
|
|
92
|
+
const { current: { elem, top }, } = lastElementRef;
|
|
93
|
+
if (!elem) {
|
|
94
|
+
return { restored: false };
|
|
95
|
+
}
|
|
96
|
+
const newTop = elem.getBoundingClientRect().top;
|
|
97
|
+
const heightDiff = newTop - top;
|
|
98
|
+
if (heightDiff) {
|
|
99
|
+
window.scrollBy({ left: 0, top: heightDiff });
|
|
100
|
+
}
|
|
101
|
+
lastElementRef.current = { elem: null, top: 0 };
|
|
102
|
+
return { restored: heightDiff !== 0 };
|
|
103
|
+
}, []);
|
|
104
|
+
return useMemo(() => ({ save, restore }), [restore, save]);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* This hook permits to "block" the scroll position of a DOM element.
|
|
108
|
+
* The idea is that we should be able to update DOM content above this element
|
|
109
|
+
* but the screen position of this element should not change.
|
|
110
|
+
*
|
|
111
|
+
* Feature motivated by the Tabs groups: clicking on a tab may affect tabs of
|
|
112
|
+
* the same group upper in the tree, yet to avoid a bad UX, the clicked tab must
|
|
113
|
+
* remain under the user mouse.
|
|
114
|
+
*
|
|
115
|
+
* @see https://github.com/facebook/docusaurus/pull/5618
|
|
116
|
+
*/
|
|
117
|
+
export function useScrollPositionBlocker() {
|
|
118
|
+
const scrollController = useScrollController();
|
|
119
|
+
const scrollPositionSaver = useScrollPositionSaver();
|
|
120
|
+
const nextLayoutEffectCallbackRef = useRef(undefined);
|
|
121
|
+
const blockElementScrollPositionUntilNextRender = useCallback((el) => {
|
|
122
|
+
scrollPositionSaver.save(el);
|
|
123
|
+
scrollController.disableScrollEvents();
|
|
124
|
+
nextLayoutEffectCallbackRef.current = () => {
|
|
125
|
+
const { restored } = scrollPositionSaver.restore();
|
|
126
|
+
nextLayoutEffectCallbackRef.current = undefined;
|
|
127
|
+
// Restoring the former scroll position will trigger a scroll event. We
|
|
128
|
+
// need to wait for next scroll event to happen before enabling the
|
|
129
|
+
// scrollController events again.
|
|
130
|
+
if (restored) {
|
|
131
|
+
const handleScrollRestoreEvent = () => {
|
|
132
|
+
scrollController.enableScrollEvents();
|
|
133
|
+
window.removeEventListener('scroll', handleScrollRestoreEvent);
|
|
134
|
+
};
|
|
135
|
+
window.addEventListener('scroll', handleScrollRestoreEvent);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
scrollController.enableScrollEvents();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}, [scrollController, scrollPositionSaver]);
|
|
142
|
+
useLayoutEffect(() => {
|
|
143
|
+
nextLayoutEffectCallbackRef.current?.();
|
|
144
|
+
});
|
|
145
|
+
return {
|
|
146
|
+
blockElementScrollPositionUntilNextRender,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function smoothScrollNative(top) {
|
|
150
|
+
window.scrollTo({ top, behavior: 'smooth' });
|
|
151
|
+
return () => {
|
|
152
|
+
// Nothing to cancel, it's natively cancelled if user tries to scroll down
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function smoothScrollPolyfill(top) {
|
|
156
|
+
let raf = null;
|
|
157
|
+
const isUpScroll = document.documentElement.scrollTop > top;
|
|
158
|
+
function rafRecursion() {
|
|
159
|
+
const currentScroll = document.documentElement.scrollTop;
|
|
160
|
+
if ((isUpScroll && currentScroll > top) ||
|
|
161
|
+
(!isUpScroll && currentScroll < top)) {
|
|
162
|
+
raf = requestAnimationFrame(rafRecursion);
|
|
163
|
+
window.scrollTo(0, Math.floor((currentScroll - top) * 0.85) + top);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
rafRecursion();
|
|
167
|
+
// Break the recursion. Prevents the user from "fighting" against that
|
|
168
|
+
// recursion producing a weird UX
|
|
169
|
+
return () => raf && cancelAnimationFrame(raf);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* A "smart polyfill" of `window.scrollTo({ top, behavior: "smooth" })`.
|
|
173
|
+
* This currently always uses a polyfilled implementation unless
|
|
174
|
+
* `scroll-behavior: smooth` has been set in CSS, because native support
|
|
175
|
+
* detection for scroll behavior seems unreliable.
|
|
176
|
+
*
|
|
177
|
+
* This hook does not do anything by itself: it returns a start and a stop
|
|
178
|
+
* handle. You can execute either handle at any time.
|
|
179
|
+
*/
|
|
180
|
+
export function useSmoothScrollTo() {
|
|
181
|
+
const cancelRef = useRef(null);
|
|
182
|
+
const isBrowser = useIsBrowser();
|
|
183
|
+
// Not all have support for smooth scrolling (particularly Safari mobile iOS)
|
|
184
|
+
// TODO proper detection is currently unreliable!
|
|
185
|
+
// see https://github.com/wessberg/scroll-behavior-polyfill/issues/16
|
|
186
|
+
// For now, we only use native scroll behavior if smooth is already set,
|
|
187
|
+
// because otherwise the polyfill produces a weird UX when both CSS and JS try
|
|
188
|
+
// to scroll a page, and they cancel each other.
|
|
189
|
+
const supportsNativeSmoothScrolling = isBrowser &&
|
|
190
|
+
getComputedStyle(document.documentElement).scrollBehavior === 'smooth';
|
|
191
|
+
return {
|
|
192
|
+
startScroll: (top) => {
|
|
193
|
+
cancelRef.current = supportsNativeSmoothScrolling
|
|
194
|
+
? smoothScrollNative(top)
|
|
195
|
+
: smoothScrollPolyfill(top);
|
|
196
|
+
},
|
|
197
|
+
cancelScroll: () => cancelRef.current?.(),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=scrollUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scrollUtils.js","sourceRoot":"","sources":["../../src/utils/scrollUtils.tsx"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,EACZ,WAAW,EACX,UAAU,EACV,SAAS,EACT,eAAe,EACf,OAAO,EACP,MAAM,GAEP,MAAM,OAAO,CAAC;AACf,OAAO,oBAAoB,MAAM,kCAAkC,CAAC;AACpE,OAAO,Y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Y,CAAC,CAAC;YAC1C,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,aAAa,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC;SACpE;IACH,CAAC;IACD,YAAY,EAAE,CAAC;IAEf,sEAAsE;IACtE,iCAAiC;IACjC,OAAO,GAAG,EAAE,CAAC,GAAG,IAAI,oBAAoB,CAAC,GAAG,CAAC,CAAC;AAChD,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB;IAc/B,MAAM,SAAS,GAAG,MAAM,CAAyB,IAAI,CAAC,CAAC;IACvD,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IACjC,6EAA6E;IAC7E,iDAAiD;IACjD,qEAAqE;IACrE,wEAAwE;IACxE,8EAA8E;IAC9E,gDAAgD;IAChD,MAAM,6BAA6B,GACjC,SAAS;QACT,gBAAgB,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC,cAAc,KAAK,QAAQ,CAAC;IACzE,OAAO;QACL,WAAW,EAAE,CAAC,GAAW,EAAE,EAAE;YAC3B,SAAS,CAAC,OAAO,GAAG,6BAA6B;gBAC/C,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC;gBACzB,CAAC,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAC;QAChC,CAAC;QACD,YAAY,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE;KAC1C,CAAC;AACJ,CAAC"}
|
|
@@ -5,4 +5,17 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
export declare const DEFAULT_SEARCH_TAG = "default";
|
|
8
|
+
/** The search tag to append as each doc's metadata. */
|
|
8
9
|
export declare function docVersionSearchTag(pluginId: string, versionName: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Gets the relevant context information for contextual search.
|
|
12
|
+
*
|
|
13
|
+
* The value is generic and not coupled to Algolia/DocSearch, since we may want
|
|
14
|
+
* to support multiple search engines, or allowing users to use their own search
|
|
15
|
+
* engine solution.
|
|
16
|
+
*/
|
|
17
|
+
export declare function useContextualSearchFilters(): {
|
|
18
|
+
locale: string;
|
|
19
|
+
tags: string[];
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=searchUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"searchUtils.d.ts","sourceRoot":"","sources":["../../src/utils/searchUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AASH,eAAO,MAAM,kBAAkB,YAAY,CAAC;AAE5C,uDAAuD;AACvD,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,MAAM,EAChB,WAAW,EAAE,MAAM,GAClB,MAAM,CAER;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,IAAI;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAA;CAAC,CAkC7E"}
|
package/lib/utils/searchUtils.js
CHANGED
|
@@ -4,7 +4,44 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
import { useAllDocsData, useActivePluginAndVersion, } from '@docusaurus/plugin-content-docs/client';
|
|
8
|
+
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
|
9
|
+
import { useDocsPreferredVersionByPluginId } from '../contexts/docsPreferredVersion';
|
|
7
10
|
export const DEFAULT_SEARCH_TAG = 'default';
|
|
11
|
+
/** The search tag to append as each doc's metadata. */
|
|
8
12
|
export function docVersionSearchTag(pluginId, versionName) {
|
|
9
13
|
return `docs-${pluginId}-${versionName}`;
|
|
10
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Gets the relevant context information for contextual search.
|
|
17
|
+
*
|
|
18
|
+
* The value is generic and not coupled to Algolia/DocSearch, since we may want
|
|
19
|
+
* to support multiple search engines, or allowing users to use their own search
|
|
20
|
+
* engine solution.
|
|
21
|
+
*/
|
|
22
|
+
export function useContextualSearchFilters() {
|
|
23
|
+
const { i18n } = useDocusaurusContext();
|
|
24
|
+
const allDocsData = useAllDocsData();
|
|
25
|
+
const activePluginAndVersion = useActivePluginAndVersion();
|
|
26
|
+
const docsPreferredVersionByPluginId = useDocsPreferredVersionByPluginId();
|
|
27
|
+
// This can't use more specialized hooks because we are mapping over all
|
|
28
|
+
// plugin instances.
|
|
29
|
+
function getDocPluginTags(pluginId) {
|
|
30
|
+
const activeVersion = activePluginAndVersion?.activePlugin.pluginId === pluginId
|
|
31
|
+
? activePluginAndVersion.activeVersion
|
|
32
|
+
: undefined;
|
|
33
|
+
const preferredVersion = docsPreferredVersionByPluginId[pluginId];
|
|
34
|
+
const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast);
|
|
35
|
+
const version = activeVersion ?? preferredVersion ?? latestVersion;
|
|
36
|
+
return docVersionSearchTag(pluginId, version.name);
|
|
37
|
+
}
|
|
38
|
+
const tags = [
|
|
39
|
+
DEFAULT_SEARCH_TAG,
|
|
40
|
+
...Object.keys(allDocsData).map(getDocPluginTags),
|
|
41
|
+
];
|
|
42
|
+
return {
|
|
43
|
+
locale: i18n.currentLocale,
|
|
44
|
+
tags,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=searchUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"searchUtils.js","sourceRoot":"","sources":["../../src/utils/searchUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,cAAc,EACd,yBAAyB,GAC1B,MAAM,wCAAwC,CAAC;AAChD,OAAO,oBAAoB,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAC,iCAAiC,EAAC,MAAM,kCAAkC,CAAC;AAEnF,MAAM,CAAC,MAAM,kBAAkB,GAAG,SAAS,CAAC;AAE5C,uDAAuD;AACvD,MAAM,UAAU,mBAAmB,CACjC,QAAgB,EAChB,WAAmB;IAEnB,OAAO,QAAQ,QAAQ,IAAI,WAAW,EAAE,CAAC;AAC3C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,0BAA0B;IACxC,MAAM,EAAC,IAAI,EAAC,GAAG,oBAAoB,EAAE,CAAC;IACtC,MAAM,WAAW,GAAG,cAAc,EAAE,CAAC;IACrC,MAAM,sBAAsB,GAAG,yBAAyB,EAAE,CAAC;IAC3D,MAAM,8BAA8B,GAAG,iCAAiC,EAAE,CAAC;IAE3E,wEAAwE;IACxE,oBAAoB;IACpB,SAAS,gBAAgB,CAAC,QAAgB;QACxC,MAAM,aAAa,GACjB,sBAAsB,EAAE,YAAY,CAAC,QAAQ,KAAK,QAAQ;YACxD,CAAC,CAAC,sBAAsB,CAAC,aAAa;YACtC,CAAC,CAAC,SAAS,CAAC;QAEhB,MAAM,gBAAgB,GAAG,8BAA8B,CAAC,QAAQ,CAAC,CAAC;QAElE,MAAM,aAAa,GAAG,WAAW,CAAC,QAAQ,CAAE,CAAC,QAAQ,CAAC,IAAI,CACxD,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CACf,CAAC;QAEH,MAAM,OAAO,GAAG,aAAa,IAAI,gBAAgB,IAAI,aAAa,CAAC;QAEnE,OAAO,mBAAmB,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACrD,CAAC;IAED,MAAM,IAAI,GAAG;QACX,kBAAkB;QAClB,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC,gBAAgB,CAAC;KAClD,CAAC;IAEF,OAAO;QACL,MAAM,EAAE,IAAI,CAAC,aAAa;QAC1B,IAAI;KACL,CAAC;AACJ,CAAC"}
|
|
@@ -6,20 +6,28 @@
|
|
|
6
6
|
*/
|
|
7
7
|
declare const StorageTypes: readonly ["localStorage", "sessionStorage", "none"];
|
|
8
8
|
export declare type StorageType = typeof StorageTypes[number];
|
|
9
|
-
export
|
|
9
|
+
export declare type StorageSlot = {
|
|
10
10
|
get: () => string | null;
|
|
11
11
|
set: (value: string) => void;
|
|
12
12
|
del: () => void;
|
|
13
|
-
}
|
|
13
|
+
};
|
|
14
14
|
/**
|
|
15
|
-
* Creates an
|
|
15
|
+
* Creates an interface to work on a particular key in the storage model.
|
|
16
|
+
* Note that this function only initializes the interface, but doesn't allocate
|
|
17
|
+
* anything by itself (i.e. no side-effects).
|
|
18
|
+
*
|
|
19
|
+
* The API is fail-safe, since usage of browser storage should be considered
|
|
20
|
+
* unreliable. Local storage might simply be unavailable (iframe + browser
|
|
21
|
+
* security) or operations might fail individually. Please assume that using
|
|
22
|
+
* this API can be a no-op. See also https://github.com/facebook/docusaurus/issues/6036
|
|
16
23
|
*/
|
|
17
|
-
export declare
|
|
18
|
-
persistence?:
|
|
19
|
-
}
|
|
24
|
+
export declare function createStorageSlot(key: string, options?: {
|
|
25
|
+
persistence?: StorageType;
|
|
26
|
+
}): StorageSlot;
|
|
20
27
|
/**
|
|
21
|
-
* Returns a list of all the keys currently stored in browser storage
|
|
28
|
+
* Returns a list of all the keys currently stored in browser storage,
|
|
22
29
|
* or an empty list if browser storage can't be accessed.
|
|
23
30
|
*/
|
|
24
31
|
export declare function listStorageKeys(storageType?: StorageType): string[];
|
|
25
32
|
export {};
|
|
33
|
+
//# sourceMappingURL=storageUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storageUtils.d.ts","sourceRoot":"","sources":["../../src/utils/storageUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,QAAA,MAAM,YAAY,qDAAsD,CAAC;AAEzE,oBAAY,WAAW,GAAG,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC;AA+CtD,oBAAY,WAAW,GAAG;IACxB,GAAG,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC;IACzB,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7B,GAAG,EAAE,MAAM,IAAI,CAAC;CACjB,CAAC;AAuBF;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE;IAAC,WAAW,CAAC,EAAE,WAAW,CAAA;CAAC,GACpC,WAAW,CAmCb;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,WAAW,GAAE,WAAgC,GAC5C,MAAM,EAAE,CAcV"}
|
|
@@ -6,34 +6,37 @@
|
|
|
6
6
|
*/
|
|
7
7
|
const StorageTypes = ['localStorage', 'sessionStorage', 'none'];
|
|
8
8
|
const DefaultStorageType = 'localStorage';
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Will return `null` if browser storage is unavailable (like running Docusaurus
|
|
11
|
+
* in an iframe). This should NOT be called in SSR.
|
|
12
|
+
*
|
|
13
|
+
* @see https://github.com/facebook/docusaurus/pull/4501
|
|
14
|
+
*/
|
|
11
15
|
function getBrowserStorage(storageType = DefaultStorageType) {
|
|
12
16
|
if (typeof window === 'undefined') {
|
|
13
|
-
throw new Error('Browser storage is not available on
|
|
17
|
+
throw new Error('Browser storage is not available on Node.js/Docusaurus SSR process.');
|
|
14
18
|
}
|
|
15
19
|
if (storageType === 'none') {
|
|
16
20
|
return null;
|
|
17
21
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
22
|
+
try {
|
|
23
|
+
return window[storageType];
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
logOnceBrowserStorageNotAvailableWarning(err);
|
|
27
|
+
return null;
|
|
26
28
|
}
|
|
27
29
|
}
|
|
30
|
+
let hasLoggedBrowserStorageNotAvailableWarning = false;
|
|
28
31
|
/**
|
|
29
|
-
* Poor man's memoization to avoid logging multiple times the same warning
|
|
30
|
-
* Sometimes, localStorage
|
|
32
|
+
* Poor man's memoization to avoid logging multiple times the same warning.
|
|
33
|
+
* Sometimes, `localStorage`/`sessionStorage` is unavailable due to browser
|
|
34
|
+
* policies.
|
|
31
35
|
*/
|
|
32
|
-
let hasLoggedBrowserStorageNotAvailableWarning = false;
|
|
33
36
|
function logOnceBrowserStorageNotAvailableWarning(error) {
|
|
34
37
|
if (!hasLoggedBrowserStorageNotAvailableWarning) {
|
|
35
38
|
console.warn(`Docusaurus browser storage is not available.
|
|
36
|
-
Possible reasons: running Docusaurus in an
|
|
39
|
+
Possible reasons: running Docusaurus in an iframe, in an incognito browser session, or using too strict browser privacy settings.`, error);
|
|
37
40
|
hasLoggedBrowserStorageNotAvailableWarning = true;
|
|
38
41
|
}
|
|
39
42
|
}
|
|
@@ -42,10 +45,10 @@ const NoopStorageSlot = {
|
|
|
42
45
|
set: () => { },
|
|
43
46
|
del: () => { },
|
|
44
47
|
};
|
|
45
|
-
//
|
|
48
|
+
// Fail-fast, as storage APIs should not be used during the SSR process
|
|
46
49
|
function createServerStorageSlot(key) {
|
|
47
50
|
function throwError() {
|
|
48
|
-
throw new Error(`Illegal storage API usage for storage key
|
|
51
|
+
throw new Error(`Illegal storage API usage for storage key "${key}".
|
|
49
52
|
Docusaurus storage APIs are not supposed to be called on the server-rendering process.
|
|
50
53
|
Please only call storage APIs in effects and event handlers.`);
|
|
51
54
|
}
|
|
@@ -56,24 +59,53 @@ Please only call storage APIs in effects and event handlers.`);
|
|
|
56
59
|
};
|
|
57
60
|
}
|
|
58
61
|
/**
|
|
59
|
-
* Creates an
|
|
62
|
+
* Creates an interface to work on a particular key in the storage model.
|
|
63
|
+
* Note that this function only initializes the interface, but doesn't allocate
|
|
64
|
+
* anything by itself (i.e. no side-effects).
|
|
65
|
+
*
|
|
66
|
+
* The API is fail-safe, since usage of browser storage should be considered
|
|
67
|
+
* unreliable. Local storage might simply be unavailable (iframe + browser
|
|
68
|
+
* security) or operations might fail individually. Please assume that using
|
|
69
|
+
* this API can be a no-op. See also https://github.com/facebook/docusaurus/issues/6036
|
|
60
70
|
*/
|
|
61
|
-
export
|
|
71
|
+
export function createStorageSlot(key, options) {
|
|
62
72
|
if (typeof window === 'undefined') {
|
|
63
73
|
return createServerStorageSlot(key);
|
|
64
74
|
}
|
|
65
|
-
const browserStorage = getBrowserStorage(options
|
|
75
|
+
const browserStorage = getBrowserStorage(options?.persistence);
|
|
66
76
|
if (browserStorage === null) {
|
|
67
77
|
return NoopStorageSlot;
|
|
68
78
|
}
|
|
69
79
|
return {
|
|
70
|
-
get: () =>
|
|
71
|
-
|
|
72
|
-
|
|
80
|
+
get: () => {
|
|
81
|
+
try {
|
|
82
|
+
return browserStorage.getItem(key);
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
console.error(`Docusaurus storage error, can't get key=${key}`, err);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
set: (value) => {
|
|
90
|
+
try {
|
|
91
|
+
browserStorage.setItem(key, value);
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
console.error(`Docusaurus storage error, can't set ${key}=${value}`, err);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
del: () => {
|
|
98
|
+
try {
|
|
99
|
+
browserStorage.removeItem(key);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
console.error(`Docusaurus storage error, can't delete key=${key}`, err);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
73
105
|
};
|
|
74
|
-
}
|
|
106
|
+
}
|
|
75
107
|
/**
|
|
76
|
-
* Returns a list of all the keys currently stored in browser storage
|
|
108
|
+
* Returns a list of all the keys currently stored in browser storage,
|
|
77
109
|
* or an empty list if browser storage can't be accessed.
|
|
78
110
|
*/
|
|
79
111
|
export function listStorageKeys(storageType = DefaultStorageType) {
|
|
@@ -90,3 +122,4 @@ export function listStorageKeys(storageType = DefaultStorageType) {
|
|
|
90
122
|
}
|
|
91
123
|
return keys;
|
|
92
124
|
}
|
|
125
|
+
//# sourceMappingURL=storageUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storageUtils.js","sourceRoot":"","sources":["../../src/utils/storageUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,YAAY,GAAG,CAAC,cAAc,EAAE,gBAAgB,EAAE,MAAM,CAAU,CAAC;AAIzE,MAAM,kBAAkB,GAAgB,cAAc,CAAC;AAEvD;;;;;GAKG;AACH,SAAS,iBAAiB,CACxB,cAA2B,kBAAkB;IAE7C,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,MAAM,IAAI,KAAK,CACb,qEAAqE,CACtE,CAAC;KACH;IACD,IAAI,WAAW,KAAK,MAAM,EAAE;QAC1B,OAAO,IAAI,CAAC;KACb;IACD,IAAI;QACF,OAAO,MAAM,CAAC,WAAW,CAAC,CAAC;KAC5B;IAAC,OAAO,GAAG,EAAE;QACZ,wCAAwC,CAAC,GAAY,CAAC,CAAC;QACvD,OAAO,IAAI,CAAC;KACb;AACH,CAAC;AAED,IAAI,0CAA0C,GAAG,KAAK,CAAC;AACvD;;;;GAIG;AACH,SAAS,wCAAwC,CAAC,KAAY;IAC5D,IAAI,CAAC,0CAA0C,EAAE;QAC/C,OAAO,CAAC,IAAI,CACV;kIAC4H,EAC5H,KAAK,CACN,CAAC;QACF,0CAA0C,GAAG,IAAI,CAAC;KACnD;AACH,CAAC;AASD,MAAM,eAAe,GAAgB;IACnC,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;IACf,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;IACb,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;CACd,CAAC;AAEF,uEAAuE;AACvE,SAAS,uBAAuB,CAAC,GAAW;IAC1C,SAAS,UAAU;QACjB,MAAM,IAAI,KAAK,CAAC,8CAA8C,GAAG;;6DAER,CAAC,CAAC;IAC7D,CAAC;IAED,OAAO;QACL,GAAG,EAAE,UAAU;QACf,GA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AAE;QACnB,OAAO,EAAE,CAAC;KACX;IAED,MAAM,IAAI,GAAa,EAAE,CAAC;IAC1B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,cAAc,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;QACjD,MAAM,GAAG,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAClC,IAAI,GAAG,KAAK,IAAI,EAAE;YAChB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SAChB;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
import type { TagsListItem } from '@docusaurus/utils';
|
|
8
|
+
export declare const translateTagsPageTitle: () => string;
|
|
9
|
+
export declare type TagLetterEntry = {
|
|
10
|
+
letter: string;
|
|
11
|
+
tags: TagsListItem[];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Takes a list of tags (as provided by the content plugins), and groups them by
|
|
15
|
+
* their initials.
|
|
16
|
+
*/
|
|
17
|
+
export declare function listTagsByLetters(tags: readonly TagsListItem[]): TagLetterEntry[];
|
|
18
|
+
//# sourceMappingURL=tagsUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tagsUtils.d.ts","sourceRoot":"","sources":["../../src/utils/tagsUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAC;AAEpD,eAAO,MAAM,sBAAsB,QAAO,MAKtC,CAAC;AAEL,oBAAY,cAAc,GAAG;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,YAAY,EAAE,CAAA;CAAC,CAAC;AAMpE;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,SAAS,YAAY,EAAE,GAC5B,cAAc,EAAE,CAoBlB"}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
import { translate } from '@docusaurus/Translate';
|
|
8
|
+
export const translateTagsPageTitle = () => translate({
|
|
9
|
+
id: 'theme.tags.tagsPageTitle',
|
|
10
|
+
message: 'Tags',
|
|
11
|
+
description: 'The title of the tag list page',
|
|
12
|
+
});
|
|
13
|
+
function getTagLetter(tag) {
|
|
14
|
+
return tag[0].toUpperCase();
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Takes a list of tags (as provided by the content plugins), and groups them by
|
|
18
|
+
* their initials.
|
|
19
|
+
*/
|
|
20
|
+
export function listTagsByLetters(tags) {
|
|
21
|
+
const groups = {};
|
|
22
|
+
Object.values(tags).forEach((tag) => {
|
|
23
|
+
const initial = getTagLetter(tag.label);
|
|
24
|
+
groups[initial] ??= [];
|
|
25
|
+
groups[initial].push(tag);
|
|
26
|
+
});
|
|
27
|
+
return (Object.entries(groups)
|
|
28
|
+
// Sort letters
|
|
29
|
+
.sort(([letter1], [letter2]) => letter1.localeCompare(letter2))
|
|
30
|
+
.map(([letter, letterTags]) => {
|
|
31
|
+
// Sort tags inside a letter
|
|
32
|
+
const sortedTags = letterTags.sort((tag1, tag2) => tag1.label.localeCompare(tag2.label));
|
|
33
|
+
return { letter, tags: sortedTags };
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=tagsUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tagsUtils.js","sourceRoot":"","sources":["../../src/utils/tagsU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AC;IACpC,CAAC,CAAC,CACL,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
import type { TOCItem } from '@docusaurus/mdx-loader';
|
|
8
|
+
export declare type TOCTreeNode = {
|
|
9
|
+
readonly value: string;
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly level: number;
|
|
12
|
+
readonly children: readonly TOCTreeNode[];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Takes a flat TOC list (from the MDX loader) and treeifies it into what the
|
|
16
|
+
* TOC components expect. Memoized for performance.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useTreeifiedTOC(toc: TOCItem[]): readonly TOCTreeNode[];
|
|
19
|
+
/**
|
|
20
|
+
* Takes a flat TOC list (from the MDX loader) and treeifies it into what the
|
|
21
|
+
* TOC components expect, applying the `minHeadingLevel` and `maxHeadingLevel`.
|
|
22
|
+
* Memoized for performance.
|
|
23
|
+
*
|
|
24
|
+
* **Important**: this is not the same as `useTreeifiedTOC(toc.filter(...))`,
|
|
25
|
+
* because we have to filter the TOC after it has been treeified. This is mostly
|
|
26
|
+
* to ensure that weird TOC structures preserve their semantics. For example, an
|
|
27
|
+
* h3-h2-h4 sequence should not be treeified as an "h3 > h4" hierarchy with
|
|
28
|
+
* min=3, max=4, but should rather be "[h3, h4]" (since the h2 heading has split
|
|
29
|
+
* the two headings and they are not parent-children)
|
|
30
|
+
*/
|
|
31
|
+
export declare function useFilteredAndTreeifiedTOC({ toc, minHeadingLevel, maxHeadingLevel, }: {
|
|
32
|
+
toc: readonly TOCItem[];
|
|
33
|
+
minHeadingLevel: number;
|
|
34
|
+
maxHeadingLevel: number;
|
|
35
|
+
}): readonly TOCTreeNode[];
|
|
36
|
+
//# sourceMappingURL=tocUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tocUtils.d.ts","sourceRoot":"","sources":["../../src/utils/tocUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,wBAAwB,CAAC;AAEpD,oBAAY,WAAW,GAAG;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,CAAC;CAC3C,CAAC;AAuCF;;;GAGG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,SAAS,WAAW,EAAE,CAEtE;AAiCD;;;;;;;;;;;GAWG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,GAAG,EACH,eAAe,EACf,eAAe,GAChB,EAAE;IACD,GAAG,EAAE,SAAS,OAAO,EAAE,CAAC;IACxB,eAAe,EAAE,MAAM,CAAC;IACxB,eAAe,EAAE,MAAM,CAAC;CACzB,GAAG,SAAS,WAAW,EAAE,CAKzB"}
|
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
import { useMemo } from 'react';
|
|
8
|
+
function treeifyTOC(flatTOC) {
|
|
9
|
+
const headings = flatTOC.map((heading) => ({
|
|
10
|
+
...heading,
|
|
11
|
+
parentIndex: -1,
|
|
12
|
+
children: [],
|
|
13
|
+
}));
|
|
14
|
+
// Keep track of which previous index would be the current heading's direct
|
|
15
|
+
// parent. Each entry <i> is the last index of the `headings` array at heading
|
|
16
|
+
// level <i>. We will modify these indices as we iterate through all headings.
|
|
17
|
+
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
18
|
+
// indices 0 and 1 will remain unused.
|
|
19
|
+
const prevIndexForLevel = Array(7).fill(-1);
|
|
20
|
+
headings.forEach((curr, currIndex) => {
|
|
21
|
+
// Take the last seen index for each ancestor level. the highest index will
|
|
22
|
+
// be the direct ancestor of the current heading.
|
|
23
|
+
const ancestorLevelIndexes = prevIndexForLevel.slice(2, curr.level);
|
|
24
|
+
curr.parentIndex = Math.max(...ancestorLevelIndexes);
|
|
25
|
+
// Mark that curr.level was last seen at the current index.
|
|
26
|
+
prevIndexForLevel[curr.level] = currIndex;
|
|
27
|
+
});
|
|
28
|
+
const rootNodes = [];
|
|
29
|
+
// For a given parentIndex, add each Node into that parent's `children` array
|
|
30
|
+
headings.forEach((heading) => {
|
|
31
|
+
const { parentIndex, ...rest } = heading;
|
|
32
|
+
if (parentIndex >= 0) {
|
|
33
|
+
headings[parentIndex].children.push(rest);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
rootNodes.push(rest);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return rootNodes;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Takes a flat TOC list (from the MDX loader) and treeifies it into what the
|
|
43
|
+
* TOC components expect. Memoized for performance.
|
|
44
|
+
*/
|
|
45
|
+
export function useTreeifiedTOC(toc) {
|
|
46
|
+
return useMemo(() => treeifyTOC(toc), [toc]);
|
|
47
|
+
}
|
|
48
|
+
function filterTOC({ toc, minHeadingLevel, maxHeadingLevel, }) {
|
|
49
|
+
function isValid(item) {
|
|
50
|
+
return item.level >= minHeadingLevel && item.level <= maxHeadingLevel;
|
|
51
|
+
}
|
|
52
|
+
return toc.flatMap((item) => {
|
|
53
|
+
const filteredChildren = filterTOC({
|
|
54
|
+
toc: item.children,
|
|
55
|
+
minHeadingLevel,
|
|
56
|
+
maxHeadingLevel,
|
|
57
|
+
});
|
|
58
|
+
if (isValid(item)) {
|
|
59
|
+
return [
|
|
60
|
+
{
|
|
61
|
+
...item,
|
|
62
|
+
children: filteredChildren,
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
return filteredChildren;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Takes a flat TOC list (from the MDX loader) and treeifies it into what the
|
|
71
|
+
* TOC components expect, applying the `minHeadingLevel` and `maxHeadingLevel`.
|
|
72
|
+
* Memoized for performance.
|
|
73
|
+
*
|
|
74
|
+
* **Important**: this is not the same as `useTreeifiedTOC(toc.filter(...))`,
|
|
75
|
+
* because we have to filter the TOC after it has been treeified. This is mostly
|
|
76
|
+
* to ensure that weird TOC structures preserve their semantics. For example, an
|
|
77
|
+
* h3-h2-h4 sequence should not be treeified as an "h3 > h4" hierarchy with
|
|
78
|
+
* min=3, max=4, but should rather be "[h3, h4]" (since the h2 heading has split
|
|
79
|
+
* the two headings and they are not parent-children)
|
|
80
|
+
*/
|
|
81
|
+
export function useFilteredAndTreeifiedTOC({ toc, minHeadingLevel, maxHeadingLevel, }) {
|
|
82
|
+
return useMemo(() => filterTOC({ toc: treeifyTOC(toc), minHeadingLevel, maxHeadingLevel }), [toc, minHeadingLevel, maxHeadingLevel]);
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=tocUtils.js.map
|