-
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 Bindings Panel to Block Inspector #61527
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9b3aa18
Add initial pass at bindings panel in Block Inspector
artemiomorales 5551f32
Add bindings panel to inspector controls with tabs
artemiomorales 1427173
Revise classnames and structure, add styles
artemiomorales ebc8e26
Rename BindingsPanel to BlockBindingsPanel
artemiomorales 443c1b6
Include Bindings Panel with hooks instead
artemiomorales 0485eeb
Revert extraneous changes
artemiomorales cc01bb7
Revert deletion of space
artemiomorales 140d6bd
Remove unnecessary unlock
artemiomorales b8bbfff
Remove unused declaration
artemiomorales 61e7b74
Simplify check for bindings
artemiomorales 39c43f5
Rename file; update imports
artemiomorales 3771101
Merge useSelect calls
artemiomorales f341e60
Use block context to look up bindings
artemiomorales 7aee321
Add handling for unknown sources
artemiomorales 6492787
Remove unnecessary use of index
artemiomorales 22086a5
Simplify access of bindings
artemiomorales c664535
Use existing HStack instead of CSS
artemiomorales 1c74ddc
Remove error state; show source name if label is undefined
artemiomorales File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as blocksStore } from '@wordpress/blocks'; | ||
import { | ||
PanelBody, | ||
__experimentalHStack as HStack, | ||
__experimentalItemGroup as ItemGroup, | ||
__experimentalItem as Item, | ||
} from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../lock-unlock'; | ||
import InspectorControls from '../components/inspector-controls'; | ||
|
||
export const BlockBindingsPanel = ( { metadata } ) => { | ||
const { bindings } = metadata || {}; | ||
const { sources } = useSelect( ( select ) => { | ||
const _sources = unlock( | ||
select( blocksStore ) | ||
).getAllBlockBindingsSources(); | ||
|
||
return { | ||
sources: _sources, | ||
}; | ||
}, [] ); | ||
|
||
if ( ! bindings ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<InspectorControls> | ||
<PanelBody | ||
title={ __( 'Bindings' ) } | ||
className="components-panel__block-bindings-panel" | ||
> | ||
<ItemGroup isBordered isSeparated size="large"> | ||
{ Object.keys( bindings ).map( ( key ) => { | ||
return ( | ||
<Item key={ key }> | ||
<HStack> | ||
<span>{ key }</span> | ||
<span className="components-item__block-bindings-source"> | ||
{ sources[ bindings[ key ].source ] ? ( | ||
sources[ bindings[ key ].source ] | ||
.label | ||
) : ( | ||
<span className="error"> | ||
Error: Unknown Source | ||
</span> | ||
) } | ||
</span> | ||
</HStack> | ||
</Item> | ||
); | ||
} ) } | ||
</ItemGroup> | ||
</PanelBody> | ||
</InspectorControls> | ||
); | ||
}; | ||
|
||
export default { | ||
edit: BlockBindingsPanel, | ||
attributeKeys: [ 'metadata' ], | ||
hasSupport() { | ||
return true; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.components-panel__block-bindings-panel .components-item__block-bindings-source { | ||
|
||
color: $gray-700; | ||
|
||
.error { | ||
color: $alert-red; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should be translatable.
What about the case when the source gets registered only on the server? It's all custom sources these days because the client-side API is still private. @SantosGuillamot and I were discussing exposing the registered sources from the server in a different PR
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.
@gziolo I don't follow 🤔 Sources that get registered on the server using
register_block_bindings_source()
need to provide alabel
, which is what we would use here. The error would only arise when users try to bind a block to a source and mistype the source's name, or if they attempt to connect to a source that hasn't been registered. And as far as I see, the panel would get rendered regardless of what kind of block it is, core or custom, as long as it has themetadata.bindings
property (though this should probably be tested — will take a look at that).To clarify, what are you referring to when you say "client-side API", and how does it relate to this scenario?
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 sources we are consuming in the editor need to be registered in JS as well. They provide functions to know how to get the values and how to update them in the editor. One example could be the post-meta source that is registered here.
Registering in the client is not mandatory, and it doesn't mean it won't work on the server. So it is not exactly an error. I believe right now we are using "Dynamic Data" as a generic message, but we could also fallback to the key maybe.
In the future, as Greg mentions, we would like to reuse the server registration somehow, but that will be handled in a separate PR.
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.
Ok got it! Thanks so much for the explanation, that's very helpful 😄
I can revise to use the name in the binding when a label isn't present 👍
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.
Update: I've replaced the error with the machine-readable name.