新增根 biome.json(v2.5,v2 起原生支持 monorepo,子包自动向上查找):
- formatter: 2 空格缩进、单引号、行宽 100、LF、尾逗号、箭头函数括号
(匹配现有代码风格)
- linter: recommended preset + 项目化裁剪
- useTemplate/useTemplate off(geometry.ts 故意保留字符串拼接,
且测试用精确浮点断言锁定行为)
- noNonNullAssertion off(测试与 DOM 访问里的 ! 是惯用写法)
- noConsole off(4 个 lib 用 console.error/warn 做错误上报)
- CSS 关闭 noDescendingSpecificity/noDuplicateProperties
(手写 CSS 的 fallback 模式)
- useImportType/useNodejsImportProtocol/noUnusedVariables warn
- vcs.useIgnoreFile 自动读 .gitignore;排除 public/target/node_modules/
dist/.dioxus/static + Tailwind 入口 input.css(含 @theme 等 Biome 无法
解析的 at-rules)
首次格式化覆盖 29 个源文件:import 排序、node: 协议、引号/缩进统一,
并修复 index.ts 两处 forEach 回调隐式返回值(useIterableCallbackReturn)。
90 个 vitest 用例全绿,确认无语义改动。
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
export interface Rect {
|
||
x: number;
|
||
y: number;
|
||
w: number;
|
||
h: number;
|
||
}
|
||
|
||
// 计算图片在视口居中、contain 适配后的目标 rect。
|
||
// naturalW/H: 图片真实像素尺寸;vw/vh: 视口尺寸。
|
||
export function fitCentered(naturalW: number, naturalH: number, vw: number, vh: number): Rect {
|
||
var maxW = vw * 0.92;
|
||
var maxH = vh * 0.88;
|
||
var scale = Math.min(maxW / naturalW, maxH / naturalH, 1);
|
||
var w = naturalW * scale;
|
||
var h = naturalH * scale;
|
||
return {
|
||
x: (vw - w) / 2,
|
||
y: (vh - h) / 2,
|
||
w: w,
|
||
h: h,
|
||
};
|
||
}
|
||
|
||
// 把目标 rect 转成 transform 字符串。
|
||
// baseW/baseH 是 img 元素的布局尺寸(=居中目标尺寸),scale 相对它缩放。
|
||
// transform-origin 为 top left(见 CSS),translate 到 rect 左上角后 scale。
|
||
// - 居中态:scale=1(base 就是居中尺寸)
|
||
// - originRect 态:scale = originRect.w / base.w(缩小)
|
||
export function transformFor(rect: Rect, baseW: number, baseH: number): string {
|
||
var sx = baseW > 0 ? rect.w / baseW : 1;
|
||
var sy = baseH > 0 ? rect.h / baseH : 1;
|
||
return 'translate(' + rect.x + 'px,' + rect.y + 'px) scale(' + sx + ',' + sy + ')';
|
||
}
|
||
|
||
// 原图 URL = data-src 去 query。data-src 形如 "/uploads/x.webp?w=800"。
|
||
export function originalUrl(dataSrc: string | null): string {
|
||
return (dataSrc || '').split('?')[0];
|
||
}
|