1
- require ( './style.scss' ) ;
2
1
import * as React from 'react' ;
3
2
import * as ReactDOM from 'react-dom' ;
4
-
5
3
import ReactDiff , { DiffMethod } from '../../lib/index' ;
6
-
4
+ import CommentBlock from './CommentBlock' ;
5
+ import { CommentInfo } from '../../lib/index' ;
7
6
const oldJs = require ( './diff/javascript/old.rjs' ) . default ;
8
7
const newJs = require ( './diff/javascript/new.rjs' ) . default ;
9
-
10
8
const logo = require ( '../../logo.png' ) ;
9
+ require ( './style.scss' ) ;
11
10
12
11
interface ExampleState {
13
12
splitView ?: boolean ;
14
13
highlightLine ?: string [ ] ;
15
14
language ?: string ;
16
15
enableSyntaxHighlighting ?: boolean ;
17
16
compareMethod ?: DiffMethod ;
17
+ comments : any [ ] ;
18
18
}
19
19
20
20
const P = ( window as any ) . Prism ;
@@ -25,13 +25,27 @@ class Example extends React.Component<{}, ExampleState> {
25
25
this . state = {
26
26
highlightLine : [ ] ,
27
27
enableSyntaxHighlighting : true ,
28
+ comments : [
29
+ {
30
+ body : {
31
+ lineId : 'L-12-beforeCommit-afterCommit-test/test.jsx' ,
32
+ text : 'Awesome\ncomment!' ,
33
+ fileId : 'test/test.jsx' ,
34
+ prefix : 'L' ,
35
+ lineNumber : 12 ,
36
+ specifier : 'beforeCommit-afterCommit'
37
+ }
38
+ }
39
+ ]
28
40
} ;
29
41
}
30
42
31
43
private onLineNumberClick = (
32
44
id : string ,
33
- e : React . MouseEvent < HTMLTableCellElement > ,
45
+ uiniqueLindeId : string ,
46
+ e : React . MouseEvent < HTMLTableCellElement >
34
47
) : void => {
48
+ console . log ( uiniqueLindeId ) ;
35
49
let highlightLine = [ id ] ;
36
50
if ( e . shiftKey && this . state . highlightLine . length === 1 ) {
37
51
const [ dir , oldId ] = this . state . highlightLine [ 0 ] . split ( '-' ) ;
@@ -46,45 +60,99 @@ class Example extends React.Component<{}, ExampleState> {
46
60
}
47
61
}
48
62
this . setState ( {
49
- highlightLine,
63
+ highlightLine
50
64
} ) ;
51
65
} ;
52
66
53
- private syntaxHighlight = ( str : string ) : any => {
54
- if ( ! str ) return ;
55
- const language = P . highlight ( str , P . languages . javascript ) ;
56
- return < span dangerouslySetInnerHTML = { { __html : language } } /> ;
67
+ private syntaxHighlight = ( source : string , lineId ? : string ) : any => {
68
+ if ( ! source ) return ;
69
+ const language = P . highlight ( source , P . languages . javascript ) ;
70
+ return < span id = { lineId } dangerouslySetInnerHTML = { { __html : language } } /> ;
57
71
} ;
58
72
59
- public render ( ) : JSX . Element {
73
+ private updateComment = ( commentInfo : any , text ?: string ) => {
74
+ const updatedComments = this . state . comments . map ( comment => {
75
+ console . log ( comment ) ;
76
+ if ( comment . lineId === commentInfo . lineId ) {
77
+ return {
78
+ ...commentInfo ,
79
+ lineId : commentInfo . uniqueLineId ,
80
+ body : text
81
+ } ;
82
+ }
83
+ return comment ;
84
+ } ) ;
85
+
86
+ this . setState ( {
87
+ comments : updatedComments
88
+ } ) ;
89
+ } ;
90
+
91
+ private removeComment = ( lineId : string ) => {
92
+ const updatedComments = this . state . comments . filter (
93
+ comment => comment . lineId !== lineId
94
+ ) ;
95
+ this . setState ( { comments : updatedComments } ) ;
96
+ } ;
97
+
98
+ private createComment = ( commentInfo : CommentInfo ) => {
99
+ const updatedComments = [
100
+ ...this . state . comments ,
101
+ {
102
+ body : {
103
+ ...commentInfo ,
104
+ lineId : commentInfo . lineId ,
105
+ text : ''
106
+ }
107
+ }
108
+ ] ;
109
+ this . setState ( { comments : updatedComments } ) ;
110
+ } ;
111
+
112
+ /**
113
+ *
114
+ * helper that return Array with uniqueLineIds (comment.lineId)
115
+ *
116
+ * @param arr Array with commentLineIds
117
+ *
118
+ */
60
119
120
+ private getlineIdsArray = ( arr : any [ ] ) => {
121
+ return arr . reduce ( ( acc : Array < string > , comment ) => {
122
+ acc . push ( comment . body . lineId ) ;
123
+ return acc ;
124
+ } , [ ] ) ;
125
+ } ;
126
+
127
+ public render ( ) : JSX . Element {
61
128
return (
62
- < div className = " react-diff-viewer-example" >
63
- < div className = " radial" > </ div >
64
- < div className = " banner" >
65
- < div className = " img-container" >
66
- < img src = { logo } alt = " React Diff Viewer Logo" />
129
+ < div className = ' react-diff-viewer-example' >
130
+ < div className = ' radial' > </ div >
131
+ < div className = ' banner' >
132
+ < div className = ' img-container' >
133
+ < img src = { logo } alt = ' React Diff Viewer Logo' />
67
134
</ div >
68
135
< p >
69
136
A simple and beautiful text diff viewer made with{ ' ' }
70
- < a href = " https://github.com/kpdecker/jsdiff" target = " _blank" >
137
+ < a href = ' https://github.com/kpdecker/jsdiff' target = ' _blank' >
71
138
Diff{ ' ' }
72
139
</ a >
73
140
and{ ' ' }
74
- < a href = " https://reactjs.org" target = " _blank" >
141
+ < a href = ' https://reactjs.org' target = ' _blank' >
75
142
React.{ ' ' }
76
143
</ a >
77
- Featuring split view, inline view, word diff, line highlight and more.
144
+ Featuring split view, inline view, word diff, line highlight and
145
+ more.
78
146
</ p >
79
- < div className = " cta" >
80
- < a href = " https://github.com/praneshr/react-diff-viewer#install" >
81
- < button type = " button" className = " btn btn-primary btn-lg" >
147
+ < div className = ' cta' >
148
+ < a href = ' https://github.com/praneshr/react-diff-viewer#install' >
149
+ < button type = ' button' className = ' btn btn-primary btn-lg' >
82
150
Documentation
83
151
</ button >
84
152
</ a >
85
153
</ div >
86
154
</ div >
87
- < div className = " diff-viewer" >
155
+ < div className = ' diff-viewer' >
88
156
< ReactDiff
89
157
highlightLines = { this . state . highlightLine }
90
158
onLineNumberClick = { this . onLineNumberClick }
@@ -93,13 +161,32 @@ class Example extends React.Component<{}, ExampleState> {
93
161
newValue = { newJs }
94
162
renderContent = { this . syntaxHighlight }
95
163
useDarkTheme
96
- leftTitle = "webpack.config.js master@2178133 - pushed 2 hours ago."
97
- rightTitle = "webpack.config.js master@64207ee - pushed 13 hours ago."
164
+ leftTitle = 'webpack.config.js master@2178133 - pushed 2 hours ago.'
165
+ rightTitle = 'webpack.config.js master@64207ee - pushed 13 hours ago.'
166
+ afterCommit = { 'afterCommit' }
167
+ beforeCommit = { 'beforeCommit' }
168
+ commentLineIds = { this . getlineIdsArray ( this . state . comments ) }
169
+ getCommentInfo = { commentInfo => this . createComment ( commentInfo ) }
170
+ renderCommentBlock = { commentInfo => {
171
+ console . log ( commentInfo ) ;
172
+ const currComment = this . state . comments . find (
173
+ comment => comment . body . lineId === commentInfo . lineId
174
+ ) ;
175
+ return (
176
+ < CommentBlock
177
+ updateComment = { this . updateComment }
178
+ removeComment = { this . removeComment }
179
+ comment = { currComment }
180
+ show = { ! ! currComment . body . text }
181
+ />
182
+ ) ;
183
+ } }
184
+ fileId = { 'test/test.jsx' }
98
185
/>
99
186
</ div >
100
187
< footer >
101
188
Made with 💓 by{ ' ' }
102
- < a href = " https://praneshravi.in" target = " _blank" >
189
+ < a href = ' https://praneshravi.in' target = ' _blank' >
103
190
Pranesh Ravi
104
191
</ a >
105
192
</ footer >
0 commit comments