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(); const { t } = useI18n();
// //
let editor: monaco.editor.IStandaloneCodeEditor; let editor: monaco.editor.IStandaloneCodeEditor;
const codeContainerRef = ref(); 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 // ref
const fullRef = ref<HTMLElement | null>(); const fullRef = ref<HTMLElement | null>();
// //
@ -235,12 +208,49 @@
} }
function format() { function format() {
if (editor) { if (editor && editor.getValue() !== '' && props.language !== LanguageEnum.PLAINTEXT) {
// TODO: editor.updateOptions({ readOnly: false });
editor.getAction('editor.action.formatDocument')?.run(); //
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( watch(
() => props.modelValue, () => props.modelValue,
(newValue) => { (newValue) => {
@ -250,7 +260,8 @@
editor.setValue(newValue); editor.setValue(newValue);
} }
} }
} },
{ immediate: true }
); );
watch( watch(
@ -264,6 +275,7 @@
watch( watch(
() => props.language, () => props.language,
(newValue) => { (newValue) => {
currentLanguage.value = newValue;
monaco.editor.setModelLanguage(editor.getModel()!, newValue.toLowerCase()); // ENUM monaco monaco.editor.setModelLanguage(editor.getModel()!, newValue.toLowerCase()); // ENUM monaco
} }
); );
@ -275,6 +287,17 @@
onMounted(() => { onMounted(() => {
init(); init();
setEditBoxBg(); setEditBoxBg();
if (props.readOnly) {
format();
}
});
onUpdated(() => {
setTimeout(() => {
if (props.readOnly) {
format();
}
}, 100); // TODO:
}); });
return { return {

View File

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

View File

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