62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
"use client";
|
|
|
|
import { useLayoutEffect } from "react";
|
|
|
|
import { useLocalSettings } from "@/core/settings";
|
|
import {
|
|
applyFontScaleToDocument,
|
|
normalizeFontScale,
|
|
normalizeFontScaleMode,
|
|
} from "@/core/ui/page-zoom";
|
|
|
|
/**
|
|
* 基准字号 = 16px,对应《深蓝色 UI 规范》§2.1.2 后台系统正文字号(16px Regular)。
|
|
* 「仅文字」把倍数写到 `--ui-font-scale`,只放大 Tailwind 字号 token 和正文继承字号。
|
|
* 「整页缩放」改在 `<html>` 上设置 CSS zoom,写死的 px 字号、间距和图标一起变;
|
|
* 此时 `--ui-font-scale` 固定为 1,避免和 zoom 叠乘。
|
|
* `fontScale === 1` 即默认 16px。
|
|
*/
|
|
export const BASE_FONT_PX = 16;
|
|
|
|
export const FONT_SCALE_OPTIONS: { value: number; label: string; px: number }[] =
|
|
[
|
|
{ value: 0.875, label: "小", px: 14 },
|
|
{ value: 1, label: "标准", px: 16 },
|
|
{ value: 1.125, label: "大", px: 18 },
|
|
{ value: 1.25, label: "特大", px: 20 },
|
|
];
|
|
|
|
export const FONT_SCALE_MODE_OPTIONS: {
|
|
value: "text" | "page";
|
|
label: string;
|
|
description: string;
|
|
}[] = [
|
|
{
|
|
value: "text",
|
|
label: "仅文字",
|
|
description: "只放大文字,间距和布局保持原样。",
|
|
},
|
|
{
|
|
value: "page",
|
|
label: "整页缩放",
|
|
description: "文字、间距、图标和写死的像素一起放大。",
|
|
},
|
|
];
|
|
|
|
/**
|
|
* 把字号设置写到文档根节点。挂在应用根部,刷新后、绘制前生效。不渲染任何 DOM。
|
|
*/
|
|
export function FontScaleApplier() {
|
|
const [settings] = useLocalSettings();
|
|
const { fontScale, fontScaleMode } = settings.display;
|
|
|
|
useLayoutEffect(() => {
|
|
applyFontScaleToDocument(
|
|
normalizeFontScaleMode(fontScaleMode),
|
|
normalizeFontScale(fontScale),
|
|
);
|
|
}, [fontScale, fontScaleMode]);
|
|
|
|
return null;
|
|
}
|