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: Extract LinkContainer from FormatToolbar #9192

Merged
merged 3 commits into from
Aug 22, 2018

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Aug 21, 2018

Description

Extracted out of #9012.

This PR extracts LinkContainer to make it easier to provide React Native implementation for the parts that need to work differently on mobile devices.

How has this been tested?

npm test
npm run build

  1. Add paragraph block.
  2. Type some text.
  3. Select text and turn it into link.
  4. Type link url and save it.
  5. Edit just saved url and save it.
  6. Add more text and turn it into link.
  7. Go back to the previously added link and try to edit it.
  8. Remove added link.

All of those should work properly.

Types of changes

There should be no visual changes. It should be still possible to add/remove links from RichText powered fields like Paragraph block.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo added [Type] Code Quality Issues or PRs that relate to code quality Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) labels Aug 21, 2018
@gziolo gziolo added this to the 3.7 milestone Aug 21, 2018
@gziolo gziolo self-assigned this Aug 21, 2018
import { __ } from '@wordpress/i18n';
import { displayShortcut } from '@wordpress/keycodes';

export const DEFAULT_CONTROLS = [ 'bold', 'italic', 'strikethrough', 'link', 'code' ];
Copy link
Member Author

Choose a reason for hiding this comment

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

@iseulde I moved those contants to its own file. It's now easy to notice that code is listed as a default control, but it's not provided in formatting controls. Any thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

I'm also not sure why there are two constants, and why code is there... code does have a shortcut and a pattern, but this constant is not used there.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps code should be taken away 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.

yes, let's remove it.

Copy link
Contributor

@SergioEstevao SergioEstevao left a comment

Choose a reason for hiding this comment

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

This is looking good and it will make it easier to port the toolbar to RN.

@gziolo
Copy link
Member Author

gziolo commented Aug 21, 2018

I added fix in e08af7b which resolves the issue with race condition between props update and internal state changes. To make it work properly the derived state always set linkValue based on formats prop and it changes explicitly only when the user types.

@gziolo gziolo requested a review from a team August 21, 2018 13:05
@@ -1000,7 +999,7 @@ RichText.contextTypes = {
};

RichText.defaultProps = {
formattingControls: DEFAULT_FORMATS,
formattingControls: FORMATTING_CONTROLS.map( ( { format } ) => format ),
Copy link
Member Author

Choose a reason for hiding this comment

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

@iseulde - as you pointed out, we don't need this constant as we can produce it from the default FORMATTING_CONTROLS.

@gziolo gziolo force-pushed the update/split-formatting-toolbars branch from f2dfed9 to d2fc77a Compare August 21, 2018 13:39
Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Looks good! We're definitely overdue to break up this component 🙂

I'm wondering if we should move all of the code that knows about links out of FormatToolbar?

@@ -140,19 +107,16 @@ class FormatToolbar extends Component {
}

addLink() {
Copy link
Member

Choose a reason for hiding this comment

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

Why not move these methods to LinkContainer?

Copy link
Member Author

Choose a reason for hiding this comment

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

It should be now possible as I removed the usage of setState in both methods shared with toolbarControls variable. I will double check if this is expected on mobile and open a follow-up PR 👍

@gziolo gziolo force-pushed the update/split-formatting-toolbars branch from d2fc77a to 7b73a20 Compare August 22, 2018 06:02
@@ -140,19 +107,16 @@ class FormatToolbar extends Component {
}

addLink() {
Copy link
Member Author

Choose a reason for hiding this comment

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

It should be now possible as I removed the usage of setState in both methods shared with toolbarControls variable. I will double check if this is expected on mobile and open a follow-up PR 👍

@@ -164,8 +129,6 @@ class FormatToolbar extends Component {
rel: this.state.opensInNewWindow ? 'noreferrer noopener' : null,
value,
} } );

this.setState( { linkValue: value, isEditingLink: false } );
if ( ! this.props.formats.link.value ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

We call this.props.onChange so we don't need to update state explicitly because getDerivedStateFromProps will handle it properly.

@gziolo
Copy link
Member Author

gziolo commented Aug 22, 2018

I'm wondering if we should move all of the code that knows about links out of FormatToolbar?

As commented already. It wasn't possible initially because both Toolbar and LinkContainer shared the same methods which depended on shared state. It's no longer an issue so we can take another step. I would prefer to do it in its own PR so we could unblock React Native implementation (#9012).

Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@gziolo gziolo merged commit 649320e into master Aug 22, 2018
@gziolo gziolo deleted the update/split-formatting-toolbars branch August 22, 2018 07:02
@ellatrix ellatrix mentioned this pull request Aug 22, 2018
4 tasks
@gziolo gziolo mentioned this pull request Aug 22, 2018
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants