260115_01_ENH : SMS 에디터 수정 및 이미지 리로드 관련 버그 수정
This commit is contained in:
195
src/components/kri-editor/KriEditorSms.tsx
Normal file
195
src/components/kri-editor/KriEditorSms.tsx
Normal file
@@ -0,0 +1,195 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cn } from "./utils/cn"; // 기존 "@/lib/utils" 제거
|
||||
import type { KriEditorProps } from "./types";
|
||||
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
||||
import { useTextareaInsert } from "./hooks/useTextareaInsert";
|
||||
|
||||
type SmsEditorProps = Omit<KriEditorProps, "mode"> & {
|
||||
mode: "sms";
|
||||
};
|
||||
|
||||
export function KriEditorSms(props: SmsEditorProps) {
|
||||
const {
|
||||
label,
|
||||
value,
|
||||
onChange,
|
||||
placeholder = "문자 내용을 입력하세요...",
|
||||
className,
|
||||
debounceMs = 350,
|
||||
smsTitle = "",
|
||||
changeSmsType,
|
||||
onEditorReady,
|
||||
} = props;
|
||||
|
||||
const [text, setText] = React.useState<string>(value || "");
|
||||
const [byteSize, setByteSize] = React.useState(0);
|
||||
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null);
|
||||
const latestTextRef = React.useRef(text);
|
||||
const lastTypeRef = React.useRef<"SMS" | "LMS" | null>(null);
|
||||
|
||||
const { scheduleChange, flushChange } = useDebouncedChange(onChange, debounceMs);
|
||||
const ti = useTextareaInsert(textareaRef);
|
||||
|
||||
// 한글은 2byte, 영문/숫자/특수문자는 1byte로 계산
|
||||
const calculateBytes = React.useCallback((str: string): number => {
|
||||
let bytes = 0;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const char = str.charAt(i);
|
||||
if (escape(char).length > 4) {
|
||||
bytes += 2;
|
||||
} else {
|
||||
bytes += 1;
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}, []);
|
||||
|
||||
const hasTitle = (smsTitle?.length ?? 0) > 0;
|
||||
const messageType: "SMS" | "LMS" = (byteSize > 90 || hasTitle) ? "LMS" : "SMS";
|
||||
const maxBytes = messageType === "LMS" ? 2000 : 90;
|
||||
const messageTypeColor = messageType === "LMS" ? 'bg-blue-500' : 'bg-red-500';
|
||||
|
||||
const handleTextChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
const next = e.target.value;
|
||||
const bytes = calculateBytes(next);
|
||||
|
||||
// 최대 바이트 초과 방지
|
||||
if (bytes > 2000) {
|
||||
return;
|
||||
}
|
||||
|
||||
setText(next);
|
||||
setByteSize(bytes);
|
||||
latestTextRef.current = next;
|
||||
scheduleChange(next);
|
||||
};
|
||||
|
||||
const handleBlur = () => {
|
||||
flushChange(latestTextRef.current);
|
||||
};
|
||||
|
||||
// 외부 value 변경 시 동기화
|
||||
React.useEffect(() => {
|
||||
if ((value || "") !== latestTextRef.current) {
|
||||
const newText = value || "";
|
||||
setText(newText);
|
||||
setByteSize(calculateBytes(newText));
|
||||
latestTextRef.current = newText;
|
||||
}
|
||||
}, [value, calculateBytes]);
|
||||
|
||||
// 바이트 계산 초기화
|
||||
React.useEffect(() => {
|
||||
setByteSize(calculateBytes(text));
|
||||
}, [text, calculateBytes]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!changeSmsType) return;
|
||||
if (lastTypeRef.current === messageType) return;
|
||||
|
||||
lastTypeRef.current = messageType;
|
||||
changeSmsType(messageType);
|
||||
}, [messageType, changeSmsType]);
|
||||
|
||||
// onEditorReady 콜백
|
||||
React.useEffect(() => {
|
||||
if (textareaRef.current && onEditorReady) {
|
||||
onEditorReady(textareaRef.current);
|
||||
}
|
||||
}, [onEditorReady]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!props.onCommandReady) return;
|
||||
|
||||
props.onCommandReady({
|
||||
focus: () => textareaRef.current?.focus(),
|
||||
insertText: (placeholder: string) => {
|
||||
const current = latestTextRef.current ?? "";
|
||||
|
||||
ti.insertText(
|
||||
placeholder,
|
||||
current,
|
||||
(next) => {
|
||||
// 로컬 반영
|
||||
setText(next);
|
||||
setByteSize(calculateBytes(next));
|
||||
latestTextRef.current = next;
|
||||
},
|
||||
(next) => {
|
||||
// 부모 반영(즉시) - debounce를 이기기 위해 flush 권장
|
||||
flushChange(next);
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
return () => props.onCommandReady?.(null);
|
||||
}, [props.onCommandReady, ti, calculateBytes, flushChange]);
|
||||
|
||||
return (
|
||||
<div className={cn("w-full max-w-[320px] mx-auto", className)}>
|
||||
{label && (
|
||||
<div className="mb-2 text-sm font-semibold">
|
||||
{label}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 휴대전화 디자인 */}
|
||||
<div className="relative w-full h-[600px] bg-white rounded-[40px] shadow-2xl border-8 border-gray-800 overflow-hidden">
|
||||
{/* 상단 노치 */}
|
||||
<div className="absolute top-0 left-1/2 -translate-x-1/2 w-32 h-6 bg-gray-800 rounded-b-2xl z-10" />
|
||||
|
||||
{/* 스크린 영역 */}
|
||||
<div className="h-full bg-gray-50 pt-8 pb-4 px-4 flex flex-col">
|
||||
{/* 헤더 - 바이트 및 타입 표시 */}
|
||||
<div className="flex justify-between items-center mb-3 px-2">
|
||||
<span className="text-xs text-gray-600 font-medium">
|
||||
{byteSize}/{maxBytes}byte
|
||||
</span>
|
||||
<span className={`${messageTypeColor} text-white px-3 py-1 rounded-md text-xs font-bold`}>
|
||||
{messageType}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* 메시지 본문 (textarea) */}
|
||||
<div className="flex-1 bg-white rounded-2xl shadow-sm overflow-hidden flex flex-col">
|
||||
{/* LMS 제목 */}
|
||||
{smsTitle && (
|
||||
<div className="px-4 pt-3 pb-2">
|
||||
<div className="font-bold text-sm text-gray-800">
|
||||
{smsTitle}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Textarea - 에디터 */}
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
className="flex-1 w-full p-4 text-sm text-gray-700 outline-none resize-none bg-transparent leading-relaxed"
|
||||
value={text}
|
||||
onChange={handleTextChange}
|
||||
onBlur={handleBlur}
|
||||
placeholder={placeholder}
|
||||
style={{ minHeight: smsTitle ? '320px' : '360px' }}
|
||||
{...ti.bindSelectionHandlers}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 바이트 초과 경고 */}
|
||||
{byteSize > maxBytes && (
|
||||
<div className="mt-2 px-2">
|
||||
<p className="text-xs text-red-600 font-medium text-center">
|
||||
⚠️ 최대 용량 초과 ({byteSize - maxBytes}byte)
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 하단 홈 버튼 */}
|
||||
<div className="absolute bottom-2 left-1/2 -translate-x-1/2 w-32 h-1 bg-gray-800 rounded-full" />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user