feat(editor): code组件新增options props

This commit is contained in:
roymondchen 2022-09-23 19:54:04 +08:00 committed by jia000
parent dafcf33255
commit 1c3565035c
4 changed files with 144 additions and 148 deletions

View File

@ -3,6 +3,7 @@
:style="`height: ${height}`" :style="`height: ${height}`"
:init-values="model[name]" :init-values="model[name]"
:language="language" :language="language"
:options="config.options"
@save="save" @save="save"
></magic-code-editor> ></magic-code-editor>
</template> </template>
@ -17,6 +18,7 @@ const props = defineProps<{
name: string; name: string;
config: { config: {
language?: string; language?: string;
options?: Object;
}; };
prop: string; prop: string;
}>(); }>();

View File

@ -12,6 +12,7 @@ const props = defineProps<{
name: string; name: string;
text?: string; text?: string;
formTitle?: string; formTitle?: string;
codeOptions?: Object;
}; };
model: any; model: any;
name: string; name: string;
@ -20,17 +21,24 @@ const props = defineProps<{
const emit = defineEmits(['change']); const emit = defineEmits(['change']);
const formConfig = computed(() => ({ const formConfig = computed(() => {
...props.config, const { codeOptions, ...config } = props.config;
return {
...config,
text: '', text: '',
type: 'link', type: 'link',
form: [ form: [
{ {
name: props.name, name: props.name,
type: 'vs-code', type: 'vs-code',
options: {
tabSize: 2,
...(codeOptions || {}),
},
}, },
], ],
})); };
});
const modelValue = reactive<{ form: Record<string, string> }>({ const modelValue = reactive<{ form: Record<string, string> }>({
form: { form: {

View File

@ -63,7 +63,7 @@ export default {
app.component('m-fields-ui-select', uiSelect); app.component('m-fields-ui-select', uiSelect);
app.component('m-fields-code-link', CodeLink); app.component('m-fields-code-link', CodeLink);
app.component('m-fields-vs-code', Code); app.component('m-fields-vs-code', Code);
app.component(CodeEditor.name, CodeEditor); app.component('magic-code-editor', CodeEditor);
app.component('m-fields-code-select', CodeSelect); app.component('m-fields-code-select', CodeSelect);
}, },
}; };

View File

@ -2,12 +2,35 @@
<div ref="codeEditor" class="magic-code-editor"></div> <div ref="codeEditor" class="magic-code-editor"></div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, onMounted, onUnmounted, ref, watch } from 'vue'; import { onMounted, onUnmounted, ref, watch } from 'vue';
import { throttle } from 'lodash-es'; 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';
const props = withDefaults(
defineProps<{
initValues?: string | Object;
modifiedValues?: string | Object;
type?: string;
language?: string;
options?: {
[key: string]: any;
};
autoSave?: boolean;
}>(),
{
type: '',
autoSave: true,
language: 'javascript',
options: () => ({
tabSize: 2,
}),
},
);
const emit = defineEmits(['initd', 'save']);
const toString = (v: string | any, language: string): string => { const toString = (v: string | any, language: string): string => {
let value = ''; let value = '';
if (typeof v !== 'string') { if (typeof v !== 'string') {
@ -24,52 +47,21 @@ const toString = (v: string | any, language: string): string => {
return value; return value;
}; };
export default defineComponent({ let vsEditor: monaco.editor.IStandaloneCodeEditor | null = null;
name: 'magic-code-editor', let vsDiffEditor: monaco.editor.IStandaloneDiffEditor | null = null;
props: { const values = ref('');
initValues: { const loading = ref(false);
type: [String, Object], const codeEditor = ref<HTMLDivElement>();
},
modifiedValues: { const resizeObserver = new globalThis.ResizeObserver(
type: [String, Object],
},
type: {
type: String,
default: () => '',
},
language: {
type: String,
default: () => 'javascript',
},
options: {
type: Object,
default: () => ({}),
},
},
emits: ['initd', 'save'],
setup(props, { emit }) {
let vsEditor: monaco.editor.IStandaloneCodeEditor | null = null;
let vsDiffEditor: monaco.editor.IStandaloneDiffEditor | null = null;
const values = ref('');
const loading = ref(false);
const codeEditor = ref<HTMLDivElement>();
const resizeObserver = new globalThis.ResizeObserver(
throttle((): void => { throttle((): void => {
vsEditor?.layout(); vsEditor?.layout();
vsDiffEditor?.layout(); vsDiffEditor?.layout();
}, 300), }, 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);
if (props.type === 'diff') { if (props.type === 'diff') {
@ -83,12 +75,12 @@ export default defineComponent({
} }
return vsEditor?.setValue(values.value); return vsEditor?.setValue(values.value);
}; };
const getEditorValue = () => const getEditorValue = () =>
props.type === 'diff' ? vsDiffEditor?.getModifiedEditor().getValue() : vsEditor?.getValue(); props.type === 'diff' ? vsDiffEditor?.getModifiedEditor().getValue() : vsEditor?.getValue();
const init = async () => { const init = async () => {
if (!codeEditor.value) return; if (!codeEditor.value) return;
const options = { const options = {
@ -118,16 +110,16 @@ export default defineComponent({
} }
}); });
if (props.type !== 'diff') { if (props.type !== 'diff' && props.autoSave) {
vsEditor?.onDidBlurEditorWidget(() => { vsEditor?.onDidBlurEditorWidget(() => {
emit('save', getEditorValue()); emit('save', getEditorValue());
}); });
} }
resizeObserver.observe(codeEditor.value); resizeObserver.observe(codeEditor.value);
}; };
watch( watch(
() => props.initValues, () => props.initValues,
(v, preV) => { (v, preV) => {
if (v !== preV) { if (v !== preV) {
@ -138,23 +130,19 @@ export default defineComponent({
deep: true, deep: true,
immediate: true, immediate: true,
}, },
); );
onMounted(async () => { onMounted(async () => {
loading.value = true; loading.value = true;
init(); init();
}); });
onUnmounted(() => { onUnmounted(() => {
resizeObserver.disconnect(); resizeObserver.disconnect();
}); });
return {
values,
loading,
codeEditor,
defineExpose({
getEditor() { getEditor() {
return vsEditor || vsDiffEditor; return vsEditor || vsDiffEditor;
}, },
@ -165,7 +153,5 @@ export default defineComponent({
vsEditor?.focus(); vsEditor?.focus();
vsDiffEditor?.focus(); vsDiffEditor?.focus();
}, },
};
},
}); });
</script> </script>