Skip to content

Commit

Permalink
Make IDE component contain Resizable
Browse files Browse the repository at this point in the history
Note that this make the IDE/index component stateful, handling the
editor width.
  • Loading branch information
remo5000 committed May 25, 2018
1 parent 4f75ca2 commit d9cfa25
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 46 deletions.
24 changes: 2 additions & 22 deletions src/components/IDE/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react'

import Resizable from 're-resizable'
import AceEditor from 'react-ace'

import { Button, IconName, Intent } from '@blueprintjs/core'
Expand All @@ -17,9 +16,7 @@ import 'brace/theme/cobalt'
*/
export interface IEditorProps {
editorValue: string
editorWidth: number
handleEditorValueChange: (newCode: string) => void
handleEditorWidthChange: (widthChange: number) => void
handleEvalEditor: () => void
}

Expand All @@ -43,24 +40,7 @@ class Editor extends React.Component<IEditorProps, {}> {
)
const runButton = genericButton('', 'play', this.props.handleEvalEditor)
return (
<Resizable
className="Editor"
size={{ width: this.props.editorWidth, height: '100%' }}
// tslint:disable-next-line jsx-no-lambda
onResizeStop={(e, direction, ref, d) => {
this.props.handleEditorWidthChange(d.width)
}}
enable={{
top: false,
right: true,
bottom: false,
left: false,
topRight: false,
bottomRight: false,
bottomLeft: false,
topLeft: false
}}
>
<div className="Editor">
<div className="row editor-control end-xs">
<div className="col-xs-1">{runButton}</div>
</div>
Expand All @@ -77,7 +57,7 @@ class Editor extends React.Component<IEditorProps, {}> {
highlightActiveLine={false}
/>
</div>
</Resizable>
</div>
)
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/components/IDE/__tests__/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import Editor, { IEditorProps } from '../Editor'
test('Editor renders correctly', () => {
const props: IEditorProps = {
editorValue: '',
editorWidth: 500,
handleEditorWidthChange: widthChange => {},
handleEditorValueChange: newCode => {},
handleEvalEditor: () => {}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/IDE/__tests__/__snapshots__/Editor.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Editor renders correctly 1`] = `
"<undefined className=\\"Editor\\" size={{...}} onResizeStop={[Function: onResizeStop]} enable={{...}}>
"<div className=\\"Editor\\">
<div className=\\"row editor-control end-xs\\">
<div className=\\"col-xs-1\\">
<Blueprint2.Button onClick={[Function: handleEvalEditor]} className=\\"pt-minimal col-xs-12\\" intent=\\"none\\" icon=\\"play\\" />
Expand All @@ -10,5 +10,5 @@ exports[`Editor renders correctly 1`] = `
<div className=\\"row editor-react-ace\\">
<ReactAce className=\\"react-ace\\" mode=\\"javascript\\" theme=\\"cobalt\\" value=\\"\\" onChange={[Function: handleEditorValueChange]} width=\\"100%\\" height=\\"100%\\" fontSize={14} highlightActiveLine={false} name=\\"brace-editor\\" focus={false} showGutter={true} onPaste={{...}} onLoad={{...}} onScroll={{...}} minLines={{...}} maxLines={{...}} readOnly={false} showPrintMargin={true} tabSize={4} cursorStart={1} editorProps={{...}} style={{...}} scrollMargin={{...}} setOptions={{...}} wrapEnabled={false} enableBasicAutocompletion={false} enableLiveAutocompletion={false} />
</div>
</undefined>"
</div>"
`;
50 changes: 39 additions & 11 deletions src/components/IDE/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
import * as React from 'react'

import Resizable from 're-resizable'

import EditorContainer from '../../containers/IDE/EditorContainer'
import ReplContainer from '../../containers/IDE/ReplContainer'

const IDE: React.SFC<{}> = () => (
<div className="IDE">
<div className="row ide-content-parent">
<div className="editor-parent">
<EditorContainer />
</div>
<div className="repl-parent">
<ReplContainer />
export interface IIDEProps {
editorWidth: number
handleEditorWidthChange: (widthChange: number) => void
}

class IDE extends React.Component<IIDEProps, {}> {
public render() {
return (
<div className="IDE">
<div className="row ide-content-parent">
<Resizable
className="editor-parent"
size={{ width: this.props.editorWidth, height: '100%' }}
// tslint:disable-next-line jsx-no-lambda
onResizeStop={(e, direction, ref, d) => {
this.props.handleEditorWidthChange(d.width)
}}
enable={{
top: false,
right: true,
bottom: false,
left: false,
topRight: false,
bottomRight: false,
bottomLeft: false,
topLeft: false
}}
>
<EditorContainer />
</Resizable>
<div className="repl-parent">
<ReplContainer />
</div>
</div>
</div>
</div>
</div>
)
)
}
}

export default IDE
4 changes: 2 additions & 2 deletions src/components/Playground.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as React from 'react'

import IDE from './IDE'
import IDEContainer from '../containers/IDE'

const Playground: React.SFC<{}> = () => {
return (
<div className="Playground">
<IDE />
<IDEContainer />
</div>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/__tests__/__snapshots__/Playground.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

exports[`Playground renders correctly 1`] = `
"<div className=\\"Playground\\">
<IDE />
<Connect(IDE) />
</div>"
`;
9 changes: 3 additions & 6 deletions src/containers/IDE/EditorContainer.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux'
import { bindActionCreators, Dispatch } from 'redux'

import { evalEditor, updateEditorValue, updateEditorWidth } from '../../actions/playground'
import { evalEditor, updateEditorValue } from '../../actions/playground'
import Editor, { IEditorProps } from '../../components/IDE/Editor'
import { IState } from '../../reducers/states'

type StateProps = Pick<IEditorProps, 'editorValue'> & Pick<IEditorProps, 'editorWidth'>
type StateProps = Pick<IEditorProps, 'editorValue'>
type DispatchProps = Pick<IEditorProps, 'handleEditorValueChange'> &
Pick<IEditorProps, 'handleEditorWidthChange'> &
Pick<IEditorProps, 'handleEvalEditor'>

/** Provides the editorValue of the `IPlaygroundState` of the `IState` as a
* `StateProps` to the Playground component
*/
const mapStateToProps: MapStateToProps<StateProps, {}, IState> = state => {
return {
editorValue: state.playground.editorValue,
editorWidth: state.playground.editorWidth
editorValue: state.playground.editorValue
}
}

const mapDispatchToProps: MapDispatchToProps<DispatchProps, {}> = (dispatch: Dispatch<any>) =>
bindActionCreators(
{
handleEditorValueChange: updateEditorValue,
handleEditorWidthChange: updateEditorWidth,
handleEvalEditor: evalEditor
},
dispatch
Expand Down
28 changes: 28 additions & 0 deletions src/containers/IDE/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux'
import { bindActionCreators, Dispatch } from 'redux'

import { updateEditorWidth } from '../../actions/playground'
import IDE, { IIDEProps } from '../../components/IDE/'
import { IState } from '../../reducers/states'

type StateProps = Pick<IIDEProps, 'editorWidth'>
type DispatchProps = Pick<IIDEProps, 'handleEditorWidthChange'>

/** Provides the editorValue of the `IPlaygroundState` of the `IState` as a
* `StateProps` to the Playground component
*/
const mapStateToProps: MapStateToProps<StateProps, {}, IState> = state => {
return {
editorWidth: state.playground.editorWidth
}
}

const mapDispatchToProps: MapDispatchToProps<DispatchProps, {}> = (dispatch: Dispatch<any>) =>
bindActionCreators(
{
handleEditorWidthChange: updateEditorWidth
},
dispatch
)

export default connect(mapStateToProps, mapDispatchToProps)(IDE)

0 comments on commit d9cfa25

Please sign in to comment.