_onScroll()

in website/src/components/markdown-page.js [197:232]


  _onScroll(evt) {
    if (this._internalScroll) {
      // do not react to scrolling set by _jumpTo
      this._internalScroll = false;
      return;
    }

    let {_anchorPositions} = this;
    const top = evt.target.scrollTop;

    // Calculate all scroll positions of h2, h3 once
    if (!_anchorPositions) {
      _anchorPositions = {};
      // generate mapping of anchor id -> scrollTop
      const anchors = this.refs.container.querySelectorAll('h2[id],h3[id],h4[id],h5[id]');
      for (const anchor of anchors) {
        _anchorPositions[anchor.id] = this._getScrollPosition(anchor);
      }
      this._anchorPositions = _anchorPositions;
    }

    let currentSection;
    for (const id in _anchorPositions) {
      if (_anchorPositions[id] <= top) {
        currentSection = id;
      } else {
        break;
      }
    }

    if (currentSection !== this._currentSection) {
      this._currentSection = currentSection;
      // update query string in url
      this.props.updateQueryString(currentSection ? `?section=${currentSection}` : '');
    }
  }