TableOfContents.jsx 2.9 KB

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