preferences.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import type { DeepPartial } from '@vben-core/typings';
  2. import type {
  3. CustomPreferencesField,
  4. CustomPreferencesRecord,
  5. InitialOptions,
  6. Preferences,
  7. PreferencesExtension,
  8. } from './types';
  9. import { markRaw, reactive, readonly, watch } from 'vue';
  10. import { StorageManager } from '@vben-core/shared/cache';
  11. import { isMacOs, merge } from '@vben-core/shared/utils';
  12. import {
  13. breakpointsTailwind,
  14. useBreakpoints,
  15. useDebounceFn,
  16. } from '@vueuse/core';
  17. import { defaultPreferences } from './config';
  18. import { updateCSSVariables } from './update-css-variables';
  19. const STORAGE_KEYS = {
  20. CUSTOM: 'preferences-custom',
  21. MAIN: 'preferences',
  22. LOCALE: 'preferences-locale',
  23. THEME: 'preferences-theme',
  24. } as const;
  25. class PreferenceManager {
  26. private cache: StorageManager;
  27. private customPreferencesExtension: null | PreferencesExtension<any> = null;
  28. private customState = reactive<CustomPreferencesRecord>({});
  29. private debouncedSave: () => void;
  30. private initialCustomPreferences: CustomPreferencesRecord = {};
  31. private initialPreferences: Preferences = defaultPreferences;
  32. private isInitialized = false;
  33. private state: Preferences;
  34. constructor() {
  35. this.cache = new StorageManager();
  36. // 构造函数不再同步读取缓存,使用默认值初始化
  37. // 真正的缓存加载在 initPreferences 中完成(已经是 async)
  38. this.state = reactive<Preferences>({...defaultPreferences});
  39. this.debouncedSave = useDebounceFn(() => this.saveToCache(), 150);
  40. }
  41. /**
  42. * 清除所有缓存的偏好设置
  43. */
  44. clearCache = async () => {
  45. await Promise.all(
  46. Object.values(STORAGE_KEYS).map((key) => this.cache.removeItem(key)),
  47. );
  48. };
  49. /**
  50. * 获取扩展偏好设置
  51. */
  52. getCustomPreferences = <
  53. TCustomPreferences extends object = CustomPreferencesRecord,
  54. >() => {
  55. return readonly(this.customState) as Readonly<TCustomPreferences>;
  56. };
  57. /**
  58. * 获取初始化扩展偏好设置
  59. */
  60. getInitialCustomPreferences = <
  61. TCustomPreferences extends object = CustomPreferencesRecord,
  62. >() => {
  63. return this.cloneValue(
  64. this.initialCustomPreferences,
  65. ) as Readonly<TCustomPreferences>;
  66. };
  67. /**
  68. * 获取初始化偏好设置
  69. */
  70. getInitialPreferences = () => {
  71. return this.initialPreferences;
  72. };
  73. /**
  74. * 获取当前偏好设置(只读)
  75. */
  76. getPreferences = () => {
  77. return readonly(this.state);
  78. };
  79. /**
  80. * 获取扩展偏好设置配置
  81. */
  82. getPreferencesExtension = <
  83. TCustomPreferences extends object = CustomPreferencesRecord,
  84. >() => {
  85. return this.customPreferencesExtension
  86. ? (this.cloneValue(this.customPreferencesExtension) as Readonly<
  87. PreferencesExtension<TCustomPreferences>
  88. >)
  89. : null;
  90. };
  91. /**
  92. * 初始化偏好设置
  93. * @param options - 初始化配置项
  94. * @param options.namespace - 命名空间,用于隔离不同应用的配置
  95. * @param options.overrides - 要覆盖的偏好设置
  96. */
  97. initPreferences = async <
  98. TCustomPreferences extends object = CustomPreferencesRecord,
  99. >({
  100. namespace,
  101. overrides,
  102. extension,
  103. }: InitialOptions<TCustomPreferences>) => {
  104. // 防止重复初始化
  105. if (this.isInitialized) {
  106. return;
  107. }
  108. // 使用命名空间初始化存储管理器
  109. this.cache = new StorageManager({ prefix: namespace });
  110. // 合并初始偏好设置
  111. this.initialPreferences = merge({}, overrides, defaultPreferences);
  112. this.customPreferencesExtension = extension ?? null;
  113. this.initialCustomPreferences = this.resolveCustomPreferencesDefaults(
  114. this.customPreferencesExtension,
  115. );
  116. // 加载缓存的偏好设置并与初始配置合并
  117. const cachedPreferences = (await this.loadFromCache()) || {};
  118. const mergedPreference = merge(
  119. {},
  120. cachedPreferences,
  121. this.initialPreferences,
  122. );
  123. // 更新偏好设置
  124. this.updatePreferences(mergedPreference);
  125. const cachedCustom = (await this.loadCustomFromCache()) || {};
  126. this.replaceCustomPreferences(
  127. merge(
  128. {},
  129. this.sanitizeCustomPreferences(cachedCustom),
  130. this.initialCustomPreferences,
  131. ),
  132. );
  133. await this.saveToCache();
  134. // 设置监听器
  135. this.setupWatcher();
  136. // 初始化平台标识
  137. this.initPlatform();
  138. this.isInitialized = true;
  139. };
  140. /**
  141. * 重置偏好设置到初始状态
  142. */
  143. resetPreferences = async () => {
  144. // 将状态重置为初始偏好设置
  145. Object.assign(this.state, this.initialPreferences);
  146. this.replaceCustomPreferences(this.initialCustomPreferences);
  147. // 保存偏好设置至缓存
  148. await this.saveToCache();
  149. // 直接触发 UI 更新
  150. this.handleUpdates(this.state);
  151. };
  152. /**
  153. * 更新扩展偏好设置
  154. * @param updates - 要更新的扩展偏好设置
  155. */
  156. updateCustomPreferences = <
  157. TCustomPreferences extends object = CustomPreferencesRecord,
  158. >(
  159. updates: DeepPartial<TCustomPreferences>,
  160. ) => {
  161. if (!this.customPreferencesExtension) {
  162. return;
  163. }
  164. const sanitizedUpdates = this.sanitizeCustomPreferences(
  165. updates as DeepPartial<CustomPreferencesRecord>,
  166. );
  167. if (Object.keys(sanitizedUpdates).length === 0) {
  168. return;
  169. }
  170. this.replaceCustomPreferences(
  171. merge({}, sanitizedUpdates, markRaw(this.customState)),
  172. );
  173. this.debouncedSave();
  174. };
  175. /**
  176. * 更新偏好设置
  177. * @param updates - 要更新的偏好设置
  178. */
  179. updatePreferences = (updates: DeepPartial<Preferences>) => {
  180. // 深度合并更新内容和当前状态
  181. const mergedState = merge({}, updates, markRaw(this.state));
  182. Object.assign(this.state, mergedState);
  183. // 根据更新的值执行更新
  184. this.handleUpdates(updates);
  185. // 保存到缓存(fire-and-forget,通过 debounce 控制频率)
  186. this.debouncedSave();
  187. };
  188. private cloneValue<T>(value: T): T {
  189. if (Array.isArray(value)) {
  190. return value.map((item) => this.cloneValue(item)) as T;
  191. }
  192. if (value && typeof value === 'object') {
  193. return Object.fromEntries(
  194. Object.entries(value as Record<string, unknown>).map(
  195. ([key, nestedValue]) => [key, this.cloneValue(nestedValue)],
  196. ),
  197. ) as T;
  198. }
  199. return value;
  200. }
  201. /**
  202. * 处理更新
  203. * @param updates - 更新的偏好设置
  204. */
  205. private handleUpdates(updates: DeepPartial<Preferences>) {
  206. const { theme, app } = updates;
  207. if (
  208. theme &&
  209. (Object.keys(theme).length > 0 || Reflect.has(theme, 'fontSize'))
  210. ) {
  211. updateCSSVariables(this.state);
  212. }
  213. if (
  214. app &&
  215. (Reflect.has(app, 'colorGrayMode') || Reflect.has(app, 'colorWeakMode'))
  216. ) {
  217. this.updateColorMode(this.state);
  218. }
  219. }
  220. /**
  221. * 初始化平台标识
  222. */
  223. private initPlatform() {
  224. document.documentElement.dataset.platform = isMacOs() ? 'macOs' : 'window';
  225. }
  226. private isAlmostInteger(value: number, epsilon = Number.EPSILON * 10) {
  227. return Math.abs(value - Math.round(value)) < epsilon;
  228. }
  229. private isValidCustomPreferenceValue(
  230. field: CustomPreferencesField,
  231. value: unknown,
  232. ) {
  233. switch (field.component) {
  234. case 'number': {
  235. if (typeof value !== 'number' || !Number.isFinite(value)) {
  236. return false;
  237. }
  238. const max = this.resolveNumericConstraint(field.componentProps?.max);
  239. const min = this.resolveNumericConstraint(field.componentProps?.min);
  240. const step = this.resolveNumericConstraint(field.componentProps?.step);
  241. if (min !== undefined && value < min) {
  242. return false;
  243. }
  244. if (max !== undefined && value > max) {
  245. return false;
  246. }
  247. if (step !== undefined) {
  248. if (step <= 0) {
  249. return false;
  250. }
  251. const stepBase = min ?? 0;
  252. const stepCount = (value - stepBase) / step;
  253. if (!this.isAlmostInteger(stepCount)) {
  254. return false;
  255. }
  256. }
  257. return true;
  258. }
  259. case 'select': {
  260. return (
  261. typeof value === 'string' &&
  262. field.options.some((option) => option.value === value)
  263. );
  264. }
  265. case 'switch': {
  266. return typeof value === 'boolean';
  267. }
  268. default: {
  269. return typeof value === 'string';
  270. }
  271. }
  272. }
  273. /**
  274. * 从缓存加载扩展偏好设置
  275. * @returns 缓存的扩展偏好设置,如果不存在则返回 null
  276. */
  277. private async loadCustomFromCache(): Promise<CustomPreferencesRecord | null> {
  278. return this.cache.getItem<CustomPreferencesRecord>(STORAGE_KEYS.CUSTOM);
  279. }
  280. /**
  281. * 从缓存加载偏好设置
  282. * @returns 缓存的偏好设置,如果不存在则返回 null
  283. */
  284. private async loadFromCache(): Promise<null | Preferences> {
  285. return this.cache.getItem<Preferences>(STORAGE_KEYS.MAIN);
  286. }
  287. private replaceCustomPreferences(preferences: CustomPreferencesRecord) {
  288. Object.keys(this.customState).forEach((key) => {
  289. Reflect.deleteProperty(this.customState, key);
  290. });
  291. Object.assign(this.customState, preferences);
  292. }
  293. private resolveCustomPreferencesDefaults(
  294. extension: null | PreferencesExtension<any>,
  295. ) {
  296. if (!extension) {
  297. return {};
  298. }
  299. const result: CustomPreferencesRecord = {};
  300. for (const field of extension.fields) {
  301. result[field.key] = field.defaultValue;
  302. }
  303. return result;
  304. }
  305. private resolveNumericConstraint(value: unknown) {
  306. return typeof value === 'number' && Number.isFinite(value)
  307. ? value
  308. : undefined;
  309. }
  310. private sanitizeCustomPreferences(
  311. updates: DeepPartial<CustomPreferencesRecord>,
  312. ) {
  313. if (!this.customPreferencesExtension) {
  314. return {};
  315. }
  316. const result: CustomPreferencesRecord = {};
  317. for (const field of this.customPreferencesExtension.fields) {
  318. const value = updates[field.key];
  319. if (
  320. value !== undefined &&
  321. this.isValidCustomPreferenceValue(field, value)
  322. ) {
  323. result[field.key] = value;
  324. }
  325. }
  326. return result;
  327. }
  328. /**
  329. * 保存偏好设置到缓存
  330. */
  331. private async saveToCache() {
  332. try {
  333. await this.cache.setItem(STORAGE_KEYS.MAIN, this.state);
  334. await this.cache.setItem(STORAGE_KEYS.LOCALE, this.state.app.locale);
  335. await this.cache.setItem(STORAGE_KEYS.THEME, this.state.theme.mode);
  336. if (this.customPreferencesExtension) {
  337. await this.cache.setItem(STORAGE_KEYS.CUSTOM, {
  338. ...this.customState,
  339. });
  340. return;
  341. }
  342. await this.cache.removeItem(STORAGE_KEYS.CUSTOM);
  343. } catch (error) {
  344. console.error('Failed to save preferences to cache:', error);
  345. }
  346. }
  347. /**
  348. * 监听状态和系统偏好设置的变化
  349. */
  350. private setupWatcher() {
  351. if (this.isInitialized) {
  352. return;
  353. }
  354. // 监听断点,判断是否移动端
  355. const breakpoints = useBreakpoints(breakpointsTailwind);
  356. const isMobile = breakpoints.smaller('md');
  357. watch(
  358. () => isMobile.value,
  359. (val) => {
  360. this.updatePreferences({
  361. app: { isMobile: val },
  362. });
  363. },
  364. { immediate: true },
  365. );
  366. // 监听系统主题偏好设置变化
  367. window
  368. .matchMedia('(prefers-color-scheme: dark)')
  369. .addEventListener('change', ({ matches: isDark }) => {
  370. // 仅在自动模式下跟随系统主题
  371. if (this.state.theme.mode === 'auto') {
  372. // 先应用实际的主题
  373. this.updatePreferences({
  374. theme: { mode: isDark ? 'dark' : 'light' },
  375. });
  376. // 再恢复为 auto 模式,保持跟随系统的状态
  377. this.updatePreferences({
  378. theme: { mode: 'auto' },
  379. });
  380. }
  381. });
  382. }
  383. /**
  384. * 更新页面颜色模式(灰色、色弱)
  385. * @param preference - 偏好设置
  386. */
  387. private updateColorMode(preference: Preferences) {
  388. const { colorGrayMode, colorWeakMode } = preference.app;
  389. const dom = document.documentElement;
  390. dom.classList.toggle('invert-mode', colorWeakMode);
  391. dom.classList.toggle('grayscale-mode', colorGrayMode);
  392. }
  393. }
  394. const preferencesManager = new PreferenceManager();
  395. export { PreferenceManager, preferencesManager };