Skip to content

Commit

Permalink
Disable selection of template blocks
Browse files Browse the repository at this point in the history
This means that you can't see any reference to their settings!

Note that we cannot do this same patch for the post content
block because once the block has been disabled, you can't
edit the inner blocks. Will have to find a different patch for that.
  • Loading branch information
noahtallen committed Aug 3, 2019
1 parent e5ed9c4 commit 20e8c2f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* global fullSiteEditing */

/**
* External dependencies
*/
import { select, dispatch, subscribe } from '@wordpress/data';
import { get } from 'lodash';

/**
* Removes the block settings panel if the template or post content blocks are selected.
* Since it is not possible to disable the block settings entirely through Gutenberg state,
* we use a hack to deselect the block, which removes the option to change its settings.
* This is also done officially in the core PostTitle block, so there is prior art.
*
* @see https://github.com/WordPress/gutenberg/blob/master/packages/editor/src/components/post-title/index.js
*/
const unsubscribe = subscribe( () => {
// We don't care about this on the template or post editor
if ( 'page' !== fullSiteEditing.editorPostType ) {
return unsubscribe();
}

// Determine which block we have selected:
const selectedBlock = select( 'core/editor' ).getSelectedBlock();
const blockName = get( selectedBlock, 'name', null );

// If we have selected one the template block, deselect it.
// Note: this does not work for post content because you can't
// edit inner blocks if the block is not selected.
if ( 'a8c/template' === blockName ) {
dispatch( 'core/block-editor' ).clearSelectedBlock( blockName );
}
} );
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import './block-inserter';
import './template-validity-override';
import './image-block-keywords';
import './disable-block-settings';
import './style.scss';

0 comments on commit 20e8c2f

Please sign in to comment.