use-preferences.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { computed } from 'vue';
  2. import { diff } from '@vben-core/shared';
  3. import { isDarkTheme, preferencesManager } from './preferences';
  4. function usePreferences() {
  5. const preferences = preferencesManager.getPreferences();
  6. const initialPreferences = preferencesManager.getInitialPreferences();
  7. /**
  8. * @zh_CN 计算偏好设置的变化
  9. */
  10. const diffPreference = computed(() => {
  11. return diff(initialPreferences, preferences);
  12. });
  13. const appPreferences = computed(() => preferences.app);
  14. const shortcutKeysPreferences = computed(() => preferences.shortcutKeys);
  15. /**
  16. * @zh_CN 判断是否为暗黑模式
  17. * @param preferences - 当前偏好设置对象,它的主题值将被用来判断是否为暗黑模式。
  18. * @returns 如果主题为暗黑模式,返回 true,否则返回 false。
  19. */
  20. const isDark = computed(() => {
  21. return isDarkTheme(preferences.theme.mode);
  22. });
  23. const isMobile = computed(() => {
  24. return appPreferences.value.isMobile;
  25. });
  26. const theme = computed(() => {
  27. return isDark.value ? 'dark' : 'light';
  28. });
  29. /**
  30. * @zh_CN 布局方式
  31. */
  32. const layout = computed(() =>
  33. isMobile.value ? 'sidebar-nav' : appPreferences.value.layout,
  34. );
  35. /**
  36. * @zh_CN 是否全屏显示content,不需要侧边、底部、顶部、tab区域
  37. */
  38. const isFullContent = computed(
  39. () => appPreferences.value.layout === 'full-content',
  40. );
  41. /**
  42. * @zh_CN 是否侧边导航模式
  43. */
  44. const isSideNav = computed(
  45. () => appPreferences.value.layout === 'sidebar-nav',
  46. );
  47. /**
  48. * @zh_CN 是否侧边混合模式
  49. */
  50. const isSideMixedNav = computed(
  51. () => appPreferences.value.layout === 'sidebar-mixed-nav',
  52. );
  53. /**
  54. * @zh_CN 是否为头部导航模式
  55. */
  56. const isHeaderNav = computed(
  57. () => appPreferences.value.layout === 'header-nav',
  58. );
  59. /**
  60. * @zh_CN 是否为混合导航模式
  61. */
  62. const isMixedNav = computed(
  63. () => appPreferences.value.layout === 'mixed-nav',
  64. );
  65. /**
  66. * @zh_CN 是否包含侧边导航模式
  67. */
  68. const isSideMode = computed(() => {
  69. return isMixedNav.value || isSideMixedNav.value || isSideNav.value;
  70. });
  71. const sidebarCollapsed = computed(() => {
  72. return preferences.sidebar.collapsed;
  73. });
  74. /**
  75. * @zh_CN 是否开启keep-alive
  76. * 在tabs可见以及开启keep-alive的情况下才开启
  77. */
  78. const keepAlive = computed(
  79. () => preferences.tabbar.enable && preferences.tabbar.keepAlive,
  80. );
  81. /**
  82. * @zh_CN 登录注册页面布局是否为左侧
  83. */
  84. const authPanelLeft = computed(() => {
  85. return appPreferences.value.authPageLayout === 'panel-left';
  86. });
  87. /**
  88. * @zh_CN 登录注册页面布局是否为左侧
  89. */
  90. const authPanelRight = computed(() => {
  91. return appPreferences.value.authPageLayout === 'panel-right';
  92. });
  93. /**
  94. * @zh_CN 登录注册页面布局是否为中间
  95. */
  96. const authPanelCenter = computed(() => {
  97. return appPreferences.value.authPageLayout === 'panel-center';
  98. });
  99. /**
  100. * @zh_CN 内容是否已经最大化
  101. * 排除 full-content模式
  102. */
  103. const contentIsMaximize = computed(() => {
  104. const headerIsHidden = preferences.header.hidden;
  105. const sidebarIsHidden = preferences.sidebar.hidden;
  106. return headerIsHidden && sidebarIsHidden && !isFullContent.value;
  107. });
  108. /**
  109. * @zh_CN 是否启用全局搜索快捷键
  110. */
  111. const globalSearchShortcutKey = computed(() => {
  112. const { enable, globalSearch } = shortcutKeysPreferences.value;
  113. return enable && globalSearch;
  114. });
  115. /**
  116. * @zh_CN 是否启用全局注销快捷键
  117. */
  118. const globalLogoutShortcutKey = computed(() => {
  119. const { enable, globalLogout } = shortcutKeysPreferences.value;
  120. return enable && globalLogout;
  121. });
  122. const globalLockScreenShortcutKey = computed(() => {
  123. const { enable, globalLockScreen } = shortcutKeysPreferences.value;
  124. return enable && globalLockScreen;
  125. });
  126. /**
  127. * @zh_CN 是否启用全局偏好设置快捷键
  128. */
  129. const globalPreferencesShortcutKey = computed(() => {
  130. const { enable, globalPreferences } = shortcutKeysPreferences.value;
  131. return enable && globalPreferences;
  132. });
  133. return {
  134. authPanelCenter,
  135. authPanelLeft,
  136. authPanelRight,
  137. contentIsMaximize,
  138. diffPreference,
  139. globalLockScreenShortcutKey,
  140. globalLogoutShortcutKey,
  141. globalPreferencesShortcutKey,
  142. globalSearchShortcutKey,
  143. isDark,
  144. isFullContent,
  145. isHeaderNav,
  146. isMixedNav,
  147. isMobile,
  148. isSideMixedNav,
  149. isSideMode,
  150. isSideNav,
  151. keepAlive,
  152. layout,
  153. sidebarCollapsed,
  154. theme,
  155. };
  156. }
  157. export { usePreferences };