finalize typescript migration
This commit is contained in:
@@ -2,26 +2,36 @@ export const MARKUP_DEFAULT_SHAPE = 'arrow';
|
||||
export const MARKUP_DEFAULT_COLOR = '#ff0000';
|
||||
export const MARKUP_PREVIEW_REFERENCE_DIM = 512;
|
||||
|
||||
function clampFraction(value) {
|
||||
export interface MarkupShape {
|
||||
kind: string;
|
||||
x1: number;
|
||||
y1: number;
|
||||
x2: number;
|
||||
y2: number;
|
||||
width: number;
|
||||
color: string;
|
||||
}
|
||||
|
||||
function clampFraction(value: unknown): number {
|
||||
const numeric = Number(value);
|
||||
if (!Number.isFinite(numeric)) return 0;
|
||||
return Math.max(0, Math.min(1, numeric));
|
||||
}
|
||||
|
||||
export function sanitizeMarkupColor(color, fallback = MARKUP_DEFAULT_COLOR) {
|
||||
export function sanitizeMarkupColor(color: unknown, fallback: string = MARKUP_DEFAULT_COLOR): string {
|
||||
if (typeof color !== 'string') return fallback;
|
||||
const value = color.trim();
|
||||
return /^#[0-9a-fA-F]{6}$/.test(value) ? value.toLowerCase() : fallback;
|
||||
}
|
||||
|
||||
export function sanitizeMarkupShape(
|
||||
shape,
|
||||
fallbackShape = MARKUP_DEFAULT_SHAPE,
|
||||
fallbackColor = MARKUP_DEFAULT_COLOR,
|
||||
fallbackWidth = 3,
|
||||
) {
|
||||
shape: Partial<MarkupShape> | null | undefined,
|
||||
fallbackShape: string = MARKUP_DEFAULT_SHAPE,
|
||||
fallbackColor: string = MARKUP_DEFAULT_COLOR,
|
||||
fallbackWidth: number = 3,
|
||||
): MarkupShape | null {
|
||||
if (!shape || typeof shape !== 'object') return null;
|
||||
const kind = ['line', 'rectangle', 'circle', 'arrow'].includes(shape.kind) ? shape.kind : fallbackShape;
|
||||
const kind = (shape.kind && ['line', 'rectangle', 'circle', 'arrow'].includes(shape.kind)) ? shape.kind : fallbackShape;
|
||||
const x1 = clampFraction(shape.x1);
|
||||
const y1 = clampFraction(shape.y1);
|
||||
const x2 = clampFraction(shape.x2);
|
||||
@@ -39,15 +49,15 @@ export function sanitizeMarkupShape(
|
||||
}
|
||||
|
||||
export function parseMarkupShapes(
|
||||
markupShapes,
|
||||
fallbackShape = MARKUP_DEFAULT_SHAPE,
|
||||
fallbackColor = MARKUP_DEFAULT_COLOR,
|
||||
fallbackWidth = 3,
|
||||
) {
|
||||
markupShapes: unknown,
|
||||
fallbackShape: string = MARKUP_DEFAULT_SHAPE,
|
||||
fallbackColor: string = MARKUP_DEFAULT_COLOR,
|
||||
fallbackWidth: number = 3,
|
||||
): MarkupShape[] {
|
||||
if (Array.isArray(markupShapes)) {
|
||||
return markupShapes
|
||||
.map((shape) => sanitizeMarkupShape(shape, fallbackShape, fallbackColor, fallbackWidth))
|
||||
.filter(Boolean);
|
||||
.filter((s): s is MarkupShape => s != null);
|
||||
}
|
||||
|
||||
if (typeof markupShapes !== 'string' || !markupShapes.trim()) return [];
|
||||
@@ -57,13 +67,13 @@ export function parseMarkupShapes(
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed
|
||||
.map((shape) => sanitizeMarkupShape(shape, fallbackShape, fallbackColor, fallbackWidth))
|
||||
.filter(Boolean);
|
||||
.filter((s): s is MarkupShape => s != null);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function getArrowGeometry(shape, imageWidth, imageHeight) {
|
||||
export function getArrowGeometry(shape: MarkupShape, imageWidth: number, imageHeight: number) {
|
||||
const x1 = shape.x1 * imageWidth;
|
||||
const y1 = shape.y1 * imageHeight;
|
||||
const x2 = shape.x2 * imageWidth;
|
||||
@@ -90,7 +100,7 @@ export function getArrowGeometry(shape, imageWidth, imageHeight) {
|
||||
};
|
||||
}
|
||||
|
||||
export function getMarkupPreviewStrokeWidth(width, imageWidth, imageHeight) {
|
||||
export function getMarkupPreviewStrokeWidth(width: number, imageWidth: number, imageHeight: number) {
|
||||
const normalizedWidth = Math.max(1, Math.round(Number(width) || 1));
|
||||
const longestDim = Math.max(1, Number(imageWidth) || 0, Number(imageHeight) || 0);
|
||||
const scale = Math.max(1, longestDim / MARKUP_PREVIEW_REFERENCE_DIM);
|
||||
|
||||
Reference in New Issue
Block a user