uni-im-chat-input.vue 14.1 KB
Newer Older
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
1
<template>
2 3 4 5 6 7 8 9 10 11
  <view class="uni-im-chat-input" :style="{height:chatInputBoxHeight}">
    <view id="drag-line"></view>
    <view class="top-menu">
      <uni-im-icons v-for="(item,index) in menuList" :key="index" class="item"
        @click.native.stop="clickMenu(index,$event)"
        :title.native="`选择${item.title},并发送`"
        :code="item.iconCode" size="24" color="#666666" 
      ></uni-im-icons>
      <!-- #ifdef H5 -->
      <view v-for="(item,index) in extToolBar" :key="index" class="item">
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
12 13 14
        <component :is="item.component" v-bind="item.props" @sendCodeMsg="sendCodeMsg" 
          @showMenberList="showMenberList"
        ></component>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
      </view>
      <!-- #endif -->
    </view>
    <view class="main">
      <!-- 切换为语音模式 -->
      <!-- #ifdef H5 -->
      <!-- <view class="camera-filled-box icon" @click="chooseFileSendMsg('image')">
        <uni-icons type="camera-filled" size="20px" color="#FFF"></uni-icons>
      </view> -->
      <!-- #endif -->
      
      <uni-im-icons @click="changeSoundIsShow" :code="soundIsShow?'e69f':'e684'" size="30" class="icon"></uni-im-icons>
      
      <view class="editor-box-parent">
        <view class="editor-box">
          <uni-im-sound v-show="soundIsShow" @sendSoundMsg="sendSoundMsg" class="uni-im-sound"></uni-im-sound>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
31
          <uni-im-editor @touchmove.stop v-show="!soundIsShow" @input="oninput" @confirm="confirm" @change="updateModelValue" class="editor" ref="editor"></uni-im-editor>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        </view>
        <slot name="about-msg"></slot>
      </view>
      
      <uni-im-icons @click="setShowMore('emoji')" :code="emojiIsShow?'e69f':'e646'" size="30" class="icon"></uni-im-icons>
      <text v-if="!soundIsShow&&canSend" @click="confirm" class="icon confirm">发送</text>
      <uni-im-icons v-else @click="setShowMore('menu')" code="e75a" size="30" class="icon"></uni-im-icons>
    </view>

    
    <view v-if="showMore" class="media-box" :style="{height:keyboardMaxHeight - bHeight +'px'}">
      <view v-if="showMore == 'menu'" class="menu">
        <view class="menu-item" v-for="(item,index) in menuList" :key="index" @click.stop="clickMenu(index,$event)">
          <view class="menu-item-icon">
            <uni-im-icons :code="item.iconCode" size="26"></uni-im-icons>
          </view>
          <text class="menu-item-text">{{item.title}}</text>
        </view>
      </view>
      <scroll-view v-if="showMore == 'emoji'" :scroll-y="true" class="emoji-list-box">
        <text v-for="(uniCodeEmoji,index) in emojiCodes" :key="index"
          @click.stop="clickEmojiItem(uniCodeEmoji,$event)" class="item">{{uniCodeEmoji}}</text>
      </scroll-view>
    </view>
    <!-- #ifndef H5 -->
    <!-- 调整输入框的位置,占位专用:键盘高度 或 全面屏底部不可用区域高度 -->
    <view v-else-if="raiseEditor || keyboardHeight" :style="{height: keyboardMaxHeight - bHeight + 'px'}"></view>
    <view :style="{height: bHeight + 'px'}"></view>
    <!-- #endif -->
    
    <view class="send-msg-btn-box">
      <text class="send-btn-tip">↵ 发送 / shift + ↵ 换行</text>
      <button @click="confirm" :disabled="!canSend" class="send" type="primary">发送</button>
    </view>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
66 67 68 69
  </view>
</template>

<script>
70
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
71
  import {markRaw} from "vue";
