zs 1 year ago
parent
commit
72b4a0a4db
1 changed files with 51 additions and 1 deletions
  1. 51 1
      src/layout/parts/Tagsbar.vue

+ 51 - 1
src/layout/parts/Tagsbar.vue

@@ -1,6 +1,6 @@
 <template>
   <div class="tags-container">
-    <el-scrollbar class="scroll-container" :vertical="false">
+    <el-scrollbar class="scroll-container" :vertical="false" @wheel.prevent="handleScroll">
       <router-link
         ref="tagRef"
         v-for="tag in visitedViews"
@@ -8,6 +8,7 @@
         :class="'tags-item ' + (isActive(tag) ? 'active' : '')"
         :to="{ path: tag.path, query: tag.query }"
         @click.middle="!isAffix(tag) ? closeSelectedTag(tag) : ''"
+        @contextmenu.prevent="openContentMenu(tag, $event)"
       >
         {{ translateRouteTitle(tag.title) }}
         <SvgIcon class="close-icon" icon-class="close" v-if="!isAffix(tag)" @click.prevent.stop="closeSelectedTag(tag)"></SvgIcon>
@@ -86,6 +87,15 @@ watch(
   }
 )
 
+const contentMenuVisible = ref(false) // 右键菜单是否显示
+watch(contentMenuVisible, (value) => {
+  if (value) {
+    document.body.addEventListener('click', closeContentMenu)
+  } else {
+    document.body.removeEventListener('click', closeContentMenu)
+  }
+})
+
 /**
  * 过滤出需要固定的标签
  */
@@ -238,6 +248,46 @@ function closeAllTags(view) {
   })
 }
 
+/**
+ * 打开右键菜单
+ */
+function openContentMenu(tag, e) {
+  const menuMinWidth = 105
+  const offsetLeft = proxy?.$el.getBoundingClientRect().left // container margin left
+  const offsetWidth = proxy?.$el.offsetWidth // container width
+  const maxLeft = offsetWidth - menuMinWidth // left boundary
+  const l = e.clientX - offsetLeft + 15 // 15: margin right
+
+  if (l > maxLeft) {
+    left.value = maxLeft
+  } else {
+    left.value = l
+  }
+
+  // 混合模式下,需要减去顶部菜单(fixed)的高度
+  if (layout.value === 'mix') {
+    top.value = e.clientY - 50
+  } else {
+    top.value = e.clientY
+  }
+
+  contentMenuVisible.value = true
+  selectedTag.value = tag
+}
+
+/**
+ * 关闭右键菜单
+ */
+function closeContentMenu() {
+  contentMenuVisible.value = false
+}
+
+/**
+ * 滚动事件
+ */
+function handleScroll() {
+  closeContentMenu()
+}
 onMounted(() => {
   initTags()
 })