admin管理员组文章数量:1192816
Is there a way to detect when the position of a block has changed? I am trying to do something when a block is moved.
I'm trying:
var blockIndex = wp.data.select( "core/block-editor" ).getBlockIndex( clientId );
const [ index, setIndex ] = useState(blockIndex);
useEffect( () => {
console.log('Block moved');
}, [ blockIndex ] );
Is there a way to detect when the position of a block has changed? I am trying to do something when a block is moved.
I'm trying:
var blockIndex = wp.data.select( "core/block-editor" ).getBlockIndex( clientId );
const [ index, setIndex ] = useState(blockIndex);
useEffect( () => {
console.log('Block moved');
}, [ blockIndex ] );
Share
Improve this question
asked Aug 5, 2022 at 11:32
CyberJCyberJ
4375 silver badges19 bronze badges
1 Answer
Reset to default 3I don't know if there's an event or Gutenberg hook like blockMove
or blockIndexChange
, but you can use subscribe()
from the @wordpress/data package to listen to the block index changes.
Working Example using hooks
WordPress dependencies:
import { useBlockProps } from '@wordpress/block-editor'; import { useState, useEffect } from '@wordpress/element'; import { useSelect, subscribe } from '@wordpress/data';
The block's
edit()
function:function edit( { clientId } ) { const { getBlockIndex } = useSelect( 'core/block-editor' ); // Set initial index. const [ currentBlockIndex, setBlockIndex ] = useState( getBlockIndex( clientId ) ); // Subscribe once this block has been attached to the DOM. useEffect( () => { // Listen to block index changes and set the current index if necessary. // When you're done listening, just call unsubscribe(). const unsubscribe = subscribe( () => { const blockIndex = getBlockIndex( clientId ); if ( currentBlockIndex !== blockIndex ) { console.log( `block move detected; from index ${ currentBlockIndex} to ${ blockIndex }` ); setBlockIndex( blockIndex ); // update current index } } ); console.log( `subscribed; clientId is ${ clientId }` ); // Unsubscribe; for example once this block is removed from the editor. return function cleanup() { unsubscribe(); console.log( `unsubscribed - stopped listening to index changes; clientId is ${ clientId }` ); }; }, [ clientId, currentBlockIndex ] ); // note the currentBlockIndex return ( <div { ...useBlockProps() }> Current block index: { currentBlockIndex } </div> ); }
So the
useEffect
callback will be called when the block is explicitly moved or that its index changed because another block was moved to the above block's current position.
You can also see my answer here for an example outside an edit
function, which uses wp.domReady()
and adds a block style once the current post type is determined.
本文标签: Gutenberg on block move
版权声明:本文标题:Gutenberg on block move 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738473382a2088721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论