summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/merge_conflicts/components/diff_file_editor.js
blob: c1832d034efee31e68159e44ea3fd53a2005829a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* eslint-disable no-useless-computed-key, object-shorthand, no-param-reassign */
/* global ace */

import Vue from 'vue';
import axios from '~/lib/utils/axios_utils';
import flash from '~/flash';
import { __ } from '~/locale';

((global) => {
  global.mergeConflicts = global.mergeConflicts || {};

  global.mergeConflicts.diffFileEditor = Vue.extend({
    props: {
      file: {
        type: Object,
        required: true,
      },
      onCancelDiscardConfirmation: {
        type: Function,
        required: true,
      },
      onAcceptDiscardConfirmation: {
        type: Function,
        required: true,
      },
    },
    data() {
      return {
        saved: false,
        loading: false,
        fileLoaded: false,
        originalContent: '',
      };
    },
    computed: {
      classObject() {
        return {
          'saved': this.saved,
          'is-loading': this.loading
        };
      }
    },
    watch: {
      ['file.showEditor'](val) {
        this.resetEditorContent();

        if (!val || this.fileLoaded || this.loading) {
          return;
        }

        this.loadEditor();
      }
    },
    mounted() {
      if (this.file.loadEditor) {
        this.loadEditor();
      }
    },
    methods: {
      loadEditor() {
        this.loading = true;

        axios.get(this.file.content_path)
          .then(({ data }) => {
            const content = this.$el.querySelector('pre');
            const fileContent = document.createTextNode(data.content);

            content.textContent = fileContent.textContent;

            this.originalContent = data.content;
            this.fileLoaded = true;
            this.editor = ace.edit(content);
            this.editor.$blockScrolling = Infinity; // Turn off annoying warning
            this.editor.getSession().setMode(`ace/mode/${data.blob_ace_mode}`);
            this.editor.on('change', () => {
              this.saveDiffResolution();
            });
            this.saveDiffResolution();
            this.loading = false;
          })
          .catch(() => {
            flash(__('An error occurred while loading the file'));
            this.loading = false;
          });
      },
      saveDiffResolution() {
        this.saved = true;

        // This probably be better placed in the data provider
        this.file.content = this.editor.getValue();
        this.file.resolveEditChanged = this.file.content !== this.originalContent;
        this.file.promptDiscardConfirmation = false;
      },
      resetEditorContent() {
        if (this.fileLoaded) {
          this.editor.setValue(this.originalContent, -1);
        }
      },
      cancelDiscardConfirmation(file) {
        this.onCancelDiscardConfirmation(file);
      },
      acceptDiscardConfirmation(file) {
        this.onAcceptDiscardConfirmation(file);
      }
    }
  });
})(window.gl || (window.gl = {}));