CodeMirror 是一款网页中的代码编辑器,能提供例如高亮、缩进、行号、自动补全、语法检查等基础功能
https://codemirror.net/
安装
1 2 3 4 5 6 7 8 9
| npm install codemirror npm install @codemirror/view npm install @codemirror/state
npm install @codemirror/lang-sql npm install @codemirror/commands npm install @codemirror/autocomplete
|
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import { EditorView, keymap} from '@codemirror/view'; import { acceptCompletion } from '@codemirror/autocomplete' import { basicSetup } from 'codemirror'; import { sql } from '@codemirror/lang-sql';
const sqlBlock = document.getElementById('sql-block'); const editorView = new EditorView({ extensions: [ basicSetup, sql(), EditorView.updateListener.of((e) => { sqlstring = e.state.doc.toString(); }), keymap.of([ key: 'Tab', run: acceptCompletion , ]), ], doc: sqlstring, parent: sqlBlock, });
editorView.dispatch({ changes: [{ from: 0, to: editorView.viewState.state.doc.toString().length, insert: 'text', }], });
editorView.destroy();
|
代码检查功能
Codemirror 内置代码检查接口,但是需要外部工具配套使用。
且外部工具必须能运行在浏览器 js 中
https://juejin.cn/s/codemirror%206%20lint%20example