From 24f543e667144feab804ff0b49cba3510fff809b Mon Sep 17 00:00:00 2001
From: "meilin.huang" <954537473@qq.com>
Date: Fri, 25 Nov 2022 18:59:23 +0800
Subject: [PATCH] =?UTF-8?q?refactor:=20redis=E5=80=BC=E6=A0=BC=E5=BC=8F?=
 =?UTF-8?q?=E5=8C=96=E6=96=B9=E5=BC=8F=E8=B0=83=E6=95=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
 mayfly_go_web/package.json                    |   2 +-
 .../src/components/monaco/MonacoEditor.vue    |  35 ++++--
 mayfly_go_web/src/theme/app.scss              |   4 +
 .../src/views/ops/redis/FormatInput.vue       | 108 ++++++++++++++++++
 .../src/views/ops/redis/HashValue.vue         |   9 +-
 .../src/views/ops/redis/ListValue.vue         |   7 +-
 .../src/views/ops/redis/RedisEdit.vue         |   3 +-
 .../src/views/ops/redis/SetValue.vue          |   9 +-
 .../src/views/ops/redis/StringValue.vue       |  22 +---
 mayfly_go_web/yarn.lock                       |   8 +-
 10 files changed, 162 insertions(+), 45 deletions(-)
 create mode 100644 mayfly_go_web/src/views/ops/redis/FormatInput.vue
diff --git a/mayfly_go_web/package.json b/mayfly_go_web/package.json
index a39c4b12..a0267546 100644
--- a/mayfly_go_web/package.json
+++ b/mayfly_go_web/package.json
@@ -13,7 +13,7 @@
     "countup.js": "^2.0.7",
     "cropperjs": "^1.5.11",
     "echarts": "^5.3.3",
-    "element-plus": "^2.2.22",
+    "element-plus": "^2.2.23",
     "jsencrypt": "^3.2.1",
     "lodash": "^4.17.21",
     "mitt": "^3.0.0",
diff --git a/mayfly_go_web/src/components/monaco/MonacoEditor.vue b/mayfly_go_web/src/components/monaco/MonacoEditor.vue
index 76c6828b..99fd9e4d 100644
--- a/mayfly_go_web/src/components/monaco/MonacoEditor.vue
+++ b/mayfly_go_web/src/components/monaco/MonacoEditor.vue
@@ -11,7 +11,7 @@
 import { ref, watch, toRefs, reactive, onMounted, onBeforeUnmount } from 'vue';
 import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js?worker';
 import * as monaco from 'monaco-editor';
-import { editor } from 'monaco-editor';
+import { editor, languages } from 'monaco-editor';
 import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'
 // 主题仓库 https://github.com/brijeshb42/monaco-themes
 // 主题例子 https://editor.bitwiser.in/
@@ -59,6 +59,10 @@ const languages = [
         value: 'shell',
         label: 'Shell',
     },
