Skip to content

Commit 68c549f

Browse files
feat: extend event object properties data and clonedData
1 parent c008141 commit 68c549f

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/useDraggable.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import type { Fn, RefOrElement, RefOrValue } from './types'
1414
import { error } from './utils/log'
1515

1616
import {
17+
extend,
1718
forEachObject,
1819
getElementBySelector,
1920
insertElement,
2021
insertNodeAt,
2122
isHTMLElement,
23+
isOn,
2224
isString,
2325
isUndefined,
2426
mergeOptionsEvents,
@@ -51,6 +53,24 @@ function tryOnMounted(fn: Fn) {
5153
else nextTick(fn)
5254
}
5355

56+
let data: any = null
57+
let clonedData: any = null
58+
59+
function setCurrentData(
60+
_data: typeof data = null,
61+
_clonedData: typeof data = null
62+
) {
63+
data = _data
64+
clonedData = _clonedData
65+
}
66+
67+
function getCurrentData() {
68+
return {
69+
data,
70+
clonedData
71+
}
72+
}
73+
5474
const CLONE_ELEMENT_KEY = Symbol('cloneElement')
5575

5676
interface DraggableEvent<T = any> extends SortableEvent {
@@ -135,8 +155,7 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
135155
function onStart(evt: DraggableEvent) {
136156
const data = unref(unref(list)?.[evt.oldIndex!])
137157
const clonedData = clone(data)
138-
evt.data = data
139-
evt.clonedData = clonedData
158+
setCurrentData(data, clonedData)
140159
evt.item[CLONE_ELEMENT_KEY] = clonedData
141160
}
142161

@@ -195,14 +214,21 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
195214
moveArrayElement(unref(list), oldIndex!, newIndex!)
196215
}
197216

217+
function onEnd() {
218+
nextTick(() => {
219+
setCurrentData()
220+
})
221+
}
222+
198223
/**
199224
* preset options
200225
*/
201226
const presetOptions: UseDraggableOptions<T> = {
202227
onUpdate,
203228
onStart,
204229
onAdd,
205-
onRemove
230+
onRemove,
231+
onEnd
206232
}
207233

208234
function getTarget(target?: HTMLElement) {
@@ -222,6 +248,16 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
222248
function mergeOptions() {
223249
// eslint-disable-next-line
224250
const { immediate, clone, ...restOptions } = unref(options) ?? {}
251+
252+
forEachObject(restOptions, (key, fn) => {
253+
if (!isOn(key)) return
254+
restOptions[key] = (evt: DraggableEvent, ...args: any[]) => {
255+
const data = getCurrentData()
256+
extend(evt, data)
257+
fn(evt, ...args)
258+
}
259+
})
260+
225261
return mergeOptionsEvents(
226262
list === null ? {} : presetOptions,
227263
restOptions

src/utils/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,18 @@ export function forEachObject<T extends Record<string, any>>(
178178
fn(key, obj[key])
179179
})
180180
}
181+
182+
/**
183+
*
184+
* @param key
185+
*/
186+
export function isOn(key: any) {
187+
return (
188+
key.charCodeAt(0) === 111 /* o */ &&
189+
key.charCodeAt(1) === 110 /* n */ &&
190+
// uppercase letter
191+
(key.charCodeAt(2) > 122 || key.charCodeAt(2) < 97)
192+
)
193+
}
194+
195+
export const extend = Object.assign

0 commit comments

Comments
 (0)