<!-- webview only -->
<template>
  <div class="bl___cover-textarea" :style="positionStyle"
       @linechange="linechangeEvent"
       @input="inputEvent"
       @focus="focusEvent"
       @blur="blurEvent"
       @confirm="confirmEvent"
       @keyboardheightchange="keyboardheightchangeEvent"
       @framechange="framechange"
  >
  </div>
</template>

<script>
import {naDataToObj} from '../util/logic';

export default {
  name: 'Bili_CoverTextarea',
  data() {
    return {
      positionStyle: {},
      classKey: ''
    };
  },
  props: {
    "value": {
      type: String,
      default: ''
    },
    "placeholder": {
      type: String,
      default: ''
    },
    "placeholderStyle": {
      type: Object,
      default: ''
    },
    "placeholderClass": {
      type: String,
      default: ''
    },
    "disabled": {
      type: Boolean,
      default: false
    },
    "maxlength": {
      type: Number,
      default: 140
    },
    "cursorSpacing": {
      type: Number,
      default: 0
    },
    "focus": {
      type: Boolean,
      default: false
    },
    "autoHeight": {
      type: Boolean,
      default: false
    },
    "confirmType": {
      type: String,
      default: 'done'
    },
    "cursor": {
      type: Number,
      default: -1
    },
    "animation": {
      type: String,
      default: ''
    },
    "showConfirmBar": {
      type: Boolean,
      default: true
    },
    "selectionStart": {
      type: Number,
      default: -1
    },
    "selectionEnd": {
      type: Number,
      default: -1
    },
    "adjustPosition": {
      type: Boolean,
      default: true
    },
    "holdKeyboard": {
      type: Boolean,
      default: false
    },
    "disableDefaultPadding": {
      type: Boolean,
      default: false
    }
  },
  watch: {
    value(value) {
      this.changeAttr('value', value);
    },
    placeholder(value) {
      this.changeAttr('placeholder', value);
    },
    placeholderStyle() {
      this.dealStyle();
    },
    placeholderClass() {
      this.dealStyle();
    },
    disabled(value) {
      this.changeAttr('disabled', value);
    },
    maxlength(value) {
      this.changeAttr('maxlength', value);
    },
    cursorSpacing(value) {
      this.changeAttr('cursorSpacing', value);
    },
    focus(value) {
      this.changeAttr('focus', value);
    },
    animation(value) {
      this.changeAttr('animation', value);
    },
    autoHeight(value) {
      this.changeAttr('autoHeight', value);
    },
    confirmType(value) {
      this.changeAttr('confirmType', value);
    },
    cursor(value) {
      this.changeAttr('cursor', value);
    },
    showConfirmBar(value) {
      this.changeAttr('showConfirmBar', value);
    },
    selectionStart(value) {
      this.changeAttr('selectionStart', value);
    },
    selectionEnd(value) {
      this.changeAttr('selectionEnd', value);
    },
    adjustPosition(value) {
      this.changeAttr('adjustPosition', value);
    },
    holdKeyboard(value) {
      this.changeAttr('holdKeyboard', value);
    },
    disableDefaultPadding(value) {
      this.changeAttr('disableDefaultPadding', value);
    },
  },
  methods: {
    dealStyle() {
      let _getStyleByClass = window.__smallAppFE.COVERVIEWDATA[this.classKey][this.placeholderClass];
      let _output = {};
      if (_getStyleByClass) {
        // 特殊处理placeholder-class的样式
        if (this.placeholderStyle) {
          Object.assign(_output, _getStyleByClass, this.placeholderStyle);
        } else {
          _output = _getStyleByClass;
        }
      }
      this.changeAttr('placeholderStyle', _output);
    },
    inputEvent(e) {
      this.$emit('input', {
        detail: e.data
      });
    },
    linechangeEvent(e) {
      this.$emit('linechange', {
        detail: e.data
      });
    },
    focusEvent(e) {
      this.$emit('focus', {
        detail: e.data
      });
    },
    blurEvent(e) {
      this.$emit('blur', {
        detail: e.data
      });
    },
    confirmEvent(e) {
      this.$emit('confirm', {
        detail: e.data
      });
    },
    keyboardheightchangeEvent(e) {
      this.$emit('keyboardheightchange', {
        detail: e.data
      });
    },
    changeAttr(key, value) {
      let _obj = {};
      _obj[key] = value;
      SmallApp.postMessage({
        "module": "dom", // cover-view 的所有通信固定使用 dom
        "method": "updateAttrs", // addElement...
        "args": {
          id: this.$el.getAttribute('nodeid'),
          attrs: _obj
        }
      });
    },
    framechange(e) {
      // 修复默认v-show为false，然后再设置为true，cover-view展示错误。
      if (getComputedStyle(this.$el).display !== 'none') {
        naDataToObj(this.$el, e.data, {
          type: 'textarea',
          autoHeight: this.autoHeight
        });
      }
    }
  },
  mounted() {
    this.classKey = this.$el.getAttribute('classkey');
  }
};
</script>

<style lang="less">
.bl___cover-textarea {
  font-size: 13px;
  padding: 2px;
  word-wrap: break-word;
  overflow: hidden;
  width: var(--width);
  height: var(--height);
  --width: '300px';
  --height: '150px';
}
</style>

