-
Notifications
You must be signed in to change notification settings - Fork 7.7k
add non-jsx view to interactive code samples #141
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
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
Deploy preview ready! Built with commit 8a0eda3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some feedback.
Here's a rough prototype I made (demonstrating the feedback I left in the PR) if you're interested:
https://gist.github.com/bvaughn/c65d556693fed3dbc6ab2025efac2c2b
@@ -11,13 +11,19 @@ import ReactDOM from 'react-dom'; | |||
import Remarkable from 'remarkable'; | |||
// TODO: switch back to the upstream version of react-live | |||
// once https://github.com/FormidableLabs/react-live/issues/37 is fixed. | |||
import {LiveProvider, LiveEditor} from '@gaearon/react-live'; | |||
import {LiveEditor, LiveProvider} from '@gaearon/react-live'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆 Love the ninja alpha-sort
|
||
const compile = code => | ||
Babel.transform(code, {presets: ['es2015', 'react']}).code; // eslint-disable-line no-undef | ||
|
||
const compileJsxToJS = code => Babel.transform(code, {presets: ['react']}).code; // eslint-disable-line no-undef |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you feel about these names for improved clarity?
// eslint-disable-next-line no-undef
const compileES5 = code =>
Babel.transform(code, {presets: ['es2015', 'react']}).code;
// eslint-disable-next-line no-undef
const compileES6 = code => Babel.transform(code, {presets: ['react']}).code;
spellCheck="false" | ||
dangerouslySetInnerHTML={{__html: prism(compiledJsx)}} | ||
/> | ||
)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's re-use the LiveProvider
+LiveEditor
to show the non-JSX code too. This way we don't have to use Prism directly. So something like:
<LiveProvider code={showJSX ? code : compiledES6} mountStylesheet={false}>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, compiledES6 would be editable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good point. I guess this wouldn't quite work as proposed. Maybe we should make the ES6 preview not editable?
Back to JSX Editor | ||
</a> | ||
)} | ||
</MetaTitle> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bvaughn Thank you very much, I'll jump on it. |
@bvaughn I have made both ES5 and ES6 editable. There might be small problem when coming back to If this scenario is not acceptable than we can make |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might be small problem when coming back to JSX view since what we have changed in JS wont be applied to JSX.
If this scenario is not acceptable than we can make JSX compiled view to be preview only.
That's a good point!
I think it's probably okay. I don't really think that's a common use case or important enough to warrant added complexity.
@@ -282,7 +312,7 @@ class CodeEditor extends Component { | |||
} | |||
|
|||
_onChange = code => { | |||
this.setState(this._updateState(code)); | |||
this.setState(this._updateState(code, this.state.showJSX)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should change this to:
this.setState(state => this._updateState(code, state.showJSX));
The benefit of using a callback for setState
is that it avoids references potentially stale values in this.state
. This didn't matter before because we weren't references this.state
but now that we are....we should use the callback form. 😄
return { | ||
compiled: compile(code), | ||
let newState = { | ||
compiled: compileES5(code), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should code
always go into newState
? What's the benefit of only adding it conditionally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I thought here is that we want to keep code only if we are in JSX view.
If we mix code from ES5 view with ES6 it wont work any more.
There was a problem hiding this comment.
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 mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 1-way edit flow for JSX-to-non-JSX bugs me a bit but I think it's probably not an actual problem and I can't think of a reasonable solution. 😄
Thanks for all of your work on this!
return { | ||
compiled: compile(code), | ||
let newState = { | ||
compiled: compileES5(code), |
There was a problem hiding this comment.
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 mean.
Yes indeed, it is cost for ES5 being editable. Thank you very much for guidance it was awesome. |
It wasn't overridden? I just tweaked the wording slightly when testing the changes to the home template. (Without those changes, a blocked Babel script just caused the home page to show "Loading examples" forever.) |
Yes I saw the reason, also sometimes after the page is started and you scroll down, new refresh happens with editors being in the place. But regarding is it overridden, it is. Here is raw file on master branch for |
Oh whoa! I see what you mean! Looks like something went wrong with the merge. Thank you for pointing that out! I'll fix. |
@bvaughn your welcome, no problems. |
😅 too much context-switching this morning. Very sorry this happened. I'll get it fixed right away. |
hh I believe, no problems at all, it is good it is noticed on time. |
Sync with reactjs.org @ 8803c63
…js#141) * finished translating para 1 and 2 * finished translating para 3 and 4 * altered wording * use Chinese character of question mark * use Chinese character of full stop * Update content/docs/faq-internals.md Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com> * Update content/docs/faq-internals.md Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com> * Update content/docs/faq-internals.md Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com> * Update content/docs/faq-internals.md Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com> * Update faq-internals.md * Update content/docs/faq-internals.md Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com> * Update content/docs/faq-internals.md Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com> * Update content/docs/faq-internals.md Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com> * Update content/docs/faq-internals.md Co-Authored-By: ChiuMungZitAlexander <zhaomengzhe@qq.com>
#99
Hello @bvaughn .
Here is implementation of code interaction view:

and once clicked on

VIEW COMPILED JSX
we get this: