From 082c7ff2da971124d4fddeb80ce38becb5f19ebc Mon Sep 17 00:00:00 2001 From: Lucas Fernandes da Costa Date: Sat, 28 Dec 2024 13:52:08 -0300 Subject: [PATCH] fix drag preview on documents and add scroll on drag --- apps/web/src/components/DocumentsTree.tsx | 77 +---------- apps/web/src/components/DragLayer.tsx | 161 ++++++++++++++++++++++ apps/web/src/components/Layout.tsx | 3 + 3 files changed, 165 insertions(+), 76 deletions(-) create mode 100644 apps/web/src/components/DragLayer.tsx diff --git a/apps/web/src/components/DocumentsTree.tsx b/apps/web/src/components/DocumentsTree.tsx index aa0ac508..5946cb38 100644 --- a/apps/web/src/components/DocumentsTree.tsx +++ b/apps/web/src/components/DocumentsTree.tsx @@ -1,5 +1,5 @@ import { ascend, sortWith } from 'ramda' -import { useDrag, useDrop, useDragLayer, XYCoord } from 'react-dnd' +import { useDrag, useDrop } from 'react-dnd' import { EllipsisHorizontalIcon, TrashIcon, @@ -14,7 +14,6 @@ import IconSelector from './IconSelector' import { Menu, Transition } from '@headlessui/react' import ReactDOM from 'react-dom' import { - CSSProperties, MouseEventHandler, useCallback, useEffect, @@ -31,79 +30,6 @@ import { import { List, Stack } from 'immutable' import { getEmptyImage } from 'react-dnd-html5-backend' -function getItemStyles( - initialCursorOffset: XYCoord | null, - initialOffset: XYCoord | null, - currentOffset: XYCoord | null -) { - if (!initialOffset || !currentOffset || !initialCursorOffset) { - return { - display: 'none', - } - } - - const x = initialCursorOffset?.x + (currentOffset.x - initialOffset.x) - const y = initialCursorOffset?.y + (currentOffset.y - initialOffset.y) - const transform = `translate(${x}px, ${y}px)` - - return { - transform, - WebkitTransform: transform, - } -} - -const layerStyles: CSSProperties = { - position: 'fixed', - pointerEvents: 'none', - zIndex: 100, - left: 0, - top: 0, - width: '100%', - height: '100%', -} - -const DocDragLayer = () => { - const { - item, - isDragging, - initialCursorOffset, - initialFileOffset, - currentFileOffset, - } = useDragLayer((monitor) => ({ - item: monitor.getItem(), - itemType: monitor.getItemType(), - initialCursorOffset: monitor.getInitialClientOffset(), - initialFileOffset: monitor.getInitialSourceClientOffset(), - currentFileOffset: monitor.getSourceClientOffset(), - isDragging: monitor.isDragging(), - })) - - if (!isDragging) { - return null - } - - return ( -
-
-
- - {item.title || 'Untitled'} -
-
-
- ) -} - function useIsDocExpanded(doc: ApiDocument, startsOpen: boolean) { const [isExpanded, _setIsExpanded] = useState( localStorage.getItem(`briefer:document:${doc.id}:expanded`) === '1' || @@ -212,7 +138,6 @@ function DocumentTree(props: Props) { return ( <> -