From 89de5869c5c913587e613a2172c197e816cd1356 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 21 Apr 2020 14:42:49 +0200 Subject: [PATCH] Fix cursor and scroll position in codemirror after undo --- www/common/sframe-common-codemirror.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/www/common/sframe-common-codemirror.js b/www/common/sframe-common-codemirror.js index 40e820d41..dc046cd62 100644 --- a/www/common/sframe-common-codemirror.js +++ b/www/common/sframe-common-codemirror.js @@ -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'; }