{"id":178,"date":"2018-04-08T17:07:33","date_gmt":"2018-04-08T21:07:33","guid":{"rendered":"https:\/\/zhouanbo.com\/?p=178"},"modified":"2019-11-01T15:01:45","modified_gmt":"2019-11-01T19:01:45","slug":"using-ace-editor-with-react","status":"publish","type":"post","link":"https:\/\/zhouanbo.com\/?p=178","title":{"rendered":"Using Ace Editor With React"},"content":{"rendered":"<p>React is a modern front end framework, and Ace editor is a popular javascript based code editor. However, things can get complicated in the process of integrating the two since Ace editor is not designed to work with react. &#8220;brace&#8221;, the browserify compatible version of Ace editor, and &#8220;react-ace&#8221;, the &#8220;brace&#8221; editor embedded in a react component, can be used to smooth the process. I noted down the procedure as follows:<\/p>\n<p><!-- more --><\/p>\n<p>In the project folder, install the node version of Ace editor &#8220;brace&#8221;, and the &#8220;react-ace&#8221; package that wrap the &#8220;brace&#8221; editor as a react component:<\/p>\n<pre><code class=\"bash\">npm install brace --save\nnpm install react-ace --save\n<\/code><\/pre>\n<p>require the &#8220;brace&#8221; and &#8220;react-act&#8221; packages, together with a &#8220;sh&#8221; syntax highlighting feature and &#8220;chrome&#8221; theme from &#8220;brace&#8221; package:<\/p>\n<pre><code class=\"javascript\">var brace = require('brace');\nvar AceEditor = require('react-ace');\nrequire('brace\/mode\/sh');\nrequire('brace\/theme\/chrome');\n<\/code><\/pre>\n<p>inside the component, return the editor in for rendering:<\/p>\n<pre><code class=\"javascript\">&lt;AceEditor\n  mode=\"sh\"\n  theme=\"chrome\"\n  name=\"code\"\n  width=\"100%\"\n  maxLines={50}\n  ref=\"ace\"\n  fontSize={18}\n  value=\"#type your code here\"\n  editorProps={{$blockScrolling: Infinity}}\n  onLoad={(editor) =&gt; {\n    editor.focus();\n    editor.getSession().setUseWrapMode(true);\n  }}\n\/&gt;\n<\/code><\/pre>\n<p>There are two ways to address auto focusing and line wrapping: either by receiving a parameter from onLoad event and call the ace editor native methods (as shown), or by setting a <code>ref=\"ace\"<\/code> attribute within the AceEditor tag, and then call method like <code>this.refs.ace.editor.focus<\/code> at &#8220;componentDidMount&#8221; lifecycle.<\/p>\n<p>Also, setting <code>width: 100%<\/code> will allow the editor scale with the container, however, setting<\/p>\n<pre><code class=\"javascript\">height: 100%\n<\/code><\/pre>\n<p>will just disable the editor. The work around would be setting a large value of maxLines (for example: 500) and add<\/p>\n<pre><code class=\"javascript\">editorProps={{$blockScrolling: Infinity}}\n<\/code><\/pre>\n<p>attribute to the editor.<\/p>\n<p>Ace editor will allow performing undo actions until the editor is in it&#8217;s original empty state, even though the content of the editor is loaded immediately when the component initialize. To fix this, remove the undo history right after the component mounts:<\/p>\n<pre><code class=\"javascript\">var undo_manager = this.refs.ace.editor.getSession().getUndoManager();\nundo_manager.reset();\nthis.refs.ace.editor.getSession().setUndoManager(undo_manager);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>React is a modern front end framework, and Ace editor is a popular javascript based code editor. However, things can get complicated in the process of integrating the two since Ace editor is not designed to work with react. &#8220;brace&#8221;, &#8230; <a class=\"styledbutton\" href=\"https:\/\/zhouanbo.com\/?p=178\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[6],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6eWPT-2S","_links":{"self":[{"href":"https:\/\/zhouanbo.com\/index.php?rest_route=\/wp\/v2\/posts\/178"}],"collection":[{"href":"https:\/\/zhouanbo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhouanbo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhouanbo.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhouanbo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178"}],"version-history":[{"count":8,"href":"https:\/\/zhouanbo.com\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":280,"href":"https:\/\/zhouanbo.com\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions\/280"}],"wp:attachment":[{"href":"https:\/\/zhouanbo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhouanbo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhouanbo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}