-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ages-d&d Feature/#501 allow rearrange pages d&d
- Loading branch information
Showing
9 changed files
with
140 additions
and
30 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
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
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
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
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
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,58 @@ | ||
import { useCanvasContext } from '@/core/providers'; | ||
import { | ||
draggable, | ||
dropTargetForElements, | ||
} from '@atlaskit/pragmatic-drag-and-drop/element/adapter'; | ||
import { useEffect, useState } from 'react'; | ||
import invariant from 'tiny-invariant'; | ||
|
||
export const useDragDropThumb = ( | ||
divRef: React.RefObject<HTMLDivElement>, | ||
pageIndex: number | ||
) => { | ||
const { fullDocument } = useCanvasContext(); | ||
const page = fullDocument.pages[pageIndex]; | ||
const [dragging, setDragging] = useState<boolean>(false); | ||
const [isDraggedOver, setIsDraggedOver] = useState(false); | ||
|
||
// Drag | ||
useEffect(() => { | ||
const el = divRef.current; | ||
invariant(el); | ||
return draggable({ | ||
element: el, | ||
getInitialData: () => ({ | ||
pageId: page.id, //fullDocument.pages[pageIndex].id, | ||
type: 'thumbPage', | ||
}), | ||
onDragStart: () => { | ||
setDragging(true); | ||
}, | ||
onDrop: () => setDragging(false), | ||
}); | ||
}, [divRef.current, pageIndex, fullDocument.pages]); | ||
|
||
// Drop | ||
useEffect(() => { | ||
const el = divRef.current; | ||
invariant(el); | ||
|
||
return dropTargetForElements({ | ||
element: el, | ||
getData: () => ({ | ||
pageId: page.id, //fullDocument.pages[pageIndex].id, | ||
type: 'thumbPage', | ||
}), | ||
onDragEnter: () => setIsDraggedOver(true), | ||
onDragLeave: () => setIsDraggedOver(false), | ||
onDrop: () => { | ||
setIsDraggedOver(false); | ||
}, | ||
}); | ||
}, [divRef.current, pageIndex, fullDocument.pages]); | ||
|
||
return { | ||
dragging, | ||
isDraggedOver, | ||
}; | ||
}; |
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
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,25 @@ | ||
import { useEffect } from 'react'; | ||
import { monitorForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter'; | ||
import { useCanvasContext } from '@/core/providers'; | ||
|
||
export const useMonitorDropThumb = () => { | ||
const { fullDocument, swapPages } = useCanvasContext(); | ||
|
||
// Monitor | ||
useEffect(() => { | ||
return monitorForElements({ | ||
onDrop({ source, location }) { | ||
const destination = location.current.dropTargets[0]; | ||
if (!destination || source.data.pageId === destination.data.pageId) { | ||
return; | ||
} | ||
if (destination.data.type === 'thumbPage') { | ||
swapPages( | ||
String(source.data.pageId), | ||
String(destination.data.pageId) | ||
); | ||
} | ||
}, | ||
}); | ||
}, [fullDocument.pages]); | ||
}; |
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