diffEditor.vue 1.9 KB
Newer Older
B
baiy 已提交
1
<template>
B
baiy 已提交
2
    <div ref="container" class="diff-editor" :style="`height:${height};width:${width}`"></div>
B
baiy 已提交
3 4
</template>
<script>
B
baiy 已提交
5
import {createMerge} from "../library/editor";
B
baiy 已提交
6 7 8 9 10 11 12 13 14 15 16

export default {
    name: 'diffEditor',
    props: {
        value: {
            type: Object
        },
        language: {
            type: String,
            default: ""
        },
B
baiy 已提交
17
        collapse: {
18
            type: Boolean,
B
baiy 已提交
19
            default: false
20
        },
B
baiy 已提交
21 22
        height: {
            type: String,
B
baiy 已提交
23
            default: "100%"
B
baiy 已提交
24 25 26 27 28 29 30 31
        },
        width: {
            type: String,
            default: "100%"
        },

    },
    watch: {
B
baiy 已提交
32 33 34 35 36
        value:{
            handler(){
                this.reset()
            },
            deep: true
B
baiy 已提交
37 38 39
        },
        language(newValue) {
            if (this.editor !== null) {
B
baiy 已提交
40
                this.editor.customSetEditorLanguage(newValue);
B
baiy 已提交
41 42
            }
        },
B
baiy 已提交
43 44
        collapse() {
            this.reset()
B
baiy 已提交
45 46 47
        }
    },
    mounted() {
B
baiy 已提交
48
        this.reset()
B
baiy 已提交
49 50 51 52 53 54 55
    },
    data() {
        return {
            editor: null,
        }
    },
    methods: {
B
baiy 已提交
56 57 58 59
        reset() {
            this.$refs.container.innerHTML = "";
            this.initEditor();
        },
B
baiy 已提交
60
        initEditor() {
B
baiy 已提交
61 62 63 64 65
            this.editor = createMerge(this.value, this.$refs.container, this.language, {collapseIdentical: this.collapse ? 2 : false})
            this.editor.customChange((original, modified) => {
                if (original !== this.value.original || modified !== this.value.modified) {
                    this.value.original = original
                    this.value.modified = modified
B
baiy 已提交
66 67 68 69 70 71 72 73
                    this.$emit('input', this.value)
                }
            })
        },
    }
};
</script>

B
baiy 已提交
74 75 76 77 78 79 80 81 82
<style>
.diff-editor .CodeMirror-merge{
    border: none;
}
.diff-editor .CodeMirror-merge,.diff-editor .CodeMirror-merge-pane, .diff-editor .CodeMirror-merge .CodeMirror {
    height: 100%;
}
</style>