use-preferences.ts 4.6 KB

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