-
This is actually developer help question, I asked it at wordpress stack exchange, but without proper answer. I want to have nested block editor as a 'window' inside another block editor. Parent block editor would serve to edit page content, while nested block editor would serve to edit content of certain custom post type. This is what I attempted, and editor does appear, but it seems it is not connected with post content. Is it possible to do what I had in mind? /**
* Component for entering biography
*/
import { BlockEditorProvider, BlockList, WritingFlow, ObserveTyping } from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import { useState, useEffect, useSelect } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { EntityProvider, useEntityBlockEditor, useEntityId } from '@wordpress/core-data';
// const TABLE = 'tla_mit_lecturers_biography';
const BiographyModal = ({ name, showButton, postID, setPostID }) => {
const [ isOpen, setIsOpen ] = useState(false);
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
'biographies',
{ id: postID }
);
const retrievedID = useEntityId( 'postType', 'biographies' );
useEffect(() => setPostID(retrievedID), [retrievedID])
const editorSettings = {
allowedBlockTypes: ['core/paragraph']
}
const editorInstance =
// <EntityProvider
// kind="postType"
// type="biographies"
// id={postID}
// >
<BlockEditorProvider
value={ blocks }
onChange={ onChange }
onInput={ onInput }
// selection={ selection }
settings={ editorSettings }
useSubRegistry={ true }
>
<WritingFlow>
<ObserveTyping>
<BlockList />
</ObserveTyping>
</WritingFlow>
</BlockEditorProvider>;
// </EntityProvider>;
const modal =
<div className="biography-modal">
<h3>{ `Životopis ${ name }` }</h3>
{editorInstance}
<Button isDefault onClick={ () => setIsOpen(false) }>
Završi
</Button>
</div>;
return (
<div className="biography-module">
{
(showButton || isOpen) &&
<Button isDefault disabled={isOpen} onClick={ () => setIsOpen( true ) }>Uredi životopis</Button>
}
{ isOpen && modal }
</div>
)
}
export default BiographyModal; |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Seems like it should work, except I don't understand what is happening with this bit:
Is it needed? I think your component should know what the id is via the prop rather than setting it itself. This might be where it's going wrong.
|
Beta Was this translation helpful? Give feedback.
-
Ok, I understand my mistake. From what you can see in the code, I attempted placing |
Beta Was this translation helpful? Give feedback.
-
OK, it works now, however, I am still missing one thing - if post is not created yet, nested editor does not create it upon first save - it has to have post ID in advance? But what if user opens editor and then closes the empty editor - post should not be created immediately? I would like it to function just like main editor - post is created only upon save. And another thing - I cannot use main editor's inserter? I must provide separate instance for the nested editor? |
Beta Was this translation helpful? Give feedback.
-
I don't really have an answer for this, you'd need to experiment. Most of the examples in Gutenberg now (reusable block, template part) create a post up front. It might be that you can make your block work using a normal implementation of inner blocks until the user wants to save for the first time, at which point you make the entity save the current inner blocks and switch it over to using the entity block editor.
That should work. Check the reusable block and template part blocks for examples: |
Beta Was this translation helpful? Give feedback.
Seems like it should work, except I don't understand what is happening with this bit:
Is it needed? I think your component should know what the id is via the prop rather than setting it itself. This might be where it's going wrong.
useEntityId
will also only work within anEntityProvider
(you'd need a parent component implementing this so that the hook can access the react context).