TableOfContents.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React, { useCallback, useEffect } from 'react';
  2. import PropTypes from 'prop-types';
  3. import loggerFactory from '@alias/logger';
  4. import { withTranslation } from 'react-i18next';
  5. import PageContainer from '../services/PageContainer';
  6. import NavigationContainer from '../services/NavigationContainer';
  7. import { withUnstatedContainers } from './UnstatedUtils';
  8. import TopOfTableContents from './TopOfTableContents';
  9. import StickyStretchableScroller from './StickyStretchableScroller';
  10. import RecentlyCreatedIcon from './Icons/RecentlyCreatedIcon';
  11. // eslint-disable-next-line no-unused-vars
  12. const logger = loggerFactory('growi:TableOfContents');
  13. /**
  14. * @author Yuki Takei <yuki@weseek.co.jp>
  15. *
  16. */
  17. const TableOfContents = (props) => {
  18. const { pageContainer, navigationContainer, isGuestUserMode } = props;
  19. const { pageUser } = pageContainer.state;
  20. const isUserPage = pageUser != null;
  21. const calcViewHeight = useCallback(() => {
  22. // calculate absolute top of '#revision-toc' element
  23. const containerElem = document.querySelector('#revision-toc');
  24. const containerTop = containerElem.getBoundingClientRect().top;
  25. // window height - revisionToc top - .system-version - .grw-fab-container height - top-of-table-contents height
  26. if (isUserPage) {
  27. return window.innerHeight - containerTop - 20 - 155 - 26 - 40;
  28. }
  29. return window.innerHeight - containerTop - 20 - 155 - 26;
  30. }, []);
  31. const { tocHtml } = pageContainer.state;
  32. // execute after generation toc html
  33. useEffect(() => {
  34. const tocDom = document.getElementById('revision-toc-content');
  35. const anchorsInToc = Array.from(tocDom.getElementsByTagName('a'));
  36. navigationContainer.addSmoothScrollEvent(anchorsInToc);
  37. }, [tocHtml, navigationContainer]);
  38. return (
  39. <>
  40. <TopOfTableContents isGuestUserMode={isGuestUserMode} />
  41. <StickyStretchableScroller
  42. contentsElemSelector=".revision-toc .markdownIt-TOC"
  43. stickyElemSelector="#revision-toc"
  44. calcViewHeightFunc={calcViewHeight}
  45. >
  46. <div
  47. id="revision-toc-content"
  48. className="revision-toc-content top-of-table-contents"
  49. // eslint-disable-next-line react/no-danger
  50. dangerouslySetInnerHTML={{
  51. __html: tocHtml,
  52. }}
  53. />
  54. </StickyStretchableScroller>
  55. { isUserPage && (
  56. <div className="mt-3 d-flex justify-content-around">
  57. {/* <a className="btn btn-outline-secondary btn-sm" href="#bookmarks-list">
  58. <i className="mr-2 icon-star"></i>
  59. <span>Bookmarks</span>
  60. </a> */}
  61. <button
  62. type="button"
  63. className="btn btn-outline-secondary btn-sm"
  64. href="#bookmarks-list"
  65. onClick={() => navigationContainer.smoothScrollIntoview('bookmarks-list', 40)}
  66. >
  67. <i className="mr-2 icon-star"></i>
  68. <span>Bookmarks</span>
  69. </button>
  70. <a className="btn btn-outline-secondary btn-sm" href="#recently-created-list">
  71. <i className="grw-icon-container-recently-created mr-2"><RecentlyCreatedIcon /></i>
  72. <span>Recently Created</span>
  73. </a>
  74. </div>
  75. )}
  76. </>
  77. );
  78. };
  79. /**
  80. * Wrapper component for using unstated
  81. */
  82. const TableOfContentsWrapper = withUnstatedContainers(TableOfContents, [PageContainer, NavigationContainer]);
  83. TableOfContents.propTypes = {
  84. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  85. navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
  86. isGuestUserMode: PropTypes.bool.isRequired,
  87. };
  88. export default withTranslation()(TableOfContentsWrapper);