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

Writing flow: fix horizontal caret placing for empty editable with placeholder #30463

Merged
merged 3 commits into from
Apr 2, 2021
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-library/src/query-pagination-next/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function QueryPaginationNextEdit( {
<PlainText
__experimentalVersion={ 2 }
tagName="a"
style={ { display: 'inline-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.

Not really related, but saw this when running full site editor e2e tests. Looks like the warnings are ignored so let's log an error instead.

https://github.com/WordPress/gutenberg/pull/30463/files#diff-cea529f9fa9034144e2e4f14ec3e87baa167522d3f3cda77b5ef904e47b817f0R18

aria-label={ __( 'Next page link' ) }
placeholder={ __( 'Next Page' ) }
value={ label }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function QueryPaginationPreviousEdit( {
<PlainText
__experimentalVersion={ 2 }
tagName="a"
style={ { display: 'inline-block' } }
aria-label={ __( 'Previous page link' ) }
placeholder={ __( 'Previous Page' ) }
value={ label }
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/site-title/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function SiteTitleEdit( { attributes, setAttributes } ) {
<TagName { ...blockProps }>
<RichText
tagName="a"
style={ { display: 'inline-block' } }
aria-label={ __( 'Site title text' ) }
placeholder={ __( 'Write site title…' ) }
value={ title }
Expand Down
1 change: 1 addition & 0 deletions packages/dom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ _Parameters_

- _container_ `Element`: Focusable element.
- _isReverse_ `boolean`: True for end, false for start.
- _mayUseScroll_ `boolean`: Whether to allow scrolling.

<a name="placeCaretAtVerticalEdge" href="#placeCaretAtVerticalEdge">#</a> **placeCaretAtVerticalEdge**

Expand Down
51 changes: 35 additions & 16 deletions packages/dom/src/dom/place-caret-at-horizontal-edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
*/
import { includes } from 'lodash';

/**
* Internal dependencies
*/
import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';

/**
* Places the caret at start or end of a given element.
*
* @param {Element} container Focusable element.
* @param {boolean} isReverse True for end, false for start.
* @param {Element} container Focusable element.
* @param {boolean} isReverse True for end, false for start.
* @param {boolean} mayUseScroll Whether to allow scrolling.
*/
export default function placeCaretAtHorizontalEdge( container, isReverse ) {
export default function placeCaretAtHorizontalEdge(
container,
isReverse,
mayUseScroll
) {
if ( ! container ) {
return;
}
Expand Down Expand Up @@ -37,25 +47,34 @@ export default function placeCaretAtHorizontalEdge( container, isReverse ) {
return;
}

// Select on extent child of the container, not the container itself. This
// avoids the selection always being `endOffset` of 1 when placed at end,
// where `startContainer`, `endContainer` would always be container itself.
const rangeTarget = container[ isReverse ? 'lastChild' : 'firstChild' ];
const { ownerDocument } = container;
const containerRect = container.getBoundingClientRect();
// When placing at the end (isReverse), find the closest range to the bottom
// right corner. When placing at the start, to the top left corner.
const x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
const y = isReverse ? containerRect.bottom - 1 : containerRect.top + 1;
const range = hiddenCaretRangeFromPoint( ownerDocument, x, y, container );

// If no range range can be created or it is outside the container, the
// element may be out of view.
if (
! range ||
! range.startContainer ||
! container.contains( range.startContainer )
) {
if ( ! mayUseScroll ) {
return;
}

// If no range target, it implies that the container is empty. Focusing is
// sufficient for caret to be placed correctly.
if ( ! rangeTarget ) {
// Only try to scroll into view once to avoid an infinite loop.
mayUseScroll = false;
container.scrollIntoView( isReverse );
placeCaretAtHorizontalEdge( container, isReverse, mayUseScroll );
return;
}

const { ownerDocument } = container;
const { defaultView } = ownerDocument;
const selection = defaultView.getSelection();
const range = ownerDocument.createRange();

range.selectNodeContents( rangeTarget );
range.collapse( ! isReverse );

selection.removeAllRanges();
selection.addRange( range );
}
2 changes: 1 addition & 1 deletion packages/rich-text/src/component/use-inline-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function useInlineWarning( { ref } ) {

if ( computedStyle.display === 'inline' ) {
// eslint-disable-next-line no-console
console.warn( message );
console.error( message );
}
}
}, [] );
Expand Down
2 changes: 2 additions & 0 deletions packages/rich-text/src/to-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ export function toTree( {
// selection. The placeholder is also not editable after
// all.
contenteditable: 'false',
style:
'pointer-events:none;user-select:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;',
Copy link
Contributor

Choose a reason for hiding this comment

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

Never ran into user-select before, TIL.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, everything selection related is a bit obscure

},
} );
}
Expand Down