Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue dropping with out placeholder #736

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions __tests__/events/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,59 @@ describe('Testing events', () => {
expect(event.dataTransfer.dropEffect).toBe(undefined)
})

test('should correctly drop item in place without placeholder', () => {
jest.useFakeTimers()

const placeholder = document.createElement('DIV')
sortable(ul, {
items: 'li',
copy: true,
acceptFrom: false,
placeholderClass: 'test-placeholder'
})

sortable(ul2, {
items: 'li',
acceptFrom: 'ul',
placeholder,
placeholderClass: 'test-placeholder2'
})

let childcount = ul.children.length

let event = new CustomEvent('dragstart')
event.dataTransfer = dataTransferObj
Object.defineProperty(event, 'target', {value: li, enumerable: true})
ul.dispatchEvent(event)

event = new CustomEvent('dragover')
event.dataTransfer = dataTransferObj
fifthLi.dispatchEvent(event)
jest.advanceTimersByTime(1)
expect(getIndex(placeholder, ul2.children)).not.toEqual(-1)
expect(ul.children.length).toEqual(childcount + 1)

event = new CustomEvent('drop')
ul2.dispatchEvent(event)
expect(ul.children.length).toEqual(childcount)

event = new CustomEvent('dragstart')
event.dataTransfer = dataTransferObj
Object.defineProperty(event, 'target', {value: li, enumerable: true})
ul.dispatchEvent(event)

event = new CustomEvent('dragover')
event.dataTransfer = dataTransferObj
ul2.dispatchEvent(event)
jest.advanceTimersByTime(1)
expect(getIndex(placeholder, ul2.children)).toEqual(-1)
expect(ul.children.length).toEqual(childcount + 1)

event = new CustomEvent('drop')
ul2.dispatchEvent(event)
expect(ul.children.length).toEqual(childcount + 1)
})

test.skip('should correctly place moved item into correct index', () => {
sortable(ul, {
items: 'li',
Expand Down
15 changes: 10 additions & 5 deletions src/html5sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,16 @@ export default function sortable (sortableElements, options: configuration|objec
.filter(placeholder => placeholder instanceof HTMLElement)
// only elements in DOM
.filter(isInDom)[0]
// attach element after placeholder
after(visiblePlaceholder, dragging)
// remove placeholder from dom
visiblePlaceholder.remove()

if (visiblePlaceholder) {
// attach element after placeholder
after(visiblePlaceholder, dragging)
// remove placeholder from dom
visiblePlaceholder.remove()
} else {
// set the dropped value to 'false' to delete copied dragging at the time of 'dragend'
data(dragging, 'dropped', 'false')
return
}
/*
* Fires Custom Event - 'sortstop'
*/
Expand Down