-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block editor: scroll selected block only if it has no focus (#30924)
- Loading branch information
Showing
5 changed files
with
76 additions
and
62 deletions.
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
64 changes: 64 additions & 0 deletions
64
packages/block-editor/src/components/block-list/use-block-props/use-scroll-into-view.js
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import scrollIntoView from 'dom-scroll-into-view'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { getScrollContainer } from '@wordpress/dom'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../../store'; | ||
|
||
export function useScrollIntoView( clientId ) { | ||
const ref = useRef(); | ||
const isSelectionEnd = useSelect( | ||
( select ) => { | ||
const { isBlockSelected, getBlockSelectionEnd } = select( | ||
blockEditorStore | ||
); | ||
|
||
return ( | ||
isBlockSelected( clientId ) || | ||
getBlockSelectionEnd() === clientId | ||
); | ||
}, | ||
[ clientId ] | ||
); | ||
|
||
useEffect( () => { | ||
if ( ! isSelectionEnd ) { | ||
return; | ||
} | ||
|
||
const extentNode = ref.current; | ||
|
||
// If the block is focused, the browser will already have scrolled into | ||
// view if necessary. | ||
if ( extentNode.contains( extentNode.ownerDocument.activeElement ) ) { | ||
return; | ||
} | ||
|
||
const scrollContainer = getScrollContainer( extentNode ); | ||
|
||
// If there's no scroll container, it follows that there's no scrollbar | ||
// and thus there's no need to try to scroll into view. | ||
if ( ! scrollContainer ) { | ||
return; | ||
} | ||
|
||
scrollIntoView( extentNode, scrollContainer, { | ||
onlyScrollIfNeeded: true, | ||
} ); | ||
}, [ isSelectionEnd ] ); | ||
|
||
return ref; | ||
} |
67 changes: 7 additions & 60 deletions
67
packages/block-editor/src/components/selection-scroll-into-view/index.js
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 |
---|---|---|
@@ -1,70 +1,17 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import scrollIntoView from 'dom-scroll-into-view'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { getScrollContainer } from '@wordpress/dom'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlockDOMNode } from '../../utils/dom'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
export function useScrollSelectionIntoView( ref ) { | ||
// Although selectionRootClientId isn't used directly in calculating | ||
// whether scrolling should occur, it is used as a dependency of the | ||
// effect to take into account situations where a block might be moved | ||
// to a different parent. In this situation, the selectionEnd clientId | ||
// remains the same, so the rootClientId is used to trigger the effect. | ||
const { selectionRootClientId, selectionEnd } = useSelect( ( select ) => { | ||
const selectors = select( blockEditorStore ); | ||
const selectionEndClientId = selectors.getBlockSelectionEnd(); | ||
return { | ||
selectionEnd: selectionEndClientId, | ||
selectionRootClientId: selectors.getBlockRootClientId( | ||
selectionEndClientId | ||
), | ||
}; | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
if ( ! selectionEnd ) { | ||
return; | ||
} | ||
|
||
const { ownerDocument } = ref.current; | ||
const extentNode = getBlockDOMNode( selectionEnd, ownerDocument ); | ||
|
||
if ( ! extentNode ) { | ||
return; | ||
} | ||
|
||
const scrollContainer = getScrollContainer( extentNode ); | ||
|
||
// If there's no scroll container, it follows that there's no scrollbar | ||
// and thus there's no need to try to scroll into view. | ||
if ( ! scrollContainer ) { | ||
return; | ||
} | ||
|
||
scrollIntoView( extentNode, scrollContainer, { | ||
onlyScrollIfNeeded: true, | ||
} ); | ||
}, [ selectionRootClientId, selectionEnd ] ); | ||
} | ||
import deprecated from '@wordpress/deprecated'; | ||
|
||
/** | ||
* Scrolls the multi block selection end into view if not in view already. This | ||
* is important to do after selection by keyboard. | ||
* | ||
* @deprecated | ||
*/ | ||
export function MultiSelectScrollIntoView() { | ||
const ref = useRef(); | ||
useScrollSelectionIntoView( ref ); | ||
return <div ref={ ref } />; | ||
deprecated( 'wp.blockEditor.MultiSelectScrollIntoView', { | ||
hint: 'This behaviour is now built-in.', | ||
} ); | ||
return null; | ||
} |