72
  import emojiCodes from './emojiCodes.js';
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
73 74
  let currentModelValue = '';
  export default {
75
    name: 'uni-im-chat-input',
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
76
    emits: ["update:modelValue","confirm","input","sendSoundMsg","sendCodeMsg","showMenberList"],
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    data() {
      return {
        bHeight:uniIm.systemInfo.safeAreaInsets.bottom/2,
        raiseEditor: false,
        showMore: false,
        soundIsShow: false,
        emojiIsShow: false,
        menuIsShow: false,
        menuList: [{
            "title": "图片",
            "iconCode": "e7be"
          },
          {
            "title": "视频",
            "iconCode": "e690"
          },
          {
            "title": "文件",
            "iconCode": "e69e"
          }
        ],
        emojiCodes,
        chatInputBoxHeight:'auto',
DCloud_JSON's avatar
DCloud_JSON 已提交
100 101
      }
    },
102 103 104
    computed: {
      canSend() {
        return typeof this.modelValue != "string" || this.modelValue.trim().length > 0;
105 106 107 108 109
      },
      // #ifdef H5
      extToolBar(){
        // 调用扩展点,扩展程序可以在消息输入框新增一个工具类的项
        const currentConversationId = uniIm.currentConversationId
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
        if (currentConversationId) {
          return uniIm.extensions
            .invokeExts("input-msg-tool-bar",uniIm.conversation.find(currentConversationId))
            .filter((result) => result && result.component)
            .map((result) => {
              return {
                component: markRaw(result.component),
                props: result.props||{}
              };
            });
        }else{
          return []
        }
123
      }
124
      // #endif
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
125
    },
126 127 128 129 130 131 132 133 134 135 136 137 138
    props: {
      modelValue: {
        type: [String, Object],
        default: ""
      },
      keyboardMaxHeight: {
        type: Number,
        default: 0
      },
      keyboardHeight: {
        type: Number,
        default: 0
      }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
139
    },
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    mounted() {
      currentModelValue = this.modelValue;
      // #ifdef H5
      // pc宽屏支持拖动改变输入框高度
      if(uniIm.isWidescreen){
        this.chatInputBoxHeight = uni.getStorageSync('uni-im-data:chatInputBoxHeight') || '300px';
        // 拖动uni-im-chat-input的顶部,改变高度
        let startY,startHeight,isMove = false;
        const dragLine = document.querySelector('#drag-line');
        dragLine.addEventListener('mousedown', (e) => {
          startY = e.clientY;
          startHeight = parseInt(this.chatInputBoxHeight);
          isMove = true;
        });
        document.addEventListener('mousemove', (e) => {
          if (isMove) {
            const moveY = startY - e.clientY;
            const height = startHeight + moveY;
158
            if(height > 800){
159 160 161 162 163 164 165
              // 改变鼠标样式,为向下箭头
              document.body.style.cursor = 's-resize';
            }else if(height < 200){
              // 改变鼠标样式,为向上箭头
              document.body.style.cursor = 'n-resize';
            }else{
              this.chatInputBoxHeight = height + 'px';
DCloud_JSON's avatar
DCloud_JSON 已提交
166 167
            }
          }
168 169 170 171 172 173 174 175 176 177
        });
        document.addEventListener('mouseup', () => {
          if(isMove){
            document.body.style.cursor = '';
            isMove = false;
            uni.setStorageSync('uni-im-data:chatInputBoxHeight',this.chatInputBoxHeight)
          }
        });
      }
      // #endif
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
178
    },
179 180 181 182 183
    watch: {
      modelValue:{
        handler(modelValue,oldValue) {
          // console.log('###modelValue', modelValue,JSON.stringify(currentModelValue) != JSON.stringify(modelValue) );
          if(JSON.stringify(currentModelValue) != JSON.stringify(modelValue) ) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
184 185
            const {html} = modelValue
            this.setContent(html?{html}:modelValue)
186 187 188 189 190
          }
        },
        deep: true,
        immediate: true
      }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
191
    },
