TableOfContents.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { useCallback } from 'react';
  2. import ReactMarkdown from 'react-markdown';
  3. import { useIsUserPage } from '~/stores/context';
  4. import { useTocOptions } from '~/stores/renderer';
  5. import loggerFactory from '~/utils/logger';
  6. import { StickyStretchableScroller } from './StickyStretchableScroller';
  7. import styles from './TableOfContents.module.scss';
  8. // eslint-disable-next-line no-unused-vars
  9. const logger = loggerFactory('growi:TableOfContents');
  10. const TableOfContents = (): JSX.Element => {
  11. const { data: isUserPage } = useIsUserPage();
  12. // const [tocHtml, setTocHtml] = useState('');
  13. const { data: rendererOptions } = useTocOptions();
  14. const calcViewHeight = useCallback(() => {
  15. // calculate absolute top of '#revision-toc' element
  16. const parentElem = document.querySelector('.grw-side-contents-container');
  17. const containerElem = document.querySelector('#revision-toc');
  18. // rendererOptions for redo calcViewHeight()
  19. // see: https://github.com/weseek/growi/pull/6791
  20. if (parentElem == null || containerElem == null || rendererOptions == null) {
  21. return 0;
  22. }
  23. const parentBottom = parentElem.getBoundingClientRect().bottom;
  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, rendererOptions]);
  36. return (
  37. <div id="revision-toc" className={`revision-toc ${styles['revision-toc']}`}>
  38. <StickyStretchableScroller
  39. stickyElemSelector=".grw-side-contents-sticky-container"
  40. calcViewHeight={calcViewHeight}
  41. >
  42. <div
  43. id="revision-toc-content"
  44. className="revision-toc-content mb-3"
  45. >
  46. {/* parse blank to show toc (https://github.com/weseek/growi/pull/6277) */}
  47. <ReactMarkdown {...rendererOptions}>
  48. {''}
  49. </ReactMarkdown>
  50. </div>
  51. </StickyStretchableScroller>
  52. </div>
  53. );
  54. };
  55. export default TableOfContents;