Skip to content

Commit c9db995

Browse files
committed
1 parent a238899 commit c9db995

File tree

6 files changed

+51
-36
lines changed

6 files changed

+51
-36
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
3.0.5
2+
==================
3+
* https://github.com/scniro/react-codemirror2/issues/31 (take 2)
4+
15
3.0.4
26
==================
37
* https://github.com/scniro/react-codemirror2/issues/31

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: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ var Controlled = (function (_super) {
192192
};
193193
Controlled.prototype.hydrate = function (props) {
194194
var _this = this;
195-
Object.keys(props.options || {}).forEach(function (key) { return _this.editor.setOption(key, props.options[key]); });
195+
Object.keys(props.options || {}).forEach(function (key) {
196+
_this.editor.setOption(key, props.options[key]);
197+
_this.mirror.setOption(key, props.options[key]);
198+
});
196199
if (!this.hydrated) {
197200
if (!this.mounted) {
198201
this.initChange(props.value || '');
@@ -219,26 +222,26 @@ var Controlled = (function (_super) {
219222
this.emulating = false;
220223
};
221224
Controlled.prototype.resolveChange = function () {
222-
var _this = this;
223-
this.editor.operation(function () {
224-
_this.emulating = true;
225-
if (_this.deferred.origin === 'redo') {
226-
_this.editor.setCursor(_this.mirror.getCursor());
227-
}
228-
_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-
}
233-
_this.emulating = false;
234-
_this.deferred = null;
235-
});
225+
this.emulating = true;
226+
if (this.deferred.origin === 'undo') {
227+
this.editor.undo();
228+
}
229+
else if (this.deferred.origin === 'redo') {
230+
this.editor.redo();
231+
}
232+
else {
233+
this.editor.replaceRange(this.deferred.text, this.deferred.from, this.deferred.to, this.deferred.origin);
234+
}
235+
this.emulating = false;
236+
this.deferred = null;
236237
};
237238
Controlled.prototype.mirrorChange = function (deferred) {
238239
if (deferred.origin === 'undo') {
240+
this.editor.setHistory(this.mirror.getHistory());
239241
this.mirror.undo();
240242
}
241243
else if (deferred.origin === 'redo') {
244+
this.editor.setHistory(this.mirror.getHistory());
242245
this.mirror.redo();
243246
}
244247
else {
@@ -263,8 +266,10 @@ var Controlled = (function (_super) {
263266
this.shared = new Shared(this.editor, this.props);
264267
this.mirror = cm(function () {
265268
});
266-
this.editor.on('cursorActivity', function () {
269+
this.editor.on('electricInput', function () {
267270
_this.mirror.setHistory(_this.editor.getHistory());
271+
});
272+
this.editor.on('cursorActivity', function () {
268273
_this.mirror.setCursor(_this.editor.getCursor());
269274
});
270275
this.editor.on('beforeChange', function (cm, data) {
@@ -484,6 +489,7 @@ var UnControlled = (function (_super) {
484489
this.editor.scrollTo(this.props.scroll.x, this.props.scroll.y);
485490
}
486491
this.mounted = true;
492+
this.editor.clearHistory();
487493
if (this.props.editorDidMount) {
488494
this.props.editorDidMount(this.editor, this.editor.getValue(), this.initCb);
489495
}

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-codemirror2",
3-
"version": "3.0.4",
3+
"version": "3.0.5",
44
"description": "a tiny react codemirror component wrapper",
55
"main": "index.js",
66
"typings": "index.d.ts",

src/index.tsx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,10 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
293293
/** @internal */
294294
private hydrate(props) {
295295

296-
Object.keys(props.options || {}).forEach(key => this.editor.setOption(key, props.options[key]));
296+
Object.keys(props.options || {}).forEach(key => {
297+
this.editor.setOption(key, props.options[key]);
298+
this.mirror.setOption(key, props.options[key]);
299+
});
297300

298301
if (!this.hydrated) {
299302

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

336-
this.editor.operation(() => {
337-
this.emulating = true;
338-
339-
if (this.deferred.origin === 'redo') {
340-
this.editor.setCursor(this.mirror.getCursor());
341-
}
339+
this.emulating = true;
342340

343-
this.editor.replaceRange(this.deferred.text.join('\n'), this.deferred.from, this.deferred.to, this.deferred.origin);
344-
this.editor.setHistory(this.mirror.getHistory());
341+
if (this.deferred.origin === 'undo') {
342+
this.editor.undo();
343+
} else if (this.deferred.origin === 'redo') {
344+
this.editor.redo();
345+
} else {
346+
this.editor.replaceRange(this.deferred.text, this.deferred.from, this.deferred.to, this.deferred.origin);
347+
}
345348

346-
if (this.deferred.origin === 'undo') {
347-
this.editor.setCursor(this.mirror.getCursor());
348-
}
349-
this.emulating = false;
350-
this.deferred = null;
351-
});
349+
this.emulating = false;
350+
this.deferred = null;
352351
}
353352

354353
/** @internal */
355354
private mirrorChange(deferred) {
356355

357356
if (deferred.origin === 'undo') {
357+
this.editor.setHistory(this.mirror.getHistory());
358358
this.mirror.undo();
359359
} else if (deferred.origin === 'redo') {
360+
this.editor.setHistory(this.mirror.getHistory());
360361
this.mirror.redo();
361362
} else {
362363
this.mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
@@ -391,8 +392,11 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
391392
this.mirror = (cm as any)(() => {
392393
});
393394

394-
this.editor.on('cursorActivity', () => {
395+
this.editor.on('electricInput', () => {
395396
this.mirror.setHistory(this.editor.getHistory());
397+
});
398+
399+
this.editor.on('cursorActivity', () => {
396400
this.mirror.setCursor(this.editor.getCursor());
397401
});
398402

@@ -412,7 +416,6 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
412416
this.props.onBeforeChange(this.editor, this.deferred, phantomChange);
413417
});
414418

415-
416419
this.editor.on('change', (cm, data) => {
417420

418421
if (!this.mounted) {
@@ -664,6 +667,8 @@ export class UnControlled extends React.Component<IUnControlledCodeMirror, any>
664667

665668
this.mounted = true;
666669

670+
this.editor.clearHistory();
671+
667672
if (this.props.editorDidMount) {
668673
this.props.editorDidMount(this.editor, this.editor.getValue(), this.initCb);
669674
}

0 commit comments

Comments
 (0)