mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-04-06 03:57:56 +08:00
fix(editor): 代码编辑器大小变化没有自适应大小
This commit is contained in:
parent
f9d23a4423
commit
7dc54797d2
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent, onMounted, onUnmounted, ref, watch } from 'vue';
|
import { defineComponent, onMounted, onUnmounted, ref, watch } from 'vue';
|
||||||
|
import { throttle } from 'lodash-es';
|
||||||
import * as monaco from 'monaco-editor';
|
import * as monaco from 'monaco-editor';
|
||||||
import serialize from 'serialize-javascript';
|
import serialize from 'serialize-javascript';
|
||||||
|
|
||||||
@ -36,14 +37,19 @@ export default defineComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
type: {
|
type: {
|
||||||
type: [String],
|
type: String,
|
||||||
default: () => '',
|
default: () => '',
|
||||||
},
|
},
|
||||||
|
|
||||||
language: {
|
language: {
|
||||||
type: [String],
|
type: String,
|
||||||
default: () => 'javascript',
|
default: () => 'javascript',
|
||||||
},
|
},
|
||||||
|
|
||||||
|
options: {
|
||||||
|
type: Object,
|
||||||
|
default: () => ({}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
emits: ['initd', 'save'],
|
emits: ['initd', 'save'],
|
||||||
@ -51,10 +57,18 @@ export default defineComponent({
|
|||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
let vsEditor: monaco.editor.IStandaloneCodeEditor | null = null;
|
let vsEditor: monaco.editor.IStandaloneCodeEditor | null = null;
|
||||||
let vsDiffEditor: monaco.editor.IStandaloneDiffEditor | null = null;
|
let vsDiffEditor: monaco.editor.IStandaloneDiffEditor | null = null;
|
||||||
|
|
||||||
const values = ref('');
|
const values = ref('');
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const codeEditor = ref<HTMLDivElement>();
|
const codeEditor = ref<HTMLDivElement>();
|
||||||
|
|
||||||
|
const resizeObserver = new globalThis.ResizeObserver(
|
||||||
|
throttle((): void => {
|
||||||
|
vsEditor?.layout();
|
||||||
|
vsDiffEditor?.layout();
|
||||||
|
}, 300),
|
||||||
|
);
|
||||||
|
|
||||||
const setEditorValue = (v: string | any, m: string | any) => {
|
const setEditorValue = (v: string | any, m: string | any) => {
|
||||||
values.value = toString(v, props.language);
|
values.value = toString(v, props.language);
|
||||||
|
|
||||||
@ -71,11 +85,6 @@ export default defineComponent({
|
|||||||
return vsEditor?.setValue(values.value);
|
return vsEditor?.setValue(values.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const resizeHandler = () => {
|
|
||||||
vsEditor?.layout();
|
|
||||||
vsDiffEditor?.layout();
|
|
||||||
};
|
|
||||||
|
|
||||||
const getEditorValue = () =>
|
const getEditorValue = () =>
|
||||||
props.type === 'diff' ? vsDiffEditor?.getModifiedEditor().getValue() : vsEditor?.getValue();
|
props.type === 'diff' ? vsDiffEditor?.getModifiedEditor().getValue() : vsEditor?.getValue();
|
||||||
|
|
||||||
@ -88,8 +97,9 @@ export default defineComponent({
|
|||||||
tabSize: 2,
|
tabSize: 2,
|
||||||
theme: 'vs-dark',
|
theme: 'vs-dark',
|
||||||
fontFamily: 'dm, Menlo, Monaco, "Courier New", monospace',
|
fontFamily: 'dm, Menlo, Monaco, "Courier New", monospace',
|
||||||
fontSize: 15,
|
fontSize: 14,
|
||||||
formatOnPaste: true,
|
formatOnPaste: true,
|
||||||
|
...props.options,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (props.type === 'diff') {
|
if (props.type === 'diff') {
|
||||||
@ -117,7 +127,7 @@ export default defineComponent({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
globalThis.addEventListener('resize', resizeHandler);
|
resizeObserver.observe(codeEditor.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
@ -140,7 +150,7 @@ export default defineComponent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
globalThis.removeEventListener('resize', resizeHandler);
|
resizeObserver.disconnect();
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user