This commit is contained in:
刘宗洋
2022-11-19 15:05:44 +08:00
parent 480e930385
commit 231af72444
3 changed files with 38 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="zh_CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

View File

@@ -1,9 +1,20 @@
/* eslint-disable */
import {IDisposable} from 'monaco-editor';
declare module '*.vue' {
import type { DefineComponent } from 'vue';
const component: DefineComponent<{}, {}, any>;
export default component;
}
declare global {
interface Window {
completionItemProvider?: IDisposable | undefined;
}
}
declare module 'sql-formatter';
declare module 'jsoneditor';
declare module 'asciinema-player';

View File

@@ -246,7 +246,7 @@
</template>
<script lang="ts" setup>
import { onMounted, computed, toRefs, reactive, ref, watch } from 'vue';
import {onMounted, computed, toRefs, reactive, ref, watch} from 'vue';
import { dbApi } from './api';
import { format as sqlFormatter } from 'sql-formatter';
@@ -362,8 +362,7 @@ const getThemeConfig: any = computed(() => {
return store.state.themeConfig.themeConfig;
});
let monacoEditor: editor.IStandaloneCodeEditor = null;
let completionItemProvider: any = null;
let monacoEditor = {} as editor.IStandaloneCodeEditor;
self.MonacoEnvironment = {
getWorker() {
@@ -387,7 +386,7 @@ const initMonacoEditor = () => {
mouseWheelZoom: true, // 在按住Ctrl键的同时使用鼠标滚轮时在编辑器中缩放字体
overviewRulerBorder: false, // 不要滚动条的边框
tabSize: 2, // tab 缩进长度
fontFamily: 'JetBrainsMono', // 字体 暂时不要设置,否则光标容易错位
// fontFamily: 'JetBrainsMono', // 字体 暂时不要设置,否则光标容易错位
fontWeight: 'bold',
// letterSpacing: 1, 字符间距
// quickSuggestions:false, // 禁用代码提示
@@ -402,14 +401,14 @@ const initMonacoEditor = () => {
// A label of the action that will be presented to the user.
label: '执行SQL',
// A precondition for this action.
precondition: null,
precondition: undefined,
// A rule to evaluate on top of the precondition in order to dispatch the keybindings.
keybindingContext: null,
keybindingContext: undefined,
keybindings: [
// chord
monaco.KeyMod.chord(
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyR
)
, 0)
],
contextMenuGroupId: 'navigation',
contextMenuOrder: 1.5,
@@ -430,14 +429,14 @@ const initMonacoEditor = () => {
// A label of the action that will be presented to the user.
label: '格式化SQL',
// A precondition for this action.
precondition: null,
precondition: undefined,
// A rule to evaluate on top of the precondition in order to dispatch the keybindings.
keybindingContext: null,
keybindingContext: undefined,
keybindings: [
// chord
monaco.KeyMod.chord(
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyF
)
,0)
],
contextMenuGroupId: 'navigation',
contextMenuOrder: 2,
@@ -456,10 +455,9 @@ const initMonacoEditor = () => {
// monaco.editor.setTheme('hc-black');
// 参考 https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-completion-provider-example
completionItemProvider = monaco.languages.registerCompletionItemProvider('sql', {
self.completionItemProvider = monaco.languages.registerCompletionItemProvider('sql', {
triggerCharacters: ['.'],
provideCompletionItems: async (model: editor.ITextModel, position: Position): Promise<languages.CompletionList | null | undefined> => {
let word = model.getWordUntilPosition(position);
const { lineNumber, column } = position
const { startColumn, endColumn } = word
@@ -701,6 +699,7 @@ select * from invisit v where`.match(/(join|from)\s+(\w*-?\w*\.?\w+)\s*(as)?\s*(
}
onMounted(() => {
self.completionItemProvider?.dispose()
setHeight();
initMonacoEditor();
// 监听浏览器窗口大小变化,更新对应组件高度
@@ -1001,7 +1000,7 @@ const getColumnTip = (tableName: string, columnName: string) => {
const getSql = () => {
let res = '' as string | undefined;
// 编辑器还没初始化
if (!monacoEditor.getModel) {
if (!monacoEditor?.getModel) {
return res;
}
// 选择选中的sql
@@ -1514,6 +1513,9 @@ const addRow = async () => {
*/
const formatSql = () => {
let selection = monacoEditor.getSelection()
if(!selection){
return;
}
let sql = monacoEditor.getModel()?.getValueInRange(selection)
// 有选中sql则格式化并替换选中sql, 否则格式化编辑器所有内容
if (sql) {
@@ -1527,12 +1529,15 @@ const formatSql = () => {
* 替换选中的内容
*/
const replaceSelection = (str: string, selection: any) => {
const model = monacoEditor.getModel();
if(!model){
return;
}
if (!selection) {
monacoEditor.getModel().setValue(str);
model.setValue(str);
return;
}
const { startLineNumber, endLineNumber, startColumn, endColumn } = selection
const model = monacoEditor.getModel();
const textBeforeSelection = model.getValueInRange({
startLineNumber: 1,