-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Drag and Drop functionality to Breakout Rooms
- Loading branch information
Showing
9 changed files
with
391 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -6,6 +6,9 @@ | |
"author": "Håvar Aambø Fosstveit <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@dnd-kit/core": "^6.3.1", | ||
"@dnd-kit/modifiers": "^9.0.0", | ||
"@dnd-kit/utilities": "^3.2.2", | ||
"@emotion/cache": "^11.13.1", | ||
"@emotion/react": "^11.13.3", | ||
"@emotion/styled": "^11.13.0", | ||
|
This file contains 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
153 changes: 153 additions & 0 deletions
153
src/components/draganddrop/DragAndDropContextWrapper.tsx
This file contains 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,153 @@ | ||
import React from 'react'; | ||
import { | ||
DndContext, | ||
DragEndEvent, | ||
DragOverEvent, | ||
DragStartEvent, | ||
KeyboardSensor, | ||
PointerSensor, | ||
useSensor, | ||
useSensors | ||
} from '@dnd-kit/core'; | ||
import { moveToBreakoutRoom } from '../../store/actions/roomActions'; | ||
import { Peer } from '../../store/slices/peersSlice'; | ||
import { roomSessionsActions } from '../../store/slices/roomSessionsSlice'; | ||
import { useAppDispatch, useAppSelector } from '../../store/hooks'; | ||
import { parentParticipantListSelector, currentRoomSessionSelector, peersArraySelector, roomSessionsArraySelector } from '../../store/selectors'; | ||
|
||
type DndContextWrapperProps = { | ||
children: React.ReactNode; | ||
// eslint-disable-next-line no-unused-vars | ||
setShowParticipantsList: (showParticipantsList: Peer[]) => void; | ||
// eslint-disable-next-line no-unused-vars | ||
setDragOver: (dragOver: string) => void; | ||
draggedPeerIds: string[]; | ||
// eslint-disable-next-line no-unused-vars | ||
setDraggedPeerIds: (draggedPeerIds: string[]) => void; | ||
activePeer: Peer | null; | ||
// eslint-disable-next-line no-unused-vars | ||
setActivePeer: (activePeer: Peer | null) => void; | ||
} | ||
|
||
const DndContextWrapper = ({ children, setShowParticipantsList, setDragOver, draggedPeerIds, setDraggedPeerIds, activePeer, setActivePeer }: DndContextWrapperProps): JSX.Element => { | ||
const dispatch = useAppDispatch(); | ||
const participants = useAppSelector(parentParticipantListSelector); | ||
const currentRoom = useAppSelector(currentRoomSessionSelector); | ||
const totalPeers = useAppSelector(peersArraySelector); | ||
const allRooms = useAppSelector(roomSessionsArraySelector); | ||
const selectedPeersMap = new Map(); | ||
|
||
for (let i = 0; i < allRooms.length; i++) { | ||
selectedPeersMap.set(allRooms[i].sessionId, allRooms[i].selectedPeers); | ||
} | ||
|
||
// Drag activation constraints | ||
const pointerSensor = useSensor(PointerSensor, { | ||
activationConstraint: { | ||
distance: 10, | ||
} | ||
}); | ||
const keyboardSensor = useSensor(KeyboardSensor); | ||
const sensors = useSensors( | ||
pointerSensor, | ||
keyboardSensor, | ||
); | ||
|
||
// Function to deselect all peers not being dragged | ||
const deselectPeers = (ids: string[]) => { | ||
selectedPeersMap.forEach((selectedPeers: string[], roomid: string) => { | ||
selectedPeers.forEach((peerId) => { | ||
if (!ids.includes(peerId)) { | ||
dispatch(roomSessionsActions.deselectPeer({ sessionId: roomid, peerId: peerId })); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
const handleDragStart = (event: DragStartEvent) => { | ||
const { active } = event; | ||
const activeId = active.id as string; | ||
const draggedPeer = totalPeers.find((peer) => peer.id == activeId); | ||
|
||
// Ensure we are dragging a peer | ||
if (!draggedPeer) { | ||
return; | ||
} | ||
|
||
const dragging = !currentRoom.selectedPeers.includes(activeId) ? [ activeId ] : [ ...new Set([ ...currentRoom.selectedPeers, activeId ]) ]; | ||
const newList = participants.filter((peer) => !dragging.includes(peer.id)); | ||
const deselect = !Array.from(selectedPeersMap.values()).flat() | ||
.every((peerId) => dragging.includes(peerId)); | ||
|
||
// Deselect peers if there are selected peers not being dragged | ||
if (deselect) { | ||
deselectPeers(dragging); | ||
} | ||
|
||
setActivePeer(draggedPeer); | ||
setDraggedPeerIds(dragging); | ||
setShowParticipantsList(newList); | ||
}; | ||
|
||
const handleDragEnd = (event: DragEndEvent) => { | ||
const { over } = event; | ||
|
||
// Ensure over some droppable component | ||
if (!over) { | ||
setShowParticipantsList(participants); | ||
setDraggedPeerIds([]); | ||
setActivePeer(null); | ||
|
||
return; | ||
} | ||
|
||
const roomId = over.id as string; | ||
|
||
// Drop peer if peer is over a new breakout room | ||
if (roomId !== activePeer?.sessionId && draggedPeerIds) { | ||
draggedPeerIds.forEach((peerId) => { | ||
dispatch(moveToBreakoutRoom(peerId, roomId, currentRoom.sessionId)); | ||
}); | ||
} | ||
|
||
// Reset when drag has ended | ||
// setShowParticipantsList(participants); // Uncomment if necessary but will produce a visual glitch | ||
setDraggedPeerIds([]); | ||
setActivePeer(null); | ||
setDragOver(''); | ||
}; | ||
|
||
const handleDragCancel = () => { | ||
// Reset all when drag is cancelled | ||
setShowParticipantsList(participants); | ||
setDraggedPeerIds([]); | ||
setActivePeer(null); | ||
setDragOver(''); | ||
}; | ||
|
||
const handleDragOver = (event: DragOverEvent) => { | ||
const { over } = event; | ||
|
||
if (!over) { | ||
setDragOver(''); | ||
|
||
return; | ||
} | ||
|
||
setDragOver(over.id as string); | ||
}; | ||
|
||
return ( | ||
<DndContext | ||
sensors={sensors} | ||
onDragStart={handleDragStart} | ||
onDragEnd={handleDragEnd} | ||
onDragCancel={handleDragCancel} | ||
onDragOver={handleDragOver} | ||
> | ||
{children} | ||
</DndContext> | ||
); | ||
}; | ||
|
||
export default DndContextWrapper; |
This file contains 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,31 @@ | ||
import React from 'react'; | ||
import { useDraggable } from '@dnd-kit/core'; | ||
|
||
type DraggableElementProps = { | ||
children: React.ReactNode; | ||
id: string; | ||
disabled: boolean; | ||
}; | ||
|
||
const DraggableWrapper = ({ children, id, disabled }: DraggableElementProps): JSX.Element => { | ||
const { attributes, listeners, setNodeRef } = useDraggable({ | ||
id: id, | ||
disabled: disabled | ||
}); | ||
const style ={ | ||
cursor: disabled ? 'auto' : 'grab' | ||
}; | ||
|
||
return ( | ||
<div | ||
ref={setNodeRef} | ||
{...attributes} | ||
{...listeners} | ||
style={style} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default DraggableWrapper; |
This file contains 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,23 @@ | ||
import React from 'react'; | ||
import { useDroppable } from '@dnd-kit/core'; | ||
|
||
type DroppableElementProps = { | ||
children: React.ReactNode; | ||
id: string; | ||
}; | ||
|
||
const DroppableWrapper = ({ children, id }: DroppableElementProps): JSX.Element => { | ||
const { setNodeRef } = useDroppable({ | ||
id: id, | ||
}); | ||
|
||
return ( | ||
<div | ||
ref={setNodeRef} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default DroppableWrapper; |
This file contains 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,37 @@ | ||
import { Box, styled } from '@mui/material'; | ||
|
||
type GhostObjectProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
// eslint-disable-next-line no-empty-pattern | ||
const GhostDiv = styled(Box)(({ }) => ({ | ||
opacity: 0.7, | ||
animation: 'pulse 3s ease-in-out 0.5s infinite', | ||
|
||
'@keyframes pulse': { | ||
'0%': { | ||
opacity: 0.9, | ||
}, | ||
|
||
'50%': { | ||
opacity: 0.6, | ||
}, | ||
|
||
'100%': { | ||
opacity: 0.9, | ||
}, | ||
} | ||
})); | ||
|
||
// Pulsing object wrapper to show were peers wil be dropped | ||
const GhostObject = ({ children }: GhostObjectProps): JSX.Element => { | ||
|
||
return ( | ||
<GhostDiv> | ||
{children} | ||
</GhostDiv> | ||
); | ||
}; | ||
|
||
export default GhostObject; |
Oops, something went wrong.