scniro / react-codemirror2

Codemirror integrated components for React
MIT License
1.66k stars 193 forks source link

addLineClass #46

Closed murtyjones closed 6 years ago

murtyjones commented 6 years ago

Is it possible to access commands such as addLineClass in react-codemirror2?

I was attempting to set the node like so:

<CodeMirror
  ref={ c => this.codeMirror = c }
/>

and add a class to a line like so:

this.codeMirror.addLineClass((nextState.errorLine - 1), 'errorLine')

No luck though.

scniro commented 6 years ago

No need to store the ref yourself, just manipulate the instance given to you! In this case, we'll persist it in editorDidMount (most a lot of callbacks in the docs pass this back). This keeps coming up - I think I'll add a "getting the instance" piece to the readme...

constructor() {
  this.instance = null; // store here on `editorDidMount`
}

render() {
  <CodeMirror editorDidMount={(editor) => { this.instance = editor }}/>
}

then you can this.instance.addLineClass((nextState.errorLine - 1), 'errorLine')

murtyjones commented 6 years ago

Thanks! I also realized that I forgot an argument in addLineClass. In my case, it should've been:

this.codeMirror.addLineClass((nextState.errorLine - 1), 'wrap', 'errorLine')

rather than:

this.codeMirror.addLineClass((nextState.errorLine - 1), 'errorLine')

it's working now. Thanks again.