美文网首页
在线代码编辑器CodeMirror的轻量React组件

在线代码编辑器CodeMirror的轻量React组件

作者: KK_boy | 来源:发表于2020-09-22 15:48 被阅读0次

react-codemirror

CodeMirror component for React. demo @uiwjs.github.io/react-codemirror/

<a href="https://uiwjs.github.io/react-codemirror/"><img src="https://raw.githubusercontent.com/uiwjs/react-codemirror/master/react-codemirror.png" /></a>

Features:

🌱 Automatically load mode files based on configuration.
🚀 Quickly and easily configure the API.

Install

# Not dependent on uiw.
npm install @uiw/react-codemirror --save

Usage

import CodeMirror from '@uiw/react-codemirror';
import 'codemirror/keymap/sublime';
import 'codemirror/theme/monokai.css';

const code = 'const a = 0;';

<CodeMirror
  value={code}
  options={{
    theme: 'monokai',
    keyMap: 'sublime',
    mode: 'jsx',
  }}
/>

requiring codemirror resources, This is often the case when specifying certain language modes and themes. Just set the mode, the language resources will automatically lazy loading.

import CodeMirror from '@uiw/react-codemirror';
import 'codemirror/addon/display/autorefresh';
import 'codemirror/addon/comment/comment';
import 'codemirror/addon/edit/matchbrackets';
import 'codemirror/keymap/sublime';
import 'codemirror/theme/monokai.css';

const code = 'const a = 0;';

<CodeMirror
  value={code}
  options={{
    theme: 'monokai',
    tabSize: 2,
    keyMap: 'sublime',
    mode: 'jsx',
  }}
/>

Change Theme

import CodeMirror from '@uiw/react-codemirror';
import 'codemirror/addon/display/autorefresh';
import 'codemirror/addon/comment/comment';
import 'codemirror/addon/edit/matchbrackets';
import 'codemirror/keymap/sublime';
+ import 'codemirror/theme/monokai.css';

const code = 'const a = 0;';

  <CodeMirror
    value={code}
    options={{
+    theme: 'monokai',
      keyMap: 'sublime',
      mode: 'jsx',
    }}
  />

相关文章

网友评论

      本文标题:在线代码编辑器CodeMirror的轻量React组件

      本文链接:https://www.haomeiwen.com/subject/cjmwyktx.html