mirror of
https://gitee.com/dapppp/ruoyi-plus-vben5.git
synced 2026-04-03 16:23:22 +08:00
feat: 侧边栏宽度拖拽改为composable实现,同时修复tabbar.ts文件lint报错
This commit is contained in:
@@ -2,6 +2,7 @@ export * from './use-is-mobile';
|
|||||||
export * from './use-layout-style';
|
export * from './use-layout-style';
|
||||||
export * from './use-namespace';
|
export * from './use-namespace';
|
||||||
export * from './use-priority-value';
|
export * from './use-priority-value';
|
||||||
|
export * from './use-resizable';
|
||||||
export * from './use-scroll-lock';
|
export * from './use-scroll-lock';
|
||||||
export * from './use-simple-locale';
|
export * from './use-simple-locale';
|
||||||
export * from './use-sortable';
|
export * from './use-sortable';
|
||||||
|
|||||||
72
packages/@core/composables/src/use-resizable.ts
Normal file
72
packages/@core/composables/src/use-resizable.ts
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import { onUnmounted, ref } from 'vue';
|
||||||
|
|
||||||
|
interface ResizableOptions {
|
||||||
|
max?: number;
|
||||||
|
min?: number;
|
||||||
|
onChange?: (newWidth: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useResizable(options: ResizableOptions = {}) {
|
||||||
|
const { min = 0, max = 999, onChange } = options;
|
||||||
|
|
||||||
|
const isDragging = ref(false);
|
||||||
|
|
||||||
|
let cleanup: (() => void) | null = null;
|
||||||
|
|
||||||
|
let userSelect = '';
|
||||||
|
let cursor = '';
|
||||||
|
|
||||||
|
const startDrag = (e: MouseEvent, width: number) => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
|
isDragging.value = true;
|
||||||
|
const startX = e.clientX;
|
||||||
|
const startWidth = width;
|
||||||
|
|
||||||
|
userSelect = document.body.style.userSelect;
|
||||||
|
cursor = document.body.style.cursor;
|
||||||
|
|
||||||
|
document.body.style.userSelect = 'none';
|
||||||
|
document.body.style.cursor = 'col-resize';
|
||||||
|
|
||||||
|
const onMouseMove = (moveEvent: MouseEvent) => {
|
||||||
|
if (!isDragging.value) return;
|
||||||
|
|
||||||
|
const deltaX = moveEvent.clientX - startX;
|
||||||
|
let newWidth = startWidth + deltaX;
|
||||||
|
|
||||||
|
newWidth = Math.min(max, Math.max(min, newWidth));
|
||||||
|
|
||||||
|
onChange?.(newWidth);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onMouseUp = () => {
|
||||||
|
if (!isDragging.value) return;
|
||||||
|
cleanup?.();
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('mousemove', onMouseMove, { passive: true });
|
||||||
|
document.addEventListener('mouseup', onMouseUp);
|
||||||
|
|
||||||
|
cleanup = () => {
|
||||||
|
document.removeEventListener('mousemove', onMouseMove);
|
||||||
|
document.removeEventListener('mouseup', onMouseUp);
|
||||||
|
|
||||||
|
document.body.style.userSelect = userSelect;
|
||||||
|
document.body.style.cursor = cursor;
|
||||||
|
|
||||||
|
isDragging.value = false;
|
||||||
|
cleanup = null;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
cleanup?.();
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
isDragging,
|
||||||
|
startDrag,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import type { CSSProperties } from 'vue';
|
import type { CSSProperties } from 'vue';
|
||||||
|
|
||||||
import { computed, ref, shallowRef, useSlots, watchEffect } from 'vue';
|
import { computed, shallowRef, useSlots, watchEffect } from 'vue';
|
||||||
|
|
||||||
|
import { useResizable } from '@vben-core/composables';
|
||||||
import { VbenScrollbar } from '@vben-core/shadcn-ui';
|
import { VbenScrollbar } from '@vben-core/shadcn-ui';
|
||||||
|
|
||||||
import { useScrollLock } from '@vueuse/core';
|
import { useScrollLock } from '@vueuse/core';
|
||||||
@@ -255,31 +256,18 @@ function handleMouseleave() {
|
|||||||
extraVisible.value = false;
|
extraVisible.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isDragging = ref(false);
|
const { isDragging, startDrag } = useResizable({
|
||||||
|
min: 160,
|
||||||
function handleDragSidebar(e: MouseEvent) {
|
max: 320,
|
||||||
e.preventDefault();
|
onChange: (newWidth) => {
|
||||||
|
|
||||||
isDragging.value = true;
|
|
||||||
|
|
||||||
const startX = e.clientX;
|
|
||||||
const startWidth = props.width;
|
|
||||||
|
|
||||||
function onMouseMove(moveEvent: MouseEvent) {
|
|
||||||
const deltaX = moveEvent.clientX - startX;
|
|
||||||
const newWidth = Math.min(320, Math.max(160, startWidth + deltaX));
|
|
||||||
emit('update:width', newWidth);
|
emit('update:width', newWidth);
|
||||||
}
|
},
|
||||||
|
});
|
||||||
|
|
||||||
function onMouseUp() {
|
const handleDragSidebar = (e: MouseEvent) => {
|
||||||
isDragging.value = false;
|
const { width } = props;
|
||||||
document.removeEventListener('mousemove', onMouseMove);
|
startDrag(e, width);
|
||||||
document.removeEventListener('mouseup', onMouseUp);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener('mousemove', onMouseMove);
|
|
||||||
document.addEventListener('mouseup', onMouseUp);
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -484,10 +484,6 @@ function handleHeaderToggle() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const idMainContent = ELEMENT_ID_MAIN_CONTENT;
|
const idMainContent = ELEMENT_ID_MAIN_CONTENT;
|
||||||
|
|
||||||
function handleUpdateSidebarWidth(val: number) {
|
|
||||||
emit('update:sidebar-width', val);
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -515,7 +511,7 @@ function handleUpdateSidebarWidth(val: number) {
|
|||||||
:width="getSidebarWidth"
|
:width="getSidebarWidth"
|
||||||
:z-index="sidebarZIndex"
|
:z-index="sidebarZIndex"
|
||||||
@leave="() => emit('sideMouseLeave')"
|
@leave="() => emit('sideMouseLeave')"
|
||||||
@update:width="handleUpdateSidebarWidth"
|
@update:width="(val) => emit('update:sidebar-width', val)"
|
||||||
>
|
>
|
||||||
<template v-if="isSideMode && !isMixedNav" #logo>
|
<template v-if="isSideMode && !isMixedNav" #logo>
|
||||||
<slot name="logo"></slot>
|
<slot name="logo"></slot>
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import type { ComputedRef } from 'vue';
|
import type { ComputedRef } from 'vue';
|
||||||
import type { RouteLocationNormalized, Router, RouteRecordNormalized } from 'vue-router';
|
import type {
|
||||||
|
RouteLocationNormalized,
|
||||||
|
Router,
|
||||||
|
RouteRecordNormalized,
|
||||||
|
} from 'vue-router';
|
||||||
|
|
||||||
import type { TabDefinition } from '@vben-core/typings';
|
import type { TabDefinition } from '@vben-core/typings';
|
||||||
|
|
||||||
@@ -66,7 +70,9 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
|||||||
*/
|
*/
|
||||||
async _bulkCloseByKeys(keys: string[]) {
|
async _bulkCloseByKeys(keys: string[]) {
|
||||||
const keySet = new Set(keys);
|
const keySet = new Set(keys);
|
||||||
this.tabs = this.tabs.filter((item) => !keySet.has(getTabKeyFromTab(item)));
|
this.tabs = this.tabs.filter(
|
||||||
|
(item) => !keySet.has(getTabKeyFromTab(item)),
|
||||||
|
);
|
||||||
if (isVisitHistory()) {
|
if (isVisitHistory()) {
|
||||||
this.visitHistory.remove(...keys);
|
this.visitHistory.remove(...keys);
|
||||||
}
|
}
|
||||||
@@ -130,20 +136,25 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
|||||||
if (tabIndex === -1) {
|
if (tabIndex === -1) {
|
||||||
const maxCount = preferences.tabbar.maxCount;
|
const maxCount = preferences.tabbar.maxCount;
|
||||||
// 获取动态路由打开数,超过 0 即代表需要控制打开数
|
// 获取动态路由打开数,超过 0 即代表需要控制打开数
|
||||||
const maxNumOfOpenTab = (routeTab?.meta?.maxNumOfOpenTab ?? -1) as number;
|
const maxNumOfOpenTab = (routeTab?.meta?.maxNumOfOpenTab ??
|
||||||
|
-1) as number;
|
||||||
// 如果动态路由层级大于 0 了,那么就要限制该路由的打开数限制了
|
// 如果动态路由层级大于 0 了,那么就要限制该路由的打开数限制了
|
||||||
// 获取到已经打开的动态路由数, 判断是否大于某一个值
|
// 获取到已经打开的动态路由数, 判断是否大于某一个值
|
||||||
if (
|
if (
|
||||||
maxNumOfOpenTab > 0 &&
|
maxNumOfOpenTab > 0 &&
|
||||||
this.tabs.filter((tab) => tab.name === routeTab.name).length >= maxNumOfOpenTab
|
this.tabs.filter((tab) => tab.name === routeTab.name).length >=
|
||||||
|
maxNumOfOpenTab
|
||||||
) {
|
) {
|
||||||
// 关闭第一个
|
// 关闭第一个
|
||||||
const index = this.tabs.findIndex((item) => item.name === routeTab.name);
|
const index = this.tabs.findIndex(
|
||||||
|
(item) => item.name === routeTab.name,
|
||||||
|
);
|
||||||
index !== -1 && this.tabs.splice(index, 1);
|
index !== -1 && this.tabs.splice(index, 1);
|
||||||
} else if (maxCount > 0 && this.tabs.length >= maxCount) {
|
} else if (maxCount > 0 && this.tabs.length >= maxCount) {
|
||||||
// 关闭第一个
|
// 关闭第一个
|
||||||
const index = this.tabs.findIndex(
|
const index = this.tabs.findIndex(
|
||||||
(item) => !Reflect.has(item.meta, 'affixTab') || !item.meta.affixTab
|
(item) =>
|
||||||
|
!Reflect.has(item.meta, 'affixTab') || !item.meta.affixTab,
|
||||||
);
|
);
|
||||||
index !== -1 && this.tabs.splice(index, 1);
|
index !== -1 && this.tabs.splice(index, 1);
|
||||||
}
|
}
|
||||||
@@ -183,7 +194,9 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
|||||||
this.tabs = newTabs.length > 0 ? newTabs : [...this.tabs].splice(0, 1);
|
this.tabs = newTabs.length > 0 ? newTabs : [...this.tabs].splice(0, 1);
|
||||||
// 设置访问历史记录
|
// 设置访问历史记录
|
||||||
if (isVisitHistory()) {
|
if (isVisitHistory()) {
|
||||||
this.visitHistory.retain(this.tabs.map((item) => getTabKeyFromTab(item)));
|
this.visitHistory.retain(
|
||||||
|
this.tabs.map((item) => getTabKeyFromTab(item)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
await this._goToDefaultTab(router);
|
await this._goToDefaultTab(router);
|
||||||
this.updateCacheTabs();
|
this.updateCacheTabs();
|
||||||
@@ -220,7 +233,9 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
|||||||
|
|
||||||
for (const key of closeKeys) {
|
for (const key of closeKeys) {
|
||||||
if (key !== getTabKeyFromTab(tab)) {
|
if (key !== getTabKeyFromTab(tab)) {
|
||||||
const closeTab = this.tabs.find((item) => getTabKeyFromTab(item) === key);
|
const closeTab = this.tabs.find(
|
||||||
|
(item) => getTabKeyFromTab(item) === key,
|
||||||
|
);
|
||||||
if (!closeTab) {
|
if (!closeTab) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -290,12 +305,14 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await (previousTab ? this._goToTab(previousTab, router) : this._goToDefaultTab(router));
|
await (previousTab
|
||||||
|
? this._goToTab(previousTab, router)
|
||||||
|
: this._goToDefaultTab(router));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 未开启访问历史记录,直接跳转下一个或上一个tab
|
// 未开启访问历史记录,直接跳转下一个或上一个tab
|
||||||
const index = this.getTabs.findIndex(
|
const index = this.getTabs.findIndex(
|
||||||
(item) => getTabKeyFromTab(item) === getTabKey(currentRoute.value)
|
(item) => getTabKeyFromTab(item) === getTabKey(currentRoute.value),
|
||||||
);
|
);
|
||||||
|
|
||||||
const before = this.getTabs[index - 1];
|
const before = this.getTabs[index - 1];
|
||||||
@@ -319,7 +336,9 @@ 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((item) => getTabKeyFromTab(item) === originKey);
|
const index = this.tabs.findIndex(
|
||||||
|
(item) => getTabKeyFromTab(item) === originKey,
|
||||||
|
);
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -335,7 +354,9 @@ export const useTabbarStore = defineStore('core-tabbar', {
|
|||||||
* @param key
|
* @param key
|
||||||
*/
|
*/
|
||||||
getTabByKey(key: string) {
|
getTabByKey(key: string) {
|
||||||
return this.getTabs.find((item) => getTabKeyFromTab(item) === key) as TabDefinition;
|
return this.getTabs.find(
|
||||||
|
(item) => getTabKeyFromTab(item) === key,
|
||||||
|
) as TabDefinition;
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* @zh_CN 新窗口打开标签页
|
* @zh_CN 新窗口打开标签页
|
||||||
@@ -656,14 +677,21 @@ function isTabShown(tab: TabDefinition) {
|
|||||||
* @param tab
|
* @param tab
|
||||||
*/
|
*/
|
||||||
function getTabKey(tab: RouteLocationNormalized | RouteRecordNormalized) {
|
function getTabKey(tab: RouteLocationNormalized | RouteRecordNormalized) {
|
||||||
const { fullPath, path, meta: { fullPathKey } = {}, query = {} } = tab as RouteLocationNormalized;
|
const {
|
||||||
|
fullPath,
|
||||||
|
path,
|
||||||
|
meta: { fullPathKey } = {},
|
||||||
|
query = {},
|
||||||
|
} = tab as RouteLocationNormalized;
|
||||||
// pageKey可能是数组(查询参数重复时可能出现)
|
// pageKey可能是数组(查询参数重复时可能出现)
|
||||||
const pageKey = Array.isArray(query.pageKey) ? query.pageKey[0] : query.pageKey;
|
const pageKey = Array.isArray(query.pageKey)
|
||||||
|
? query.pageKey[0]
|
||||||
|
: query.pageKey;
|
||||||
let rawKey;
|
let rawKey;
|
||||||
if (pageKey) {
|
if (pageKey) {
|
||||||
rawKey = pageKey;
|
rawKey = pageKey;
|
||||||
} else {
|
} else {
|
||||||
rawKey = fullPathKey === false ? path : fullPath ?? path;
|
rawKey = fullPathKey === false ? path : (fullPath ?? path);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return decodeURIComponent(rawKey);
|
return decodeURIComponent(rawKey);
|
||||||
|
|||||||
Reference in New Issue
Block a user