feat(代码编辑器): 代码自动格式化

This commit is contained in:
baiqi 2024-02-26 23:09:14 +08:00 committed by Craftsman
parent 9b2c2552ce
commit e0cbf9ae2a
3 changed files with 63 additions and 50 deletions

View File

@ -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<HTMLElement | null>();
//
@ -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 {

View File

@ -121,12 +121,14 @@
activeLayout === 'horizontal' ? ' pr-[16px]' : ''
}`"
>
<MsTab
v-model:active-key="requestVModel.activeTab"
:content-tab-list="contentTabList"
:get-text-func="getTabBadge"
class="no-content relative mb-[8px] border-b border-[var(--color-text-n8)]"
/>
<div>
<MsTab
v-model:active-key="requestVModel.activeTab"
:content-tab-list="contentTabList"
:get-text-func="getTabBadge"
class="no-content relative mb-[8px] border-b border-[var(--color-text-n8)]"
/>
</div>
<div class="tab-pane-container">
<template v-if="requestVModel.activeTab === RequestComposition.PLUGIN || isInitPluginForm">
<a-spin

View File

@ -109,7 +109,7 @@
<MsCodeEditor
v-if="activeTab === ResponseComposition.BODY"
ref="responseEditorRef"
:model-value="props.response.requestResults[0]?.responseResult?.body || ''"
:model-value="props.response.requestResults[0]?.responseResult?.body"
:language="responseLanguage"
theme="vs"
height="100%"
@ -130,7 +130,6 @@
</MsCodeEditor>
<MsCodeEditor
v-else-if="activeTab === ResponseComposition.CONSOLE"
ref="responseEditorRef"
:model-value="response.console.trim()"
:language="LanguageEnum.PLAINTEXT"
theme="MS-text"
@ -284,17 +283,6 @@
});
const responseEditorRef = ref<InstanceType<typeof MsCodeEditor>>();
watch(
() => props.response,
(obj) => {
if (obj.requestResults[0].responseResult.body.trim() !== '') {
nextTick(() => {
responseEditorRef.value?.format();
});
}
}
);
const responseTabList = [
{
label: t('apiTestDebug.responseBody'),