This commit is contained in:
dap
2025-05-19 21:33:49 +08:00
27 changed files with 476 additions and 151 deletions

View File

@@ -186,6 +186,12 @@ const defaultPreferences: Preferences = {
colorWeakMode: false, colorWeakMode: false,
compact: false, compact: false,
contentCompact: 'wide', contentCompact: 'wide',
contentCompactWidth: 1200,
contentPadding: 16,
contentPaddingBottom: 16,
contentPaddingLeft: 16,
contentPaddingRight: 16,
contentPaddingTop: 16,
defaultAvatar: defaultAvatar:
'https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp', 'https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp',
defaultHomePath: '/analytics', defaultHomePath: '/analytics',
@@ -200,6 +206,7 @@ const defaultPreferences: Preferences = {
name: 'Vben Admin', name: 'Vben Admin',
preferencesButtonPosition: 'auto', preferencesButtonPosition: 'auto',
watermark: false, watermark: false,
zIndex: 200,
}, },
breadcrumb: { breadcrumb: {
enable: true, enable: true,
@@ -220,9 +227,11 @@ const defaultPreferences: Preferences = {
footer: { footer: {
enable: false, enable: false,
fixed: false, fixed: false,
height: 32,
}, },
header: { header: {
enable: true, enable: true,
height: 50,
hidden: false, hidden: false,
menuAlign: 'start', menuAlign: 'start',
mode: 'fixed', mode: 'fixed',
@@ -248,11 +257,14 @@ const defaultPreferences: Preferences = {
collapsed: false, collapsed: false,
collapsedButton: true, collapsedButton: true,
collapsedShowTitle: false, collapsedShowTitle: false,
collapseWidth: 60,
enable: true, enable: true,
expandOnHover: true, expandOnHover: true,
extraCollapse: false, extraCollapse: false,
extraCollapsedWidth: 60,
fixedButton: true, fixedButton: true,
hidden: false, hidden: false,
mixedWidth: 80,
width: 224, width: 224,
}, },
tabbar: { tabbar: {
@@ -319,6 +331,18 @@ interface AppPreferences {
compact: boolean; compact: boolean;
/** Whether to enable content compact mode */ /** Whether to enable content compact mode */
contentCompact: ContentCompactType; contentCompact: ContentCompactType;
/** Content compact width */
contentCompactWidth: number;
/** Content padding */
contentPadding: number;
/** Content bottom padding */
contentPaddingBottom: number;
/** Content left padding */
contentPaddingLeft: number;
/** Content right padding */
contentPaddingRight: number;
/** Content top padding */
contentPaddingTop: number;
// /** Default application avatar */ // /** Default application avatar */
defaultAvatar: string; defaultAvatar: string;
/** Default homepage path */ /** Default homepage path */
@@ -349,6 +373,8 @@ interface AppPreferences {
* @zh_CN Whether to enable watermark * @zh_CN Whether to enable watermark
*/ */
watermark: boolean; watermark: boolean;
/** z-index */
zIndex: number;
} }
interface BreadcrumbPreferences { interface BreadcrumbPreferences {
/** Whether breadcrumbs are enabled */ /** Whether breadcrumbs are enabled */
@@ -385,11 +411,15 @@ interface FooterPreferences {
enable: boolean; enable: boolean;
/** Whether the footer is fixed */ /** Whether the footer is fixed */
fixed: boolean; fixed: boolean;
/** Footer height */
height: number;
} }
interface HeaderPreferences { interface HeaderPreferences {
/** Whether the header is enabled */ /** Whether the header is enabled */
enable: boolean; enable: boolean;
/** Header height */
height: number;
/** Whether the header is hidden, css-hidden */ /** Whether the header is hidden, css-hidden */
hidden: boolean; hidden: boolean;
/** Header menu alignment */ /** Header menu alignment */
@@ -422,16 +452,22 @@ interface SidebarPreferences {
collapsedButton: boolean; collapsedButton: boolean;
/** Whether to show title when sidebar is collapsed */ /** Whether to show title when sidebar is collapsed */
collapsedShowTitle: boolean; collapsedShowTitle: boolean;
/** Sidebar collapse width */
collapseWidth: number;
/** Whether the sidebar is visible */ /** Whether the sidebar is visible */
enable: boolean; enable: boolean;
/** Menu auto-expand state */ /** Menu auto-expand state */
expandOnHover: boolean; expandOnHover: boolean;
/** Whether the sidebar extension area is collapsed */ /** Whether the sidebar extension area is collapsed */
extraCollapse: boolean; extraCollapse: boolean;
/** Sidebar extension area collapse width */
extraCollapsedWidth: number;
/** Whether the sidebar fixed button is visible */ /** Whether the sidebar fixed button is visible */
fixedButton: boolean; fixedButton: boolean;
/** Whether the sidebar is hidden - css */ /** Whether the sidebar is hidden - css */
hidden: boolean; hidden: boolean;
/** Mixed sidebar width */
mixedWidth: number;
/** Sidebar width */ /** Sidebar width */
width: number; width: number;
} }

View File

@@ -214,7 +214,7 @@ server {
使用 nginx 处理项目部署后的跨域问题 使用 nginx 处理项目部署后的跨域问题
1. 配置前端项目接口地址,在项目目录下的``.env.production`文件中配置: 1. 配置前端项目接口地址,在项目目录下的`.env.production`文件中配置:
```bash ```bash
VITE_GLOB_API_URL=/api VITE_GLOB_API_URL=/api

View File

@@ -339,6 +339,10 @@ interface RouteMeta {
| 'success' | 'success'
| 'warning' | 'warning'
| string; | string;
/**
* 路由的完整路径作为key默认true
*/
fullPathKey?: boolean;
/** /**
* 当前路由的子级在菜单中不展现 * 当前路由的子级在菜单中不展现
* @default false * @default false
@@ -502,6 +506,13 @@ interface RouteMeta {
用于配置页面的徽标颜色。 用于配置页面的徽标颜色。
### fullPathKey
- 类型:`boolean`
- 默认值:`true`
是否将路由的完整路径作为tab key默认true
### activePath ### activePath
- 类型:`string` - 类型:`string`
@@ -602,3 +613,32 @@ const { refresh } = useRefresh();
refresh(); refresh();
</script> </script>
``` ```
## 标签页与路由控制
在某些场景下需要单个路由打开多个标签页或者修改路由的query不打开新的标签页
每个标签页Tab使用唯一的key标识设置Tab key有三种方式优先级由高到低
- 使用路由query参数pageKey
```vue
<script setup lang="ts">
import { useRouter } from 'vue-router';
// 跳转路由
const router = useRouter();
router.push({
path: 'path',
query: {
pageKey: 'key',
},
});
```
- 路由的完整路径作为key
`meta` 属性中的 `fullPathKey`不为false则使用路由`fullPath`作为key
- 路由的path作为key
`meta` 属性中的 `fullPathKey`为false则使用路由`path`作为key

View File

@@ -185,6 +185,12 @@ const defaultPreferences: Preferences = {
colorWeakMode: false, colorWeakMode: false,
compact: false, compact: false,
contentCompact: 'wide', contentCompact: 'wide',
contentCompactWidth: 1200,
contentPadding: 16,
contentPaddingBottom: 16,
contentPaddingLeft: 16,
contentPaddingRight: 16,
contentPaddingTop: 16,
defaultAvatar: defaultAvatar:
'https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp', 'https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp',
defaultHomePath: '/analytics', defaultHomePath: '/analytics',
@@ -199,6 +205,7 @@ const defaultPreferences: Preferences = {
name: 'Vben Admin', name: 'Vben Admin',
preferencesButtonPosition: 'auto', preferencesButtonPosition: 'auto',
watermark: false, watermark: false,
zIndex: 200,
}, },
breadcrumb: { breadcrumb: {
enable: true, enable: true,
@@ -219,9 +226,11 @@ const defaultPreferences: Preferences = {
footer: { footer: {
enable: false, enable: false,
fixed: false, fixed: false,
height: 32,
}, },
header: { header: {
enable: true, enable: true,
height: 50,
hidden: false, hidden: false,
menuAlign: 'start', menuAlign: 'start',
mode: 'fixed', mode: 'fixed',
@@ -247,11 +256,14 @@ const defaultPreferences: Preferences = {
collapsed: false, collapsed: false,
collapsedButton: true, collapsedButton: true,
collapsedShowTitle: false, collapsedShowTitle: false,
collapseWidth: 60,
enable: true, enable: true,
expandOnHover: true, expandOnHover: true,
extraCollapse: false, extraCollapse: false,
extraCollapsedWidth: 60,
fixedButton: true, fixedButton: true,
hidden: false, hidden: false,
mixedWidth: 80,
width: 224, width: 224,
}, },
tabbar: { tabbar: {
@@ -318,6 +330,18 @@ interface AppPreferences {
compact: boolean; compact: boolean;
/** 是否开启内容紧凑模式 */ /** 是否开启内容紧凑模式 */
contentCompact: ContentCompactType; contentCompact: ContentCompactType;
/** 内容紧凑宽度 */
contentCompactWidth: number;
/** 内容内边距 */
contentPadding: number;
/** 内容底部内边距 */
contentPaddingBottom: number;
/** 内容左侧内边距 */
contentPaddingLeft: number;
/** 内容右侧内边距 */
contentPaddingRight: number;
/** 内容顶部内边距 */
contentPaddingTop: number;
// /** 应用默认头像 */ // /** 应用默认头像 */
defaultAvatar: string; defaultAvatar: string;
/** 默认首页地址 */ /** 默认首页地址 */
@@ -348,6 +372,8 @@ interface AppPreferences {
* @zh_CN 是否开启水印 * @zh_CN 是否开启水印
*/ */
watermark: boolean; watermark: boolean;
/** z-index */
zIndex: number;
} }
interface BreadcrumbPreferences { interface BreadcrumbPreferences {
@@ -385,11 +411,15 @@ interface FooterPreferences {
enable: boolean; enable: boolean;
/** 底栏是否固定 */ /** 底栏是否固定 */
fixed: boolean; fixed: boolean;
/** 底栏高度 */
height: number;
} }
interface HeaderPreferences { interface HeaderPreferences {
/** 顶栏是否启用 */ /** 顶栏是否启用 */
enable: boolean; enable: boolean;
/** 顶栏高度 */
height: number;
/** 顶栏是否隐藏,css-隐藏 */ /** 顶栏是否隐藏,css-隐藏 */
hidden: boolean; hidden: boolean;
/** 顶栏菜单位置 */ /** 顶栏菜单位置 */
@@ -423,16 +453,22 @@ interface SidebarPreferences {
collapsedButton: boolean; collapsedButton: boolean;
/** 侧边栏折叠时是否显示title */ /** 侧边栏折叠时是否显示title */
collapsedShowTitle: boolean; collapsedShowTitle: boolean;
/** 侧边栏折叠宽度 */
collapseWidth: number;
/** 侧边栏是否可见 */ /** 侧边栏是否可见 */
enable: boolean; enable: boolean;
/** 菜单自动展开状态 */ /** 菜单自动展开状态 */
expandOnHover: boolean; expandOnHover: boolean;
/** 侧边栏扩展区域是否折叠 */ /** 侧边栏扩展区域是否折叠 */
extraCollapse: boolean; extraCollapse: boolean;
/** 侧边栏扩展区域折叠宽度 */
extraCollapsedWidth: number;
/** 侧边栏固定按钮是否可见 */ /** 侧边栏固定按钮是否可见 */
fixedButton: boolean; fixedButton: boolean;
/** 侧边栏是否隐藏 - css */ /** 侧边栏是否隐藏 - css */
hidden: boolean; hidden: boolean;
/** 混合侧边栏宽度 */
mixedWidth: number;
/** 侧边栏宽度 */ /** 侧边栏宽度 */
width: number; width: number;
} }

View File

@@ -12,7 +12,7 @@
"license": "MIT", "license": "MIT",
"type": "module", "type": "module",
"scripts": { "scripts": {
"stub": "pnpm unbuild" "stub": "pnpm unbuild --stub"
}, },
"files": [ "files": [
"dist" "dist"

View File

@@ -1,3 +1,8 @@
import type { RouteLocationNormalized } from 'vue-router'; import type { RouteLocationNormalized } from 'vue-router';
export type TabDefinition = RouteLocationNormalized; export interface TabDefinition extends RouteLocationNormalized {
/**
* 标签页的key
*/
key?: string;
}

View File

@@ -43,6 +43,10 @@ interface RouteMeta {
| 'success' | 'success'
| 'warning' | 'warning'
| string; | string;
/**
* 路由的完整路径作为key默认true
*/
fullPathKey?: boolean;
/** /**
* 当前路由的子级在菜单中不展现 * 当前路由的子级在菜单中不展现
* @default false * @default false

View File

@@ -10,6 +10,12 @@ exports[`defaultPreferences immutability test > should not modify the config obj
"colorWeakMode": false, "colorWeakMode": false,
"compact": false, "compact": false,
"contentCompact": "wide", "contentCompact": "wide",
"contentCompactWidth": 1200,
"contentPadding": 16,
"contentPaddingBottom": 16,
"contentPaddingLeft": 16,
"contentPaddingRight": 16,
"contentPaddingTop": 16,
"defaultAvatar": "https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp", "defaultAvatar": "https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp",
"defaultHomePath": "/analytics", "defaultHomePath": "/analytics",
"dynamicTitle": true, "dynamicTitle": true,
@@ -23,6 +29,7 @@ exports[`defaultPreferences immutability test > should not modify the config obj
"name": "Vben Admin", "name": "Vben Admin",
"preferencesButtonPosition": "auto", "preferencesButtonPosition": "auto",
"watermark": false, "watermark": false,
"zIndex": 200,
}, },
"breadcrumb": { "breadcrumb": {
"enable": true, "enable": true,
@@ -43,9 +50,11 @@ exports[`defaultPreferences immutability test > should not modify the config obj
"footer": { "footer": {
"enable": false, "enable": false,
"fixed": false, "fixed": false,
"height": 32,
}, },
"header": { "header": {
"enable": true, "enable": true,
"height": 50,
"hidden": false, "hidden": false,
"menuAlign": "start", "menuAlign": "start",
"mode": "fixed", "mode": "fixed",
@@ -68,14 +77,17 @@ exports[`defaultPreferences immutability test > should not modify the config obj
}, },
"sidebar": { "sidebar": {
"autoActivateChild": false, "autoActivateChild": false,
"collapseWidth": 60,
"collapsed": false, "collapsed": false,
"collapsedButton": true, "collapsedButton": true,
"collapsedShowTitle": false, "collapsedShowTitle": false,
"enable": true, "enable": true,
"expandOnHover": true, "expandOnHover": true,
"extraCollapse": false, "extraCollapse": false,
"extraCollapsedWidth": 60,
"fixedButton": true, "fixedButton": true,
"hidden": false, "hidden": false,
"mixedWidth": 80,
"width": 224, "width": 224,
}, },
"tabbar": { "tabbar": {

View File

@@ -9,6 +9,12 @@ const defaultPreferences: Preferences = {
colorWeakMode: false, colorWeakMode: false,
compact: false, compact: false,
contentCompact: 'wide', contentCompact: 'wide',
contentCompactWidth: 1200,
contentPadding: 16,
contentPaddingBottom: 16,
contentPaddingLeft: 16,
contentPaddingRight: 16,
contentPaddingTop: 16,
defaultAvatar: defaultAvatar:
'https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp', 'https://unpkg.com/@vbenjs/static-source@0.1.7/source/avatar-v1.webp',
defaultHomePath: '/analytics', defaultHomePath: '/analytics',
@@ -23,6 +29,7 @@ const defaultPreferences: Preferences = {
name: 'Vben Admin', name: 'Vben Admin',
preferencesButtonPosition: 'auto', preferencesButtonPosition: 'auto',
watermark: false, watermark: false,
zIndex: 200,
}, },
breadcrumb: { breadcrumb: {
enable: true, enable: true,
@@ -43,13 +50,16 @@ const defaultPreferences: Preferences = {
footer: { footer: {
enable: false, enable: false,
fixed: false, fixed: false,
height: 32,
}, },
header: { header: {
enable: true, enable: true,
height: 50,
hidden: false, hidden: false,
menuAlign: 'start', menuAlign: 'start',
mode: 'fixed', mode: 'fixed',
}, },
logo: { logo: {
enable: true, enable: true,
source: 'https://unpkg.com/@vbenjs/static-source@0.1.7/source/logo-v1.webp', source: 'https://unpkg.com/@vbenjs/static-source@0.1.7/source/logo-v1.webp',
@@ -71,11 +81,14 @@ const defaultPreferences: Preferences = {
collapsed: false, collapsed: false,
collapsedButton: true, collapsedButton: true,
collapsedShowTitle: false, collapsedShowTitle: false,
collapseWidth: 60,
enable: true, enable: true,
expandOnHover: true, expandOnHover: true,
extraCollapse: false, extraCollapse: false,
extraCollapsedWidth: 60,
fixedButton: true, fixedButton: true,
hidden: false, hidden: false,
mixedWidth: 80,
width: 224, width: 224,
}, },
tabbar: { tabbar: {

View File

@@ -33,6 +33,18 @@ interface AppPreferences {
compact: boolean; compact: boolean;
/** 是否开启内容紧凑模式 */ /** 是否开启内容紧凑模式 */
contentCompact: ContentCompactType; contentCompact: ContentCompactType;
/** 内容紧凑宽度 */
contentCompactWidth: number;
/** 内容内边距 */
contentPadding: number;
/** 内容底部内边距 */
contentPaddingBottom: number;
/** 内容左侧内边距 */
contentPaddingLeft: number;
/** 内容右侧内边距 */
contentPaddingRight: number;
/** 内容顶部内边距 */
contentPaddingTop: number;
// /** 应用默认头像 */ // /** 应用默认头像 */
defaultAvatar: string; defaultAvatar: string;
/** 默认首页地址 */ /** 默认首页地址 */
@@ -63,6 +75,8 @@ interface AppPreferences {
* @zh_CN 是否开启水印 * @zh_CN 是否开启水印
*/ */
watermark: boolean; watermark: boolean;
/** z-index */
zIndex: number;
} }
interface BreadcrumbPreferences { interface BreadcrumbPreferences {
@@ -100,11 +114,15 @@ interface FooterPreferences {
enable: boolean; enable: boolean;
/** 底栏是否固定 */ /** 底栏是否固定 */
fixed: boolean; fixed: boolean;
/** 底栏高度 */
height: number;
} }
interface HeaderPreferences { interface HeaderPreferences {
/** 顶栏是否启用 */ /** 顶栏是否启用 */
enable: boolean; enable: boolean;
/** 顶栏高度 */
height: number;
/** 顶栏是否隐藏,css-隐藏 */ /** 顶栏是否隐藏,css-隐藏 */
hidden: boolean; hidden: boolean;
/** 顶栏菜单位置 */ /** 顶栏菜单位置 */
@@ -138,16 +156,22 @@ interface SidebarPreferences {
collapsedButton: boolean; collapsedButton: boolean;
/** 侧边栏折叠时是否显示title */ /** 侧边栏折叠时是否显示title */
collapsedShowTitle: boolean; collapsedShowTitle: boolean;
/** 侧边栏折叠宽度 */
collapseWidth: number;
/** 侧边栏是否可见 */ /** 侧边栏是否可见 */
enable: boolean; enable: boolean;
/** 菜单自动展开状态 */ /** 菜单自动展开状态 */
expandOnHover: boolean; expandOnHover: boolean;
/** 侧边栏扩展区域是否折叠 */ /** 侧边栏扩展区域是否折叠 */
extraCollapse: boolean; extraCollapse: boolean;
/** 侧边栏扩展区域折叠宽度 */
extraCollapsedWidth: number;
/** 侧边栏固定按钮是否可见 */ /** 侧边栏固定按钮是否可见 */
fixedButton: boolean; fixedButton: boolean;
/** 侧边栏是否隐藏 - css */ /** 侧边栏是否隐藏 - css */
hidden: boolean; hidden: boolean;
/** 混合侧边栏宽度 */
mixedWidth: number;
/** 侧边栏宽度 */ /** 侧边栏宽度 */
width: number; width: number;
} }

View File

@@ -36,7 +36,7 @@ export interface VbenButtonGroupProps
btnClass?: any; btnClass?: any;
gap?: number; gap?: number;
multiple?: boolean; multiple?: boolean;
options?: { label: CustomRenderType; value: ValueType }[]; options?: { [key: string]: any; label: CustomRenderType; value: ValueType }[];
showIcon?: boolean; showIcon?: boolean;
size?: 'large' | 'middle' | 'small'; size?: 'large' | 'middle' | 'small';
} }

View File

@@ -119,7 +119,7 @@ async function onBtnClick(value: ValueType) {
<CircleCheckBig v-else-if="innerValue.includes(btn.value)" /> <CircleCheckBig v-else-if="innerValue.includes(btn.value)" />
<Circle v-else /> <Circle v-else />
</div> </div>
<slot name="option" :label="btn.label" :value="btn.value"> <slot name="option" :label="btn.label" :value="btn.value" :data="btn">
<VbenRenderContent :content="btn.label" /> <VbenRenderContent :content="btn.label" />
</slot> </slot>
</Button> </Button>
@@ -127,6 +127,9 @@ async function onBtnClick(value: ValueType) {
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
.vben-check-button-group { .vben-check-button-group {
display: flex;
flex-wrap: wrap;
&:deep(.size-large) button { &:deep(.size-large) button {
.icon-wrapper { .icon-wrapper {
margin-right: 0.3rem; margin-right: 0.3rem;
@@ -159,5 +162,16 @@ async function onBtnClick(value: ValueType) {
} }
} }
} }
&.no-gap > :deep(button):nth-of-type(1) {
border-right-width: 0;
}
&.no-gap {
:deep(button + button) {
margin-right: -1px;
border-left-width: 1px;
}
}
} }
</style> </style>

View File

@@ -224,15 +224,20 @@ defineExpose({
:class=" :class="
cn('cursor-pointer', getNodeClass?.(item), { cn('cursor-pointer', getNodeClass?.(item), {
'data-[selected]:bg-accent': !multiple, 'data-[selected]:bg-accent': !multiple,
'cursor-not-allowed': disabled,
})
"
v-bind="
Object.assign(item.bind, {
onfocus: disabled ? 'this.blur()' : undefined,
}) })
" "
v-bind="item.bind"
@select=" @select="
(event) => { (event) => {
if (event.detail.originalEvent.type === 'click') { if (event.detail.originalEvent.type === 'click') {
event.preventDefault(); event.preventDefault();
} }
onSelect(item, event.detail.isSelected); !disabled && onSelect(item, event.detail.isSelected);
} }
" "
@toggle=" @toggle="
@@ -240,7 +245,7 @@ defineExpose({
if (event.detail.originalEvent.type === 'click') { if (event.detail.originalEvent.type === 'click') {
event.preventDefault(); event.preventDefault();
} }
onToggle(item); !disabled && onToggle(item);
} }
" "
class="tree-node focus:ring-grass8 my-0.5 flex items-center rounded px-2 py-1 outline-none focus:ring-2" class="tree-node focus:ring-grass8 my-0.5 flex items-center rounded px-2 py-1 outline-none focus:ring-2"
@@ -262,10 +267,11 @@ defineExpose({
<Checkbox <Checkbox
v-if="multiple" v-if="multiple"
:checked="isSelected" :checked="isSelected"
:disabled="disabled"
:indeterminate="isIndeterminate" :indeterminate="isIndeterminate"
@click=" @click="
() => { () => {
handleSelect(); !disabled && handleSelect();
// onSelect(item, !isSelected); // onSelect(item, !isSelected);
} }
" "
@@ -276,7 +282,7 @@ defineExpose({
(_event) => { (_event) => {
// $event.stopPropagation(); // $event.stopPropagation();
// $event.preventDefault(); // $event.preventDefault();
handleSelect(); !disabled && handleSelect();
// onSelect(item, !isSelected); // onSelect(item, !isSelected);
} }
" "

View File

@@ -40,14 +40,14 @@ const style = computed(() => {
const tabsView = computed(() => { const tabsView = computed(() => {
return props.tabs.map((tab) => { return props.tabs.map((tab) => {
const { fullPath, meta, name, path } = tab || {}; const { fullPath, meta, name, path, key } = tab || {};
const { affixTab, icon, newTabTitle, tabClosable, title } = meta || {}; const { affixTab, icon, newTabTitle, tabClosable, title } = meta || {};
return { return {
affixTab: !!affixTab, affixTab: !!affixTab,
closable: Reflect.has(meta, 'tabClosable') ? !!tabClosable : true, closable: Reflect.has(meta, 'tabClosable') ? !!tabClosable : true,
fullPath, fullPath,
icon: icon as string, icon: icon as string,
key: fullPath || path, key,
meta, meta,
name, name,
path, path,

View File

@@ -47,14 +47,14 @@ const typeWithClass = computed(() => {
const tabsView = computed(() => { const tabsView = computed(() => {
return props.tabs.map((tab) => { return props.tabs.map((tab) => {
const { fullPath, meta, name, path } = tab || {}; const { fullPath, meta, name, path, key } = tab || {};
const { affixTab, icon, newTabTitle, tabClosable, title } = meta || {}; const { affixTab, icon, newTabTitle, tabClosable, title } = meta || {};
return { return {
affixTab: !!affixTab, affixTab: !!affixTab,
closable: Reflect.has(meta, 'tabClosable') ? !!tabClosable : true, closable: Reflect.has(meta, 'tabClosable') ? !!tabClosable : true,
fullPath, fullPath,
icon: icon as string, icon: icon as string,
key: fullPath || path, key,
meta, meta,
name, name,
path, path,

View File

@@ -193,67 +193,107 @@ export function useElementPlusDesignTokens() {
'--el-border-radius-base': getCssVariableValue('--radius', false), '--el-border-radius-base': getCssVariableValue('--radius', false),
'--el-color-danger': getCssVariableValue('--destructive-500'), '--el-color-danger': getCssVariableValue('--destructive-500'),
'--el-color-danger-dark-2': getCssVariableValue('--destructive'), '--el-color-danger-dark-2': isDark.value
'--el-color-danger-light-3': getCssVariableValue('--destructive-400'), ? getCssVariableValue('--destructive-400')
'--el-color-danger-light-5': getCssVariableValue('--destructive-300'), : getCssVariableValue('--destructive-600'),
'--el-color-danger-light-7': getCssVariableValue('--destructive-200'), '--el-color-danger-light-3': isDark.value
? getCssVariableValue('--destructive-600')
: getCssVariableValue('--destructive-400'),
'--el-color-danger-light-5': isDark.value
? getCssVariableValue('--destructive-700')
: getCssVariableValue('--destructive-300'),
'--el-color-danger-light-7': isDark.value
? getCssVariableValue('--destructive-800')
: getCssVariableValue('--destructive-200'),
'--el-color-danger-light-8': isDark.value '--el-color-danger-light-8': isDark.value
? border ? getCssVariableValue('--destructive-900')
: getCssVariableValue('--destructive-100'), : getCssVariableValue('--destructive-100'),
'--el-color-danger-light-9': isDark.value '--el-color-danger-light-9': isDark.value
? accent ? getCssVariableValue('--destructive-950')
: getCssVariableValue('--destructive-50'), : getCssVariableValue('--destructive-50'),
'--el-color-error': getCssVariableValue('--destructive-500'), '--el-color-error': getCssVariableValue('--destructive-500'),
'--el-color-error-dark-2': getCssVariableValue('--destructive'), '--el-color-error-dark-2': isDark.value
'--el-color-error-light-3': getCssVariableValue('--destructive-400'), ? getCssVariableValue('--destructive-400')
'--el-color-error-light-5': getCssVariableValue('--destructive-300'), : getCssVariableValue('--destructive-600'),
'--el-color-error-light-7': getCssVariableValue('--destructive-200'), '--el-color-error-light-3': isDark.value
? getCssVariableValue('--destructive-600')
: getCssVariableValue('--destructive-400'),
'--el-color-error-light-5': isDark.value
? getCssVariableValue('--destructive-700')
: getCssVariableValue('--destructive-300'),
'--el-color-error-light-7': isDark.value
? getCssVariableValue('--destructive-800')
: getCssVariableValue('--destructive-200'),
'--el-color-error-light-8': isDark.value '--el-color-error-light-8': isDark.value
? border ? getCssVariableValue('--destructive-900')
: getCssVariableValue('--destructive-100'), : getCssVariableValue('--destructive-100'),
'--el-color-error-light-9': isDark.value '--el-color-error-light-9': isDark.value
? accent ? getCssVariableValue('--destructive-950')
: getCssVariableValue('--destructive-50'), : getCssVariableValue('--destructive-50'),
'--el-color-info-light-5': border,
'--el-color-info-light-8': border, '--el-color-info-light-8': border,
'--el-color-info-light-9': getCssVariableValue('--info'), // getCssVariableValue('--secondary'), '--el-color-info-light-9': getCssVariableValue('--info'), // getCssVariableValue('--secondary'),
'--el-color-primary': getCssVariableValue('--primary-500'), '--el-color-primary': getCssVariableValue('--primary-500'),
'--el-color-primary-dark-2': getCssVariableValue('--primary'), '--el-color-primary-dark-2': isDark.value
'--el-color-primary-light-3': getCssVariableValue('--primary-400'), ? getCssVariableValue('--primary-400')
'--el-color-primary-light-5': getCssVariableValue('--primary-300'), : getCssVariableValue('--primary-600'),
'--el-color-primary-light-3': isDark.value
? getCssVariableValue('--primary-600')
: getCssVariableValue('--primary-400'),
'--el-color-primary-light-5': isDark.value
? getCssVariableValue('--primary-700')
: getCssVariableValue('--primary-300'),
'--el-color-primary-light-7': isDark.value '--el-color-primary-light-7': isDark.value
? border ? getCssVariableValue('--primary-800')
: getCssVariableValue('--primary-200'), : getCssVariableValue('--primary-200'),
'--el-color-primary-light-8': isDark.value '--el-color-primary-light-8': isDark.value
? border ? getCssVariableValue('--primary-900')
: getCssVariableValue('--primary-100'), : getCssVariableValue('--primary-100'),
'--el-color-primary-light-9': isDark.value '--el-color-primary-light-9': isDark.value
? accent ? getCssVariableValue('--primary-950')
: getCssVariableValue('--primary-50'), : getCssVariableValue('--primary-50'),
'--el-color-success': getCssVariableValue('--success-500'), '--el-color-success': getCssVariableValue('--success-500'),
'--el-color-success-dark-2': getCssVariableValue('--success'), '--el-color-success-dark-2': isDark.value
'--el-color-success-light-3': getCssVariableValue('--success-400'), ? getCssVariableValue('--success-400')
'--el-color-success-light-5': getCssVariableValue('--success-300'), : getCssVariableValue('--success-600'),
'--el-color-success-light-7': getCssVariableValue('--success-200'), '--el-color-success-light-3': isDark.value
? getCssVariableValue('--success-600')
: getCssVariableValue('--success-400'),
'--el-color-success-light-5': isDark.value
? getCssVariableValue('--success-700')
: getCssVariableValue('--success-300'),
'--el-color-success-light-7': isDark.value
? getCssVariableValue('--success-800')
: getCssVariableValue('--success-200'),
'--el-color-success-light-8': isDark.value '--el-color-success-light-8': isDark.value
? border ? getCssVariableValue('--success-900')
: getCssVariableValue('--success-100'), : getCssVariableValue('--success-100'),
'--el-color-success-light-9': isDark.value '--el-color-success-light-9': isDark.value
? accent ? getCssVariableValue('--success-950')
: getCssVariableValue('--success-50'), : getCssVariableValue('--success-50'),
'--el-color-warning': getCssVariableValue('--warning-500'), '--el-color-warning': getCssVariableValue('--warning-500'),
'--el-color-warning-dark-2': getCssVariableValue('--warning'), '--el-color-warning-dark-2': isDark.value
'--el-color-warning-light-3': getCssVariableValue('--warning-400'), ? getCssVariableValue('--warning-400')
'--el-color-warning-light-5': getCssVariableValue('--warning-300'), : getCssVariableValue('--warning-600'),
'--el-color-warning-light-7': getCssVariableValue('--warning-200'), '--el-color-warning-light-3': isDark.value
? getCssVariableValue('--warning-600')
: getCssVariableValue('--warning-400'),
'--el-color-warning-light-5': isDark.value
? getCssVariableValue('--warning-700')
: getCssVariableValue('--warning-300'),
'--el-color-warning-light-7': isDark.value
? getCssVariableValue('--warning-800')
: getCssVariableValue('--warning-200'),
'--el-color-warning-light-8': isDark.value '--el-color-warning-light-8': isDark.value
? border ? getCssVariableValue('--warning-900')
: getCssVariableValue('--warning-100'), : getCssVariableValue('--warning-100'),
'--el-color-warning-light-9': isDark.value '--el-color-warning-light-9': isDark.value
? accent ? getCssVariableValue('--warning-950')
: getCssVariableValue('--warning-50'), : getCssVariableValue('--warning-50'),
'--el-fill-color': getCssVariableValue('--accent'), '--el-fill-color': getCssVariableValue('--accent'),

View File

@@ -1,3 +1,4 @@
import type { ComputedRef } from 'vue';
import type { RouteLocationNormalized } from 'vue-router'; import type { RouteLocationNormalized } from 'vue-router';
import { useTabbarStore } from '@vben/stores'; import { useTabbarStore } from '@vben/stores';
@@ -52,7 +53,24 @@ export function useTabs() {
await tabbarStore.closeTabByKey(key, router); await tabbarStore.closeTabByKey(key, router);
} }
async function setTabTitle(title: string) { /**
* 设置当前标签页的标题
*
* @description 支持设置静态标题字符串或动态计算标题
* @description 动态标题会在每次渲染时重新计算,适用于多语言或状态相关的标题
*
* @param title - 标题内容
* - 静态标题: 直接传入字符串
* - 动态标题: 传入 ComputedRef
*
* @example
* // 静态标题
* setTabTitle('标签页')
*
* // 动态标题(多语言)
* setTabTitle(computed(() => t('page.title')))
*/
async function setTabTitle(title: ComputedRef<string> | string) {
tabbarStore.setUpdateTime(); tabbarStore.setUpdateTime();
await tabbarStore.setTabTitle(route, title); await tabbarStore.setTabTitle(route, title);
} }

View File

@@ -38,7 +38,7 @@ const { authPanelCenter, authPanelLeft, authPanelRight, isDark } =
<template> <template>
<div <div
:class="[isDark]" :class="[isDark ? 'dark' : '']"
class="flex min-h-full flex-1 select-none overflow-x-hidden" class="flex min-h-full flex-1 select-none overflow-x-hidden"
> >
<template v-if="toolbar"> <template v-if="toolbar">

View File

@@ -1 +1,2 @@
export { default as AuthPageLayout } from './authentication.vue'; export { default as AuthPageLayout } from './authentication.vue';
export * from './types';

View File

@@ -9,7 +9,7 @@ import { computed } from 'vue';
import { RouterView } from 'vue-router'; import { RouterView } from 'vue-router';
import { preferences, usePreferences } from '@vben/preferences'; import { preferences, usePreferences } from '@vben/preferences';
import { storeToRefs, useTabbarStore } from '@vben/stores'; import { getTabKey, storeToRefs, useTabbarStore } from '@vben/stores';
import { IFrameRouterView } from '../../iframe'; import { IFrameRouterView } from '../../iframe';
@@ -115,13 +115,13 @@ function transformComponent(
:is="transformComponent(Component, route)" :is="transformComponent(Component, route)"
v-if="renderRouteView" v-if="renderRouteView"
v-show="!route.meta.iframeSrc" v-show="!route.meta.iframeSrc"
:key="route.fullPath" :key="getTabKey(route)"
/> />
</KeepAlive> </KeepAlive>
<component <component
:is="Component" :is="Component"
v-else-if="renderRouteView" v-else-if="renderRouteView"
:key="route.fullPath" :key="getTabKey(route)"
/> />
</Transition> </Transition>
<template v-else> <template v-else>
@@ -134,13 +134,13 @@ function transformComponent(
:is="transformComponent(Component, route)" :is="transformComponent(Component, route)"
v-if="renderRouteView" v-if="renderRouteView"
v-show="!route.meta.iframeSrc" v-show="!route.meta.iframeSrc"
:key="route.fullPath" :key="getTabKey(route)"
/> />
</KeepAlive> </KeepAlive>
<component <component
:is="Component" :is="Component"
v-else-if="renderRouteView" v-else-if="renderRouteView"
:key="route.fullPath" :key="getTabKey(route)"
/> />
</template> </template>
</RouterView> </RouterView>

View File

@@ -180,8 +180,16 @@ const headerSlots = computed(() => {
<VbenAdminLayout <VbenAdminLayout
v-model:sidebar-extra-visible="sidebarExtraVisible" v-model:sidebar-extra-visible="sidebarExtraVisible"
:content-compact="preferences.app.contentCompact" :content-compact="preferences.app.contentCompact"
:content-compact-width="preferences.app.contentCompactWidth"
:content-padding="preferences.app.contentPadding"
:content-padding-bottom="preferences.app.contentPaddingBottom"
:content-padding-left="preferences.app.contentPaddingLeft"
:content-padding-right="preferences.app.contentPaddingRight"
:content-padding-top="preferences.app.contentPaddingTop"
:footer-enable="preferences.footer.enable" :footer-enable="preferences.footer.enable"
:footer-fixed="preferences.footer.fixed" :footer-fixed="preferences.footer.fixed"
:footer-height="preferences.footer.height"
:header-height="preferences.header.height"
:header-hidden="preferences.header.hidden" :header-hidden="preferences.header.hidden"
:header-mode="preferences.header.mode" :header-mode="preferences.header.mode"
:header-theme="headerTheme" :header-theme="headerTheme"
@@ -196,11 +204,15 @@ const headerSlots = computed(() => {
:sidebar-fixed-button="preferences.sidebar.fixedButton" :sidebar-fixed-button="preferences.sidebar.fixedButton"
:sidebar-expand-on-hover="preferences.sidebar.expandOnHover" :sidebar-expand-on-hover="preferences.sidebar.expandOnHover"
:sidebar-extra-collapse="preferences.sidebar.extraCollapse" :sidebar-extra-collapse="preferences.sidebar.extraCollapse"
:sidebar-extra-collapsed-width="preferences.sidebar.extraCollapsedWidth"
:sidebar-hidden="preferences.sidebar.hidden" :sidebar-hidden="preferences.sidebar.hidden"
:sidebar-mixed-width="preferences.sidebar.mixedWidth"
:sidebar-theme="sidebarTheme" :sidebar-theme="sidebarTheme"
:sidebar-width="preferences.sidebar.width" :sidebar-width="preferences.sidebar.width"
:side-collapse-width="preferences.sidebar.collapseWidth"
:tabbar-enable="preferences.tabbar.enable" :tabbar-enable="preferences.tabbar.enable"
:tabbar-height="preferences.tabbar.height" :tabbar-height="preferences.tabbar.height"
:z-index="preferences.app.zIndex"
@side-mouse-leave="handleSideMouseLeave" @side-mouse-leave="handleSideMouseLeave"
@toggle-sidebar="toggleSidebar" @toggle-sidebar="toggleSidebar"
@update:sidebar-collapse=" @update:sidebar-collapse="

View File

@@ -140,7 +140,10 @@ function useMixedMenu() {
watch( watch(
() => route.path, () => route.path,
(path) => { (path) => {
const currentPath = (route?.meta?.activePath as string) ?? path; const currentPath = route?.meta?.activePath ?? route?.meta?.link ?? path;
if (willOpenedByWindow(currentPath)) {
return;
}
calcSideMenus(currentPath); calcSideMenus(currentPath);
if (rootMenuPath.value) if (rootMenuPath.value)
defaultSubMap.set(rootMenuPath.value, currentPath); defaultSubMap.set(rootMenuPath.value, currentPath);

View File

@@ -30,7 +30,7 @@ const {
} = useTabbar(); } = useTabbar();
const menus = computed(() => { const menus = computed(() => {
const tab = tabbarStore.getTabByPath(currentActive.value); const tab = tabbarStore.getTabByKey(currentActive.value);
const menus = createContextMenus(tab); const menus = createContextMenus(tab);
return menus.map((item) => { return menus.map((item) => {
return { return {

View File

@@ -22,7 +22,7 @@ import {
X, X,
} from '@vben/icons'; } from '@vben/icons';
import { $t, useI18n } from '@vben/locales'; import { $t, useI18n } from '@vben/locales';
import { useAccessStore, useTabbarStore } from '@vben/stores'; import { getTabKey, useAccessStore, useTabbarStore } from '@vben/stores';
import { filterTree } from '@vben/utils'; import { filterTree } from '@vben/utils';
export function useTabbar() { export function useTabbar() {
@@ -44,8 +44,11 @@ export function useTabbar() {
toggleTabPin, toggleTabPin,
} = useTabs(); } = useTabs();
/**
* 当前路径对应的tab的key
*/
const currentActive = computed(() => { const currentActive = computed(() => {
return route.fullPath; return getTabKey(route);
}); });
const { locale } = useI18n(); const { locale } = useI18n();
@@ -73,7 +76,8 @@ export function useTabbar() {
// 点击tab,跳转路由 // 点击tab,跳转路由
const handleClick = (key: string) => { const handleClick = (key: string) => {
router.push(key); const { fullPath, path } = tabbarStore.getTabByKey(key);
router.push(fullPath || path);
}; };
// 关闭tab // 关闭tab
@@ -100,7 +104,7 @@ export function useTabbar() {
); );
watch( watch(
() => route.path, () => route.fullPath,
() => { () => {
const meta = route.matched?.[route.matched.length - 1]?.meta; const meta = route.matched?.[route.matched.length - 1]?.meta;
tabbarStore.addTab({ tabbarStore.addTab({

View File

@@ -22,12 +22,13 @@ describe('useAccessStore', () => {
const tab: any = { const tab: any = {
fullPath: '/home', fullPath: '/home',
meta: {}, meta: {},
key: '/home',
name: 'Home', name: 'Home',
path: '/home', path: '/home',
}; };
store.addTab(tab); const addNewTab = store.addTab(tab);
expect(store.tabs.length).toBe(1); expect(store.tabs.length).toBe(1);
expect(store.tabs[0]).toEqual(tab); expect(store.tabs[0]).toEqual(addNewTab);
}); });
it('adds a new tab if it does not exist', () => { it('adds a new tab if it does not exist', () => {
@@ -38,20 +39,22 @@ describe('useAccessStore', () => {
name: 'New', name: 'New',
path: '/new', path: '/new',
}; };
store.addTab(newTab); const addNewTab = store.addTab(newTab);
expect(store.tabs).toContainEqual(newTab); expect(store.tabs).toContainEqual(addNewTab);
}); });
it('updates an existing tab instead of adding a new one', () => { it('updates an existing tab instead of adding a new one', () => {
const store = useTabbarStore(); const store = useTabbarStore();
const initialTab: any = { const initialTab: any = {
fullPath: '/existing', fullPath: '/existing',
meta: {}, meta: {
fullPathKey: false,
},
name: 'Existing', name: 'Existing',
path: '/existing', path: '/existing',
query: {}, query: {},
}; };
store.tabs.push(initialTab); store.addTab(initialTab);
const updatedTab = { ...initialTab, query: { id: '1' } }; const updatedTab = { ...initialTab, query: { id: '1' } };
store.addTab(updatedTab); store.addTab(updatedTab);
expect(store.tabs.length).toBe(1); expect(store.tabs.length).toBe(1);
@@ -60,9 +63,12 @@ describe('useAccessStore', () => {
it('closes all tabs', async () => { it('closes all tabs', async () => {
const store = useTabbarStore(); const store = useTabbarStore();
store.tabs = [ store.addTab({
{ fullPath: '/home', meta: {}, name: 'Home', path: '/home' }, fullPath: '/home',
] as any; meta: {},
name: 'Home',
path: '/home',
} as any);
router.replace = vi.fn(); router.replace = vi.fn();
await store.closeAllTabs(router); await store.closeAllTabs(router);
@@ -157,7 +163,7 @@ describe('useAccessStore', () => {
path: '/contact', path: '/contact',
} as any); } as any);
await store._bulkCloseByPaths(['/home', '/contact']); await store._bulkCloseByKeys(['/home', '/contact']);
expect(store.tabs).toHaveLength(1); expect(store.tabs).toHaveLength(1);
expect(store.tabs[0]?.name).toBe('About'); expect(store.tabs[0]?.name).toBe('About');
@@ -183,9 +189,8 @@ describe('useAccessStore', () => {
name: 'Contact', name: 'Contact',
path: '/contact', path: '/contact',
}; };
store.addTab(targetTab); const addTargetTab = store.addTab(targetTab);
await store.closeLeftTabs(addTargetTab);
await store.closeLeftTabs(targetTab);
expect(store.tabs).toHaveLength(1); expect(store.tabs).toHaveLength(1);
expect(store.tabs[0]?.name).toBe('Contact'); expect(store.tabs[0]?.name).toBe('Contact');
@@ -205,7 +210,7 @@ describe('useAccessStore', () => {
name: 'About', name: 'About',
path: '/about', path: '/about',
}; };
store.addTab(targetTab); const addTargetTab = store.addTab(targetTab);
store.addTab({ store.addTab({
fullPath: '/contact', fullPath: '/contact',
meta: {}, meta: {},
@@ -213,7 +218,7 @@ describe('useAccessStore', () => {
path: '/contact', path: '/contact',
} as any); } as any);
await store.closeOtherTabs(targetTab); await store.closeOtherTabs(addTargetTab);
expect(store.tabs).toHaveLength(1); expect(store.tabs).toHaveLength(1);
expect(store.tabs[0]?.name).toBe('About'); expect(store.tabs[0]?.name).toBe('About');
@@ -227,7 +232,7 @@ describe('useAccessStore', () => {
name: 'Home', name: 'Home',
path: '/home', path: '/home',
}; };
store.addTab(targetTab); const addTargetTab = store.addTab(targetTab);
store.addTab({ store.addTab({
fullPath: '/about', fullPath: '/about',
meta: {}, meta: {},
@@ -241,7 +246,7 @@ describe('useAccessStore', () => {
path: '/contact', path: '/contact',
} as any); } as any);
await store.closeRightTabs(targetTab); await store.closeRightTabs(addTargetTab);
expect(store.tabs).toHaveLength(1); expect(store.tabs).toHaveLength(1);
expect(store.tabs[0]?.name).toBe('Home'); expect(store.tabs[0]?.name).toBe('Home');

View File

@@ -1,4 +1,9 @@
import type { Router, RouteRecordNormalized } from 'vue-router'; import type { ComputedRef } from 'vue';
import type {
RouteLocationNormalized,
Router,
RouteRecordNormalized,
} from 'vue-router';
import type { TabDefinition } from '@vben-core/typings'; import type { TabDefinition } from '@vben-core/typings';
@@ -52,23 +57,23 @@ export const useTabbarStore = defineStore('core-tabbar', {
/** /**
* Close tabs in bulk * Close tabs in bulk
*/ */
async _bulkCloseByPaths(paths: string[]) { async _bulkCloseByKeys(keys: string[]) {
this.tabs = this.tabs.filter((item) => { const keySet = new Set(keys);
return !paths.includes(getTabPath(item)); this.tabs = this.tabs.filter(
}); (item) => !keySet.has(getTabKeyFromTab(item)),
);
this.updateCacheTabs(); await this.updateCacheTabs();
}, },
/** /**
* @zh_CN 关闭标签页 * @zh_CN 关闭标签页
* @param tab * @param tab
*/ */
_close(tab: TabDefinition) { _close(tab: TabDefinition) {
const { fullPath } = tab;
if (isAffixTab(tab)) { if (isAffixTab(tab)) {
return; return;
} }
const index = this.tabs.findIndex((item) => item.fullPath === fullPath); const index = this.tabs.findIndex((item) => equalTab(item, tab));
index !== -1 && this.tabs.splice(index, 1); index !== -1 && this.tabs.splice(index, 1);
}, },
/** /**
@@ -101,14 +106,17 @@ export const useTabbarStore = defineStore('core-tabbar', {
* @zh_CN 添加标签页 * @zh_CN 添加标签页
* @param routeTab * @param routeTab
*/ */
addTab(routeTab: TabDefinition) { addTab(routeTab: TabDefinition): TabDefinition {
const tab = cloneTab(routeTab); let tab = cloneTab(routeTab);
if (!tab.key) {
tab.key = getTabKey(routeTab);
}
if (!isTabShown(tab)) { if (!isTabShown(tab)) {
return; return tab;
} }
const tabIndex = this.tabs.findIndex((tab) => { const tabIndex = this.tabs.findIndex((item) => {
return getTabPath(tab) === getTabPath(routeTab); return equalTab(item, tab);
}); });
if (tabIndex === -1) { if (tabIndex === -1) {
@@ -154,10 +162,11 @@ export const useTabbarStore = defineStore('core-tabbar', {
mergedTab.meta.newTabTitle = curMeta.newTabTitle; mergedTab.meta.newTabTitle = curMeta.newTabTitle;
} }
} }
tab = mergedTab;
this.tabs.splice(tabIndex, 1, mergedTab); this.tabs.splice(tabIndex, 1, mergedTab);
} }
this.updateCacheTabs(); this.updateCacheTabs();
return tab;
}, },
/** /**
* @zh_CN 关闭所有标签页 * @zh_CN 关闭所有标签页
@@ -173,65 +182,63 @@ export const useTabbarStore = defineStore('core-tabbar', {
* @param tab * @param tab
*/ */
async closeLeftTabs(tab: TabDefinition) { async closeLeftTabs(tab: TabDefinition) {
const index = this.tabs.findIndex( const index = this.tabs.findIndex((item) => equalTab(item, tab));
(item) => getTabPath(item) === getTabPath(tab),
);
if (index < 1) { if (index < 1) {
return; return;
} }
const leftTabs = this.tabs.slice(0, index); const leftTabs = this.tabs.slice(0, index);
const paths: string[] = []; const keys: string[] = [];
for (const item of leftTabs) { for (const item of leftTabs) {
if (!isAffixTab(item)) { if (!isAffixTab(item)) {
paths.push(getTabPath(item)); keys.push(item.key as string);
} }
} }
await this._bulkCloseByPaths(paths); await this._bulkCloseByKeys(keys);
}, },
/** /**
* @zh_CN 关闭其他标签页 * @zh_CN 关闭其他标签页
* @param tab * @param tab
*/ */
async closeOtherTabs(tab: TabDefinition) { async closeOtherTabs(tab: TabDefinition) {
const closePaths = this.tabs.map((item) => getTabPath(item)); const closeKeys = this.tabs.map((item) => getTabKeyFromTab(item));
const paths: string[] = []; const keys: string[] = [];
for (const path of closePaths) { for (const key of closeKeys) {
if (path !== tab.fullPath) { if (key !== tab.key) {
const closeTab = this.tabs.find((item) => getTabPath(item) === path); const closeTab = this.tabs.find(
(item) => getTabKeyFromTab(item) === key,
);
if (!closeTab) { if (!closeTab) {
continue; continue;
} }
if (!isAffixTab(closeTab)) { if (!isAffixTab(closeTab)) {
paths.push(getTabPath(closeTab)); keys.push(closeTab.key as string);
} }
} }
} }
await this._bulkCloseByPaths(paths); await this._bulkCloseByKeys(keys);
}, },
/** /**
* @zh_CN 关闭右侧标签页 * @zh_CN 关闭右侧标签页
* @param tab * @param tab
*/ */
async closeRightTabs(tab: TabDefinition) { async closeRightTabs(tab: TabDefinition) {
const index = this.tabs.findIndex( const index = this.tabs.findIndex((item) => equalTab(item, tab));
(item) => getTabPath(item) === getTabPath(tab),
);
if (index !== -1 && index < this.tabs.length - 1) { if (index !== -1 && index < this.tabs.length - 1) {
const rightTabs = this.tabs.slice(index + 1); const rightTabs = this.tabs.slice(index + 1);
const paths: string[] = []; const keys: string[] = [];
for (const item of rightTabs) { for (const item of rightTabs) {
if (!isAffixTab(item)) { if (!isAffixTab(item)) {
paths.push(getTabPath(item)); keys.push(item.key as string);
} }
} }
await this._bulkCloseByPaths(paths); await this._bulkCloseByKeys(keys);
} }
}, },
@@ -242,15 +249,14 @@ export const useTabbarStore = defineStore('core-tabbar', {
*/ */
async closeTab(tab: TabDefinition, router: Router) { async closeTab(tab: TabDefinition, router: Router) {
const { currentRoute } = router; const { currentRoute } = router;
// 关闭不是激活选项卡 // 关闭不是激活选项卡
if (getTabPath(currentRoute.value) !== getTabPath(tab)) { if (getTabKey(currentRoute.value) !== getTabKeyFromTab(tab)) {
this._close(tab); this._close(tab);
this.updateCacheTabs(); this.updateCacheTabs();
return; return;
} }
const index = this.getTabs.findIndex( const index = this.getTabs.findIndex(
(item) => getTabPath(item) === getTabPath(currentRoute.value), (item) => getTabKeyFromTab(item) === getTabKey(currentRoute.value),
); );
const before = this.getTabs[index - 1]; const before = this.getTabs[index - 1];
@@ -277,7 +283,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
async closeTabByKey(key: string, router: Router) { async closeTabByKey(key: string, router: Router) {
const originKey = decodeURIComponent(key); const originKey = decodeURIComponent(key);
const index = this.tabs.findIndex( const index = this.tabs.findIndex(
(item) => getTabPath(item) === originKey, (item) => getTabKeyFromTab(item) === originKey,
); );
if (index === -1) { if (index === -1) {
return; return;
@@ -290,12 +296,12 @@ export const useTabbarStore = defineStore('core-tabbar', {
}, },
/** /**
* 根据路径获取标签页 * 根据tab的key获取tab
* @param path * @param key
*/ */
getTabByPath(path: string) { getTabByKey(key: string) {
return this.getTabs.find( return this.getTabs.find(
(item) => getTabPath(item) === path, (item) => getTabKeyFromTab(item) === key,
) as TabDefinition; ) as TabDefinition;
}, },
/** /**
@@ -311,22 +317,19 @@ export const useTabbarStore = defineStore('core-tabbar', {
* @param tab * @param tab
*/ */
async pinTab(tab: TabDefinition) { async pinTab(tab: TabDefinition) {
const index = this.tabs.findIndex( const index = this.tabs.findIndex((item) => equalTab(item, tab));
(item) => getTabPath(item) === getTabPath(tab), if (index === -1) {
); return;
if (index !== -1) {
const oldTab = this.tabs[index];
tab.meta.affixTab = true;
tab.meta.title = oldTab?.meta?.title as string;
// this.addTab(tab);
this.tabs.splice(index, 1, tab);
} }
const oldTab = this.tabs[index];
tab.meta.affixTab = true;
tab.meta.title = oldTab?.meta?.title as string;
// this.addTab(tab);
this.tabs.splice(index, 1, tab);
// 过滤固定tabs后面更改affixTabOrder的值的话可能会有问题目前行464排序affixTabs没有设置值 // 过滤固定tabs后面更改affixTabOrder的值的话可能会有问题目前行464排序affixTabs没有设置值
const affixTabs = this.tabs.filter((tab) => isAffixTab(tab)); const affixTabs = this.tabs.filter((tab) => isAffixTab(tab));
// 获得固定tabs的index // 获得固定tabs的index
const newIndex = affixTabs.findIndex( const newIndex = affixTabs.findIndex((item) => equalTab(item, tab));
(item) => getTabPath(item) === getTabPath(tab),
);
// 交换位置重新排序 // 交换位置重新排序
await this.sortTabs(index, newIndex); await this.sortTabs(index, newIndex);
}, },
@@ -371,9 +374,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
if (tab?.meta?.newTabTitle) { if (tab?.meta?.newTabTitle) {
return; return;
} }
const findTab = this.tabs.find( const findTab = this.tabs.find((item) => equalTab(item, tab));
(item) => getTabPath(item) === getTabPath(tab),
);
if (findTab) { if (findTab) {
findTab.meta.newTabTitle = undefined; findTab.meta.newTabTitle = undefined;
await this.updateCacheTabs(); await this.updateCacheTabs();
@@ -401,13 +402,24 @@ export const useTabbarStore = defineStore('core-tabbar', {
/** /**
* @zh_CN 设置标签页标题 * @zh_CN 设置标签页标题
* @param tab *
* @param title * @zh_CN 支持设置静态标题字符串或计算属性作为动态标题
* @zh_CN 当标题为计算属性时,标题会随计算属性值变化而自动更新
* @zh_CN 适用于需要根据状态或多语言动态更新标题的场景
*
* @param {TabDefinition} tab - 标签页对象
* @param {ComputedRef<string> | string} title - 标题内容,支持静态字符串或计算属性
*
* @example
* // 设置静态标题
* setTabTitle(tab, '新标签页');
*
* @example
* // 设置动态标题
* setTabTitle(tab, computed(() => t('common.dashboard')));
*/ */
async setTabTitle(tab: TabDefinition, title: string) { async setTabTitle(tab: TabDefinition, title: ComputedRef<string> | string) {
const findTab = this.tabs.find( const findTab = this.tabs.find((item) => equalTab(item, tab));
(item) => getTabPath(item) === getTabPath(tab),
);
if (findTab) { if (findTab) {
findTab.meta.newTabTitle = title; findTab.meta.newTabTitle = title;
@@ -448,17 +460,15 @@ export const useTabbarStore = defineStore('core-tabbar', {
* @param tab * @param tab
*/ */
async unpinTab(tab: TabDefinition) { async unpinTab(tab: TabDefinition) {
const index = this.tabs.findIndex( const index = this.tabs.findIndex((item) => equalTab(item, tab));
(item) => getTabPath(item) === getTabPath(tab), if (index === -1) {
); return;
if (index !== -1) {
const oldTab = this.tabs[index];
tab.meta.affixTab = false;
tab.meta.title = oldTab?.meta?.title as string;
// this.addTab(tab);
this.tabs.splice(index, 1, tab);
} }
const oldTab = this.tabs[index];
tab.meta.affixTab = false;
tab.meta.title = oldTab?.meta?.title as string;
// this.addTab(tab);
this.tabs.splice(index, 1, tab);
// 过滤固定tabs后面更改affixTabOrder的值的话可能会有问题目前行464排序affixTabs没有设置值 // 过滤固定tabs后面更改affixTabOrder的值的话可能会有问题目前行464排序affixTabs没有设置值
const affixTabs = this.tabs.filter((tab) => isAffixTab(tab)); const affixTabs = this.tabs.filter((tab) => isAffixTab(tab));
// 获得固定tabs的index,使用固定tabs的下一个位置也就是活动tabs的第一个位置 // 获得固定tabs的index,使用固定tabs的下一个位置也就是活动tabs的第一个位置
@@ -591,11 +601,49 @@ function isTabShown(tab: TabDefinition) {
} }
/** /**
* @zh_CN 获取标签页路径 * 从route获取tab页的key
* @param tab * @param tab
*/ */
function getTabPath(tab: RouteRecordNormalized | TabDefinition) { function getTabKey(tab: RouteLocationNormalized | RouteRecordNormalized) {
return decodeURIComponent((tab as TabDefinition).fullPath || tab.path); const {
fullPath,
path,
meta: { fullPathKey } = {},
query = {},
} = tab as RouteLocationNormalized;
// pageKey可能是数组查询参数重复时可能出现
const pageKey = Array.isArray(query.pageKey)
? query.pageKey[0]
: query.pageKey;
let rawKey;
if (pageKey) {
rawKey = pageKey;
} else {
rawKey = fullPathKey === false ? path : (fullPath ?? path);
}
try {
return decodeURIComponent(rawKey);
} catch {
return rawKey;
}
}
/**
* 从tab获取tab页的key
* 如果tab没有key,那么就从route获取key
* @param tab
*/
function getTabKeyFromTab(tab: TabDefinition): string {
return tab.key ?? getTabKey(tab);
}
/**
* 比较两个tab是否相等
* @param a
* @param b
*/
function equalTab(a: TabDefinition, b: TabDefinition) {
return getTabKeyFromTab(a) === getTabKeyFromTab(b);
} }
function routeToTab(route: RouteRecordNormalized) { function routeToTab(route: RouteRecordNormalized) {
@@ -603,5 +651,8 @@ function routeToTab(route: RouteRecordNormalized) {
meta: route.meta, meta: route.meta,
name: route.name, name: route.name,
path: route.path, path: route.path,
key: getTabKey(route),
} as TabDefinition; } as TabDefinition;
} }
export { getTabKey };

View File

@@ -19,7 +19,7 @@ const checkValue = ref(['a', 'b']);
const options = [ const options = [
{ label: '选项1', value: 'a' }, { label: '选项1', value: 'a' },
{ label: '选项2', value: 'b' }, { label: '选项2', value: 'b', num: 999 },
{ label: '选项3', value: 'c' }, { label: '选项3', value: 'c' },
{ label: '选项4', value: 'd' }, { label: '选项4', value: 'd' },
{ label: '选项5', value: 'e' }, { label: '选项5', value: 'e' },
@@ -168,10 +168,11 @@ function onBtnClick(value: any) {
:options="options" :options="options"
v-bind="compProps" v-bind="compProps"
> >
<template #option="{ label, value }"> <template #option="{ label, value, data }">
<div class="flex items-center"> <div class="flex items-center">
<span>{{ label }}</span> <span>{{ label }}</span>
<span class="ml-2 text-gray-400">{{ value }}</span> <span class="ml-2 text-gray-400">{{ value }}</span>
<span v-if="data.num" class="white ml-2">{{ data.num }}</span>
</div> </div>
</template> </template>
</VbenCheckButtonGroup> </VbenCheckButtonGroup>