Saving editor state

Is it possible to get the editor full state, i.e. mode, content, folded parts, and focused line, and to apply it to another editor? What are the methods I can use?
Thanks

2 Likes

There is no built-in operation for this. You’ll have to build something using getValue, listSelections, and findMarks.

Thanks for the answer and sorry for the late reply.
I tried to use the methods you said, and I could copy my value and selection to another editor.
Now remain the folded parts.
From the function getAllMarks I get an array of CodeMirror.TextMarker objects, but I don’t know how to use them for setting markers. I don’t find with these objects a way to get line and char for from and to parameters for using doc.markText
Is there a way to do so?

Use the markers’ find method, it’ll tell you where they currently are.

Thanks, and sorry I missed that method!

I succeded to copy the folding pattern, and the value of the editor is correct, but the gutter still has the class CodeMirror-foldgutter-open, and does not open if I click on it, i.e. it changes class to CodeMirror-foldgutter-folded but won’t open up the folded text. May you help again?

My code:

for ( var i = 0; i < r.marks.length; i++ ){
  if ( r.marks[i].type === 'range' && r.marks[i].collapsed ){
    var fnd = r.marks[i].find(),
        mrk = doc.markText(fnd.from, fnd.to, {
          atomic: r.marks[i].atomic,
          clearOnEnter: r.marks[i].clearOnEnter,
          collapsed: r.marks[i].collapsed,
          replacedWith: r.marks[i].replacedWith.cloneNode(true)
        });
  }
}

``

That’s true, and I don’t think there’s a good wokaround for that. You could try calling editor.foldCode for all the starts of folded ranges that you found, instead of directly restoring the ranges.

Thanks! Yes, it works fine with foldCode. I had a problem because I had nested folded parts, but reversing the markers’ array did the trick.
Sweet, now I can save the state of an editor instance, store the info, and reopen it exactly how it was :slight_smile:
Thanks again, and great work!

nabab, can you post the code you used to get the folded parts (save state) and the code you used to refold properly.

I have been trying to do this unsuccessfully.

THANK YOU

I use a jQuery widget of my own, where this.cm is the codeMirror instance. Here are my 2 methods:

getState: function(){
  var $$ = this,
      doc = $$.cm.getDoc(),
      marks = doc.getAllMarks(),
      res = {
        value: $$.cm.getValue(),
        selections: doc.listSelections(),
        marks: []
      };
  if ( marks.length ){
    // We reverse the array in order to start in the last folded parts in case of nesting
    for ( var i = marks.length - 1; i >= 0; i-- ){
      if ( marks[i].collapsed && (marks[i].type === 'range') ){
        res.marks.push(marks[i].find().from);
      }
    }
  }
  return res;
},

setState: function(state){
  var $$ = this,
      doc = $$.cm.getDoc();
  if ( state.value !== undefined ){
    $$.cm.setValue(state.value);
  }
  if ( state.selections ){
    $.each(state.selections, function(i, a){
      doc.setSelection(a.anchor, a.head);
    }
  }
  if ( state.marks ){
    $.each(state.marks, function(i, a){
      $$.cm.foldCode(a);
    });
  }
};

You’re welcome!

1 Like