app-preset-text.vue 5.6 KB
Newer Older
1 2 3 4 5
<template>
    <div :class="['app-preset-text', `app-preset-text--${contentType.toLowerCase()}`]">
        <!-- 直接内容类型 -->
        <template v-if="Object.is(contentType, 'RAW')">
            <template v-if="Object.is(renderMode, 'TEXT')">
6
                <span :style="cssStyle">{{ content }}</span>
7 8
            </template>
            <template v-else-if="Object.is(renderMode, 'HEADING1')">
9
                <h1 :style="cssStyle">{{ content }}</h1>
10 11
            </template>
            <template v-else-if="Object.is(renderMode, 'HEADING2')">
12
                <h2 :style="cssStyle">{{ content }}</h2>
13 14
            </template>
            <template v-else-if="Object.is(renderMode, 'HEADING3')">
15
                <h3 :style="cssStyle">{{ content }}</h3>
16 17
            </template>
            <template v-else-if="Object.is(renderMode, 'HEADING4')">
18
                <h4 :style="cssStyle">{{ content }}</h4>
19 20
            </template>
            <template v-else-if="Object.is(renderMode, 'HEADING5')">
21
                <h5 :style="cssStyle">{{ content }}</h5>
22 23
            </template>
            <template v-else-if="Object.is(renderMode, 'HEADING6')">
24
                <h6 :style="cssStyle">{{ content }}</h6>
25 26
            </template>
            <template v-else-if="Object.is(renderMode, 'PARAGRAPH')">
27
                <p :style="cssStyle">{{ content }}</p>
28 29 30 31 32 33 34 35 36
            </template>
        </template>
        <!-- 图片类型 -->
        <template v-else-if="Object.is(contentType, 'IMAGE')">
            <img :style="cssStyle" v-if="imgUrl" :src="imgUrl" />
            <i :style="cssStyle" v-else :class="imageClass ? imageClass : ''"></i>
        </template>
        <!-- HTML类型 -->
        <template v-else-if="Object.is(contentType, 'HTML')">
37
            <div :style="cssStyle" v-html="content" />
38 39 40 41 42 43 44 45 46 47 48 49 50
        </template>
        <!-- MARKDOWN类型 -->
        <template v-else-if="Object.is(contentType, 'MARKDOWN')">
            MARKDOWN暂未支持
            <!-- <app-markdown-editor :style="cssStyle" mode="PREVIEWONLY" :itemValue="value"></app-markdown-editor> -->
        </template>
    </div>
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator';

@Component({})
51
export default class AppPresetText extends Vue {
52 53 54 55 56

    /**
     * 输入值
     *
     * @type {*}
57
     * @memberof AppPresetText
58 59 60 61 62 63 64
     */
    @Prop() public value!: any;

    /**
     * 内容类型
     *
     * @type {string}
65
     * @memberof AppPresetText
66 67 68 69 70 71 72
     */
    @Prop({ default: 'RAW' }) public contentType!: 'RAW' | 'HTML' | 'IMAGE' | 'MARKDOWN';

    /**
     * 绘制模式
     *
     * @type {string}
73
     * @memberof AppPresetText
74 75 76 77 78 79 80
     */
    @Prop({ default: 'TEXT' }) public renderMode!: 'TEXT' | 'HEADING1' | 'HEADING2' | 'HEADING3' | 'HEADING4' | 'HEADING5' | 'HEADING6' | 'PARAGRAPH';

    /**
     * 内容样式
     *
     * @type {string}
81
     * @memberof AppPresetText
82 83 84 85 86 87 88
     */
    @Prop() public contentStyle?: string;

    /**
     * 预置类型
     *
     * @type {string}
89
     * @memberof AppPresetText
90 91 92 93 94 95
     */
    @Prop({ default: 'STATIC_TEXT' }) public predefinedType!: 'FIELD_TEXT_DYNAMIC' | 'STATIC_LABEL' | 'STATIC_TEXT';

    /**
     * 图标
     *
96
     * @memberof AppPresetText
97 98 99 100 101 102
     */
    @Prop() public imageClass?: string;

    /**
     * 动态图片路径
     *
103
     * @memberof AppPresetText
104 105 106 107 108 109
     */
    protected dynaImgUrl: string = '';

    /**
     * 样式
     *
110
     * @memberof AppPresetText
111 112 113 114 115 116
     */
    protected cssStyle: string = '';

    /**
     * 图片路径
     *
117
     * @memberof AppPresetText
118 119 120 121 122
     */
    get imgUrl(): string {
        return this.dynaImgUrl;
    }

123 124 125
    /**
     * 内容
     *
126
     * @memberof AppPresetText
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
     */
    get content(): string {
        let content = this.value;
        if (this.contentType == 'HTML') {
            const items = content.match(/\{{(.+?)\}}/g);
            if (items) {
                items.forEach((item: string) => {
                    content = content.replace(/\{{(.+?)\}}/, eval(item.substring(2, item.length - 2)));
                });
            }
            content = content.replaceAll('&lt;', '<');
            content = content.replaceAll('&gt;', '>');
            content = content.replaceAll('&amp;nbsp;', ' ');
            content = content.replaceAll('&nbsp;', ' ');
        }
        return content;
    }

145 146 147
    /**
     * Vue生命周期 --- Created
     *
148
     * @memberof AppPresetText
149 150 151 152 153 154 155 156 157
     */
    created() {
        this.handleText();
        this.handleDynaImg();
    }

    /**
     * 处理文本
     *
158
     * @memberof AppPresetText
159 160 161 162 163 164 165 166 167 168
     */
    protected handleText() {
        if (this.predefinedType === 'STATIC_LABEL') {
            this.cssStyle += "white-space: nowrap;overflow: hidden;text-overflow: ellipsis;";
        }
    }

    /**
     * 处理动态图片
     *
169
     * @memberof AppPresetText
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
     */
    protected handleDynaImg() {
        // TODO 动态图片
        // if (this.value && typeof this.value == 'string') {
        //     // 默认识别文件对象形式,识别失败则为全路径模式
        //     try {
        //         const _files = JSON.parse(this.value);
        //         const file = _files instanceof Array ? _files[0] : null;
        //         const url = file && file.id ? `${this.downloadUrl}/${file.id}` : '';
        //         ImgurlBase64.getInstance()
        //         .getImgURLOfBase64(url)
        //         .then((res: any) => {
        //             this.dynaImgUrl = res;
        //         });
        //     } catch (error) {
        //         this.dynaImgUrl = this.value;
        //     }
        // }
    }

}
</script>
<style lang="less">
@import './app-preset-text.less';
</style>