Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add options, more compare types and es6 #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "react"]
}
79 changes: 72 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,92 @@
# react-diff
# react-stylable-diff

Highlights differences between two strings, uses the [diff](https://www.npmjs.com/package/diff) module
Output differences between two strings in a stylable form.

Based on [react-diff](https://www.npmjs.com/package/react-diff). Uses the [diff](https://www.npmjs.com/package/diff) module

## Installation

```
npm install react-diff
npm install react-stylable-diff
```

## Usage

Pass text to compare as `props.inputA` and `props.inputB`:

```javascript
var React = require('react');
var Diff = require('react-stylable-diff');

var MyComponent = React.createClass({
render: function() {
return (
<Diff inputA="worst" inputB="blurst"/>
);
}
});
```

You can also specify different values in `props.type`
to compare in different ways. Valid values are `'chars'`, `'words'`, `'wordsWit Space'`, `'lines'`, `'trimmedLines'`, `'sentences'`, `'css'`, and `'json'`. You can also use options (check it in [diff](https://github.com/kpdecker/jsdiff) module):


```javascript
var React = require('react');
var Diff = require('react-stylable-diff');

var MyComponent = React.createClass({
render: function() {
return (
<Diff type="lines"
options={ignoreWhitespace: true, newlineIsToken: true}
inputA="It was the best of times\nIt was the worst of times"
inputB="It was the best of times\n It was the blurst of times\n"/>
);
}
});
```

## Demo
### Styling

Outputs standard `<ins>` and `<del>` tags so you will at least
have the browser default styling for these. On my browser they
appear crossed-out or underlined.

You will probably want to add your own styles to look all fancy.

The output is wrapped in a div with class `'Difference'` so you can
attach all your style rules to that. You can override this class with
`props.className` if you like.

http://cezary.github.io/react-diff/
Here are some styles that might work:

```css
.Difference {
font-family: monospace;
}

.Difference > del {
background-color: rgb(255, 224, 224);
text-decoration: none;
}

.Difference > ins {
background-color: rgb(201, 238, 211);
text-decoration: none;
}
```

## Example

```javascript
var React = require('react');
var Diff = require('react-diff');
var Diff = require('react-stylable-diff');

var Component = React.createClass({
render: function() {
return (
<Diff inputA="gogol" inputB="google" type="chars" />
<Diff inputA="worst" inputB="blurst" type="chars" />
);
}
});
Expand Down
137 changes: 101 additions & 36 deletions dist/react-diff.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,113 @@
'use strict';

var React = require('react');
var jsdiff = require('diff');
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

Object.defineProperty(exports, "__esModule", {
value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _diff = require('diff');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var fnMap = {
chars: jsdiff.diffChars,
words: jsdiff.diffWords,
sentences: jsdiff.diffSentences,
json: jsdiff.diffJson
'chars': _diff.diffChars,
'words': _diff.diffWords,
'wordsWithSpace': _diff.diffWrodsWithSpace,
'lines': _diff.diffLines,
'trimmedLines': _diff.diffTrimmedLines,
'sentences': _diff.diffSentences,
'css': _diff.diffCss,
'json': _diff.diffJson
};

module.exports = React.createClass({
displayName: 'Diff',
/**
* Display diff in a stylable form.
*
* Default is character diff. Change with props.type. Valid values
* are 'chars', 'words', 'sentences', 'json'.
*
* - Wrapping div has class 'Difference', override with props.className
* - added parts are in <ins>
* - removed parts are in <del>
* - unchanged parts are in <span>
*/

var ReactDiff = function (_React$Component) {
_inherits(ReactDiff, _React$Component);

function ReactDiff() {
_classCallCheck(this, ReactDiff);

getDefaultProps: function getDefaultProps() {
return {
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(ReactDiff).call(this));

_this.displayName = 'Diff';

_this.defaultProps = {
inputA: '',
inputB: '',
type: 'chars'
type: 'chars',
options: null,
className: 'difference'
};
},

propTypes: {
inputA: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.object]),
inputB: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.object]),
type: React.PropTypes.oneOf(['chars', 'words', 'sentences', 'json'])
},

render: function render() {
var diff = fnMap[this.props.type](this.props.inputA, this.props.inputB);
var result = diff.map(function (part) {
var spanStyle = {
backgroundColor: part.added ? 'lightgreen' : part.removed ? 'salmon' : 'lightgrey'
};
return React.createElement(
'span',
{ style: spanStyle },
part.value

_this.propTypes = {
inputA: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.object]),
inputB: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.object]),
type: _react2.default.PropTypes.oneOf(['chars', 'words', 'wordsWithSpace', 'lines', 'trimmedLines', 'sentences', 'css', 'json']),
options: _react2.default.PropTypes.object
};
return _this;
}

_createClass(ReactDiff, [{
key: 'render',
value: function render() {
var diff = fnMap[this.props.type](this.props.inputA, this.props.inputB, this.props.options);

var result = diff.map(function (part, index) {
if (part.added) {
return _react2.default.createElement(
'ins',
{ key: index },
part.value
);
}
if (part.removed) {
return _react2.default.createElement(
'del',
{ key: index },
part.value
);
}
return _react2.default.createElement(
'span',
{ key: index },
part.value
);
});

return _react2.default.createElement(
'div',
this.props,
result
);
});
return React.createElement(
'pre',
{ className: 'diff-result' },
result
);
} });
}
}]);

return ReactDiff;
}(_react2.default.Component);

exports.default = ReactDiff;
;

53 changes: 0 additions & 53 deletions lib/react-diff.js

This file was deleted.

81 changes: 81 additions & 0 deletions lib/react-diff.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React from 'react';
import {diffChars, diffWords, diffWrodsWithSpace, diffLines, diffTrimmedLines, diffSentences, diffCss, diffJson} from 'diff';

var fnMap = {
'chars': diffChars,
'words': diffWords,
'wordsWithSpace': diffWrodsWithSpace,
'lines': diffLines,
'trimmedLines': diffTrimmedLines,
'sentences': diffSentences,
'css': diffCss,
'json': diffJson
};

/**
* Display diff in a stylable form.
*
* Default is character diff. Change with props.type. Valid values
* are 'chars', 'words', 'sentences', 'json'.
*
* - Wrapping div has class 'Difference', override with props.className
* - added parts are in <ins>
* - removed parts are in <del>
* - unchanged parts are in <span>
*/
export default class ReactDiff extends React.Component {
constructor() {
super();
this.displayName = 'Diff';

this.defaultProps = {
inputA: '',
inputB: '',
type: 'chars',
options: null,
className: 'difference'
};

this.propTypes = {
inputA: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object
]),
inputB: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object
]),
type: React.PropTypes.oneOf([
'chars',
'words',
'wordsWithSpace',
'lines',
'trimmedLines',
'sentences',
'css',
'json'
]),
options: React.PropTypes.object
}
}

render() {
var diff = fnMap[this.props.type](this.props.inputA, this.props.inputB, this.props.options);

var result = diff.map(function(part, index) {
if (part.added) {
return <ins key={index}>{part.value}</ins>;
}
if (part.removed) {
return <del key={index}>{part.value}</del>;
}
return <span key={index}>{part.value}</span>;
});

return (
<div {...this.props}>
{result}
</div>
);
}
};
Loading