2021-01-03 14:25:43 +05:30
|
|
|
<script>
|
|
|
|
import { debounce } from 'lodash';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { CONTENT_UPDATE_DEBOUNCE, EDITOR_READY_EVENT } from '~/editor/constants';
|
2021-09-30 23:02:18 +05:30
|
|
|
import Editor from '~/editor/source_editor';
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
function initSourceEditor({ el, ...args }) {
|
2021-01-03 14:25:43 +05:30
|
|
|
const editor = new Editor({
|
|
|
|
scrollbar: {
|
|
|
|
alwaysConsumeMouseWheel: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return editor.createInstance({
|
|
|
|
el,
|
|
|
|
...args,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
inheritAttrs: false,
|
|
|
|
props: {
|
|
|
|
value: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
fileName: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
// This is used to help uniquely create a monaco model
|
|
|
|
// even if two blob's share a file path.
|
|
|
|
fileGlobalId: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
extensions: {
|
|
|
|
type: [String, Array],
|
|
|
|
required: false,
|
|
|
|
default: () => null,
|
|
|
|
},
|
|
|
|
editorOptions: {
|
|
|
|
type: Object,
|
|
|
|
required: false,
|
|
|
|
default: () => ({}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
loading: true,
|
|
|
|
editor: null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
fileName(newVal) {
|
|
|
|
this.editor.updateModelLanguage(newVal);
|
|
|
|
},
|
|
|
|
value(newVal) {
|
|
|
|
if (this.editor.getValue() !== newVal) {
|
|
|
|
this.editor.setValue(newVal);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
2021-09-30 23:02:18 +05:30
|
|
|
this.editor = initSourceEditor({
|
2021-01-03 14:25:43 +05:30
|
|
|
el: this.$refs.editor,
|
|
|
|
blobPath: this.fileName,
|
|
|
|
blobContent: this.value,
|
|
|
|
blobGlobalId: this.fileGlobalId,
|
|
|
|
extensions: this.extensions,
|
|
|
|
...this.editorOptions,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editor.onDidChangeModelContent(
|
|
|
|
debounce(this.onFileChange.bind(this), CONTENT_UPDATE_DEBOUNCE),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
this.editor.dispose();
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onFileChange() {
|
|
|
|
this.$emit('input', this.editor.getValue());
|
|
|
|
},
|
2021-03-08 18:12:59 +05:30
|
|
|
getEditor() {
|
|
|
|
return this.editor;
|
|
|
|
},
|
2021-01-03 14:25:43 +05:30
|
|
|
},
|
2021-03-11 19:13:27 +05:30
|
|
|
readyEvent: EDITOR_READY_EVENT,
|
2021-01-03 14:25:43 +05:30
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div
|
2021-09-30 23:02:18 +05:30
|
|
|
:id="`source-editor-${fileGlobalId}`"
|
2021-01-03 14:25:43 +05:30
|
|
|
ref="editor"
|
|
|
|
data-editor-loading
|
2021-03-11 19:13:27 +05:30
|
|
|
@[$options.readyEvent]="$emit($options.readyEvent)"
|
2021-01-03 14:25:43 +05:30
|
|
|
>
|
|
|
|
<pre class="editor-loading-content">{{ value }}</pre>
|
|
|
|
</div>
|
|
|
|
</template>
|