PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.2
GenerateBlocks v2.0.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / hooks / useSelectedBlockElements.js
generateblocks / src / hooks Last commit date
index.js 1 year ago useAuthors.js 3 years ago useBlockStyles.js 1 year ago useDebounceState.js 4 years ago useDeviceAttributes.js 2 years ago useDeviceType.js 1 year ago useInnerBlocksCount.js 4 years ago useQueryReducer.js 1 year ago useRecordsReducer.js 3 years ago useSelectedBlockElements.js 2 years ago useStyleIndicator.js 2 years ago useTaxonomies.js 1 year ago useTaxonomyRecords.js 3 years ago
useSelectedBlockElements.js
53 lines
1 import { useSelect } from '@wordpress/data';
2 import { useLayoutEffect, useState } from '@wordpress/element';
3
4 export function useSelectedBlockElements() {
5 const {
6 getSelectedBlockClientIds,
7 } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
8
9 const clientIds = getSelectedBlockClientIds();
10 const [ elements, setElements ] = useState( [] );
11
12 useLayoutEffect( () => {
13 if ( ! clientIds.length ) {
14 return;
15 }
16
17 const queryDocument = document.querySelector( 'iframe[name="editor-canvas"]' )?.contentDocument || document;
18 const editorStylesWrapper = queryDocument.querySelector( '.editor-styles-wrapper' );
19
20 setElements( clientIds.map( ( clientId ) => {
21 if ( null === editorStylesWrapper ) {
22 return null;
23 }
24
25 return editorStylesWrapper.querySelector( `[data-block="${ clientId }"]:not(.gb-is-root-block)` );
26 } ).filter( ( element ) => null !== element ) );
27 }, [ clientIds ] );
28
29 return elements;
30 }
31
32 export function useSelectedBlockElement() {
33 const {
34 getSelectedBlockClientId,
35 } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
36
37 const clientId = getSelectedBlockClientId();
38 const [ element, setElement ] = useState( null );
39
40 useLayoutEffect( () => {
41 if ( ! clientId ) {
42 return;
43 }
44
45 const queryDocument = document.querySelector( 'iframe[name="editor-canvas"]' )?.contentDocument || document;
46 const editorStylesWrapper = queryDocument.querySelector( '.editor-styles-wrapper' );
47
48 setElement( editorStylesWrapper.querySelector( `[data-block="${ clientId }"]:not(.gb-is-root-block)` ) ?? null );
49 }, [ clientId ] );
50
51 return element;
52 }
53