Add debounce to editor
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
import { Subject } from 'rx';
|
||||||
import React, { PropTypes } from 'react';
|
import React, { PropTypes } from 'react';
|
||||||
import { createSelector } from 'reselect';
|
import { createSelector } from 'reselect';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
@ -12,6 +13,8 @@ const mapStateToProps = createSelector(
|
|||||||
(windowHeight, navHeight) => ({ height: windowHeight - navHeight - 50 })
|
(windowHeight, navHeight) => ({ height: windowHeight - navHeight - 50 })
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const editorDebounceTimeout = 750;
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
lint: true,
|
lint: true,
|
||||||
lineNumbers: true,
|
lineNumbers: true,
|
||||||
@ -26,6 +29,11 @@ const options = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export class Editor extends PureComponent {
|
export class Editor extends PureComponent {
|
||||||
|
constructor(...args) {
|
||||||
|
super(...args);
|
||||||
|
this._editorContent$ = new Subject();
|
||||||
|
this.handleChange = this.handleChange.bind(this);
|
||||||
|
}
|
||||||
static displayName = 'Editor';
|
static displayName = 'Editor';
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
height: PropTypes.number,
|
height: PropTypes.number,
|
||||||
@ -39,8 +47,32 @@ export class Editor extends PureComponent {
|
|||||||
mode: 'javascript'
|
mode: 'javascript'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
const { updateFile = (() => {}) } = this.props;
|
||||||
|
this._subscription = this._editorContent$
|
||||||
|
.debounce(editorDebounceTimeout)
|
||||||
|
.distinctUntilChanged()
|
||||||
|
.subscribe(
|
||||||
|
updateFile,
|
||||||
|
err => { throw err; }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
if (this._subscription) {
|
||||||
|
this._subscription.dispose();
|
||||||
|
this._subscription = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleChange(value) {
|
||||||
|
if (this._subscription) {
|
||||||
|
this._editorContent$.onNext(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { content, height, mode, updateFile } = this.props;
|
const { content, height, mode } = this.props;
|
||||||
const style = {};
|
const style = {};
|
||||||
if (height) {
|
if (height) {
|
||||||
style.height = height + 'px';
|
style.height = height + 'px';
|
||||||
@ -51,7 +83,7 @@ export class Editor extends PureComponent {
|
|||||||
style={ style }>
|
style={ style }>
|
||||||
<NoSSR>
|
<NoSSR>
|
||||||
<Codemirror
|
<Codemirror
|
||||||
onChange={ updateFile }
|
onChange={ this.handleChange }
|
||||||
options={{ ...options, mode }}
|
options={{ ...options, mode }}
|
||||||
value={ content } />
|
value={ content } />
|
||||||
</NoSSR>
|
</NoSSR>
|
||||||
|
Reference in New Issue
Block a user