2020-08-06 05:04:45 -04:00
|
|
|
import React, { useState, useEffect, createRef, useRef } from 'react'
|
2020-06-29 09:05:08 -04:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import classNames from 'classnames'
|
|
|
|
import OutlineList from './OutlineList'
|
|
|
|
|
2020-08-18 09:08:49 -04:00
|
|
|
function getChildrenLines(children) {
|
|
|
|
return (children || [])
|
|
|
|
.map(child => {
|
|
|
|
return getChildrenLines(child.children).concat(child.line)
|
|
|
|
})
|
|
|
|
.flat()
|
|
|
|
}
|
|
|
|
|
2020-07-28 05:37:46 -04:00
|
|
|
function OutlineItem({ outlineItem, jumpToLine, highlightedLine }) {
|
2020-06-29 09:05:08 -04:00
|
|
|
const [expanded, setExpanded] = useState(true)
|
2020-07-28 05:37:46 -04:00
|
|
|
const titleElementRef = createRef()
|
2020-08-06 05:04:45 -04:00
|
|
|
const isHighlightedRef = useRef(false)
|
2020-06-29 09:05:08 -04:00
|
|
|
|
|
|
|
const mainItemClasses = classNames('outline-item', {
|
|
|
|
'outline-item-no-children': !outlineItem.children
|
|
|
|
})
|
|
|
|
|
|
|
|
const expandCollapseIconClasses = classNames('fa', 'outline-caret-icon', {
|
|
|
|
'fa-angle-down': expanded,
|
|
|
|
'fa-angle-right': !expanded
|
|
|
|
})
|
|
|
|
|
2020-08-18 09:08:49 -04:00
|
|
|
const hasHighlightedChild =
|
|
|
|
!expanded &&
|
|
|
|
getChildrenLines(outlineItem.children).includes(highlightedLine)
|
|
|
|
|
2020-07-28 05:37:46 -04:00
|
|
|
const itemLinkClasses = classNames('outline-item-link', {
|
2020-08-18 09:08:49 -04:00
|
|
|
'outline-item-link-highlight':
|
|
|
|
highlightedLine === outlineItem.line || hasHighlightedChild
|
2020-07-28 05:37:46 -04:00
|
|
|
})
|
|
|
|
|
2020-06-29 09:05:08 -04:00
|
|
|
function handleExpandCollapseClick() {
|
|
|
|
setExpanded(!expanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleOutlineItemLinkClick() {
|
|
|
|
jumpToLine(outlineItem.line)
|
|
|
|
}
|
|
|
|
|
2020-07-28 05:37:46 -04:00
|
|
|
useEffect(
|
|
|
|
() => {
|
2020-08-06 05:04:45 -04:00
|
|
|
const wasHighlighted = isHighlightedRef.current
|
|
|
|
const isNowHighlighted = highlightedLine === outlineItem.line
|
2020-07-28 05:37:46 -04:00
|
|
|
|
2020-08-06 05:04:45 -04:00
|
|
|
isHighlightedRef.current = isNowHighlighted
|
|
|
|
|
|
|
|
if (!wasHighlighted && isNowHighlighted) {
|
|
|
|
titleElementRef.current.scrollIntoView({
|
|
|
|
block: 'center'
|
|
|
|
})
|
|
|
|
}
|
2020-07-28 05:37:46 -04:00
|
|
|
},
|
2020-08-06 05:04:45 -04:00
|
|
|
[highlightedLine, titleElementRef, isHighlightedRef]
|
2020-07-28 05:37:46 -04:00
|
|
|
)
|
|
|
|
|
2020-06-29 09:05:08 -04:00
|
|
|
return (
|
|
|
|
<li className={mainItemClasses}>
|
|
|
|
<div className="outline-item-row">
|
|
|
|
{outlineItem.children ? (
|
|
|
|
<button
|
|
|
|
className="outline-item-expand-collapse-btn"
|
|
|
|
onClick={handleExpandCollapseClick}
|
|
|
|
>
|
|
|
|
<i className={expandCollapseIconClasses} />
|
|
|
|
</button>
|
|
|
|
) : null}
|
|
|
|
<button
|
2020-07-28 05:37:46 -04:00
|
|
|
className={itemLinkClasses}
|
2020-06-29 09:05:08 -04:00
|
|
|
onClick={handleOutlineItemLinkClick}
|
2020-07-28 05:37:46 -04:00
|
|
|
ref={titleElementRef}
|
2020-06-29 09:05:08 -04:00
|
|
|
>
|
|
|
|
{outlineItem.title}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
{expanded && outlineItem.children ? (
|
|
|
|
<OutlineList
|
|
|
|
outline={outlineItem.children}
|
|
|
|
jumpToLine={jumpToLine}
|
|
|
|
isRoot={false}
|
2020-07-28 05:37:46 -04:00
|
|
|
highlightedLine={highlightedLine}
|
2020-06-29 09:05:08 -04:00
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
OutlineItem.propTypes = {
|
|
|
|
outlineItem: PropTypes.exact({
|
|
|
|
line: PropTypes.number.isRequired,
|
|
|
|
title: PropTypes.string.isRequired,
|
|
|
|
level: PropTypes.number.isRequired,
|
|
|
|
children: PropTypes.array
|
|
|
|
}).isRequired,
|
2020-07-28 05:37:46 -04:00
|
|
|
jumpToLine: PropTypes.func.isRequired,
|
|
|
|
highlightedLine: PropTypes.number
|
2020-06-29 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default OutlineItem
|