Skip to content

Commit

Permalink
Make global inserter always visible & Fix a Global inserter bug (#22115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta authored May 21, 2020
1 parent ce65934 commit e290640
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 33 deletions.
10 changes: 8 additions & 2 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ class Inserter extends Component {
blockTitle,
hasSingleBlockType,
toggleProps,
hasItems,
renderToggle = defaultRenderToggle,
} = this.props;

return renderToggle( {
onToggle,
isOpen,
disabled,
disabled: disabled || ! hasItems,
blockTitle,
hasSingleBlockType,
toggleProps,
Expand Down Expand Up @@ -249,5 +250,10 @@ export default compose( [
},
};
} ),
ifCondition( ( { hasItems } ) => hasItems ),
// The global inserter should always be visible, we are using ( ! isAppender && ! rootClientId && ! clientId ) as
// a way to detect the global Inserter.
ifCondition(
( { hasItems, isAppender, rootClientId, clientId } ) =>
hasItems || ( ! isAppender && ! rootClientId && ! clientId )
),
] )( Inserter );
1 change: 1 addition & 0 deletions packages/e2e-tests/plugins/cpt-locking.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function gutenberg_test_cpt_locking() {
),
),
array( 'core/quote' ),
array( 'core/columns' ),
);
register_post_type(
'locked-all-post',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ exports[`cpt locking template_lock all should not error when deleting the cotent
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->"
<!-- /wp:quote -->
<!-- wp:columns -->
<div class=\\"wp-block-columns\\"></div>
<!-- /wp:columns -->"
`;
exports[`cpt locking template_lock false should allow blocks to be inserted 1`] = `
Expand All @@ -27,6 +31,10 @@ exports[`cpt locking template_lock false should allow blocks to be inserted 1`]
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->
<!-- wp:columns -->
<div class=\\"wp-block-columns\\"></div>
<!-- /wp:columns -->
<!-- wp:list -->
<ul><li>List content</li></ul>
<!-- /wp:list -->"
Expand All @@ -43,7 +51,11 @@ exports[`cpt locking template_lock false should allow blocks to be moved 1`] = `
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->"
<!-- /wp:quote -->
<!-- wp:columns -->
<div class=\\"wp-block-columns\\"></div>
<!-- /wp:columns -->"
`;
exports[`cpt locking template_lock false should allow blocks to be removed 1`] = `
Expand All @@ -53,7 +65,11 @@ exports[`cpt locking template_lock false should allow blocks to be removed 1`] =
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->"
<!-- /wp:quote -->
<!-- wp:columns -->
<div class=\\"wp-block-columns\\"></div>
<!-- /wp:columns -->"
`;
exports[`cpt locking template_lock insert should allow blocks to be moved 1`] = `
Expand All @@ -67,5 +83,9 @@ exports[`cpt locking template_lock insert should allow blocks to be moved 1`] =
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote -->"
<!-- /wp:quote -->
<!-- wp:columns -->
<div class=\\"wp-block-columns\\"></div>
<!-- /wp:columns -->"
`;
33 changes: 28 additions & 5 deletions packages/e2e-tests/specs/editor/plugins/cpt-locking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ describe( 'cpt locking', () => {
await deactivatePlugin( 'gutenberg-test-plugin-cpt-locking' );
} );

const shouldRemoveTheInserter = async () => {
const shouldDisableTheInserter = async () => {
expect(
await page.$( '.edit-post-header [aria-label="Add block"]' )
).toBeNull();
await page.evaluate( () => {
const inserter = document.querySelector(
'.edit-post-header [aria-label="Add block"]'
);
return inserter.getAttribute( 'disabled' );
} )
).not.toBeNull();
};

const shouldNotAllowBlocksToBeRemoved = async () => {
Expand Down Expand Up @@ -60,7 +65,7 @@ describe( 'cpt locking', () => {
await createNewPost( { postType: 'locked-all-post' } );
} );

it( 'should remove the inserter', shouldRemoveTheInserter );
it( 'should disable the inserter', shouldDisableTheInserter );

it(
'should not allow blocks to be removed',
Expand Down Expand Up @@ -102,14 +107,32 @@ describe( 'cpt locking', () => {
'The content of your post doesn’t match the template assigned to your post type.'
);
} );

it( 'can use the global inserter in inner blocks', async () => {
await page.click( 'button[aria-label="Two columns; equal split"]' );
await page.click(
'.wp-block-column .block-editor-button-block-appender'
);
await page.type( '.block-editor-inserter__search-input', 'image' );
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Enter' );
await page.click( '.edit-post-header-toolbar__inserter-toggle' );
await page.type(
'.block-editor-inserter__search-input',
'gallery'
);
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Enter' );
expect( await page.$( '.wp-block-gallery' ) ).not.toBeNull();
} );
} );

describe( 'template_lock insert', () => {
beforeEach( async () => {
await createNewPost( { postType: 'locked-insert-post' } );
} );

it( 'should remove the inserter', shouldRemoveTheInserter );
it( 'should disable the inserter', shouldDisableTheInserter );

it(
'should not allow blocks to be removed',
Expand Down
48 changes: 26 additions & 22 deletions packages/edit-post/src/components/header/header-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,33 @@ function HeaderToolbar( { onToggleInserter, isInserterOpen } ) {
const {
hasFixedToolbar,
isInserterEnabled,
isInserterVisible,
isTextModeEnabled,
previewDeviceType,
} = useSelect(
( select ) => ( {
} = useSelect( ( select ) => {
const {
hasInserterItems,
getBlockRootClientId,
getBlockSelectionEnd,
} = select( 'core/block-editor' );
return {
hasFixedToolbar: select( 'core/edit-post' ).isFeatureActive(
'fixedToolbar'
),
// This setting (richEditingEnabled) should not live in the block editor's setting.
isInserterEnabled:
select( 'core/edit-post' ).getEditorMode() === 'visual' &&
select( 'core/editor' ).getEditorSettings().richEditingEnabled,
isInserterVisible: select( 'core/block-editor' ).hasInserterItems(),
select( 'core/editor' ).getEditorSettings()
.richEditingEnabled &&
hasInserterItems(
getBlockRootClientId( getBlockSelectionEnd() )
),
isTextModeEnabled:
select( 'core/edit-post' ).getEditorMode() === 'text',
previewDeviceType: select(
'core/edit-post'
).__experimentalGetPreviewDeviceType(),
} ),
[]
);
};
}, [] );
const isLargeViewport = useViewportMatch( 'medium' );

const displayBlockToolbar =
Expand All @@ -59,20 +65,18 @@ function HeaderToolbar( { onToggleInserter, isInserterOpen } ) {
className="edit-post-header-toolbar"
aria-label={ toolbarAriaLabel }
>
{ isInserterVisible && (
<Button
className="edit-post-header-toolbar__inserter-toggle"
isPrimary
isPressed={ isInserterOpen }
onClick={ onToggleInserter }
disabled={ ! isInserterEnabled }
icon={ plus }
label={ _x(
'Add block',
'Generic label for block inserter button'
) }
/>
) }
<Button
className="edit-post-header-toolbar__inserter-toggle"
isPrimary
isPressed={ isInserterOpen }
onClick={ onToggleInserter }
disabled={ ! isInserterEnabled }
icon={ plus }
label={ _x(
'Add block',
'Generic label for block inserter button'
) }
/>
<ToolSelector />
<EditorHistoryUndo />
<EditorHistoryRedo />
Expand Down

0 comments on commit e290640

Please sign in to comment.