Skip to content
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

Pragmatic drag and drop #138

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"lint:fix": "eslint --fix ."
},
"dependencies": {
"@atlaskit/pragmatic-drag-and-drop": "^1.1.9",
"@preact/signals": "^1.2.3",
"colorjs.io": "^0.5.0",
"preact": "^10.22.0",
Expand Down
53 changes: 43 additions & 10 deletions src/components/BoardItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,69 @@ import { Link } from "wouter-preact"
import { JSX } from "preact/jsx-runtime"
import { Pos } from "../types/pos"
import { Board } from "../types/board"
import { useEffect, useRef } from "preact/hooks"
import { draggable, dropTargetForElements, monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter"
import { combine } from "@atlaskit/pragmatic-drag-and-drop/combine"

export default function BoardItem(
{
board,
pos,
cardsNum,
handleDragEnd,
handleDragOver,
handleDragStart,
handleDrop
handleDrop,
}: {
board: Omit<Board, 'lists'>
pos: Pos
cardsNum: number
handleDragEnd: (e: JSX.TargetedDragEvent<HTMLDivElement>) => void
handleDragOver: (e: JSX.TargetedDragEvent<HTMLDivElement>) => void
handleDragStart: (e: JSX.TargetedDragEvent<HTMLDivElement>) => void
handleDrop: (e: JSX.TargetedDragEvent<HTMLDivElement>) => void
handleDrop: (elemTarget: HTMLDivElement, elemSource: HTMLDivElement) => void
}
) {
const ref = useRef<HTMLDivElement>(null)

useEffect(() => {
const el = ref.current
if (el) {
return combine(
draggable({
element: el,
onDragStart: () => {
console.log('draggable: drag start')
},
onDrop: () => {
console.log('draggable: drop')
},
}),
dropTargetForElements({
element: el,
onDragEnter: () => {
console.log('target: drag enter')
},
onDragLeave: () => {
console.log('target: drag leave')
},
onDrop: () => {
console.log('target: drop')
}
}),
monitorForElements({
onDrop({ location, source }) {
const target = location.current.dropTargets[0]
handleDrop(target.element as HTMLDivElement, source.element as HTMLDivElement)
},
}),
)
}
}, [])

return (
<div
class={`flex-column h-20 bg-primary parent-hiding-child border-solid border-2 border-color-primary hover-bg-board-item bg-${board.bgColor ? board.bgColor : 'primary'}`}
draggable
onDragEnd={handleDragEnd}
onDragOver={handleDragOver}
onDragStart={handleDragStart}
onDrop={handleDrop}
data-board-id={board.id}
data-pos={pos}
ref={ref}
>
<Link
class="p-4 cursor-pointer flex-column layout-stack-2 text-decoration-none text-primary"
Expand Down
8 changes: 1 addition & 7 deletions src/components/BoardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@ import IconClose from "./IconClose"
export default function BoardList(
{
boards,
handleDragEnd,
handleDragOver,
handleDragStart,
handleDrop,
handleToggleDialog
}: {
boards: Board[]
handleDragEnd: () => void
handleDragOver: (e: JSX.TargetedDragEvent<HTMLDivElement>) => void
handleDragStart: (e: JSX.TargetedDragEvent<HTMLDivElement>) => void
handleDrop: (e: JSX.TargetedDragEvent<HTMLDivElement>) => void
handleDrop: (elemTarget: HTMLDivElement, elemSource: HTMLDivElement) => void
handleToggleDialog: (e: JSX.TargetedMouseEvent<HTMLButtonElement>) => void
}
) {
Expand Down Expand Up @@ -81,9 +77,7 @@ export default function BoardList(
board={board}
pos={idx === 0 ? "first" : (idx === (boards.length - 1) ? "last" : "middle")}
cardsNum={board.lists.map(l => l.cards.length).reduce((accumulator, value) => { return accumulator + value }, 0)}
handleDragEnd={handleDragEnd}
handleDragOver={handleDragOver}
handleDragStart={handleDragStart}
handleDrop={handleDrop}
/>
)
Expand Down
4 changes: 0 additions & 4 deletions src/components/PageComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ export default function PageComponents() {
board={board1}
pos="first"
cardsNum={8}
handleDragEnd={() => { }}
handleDragOver={() => { }}
handleDragStart={() => { }}
handleDrop={() => { }}
/>
</div>
Expand All @@ -125,9 +123,7 @@ export default function PageComponents() {
<div>
<BoardList
boards={boards}
handleDragEnd={() => { }}
handleDragOver={() => { }}
handleDragStart={() => { }}
handleDrop={() => { }}
handleToggleDialog={() => { }}
/>
Expand Down
28 changes: 8 additions & 20 deletions src/components/PageIndex.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from 'preact/hooks'
import { useRef } from 'preact/hooks'
import { JSX } from 'preact/jsx-runtime'
import { Signal } from '@preact/signals'
import { ApplicationService } from '../applications/applicationService'
Expand All @@ -14,7 +14,6 @@ import IconMoreHoriz from './IconMoreHoriz'
import { showBoardDialog } from '../main'

export default function PageIndex({ appState }: { appState: Signal<State> }) {
const [draggingBoardId, setDraggingBoardId] = useState<string | undefined>(undefined)
const detailsElement = useRef<HTMLDetailsElement>(null)
const repository = new RepositoryLocalStorage()
const service = new ApplicationService(repository)
Expand Down Expand Up @@ -56,24 +55,15 @@ export default function PageIndex({ appState }: { appState: Signal<State> }) {
}
}

const handleDragEnd = () => setDraggingBoardId(undefined)

const handleDragOver = (e: JSX.TargetedDragEvent<HTMLDivElement>) => e.preventDefault()

const handleDragStart = (e: JSX.TargetedDragEvent<HTMLDivElement>) => {
if (e.dataTransfer) {
e.dataTransfer.effectAllowed = 'move'
}
const { boardId } = e.currentTarget.dataset
if (boardId) {
setTimeout(() => { setDraggingBoardId(boardId) }, 100)
}
}

const handleDrop = (e: JSX.TargetedDragEvent<HTMLDivElement>) => {
const { boardId, pos } = e.currentTarget.dataset
if (boardId && pos && draggingBoardId) {
appState.value = service.moveBoard(appState.value, draggingBoardId, pos as Pos, boardId)
const handleDrop = (elemTarget: HTMLDivElement, elemSource: HTMLDivElement) => {
const { boardId, pos } = elemTarget.dataset
if (boardId && pos) {
const sourceBoardId = elemSource.dataset.boardId
if (sourceBoardId) {
appState.value = service.moveBoard(appState.value, sourceBoardId, pos as Pos, boardId)
}
}
}

Expand Down Expand Up @@ -160,9 +150,7 @@ export default function PageIndex({ appState }: { appState: Signal<State> }) {
<div class="overflow-y-auto">
<BoardList
boards={appState.value.boards}
handleDragEnd={handleDragEnd}
handleDragOver={handleDragOver}
handleDragStart={handleDragStart}
handleDrop={handleDrop}
handleToggleDialog={handleToggleDialog}
/>
Expand Down
Loading