You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
onBeforeChange returns wrong third parameter when previous change is canceled #279
My intention is to block some lines from the edition.
When I try to edit code specified in linesReadOnly it works fine but when the next action is correct for example some changes in line 10 then nValue returns the wrong string (with changes that were blocked by changeObj.cancel()
Hey, just had the same issue. Whipped up a solution if you're still having this issue.
For reference, cm is the first value that is passed in from onBeforeChange which you are destructuring to an empty object.
// onBeforeChange will get triggered again with the origin of "setValue".
// This prevents an infinite loop
if (action.origin !== "setValue") {
// Getting the first selection in the array (If you are dealing with multiple cursors you would want to get all the ranges)
const oldSelection = cm.doc.sel.ranges[0];
// Getting the old scroll info from CodeMirror
const scrollInfo = cm.getScrollInfo();
const oldScrollTop = scrollInfo.top;
const oldScrollLeft = scrollInfo.left;
// Setting the data back to your old value
cm.setValue(INSERT_OLD_CODE_VALUE_HERE);
// Setting the old selections in place (If you are dealing with multiple cursors you would want to use cm.setSelections and
pass them all in)
cm.setSelection(oldSelection.anchor, oldSelection.head);
// Scrolling back to where we were
cm.scrollTo(oldScrollLeft, oldScrollTop);
}
Activity
Sruli-RD commentedon May 26, 2022
Hey, just had the same issue. Whipped up a solution if you're still having this issue.
For reference,
cmis the first value that is passed in from onBeforeChange which you are destructuring to an empty object.