code.vue 3.6 KB
Newer Older
B
baiy 已提交
1
<template>
B
baiy 已提交
2
    <div>
B
baiy 已提交
3 4 5 6
        <heightResize :append="['.page-option-block']">
            <code-editor ref="editor" v-model="current.content" :language="this.current.lang"></code-editor>
        </heightResize>
        <option-block class="page-option-block">
B
baiy 已提交
7 8 9
            <FormItem>
                <ButtonGroup>
                    <Button
B
update  
baiy 已提交
10
                        :type="current.lang !== item ? 'primary' : 'warning'"
B
baiy 已提交
11 12 13 14 15 16 17 18
                        @click="handle(item)"
                        v-for="item in buttonLang"
                        :key="item"
                    >{{ item }}
                    </Button>
                </ButtonGroup>
            </FormItem>
            <FormItem>
B
baiy 已提交
19
                <Select :placeholder="$t('code_more')" @on-change="(value)=>{handle(value)}">
B
baiy 已提交
20 21 22
                    <Option v-for="item in lang" :value="item" :key="item">{{ item }}</Option>
                </Select>
            </FormItem>
B
baiy 已提交
23
            <FormItem>
B
baiy 已提交
24 25 26 27 28
                <Select :placeholder="$t('code_indent')" v-model="current.tab">
                    <Option :value="2">{{ $t('code_indent_width',[2]) }}</Option>
                    <Option :value="4">{{ $t('code_indent_width',[4]) }}</Option>
                    <Option :value="6">{{ $t('code_indent_width',[6]) }}</Option>
                    <Option :value="8">{{ $t('code_indent_width',[8]) }}</Option>
B
baiy 已提交
29 30
                </Select>
            </FormItem>
31
            <FormItem>
B
baiy 已提交
32
                <Checkbox v-model="current.isCompress">{{ $t('code_compress') }}</Checkbox>
33
            </FormItem>
B
baiy 已提交
34
        </option-block>
B
baiy 已提交
35 36 37
    </div>
</template>
<script>
B
baiy 已提交
38
import _ from "lodash";
B
baiy 已提交
39
import codeEditor from "./components/codeEditor";
B
baiy 已提交
40
import heightResize from "./components/heightResize";
B
baiy 已提交
41
export default {
B
baiy 已提交
42 43
    components: {
        codeEditor,
B
baiy 已提交
44
        heightResize
B
baiy 已提交
45
    },
46 47 48 49
    computed: {
        buttonLang() {
            let data = _.slice(this.lang, 0, 7)
            if (this.current.lang && !data.includes(this.current.lang)) {
B
baiy 已提交
50 51 52 53
                data.push(this.current.lang)
            }
            return data
        }
B
baiy 已提交
54
    },
B
baiy 已提交
55
    created() {
B
baiy 已提交
56
        this.$initToolData('content')
B
baiy 已提交
57
    },
B
baiy 已提交
58 59
    methods: {
        handle(language) {
B
baiy 已提交
60 61 62
            if (this.current.content) {
                try {
                    this.current.lang = language;
63 64 65 66 67 68
                    if (!this.current.isCompress) {
                        let option = {tab: this.current.tab}
                        this.$refs.editor.format(language, option);
                    } else {
                        this.$refs.editor.compress(language);
                    }
B
baiy 已提交
69
                    this.$saveToolData(this.current);
B
baiy 已提交
70
                    return this.$Message.success(this.$t('code_complete').toString());
71
                } catch (e) {
B
baiy 已提交
72
                    console.log(e)
B
baiy 已提交
73
                    return this.$Modal.error({
B
baiy 已提交
74
                        title: this.$t('code_error_prompt').toString(),
75
                        content: `${e.message}`
B
baiy 已提交
76 77 78
                    });
                }
            }
B
baiy 已提交
79 80 81 82 83 84
        },
    },
    data() {
        return {
            current: {
                content: "",
85
                isCompress: false,
B
baiy 已提交
86
                lang: "",
87
                tab: 4,
B
baiy 已提交
88 89 90
            },
            lang: [
                "html",
91 92 93
                "js",
                "css",
                "xml",
B
baiy 已提交
94
                "json",
95
                "sql",
B
baiy 已提交
96 97
                "yaml",
                "php",
98
                "ts",
B
baiy 已提交
99 100 101 102 103
                "java",
                "scss",
                "less",
                "graphql",
                "markdown",
B
baiy 已提交
104
                "vue",
B
baiy 已提交
105
                "angular",
B
baiy 已提交
106 107
            ],
        };
B
baiy 已提交
108
    },
109
};
B
baiy 已提交
110
</script>