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

RichText: remove selection change listener during composition #14449

Merged
merged 1 commit into from
Mar 15, 2019
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
32 changes: 7 additions & 25 deletions packages/block-editor/src/components/rich-text/editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { BACKSPACE, DELETE } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { diffAriaProps, pickAriaProps } from './aria';
import { diffAriaProps } from './aria';

/**
* Browser dependencies
Expand Down Expand Up @@ -137,10 +137,7 @@ export default class Editable extends Component {

bindEditorNode( editorNode ) {
this.editorNode = editorNode;

if ( this.props.setRef ) {
this.props.setRef( editorNode );
}
this.props.setRef( editorNode );

if ( IS_IE ) {
if ( editorNode ) {
Expand All @@ -154,44 +151,29 @@ export default class Editable extends Component {
}

render() {
const ariaProps = pickAriaProps( this.props );
const {
tagName = 'div',
style,
record,
valueToEditableHTML,
className,
isPlaceholderVisible,
onPaste,
onInput,
onKeyDown,
onCompositionEnd,
onFocus,
onBlur,
onMouseDown,
onTouchStart,
...remainingProps
} = this.props;

ariaProps.role = 'textbox';
ariaProps[ 'aria-multiline' ] = true;
delete remainingProps.setRef;

return createElement( tagName, {
...ariaProps,
role: 'textbox',
'aria-multiline': true,
className: classnames( className, CLASS_NAME ),
contentEditable: true,
[ IS_PLACEHOLDER_VISIBLE_ATTR_NAME ]: isPlaceholderVisible,
ref: this.bindEditorNode,
style,
suppressContentEditableWarning: true,
dangerouslySetInnerHTML: { __html: valueToEditableHTML( record ) },
onPaste,
onInput,
onFocus,
onBlur,
onKeyDown,
onCompositionEnd,
onMouseDown,
onTouchStart,
...remainingProps,
ellatrix marked this conversation as resolved.
Show resolved Hide resolved
} );
}
}
6 changes: 4 additions & 2 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ export class RichText extends Component {
// Browsers setting `isComposing` to `true` will usually emit a final
// `input` event when the characters are composed.
if ( event && event.nativeEvent.isComposing ) {
// Also don't update any selection.
document.removeEventListener( 'selectionchange', this.onSelectionChange );
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think there's a simpler way togo instead of this selection enabling/disabling? maybe a flag?

Copy link
Member Author

Choose a reason for hiding this comment

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

I see this as the simplest way. There's no need to run a listener if it's not needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I mean, I think it's "cleanliest" if you remove listeners if you don't need them, add them back if you do need them again. I made the mistake before not to do that, and it had a negative performance impact (#12480), so I make a habit of taking this "cleaner" approach.

return;
}

Expand Down Expand Up @@ -444,6 +446,8 @@ export class RichText extends Component {
// Ensure the value is up-to-date for browsers that don't emit a final
// input event after composition.
this.onInput();
// Tracking selection changes can be resumed.
document.addEventListener( 'selectionchange', this.onSelectionChange );
}

/**
Expand Down Expand Up @@ -1114,8 +1118,6 @@ export class RichText extends Component {
onBlur={ this.onBlur }
onMouseDown={ this.onPointerDown }
onTouchStart={ this.onPointerDown }
multilineTag={ this.multilineTag }
multilineWrapperTags={ this.multilineWrapperTags }
setRef={ this.setRef }
/>
{ isPlaceholderVisible &&
Expand Down