Read about our upcoming Code of Conduct on this issue

repo_editor.vue 8.25 KB
Newer Older
Phil Hughes's avatar
Phil Hughes committed
1
<script>
Tim Zallmann's avatar
Tim Zallmann committed
2
import { mapState, mapGetters, mapActions } from 'vuex';
3
import { viewerInformationForPath } from '~/vue_shared/components/content_viewer/lib/viewer_utils';
Phil Hughes's avatar
Phil Hughes committed
4
import flash from '~/flash';
Tim Zallmann's avatar
Tim Zallmann committed
5
import ContentViewer from '~/vue_shared/components/content_viewer/content_viewer.vue';
6
import DiffViewer from '~/vue_shared/components/diff_viewer/diff_viewer.vue';
7
8
9
10
11
12
import {
  activityBarViews,
  viewerTypes,
  FILE_VIEW_MODE_EDITOR,
  FILE_VIEW_MODE_PREVIEW,
} from '../constants';
Phil Hughes's avatar
Phil Hughes committed
13
import Editor from '../lib/editor';
14
import ExternalLink from './external_link.vue';
15
import FileTemplatesBar from './file_templates/bar.vue';
16
import { __ } from '~/locale';
Phil Hughes's avatar
Phil Hughes committed
17
18

export default {
Tim Zallmann's avatar
Tim Zallmann committed
19
20
  components: {
    ContentViewer,
21
    DiffViewer,
22
    ExternalLink,
23
    FileTemplatesBar,
Tim Zallmann's avatar
Tim Zallmann committed
24
  },
Phil Hughes's avatar
Phil Hughes committed
25
26
27
28
29
30
31
  props: {
    file: {
      type: Object,
      required: true,
    },
  },
  computed: {
32
33
34
    ...mapState('rightPane', {
      rightPaneIsOpen: 'isOpen',
    }),
Mike Greiling's avatar
Mike Greiling committed
35
    ...mapState(['rightPanelCollapsed', 'viewer', 'panelResizing', 'currentActivityView']),
36
37
38
    ...mapGetters([
      'currentMergeRequest',
      'getStagedFile',
39
      'isEditModeActive',
40
      'isCommitModeActive',
Phil Hughes's avatar
Phil Hughes committed
41
      'isReviewModeActive',
42
    ]),
43
    ...mapGetters('fileTemplates', ['showFileTemplatesBar']),
Phil Hughes's avatar
Phil Hughes committed
44
    shouldHideEditor() {
45
      return this.file && this.file.binary;
Phil Hughes's avatar
Phil Hughes committed
46
    },
47
48
    showContentViewer() {
      return (
49
        (this.shouldHideEditor || this.isPreviewViewMode) &&
50
51
52
53
54
55
        (this.viewer !== viewerTypes.mr || !this.file.mrChange)
      );
    },
    showDiffViewer() {
      return this.shouldHideEditor && this.file.mrChange && this.viewer === viewerTypes.mr;
    },
56
    isEditorViewMode() {
57
      return this.file.viewMode === FILE_VIEW_MODE_EDITOR;
58
59
    },
    isPreviewViewMode() {
60
      return this.file.viewMode === FILE_VIEW_MODE_PREVIEW;
61
    },
Tim Zallmann's avatar
Tim Zallmann committed
62
63
    editTabCSS() {
      return {
64
        active: this.isEditorViewMode,
Tim Zallmann's avatar
Tim Zallmann committed
65
66
67
68
      };
    },
    previewTabCSS() {
      return {
69
        active: this.isPreviewViewMode,
Tim Zallmann's avatar
Tim Zallmann committed
70
71
      };
    },
72
73
74
75
    fileType() {
      const info = viewerInformationForPath(this.file.path);
      return (info && info.id) || '';
    },
76
77
78
    showEditor() {
      return !this.shouldHideEditor && this.isEditorViewMode;
    },
Phil Hughes's avatar
Phil Hughes committed
79
80
  },
  watch: {
81
82
83
84
85
    file(newVal, oldVal) {
      if (oldVal.pending) {
        this.removePendingTab(oldVal);
      }

Phil Hughes's avatar
Phil Hughes committed
86
      // Compare key to allow for files opened in review mode to be cached differently
87
      if (oldVal.key !== this.file.key) {
88
        this.initEditor();
Phil Hughes's avatar
Phil Hughes committed
89
90
91
92

        if (this.currentActivityView !== activityBarViews.edit) {
          this.setFileViewMode({
            file: this.file,
93
            viewMode: FILE_VIEW_MODE_EDITOR,
Phil Hughes's avatar
Phil Hughes committed
94
95
96
97
98
99
100
101
          });
        }
      }
    },
    currentActivityView() {
      if (this.currentActivityView !== activityBarViews.edit) {
        this.setFileViewMode({
          file: this.file,
102
          viewMode: FILE_VIEW_MODE_EDITOR,
Phil Hughes's avatar
Phil Hughes committed
103
        });
Phil Hughes's avatar
Phil Hughes committed
104
105
106
      }
    },
    rightPanelCollapsed() {
107
      this.refreshEditorDimensions();
Phil Hughes's avatar
Phil Hughes committed
108
109
    },
    viewer() {
110
111
112
      if (!this.file.pending) {
        this.createEditorInstance();
      }
Phil Hughes's avatar
Phil Hughes committed
113
    },
114
115
    panelResizing() {
      if (!this.panelResizing) {
116
        this.refreshEditorDimensions();
117
118
      }
    },
119
    rightPaneIsOpen() {
120
121
122
123
124
125
126
      this.refreshEditorDimensions();
    },
    showEditor(val) {
      if (val) {
        // We need to wait for the editor to actually be rendered.
        this.$nextTick(() => this.refreshEditorDimensions());
      }
127
    },
Phil Hughes's avatar
Phil Hughes committed
128
129
130
131
132
  },
  beforeDestroy() {
    this.editor.dispose();
  },
  mounted() {
133
134
    if (!this.editor) {
      this.editor = Editor.create();
Phil Hughes's avatar
Phil Hughes committed
135
    }
136
    this.initEditor();
Phil Hughes's avatar
Phil Hughes committed
137
138
139
  },
  methods: {
    ...mapActions([
140
      'getFileData',
Phil Hughes's avatar
Phil Hughes committed
141
142
143
144
      'getRawFileData',
      'changeFileContent',
      'setFileLanguage',
      'setEditorPosition',
Tim Zallmann's avatar
Tim Zallmann committed
145
      'setFileViewMode',
Phil Hughes's avatar
Phil Hughes committed
146
147
      'setFileEOL',
      'updateViewer',
148
      'removePendingTab',
149
      'triggerFilesChange',
Phil Hughes's avatar
Phil Hughes committed
150
    ]),
151
    initEditor() {
152
153
154
      if (this.shouldHideEditor && (this.file.content || this.file.raw)) {
        return;
      }
Phil Hughes's avatar
Phil Hughes committed
155
156
157

      this.editor.clearEditor();

158
      this.fetchFileData()
Phil Hughes's avatar
Phil Hughes committed
159
160
161
        .then(() => {
          this.createEditorInstance();
        })
162
        .catch(err => {
163
164
165
166
167
168
169
170
          flash(
            __('Error setting up editor. Please try again.'),
            'alert',
            document,
            null,
            false,
            true,
          );
Phil Hughes's avatar
Phil Hughes committed
171
172
173
          throw err;
        });
    },
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    fetchFileData() {
      if (this.file.tempFile) {
        return Promise.resolve();
      }

      return this.getFileData({
        path: this.file.path,
        makeFileActive: false,
      }).then(() =>
        this.getRawFileData({
          path: this.file.path,
        }),
      );
    },
Phil Hughes's avatar
Phil Hughes committed
188
189
190
191
    createEditorInstance() {
      this.editor.dispose();

      this.$nextTick(() => {
Phil Hughes's avatar
Phil Hughes committed
192
        if (this.viewer === viewerTypes.edit) {
Phil Hughes's avatar
Phil Hughes committed
193
194
          this.editor.createInstance(this.$refs.editor);
        } else {
195
          this.editor.createDiffInstance(this.$refs.editor, !this.isReviewModeActive);
Phil Hughes's avatar
Phil Hughes committed
196
197
198
199
200
201
202
203
        }

        this.setupEditor();
      });
    },
    setupEditor() {
      if (!this.file || !this.editor.instance) return;

204
205
206
207
208
209
      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,
      );
Phil Hughes's avatar
Phil Hughes committed
210

211
      if (this.viewer === viewerTypes.mr && this.file.mrChange) {
Tim Zallmann's avatar
Tim Zallmann committed
212
213
214
215
        this.editor.attachMergeRequestModel(this.model);
      } else {
        this.editor.attachModel(this.model);
      }
Phil Hughes's avatar
Phil Hughes committed
216

217
      this.model.onChange(model => {
Phil Hughes's avatar
Phil Hughes committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
        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,
      });
    },
251
252
253
254
255
    refreshEditorDimensions() {
      if (this.showEditor) {
        this.editor.updateDimensions();
      }
    },
Phil Hughes's avatar
Phil Hughes committed
256
  },