+    {
+        value: 'json',
+        label: 'JSON',
+    },
     {
         value: 'yaml',
         label: 'Yaml',
@@ -87,10 +91,6 @@ const languages = [
         value: 'javascript',
         label: 'Javascript',
     },
-    {
-        value: 'json',
-        label: 'JSON',
-    },
     {
         value: 'java',
         label: 'Java',
@@ -165,7 +165,7 @@ watch(() => props.language, (newValue: any) => {
 
 const monacoTextarea: any = ref(null);
 
-let monacoEditorIns: editor.IStandaloneCodeEditor = null;
+let monacoEditorIns: editor.IStandaloneCodeEditor = null as any;
 let completionItemProvider: any = null;
 
 self.MonacoEnvironment = {
@@ -185,11 +185,11 @@ const initMonacoEditorIns = () => {
     options.language = state.languageMode;
     // 从localStorage中获取,通过store可能存在父子组件都使用store报错
     options.theme = JSON.parse(localStorage.getItem('themeConfig') as string).editorTheme || 'vs';
-    monacoEditorIns = monaco.editor.create(monacoTextarea.value, Object.assign(options, props.options));
+    monacoEditorIns = monaco.editor.create(monacoTextarea.value, Object.assign(options, props.options as any));
 
     // 监听内容改变,双向绑定
     monacoEditorIns.onDidChangeModelContent(() => {
-        emit('update:modelValue', monacoEditorIns.getModel().getValue());
+        emit('update:modelValue', monacoEditorIns.getModel()?.getValue());
     })
 
     // 动态设置主题
@@ -200,6 +200,9 @@ const changeLanguage = (value: any) => {
     console.log('change lan');
     // 获取当前的文档模型
     let oldModel = monacoEditorIns.getModel()
+    if (!oldModel) {
+        return;
+    }
     // 创建一个新的文档模型
     let newModel = monaco.editor.createModel(oldModel.getValue(), value)
     // 设置成新的
@@ -213,7 +216,7 @@ const changeLanguage = (value: any) => {
 }
 
 const setEditorValue = (value: any) => {
-    monacoEditorIns.getModel().setValue(value)
+    monacoEditorIns.getModel()?.setValue(value)
 }
 
 /**
@@ -237,14 +240,14 @@ const registeShell = () => {
                     label: item,
                     kind: monaco.languages.CompletionItemKind.Keyword,
                     insertText: item,
-                });
+                } as any);
             })
             shellLan.builtins.forEach((item: any) => {
                 suggestions.push({
                     label: item,
                     kind: monaco.languages.CompletionItemKind.Property,
                     insertText: item,
-                });
+                } as any);
             })
             return {
                 suggestions: suggestions
@@ -252,6 +255,16 @@ const registeShell = () => {
         }
     })
 };
+
+const format = () => {
+    /*
+    触发自动格式化;
+   */
+    monacoEditorIns.trigger('', 'editor.action.formatDocument', '')
+}
+
+defineExpose({ format })
+
 
 
 
diff --git a/mayfly_go_web/src/views/ops/redis/HashValue.vue b/mayfly_go_web/src/views/ops/redis/HashValue.vue
index 98d6feba..f3253e24 100644
--- a/mayfly_go_web/src/views/ops/redis/HashValue.vue
+++ b/mayfly_go_web/src/views/ops/redis/HashValue.vue
@@ -1,5 +1,5 @@
 
-    
+    
         
             
                 
@@ -33,7 +33,7 @@
                     
                 
             
-            
+            
                 
                     
                         
@@ -41,8 +41,8 @@
                 
                 
                     
-                        
+                        
                     
                 
                 
@@ -68,6 +68,7 @@ import { reactive, watch, toRefs } from 'vue';
 import { redisApi } from './api';
 import { ElMessage, ElMessageBox } from 'element-plus';
 import { isTrue, notEmpty } from '@/common/assert';
+import FormatInput from './FormatInput.vue';
 
 const props = defineProps({
     visible: {
diff --git a/mayfly_go_web/src/views/ops/redis/ListValue.vue b/mayfly_go_web/src/views/ops/redis/ListValue.vue
index 59f1687d..1dbb1fc7 100644
--- a/mayfly_go_web/src/views/ops/redis/ListValue.vue
+++ b/mayfly_go_web/src/views/ops/redis/ListValue.vue
@@ -1,5 +1,5 @@
 
-    
+    
         
             
                 
@@ -18,8 +18,8 @@
             
                 
                     
-                        
+                        
                     
                 
                 
@@ -48,6 +48,7 @@
 import { reactive, watch, toRefs } from 'vue';
 import { redisApi } from './api';
 import { ElMessage } from 'element-plus';
+import FormatInput from './FormatInput.vue';
 
 const props = defineProps({
     visible: {
diff --git a/mayfly_go_web/src/views/ops/redis/RedisEdit.vue b/mayfly_go_web/src/views/ops/redis/RedisEdit.vue
index 47e367b2..a894370f 100644
--- a/mayfly_go_web/src/views/ops/redis/RedisEdit.vue
+++ b/mayfly_go_web/src/views/ops/redis/RedisEdit.vue
@@ -33,7 +33,8 @@
                         
                 
                 
-                    
+                    
                         
                     
diff --git a/mayfly_go_web/src/views/ops/redis/SetValue.vue b/mayfly_go_web/src/views/ops/redis/SetValue.vue
index dde9f1b3..8578dde8 100644
--- a/mayfly_go_web/src/views/ops/redis/SetValue.vue
+++ b/mayfly_go_web/src/views/ops/redis/SetValue.vue
@@ -1,5 +1,5 @@
 
-    
+    
         
             
                 
@@ -15,8 +15,8 @@
             
                 
                     
-                        
+                        
                     
                 
                 
@@ -40,6 +40,7 @@ import { reactive, watch, toRefs } from 'vue';
 import { redisApi } from './api';
 import { ElMessage } from 'element-plus';
 import { isTrue, notEmpty } from '@/common/assert';
+import FormatInput from './FormatInput.vue';
 
 const props = defineProps({
     visible: {
@@ -77,7 +78,7 @@ const state = reactive({
     db: '0',
     key: {
         key: '',
-        type: 'string',
+        type: 'set',
         timed: -1,
     },
     value: [{ value: '' }],
diff --git a/mayfly_go_web/src/views/ops/redis/StringValue.vue b/mayfly_go_web/src/views/ops/redis/StringValue.vue
index be980deb..5b32181e 100644
--- a/mayfly_go_web/src/views/ops/redis/StringValue.vue
+++ b/mayfly_go_web/src/views/ops/redis/StringValue.vue
@@ -12,12 +12,11 @@
             
 
             
-                
-                
+                
+                
             
         
         
@@ -33,7 +32,7 @@ import { reactive, watch, toRefs } from 'vue';
 import { redisApi } from './api';
 import { ElMessage } from 'element-plus';
 import { notEmpty } from '@/common/assert';
-import { formatJsonString } from '@/common/utils/format';
+import FormatInput from './FormatInput.vue';
 
 const props = defineProps({
     visible: {
@@ -144,24 +143,13 @@ const saveValue = async () => {
     notEmpty(state.key.key, 'key不能为空');
 
     notEmpty(state.string.value, 'value不能为空');
-    const sv = { value: formatJsonString(state.string.value, true), id: state.redisId, db: state.db };
+    const sv = { value: state.string.value, id: state.redisId, db: state.db };
     Object.assign(sv, state.key);
     await redisApi.saveStringValue.request(sv);
     ElMessage.success('数据保存成功');
     cancel();
     emit('valChange');
 };
-
-// 更改文本类型
-const onChangeTextType = (val: string) => {
-    if (val == 'json') {
-        state.string.value = formatJsonString(state.string.value, false);
-        return;
-    }
-    if (val == 'text') {
-        state.string.value = formatJsonString(state.string.value, true);
-    }
-};