Skip to content

Commit 343d30d

Browse files
committed
Fix bugs
1 parent 907062f commit 343d30d

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

demo/diff-area.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@ var Immutable = require('immutable');
33
var Draft = require('draft-js');
44
var debounce = require('lodash.debounce');
55

6-
var diff_word_mode = require('./lib/diff-word-mode');
6+
var diff_word_mode = require('../lib/diff-word-mode');
77

88
var DWM = new diff_word_mode();
99

10-
// diff_match_patch codes
11-
var DIFF = {
12-
DELETE: -1,
13-
INSERT: 1,
14-
EQUAL: 0
15-
};
16-
1710
var DEBOUNCE_WAIT = 300; // ms
1811
var DEBOUNCE_OPTS = {
1912
trailing: true // We want to update after the delay only
@@ -40,14 +33,14 @@ var DiffArea = React.createClass({
4033

4134
// Compute diff on whole texts
4235
var diffs = computeDiff(left, right);
43-
var mappingLeft = mapDiffsToBlocks(diffs, DIFF.REMOVED, state.leftState.getCurrentContent().getBlockMap());
44-
var mappingRight = mapDiffsToBlocks(diffs, DIFF.INSERTED, state.rightState.getCurrentContent().getBlockMap());
36+
var mappingLeft = mapDiffsToBlocks(diffs, diff_word_mode.DIFF_DELETE, state.leftState.getCurrentContent().getBlockMap());
37+
var mappingRight = mapDiffsToBlocks(diffs, diff_word_mode.DIFF_INSERT, state.rightState.getCurrentContent().getBlockMap());
4538
// Update the decorators
4639
state.leftState = Draft.EditorState.set(state.leftState, {
47-
decorator: createDiffsDecorator(mappingLeft, DIFF.REMOVED)
40+
decorator: createDiffsDecorator(mappingLeft, diff_word_mode.DIFF_DELETE)
4841
});
4942
state.rightState = Draft.EditorState.set(state.rightState, {
50-
decorator: createDiffsDecorator(mappingRight, DIFF.INSERTED)
43+
decorator: createDiffsDecorator(mappingRight, diff_word_mode.DIFF_INSERT)
5144
});
5245

5346
return state;
@@ -88,15 +81,15 @@ var DiffArea = React.createClass({
8881

8982
var diffs = computeDiff(left, right);
9083

91-
var mappingLeft = mapDiffsToBlocks(diffs, DIFF.REMOVED, this.state.leftState.getCurrentContent().getBlockMap());
92-
var mappingRight = mapDiffsToBlocks(diffs, DIFF.INSERTED, this.state.rightState.getCurrentContent().getBlockMap());
84+
var mappingLeft = mapDiffsToBlocks(diffs, diff_word_mode.DIFF_DELETE, this.state.leftState.getCurrentContent().getBlockMap());
85+
var mappingRight = mapDiffsToBlocks(diffs, diff_word_mode.DIFF_INSERT, this.state.rightState.getCurrentContent().getBlockMap());
9386

9487
// Update the decorators
9588
newState.leftState = Draft.EditorState.set(this.state.leftState, {
96-
decorator: createDiffsDecorator(mappingLeft, DIFF.REMOVED)
89+
decorator: createDiffsDecorator(mappingLeft, diff_word_mode.DIFF_DELETE)
9790
});
9891
newState.rightState = Draft.EditorState.set(this.state.rightState, {
99-
decorator: createDiffsDecorator(mappingRight, DIFF.INSERTED)
92+
decorator: createDiffsDecorator(mappingRight, diff_word_mode.DIFF_INSERT)
10093
});
10194
this.setState(newState);
10295
},
@@ -140,7 +133,7 @@ function mapDiffsToBlocks(diffs, type, blockMap) {
140133
diffs.forEach(function (diff) {
141134
var diffType = diff[0];
142135
var diffText = diff[1];
143-
if (diffType === DIFF.EQUAL) {
136+
if (diffType === diff_word_mode.DIFF_EQUAL) {
144137
// No highlight. Move to next difference
145138
charIndex += diffText.length;
146139
} else if (diffType === type) {
@@ -156,7 +149,13 @@ function mapDiffsToBlocks(diffs, type, blockMap) {
156149
}
157150
});
158151

159-
// `end` excluded
152+
/**
153+
* @param {Array<Range>} ranges
154+
* @param {Number} start
155+
* @param {Number} end
156+
* @return {Array<Range>} All the ranges that overlapped
157+
* start-end, cropped and re-indexed to be relative to start.
158+
*/
160159
function findRangesBetween(ranges, start, end) {
161160
var res = [];
162161
ranges.forEach(function (range) {
@@ -199,7 +198,7 @@ var RemovedSpan = function (props) {
199198
function createDiffsDecorator(mappedRanges, type) {
200199
return new Draft.CompositeDecorator([{
201200
strategy: findDiff.bind(undefined, mappedRanges, type),
202-
component: type === DIFF.INSERTED ? InsertedSpan : RemovedSpan
201+
component: type === diff_word_mode.DIFF_INSERT ? InsertedSpan : RemovedSpan
203202
}]);
204203
}
205204

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
<body>
99
<div id="content"></div>
10-
<script type="text/javascript" src="bundle.js"></script>
10+
<script src="bundle.js"></script>
1111
</body>
1212
</html>

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
{
23
"name": "draft-js-diff",
34
"version": "0.0.1",
@@ -7,8 +8,8 @@
78
"build": "browserify -t [ babelify --presets [ react ] ] ./demo/main.js -o ./demo/bundle.js && cp ./node_modules/draft-js/dist/Draft.css ./demo/draft.css",
89
"deploy": "npm run build && gh-pages -d ./demo",
910
"watch": "watch 'npm run build' ./",
10-
"serve": "http-server -p 9090 -c-1 demo/",
11-
"start": "npm run build && parallelshell 'npm run serve -s' 'npm run watch -s'"
11+
"serve": "http-server -p 9090 demo/",
12+
"start": "npm run build && npm run serve -s"
1213
},
1314
"repository": {
1415
"type": "git",

0 commit comments

Comments
 (0)