<template>
    <div class="boardPreview" ref="compBox">
        <div class="boardBox" :style="boardStyle" v-if="isReady">
            <p class="boardTxt" v-for="item,index in contentArr" :key="index" :style="boardTxtStyle" v-html="item" v-if="(index + 1) <= lineTotal">
            </p>
        </div>
    </div>
</template>
<script>
export default {
    name:"BoardPreview",
    data(){
        return {
            isReady: false,
            boardStyle:null,
            boardTxtStyle:null,
            contentArr:{
                type:Array,
                default:()=>[]
            },
            lineTotal: 0
        }
    },
    props:{
        boardWH: {
            type: String,
            default: "300*200"
        },
        tpl: {
            type: Object,
            default: ()=>{}
        },
    },
    watch:{
        boardWH:{
            handler(newV){
                this.setStyle();
            },
            immediate:true
        },
        tpl:{
            handler(newV){
                this.contentArr = this.tpl.textContent.replaceAll(/\\\\n/g, '\n').replaceAll(/\\=/g, '=').replaceAll(/\\,/g, ',').replaceAll(/ /g, '&nbsp').split('\n');
                this.setStyle();
            },
            deep:true,
            immediate:true
        }
    },
    computed:{
    },
    mounted(){
    },
    methods:{
        setStyle() {
            this.$nextTick(() => {

                let boxW = this.$refs["compBox"].clientWidth;
                let boxH = this.$refs["compBox"].clientHeight;
                let arr = this.boardWH.split("*");
                let scale = 1;
                if (boxH > 20) {  //判断是否设置了显示高度，判断>0即可，20是为了兼容误差
                    if (arr[0] / arr[1] > boxW / boxH) {
                        scale = boxW / arr[0];
                    } else {
                        scale = boxH / arr[1];
                    }
                } else {
                    if (arr[0] > boxW) {
                        scale = boxW / arr[0];
                    }
                }

                this.boardStyle =  {
                    width: `${arr[0] * scale}px`,
                    height: `${arr[1] * scale}px`,
                    "background-color":`${this.tpl.backgroundColor}`,
                    "align-items" : ['flex-start', 'flex-end', 'center'][this.tpl.verticalAlignment]  //模板的字段为为formatStyle
                }
                let fontSize = 0;
                if (_.isString(this.tpl.fontSize)) {
                    fontSize = this.tpl.fontSize.replace('px', '') * 1;
                } else {
                    fontSize = this.tpl.fontSize;
                }
                this.boardTxtStyle = {
                    "color": "#" + this.tpl.foregroundColor,  //模板的字段为fontColor
                    "font-size": `${fontSize*scale}px`,
                    "font-family": this.fontTypeDic[this.tpl.font || '3'],   //模板的字段为fontType
                    "min-height": `${fontSize * scale}px`
                }
                this.lineTotal = Math.floor(arr[1] / fontSize);
                this.isReady = true;
                
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.boardPreview{
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    .boardBox {
        background-color: #000;
        display: flex;
        overflow: hidden;
        flex-direction: column;
        justify-content: center;
        .boardTxt{
            color: #f00;
            line-height: 1;
            margin-bottom: 0;
            word-break: keep-all;
            white-space: nowrap;
        }
    }
}
</style>