Files
edgeever/packages/shared/src/image-display.ts
T
tianma-if e90e939815 feat(mobile): support image size presets
Add the four desktop-compatible image width presets to the native app and share width configuration across clients.
2026-07-21 03:50:56 +08:00

29 lines
1009 B
TypeScript

export const DEFAULT_IMAGE_WIDTH_PERCENT = 72;
export const MIN_IMAGE_WIDTH_PERCENT = 25;
export const MAX_IMAGE_WIDTH_PERCENT = 100;
export const IMAGE_WIDTH_PRESETS = [
{ id: "small", width: 35, labelKey: "editor.imageSizeSmall" },
{ id: "medium", width: 50, labelKey: "editor.imageSizeMedium" },
{ id: "large", width: 72, labelKey: "editor.imageSizeLarge" },
{ id: "full", width: 100, labelKey: "editor.imageSizeFull" },
] as const;
export type ImageWidthPresetId = (typeof IMAGE_WIDTH_PRESETS)[number]["id"];
export const clampImageWidth = (width: number): number =>
Math.min(MAX_IMAGE_WIDTH_PERCENT, Math.max(MIN_IMAGE_WIDTH_PERCENT, Math.round(width)));
export const parseImageWidth = (value: unknown): number | null => {
if (typeof value === "number" && Number.isFinite(value)) {
return clampImageWidth(value);
}
if (typeof value !== "string") {
return null;
}
const match = /(\d+(?:\.\d+)?)/.exec(value);
return match ? clampImageWidth(Number(match[1])) : null;
};