-
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
Template Part Block: Add a block label #24557
Changes from all commits
df890f8
aba1d59
765c441
265a487
3dcb166
99c6daa
b381994
8502f49
b6df020
c5cb850
e02db48
9c22072
791e110
062cb35
56b8e36
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 |
---|---|---|
|
@@ -481,6 +481,35 @@ export const getBlockParents = createSelector( | |
( state ) => [ state.blocks.parents ] | ||
); | ||
|
||
/** | ||
* Given a block client ID, returns the direct parent. | ||
* | ||
* @param {Object} state Editor state. | ||
* @param {string} clientId Block from which to find root client ID. | ||
* | ||
* @return {string} ClientID of the parent block. | ||
*/ | ||
export const getBlockParent = createSelector( | ||
( state, clientId ) => { | ||
return state.blocks.parents[ clientId ]; | ||
}, | ||
( state ) => [ state.blocks.parents ] | ||
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. Not totally sure, maybe we should change this to |
||
); | ||
|
||
/** | ||
* Returns the current id of the Block toolbar | ||
* | ||
* @param {Object} state Editor state. | ||
* | ||
* @return {string} Id of the currently focused block toolbar. | ||
*/ | ||
export const getToolbarId = createSelector( | ||
( state ) => { | ||
return state.toolbarId; | ||
}, | ||
( state ) => [ state.toolbarId ] | ||
); | ||
|
||
/** | ||
* Given a block client ID and a block name, | ||
* returns the list of all its parents from top to bottom, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
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. Note: We can definitely talk about refactoring the labels component for reusability if we move forward with template part labels |
||
* External dependencies | ||
*/ | ||
import { capitalize } from 'lodash'; | ||
import cx from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef, useState, useEffect } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
import { Icon } from '@wordpress/components'; | ||
|
||
function detectOverlap( e1, e2 ) { | ||
const rect1 = e1 && e1.getBoundingClientRect(); | ||
const rect2 = e2 && e2.getBoundingClientRect(); | ||
|
||
let overlap = null; | ||
if ( rect1 && rect2 ) { | ||
overlap = ! ( | ||
rect1.right < rect2.left || | ||
rect1.left > rect2.right || | ||
rect1.bottom < rect2.top || | ||
rect1.top > rect2.bottom | ||
); | ||
return overlap; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default function TemplatePartLabel( { postId, slug } ) { | ||
const [ title ] = useEntityProp( | ||
'postType', | ||
'wp_template_part', | ||
'title', | ||
postId | ||
); | ||
Comment on lines
+34
to
+39
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. I don't have a strong opinion on this, but figured I would point it out. It seems we are also using this 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. I don't have a strong opinion either. If this becomes widely shared, I'd err on the side of removing implicit dependencies and moving |
||
|
||
const { toolbarId } = useSelect( ( select ) => { | ||
const { getToolbarId } = select( 'core/block-editor' ); | ||
return { toolbarId: getToolbarId() }; | ||
} ); | ||
|
||
const labelElement = useRef( null ); | ||
const [ toolbarElement, setToolbarElement ] = useState( null ); | ||
|
||
useEffect( () => { | ||
const toolbar = document.getElementById( toolbarId ); | ||
setToolbarElement( toolbar ); | ||
}, [ toolbarId ] ); | ||
|
||
const isOverlapped = detectOverlap( labelElement.current, toolbarElement ); | ||
const label = capitalize( title || slug ); | ||
|
||
return ( | ||
<div | ||
className={ cx( 'wp-block-template-part__label-container', { | ||
overlapped: isOverlapped, | ||
} ) } | ||
> | ||
<div className="wp-block-template-part__label-layout"> | ||
<div | ||
ref={ labelElement } | ||
className="wp-block-template-part__label-content" | ||
> | ||
<Icon icon="block-default" size={ 13 } /> | ||
<span className="wp-block-template-part__label-text"> | ||
{ label } | ||
</span> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
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.
Note:
After combing through
core/block-editor
package's selectors, I couldn't find one that would only provide information about the direct parent. It seemed as if the other alternatives, like getBlockParents, returned all ancestor ids. This can be changed if someone has better suggestions.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.
this seems like a legitimate change to me if the behavior doesn't exist yet 👍
I'm curious how the "select parent block" functionality works without this, though??
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.
Great question! I'll look into it. That might give us insight into a selector we can use 🙂
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.
The only reference I could find to select parent block was in entity-record-item.js L42. It looks like they select the parent block by deriving information about the direct parent from lines 16 - 29.
This is an option if we don't want to include a new selector to the block-editor store. I don't have a strong opinion either way.