-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Drag and drop: fix drop zones on block drag #67317
Merged
ellatrix
merged 10 commits into
trunk
from
revert/2e4b8d8a0593f3791fb580d127c51a9f334bf59d
Nov 27, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dc20d69
Revert "Fix media placeholder to only activate for media objects. (#6…
ellatrix 1b66762
Remove html transfer
ellatrix 83c7b3c
Add block types to data transfer
ellatrix 249dc26
Fix pattern drop
ellatrix 16468e8
fix e2e
ellatrix 9c34f2e
changelog
ellatrix e875496
memo
ellatrix daa7f35
cleanup
ellatrix 6897d91
Add comment
ellatrix 74d4822
Address feedback
ellatrix 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 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
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ import { __experimentalUseDropZone as useDropZone } from '@wordpress/compose'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import type { DropType, DropZoneProps } from './types'; | ||
import type { DropZoneProps } from './types'; | ||
import type { WordPressComponentProps } from '../context'; | ||
|
||
/** | ||
|
@@ -47,19 +47,22 @@ export function DropZoneComponent( { | |
onFilesDrop, | ||
onHTMLDrop, | ||
onDrop, | ||
isEligible = () => true, | ||
...restProps | ||
}: WordPressComponentProps< DropZoneProps, 'div', false > ) { | ||
const [ isDraggingOverDocument, setIsDraggingOverDocument ] = | ||
useState< boolean >(); | ||
const [ isDraggingOverElement, setIsDraggingOverElement ] = | ||
useState< boolean >(); | ||
const [ type, setType ] = useState< DropType >(); | ||
const [ isActive, setIsActive ] = useState< boolean >(); | ||
const ref = useDropZone( { | ||
onDrop( event ) { | ||
const files = event.dataTransfer | ||
? getFilesFromDataTransfer( event.dataTransfer ) | ||
: []; | ||
const html = event.dataTransfer?.getData( 'text/html' ); | ||
if ( ! event.dataTransfer ) { | ||
return; | ||
} | ||
|
||
const files = getFilesFromDataTransfer( event.dataTransfer ); | ||
const html = event.dataTransfer.getData( 'text/html' ); | ||
|
||
/** | ||
* From Windows Chrome 96, the `event.dataTransfer` returns both file object and HTML. | ||
|
@@ -76,32 +79,31 @@ export function DropZoneComponent( { | |
onDragStart( event ) { | ||
setIsDraggingOverDocument( true ); | ||
|
||
let _type: DropType = 'default'; | ||
if ( ! event.dataTransfer ) { | ||
return; | ||
} | ||
|
||
/** | ||
* From Windows Chrome 96, the `event.dataTransfer` returns both file object and HTML. | ||
* The order of the checks is important to recognize the HTML drop. | ||
*/ | ||
if ( event.dataTransfer?.types.includes( 'text/html' ) ) { | ||
_type = 'html'; | ||
if ( event.dataTransfer.types.includes( 'text/html' ) ) { | ||
setIsActive( !! onHTMLDrop ); | ||
} else if ( | ||
// Check for the types because sometimes the files themselves | ||
// are only available on drop. | ||
event.dataTransfer?.types.includes( 'Files' ) || | ||
( event.dataTransfer | ||
? getFilesFromDataTransfer( event.dataTransfer ) | ||
: [] | ||
).length > 0 | ||
event.dataTransfer.types.includes( 'Files' ) || | ||
getFilesFromDataTransfer( event.dataTransfer ).length > 0 | ||
) { | ||
_type = 'file'; | ||
setIsActive( !! onFilesDrop ); | ||
} else { | ||
setIsActive( !! onDrop && isEligible( event.dataTransfer ) ); | ||
} | ||
|
||
setType( _type ); | ||
}, | ||
onDragEnd() { | ||
setIsDraggingOverElement( false ); | ||
setIsDraggingOverDocument( false ); | ||
setType( undefined ); | ||
setIsActive( undefined ); | ||
}, | ||
onDragEnter() { | ||
setIsDraggingOverElement( true ); | ||
|
@@ -112,14 +114,9 @@ export function DropZoneComponent( { | |
} ); | ||
|
||
const classes = clsx( 'components-drop-zone', className, { | ||
'is-active': | ||
( isDraggingOverDocument || isDraggingOverElement ) && | ||
( ( type === 'file' && onFilesDrop ) || | ||
( type === 'html' && onHTMLDrop ) || | ||
( type === 'default' && onDrop ) ), | ||
'is-active': isActive, | ||
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. Nice, this looks a lot better! |
||
'is-dragging-over-document': isDraggingOverDocument, | ||
'is-dragging-over-element': isDraggingOverElement, | ||
[ `is-dragging-${ type }` ]: !! type, | ||
} ); | ||
|
||
return ( | ||
|
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
Oops, something went wrong.
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.
Would the event ever not have a dataTransfer? If so, we're changing the behaviour of this function because currently on trunk onDrop gets called if it exists, regardless of dataTransfer.
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 will never be the case, it's just to make TypeScript happy. We already had these conditions before