Skip to content

Commit

Permalink
React rewrite of Babel REPL (pull request #1297 from bvaughn/repl-2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn authored Aug 15, 2017
2 parents 619ca74 + 4951d6f commit 9e24125
Show file tree
Hide file tree
Showing 25 changed files with 8,435 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@ node_modules
.DS_Store
npm-debug.log
_includes/readmes/*.md

# testing
coverage

# production
build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
9 changes: 9 additions & 0 deletions js/repl/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[ignore]

[include]

[libs]

[lints]

[options]
15 changes: 15 additions & 0 deletions js/repl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Babel REPL

## Development

```sh
yarn install
yarn start
```

Dev site is accessible at [localhost:3000](http://localhost:3000/).

## Production build
```sh
yarn build
```
25 changes: 25 additions & 0 deletions js/repl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "babel-repl",
"version": "0.0.1",
"private": true,
"dependencies": {
"flow-bin": "^0.52.0",
"glamor": "^2.20.39",
"lodash.camelcase": "^4.3.0",
"lz-string": "^1.4.4",
"prettier": "^1.5.3",
"react": "^16.0.0-beta.3",
"react-dom": "^16.0.0-beta.3",
"react-scripts": "1.0.10"
},
"scripts": {
"build": "react-scripts build",
"eject": "react-scripts eject",
"flow": "flow",
"postbuild": "cp -r ./build ../../_site/repl2",
"prettier": "prettier --single-quote --write 'src/**/*.{js,jsx}'",
"start": "react-scripts start",
"test": "react-scripts test --env=jsdom"
},
"devDependencies": {}
}
Binary file added js/repl/public/favicon.ico
Binary file not shown.
55 changes: 55 additions & 0 deletions js/repl/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">

<!--
Load shared resources from CDN to maximize on caching.
This enables us to share assets with other REPLs (eg Prettier).
-->
<script src="https://unpkg.com/babel-standalone@^6"></script>
<script src="https://unpkg.com/codemirror@5/lib/codemirror.js"></script>
<script src="https://unpkg.com/codemirror@5/mode/javascript/javascript.js"></script>
<script src="https://unpkg.com/codemirror@5/mode/xml/xml.js"></script>
<script src="https://unpkg.com/codemirror@5/mode/jsx/jsx.js"></script>
<script src="https://unpkg.com/codemirror@5/keymap/sublime.js"></script>
<script src="https://unpkg.com/codemirror@5/addon/display/placeholder.js"></script>
<script src="https://unpkg.com/codemirror@5/addon/selection/active-line.js"></script>
<link rel="stylesheet" href="https://unpkg.com/codemirror@5//lib/codemirror.css">

