Skip to content

Commit

Permalink
fix(ui): default drag actions triggering while dragging cards
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinfreund committed Mar 4, 2024
1 parent c629c31 commit 021d0a0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
25 changes: 18 additions & 7 deletions src/ui/components/DraggableCard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const registeredContainers = new Set<Element>()
let isDragging = false

export class DraggableCard extends HTMLElement {
container: HTMLElement
Expand All @@ -18,27 +19,31 @@ export class DraggableCard extends HTMLElement {
this.container.addEventListener('dragenter', this.handleDragOver)
this.container.addEventListener('dragover', this.handleDragOver)
}

this.addEventListener('dragstart', this.handleDragStart)
this.addEventListener('dragend', this.handleDragEnd)
this.addEventListener('drop', this.handleDrop)
}

handleDragStart = (event: DragEvent) => {
if (event.dataTransfer === null || !(event.target instanceof Element)) {
return
}

if (this.container) {
const draggedEl = Array.from(this.container.children).find((el) => el === event.target)
if (draggedEl) {
event.dataTransfer.effectAllowed = 'move'
event.dataTransfer.dropEffect = 'move'
event.dataTransfer.setData('text/plain', draggedEl.id)
}
const draggedEl = Array.from(this.container.children).find((el) => el === event.target)
if (draggedEl) {
isDragging = true
event.dataTransfer.effectAllowed = 'move'
event.dataTransfer.dropEffect = 'move'
event.dataTransfer.setData('text/plain', draggedEl.id)
}
}

handleDragEnd = (event: DragEvent) => {
const draggedEl = getDragEventData(event, this.container)
if (draggedEl) {
drop(this.container, draggedEl, event.clientX)
isDragging = false
}
}

Expand All @@ -48,6 +53,12 @@ export class DraggableCard extends HTMLElement {
event.preventDefault()
}
}

handleDrop = (event: DragEvent) => {
if (isDragging) {
event.preventDefault()
}
}
}

function getDragEventData (event: DragEvent, container: Element): Element | null {
Expand Down
3 changes: 0 additions & 3 deletions src/ui/components/JokerCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ export class JokerCard extends DraggableCard {

this.suitSelect = this.fragment.querySelector('[data-j-suit]') as HTMLSelectElement
this.suitSelect.name = `joker-suit-${id}`

this.addEventListener('dragstart', this.handleDragStart)
this.addEventListener('dragend', this.handleDragEnd)
}

get [Symbol.toStringTag] () {
Expand Down
2 changes: 0 additions & 2 deletions src/ui/components/PlayingCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export class PlayingCard extends DraggableCard {
this.isPlayedCheckbox.click()
}
}, { capture: true })
this.addEventListener('dragstart', this.handleDragStart)
this.addEventListener('dragend', this.handleDragEnd)
}

get [Symbol.toStringTag] () {
Expand Down

0 comments on commit 021d0a0

Please sign in to comment.