1
1
import { FC , useEffect , useMemo , useRef , useState } from "react" ;
2
- import { Editor } from "@monaco-editor/react" ;
2
+ import { DiffEditor , Editor } from "@monaco-editor/react" ;
3
3
4
4
// import * as monaco from "monaco-editor";
5
5
import {
@@ -14,20 +14,21 @@ const { useToken } = theme;
14
14
const FileCoverageDetail : FC < {
15
15
fileContent : string ;
16
16
fileCodeChange : number [ ] ;
17
+ fileCodeChangeContent : string ;
17
18
fileCoverage : any ;
18
- } > = ( { fileContent, fileCoverage, fileCodeChange } ) => {
19
+ } > = ( { fileContent, fileCoverage, fileCodeChange, fileCodeChangeContent } ) => {
19
20
const { token } = useToken ( ) ;
20
21
const { lines } = coreFn ( fileCoverage , fileContent ) ;
21
22
22
23
const linesState = useMemo ( ( ) => {
23
24
return lines . map ( ( line , index ) => {
24
25
return {
25
26
lineNumber : index + 1 ,
26
- change : fileCodeChange . includes ( index + 1 ) ,
27
+ change : false ,
27
28
hit : line . executionNumber ,
28
29
} ;
29
30
} ) ;
30
- } , [ lines , fileCodeChange ] ) ;
31
+ } , [ lines ] ) ;
31
32
32
33
const lineNumbersMinChars = useMemo ( ( ) => {
33
34
const maxHit = Math . max ( ...linesState . map ( ( line ) => line . hit ) ) ;
@@ -62,7 +63,20 @@ const FileCoverageDetail: FC<{
62
63
}
63
64
useEffect ( ( ) => {
64
65
if ( editor ) {
65
- editor ?. deltaDecorations ?.(
66
+ let needDeltaDecorationsEditor = null ;
67
+
68
+ if ( fileCodeChangeContent ) {
69
+ const originalEditor = editor . getOriginalEditor ( ) ;
70
+ originalEditor . updateOptions ( {
71
+ lineNumbers : false ,
72
+ } ) ;
73
+ const modifiedEditor = editor . getModifiedEditor ( ) ;
74
+ needDeltaDecorationsEditor = modifiedEditor ;
75
+ } else {
76
+ needDeltaDecorationsEditor = editor ;
77
+ }
78
+
79
+ needDeltaDecorationsEditor ?. deltaDecorations ?.(
66
80
[ ] , // oldDecorations 每次清空上次标记的
67
81
decorations . map (
68
82
( { inlineClassName, startLine, startCol, endLine, endCol } ) => ( {
@@ -91,36 +105,70 @@ const FileCoverageDetail: FC<{
91
105
// border: "1px solid " + token.colorBorder,
92
106
} }
93
107
>
94
- < Editor
95
- value = { fileContent }
96
- theme = { token . colorBgBase === "#000" ? "nightOwl" : "vs" }
97
- height = { "calc(100vh - 200px)" }
98
- // height={`${18 * (lineCount + 1)}px`}
99
- language = { "javascript" }
100
- onMount = { handleEditorDidMount }
101
- options = { {
102
- lineHeight : 18 ,
103
- lineNumbers : ( lineNumber ) => {
104
- return lineNumbers (
105
- lineNumber ,
106
- linesState ,
107
- token . colorBgBase === "#000" ,
108
- ) ;
109
- } ,
110
- lineNumbersMinChars : lineNumbersMinChars ,
111
- readOnly : true ,
112
- folding : false ,
113
- minimap : { enabled : false } ,
114
- scrollBeyondLastLine : false ,
115
- showUnused : false ,
116
- fontSize : 12 ,
117
- fontFamily : "IBMPlexMono" ,
118
- scrollbar : {
119
- // handleMouseWheel: false,
120
- } ,
121
- contextmenu : false ,
122
- } }
123
- />
108
+ { fileCodeChangeContent ? (
109
+ < DiffEditor
110
+ original = { fileCodeChangeContent }
111
+ modified = { fileContent }
112
+ // theme={"vs-dark"}
113
+ height = { "calc(100vh - 200px)" }
114
+ // height={`${18 * (lineCount + 1)}px`}
115
+ language = { "javascript" }
116
+ onMount = { handleEditorDidMount }
117
+ options = { {
118
+ lineHeight : 18 ,
119
+ lineNumbers : ( lineNumber ) => {
120
+ return lineNumbers (
121
+ lineNumber ,
122
+ linesState ,
123
+ token . colorBgBase === "#000" ,
124
+ ) ;
125
+ } ,
126
+ lineNumbersMinChars : lineNumbersMinChars ,
127
+ readOnly : true ,
128
+ folding : false ,
129
+ minimap : { enabled : false } ,
130
+ scrollBeyondLastLine : false ,
131
+ showUnused : false ,
132
+ fontSize : 12 ,
133
+ fontFamily : "IBMPlexMono" ,
134
+ scrollbar : {
135
+ // handleMouseWheel: false,
136
+ } ,
137
+ contextmenu : false ,
138
+ } }
139
+ />
140
+ ) : (
141
+ < Editor
142
+ value = { fileContent }
143
+ theme = { token . colorBgBase === "#000" ? "nightOwl" : "vs" }
144
+ height = { "calc(100vh - 200px)" }
145
+ // height={`${18 * (lineCount + 1)}px`}
146
+ language = { "javascript" }
147
+ onMount = { handleEditorDidMount }
148
+ options = { {
149
+ lineHeight : 18 ,
150
+ lineNumbers : ( lineNumber ) => {
151
+ return lineNumbers (
152
+ lineNumber ,
153
+ linesState ,
154
+ token . colorBgBase === "#000" ,
155
+ ) ;
156
+ } ,
157
+ lineNumbersMinChars : lineNumbersMinChars ,
158
+ readOnly : true ,
159
+ folding : false ,
160
+ minimap : { enabled : false } ,
161
+ scrollBeyondLastLine : false ,
162
+ showUnused : false ,
163
+ fontSize : 12 ,
164
+ fontFamily : "IBMPlexMono" ,
165
+ scrollbar : {
166
+ // handleMouseWheel: false,
167
+ } ,
168
+ contextmenu : false ,
169
+ } }
170
+ />
171
+ ) }
124
172
</ div >
125
173
) ;
126
174
} ;
0 commit comments