使用场景:项目中需要手动输入SQL字段,根据后台返回字段进行提示
1. index.html 下引用(我使用的是静态文件)
文件位置 node_modules\monaco-editor\min
<body>
<div id="app"></div>
<script defer src="./static/monaco/min/vs/loader.js"></script>
<script defer>
window.addEventListener('DOMContentLoaded',function(){
require.config({ paths: { 'vs': './static/monaco/min/vs' } });
require(['vs/editor/editor.main'], function () {})
})
</script>
</body>
2. 具体场景使用
<template>
<div class="sql-editor"></div>
</template>
<script>
export default {
name: 'ExecuteAction',
data() {
return {
KEYWORDS: [
{
label: "*",
insertText: "*",
kind: 17,
detail: '关键字'
},
{
label: "SELECT",
insertText: "SELECT",
kind: 17,
detail: '关键字'
},
{
label: "FROM",
insertText: "FROM",
kind: 17,
detail: '关键字'
},
{
label: "ORDER",
insertText: "ORDER",
kind: 17,
detail: '关键字'
},
{
label: "BY",
insertText: "BY",
kind: 17,
detail: '关键字'
},
{
label: "ASC",
insertText: "ASC",
kind: 17,
detail: '关键字'
},
{
label: "UPDATE",
insertText: "UPDATE",
kind: 17,
detail: '关键字'
},
{
label: "WHERE",
insertText: "WHERE",
kind: 17,
detail: '关键字'
},
{
label: "INSERT",
insertText: "INSERT",
kind: 17,
detail: '关键字'
},
{
label: "MAX",
insertText: "MAX",
kind: 17,
detail: '关键字'
},
{
label: "MIN",
insertText: "MIN",
kind: 17,
detail: '关键字'
},
{
label: "SUM",
insertText: "SUM",
kind: 17,
detail: '关键字'
}
],
DBNAMES: [
],
DB_FIELDS: {
}
}
},
mounted() {
this.ColumnTableName()
const container = document.querySelector('.sql-editor')
this.editor = monaco.editor.create(container, {
minimap: { enabled: false },
value: '',
language: 'sql',
contextmenu: false,
theme: 'BlackTheme',
})
monaco.editor.defineTheme('BlackTheme', {
base: 'vs',
inherit: true,
rules: [{ background: '#000000' }],
colors: {
"editor.background": "#f7f8fa",
"editorWidget.background": "#ffffff",
"editor.foreground": "#000000",
"editorSuggestWidget.background": "#f3f3f3",
"list.highlightForeground": "#ff4444",
"list.hoverBackground": "#e3e2e2",
"list.activeSelectionBackground": "#0060c0",
}
});
this.completionItemProvider = monaco.languages.registerCompletionItemProvider('sql', {
provideCompletionItems: this.CompletionItemKindController,
triggerCharacters: [' ']
})
this.editor.onDidChangeModelContent(() => {
const newValue = this.editor.getValue()
this.$emit('changeTextarea', newValue)
console.log(newValue)
})
monaco.editor.setTheme('BlackTheme');
},
beforeDestroy() {
if (this.editor) {
this.editor.dispose()
this.editor = null
}
if (this.completionItemProvider) {
this.completionItemProvider.dispose()
this.editor = null
}
},
methods: {
CompletionItemKindController(model, position) {
console.log(model,position);
let suggestions = []
const { lineNumber, column } = position
const textBeforePointer = model.getValueInRange({
startLineNumber: lineNumber,
startColumn: 0,
endLineNumber: lineNumber,
endColumn: column,
})
const contents = textBeforePointer.trim().split(/\s+/)
const lastContents = contents[contents?.length - 1]
if (lastContents) {
const lowerContent = lastContents.toLowerCase()
if (lowerContent === 'from') {
suggestions = this.copyObj(this.DBNAMES)
} else if (lowerContent === 'where') {
const DBNAMEIDX = contents.findLastIndex((item) => (item.toLowerCase() === 'from'))
const DBNAME = contents[DBNAMEIDX + 1]
const DBNAME_ARR = DBNAME.split(',')
let fieldsTmp = []
for (let i = 0; i < DBNAME_ARR.length; i++) {
const fields = this.DB_FIELDS[DBNAME_ARR[i]]
fieldsTmp = fieldsTmp.concat(fields)
}
suggestions = this.copyObj(fieldsTmp)
} else {
suggestions = this.copyObj(this.KEYWORDS)
}
}
console.log(suggestions)
return {
suggestions
}
},
GetEditorValue() {
if (!this.editor) return
return this.editor.getValue()
},
SetEditorValue(value) {
if (!this.editor) return
this.editor.setValue(value)
},
copyObj(obj) {
return JSON.parse(JSON.stringify(obj))
}
}
}
</script>
注意1
- KEYWORDS 为默认代码提示语句(根据具体业务)
- DBNAMES 为一级代码提示
- DB_FIELDS 为二级代码提示
- 具体提示方法通过CompletionItemKindController方法处理
注意2
二级代码需要用一级代码的id为键名
举例
DBNAMES = [ { label: "current_db1", insertText: "current_catalog_db1", kind: 1, detail: '数据库表名' },]
DB_FIELDS = {
current_db1:[
{
label: "id",
insertText: "id",
kind: 3,
detail: '字段名'
},
]
}