TableOfContents.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React, { useCallback } from 'react';
  2. import { pagePathUtils } from '@growi/core/dist/utils';
  3. import ReactMarkdown from 'react-markdown';
  4. import { useCurrentPagePath } from '~/stores/page';
  5. import { useTocOptions } from '~/stores/renderer';
  6. import loggerFactory from '~/utils/logger';
  7. import { StickyStretchableScroller } from './StickyStretchableScroller';
  8. import styles from './TableOfContents.module.scss';
  9. const { isUsersHomepage: _isUsersHomepage } = pagePathUtils;
  10. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  11. const logger = loggerFactory('growi:TableOfContents');
  12. const TableOfContents = (): JSX.Element => {
  13. const { data: currentPagePath } = useCurrentPagePath();
  14. const isUsersHomePage = currentPagePath != null && _isUsersHomepage(currentPagePath);
  15. const { data: rendererOptions } = useTocOptions();
  16. const calcViewHeight = useCallback(() => {
  17. // calculate absolute top of '#revision-toc' element
  18. const parentElem = document.querySelector('.grw-side-contents-container');
  19. const containerElem = document.querySelector('#revision-toc');
  20. // rendererOptions for redo calcViewHeight()
  21. // see: https://github.com/weseek/growi/pull/6791
  22. if (parentElem == null || containerElem == null || rendererOptions == null) {
  23. return 0;
  24. }
  25. const parentBottom = parentElem.getBoundingClientRect().bottom;
  26. const containerTop = containerElem.getBoundingClientRect().top;
  27. const containerComputedStyle = getComputedStyle(containerElem);
  28. const containerPaddingTop = parseFloat(containerComputedStyle['padding-top']);
  29. // get smaller bottom line of window height - .system-version height - margin 5px) and containerTop
  30. let bottom = Math.min(window.innerHeight - 20 - 5, parentBottom);
  31. if (isUsersHomePage) {
  32. // raise the bottom line by the height and margin-top of UserContentLinks
  33. bottom -= 90;
  34. }
  35. // bottom - revisionToc top
  36. return bottom - (containerTop + containerPaddingTop);
  37. }, [isUsersHomePage, rendererOptions]);
  38. return (
  39. <div id="revision-toc" className={`revision-toc ${styles['revision-toc']}`}>
  40. <StickyStretchableScroller
  41. stickyElemSelector=".grw-side-contents-sticky-container"
  42. calcViewHeight={calcViewHeight}
  43. >
  44. <div
  45. id="revision-toc-content"
  46. data-testid="revision-toc-content"
  47. className="revision-toc-content mb-3"
  48. >
  49. {/* parse blank to show toc (https://github.com/weseek/growi/pull/6277) */}
  50. <ReactMarkdown {...rendererOptions}>{' '}</ReactMarkdown>
  51. </div>
  52. </StickyStretchableScroller>
  53. </div>
  54. );
  55. };
  56. export default TableOfContents;