Update PageMenu.tsx

This commit is contained in:
Steve Ruiz 2022-06-29 19:06:56 +01:00
parent 80ed0bf27b
commit d27e7dc249

View file

@ -83,7 +83,7 @@ function PageMenuContent({ onClose }: { onClose: () => void }) {
const handleDragStart = React.useCallback((ev: React.DragEvent<HTMLDivElement>) => { const handleDragStart = React.useCallback((ev: React.DragEvent<HTMLDivElement>) => {
setDragId(ev.currentTarget.id) setDragId(ev.currentTarget.id)
setDropIndex(sortedPages.findIndex((p) => p.id === ev.currentTarget.id))
ev.dataTransfer.effectAllowed = 'move' ev.dataTransfer.effectAllowed = 'move'
}, []) }, [])
@ -91,16 +91,12 @@ function PageMenuContent({ onClose }: { onClose: () => void }) {
(ev: React.DragEvent<HTMLDivElement>) => { (ev: React.DragEvent<HTMLDivElement>) => {
ev.preventDefault() ev.preventDefault()
const dropBox = sortedPages.find((p) => p.id === ev.currentTarget.id) let dropIndex = sortedPages.findIndex((p) => p.id === ev.currentTarget.id)
if (!dropBox) return
const indices = sortedPages.map((p) => p.childIndex ?? 0).sort()
const index = indices.indexOf(dropBox.childIndex ?? 0)
const rect = ev.currentTarget.getBoundingClientRect() const rect = ev.currentTarget.getBoundingClientRect()
const ny = (ev.clientY - rect.top) / rect.height const ny = (ev.clientY - rect.top) / rect.height
const dropIndex = ny < 0.5 ? index : index + 1
dropIndex = ny < 0.5 ? dropIndex : dropIndex + 1
setDropIndex(dropIndex) setDropIndex(dropIndex)
}, },
@ -109,6 +105,7 @@ function PageMenuContent({ onClose }: { onClose: () => void }) {
const handleDrop = React.useCallback(() => { const handleDrop = React.useCallback(() => {
if (dragId !== null && dropIndex !== null) { if (dragId !== null && dropIndex !== null) {
console.log('moving page', app.document.pages[dragId].name, dropIndex)
app.movePage(dragId, dropIndex) app.movePage(dragId, dropIndex)
} }
@ -138,7 +135,9 @@ function PageMenuContent({ onClose }: { onClose: () => void }) {
draggable={true} draggable={true}
> >
<PageButton> <PageButton>
<span id={page.id}>{page.name || 'Page'}</span> <span id={page.id}>
{page.name || 'Page'} - {page.childIndex}
</span>
<DropdownMenu.ItemIndicator> <DropdownMenu.ItemIndicator>
<SmallIcon> <SmallIcon>
<CheckIcon /> <CheckIcon />