feat(editor): customCreateMonacoEditor options中新增editorCustomType,可以用于创建不同的editor

This commit is contained in:
roymondchen 2026-01-07 17:38:37 +08:00
parent df611571c7
commit cfaaaad66e
8 changed files with 66 additions and 6 deletions

View File

@ -9,6 +9,7 @@
}" }"
:autosize="config.autosize" :autosize="config.autosize"
:parse="config.parse" :parse="config.parse"
:editor-custom-type="config.mFormItemType"
@save="save" @save="save"
></MagicCodeEditor> ></MagicCodeEditor>
</template> </template>

View File

@ -234,6 +234,7 @@ const dataSourceFieldsConfig: FormConfig = [
name: 'defaultValue', name: 'defaultValue',
text: '默认值', text: '默认值',
parse: true, parse: true,
mFormItemType: 'data-source-field-defaultValue',
type: (mForm: FormState | undefined, { model }: any) => { type: (mForm: FormState | undefined, { model }: any) => {
if (model.type === 'number') return 'number'; if (model.type === 'number') return 'number';
if (model.type === 'boolean') return 'select'; if (model.type === 'boolean') return 'select';

View File

@ -37,8 +37,9 @@
<MagicCodeEditor <MagicCodeEditor
v-if="config.advanced && showCode" v-if="config.advanced && showCode"
:init-values="model[name]" editor-custom-type="m-fields-key-value"
language="javascript" language="javascript"
:init-values="model[name]"
:options="{ :options="{
readOnly: disabled, readOnly: disabled,
}" }"

View File

@ -1,5 +1,5 @@
<template> <template>
<div :class="`magic-code-editor`"> <div class="magic-code-editor">
<Teleport to="body" :disabled="!fullScreen"> <Teleport to="body" :disabled="!fullScreen">
<div :class="{ 'magic-code-editor-wrapper': true, 'full-screen': fullScreen }" :style="{ height: computeHeight }"> <div :class="{ 'magic-code-editor-wrapper': true, 'full-screen': fullScreen }" :style="{ height: computeHeight }">
<TMagicButton <TMagicButton
@ -47,6 +47,7 @@ const props = withDefaults(
minRows?: number; minRows?: number;
maxRows?: number; maxRows?: number;
}; };
editorCustomType?: string;
}>(), }>(),
{ {
initValues: '', initValues: '',
@ -63,6 +64,7 @@ const props = withDefaults(
const emit = defineEmits(['initd', 'save']); const emit = defineEmits(['initd', 'save']);
const autoHeight = ref<string>(''); const autoHeight = ref<string>('');
let cachedExtraHeight: number | null = null;
const computeHeight = computed(() => { const computeHeight = computed(() => {
if (fullScreen.value) { if (fullScreen.value) {
@ -80,6 +82,41 @@ const computeHeight = computed(() => {
return '100%'; return '100%';
}); });
const calculateExtraHeight = (): number => {
let extraHeight = 10; //
if (vsEditor && codeEditorEl.value) {
try {
//
const editorElement = codeEditorEl.value.querySelector('.monaco-editor');
const scrollableElement = codeEditorEl.value.querySelector('.monaco-scrollable-element');
if (editorElement && scrollableElement) {
const editorRect = editorElement.getBoundingClientRect();
const scrollableRect = scrollableElement.getBoundingClientRect();
//
extraHeight = Math.max(editorRect.height - scrollableRect.height, 0);
// 使
if (extraHeight === 0) {
const editorOptions = vsEditor.getOptions();
const scrollBeyondLastLine = editorOptions.get(monaco.editor.EditorOption.scrollBeyondLastLine);
const padding = editorOptions.get(monaco.editor.EditorOption.padding);
const lineHeight = editorOptions.get(monaco.editor.EditorOption.lineHeight) || 20;
extraHeight = (scrollBeyondLastLine ? lineHeight : 0) + (padding?.top || 0) + (padding?.bottom || 0) + 10; //
}
}
} catch (error) {
//
console.warn('Failed to calculate editor extra height:', error);
}
}
return extraHeight;
};
const setAutoHeight = (v = '') => { const setAutoHeight = (v = '') => {
let lines = Math.max(v.split('\n').length, props.autosize?.minRows || 1); let lines = Math.max(v.split('\n').length, props.autosize?.minRows || 1);
if (v) { if (v) {
@ -95,7 +132,12 @@ const setAutoHeight = (v = '') => {
lineHeight = editorOptions.get(monaco.editor.EditorOption.lineHeight) || 20; lineHeight = editorOptions.get(monaco.editor.EditorOption.lineHeight) || 20;
} }
const newHeight = `${lines * lineHeight + 10}px`; //
if (cachedExtraHeight === null) {
cachedExtraHeight = calculateExtraHeight();
}
const newHeight = `${lines * lineHeight + cachedExtraHeight}px`;
// //
if (autoHeight.value !== newHeight) { if (autoHeight.value !== newHeight) {
@ -210,10 +252,14 @@ const init = async () => {
await nextTick(); await nextTick();
} }
//
cachedExtraHeight = null;
const options = { const options = {
value: values.value, value: values.value,
language: props.language, language: props.language,
theme: 'vs-dark', theme: 'vs-dark',
editorCustomType: props.editorCustomType,
...props.options, ...props.options,
}; };
@ -297,6 +343,9 @@ onBeforeUnmount(() => {
vsEditor = null; vsEditor = null;
vsDiffEditor = null; vsDiffEditor = null;
//
cachedExtraHeight = null;
}); });
onUnmounted(() => { onUnmounted(() => {
codeEditorEl.value?.removeEventListener('keydown', handleKeyDown); codeEditorEl.value?.removeEventListener('keydown', handleKeyDown);

View File

@ -7,7 +7,13 @@
<slot name="content-before"></slot> <slot name="content-before"></slot>
<slot name="src-code" v-if="showSrc"> <slot name="src-code" v-if="showSrc">
<CodeEditor class="m-editor-content" :init-values="root" :options="codeOptions" @save="saveCode"></CodeEditor> <CodeEditor
class="m-editor-content"
editor-custom-type="m-editor-content"
:init-values="root"
:options="codeOptions"
@save="saveCode"
></CodeEditor>
</slot> </slot>
<SplitView <SplitView

View File

@ -32,6 +32,7 @@
<CodeEditor <CodeEditor
v-if="showSrc" v-if="showSrc"
class="m-editor-props-panel-src-code" class="m-editor-props-panel-src-code"
editor-custom-type="m-editor-props-panel-src-code"
:height="`${editorContentHeight}px`" :height="`${editorContentHeight}px`"
:init-values="codeValueKey ? values[codeValueKey] : values" :init-values="codeValueKey ? values[codeValueKey] : values"
:options="codeOptions" :options="codeOptions"

View File

@ -123,12 +123,12 @@ export interface EditorInstallOptions {
customCreateMonacoEditor: ( customCreateMonacoEditor: (
monaco: typeof Monaco, monaco: typeof Monaco,
codeEditorEl: HTMLElement, codeEditorEl: HTMLElement,
options: Monaco.editor.IStandaloneEditorConstructionOptions, options: Monaco.editor.IStandaloneEditorConstructionOptions & { editorCustomType?: string },
) => Monaco.editor.IStandaloneCodeEditor; ) => Monaco.editor.IStandaloneCodeEditor;
customCreateMonacoDiffEditor: ( customCreateMonacoDiffEditor: (
monaco: typeof Monaco, monaco: typeof Monaco,
codeEditorEl: HTMLElement, codeEditorEl: HTMLElement,
options: Monaco.editor.IStandaloneEditorConstructionOptions, options: Monaco.editor.IStandaloneEditorConstructionOptions & { editorCustomType?: string },
) => Monaco.editor.IStandaloneDiffEditor; ) => Monaco.editor.IStandaloneDiffEditor;
[key: string]: any; [key: string]: any;
} }

View File

@ -43,6 +43,7 @@ export interface CodeConfig extends FormItem {
minRows?: number; minRows?: number;
maxRows?: number; maxRows?: number;
}; };
mFormItemType?: string;
} }
export interface CodeLinkConfig extends FormItem { export interface CodeLinkConfig extends FormItem {