Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Accessing the editor instance this.editor #1

Open
raine opened this issue Oct 16, 2014 · 6 comments
Open

Accessing the editor instance this.editor #1

raine opened this issue Oct 16, 2014 · 6 comments

Comments

@raine
Copy link

raine commented Oct 16, 2014

What's the right way to access the editor instance, for example to use the configuration API?

http://codemirror.net/doc/manual.html#api_configuration

@ForbesLindesay
Copy link
Owner

It's a good question, this is my first real foray into reusable react components, so I'm not entirely sure. I would guess that any configuration options should be added as properties, rather than providing a method to get the code mirror instance, if possible. There may of course be scenarios that can't be handled that way, but I don't know how to solve that.

@raine
Copy link
Author

raine commented Oct 17, 2014

I guess the component would then have to know which properties to reset with cm.setOption(key, val) when they're changed.

@ForbesLindesay
Copy link
Owner

It could just do a for/in loop over all the properties?

@jrunning
Copy link

I don't know how "Reacty" it is, but I wound up using CodeMirror.defineOption. For example:

var React = require('react'),
    CodeMirror = React.createFactory(require('react-code-mirror')),
    CMGlobal = require('codemirror');

CMGlobal.defineOption('myCallback', null, function(editor, myCallback) {
  myCallback && myCallback(editor);
};

var MyComponent = React.createClass({
  setEditor: function(editor) {
    this.setState({ editor: editor });
  },

  render: function() {
    return CodeMirror({
      // ...
      myCallback: this.setEditor,
    });
});

I'm pretty new to React, so I don't know if this is the best way to do it, but it worked for me.

@ForbesLindesay
Copy link
Owner

That looks like a pretty good work around (I didn't know CodeMirror had that API). The only downside is that those options are then global across all codemirror instances, but that's probably not disastrous if you keep careful track of all the options you define.

@jrunning
Copy link

jrunning commented Mar 2, 2015

I just realized there's a much simpler way to do this:

var React = require('react'),
    CodeMirror = require('react-code-mirror');

var MyComponent = React.createClass({
  getEditor: function() {
    return this.refs.CodeMirror.editor;
  },

  render: function() {
    return <CodeMirror ref="CodeMirror" />;
  }
});

I knew about Refs before but didn't connect the dots until just now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants