2021-03-18 05:52:36 -04:00
|
|
|
import React from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import ReactDOM from 'react-dom'
|
|
|
|
import { Dropdown } from 'react-bootstrap'
|
2022-01-10 10:46:46 -05:00
|
|
|
import { useEditorContext } from '../../../shared/context/editor-context'
|
2021-03-18 05:52:36 -04:00
|
|
|
import { useFileTreeMainContext } from '../contexts/file-tree-main'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
import FileTreeItemMenuItems from './file-tree-item/file-tree-item-menu-items'
|
|
|
|
|
|
|
|
function FileTreeContextMenu() {
|
2024-01-26 04:23:48 -05:00
|
|
|
const { permissionsLevel } = useEditorContext()
|
2022-01-10 10:46:46 -05:00
|
|
|
const { contextMenuCoords, setContextMenuCoords } = useFileTreeMainContext()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
if (permissionsLevel === 'readOnly' || !contextMenuCoords) return null
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
function close() {
|
|
|
|
// reset context menu
|
|
|
|
setContextMenuCoords(null)
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function handleToggle(wantOpen: boolean) {
|
2020-11-26 09:22:30 -05:00
|
|
|
if (!wantOpen) close()
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleClick() {
|
|
|
|
handleToggle(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReactDOM.createPortal(
|
|
|
|
<Dropdown
|
|
|
|
onClick={handleClick}
|
|
|
|
open
|
2020-12-09 06:56:24 -05:00
|
|
|
id="dropdown-file-tree-context-menu"
|
2020-11-26 09:22:30 -05:00
|
|
|
onToggle={handleToggle}
|
|
|
|
>
|
|
|
|
<FakeDropDownToggle bsRole="toggle" />
|
|
|
|
<Dropdown.Menu className="context-menu" style={contextMenuCoords}>
|
|
|
|
<FileTreeItemMenuItems />
|
|
|
|
</Dropdown.Menu>
|
|
|
|
</Dropdown>,
|
2024-01-26 04:23:48 -05:00
|
|
|
document.body
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fake component required as Dropdowns require a Toggle, even tho we don't want
|
|
|
|
// one for the context menu
|
2024-01-26 04:23:48 -05:00
|
|
|
const FakeDropDownToggle = React.forwardRef<undefined, { bsRole: string }>(
|
|
|
|
({ bsRole }, ref) => {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-05-13 06:21:35 -04:00
|
|
|
FakeDropDownToggle.displayName = 'FakeDropDownToggle'
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
export default FileTreeContextMenu
|