-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add block inspector to the widget screen #16203
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,8 @@ | |
background: $light-gray-200; | ||
} | ||
} | ||
|
||
.block-editor-block-inspector__card { | ||
margin: 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Component which calls onBlockSelected prop when a block becomes selected. It | ||
* can be used to ensuring that only one block appears as selected | ||
* when multiple editors exist in a page. | ||
* | ||
* @type {WPComponent} | ||
*/ | ||
|
||
class SelectionObserver extends Component { | ||
componentDidUpdate( prevProps ) { | ||
const { | ||
hasSelectedBlock, | ||
onBlockSelected, | ||
isSelectedArea, | ||
clearSelectedBlock, | ||
} = this.props; | ||
|
||
if ( hasSelectedBlock && ! prevProps.hasSelectedBlock ) { | ||
onBlockSelected(); | ||
} | ||
|
||
if ( ! isSelectedArea && prevProps.isSelectedArea ) { | ||
clearSelectedBlock(); | ||
} | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
const { hasSelectedBlock } = select( 'core/block-editor' ); | ||
|
||
return { | ||
hasSelectedBlock: hasSelectedBlock(), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => { | ||
const { clearSelectedBlock } = dispatch( 'core/block-editor' ); | ||
|
||
return { | ||
clearSelectedBlock, | ||
}; | ||
} ), | ||
] )( SelectionObserver ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useState } from '@wordpress/element'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
|
@@ -10,8 +11,18 @@ import { withSelect } from '@wordpress/data'; | |
import WidgetArea from '../widget-area'; | ||
|
||
function WidgetAreas( { areas, blockEditorSettings } ) { | ||
const [ selectedArea, setSelectedArea ] = useState( 0 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really need this here? It feels like this could be local state for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The widget area controls which of the areas contains a selected block, when a new area is selected other becomes unselected, each WidgetArea is a sibling of each other and siblings can not "communicate" between each other. This local state in WidgetAreas that is the parent of each WidgetArea allows the WidgetArea components to communicate with each other, when one becomes selected the others become unselected and the corresponding effects get executed, I don't think is possible to make this state part of the WidgetArea component. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok makes sense. |
||
const onBlockSelectedInArea = useMemo( | ||
() => areas.map( ( value, index ) => ( () => { | ||
setSelectedArea( index ); | ||
} ) ), | ||
[ areas, setSelectedArea ] | ||
); | ||
|
||
return areas.map( ( { id }, index ) => ( | ||
<WidgetArea | ||
isSelectedArea={ index === selectedArea } | ||
onBlockSelected={ onBlockSelectedInArea[ index ] } | ||
blockEditorSettings={ blockEditorSettings } | ||
key={ id } | ||
id={ id } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useSelect, useDispatch? ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use hooks for this component but hooks did not work as expected, I always got unexpected results.
I tried to use a simple version where I relied on effects that execute when prop changes, and I also tried a usePrevious hook https://usehooks.com/usePrevious/ that allowed an implementation almost equal to this class component version, and both versions did not work properly. It is a very strange case I may be missing something.