1
- import React , { useState , useCallback } from 'react'
1
+ import React , { useState , useCallback , Fragment } from 'react'
2
2
import styled from '@emotion/styled'
3
3
import {
4
4
Diff as RDiff ,
@@ -11,11 +11,13 @@ import {
11
11
ViewType ,
12
12
DiffType ,
13
13
HunkTokens ,
14
+ TokenNode ,
14
15
} from 'react-diff-view'
15
16
import DiffHeader from './DiffHeader'
16
17
import { getComments } from './DiffComment'
17
18
import { replaceAppDetails } from '../../../utils'
18
19
import type { Theme } from '../../../theme'
20
+ import type { DefaultRenderToken } from 'react-diff-view/types/context'
19
21
20
22
const copyPathPopoverContentOpts = {
21
23
default : 'Click to copy file path' ,
@@ -54,8 +56,7 @@ const DiffView = styled(RDiff)<DiffViewProps>`
54
56
}
55
57
56
58
td.diff-gutter .diff-line-normal {
57
- background-color: ${ ( { theme } ) => theme . gutterInsertBackground } ;
58
- // background-color: ${ ( { theme } ) => theme . diff . gutterInsertBackground } ;
59
+ background-color: ${ ( { theme } ) => theme . diff . gutterInsertBackground } ;
59
60
border-color: ${ ( { theme } ) => theme . greenBorder } ;
60
61
}
61
62
@@ -80,26 +81,25 @@ const DiffView = styled(RDiff)<DiffViewProps>`
80
81
81
82
// From diff global
82
83
.diff {
83
- background-color: ${ ( { theme } ) => theme . diff . backgroundColor } ;
84
- color: ${ ( { theme } ) => theme . diff . text } ;
84
+ background-color: ${ ( { theme } ) => theme . background } ;
85
+ color: ${ ( { theme } ) => theme . text } ;
85
86
tab-size: 4;
86
87
hyphens: none;
87
88
}
88
89
89
90
.diff::selection {
90
- background-color: ${ ( { theme } ) => theme . diff . selectionMackground } ;
91
+ background-color: ${ ( { theme } ) => theme . diff . selectionBackground } ;
91
92
}
92
93
93
94
.diff-decoration {
94
95
line-height: 2;
95
96
font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier,
96
97
monospace;
97
- background-color: ${ ( { theme } ) => theme . diff . decorationBackground } ;
98
98
}
99
99
100
100
.diff-decoration-content {
101
101
padding-left: 0.5em;
102
- background-color: ${ ( { theme } ) => theme . diff . contentBackground } ;
102
+ background-color: ${ ( { theme } ) => theme . diff . decorationContentBackground } ;
103
103
color: ${ ( { theme } ) => theme . diff . decorationContent } ;
104
104
}
105
105
@@ -161,6 +161,27 @@ const isDiffCollapsedByDefault = ({
161
161
hunks : HunkData [ ]
162
162
} ) => ( type === 'delete' || hunks . length > 5 ? true : undefined )
163
163
164
+ const renderToken = (
165
+ token : TokenNode ,
166
+ renderDefault : DefaultRenderToken ,
167
+ index : number
168
+ ) => {
169
+ switch ( token . type ) {
170
+ case 'space' :
171
+ console . log ( token )
172
+ return (
173
+ < span key = { index } className = "space" >
174
+ { token . children &&
175
+ token . children . map ( ( token , index ) =>
176
+ renderToken ( token , renderDefault , index )
177
+ ) }
178
+ </ span >
179
+ )
180
+ default :
181
+ return renderDefault ( token , index )
182
+ }
183
+ }
184
+
164
185
interface DiffProps {
165
186
packageName : string
166
187
oldPath : string
@@ -250,6 +271,15 @@ const Diff = ({
250
271
toVersion,
251
272
} )
252
273
274
+ const updatedHunks = React . useMemo ( ( ) => getHunksWithAppName ( hunks ) , [ hunks ] )
275
+ const tokens : HunkTokens = React . useMemo (
276
+ ( ) =>
277
+ tokenize ( hunks , {
278
+ enhancers : [ markEdits ( updatedHunks ) ] ,
279
+ } ) ,
280
+ [ hunks , updatedHunks ]
281
+ )
282
+
253
283
return (
254
284
< Container >
255
285
< DiffHeader
@@ -285,35 +315,28 @@ const Diff = ({
285
315
viewType = { diffViewStyle }
286
316
diffType = { type }
287
317
hunks = { hunks }
318
+ renderToken = { renderToken }
319
+ tokens = { tokens }
288
320
widgets = { diffComments }
289
321
optimizeSelection = { true }
290
322
selectedChanges = { selectedChanges }
291
323
>
292
- { ( originalHunks : HunkData [ ] ) => {
293
- const updatedHunks = getHunksWithAppName ( originalHunks )
294
-
295
- const options = {
296
- enhancers : [ markEdits ( updatedHunks ) ] ,
297
- }
298
-
299
- const tokens : HunkTokens = tokenize ( updatedHunks , options )
300
-
301
- return (
302
- < >
324
+ { ( hunks : HunkData [ ] ) =>
325
+ hunks . map ( ( hunk ) => (
326
+ < Fragment key = { hunk . content } >
303
327
{ updatedHunks . map ( ( hunk ) => [
304
328
< Decoration key = { 'decoration-' + hunk . content } >
305
329
< More > { hunk . content } </ More >
306
330
</ Decoration > ,
307
331
< Hunk
308
332
key = { hunk . content }
309
333
hunk = { hunk }
310
- tokens = { tokens }
311
334
gutterEvents = { { onClick : onToggleChangeSelection } }
312
335
/> ,
313
336
] ) }
314
- </ >
315
- )
316
- } }
337
+ </ Fragment >
338
+ ) )
339
+ }
317
340
</ DiffView >
318
341
) }
319
342
</ Container >
0 commit comments