-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FSE: Append new blocks inside post content block (#34425)
This PR replaces the block inserter in the header toolbar with a custom one that ensures that new blocks are appended inside the post content block. Since the default inserter in the header toolbar rendered by Gutenberg doesn't set any rootClientId, we cannot define which block should contain any new inserted block. This PR aims to solve that by rendering a replica of the same header inserter but using the client id of the post content block as rootClientId. The same client id is also used now when inserting a starter page template to avoid that content being appended after the footer (#34330). In order to don't display the default header inserter, we lock the template making impossible to insert new blocks in the root (thus the header doesn't show the default block inserter). But this also prevents the user from removing existing blocks, which solves the issue of having the post content block removable (#34331).
- Loading branch information
Showing
8 changed files
with
107 additions
and
2 deletions.
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
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
39 changes: 39 additions & 0 deletions
39
...ll-site-editing/full-site-editing-plugin/full-site-editing/editor/block-inserter/index.js
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,39 @@ | ||
/* global fullSiteEditing */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import domReady from '@wordpress/dom-ready'; | ||
import { render } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostContentBlockAppender from './post-content-block-appender'; | ||
|
||
/** | ||
* Renders a custom block inserter that will append new blocks inside the post content block. | ||
*/ | ||
function renderPostContentBlockInserter() { | ||
if ( 'page' !== fullSiteEditing.editorPostType ) { | ||
return; | ||
} | ||
|
||
const editPostHeaderToolbarInception = setInterval( () => { | ||
const headerToolbar = document.querySelector( '.edit-post-header-toolbar' ); | ||
|
||
if ( ! headerToolbar ) { | ||
return; | ||
} | ||
clearInterval( editPostHeaderToolbarInception ); | ||
|
||
const blockInserterContainer = document.createElement( 'div' ); | ||
blockInserterContainer.classList.add( 'fse-post-content-block-inserter' ); | ||
|
||
headerToolbar.insertBefore( blockInserterContainer, headerToolbar.firstChild ); | ||
|
||
render( <PostContentBlockAppender />, blockInserterContainer ); | ||
} ); | ||
} | ||
|
||
domReady( () => renderPostContentBlockInserter() ); |
26 changes: 26 additions & 0 deletions
26
...ite-editing-plugin/full-site-editing/editor/block-inserter/post-content-block-appender.js
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,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Inserter } from '@wordpress/editor'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
const PostContentBlockAppender = compose( | ||
withSelect( select => { | ||
const { getBlocks, getEditorSettings } = select( 'core/editor' ); | ||
const { getEditorMode } = select( 'core/edit-post' ); | ||
|
||
const postContentBlock = getBlocks().find( block => block.name === 'a8c/post-content' ); | ||
|
||
return { | ||
rootClientId: postContentBlock ? postContentBlock.clientId : '', | ||
showInserter: getEditorMode() === 'visual' && getEditorSettings().richEditingEnabled, | ||
}; | ||
} ) | ||
)( ( { rootClientId, showInserter } ) => { | ||
return ( | ||
<Inserter rootClientId={ rootClientId } disabled={ ! showInserter } position="bottom right" /> | ||
); | ||
} ); | ||
|
||
export default PostContentBlockAppender; |
5 changes: 5 additions & 0 deletions
5
apps/full-site-editing/full-site-editing-plugin/full-site-editing/editor/index.js
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,5 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './block-inserter'; | ||
import './template-validity-override'; |
25 changes: 25 additions & 0 deletions
25
...ing/full-site-editing-plugin/full-site-editing/editor/template-validity-override/index.js
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,25 @@ | ||
/* global fullSiteEditing */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import domReady from '@wordpress/dom-ready'; | ||
import { dispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Forces the template validity. | ||
* | ||
* This is a work-around for the existing core issue that is showing a template mismatch warning when there is a parent | ||
* block with a locked template containing a nested InnerBlocks with an unlocked template. | ||
* | ||
* @see https://github.com/WordPress/gutenberg/issues/11681 | ||
*/ | ||
function resetTemplateValidity() { | ||
if ( 'page' !== fullSiteEditing.editorPostType ) { | ||
return; | ||
} | ||
|
||
dispatch( 'core/editor' ).setTemplateValidity( true ); | ||
} | ||
|
||
domReady( () => resetTemplateValidity() ); |
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