zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<template>
  <div
    class="app-monaco-sql"
    :style="`height:calc(50vh - ${complementHeight}px)`"
    ref="monacoEditorRef"
  />
</template>
 
 
<script setup>
import * as monaco from 'monaco-editor';
import {language} from 'monaco-editor/esm/vs/basic-languages/sql/sql';
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
 
self.MonacoEnvironment = {
  getWorker() {
    return new editorWorker();
  }
}
/**
 * 定义从父组件接收的属性
 */
const props = defineProps({
  complementHeight: {
    type: Number,
    default: 0
  }
});
 
const monacoEditorRef = ref();
 
/**
 * 代码
 * @type {Ref<UnwrapRef<string>>}
 */
const code = ref('')
 
/**
 *  获取 SQL 的关键字
 */
const {keywords} = language
 
let editor
 
/**
 * 初始化 SQL 代码和表格数据
 * @type {{当前月: *[]}}
 */
const tables = {
  '当前月': []
}
 
/**
 * 编辑器的主题设置
 * @type {string}
 */
const theme = 'Light';
 
/**
 * 组件挂载后创建编辑器实例
 */
const init = ({sql}) => {
  onDispose();
  initAutoCompletion();
  editor = monaco.editor.create(
    monacoEditorRef.value,
    {
      value: sql,
      language: 'sql',
      readOnly: false,
      automaticLayout: true,
      contextmenu: false,
      /**
       * 颜色装饰器
       */
      colorDecorators: true,
      theme,
      minimap: {
        enabled: false
      },
      tabSize: 2,
      fontSize: 16
    }
  );
};
 
/**
 * 组件卸载前销毁编辑器实例
 */
const onDispose = () => {
  if (editor) {
    editor.dispose();
  }
}
 
/**
 * @description: 获取编辑器中填写的值
 */
const getValue = () => {
  return editor.getValue();
}
 
/**
 * @description: 初始化自动补全
 */
const initAutoCompletion = () => {
  monaco.languages.registerCompletionItemProvider('sql', {
    triggerCharacters: ['.', ' ', ...keywords],
    provideCompletionItems: (model, position) => {
      let suggestions = []
      const {lineNumber, column} = position
      const textBeforePointer = model.getValueInRange({
        startLineNumber: lineNumber,
        startColumn: 0,
        endLineNumber: lineNumber,
        endColumn: column
      })
      const words = textBeforePointer.trim().split(/\s+/)
      const lastWord = words[words.length - 1]
 
      if (lastWord.endsWith('.')) {
        const tableName = lastWord.slice(0, lastWord.length - 1)
        if (Object.keys(tables).includes(tableName)) {
          suggestions = [...getFieldsSuggest(tableName)]
        }
      } else if (lastWord === '.') {
        suggestions = []
      } else {
        suggestions = [...getTableSuggest(), ...getKeywordsSuggest()]
      }
 
      return {
        suggestions
      }
    }
  })
}
 
/**
 * @description: 获取关键字的补全列表
 *
 */
const getKeywordsSuggest = () => {
  return keywords.map((key) => ({
    label: key,
    kind: monaco.languages.CompletionItemKind.Keyword,
    insertText: key
  }))
}
 
/**
 * @description: 获取表名的补全列表
 */
const getTableSuggest = () => {
  return Object.keys(tables).map((key) => ({
    label: key,
    kind: monaco.languages.CompletionItemKind.Variable,
    insertText: key
  }))
}
 
/**
 * @description: 根据表名获取字段补全列表
 * @param {*} tableName
 */
const getFieldsSuggest = (tableName) => {
  const fields = tables[tableName]
  if (!fields) {
    return []
  }
  return fields.map((name) => ({
    label: name,
    kind: monaco.languages.CompletionItemKind.Field,
    insertText: name
  }))
}
 
/**
 * 暴露给父组件的参数和方法 (外部需要什么,都可以从这里暴露出去)
 */
defineExpose({
  init,
  getValue,
});
 
</script>
 
<style lang="scss">
.app-monaco-sql {
  .margin {
    background: var(--el-color-primary-light-9);
  }
 
  .monaco-editor-background {
    background: var(--el-color-primary-light-9);
  }
}
</style>