gitlab-org--gitlab-foss/app/assets/javascripts/repo/components/repo_editor.vue

85 lines
1.7 KiB
Vue
Raw Normal View History

<script>
2017-07-24 12:15:41 -04:00
/* global monaco */
import { mapGetters, mapActions } from 'vuex';
import flash from '../../flash';
import monacoLoader from '../monaco_loader';
import Editor from '../lib/editor';
2017-07-24 12:15:41 -04:00
export default {
2017-08-03 22:11:14 -04:00
destroyed() {
this.editor.dispose();
2017-08-03 22:11:14 -04:00
},
2017-07-24 12:15:41 -04:00
mounted() {
if (this.monaco) {
this.initMonaco();
} else {
monacoLoader(['vs/editor/editor.main'], () => {
this.editor = Editor.create(monaco);
this.initMonaco();
});
}
2017-10-19 06:55:27 -04:00
},
methods: {
...mapActions([
'getRawFileData',
'changeFileContent',
]),
initMonaco() {
if (this.shouldHideEditor) return;
this.editor.clearEditor();
2017-08-15 15:53:41 -04:00
this.getRawFileData(this.activeFile)
.then(() => {
this.editor.createInstance(this.$el);
})
.then(() => this.setupEditor())
2017-11-22 11:40:06 -05:00
.catch(() => flash('Error setting up monaco. Please try again.'));
},
setupEditor() {
if (!this.activeFile) return;
const model = this.editor.createModel(this.activeFile);
this.editor.attachModel(model);
model.onChange((m) => {
this.changeFileContent({
file: this.activeFile,
content: m.getValue(),
2017-10-05 05:54:46 -04:00
});
});
},
},
watch: {
activeFile(oldVal, newVal) {
2017-10-27 13:16:28 -04:00
if (newVal && !newVal.active) {
this.initMonaco();
2017-10-05 05:54:46 -04:00
}
},
2017-07-24 12:15:41 -04:00
},
2017-08-15 14:16:42 -04:00
computed: {
...mapGetters([
'activeFile',
'activeFileExtension',
]),
2017-08-15 14:16:42 -04:00
shouldHideEditor() {
return this.activeFile.binary && !this.activeFile.raw;
2017-08-15 14:16:42 -04:00
},
},
2017-07-24 12:15:41 -04:00
};
</script>
2017-07-24 12:15:41 -04:00
<template>
<div
id="ide"
class="blob-viewer-container blob-editor-container"
>
<div
v-if="shouldHideEditor"
v-html="activeFile.html"
>
</div>
</div>
</template>