index.vue 6.69 KB
<!--
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-04-20 15:15:08
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-06-29 11:20:35
 * @FilePath: \scrm-micro-battles\src\views\battle\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%
 im
-->
<!--
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-04-20 15:15:08
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-06-10 14:48:12
 * @FilePath: \scrm-micro-battles\src\views\battle\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="text-area">
    <w-textarea ref="textContent" v-model="textValue" @updateText="updateText"></w-textarea>
    <div class="btn-group-panel">
      <div class="btn-group">
        <div class="add-btn" v-for="(item, k) in btnList" :key="k" @click="selectItem(item)">
          <template v-if="item.type == 1">
            <img class="emotion-item" src="@/assets/textarea/expression.png" />
            <span class="emotion-item">{{ item.name }}</span>
          </template>
          <template v-else>
            <img class="emotion-item" src="@/assets/textarea/add.png" />
            <span class="emotion-item">{{ item.name }}</span>
          </template>
        </div>
      </div>
      <span v-if="showNewMaxNum" class="text-num-up">{{ textNum }}/{{ maxNum }}</span>
      <div v-else class="text-num">{{ textNum }}/{{ maxNum }}</div>
    </div>
    <div v-show="emotionStatus" class="emotion-panel">
      <emotion-words class="emotion-panel-box" @emotion="handleEmotion" :height="280"></emotion-words>
    </div>
  </div>
</template>
<script>
import emoji from '@/utils/emo'
import WTextarea from './WTextarea'
import EmotionWords from './EmotionWords'
export default {
  data() {
    return {
      textNum: 0,
      limitContent: '',
      emotionStatus: false
    }
  },
  props: {
    btnList: {
      type: Array,
      default: () => []
    },
    mapList: {
      type: Array,
      default: () => []
    },
    maxNum: {
      typr: Number,
      default: 200
    },
    showNewMaxNum:Boolean
  },
  components: {
    WTextarea,
    EmotionWords
  },
  methods: {
    selectItem(item) {
      if (item.type == 1) {
        this.selectEmotion()
      } else {
        this.selectTag(item.name)
      }
    },
    //选择标签
    selectTag(name) {
      this.$refs.textContent.addTag(name)
    },
    updateText(val) {
      const html = this.getTextValue(val),
        text = html.replace(/\[.+?\]/g, '').replace(/\${.+?}/g, '')
      this.textNum = text.length
      if (this.textNum > this.maxNum) {
        this.textNum = this.maxNum
        this.update(this.limitContent)
      } else {
        this.limitContent = val
      }
    },
    //展开表情
    selectEmotion() {
      this.emotionStatus = !this.emotionStatus
    },
    //选择表情
    handleEmotion(i) {
      this.emotionStatus = false
      const list = emoji.flat(Infinity)
      let index = list.indexOf(i) + 1
      if (index < 10) {
        index = '00' + index
      } else if (index >= 10 && index < 100) {
        index = '0' + index
      }
      this.$refs.textContent.addImg(`https://img01.glsx.com.cn/weapp/resource/saas/scrm/emoji/${index}.png`)
    },
    getTextValue(val) {
      const html = val ? val : this.$refs.textContent.$refs.wTextareaContent.innerHTML
      const _this = this
      const list = emoji.flat(Infinity)
      let htm =
        html &&
        html
          .replace(/<span>([\s\S]*?)<\/span>/g, '$1')
          .replace(/<img.+?src="https:\/\/img01.glsx.com.cn\/weapp\/resource\/saas\/scrm\/emoji\//g, '')
          .replace(/<br>/g, '\n')
      htm = htm.replace(/<wise.+?>(.+?)(?:<\/wise>)/g, (str, v) => {
        return _this.mapList[v]
      })
      return (
        htm &&
        htm.replace(/(\d){3}.png">/g, function(str) {
          let s = list[parseInt(str.split('.')[0]) - 1]
          return `[${s}]`
        })
      )
    },
    update(attContent) {
      if (!attContent) {
        return
      }
      let content = attContent.replace(/(\$\{.+?\})/g, str => {
        for (let i in this.mapList) {
          if (this.mapList[i] == str) {
            return `<wise id="1" contenteditable="false">${i}</wise>`
          }
        }
      })
      let htm =
        content &&
        content.replace(/\[[\u4E00-\u9FA56]{1,4}]/gi, function(res) {
          let word = res.replace(/\[|\]/gi, '')
          const list = emoji.flat(Infinity)
          let index = parseInt(list.indexOf(word)) + 1
          if (index < 10) {
            index = '00' + index
          } else if (index >= 10 && index < 100) {
            index = '0' + index
          }
          return `<img src="https://img01.glsx.com.cn/weapp/resource/saas/scrm/emoji/${index}.png">`
        })
      if (htm.substr(-1, 1) == '>' && navigator.userAgent.indexOf('Firefox') > 0) {
        htm += '&nbsp;'
      }
      setTimeout(() => {
        document.querySelector('.w-textarea_input').innerHTML = htm
      const html = this.getTextValue(htm),
        text = html.replace(/\[.+?\]/g, '').replace(/\${.+?}/g, '')
      this.textNum = text.length
      if(this.textNum == 200) this.limitContent = htm
        this.$refs.textContent.setFocus()
      }, 100)
    }
  }
}
</script>
<style lang="scss" scoped>
.text-area {
  background: #fff;
  // width: 448px;
}
.btn-group-panel {
  display: flex;
  flex-wrap: wrap;
  padding: 10px 0 10px 20px;
  border: 1px solid #e5e5e5;
  border-top: 0;
  background: #fafbfe;
  border-bottom-right-radius: 4px;
  border-bottom-left-radius: 4px;
  .btn-group {
    display: flex;
    column-gap: 20px;
    line-height: 20px;
    .add-btn {
      display: inline-flex;
      position: relative;
      align-items: center;
      font-size: 16px;
      color: #666;
      cursor: pointer;
      span {
        font-size: 14px;
        margin-left: 5px;
        cursor: pointer;
      }
      input {
        position: absolute;
        width: 100%;
        cursor: pointer;
        font-size: 0;
        height: 40px;
      }
      img {
        width: 16px;
        height: 16px;
        cursor: pointer;
      }
      .el-link {
        position: absolute;
        width: 100%;
      }
    }
  }
  .text-num {
    padding-right: 10px;
    font-size: 14px;
    color: #999999;
    position: absolute;
      right: -85px;
  }
  .text-num-up{
    font-size: 14px;
    color: #999999;
    position: absolute;
    right: 12px;
    bottom: 100px;
    background: #fff;
    line-height: 16px;
  }
}
.date-picker {
  margin-top: 10px;
  font-size: 14px;
  color: #999;
}
</style>