Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change replaceInnerBlocks updateSelection property to false #26312

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ _Parameters_

- _rootClientId_ `string`: Client ID of the block whose InnerBlocks will re replaced.
- _blocks_ `Array<Object>`: Block objects to insert as new InnerBlocks
- _updateSelection_ `?boolean`: If true block selection will be updated. If false, block selection will not change. Defaults to true.
- _updateSelection_ `?boolean`: If true block selection will be updated. If false, block selection will not change. Defaults to false.

_Returns_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ function BlockVariationPicker( { isVisible, onClose, clientId, variations } ) {
const onVariationSelect = ( variation ) => {
replaceInnerBlocks(
clientId,
createBlocksFromInnerBlocksTemplate( variation.innerBlocks ),
false
createBlocksFromInnerBlocksTemplate( variation.innerBlocks )
);
onClose();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ The previous example creates an InnerBlocks area containing two columns one with

### `templateInsertUpdatesSelection`
* **Type:** `Boolean`
* **Default:** `true`
* **Default:** `false`

If true when child blocks in the template are inserted the selection is updated.
If false the selection should not be updated when child blocks specified in the template are inserted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ describe( 'useBlockSync hook', () => {
expect( onInput ).not.toHaveBeenCalled();
expect( replaceInnerBlocks ).toHaveBeenCalledWith(
'test', // It should use the given client ID.
fakeBlocks, // It should use the controlled blocks value.
false // It shoudl not update the selection state.
fakeBlocks // It should use the controlled blocks value.
);

const testBlocks = [
Expand All @@ -119,11 +118,7 @@ describe( 'useBlockSync hook', () => {
expect( onChange ).not.toHaveBeenCalled();
expect( onInput ).not.toHaveBeenCalled();
expect( resetBlocks ).not.toHaveBeenCalled();
expect( replaceInnerBlocks ).toHaveBeenCalledWith(
'test',
testBlocks,
false
);
expect( replaceInnerBlocks ).toHaveBeenCalledWith( 'test', testBlocks );
} );

it( 'does not add the controlled blocks to the block-editor store if the store already contains them', async () => {
Expand Down Expand Up @@ -315,7 +310,7 @@ describe( 'useBlockSync hook', () => {
);
} );

expect( replaceInnerBlocks ).toHaveBeenCalledWith( 'test', [], false );
expect( replaceInnerBlocks ).toHaveBeenCalledWith( 'test', [] );
expect( onChange ).not.toHaveBeenCalled();
expect( onInput ).not.toHaveBeenCalled();
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default function useBlockSync( {
if ( clientId ) {
setHasControlledInnerBlocks( clientId, true );
__unstableMarkNextChangeAsNotPersistent();
replaceInnerBlocks( clientId, controlledBlocks, false );
replaceInnerBlocks( clientId, controlledBlocks );
} else {
resetBlocks( controlledBlocks );
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,14 @@ export function removeBlock( clientId, selectPrevious ) {
*
* @param {string} rootClientId Client ID of the block whose InnerBlocks will re replaced.
* @param {Object[]} blocks Block objects to insert as new InnerBlocks
* @param {?boolean} updateSelection If true block selection will be updated. If false, block selection will not change. Defaults to true.
* @param {?boolean} updateSelection If true block selection will be updated. If false, block selection will not change. Defaults to false.
*
* @return {Object} Action object.
*/
export function replaceInnerBlocks(
rootClientId,
blocks,
updateSelection = true
updateSelection = false
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
) {
return {
type: 'REPLACE_INNER_BLOCKS',
Expand Down
8 changes: 4 additions & 4 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,17 +1100,17 @@ describe( 'actions', () => {
blocks: [ block ],
rootClientId: 'root',
time: expect.any( Number ),
updateSelection: true,
updateSelection: false,
} );
} );

it( 'should return the REPLACE_INNER_BLOCKS action with updateSelection false', () => {
expect( replaceInnerBlocks( 'root', [ block ], false ) ).toEqual( {
it( 'should return the REPLACE_INNER_BLOCKS action with updateSelection true', () => {
expect( replaceInnerBlocks( 'root', [ block ], true ) ).toEqual( {
type: 'REPLACE_INNER_BLOCKS',
blocks: [ block ],
rootClientId: 'root',
time: expect.any( Number ),
updateSelection: false,
updateSelection: true,
} );
} );
} );
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/buttons/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function ButtonsEdit( {
type: 'default',
alignments: [],
},
templateInsertUpdatesSelection: true,
} );
return (
<>
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/buttons/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default function ButtonsEdit( {
marginHorizontal={ spacing }
marginVertical={ spacing }
__experimentalLayout={ { type: 'default', alignments: [] } }
templateInsertUpdatesSelection
/>
</>
);
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ const ColumnsEditContainerWrapper = withDispatch(
}
}

replaceInnerBlocks( clientId, innerBlocks, false );
replaceInnerBlocks( clientId, innerBlocks );
},
} )
)( ColumnsEditContainer );
Expand Down Expand Up @@ -235,7 +235,8 @@ function Placeholder( { clientId, name, setAttributes } ) {
clientId,
createBlocksFromInnerBlocksTemplate(
nextVariation.innerBlocks
)
),
true
);
}
} }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/columns/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ const ColumnsEditContainerWrapper = withDispatch(
);
}

replaceInnerBlocks( clientId, innerBlocks, false );
replaceInnerBlocks( clientId, innerBlocks );
},
onAddNextColumn: () => {
const { clientId } = ownProps;
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ function CoverEdit( {
{
className: 'wp-block-cover__inner-container',
},
{ template: INNER_BLOCKS_TEMPLATE }
{
template: INNER_BLOCKS_TEMPLATE,
templateInsertUpdatesSelection: true,
}
);

if ( ! hasBackground ) {
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/cover/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,10 @@ const Cover = ( {
pointerEvents="box-none"
style={ [ styles.content, { minHeight: CONTAINER_HEIGHT } ] }
>
<InnerBlocks template={ INNER_BLOCKS_TEMPLATE } />
<InnerBlocks
template={ INNER_BLOCKS_TEMPLATE }
templateInsertUpdatesSelection
/>
</View>

<View pointerEvents="none" style={ styles.overlayContainer }>
Expand Down
9 changes: 2 additions & 7 deletions packages/block-library/src/media-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,8 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) {
} );

const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-media-text__content',
},
{
template: TEMPLATE,
templateInsertUpdatesSelection: false,
}
{ className: 'wp-block-media-text__content' },
{ template: TEMPLATE }
);

return (
Expand Down
5 changes: 1 addition & 4 deletions packages/block-library/src/media-text/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,7 @@ class MediaTextEdit extends Component {
...innerBlockContainerStyle,
} }
>
<InnerBlocks
template={ TEMPLATE }
templateInsertUpdatesSelection={ false }
/>
<InnerBlocks template={ TEMPLATE } />
</View>
</View>
</>
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ function Navigation( {
isSelected
? InnerBlocks.DefaultAppender
: false,
templateInsertUpdatesSelection: false,
__experimentalAppenderTagName: 'li',
__experimentalCaptureToolbars: true,
// Template lock set to false here so that the Nav
Expand Down Expand Up @@ -214,7 +213,8 @@ export default compose( [
}
dispatch( 'core/block-editor' ).replaceInnerBlocks(
clientId,
blocks
blocks,
true
);
},
};
Expand Down
5 changes: 1 addition & 4 deletions packages/block-library/src/query-loop/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ export default function QueryLoopEdit( {
>
{ blockContext ===
( activeBlockContext || blockContexts[ 0 ] ) ? (
<InnerBlocks
template={ TEMPLATE }
templateInsertUpdatesSelection={ false }
/>
<InnerBlocks template={ TEMPLATE } />
) : (
<BlockPreview
blocks={ blocks }
Expand Down
5 changes: 1 addition & 4 deletions packages/block-library/src/query/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ export function QueryContent( {
</BlockControls>
<div { ...blockProps }>
<QueryProvider>
<InnerBlocks
template={ TEMPLATE }
templateInsertUpdatesSelection={ false }
/>
<InnerBlocks template={ TEMPLATE } />
</QueryProvider>
</div>
</>
Expand Down
1 change: 1 addition & 0 deletions packages/e2e-tests/plugins/block-context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
el( InnerBlocks, {
template: [ [ 'gutenberg/test-context-consumer', {} ] ],
templateLock: 'all',
templateInsertUpdatesSelection: true,
} )
);
},
Expand Down
17 changes: 9 additions & 8 deletions packages/e2e-tests/plugins/inner-blocks-templates/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
( function() {
( function () {
var registerBlockType = wp.blocks.registerBlockType;
var createBlock = wp.blocks.createBlock;
var el = wp.element.createElement;
Expand All @@ -24,7 +24,7 @@
],
];

var save = function() {
var save = function () {
return el( InnerBlocks.Content );
};

Expand All @@ -33,7 +33,7 @@
icon: 'cart',
category: 'text',

edit: function( props ) {
edit: function ( props ) {
return el( InnerBlocks, {
template: TEMPLATE,
} );
Expand All @@ -47,7 +47,7 @@
icon: 'cart',
category: 'text',

edit: function( props ) {
edit: function ( props ) {
return el( InnerBlocks, {
template: TEMPLATE,
templateLock: 'all',
Expand All @@ -62,9 +62,10 @@
icon: 'cart',
category: 'text',

edit: function( props ) {
edit: function ( props ) {
return el( InnerBlocks, {
template: TEMPLATE_PARAGRAPH_PLACEHOLDER,
templateInsertUpdatesSelection: true,
} );
},

Expand All @@ -86,7 +87,7 @@
'test/test-inner-blocks-locking-all',
'test/test-inner-blocks-paragraph-placeholder',
],
transform: function( attributes, innerBlocks ) {
transform: function ( attributes, innerBlocks ) {
return createBlock(
'test/test-inner-blocks-transformer-target',
attributes,
Expand All @@ -99,7 +100,7 @@
{
type: 'block',
blocks: [ 'test/i-dont-exist' ],
transform: function( attributes, innerBlocks ) {
transform: function ( attributes, innerBlocks ) {
return createBlock(
'test/test-inner-blocks-transformer-target',
attributes,
Expand All @@ -110,7 +111,7 @@
],
},

edit: function( props ) {
edit: function ( props ) {
return el( InnerBlocks, {
template: TEMPLATE,
} );
Expand Down