Skip to content

Commit ab2ffa7

Browse files
committed
1 parent 3a149ab commit ab2ffa7

File tree

6 files changed

+105
-161
lines changed

6 files changed

+105
-161
lines changed

docs/app.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,28 @@ var Controlled = (function (_super) {
222222
var _this = this;
223223
this.editor.operation(function () {
224224
_this.emulating = true;
225+
if (_this.deferred.origin === 'redo') {
226+
_this.editor.setCursor(_this.mirror.getCursor());
227+
}
225228
_this.editor.replaceRange(_this.deferred.text.join('\n'), _this.deferred.from, _this.deferred.to, _this.deferred.origin);
229+
_this.editor.setHistory(_this.mirror.getHistory());
230+
if (_this.deferred.origin === 'undo') {
231+
_this.editor.setCursor(_this.mirror.getCursor());
232+
}
226233
_this.emulating = false;
227234
_this.deferred = null;
228235
});
229236
};
230237
Controlled.prototype.mirrorChange = function (deferred) {
231-
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
238+
if (deferred.origin === 'undo') {
239+
this.mirror.undo();
240+
}
241+
else if (deferred.origin === 'redo') {
242+
this.mirror.redo();
243+
}
244+
else {
245+
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
246+
}
232247
return this.mirror.getValue();
233248
};
234249
Controlled.prototype.componentWillMount = function () {
@@ -248,16 +263,14 @@ var Controlled = (function (_super) {
248263
this.shared = new Shared(this.editor, this.props);
249264
this.mirror = cm(function () {
250265
});
266+
this.editor.on('cursorActivity', function () {
267+
_this.mirror.setHistory(_this.editor.getHistory());
268+
_this.mirror.setCursor(_this.editor.getCursor());
269+
});
251270
this.editor.on('beforeChange', function (cm, data) {
252271
if (_this.emulating) {
253272
return;
254273
}
255-
if (data.origin === 'undo') {
256-
return;
257-
}
258-
if (data.origin === 'redo') {
259-
return;
260-
}
261274
data.cancel();
262275
_this.deferred = data;
263276
var phantomChange = _this.mirrorChange(_this.deferred);

package-lock.json

Lines changed: 22 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-codemirror2",
3-
"version": "3.0.3",
3+
"version": "3.0.4",
44
"description": "a tiny react codemirror component wrapper",
55
"main": "index.js",
66
"typings": "index.d.ts",
@@ -61,7 +61,7 @@
6161
"@nteract/mockument": "1.0.4",
6262
"@types/codemirror": "0.0.50",
6363
"@types/jest": "21.1.5",
64-
"@types/react": "16.0.18",
64+
"@types/react": "16.0.19",
6565
"babel-core": "6.26.0",
6666
"babel-jest": "21.2.0",
6767
"babel-loader": "7.1.2",
@@ -91,9 +91,9 @@
9191
"sass-loader": "6.0.6",
9292
"sinon": "4.0.2",
9393
"style-loader": "0.19.0",
94-
"ts-jest": "21.1.3",
95-
"typescript": "2.5.3",
96-
"typescript-formatter": "6.1.0",
94+
"ts-jest": "21.1.4",
95+
"typescript": "2.6.1",
96+
"typescript-formatter": "7.0.0",
9797
"webpack": "3.8.1"
9898
}
9999
}

src/index.tsx

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import * as codemirror from 'codemirror';
44
let cm;
55

66
const IS_MOBILE = typeof navigator === 'undefined' || (
7-
navigator.userAgent.match(/Android/i)
8-
|| navigator.userAgent.match(/webOS/i)
9-
|| navigator.userAgent.match(/iPhone/i)
10-
|| navigator.userAgent.match(/iPad/i)
11-
|| navigator.userAgent.match(/iPod/i)
12-
|| navigator.userAgent.match(/BlackBerry/i)
13-
|| navigator.userAgent.match(/Windows Phone/i)
14-
);
7+
navigator.userAgent.match(/Android/i)
8+
|| navigator.userAgent.match(/webOS/i)
9+
|| navigator.userAgent.match(/iPhone/i)
10+
|| navigator.userAgent.match(/iPad/i)
11+
|| navigator.userAgent.match(/iPod/i)
12+
|| navigator.userAgent.match(/BlackBerry/i)
13+
|| navigator.userAgent.match(/Windows Phone/i)
14+
);
1515

1616
declare let require: any;
1717

@@ -335,7 +335,17 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
335335

336336
this.editor.operation(() => {
337337
this.emulating = true;
338+
339+
if (this.deferred.origin === 'redo') {
340+
this.editor.setCursor(this.mirror.getCursor());
341+
}
342+
338343
this.editor.replaceRange(this.deferred.text.join('\n'), this.deferred.from, this.deferred.to, this.deferred.origin);
344+
this.editor.setHistory(this.mirror.getHistory());
345+
346+
if (this.deferred.origin === 'undo') {
347+
this.editor.setCursor(this.mirror.getCursor());
348+
}
339349
this.emulating = false;
340350
this.deferred = null;
341351
});
@@ -344,7 +354,13 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
344354
/** @internal */
345355
private mirrorChange(deferred) {
346356

347-
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
357+
if (deferred.origin === 'undo') {
358+
this.mirror.undo();
359+
} else if (deferred.origin === 'redo') {
360+
this.mirror.redo();
361+
} else {
362+
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
363+
}
348364

349365
return this.mirror.getValue();
350366
}
@@ -375,20 +391,17 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
375391
this.mirror = (cm as any)(() => {
376392
});
377393

394+
this.editor.on('cursorActivity', () => {
395+
this.mirror.setHistory(this.editor.getHistory());
396+
this.mirror.setCursor(this.editor.getCursor());
397+
});
398+
378399
this.editor.on('beforeChange', (cm, data) => {
379400

380401
if (this.emulating) {
381402
return;
382403
}
383404

384-
if (data.origin === 'undo') {
385-
return;
386-
}
387-
388-
if (data.origin === 'redo') {
389-
return;
390-
}
391-
392405
data.cancel();
393406

394407
this.deferred = data;
@@ -399,6 +412,7 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
399412
this.props.onBeforeChange(this.editor, this.deferred, phantomChange);
400413
});
401414

415+
402416
this.editor.on('change', (cm, data) => {
403417

404418
if (!this.mounted) {

0 commit comments

Comments
 (0)