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