mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
7c97f8ab6e
* Use new JSX runtime and update Babel Node target * Update .eslintrc * Remove React imports GitOrigin-RevId: 559de0267f8f2934c56a860ea8701bb522aa861a
84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
import { useEffect, createRef } from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import classNames from 'classnames'
|
|
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
|
|
|
|
import { useFileTreeMainContext } from '../../contexts/file-tree-main'
|
|
import { useDraggable } from '../../contexts/file-tree-draggable'
|
|
|
|
import FileTreeItemName from './file-tree-item-name'
|
|
import FileTreeItemMenu from './file-tree-item-menu'
|
|
import { useFileTreeSelectable } from '../../contexts/file-tree-selectable'
|
|
|
|
function FileTreeItemInner({ id, name, isSelected, icons }) {
|
|
const { hasWritePermissions, setContextMenuCoords } = useFileTreeMainContext()
|
|
|
|
const { selectedEntityIds } = useFileTreeSelectable()
|
|
|
|
const hasMenu =
|
|
hasWritePermissions && isSelected && selectedEntityIds.size === 1
|
|
|
|
const { isDragging, dragRef, setIsDraggable } = useDraggable(id)
|
|
|
|
const itemRef = createRef()
|
|
|
|
useEffect(() => {
|
|
const item = itemRef.current
|
|
if (isSelected && item) {
|
|
// we're delaying scrolling due to a race condition with other elements,
|
|
// mainly the Outline, being resized inside the same panel, causing the
|
|
// FileTree to have its viewport shrinked after the selected item is
|
|
// scrolled into the view, hiding it again.
|
|
// See `left-pane-resize-all` in `file-tree-controller` for more information.
|
|
setTimeout(() => {
|
|
if (item) {
|
|
scrollIntoViewIfNeeded(item, {
|
|
scrollMode: 'if-needed',
|
|
})
|
|
}
|
|
}, 100)
|
|
}
|
|
}, [isSelected, itemRef])
|
|
|
|
function handleContextMenu(ev) {
|
|
ev.preventDefault()
|
|
setContextMenuCoords({
|
|
top: ev.pageY,
|
|
left: ev.pageX,
|
|
})
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className={classNames('entity', {
|
|
'dnd-draggable-dragging': isDragging,
|
|
})}
|
|
role="presentation"
|
|
ref={dragRef}
|
|
onContextMenu={handleContextMenu}
|
|
>
|
|
<div
|
|
className="entity-name entity-name-react"
|
|
role="presentation"
|
|
ref={itemRef}
|
|
>
|
|
{icons}
|
|
<FileTreeItemName
|
|
name={name}
|
|
isSelected={isSelected}
|
|
setIsDraggable={setIsDraggable}
|
|
/>
|
|
{hasMenu ? <FileTreeItemMenu id={id} /> : null}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
FileTreeItemInner.propTypes = {
|
|
id: PropTypes.string.isRequired,
|
|
name: PropTypes.string.isRequired,
|
|
isSelected: PropTypes.bool.isRequired,
|
|
icons: PropTypes.node,
|
|
}
|
|
|
|
export default FileTreeItemInner
|