11// @flow
22import React from 'react' ;
33import CodeMirror from 'codemirror' ;
4- import styled , { injectGlobal , keyframes } from 'styled-components' ;
5-
6- import 'codemirror/lib/codemirror.css' ;
7- import 'codemirror/addon/dialog/dialog.css' ;
8- import 'codemirror/addon/hint/show-hint.css' ;
9- import 'codemirror/addon/tern/tern.css' ;
10- import 'codemirror/mode/jsx/jsx' ;
11- import 'codemirror/keymap/sublime' ;
12- import 'codemirror/addon/fold/xml-fold' ; // Needed to match JSX
13- import 'codemirror/addon/edit/matchtags' ;
14- import 'codemirror/addon/edit/closebrackets' ;
15- import 'codemirror/addon/comment/comment' ;
16- import 'codemirror/addon/selection/active-line' ;
17- import 'codemirror/addon/fold/foldcode' ;
18- import 'codemirror/addon/fold/foldgutter' ;
19- import 'codemirror/addon/fold/brace-fold' ;
4+ import styled from 'styled-components' ;
5+
6+ import { getCodeMirror } from 'app/utils/codemirror' ;
7+
208import 'codemirror/addon/dialog/dialog' ;
219import 'codemirror/addon/hint/show-hint' ;
2210import 'codemirror/addon/tern/tern' ;
2311
2412import type { Preferences } from 'app/store/preferences/reducer' ;
2513
26- import theme from '../../../../../../../../common/theme' ;
2714import Header from './Header' ;
2815
2916const documentCache = { } ;
@@ -45,15 +32,15 @@ const Container = styled.div`
4532 display: flex;
4633 flex-direction: column;
4734 width: 100%;
48- height: 100%;
35+ height: calc( 100% - 3rem) ;
4936 overflow: auto;
5037` ;
5138
5239const CodeContainer = styled . div `
5340 flex: 1 1 auto;
5441 position: relative;
5542 overflow: auto;
56- height: 100%;
43+ height: calc( 100% - 6rem) ;
5744` ;
5845
5946const handleError = ( cm , currentError , nextError , nextCode , nextId ) => {
@@ -75,7 +62,7 @@ const handleError = (cm, currentError, nextError, nextCode, nextId) => {
7562 nextError . line !== 0 &&
7663 nextError . line <= code . split ( '\n' ) . length
7764 ) {
78- cm . addLineClass ( nextError . line , 'background' , 'cm-line-error' ) ;
65+ cm . addLineClass ( nextError . line - 1 , 'background' , 'cm-line-error' ) ;
7966 }
8067 }
8168} ;
@@ -119,7 +106,7 @@ export default class CodeEditor extends React.PureComponent {
119106 }
120107 }
121108
122- updateCodeMirrorCode ( code ) {
109+ updateCodeMirrorCode ( code : string ) {
123110 const pos = this . codemirror . getCursor ( ) ;
124111 this . codemirror . setValue ( code ) ;
125112 this . codemirror . setCursor ( pos ) ;
@@ -139,20 +126,7 @@ export default class CodeEditor extends React.PureComponent {
139126
140127 documentCache [ id ] = new CodeMirror . Doc ( code || '' , 'jsx' ) ;
141128
142- this . codemirror = new CodeMirror ( el , {
143- mode : 'jsx' ,
144- theme : 'oceanic' ,
145- keyMap : 'sublime' ,
146- indentUnit : 2 ,
147- autoCloseBrackets : true ,
148- matchTags : { bothTags : true } ,
149- foldGutter : true ,
150- gutters : [ 'CodeMirror-linenumbers' , 'CodeMirror-foldgutter' ] ,
151- value : documentCache [ id ] ,
152- lineNumbers : true ,
153- lineWrapping : true ,
154- styleActiveLine : true ,
155- } ) ;
129+ this . codemirror = getCodeMirror ( el , documentCache [ id ] ) ;
156130
157131 this . codemirror . on ( 'change' , this . handleChange ) ;
158132
@@ -281,54 +255,3 @@ export default class CodeEditor extends React.PureComponent {
281255 ) ;
282256 }
283257}
284-
285- const fadeInAnimation = keyframes `
286- 0% { background-color: #374140; }
287- 100% { background-color: #561011; }
288- ` ;
289-
290- injectGlobal `
291- .cm-s-oceanic.CodeMirror {
292- font-family: 'Source Code Pro', monospace;
293- background: ${ theme . background2 ( ) } ;
294- color: #e0e0e0;
295- height: 100%;
296- font-size: 14px;
297- font-weight: 500;
298- }
299- .cm-s-oceanic div.CodeMirror-selected { background: #343D46; }
300- .cm-s-oceanic .CodeMirror-line::selection, .cm-s-oceanic .CodeMirror-line > span::selection, .cm-s-oceanic .CodeMirror-line > span > span::selection { background: #65737E; }
301- .cm-s-oceanic .CodeMirror-line::-moz-selection, .cm-s-oceanic .CodeMirror-line > span::-moz-selection, .cm-s-oceanic .CodeMirror-line > span > span::-moz-selection { background: #65737E; }
302- .cm-s-oceanic .CodeMirror-gutters {
303- background: ${ theme . background2 ( ) } ;
304- border-right: 0px;
305- }
306- .cm-s-oceanic .CodeMirror-guttermarker { color: #ac4142; }
307- .cm-s-oceanic .CodeMirror-guttermarker-subtle { color: #505050; }
308- .cm-s-oceanic .CodeMirror-linenumber { color: #505050; }
309- .cm-s-oceanic .CodeMirror-cursor { border-left: 1px solid #b0b0b0; }
310-
311- .cm-s-oceanic span.cm-comment { color: #8f5536; }
312- .cm-s-oceanic span.cm-atom { color: #aa759f; }
313- .cm-s-oceanic span.cm-number { color: #aa759f; }
314-
315- .cm-s-oceanic span.cm-property, .cm-s-oceanic span.cm-attribute { color: #D8DEE9; }
316- .cm-s-oceanic span.cm-keyword { color: #C594C5; }
317- .cm-s-oceanic span.cm-string { color: #99C794; }
318-
319- .cm-s-oceanic span.cm-variable { color: #FAC863; }
320- .cm-s-oceanic span.cm-variable-2 { color: #6a9fb5; }
321- .cm-s-oceanic span.cm-def { color: #FAC863; }
322- .cm-s-oceanic span.cm-bracket { color: #e0e0e0; }
323- .cm-s-oceanic span.cm-tag { color: #EC5f67; }
324- .cm-s-oceanic span.cm-link { color: #aa759f; }
325- .cm-s-oceanic span.cm-error { background: #ac4142; color: #b0b0b0; }
326-
327- .cm-s-oceanic .CodeMirror-activeline-background { background: #374140; }
328- .cm-s-oceanic .CodeMirror-matchingbracket { text-decoration: underline; color: white !important; }
329- .cm-s-oceanic span.CodeMirror-matchingtag { background-color: inherit; }
330- .cm-s-oceanic span.cm-tag.CodeMirror-matchingtag { text-decoration: underline; }
331- .cm-s-oceanic span.cm-tag.cm-bracket.CodeMirror-matchingtag { text-decoration: none; }
332-
333- .cm-s-oceanic div.cm-line-error.CodeMirror-linebackground { animation: ${ fadeInAnimation } 0.3s; background-color: #561011; }
334- ` ;
0 commit comments