Skip to content

Commit

Permalink
fix swapEnd hasChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
TahaSh committed Sep 30, 2024
1 parent 7b65f90 commit fb04c4b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/veloxi-plugin/SwapyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,15 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
const dragEventPlugin = context.useEventPlugin(DragEventPlugin)
dragEventPlugin.on(DragEvent, onDrag)

const MAX_SCROLL_SPEED = 20
const MAX_SCROLL_THRESHOLD = 100

let root: View
let scrollContainer: ScrollContainer
let slots: View[]
let items: View[]
let draggingItem: View
let slotItemMapOnDragStart: SwapEventMap | null = null
let slotItemMap: SwapEventMap = new Map()
let previousSlotItemMap: SwapEventMap = new Map()
let offsetX: number | null
Expand All @@ -152,6 +156,8 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
let hasSwapped: boolean = false
let triggerSwap = () => {}
let shouldAutoScrollOnDrag = false
let scrollDY = 0
let scrollDX = 0

context.api({
setEnabled(isEnabled) {
Expand Down Expand Up @@ -209,7 +215,6 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (

function prepareSwap(newSlotItemMap: SwapEventMap) {
return () => {
hasSwapped = !mapsAreEqual(newSlotItemMap, previousSlotItemMap)
slotItemMap = newSlotItemMap
previousSlotItemMap = new Map(slotItemMap)
}
Expand Down Expand Up @@ -326,10 +331,6 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
draggingItem.scale.x !== 1 || draggingItem.scale.y !== 1
)
}
const MAX_SCROLL_SPEED = 20
const MAX_SCROLL_THRESHOLD = 100
let scrollDY = 0
let scrollDX = 0

context.subscribeToEvents((eventBus: EventBus) => {
eventBus.subscribeToEvent(Events.PointerMoveEvent, ({ x, y }) => {
Expand Down Expand Up @@ -413,6 +414,9 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
startedDragging = true
context.emit(SwapStartEvent, {})
}
if (slotItemMapOnDragStart === null) {
slotItemMapOnDragStart = new Map(slotItemMap)
}
draggingEvent = event
updateDraggingPosition()
slots.forEach((slot) => {
Expand Down Expand Up @@ -491,12 +495,15 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
}
triggerSwap = () => {}

hasSwapped = !mapsAreEqual(slotItemMap, slotItemMapOnDragStart!)

context.emit(SwapEndEvent, {
data: createEventData(slotItemMap),
hasChanged: hasSwapped
})

hasSwapped = false
slotItemMapOnDragStart = null
scrollContainer.endScrollTracking()
}
requestAnimationFrame(() => {
Expand Down

0 comments on commit fb04c4b

Please sign in to comment.