Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from 7 commits
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
3 changes: 2 additions & 1 deletion components/drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class DropZone extends Component {

render() {
const { className, label } = this.props;
const { isDraggingOverDocument, isDraggingOverElement, position } = this.state;
const { isDraggingOverDocument, isDraggingOverElement, position, isReorderingInProgress } = this.state;
Copy link
Contributor Author

@chriskmnds chriskmnds Dec 17, 2017

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.

const classes = classnames( 'components-drop-zone', className, {
'is-active': isDraggingOverDocument || isDraggingOverElement,
'is-dragging-over-document': isDraggingOverDocument,
Expand All @@ -71,6 +71,7 @@ class DropZone extends Component {
'is-close-to-bottom': position && position.y === 'bottom',
'is-close-to-left': position && position.x === 'left',
'is-close-to-right': position && position.x === 'right',
'is-reordering-in-progress': isReorderingInProgress,
} );

return (
Expand Down
3 changes: 3 additions & 0 deletions components/higher-order/with-dnd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# withDragAndDrop

// @TODO: CLK
90 changes: 90 additions & 0 deletions components/higher-order/with-dnd/index.js
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 ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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' );
Copy link
Member

@aduth aduth Dec 18, 2017

Choose a reason for hiding this comment

The 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 Array#indexOf returns -1 if the item is not in the array. This is a truthy value; conversely, 0 is a valid return value but is falsy.

Instead, you might consider:

  • Comparing against -1
    • -1 !== target.className.split( ' ' ).indexOf( 'is-reordering-in-progress' );
  • Using Lodash's _.includes which is a bit more semantic to what you're trying to test
    • includes( target.className.split( ' ' ), 'is-reordering-in-progress' );
  • Using Element#classList.contains which is designed for what you're trying to do here:
    • target.classList.contains( 'is-reordering-in-progress' );

Copy link
Member

Choose a reason for hiding this comment

The 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 split, i.e. split( ' ' )

You might consider installing an ESLint plugin for your editor to help surface these styling issues more apparently:

https://eslint.org/docs/user-guide/integrations

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 );
};
}
1 change: 1 addition & 0 deletions components/higher-order/with-dnd/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// @todo :clk:tests
1 change: 1 addition & 0 deletions components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ export { default as withFocusReturn } from './higher-order/with-focus-return';
export { default as withInstanceId } from './higher-order/with-instance-id';
export { default as withSpokenMessages } from './higher-order/with-spoken-messages';
export { default as withState } from './higher-order/with-state';
export { default as withDragAndDropContext, withDragSource, withDropTarget } from './higher-order/with-dnd';
36 changes: 36 additions & 0 deletions editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ export function replaceBlock( uid, block ) {
return replaceBlocks( uid, block );
}

/**
* @todo :clk:doc
* @param {[type]} uid [description]
* @param {[type]} index [description]
* @return {[type]} [description]
*/
export function moveBlockToIndex( uid, index ) {
return {
type: 'MOVE_BLOCK_TO_INDEX',
uid,
index
};
}

export function insertBlock( block, position ) {
return insertBlocks( [ block ], position );
}
Expand Down Expand Up @@ -599,3 +613,25 @@ export function appendDefaultBlock() {
type: 'APPEND_DEFAULT_BLOCK',
};
}

/**
* @todo :clk:doc
* [startReordering description]
* @return {[type]} [description]
*/
export function startReordering( ) {
return {
type: 'START_DRAG_AND_DROP'
};
}

/**
* @todo :clk:doc
* [stopReordering description]
* @return {[type]} [description]
*/
export function stopReordering( ) {
return {
type: 'STOP_DRAG_AND_DROP'
};
}
3 changes: 2 additions & 1 deletion editor/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { compose } from '@wordpress/element';
*/
import { insertBlocks } from '../../actions';

