-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdiffEditor.js
219 lines (191 loc) · 7.49 KB
/
diffEditor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var React = require('react');
var Draft = require('draft-js');
var debounce = require('just-debounce');
var diffWordMode = require('./diffWordMode');
var diffDecoratorStrategies = require('./diffDecoratorStrategies');
var diffDecorator = require('./diffDecorator');
var EDITOR_PROP_SHAPE = React.PropTypes.shape({
hidden: React.PropTypes.bool,
initial: React.PropTypes.string,
onChange: React.PropTypes.func,
readOnly: React.PropTypes.bool,
state: React.PropTypes.instanceOf(Draft.EditorState)
});
/**
* Displays two Draft.Editor decorated with diffs.
* @prop {Number} [debounceWait=-1] Milliseconds. Delay for the
* updating the diffs. -1 to disable debouncing.
* @prop {Object} [before] Props for the before editor (containing the old text)
* @prop {Object} [after] Props for the after editor (containing the new text)
* @prop {String} [before.initial=''] The initial before text
* @prop {String} [after.initial=''] The initial after text
* @prop {Boolean} [before.hidden=false] Whether to actually display an editor
* @prop {Boolean} [after.hidden=false] Whether to actually display an editor
* @prop {Boolean} [before.readOnly=false] Make the before editor read only.
* @prop {Boolean} [after.readOnly=false] Make the after editor read only.
* @prop {Function} [after.onChange] Callback called with the after EditorState changes.
* @prop {Function} [before.onChange] Callback called when the before EditorState changes.
* @prop {Draft.EditorState} [after.state] Be sure to pass back the
* updated state if you listen to after.onChange.
* @prop {Draft.EditorState} [before.state] Be sure to pass back the
* updated state if you listen to before.onChange.
*/
var DiffEditor = React.createClass({
propTypes: {
before: EDITOR_PROP_SHAPE,
after: EDITOR_PROP_SHAPE,
debounceWait: React.PropTypes.number
},
getDefaultProps: function() {
var defaultEditorProps = {
hidden: false,
initial: '',
onChange: null,
readOnly: false,
state: null
};
return {
before: defaultEditorProps,
after: defaultEditorProps,
debounceWait: -1
};
},
getInitialState: function () {
// Anti-patterns everywhere...
return this.createInitialState(this.props);
},
createInitialState: function (props) {
// Make a debounced diff update
if (props.debounceWait >= 0) {
this.debouncedUpdateDiffs =
debounce(this.updateDiffs,
props.debounceWait,
false, // trailing
true); // guarantee waiting time
}
var state = {
beforeState: props.before.state || editorStateFromText(props.before.initial),
afterState: props.after.state || editorStateFromText(props.after.initial)
};
return diffDecorateEditors(state);
},
componentWillReceiveProps: function (props) {
// New initial state ?
if (props.before.initial !== this.props.before.initial
|| props.after.initial !== this.props.after.initial) {
return this.setState(this.createInitialState(props));
} else {
var newState = {
beforeState: props.before.state || this.state.beforeState,
afterState: props.after.state || this.state.afterState
};
if (this.props.debounceWait >= 0) {
// Update diff later
this.setState(newState);
this.debouncedUpdateDiffs();
} else {
// Update diff now
this.setState(diffDecorateEditors(newState));
}
}
},
updateDiffs: function () {
this.setState(diffDecorateEditors(this.state));
},
onChange: function (beforeState, afterState) {
// Texts changed ?
var afterChanged = contentChanged(this.state.afterState, afterState);
var beforeChanged = contentChanged(this.state.beforeState, beforeState);
var newState = {
beforeState: beforeState,
afterState: afterState
};
if (beforeChanged || afterChanged) {
// Update diffs
if (this.props.debounceWait >= 0) {
// Update diff later
this.setState(newState);
this.debouncedUpdateDiffs();
} else {
// Update diff now
this.setState(diffDecorateEditors(newState));
}
} else {
this.setState(newState);
}
},
onAfterChange: function (afterState) {
var afterChanged = contentChanged(this.state.afterState, afterState);
if (this.props.after.onChange && afterChanged) {
this.props.after.onChange(afterState);
} else {
this.onChange(this.state.beforeState, afterState);
}
},
onBeforeChange: function (beforeState) {
var beforeChanged = contentChanged(this.state.beforeState, beforeState);
if (this.props.before.onChange && beforeChanged) {
this.props.before.onChange(beforeState);
} else {
this.onChange(beforeState, this.state.afterState);
}
},
render: function () {
var before;
if (!this.props.before.hidden) {
before = <div className='diff-before'>
<Draft.Editor
readOnly={this.props.before.readOnly}
editorState={this.state.beforeState}
onChange={this.onBeforeChange}
/>
</div>;
}
var after;
if (!this.props.after.hidden) {
after = <div className='diff-after'>
<Draft.Editor
readOnly={this.props.after.readOnly}
editorState={this.state.afterState}
onChange={this.onAfterChange}
/>
</div>;
}
return <div className='diff-editor'>
{before}
{after}
</div>;
}
});
function contentChanged(editorState1, editorState2) {
return editorState1.getCurrentContent() !== editorState2.getCurrentContent();
}
function editorStateFromText(text) {
var content = Draft.ContentState.createFromText(text);
return Draft.EditorState.createWithContent(content);
}
function diffDecorateEditors(state) {
var beforeState = state.beforeState;
var afterState = state.afterState;
var beforeContentState = beforeState.getCurrentContent();
var afterContentState = afterState.getCurrentContent();
var decorators = createDiffsDecorators(beforeContentState, afterContentState);
return {
beforeState: Draft.EditorState.set(beforeState, { decorator: decorators.before }),
afterState: Draft.EditorState.set(afterState, { decorator: decorators.after })
};
}
function createDiffsDecorators(beforeContentState, afterContentState) {
// Compute diff on whole texts
var before = beforeContentState.getPlainText();
var after = afterContentState.getPlainText();
var diffs = diffWordMode(before, after);
// Create strategies
var beforeStrategies = diffDecoratorStrategies(diffs, false, beforeContentState.getBlockMap());
var afterStrategies = diffDecoratorStrategies(diffs, true, afterContentState.getBlockMap());
return {
before: diffDecorator(beforeStrategies),
after: diffDecorator(afterStrategies)
};
}
module.exports = DiffEditor;