Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions src/file-attachment-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ function onDragenter(event: DragEvent) {
if (dragging) {
clearTimeout(dragging)
}

const dragEvent = new CustomEvent('file-attachment-dragged', {
bubbles: true,
cancelable: true,
detail: {
dragEvent: event,
attachmentElement: target
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about calling this target instead? attachmentElement made me instinctually think it was the thing being dragged, rather than the element we are dragging over

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good distinction! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've now removed the custom event entirely 🎉

}
})
target.dispatchEvent(dragEvent)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't check if the event was cancelled. Do we need the event to be cancellable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koddsson that's a really good question! Even though it doesn't need to be for https://github.com/github/github/pull/190219, since this event could be used for any number of things down the road being able to preventDefault() on it and then checking if the event was cancelled might be useful. I'll set cancelable: true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, Wednesday morning 🤦. It already is cancellable. Really I could go either way. I don't think we're going to regret having it be cancellable, but in truth I don't think we'll ever need to check if it's been cancelled. Probably less is more in this case, but I'll wait to hear back.


dragging = window.setTimeout(() => target.removeAttribute('hover'), 200)

const transfer = event.dataTransfer
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ describe('file-attachment', function () {
assert.equal('test.png', event.detail.attachments[0].file.name)
assert.equal(0, input.files.length)
})

it('fires a file-attachment-dragged event on dragenter', async function () {
const listener = once('file-attachment-dragged')
const dragEvent = new Event('dragenter', {bubbles: true})
input.dispatchEvent(dragEvent)
const event = await listener
assert.equal(dragEvent, event.detail.dragEvent)
assert.equal(fileAttachment, event.detail.attachmentElement)
})

it('fires a file-attachment-dragged event on dragover', async function () {
const listener = once('file-attachment-dragged')
const dragEvent = new Event('dragover', {bubbles: true})
input.dispatchEvent(dragEvent)
const event = await listener
assert.equal(dragEvent, event.detail.dragEvent)
assert.equal(fileAttachment, event.detail.attachmentElement)
})
})
})

Expand Down