| 1 |
import { applyFilters } from '@wordpress/hooks'; |
| 2 |
import { useRef, useEffect } from '@wordpress/element'; |
| 3 |
import getUAGEditorStateLocalStorage from '@Controls/getUAGEditorStateLocalStorage'; |
| 4 |
import { select } from '@wordpress/data'; |
| 5 |
|
| 6 |
const InspectorTab = ( props ) => { |
| 7 |
const { children, isActive, type } = props; |
| 8 |
const tabRef = useRef( null ); |
| 9 |
|
| 10 |
const tabContent = function () { |
| 11 |
return applyFilters( |
| 12 |
`uag_${ type }_tab_content`, |
| 13 |
'', |
| 14 |
props.parentProps |
| 15 |
); |
| 16 |
}; |
| 17 |
|
| 18 |
useEffect( () => { |
| 19 |
|
| 20 |
const { getSelectedBlock } = select( 'core/block-editor' ); |
| 21 |
const blockName = getSelectedBlock()?.name; |
| 22 |
const uagSettingState = getUAGEditorStateLocalStorage( 'uagSettingState' ); |
| 23 |
|
| 24 |
if ( uagSettingState ) { |
| 25 |
const inspectorTabName = uagSettingState[blockName]?.selectedTab; |
| 26 |
const panelBodyClass = uagSettingState[blockName]?.selectedPanel; |
| 27 |
const settingsPopup = uagSettingState[blockName]?.selectedSetting; |
| 28 |
const selectedInnerTab = uagSettingState[blockName]?.selectedInnerTab; |
| 29 |
|
| 30 |
// This code is to fix the side-effect of the editor responsive click settings panel refresh issue AND aldo for preserving state for better block editor experinence. |
| 31 |
if ( inspectorTabName && type === inspectorTabName ) { |
| 32 |
let panelToActivate = false |
| 33 |
if ( panelBodyClass ) { |
| 34 |
panelToActivate = tabRef.current.querySelector( `.${panelBodyClass}` ); |
| 35 |
} else { |
| 36 |
panelToActivate = tabRef.current.querySelector( '.is-opened' ); |
| 37 |
} |
| 38 |
|
| 39 |
if ( panelToActivate ) { |
| 40 |
if ( ! panelToActivate.classList.contains( 'is-opened' ) ) { |
| 41 |
panelToActivate.querySelector( '.components-button' ).click(); |
| 42 |
} |
| 43 |
if ( selectedInnerTab ) { |
| 44 |
// Need a delay to open the popup as the makup load just after the above click function called. |
| 45 |
setTimeout( function() { |
| 46 |
const selectedInnerTabToActivate = panelToActivate.querySelector( selectedInnerTab ); |
| 47 |
if ( selectedInnerTabToActivate && ! selectedInnerTabToActivate.classList.contains( 'active-tab' ) ) { |
| 48 |
selectedInnerTabToActivate.click(); |
| 49 |
} |
| 50 |
}, 100 ); |
| 51 |
} |
| 52 |
if ( settingsPopup ) { |
| 53 |
// Need a delay to open the popup as the makup load just after the above click function called. |
| 54 |
setTimeout( function() { |
| 55 |
const settingsPopupToActivate = panelToActivate.querySelector( settingsPopup ); |
| 56 |
|
| 57 |
if ( settingsPopupToActivate && ! settingsPopupToActivate.classList.contains( 'active' ) ) { |
| 58 |
settingsPopupToActivate.querySelector( '.components-button' ).click(); |
| 59 |
} |
| 60 |
}, 100 ); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
}, [] ); |
| 66 |
|
| 67 |
return ( |
| 68 |
<div |
| 69 |
style={ { |
| 70 |
display: isActive ? 'block' : 'none', |
| 71 |
} } |
| 72 |
className={ `uagb-inspector-tab uagb-tab-content-${ type }` } |
| 73 |
ref={tabRef} |
| 74 |
> |
| 75 |
{ Array.isArray( children ) |
| 76 |
? children.map( ( item ) => item ) |
| 77 |
: children } |
| 78 |
{ tabContent() } |
| 79 |
</div> |
| 80 |
); |
| 81 |
}; |
| 82 |
|
| 83 |
export default InspectorTab; |
| 84 |
|
| 85 |
export const UAGTabs = { |
| 86 |
general: { |
| 87 |
key: 'general', |
| 88 |
type: 'general', |
| 89 |
}, |
| 90 |
style: { |
| 91 |
key: 'style', |
| 92 |
type: 'style', |
| 93 |
}, |
| 94 |
advance: { |
| 95 |
key: 'advance', |
| 96 |
type: 'advance', |
| 97 |
}, |
| 98 |
} |
| 99 |
|