Skip to content

Commit 2c3d317

Browse files
authored
Merge pull request #1515 from swagger-api/feature/configs-selector-for-editor-options
Add editorOptions from state ( relies on configsPlugin )
2 parents 08776a9 + 01be718 commit 2c3d317

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/plugins/editor/components/editor-container.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,33 @@ export default class EditorContainer extends React.Component {
2020
}
2121

2222
render() {
23-
let { specSelectors, getComponent, errSelectors, fn, readOnly, editorSelectors } = this.props
23+
let { specSelectors, getComponent, errSelectors, fn, editorSelectors, configsSelectors } = this.props
2424

2525
let Editor = getComponent("Editor")
2626

2727
let wrapperClasses = ["editor-wrapper"]
28+
const readOnly = !!configsSelectors.get("readOnly")
2829

2930
if(readOnly) {
3031
wrapperClasses.push("read-only")
3132
}
3233

3334
let propsForEditor = this.props
3435

36+
const editorOptions = {
37+
enableLiveAutocompletion: configsSelectors.get("editorLiveAutocomplete"),
38+
readOnly: readOnly,
39+
highlightActiveLine: !readOnly,
40+
highlightGutterLine: !readOnly,
41+
}
42+
3543
return (
3644
<div id='editor-wrapper' className={wrapperClasses.join(" ")}>
3745
{ readOnly ? <h2 className="editor-readonly-watermark">Read Only</h2> : null }
3846
<Editor
3947
{...propsForEditor}
4048
value={specSelectors.specStr()}
49+
editorOptions={editorOptions}
4150
specObject={specSelectors.specJson().toJS()}
4251
errors={errSelectors.allErrors()}
4352
onChange={this.onChange}
@@ -56,11 +65,11 @@ EditorContainer.defaultProps = {
5665

5766
EditorContainer.propTypes = {
5867
specActions: PropTypes.object.isRequired,
68+
configsSelectors: PropTypes.object.isRequired,
5969
onChange: PropTypes.func,
6070
fn: PropTypes.object,
6171
specSelectors: PropTypes.object.isRequired,
6272
errSelectors: PropTypes.object.isRequired,
6373
editorSelectors: PropTypes.object.isRequired,
6474
getComponent: PropTypes.func.isRequired,
65-
readOnly: PropTypes.bool
6675
}

src/plugins/editor/components/editor.jsx

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { placeMarkerDecorations } from "../editor-helpers/marker-placer"
66
import Im, { fromJS } from "immutable"
77
import ImPropTypes from "react-immutable-proptypes"
88

9+
import isUndefined from "lodash/isUndefined"
10+
import omit from "lodash/omit"
911
import isEqual from "lodash/isEqual"
1012
import isEmpty from "lodash/isEmpty"
1113

@@ -69,10 +71,11 @@ export default function makeEditor({ editorPluginsToRun }) {
6971
langTools, AST, specObject
7072
})
7173

72-
7374
editor.setHighlightActiveLine(false)
7475
editor.setHighlightActiveLine(true)
7576

77+
this.syncOptionsFromState(this.props.editorOptions)
78+
7679
props.editorActions.onLoad({...props, langTools, editor})
7780
}
7881

@@ -118,24 +121,6 @@ export default function makeEditor({ editorPluginsToRun }) {
118121
}
119122
}
120123

121-
setReadOnlyOptions = (nextProps) => {
122-
let { state } = this
123-
124-
if(nextProps.readOnly === true && state.editor) {
125-
state.editor.setOptions({
126-
readOnly: true,
127-
highlightActiveLine: false,
128-
highlightGutterLine: false
129-
})
130-
} else if(state.editor) {
131-
state.editor.setOptions({
132-
readOnly: false,
133-
highlightActiveLine: true,
134-
highlightGutterLine: true
135-
})
136-
}
137-
}
138-
139124
updateMarkerAnnotations = (nextProps, { force } = {}) => {
140125
let { state } = this
141126
let { onMarkerLineUpdate } = nextProps
@@ -157,6 +142,7 @@ export default function makeEditor({ editorPluginsToRun }) {
157142
// allows our custom Editor styling for IE10 to take effect
158143
var doc = document.documentElement
159144
doc.setAttribute("data-useragent", navigator.userAgent)
145+
this.syncOptionsFromState(this.props.editorOptions)
160146
}
161147

162148
componentDidMount() {
@@ -175,9 +161,12 @@ export default function makeEditor({ editorPluginsToRun }) {
175161
let wasEmptyBefore = (k) => nextProps[k] && (!this.props[k] || isEmpty(this.props[k]))
176162

177163
this.updateErrorAnnotations(nextProps)
178-
this.setReadOnlyOptions(nextProps)
179164
this.updateMarkerAnnotations(nextProps)
180165

166+
if(hasChanged("editorOptions")) {
167+
this.syncOptionsFromState(nextProps.editorOptions)
168+
}
169+
181170
if(state.editor && nextProps.goToLine && nextProps.goToLine.line && hasChanged("goToLine")) {
182171
state.editor.gotoLine(nextProps.goToLine.line)
183172
nextProps.editorActions.jumpToLine(null)
@@ -189,6 +178,21 @@ export default function makeEditor({ editorPluginsToRun }) {
189178

190179
}
191180

181+
syncOptionsFromState(editorOptions) {
182+
const { editor } = this.state
183+
if(!editor) {
184+
return
185+
}
186+
187+
const setOptions = omit(editorOptions, ["readOnly"])
188+
editor.setOptions(setOptions)
189+
190+
const readOnly = isUndefined(editorOptions.readOnly)
191+
? false
192+
: editorOptions.readOnly // If its undefined, default to false.
193+
editor.setReadOnly(readOnly)
194+
}
195+
192196
yaml = this.yaml || "";
193197

194198
shouldComponentUpdate(nextProps) {
@@ -199,7 +203,6 @@ export default function makeEditor({ editorPluginsToRun }) {
199203
}
200204

201205
render() {
202-
let { readOnly } = this.props
203206
const value = this.yaml
204207

205208
return (
@@ -214,7 +217,6 @@ export default function makeEditor({ editorPluginsToRun }) {
214217
height="100%"
215218
tabSize={2}
216219
fontSize={14}
217-
readOnly={readOnly}
218220
useSoftTabs="true"
219221
wrapEnabled={true}
220222
editorProps={{
@@ -250,12 +252,11 @@ export default function makeEditor({ editorPluginsToRun }) {
250252
Editor.propTypes = {
251253
specId: PropTypes.string,
252254
value: PropTypes.string,
255+
editorOptions: PropTypes.object,
253256

254257
onChange: PropTypes.func,
255258
onMarkerLineUpdate: PropTypes.func,
256259

257-
readOnly: PropTypes.bool,
258-
259260
markers: PropTypes.object,
260261
goToLine: PropTypes.object,
261262
specObject: PropTypes.object.isRequired,
@@ -271,9 +272,9 @@ export default function makeEditor({ editorPluginsToRun }) {
271272
onChange: NOOP,
272273
onMarkerLineUpdate: NOOP,
273274
markers: {},
274-
readOnly: false,
275275
goToLine: {},
276276
errors: fromJS([]),
277+
editorOptions: {},
277278
}
278279

279280
return Editor

0 commit comments

Comments
 (0)