InputXml.vue 2.73 KB
Newer Older
liuxin committed
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
<template>
    <el-input
        :id="id"
        ref="form-item"
        v-model="value"
        type="textarea"
        v-bind="$attrs"
        v-on="$listeners"
        v-bind:disabled="isDisable"
        :rows="maxRows"
        resize="none"
        @change="onChange"
    />
</template>

<script>
export default {
    data: function () {
        return {
            value: "",
            isXml: false,
        };
    },
    props: {
        maxRows: {
            type: Number,
            default: 3,
        },
        maxCols: {
            type: Number,
            default: 4,
        },
        disabled: {
            type: Boolean,
            default: false,
        },
        id: {
            type: String,
            default: undefined,
        },
        model: {
            type: Object,
            default: undefined,
        },
    },
    computed: {
        // model: {
        //     get() {
        //         let value = this.format(this.value);
        //         return value;
        //     },
        //     set(newVal) {
        //         this.$emit("input", newVal);
        //     },
        // },
潘际乾 committed
56 57 58
        mode () {
            return this.$store.state.Status.mode
        },
liuxin committed
59 60 61 62 63 64 65 66 67 68 69
        isDisable: {
            get() {
                return this.mode === "display" || this.disabled;
            },
        },
    },
    methods: {
        show: function (rows) {
            let strs = rows.length > 0 ? rows[0] : "";
            if (strs.startsWith("<?xml")) {
                this.isXml = true;
70 71
                // .点符号匹配除换行符\n以外的任意字符,相匹配包括换行符在内的任意字符,可以用[\s\S]*
                var pattern = /<tdfmt\s[^>]*>([\s\S]*)<\/tdfmt>/;
liuxin committed
72 73
                var temp = pattern.exec(strs);
                if (temp != null) {
74
                    strs = temp[1];
liuxin committed
75 76
                }
            }
77
            return strs.replace(/<br\/>/g, "\n");
liuxin committed
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
        },
        format: function (value) {
            if(this.isXml){
                var len = value.length;
                value = value.replace(/\n/g, "<br/>")
                var head = "<?xml version='1.0'?><tdfmt sel-start='" + len + "'>";
                var tail = "</tdfmt>";
                return head + value + tail;
            }
            return value;
        },
        onChange() {    
            if (this.model && this.model.rows) {
                this.model["rows"][0] = this.format(this.value);
                this.$emit("change", this.value);
            }
        },
    },
    watch: {
        model: function () {
            let rows = this.model["rows"] || [];
            this.value = this.show(rows);
        },
    },
};
</script>

<style>
/* .el-input.highlight .el-input__inner{
  border-color: red;
} */
</style>