Bläddra i källkod

添加右键菜单操作

wangwei 4 år sedan
förälder
incheckning
f887d70402
5 ändrade filer med 63 tillägg och 13 borttagningar
  1. 8 3
      package-lock.json
  2. 2 1
      package.json
  3. 49 3
      src/components/ChatFrame.vue
  4. 1 1
      src/components/ChatList.vue
  5. 3 5
      src/main.js

+ 8 - 3
package-lock.json

@@ -14384,9 +14384,9 @@
       "dev": true
     },
     "v-contextmenu": {
-      "version": "3.0.0",
-      "resolved": "https://registry.npmjs.org/v-contextmenu/-/v-contextmenu-3.0.0.tgz",
-      "integrity": "sha512-zi38JxmTt66TmljgV1JbfEa9WvoQkpzRuEwZK7Tjb2XoRejbWLozQtkyTWXJa6x6Y3FrVDfgT36w01gpTpo41A=="
+      "version": "2.9.0",
+      "resolved": "https://registry.npmjs.org/v-contextmenu/-/v-contextmenu-2.9.0.tgz",
+      "integrity": "sha512-8FnZXOfELGvfX9Zv8mmdmsxpZzS83kSS6eNyxMekHN+yo1+cZM2oeKOdpLH/Rs/n2LSJkhfxxzgS6EeGGRQyVA=="
     },
     "v-viewer": {
       "version": "1.6.4",
@@ -14530,6 +14530,11 @@
       "resolved": "https://registry.npmjs.org/vue-router/-/vue-router-3.5.2.tgz",
       "integrity": "sha512-807gn82hTnjCYGrnF3eNmIw/dk7/GE4B5h69BlyCK9KHASwSloD1Sjcn06zg9fVG4fYH2DrsNBZkpLtb25WtaQ=="
     },
+    "vue-runtime-helpers": {
+      "version": "1.1.2",
+      "resolved": "https://registry.npmjs.org/vue-runtime-helpers/-/vue-runtime-helpers-1.1.2.tgz",
+      "integrity": "sha512-pZfGp+PW/IXEOyETE09xQHR1CKkR9HfHZdnMD/FVLUNI+HxYTa82evx5WrF6Kz4s82qtqHvMZ8MZpbk2zT2E1Q=="
+    },
     "vue-socket.io": {
       "version": "3.0.10",
       "resolved": "https://registry.npmjs.org/vue-socket.io/-/vue-socket.io-3.0.10.tgz",

+ 2 - 1
package.json

@@ -19,10 +19,11 @@
     "localforage": "^1.9.0",
     "socket.io-client": "^4.1.3",
     "socketio-file-upload": "^0.7.3",
-    "v-contextmenu": "^3.0.0",
+    "v-contextmenu": "^2.9.0",
     "v-viewer": "^1.6.4",
     "vue": "^2.5.2",
     "vue-router": "^3.0.1",
+    "vue-runtime-helpers": "^1.1.2",
     "vue-socket.io": "^3.0.10",
     "vuex": "^3.6.2"
   },

+ 49 - 3
src/components/ChatFrame.vue

@@ -24,7 +24,7 @@
                   src="https://img2.baidu.com/it/u=1077360284,2857506492&fm=26&fmt=auto&gp=0.jpg"
                 ></el-avatar>
               </div>
-              <div class="group-record-item">
+              <div class="firend-group-item">
                 <!-- 链接 -->
                 <a
                   class="message-content-text receive-record-item"
@@ -71,7 +71,11 @@
             </div>
 
             <!-- self send msg record -->
-            <div v-if="item.toid !== socket.id" class="record-item send-record">
+            <div
+              v-if="item.toid !== socket.id"
+              v-contextmenu="'message' + item.msg + item.toid"
+              class="record-item send-record"
+            >
               <!-- 链接 -->
               <a
                 class="message-content-text"
@@ -125,6 +129,15 @@
                   src="https://img2.baidu.com/it/u=1077360284,2857506492&fm=26&fmt=auto&gp=0.jpg"
                 ></el-avatar>
               </div> -->
+              <!-- 自定义右键菜单 -->
+              <v-contextmenu :ref="'message' + item.msg + item.toid">
+                <v-contextmenu-item v-if="item.type === 'text'" @click="handleCommand('COPY', item)"
+                  >复制</v-contextmenu-item
+                >
+                <v-contextmenu-item @click="handleCommand('REVOKE', item)"
+                  >撤回</v-contextmenu-item
+                >
+              </v-contextmenu>
             </div>
           </div>
         </el-scrollbar>
