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

Editor: Add components for editor mentions #10142

Merged
merged 9 commits into from
Feb 28, 2017

Conversation

donnapep
Copy link
Contributor

To facilitate merging, this PR separates out the React components from PR #8028, which adds support for mentions to the editor. Still to come is a PR that leverages these components.

Dependent on PR #9969.

cc @mtias

@donnapep donnapep added [Feature] Post/Page Editor The editor for editing posts and pages. [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Dec 17, 2016
@donnapep donnapep force-pushed the add/editor-mentions-components branch from 896d623 to b6a176e Compare December 29, 2016 17:39
@gwwar gwwar self-requested a review January 11, 2017 22:59
/**
* Module variables
*/
const VK = tinymce.util.VK;
Copy link
Contributor

Choose a reason for hiding this comment

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

May be worth us adding a comment to explain what TinyMCE's rather cryptically-named VK is for.

Copy link
Contributor

Choose a reason for hiding this comment

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

return suggestions
.filter( ( { user_login, display_name } ) => {
// Start of string or preceded by a space.
const matcher = new RegExp( `^${ query }|\\s${ query }`, 'ig' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Do usernames ever use reserved regex characters? Any reason for testing against a single whitespace vs many?

An alternative might just be to use lodash trim and startsWith

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Re: whitespace. It's matching against ${ user_login } ${ display_name }, so whether the regex uses \s or \s+, the result would be the same.

I also tried creating a user with a user name starting with ^ and it wasn't allowed.


constructor( props ) {
super( props );
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't look like we need a custom constructor here.


updatePosition( state, { left, top } = this.getPosition( state ) ) {
const { editor, node } = this.props;
const mceToolbar = tinymce.$( '.mce-toolbar-grp', editor.getContainer() )[ 0 ];
Copy link
Contributor

Choose a reason for hiding this comment

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

It's unlikely to happen, but should we bulletproof this if we can't find the node?

return null;
}

getSelectedSuggestionIndex() {
Copy link
Contributor

@gwwar gwwar Jan 13, 2017

Choose a reason for hiding this comment

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

I think we can use findIndex here. If you pass through undefined or null as the first argument, this method will return -1.

if ( ! this.state.selectedSuggestionId ) {
    return 0;
}

return findIndex( this.matchingSuggestions, ( suggestion ) => {
    return suggestion.ID === this.state.selectedSuggestionId;
} );

I'd also suggest updating the no-match value from null to -1, which is common for these indexOf functions.


for ( let i = 0; i < this.matchingSuggestions.length; i++ ) {
if ( this.matchingSuggestions[ i ].ID === this.state.selectedSuggestionId ) {
return this.matchingSuggestions[ i ];
Copy link
Contributor

@gwwar gwwar Jan 13, 2017

Choose a reason for hiding this comment

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

How about:

const index = this.getSelectedSuggestionIndex();
if ( index >= 0 ) {
    return this.matchingSuggestions[ index ];
}
return null;


insertSuggestion = ( suggestion ) => {
const { editor } = this.props;
const re = /@\S*/;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this never changes, we can move this up out of the function as a constant;

insertSuggestion = ( suggestion ) => {
const { editor } = this.props;
const re = /@\S*/;
const markup = <EditorMention username={ suggestion.user_login } />;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we do an early return if suggestion is falsey?

return;
}

if ( keyCode === VK.DOWN ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe move the prevent default to above this line?

Also a switch might read a little nicer here, though that's optional.

@mtias mtias added [Status] Needs Author Reply and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Jan 24, 2017
@donnapep donnapep force-pushed the add/editor-mentions-components branch from ab28552 to 88c3e6e Compare February 24, 2017 20:27
@matticbot matticbot added the [Size] XL Probably needs to be broken down into multiple smaller issues label Feb 24, 2017
@obenland obenland force-pushed the add/editor-mentions-components branch from 2b059c0 to 1ee6862 Compare February 25, 2017 00:05
@matticbot matticbot added the [Size] XL Probably needs to be broken down into multiple smaller issues label Feb 25, 2017
@obenland obenland force-pushed the add/editor-mentions-components branch from 1ee6862 to c775c8f Compare February 25, 2017 00:12
@matticbot matticbot added the [Size] XL Probably needs to be broken down into multiple smaller issues label Feb 25, 2017
@obenland obenland force-pushed the add/editor-mentions-components branch from c775c8f to f0d0f89 Compare February 25, 2017 03:32
@matticbot matticbot added the [Size] XL Probably needs to be broken down into multiple smaller issues label Feb 25, 2017
@obenland obenland added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] Needs Author Reply labels Feb 25, 2017
@obenland
Copy link
Member

Let's give this another round of eyes? @gwwar most of your concerns have been addressed I think, with the exception of your regex question. Maybe @donnapep has more insights there?

if ( query ) {
suggestions = suggestions.filter( ( { user_login: userLogin, display_name: displayName } ) => {
// Start of string or preceded by a space.
const matcher = new RegExp( `^${ query }|\\s${ query }`, 'ig' );
Copy link
Contributor

Choose a reason for hiding this comment

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

I just want to verify that we intend to enable regex searches here. For example a user typing "foo.bar" for query will match the literal "foo.bar" along with any strings like "fooxbar" where the period can be replaced with any character.

If we intend to only do literal searches, we'd want to escape any reserved regex characters from the query.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see what you're saying. Perhaps we can address this in #10321

Copy link
Contributor

@gwwar gwwar left a comment

Choose a reason for hiding this comment

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

Unless folks have any objections, let's 🚢

Even though this change should be pretty safe. Let's also remember to do a bit of smoke testing on stage before deploying

@obenland
Copy link
Member

w00t! 🎉

Just so I understand correctly, when you say smoke-testing, do you mean with #10321?

@donnapep donnapep merged commit e8e4bb0 into master Feb 28, 2017
@donnapep donnapep deleted the add/editor-mentions-components branch February 28, 2017 11:13
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Feb 28, 2017
@mtias
Copy link
Member

mtias commented Feb 28, 2017

Nice to see this being merged!

@gwwar
Copy link
Contributor

gwwar commented Feb 28, 2017

Just so I understand correctly, when you say smoke-testing, do you mean with #10321?

@obenland No I meant this PR. Safe changes have led to folks have skipping this step in the past, which sometimes causes issues.

@donnapep
Copy link
Contributor Author

@gwwar I poked around the editor a bit before deploying and didn't see any issues. Thanks for the review!

} );
}

return suggestions.slice( 0, 10 );
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use lodash slice here for safety, or add a falsey check. soslice( suggestions, 0, 10 ) or suggestions && suggestions.slice( 0, 10 );

Also do we ever expect to have suggestions if query is falsey?

Copy link
Member

Choose a reason for hiding this comment

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

I was just looking at getUserSuggestions() and I wonder if it would make sense to fall back to an empty array there?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that looks like a better default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Post/Page Editor The editor for editing posts and pages. OSS Citizen [Size] XL Probably needs to be broken down into multiple smaller issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants