vue codemirror sql编辑器功能 可自定义提醒(关键字,库名,表名),高亮,主题
工作中再一次需要开发sql编辑器,优化上篇文章内容 https://www.cnblogs.com/Lu-Lu/p/14388888.html
本次功能是tab页打开多个sql编辑器,效果图:

安装:
cnpm i codemirror
HTML:
<textarea
:id="'mycode'+(index*1+1)"
:ref="'mycode'+(index*1+1)"
v-model="item.sqlContent"
class="CodeMirror-hints"
:class="'mycode'+(index*1+1)"
placeholder="按Ctrl键进行代码提示"
/>
JS:
import CodeMirror from 'codemirror'
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/solarized.css'
import 'codemirror/addon/edit/closebrackets.js'
import 'codemirror/mode/sql/sql.js'
import 'codemirror/addon/display/autorefresh'
import 'codemirror/addon/hint/show-hint.js'
import 'codemirror/addon/hint/show-hint.css'
import 'codemirror/addon/hint/sql-hint.js'
init (ind, sqlId) {
const _this = this
let code = 'mycode'+ind
this.$nextTick(() => {
// 实例初始化
this.editor = CodeMirror.fromTextArea(this.$refs[code][0], {
tabSize: 4,
mode: 'text/x-mysql', //语言sql
theme: 'solarized', // 主题
autoCloseBrackets: true, // 在键入时自动关闭括号和引号
autoRefresh: true,
styleActiveLine: true,
lineNumbers: true,
line: true,
lineWrapping: true,
readOnly: "nocursor", //默认只读,无光标,如果是true好像有bug
hintOptions: { //自定义提示内容
completeSingle: false,
hint: this.handleShowHint2,
},
// extraKeys: {
// 'Ctrl-Space': editor => {
// editor.showHint()
// }
// }
})
// 下方代码基本不用改动
this.editor.on('keypress', editor => {
const __Cursor = editor.getDoc().getCursor()
const __Token = editor.getTokenAt(__Cursor)
// console.log(__Cursor)
// console.log(__Token)
if (
__Token.type &&
__Token.type !== 'string' &&
__Token.type !== 'punctuation' &&
__Token.string.indexOf('.') === -1
) {
// 把输入的关键字统一变成大写字母
editor.replaceRange(
__Token.string.toUpperCase(),
{
line: __Cursor.line,
ch: __Token.start
},
{
line: __Cursor.line,
ch: __Token.end
},
__Token.string
)
}
if (this.timeout) {
clearTimeout(this.timeout)
}
// this.timeout = setTimeout(() => {
// console.log('调用接口')
// }, 500)
editor.showHint()
})
// 用户实时输入监听
this.editor.on('cursorActivity', function (editor) {
const __Cursor = editor.getDoc().getCursor()
const __Token = editor.getTokenAt(__Cursor)
const { string } = __Token
// console.log(__Cursor, __Token, string)
// console.log(string)
_this.formatHint(string)
if (string.charAt(string.length - 1) === '.') {
const curIndex = __Token.start
const curLine = _this.editor.getLine(__Cursor.line)
const key = curLine.slice(curLine.slice(0, curIndex).lastIndexOf(' ') + 1, curIndex) // 点前的关键字
console.log('keykeykeykeykey', key)
}
})
console.log('让编辑器每次在调用的时候进行自动刷新', this.editor)
this.editor.on('change', editor => {
this.editableTabs[ind-1].sql = editor.getValue()
if (this.$emit) {
this.$emit('input', this.editableTabs[ind-1].sql)
}
})
// 将所有codemirror存放对象中
this.codeMirrorObj[sqlId] = this.editor
console.log(this.codeMirrorObj)
})
},
// 获取关键词/库名/表名接口
getKeyWordList(data) {
this.hintOp = []
api.getKeyWordList(data.datasourceId).then((req) => {
if(req.data.state) {
req.data.value.forEach((item) => {
let obj = {
text: item.wordname,
displayText: item.wordname,
displayInfo: item.wordfrom,
type: item.wordtype,
render: this.hintRender,
}
this.hintOp.push(obj)
})
}
this.hintOpAll = this.hintOp
})
},
// 过滤hintOption
formatHint(val) {
this.hintOp = []
this.hintOpAll.forEach((item) => {
//统一变成大写去匹配,否则无法匹配大小写
if(item.displayText.toUpperCase().indexOf(val.toUpperCase()) == 0) {
this.hintOp.push(item)
}
})
},
handleShowHint2(cmInstance, hintOptions) {
let cursor = cmInstance.getCursor();
let cursorLine = cmInstance.getLine(cursor.line);
let end = cursor.ch;
let start = end;
let token = cmInstance.getTokenAt(cursor)
// console.log(cmInstance, cursor, cursorLine, end, token)
return {
list: this.hintOp,
// [{
// text: "abcd",
// displayText: "abcd",
// displayInfo: "提示信息1",
// render: this.hintRender
// }, {
// text: "qwer",
// displayText: "qwer",
// displayInfo: "提示信息2",
// render: this.hintRender
// }],
from: {
ch: token.start, line: cursor.line
},
to: {
ch: token.end, line: cursor.line
}
}
},
// 提示框显示及样式
hintRender(element, self, data) {
let div = document.createElement("div");
div.setAttribute("class", "autocomplete-div");
// 添加弹出框表/字段图标
let divIcon = ''
if(data.type == 'table') {
divIcon = document.createElement("i");
divIcon.setAttribute("class", "el-icon-date");
divIcon.style.color = 'blue'
} else if(data.type == 'field') {
divIcon = document.createElement("i");
divIcon.setAttribute("class", "el-icon-film");
divIcon.style.color = 'blue'
} else {
divIcon = document.createElement("i");
}
let divText = document.createElement("span");
divText.setAttribute("class", "autocomplete-name");
divText.innerText = data.displayText;
divText.style.display = 'inline-block'
divText.style.overflow = 'hidden'
divText.style.whiteSpace = 'nowrap'
divText.style.textOverflow = 'ellipsis'
divText.style.marginRight = '10px'
let divInfo = document.createElement("span");
divInfo.setAttribute("class", "autocomplete-hint");
divInfo.innerText = data.displayInfo;
divInfo.style.display = 'inline-block'
divInfo.style.float = 'right'
divInfo.style.color = 'gray'
divInfo.style.maxWidth = '150px'
divInfo.style.overflow = 'hidden'
divInfo.style.whiteSpace = 'nowrap'
divInfo.style.textOverflow = 'ellipsis'
div.appendChild(divIcon);
div.appendChild(divText);
div.appendChild(divInfo);
element.appendChild(div);
},
文章禁止转载或请注明出处!
vue codemirror sql编辑器功能 可自定义提醒(关键字,库名,表名),高亮,主题的更多相关文章
- 为了让开发者写MaxCompute SQL更爽,DataWorks 增强SQL 编辑器功能
众所周知,数据开发和分析的同学每天都要花大量时间写MaxCompute SQL:Dataworks作为数据开发的IDE直接影响着大家的开发效率,这次新上线的Dataworks我们在编辑体验上做了很多工 ...
- SQL保留关键字不能用作表名
com.microsoft.sqlserver.jdbc.SQLServerException: 关键字 'User' 附近有语法错误. 一看就是SQL语句错误,发现控制台console上打印出来的S ...
- 小白日记41:kali渗透测试之Web渗透-SQL手工注入(三)-猜测列名、表名、库名、字段内容,数据库写入
SQL手工注入 靶机:metasploitable(低) 1.当无权读取infomation_schema库[MySQL最重要的源数据库,必须有root权限]/拒绝union.order by语句 ...
- SQL Server 在数据库中查找字符串(不知道表名的情况下 查找字符串)
declare @key varchar(30)set @key = '广州' --替换为要查找的字符串DECLARE @tabName VARCHAR(40),@colName VARCHAR(40 ...
- java连接sqlserver2008报错 java.sql.SQLException: 对象名 '表名' 无效.
注意:c3p0的数据库配置方式为: <named-config name="sqlsvr"> <property name="driverClass&q ...
- 【转】Sql Server查看所有数据库名,表名,字段名(SQL语句)
-- 获取所有数据库名 select * from master..SysDatabases; -- 获取hotline数据库中所有表名 select name from hotline..SysOb ...
- 使用自定义验证组件库扩展 Windows 窗体
使用自定义验证组件库扩展 Windows 窗体 1(共 1)对本文的评价是有帮助 - 评价此主题 发布日期 : 8/24/20 ...
- SQL语句表名或者字段名和保留字冲突解决方法
最近开发遇到一个很奇葩的问题,简单做一下笔记 select * from Add ... 以上SQL语句会报错. 原因Add是表名,SQL语句保留字中又有Add 解决方法: select * from ...
- 在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示
在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示) 1.使用npm安装依赖 npm install --save codemirror; 2.在页面中放入如下代码 ...
- 【Unity】自定义编辑器窗口——拓展编辑器功能
最近学习了Unity自定义编辑器窗口,下面简单总结,方便用到时回顾. 新建一个脚本: using UnityEngine; using System.Collections; using UnityE ...
随机推荐
- 7.22考试总结(NOIP模拟23)[联·赛·题]
不拼尽全力去试一下,又怎么会知道啊 前言 又是被细节问题搞掉的一天. T1 的话,与正解相差无几,少打了两个 else 一个 ls 打成了 rs,然后就爆零了(本来还有 45pts 的),然后加了一个 ...
- python-去掉写入csv文件的多余的一行空白行
如执行下面的代码: 1 import csv 2 3 if __name__ == "__main__": 4 5 content1 = ['hello'] 6 content2 ...
- vue3使用表格el-table-infinite-scroll.js:18 Uncaught (in promise) Error: [el-table-infinite-scroll]: .el-scrollbar__wrap element not found.
先看下表格里面有没有这个el-scrollbar__wrap class类 没有的话升级一下element-plus到最新的就行 你可以先查看element-plus的版本 npm view elem ...
- vscode git bash终端配置:“”message": "此项已弃用,配置默认 shell 的新推荐方法是在 `#terminal.integrated.profiles.windows#
当VSCode升级至1.57.1(2021.6.17)时,会出现警告提示:""message": "此项已弃用,配置默认 shell 的新推荐方法是在 `#te ...
- 一文搞懂 ARM 64 系列: 寄存器
ARM 64中包含多种寄存器,下面介绍一些常见的寄存器. 1 通用寄存器 ARM 64包含31个64bit寄存器,记为X0~X30. 每一个通用寄存器,它的低32bit都可以被访问,记为W0~W30. ...
- ASP.NET MVC 出现: Uncaught ReferenceError: $ is not defined
ASP.NET MVC 出现: Uncaught ReferenceError: $ is not defined 错误 将 _Layout.cshtml 中的三行代码,移动到 <head> ...
- ssm框架使springmvc放行资源(java配置类)
在springmvc中,如果配置了拦截所有请求交给springmvc处理,会出现一些静态web资源加载不出来的情况,或者想放行指定web资源可以通过修改通过修改配置达到相应目的,这里使用覆写WebMv ...
- DHorse v1.5.1 发布,基于 k8s 的发布平台
版本说明 新增特性 支持k8s的v1.30.x版本: 优化特性 优化回滚功能: 修复注册来源的回滚问题: 新增和修改应用时校验应用名: 升级kubernetes-client至v6.13.0: 调整部 ...
- MySQL bit类型增加索引后查询结果不正确案例浅析
昨天同事遇到的一个案例,这里简单描述一下:一个表里面有一个bit类型的字段,同事在优化相关SQL的过程中,给这个表的bit类型的字段新增了一个索引,然后测试验证 时,居然发现SQL语句执行结果跟不加索 ...
- 视觉语言跨模态特征语义相似度计算改进--表征空间维度语义依赖感知聚合算法 ACM MM
论文链接:Unlocking the Power of Cross-Dimensional Semantic Dependency for Image-Text Matching (ACM MM23) ...