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

137 lines
2.9 KiB
Vue
Raw Normal View History

<script>
2017-07-24 12:15:41 -04:00
/* global monaco */
import { mapState, 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 {
2018-01-06 13:59:49 -05:00
computed: {
...mapGetters([
'activeFile',
'activeFileExtension',
]),
...mapState([
'leftPanelCollapsed',
'rightPanelCollapsed',
'panelResizing',
]),
shouldHideEditor() {
return this.activeFile.binary && !this.activeFile.raw;
},
},
watch: {
activeFile(oldVal, newVal) {
if (newVal && !newVal.active) {
this.initMonaco();
}
},
leftPanelCollapsed() {
this.editor.updateDimensions();
},
rightPanelCollapsed() {
this.editor.updateDimensions();
},
panelResizing(isResizing) {
if (isResizing === false) {
this.editor.updateDimensions();
}
},
},
2018-01-06 14:54:45 -05:00
beforeDestroy() {
this.editor.dispose();
},
mounted() {
if (this.editor && 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',
'setFileLanguage',
'setEditorPosition',
'setFileEOL',
]),
initMonaco() {
if (this.shouldHideEditor) return;
this.editor.clearEditor();
2017-08-15 15:53:41 -04:00
this.getRawFileData(this.activeFile)
.then(() => {
2017-11-28 11:51:37 -05:00
this.editor.createInstance(this.$refs.editor);
})
.then(() => this.setupEditor())
2018-01-19 04:38:34 -05:00
.catch((err) => {
flash('Error setting up monaco. Please try again.', 'alert', document, null, false, true);
throw err;
});
},
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
});
});
// Handle Cursor Position
this.editor.onPositionChange((instance, e) => {
this.setEditorPosition({
editorRow: e.position.lineNumber,
editorColumn: e.position.column,
});
});
this.editor.setPosition({
lineNumber: this.activeFile.editorRow,
column: this.activeFile.editorColumn,
});
// Handle File Language
this.setFileLanguage({
fileLanguage: model.language,
});
// Get File eol
this.setFileEOL({
eol: model.eol,
});
},
},
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>
2017-11-28 11:51:37 -05:00
<div
v-show="!shouldHideEditor"
ref="editor"
class="multi-file-editor-holder"
2017-11-28 11:51:37 -05:00
>
</div>
</div>
</template>