All files / app/assets/javascripts/merge_conflicts/components diff_file_editor.vue

3.7% Statements 1/27
0% Branches 0/8
0% Functions 0/13
3.84% Lines 1/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132      1x                                                                                                                                                                                                                                                                
<script>
import { GlButton } from '@gitlab/ui';
import { debounce } from 'lodash';
// eslint-disable-next-line no-restricted-imports
import { mapActions } from 'vuex';
import { createAlert } from '~/alert';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import { INTERACTIVE_RESOLVE_MODE } from '../constants';
 
export default {
  components: {
    GlButton,
  },
  props: {
    file: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      saved: false,
      fileLoaded: false,
      originalContent: '',
    };
  },
  computed: {
    classObject() {
      return {
        saved: this.saved,
      };
    },
  },
  watch: {
    'file.showEditor': function showEditorWatcher(val) {
      this.resetEditorContent();
 
      Iif (!val || this.fileLoaded) {
        return;
      }
 
      this.loadEditor();
    },
  },
  mounted() {
    Iif (this.file.loadEditor) {
      this.loadEditor();
    }
  },
  methods: {
    ...mapActions(['setFileResolveMode', 'setPromptConfirmationState', 'updateFile']),
    loadEditor() {
      const EditorPromise = import(/* webpackChunkName: 'SourceEditor' */ '~/editor/source_editor');
      const DataPromise = axios.get(this.file.content_path);
 
      Promise.all([EditorPromise, DataPromise])
        .then(
          ([
            { default: SourceEditor },
            {
              data: { content, new_path: path },
            },
          ]) => {
            const contentEl = this.$el.querySelector('.editor');
 
            this.originalContent = content;
            this.fileLoaded = true;
 
            this.editor = new SourceEditor().createInstance({
              el: contentEl,
              blobPath: path,
              blobContent: content,
            });
            this.editor.onDidChangeModelContent(debounce(this.saveDiffResolution.bind(this), 250));
          },
        )
        .catch(() => {
          createAlert({
            message: __('An error occurred while loading the file'),
          });
        });
    },
    saveDiffResolution() {
      this.saved = true;
 
      this.updateFile({
        ...this.file,
        content: this.editor.getValue(),
        resolveEditChanged: this.file.content !== this.originalContent,
        promptDiscardConfirmation: false,
      });
    },
    resetEditorContent() {
      Iif (this.fileLoaded) {
        this.editor.setValue(this.originalContent);
      }
    },
    acceptDiscardConfirmation(file) {
      this.setPromptConfirmationState({ file, promptDiscardConfirmation: false });
      this.setFileResolveMode({ file, mode: INTERACTIVE_RESOLVE_MODE });
    },
    cancelDiscardConfirmation(file) {
      this.setPromptConfirmationState({ file, promptDiscardConfirmation: false });
    },
  },
};
</script>
<template>
  <div v-show="file.showEditor">
    <div v-if="file.promptDiscardConfirmation" class="discard-changes-alert">
      {{ __('Are you sure you want to discard your changes?') }}
      <div class="gl-ml-3 gl-display-inline-block">
        <gl-button
          size="small"
          variant="danger"
          category="secondary"
          @click="acceptDiscardConfirmation(file)"
        >
          {{ __('Discard changes') }}
        </gl-button>
        <gl-button size="small" @click="cancelDiscardConfirmation(file)">
          {{ __('Cancel') }}
        </gl-button>
      </div>
    </div>
    <div :class="classObject" class="editor-wrap">
      <div class="editor" style="height: 350px" data-editor-loading="true"></div>
    </div>
  </div>
</template>