From 61fe24f8582e319af3e2a1c405bde5db0e36c224 Mon Sep 17 00:00:00 2001 From: Andrio Celos Date: Fri, 1 Sep 2023 09:48:55 +1000 Subject: [PATCH] Fix drag and drop on Chrome --- TableturfBattleClient/src/Pages/DeckEditPage.ts | 10 ++++------ TableturfBattleClient/src/Pages/DeckListPage.ts | 10 ++++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/TableturfBattleClient/src/Pages/DeckEditPage.ts b/TableturfBattleClient/src/Pages/DeckEditPage.ts index 9184628..0e352cc 100644 --- a/TableturfBattleClient/src/Pages/DeckEditPage.ts +++ b/TableturfBattleClient/src/Pages/DeckEditPage.ts @@ -174,8 +174,7 @@ function createDeckEditCardButton(cardNumber: number) { function deckEditCardButton_dragover(e: DragEvent) { e.preventDefault(); if (e.dataTransfer == null) return; - const indexString = e.dataTransfer.getData('application/tableturf-card-index'); - if (indexString != '' && draggingCardButton != null) { + if (draggingCardButton != null) { e.dataTransfer.dropEffect = 'move'; if (e.currentTarget != draggingCardButton && e.currentTarget != deckCardListEdit) { // Move the card being dragged into the new position as a preview. @@ -194,15 +193,14 @@ function deckEditCardButton_dragover(e: DragEvent) { function deckEditCardButton_drop(e: DragEvent) { e.preventDefault(); if (e.dataTransfer == null) return; - const indexString = e.dataTransfer.getData('application/tableturf-card-index'); - if (indexString) { - const index = parseInt(indexString); + if (draggingCardButton != null) { + const index = deckEditCardButtons.entries.findIndex(el => el.button.buttonElement == draggingCardButton); let newIndex = 0; for (let el = deckCardListEdit.firstElementChild; el != null; el = el.nextElementSibling) { if (el == draggingCardButton) break; newIndex++; } - if (newIndex == index) return; + if (index < 0 || newIndex == index) return; console.log(`Moving card ${index} to ${newIndex}.`); deckEditCardButtons.move(index, newIndex); diff --git a/TableturfBattleClient/src/Pages/DeckListPage.ts b/TableturfBattleClient/src/Pages/DeckListPage.ts index e980f8e..35e1939 100644 --- a/TableturfBattleClient/src/Pages/DeckListPage.ts +++ b/TableturfBattleClient/src/Pages/DeckListPage.ts @@ -152,8 +152,7 @@ function createDeckButton(deck: Deck) { function deckButton_dragover(e: DragEvent) { e.preventDefault(); if (e.dataTransfer == null) return; - const indexString = e.dataTransfer.getData('application/tableturf-deck-index'); - if (indexString != '' && draggingDeckButton != null) { + if (draggingDeckButton != null) { e.dataTransfer.dropEffect = 'move'; if (e.currentTarget != draggingDeckButton && e.currentTarget != deckList) { // Move the deck being dragged into the new position as a preview. @@ -172,15 +171,14 @@ function deckButton_dragover(e: DragEvent) { function deckButton_drop(e: DragEvent) { e.preventDefault(); if (e.dataTransfer == null) return; - const indexString = e.dataTransfer.getData('application/tableturf-deck-index'); - if (indexString) { - const index = parseInt(indexString); + if (draggingDeckButton != null) { + const index = deckButtons.entries.findIndex(el => el.button.buttonElement == draggingDeckButton); let newIndex = 0; for (let el = deckList.firstElementChild; el != null; el = el.nextElementSibling) { if (el == draggingDeckButton) break; newIndex++; } - if (newIndex == index) return; + if (index < 0 || newIndex == index) return; console.log(`Moving deck ${index} to ${newIndex}.`); deckButtons.move(index, newIndex);