file-tree-item-inner.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { useEffect, useRef } from 'react'
  2. import PropTypes from 'prop-types'
  3. import classNames from 'classnames'
  4. import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
  5. import { useEditorContext } from '../../../../shared/context/editor-context'
  6. import { useFileTreeMainContext } from '../../contexts/file-tree-main'
  7. import { useDraggable } from '../../contexts/file-tree-draggable'
  8. import FileTreeItemName from './file-tree-item-name'
  9. import FileTreeItemMenu from './file-tree-item-menu'
  10. import { useFileTreeSelectable } from '../../contexts/file-tree-selectable'
  11. function FileTreeItemInner({ id, name, isSelected, icons }) {
  12. const { permissionsLevel } = useEditorContext(editorContextPropTypes)
  13. const { setContextMenuCoords } = useFileTreeMainContext()
  14. const { selectedEntityIds } = useFileTreeSelectable()
  15. const hasMenu =
  16. permissionsLevel !== 'readOnly' &&
  17. isSelected &&
  18. selectedEntityIds.size === 1
  19. const { isDragging, dragRef, setIsDraggable } = useDraggable(id)
  20. const itemRef = useRef()
  21. useEffect(() => {
  22. const item = itemRef.current
  23. if (isSelected && item) {
  24. // we're delaying scrolling due to a race condition with other elements,
  25. // mainly the Outline, being resized inside the same panel, causing the
  26. // FileTree to have its viewport shrinked after the selected item is
  27. // scrolled into the view, hiding it again.
  28. // See `left-pane-resize-all` in `file-tree-controller` for more information.
  29. setTimeout(() => {
  30. if (item) {
  31. scrollIntoViewIfNeeded(item, {
  32. scrollMode: 'if-needed',
  33. })
  34. }
  35. }, 100)
  36. }
  37. }, [isSelected, itemRef])
  38. function handleContextMenu(ev) {
  39. ev.preventDefault()
  40. setContextMenuCoords({
  41. top: ev.pageY,
  42. left: ev.pageX,
  43. })
  44. }
  45. return (
  46. <div
  47. className={classNames('entity', {
  48. 'dnd-draggable-dragging': isDragging,
  49. })}
  50. role="presentation"
  51. ref={dragRef}
  52. onContextMenu={handleContextMenu}
  53. >
  54. <div
  55. className="entity-name entity-name-react"
  56. role="presentation"
  57. ref={itemRef}
  58. >
  59. {icons}
  60. <FileTreeItemName
  61. name={name}
  62. isSelected={isSelected}
  63. setIsDraggable={setIsDraggable}
  64. />
  65. {hasMenu ? <FileTreeItemMenu id={id} /> : null}
  66. </div>
  67. </div>
  68. )
  69. }
  70. FileTreeItemInner.propTypes = {
  71. id: PropTypes.string.isRequired,
  72. name: PropTypes.string.isRequired,
  73. isSelected: PropTypes.bool.isRequired,
  74. icons: PropTypes.node,
  75. }
  76. const editorContextPropTypes = {
  77. permissionsLevel: PropTypes.oneOf(['readOnly', 'readAndWrite', 'owner']),
  78. }
  79. export default FileTreeItemInner