admin-vben/packages/@core/preferences/src/use-preferences.ts

185 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-05-19 13:20:42 +00:00
import { computed } from 'vue';
2024-06-08 11:49:06 +00:00
import { diff } from '@vben-core/toolkit';
2024-06-01 15:15:29 +00:00
import { isDarkTheme, preferencesManager } from './preferences';
2024-05-19 13:20:42 +00:00
2024-06-01 15:15:29 +00:00
function usePreferences() {
const preferences = preferencesManager.getPreferences();
const initialPreferences = preferencesManager.getInitialPreferences();
2024-05-19 13:20:42 +00:00
/**
* @zh_CN
*/
const diffPreference = computed(() => {
2024-06-01 15:15:29 +00:00
return diff(initialPreferences, preferences);
2024-05-19 13:20:42 +00:00
});
2024-06-09 04:53:38 +00:00
const appPreferences = computed(() => preferences.app);
2024-06-16 05:43:33 +00:00
const shortcutKeysPreferences = computed(() => preferences.shortcutKeys);
2024-05-19 13:20:42 +00:00
/**
* @zh_CN
2024-06-01 15:15:29 +00:00
* @param preferences -
2024-05-19 13:20:42 +00:00
* @returns true false
*/
const isDark = computed(() => {
2024-06-23 11:17:31 +00:00
return isDarkTheme(preferences.theme.mode);
2024-05-19 13:20:42 +00:00
});
const isMobile = computed(() => {
return appPreferences.value.isMobile;
});
2024-05-19 13:20:42 +00:00
const theme = computed(() => {
return isDark.value ? 'dark' : 'light';
});
/**
* @zh_CN
*/
const layout = computed(() =>
isMobile.value ? 'sidebar-nav' : appPreferences.value.layout,
2024-05-19 13:20:42 +00:00
);
/**
* @zh_CN contenttab
*/
2024-06-01 15:15:29 +00:00
const isFullContent = computed(
2024-06-09 04:53:38 +00:00
() => appPreferences.value.layout === 'full-content',
2024-06-01 15:15:29 +00:00
);
2024-05-19 13:20:42 +00:00
/**
* @zh_CN
*/
2024-06-09 07:39:11 +00:00
const isSideNav = computed(
() => appPreferences.value.layout === 'sidebar-nav',
);
2024-05-19 13:20:42 +00:00
/**
* @zh_CN
*/
2024-06-01 15:15:29 +00:00
const isSideMixedNav = computed(
2024-06-09 07:39:11 +00:00
() => appPreferences.value.layout === 'sidebar-mixed-nav',
2024-06-01 15:15:29 +00:00
);
2024-05-19 13:20:42 +00:00
/**
* @zh_CN
*/
2024-06-01 15:15:29 +00:00
const isHeaderNav = computed(
2024-06-09 04:53:38 +00:00
() => appPreferences.value.layout === 'header-nav',
2024-06-01 15:15:29 +00:00
);
2024-05-19 13:20:42 +00:00
/**
* @zh_CN
*/
2024-06-09 04:53:38 +00:00
const isMixedNav = computed(
() => appPreferences.value.layout === 'mixed-nav',
);
2024-05-19 13:20:42 +00:00
/**
* @zh_CN
*/
const isSideMode = computed(() => {
return isMixedNav.value || isSideMixedNav.value || isSideNav.value;
});
const sidebarCollapsed = computed(() => {
return preferences.sidebar.collapsed;
});
2024-05-19 13:20:42 +00:00
/**
* @zh_CN keep-alive
* tabskeep-alive
*/
const keepAlive = computed(
2024-06-09 04:53:38 +00:00
() => preferences.tabbar.enable && preferences.tabbar.keepAlive,
2024-05-19 13:20:42 +00:00
);
/**
* @zh_CN
*/
const authPanelLeft = computed(() => {
2024-06-09 04:53:38 +00:00
return appPreferences.value.authPageLayout === 'panel-left';
2024-05-19 13:20:42 +00:00
});
/**
* @zh_CN
*/
const authPanelRight = computed(() => {
2024-06-09 04:53:38 +00:00
return appPreferences.value.authPageLayout === 'panel-right';
2024-05-19 13:20:42 +00:00
});
/**
* @zh_CN
*/
const authPanelCenter = computed(() => {
2024-06-09 04:53:38 +00:00
return appPreferences.value.authPageLayout === 'panel-center';
2024-05-19 13:20:42 +00:00
});
/**
* @zh_CN
* full-content
*/
const contentIsMaximize = computed(() => {
const headerIsHidden = preferences.header.hidden;
const sidebarIsHidden = preferences.sidebar.hidden;
return headerIsHidden && sidebarIsHidden && !isFullContent.value;
});
2024-06-16 05:43:33 +00:00
/**
* @zh_CN
*/
const globalSearchShortcutKey = computed(() => {
const { enable, globalSearch } = shortcutKeysPreferences.value;
return enable && globalSearch;
});
/**
* @zh_CN
*/
const globalLogoutShortcutKey = computed(() => {
const { enable, globalLogout } = shortcutKeysPreferences.value;
return enable && globalLogout;
});
const globalLockScreenShortcutKey = computed(() => {
const { enable, globalLockScreen } = shortcutKeysPreferences.value;
return enable && globalLockScreen;
});
2024-06-16 05:43:33 +00:00
/**
* @zh_CN
*/
const globalPreferencesShortcutKey = computed(() => {
const { enable, globalPreferences } = shortcutKeysPreferences.value;
return enable && globalPreferences;
});
2024-05-19 13:20:42 +00:00
return {
authPanelCenter,
authPanelLeft,
authPanelRight,
contentIsMaximize,
2024-05-19 13:20:42 +00:00
diffPreference,
globalLockScreenShortcutKey,
2024-06-16 05:43:33 +00:00
globalLogoutShortcutKey,
globalPreferencesShortcutKey,
globalSearchShortcutKey,
2024-05-19 13:20:42 +00:00
isDark,
isFullContent,
isHeaderNav,
isMixedNav,
isMobile,
2024-05-19 13:20:42 +00:00
isSideMixedNav,
isSideMode,
isSideNav,
keepAlive,
layout,
sidebarCollapsed,
2024-05-19 13:20:42 +00:00
theme,
};
}
2024-06-01 15:15:29 +00:00
export { usePreferences };