TableOfContents.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { 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 (isUserPage) {
  32. // raise the bottom line by the height and margin-top of UserContentLinks
  33. bottom -= 45;
  34. }
  35. // bottom - revisionToc top
  36. return bottom - (containerTop + containerPaddingTop);
  37. }, [isUserPage, 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}>
  51. {''}
  52. </ReactMarkdown>
  53. </div>
  54. </StickyStretchableScroller>
  55. </div>
  56. );
  57. };
  58. export default TableOfContents;