-
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
Reorder blocks via drag & drop (v1. using React-dnd). #4056
Changes from 7 commits
cca62d7
264d3cb
b00ec8c
2bfc54f
20f0c9d
e629af0
93bdd74
6ea53a4
9cd6578
866238a
de1b3fd
6ee16a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# withDragAndDrop | ||
|
||
// @TODO: CLK |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { DragDropContext, DragSource, DropTarget } from 'react-dnd'; | ||
import HTML5Backend from 'react-dnd-html5-backend'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
/* Testing... */ | ||
const ModifiedBackend = ( ...args ) => { | ||
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. Experimenting with a custom provider for react-dnd due to the conflicts created with dropzones. |
||
const instance = new HTML5Backend( ...args ); | ||
|
||
const listeners = [ | ||
'handleTopDragStart', | ||
'handleTopDragStartCapture', | ||
'handleTopDragEndCapture', | ||
'handleTopDragEnter', | ||
'handleTopDragEnterCapture', | ||
'handleTopDragLeaveCapture', | ||
'handleTopDragOver', | ||
'handleTopDragOverCapture', | ||
'handleTopDrop', | ||
'handleTopDropCapture', | ||
'handleSelectStart', | ||
]; | ||
|
||
const shouldIgnoreTarget = ( target ) => { | ||
return target.className.split(' ').indexOf( 'is-reordering-in-progress' ); | ||
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. This logic doesn't do as you might expect. I assume you're checking whether the target has the specified class assigned, but Instead, you might consider:
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. There's a styling issue here: there needs to be a space between the parentheses of You might consider installing an ESLint plugin for your editor to help surface these styling issues more apparently: 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. Thanks @aduth for your comments and pointers. This part of the code is actually not used anymore, as I have instead found a patch that fixes the issues I was trying to get to here. I will send an update soon, so apologies for any inconvenience. I may actually refrain from using react-dnd altogether. I am not too happy with all the clutter that is created. I am running a separate experiment right now with the existing drop-zones. I will share soon. |
||
} | ||
|
||
listeners.forEach( name => { | ||
const original = instance[ name ]; | ||
|
||
instance[ name ] = ( e, ...extraArgs ) => { | ||
|
||
if ( name === 'handleTopDrop' ) { | ||
console.log( e.target ); | ||
console.log( e.target.className ); | ||
} | ||
|
||
if ( ! shouldIgnoreTarget( e.target ) ) { | ||
original( e, ...extraArgs ); | ||
} | ||
}; | ||
}); | ||
|
||
return instance; | ||
}; | ||
|
||
|
||
|
||
/** | ||
* A wrapper around react-dnd DragDropContext higher order component. | ||
* @param { Function } WrappedComponent Component to be wrapped with drag & drop context. | ||
* @return { Function } A component wrapped with the react-dnd HOC. | ||
*/ | ||
export default function withDragAndDropContext( WrappedComponent ) { | ||
return DragDropContext( HTML5Backend )( WrappedComponent ); | ||
} | ||
|
||
/** | ||
* @todo :clk:doc | ||
* A wrapper around react-dnd DragSource higher order component. | ||
* @param {[type]} itemType [description] | ||
* @param {[type]} sourceSpec [description] | ||
* @param {[type]} sourceCollect [description] | ||
* @return {[type]} [description] | ||
*/ | ||
export function withDragSource( itemType, sourceSpec, sourceCollect ) { | ||
return ( WrappedComponent ) => { | ||
return DragSource( itemType, sourceSpec, sourceCollect )( WrappedComponent ); | ||
}; | ||
} | ||
|
||
/** | ||
* @todo :clk:doc | ||
* A wrapper around react-dnd DropTarget higher order component. | ||
* @param {[type]} itemType [description] | ||
* @param {[type]} targetSpec [description] | ||
* @param {[type]} targetCollect [description] | ||
* @return {[type]} [description] | ||
*/ | ||
export function withDropTarget( itemType, targetSpec, targetCollect ) { | ||
return ( WrappedComponent ) => { | ||
return DropTarget( itemType, targetSpec, targetCollect )( WrappedComponent ); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// @todo :clk:tests |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import { __, sprintf } from '@wordpress/i18n'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { DragAndDropSource, DragAndDropTarget } from '../draggable'; | ||
import BlockMover from '../block-mover'; | ||
import BlockDropZone from '../block-drop-zone'; | ||
import BlockSettingsMenu from '../block-settings-menu'; | ||
|
@@ -46,6 +47,7 @@ import { | |
stopTyping, | ||
updateBlockAttributes, | ||
toggleSelection, | ||
moveBlockToIndex, | ||
} from '../../actions'; | ||
import { | ||
getBlock, | ||
|
@@ -62,7 +64,9 @@ import { | |
isSelectionEnabled, | ||
isTyping, | ||
getBlockMode, | ||
isReorderingInProgress, | ||
} from '../../selectors'; | ||
import { DRAGGABLE_BLOCK } from '../../constants'; | ||
|
||
const { BACKSPACE, ESCAPE, DELETE, ENTER, UP, RIGHT, DOWN, LEFT } = keycodes; | ||
|
||
|
@@ -397,7 +401,40 @@ export class BlockListBlock extends Component { | |
onClick={ this.onClick } | ||
{ ...wrapperProps } | ||
> | ||
<BlockDropZone index={ order } /> | ||
|
||
|
||
|
||
{ true && | ||
<DragAndDropSource | ||
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. This is the gist of it, part 1. A component that we can drag. |
||
draggableType={ DRAGGABLE_BLOCK } | ||
dragSourceUid={ this.props.uid } | ||
index={ order } | ||
> | ||
<div>DRAG</div> | ||
</DragAndDropSource> | ||
} | ||
|
||
{ true && | ||
<DragAndDropTarget | ||
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. This is the gist of it, part 2. A component that we can drop to. |
||
draggableType={ DRAGGABLE_BLOCK } | ||
dropTargetUid={ this.props.uid } | ||
index={ order } | ||
hoverIndexBreakpoint={ false } | ||
reIndexCallback={ this.props.onMoveBlockToIndex } | ||
> | ||
<div>DROP HERE</div> | ||
</DragAndDropTarget> | ||
} | ||
|
||
|
||
|
||
{ false && ! this.props.isReorderingInProgress && | ||
<BlockDropZone index={ order } isReorderingInProgress={ this.props.isReorderingInProgress } /> | ||
} | ||
|
||
{ false && <BlockDropZone index={ order } isReorderingInProgress={ this.props.isReorderingInProgress } /> } | ||
|
||
|
||
{ ( showUI || isHovered ) && <BlockMover uids={ [ block.uid ] } /> } | ||
{ ( showUI || isHovered ) && <BlockSettingsMenu uids={ [ block.uid ] } /> } | ||
{ showUI && isValid && showContextualToolbar && <BlockContextualToolbar /> } | ||
|
@@ -467,6 +504,7 @@ const mapStateToProps = ( state, { uid } ) => ( { | |
meta: getEditedPostAttribute( state, 'meta' ), | ||
mode: getBlockMode( state, uid ), | ||
isSelectionEnabled: isSelectionEnabled( state ), | ||
isReorderingInProgress: isReorderingInProgress( state ), | ||
} ); | ||
|
||
const mapDispatchToProps = ( dispatch, ownProps ) => ( { | ||
|
@@ -477,6 +515,7 @@ const mapDispatchToProps = ( dispatch, ownProps ) => ( { | |
onSelect() { | ||
dispatch( selectBlock( ownProps.uid ) ); | ||
}, | ||
|
||
onDeselect() { | ||
dispatch( clearSelectedBlock() ); | ||
}, | ||
|
@@ -496,6 +535,7 @@ const mapDispatchToProps = ( dispatch, ownProps ) => ( { | |
uid: ownProps.uid, | ||
} ); | ||
}, | ||
|
||
onMouseLeave() { | ||
dispatch( { | ||
type: 'TOGGLE_BLOCK_HOVERED', | ||
|
@@ -527,9 +567,14 @@ const mapDispatchToProps = ( dispatch, ownProps ) => ( { | |
onMetaChange( meta ) { | ||
dispatch( editPost( { meta } ) ); | ||
}, | ||
|
||
toggleSelection( selectionEnabled ) { | ||
dispatch( toggleSelection( selectionEnabled ) ); | ||
}, | ||
|
||
onMoveBlockToIndex( uid, index ) { | ||
dispatch( moveBlockToIndex( uid, index ) ); | ||
}, | ||
} ); | ||
|
||
BlockListBlock.className = 'editor-block-list__block-edit'; | ||
|
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.
Never really mind this flag - I am experimenting with a couple of ideas to sort out the conflicts with the current drop-zones.