李建英 發表於 2022-8-26 17:13:00

react中CodeMirror (代码编辑器)

<p><span style="font-size: 14pt"><strong>前言:</strong></span></p>
<p><span style="font-size: 14pt"><strong> </strong><span style="font-size: 16px"> 实现一个在react项目中页面展示代码编辑器的效果。</span></span></p>
<h1><span style="font-size: 18px">codemirror:</span></h1>
<p><span style="font-size: 18px">  <span style="font-size: 16px">使用JavaScript为浏览器实现的多功能文本编辑器。codemirror作用:专门用于编辑代码,并带有实现更高级编辑功能的多种语言模式和附加组件</span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 16px">实现效果</span></span></p>
<p><img src="https://img2022.cnblogs.com/blog/2749674/202208/2749674-20220826171003484-896556970.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><span style="font-size: 16px"><br class="Apple-interchange-newline">1. 首先从react-codemirror2中引入Codemirror组件  </span></span></p>
<div class="cnblogs_code">
<pre>import {UnControlled <span style="color: rgba(0, 0, 255, 1)">as</span> CodeMirror} <span style="color: rgba(0, 0, 255, 1)">from</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">react-codemirror2</span><span style="color: rgba(128, 0, 0, 1)">'</span></pre>
</div>
<p><span style="font-size: 16px">2. 他的核心在于引入codemirror.js和codemirror.css</span></p>
<div class="cnblogs_code">
<pre>import <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">codemirror/lib/codemirror.js</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
import </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">codemirror/lib/codemirror.css</span><span style="color: rgba(128, 0, 0, 1)">'</span></pre>
</div>
<p><span style="font-size: 16px">3.&nbsp; codemirror的背景主题(可以设置自己喜欢的主题样式),同时他还需要在options里面设置theme:'yonce'</span></p>
<div class="cnblogs_code">
<pre>import <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">codemirror/theme/yonce.css</span><span style="color: rgba(128, 0, 0, 1)">'</span></pre>
</div>
<p><span style="font-size: 16px">4. 引入一些代码模式</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 代码模式,clike是包含java,c++等模式的</span>
import 'codemirror/mode/clike/clike'<span style="color: rgba(0, 0, 0, 1)">   
import </span>'codemirror/mode/javascript/javascript'   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">js</span>
import 'codemirror/mode/python/python.js'      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">python</span></pre>
</div>
<p><span style="font-size: 16px">&nbsp;5. 代码高亮和代码折叠</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">代码高亮</span>
import 'codemirror/addon/selection/active-line'<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 代码折叠</span>
import 'codemirror/addon/fold/foldgutter.css'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'codemirror/addon/fold/foldcode.js'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'codemirror/addon/fold/foldgutter.js'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'codemirror/addon/fold/brace-fold.js'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'codemirror/addon/fold/comment-fold.js';</pre>
</div>
<p><span style="font-size: 16px">6、可以通过插件获取到simple和overlay两种,但是需要引入js和css,咋了好久才找到,分别是:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">代码滚动</span>
import 'codemirror/addon/scroll/simplescrollbars.js'<span style="color: rgba(0, 0, 0, 1)">
import </span>'codemirror/addon/scroll/simplescrollbars.css'</pre>
</div>
<p>渲染代码</p>
<div class="cnblogs_code">
<div>
<div> </div>
<div>&nbsp;</div>
<div> const {text} = this.state;//自己在state中定义初始代码编辑框中的值</div>
</div>
<pre><br>&lt;<span style="color: rgba(0, 0, 0, 1)">CodeMirror
                  id</span>='scriptDesc'<span style="color: rgba(0, 0, 0, 1)">
                  editorDidMount</span>={editor =&gt; { <span style="color: rgba(0, 0, 255, 1)">this</span>.instance =<span style="color: rgba(0, 0, 0, 1)"> editor; }}
                  value</span>=<span style="color: rgba(0, 0, 0, 1)">{text}
                  onChange</span>={<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.changeCode}
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> eslint-disable-next-line no-return-assign</span>
                  ref={(c) =&gt; <span style="color: rgba(0, 0, 255, 1)">this</span>.myCodeMirror = c}<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加ref属性获取dome节点</span>
                  options =<span style="color: rgba(0, 0, 0, 1)"> {{
                      lineNumbers: </span><span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 显示行号</span>
                      mode: {name: 'text/x-java' || 'javascript'}, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 语言</span>
                      autofocus: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自动获取焦点</span>
                      styleActiveLine: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 光标代码高亮</span>
                      theme: 'yonce', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 主题</span>
                      scrollbarStyle: 'overlay'<span style="color: rgba(0, 0, 0, 1)">,
                      lineWrapping: </span><span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 代码自动换行</span>
                      foldGutter: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
                      gutters: [</span>'CodeMirror-linenumbers', 'CodeMirrorfoldgutter'] <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> end</span>
<span style="color: rgba(0, 0, 0, 1)">                  }}
            </span>/&gt;</pre>
</div>
<p>方法</p>
<div class="cnblogs_code">
<pre>changeCode = (CodeMirror, changeObj, value) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!value) <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取 CodeMirror.doc.getValue()</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 赋值 CodeMirror.doc.setValue(value) // 会触发 onChange 事件,小心进入无线递归。</span>
      <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({text: value});
};</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/qing1224/p/16628272.html
頁: [1]
查看完整版本: react中CodeMirror (代码编辑器)