One of the main selling points for Rookeries, is its ability to let the
writer write content using Markdown, and getting a live preview of the result.
Since the user will be writing in code, it would be a better experience to
use a text editor meant for code. Rather than try to undertake a large task
like this myself, I decided to look around for an editor.
Choosing a Text Editor
My three main concerns for a code editor is for it work properly, integrate
nicely with React and have nice syntax highlighting. After a bit of searching
I found out about the following code editors:
- Ace
- Very powerful and flexible code editor.
- React integration – react-ace
- Ran into issues where braces that handled the syntax theme loading, conflicted with browserify.
- Code Mirror
- Very elegant code editor.
- Had two React integrations:
- EditArea
- A much older code/text editor, and no longer supported.
Setting up a Code Mirror-React Component
In the end I chose Code Mirror and specifically the react-codemirror project.
The syntax highlighting does not quite work yet, but I will work on that. The
other integration looked more mature, but ultimately did not want to work for
me.
Setting up the Code Mirror component in Coffee-JSX turned out fairly easy:
React = require 'react'
CodeMirror = require 'react-codemirror'
MyView = React.createClass(
render: () ->
options = {lineNumbers: true, mode:'markdown', theme: 'monokai'}
return (
<codemirror value={ code } onChange={ @updateCode } options={ options }></codemirror>
)
)
CSS Gotcha!
Oddly enough just setting up the CodeMirror component did not work right out
of the box. In fact the result was rendering each line as a separated out
box. After a bit of digging, I found that the styling was based on the
pre-existing CSS style provided by Bootstrap... not by Code Mirror!
The reason turned out to be simple: I had not included the CSS from Code
Mirror into the app. Since I did not find a CDN with Code Mirror setup, I
decided to simply include Code Mirror's CSS into my LESS stylesheets for
Rookeries, based on the example in react-codemirror:
@import (inline) "../../../node_modules/codemirror/lib/codemirror.css";
This worked well enough, and I've included some of the CodeMirror themes as
well.
# Outstanding Issues
Currently the text editor works however the setup is missing an important
feature: syntax highlighting. In theory Code Mirror should enable syntax
highlighting based on the passed in mode. Setting the mode to 'markdown' does
not seems to achieve the expected result.