-
Notifications
You must be signed in to change notification settings - Fork 605
🐛(frontend) fix DnD hover flash and enable drop on pinned documents #2531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
magopian
wants to merge
2
commits into
suitenumerique:main
Choose a base branch
from
magopian:fix/nesting-document-via-drag-and-drop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
246 changes: 246 additions & 0 deletions
246
src/frontend/apps/impress/src/features/docs/DocDndContext.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| import { | ||
| DndContext, | ||
| DragOverlay, | ||
| Modifier, | ||
| UniqueIdentifier, | ||
| } from '@dnd-kit/core'; | ||
| import { getEventCoordinates } from '@dnd-kit/utilities'; | ||
| import { useModal } from '@gouvfr-lasuite/cunningham-react'; | ||
| import { TreeViewMoveModeEnum } from '@gouvfr-lasuite/ui-kit'; | ||
| import dynamic from 'next/dynamic'; | ||
| import { | ||
| PropsWithChildren, | ||
| createContext, | ||
| useContext, | ||
| useEffect, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import { Card, Text } from '@/components'; | ||
| import { Doc, useMoveDoc, useTrans } from '@/docs/doc-management'; | ||
|
|
||
| import { DocDragEndData, useDragAndDrop } from './docs-grid/hooks/useDragAndDrop'; | ||
|
|
||
| const ModalConfirmationMoveDoc = dynamic( | ||
| () => | ||
| import('./docs-grid/components/ModalConfimationMoveDoc').then((mod) => ({ | ||
| default: mod.ModalConfirmationMoveDoc, | ||
| })), | ||
| { ssr: false }, | ||
| ); | ||
|
|
||
| const snapToTopLeft: Modifier = ({ | ||
| activatorEvent, | ||
| draggingNodeRect, | ||
| transform, | ||
| }) => { | ||
| if (draggingNodeRect && activatorEvent) { | ||
| const activatorCoordinates = getEventCoordinates(activatorEvent); | ||
| if (!activatorCoordinates) { | ||
| return transform; | ||
| } | ||
| const offsetX = activatorCoordinates.x - draggingNodeRect.left; | ||
| const offsetY = activatorCoordinates.y - draggingNodeRect.top; | ||
| return { | ||
| ...transform, | ||
| x: transform.x + offsetX - 3, | ||
| y: transform.y + offsetY - 3, | ||
| }; | ||
| } | ||
| return transform; | ||
| }; | ||
|
|
||
| type DocDndContextValue = { | ||
| selectedDoc: Doc | undefined; | ||
| canDrag: boolean; | ||
| canDrop: boolean | undefined; | ||
| updateCanDrop: (canDrop: boolean, isOver: boolean) => void; | ||
| isDraggableDisabled: boolean; | ||
| }; | ||
|
|
||
| const DocDndCtx = createContext<DocDndContextValue | null>(null); | ||
|
|
||
| export const useDocDnd = () => useContext(DocDndCtx); | ||
|
|
||
| export const DocDndProvider = ({ children }: PropsWithChildren) => { | ||
| const { mutateAsync: handleMove, isError } = useMoveDoc(); | ||
| const modalConfirmation = useModal(); | ||
| const onDragData = useRef<DocDragEndData | null>(null); | ||
| const { untitledDocument } = useTrans(); | ||
| const { t } = useTranslation(); | ||
|
|
||
| const handleMoveDoc = async () => { | ||
| if (!onDragData.current) { | ||
| return; | ||
| } | ||
| const { sourceDocumentId, target } = onDragData.current; | ||
| const targetDocumentId = target.id; | ||
| // Strip the `favorite-` prefix that favorite droppables add to avoid | ||
| // dnd-kit ID collisions, so we compare raw document IDs. | ||
| const normalizedSourceId = sourceDocumentId.replace(/^favorite-/, ''); | ||
| const normalizedTargetId = targetDocumentId.replace(/^favorite-/, ''); | ||
| const dragSnapshot = onDragData.current; | ||
| modalConfirmation.onClose(); | ||
| if (!normalizedSourceId || !normalizedTargetId || normalizedSourceId === normalizedTargetId) { | ||
| onDragData.current = null; | ||
| return; | ||
| } | ||
| try { | ||
| await handleMove({ | ||
| sourceDocumentId: normalizedSourceId, | ||
| targetDocumentId: normalizedTargetId, | ||
| position: TreeViewMoveModeEnum.FIRST_CHILD, | ||
| }); | ||
| } finally { | ||
| // Only clear if no newer drag has been queued while the request was in-flight. | ||
| if (onDragData.current === dragSnapshot) { | ||
| onDragData.current = null; | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| const onDrag = (data: DocDragEndData) => { | ||
| onDragData.current = data; | ||
| if (data.source.nb_accesses_direct <= 1) { | ||
| void handleMoveDoc(); | ||
| return; | ||
| } | ||
| modalConfirmation.open(); | ||
| }; | ||
|
|
||
| const { | ||
| selectedDoc, | ||
| canDrag, | ||
| canDrop, | ||
| sensors, | ||
| handleDragStart, | ||
| handleDragEnd, | ||
| handleDragCancel, | ||
| updateCanDrop, | ||
| } = useDragAndDrop(onDrag); | ||
|
|
||
| const dndAccessibility = useMemo( | ||
| () => ({ | ||
| screenReaderInstructions: { | ||
| draggable: t( | ||
| 'To pick up a draggable item, press space or enter. While dragging, use the arrow keys to move the item. Press space or enter again to drop the item in its new position, or press escape to cancel.', | ||
| ), | ||
| }, | ||
| announcements: { | ||
| onDragStart({ active }: { active: { id: UniqueIdentifier } }) { | ||
| return t('Picked up document {{id}}.', { id: active.id }); | ||
| }, | ||
| onDragOver({ | ||
| active, | ||
| over, | ||
| }: { | ||
| active: { id: UniqueIdentifier }; | ||
| over: { id: UniqueIdentifier } | null; | ||
| }) { | ||
| if (over) { | ||
| return t('Document {{activeId}} is over document {{overId}}.', { | ||
| activeId: active.id, | ||
| overId: over.id, | ||
| }); | ||
| } | ||
| return t('Document {{id}} is no longer over a droppable area.', { | ||
| id: active.id, | ||
| }); | ||
| }, | ||
| onDragEnd({ | ||
| active, | ||
| over, | ||
| }: { | ||
| active: { id: UniqueIdentifier }; | ||
| over: { id: UniqueIdentifier } | null; | ||
| }) { | ||
| if (over) { | ||
| return t( | ||
| 'Document {{activeId}} was dropped over document {{overId}}.', | ||
| { activeId: active.id, overId: over.id }, | ||
| ); | ||
| } | ||
| return t('Document {{id}} was dropped.', { id: active.id }); | ||
| }, | ||
| onDragCancel({ active }: { active: { id: UniqueIdentifier } }) { | ||
| return t('Dragging was cancelled. Document {{id}} was returned to its original position.', { | ||
| id: active.id, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }, | ||
| }, | ||
| }), | ||
| [t], | ||
| ); | ||
|
|
||
| const overlayText = useMemo(() => { | ||
| if (!canDrag) { | ||
| return t('You must be the owner to move the document'); | ||
| } | ||
| if (canDrop === false) { | ||
| return t('You must be at least the administrator of the target document'); | ||
| } | ||
| return selectedDoc?.title || untitledDocument; | ||
| }, [canDrag, canDrop, selectedDoc?.title, t, untitledDocument]); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const cannotMoveDoc = | ||
| !canDrag || (canDrop !== undefined && !canDrop) || isError; | ||
|
|
||
| const [isDraggableDisabled, setIsDraggableDisabled] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| const checkModal = () => { | ||
| const modalOpen = document.querySelector('[role="dialog"]'); | ||
| setIsDraggableDisabled(!!modalOpen); | ||
| }; | ||
| checkModal(); | ||
| const observer = new MutationObserver(checkModal); | ||
| observer.observe(document.body, { childList: true, subtree: true }); | ||
| return () => observer.disconnect(); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <DocDndCtx.Provider | ||
| value={{ selectedDoc, canDrag, canDrop, updateCanDrop, isDraggableDisabled }} | ||
| > | ||
| <DndContext | ||
| sensors={sensors} | ||
| modifiers={[snapToTopLeft]} | ||
| onDragStart={handleDragStart} | ||
| onDragEnd={handleDragEnd} | ||
| onDragCancel={handleDragCancel} | ||
| accessibility={dndAccessibility} | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| > | ||
| {children} | ||
| <DragOverlay dropAnimation={null}> | ||
| <Card | ||
| $width="fit-content" | ||
| $radius="12px" | ||
| data-testid="drag-doc-overlay" | ||
| role="alert" | ||
| aria-label={t('Drag and drop status')} | ||
| $theme={cannotMoveDoc ? 'error' : 'brand'} | ||
| $variation="tertiary" | ||
| $scope="semantic" | ||
| > | ||
| <Text $size="xs" $weight="500" $withThemeInherited> | ||
| {overlayText} | ||
| </Text> | ||
| </Card> | ||
| </DragOverlay> | ||
| </DndContext> | ||
| {modalConfirmation.isOpen && ( | ||
| <ModalConfirmationMoveDoc | ||
| isOpen={modalConfirmation.isOpen} | ||
| onClose={modalConfirmation.onClose} | ||
| onConfirm={handleMoveDoc} | ||
| targetDocumentTitle={ | ||
| onDragData.current?.target.title || untitledDocument | ||
| } | ||
| /> | ||
| )} | ||
| </DocDndCtx.Provider> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.