2022-11-14 04:42:06 -05:00
|
|
|
import { useState, useEffect, useRef, memo } from 'react'
|
2020-06-29 09:05:08 -04:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-24 08:20:54 -04:00
|
|
|
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
|
2020-06-29 09:05:08 -04:00
|
|
|
import classNames from 'classnames'
|
2020-09-15 08:48:08 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2020-09-01 04:09:04 -04:00
|
|
|
import OutlineList from './outline-list'
|
2020-09-21 10:14:36 -04:00
|
|
|
import Icon from '../../../shared/components/icon'
|
2020-06-29 09:05:08 -04:00
|
|
|
|
2022-11-14 04:42:06 -05:00
|
|
|
const OutlineItem = memo(function OutlineItem({
|
|
|
|
outlineItem,
|
|
|
|
jumpToLine,
|
|
|
|
highlightedLine,
|
|
|
|
matchesHighlightedLine,
|
|
|
|
containsHighlightedLine,
|
|
|
|
}) {
|
2020-09-15 08:48:08 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2020-06-29 09:05:08 -04:00
|
|
|
const [expanded, setExpanded] = useState(true)
|
2022-05-11 07:26:47 -04:00
|
|
|
const titleElementRef = useRef()
|
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', {
|
2021-04-27 03:52:58 -04:00
|
|
|
'outline-item-no-children': !outlineItem.children,
|
2020-06-29 09:05:08 -04:00
|
|
|
})
|
|
|
|
|
2022-11-14 04:42:06 -05:00
|
|
|
const hasHighlightedChild = !expanded && containsHighlightedLine
|
|
|
|
const isHighlighted = matchesHighlightedLine || hasHighlightedChild
|
2020-09-15 08:48:08 -04:00
|
|
|
|
2020-07-28 05:37:46 -04:00
|
|
|
const itemLinkClasses = classNames('outline-item-link', {
|
2021-04-27 03:52:58 -04:00
|
|
|
'outline-item-link-highlight': isHighlighted,
|
2020-07-28 05:37:46 -04:00
|
|
|
})
|
|
|
|
|
2020-06-29 09:05:08 -04:00
|
|
|
function handleExpandCollapseClick() {
|
|
|
|
setExpanded(!expanded)
|
|
|
|
}
|
|
|
|
|
2022-04-08 05:07:13 -04:00
|
|
|
function handleOutlineItemLinkClick(event) {
|
|
|
|
const syncToPdf = event.detail === 2 // double-click = sync to PDF
|
|
|
|
jumpToLine(outlineItem.line, syncToPdf)
|
2020-06-29 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
useEffect(() => {
|
|
|
|
const wasHighlighted = isHighlightedRef.current
|
|
|
|
isHighlightedRef.current = isHighlighted
|
2020-07-28 05:37:46 -04:00
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
if (!wasHighlighted && isHighlighted) {
|
|
|
|
scrollIntoViewIfNeeded(titleElementRef.current, {
|
|
|
|
scrollMode: 'if-needed',
|
2021-04-27 03:52:58 -04:00
|
|
|
block: 'center',
|
2020-12-15 05:23:54 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [isHighlighted, titleElementRef, isHighlightedRef])
|
2020-07-28 05:37:46 -04:00
|
|
|
|
2020-09-15 08:48:08 -04:00
|
|
|
// don't set the aria-expanded attribute when there are no children
|
|
|
|
const ariaExpandedValue = outlineItem.children ? expanded : undefined
|
|
|
|
|
2020-06-29 09:05:08 -04:00
|
|
|
return (
|
2020-09-15 08:48:08 -04:00
|
|
|
<li
|
|
|
|
className={mainItemClasses}
|
|
|
|
aria-expanded={ariaExpandedValue}
|
|
|
|
role="treeitem"
|
|
|
|
aria-current={isHighlighted}
|
|
|
|
aria-label={outlineItem.title}
|
|
|
|
>
|
2020-06-29 09:05:08 -04:00
|
|
|
<div className="outline-item-row">
|
|
|
|
{outlineItem.children ? (
|
|
|
|
<button
|
|
|
|
className="outline-item-expand-collapse-btn"
|
|
|
|
onClick={handleExpandCollapseClick}
|
2020-09-15 08:48:08 -04:00
|
|
|
aria-label={expanded ? t('collapse') : t('expand')}
|
2020-06-29 09:05:08 -04:00
|
|
|
>
|
2020-09-21 10:14:36 -04:00
|
|
|
<Icon
|
|
|
|
type={expanded ? 'angle-down' : 'angle-right'}
|
2022-01-19 06:56:57 -05:00
|
|
|
className="outline-caret-icon"
|
2020-09-21 10:14:36 -04:00
|
|
|
/>
|
2020-06-29 09:05:08 -04:00
|
|
|
</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 ? (
|
2022-11-14 04:42:06 -05:00
|
|
|
// highlightedLine is only provided to this list if the list contains
|
|
|
|
// the highlighted line. This means that whenever the list does not
|
|
|
|
// contain the highlighted line, the props provided to it are the same
|
|
|
|
// and the component can be memoized.
|
2020-06-29 09:05:08 -04:00
|
|
|
<OutlineList
|
|
|
|
outline={outlineItem.children}
|
|
|
|
jumpToLine={jumpToLine}
|
|
|
|
isRoot={false}
|
2022-11-14 04:42:06 -05:00
|
|
|
highlightedLine={containsHighlightedLine ? highlightedLine : null}
|
|
|
|
containsHighlightedLine={containsHighlightedLine}
|
2020-06-29 09:05:08 -04:00
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</li>
|
|
|
|
)
|
2022-11-14 04:42:06 -05:00
|
|
|
})
|
2020-06-29 09:05:08 -04:00
|
|
|
|
|
|
|
OutlineItem.propTypes = {
|
|
|
|
outlineItem: PropTypes.exact({
|
|
|
|
line: PropTypes.number.isRequired,
|
|
|
|
title: PropTypes.string.isRequired,
|
2020-09-15 08:48:08 -04:00
|
|
|
level: PropTypes.number,
|
2021-04-27 03:52:58 -04:00
|
|
|
children: PropTypes.array,
|
2022-11-03 08:43:59 -04:00
|
|
|
// Used for caching in CM6
|
|
|
|
from: PropTypes.number,
|
|
|
|
to: PropTypes.number,
|
2020-06-29 09:05:08 -04:00
|
|
|
}).isRequired,
|
2020-07-28 05:37:46 -04:00
|
|
|
jumpToLine: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
highlightedLine: PropTypes.number,
|
2022-11-14 04:42:06 -05:00
|
|
|
matchesHighlightedLine: PropTypes.bool,
|
|
|
|
containsHighlightedLine: PropTypes.bool,
|
2020-06-29 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default OutlineItem
|