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

Fix CodeEditor not loading when WordPress is installed in a subfolder #6777

Merged
merged 9 commits into from
May 23, 2018

Conversation

ajitbohra
Copy link
Member

@ajitbohra ajitbohra commented May 16, 2018

Description

How has this been tested?

Manual

Types of changes

Bug fixes

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 requested review from noisysocks and westonruter May 17, 2018 09:05
@gziolo gziolo added [Feature] UI Components Impacts or related to the UI component system [Type] Bug An existing feature does not function as intended labels May 17, 2018
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.

Thanks so much for looking into this! ❤️

I think that a simpler way to fix #6737 is to change components/code-editor/editor.js:107 to use defaultValue instead of value. We then don't need to worry about changing onBlur to onChange.

@@ -99,12 +99,12 @@ class CodeEditor extends Component {
}

if ( ! this.props.focus && this.editor.hasFocus() ) {
document.activeElement.blur();
document.activeElement.change();
Copy link
Member

Choose a reason for hiding this comment

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

HTMLElement doesn't have a change() method, so this would always result in an undefined is not a function exception.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement

}
}

render() {
return <textarea ref={ ( ref ) => ( this.textarea = ref ) } value={ this.props.value } />;
return <textarea ref={ ( ref ) => ( this.textarea = ref ) } value={ this.props.value } onChange={ this.props.onChange } />;
Copy link
Member

Choose a reason for hiding this comment

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

We're calling this.props.onChange() both here in the text field's onChange handler and also in our this.editor.on( 'change', ... ) handler. I fear that this means we're calling this.props.onChange() twice on every keystroke.

I think that a better approach would be to make this text area an uncontrolled component and then let CodeMirror be solely responsible for handling user input and dispatching its change event.

return <textarea ref={ ( ref ) => ( this.textarea = ref ) } defaultValue={ this.props.value } />;

https://reactjs.org/docs/uncontrolled-components.html

@noisysocks noisysocks changed the title Fix/6737 Fix CodeEditor not loading when WordPress is installed in a subfolder May 18, 2018
@ajitbohra
Copy link
Member Author

ajitbohra commented May 18, 2018

@noisysocks

Thanks for the detailed review and info. Initially thought to use defaultValue but don't know why took that adventurous turn!

Have updated & tested the PR.

@noisysocks
Copy link
Member

I tested this locally and it looks good to go 👍

There's one failing snapshot test that we'll need to fix before merging. This should be just a matter of updating the snapshot tests using this command:

$(npm bin)/jest --config test/unit/jest.config.json --updateSnapshot

@ajitbohra
Copy link
Member Author

@noisysocks snapshots updated

@noisysocks noisysocks merged commit 81bf90c into WordPress:master May 23, 2018
@@ -21,7 +21,7 @@ function loadScript() {
}

const script = document.createElement( 'script' );
script.src = `/wp-admin/load-scripts.php?load=${ handles.join( ',' ) }`;
script.src = `${ wpApiSettings.schema.url }/wp-admin/load-scripts.php?load=${ handles.join( ',' ) }`;
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 probably not the only place where we use this, but we should move away from using globals. I'm also wondering if the CodeEditor belongs in the components module since the components module is supposed to be for generic components (without WP dependency?).

Anyway, this is not urgent and will have to be figured out when we move components to an npm dependency anyway. And this is not specific to this component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Type] Bug An existing feature does not function as intended
Projects
None yet
5 participants