TableOfContents.jsx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { useCallback, useEffect } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import loggerFactory from '~/utils/logger';
  5. import PageContainer from '~/client/services/PageContainer';
  6. import { addSmoothScrollEvent } from '~/client/util/smooth-scroll';
  7. import { blinkElem } from '~/client/util/blink-section-header';
  8. import { withUnstatedContainers } from './UnstatedUtils';
  9. import StickyStretchableScroller from './StickyStretchableScroller';
  10. // eslint-disable-next-line no-unused-vars
  11. const logger = loggerFactory('growi:TableOfContents');
  12. /**
  13. * @author Yuki Takei <yuki@weseek.co.jp>
  14. *
  15. */
  16. const TableOfContents = (props) => {
  17. const { t, pageContainer } = props;
  18. const { pageUser } = pageContainer.state;
  19. const isUserPage = pageUser != null;
  20. const calcViewHeight = useCallback(() => {
  21. // calculate absolute top of '#revision-toc' element
  22. const parentElem = document.querySelector('.grw-side-contents-container');
  23. const parentBottom = parentElem.getBoundingClientRect().bottom;
  24. const containerElem = document.querySelector('#revision-toc');
  25. const containerTop = containerElem.getBoundingClientRect().top;
  26. const containerComputedStyle = getComputedStyle(containerElem);
  27. const containerPaddingTop = parseFloat(containerComputedStyle['padding-top']);
  28. // get smaller bottom line of window height - the height of ContentLinkButtons and .system-version height) and containerTop
  29. let bottom = Math.min(window.innerHeight - 41 - 20, parentBottom);
  30. if (isUserPage) {
  31. // raise the bottom line by the height and margin-top of UserContentLinks
  32. bottom -= 45;
  33. }
  34. // bottom - revisionToc top
  35. return bottom - (containerTop + containerPaddingTop);
  36. }, [isUserPage]);
  37. const { tocHtml } = pageContainer.state;
  38. // execute after generation toc html
  39. useEffect(() => {
  40. const tocDom = document.getElementById('revision-toc-content');
  41. const anchorsInToc = Array.from(tocDom.getElementsByTagName('a'));
  42. addSmoothScrollEvent(anchorsInToc, blinkElem);
  43. }, [tocHtml]);
  44. return (
  45. <StickyStretchableScroller
  46. contentsElemSelector=".revision-toc .markdownIt-TOC"
  47. stickyElemSelector=".grw-side-contents-sticky-container"
  48. calcViewHeightFunc={calcViewHeight}
  49. >
  50. { tocHtml !== ''
  51. ? (
  52. <div
  53. id="revision-toc-content"
  54. className="revision-toc-content mb-3"
  55. // eslint-disable-next-line react/no-danger
  56. dangerouslySetInnerHTML={{ __html: tocHtml }}
  57. />
  58. )
  59. : (
  60. <div
  61. id="revision-toc-content"
  62. className="revision-toc-content mb-2"
  63. >
  64. <span className="text-muted">({t('page_table_of_contents.empty')})</span>
  65. </div>
  66. ) }
  67. </StickyStretchableScroller>
  68. );
  69. };
  70. /**
  71. * Wrapper component for using unstated
  72. */
  73. const TableOfContentsWrapper = withUnstatedContainers(TableOfContents, [PageContainer]);
  74. TableOfContents.propTypes = {
  75. t: PropTypes.func.isRequired, // i18next
  76. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  77. };
  78. export default withTranslation()(TableOfContentsWrapper);