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: selectionChange: bind on focus, unbind on blur #12480

Merged
merged 2 commits into from
Nov 30, 2018
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
26 changes: 8 additions & 18 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class RichText extends Component {

this.onSetup = this.onSetup.bind( this );
this.onFocus = this.onFocus.bind( this );
this.onBlur = this.onBlur.bind( this );
this.onChange = this.onChange.bind( this );
this.onDeleteKeyDown = this.onDeleteKeyDown.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
Expand All @@ -108,7 +109,6 @@ export class RichText extends Component {
this.isEmpty = this.isEmpty.bind( this );
this.valueToFormat = this.valueToFormat.bind( this );
this.setRef = this.setRef.bind( this );
this.isActive = this.isActive.bind( this );
this.valueToEditableHTML = this.valueToEditableHTML.bind( this );

this.formatToValue = memize( this.formatToValue.bind( this ), { size: 1 } );
Expand All @@ -129,22 +129,10 @@ export class RichText extends Component {
this.lastHistoryValue = value;
}

componentDidMount() {
document.addEventListener( 'selectionchange', this.onSelectionChange );
}

componentWillUnmount() {
document.removeEventListener( 'selectionchange', this.onSelectionChange );
}

setRef( node ) {
this.editableRef = node;
}

isActive() {
return this.editableRef === document.activeElement;
}

/**
* Handles the onSetup event for the TinyMCE component.
*
Expand Down Expand Up @@ -354,6 +342,12 @@ export class RichText extends Component {
if ( unstableOnFocus ) {
unstableOnFocus();
}

document.addEventListener( 'selectionchange', this.onSelectionChange );
Copy link
Member

Choose a reason for hiding this comment

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

What of these lines then?

// Ensure it's the active element. This is a global event.
if ( ! this.isActive() ) {
return;
}

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, we should be able to remove this.

}

onBlur() {
document.removeEventListener( 'selectionchange', this.onSelectionChange );
Copy link
Member

Choose a reason for hiding this comment

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

I guess blur, despite bubbling, doesn't fire when moving between elements in the same contenteditable ?

https://codepen.io/aduth/pen/VVqOmq?editors=1111

(In other words, I suspected there could be an issue, but it doesn't appear so)

Copy link
Member Author

@ellatrix ellatrix Nov 30, 2018

Choose a reason for hiding this comment

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

No, it shouldn't, it starts at the contenteditable element.

}

/**
Expand Down Expand Up @@ -392,11 +386,6 @@ export class RichText extends Component {
* Handles the `selectionchange` event: sync the selection to local state.
*/
onSelectionChange() {
// Ensure it's the active element. This is a global event.
if ( ! this.isActive() ) {
Copy link
Member

@aduth aduth Nov 30, 2018

Choose a reason for hiding this comment

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

I think I'd noted it elsewhere, but there's a small difference between isActive and onFocus in that the prior was testing the div wrapper as the active element, the latter exclusively applied to the TinyMCE element (i.e. not considering toolbars and such). When last I tested this, I recall it being a reasonable difference in that (a) all other elements are non-visual or slot-rendered and (b) are not relevant to the selection anyways.

Does that sound accurate to you?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, I think both are checking the contenteditable element? this.editableRef is the contenteditable element, and onFocus is a listener on that element. Maybe you're thinking about setFocusedElement?

Copy link
Member

Choose a reason for hiding this comment

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

Hm, I think both are checking the contenteditable element? this.editableRef is the contenteditable element, and onFocus is a listener on that element. Maybe you're thinking about setFocusedElement?

Ah! I am!

I still think we should consolidate these, as having multiple concepts of focus here is particularly confusing for me, but it's not relevant to the change proposed here.

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, tbh I'm not sure why there's something like setFocusedElement on the wrapper...

Copy link
Member

Choose a reason for hiding this comment

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

My hope was that at some point, we could get rid of the wrapper, at least in the sense of having it be a <Fragment> instead of an actual DOM div node.

return;
}

const { start, end, formats } = this.createRecord();

if ( start !== this.state.start || end !== this.state.end ) {
Expand Down Expand Up @@ -891,6 +880,7 @@ export class RichText extends Component {
onCompositionEnd={ this.onCompositionEnd }
onKeyDown={ this.onKeyDown }
onFocus={ this.onFocus }
onBlur={ this.onBlur }
multilineTag={ this.multilineTag }
multilineWrapperTags={ this.multilineWrapperTags }
setRef={ this.setRef }
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/components/rich-text/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ export default class TinyMCE extends Component {
onInput,
onKeyDown,
onCompositionEnd,
onBlur,
} = this.props;

/*
Expand Down Expand Up @@ -377,6 +378,7 @@ export default class TinyMCE extends Component {
onPaste,
onInput,
onFocus: this.onFocus,
onBlur,
onKeyDown,
onCompositionEnd,
} );
Expand Down