|
| 1 | +<template> |
| 2 | + <div class="tags-view-container"> |
| 3 | + <el-scrollbar wrap-class="tags-view-wrapper"> |
| 4 | + <router-link |
| 5 | + v-for="tag in visitedViews" |
| 6 | + :key="tag.path" |
| 7 | + :to="{ path: tag.path, query: tag.query }" |
| 8 | + class="tags-view-item" |
| 9 | + :class="isActive(tag) ? 'active' : ''" |
| 10 | + @contextmenu.prevent="openMenu(tag, $event)" |
| 11 | + > |
| 12 | + {{ tag.title }} |
| 13 | + <el-icon |
| 14 | + v-if="visitedViews.length > 1" |
| 15 | + class="el-icon-close" |
| 16 | + @click.prevent.stop="closeSelectedTag(tag)" |
| 17 | + > |
| 18 | + <Close /> |
| 19 | + </el-icon> |
| 20 | + </router-link> |
| 21 | + </el-scrollbar> |
| 22 | + </div> |
| 23 | +</template> |
| 24 | + |
| 25 | +<script setup lang="ts"> |
| 26 | +import { computed } from 'vue'; |
| 27 | +import { useRoute, useRouter } from 'vue-router'; |
| 28 | +import { Close } from '@element-plus/icons-vue'; |
| 29 | +import { visitedViews, delView, type TagView } from '@/store/tagsView'; |
| 30 | +
|
| 31 | +const route = useRoute(); |
| 32 | +const router = useRouter(); |
| 33 | +
|
| 34 | +const isActive = (tag: TagView) => { |
| 35 | + return tag.path === route.path; |
| 36 | +}; |
| 37 | +
|
| 38 | +const closeSelectedTag = async (view: TagView) => { |
| 39 | + const views = await delView(view); |
| 40 | + if (isActive(view)) { |
| 41 | + toLastView(views, view); |
| 42 | + } |
| 43 | +}; |
| 44 | +
|
| 45 | +const toLastView = (views: TagView[], view: TagView) => { |
| 46 | + const latestView = views.slice(-1)[0]; |
| 47 | + if (latestView) { |
| 48 | + router.push((latestView.fullPath || latestView.path) as string); |
| 49 | + } else { |
| 50 | + // default redirect to home or somewhere safe if no views |
| 51 | + router.push('/'); |
| 52 | + } |
| 53 | +}; |
| 54 | +
|
| 55 | +const openMenu = (tag: TagView, e: MouseEvent) => { |
| 56 | + // context menu logic can be added here if needed |
| 57 | +}; |
| 58 | +</script> |
| 59 | + |
| 60 | +<style lang="scss" scoped> |
| 61 | +.tags-view-container { |
| 62 | + height: 34px; |
| 63 | + width: 100%; |
| 64 | + flex-shrink: 0; // prevent being squished by main content |
| 65 | + background: var(--bg-main); |
| 66 | + border-bottom: 1px solid var(--sidebar-border); |
| 67 | + box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04); |
| 68 | + z-index: 10; // ensure it is above the main scrollbar content if any shadows exist |
| 69 | + overflow: hidden; |
| 70 | + |
| 71 | + .tags-view-wrapper { |
| 72 | + .tags-view-item { |
| 73 | + display: inline-block; |
| 74 | + position: relative; |
| 75 | + cursor: pointer; |
| 76 | + height: 26px; |
| 77 | + line-height: 26px; |
| 78 | + border: 1px solid var(--sidebar-border); |
| 79 | + color: var(--text-primary); |
| 80 | + background: var(--panel-bg); |
| 81 | + padding: 0 8px; |
| 82 | + font-size: 13px; |
| 83 | + margin-left: 5px; |
| 84 | + margin-top: 4px; |
| 85 | + border-radius: 4px; |
| 86 | + text-decoration: none; |
| 87 | + |
| 88 | + &:first-of-type { |
| 89 | + margin-left: 15px; |
| 90 | + } |
| 91 | + |
| 92 | + &.active { |
| 93 | + background-color: var(--color-primary); |
| 94 | + color: #fff; |
| 95 | + border-color: var(--color-primary); |
| 96 | + |
| 97 | + &::before { |
| 98 | + content: ''; |
| 99 | + background: #fff; |
| 100 | + display: inline-block; |
| 101 | + width: 8px; |
| 102 | + height: 8px; |
| 103 | + border-radius: 50%; |
| 104 | + position: relative; |
| 105 | + margin-right: 2px; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + .el-icon-close { |
| 110 | + width: 16px; |
| 111 | + height: 16px; |
| 112 | + vertical-align: middle; |
| 113 | + border-radius: 50%; |
| 114 | + text-align: center; |
| 115 | + transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1); |
| 116 | + transform-origin: 100% 50%; |
| 117 | + margin-left: 2px; |
| 118 | + |
| 119 | + &:before { |
| 120 | + transform: scale(0.6); |
| 121 | + display: inline-block; |
| 122 | + } |
| 123 | + |
| 124 | + &:hover { |
| 125 | + background-color: #b4bccc; |
| 126 | + color: #fff; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +</style> |
0 commit comments