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
<template>
  <a-modal width="100%" :visible="visible" :title="title" @cancel="visible=false" :footer="null" :maskClosable="false">
    <a-row>
      <a-col :span="12"><span class="file-version-text">版本号:{{fileVersionArray[0]}}</span></a-col>
      <a-col :span="12"><span class="file-version-text">版本号:{{fileVersionArray[1]}}</span></a-col>
    </a-row>
    <div class="code-diff-container">
      <CodeDiff :old-string="oldVersionContent" :new-string="newVersionContent" outputFormat="side-by-side"/>
    </div>
  </a-modal>
</template>
 
<script>
  import CodeDiff from 'vue-code-diff'
 
  export default {
    name: 'FileCompareModal',
    components: { CodeDiff },
    props: {
      fileDiffObject: {
        type: Object
      },
      fileVersionArray: {
        type: Array
      }
    },
    watch: {
      visible: {
        handler(value) {
          if (value) {
            const { fileDiffObject } = this
            let fileContent = ''
            for (let key in fileDiffObject) {
              if (fileDiffObject.hasOwnProperty(key)) {
                fileDiffObject[key].forEach((content, index) => {
                  fileContent += content
                  if (index !== fileDiffObject[key].length - 1) {
                    fileContent += '\n'
                  }
                })
                if (key === 'first') {
                  this.oldVersionContent = fileContent
                  fileContent = ''
                }
                else this.newVersionContent = fileContent
              }
            }
          }
          else this.newVersionContent = this.oldVersionContent = ''
        }
      }
    },
    data() {
      return {
        visible: false,
        title: '',
        oldVersionContent: '',
        newVersionContent: ''
      }
    },
    methods: {}
  }
</script>
 
<style scoped>
  .file-version-text {
    font-weight: bold;
    font-size: 16px;
    margin-left: 5%;
  }
 
  .code-diff-container {
    height: 500px;
    overflow: auto;
    margin-top: 20px
  }
</style>