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;
text: '', return {
type: 'link', ...config,
form: [ text: '',
{ type: 'link',
name: props.name, form: [
type: 'vs-code', {
}, name: props.name,
], 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,148 +47,111 @@ 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], throttle((): void => {
}, vsEditor?.layout();
vsDiffEditor?.layout();
}, 300),
);
type: { const setEditorValue = (v: string | any, m: string | any) => {
type: String, values.value = toString(v, props.language);
default: () => '',
},
language: { if (props.type === 'diff') {
type: String, const originalModel = monaco.editor.createModel(values.value, 'text/javascript');
default: () => 'javascript', const modifiedModel = monaco.editor.createModel(toString(m, props.language), 'text/javascript');
},
options: { return vsDiffEditor?.setModel({
type: Object, original: originalModel,
default: () => ({}), modified: modifiedModel,
}, });
}
return vsEditor?.setValue(values.value);
};
const getEditorValue = () =>
props.type === 'diff' ? vsDiffEditor?.getModifiedEditor().getValue() : vsEditor?.getValue();
const init = async () => {
if (!codeEditor.value) return;
const options = {
value: values.value,
language: props.language,
theme: 'vs-dark',
...props.options,
};
if (props.type === 'diff') {
vsDiffEditor = monaco.editor.createDiffEditor(codeEditor.value, options);
} else {
vsEditor = monaco.editor.create(codeEditor.value, options);
}
setEditorValue(props.initValues, props.modifiedValues);
loading.value = false;
emit('initd', vsEditor);
codeEditor.value.addEventListener('keydown', (e) => {
if (e.keyCode === 83 && (navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
e.stopPropagation();
emit('save', getEditorValue());
}
});
if (props.type !== 'diff' && props.autoSave) {
vsEditor?.onDidBlurEditorWidget(() => {
emit('save', getEditorValue());
});
}
resizeObserver.observe(codeEditor.value);
};
watch(
() => props.initValues,
(v, preV) => {
if (v !== preV) {
setEditorValue(props.initValues, props.modifiedValues);
}
},
{
deep: true,
immediate: true,
},
);
onMounted(async () => {
loading.value = true;
init();
});
onUnmounted(() => {
resizeObserver.disconnect();
});
defineExpose({
getEditor() {
return vsEditor || vsDiffEditor;
}, },
emits: ['initd', 'save'], setEditorValue,
setup(props, { emit }) { focus() {
let vsEditor: monaco.editor.IStandaloneCodeEditor | null = null; vsEditor?.focus();
let vsDiffEditor: monaco.editor.IStandaloneDiffEditor | null = null; vsDiffEditor?.focus();
const values = ref('');
const loading = ref(false);
const codeEditor = ref<HTMLDivElement>();
const resizeObserver = new globalThis.ResizeObserver(
throttle((): void => {
vsEditor?.layout();
vsDiffEditor?.layout();
}, 300),
);
const setEditorValue = (v: string | any, m: string | any) => {
values.value = toString(v, props.language);
if (props.type === 'diff') {
const originalModel = monaco.editor.createModel(values.value, 'text/javascript');
const modifiedModel = monaco.editor.createModel(toString(m, props.language), 'text/javascript');
return vsDiffEditor?.setModel({
original: originalModel,
modified: modifiedModel,
});
}
return vsEditor?.setValue(values.value);
};
const getEditorValue = () =>
props.type === 'diff' ? vsDiffEditor?.getModifiedEditor().getValue() : vsEditor?.getValue();
const init = async () => {
if (!codeEditor.value) return;
const options = {
value: values.value,
language: props.language,
theme: 'vs-dark',
...props.options,
};
if (props.type === 'diff') {
vsDiffEditor = monaco.editor.createDiffEditor(codeEditor.value, options);
} else {
vsEditor = monaco.editor.create(codeEditor.value, options);
}
setEditorValue(props.initValues, props.modifiedValues);
loading.value = false;
emit('initd', vsEditor);
codeEditor.value.addEventListener('keydown', (e) => {
if (e.keyCode === 83 && (navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
e.stopPropagation();
emit('save', getEditorValue());
}
});
if (props.type !== 'diff') {
vsEditor?.onDidBlurEditorWidget(() => {
emit('save', getEditorValue());
});
}
resizeObserver.observe(codeEditor.value);
};
watch(
() => props.initValues,
(v, preV) => {
if (v !== preV) {
setEditorValue(props.initValues, props.modifiedValues);
}
},
{
deep: true,
immediate: true,
},
);
onMounted(async () => {
loading.value = true;
init();
});
onUnmounted(() => {
resizeObserver.disconnect();
});
return {
values,
loading,
codeEditor,
getEditor() {
return vsEditor || vsDiffEditor;
},
setEditorValue,
focus() {
vsEditor?.focus();
vsDiffEditor?.focus();
},
};
}, },
}); });
</script> </script>