TableOfContents.tsx 2.6 KB

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