-
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
Set scroll velocity from drag distance #23082
Changes from all commits
f9ba478
5c05196
7af31e1
8a91a7b
dd2a314
1fcbd72
297c04a
b45996a
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 |
---|---|---|
|
@@ -4,8 +4,42 @@ | |
import { Draggable } from '@wordpress/components'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
import { getScrollContainer } from '@wordpress/dom'; | ||
|
||
const BlockDraggable = ( { children, clientIds, cloneClassname } ) => { | ||
const SCROLL_INACTIVE_DISTANCE_PX = 50; | ||
const SCROLL_INTERVAL_MS = 25; | ||
const PIXELS_PER_SECOND_PER_DISTANCE = 5; | ||
const VELOCITY_MULTIPLIER = | ||
PIXELS_PER_SECOND_PER_DISTANCE * ( SCROLL_INTERVAL_MS / 1000 ); | ||
|
||
function startScrollingY( nodeRef, velocityRef ) { | ||
return setInterval( () => { | ||
if ( nodeRef.current && velocityRef.current ) { | ||
const newTop = nodeRef.current.scrollTop + velocityRef.current; | ||
|
||
nodeRef.current.scroll( { | ||
top: newTop, | ||
// behavior: 'smooth' // seems to hurt performance, better to use a small scroll interval | ||
} ); | ||
} | ||
}, SCROLL_INTERVAL_MS ); | ||
} | ||
|
||
function getVerticalScrollParent( node ) { | ||
if ( node === null ) { | ||
return null; | ||
} | ||
|
||
return getScrollContainer( node ); | ||
} | ||
|
||
const BlockDraggable = ( { | ||
children, | ||
clientIds, | ||
cloneClassname, | ||
onDragStart, | ||
onDragEnd, | ||
} ) => { | ||
const { srcRootClientId, index, isDraggable } = useSelect( | ||
( select ) => { | ||
const { | ||
|
@@ -30,6 +64,14 @@ const BlockDraggable = ( { children, clientIds, cloneClassname } ) => { | |
[ clientIds ] | ||
); | ||
const isDragging = useRef( false ); | ||
|
||
// @todo - do this for horizontal scroll | ||
const dragStartY = useRef( null ); | ||
const velocityY = useRef( null ); | ||
const scrollParentY = useRef( null ); | ||
|
||
const scrollEditorInterval = useRef( null ); | ||
|
||
const { startDraggingBlocks, stopDraggingBlocks } = useDispatch( | ||
'core/block-editor' | ||
); | ||
|
@@ -40,6 +82,11 @@ const BlockDraggable = ( { children, clientIds, cloneClassname } ) => { | |
if ( isDragging.current ) { | ||
stopDraggingBlocks(); | ||
} | ||
|
||
if ( scrollEditorInterval.current ) { | ||
clearInterval( scrollEditorInterval.current ); | ||
scrollEditorInterval.current = null; | ||
} | ||
}; | ||
}, [] ); | ||
|
||
|
@@ -60,13 +107,51 @@ const BlockDraggable = ( { children, clientIds, cloneClassname } ) => { | |
cloneClassname={ cloneClassname } | ||
elementId={ blockElementId } | ||
transferData={ transferData } | ||
onDragStart={ () => { | ||
onDragStart={ ( event ) => { | ||
startDraggingBlocks(); | ||
isDragging.current = true; | ||
dragStartY.current = event.clientY; | ||
|
||
// find nearest parent(s) to scroll | ||
scrollParentY.current = getVerticalScrollParent( | ||
document.getElementById( blockElementId ) | ||
); | ||
scrollEditorInterval.current = startScrollingY( | ||
scrollParentY, | ||
velocityY | ||
); | ||
if ( onDragStart ) { | ||
onDragStart(); | ||
} | ||
} } | ||
onDragOver={ ( event ) => { | ||
const distanceY = event.clientY - dragStartY.current; | ||
if ( distanceY > SCROLL_INACTIVE_DISTANCE_PX ) { | ||
velocityY.current = | ||
VELOCITY_MULTIPLIER * | ||
( distanceY - SCROLL_INACTIVE_DISTANCE_PX ); | ||
} else if ( distanceY < -SCROLL_INACTIVE_DISTANCE_PX ) { | ||
velocityY.current = | ||
VELOCITY_MULTIPLIER * | ||
( distanceY + SCROLL_INACTIVE_DISTANCE_PX ); | ||
} else { | ||
velocityY.current = 0; | ||
} | ||
Comment on lines
+128
to
+139
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. A usability issue I had is that the feature is less effective if you're dragging from near the top or the bottom of the screen. When dragging from the top downwards, if I wanted to aim for a block that I didn't need to scroll to but was nearer the lower part of the screen, I found I would scroll past my target very quickly as I moved my cursor down. The opposite was true when dragging upwards from near the top of the screen, the page wouldn't scroll very quickly because there wasn't much distance I could move the cursor up. An answer might be to make the velocity calculation based on the percentage the cursor has moved towards the edge of the screen (or probably scroll container), in pseudocode for moving down (I haven't factored the inactive scroll distance for this): const movableDistance = screenHeight - dragStartY.current;
const dragDistance = event.clientY - dragStartY.current;
const distancePercentage = dragDistance / movableDistance;
// distancePercentage would be a much smaller value, so VELOCITY_MULTIPLIER would have to be adjusted.
velocityY.current = VELOCITY_MULTIPLIER * distancePercentage An interesting iteration might also be to try easing on the calculation (e.g. https://easings.net/#easeInQuart), so that the speed isn't too dramatic at first, but worth trying one thing at a time. 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. Those are great suggestions. Given that this patch is already considered to be an improvement, I would suggest we raise those as tickets for future work and get this merged. That way we can compare multiple strategies while not missing the 5.5 window. 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. @gravityrail I do still have concerns about usability, I feel like it's pretty easy to make the page start scrolling, but harder to make it stop in the right place. Given others have said they're happy with the change, lets push on and address this as a follow-up 👍 You mentioned that you don't have a lot of time to work on this, would it be better if I try out the suggestion in a follow-up PR? 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 would love it if you could do a follow up PR with your suggestions. Happy to pitch in and test. |
||
} } | ||
onDragEnd={ () => { | ||
stopDraggingBlocks(); | ||
isDragging.current = false; | ||
dragStartY.current = null; | ||
scrollParentY.current = null; | ||
|
||
if ( scrollEditorInterval.current ) { | ||
clearInterval( scrollEditorInterval.current ); | ||
scrollEditorInterval.current = null; | ||
} | ||
|
||
if ( onDragEnd ) { | ||
onDragEnd(); | ||
} | ||
} } | ||
> | ||
{ ( { onDraggableStart, onDraggableEnd } ) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ function selector( select ) { | |
isCaretWithinFormattedText, | ||
getSettings, | ||
getLastMultiSelectedBlockClientId, | ||
isDraggingBlocks, | ||
} = select( 'core/block-editor' ); | ||
return { | ||
isNavigationMode: isNavigationMode(), | ||
|
@@ -40,6 +41,7 @@ function selector( select ) { | |
hasMultiSelection: hasMultiSelection(), | ||
hasFixedToolbar: getSettings().hasFixedToolbar, | ||
lastClientId: getLastMultiSelectedBlockClientId(), | ||
isDragging: isDraggingBlocks(), | ||
}; | ||
} | ||
|
||
|
@@ -60,6 +62,7 @@ function BlockPopover( { | |
hasMultiSelection, | ||
hasFixedToolbar, | ||
lastClientId, | ||
isDragging, | ||
} = useSelect( selector, [] ); | ||
const isLargeViewport = useViewportMatch( 'medium' ); | ||
const [ isToolbarForced, setIsToolbarForced ] = useState( false ); | ||
|
@@ -96,7 +99,8 @@ function BlockPopover( { | |
! shouldShowBreadcrumb && | ||
! shouldShowContextualToolbar && | ||
! isToolbarForced && | ||
! showEmptyBlockSideInserter | ||
! showEmptyBlockSideInserter && | ||
! isDragging | ||
) { | ||
return null; | ||
} | ||
|
@@ -136,6 +140,14 @@ function BlockPopover( { | |
setIsInserterShown( false ); | ||
} | ||
|
||
function onDragStart() { | ||
setIsToolbarForced( true ); | ||
} | ||
|
||
function onDragEnd() { | ||
setIsToolbarForced( false ); | ||
} | ||
|
||
// Position above the anchor, pop out towards the right, and position in the | ||
// left corner. For the side inserter, pop out towards the left, and | ||
// position in the right corner. | ||
|
@@ -165,8 +177,10 @@ function BlockPopover( { | |
onBlur={ () => setIsToolbarForced( false ) } | ||
shouldAnchorIncludePadding | ||
// Popover calculates the width once. Trigger a reset by remounting | ||
// the component. | ||
key={ shouldShowContextualToolbar } | ||
// the component. We include both shouldShowContextualToolbar and isToolbarForced | ||
// in the key to prevent the component being unmounted unexpectedly when isToolbarForced = true, | ||
// e.g. during drag and drop | ||
key={ shouldShowContextualToolbar || isToolbarForced } | ||
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 wasn't able to determine what effect this change has, updating the comment would be great. 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. Basically, if I will push a change that explains this. 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. @ellatrix Maybe we don't need this key anymore since now we compute recompute when the width/height changes in Popover? |
||
> | ||
{ ( shouldShowContextualToolbar || isToolbarForced ) && ( | ||
<div | ||
|
@@ -198,6 +212,8 @@ function BlockPopover( { | |
// it should focus the toolbar right after the mount. | ||
focusOnMount={ isToolbarForced } | ||
data-align={ align } | ||
onDragStart={ onDragStart } | ||
onDragEnd={ onDragEnd } | ||
/> | ||
) } | ||
{ shouldShowBreadcrumb && ( | ||
|
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.
Do you think there's away to extract the logic into a dedicated hook as it's kind of obscures the code of the component. Would something like that (or close) work?
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.
Maybe. I don't have a ton of bandwidth to refine this so you should feel free to mess around with it and propose a better-factored alternative.