Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
TahaSh committed Aug 5, 2024
1 parent c74cdcf commit 6655c8e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ class Swapy {

private _applyOrder(map: Map<string, string | null>) {
Array.from(map.keys()).forEach((slotName) => {
if (map.get(slotName) === this._previousMap?.get(slotName)) {
return
}
const itemName = map.get(slotName)
if (!itemName) return
const slot = this._slotElMap.get(slotName)
Expand Down
26 changes: 17 additions & 9 deletions src/veloxi-plugin/SwapyPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DragEvent, DragEventPlugin, PluginFactory, View } from 'veloxi'
import { AnimationType, Config } from '../instance'
import { mapsAreEqual } from '../utils'

type SwapEventArray = Array<{ slot: string; item: string | null }>
type SwapEventMap = Map<string, string | null>
Expand Down Expand Up @@ -62,13 +63,15 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
let items: View[]
let draggingItem: View
let slotItemMap: SwapEventMap = new Map()
let previousSlotItemMap: SwapEventMap = new Map()
let offsetX: number | null
let offsetY: number | null
let initialWidth: number | null
let initialHeight: number | null
let enabled = true
let draggingEvent: DragEvent | null
let isContinuousMode: boolean
let draggingSlot: View | null

context.api({
setEnabled(isEnabled) {
Expand Down Expand Up @@ -125,13 +128,16 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (

setupRemainingChildren()

previousSlotItemMap = new Map(slotItemMap)
context.emit(InitEvent, { data: createEventData(slotItemMap) })
})

function setupItem(item: View) {
const animation = getAnimation()
item.styles.position = 'relative'
item.styles.touchAction = 'none'
item.styles.userSelect = 'none'
item.styles.webkitUserSelect = 'none'
item.position.setAnimator(animation.animator, animation.config)
item.scale.setAnimator(animation.animator, animation.config)
item.layoutTransition(true)
Expand All @@ -152,6 +158,7 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
setupItem(view)
setupRemainingChildren()
items = context.getViews('item')
previousSlotItemMap = new Map(slotItemMap)
context.emit(SwapEvent, { data: createEventData(slotItemMap) })
}
})
Expand Down Expand Up @@ -200,11 +207,13 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
if (!enabled) return
const withHandle = event.view.name === 'handle'
draggingItem = withHandle ? event.view.getParent('item')! : event.view
if (!draggingSlot) {
draggingSlot = draggingItem.getParent('slot')!
}
if (event.isDragging) {
draggingEvent = event
updateDraggingPosition()
slots.forEach((slot) => {
const draggingSlot = draggingItem.getParent('slot')!
if (!slot.intersects(event.pointerX, event.pointerY)) {
if (slot !== draggingSlot) {
slot.element.removeAttribute('data-swapy-highlighted')
Expand All @@ -219,7 +228,7 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
}
const targetSlotName = slot.element.dataset.swapySlot
const targetItemName = slot.getChild('item')?.element.dataset.swapyItem
const draggingSlotName = draggingSlot.element.dataset.swapySlot
const draggingSlotName = draggingSlot!.element.dataset.swapySlot
const draggingItemName = draggingItem.element.dataset.swapyItem
if (!targetSlotName || !draggingSlotName || !draggingItemName) {
return
Expand All @@ -230,23 +239,22 @@ export const SwapyPlugin: PluginFactory<SwapyConfig, SwapyPluginApi> = (
} else {
slotItemMap.set(draggingSlotName, null)
}
context.emit(SwapEvent, { data: createEventData(slotItemMap) })
if (!mapsAreEqual(slotItemMap, previousSlotItemMap)) {
previousSlotItemMap = new Map(slotItemMap)
draggingSlot = null
context.emit(SwapEvent, { data: createEventData(slotItemMap) })
}
})

items.forEach((item) => {
item.styles.zIndex = item === draggingItem ? '2' : ''
item.styles.userSelect = 'none'
item.styles.webkitUserSelect = 'none'
})
} else {
slots.forEach((slot) => {
slot.element.removeAttribute('data-swapy-highlighted')
})
items.forEach((item) => {
item.styles.userSelect = ''
item.styles.webkitUserSelect = ''
})
draggingItem.position.reset()
draggingSlot = null
offsetX = null
offsetY = null
initialWidth = null
Expand Down

0 comments on commit 6655c8e

Please sign in to comment.