Skip to content

Commit

Permalink
#31
Browse files Browse the repository at this point in the history
  • Loading branch information
scniro committed Nov 1, 2017
1 parent a238899 commit c9db995
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.0.5
==================
* https://github.com/scniro/react-codemirror2/issues/31 (take 2)

3.0.4
==================
* https://github.com/scniro/react-codemirror2/issues/31
Expand Down
2 changes: 1 addition & 1 deletion docs/app.min.js

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ var Controlled = (function (_super) {
};
Controlled.prototype.hydrate = function (props) {
var _this = this;
Object.keys(props.options || {}).forEach(function (key) { return _this.editor.setOption(key, props.options[key]); });
Object.keys(props.options || {}).forEach(function (key) {
_this.editor.setOption(key, props.options[key]);
_this.mirror.setOption(key, props.options[key]);
});
if (!this.hydrated) {
if (!this.mounted) {
this.initChange(props.value || '');
Expand All @@ -219,26 +222,26 @@ var Controlled = (function (_super) {
this.emulating = false;
};
Controlled.prototype.resolveChange = function () {
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;
});
this.emulating = true;
if (this.deferred.origin === 'undo') {
this.editor.undo();
}
else if (this.deferred.origin === 'redo') {
this.editor.redo();
}
else {
this.editor.replaceRange(this.deferred.text, this.deferred.from, this.deferred.to, this.deferred.origin);
}
this.emulating = false;
this.deferred = null;
};
Controlled.prototype.mirrorChange = function (deferred) {
if (deferred.origin === 'undo') {
this.editor.setHistory(this.mirror.getHistory());
this.mirror.undo();
}
else if (deferred.origin === 'redo') {
this.editor.setHistory(this.mirror.getHistory());
this.mirror.redo();
}
else {
Expand All @@ -263,8 +266,10 @@ var Controlled = (function (_super) {
this.shared = new Shared(this.editor, this.props);
this.mirror = cm(function () {
});
this.editor.on('cursorActivity', function () {
this.editor.on('electricInput', function () {
_this.mirror.setHistory(_this.editor.getHistory());
});
this.editor.on('cursorActivity', function () {
_this.mirror.setCursor(_this.editor.getCursor());
});
this.editor.on('beforeChange', function (cm, data) {
Expand Down Expand Up @@ -484,6 +489,7 @@ var UnControlled = (function (_super) {
this.editor.scrollTo(this.props.scroll.x, this.props.scroll.y);
}
this.mounted = true;
this.editor.clearHistory();
if (this.props.editorDidMount) {
this.props.editorDidMount(this.editor, this.editor.getValue(), this.initCb);
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-codemirror2",
"version": "3.0.4",
"version": "3.0.5",
"description": "a tiny react codemirror component wrapper",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
39 changes: 22 additions & 17 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
/** @internal */
private hydrate(props) {

Object.keys(props.options || {}).forEach(key => this.editor.setOption(key, props.options[key]));
Object.keys(props.options || {}).forEach(key => {
this.editor.setOption(key, props.options[key]);
this.mirror.setOption(key, props.options[key]);
});

if (!this.hydrated) {

Expand Down Expand Up @@ -333,30 +336,28 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
/** @internal */
private resolveChange() {

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

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

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.undo();
} else if (this.deferred.origin === 'redo') {
this.editor.redo();
} else {
this.editor.replaceRange(this.deferred.text, this.deferred.from, this.deferred.to, this.deferred.origin);
}

if (this.deferred.origin === 'undo') {
this.editor.setCursor(this.mirror.getCursor());
}
this.emulating = false;
this.deferred = null;
});
this.emulating = false;
this.deferred = null;
}

/** @internal */
private mirrorChange(deferred) {

if (deferred.origin === 'undo') {
this.editor.setHistory(this.mirror.getHistory());
this.mirror.undo();
} else if (deferred.origin === 'redo') {
this.editor.setHistory(this.mirror.getHistory());
this.mirror.redo();
} else {
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
Expand Down Expand Up @@ -391,8 +392,11 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
this.mirror = (cm as any)(() => {
});

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

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

Expand All @@ -412,7 +416,6 @@ 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 Expand Up @@ -664,6 +667,8 @@ export class UnControlled extends React.Component<IUnControlledCodeMirror, any>

this.mounted = true;

this.editor.clearHistory();

if (this.props.editorDidMount) {
this.props.editorDidMount(this.editor, this.editor.getValue(), this.initCb);
}
Expand Down

0 comments on commit c9db995

Please sign in to comment.