TableOfContents.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import loggerFactory from '@alias/logger';
  4. import { withTranslation } from 'react-i18next';
  5. import { debounce } from 'throttle-debounce';
  6. import StickyEvents from 'sticky-events';
  7. import AppContainer from '../services/AppContainer';
  8. import PageContainer from '../services/PageContainer';
  9. import { isUserPage } from '../../../lib/util/path-utils';
  10. import { createSubscribedElement } from './UnstatedUtils';
  11. const logger = loggerFactory('growi:TableOfContents');
  12. // get these value with
  13. // document.querySelector('.revision-toc').getBoundingClientRect().top
  14. const DEFAULT_REVISION_TOC_TOP_FOR_GROWI_LAYOUT = 190;
  15. const DEFAULT_REVISION_TOC_TOP_FOR_GROWI_LAYOUT_USER_PAGE = 230;
  16. const DEFAULT_REVISION_TOC_TOP_FOR_KIBELA_LAYOUT = 105;
  17. /**
  18. * @author Yuki Takei <yuki@weseek.co.jp>
  19. *
  20. * @export
  21. * @class TableOfContents
  22. * @extends {React.Component}
  23. */
  24. class TableOfContents extends React.Component {
  25. constructor(props) {
  26. super(props);
  27. this.init = this.init.bind(this);
  28. this.resetScrollbarDebounced = debounce(100, this.resetScrollbar);
  29. const { layoutType } = this.props.appContainer.config;
  30. const { path } = this.props.pageContainer.state;
  31. this.defaultRevisionTocTop = DEFAULT_REVISION_TOC_TOP_FOR_GROWI_LAYOUT;
  32. if (isUserPage(path)) {
  33. this.defaultRevisionTocTop = DEFAULT_REVISION_TOC_TOP_FOR_GROWI_LAYOUT_USER_PAGE;
  34. }
  35. if (layoutType === 'kibela') {
  36. this.defaultRevisionTocTop = DEFAULT_REVISION_TOC_TOP_FOR_KIBELA_LAYOUT;
  37. }
  38. }
  39. componentDidMount() {
  40. this.init();
  41. this.resetScrollbar();
  42. }
  43. componentDidUpdate() {
  44. this.resetScrollbar();
  45. }
  46. init() {
  47. /*
  48. * set event listener
  49. */
  50. // resize
  51. window.addEventListener('resize', (event) => {
  52. this.resetScrollbarDebounced(this.defaultRevisionTocTop);
  53. });
  54. // sticky
  55. // See: https://github.com/ryanwalters/sticky-events
  56. const stickyEvents = new StickyEvents({
  57. stickySelector: '#revision-toc',
  58. });
  59. const { stickySelector } = stickyEvents;
  60. const elem = document.querySelector(stickySelector);
  61. elem.addEventListener(StickyEvents.STUCK, (event) => {
  62. logger.debug('StickyEvents.STUCK detected');
  63. this.resetScrollbar();
  64. });
  65. elem.addEventListener(StickyEvents.UNSTUCK, (event) => {
  66. logger.debug('StickyEvents.UNSTUCK detected');
  67. this.resetScrollbar(this.defaultRevisionTocTop);
  68. });
  69. }
  70. getCurrentRevisionTocTop() {
  71. // calculate absolute top of '#revision-toc' element
  72. const revisionTocElem = document.querySelector('.revision-toc');
  73. return revisionTocElem.getBoundingClientRect().top;
  74. }
  75. resetScrollbar(defaultRevisionTocTop) {
  76. const tocContentElem = document.querySelector('.revision-toc .markdownIt-TOC');
  77. if (tocContentElem == null) {
  78. return;
  79. }
  80. const revisionTocTop = defaultRevisionTocTop || this.getCurrentRevisionTocTop();
  81. // window height - revisionTocTop - .system-version height
  82. const viewHeight = window.innerHeight - revisionTocTop - 20;
  83. const tocContentHeight = tocContentElem.getBoundingClientRect().height + 15; // add margin
  84. logger.debug('viewHeight', viewHeight);
  85. logger.debug('tocContentHeight', tocContentHeight);
  86. if (viewHeight < tocContentHeight) {
  87. $('#revision-toc-content').slimScroll({
  88. railVisible: true,
  89. position: 'right',
  90. height: viewHeight,
  91. });
  92. }
  93. else {
  94. $('#revision-toc-content').slimScroll({ destroy: true });
  95. }
  96. }
  97. render() {
  98. const { tocHtml } = this.props.pageContainer.state;
  99. return (
  100. <div
  101. id="revision-toc-content"
  102. className="revision-toc-content"
  103. // eslint-disable-next-line react/no-danger
  104. dangerouslySetInnerHTML={{
  105. __html: tocHtml,
  106. }}
  107. />
  108. );
  109. }
  110. }
  111. /**
  112. * Wrapper component for using unstated
  113. */
  114. const TableOfContentsWrapper = (props) => {
  115. return createSubscribedElement(TableOfContents, props, [AppContainer, PageContainer]);
  116. };
  117. TableOfContents.propTypes = {
  118. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  119. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  120. };
  121. export default withTranslation()(TableOfContentsWrapper);