TableOfContents.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { type JSX, useCallback } from 'react';
  2. import { pagePathUtils } from '@growi/core/dist/utils';
  3. import ReactMarkdown from 'react-markdown';
  4. import { useCurrentPagePath } from '~/states/page';
  5. import { useTocOptions } from '~/states/ui/toc';
  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. type Props = {
  13. tagsElementHeight?: number;
  14. };
  15. const TableOfContents = ({ tagsElementHeight }: Props): JSX.Element => {
  16. const currentPagePath = useCurrentPagePath();
  17. const isUsersHomePage =
  18. currentPagePath != null && _isUsersHomepage(currentPagePath);
  19. const { data: rendererOptions } = useTocOptions();
  20. const calcViewHeight = useCallback(() => {
  21. // calculate absolute top of '#revision-toc' element
  22. const parentElem = document.querySelector('.grw-side-contents-container');
  23. const containerElem = document.querySelector('#revision-toc');
  24. // rendererOptions for redo calcViewHeight()
  25. // see: https://github.com/growilabs/growi/pull/6791
  26. if (
  27. parentElem == null ||
  28. containerElem == null ||
  29. rendererOptions == null ||
  30. tagsElementHeight == null
  31. ) {
  32. return 0;
  33. }
  34. const parentBottom = parentElem.getBoundingClientRect().bottom;
  35. const containerTop = containerElem.getBoundingClientRect().top;
  36. const containerComputedStyle = getComputedStyle(containerElem);
  37. const containerPaddingTop = parseFloat(
  38. containerComputedStyle['padding-top'],
  39. );
  40. // get smaller bottom line of window height - .system-version height - margin 5px) and containerTop
  41. let bottom = Math.min(window.innerHeight - 20 - 5, parentBottom);
  42. if (isUsersHomePage) {
  43. // raise the bottom line by the height and margin-top of UserContentLinks
  44. bottom -= 90;
  45. }
  46. // bottom - revisionToc top
  47. return bottom - (containerTop + containerPaddingTop);
  48. }, [isUsersHomePage, rendererOptions, tagsElementHeight]);
  49. return (
  50. <div id="revision-toc" className={`revision-toc ${styles['revision-toc']}`}>
  51. <StickyStretchableScroller
  52. stickyElemSelector=".grw-side-contents-sticky-container"
  53. calcViewHeight={calcViewHeight}
  54. >
  55. <div
  56. id="revision-toc-content"
  57. data-testid="revision-toc-content"
  58. className="revision-toc-content mb-3"
  59. >
  60. {/* parse blank to show toc (https://github.com/growilabs/growi/pull/6277) */}
  61. <ReactMarkdown {...rendererOptions}> </ReactMarkdown>
  62. </div>
  63. </StickyStretchableScroller>
  64. </div>
  65. );
  66. };
  67. export default TableOfContents;