From e0cbf9ae2af722a6428354f0c3f8038a433b6254 Mon Sep 17 00:00:00 2001 From: baiqi Date: Mon, 26 Feb 2024 23:09:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BB=A3=E7=A0=81=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=99=A8):=20=E4=BB=A3=E7=A0=81=E8=87=AA=E5=8A=A8=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/pure/ms-code-editor/index.vue | 85 ++++++++++++------- .../components/requestComposition/index.vue | 14 +-- .../requestComposition/response.vue | 14 +-- 3 files changed, 63 insertions(+), 50 deletions(-) diff --git a/frontend/src/components/pure/ms-code-editor/index.vue b/frontend/src/components/pure/ms-code-editor/index.vue index 29c7f67b93..0405fbf567 100644 --- a/frontend/src/components/pure/ms-code-editor/index.vue +++ b/frontend/src/components/pure/ms-code-editor/index.vue @@ -82,35 +82,8 @@ const { t } = useI18n(); // 编辑器实例,每次调用组件都会创建独立的实例 let editor: monaco.editor.IStandaloneCodeEditor; - const codeContainerRef = ref(); - const init = () => { - // 注册自定义主题 TODO:自定义主题高亮色还没配置 - Object.keys(MsCodeEditorTheme).forEach((e) => { - monaco.editor.defineTheme(e, MsCodeEditorTheme[e as CustomTheme]); - }); - editor = monaco.editor.create(codeContainerRef.value, { - value: props.modelValue, - automaticLayout: true, - padding: { - top: 12, - bottom: 12, - }, - minimap: { - enabled: false, // 将代码块预览隐藏 - }, - ...props, - }); - - // 监听值的变化 - editor.onDidChangeModelContent(() => { - const value = editor.getValue(); // 给父组件实时返回最新文本 - emit('update:modelValue', value); - emit('change', value); - }); - }; - // 用于全屏的容器 ref const fullRef = ref(); // 当前主题 @@ -235,12 +208,49 @@ } function format() { - if (editor) { - // 格式化代码 TODO:需要额外的格式化插件或编写格式化配置 - editor.getAction('editor.action.formatDocument')?.run(); + if (editor && editor.getValue() !== '' && props.language !== LanguageEnum.PLAINTEXT) { + editor.updateOptions({ readOnly: false }); + // 执行格式化代码的动作 + editor + .getAction('editor.action.formatDocument') + ?.run() + .then(() => { + const value = editor.getValue(); // 给父组件实时返回最新文本 + editor.updateOptions({ readOnly: true }); + emit('update:modelValue', value); + emit('change', value); + }); } } + const init = () => { + // 注册自定义主题 TODO:自定义主题高亮色还没配置 + Object.keys(MsCodeEditorTheme).forEach((e) => { + monaco.editor.defineTheme(e, MsCodeEditorTheme[e as CustomTheme]); + }); + editor = monaco.editor.create(codeContainerRef.value, { + value: props.modelValue, + automaticLayout: true, + padding: { + top: 12, + bottom: 12, + }, + minimap: { + enabled: false, // 将代码块预览隐藏 + }, + contextmenu: !props.readOnly, // 只读模式下禁用右键菜单 + ...props, + language: props.language.toLowerCase(), + }); + + // 监听值的变化 + editor.onDidChangeModelContent(() => { + const value = editor.getValue(); // 给父组件实时返回最新文本 + emit('update:modelValue', value); + emit('change', value); + }); + }; + watch( () => props.modelValue, (newValue) => { @@ -250,7 +260,8 @@ editor.setValue(newValue); } } - } + }, + { immediate: true } ); watch( @@ -264,6 +275,7 @@ watch( () => props.language, (newValue) => { + currentLanguage.value = newValue; monaco.editor.setModelLanguage(editor.getModel()!, newValue.toLowerCase()); // 设置语言,语言 ENUM 是大写的,但是 monaco 需要小写 } ); @@ -275,6 +287,17 @@ onMounted(() => { init(); setEditBoxBg(); + if (props.readOnly) { + format(); + } + }); + + onUpdated(() => { + setTimeout(() => { + if (props.readOnly) { + format(); + } + }, 100); // TODO:暂未找到准确的格式化时机 }); return { diff --git a/frontend/src/views/api-test/components/requestComposition/index.vue b/frontend/src/views/api-test/components/requestComposition/index.vue index 93053b1ee5..f2adc2d87c 100644 --- a/frontend/src/views/api-test/components/requestComposition/index.vue +++ b/frontend/src/views/api-test/components/requestComposition/index.vue @@ -121,12 +121,14 @@ activeLayout === 'horizontal' ? ' pr-[16px]' : '' }`" > - +
+ +