260714_01_ENH: 테스트 서버에서 기동 후 중간 테스트를 위한 커밋
This commit is contained in:
@@ -1,195 +1,119 @@
|
||||
"use client";
|
||||
'use client'
|
||||
|
||||
import React from "react";
|
||||
import { cn } from "@kri/common/ui";
|
||||
import type { KriEditorProps } from "./types";
|
||||
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
||||
import { useTextareaInsert } from "./hooks/useTextareaInsert";
|
||||
import * as React from 'react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import type { KriEditorProps } from './types'
|
||||
import { useDebouncedChange } from './hooks/useDebouncedChange'
|
||||
import { useTextareaInsert } from './hooks/useTextareaInsert'
|
||||
|
||||
type SmsEditorProps = Omit<KriEditorProps, "mode"> & {
|
||||
mode: "sms";
|
||||
};
|
||||
type SmsEditorProps = Omit<KriEditorProps, 'mode'> & { mode: 'sms' }
|
||||
|
||||
export function KriEditorSms(props: SmsEditorProps) {
|
||||
const {
|
||||
label,
|
||||
value,
|
||||
onChange,
|
||||
placeholder = "문자 내용을 입력하세요...",
|
||||
className,
|
||||
debounceMs = 350,
|
||||
smsTitle = "",
|
||||
changeSmsType,
|
||||
onEditorReady,
|
||||
} = props;
|
||||
function calculateBytes(value: string): number {
|
||||
return Array.from(value).reduce((total, character) => total + (character.charCodeAt(0) > 127 ? 2 : 1), 0)
|
||||
}
|
||||
|
||||
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);
|
||||
export function KriEditorSms({
|
||||
label,
|
||||
value,
|
||||
onChange,
|
||||
placeholder = '문자 내용을 입력하세요...',
|
||||
className,
|
||||
debounceMs = 350,
|
||||
smsTitle = '',
|
||||
changeSmsType,
|
||||
onEditorReady,
|
||||
onCommandReady,
|
||||
smsMaxBytes = 90,
|
||||
lmsMaxBytes = 2000,
|
||||
isSmsReadOnly = false,
|
||||
}: SmsEditorProps) {
|
||||
const [text, setText] = React.useState(value || '')
|
||||
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 textareaInsert = useTextareaInsert(textareaRef)
|
||||
const byteSize = React.useMemo(() => calculateBytes(text), [text])
|
||||
const messageType: 'SMS' | 'LMS' = byteSize > smsMaxBytes || Boolean(smsTitle) ? 'LMS' : 'SMS'
|
||||
const maxBytes = messageType === 'SMS' ? smsMaxBytes : lmsMaxBytes
|
||||
|
||||
const { scheduleChange, flushChange } = useDebouncedChange(onChange, debounceMs);
|
||||
const ti = useTextareaInsert(textareaRef);
|
||||
const commitText = React.useCallback((next: string, immediate = false) => {
|
||||
setText(next)
|
||||
latestTextRef.current = next
|
||||
if (immediate) flushChange(next)
|
||||
else scheduleChange(next)
|
||||
}, [flushChange, scheduleChange])
|
||||
|
||||
// 한글은 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;
|
||||
}
|
||||
React.useEffect(() => {
|
||||
if ((value || '') !== latestTextRef.current) {
|
||||
setText(value || '')
|
||||
latestTextRef.current = value || ''
|
||||
}
|
||||
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]);
|
||||
}, [value])
|
||||
|
||||
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]);
|
||||
if (lastTypeRef.current === messageType) return
|
||||
lastTypeRef.current = messageType
|
||||
changeSmsType?.(messageType)
|
||||
}, [changeSmsType, messageType])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!props.onCommandReady) return;
|
||||
onEditorReady?.(textareaRef.current)
|
||||
}, [onEditorReady])
|
||||
|
||||
props.onCommandReady({
|
||||
React.useEffect(() => {
|
||||
if (!onCommandReady) return
|
||||
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);
|
||||
},
|
||||
);
|
||||
insertText: (insertedText) => {
|
||||
textareaInsert.insertText(
|
||||
insertedText,
|
||||
latestTextRef.current,
|
||||
(next) => commitText(next),
|
||||
(next) => commitText(next, true),
|
||||
)
|
||||
},
|
||||
});
|
||||
|
||||
return () => props.onCommandReady?.(null);
|
||||
}, [props.onCommandReady, ti, calculateBytes, flushChange]);
|
||||
})
|
||||
return () => onCommandReady(null)
|
||||
}, [commitText, onCommandReady, textareaInsert])
|
||||
|
||||
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`}>
|
||||
<div className={cn('mx-auto w-full max-w-[320px]', className)}>
|
||||
{label ? <div className="mb-2 text-sm font-semibold">{label}</div> : null}
|
||||
<div className="relative h-[600px] w-full overflow-hidden rounded-[40px] border-8 border-gray-800 bg-white shadow-2xl">
|
||||
<div className="absolute top-0 left-1/2 z-10 h-6 w-32 -translate-x-1/2 rounded-b-2xl bg-gray-800" />
|
||||
<div className="flex h-full flex-col bg-gray-50 px-4 pt-8 pb-4">
|
||||
<div className="mb-3 flex items-center justify-between px-2">
|
||||
<span className="text-xs font-medium text-gray-600">{byteSize}/{maxBytes}byte</span>
|
||||
<span className={cn('rounded-md px-3 py-1 text-xs font-bold text-white', messageType === 'LMS' ? 'bg-blue-500' : 'bg-red-500')}>
|
||||
{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 className="flex flex-1 flex-col overflow-hidden rounded-2xl bg-white shadow-sm">
|
||||
{smsTitle ? <div className="px-4 pt-3 pb-2 text-sm font-bold text-gray-800">{smsTitle}</div> : null}
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
className="w-full flex-1 resize-none bg-transparent p-4 text-sm leading-relaxed text-gray-700 outline-none"
|
||||
value={text}
|
||||
readOnly={isSmsReadOnly}
|
||||
onChange={(event) => {
|
||||
const next = event.target.value
|
||||
if (calculateBytes(next) <= lmsMaxBytes) commitText(next)
|
||||
}}
|
||||
onBlur={() => flushChange(latestTextRef.current)}
|
||||
placeholder={placeholder}
|
||||
style={{ minHeight: smsTitle ? '320px' : '360px' }}
|
||||
{...textareaInsert.bindSelectionHandlers}
|
||||
/>
|
||||
</div>
|
||||
{byteSize > maxBytes ? (
|
||||
<p className="mt-2 px-2 text-center text-xs font-medium text-red-600">
|
||||
최대 용량을 {byteSize - maxBytes}byte 초과했습니다.
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="absolute bottom-2 left-1/2 h-1 w-32 -translate-x-1/2 rounded-full bg-gray-800" />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user