* feat: enable project-scoped preferences extension tabs Add a typed extension schema so subprojects can define extra settings, render them in the shared preferences drawer only when configured, and consume them in playground as a real feature demo. Extension labels now follow locale keys instead of hardcoded app-specific strings. Constraint: Reuse the shared preferences drawer and field blocks Rejected: Add app-specific fields to core preferences | too tightly coupled Rejected: Inline localized label objects | breaks existing locale-key flow Confidence: high Scope-risk: moderate Reversibility: clean Directive: Keep extension labels as locale keys rendered via $t in UI Tested: Vitest preferences tests Tested: Turbo typecheck for preferences, layouts, web-antd, and playground Tested: ESLint for touched preferences and playground files Not-tested: Manual browser interaction in playground preferences drawer * fix: satisfy lint formatting for preferences extension demo Adjust the playground preferences extension demo template so formatter and Vue template lint rules agree on the rendered markup. This keeps CI green without changing runtime behavior. Constraint: Must preserve the existing demo behavior while fixing CI only Rejected: Disable the Vue newline rule | would weaken shared lint guarantees Confidence: high Scope-risk: narrow Reversibility: clean Directive: Prefer computed/template structures that avoid formatter-vs-lint conflicts Tested: pnpm run lint Not-tested: Manual browser interaction in playground preferences extension demo * fix: harden custom preferences validation and i18n labels Tighten custom preferences handling so numeric extension fields respect min, max, and step constraints. Number inputs now ignore NaN values, and web-antd extension metadata uses locale keys instead of raw strings. Also align tip-based hover guards in shared preference inputs/selects. Constraint: Keep fixes scoped to verified findings only Rejected: Broader refactor of preferences field components | not needed for these fixes Confidence: high Scope-risk: narrow Reversibility: clean Directive: Reuse the same validation path for updates and cache hydration Tested: Vitest preferences tests Tested: ESLint for touched preferences and widget files Tested: Typecheck for web-antd, layouts, and core preferences Not-tested: Manual browser interaction for all preference field variants * fix: remove localized default from playground extension config Drop the hardcoded Chinese default value from the playground extension report title field and fall back to an empty string instead. This keeps extension config locale-neutral while preserving localized labels and placeholders through translation keys. Constraint: Keep the fix limited to the verified localized default issue Rejected: Compute the default from runtime locale in config | unnecessary for this finding Confidence: high Scope-risk: narrow Reversibility: clean Directive: Avoid embedding localized literals in extension default values Tested: ESLint for playground/src/preferences.ts Tested: Oxfmt check for playground/src/preferences.ts Not-tested: Manual playground preferences interaction * docs: document project-scoped preferences extension workflow Add Chinese and English guide sections explaining how to define, initialize, read, and update project-scoped preferences extensions. Also document numeric field validation and point readers to the playground demo for a complete example. Constraint: Keep this docs-only and aligned with the shipped API Rejected: Update only Chinese docs | would leave English docs inconsistent Confidence: high Scope-risk: narrow Reversibility: clean Directive: Keep zh/en examples and playground demo paths synchronized Tested: git diff --check; pnpm build:docs Not-tested: Manual browser review of the rendered docs site * fix: harden custom preferences defaults and baselines Use a locale-neutral default for the web-antd report title. Also stop preference getters from exposing mutable baseline or extension schema objects, and add a regression test for external mutation attempts. Constraint: Keep behavior compatible with the shipped preferences API Rejected: Return raw refs with readonly typing only | callers could still mutate internals Confidence: high Scope-risk: narrow Reversibility: clean Directive: Keep defensive copies for baseline and schema getters unless storage semantics change Tested: eslint, oxlint, targeted vitest, filtered typecheck, git diff --check Not-tested: Full monorepo typecheck and test suite * test: relax custom preference cache key matching Avoid coupling the custom-number cache test to one exact localStorage key string. Match the intended cache lookup more loosely so the test still verifies filtering behavior without depending on the full namespaced cache key. Constraint: Focus the test on cache filtering behavior Rejected: Assert one exact key | brittle with namespace changes Confidence: high Scope-risk: narrow Reversibility: clean Directive: Prefer behavior tests over literal storage keys Tested: targeted vitest, eslint, git diff --check Not-tested: Full monorepo test suite --------- Co-authored-by: caisin <caisin@caisins-Mac-mini.local>
268 lines
6.5 KiB
TypeScript
268 lines
6.5 KiB
TypeScript
import { computed } from 'vue';
|
||
|
||
import { diff } from '@vben-core/shared/utils';
|
||
|
||
import { preferencesManager } from './preferences';
|
||
import { isDarkTheme } from './update-css-variables';
|
||
|
||
function usePreferences() {
|
||
const preferences = preferencesManager.getPreferences();
|
||
const customPreferences = preferencesManager.getCustomPreferences();
|
||
const initialPreferences = preferencesManager.getInitialPreferences();
|
||
const initialCustomPreferences =
|
||
preferencesManager.getInitialCustomPreferences();
|
||
const preferencesExtension = computed(() =>
|
||
preferencesManager.getPreferencesExtension(),
|
||
);
|
||
/**
|
||
* @zh_CN 计算偏好设置的变化
|
||
*/
|
||
const diffPreference = computed(() => {
|
||
return diff(initialPreferences, preferences);
|
||
});
|
||
|
||
const diffCustomPreference = computed(() => {
|
||
return diff(initialCustomPreferences, customPreferences);
|
||
});
|
||
|
||
const appPreferences = computed(() => preferences.app);
|
||
|
||
const shortcutKeysPreferences = computed(() => preferences.shortcutKeys);
|
||
|
||
/**
|
||
* @zh_CN 判断是否为暗黑模式
|
||
* @param preferences - 当前偏好设置对象,它的主题值将被用来判断是否为暗黑模式。
|
||
* @returns 如果主题为暗黑模式,返回 true,否则返回 false。
|
||
*/
|
||
const isDark = computed(() => {
|
||
return isDarkTheme(preferences.theme.mode);
|
||
});
|
||
|
||
const locale = computed(() => {
|
||
return preferences.app.locale;
|
||
});
|
||
|
||
const isMobile = computed(() => {
|
||
return appPreferences.value.isMobile;
|
||
});
|
||
|
||
const theme = computed(() => {
|
||
return isDark.value ? 'dark' : 'light';
|
||
});
|
||
|
||
/**
|
||
* @zh_CN 布局方式
|
||
*/
|
||
const layout = computed(() =>
|
||
isMobile.value ? 'sidebar-nav' : appPreferences.value.layout,
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 是否显示顶栏
|
||
*/
|
||
const isShowHeaderNav = computed(() => {
|
||
return preferences.header.enable;
|
||
});
|
||
|
||
/**
|
||
* @zh_CN 是否全屏显示content,不需要侧边、底部、顶部、tab区域
|
||
*/
|
||
const isFullContent = computed(
|
||
() => appPreferences.value.layout === 'full-content',
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 是否侧边导航模式
|
||
*/
|
||
const isSideNav = computed(
|
||
() => appPreferences.value.layout === 'sidebar-nav',
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 是否侧边混合模式
|
||
*/
|
||
const isSideMixedNav = computed(
|
||
() => appPreferences.value.layout === 'sidebar-mixed-nav',
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 是否为头部导航模式
|
||
*/
|
||
const isHeaderNav = computed(
|
||
() => appPreferences.value.layout === 'header-nav',
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 是否为头部混合导航模式
|
||
*/
|
||
const isHeaderMixedNav = computed(
|
||
() => appPreferences.value.layout === 'header-mixed-nav',
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 是否为顶部通栏+侧边导航模式
|
||
*/
|
||
const isHeaderSidebarNav = computed(
|
||
() => appPreferences.value.layout === 'header-sidebar-nav',
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 是否为混合导航模式
|
||
*/
|
||
const isMixedNav = computed(
|
||
() => appPreferences.value.layout === 'mixed-nav',
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 是否包含侧边导航模式
|
||
*/
|
||
const isSideMode = computed(() => {
|
||
return (
|
||
isMixedNav.value ||
|
||
isSideMixedNav.value ||
|
||
isSideNav.value ||
|
||
isHeaderMixedNav.value ||
|
||
isHeaderSidebarNav.value
|
||
);
|
||
});
|
||
|
||
const sidebarCollapsed = computed(() => {
|
||
return preferences.sidebar.collapsed;
|
||
});
|
||
|
||
/**
|
||
* @zh_CN 是否开启keep-alive
|
||
* 在tabs可见以及开启keep-alive的情况下才开启
|
||
*/
|
||
const keepAlive = computed(
|
||
() => preferences.tabbar.enable && preferences.tabbar.keepAlive,
|
||
);
|
||
|
||
/**
|
||
* @zh_CN 登录注册页面布局是否为左侧
|
||
*/
|
||
const authPanelLeft = computed(() => {
|
||
return appPreferences.value.authPageLayout === 'panel-left';
|
||
});
|
||
|
||
/**
|
||
* @zh_CN 登录注册页面布局是否为右侧
|
||
*/
|
||
const authPanelRight = computed(() => {
|
||
return appPreferences.value.authPageLayout === 'panel-right';
|
||
});
|
||
|
||
/**
|
||
* @zh_CN 登录注册页面布局是否为中间
|
||
*/
|
||
const authPanelCenter = computed(() => {
|
||
return appPreferences.value.authPageLayout === 'panel-center';
|
||
});
|
||
|
||
/**
|
||
* @zh_CN 内容是否已经最大化
|
||
* 排除 full-content模式
|
||
*/
|
||
const contentIsMaximize = computed(() => {
|
||
const headerIsHidden = preferences.header.hidden;
|
||
const sidebarIsHidden = preferences.sidebar.hidden;
|
||
return headerIsHidden && sidebarIsHidden && !isFullContent.value;
|
||
});
|
||
|
||
/**
|
||
* @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;
|
||
});
|
||
|
||
/**
|
||
* @zh_CN 偏好设置按钮位置
|
||
*/
|
||
const preferencesButtonPosition = computed(() => {
|
||
const { enablePreferences, preferencesButtonPosition } = preferences.app;
|
||
|
||
// 如果没有启用偏好设置按钮
|
||
if (!enablePreferences) {
|
||
return {
|
||
fixed: false,
|
||
header: false,
|
||
};
|
||
}
|
||
|
||
const { header, sidebar } = preferences;
|
||
const headerHidden = header.hidden;
|
||
const sidebarHidden = sidebar.hidden;
|
||
|
||
const contentIsMaximize = headerHidden && sidebarHidden;
|
||
|
||
const isHeaderPosition = preferencesButtonPosition === 'header';
|
||
|
||
// 如果设置了固定位置
|
||
if (preferencesButtonPosition !== 'auto') {
|
||
return {
|
||
fixed: preferencesButtonPosition === 'fixed',
|
||
header: isHeaderPosition,
|
||
};
|
||
}
|
||
|
||
// 如果是全屏模式或者没有固定在顶部,
|
||
const fixed =
|
||
contentIsMaximize ||
|
||
isFullContent.value ||
|
||
isMobile.value ||
|
||
!isShowHeaderNav.value;
|
||
|
||
return {
|
||
fixed,
|
||
header: !fixed,
|
||
};
|
||
});
|
||
|
||
return {
|
||
authPanelCenter,
|
||
authPanelLeft,
|
||
authPanelRight,
|
||
contentIsMaximize,
|
||
customPreferences,
|
||
diffPreference,
|
||
diffCustomPreference,
|
||
globalLockScreenShortcutKey,
|
||
globalLogoutShortcutKey,
|
||
globalSearchShortcutKey,
|
||
isDark,
|
||
isFullContent,
|
||
isHeaderMixedNav,
|
||
isHeaderNav,
|
||
isHeaderSidebarNav,
|
||
isMixedNav,
|
||
isMobile,
|
||
isSideMixedNav,
|
||
isSideMode,
|
||
isSideNav,
|
||
keepAlive,
|
||
layout,
|
||
locale,
|
||
preferencesExtension,
|
||
preferencesButtonPosition,
|
||
sidebarCollapsed,
|
||
theme,
|
||
};
|
||
}
|
||
|
||
export { usePreferences };
|