@@ -3,17 +3,10 @@ var Immutable = require('immutable');
3
3
var Draft = require ( 'draft-js' ) ;
4
4
var debounce = require ( 'lodash.debounce' ) ;
5
5
6
- var diff_word_mode = require ( './lib/diff-word-mode' ) ;
6
+ var diff_word_mode = require ( '.. /lib/diff-word-mode' ) ;
7
7
8
8
var DWM = new diff_word_mode ( ) ;
9
9
10
- // diff_match_patch codes
11
- var DIFF = {
12
- DELETE : - 1 ,
13
- INSERT : 1 ,
14
- EQUAL : 0
15
- } ;
16
-
17
10
var DEBOUNCE_WAIT = 300 ; // ms
18
11
var DEBOUNCE_OPTS = {
19
12
trailing : true // We want to update after the delay only
@@ -40,14 +33,14 @@ var DiffArea = React.createClass({
40
33
41
34
// Compute diff on whole texts
42
35
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 ( ) ) ;
45
38
// Update the decorators
46
39
state . leftState = Draft . EditorState . set ( state . leftState , {
47
- decorator : createDiffsDecorator ( mappingLeft , DIFF . REMOVED )
40
+ decorator : createDiffsDecorator ( mappingLeft , diff_word_mode . DIFF_DELETE )
48
41
} ) ;
49
42
state . rightState = Draft . EditorState . set ( state . rightState , {
50
- decorator : createDiffsDecorator ( mappingRight , DIFF . INSERTED )
43
+ decorator : createDiffsDecorator ( mappingRight , diff_word_mode . DIFF_INSERT )
51
44
} ) ;
52
45
53
46
return state ;
@@ -88,15 +81,15 @@ var DiffArea = React.createClass({
88
81
89
82
var diffs = computeDiff ( left , right ) ;
90
83
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 ( ) ) ;
93
86
94
87
// Update the decorators
95
88
newState . leftState = Draft . EditorState . set ( this . state . leftState , {
96
- decorator : createDiffsDecorator ( mappingLeft , DIFF . REMOVED )
89
+ decorator : createDiffsDecorator ( mappingLeft , diff_word_mode . DIFF_DELETE )
97
90
} ) ;
98
91
newState . rightState = Draft . EditorState . set ( this . state . rightState , {
99
- decorator : createDiffsDecorator ( mappingRight , DIFF . INSERTED )
92
+ decorator : createDiffsDecorator ( mappingRight , diff_word_mode . DIFF_INSERT )
100
93
} ) ;
101
94
this . setState ( newState ) ;
102
95
} ,
@@ -140,7 +133,7 @@ function mapDiffsToBlocks(diffs, type, blockMap) {
140
133
diffs . forEach ( function ( diff ) {
141
134
var diffType = diff [ 0 ] ;
142
135
var diffText = diff [ 1 ] ;
143
- if ( diffType === DIFF . EQUAL ) {
136
+ if ( diffType === diff_word_mode . DIFF_EQUAL ) {
144
137
// No highlight. Move to next difference
145
138
charIndex += diffText . length ;
146
139
} else if ( diffType === type ) {
@@ -156,7 +149,13 @@ function mapDiffsToBlocks(diffs, type, blockMap) {
156
149
}
157
150
} ) ;
158
151
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
+ */
160
159
function findRangesBetween ( ranges , start , end ) {
161
160
var res = [ ] ;
162
161
ranges . forEach ( function ( range ) {
@@ -199,7 +198,7 @@ var RemovedSpan = function (props) {
199
198
function createDiffsDecorator ( mappedRanges , type ) {
200
199
return new Draft . CompositeDecorator ( [ {
201
200
strategy : findDiff . bind ( undefined , mappedRanges , type ) ,
202
- component : type === DIFF . INSERTED ? InsertedSpan : RemovedSpan
201
+ component : type === diff_word_mode . DIFF_INSERT ? InsertedSpan : RemovedSpan
203
202
} ] ) ;
204
203
}
205
204
0 commit comments