mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2026-01-19 11:18:13 +00:00
feat(editor): customCreateMonacoEditor options中新增editorCustomType,可以用于创建不同的editor
This commit is contained in:
parent
df611571c7
commit
cfaaaad66e
@ -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>
|
||||||
|
|||||||
@ -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';
|
||||||
|
|||||||
@ -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,
|
||||||
}"
|
}"
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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"
|
||||||
|
|||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user