MulRowInput.vue 6.45 KB
Newer Older
fukai committed
1 2 3 4 5 6
<template>
  <el-input ref="input" :id="id" type="textarea" 
    :cols="cols"
    :rows="rows"
    v-bind="attrs"  v-bind:disabled="isDisable"
    :value="value"
fukai committed
7
    @input="onInput"
fukai committed
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 v-slot:suffix>
      <slot name="suffix"></slot>
    </template>
  </el-input>
</template>

<script>
const SP_STR = "✌";
const SP_CHAR=SP_STR.charCodeAt(0);
const LINE_CHAR="\n"
const SWIFT_CHARS = " \r'()+,-./01234567890:?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const SWIFTZ_CHARS = " \r!\"#%&'()+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz{";

const CHARSET = ["","",SWIFT_CHARS,SWIFTZ_CHARS]

export default {
  props: {
    value: {
      type: [String, Number],
      default: undefined
    },
    disabled: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: undefined
    },
    cols:{
        type:Number,
        default:1
    },
    rows:{
        type:Number,
        default:1
    },
    chsbit:{
      type:Number,
      default:2
    },
    charmod:{
      type:Number,
      default:0   //0任意模式,1英文模式,2 swift模式 3 swiftz模式
    }
  },
  computed: {
fukai committed
56
    
fukai committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    mode () {
      return this.$store.state.Status.mode
    },
    isDisable: {
      get () {
        return this.mode === 'display' || this.disabled
      }
    },
    highlight () {
      return this.$store.state.Status.highlights.indexOf(this.id) !== -1
    },
    highlightChanges () {
      return this.$store.state.Status.highlightChanges.indexOf(this.id) !== -1
    },
    attrs(){
      if(this.mode === 'display' || this.disabled)
      {
        let {placeholder,...rest} = this.$attrs
        return rest
      }
      return this.$attrs
    }
  },
  methods:{
    formatVal(oriVal){
        let val = oriVal;
        if(!val){
            return val;
        }
        let position = this.getCursorPosition();
        val = val.substring(0,position)+SP_STR+val.substring(position)
        let arr = val.split(/\r?\n|\r/g);
        let arrNew = [];
        arr.forEach(line=>{
            let lineArr = this.formatLine(line);
            arrNew.push(...lineArr);
        });
        if(arrNew.length > this.rows){
            let obj = this.getTextarea();
            //内容已经输满了
            if(obj.selectionStart == obj.selectionEnd && oriVal.length == this.value.length+1){
              this.$position = position - 1;
              return this.value;
            }
            arrNew = arrNew.slice(0,this.rows);
        }
        let result = arrNew.join(LINE_CHAR);
        position = result.indexOf(SP_STR);
        result = result.substring(0,position)+result.substring(position+1);
        this.$position = position
        return result;
    },
    formatLine(val){
        if(!val){
            return [""];
        }
        if(val == SP_STR){
          return [SP_STR]
        }
        let valCopy = val;
        let newVal = [];
        while(valCopy && valCopy!=SP_STR){
            let length = 0;
            let i=0;
            let spCnt =0;
            let autoLine = "";
            for(; i<valCopy.length && i<this.cols+spCnt && length<this.cols; i++) {
              var c = valCopy.charCodeAt(i);
              let cStr = valCopy.charAt(i);
              if(c == SP_CHAR){
                spCnt++;
                autoLine+=SP_STR;
                continue;
              }
              else if ((c >= 0x0001 && c <= 0x007e) || ( c >= 0xff60 && c <= 0xff9f )){ 
                //判断字符集
                if(this.charmod>1 && CHARSET[this.charmod].indexOf(cStr)==-1){
                  spCnt++;
                  //非法字符集
                  continue;
                }
                length ++;
                autoLine+=cStr;
                
              }
              else if(length <= this.cols - this.chsbit){
                //中文字符
               if(this.charmod){
                  spCnt++;
                  //非法字符集
                  continue;
               }
                length += this.chsbit;
                autoLine+=cStr;
              }
              else{
                break;
              }
            }
            //autoLine = valCopy.substring(0,i);
            newVal.push(autoLine);
            valCopy = valCopy.substring(i);
        }
        //不要丢失光标
        if(valCopy == SP_STR){
          newVal[newVal.length - 1] = newVal[newVal.length - 1]+SP_STR;
        }
        return newVal;
    },
    getTextarea(){
      return this.$refs.input.$el.children[0];
    },
    getCursorPosition(){
        let obj = this.getTextarea();
        let position = obj.selectionStart
        return position;
    },
    setCursorPosition(position){
      let obj = this.getTextarea();
      if(obj.createTextRange) {
        var tr = obj.createTextRange();
        tr.collapse(true);
        tr.moveStart('character', position);
        tr.select();
      }
      else if (obj.setSelectionRange)
        obj.setSelectionRange(position, position);
    },
    onInput(newVal){
       let extraCnt = 0;
        if(this.value && newVal.length == this.value.length -1 ){
          let obj = this.getTextarea();
          if(obj.selectionStart == obj.selectionEnd && obj.selectionStart<newVal.length && this.value.charAt(obj.selectionStart)=="\n"){
            //删除中间换行符,额外再筛除一个字符,避免被重新格式化
            newVal = newVal.substring(0,obj.selectionStart - 1)+newVal.substring(obj.selectionStart)
            extraCnt = -2;
          }
        }
        newVal = this.formatVal(newVal);
        console.log(newVal)
        this.$emitNewValue = newVal;
        this.$emit('input', newVal)
        setTimeout(()=>{
         this.setCursorPosition(this.$position+extraCnt)   
        },0)
    }
  },
  watch:{
    cols(){
      let newVal = this.formatVal(this.value);
      this.$emitNewValue = newVal;
      this.$emit('input', newVal)
    },
    rows(){
      let newVal = this.formatVal(this.value);
      this.$emitNewValue = newVal;
      this.$emit('input', newVal)
    },
    value(val,oldval){
      if(val == this.$emitNewValue){
        return;
      }
      let newVal = this.formatVal(val);
      this.$emit('input', newVal);
    }
  }
}
</script>

<style>
/* .el-input.highlight .el-input__inner{
  border-color: red;
}
.el-textarea.highlight .el-textarea__inner {
  border-color: red;
}
.el-input.change-light .el-input__inner{
  border-color: #E6A23C;
}
.el-textarea.change-light .el-textarea__inner {
  border-color: #E6A23C;
} */
</style>