2018-03-20 10:12:48 -04:00
|
|
|
<script>
|
2018-03-25 08:02:09 -04:00
|
|
|
import { mapState, mapGetters, mapActions } from 'vuex';
|
2019-04-17 16:41:07 -04:00
|
|
|
import { viewerInformationForPath } from '~/vue_shared/components/content_viewer/lib/viewer_utils';
|
2018-03-20 10:12:48 -04:00
|
|
|
import flash from '~/flash';
|
2018-04-05 07:12:40 -04:00
|
|
|
import ContentViewer from '~/vue_shared/components/content_viewer/content_viewer.vue';
|
2018-06-13 05:35:52 -04:00
|
|
|
import DiffViewer from '~/vue_shared/components/diff_viewer/diff_viewer.vue';
|
2019-07-23 18:13:36 -04:00
|
|
|
import {
|
|
|
|
activityBarViews,
|
|
|
|
viewerTypes,
|
|
|
|
FILE_VIEW_MODE_EDITOR,
|
|
|
|
FILE_VIEW_MODE_PREVIEW,
|
|
|
|
} from '../constants';
|
2018-03-20 10:12:48 -04:00
|
|
|
import Editor from '../lib/editor';
|
2018-05-31 05:08:34 -04:00
|
|
|
import ExternalLink from './external_link.vue';
|
2018-08-16 07:41:21 -04:00
|
|
|
import FileTemplatesBar from './file_templates/bar.vue';
|
2019-06-24 01:30:35 -04:00
|
|
|
import { __ } from '~/locale';
|
2018-03-20 10:12:48 -04:00
|
|
|
|
|
|
|
export default {
|
2018-04-05 07:12:40 -04:00
|
|
|
components: {
|
|
|
|
ContentViewer,
|
2018-06-13 05:35:52 -04:00
|
|
|
DiffViewer,
|
2018-05-31 05:08:34 -04:00
|
|
|
ExternalLink,
|
2018-08-16 07:41:21 -04:00
|
|
|
FileTemplatesBar,
|
2018-04-05 07:12:40 -04:00
|
|
|
},
|
2018-03-20 10:12:48 -04:00
|
|
|
props: {
|
|
|
|
file: {
|
|
|
|
type: Object,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
computed: {
|
2018-09-27 17:57:30 -04:00
|
|
|
...mapState('rightPane', {
|
|
|
|
rightPaneIsOpen: 'isOpen',
|
|
|
|
}),
|
2020-01-06 10:07:26 -05:00
|
|
|
...mapState([
|
|
|
|
'rightPanelCollapsed',
|
|
|
|
'viewer',
|
|
|
|
'panelResizing',
|
|
|
|
'currentActivityView',
|
|
|
|
'renderWhitespaceInCode',
|
|
|
|
]),
|
2018-05-01 05:46:28 -04:00
|
|
|
...mapGetters([
|
|
|
|
'currentMergeRequest',
|
|
|
|
'getStagedFile',
|
2018-05-02 10:30:51 -04:00
|
|
|
'isEditModeActive',
|
2018-05-01 05:46:28 -04:00
|
|
|
'isCommitModeActive',
|
2018-05-03 09:28:41 -04:00
|
|
|
'isReviewModeActive',
|
2018-05-01 05:46:28 -04:00
|
|
|
]),
|
2018-08-16 07:41:21 -04:00
|
|
|
...mapGetters('fileTemplates', ['showFileTemplatesBar']),
|
2018-03-20 10:12:48 -04:00
|
|
|
shouldHideEditor() {
|
2019-04-17 16:41:51 -04:00
|
|
|
return this.file && this.file.binary;
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
2018-06-13 05:35:52 -04:00
|
|
|
showContentViewer() {
|
|
|
|
return (
|
2019-06-27 13:42:02 -04:00
|
|
|
(this.shouldHideEditor || this.isPreviewViewMode) &&
|
2018-06-13 05:35:52 -04:00
|
|
|
(this.viewer !== viewerTypes.mr || !this.file.mrChange)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
showDiffViewer() {
|
|
|
|
return this.shouldHideEditor && this.file.mrChange && this.viewer === viewerTypes.mr;
|
|
|
|
},
|
2019-06-27 13:42:02 -04:00
|
|
|
isEditorViewMode() {
|
2019-07-23 18:13:36 -04:00
|
|
|
return this.file.viewMode === FILE_VIEW_MODE_EDITOR;
|
2019-06-27 13:42:02 -04:00
|
|
|
},
|
|
|
|
isPreviewViewMode() {
|
2019-07-23 18:13:36 -04:00
|
|
|
return this.file.viewMode === FILE_VIEW_MODE_PREVIEW;
|
2019-06-27 13:42:02 -04:00
|
|
|
},
|
2018-04-05 07:12:40 -04:00
|
|
|
editTabCSS() {
|
|
|
|
return {
|
2019-06-27 13:42:02 -04:00
|
|
|
active: this.isEditorViewMode,
|
2018-04-05 07:12:40 -04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
previewTabCSS() {
|
|
|
|
return {
|
2019-06-27 13:42:02 -04:00
|
|
|
active: this.isPreviewViewMode,
|
2018-04-05 07:12:40 -04:00
|
|
|
};
|
|
|
|
},
|
2019-04-17 16:41:07 -04:00
|
|
|
fileType() {
|
|
|
|
const info = viewerInformationForPath(this.file.path);
|
|
|
|
return (info && info.id) || '';
|
|
|
|
},
|
2019-06-27 13:42:02 -04:00
|
|
|
showEditor() {
|
|
|
|
return !this.shouldHideEditor && this.isEditorViewMode;
|
|
|
|
},
|
2020-01-06 10:07:26 -05:00
|
|
|
editorOptions() {
|
|
|
|
return {
|
|
|
|
renderWhitespace: this.renderWhitespaceInCode ? 'all' : 'none',
|
|
|
|
};
|
|
|
|
},
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
|
|
|
watch: {
|
2018-05-18 09:00:31 -04:00
|
|
|
file(newVal, oldVal) {
|
|
|
|
if (oldVal.pending) {
|
|
|
|
this.removePendingTab(oldVal);
|
|
|
|
}
|
|
|
|
|
2018-04-03 06:10:01 -04:00
|
|
|
// Compare key to allow for files opened in review mode to be cached differently
|
2018-05-18 09:00:31 -04:00
|
|
|
if (oldVal.key !== this.file.key) {
|
2018-06-08 00:56:39 -04:00
|
|
|
this.initEditor();
|
2018-05-01 09:42:55 -04:00
|
|
|
|
|
|
|
if (this.currentActivityView !== activityBarViews.edit) {
|
|
|
|
this.setFileViewMode({
|
|
|
|
file: this.file,
|
2019-07-23 18:13:36 -04:00
|
|
|
viewMode: FILE_VIEW_MODE_EDITOR,
|
2018-05-01 09:42:55 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
currentActivityView() {
|
|
|
|
if (this.currentActivityView !== activityBarViews.edit) {
|
|
|
|
this.setFileViewMode({
|
|
|
|
file: this.file,
|
2019-07-23 18:13:36 -04:00
|
|
|
viewMode: FILE_VIEW_MODE_EDITOR,
|
2018-05-01 09:42:55 -04:00
|
|
|
});
|
2018-03-20 10:12:48 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
rightPanelCollapsed() {
|
2019-06-27 13:42:02 -04:00
|
|
|
this.refreshEditorDimensions();
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
|
|
|
viewer() {
|
2018-07-26 10:56:56 -04:00
|
|
|
if (!this.file.pending) {
|
|
|
|
this.createEditorInstance();
|
|
|
|
}
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
2018-03-28 10:06:50 -04:00
|
|
|
panelResizing() {
|
|
|
|
if (!this.panelResizing) {
|
2019-06-27 13:42:02 -04:00
|
|
|
this.refreshEditorDimensions();
|
2018-03-28 10:06:50 -04:00
|
|
|
}
|
|
|
|
},
|
2018-09-27 17:57:30 -04:00
|
|
|
rightPaneIsOpen() {
|
2019-06-27 13:42:02 -04:00
|
|
|
this.refreshEditorDimensions();
|
|
|
|
},
|
|
|
|
showEditor(val) {
|
|
|
|
if (val) {
|
|
|
|
// We need to wait for the editor to actually be rendered.
|
|
|
|
this.$nextTick(() => this.refreshEditorDimensions());
|
|
|
|
}
|
2018-06-12 09:12:39 -04:00
|
|
|
},
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
this.editor.dispose();
|
|
|
|
},
|
|
|
|
mounted() {
|
2018-06-07 17:34:50 -04:00
|
|
|
if (!this.editor) {
|
2020-01-06 10:07:26 -05:00
|
|
|
this.editor = Editor.create(this.editorOptions);
|
2018-03-20 10:12:48 -04:00
|
|
|
}
|
2018-06-08 00:56:39 -04:00
|
|
|
this.initEditor();
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
...mapActions([
|
2018-07-26 10:56:56 -04:00
|
|
|
'getFileData',
|
2018-03-20 10:12:48 -04:00
|
|
|
'getRawFileData',
|
|
|
|
'changeFileContent',
|
|
|
|
'setFileLanguage',
|
|
|
|
'setEditorPosition',
|
2018-04-05 07:12:40 -04:00
|
|
|
'setFileViewMode',
|
2018-03-20 10:12:48 -04:00
|
|
|
'setFileEOL',
|
|
|
|
'updateViewer',
|
2018-05-18 09:00:31 -04:00
|
|
|
'removePendingTab',
|
2019-06-04 18:31:22 -04:00
|
|
|
'triggerFilesChange',
|
2018-03-20 10:12:48 -04:00
|
|
|
]),
|
2018-06-08 00:56:39 -04:00
|
|
|
initEditor() {
|
2019-07-03 08:14:44 -04:00
|
|
|
if (this.shouldHideEditor && (this.file.content || this.file.raw)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-20 10:12:48 -04:00
|
|
|
|
|
|
|
this.editor.clearEditor();
|
|
|
|
|
2019-10-04 11:06:38 -04:00
|
|
|
this.fetchFileData()
|
2018-03-20 10:12:48 -04:00
|
|
|
.then(() => {
|
|
|
|
this.createEditorInstance();
|
|
|
|
})
|
2018-03-23 04:41:43 -04:00
|
|
|
.catch(err => {
|
2019-06-24 01:30:35 -04:00
|
|
|
flash(
|
|
|
|
__('Error setting up editor. Please try again.'),
|
|
|
|
'alert',
|
|
|
|
document,
|
|
|
|
null,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
);
|
2018-03-20 10:12:48 -04:00
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
},
|
2019-10-04 11:06:38 -04:00
|
|
|
fetchFileData() {
|
|
|
|
if (this.file.tempFile) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getFileData({
|
|
|
|
path: this.file.path,
|
|
|
|
makeFileActive: false,
|
|
|
|
}).then(() =>
|
|
|
|
this.getRawFileData({
|
|
|
|
path: this.file.path,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
},
|
2018-03-20 10:12:48 -04:00
|
|
|
createEditorInstance() {
|
|
|
|
this.editor.dispose();
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
2018-05-04 08:03:35 -04:00
|
|
|
if (this.viewer === viewerTypes.edit) {
|
2018-03-20 10:12:48 -04:00
|
|
|
this.editor.createInstance(this.$refs.editor);
|
|
|
|
} else {
|
2018-05-01 05:46:28 -04:00
|
|
|
this.editor.createDiffInstance(this.$refs.editor, !this.isReviewModeActive);
|
2018-03-20 10:12:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setupEditor();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setupEditor() {
|
|
|
|
if (!this.file || !this.editor.instance) return;
|
|
|
|
|
2018-04-06 17:48:52 -04:00
|
|
|
const head = this.getStagedFile(this.file.path);
|
|
|
|
|
|
|
|
this.model = this.editor.createModel(
|
|
|
|
this.file,
|
|
|
|
this.file.staged && this.file.key.indexOf('unstaged-') === 0 ? head : null,
|
|
|
|
);
|
2018-03-20 10:12:48 -04:00
|
|
|
|
2018-05-10 11:44:57 -04:00
|
|
|
if (this.viewer === viewerTypes.mr && this.file.mrChange) {
|
2018-03-23 06:45:43 -04:00
|
|
|
this.editor.attachMergeRequestModel(this.model);
|
|
|
|
} else {
|
|
|
|
this.editor.attachModel(this.model);
|
|
|
|
}
|
2018-03-20 10:12:48 -04:00
|
|
|
|
2018-03-23 04:41:43 -04:00
|
|
|
this.model.onChange(model => {
|
2018-03-20 10:12:48 -04:00
|
|
|
const { file } = model;
|
|
|
|
|
|
|
|
if (file.active) {
|
|
|
|
this.changeFileContent({
|
|
|
|
path: file.path,
|
|
|
|
content: model.getModel().getValue(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle Cursor Position
|
|
|
|
this.editor.onPositionChange((instance, e) => {
|
|
|
|
this.setEditorPosition({
|
|
|
|
editorRow: e.position.lineNumber,
|
|
|
|
editorColumn: e.position.column,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editor.setPosition({
|
|
|
|
lineNumber: this.file.editorRow,
|
|
|
|
column: this.file.editorColumn,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle File Language
|
|
|
|
this.setFileLanguage({
|
|
|
|
fileLanguage: this.model.language,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get File eol
|
|
|
|
this.setFileEOL({
|
|
|
|
eol: this.model.eol,
|
|
|
|
});
|
|
|
|
},
|
2019-06-27 13:42:02 -04:00
|
|
|
refreshEditorDimensions() {
|
|
|
|
if (this.showEditor) {
|
|
|
|
this.editor.updateDimensions();
|
|
|
|
}
|
|
|
|
},
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
2018-05-04 08:03:35 -04:00
|
|
|
viewerTypes,
|
2019-07-23 18:13:36 -04:00
|
|
|
FILE_VIEW_MODE_EDITOR,
|
|
|
|
FILE_VIEW_MODE_PREVIEW,
|
2018-03-20 10:12:48 -04:00
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2018-11-16 15:07:38 -05:00
|
|
|
<div id="ide" class="blob-viewer-container blob-editor-container">
|
2018-08-16 07:41:21 -04:00
|
|
|
<div class="ide-mode-tabs clearfix">
|
2020-02-03 13:08:46 -05:00
|
|
|
<ul v-if="!shouldHideEditor && isEditModeActive" class="nav-links float-left border-bottom-0">
|
2018-04-05 07:12:40 -04:00
|
|
|
<li :class="editTabCSS">
|
|
|
|
<a
|
|
|
|
href="javascript:void(0);"
|
|
|
|
role="button"
|
2019-07-23 18:13:36 -04:00
|
|
|
@click.prevent="setFileViewMode({ file, viewMode: $options.FILE_VIEW_MODE_EDITOR })"
|
2018-11-16 15:07:38 -05:00
|
|
|
>
|
2019-06-24 01:30:35 -04:00
|
|
|
<template v-if="viewer === $options.viewerTypes.edit">{{ __('Edit') }}</template>
|
|
|
|
<template v-else>{{ __('Review') }}</template>
|
2018-04-05 07:12:40 -04:00
|
|
|
</a>
|
|
|
|
</li>
|
2018-11-16 15:07:38 -05:00
|
|
|
<li v-if="file.previewMode" :class="previewTabCSS">
|
2018-04-05 07:12:40 -04:00
|
|
|
<a
|
|
|
|
href="javascript:void(0);"
|
|
|
|
role="button"
|
2019-07-23 18:13:36 -04:00
|
|
|
@click.prevent="setFileViewMode({ file, viewMode: $options.FILE_VIEW_MODE_PREVIEW })"
|
2019-06-24 01:30:35 -04:00
|
|
|
>{{ file.previewMode.previewTitle }}</a
|
2018-11-16 15:07:38 -05:00
|
|
|
>
|
2018-04-05 07:12:40 -04:00
|
|
|
</li>
|
|
|
|
</ul>
|
2018-11-16 15:07:38 -05:00
|
|
|
<external-link :file="file" />
|
2018-03-20 10:12:48 -04:00
|
|
|
</div>
|
2018-11-16 15:07:38 -05:00
|
|
|
<file-templates-bar v-if="showFileTemplatesBar(file.name)" />
|
2018-03-20 10:12:48 -04:00
|
|
|
<div
|
2019-06-27 13:42:02 -04:00
|
|
|
v-show="showEditor"
|
2018-03-20 10:12:48 -04:00
|
|
|
ref="editor"
|
2018-05-01 05:46:28 -04:00
|
|
|
:class="{
|
|
|
|
'is-readonly': isCommitModeActive,
|
2018-07-26 10:56:56 -04:00
|
|
|
'is-deleted': file.deleted,
|
2018-11-16 15:07:38 -05:00
|
|
|
'is-added': file.tempFile,
|
2018-05-01 05:46:28 -04:00
|
|
|
}"
|
2018-06-11 05:49:47 -04:00
|
|
|
class="multi-file-editor-holder"
|
2019-06-04 18:31:22 -04:00
|
|
|
@focusout="triggerFilesChange"
|
2018-11-16 15:07:38 -05:00
|
|
|
></div>
|
2018-04-05 07:12:40 -04:00
|
|
|
<content-viewer
|
2018-06-13 05:35:52 -04:00
|
|
|
v-if="showContentViewer"
|
2018-04-05 07:12:40 -04:00
|
|
|
:content="file.content || file.raw"
|
2018-04-10 11:48:02 -04:00
|
|
|
:path="file.rawPath || file.path"
|
2019-11-13 22:06:25 -05:00
|
|
|
:file-path="file.path"
|
2018-04-06 13:49:06 -04:00
|
|
|
:file-size="file.size"
|
2018-11-16 15:07:38 -05:00
|
|
|
:project-path="file.projectId"
|
2019-04-17 16:41:07 -04:00
|
|
|
:type="fileType"
|
2018-11-16 15:07:38 -05:00
|
|
|
/>
|
2018-06-13 05:35:52 -04:00
|
|
|
<diff-viewer
|
|
|
|
v-if="showDiffViewer"
|
|
|
|
:diff-mode="file.mrChange.diffMode"
|
|
|
|
:new-path="file.mrChange.new_path"
|
|
|
|
:new-sha="currentMergeRequest.sha"
|
|
|
|
:old-path="file.mrChange.old_path"
|
|
|
|
:old-sha="currentMergeRequest.baseCommitSha"
|
2018-11-16 15:07:38 -05:00
|
|
|
:project-path="file.projectId"
|
|
|
|
/>
|
2018-03-20 10:12:48 -04:00
|
|
|
</div>
|
|
|
|
</template>
|