Skip to content

Commit

Permalink
List View: Displace list view items when dragging (a bit more WYSIWYG) (
Browse files Browse the repository at this point in the history
#56625)

* Include earlier experiment to make drag chip resemble the list view item

* Try displacing list view items via transform: translateY while dragging

* Try adding a bit of styling when nesting

* Try to fix tests

* Offset the top of the table to match the displacement

* Improve the experience when beginning to drag a block

* Smoothly hide gap when dragging outside of the list view area

* Merge in changes from drag to collapsed block to expand PR

* Try collapsing selected blocks down to a single row space while dragging

* Fix jumpiness in Safari

* Try making styling match the selected state

* Try smoothly dropping to the final position

* Fix transitions above dragged items

* Fix flickering issue in Safari

* Try a transparent background color

* Fix drag behaviour in navigation block

* Add vertical drop indicator

* Try using existing drop indicator line

* Add reduced motion, fix e2e test

* Tidy up classname for drag chip in prep for subsequent changes

* Remove indent from drop chip, ensure indent is applied to the position

* Try updating the drag chip to use a border

* Try drop indicator line as preview of final list item

* Try offsets for nesting level of drop indicator, and use darker color if adjacent block has selected branch

* Use drag chip as on trunk, but with opacity of 0.8

* Consolidate drop indicator logic

* Fix drop indicator when dragging files

* Don't add nesting classname when dragging files as the editor thinks we can nest for any block

* Move displacement logic to a helper function, add tests

* Remove unneeded changes from useMovingAnimation

* Add explanatory comments

* Try a timeout for the list view drop zone updates of 50ms instead of 200ms
  • Loading branch information
andrewserong authored Jan 22, 2024
1 parent 6ab0f47 commit 2997bad
Show file tree
Hide file tree
Showing 15 changed files with 853 additions and 89 deletions.
21 changes: 16 additions & 5 deletions packages/block-editor/src/components/block-draggable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ import { __unstableUseBlockRef as useBlockRef } from '../block-list/use-block-pr
import { isDropTargetValid } from '../use-block-drop-zone';

const BlockDraggable = ( {
appendToOwnerDocument,
children,
clientIds,
cloneClassname,
elementId,
onDragStart,
onDragEnd,
fadeWhenDisabled = false,
dragComponent,
} ) => {
const {
srcRootClientId,
Expand Down Expand Up @@ -181,6 +184,7 @@ const BlockDraggable = ( {

return (
<Draggable
appendToOwnerDocument={ appendToOwnerDocument }
cloneClassname={ cloneClassname }
__experimentalTransferDataType="wp-blocks"
transferData={ transferData }
Expand Down Expand Up @@ -210,12 +214,19 @@ const BlockDraggable = ( {
}
} }
__experimentalDragComponent={
<BlockDraggableChip
count={ clientIds.length }
icon={ icon }
fadeWhenDisabled
/>
// Check against `undefined` so that `null` can be used to disable
// the default drag component.
dragComponent !== undefined ? (
dragComponent
) : (
<BlockDraggableChip
count={ clientIds.length }
icon={ icon }
fadeWhenDisabled
/>
)
}
elementId={ elementId }
>
{ ( { onDraggableStart, onDraggableEnd } ) => {
return children( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ const ListViewBlockContents = forwardRef(
setInsertedBlock={ setInsertedBlock }
/>
) }
<BlockDraggable clientIds={ draggableClientIds }>
<BlockDraggable
appendToOwnerDocument
clientIds={ draggableClientIds }
cloneClassname={ 'block-editor-list-view-draggable-chip' }
>
{ ( { draggable, onDragStart, onDragEnd } ) => (
<ListViewBlockSelectButton
ref={ ref }
Expand Down
9 changes: 9 additions & 0 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ import AriaReferencedText from './aria-referenced-text';

function ListViewBlock( {
block: { clientId },
displacement,
isAfterDraggedBlocks,
isDragged,
isNesting,
isSelected,
isBranchSelected,
selectBlock,
Expand Down Expand Up @@ -270,6 +273,11 @@ function ListViewBlock( {
'has-single-cell': ! showBlockActions,
'is-synced': blockInformation?.isSynced,
'is-draggable': canMove,
'is-displacement-normal': displacement === 'normal',
'is-displacement-up': displacement === 'up',
'is-displacement-down': displacement === 'down',
'is-after-dragged-blocks': isAfterDraggedBlocks,
'is-nesting': isNesting,
} );

// Only include all selected blocks if the currently clicked on block
Expand All @@ -296,6 +304,7 @@ function ListViewBlock( {
return (
<ListViewLeaf
className={ classes }
isDragged={ isDragged }
onKeyDown={ onKeyDown }
onMouseEnter={ onMouseEnter }
onMouseLeave={ onMouseLeave }
Expand Down
40 changes: 30 additions & 10 deletions packages/block-editor/src/components/list-view/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { AsyncModeProvider, useSelect } from '@wordpress/data';
import { Appender } from './appender';
import ListViewBlock from './block';
import { useListViewContext } from './context';
import { isClientIdSelected } from './utils';
import { getDragDisplacementValues, isClientIdSelected } from './utils';
import { store as blockEditorStore } from '../../store';
import useBlockDisplayInformation from '../use-block-display-information';

Expand Down Expand Up @@ -91,7 +91,6 @@ function ListViewBranch( props ) {
selectedClientIds,
level = 1,
path = '',
isBranchDragged = false,
isBranchSelected = false,
listPosition = 0,
fixedListWindow,
Expand All @@ -115,7 +114,14 @@ function ListViewBranch( props ) {
[ parentId ]
);

const { expandedState, draggedClientIds } = useListViewContext();
const {
blockDropPosition,
blockDropTargetIndex,
firstDraggedBlockIndex,
blockIndexes,
expandedState,
draggedClientIds,
} = useListViewContext();

if ( ! canParentExpand ) {
return null;
Expand Down Expand Up @@ -143,6 +149,21 @@ function ListViewBranch( props ) {
);
}

const isDragged = !! draggedClientIds?.includes( clientId );

// Determine the displacement of the block while dragging. This
// works out whether the current block should be displaced up or
// down, relative to the dragged blocks and the drop target.
const { displacement, isAfterDraggedBlocks, isNesting } =
getDragDisplacementValues( {
blockIndexes,
blockDropTargetIndex,
blockDropPosition,
clientId,
firstDraggedBlockIndex,
isDragged,
} );

const { itemInView } = fixedListWindow;
const blockInView = itemInView( nextPosition );

Expand All @@ -158,8 +179,6 @@ function ListViewBranch( props ) {
? expandedState[ clientId ] ?? isExpanded
: undefined;

const isDragged = !! draggedClientIds?.includes( clientId );

// Make updates to the selected or dragged blocks synchronous,
// but asynchronous for any other block.
const isSelected = isClientIdSelected(
Expand All @@ -178,7 +197,6 @@ function ListViewBranch( props ) {
const showBlock =
isDragged ||
blockInView ||
isBranchDragged ||
( isSelected && clientId === selectedClientIds[ 0 ] );
return (
<AsyncModeProvider key={ clientId } value={ ! isSelected }>
Expand All @@ -188,25 +206,28 @@ function ListViewBranch( props ) {
selectBlock={ selectBlock }
isSelected={ isSelected }
isBranchSelected={ isSelectedBranch }
isDragged={ isDragged || isBranchDragged }
isDragged={ isDragged }
level={ level }
position={ position }
rowCount={ rowCount }
siblingBlockCount={ blockCount }
showBlockMovers={ showBlockMovers }
path={ updatedPath }
isExpanded={ shouldExpand }
isExpanded={ isDragged ? false : shouldExpand }
listPosition={ nextPosition }
selectedClientIds={ selectedClientIds }
isSyncedBranch={ syncedBranch }
displacement={ displacement }
isAfterDraggedBlocks={ isAfterDraggedBlocks }
isNesting={ isNesting }
/>
) }
{ ! showBlock && (
<tr>
<td className="block-editor-list-view-placeholder" />
</tr>
) }
{ hasNestedBlocks && shouldExpand && (
{ hasNestedBlocks && shouldExpand && ! isDragged && (
<ListViewBranch
parentId={ clientId }
blocks={ innerBlocks }
Expand All @@ -217,7 +238,6 @@ function ListViewBranch( props ) {
listPosition={ nextPosition + 1 }
fixedListWindow={ fixedListWindow }
isBranchSelected={ isSelectedBranch }
isBranchDragged={ isDragged || isBranchDragged }
selectedClientIds={ selectedClientIds }
isExpanded={ isExpanded }
isSyncedBranch={ syncedBranch }
Expand Down
Loading

1 comment on commit 2997bad

@github-actions
Copy link

@github-actions github-actions bot commented on 2997bad Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2997bad.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7618449979
📝 Reported issues:

Please sign in to comment.