1
1
import React from 'react' ;
2
2
import { CommonEditorProps , Position } from '../types' ;
3
3
import MonacoReact , { Monaco } from "@monaco-editor/react" ;
4
+ import { connect } from 'react-redux' ;
5
+ import State from '../state' ;
4
6
5
7
import styles from './Editor.module.css' ;
6
8
import { config , grammar } from './rust_monaco_def' ;
7
9
8
- class CodeByteOffsets {
9
- readonly code : string ;
10
- readonly lines : string [ ] ;
11
-
12
- constructor ( code : string ) {
13
- this . code = code ;
14
- this . lines = code . split ( '\n' ) ;
15
- }
16
-
17
- public lineToOffsets ( line : number ) {
18
- const precedingBytes = this . bytesBeforeLine ( line ) ;
19
-
20
- const highlightedLine = this . lines [ line ] ;
21
- const highlightedBytes = highlightedLine . length ;
22
-
23
- return [ precedingBytes , precedingBytes + highlightedBytes ] ;
24
- }
25
-
26
- public rangeToOffsets ( start : Position , end : Position ) {
27
- const startBytes = this . positionToBytes ( start ) ;
28
- const endBytes = this . positionToBytes ( end ) ;
29
- return [ startBytes , endBytes ] ;
30
- }
31
-
32
- private positionToBytes ( position : Position ) {
33
- // Subtract one as this logic is zero-based and the columns are one-based
34
- return this . bytesBeforeLine ( position . line ) + position . column - 1 ;
35
- }
36
-
37
- private bytesBeforeLine ( line : number ) {
38
- // Subtract one as this logic is zero-based and the lines are one-based
39
- line -= 1 ;
40
-
41
- const precedingLines = this . lines . slice ( 0 , line ) ;
42
-
43
- // Add one to account for the newline we split on and removed
44
- return precedingLines . map ( l => l . length + 1 ) . reduce ( ( a , b ) => a + b ) ;
45
- }
46
- }
47
-
48
10
const modeId = 'my-rust' ;
49
11
50
12
const initMonaco = ( monaco : Monaco ) => {
@@ -62,18 +24,28 @@ const initMonaco = (monaco: Monaco) => {
62
24
} ) ;
63
25
64
26
monaco . languages . onLanguage ( modeId , async ( ) => {
65
- console . log ( modeId ) ;
66
-
67
27
monaco . languages . setLanguageConfiguration ( modeId , config ) ;
68
28
monaco . languages . setMonarchTokensProvider ( modeId , grammar ) ;
69
29
} ) ;
70
30
} ;
71
31
72
- const MonacoEditor : React . SFC < CommonEditorProps > = props => {
32
+ interface PropsFromState {
33
+ theme : string ;
34
+ }
35
+
36
+ const mapStateToProps = ( state : State ) => {
37
+ const { configuration : { monacoTheme : theme , } } = state ;
38
+ return { theme } ;
39
+ } ;
40
+
41
+
42
+ type MonacoEditorProps = CommonEditorProps & PropsFromState ;
43
+
44
+ const MonacoEditor : React . SFC < MonacoEditorProps > = props => {
73
45
return (
74
46
< MonacoReact
75
47
language = { modeId }
76
- theme = "vscode-dark-plus"
48
+ theme = { props . theme }
77
49
loading = "Loading the Monaco editor..."
78
50
className = { styles . advanced }
79
51
value = { props . code }
@@ -83,4 +55,4 @@ const MonacoEditor: React.SFC<CommonEditorProps> = props => {
83
55
) ;
84
56
}
85
57
86
- export default MonacoEditor ;
58
+ export default connect < PropsFromState , undefined , CommonEditorProps > ( mapStateToProps ) ( MonacoEditor ) ;
0 commit comments