From bce0e5fec1b9f51227770c75fc20b790c46ba9a5 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 28 Feb 2017 16:10:23 -0800 Subject: [PATCH] Fixed some editor bugs (#97) * Fixed some editor bugs * Some fixes * Update index.js --- app/ui/components/base/Lazy.js | 23 +++++++ app/ui/components/codemirror/Editor.js | 33 +++++---- app/ui/components/codemirror/OneLineEditor.js | 18 +++++ .../editors/RequestHeadersEditor.js | 45 +++++++------ app/ui/components/keyvalueeditor/Editor.js | 67 ++++++++++++++----- app/ui/components/keyvalueeditor/Row.js | 2 +- app/ui/components/viewers/ResponseRaw.js | 7 +- app/ui/components/viewers/ResponseViewer.js | 16 ++--- app/ui/css/components/keyvalueeditor.less | 11 +++ 9 files changed, 154 insertions(+), 68 deletions(-) create mode 100644 app/ui/components/base/Lazy.js diff --git a/app/ui/components/base/Lazy.js b/app/ui/components/base/Lazy.js new file mode 100644 index 0000000000..2282f8acf9 --- /dev/null +++ b/app/ui/components/base/Lazy.js @@ -0,0 +1,23 @@ +import React, {PureComponent, PropTypes} from 'react'; + +class Lazy extends PureComponent { + constructor (props) { + super(props); + this.state = {show: false}; + } + + componentDidMount () { + const delay = this.props.delay || 0; + setTimeout(() => this.setState({show: true}), delay); + } + + render () { + return this.state.show ? this.props.children : null; + } +} + +Lazy.propTypes = { + delay: PropTypes.number, +}; + +export default Lazy; diff --git a/app/ui/components/codemirror/Editor.js b/app/ui/components/codemirror/Editor.js index 79198c6b27..6b0645fda3 100644 --- a/app/ui/components/codemirror/Editor.js +++ b/app/ui/components/codemirror/Editor.js @@ -132,7 +132,11 @@ class Editor extends PureComponent { * @returns {boolean} */ hasFocus () { - return this.codeMirror.hasFocus(); + if (this.codeMirror) { + return this.codeMirror.hasFocus(); + } else { + return false; + } } /** @@ -162,7 +166,6 @@ class Editor extends PureComponent { } const {value, debounceMillis: ms} = this.props; - this.codeMirror = CodeMirror.fromTextArea(textarea, BASE_CODEMIRROR_OPTIONS); // Set default listeners @@ -183,11 +186,11 @@ class Editor extends PureComponent { }); } - // Do this a bit later so we don't block the render process - window.requestAnimationFrame(() => { - // Set editor options - this._codemirrorSetOptions(); + // Set editor options + this._codemirrorSetOptions(); + // Do this a bit later so we don't block the render process + setTimeout(() => { // Actually set the value this._codemirrorSetValue(value || ''); @@ -198,15 +201,9 @@ class Editor extends PureComponent { // Unset default cursor of [0, 0]; this.codeMirror.setCursor({line: -1, ch: -1}); - }); + }, 10); }; - _debounce (fn) { - const {debounceMillis} = this.props; - const ms = typeof debounceMillis === 'number' ? debounceMillis : DEBOUNCE_MILLIS; - return misc.debounce(fn, ms); - } - _isJSON (mode) { if (!mode) { return false; @@ -568,10 +565,12 @@ class Editor extends PureComponent { return (
-