| 1 |
import { PanelBody } from '@wordpress/components'; |
| 2 |
import { useRef } from '@wordpress/element'; |
| 3 |
import React from 'react'; |
| 4 |
import getUAGEditorStateLocalStorage from '@Controls/getUAGEditorStateLocalStorage'; |
| 5 |
import { select } from '@wordpress/data'; |
| 6 |
|
| 7 |
const UAGAdvancedPanelBody = ( props ) => { |
| 8 |
|
| 9 |
const { |
| 10 |
children |
| 11 |
} = props; |
| 12 |
|
| 13 |
const panelRef = useRef( null ); |
| 14 |
const uagSettingState = getUAGEditorStateLocalStorage( 'uagSettingState' ); |
| 15 |
|
| 16 |
const onPanelToggle = () => { |
| 17 |
|
| 18 |
if ( 'enabled' === uagb_blocks_info.collapse_panels ) { |
| 19 |
const siblings = getSiblings( panelRef.current ); |
| 20 |
|
| 21 |
siblings.forEach( ( element ) => { |
| 22 |
element.querySelector( '.components-button' ).click(); |
| 23 |
} ); |
| 24 |
} |
| 25 |
|
| 26 |
// Below code is to set the setting state of Tab for each block. |
| 27 |
const { getSelectedBlock } = select( 'core/block-editor' ); |
| 28 |
const blockName = getSelectedBlock()?.name; |
| 29 |
|
| 30 |
let match = false; |
| 31 |
panelRef?.current?.classList.forEach( |
| 32 |
function( value ) { |
| 33 |
if ( value.includes( 'uag-advance-panel-body' ) ) { |
| 34 |
match = value |
| 35 |
} |
| 36 |
} |
| 37 |
); |
| 38 |
let inspectorTabName = 'style'; |
| 39 |
if ( panelRef?.current?.parentElement?.classList.contains( 'uagb-tab-content-general' ) ) { |
| 40 |
inspectorTabName = 'general'; |
| 41 |
} |
| 42 |
if ( panelRef?.current?.parentElement?.classList.contains( 'uagb-tab-content-advance' ) ) { |
| 43 |
inspectorTabName = 'advance'; |
| 44 |
} |
| 45 |
|
| 46 |
const data = { |
| 47 |
...uagSettingState, |
| 48 |
[blockName] : { |
| 49 |
...uagSettingState?.[blockName], |
| 50 |
selectedPanel: match, |
| 51 |
selectedTab : inspectorTabName |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
const uagLocalStorage = getUAGEditorStateLocalStorage(); |
| 56 |
if ( uagLocalStorage ) { |
| 57 |
uagLocalStorage.setItem( 'uagSettingState', JSON.stringify ( data ) ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
const getSiblings = function ( elem ) { |
| 62 |
|
| 63 |
const siblings = []; |
| 64 |
let sibling = elem.parentNode.firstChild; |
| 65 |
|
| 66 |
while ( sibling ) { |
| 67 |
if ( sibling.nodeType === 1 && sibling !== elem && sibling.classList.contains( 'is-opened' ) ) { |
| 68 |
siblings.push( sibling ); |
| 69 |
} |
| 70 |
sibling = sibling.nextSibling |
| 71 |
} |
| 72 |
|
| 73 |
return siblings; |
| 74 |
|
| 75 |
}; |
| 76 |
|
| 77 |
const panelTitle = props?.title ? props?.title.toLowerCase().replace( /[^a-zA-Z ]/g, '' ).replace( /\s+/g, '-' ) : ''; |
| 78 |
|
| 79 |
return ( |
| 80 |
<PanelBody |
| 81 |
{ ...props } |
| 82 |
onToggle={onPanelToggle} |
| 83 |
ref={panelRef} |
| 84 |
className={`uag-advance-panel-body-${panelTitle}`} |
| 85 |
> |
| 86 |
{ children } |
| 87 |
</PanelBody> |
| 88 |
); |
| 89 |
} |
| 90 |
export default React.memo( UAGAdvancedPanelBody ); |
| 91 |
|