preferences.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. this.initialPreferences, // 初始化配置优先,缓存仅补齐缺失字段
  121. cachedPreferences,
  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 = (updates: DeepPartial<object>) => {
  157. if (!this.customPreferencesExtension) {
  158. return;
  159. }
  160. const sanitizedUpdates = this.sanitizeCustomPreferences(
  161. updates as DeepPartial<CustomPreferencesRecord>,
  162. );
  163. if (Object.keys(sanitizedUpdates).length === 0) {
  164. return;
  165. }
  166. this.replaceCustomPreferences(
  167. merge({}, sanitizedUpdates, markRaw(this.customState)),
  168. );
  169. this.debouncedSave();
  170. };
  171. /**
  172. * 更新偏好设置
  173. * @param updates - 要更新的偏好设置
  174. */
  175. updatePreferences = (updates: DeepPartial<Preferences>) => {
  176. // 深度合并更新内容和当前状态
  177. const mergedState = merge({}, updates, markRaw(this.state));
  178. Object.assign(this.state, mergedState);
  179. // 根据更新的值执行更新
  180. this.handleUpdates(updates);
  181. // 保存到缓存(fire-and-forget,通过 debounce 控制频率)
  182. this.debouncedSave();
  183. };
  184. private cloneValue<T>(value: T): T {
  185. if (Array.isArray(value)) {
  186. return value.map((item) => this.cloneValue(item)) as T;
  187. }
  188. if (value && typeof value === 'object') {
  189. return Object.fromEntries(
  190. Object.entries(value as Record<string, unknown>).map(
  191. ([key, nestedValue]) => [key, this.cloneValue(nestedValue)],
  192. ),
  193. ) as T;
  194. }
  195. return value;
  196. }
  197. /**
  198. * 处理更新
  199. * @param updates - 更新的偏好设置
  200. */
  201. private handleUpdates(updates: DeepPartial<Preferences>) {
  202. const { theme, app } = updates;
  203. if (
  204. theme &&
  205. (Object.keys(theme).length > 0 || Reflect.has(theme, 'fontSize'))
  206. ) {
  207. updateCSSVariables(this.state);
  208. }
  209. if (
  210. app &&
  211. (Reflect.has(app, 'colorGrayMode') || Reflect.has(app, 'colorWeakMode'))
  212. ) {
  213. this.updateColorMode(this.state);
  214. }
  215. }
  216. /**
  217. * 初始化平台标识
  218. */
  219. private initPlatform() {
  220. document.documentElement.dataset.platform = isMacOs() ? 'macOs' : 'window';
  221. }
  222. private isAlmostInteger(value: number, epsilon = Number.EPSILON * 10) {
  223. return Math.abs(value - Math.round(value)) < epsilon;
  224. }
  225. private isValidCustomPreferenceValue(
  226. field: CustomPreferencesField,
  227. value: unknown,
  228. ) {
  229. switch (field.component) {
  230. case 'number': {
  231. if (typeof value !== 'number' || !Number.isFinite(value)) {
  232. return false;
  233. }
  234. const max = this.resolveNumericConstraint(field.componentProps?.max);
  235. const min = this.resolveNumericConstraint(field.componentProps?.min);
  236. const step = this.resolveNumericConstraint(field.componentProps?.step);
  237. if (min !== undefined && value < min) {
  238. return false;
  239. }
  240. if (max !== undefined && value > max) {
  241. return false;
  242. }
  243. if (step !== undefined) {
  244. if (step <= 0) {
  245. return false;
  246. }
  247. const stepBase = min ?? 0;
  248. const stepCount = (value - stepBase) / step;
  249. if (!this.isAlmostInteger(stepCount)) {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. case 'select': {
  256. return (
  257. typeof value === 'string' &&
  258. field.options.some((option) => option.value === value)
  259. );
  260. }
  261. case 'switch': {
  262. return typeof value === 'boolean';
  263. }
  264. default: {
  265. return typeof value === 'string';
  266. }
  267. }
  268. }
  269. /**
  270. * 从缓存加载扩展偏好设置
  271. * @returns 缓存的扩展偏好设置,如果不存在则返回 null
  272. */
  273. private async loadCustomFromCache(): Promise<CustomPreferencesRecord | null> {
  274. return this.cache.getItem<CustomPreferencesRecord>(STORAGE_KEYS.CUSTOM);
  275. }
  276. /**
  277. * 从缓存加载偏好设置
  278. * @returns 缓存的偏好设置,如果不存在则返回 null
  279. */
  280. private async loadFromCache(): Promise<null | Preferences> {
  281. return this.cache.getItem<Preferences>(STORAGE_KEYS.MAIN);
  282. }
  283. private replaceCustomPreferences(preferences: CustomPreferencesRecord) {
  284. Object.keys(this.customState).forEach((key) => {
  285. Reflect.deleteProperty(this.customState, key);
  286. });
  287. Object.assign(this.customState, preferences);
  288. }
  289. private resolveCustomPreferencesDefaults(
  290. extension: null | PreferencesExtension<any>,
  291. ) {
  292. if (!extension) {
  293. return {};
  294. }
  295. const result: CustomPreferencesRecord = {};
  296. for (const field of extension.fields) {
  297. result[field.key] = field.defaultValue;
  298. }
  299. return result;
  300. }
  301. private resolveNumericConstraint(value: unknown) {
  302. return typeof value === 'number' && Number.isFinite(value)
  303. ? value
  304. : undefined;
  305. }
  306. private sanitizeCustomPreferences(
  307. updates: DeepPartial<CustomPreferencesRecord>,
  308. ) {
  309. if (!this.customPreferencesExtension) {
  310. return {};
  311. }
  312. const result: CustomPreferencesRecord = {};
  313. for (const field of this.customPreferencesExtension.fields) {
  314. const value = updates[field.key];
  315. if (
  316. value !== undefined &&
  317. this.isValidCustomPreferenceValue(field, value)
  318. ) {
  319. result[field.key] = value;
  320. }
  321. }
  322. return result;
  323. }
  324. /**
  325. * 保存偏好设置到缓存
  326. */
  327. private async saveToCache() {
  328. try {
  329. await this.cache.setItem(STORAGE_KEYS.MAIN, this.state);
  330. await this.cache.setItem(STORAGE_KEYS.LOCALE, this.state.app.locale);
  331. await this.cache.setItem(STORAGE_KEYS.THEME, this.state.theme.mode);
  332. if (this.customPreferencesExtension) {
  333. await this.cache.setItem(STORAGE_KEYS.CUSTOM, {
  334. ...this.customState,
  335. });
  336. return;
  337. }
  338. await this.cache.removeItem(STORAGE_KEYS.CUSTOM);
  339. } catch (error) {
  340. console.error('Failed to save preferences to cache:', error);
  341. }
  342. }
  343. /**
  344. * 监听状态和系统偏好设置的变化
  345. */
  346. private setupWatcher() {
  347. if (this.isInitialized) {
  348. return;
  349. }
  350. // 监听断点,判断是否移动端
  351. const breakpoints = useBreakpoints(breakpointsTailwind);
  352. const isMobile = breakpoints.smaller('md');
  353. watch(
  354. () => isMobile.value,
  355. (val) => {
  356. this.updatePreferences({
  357. app: { isMobile: val },
  358. });
  359. },
  360. { immediate: true },
  361. );
  362. // 监听系统主题偏好设置变化
  363. window
  364. .matchMedia('(prefers-color-scheme: dark)')
  365. .addEventListener('change', ({ matches: isDark }) => {
  366. // 仅在自动模式下跟随系统主题
  367. if (this.state.theme.mode === 'auto') {
  368. // 先应用实际的主题
  369. this.updatePreferences({
  370. theme: { mode: isDark ? 'dark' : 'light' },
  371. });
  372. // 再恢复为 auto 模式,保持跟随系统的状态
  373. this.updatePreferences({
  374. theme: { mode: 'auto' },
  375. });
  376. }
  377. });
  378. }
  379. /**
  380. * 更新页面颜色模式(灰色、色弱)
  381. * @param preference - 偏好设置
  382. */
  383. private updateColorMode(preference: Preferences) {
  384. const { colorGrayMode, colorWeakMode } = preference.app;
  385. const dom = document.documentElement;
  386. dom.classList.toggle('invert-mode', colorWeakMode);
  387. dom.classList.toggle('grayscale-mode', colorGrayMode);
  388. }
  389. }
  390. const preferencesManager = new PreferenceManager();
  391. export { PreferenceManager, preferencesManager };