192 193 194
    methods: {
      sendSoundMsg(e) {
        this.$emit('sendSoundMsg',e)
DCloud_JSON's avatar
DCloud_JSON 已提交
195
      },
196 197 198
      sendCodeMsg(e) {
        this.$emit('sendCodeMsg',e)
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
199 200 201
      showMenberList(e) {
        this.$emit('showMenberList',e)
      },
202 203 204 205
      oninput(e) {
        currentModelValue = e.value;
        this.$emit('update:modelValue', e.value)
        this.$emit('input',e)
DCloud_JSON's avatar
DCloud_JSON 已提交
206
      },
207 208 209
      updateModelValue({value}) {
        this.$emit('update:modelValue', value)
      },
210 211 212 213
      focus() {
        // console.log('focus');
        this.$refs.editor.callRmd('$focus')
      },
214
      async addCallUser({user_id, nickname},needDeleteLeftART = true,DL=0) {
215 216 217 218 219 220 221 222 223 224
        // 隐藏发送语音消息模式
        this.soundIsShow = false;
        
        this.raiseEditor = true;
        setTimeout(()=>this.raiseEditor = false,2000)
        
        if(needDeleteLeftART){
          // console.error('needDeleteLeftART');
          // DL 是需要删除的个数,因为web-pc端用户可以输入关键词筛选用户导致多出字符
          this.$refs.editor.callRmd('$deleteLeftChar', 1+DL)
DCloud_JSON's avatar
DCloud_JSON 已提交
225
        }else{
226 227 228
          // console.error('no needDeleteLeftART');
          await uniIm.utils.sleep(100)
          this.$refs.editor.callRmd('$focus')
DCloud_JSON's avatar
DCloud_JSON 已提交
229
        }
230
        await uniIm.utils.sleep(10)
DCloud_JSON's avatar
DCloud_JSON 已提交
231
        
232
        // 提醒末尾的此空格在margin-right: -3px;内,用于解决办法浏览器非文本节点后的光标定位不正确的问题
233
        const html =`<span class="nickname" contenteditable="false" user_id="${user_id}">@${nickname}</span>&nbsp;`
234 235
        this.addHtmlToCursor(html)
        // setTimeout(()=>this.$refs.editor.callRmd('$restoreCursor'),500)
DCloud_JSON's avatar
DCloud_JSON 已提交
236
      },
237 238 239 240 241 242 243 244
      setShowMore(type) {
        if (this.showMore == type && this.keyboardHeight === 0) {
          this.showMore = false;
        } else {
          this.showMore = type;
          if(this.soundIsShow && type === 'emoji'){
            this.soundIsShow = false
          }
DCloud_JSON's avatar
DCloud_JSON 已提交
245
        }
246 247 248 249 250 251
      },
      clickEmojiItem(uniCodeEmoji, event) {
        console.log('clickEmojiItem', uniCodeEmoji, event);
        this.addHtmlToCursor(uniCodeEmoji,false)
      },
      addHtmlToCursor(html,focus=true) {
252
        this.$refs.editor.callRmd('$addHtmlToCursor',html,focus)
253 254
      },
      setContent(content) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
255
        // console.log('setContent', content);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        this.$refs.editor.callRmd('$setContent',content)
        // this.$emit('update:modelValue', content)
      },
      clickMenu(index, event) {
        console.log('clickMenu', index, event);
        let parrent = this.$parent
        // #ifdef H5
        parrent = this.$parent.$parent.$parent
        // #endif
        
        if (index < 2) {
          parrent.chooseFileSendMsg(index === 0 ? 'image' : 'video')
        }
        if (index === 2) {
          // #ifdef APP
          return uni.showToast({
            title: '暂不支持,发送文件',
            icon: 'none'
          });
          // #endif
          parrent.chooseFileSendMsg('all')
        }
      },
      changeSoundIsShow() {
        console.log('changeSoundIsShow');
        this.soundIsShow = !this.soundIsShow;
        this.showMore = false;
      },
      confirm() {
        console.log('confirm');
        this.$emit('confirm');
      }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
288 289 290 291
    }
  }
</script>

