-
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
API for Inter-block communication #5012
Comments
I should also add, that these selectors are amazing to have: https://github.com/WordPress/gutenberg/blob/master/editor/store/index.js#L14 I would love to also be able to access all of the post's taxonomy's terms. That would be so cool. For example, I could dynamically populate embeds of related content - or suggest them - based on categories and tags as they change. |
@Shelob9, thanks for opening this issue and your detailed description. I think we both are on the same page in terms how |
Maybe I'm not understanding properly but it seems that there's no need for another API to achieve this. The A plugin should be able to filter this list of blocks to get attributes from the desired related block. |
import counter from '../counter';
...
<button
//When button is increased, increase count
onClick={() => counter.dispatch({ type: 'INCREMENT' })}
>
... @youknowriad - the other parts is related to dispatching actions. It is possible to dispatch an action from the store you own but we don't have any abstraction that mirrors |
RE: @gziolo @youknowriad said:
Is the answer to these questions, yes? If so, then we're largely covered.
|
Another part of this -- more a documentation issue -- is that I may want a private store. IE I want to be able to communicate between my own blocks, without allowing I think the solution here would be to register my own |
Moving from over here: #5006 (comment) My use case is when there is an initial configuration object that I want to be editable by other developers if they'd like to extend my block further. For instance (just a hypothetical) let's say I have an "Aspect Ratio Responsive Image Block." In the Inspector Controls I have a single dropdown menu which has a list of aspect ratios to choose from. However, I'd like to also allow other developers to make their own plugin, extend out my block, and add their own aspect ratios to that dropdown menu (or remove existing ones). The way I was trying to solve this was to create the initial default list, the variable So this would require the ability to subscribe to changes in other blocks and make modifications to data there. I'm not sure if this is currently possible with a mix of reducers and
And then modifying that data elsewhere could be something like:
That's really rough, but I think it gives some idea of what I'm thinking. If I'm off-base and there's a way to do this now without much difficulty I'd be happy to use that approach. But can't seem to find one that works at the moment. |
@JasonHoffmann Thank you. I think you're getting at why, even though what you need is pretty much possible right now -- at master, not current version -- this is still worth discussing. So, you almost have problem solved with wp.data, see this example: https://gitlab.com/caldera-labs/gutenberg-examples/ex-6-counter/blob/master/src/counter.js That's making a new store, just an integer in this case, but could be an array of aspect ratios available to a component, like the one in your inspector controls. Ok, fine, but how does my plugin extending yours add an aspect ratio, or make it so 4:3 aspect ratio is never acceptable? Do I dispatch an action you registered in your store? I think, but how do I know you've registered this action? How do you make sure that I don't push some totally invalid data into your array? Whose responsibility is it to validate the data? Also, as a plugin developer extending your plugin how do I discover the shape of your selectors and what selectors you'd made available? I don't know the Redux ecosystem well enough to know how much of this can be solved through existing tools. I do know that the serious weakness in the PHP WordPress hooks is that, opposed to a modern, object-oriented event framework, we can't discovery parameters of events, or enforcing types on the return values. I develop a popular plugin that has tons of add-ons and site-specific code assuming our hooks work a certain way. Developing extensible software around assumptions is in my opinion, not as good as having declarative and discoverable APIs, like the one being created here. #TL;DR |
Please note that API has slightly changed to
Attributes are stored inside Redux state, too. So it should be possible to open for public usage selector which fetches this data and then you could use |
|
I think that you can at least browse available selectors after @aduth refactored |
I'm still a bit unclear about how to use I've posted a gist of what I'm working on, which is a pretty basic Syntax Highlighting block. I have a list of languages in an array,
|
@gizlo This is amazing information. I still don't get 100% the best way for the add-on plugin to push new options into the store created with @JasonHoffmann I found reading this to be really helpful when learning Redux in a WordPress context https://github.com/WordPress/gutenberg/blob/master/data/index.js @youknowriad This feels exactly like the kind of thing you were talking about when I asked if the example code for the tutorial I'm working about felt right. So some pseudo-tutorial that I think summarizes what we've learned here. If these paragraphs and outline are correct, then I'll write this up for real, once this release is done. Writing Extensible BlocksLet's say there are two plugins. One is a "core" plugin, the other is an "add-on". The "core" plugin is some sort of video player plugin that has a block for selecting the video aspect ratio. The "add-on" plugin that adds support for new aspect ratios. In order to make the aspect ratio feature extensible, the list of available aspect ratios used in the aspect ratios selectors needs some way for the add-on plugin to add aspect ratios. First, the core plugin will need to move the array of aspect ratios into a "store" registered with
So, I started this way, which ended poorly:
So that was a bad idea I think, but then I re-read the Redux docs on side effects because of all of your comments and what Riad keeps saying. So I want to refactor the above code example, which probably would never would work based on this answer from The Oracle
|
@JasonHoffmann this is more or less how it should look like when using I have already explained everything in depth in one of my previous comments. Linking here in case you missed: #5006 (comment). |
@Shelob9 I think all that you wrote is going to be further simplified once |
I'm closing this one as we improved Data module in the recent days. Full documentation lives under: https://github.com/WordPress/gutenberg/tree/master/data. All actions that core uses should be now exposed for all plugins that would like to take advantage of them. The same applies to selectors. This opens a magnitude of possibilities when trying to extend editor with plugins. Let me also summarize new additions that are the most important the context of our discussion in this issue. We added the following methods:
If there are more question, I'm happy to answer them in here. If there is something else that we should consider to make the proposed API more flexible, please open a new issue to make discussion focused on the proposed concept. |
I made this trivial example of how two blocks can communicate between each other. https://gitlab.com/caldera-labs/gutenberg-examples/ex-6-counter It is Redux's counter example in WordPress.
@gziolo asked me to open an issue with use cases where plugin developers need to subscribe to changes in the editor or subscribe to changes in each other's state. We discussed it here
Specifically @gziolo said:
I gave the example of a client site I had that needed to populate WooCommerce's metabox with data from an API lookup based on one field's value. I used jQuery.on() and jQuery.val() a lot on that job. With Gutenberg, it would be crazy for me as a plugin developer to use the DOM as the single source of truth for state. I want to subscribe to state through
wp.data
, which I can.Another example would be if we had a custom block for information about a musical artist. We might also want to develop a custom block that extended the default SoundCloud block, but had a subscription to the artist block. We'd want our extended SoundCloud block to update to a playlist of that artist, if the custom artist block existed.
Relationships between posts of different types -- books to authors -- is something that plugins like Pods or Posts 2 Posts do. The UI is tricky, since you really want to edit two posts at once. In the author metabox of the books post type, you should be able to select and edit existing authors and create new authors.
With Gutenberg, I'd like to be able to develop a set of blocks for books and a set of blocks for authors. The set of blocks I'm building for books will need to include an author block, I'd like to use the same components to construct the book author block as I do constructing the blocks I need for the author post type.
As a plugin developer I need an API that provides for inter-block communication and is consistently shaped no matter which of these two contexts I'm in. Also, once this API is well defined, I can use it with other instances of
wp.data
or of Redux, which makes my components potentially useful in the front-end, which is where this gets even more fun :)Related for context:
The text was updated successfully, but these errors were encountered: