2021-06-23 05:37:08 -04:00
|
|
|
import { useRef, useEffect, useState } from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2022-07-21 04:32:34 -04:00
|
|
|
import getDroppedFiles from '@uppy/utils/lib/getDroppedFiles'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
import { DndProvider, createDndContext, useDrag, useDrop } from 'react-dnd'
|
2022-07-21 04:32:34 -04:00
|
|
|
import {
|
|
|
|
HTML5Backend,
|
|
|
|
getEmptyImage,
|
|
|
|
NativeTypes,
|
|
|
|
} from 'react-dnd-html5-backend'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
import {
|
|
|
|
findAllInTreeOrThrow,
|
2021-04-27 03:52:58 -04:00
|
|
|
findAllFolderIdsInFolders,
|
2020-11-26 09:22:30 -05:00
|
|
|
} from '../util/find-in-tree'
|
|
|
|
|
|
|
|
import { useFileTreeActionable } from './file-tree-actionable'
|
2022-01-10 10:47:01 -05:00
|
|
|
import { useFileTreeData } from '../../../shared/context/file-tree-data-context'
|
2020-11-26 09:22:30 -05:00
|
|
|
import { useFileTreeSelectable } from '../contexts/file-tree-selectable'
|
2022-01-10 10:46:46 -05:00
|
|
|
import { useEditorContext } from '../../../shared/context/editor-context'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-01-05 05:57:53 -05:00
|
|
|
// HACK ALERT
|
|
|
|
// DnD binds drag and drop events on window and stop propagation if the dragged
|
|
|
|
// item is not a DnD element. This break other drag and drop interfaces; in
|
|
|
|
// particular in rich text.
|
|
|
|
// This is a hacky workaround to avoid calling the DnD listeners when the
|
|
|
|
// draggable or droppable element is not within a `dnd-container` element.
|
|
|
|
const ModifiedBackend = (...args) => {
|
|
|
|
function isDndChild(elt) {
|
|
|
|
if (elt.getAttribute && elt.getAttribute('dnd-container')) return true
|
|
|
|
if (!elt.parentNode) return false
|
|
|
|
return isDndChild(elt.parentNode)
|
|
|
|
}
|
|
|
|
const instance = new HTML5Backend(...args)
|
|
|
|
|
|
|
|
const dragDropListeners = [
|
|
|
|
'handleTopDragStart',
|
|
|
|
'handleTopDragStartCapture',
|
|
|
|
'handleTopDragEndCapture',
|
|
|
|
'handleTopDragEnter',
|
|
|
|
'handleTopDragEnterCapture',
|
|
|
|
'handleTopDragLeaveCapture',
|
|
|
|
'handleTopDragOver',
|
|
|
|
'handleTopDragOverCapture',
|
|
|
|
'handleTopDrop',
|
2021-04-27 03:52:58 -04:00
|
|
|
'handleTopDropCapture',
|
2021-01-05 05:57:53 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
dragDropListeners.forEach(dragDropListener => {
|
|
|
|
const originalListener = instance[dragDropListener]
|
|
|
|
instance[dragDropListener] = (ev, ...extraArgs) => {
|
|
|
|
if (isDndChild(ev.target)) originalListener(ev, ...extraArgs)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
|
|
|
const DndContext = createDndContext(ModifiedBackend)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const DRAGGABLE_TYPE = 'ENTITY'
|
|
|
|
|
|
|
|
export function FileTreeDraggableProvider({ children }) {
|
|
|
|
const DndManager = useRef(DndContext)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DndProvider manager={DndManager.current.dragDropManager}>
|
|
|
|
{children}
|
|
|
|
</DndProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeDraggableProvider.propTypes = {
|
|
|
|
children: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(PropTypes.node),
|
2021-04-27 03:52:58 -04:00
|
|
|
PropTypes.node,
|
|
|
|
]).isRequired,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useDraggable(draggedEntityId) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
const { permissionsLevel } = useEditorContext(editorContextPropTypes)
|
2022-01-10 10:47:01 -05:00
|
|
|
const { fileTreeData } = useFileTreeData()
|
2023-10-25 05:25:13 -04:00
|
|
|
const { selectedEntityIds, isRootFolderSelected } = useFileTreeSelectable()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
const [isDraggable, setIsDraggable] = useState(true)
|
2021-02-09 04:50:16 -05:00
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
const item = { type: DRAGGABLE_TYPE }
|
2023-10-25 05:24:58 -04:00
|
|
|
const [{ isDragging, draggedEntityIds }, dragRef, preview] = useDrag({
|
2020-11-26 09:22:30 -05:00
|
|
|
item, // required, but overwritten by the return value of `begin`
|
|
|
|
begin: () => {
|
|
|
|
const draggedEntityIds = getDraggedEntityIds(
|
2023-10-25 05:25:13 -04:00
|
|
|
isRootFolderSelected ? new Set() : selectedEntityIds,
|
2020-11-26 09:22:30 -05:00
|
|
|
draggedEntityId
|
|
|
|
)
|
|
|
|
const draggedItems = findAllInTreeOrThrow(fileTreeData, draggedEntityIds)
|
|
|
|
const title = getDraggedTitle(draggedItems, t)
|
|
|
|
const forbiddenFolderIds = getForbiddenFolderIds(draggedItems)
|
|
|
|
return { ...item, title, forbiddenFolderIds, draggedEntityIds }
|
|
|
|
},
|
|
|
|
collect: monitor => ({
|
2021-04-27 03:52:58 -04:00
|
|
|
isDragging: !!monitor.isDragging(),
|
|
|
|
}),
|
2022-01-10 10:46:46 -05:00
|
|
|
canDrag: () => permissionsLevel !== 'readOnly' && isDraggable,
|
2023-10-25 05:24:58 -04:00
|
|
|
end: () => item,
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// remove the automatic preview as we're using a custom preview via
|
|
|
|
// FileTreeDraggablePreviewLayer
|
2020-12-15 05:23:54 -05:00
|
|
|
useEffect(() => {
|
|
|
|
preview(getEmptyImage())
|
|
|
|
}, [preview])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
dragRef,
|
2021-02-09 04:50:16 -05:00
|
|
|
isDragging,
|
2021-04-27 03:52:58 -04:00
|
|
|
setIsDraggable,
|
2023-10-25 05:24:58 -04:00
|
|
|
draggedEntityIds,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
const editorContextPropTypes = {
|
|
|
|
permissionsLevel: PropTypes.oneOf(['readOnly', 'readAndWrite', 'owner']),
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
export function useDroppable(droppedEntityId) {
|
2022-07-21 04:32:34 -04:00
|
|
|
const { finishMoving, setDroppedFiles, startUploadingDocOrFile } =
|
|
|
|
useFileTreeActionable()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const [{ isOver }, dropRef] = useDrop({
|
2022-07-21 04:32:34 -04:00
|
|
|
accept: [DRAGGABLE_TYPE, NativeTypes.FILE],
|
2020-11-26 09:22:30 -05:00
|
|
|
canDrop: (item, monitor) => {
|
|
|
|
const isOver = monitor.isOver({ shallow: true })
|
|
|
|
if (!isOver) return false
|
2022-07-21 04:32:34 -04:00
|
|
|
if (
|
|
|
|
item.type === DRAGGABLE_TYPE &&
|
|
|
|
item.forbiddenFolderIds.has(droppedEntityId)
|
|
|
|
)
|
|
|
|
return false
|
2020-11-26 09:22:30 -05:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
drop: (item, monitor) => {
|
|
|
|
const didDropInChild = monitor.didDrop()
|
|
|
|
if (didDropInChild) return
|
2022-07-21 04:32:34 -04:00
|
|
|
if (item.type === DRAGGABLE_TYPE) {
|
|
|
|
finishMoving(droppedEntityId, item.draggedEntityIds)
|
|
|
|
} else {
|
|
|
|
getDroppedFiles(item).then(files => {
|
|
|
|
setDroppedFiles({ files, targetFolderId: droppedEntityId })
|
|
|
|
startUploadingDocOrFile()
|
|
|
|
})
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
},
|
|
|
|
collect: monitor => ({
|
2021-04-27 03:52:58 -04:00
|
|
|
isOver: monitor.canDrop(),
|
|
|
|
}),
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
dropRef,
|
2021-04-27 03:52:58 -04:00
|
|
|
isOver,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the list of dragged entity ids. If the dragged entity is one of the
|
|
|
|
// selected entities then all the selected entites are dragged entities,
|
|
|
|
// otherwise it's the dragged entity only.
|
|
|
|
function getDraggedEntityIds(selectedEntityIds, draggedEntityId) {
|
|
|
|
if (selectedEntityIds.size > 1 && selectedEntityIds.has(draggedEntityId)) {
|
|
|
|
// dragging the multi-selected entities
|
|
|
|
return new Set(selectedEntityIds)
|
|
|
|
} else {
|
|
|
|
// not dragging the selection; only the current item
|
|
|
|
return new Set([draggedEntityId])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the draggable title. This is the name of the dragged entities if there's
|
|
|
|
// only one, otherwise it's the number of dragged entities.
|
|
|
|
function getDraggedTitle(draggedItems, t) {
|
|
|
|
if (draggedItems.size === 1) {
|
|
|
|
const draggedItem = Array.from(draggedItems)[0]
|
|
|
|
return draggedItem.entity.name
|
|
|
|
}
|
|
|
|
return t('n_items', { count: draggedItems.size })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all children folder ids of any of the dragged items.
|
|
|
|
function getForbiddenFolderIds(draggedItems) {
|
|
|
|
const draggedFoldersArray = Array.from(draggedItems)
|
|
|
|
.filter(draggedItem => {
|
|
|
|
return draggedItem.type === 'folder'
|
|
|
|
})
|
|
|
|
.map(draggedItem => draggedItem.entity)
|
|
|
|
const draggedFolders = new Set(draggedFoldersArray)
|
|
|
|
return findAllFolderIdsInFolders(draggedFolders)
|
|
|
|
}
|