1- import React , { useState , useCallback } from 'react'
1+ import React , { useState , useCallback , Fragment } from 'react'
22import styled from '@emotion/styled'
33import {
44 Diff as RDiff ,
@@ -11,11 +11,13 @@ import {
1111 ViewType ,
1212 DiffType ,
1313 HunkTokens ,
14+ TokenNode ,
1415} from 'react-diff-view'
1516import DiffHeader from './DiffHeader'
1617import { getComments } from './DiffComment'
1718import { replaceAppDetails } from '../../../utils'
1819import type { Theme } from '../../../theme'
20+ import type { DefaultRenderToken } from 'react-diff-view/types/context'
1921
2022const copyPathPopoverContentOpts = {
2123 default : 'Click to copy file path' ,
@@ -54,8 +56,7 @@ const DiffView = styled(RDiff)<DiffViewProps>`
5456 }
5557
5658 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 } ;
5960 border-color: ${ ( { theme } ) => theme . greenBorder } ;
6061 }
6162
@@ -80,26 +81,25 @@ const DiffView = styled(RDiff)<DiffViewProps>`
8081
8182 // From diff global
8283 .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 } ;
8586 tab-size: 4;
8687 hyphens: none;
8788 }
8889
8990 .diff::selection {
90- background-color: ${ ( { theme } ) => theme . diff . selectionMackground } ;
91+ background-color: ${ ( { theme } ) => theme . diff . selectionBackground } ;
9192 }
9293
9394 .diff-decoration {
9495 line-height: 2;
9596 font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier,
9697 monospace;
97- background-color: ${ ( { theme } ) => theme . diff . decorationBackground } ;
9898 }
9999
100100 .diff-decoration-content {
101101 padding-left: 0.5em;
102- background-color: ${ ( { theme } ) => theme . diff . contentBackground } ;
102+ background-color: ${ ( { theme } ) => theme . diff . decorationContentBackground } ;
103103 color: ${ ( { theme } ) => theme . diff . decorationContent } ;
104104 }
105105
@@ -161,6 +161,27 @@ const isDiffCollapsedByDefault = ({
161161 hunks : HunkData [ ]
162162} ) => ( type === 'delete' || hunks . length > 5 ? true : undefined )
163163
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+
164185interface DiffProps {
165186 packageName : string
166187 oldPath : string
@@ -250,6 +271,15 @@ const Diff = ({
250271 toVersion,
251272 } )
252273
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+
253283 return (
254284 < Container >
255285 < DiffHeader
@@ -285,35 +315,28 @@ const Diff = ({
285315 viewType = { diffViewStyle }
286316 diffType = { type }
287317 hunks = { hunks }
318+ renderToken = { renderToken }
319+ tokens = { tokens }
288320 widgets = { diffComments }
289321 optimizeSelection = { true }
290322 selectedChanges = { selectedChanges }
291323 >
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 } >
303327 { updatedHunks . map ( ( hunk ) => [
304328 < Decoration key = { 'decoration-' + hunk . content } >
305329 < More > { hunk . content } </ More >
306330 </ Decoration > ,
307331 < Hunk
308332 key = { hunk . content }
309333 hunk = { hunk }
310- tokens = { tokens }
311334 gutterEvents = { { onClick : onToggleChangeSelection } }
312335 /> ,
313336 ] ) }
314- </ >
315- )
316- } }
337+ </ Fragment >
338+ ) )
339+ }
317340 </ DiffView >
318341 ) }
319342 </ Container >
0 commit comments