TableOfContents.tsx 2.3 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. if (parentElem == null || containerElem == null) {
  19. return 0;
  20. }
  21. const parentBottom = parentElem.getBoundingClientRect().bottom;
  22. const containerTop = containerElem.getBoundingClientRect().top;
  23. const containerComputedStyle = getComputedStyle(containerElem);
  24. const containerPaddingTop = parseFloat(containerComputedStyle['padding-top']);
  25. // get smaller bottom line of window height - .system-version height - margin 5px) and containerTop
  26. let bottom = Math.min(window.innerHeight - 20 - 5, parentBottom);
  27. if (isUserPage) {
  28. // raise the bottom line by the height and margin-top of UserContentLinks
  29. bottom -= 45;
  30. }
  31. // bottom - revisionToc top
  32. return bottom - (containerTop + containerPaddingTop);
  33. }, [isUserPage]);
  34. return (
  35. <div id="revision-toc" className={`revision-toc ${styles['revision-toc']}`}>
  36. <StickyStretchableScroller
  37. stickyElemSelector=".grw-side-contents-sticky-container"
  38. calcViewHeight={calcViewHeight}
  39. >
  40. <div
  41. id="revision-toc-content"
  42. className="revision-toc-content mb-3"
  43. >
  44. {/* parse blank to show toc (https://github.com/weseek/growi/pull/6277) */}
  45. <ReactMarkdown {...rendererOptions}>
  46. {''}
  47. </ReactMarkdown>
  48. </div>
  49. </StickyStretchableScroller>
  50. </div>
  51. );
  52. };
  53. export default TableOfContents;