@@ -396,6 +409,36 @@ export default {
   },
   methods: {
     ...mapMutations("chat", ["set_friend_record", "set_room_record"]),
+
+
+    handleCommand(type, message) {
+      if (type === 'COPY') {
+      // 复制功能
+      const copy = (e) => {
+        e.preventDefault();
+        if (e.clipboardData) {
+          e.clipboardData.setData('text/plain', message.msg);
+        } else if (window.clipboardData) {
+          window.clipboardData.setData('Text', message.msg);
+        }
+      };
+      window.addEventListener('copy', copy);
+      document.execCommand('Copy');
+      window.removeEventListener('copy', copy);
+      this.$message.info('已粘贴至剪切板');
+      // eslint-disable-next-line no-undef
+    } else if (type === 'REVOKE') {
+      this.$message.error('暂不支持')
+      // 消息撤回功能
+      // this.socket.emit('revokeMessage', {
+      //   userId: this.user.userId, // 当前用户Id
+      //   username: this.user.username, // 当前用户名称
+      //   groupId: this.activeRoom.groupId, // 当前群组Id
+      //   friendId: this.activeRoom.userId, // 当前好友Id
+      //   _id: message._id, // 撤回的消息Id
+      // });
+    }
+    },
     handleSuccess(res, file) {
       console.log(`res`, res);
       this.imageUrl = URL.createObjectURL(file.raw);
@@ -619,6 +662,7 @@ export default {
     // 将输入框中的图片替换为emoji表情
     formatInputCon() {
       let inputValue = document.getElementById("charInput").innerHTML;
+      console.log(`inputValue`, inputValue)
       inputValue = inputValue.replace(/<img.*?(?:>|\/>)/gi, val => {
         let unicode =
           "!!" + val.match(/unicode=[\'\"]?([^\'\"]*)[\'\"]?/i)[1] + "!!";
@@ -627,7 +671,7 @@ export default {
       });
 
       inputValue = inputValue.replace(/&amp;/gi, "&"); // 还原&
-      inputValue = inputValue.replace(/<img src=\"http:\/\/localhost:8080/gi, "<img src=\""); // 去掉复制表情图片的baseurl 
+      inputValue = inputValue.replace(/<img src=\"http:\/\/localhost:8080/gi, "<img src=\""); // 去掉复制表情图片的baseurl
 
       return inputValue;
     },
@@ -705,6 +749,7 @@ export default {
       const data =
         e.clipboardData.getData("text/html") ||
         e.clipboardData.getData("text/plain");
+        console.log(`data`, data)
       const reg = /<img\s+.*?src=(?:'(.+?)'|"(.+?)")\s*.*?(?:>|\/>)/gim;
       const srcReg = /^(?!<img src=\"http:\/\/localhost:8080).*/gim // 匹配不以<img src=\"http:\/\/localhost:8080)开头
       const imgArr = data.match(reg);
@@ -812,6 +857,7 @@ export default {
   font-size: 14px;
   color: gray;
 }
+.firend-group-item > span,
 .group-record-item > span {
   padding: 8px 12px;
   border-radius: 5px;

+ 1 - 1
src/components/ChatList.vue

@@ -3,7 +3,7 @@
     <div class="self-wrap">
       <div class="avatar">
         <el-avatar
-          src="https://img2.baidu.com/it/u=1077360284,2857506492&fm=26&fmt=auto&gp=0.jpg"
+          src="https://img1.baidu.com/it/u=3572927457,4046757779&fm=26&fmt=auto&gp=0.jpg"
         ></el-avatar>
       </div>
       <div class="username" v-if="self">

+ 3 - 5
src/main.js

@@ -11,13 +11,11 @@ import ElementUI from 'element-ui';
 import "./assets/icon/iconfont.css"
 import 'element-ui/lib/theme-chalk/index.css';
 import 'viewerjs/dist/viewer.css';
-// import contextmenu from "v-contextmenu";
-// import "v-contextmenu/dist/themes/default.css";
-
+import contentmenu from 'v-contextmenu'
+import 'v-contextmenu/dist/index.css'
 import axios from 'axios'
 Vue.prototype.$http = axios
-
-// Vue.use(contextmenu);
+Vue.use(contentmenu);
 Vue.use(ElementUI);
 Vue.config.productionTip = false