Phil Hughes's avatar
Phil Hughes committed
257
  viewerTypes,
258
259
  FILE_VIEW_MODE_EDITOR,
  FILE_VIEW_MODE_PREVIEW,
Phil Hughes's avatar
Phil Hughes committed
260
261
262
263
};
</script>

<template>
Mike Greiling's avatar
Mike Greiling committed
264
  <div id="ide" class="blob-viewer-container blob-editor-container">
265
    <div class="ide-mode-tabs clearfix">
Mike Greiling's avatar
Mike Greiling committed
266
      <ul v-if="!shouldHideEditor && isEditModeActive" class="nav-links float-left">
Tim Zallmann's avatar
Tim Zallmann committed
267
268
269
270
        <li :class="editTabCSS">
          <a
            href="javascript:void(0);"
            role="button"
271
            @click.prevent="setFileViewMode({ file, viewMode: $options.FILE_VIEW_MODE_EDITOR })"
Mike Greiling's avatar
Mike Greiling committed
272
          >
273
274
            <template v-if="viewer === $options.viewerTypes.edit">{{ __('Edit') }}</template>
            <template v-else>{{ __('Review') }}</template>
Tim Zallmann's avatar
Tim Zallmann committed
275
276
          </a>
        </li>
Mike Greiling's avatar
Mike Greiling committed
277
        <li v-if="file.previewMode" :class="previewTabCSS">
