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> <!DOCTYPE html>
<html lang="en"> <html lang="zh_CN">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />

View File

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

View File

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