38 lines
1019 B
TypeScript
38 lines
1019 B
TypeScript
export const MARKDOWN_IMAGE_MAX_WIDTH = 480;
|
|
export const MARKDOWN_IMAGE_MAX_HEIGHT = 480;
|
|
|
|
export interface MarkdownImageDisplaySize {
|
|
readonly width: number;
|
|
readonly height: number;
|
|
}
|
|
|
|
/** Keeps small images intrinsic while fitting larger images inside the chat viewport. */
|
|
export function resolveMarkdownImageDisplaySize(input: {
|
|
readonly sourceWidth: number;
|
|
readonly sourceHeight: number;
|
|
readonly availableWidth: number;
|
|
}): MarkdownImageDisplaySize | null {
|
|
if (
|
|
!Number.isFinite(input.sourceWidth) ||
|
|
!Number.isFinite(input.sourceHeight) ||
|
|
!Number.isFinite(input.availableWidth) ||
|
|
input.sourceWidth <= 0 ||
|
|
input.sourceHeight <= 0 ||
|
|
input.availableWidth <= 0
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
const scale = Math.min(
|
|
1,
|
|
input.availableWidth / input.sourceWidth,
|
|
MARKDOWN_IMAGE_MAX_WIDTH / input.sourceWidth,
|
|
MARKDOWN_IMAGE_MAX_HEIGHT / input.sourceHeight,
|
|
);
|
|
|
|
return {
|
|
width: input.sourceWidth * scale,
|
|
height: input.sourceHeight * scale,
|
|
};
|
|
}
|