diffs.vue 2.7 KB
Newer Older
B
baiy 已提交
1 2
<template>
    <div>
B
baiy 已提交
3 4
        <div style="border: 1px solid #dcdee2; border-radius: 4px">
            <diffEditor ref="editor" v-model="current.diff" :language="current.language" :auto-height="220" />
B
baiy 已提交
5
        </div>
B
baiy 已提交
6
        <option-block>
B
baiy 已提交
7 8 9
            <FormItem>
                <ButtonGroup>
                    <Button
B
baiy 已提交
10 11
                        :type="current.language !== item.id ? 'primary' : 'warning'"
                        @click="setLanguage(item.id)"
B
baiy 已提交
12
                        v-for="item in buttonLang"
B
baiy 已提交
13 14
                        :key="item.id"
                    >{{ item.name }}
B
baiy 已提交
15 16 17 18
                    </Button>
                </ButtonGroup>
            </FormItem>
            <FormItem>
B
baiy 已提交
19 20
                <Select placeholder="更多语言" @on-change="(value)=>{setLanguage(value)}">
                    <Option v-for="item in allLang" :value="item.id" :key="item.id">{{ item.name }}</Option>
B
baiy 已提交
21 22 23
                </Select>
            </FormItem>
            <FormItem>
B
baiy 已提交
24
                <Checkbox @on-change="(value)=>inline(value)">行内对比</Checkbox>
B
baiy 已提交
25 26
            </FormItem>
        </option-block>
B
baiy 已提交
27 28 29
    </div>
</template>
<script>
B
baiy 已提交
30
import diffEditor from "./components/diffEditor";
B
baiy 已提交
31 32 33 34 35 36 37 38 39
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';

let allLang = {}
for (let lang of monaco.languages.getLanguages()) {
    allLang[lang.id] = {
        id: lang.id,
        name: lang.id === "plaintext" ? "纯文本" : lang.aliases[0]
    }
}
B
baiy 已提交
40 41

const COMMON_LANG = [
B
baiy 已提交
42 43
    "plaintext",
    "javascript",
B
baiy 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57
    "html",
    "css",
    "json",
    "python",
    "java",
    "php"
]

export default {
    components: {
        diffEditor,
    },
    computed: {
        allLang() {
B
baiy 已提交
58
            return Object.values(allLang)
B
baiy 已提交
59
        },
B
baiy 已提交
60
        buttonLang() {
B
baiy 已提交
61 62 63 64 65
            let data = COMMON_LANG.map((item) => {
                return allLang[item]
            });
            if (this.current.language && !COMMON_LANG.includes(this.current.language)) {
                data.push(allLang[this.current.language])
B
baiy 已提交
66
            }
B
baiy 已提交
67 68 69 70
            return data;
        }
    },
    created() {
B
baiy 已提交
71
        this.current = Object.assign(this.current, this.$getToolData())
B
baiy 已提交
72 73 74 75
    },
    methods: {
        setLanguage(lang) {
            this.current.language = lang;
B
baiy 已提交
76 77 78
        },
        inline(value){
            this.$refs.editor.inline(!value)
B
baiy 已提交
79 80 81
        }
    },
    watch: {
B
baiy 已提交
82 83 84
        current:{
            handler(newVal){
                if (newVal.diff.original && newVal.diff.modified){
B
baiy 已提交
85
                    this.$saveToolData(this.current);
B
baiy 已提交
86
                }
B
baiy 已提交
87
            },
B
baiy 已提交
88
            deep:true
B
baiy 已提交
89 90 91 92 93 94
        }
    },
    data() {
        return {
            current: {
                diff: {original: "", modified: ""},
B
baiy 已提交
95
                language: ""
B
baiy 已提交
96 97 98
            }
        }
    }
B
baiy 已提交
99
}
B
baiy 已提交
100
</script>