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

Add prop to disable block selection clearer in BlockList #44517

Merged
merged 4 commits into from
Oct 18, 2022
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
1 change: 1 addition & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ _Properties_
- _codeEditingEnabled_ `boolean`: Whether or not the user can switch to the code editor
- _generateAnchors_ `boolean`: Enable/Disable auto anchor generation for Heading blocks
- _\_\_experimentalCanUserUseUnfilteredHTML_ `boolean`: Whether the user should be able to use unfiltered HTML or the HTML should be filtered e.g., to remove elements considered insecure like iframes.
- _\_\_experimentalClearBlockSelection_ `boolean`: Whether the block editor should clear selection on mousedown when a block is not clicked.
- _\_\_experimentalBlockDirectory_ `boolean`: Whether the user has enabled the Block Directory
- _\_\_experimentalBlockPatterns_ `Array`: Array of objects representing the block patterns
- _\_\_experimentalBlockPatternCategories_ `Array`: Array of objects representing the block pattern categories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ import { store as blockEditorStore } from '../../store';
* @return {import('react').RefCallback} Ref callback.
*/
export function useBlockSelectionClearer() {
const { hasSelectedBlock, hasMultiSelection } =
const { getSettings, hasSelectedBlock, hasMultiSelection } =
useSelect( blockEditorStore );
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const { __experimentalClearBlockSelection: isEnabled } = getSettings();

return useRefEffect(
( node ) => {
if ( ! isEnabled ) {
return;
}

function onMouseDown( event ) {
if ( ! hasSelectedBlock() && ! hasMultiSelection() ) {
return;
Expand All @@ -42,7 +47,7 @@ export function useBlockSelectionClearer() {
node.removeEventListener( 'mousedown', onMouseDown );
};
},
[ hasSelectedBlock, hasMultiSelection, clearSelectedBlock ]
[ hasSelectedBlock, hasMultiSelection, clearSelectedBlock, isEnabled ]
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* External dependencies
*/
import { fireEvent, render } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import BlockSelectionClearer from '../';

const defaultUseSelectValues = {
hasSelectedBlock: jest.fn().mockReturnValue( false ),
hasMultiSelection: jest.fn().mockReturnValue( false ),
getSettings: jest.fn().mockReturnValue( {
__experimentalClearBlockSelection: true,
} ),
};

jest.mock( '@wordpress/data/src/components/use-dispatch', () => ( {
useDispatch: jest.fn(),
} ) );

jest.mock( '@wordpress/data/src/components/use-select', () => {
// This allows us to tweak the returned value on each test.
const mock = jest.fn();
return mock;
} );

describe( 'BlockSelectionClearer component', () => {
it( 'should clear the selected block when a selected block exists', () => {
const mockClearSelectedBlock = jest.fn();
useSelect.mockImplementation( () => ( {
...defaultUseSelectValues,
hasSelectedBlock: jest.fn().mockReturnValue( true ),
} ) );
useDispatch.mockImplementation( () => ( {
clearSelectedBlock: mockClearSelectedBlock,
} ) );

const { queryByRole } = render(
<BlockSelectionClearer>
<button>Not a block</button>
</BlockSelectionClearer>
);
const button = queryByRole( 'button' );
fireEvent.mouseDown( button.parentElement );

expect( mockClearSelectedBlock ).toBeCalled();
} );

it( 'should clear the selected block when multiple blocks are selected', () => {
const mockClearSelectedBlock = jest.fn();
useSelect.mockImplementation( () => ( {
...defaultUseSelectValues,
hasMultiSelection: jest.fn().mockReturnValue( true ),
} ) );
useDispatch.mockImplementation( () => ( {
clearSelectedBlock: mockClearSelectedBlock,
} ) );

const { queryByRole } = render(
<BlockSelectionClearer>
<button>Not a block</button>
</BlockSelectionClearer>
);
const button = queryByRole( 'button' );
fireEvent.mouseDown( button.parentElement );

expect( mockClearSelectedBlock ).toBeCalled();
} );

it( 'should not clear the block selection when no blocks are selected', () => {
const mockClearSelectedBlock = jest.fn();
useSelect.mockImplementation( () => defaultUseSelectValues );
useDispatch.mockImplementation( () => ( {
clearSelectedBlock: mockClearSelectedBlock,
} ) );

const { queryByRole } = render(
<BlockSelectionClearer>
<button>Not a block</button>
</BlockSelectionClearer>
);
const button = queryByRole( 'button' );
fireEvent.mouseDown( button.parentElement );

expect( mockClearSelectedBlock ).not.toBeCalled();
} );

it( 'should not clear the block selection when the feature is disabled', () => {
const mockClearSelectedBlock = jest.fn();
useSelect.mockImplementation( () => ( {
...defaultUseSelectValues,
hasSelectedBlock: jest.fn().mockReturnValue( true ),
getSettings: jest.fn().mockReturnValue( {
__experimentalClearBlockSelection: false,
} ),
} ) );
useDispatch.mockImplementation( () => ( {
clearSelectedBlock: mockClearSelectedBlock,
} ) );

const { queryByRole } = render(
<BlockSelectionClearer>
<button>Not a block</button>
</BlockSelectionClearer>
);
const button = queryByRole( 'button' );
fireEvent.mouseDown( button.parentElement );

expect( mockClearSelectedBlock ).not.toBeCalled();
} );
} );
2 changes: 2 additions & 0 deletions packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const PREFERENCES_DEFAULTS = {
* @property {boolean} codeEditingEnabled Whether or not the user can switch to the code editor
* @property {boolean} generateAnchors Enable/Disable auto anchor generation for Heading blocks
* @property {boolean} __experimentalCanUserUseUnfilteredHTML Whether the user should be able to use unfiltered HTML or the HTML should be filtered e.g., to remove elements considered insecure like iframes.
* @property {boolean} __experimentalClearBlockSelection Whether the block editor should clear selection on mousedown when a block is not clicked.
* @property {boolean} __experimentalBlockDirectory Whether the user has enabled the Block Directory
* @property {Array} __experimentalBlockPatterns Array of objects representing the block patterns
* @property {Array} __experimentalBlockPatternCategories Array of objects representing the block pattern categories
Expand Down Expand Up @@ -156,6 +157,7 @@ export const SETTINGS_DEFAULTS = {
canLockBlocks: true,

__experimentalCanUserUseUnfilteredHTML: false,
__experimentalClearBlockSelection: true,
__experimentalBlockDirectory: false,
__mobileEnablePageTemplates: false,
__experimentalBlockPatterns: [],
Expand Down