292
<style lang="scss">
293 294 295 296 297 298 299
.uni-im-chat-input {
  padding:0 5px;
  border-top: 1px solid #f5f5f5;
  & > view {
    // justify-content: center;
    // align-items: center;
    // align-content: center;
DCloud_JSON's avatar
DCloud_JSON 已提交
300
  }
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  .top-menu,.send-msg-btn-box{
    // 默认隐藏 pc端才显示
    display: none;
  }
  .main {
    flex: 1;
    flex-direction: row;
    justify-content: space-between;
    align-items: flex-end;
    overflow: hidden;
    .icon {
      margin:0 5px;
      margin-bottom: 20px;
      width: 30px;
      height: 30px;
      align-content: center;
      flex-shrink: 0;
      &.confirm {
        width: auto;
        color: #fff;
        font-size: .75rem;
        border-radius: 6px;
        background-color: #2faf4c;
        height: 28px;
        line-height: 28px;
326
        padding: 0 8px;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
        text-align: center;
      }
    }
    .camera-filled-box {
      background-color: #3b81fd;
      justify-content: center;
      width: 26px;
      height: 26px;
      border-radius: 100px;
    }
    .editor-box-parent{
      flex: 1;
      height: 100%;
      overflow: hidden;
      .editor-box {
        position: relative;
        overflow: hidden;
        flex: 1;
        margin:10px 5px;
        padding: 10px 8px;
        background-color: #FFF;
        min-height: 46px;
        &,.editor {
          border-radius: 10px;
        }
        .editor {
          // flex: 1;
        }
        .uni-im-sound {
          position: absolute;
          top: 0;
          left: 0;
          height: 100%;
          width: 100%;
          z-index: 10;
        }
      }
    }
  }
  .media-box {
    .menu {
      flex-direction: row;
      justify-content: self-start;
      align-items: self-start;
      padding: 10px 0;
      // height: 100%;
      .menu-item {
        margin: 15px;
        width: 60px;
        height: 60px;
        justify-content: center;
        align-items: center;
        .menu-item-icon {
          width: 40px;
          height: 40px;
          justify-content: center;
          align-items: center;
          background-color: #FFF;
          padding: 8px;
          border-radius: 10px;
          margin-bottom: 5px;
        }
        .menu-item-text {
          font-size: 12px;
          color: #666666;
        }
      }
    }
    .emoji-list-box {
      height: 100%;
      padding: 15rpx;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: space-between;
      .item {
        text-align: center;
        font-size: 65rpx;
        width: 87rpx;
        height: 87rpx;
        justify-content: center;
        align-items: center;
        display: inline-block;
      }
    }
  }
}


/* #ifdef H5 */
@media screen and (min-device-width:560px){
  .uni-im-chat-input {
    position: relative;
    flex-direction: column;
    #drag-line {
      // 鼠标变上下拖动的光标
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
422
      cursor: row-resize;
423
      height: 5px;
424 425
      position: absolute;
      left: 0;
426
      top: -3px;
427
      width: 100%;
428
      z-index: 1;
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    }
    .main {
      .icon {
        display: none;
      }
      .editor-box-parent {
        flex-direction: column-reverse;
      }
      .editor-box {
        height: 200px;
        padding: 0;
        background-color: transparent !important;
        & ::v-deep {
          .uni-im-editor-box {
            height: 100%;
          }
          .uni-im-editor{
            height: 100%;
            max-height: 100%;
          }
        }
      }
    }
    
    .top-menu {
      display: flex;
      height: 45px;
      flex-direction: row;
      justify-content: flex-start;
      align-items: center;
      background-color: #f5f5f5;
      .item {
        margin:0 20px 0 10px;
      }
    }
    .send-msg-btn-box {
      display: flex;
      flex-direction: row;
      height: 45px;
      justify-content: flex-end;
      align-items: center;
      padding:0 5px;
      .send-btn-tip {
        font-size: 12px;
        color: #666666;
      }
      .send {
        font-size: 12px;
        margin:0 10px;
        color: #fff;
      }
    }
  }
  
}
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
484
/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
485
</style>