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

Refactor click redirect (and avoid div) #27253

Merged
merged 5 commits into from
Nov 25, 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
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export {
} from './typewriter';
export { default as Warning } from './warning';
export { default as WritingFlow } from './writing-flow';
export { useCanvasClickRedirect as __unstableUseCanvasClickRedirect } from './use-canvas-click-redirect';

/*
* State Related Components
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* External dependencies
*/
import { overEvery, findLast } from 'lodash';

/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import { focus, isTextField, placeCaretAtHorizontalEdge } from '@wordpress/dom';

/**
* Given an element, returns true if the element is a tabbable text field, or
* false otherwise.
*
* @param {Element} element Element to test.
*
* @return {boolean} Whether element is a tabbable text field.
*/
const isTabbableTextField = overEvery( [
isTextField,
focus.tabbable.isTabbableIndex,
] );

export function useCanvasClickRedirect( ref ) {
useEffect( () => {
function onMouseDown( event ) {
// Only handle clicks on the canvas, not the content.
if ( event.target !== ref.current ) {
return;
}

const focusableNodes = focus.focusable.find( ref.current );
const target = findLast( focusableNodes, isTabbableTextField );

if ( ! target ) {
return;
}

placeCaretAtHorizontalEdge( target, true );
event.preventDefault();
}

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

return () => {
ref.current.addEventListener( 'mousedown', onMouseDown );
};
}, [] );
}
29 changes: 1 addition & 28 deletions packages/block-editor/src/components/writing-flow/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { overEvery, find, findLast, reverse, first, last } from 'lodash';
import { find, reverse, first, last } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -49,19 +49,6 @@ function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
}

/**
* Given an element, returns true if the element is a tabbable text field, or
* false otherwise.
*
* @param {Element} element Element to test.
*
* @return {boolean} Whether element is a tabbable text field.
*/
const isTabbableTextField = overEvery( [
isTextField,
focus.tabbable.isTabbableIndex,
] );

/**
* Returns true if the element should consider edge navigation upon a keyboard
* event of the given directional key code, or false otherwise.
Expand Down Expand Up @@ -684,14 +671,6 @@ export default function WritingFlow( { children } ) {
}
}

function focusLastTextField() {
const focusableNodes = focus.focusable.find( container.current );
const target = findLast( focusableNodes, isTabbableTextField );
if ( target ) {
placeCaretAtHorizontalEdge( target, true );
}
}

useEffect( () => {
if ( hasMultiSelection && ! isMultiSelecting ) {
multiSelectionContainer.current.focus();
Expand Down Expand Up @@ -746,12 +725,6 @@ export default function WritingFlow( { children } ) {
multiSelectionContainer={ multiSelectionContainer }
isReverse
/>
<div
aria-hidden
tabIndex={ -1 }
onClick={ focusLastTextField }
className="block-editor-writing-flow__click-redirect"
/>
</>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
Expand Down
8 changes: 0 additions & 8 deletions packages/block-editor/src/components/writing-flow/style.scss

This file was deleted.

1 change: 0 additions & 1 deletion packages/block-editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
@import "./components/url-input/style.scss";
@import "./components/url-popover/style.scss";
@import "./components/warning/style.scss";
@import "./components/writing-flow/style.scss";
@import "./hooks/anchor.scss";

// This tag marks the end of the styles that apply to editing canvas contents and need to be manipulated when we resize the editor.
Expand Down
4 changes: 0 additions & 4 deletions packages/block-library/src/block/editor.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
.edit-post-visual-editor .block-library-block__reusable-block-container {
.block-editor-writing-flow__click-redirect {
min-height: auto;
}

// Unset the padding that root containers get when they're actually root containers.
.is-root-container {
padding-left: 0;
Expand Down
16 changes: 14 additions & 2 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
__unstableUseScrollMultiSelectionIntoView as useScrollMultiSelectionIntoView,
__experimentalBlockSettingsMenuFirstItem,
__experimentalUseResizeCanvas as useResizeCanvas,
__unstableUseCanvasClickRedirect as useCanvasClickRedirect,
} from '@wordpress/block-editor';
import { Popover } from '@wordpress/components';
import { useRef } from '@wordpress/element';
Expand All @@ -30,13 +31,24 @@ export default function VisualEditor() {
const deviceType = useSelect( ( select ) => {
return select( 'core/edit-post' ).__experimentalGetPreviewDeviceType();
}, [] );
const inlineStyles = useResizeCanvas( deviceType );
const hasMetaBoxes = useSelect(
( select ) => select( 'core/edit-post' ).hasMetaBoxes(),
[]
);
const desktopCanvasStyles = {
height: '100%',
// Add a constant padding for the typewritter effect. When typing at the
// bottom, there needs to be room to scroll up.
paddingBottom: hasMetaBoxes ? null : '40vh',
};
const resizedCanvasStyles = useResizeCanvas( deviceType );

useScrollMultiSelectionIntoView( ref );
useBlockSelectionClearer( ref );
useTypewriter( ref );
useClipboardHandler( ref );
useTypingObserver( ref );
useCanvasClickRedirect( ref );

return (
<div className="edit-post-visual-editor">
Expand All @@ -46,7 +58,7 @@ export default function VisualEditor() {
ref={ ref }
className="editor-styles-wrapper"
tabIndex="-1"
style={ inlineStyles }
style={ resizedCanvasStyles || desktopCanvasStyles }
>
<WritingFlow>
<div className="edit-post-visual-editor__post-title-wrapper">
Expand Down
18 changes: 5 additions & 13 deletions packages/edit-post/src/components/visual-editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,12 @@
}
}

.editor-styles-wrapper,
.editor-styles-wrapper > .block-editor-writing-flow__click-redirect {
height: 100%;
}

.edit-post-visual-editor .block-editor-writing-flow__click-redirect {
// Allow the page to be scrolled with the last block in the middle.
min-height: 40vh;
width: 100%;
}
.editor-styles-wrapper {
cursor: text;

// Hide the extra space when there are metaboxes.
.has-metaboxes .edit-post-visual-editor .block-editor-writing-flow__click-redirect {
height: 0;
> * {
cursor: auto;
}
}

// Ideally this wrapper div is not needed but if we want to match the positioning of blocks
Expand Down