in my case, how to get the source and container DOM elements?
It's actually quite easy.. just use document.querySelector()
to get the block node and then wp.dom.getScrollContainer()
to get that node's container:
const nextBlock = wp.blocks.createBlock( 'core/paragraph' );
wp.data.dispatch( 'core/editor' ).insertBlock( nextBlock );
const source = document.querySelector( '[data-block="' + nextBlock.clientId + '"]' );
const container = wp.dom.getScrollContainer( source );
References: One Two
And here's my code:
/**
* External dependencies
*/
import scrollIntoView from 'dom-scroll-into-view';
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks'; // wp.blocks.createBlock
import { dispatch } from '@wordpress/data'; // wp.data.dispatch
import { getScrollContainer } from '@wordpress/dom'; // wp.dom.getScrollContainer
function getBlockDOMNode( clientId ) {
return document.querySelector( '[data-block="' + clientId + '"]' );
}
const nextBlock = createBlock( 'core/paragraph' );
dispatch( 'core/editor' ).insertBlock( nextBlock );
const source = getBlockDOMNode( nextBlock.clientId );
const container = source ? getScrollContainer( source ) : null;
if ( source && container ) {
scrollIntoView( source, container );
}
UPDATE
For testing the import
ed scrollIntoView()
, try this:
function scrollBlockIntoView( block ) {
const source = getBlockDOMNode( block.clientId );
const container = source ? getScrollContainer( source ) : null;
if ( source && container ) {
console.log( source, container );
scrollIntoView( source, container );
}
}
window.scrollBlockIntoView = function( block ) {
scrollBlockIntoView( block || {} );
};
And then from the browser console, run this:
scrollBlockIntoView( wp.data.select('core/editor').getBlocks()[1] )
But make sure that you have at least two blocks in the editor — e.g. a paragraph with a lengthy content and an image block.
Tried and tested working on Chrome (Windows 10).
const blockNode = getBlockDOMNode( blockId )
andconst scrollContainer = getScrollContainer( blockNode )
as in MultiSelectScrollIntoView. Maybe likescrollIntoView( blockNode, scrollContainer, { onlyScrollIfNeeded: true, } );
. Do we have the block Id fromnextBlock.id
or with the'block-'
prefix ? – Scriabin