Repository navigation
Update dropzone context API #11100
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
Merged
Merged
Update dropzone context API #11100
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2bd4e7c
Create context with the new context API
oandregal 0661613
Rename methods for consistency
oandregal d7ad171
Let lifecycle methods sit at the top
oandregal f2ac70a
Add context methods for consumers to interact with parent
oandregal 9859e82
Capitalize this.dropZones
oandregal addc695
Extract getDrapEventType method
oandregal 4fae1bf
Extract isTypeSupportedMethod
oandregal 540ac59
Extract isWithinElementBounds
oandregal 97760c8
Use new context
oandregal e6379c9
Use new Context.Consumer
oandregal a04b4de
fixup: remove no longer used method
oandregal 915aa59
Fix typo
oandregal 6146216
Do not set element before we have the DOM node
oandregal 7865ae0
Update only dropzones that changed, not all consumers
oandregal 42cb808
Do not use context as a variable name
oandregal c1f8fe3
Bind drop zone component setState
oandregal db37eed
Rename variable for consistency
oandregal 4e8a72d
Change ref to dropZoneElement
oandregal 7fb329c
Better naming
oandregal 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
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,52 +1,82 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { isEqual, find, some, filter, noop, throttle, includes } from 'lodash'; | ||
| import { isEqual, find, some, filter, throttle, includes } from 'lodash'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component, findDOMNode } from '@wordpress/element'; | ||
| import { Component, createContext, findDOMNode } from '@wordpress/element'; | ||
| import isShallowEqual from '@wordpress/is-shallow-equal'; | ||
|
|
||
| const { Provider, Consumer } = createContext( { | ||
| addDropZone: () => {}, | ||
| removeDropZone: () => {}, | ||
| } ); | ||
|
|
||
| const getDragEventType = ( { dataTransfer } ) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this could be tested now :) |
||
| if ( dataTransfer ) { | ||
| // Use lodash `includes` here as in the Edge browser `types` is implemented | ||
| // as a DomStringList, whereas in other browsers it's an array. `includes` | ||
| // happily works with both types. | ||
| if ( includes( dataTransfer.types, 'Files' ) ) { | ||
| return 'file'; | ||
| } | ||
|
|
||
| if ( includes( dataTransfer.types, 'text/html' ) ) { | ||
| return 'html'; | ||
| } | ||
| } | ||
|
|
||
| return 'default'; | ||
| }; | ||
|
|
||
| const isTypeSupported = ( type, typesSupported ) => { | ||
|
youknowriad marked this conversation as resolved.
Resolved
|
||
| return ( type === 'file' && typesSupported.onFilesDrop ) || | ||
| ( type === 'html' && typesSupported.onHTMLDrop ) || | ||
| ( type === 'default' && typesSupported.onDrop ); | ||
| }; | ||
|
|
||
| const isWithinElementBounds = ( element, x, y ) => { | ||
| const rect = element.getBoundingClientRect(); | ||
| /// make sure the rect is a valid rect | ||
| if ( rect.bottom === rect.top || rect.left === rect.right ) { | ||
| return false; | ||
| } | ||
|
|
||
| return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom; | ||
| }; | ||
|
|
||
| class DropZoneProvider extends Component { | ||
| constructor() { | ||
| super( ...arguments ); | ||
|
|
||
| // Event listeners | ||
| this.onDragOver = this.onDragOver.bind( this ); | ||
| this.onDrop = this.onDrop.bind( this ); | ||
| // Context methods so this component can receive data from consumers | ||
| this.addDropZone = this.addDropZone.bind( this ); | ||
| this.removeDropZone = this.removeDropZone.bind( this ); | ||
| // Utility methods | ||
| this.resetDragState = this.resetDragState.bind( this ); | ||
| this.toggleDraggingOverDocument = throttle( this.toggleDraggingOverDocument.bind( this ), 200 ); | ||
| this.dragOverListener = this.dragOverListener.bind( this ); | ||
| this.isWithinZoneBounds = this.isWithinZoneBounds.bind( this ); | ||
| this.onDrop = this.onDrop.bind( this ); | ||
|
|
||
| this.dropZones = []; | ||
| this.context = { | ||
| addDropZone: this.addDropZone, | ||
| removeDropZone: this.removeDropZone, | ||
|
youknowriad marked this conversation as resolved.
Resolved
|
||
| }; | ||
| this.state = { | ||
| isDraggingOverDocument: false, | ||
| hoveredDropZone: -1, | ||
| isDraggingOverDocument: false, | ||
| isDraggingOverElement: false, | ||
| position: null, | ||
| }; | ||
| this.dropzones = []; | ||
| } | ||
|
|
||
| dragOverListener( event ) { | ||
| this.toggleDraggingOverDocument( event, this.getDragEventType( event ) ); | ||
| event.preventDefault(); | ||
| } | ||
|
|
||
| getChildContext() { | ||
| return { | ||
| dropzones: { | ||
| add: ( { element, updateState, onDrop, onFilesDrop, onHTMLDrop } ) => { | ||
| this.dropzones.push( { element, updateState, onDrop, onFilesDrop, onHTMLDrop } ); | ||
| }, | ||
| remove: ( element ) => { | ||
| this.dropzones = filter( this.dropzones, ( dropzone ) => dropzone.element !== element ); | ||
| }, | ||
| }, | ||
| type: null, | ||
| }; | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| window.addEventListener( 'dragover', this.dragOverListener ); | ||
| window.addEventListener( 'dragover', this.onDragOver ); | ||
| window.addEventListener( 'drop', this.onDrop ); | ||
| window.addEventListener( 'mouseup', this.resetDragState ); | ||
|
|
||
|
|
@@ -56,11 +86,19 @@ class DropZoneProvider extends Component { | |
| } | ||
|
|
||
| componentWillUnmount() { | ||
| window.removeEventListener( 'dragover', this.dragOverListener ); | ||
| window.removeEventListener( 'dragover', this.onDragOver ); | ||
| window.removeEventListener( 'drop', this.onDrop ); | ||
| window.removeEventListener( 'mouseup', this.resetDragState ); | ||
| } | ||
|
|
||
| addDropZone( dropZone ) { | ||
| this.dropZones.push( dropZone ); | ||
| } | ||
|
|
||
| removeDropZone( dropZone ) { | ||
| this.dropZones = filter( this.dropZones, ( dz ) => dz !== dropZone ); | ||
| } | ||
|
|
||
| resetDragState() { | ||
| // Avoid throttled drag over handler calls | ||
| this.toggleDraggingOverDocument.cancel(); | ||
|
|
@@ -71,44 +109,19 @@ class DropZoneProvider extends Component { | |
| } | ||
|
|
||
| this.setState( { | ||
| isDraggingOverDocument: false, | ||
| hoveredDropZone: -1, | ||
| isDraggingOverDocument: false, | ||
| isDraggingOverElement: false, | ||
| position: null, | ||
| type: null, | ||
| } ); | ||
|
|
||
| this.dropzones.forEach( ( { updateState } ) => { | ||
| updateState( { | ||
| isDraggingOverDocument: false, | ||
| isDraggingOverElement: false, | ||
| position: null, | ||
| type: null, | ||
| } ); | ||
| } ); | ||
| } | ||
|
|
||
| getDragEventType( event ) { | ||
| if ( event.dataTransfer ) { | ||
| // Use lodash `includes` here as in the Edge browser `types` is implemented | ||
| // as a DomStringList, whereas in other browsers it's an array. `includes` | ||
| // happily works with both types. | ||
| if ( includes( event.dataTransfer.types, 'Files' ) ) { | ||
| return 'file'; | ||
| } | ||
|
|
||
| if ( includes( event.dataTransfer.types, 'text/html' ) ) { | ||
| return 'html'; | ||
| } | ||
| } | ||
|
|
||
| return 'default'; | ||
| } | ||
|
|
||
| doesDropzoneSupportType( dropzone, type ) { | ||
| return ( | ||
| ( type === 'file' && dropzone.onFilesDrop ) || | ||
| ( type === 'html' && dropzone.onHTMLDrop ) || | ||
| ( type === 'default' && dropzone.onDrop ) | ||
| ); | ||
| this.dropZones.forEach( ( dropZone ) => dropZone.setState( { | ||
|
youknowriad marked this conversation as resolved.
Resolved
|
||
| isDraggingOverDocument: false, | ||
| isDraggingOverElement: false, | ||
| position: null, | ||
| type: null, | ||
| } ) ); | ||
| } | ||
|
|
||
| toggleDraggingOverDocument( event, dragEventType ) { | ||
|
|
@@ -122,17 +135,17 @@ class DropZoneProvider extends Component { | |
|
|
||
| // Index of hovered dropzone. | ||
|
|
||
| const hoveredDropZones = filter( this.dropzones, ( dropzone ) => | ||
| this.doesDropzoneSupportType( dropzone, dragEventType ) && | ||
| this.isWithinZoneBounds( dropzone.element, detail.clientX, detail.clientY ) | ||
| const hoveredDropZones = filter( this.dropZones, ( dropzone ) => | ||
| isTypeSupported( dragEventType, dropzone ) && | ||
| isWithinElementBounds( dropzone.element, detail.clientX, detail.clientY ) | ||
| ); | ||
|
|
||
| // Find the leaf dropzone not containing another dropzone | ||
| const hoveredDropZone = find( hoveredDropZones, ( zone ) => ( | ||
| ! some( hoveredDropZones, ( subZone ) => subZone !== zone && zone.element.parentElement.contains( subZone.element ) ) | ||
| ) ); | ||
|
|
||
| const hoveredDropZoneIndex = this.dropzones.indexOf( hoveredDropZone ); | ||
| const hoveredDropZoneIndex = this.dropZones.indexOf( hoveredDropZone ); | ||
|
|
||
| let position = null; | ||
|
|
||
|
|
@@ -146,33 +159,33 @@ class DropZoneProvider extends Component { | |
| } | ||
|
|
||
| // Optimisation: Only update the changed dropzones | ||
| let dropzonesToUpdate = []; | ||
| let toUpdate = []; | ||
|
|
||
| if ( ! this.state.isDraggingOverDocument ) { | ||
| dropzonesToUpdate = this.dropzones; | ||
| toUpdate = this.dropZones; | ||
| } else if ( hoveredDropZoneIndex !== this.state.hoveredDropZone ) { | ||
| if ( this.state.hoveredDropZone !== -1 ) { | ||
| dropzonesToUpdate.push( this.dropzones[ this.state.hoveredDropZone ] ); | ||
| toUpdate.push( this.dropZones[ this.state.hoveredDropZone ] ); | ||
| } | ||
| if ( hoveredDropZone ) { | ||
| dropzonesToUpdate.push( hoveredDropZone ); | ||
| toUpdate.push( hoveredDropZone ); | ||
| } | ||
| } else if ( | ||
| hoveredDropZone && | ||
| hoveredDropZoneIndex === this.state.hoveredDropZone && | ||
| ! isEqual( position, this.state.position ) | ||
| ) { | ||
| dropzonesToUpdate.push( hoveredDropZone ); | ||
| toUpdate.push( hoveredDropZone ); | ||
| } | ||
|
|
||
| // Notifying the dropzones | ||
| dropzonesToUpdate.map( ( dropzone ) => { | ||
| const index = this.dropzones.indexOf( dropzone ); | ||
| toUpdate.map( ( dropzone ) => { | ||
| const index = this.dropZones.indexOf( dropzone ); | ||
| const isDraggingOverDropZone = index === hoveredDropZoneIndex; | ||
| dropzone.updateState( { | ||
| dropzone.setState( { | ||
| isDraggingOverDocument: isTypeSupported( dragEventType, dropzone ), | ||
| isDraggingOverElement: isDraggingOverDropZone, | ||
| position: isDraggingOverDropZone ? position : null, | ||
| isDraggingOverDocument: this.doesDropzoneSupportType( dropzone, dragEventType ), | ||
| type: isDraggingOverDropZone ? dragEventType : null, | ||
| } ); | ||
| } ); | ||
|
|
@@ -187,21 +200,9 @@ class DropZoneProvider extends Component { | |
| } | ||
| } | ||
|
|
||
| isWithinZoneBounds( dropzone, x, y ) { | ||
| const isWithinElement = ( element ) => { | ||
| const rect = element.getBoundingClientRect(); | ||
| /// make sure the rect is a valid rect | ||
| if ( rect.bottom === rect.top || rect.left === rect.right ) { | ||
| return false; | ||
| } | ||
|
|
||
| return ( | ||
| x >= rect.left && x <= rect.right && | ||
| y >= rect.top && y <= rect.bottom | ||
| ); | ||
| }; | ||
|
|
||
| return isWithinElement( dropzone ); | ||
| onDragOver( event ) { | ||
| this.toggleDraggingOverDocument( event, getDragEventType( event ) ); | ||
| event.preventDefault(); | ||
| } | ||
|
|
||
| onDrop( event ) { | ||
|
|
@@ -210,8 +211,8 @@ class DropZoneProvider extends Component { | |
| event.dataTransfer && event.dataTransfer.files.length; // eslint-disable-line no-unused-expressions | ||
|
|
||
| const { position, hoveredDropZone } = this.state; | ||
| const dragEventType = this.getDragEventType( event ); | ||
| const dropzone = this.dropzones[ hoveredDropZone ]; | ||
| const dragEventType = getDragEventType( event ); | ||
| const dropzone = this.dropZones[ hoveredDropZone ]; | ||
| const isValidDropzone = !! dropzone && this.container.contains( event.target ); | ||
| this.resetDragState(); | ||
|
|
||
|
|
@@ -233,13 +234,13 @@ class DropZoneProvider extends Component { | |
| } | ||
|
|
||
| render() { | ||
| const { children } = this.props; | ||
| return children; | ||
| return ( | ||
| <Provider value={ this.context }> | ||
| { this.props.children } | ||
| </Provider> | ||
| ); | ||
| } | ||
| } | ||
|
youknowriad marked this conversation as resolved.
Resolved
|
||
|
|
||
| DropZoneProvider.childContextTypes = { | ||
| dropzones: noop, | ||
| }; | ||
|
|
||
| export default DropZoneProvider; | ||
| export { Consumer as DropZoneConsumer }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.