file-tree-item-inner.js 2.8 KB

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