Skip to content

Commit ff778f1

Browse files
committed
initial release
1 parent 0f73f85 commit ff778f1

File tree

11 files changed

+83
-27
lines changed

11 files changed

+83
-27
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
# diffview
22
React component to display unified diffs.
3+
4+
## TODO
5+
* [x] support unified diff
6+
* [ ] fix parse-diff to support single patch (e.g. fetched from github) without diff header
7+
* [ ] split view
8+
* [ ] github styles

demo/app.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,37 @@ import React, { Component } from 'react';
22
import { Grid, Row, Col } from 'react-bootstrap';
33
import DiffView from '../src';
44
import qwest from 'qwest';
5+
import _ from 'lodash';
56

67
const contentLinks = [
8+
{
9+
url: 'https://api.github.com/repos/reactbits/diffview/commits/0f73f850f7bebfc90ee641501eed7889d6f11b45',
10+
headers: {
11+
accept: 'application/vnd.github.diff',
12+
},
13+
label: 'github',
14+
},
715
{
816
url: '/content/simple.diff',
9-
label: 'Simple',
17+
label: 'simple',
1018
},
1119
];
1220

1321
export default class App extends Component {
1422
constructor(props) {
1523
super(props);
1624
this.state = { content: '' };
17-
this.load(contentLinks[0].url);
25+
this.load(contentLinks[0]);
1826
}
1927

20-
load(url) {
21-
qwest.get(url).then((xhr, content) => {
22-
this.setState({ content });
28+
load(data) {
29+
const options = { cache: true, headers: data.headers };
30+
qwest.get(data.url, null, options).then((xhr, content) => {
31+
if (_.isObject(content) && _.isArray(content.files)) {
32+
this.setState({ content: content.files });
33+
} else {
34+
this.setState({ content });
35+
}
2336
});
2437
}
2538

@@ -30,7 +43,7 @@ export default class App extends Component {
3043
href: t.url,
3144
onClick: (e) => {
3245
e.preventDefault();
33-
this.load(t.url);
46+
this.load(t);
3447
return false;
3548
},
3649
style: {
@@ -39,12 +52,20 @@ export default class App extends Component {
3952
};
4053
return <a {...linkProps}>{t.label}</a>;
4154
});
55+
let content = null;
56+
if (_.isArray(this.state.content)) {
57+
content = this.state.content.map(file => {
58+
return <DiffView key={file.filename} source={file.patch}/>;
59+
});
60+
} else {
61+
content = <DiffView source={this.state.content}/>;
62+
}
4263
return (
4364
<Grid className="app">
4465
<Row>
4566
<Col md={8}>
4667
<div>{items}</div>
47-
<DiffView source={this.state.content}/>
68+
{content}
4869
</Col>
4970
</Row>
5071
</Grid>

lib/change.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var _react = require('react');
99

1010
var _react2 = _interopRequireDefault(_react);
1111

12+
var _classnames = require('classnames');
13+
14+
var _classnames2 = _interopRequireDefault(_classnames);
15+
1216
var _style = require('./style');
1317

1418
var _style2 = _interopRequireDefault(_style);
@@ -24,16 +28,24 @@ function Change(props) {
2428
var ln2 = props.normal ? props.ln2 : props.ln;
2529

2630
var html = props.content;
31+
var contentCell = null;
2732

2833
try {
2934
html = (0, _prismjsPackage2.default)(props.content, props.lang);
35+
contentCell = _react2.default.createElement('td', { dangerouslySetInnerHTML: { __html: html } });
3036
} catch (e) {
3137
console.log('highlight error:', e);
38+
contentCell = _react2.default.createElement(
39+
'td',
40+
null,
41+
_react2.default.createElement('pre', { dangerouslySetInnerHTML: { __html: html } })
42+
);
3243
}
3344

45+
var className = (0, _classnames2.default)(_style2.default[props.type], _style2.default.change);
3446
return _react2.default.createElement(
3547
'tr',
36-
{ className: props.type },
48+
{ className: className },
3749
_react2.default.createElement(
3850
'td',
3951
{ className: _style2.default.nostretch },
@@ -44,10 +56,6 @@ function Change(props) {
4456
{ className: _style2.default.nostretch },
4557
!props.del && ln2
4658
),
47-
_react2.default.createElement(
48-
'td',
49-
null,
50-
_react2.default.createElement('pre', { dangerouslySetInnerHTML: { __html: html } })
51-
)
59+
contentCell
5260
);
5361
}

lib/chunk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ var _style2 = _interopRequireDefault(_style);
2222
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2323

2424
function Chunk(props) {
25-
var changes = props.changes.map(function (change) {
26-
return _react2.default.createElement(_change2.default, _extends({}, change, { lang: props.lang }));
25+
var changes = props.changes.map(function (change, i) {
26+
return _react2.default.createElement(_change2.default, _extends({ key: i }, change, { lang: props.lang }));
2727
});
2828
return _react2.default.createElement(
2929
'tbody',

lib/diffview.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function Part(props) {
4141
var ext = (0, _path.extname)(fileName);
4242
var langs = (0, _langMap.languages)(ext);
4343

44-
var items = chunks.map(function (chunk) {
45-
return _react2.default.createElement(_chunk2.default, _extends({}, chunk, { lang: langs[0] }));
44+
var items = chunks.map(function (chunk, i) {
45+
return _react2.default.createElement(_chunk2.default, _extends({ key: i }, chunk, { lang: langs[0] }));
4646
});
4747

4848
return _react2.default.createElement(

lib/style.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
color: #bbb;
6161
}
6262

63+
.diff .change pre {
64+
margin: 0;
65+
padding: 0;
66+
border: none;
67+
background-color: transparent;
68+
}
69+
6370
.diff .add {
6471
background-color: #dbffdb;
6572
font-weight: 600;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-diffview",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "React component to display unified diffs.",
55
"main": "lib/index.js",
66
"scripts": {
@@ -29,7 +29,7 @@
2929
"lang-map": "^0.4.0",
3030
"lodash": "^4.0.0",
3131
"parse-diff": "^0.3.2",
32-
"prismjs-package": "^0.6.1",
32+
"prismjs-package": "^0.6.2",
3333
"react": "^0.14.6",
3434
"react-dom": "^0.14.6"
3535
},

src/change.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import classNames from 'classnames';
23
import style from './style';
34
import highlight from 'prismjs-package';
45

@@ -7,20 +8,26 @@ export default function Change(props) {
78
const ln2 = props.normal ? props.ln2 : props.ln;
89

910
let html = props.content;
11+
let contentCell = null;
1012

1113
try {
1214
html = highlight(props.content, props.lang);
15+
contentCell = <td dangerouslySetInnerHTML={{ __html: html }}/>;
1316
} catch (e) {
1417
console.log('highlight error:', e);
18+
contentCell = (
19+
<td>
20+
<pre dangerouslySetInnerHTML={{ __html: html }}/>
21+
</td>
22+
);
1523
}
1624

25+
const className = classNames(style[props.type], style.change);
1726
return (
18-
<tr className={props.type}>
27+
<tr className={className}>
1928
<td className={style.nostretch}>{!props.add && ln1}</td>
2029
<td className={style.nostretch}>{!props.del && ln2}</td>
21-
<td>
22-
<pre dangerouslySetInnerHTML={{ __html: html }} />
23-
</td>
30+
{contentCell}
2431
</tr>
2532
);
2633
}

src/chunk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import Change from './change';
33
import style from './style';
44

55
export default function Chunk(props) {
6-
const changes = props.changes.map(change => {
7-
return <Change {...change} lang={props.lang}/>;
6+
const changes = props.changes.map((change, i) => {
7+
return <Change key={i} {...change} lang={props.lang}/>;
88
});
99
return (
1010
<tbody>

src/diffview.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ function Part(props) {
1212
const ext = extname(fileName);
1313
const langs = languages(ext);
1414

15-
const items = chunks.map(chunk => {
16-
return <Chunk {...chunk} lang={langs[0]}/>;
15+
const items = chunks.map((chunk, i) => {
16+
return <Chunk key={i} {...chunk} lang={langs[0]}/>;
1717
});
1818

1919
return (

0 commit comments

Comments
 (0)