Files
web-temp/layers/utils/dataUtil.ts

67 lines
1.5 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}`;
};
// [TODO] data 타입 정의
export const getResourcesData = ({
resources,
groupsDepth = 0,
}: {
resources: any;
groupsDepth?: number;
}) => {
const group = resources[0]?.groups?.[groupsDepth];
return group ?? null;
};
// 반응형 클래스 리턴하는 함수
export const getResponsiveClass = () => {
return ["bg-[image:var(--mobile-bg)]", "sm:bg-[image:var(--pc-bg)]"];
};
// 반응형 이미지 리턴하는 함수
export const getResponsiveSrc = ({
pathArray,
type = "image",
}: {
pathArray: any;
type?: ImageType;
}): ResponsiveImageResult | ResponsiveImagePath => {
if (!pathArray?.path_mo) {
return null;
}
const resolvedImages = {
pc: getResolvedSrc(pathArray.path_pc || pathArray.path_mo),
mobile: getResolvedSrc(pathArray.path_mo),
};
if (type === "image") {
return {
mobileSrc: resolvedImages.mobile,
pcSrc: resolvedImages.pc,
};
}
return {
"--pc-bg": `url(${resolvedImages.pc})`,
"--mobile-bg": `url(${resolvedImages.mobile})`,
};
};