-
-
Notifications
You must be signed in to change notification settings - Fork 259
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
Add drag and drop support #1139
Open
JimSchofield
wants to merge
5
commits into
emberjs:master
Choose a base branch
from
JimSchofield:add-drag-and-drop-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8bef563
Add moveDrag helper for drag and drop events
JimSchofield 11f9b63
Adding dragMove test
JimSchofield fc31fec
Adding dragMove test
JimSchofield fad621f
Refactoring event listeners to accomodate ember 3.8 in github ci
JimSchofield 0ae894a
Rebasing off upstream master. Fixing issues
JimSchofield File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
addon/addon-test-support/@ember/test-helpers/dom/drag-move.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { settled } from '..'; | ||
import { registerHook, runHooks } from '../helper-hooks'; | ||
import getElement from './-get-element'; | ||
import { log } from './-logging'; | ||
import Target from './-target'; | ||
import fireEvent from './fire-event'; | ||
|
||
registerHook( | ||
'dragMove', | ||
'start', | ||
(draggableElement: Target, targetElement: Target) => { | ||
log('dragMove', draggableElement, targetElement); | ||
} | ||
); | ||
|
||
/** | ||
Drag and drop | ||
|
||
Simulates a drag and drop movement from a draggable source element that is dropped on a drop target element | ||
|
||
@public | ||
@param {element} draggableStartElement - the element or selector that the drag and drop begins on | ||
@param {element} dropTargetElement - element the element or selector that receives the drop | ||
@returns {promise} returns event with dataTransfer object | ||
*/ | ||
export default function dragMove( | ||
draggableStartElement: Target, | ||
dropTargetElement: Target | ||
): Promise<void> { | ||
const context: Record<string, string> = {}; | ||
|
||
if (!draggableStartElement) { | ||
throw new Error( | ||
`Element not found: ${draggableStartElement}. Must pass an element or selector.` | ||
); | ||
} | ||
if (!dropTargetElement) { | ||
throw new Error( | ||
`Element not found: ${dropTargetElement}. Must pass an element or selector.` | ||
); | ||
} | ||
|
||
const dragEl = getElement(draggableStartElement) as Element | HTMLElement; | ||
|
||
const targetEl = getElement(dropTargetElement) as Element | HTMLElement; | ||
|
||
return Promise.resolve() | ||
.then(() => | ||
runHooks('dragMove', 'start', draggableStartElement, dropTargetElement) | ||
) | ||
.then(async () => { | ||
fireEvent(dragEl, 'dragstart', { | ||
dataTransfer: { | ||
setData(someKey: string, value: string) { | ||
context[someKey] = value; | ||
}, | ||
}, | ||
}) | ||
.then(() => { | ||
fireEvent(targetEl, 'dragenter'); | ||
}) | ||
.then(() => { | ||
fireEvent(targetEl, 'dragover'); | ||
}) | ||
.then(() => { | ||
fireEvent(targetEl, 'drop', { | ||
dataTransfer: { | ||
getData(someKey: string) { | ||
return context[someKey]; | ||
}, | ||
}, | ||
}); | ||
}) | ||
.then(() => { | ||
return settled(); | ||
}); | ||
}) | ||
.then(() => | ||
runHooks('dragMove', 'end', draggableStartElement, dropTargetElement) | ||
); | ||
} |
73 changes: 73 additions & 0 deletions
73
addon/addon-test-support/@ember/test-helpers/dom/dragMove.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { settled } from '..'; | ||
import { registerHook, runHooks } from '../helper-hooks'; | ||
import getElement from './-get-element'; | ||
import { log } from './-logging'; | ||
import Target from './-target'; | ||
import fireEvent from './fire-event'; | ||
|
||
registerHook('fillIn', 'start', (target: Target, text: string) => { | ||
log('fillIn', target, text); | ||
}); | ||
|
||
/** | ||
Drag and drop | ||
|
||
Simulates a drag and drop movement from a draggable source element that is dropped on a drop target element | ||
|
||
@public | ||
@param {element} draggableStartElement - the element or selector that the drag and drop begins on | ||
@param {element} dropTargetElement - element the element or selector that receives the drop | ||
@returns {promise} returns event with dataTransfer object | ||
*/ | ||
export default function dragMove( | ||
draggableStartElement: Target, | ||
dropTargetElement: Target | ||
): Promise<void> { | ||
const context: Record<string, string> = {}; | ||
|
||
return Promise.resolve() | ||
.then(() => | ||
runHooks('dragMove', 'start', draggableStartElement, dropTargetElement) | ||
) | ||
.then(() => { | ||
if (!draggableStartElement) { | ||
throw new Error( | ||
`Element not found: ${draggableStartElement}. Must pass an element or selector.` | ||
); | ||
} | ||
if (!dropTargetElement) { | ||
throw new Error( | ||
`Element not found: ${dropTargetElement}. Must pass an element or selector.` | ||
); | ||
} | ||
|
||
const dragEl = getElement(draggableStartElement) as Element | HTMLElement; | ||
|
||
const targetEl = getElement(draggableStartElement) as | ||
| Element | ||
| HTMLElement; | ||
|
||
fireEvent(dragEl, 'dragstart', { | ||
dataTransfer: { | ||
setData(someKey: string, value: string) { | ||
context[someKey] = value; | ||
}, | ||
}, | ||
}); | ||
|
||
fireEvent(targetEl, 'dragover'); | ||
|
||
fireEvent(targetEl, 'drop', { | ||
dataTransfer: { | ||
getData(someKey: string) { | ||
return context[someKey]; | ||
}, | ||
}, | ||
}); | ||
|
||
return settled(); | ||
}) | ||
.then(() => | ||
runHooks('moveDrag', 'end', draggableStartElement, dropTargetElement) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { module, test } from 'qunit'; | ||
import { hbs } from 'ember-cli-htmlbars'; | ||
import { | ||
dragMove, | ||
find, | ||
render, | ||
setupContext, | ||
setupRenderingContext, | ||
teardownContext, | ||
} from '@ember/test-helpers'; | ||
import hasEmberVersion from '@ember/test-helpers/has-ember-version'; | ||
|
||
module('DOM Helper: moveDrag', function (hooks) { | ||
if (!hasEmberVersion(2, 4)) { | ||
return; | ||
} | ||
|
||
hooks.beforeEach(async function () { | ||
await setupContext(this); | ||
await setupRenderingContext(this); | ||
}); | ||
|
||
hooks.afterEach(async function () { | ||
await teardownContext(this); | ||
}); | ||
|
||
test('Drag and drop triggers native events', async function (assert) { | ||
this.set('dragstartTriggered', false); | ||
this.set('dragoverTriggered', false); | ||
this.set('dragenterTriggered', false); | ||
this.set('dropTriggered', false); | ||
|
||
this.set('handleDragStart', () => { | ||
this.set('dragStarted', true); | ||
}); | ||
this.set('handleDragOver', (event) => { | ||
// Per docs, you should prevent default on over and enter in order | ||
// for drop zone to be a valid target | ||
event.preventDefault(); | ||
this.set('dragoverTriggered', true); | ||
}); | ||
this.set('handleDragEnter', (event) => { | ||
event.preventDefault(); | ||
this.set('dragenterTriggered', true); | ||
}); | ||
this.set('handleDrop', () => { | ||
console.log('dropped'); | ||
this.set('dropTriggered', true); | ||
}); | ||
|
||
await render(hbs` | ||
<div draggable='true' class='source'>Test draggable text</div> | ||
<div class='target'>Drag text here</div> | ||
`); | ||
|
||
/* The below addEventListeners are used because Ember 3.8 did not have the `on` helper, and | ||
* this test errors in ember-test-helpers github ci. | ||
* | ||
* This is how the dom rendering above would loook like with modern ember templates: | ||
* | ||
* <div draggable='true' class='source' {{on 'dragstart' this.handleDragStart}}>Test draggable text</div> | ||
* <div class='target' | ||
* {{on "dragover" this.handleDragOver}} | ||
* {{on "dragenter" this.handleDragEnter}} | ||
* {{on "drop" this.handleDrop}} | ||
* >Drag text here</div> | ||
*/ | ||
const sourceEl = find('.source'); | ||
sourceEl.addEventListener('dragstart', this.handleDragStart); | ||
const targetEl = find('.target'); | ||
targetEl.addEventListener('drop', this.handleDrop); | ||
targetEl.addEventListener('dragover', this.handleDragOver); | ||
targetEl.addEventListener('dragenter', this.handleDragEnter); | ||
|
||
await dragMove('.source', '.target'); | ||
|
||
assert.true(this.get('dragStarted')); | ||
assert.true(this.get('dragenterTriggered')); | ||
assert.true(this.get('dragoverTriggered')); | ||
assert.true(this.get('dropTriggered')); | ||
}); | ||
|
||
test('dataTransfer mock transfers data like html api', async function (assert) { | ||
const testString = 'Test string!'; | ||
|
||
this.set('handleDragStart', (event) => { | ||
event.dataTransfer.setData('test', testString); | ||
}); | ||
this.set('handleDragOver', (event) => { | ||
event.preventDefault(); | ||
}); | ||
this.set('handleDragEnter', (event) => { | ||
event.preventDefault(); | ||
}); | ||
this.set('handleDrop', (event) => { | ||
const payload = event.dataTransfer.getData('test'); | ||
|
||
this.set('deliveredPayload', payload); | ||
}); | ||
|
||
await render(hbs` | ||
<div draggable='true' class='source'>Test draggable text</div> | ||
<div class='target'>Drag text here</div> | ||
`); | ||
|
||
const sourceEl = find('.source'); | ||
sourceEl.addEventListener('dragstart', this.handleDragStart); | ||
const targetEl = find('.target'); | ||
targetEl.addEventListener('drop', this.handleDrop); | ||
targetEl.addEventListener('dragover', this.handleDragOver); | ||
targetEl.addEventListener('dragenter', this.handleDragEnter); | ||
|
||
await dragMove('.source', '.target'); | ||
|
||
assert.equal(testString, this.get('deliveredPayload')); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a naive try to get it to work as it was before the
fireEvent
change.