function BlockDropZone( { index, isLocked, ...props } ) {
function BlockDropZone( { index, isLocked, isReorderingInProgress, ...props } ) {
if ( isLocked ) {
return null;
}
Expand Down Expand Up @@ -46,6 +46,7 @@ function BlockDropZone( { index, isLocked, ...props } ) {
return (
<DropZone
onFilesDrop={ dropFiles }
isReorderingInProgress={ isReorderingInProgress }
/>
);
}
Expand Down
47 changes: 46 additions & 1 deletion editor/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -46,6 +47,7 @@ import {
stopTyping,
updateBlockAttributes,
toggleSelection,
moveBlockToIndex,
} from '../../actions';
import {
getBlock,
Expand All @@ -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;

Expand Down Expand Up @@ -397,7 +401,40 @@ export class BlockListBlock extends Component {
onClick={ this.onClick }
{ ...wrapperProps }
>
<BlockDropZone index={ order } />



{ true &&
<DragAndDropSource
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor Author

@chriskmnds chriskmnds Dec 17, 2017

Choose a reason for hiding this comment

The 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 /> }
Expand Down Expand Up @@ -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 ) => ( {
Expand All @@ -477,6 +515,7 @@ const mapDispatchToProps = ( dispatch, ownProps ) => ( {
onSelect() {
dispatch( selectBlock( ownProps.uid ) );
},

onDeselect() {
dispatch( clearSelectedBlock() );
},
Expand All @@ -496,6 +535,7 @@ const mapDispatchToProps = ( dispatch, ownProps ) => ( {
uid: ownProps.uid,
} );
},

onMouseLeave() {
dispatch( {
type: 'TOGGLE_BLOCK_HOVERED',
Expand Down Expand Up @@ -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';
Expand Down
60 changes: 32 additions & 28 deletions editor/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import 'element-closest';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { Component, compose } from '@wordpress/element';
import { serialize } from '@wordpress/blocks';
import { withDragAndDropContext } from '@wordpress/components';

/**
* Internal dependencies
Expand Down Expand Up @@ -230,31 +231,34 @@ class BlockList extends Component {
}
}

export default connect(
( state ) => ( {
blocks: getBlockUids( state ),
selectionStart: getMultiSelectedBlocksStartUid( state ),
selectionEnd: getMultiSelectedBlocksEndUid( state ),
multiSelectedBlocks: getMultiSelectedBlocks( state ),
multiSelectedBlockUids: getMultiSelectedBlockUids( state ),
selectedBlock: getSelectedBlock( state ),
isSelectionEnabled: isSelectionEnabled( state ),
} ),
( dispatch ) => ( {
onStartMultiSelect() {
dispatch( startMultiSelect() );
},
onStopMultiSelect() {
dispatch( stopMultiSelect() );
},
onMultiSelect( start, end ) {
dispatch( multiSelect( start, end ) );
},
onSelect( uid ) {
dispatch( selectBlock( uid ) );
},
onRemove( uids ) {
dispatch( { type: 'REMOVE_BLOCKS', uids } );
},
} )
export default compose(
withDragAndDropContext,
connect(
( state ) => ( {
blocks: getBlockUids( state ),
selectionStart: getMultiSelectedBlocksStartUid( state ),
selectionEnd: getMultiSelectedBlocksEndUid( state ),
multiSelectedBlocks: getMultiSelectedBlocks( state ),
multiSelectedBlockUids: getMultiSelectedBlockUids( state ),
selectedBlock: getSelectedBlock( state ),
isSelectionEnabled: isSelectionEnabled( state ),
} ),
( dispatch ) => ( {
onStartMultiSelect() {
dispatch( startMultiSelect() );
},
onStopMultiSelect() {
dispatch( stopMultiSelect() );
},
onMultiSelect( start, end ) {
dispatch( multiSelect( start, end ) );
},
onSelect( uid ) {
dispatch( selectBlock( uid ) );
},
onRemove( uids ) {
dispatch( { type: 'REMOVE_BLOCKS', uids } );
},
} )
)
)( BlockList );
Loading