front/src/utils/pageBackground.ts
zzh c4f7144f47
All checks were successful
Build and Deploy Front / deploy (push) Successful in 4m31s
refine: 页面背景贯通整页布局,统一卡片圆角并更新 Edi 形象
页面背景联动顶栏与内容壳;去掉块级描边;侧栏/面板圆角统一 8px;上下文条淡橙底;Edi FAB 换新立绘并保留眨眼与跳动。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-25 12:33:15 +08:00

101 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 页面背景色:OMS 与官网首页共用 localStorage
*/
export const PAGE_BACKGROUND_STORAGE_KEY = 'app-page-background-color';
export const DEFAULT_PAGE_BACKGROUND = '#f0f3f4';
export const PAGE_BACKGROUND_PRESETS = [
{ color: '#f0f3f4', labelKey: 'settings.pageBg.default' },
{ color: '#f6f1e7', labelKey: 'settings.pageBg.ningzhi' },
{ color: '#f4f6f9', labelKey: 'settings.pageBg.mist' },
{ color: '#f5f4f1', labelKey: 'settings.pageBg.stone' },
{ color: '#f1f4f2', labelKey: 'settings.pageBg.sage' },
{ color: '#f5f2f7', labelKey: 'settings.pageBg.lilac' },
{ color: '#f0f2f6', labelKey: 'settings.pageBg.slate' },
{ color: '#faf9f7', labelKey: 'settings.pageBg.pearl' },
{ color: '#ffffff', labelKey: 'settings.pageBg.white' },
] as const;
function hexToRgb(hex: string): { r: number; g: number; b: number } | null {
const m = hex.replace(/^#/, '').match(/^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i);
if (!m) return null;
return {
r: parseInt(m[1], 16),
g: parseInt(m[2], 16),
b: parseInt(m[3], 16),
};
}
function rgbToHex(r: number, g: number, b: number): string {
return (
'#' +
[r, g, b]
.map((x) => Math.round(Math.max(0, Math.min(255, x))).toString(16).padStart(2, '0'))
.join('')
);
}
/** 生成略浅一档的背景色(用于官网辅助层) */
function softenHex(hex: string, mixWhite = 0.55): string {
const rgb = hexToRgb(hex);
if (!rgb) return hex;
const r = Math.round(rgb.r * (1 - mixWhite) + 255 * mixWhite);
const g = Math.round(rgb.g * (1 - mixWhite) + 255 * mixWhite);
const b = Math.round(rgb.b * (1 - mixWhite) + 255 * mixWhite);
return rgbToHex(r, g, b);
}
export function getStoredPageBackground(): string | null {
try {
return localStorage.getItem(PAGE_BACKGROUND_STORAGE_KEY);
} catch {
return null;
}
}
export function setStoredPageBackground(hex: string): void {
try {
localStorage.setItem(PAGE_BACKGROUND_STORAGE_KEY, hex);
} catch {
// ignore
}
}
/** 写入 OMS + 官网首页共用的背景 CSS 变量(含顶栏 / 整页壳) */
export function applyPageBackgroundColor(hex: string): void {
const color = hex.startsWith('#') ? hex : `#${hex}`;
const root = document.documentElement;
const isDark = document.body.getAttribute('arco-theme') === 'dark';
const warmPageBgs = new Set(['#f6f1e7', '#f5f4f1', '#faf9f7']);
const soft = softenHex(color, warmPageBgs.has(color.toLowerCase()) ? 0.42 : 0.55);
// 夜间模式由 themeColor.applyThemeMode 接管底色,避免日间预设冲掉深色壳
if (isDark) {
root.style.setProperty('--kc-page-bg', color);
root.style.setProperty('--kc-neutral', soft);
return;
}
root.style.setProperty('--app-page-bg', color);
root.style.setProperty('--app-navbar-bg', color);
// Arco 布局 / 大量业务页用 --color-bg-1,不同步则只有顶栏变、内容区仍是默认灰
root.style.setProperty('--color-bg-1', color);
root.style.setProperty(
'--app-page-bg-gradient',
`linear-gradient(180deg, color-mix(in srgb, var(--app-primary-color, #054f74) 5%, ${color}) 0%, ${color} 220px, ${color} 100%)`,
);
root.style.setProperty('--kc-page-bg', color);
root.style.setProperty('--kc-neutral', soft);
root.style.setProperty('--ai-bg', color);
root.style.setProperty('--ai-canvas', color);
root.style.setProperty('--ai-bg-soft', soft);
root.style.setProperty('--ai-header-bg', `color-mix(in srgb, ${color} 94%, transparent)`);
// 直接刷 html/body,避免仅靠变量时被实色壳盖住
root.style.backgroundColor = color;
document.body.style.backgroundColor = color;
document.body.style.backgroundImage = `linear-gradient(180deg, color-mix(in srgb, var(--app-primary-color, #054f74) 5%, ${color}) 0%, ${color} 220px, ${color} 100%)`;
}