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

Block editor: rewrite selection clearer #27468

Merged
merged 1 commit into from
Dec 6, 2020
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 @@ -19,20 +19,25 @@ export function useBlockSelectionClearer( ref ) {
return;
}

function onFocus() {
function onMouseDown( event ) {
// Only handle clicks on the canvas, not the content.
if ( event.target.closest( '.wp-block' ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition could be removed eventually when the content is directly rendered into the wrapper the mousedown event is listened for. Currently there are a few wrappers in between the selection clearer and the block for which we also need to detect clicks.

Copy link
Contributor

@youknowriad youknowriad Dec 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this PR break the selection clearer in the widgets screen (you can't click any button on the toolbar). .wp-block must not be enough.

Ii also wonder why you're using mousedown, focusing outside (without clicking) should also have the same behavior right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to restore the previous behavior for now to perform a patch release.

return;
}

clearSelectedBlock();
}

ref.current.addEventListener( 'focus', onFocus );
ref.current.addEventListener( 'mousedown', onMouseDown );

return () => {
ref.current.removeEventListener( 'focus', onFocus );
ref.current.removeEventListener( 'mousedown', onMouseDown );
};
}, [ hasSelection, clearSelectedBlock ] );
}

export default function BlockSelectionClearer( props ) {
const ref = useRef();
useBlockSelectionClearer( ref );
return <div tabIndex={ -1 } ref={ ref } { ...props } />;
return <div ref={ ref } { ...props } />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ describe( 'Order of block keyboard navigation', () => {
await page.keyboard.type( paragraphBlock );
}

// Clear the selected block and put focus in front of the block list.
await page.evaluate( () => {
document.querySelector( '.editor-styles-wrapper' ).focus();
} );
// Clear the selected block.
const paragraph = await page.$( '[data-type="core/paragraph"]' );
const box = await paragraph.boundingBox();
await page.mouse.click( box.x - 1, box.y );

await page.keyboard.press( 'Tab' );
await expect(
Expand Down Expand Up @@ -143,9 +143,13 @@ describe( 'Order of block keyboard navigation', () => {
await page.keyboard.type( paragraphBlock );
}

// Clear the selected block and put focus behind the block list.
// Clear the selected block.
const paragraph = await page.$( '[data-type="core/paragraph"]' );
const box = await paragraph.boundingBox();
await page.mouse.click( box.x - 1, box.y );

// Put focus behind the block list.
await page.evaluate( () => {
document.querySelector( '.editor-styles-wrapper' ).focus();
document
.querySelector( '.interface-interface-skeleton__sidebar' )
.focus();
Expand Down
1 change: 0 additions & 1 deletion packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export default function VisualEditor() {
<div
ref={ ref }
className="editor-styles-wrapper"
tabIndex="-1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know why this was here and why this was removed?

style={ resizedCanvasStyles || desktopCanvasStyles }
>
<WritingFlow>
Expand Down
13 changes: 10 additions & 3 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { useCallback, useRef } from '@wordpress/element';
import { useEntityBlockEditor } from '@wordpress/core-data';
import {
BlockEditorProvider,
Expand All @@ -12,6 +12,7 @@ import {
WritingFlow,
ObserveTyping,
BlockList,
__unstableUseBlockSelectionClearer as useBlockSelectionClearer,
} from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -39,8 +40,11 @@ export default function BlockEditor( { setIsInserterOpen } ) {
'postType',
templateType
);

const { setPage } = useDispatch( 'core/edit-site' );
const ref = useRef();

useBlockSelectionClearer( ref );

return (
<BlockEditorProvider
settings={ settings }
Expand All @@ -66,7 +70,10 @@ export default function BlockEditor( { setIsInserterOpen } ) {
<SidebarInspectorFill>
<BlockInspector />
</SidebarInspectorFill>
<div className="editor-styles-wrapper edit-site-block-editor__editor-styles-wrapper">
<div
ref={ ref }
className="editor-styles-wrapper edit-site-block-editor__editor-styles-wrapper"
>
<WritingFlow>
<ObserveTyping>
<BlockList className="edit-site-block-editor__block-list" />
Expand Down
5 changes: 2 additions & 3 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import { EntityProvider } from '@wordpress/core-data';
import {
BlockContextProvider,
BlockSelectionClearer,
BlockBreadcrumb,
__unstableUseEditorStyles as useEditorStyles,
__experimentalUseResizeCanvas as useResizeCanvas,
Expand Down Expand Up @@ -288,7 +287,7 @@ function Editor() {
/>
}
content={
<BlockSelectionClearer
<div
className="edit-site-visual-editor"
style={
inlineStyles
Expand All @@ -304,7 +303,7 @@ function Editor() {
/>
) }
<KeyboardShortcuts />
</BlockSelectionClearer>
</div>
}
actions={
<>
Expand Down