最近有这样一个需求,网页中须要作一个SQL编辑器,要求有一些基本的SQL编辑器功能,最后选中基于比较完善的CodeMirror来开发相关功能。本片文章涵盖的基本功能包括CodeMirror在React中的引入、输入联想、执行选中部分SQL等功能。由于项目基于React来开发,因此此篇指南也使用React作为示例。css
npm install codemirror --save
复制代码
import React from 'react';
import codemirror from 'codemirror';
require('codemirror/codemirror.css'); // CodeMirrory原生样式
require('codemirror/mode/sql/sql');
require('codemirror/mode/shell/shell');
require('codemirror/addon/display/placeholder');
require('codemirror/addon/hint/show-hint.css'); // 用来作代码提示
require('codemirror/addon/hint/show-hint.js'); // 用来作代码提示
require('codemirror/addon/hint/sql-hint.js'); // 用来作代码提示
复制代码
CodeMirror组件:react
class CodeMirror extends React.Component {
static defaultProps = {
useFocus: true
}
componentDidMount() {
this.paste = '';
const {
onChange, onBlur, options, value = '', onScroll,
onCursorActivity, onInputRead,
} = this.props;
this.editor = codemirror(this.ref, {
indentWithTabs: true,
smartIndent: true,
lineNumbers: true,
matchBrackets: true,
autofocus: true,
extraKeys: { Tab: 'autocomplete' },
hintOptions: { completeSingle: false }
lineWrapping: true,
value
});
const { editor, setCursor } = this;
setCursor(editor, true);
const changeDelay = debounce((e) => {
setCursor(e);
onChange && onChange(e.getValue());
}, 300);
editor.on('change', changeDelay);
editor.on('blur', (e) => {
setCursor(e);
onBlur && onBlur(e.getValue());
});
editor.on('cursorActivity', onCursorActivity);
editor.on('inputRead', (cm, change) => onInputRead(cm, change, editor));
onScroll && editor.on('scroll', onScroll);
}
shouldComponentUpdate({ paste = '', value = '', }) {
const { editor } = this;
if (paste !== this.paste) {
this.focus();
editor.replaceSelection(` ${paste}`);
this.paste = paste;
} else if (value !== editor.getValue()) {
editor.setOption('value', value);
editor.setValue(value);
this.fixBottom();
this.focus();
}
return false;
}
setCursor = (editor, toEnd) => {
const { line, ch } = editor.doc.getCursor();
this.cursor = { ch, line: toEnd ? line + 1 : line };
}
focus() {
const { editor } = this;
const { options: { readOnly }, useFocus } = this.props;
if (readOnly) return;
if (!useFocus) return;
editor.focus();
editor.setCursor({ ...this.cursor }, readOnly);
}
fixBottom() {
const { fixBottom } = this.props;
if (!fixBottom) return;
const { editor } = this;
const { height } = editor.getScrollInfo();
editor.scrollTo(0, height);
}
render() {
const { className, options: { readOnly } } = this.props;
return (
<div
className={`${className} ${readOnly && 'readOnly'}`}
ref={(self) => { this.ref = self; }}
/>
);
}
}
复制代码
子组件调用CodeMirror组件sql
class SqlConsole extends Component {
static defaultProps = {
value: '',
sqlPaste: '',
onChange: () => {},
}
state = {
showModal: false,
source: {},
};
uuid = '';
onCursorActivity = (cm) => {
if (!cm.getSelection()) {
console.log(cm.getSelection()); // 获取到选中部份内容,用来实现执行部份内容
}
}
onInputRead = async (cm, change, editor) => {
const { text } = change;
const dechars = [
'.',
];
const autocomplete = dechars.includes(text[0]);
if (autocomplete) {
const data = getTableList(); // 获取库表列表
editor.setOption('hintOptions', {
tables: data,
completeSingle: false
});
cm.execCommand('autocomplete');
} else {
const tableName = getTableList(); // 获取表列表
editor.setOption('hintOptions', {
tables: tableName,
completeSingle: false
});
}
cm.execCommand('autocomplete');
}
render() {
const { sqlPaste, onChange, } = this.props;
return (
<CodeMirror
className="sql"
value={pane.sql}
paste={sqlPaste}
onChange={sql => { onChange(sql); }} // sql变化事件
onCursorActivity={(cm) => this.onCursorActivity(cm)} // 用来完善选中监听
onInputRead={// 自动补全
(cm, change, editor) => this.onInputRead(cm, change, editor)
}
/>
);
}
}
复制代码
经过以上两个父子模块,可实现SQL编辑器基本功能,关键部分代码已作注释。接下来详细解释下其中的细节。shell
自动补全主要是依靠设置CodeMirror中的hintOptions来实现的,固然首先须要将依赖的hint包都引入。npm
const data = { table1: [''], table2: [''], table3: [''] };
cm.on('inputRead', (cm, change) => {
cm.setOption('hintOptions', {
tables: data,
completeSingle: false
});
cm.execCommand('autocomplete');
});
复制代码
其中cm是CodeMirror中inputRead事件的第一个参数,其实也是CodeMirror实例自己。 总结下自动补全实现的流程就是:编程
执行选中部分代码关键是要监听选中事件,获取选中部份内容,这两个方法分别是cursorActivity和getSelection。bash
cm.on('cursorActivity', (cm) => {
cm.getSelection(); // 选中内容
});
复制代码
cm为CodeMirror实例,经过这两个个简单的方法,能够实现获取选中部份内容。判断选中内容不为空时展现执行部分SQL便可。app
CodeMirror是一个功能较完善的代码编辑框,其支持不少种编程语言,以上仅展现了SQL的,该框架文档比较难啃,欢迎有依赖该框架作开发的小伙伴们一块儿交流。若是看了这篇文章对我总结的方法有任何问题,也欢迎留言,随缘回复。框架