"use client";
import { useDrag } from "react-dnd";
import { Type, Barcode, Image, Minus, Square } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { BarcodeLabelComponent } from "@/types/barcode";
import { v4 as uuidv4 } from "uuid";
const ITEMS: { type: BarcodeLabelComponent["type"]; label: string; icon: React.ReactNode; barcodeType?: string }[] = [
{ type: "text", label: "텍스트", icon: },
{ type: "barcode", label: "바코드", icon: },
{ type: "barcode", label: "QR 코드", icon: , barcodeType: "QR" },
{ type: "image", label: "이미지", icon: },
{ type: "line", label: "선", icon: },
{ type: "rectangle", label: "사각형", icon: },
];
const MM_TO_PX = 4;
function defaultComponent(type: BarcodeLabelComponent["type"], barcodeType?: string): BarcodeLabelComponent {
const id = `comp_${uuidv4()}`;
const base = { id, type, x: 10 * MM_TO_PX, y: 10 * MM_TO_PX, width: 80, height: 24, zIndex: 0 };
switch (type) {
case "text":
return { ...base, content: "텍스트", fontSize: 10, fontColor: "#000000" };
case "barcode": {
const isQR = barcodeType === "QR";
return {
...base,
width: isQR ? 100 : 120,
height: isQR ? 100 : 40,
barcodeType: barcodeType || "CODE128",
barcodeValue: isQR ? "" : "123456789",
showBarcodeText: !isQR,
};
}
case "image":
return { ...base, width: 60, height: 60, imageUrl: "", objectFit: "contain" };
case "line":
return { ...base, width: 100, height: 2, lineColor: "#000", lineWidth: 1 };
case "rectangle":
return { ...base, width: 80, height: 40, backgroundColor: "transparent", lineColor: "#000", lineWidth: 1 };
default:
return base;
}
}
function DraggableItem({
type,
label,
icon,
barcodeType,
}: {
type: BarcodeLabelComponent["type"];
label: string;
icon: React.ReactNode;
barcodeType?: string;
}) {
const [{ isDragging }, drag] = useDrag(() => ({
type: "barcode-component",
item: { component: defaultComponent(type, barcodeType) },
collect: (m) => ({ isDragging: m.isDragging() }),
}));
return (
{icon}
{label}
);
}
export function BarcodeComponentPalette() {
return (
요소 추가
{ITEMS.map((item, idx) => (
))}
);
}