Interacting with the Editor
Previously, we have looked at User Components and how to write them, but what about all the other components that are integral to our page editor like a Toolbar for users to edit components, or maybe a layers panel?
The useEditor hook allows us to read and manipulate the entire editor's internal state. Essentially, this is similar to the useNode hook we have seen previously, except this deals with the entire editor rather than with a particular Node.
Note: Unlike
useNodewhich can be only used inside User Components,useEditorcan be used anywhere within the Craft context, including User Components.
Getting state information#
Similar with the useNode, we can specify a collector function to the useEditor hook to retrieve information from the internal state.
Connectors#
WithuseEditor, you can add connectors to DOM elements anywhere in the editor to make use of the editor's internal events.
Let's say, maybe you are creating a layers panel to display all nodes as Photoshop-like layers (wink wink, craftjs-layers) and would like to change the particular Node's event state when the user clicks/hovers/drags your layers.
Or, maybe drag a button to create a new instance of a User Component
Manipulating state#
We can use the actions provided to manipulate the editor's internal state
Querying#
What if you need to know if two nodes are compatible, how to get all parents of a Node or simply deserialize all Nodes? Queries are methods that provide helpful information based on the editor's state.
Queries are also accesible via the useEditor collector function. Let's look at an example where we build a component that returns all the descendants of the current selected Node: