Fix cursor and scroll position in codemirror after undo

pull/1/head
yflory 5 years ago
parent aeb81fdfad
commit 89de5869c5

@ -218,6 +218,24 @@ define([
});
editor.focus();
// Fix cursor and scroll position after undo/redo
var undoData;
editor.on('beforeChange', function (editor, change) {
if (change.origin !== "undo" && change.origin !== "redo") { return; }
undoData = editor.getValue();
});
editor.on('change', function (editor, change) {
if (change.origin !== "undo" && change.origin !== "redo") { return; }
if (typeof(undoData) === "undefined") { return; }
var doc = editor.getValue();
var ops = ChainPad.Diff.diff(undoData, doc);
undoData = undefined;
if (!ops.length) { return; }
var cursor = posToCursor(ops[0].offset, doc);
editor.setCursor(cursor);
editor.scrollIntoView(cursor);
});
var setMode = exp.setMode = function (mode, cb) {
exp.highlightMode = mode;
if (mode === 'markdown') { mode = 'gfm'; }

Loading…
Cancel
Save