Tim Zallmann's avatar
Tim Zallmann committed
278
279
280
          <a
            href="javascript:void(0);"
            role="button"
281
            @click.prevent="setFileViewMode({ file, viewMode: $options.FILE_VIEW_MODE_PREVIEW })"
282
            >{{ file.previewMode.previewTitle }}</a
Mike Greiling's avatar
Mike Greiling committed
283
          >
Tim Zallmann's avatar
Tim Zallmann committed
284
285
        </li>
      </ul>
Mike Greiling's avatar
Mike Greiling committed
286
      <external-link :file="file" />
Phil Hughes's avatar
Phil Hughes committed
287
    </div>
Mike Greiling's avatar
Mike Greiling committed
288
    <file-templates-bar v-if="showFileTemplatesBar(file.name)" />
Phil Hughes's avatar
Phil Hughes committed
289
    <div
290
      v-show="showEditor"
Phil Hughes's avatar
Phil Hughes committed
291
      ref="editor"
292
293
      :class="{
        'is-readonly': isCommitModeActive,
294
        'is-deleted': file.deleted,
Mike Greiling's avatar
Mike Greiling committed
295
        'is-added': file.tempFile,
296
      }"
297
      class="multi-file-editor-holder"
298
      @focusout="triggerFilesChange"
Mike Greiling's avatar
Mike Greiling committed
299
    ></div>
Tim Zallmann's avatar
Tim Zallmann committed
300
    <content-viewer
301
      v-if="showContentViewer"
Tim Zallmann's avatar
Tim Zallmann committed
302
      :content="file.content || file.raw"
303
      :path="file.rawPath || file.path"
304
      :file-path="file.path"
305
      :file-size="file.size"
Mike Greiling's avatar
Mike Greiling committed
306
      :project-path="file.projectId"
307
      :type="fileType"
Mike Greiling's avatar
Mike Greiling committed
308
    />
309
310
311
312
313
314
315
    <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"
Mike Greiling's avatar
Mike Greiling committed
316
317
      :project-path="file.projectId"
    />
Phil Hughes's avatar
Phil Hughes committed
318
319
  </div>
</template>