Skip to content

Commit

Permalink
#31
Browse files Browse the repository at this point in the history
  • Loading branch information
scniro committed Oct 31, 2017
1 parent 3a149ab commit ab2ffa7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 161 deletions.
2 changes: 1 addition & 1 deletion docs/app.min.js

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,28 @@ var Controlled = (function (_super) {
var _this = this;
this.editor.operation(function () {
_this.emulating = true;
if (_this.deferred.origin === 'redo') {
_this.editor.setCursor(_this.mirror.getCursor());
}
_this.editor.replaceRange(_this.deferred.text.join('\n'), _this.deferred.from, _this.deferred.to, _this.deferred.origin);
_this.editor.setHistory(_this.mirror.getHistory());
if (_this.deferred.origin === 'undo') {
_this.editor.setCursor(_this.mirror.getCursor());
}
_this.emulating = false;
_this.deferred = null;
});
};
Controlled.prototype.mirrorChange = function (deferred) {
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
if (deferred.origin === 'undo') {
this.mirror.undo();
}
else if (deferred.origin === 'redo') {
this.mirror.redo();
}
else {
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
}
return this.mirror.getValue();
};
Controlled.prototype.componentWillMount = function () {
Expand All @@ -248,16 +263,14 @@ var Controlled = (function (_super) {
this.shared = new Shared(this.editor, this.props);
this.mirror = cm(function () {
});
this.editor.on('cursorActivity', function () {
_this.mirror.setHistory(_this.editor.getHistory());
_this.mirror.setCursor(_this.editor.getCursor());
});
this.editor.on('beforeChange', function (cm, data) {
if (_this.emulating) {
return;
}
if (data.origin === 'undo') {
return;
}
if (data.origin === 'redo') {
return;
}
data.cancel();
_this.deferred = data;
var phantomChange = _this.mirrorChange(_this.deferred);
Expand Down
93 changes: 22 additions & 71 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-codemirror2",
"version": "3.0.3",
"version": "3.0.4",
"description": "a tiny react codemirror component wrapper",
"main": "index.js",
"typings": "index.d.ts",
Expand Down Expand Up @@ -61,7 +61,7 @@
"@nteract/mockument": "1.0.4",
"@types/codemirror": "0.0.50",
"@types/jest": "21.1.5",
"@types/react": "16.0.18",
"@types/react": "16.0.19",
"babel-core": "6.26.0",
"babel-jest": "21.2.0",
"babel-loader": "7.1.2",
Expand Down Expand Up @@ -91,9 +91,9 @@
"sass-loader": "6.0.6",
"sinon": "4.0.2",
"style-loader": "0.19.0",
"ts-jest": "21.1.3",
"typescript": "2.5.3",
"typescript-formatter": "6.1.0",
"ts-jest": "21.1.4",
"typescript": "2.6.1",
"typescript-formatter": "7.0.0",
"webpack": "3.8.1"
}
}
48 changes: 31 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import * as codemirror from 'codemirror';
let cm;

const IS_MOBILE = typeof navigator === 'undefined' || (
navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
);
navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
);

declare let require: any;

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

this.editor.operation(() => {
this.emulating = true;

if (this.deferred.origin === 'redo') {
this.editor.setCursor(this.mirror.getCursor());
}

this.editor.replaceRange(this.deferred.text.join('\n'), this.deferred.from, this.deferred.to, this.deferred.origin);
this.editor.setHistory(this.mirror.getHistory());

if (this.deferred.origin === 'undo') {
this.editor.setCursor(this.mirror.getCursor());
}
this.emulating = false;
this.deferred = null;
});
Expand All @@ -344,7 +354,13 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
/** @internal */
private mirrorChange(deferred) {

this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
if (deferred.origin === 'undo') {
this.mirror.undo();
} else if (deferred.origin === 'redo') {
this.mirror.redo();
} else {
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
}

return this.mirror.getValue();
}
Expand Down Expand Up @@ -375,20 +391,17 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
this.mirror = (cm as any)(() => {
});

this.editor.on('cursorActivity', () => {
this.mirror.setHistory(this.editor.getHistory());
this.mirror.setCursor(this.editor.getCursor());
});

this.editor.on('beforeChange', (cm, data) => {

if (this.emulating) {
return;
}

if (data.origin === 'undo') {
return;
}

if (data.origin === 'redo') {
return;
}

data.cancel();

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


this.editor.on('change', (cm, data) => {

if (!this.mounted) {
Expand Down
Loading

0 comments on commit ab2ffa7

Please sign in to comment.