<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Babel REPL</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
15 changes: 15 additions & 0 deletions js/repl/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "Babel REPL",
"name": "Babel REPL",
"icons": [
{
"src": "favicon.ico",
"sizes": "192x192",
"type": "image/png"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
132 changes: 132 additions & 0 deletions js/repl/src/CodeMirror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// @flow

import { css } from 'glamor';
import React from 'react';
import { colors } from './styles';

const DEFAULT_CODE_MIRROR_OPTIONS = {
autoCloseBrackets: true,
keyMap: 'sublime',
lineNumbers: true,
matchBrackets: true,
mode: 'text/jsx',
showCursorWhenSelecting: true,
styleActiveLine: true,
tabWidth: 2
};

type Props = {
autoFocus: boolean,
onChange: (value: string) => void,
options: Object,
placeholder?: string,
value: ?string,
preserveScrollPosition: boolean
};

export default class CodeMirror extends React.Component {
static defaultProps = {
autoFocus: false,
preserveScrollPosition: false,
onChange: (value: string) => {}
};

props: Props;
state = {
isFocused: false
};

_codeMirror: any;
_textAreaRef: HTMLTextAreaElement;

componentDidMount() {
this._codeMirror = window.CodeMirror.fromTextArea(this._textAreaRef, {
...DEFAULT_CODE_MIRROR_OPTIONS,
...this.props.options
});
this._codeMirror.on('change', this._onChange);
this._codeMirror.setValue(this.props.value || '');
}

componentWillUnmount() {
// is there a lighter-weight way to remove the cm instance?
if (this._codeMirror) {
this._codeMirror.toTextArea();
}
}

componentWillReceiveProps(nextProps: Props) {
if (
nextProps.value &&
nextProps.value !== this.props.value &&
this._codeMirror.getValue() !== nextProps.value
) {
if (nextProps.preserveScrollPosition) {
var prevScrollPosition = this._codeMirror.getScrollInfo();
this._codeMirror.setValue(nextProps.value);
this._codeMirror.scrollTo(
prevScrollPosition.left,
prevScrollPosition.top
);
} else {
this._codeMirror.setValue(nextProps.value);
}
} else if (!nextProps.value) {
this._codeMirror.setValue('');
}

if (typeof nextProps.options === 'object') {
for (let optionName in nextProps.options) {
if (nextProps.options.hasOwnProperty(optionName)) {
this._updateOption(optionName, nextProps.options[optionName]);
}
}
}
}

focus() {
if (this._codeMirror) {
this._codeMirror.focus();
}
}

render() {
return (
<textarea
autoComplete="off"
autoFocus={this.props.autoFocus}
defaultValue={this.props.value}
ref={this._setTextAreaRef}
placeholder={this.props.placeholder}
/>
);
}

_updateOption(optionName: string, newValue: any) {
const oldValue = this._codeMirror.getOption(optionName);

if (oldValue !== newValue) {
this._codeMirror.setOption(optionName, newValue);
}
}

_onChange = (doc: any, change: any) => {
if (change.origin !== 'setValue') {
this.props.onChange(doc.getValue());
}
};

_setTextAreaRef = (ref: HTMLTextAreaElement) => {
this._textAreaRef = ref;
};
}

css.global('.CodeMirror', {
height: '100% !important',
width: '100% !important',
'-webkit-overflow-scrolling': 'touch'
});

css.global('pre.CodeMirror-placeholder', {
color: colors.foregroundLight
});
85 changes: 85 additions & 0 deletions js/repl/src/CodeMirrorPanel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// @flow

import { css } from 'glamor';
import CodeMirror from './CodeMirror';
import React from 'react';
import { colors } from './styles';

type Props = {
className?: string,
code: ?string,
error: ?Error,
info?: ?string,
onChange?: (value: string) => void,
options: Object,
placeholder?: string
};

export default function CodeMirrorPanel(props: Props) {
const { className = '', error, info, onChange } = props;

return (
<div className={`${styles.panel} ${className}`}>
<div className={styles.codeMirror}>
<CodeMirror
onChange={onChange}
options={{
...props.options,
readOnly: onChange == null
}}
placeholder={props.placeholder}
preserveScrollPosition={onChange == null}
value={props.code}
/>
</div>
{info &&
<pre className={styles.info}>
{info}
</pre>}
{error &&
<pre className={styles.error}>
{error.message}
</pre>}
</div>
);
}

const sharedBoxStyles = {
flex: '0 0 auto',
maxHeight: '33%',
overflow: 'auto',
margin: 0,
padding: '0.25rem 0.5rem',
whiteSpace: 'pre-wrap',
'-webkit-overflow-scrolling': 'touch'
};

const styles = {
codeMirror: css({
display: 'block',
height: '100%',
width: '100%',
overflow: 'auto'
}),
error: css({
order: 2,
backgroundColor: colors.errorBackground,
borderTop: `1px solid ${colors.errorBorder}`,
color: colors.errorForeground,
...sharedBoxStyles
}),
info: css({
order: 1,
backgroundColor: colors.infoBackground,
borderTop: `1px solid ${colors.infoBorder}`,
color: colors.infoForeground,
...sharedBoxStyles
}),
panel: css({
height: '100%',
display: 'flex',
flexDirection: 'column',
justifyContent: 'stretch',
overflow: 'auto'
})
};
Loading

0 comments on commit 9e24125

Please sign in to comment.