Files
web-temp/layers/utils/dataUtil.ts
2025-09-09 03:34:09 +00:00

55 lines
1.4 KiB
TypeScript

// 타입 import
import type {
ResponsiveImagePath,
ImageType,
ResponsiveImageResult,
} from "../types/utils/dataUtil";
// 이미지 경로 리턴하는 함수
// [TODO] 환경변수 처리 수정
export const getResolvedSrc = (path: string): string => {
const config = useRuntimeConfig();
// const isDev = process.env.NODE_ENV === "development";
// const rootPath = isDev ? "/images" : `${config.public.staticUrl}`;
const rootPath = config.public.staticUrl;
return `${rootPath}${path}`;
};
// 반응형 클래스 리턴하는 함수
export const getResponsiveClass = () => {
return ["bg-[image:var(--mobile-bg)]", "sm:bg-[image:var(--pc-bg)]"];
};
// 반응형 이미지 리턴하는 함수
export const getResponsiveResolvedSrc = (
path: ResponsiveImagePath,
type: ImageType = "img"
): ResponsiveImageResult => {
// 필수 경로가 없으면 null 반환
if (!path?.path_mo || !path?.path_pc) {
return null;
}
// 이미지 경로 해석
const resolvedImages = {
pc: getResolvedSrc(path.path_pc),
mobile: getResolvedSrc(path.path_mo),
};
// 타입별 반환 객체 생성
const resultMap: Record<ImageType, ResponsiveImageResult> = {
img: {
mobileSrc: resolvedImages.mobile,
pcSrc: resolvedImages.pc,
},
bg: {
"--pc-bg": `url(${resolvedImages.pc})`,
"--mobile-bg": `url(${resolvedImages.mobile})`,
},
};
return resultMap[type];
};