Draftjs-diff
![NPM Version](https://badge.fury.io/js/draftjs-diff.svg)
Create side-by-side text editors with highlighted diffs, using DraftJS.
Table of content:
Demo
Live demo here
Or you can serve the demo locally by cloning this repository and:
> npm install
> npm run start
... then visit http://127.0.0.1:9090
Usage
Adds the NPM package as dependency, then require:
import { DiffEditor } from 'draft-js-diff';
Using the DiffEditor
You can use the base React component shown in the demo to simply display two side-by-side editors with highlighted differences:
ReactDOM.render(
<DiffEditor
before={before}
after={after}
>
</DiffEditor>,
document.getElementById('target')
);
Differences will be enclosed in span with classes so you can apply styling on it:
.diff-delete {
background-color: #fee
}
.diff-equal {
background-color: #ffe
}
.diff-insert {
background-color: #efe
}
Be sure to include the DraftJS stylesheet too.
Creating CompositeDecorator strategies
You don't have to use the demo DiffEditor
, you can just create decorators and use them for your own Draft.Editor
. To do so, you need to create decorator strategies after diffing both texts. The source code of the DiffEditor
is a good example of this.
Computing the diffs
import { diffWordMode } from 'draft-js-diff';
const diffs = diffWordMode(oldText, newText);
Creating strategies for the diffs
From an array of diff, you can create strategies for a CompositeDecorator. Strategies are different for the editor containing the old text and the editor with the new text. And they will only work if the editors contain the whole old or new text. So you need to generate strategies for both editors.
import { diffWordMode } from 'draft-js-diff';
const oldTextStrategies = diffDecoratorStrategies(diffs, false, blockMap1);
const newTextStrategies = diffDecoratorStrategies(diffs, true, blockMap2);
newTextStrategies.getEqualStrategy()
newTextStrategies.getInsertStrategy()
newTextStrategies.getDeleteStrategy()
Creating decorators
Here is an example of decorator, based on the created strategies. Just set this decorator on the EditorState used to create the strategies.
When content change
When the texts changed (and the diffs too), you need to re-create strategies from the new diff. That's a limitation of using decorators, they are only aware of the blocks they decorate, and not the whole texts, so you need to create them anew to update the diffs.
Shortcomings
Can't do more than word-level diffing
We cannot make diffs at a character level because the created spans mess up with the edition (see https://github.com/facebook/draft-js/issues/414). Instead, we limit ourselves to diffs at a word level diffs. That's why we provide a word-level diffing implementation based on the diff_match_patch
library, which originally works at a character level.
Diffing is costly
Everytime one of the diffed text changes, we need to compute a whole new diff (in the future, we could work on optimizing this depending on the kind of change).
Here are rough order of magnitudes for the diff_match_patch
algorithm with default options.
Characters count | Diffs count | Time (ms) |
---|
1000 (~5 paragraph) | 40 | 1-5 |
6000 (~30 paragraphs) | 300 | 60 |
As the texts grow, the editing becomes laggy. You might want to stop trying to re-compute the diffs as the user types, and instead delay this calculation, for example using debouncing.
API Reference
DiffEditor
DraftDiff.DiffEditor
diffWordMode
DraftDiff.diffWordMode = function (oldText, newText)
diffDecoratorStrategies
DraftDiff.diffDecoratorStrategies = function (diffs, forNewText, blockMap)