How to change the value using data object in code mirror

328 views Asked by At

I'm using react-codemirror2 for online code editor kind of project. I want to implement my own real time collaborative platform. In quill js we can use delta object to do this. In code mirror we can get 'data' object from onBeforeChange event handler. Is there any way to use that data object like delta object? (covert to editor's value and vise versa)

1

There are 1 answers

0
Pranavan On BEST ANSWER

Finally I found a way to do this. I had to use another separate codemirror instance to obtain this.

const mirror = cm(function () { }, {});
const mirrorChange = (editor, deferred) => {
    var doc = editor.getDoc();

    if (deferred.origin === 'undo') {
        doc.setHistory(mirror.getHistory());
        mirror.undo();
    } else if (deferred.origin === 'redo') {
        doc.setHistory(mirror.getHistory());
        mirror.redo();
    } else {
        mirror.replaceRange(deferred.text, deferred.from, deferred.to, deferred.origin);
    }

    return mirror.getValue();
}

Here I can use this function inside onBeforeChange handler. Since I'm maintaining another codemirror instance, this won't a better solution. So if anyone can improve this answer, please let me know