|
|
@@ -1,10 +1,11 @@
|
|
|
-import React, { useCallback } from 'react';
|
|
|
+import React, { useCallback, useEffect } from 'react';
|
|
|
import PropTypes from 'prop-types';
|
|
|
import loggerFactory from '@alias/logger';
|
|
|
|
|
|
import { withTranslation } from 'react-i18next';
|
|
|
|
|
|
import PageContainer from '../services/PageContainer';
|
|
|
+import NavigationContainer from '../services/NavigationContainer';
|
|
|
|
|
|
import { withUnstatedContainers } from './UnstatedUtils';
|
|
|
import StickyStretchableScroller from './StickyStretchableScroller';
|
|
|
@@ -18,7 +19,7 @@ const logger = loggerFactory('growi:TableOfContents');
|
|
|
*/
|
|
|
const TableOfContents = (props) => {
|
|
|
|
|
|
- const { pageContainer } = props;
|
|
|
+ const { pageContainer, navigationContainer } = props;
|
|
|
|
|
|
const calcViewHeight = useCallback(() => {
|
|
|
// calculate absolute top of '#revision-toc' element
|
|
|
@@ -31,6 +32,13 @@ const TableOfContents = (props) => {
|
|
|
|
|
|
const { tocHtml } = pageContainer.state;
|
|
|
|
|
|
+ // execute after generation toc html
|
|
|
+ useEffect(() => {
|
|
|
+ const tocDom = document.getElementById('revision-toc-content');
|
|
|
+ const anchorsInToc = Array.from(tocDom.getElementsByTagName('a'));
|
|
|
+ navigationContainer.addSmoothScrollEvent(anchorsInToc);
|
|
|
+ }, [tocHtml, navigationContainer]);
|
|
|
+
|
|
|
return (
|
|
|
<>
|
|
|
{/* TODO GW-3253 add four contents */}
|
|
|
@@ -56,10 +64,11 @@ const TableOfContents = (props) => {
|
|
|
/**
|
|
|
* Wrapper component for using unstated
|
|
|
*/
|
|
|
-const TableOfContentsWrapper = withUnstatedContainers(TableOfContents, [PageContainer]);
|
|
|
+const TableOfContentsWrapper = withUnstatedContainers(TableOfContents, [PageContainer, NavigationContainer]);
|
|
|
|
|
|
TableOfContents.propTypes = {
|
|
|
pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
|
|
|
+ navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
|
|
|
};
|
|
|
|
|
|
export default withTranslation()(TableOfContentsWrapper);
|