@@ -6,6 +6,8 @@ import { placeMarkerDecorations } from "../editor-helpers/marker-placer"
66import Im , { fromJS } from "immutable"
77import ImPropTypes from "react-immutable-proptypes"
88
9+ import isUndefined from "lodash/isUndefined"
10+ import omit from "lodash/omit"
911import isEqual from "lodash/isEqual"
1012import 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