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"
:parse="config.parse"
:editor-custom-type="config.mFormItemType"
@save="save"
></MagicCodeEditor>
</template>

View File

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

View File

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

View File

@ -1,5 +1,5 @@
<template>
<div :class="`magic-code-editor`">
<div class="magic-code-editor">
<Teleport to="body" :disabled="!fullScreen">
<div :class="{ 'magic-code-editor-wrapper': true, 'full-screen': fullScreen }" :style="{ height: computeHeight }">
<TMagicButton
@ -47,6 +47,7 @@ const props = withDefaults(
minRows?: number;
maxRows?: number;
};
editorCustomType?: string;
}>(),
{
initValues: '',
@ -63,6 +64,7 @@ const props = withDefaults(
const emit = defineEmits(['initd', 'save']);
const autoHeight = ref<string>('');
let cachedExtraHeight: number | null = null;
const computeHeight = computed(() => {
if (fullScreen.value) {
@ -80,6 +82,41 @@ const computeHeight = computed(() => {
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 = '') => {
let lines = Math.max(v.split('\n').length, props.autosize?.minRows || 1);
if (v) {
@ -95,7 +132,12 @@ const setAutoHeight = (v = '') => {
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) {
@ -210,10 +252,14 @@ const init = async () => {
await nextTick();
}
//
cachedExtraHeight = null;
const options = {
value: values.value,
language: props.language,
theme: 'vs-dark',
editorCustomType: props.editorCustomType,
...props.options,
};
@ -297,6 +343,9 @@ onBeforeUnmount(() => {
vsEditor = null;
vsDiffEditor = null;
//
cachedExtraHeight = null;
});
onUnmounted(() => {
codeEditorEl.value?.removeEventListener('keydown', handleKeyDown);

View File

@ -7,7 +7,13 @@
<slot name="content-before"></slot>
<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>
<SplitView

View File

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

View File

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

View File

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