From 7e7b4943898105563e12ea843cd17dcea37d9438 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Thu, 15 Jan 2026 11:34:52 +0900 Subject: [PATCH] =?UTF-8?q?260115=5F01=5FENH=20:=20SMS=20=EC=97=90?= =?UTF-8?q?=EB=94=94=ED=84=B0=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EB=A6=AC=EB=A1=9C=EB=93=9C=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/kri-editor/KriEditor.tsx | 5 + .../kri-editor/KriEditorHtmlOnly.tsx | 59 ++++-- src/components/kri-editor/KriEditorSms.tsx | 195 +++++++++++++++++ .../kri-editor/KriEditorWysiwyg.tsx | 198 ++++++++++++------ .../kri-editor/hooks/useTextareaInsert.ts | 61 ++++++ src/components/kri-editor/index.ts | 2 +- src/components/kri-editor/toolbar/Toolbar.tsx | 15 +- src/components/kri-editor/types.ts | 19 +- src/components/kri-editor/utils/cn.ts | 3 + .../kri-editor/utils/editorTypeGuards.ts | 9 + 10 files changed, 470 insertions(+), 96 deletions(-) create mode 100644 src/components/kri-editor/KriEditorSms.tsx create mode 100644 src/components/kri-editor/hooks/useTextareaInsert.ts create mode 100644 src/components/kri-editor/utils/cn.ts create mode 100644 src/components/kri-editor/utils/editorTypeGuards.ts diff --git a/src/components/kri-editor/KriEditor.tsx b/src/components/kri-editor/KriEditor.tsx index 70cd21b..5c90c0a 100644 --- a/src/components/kri-editor/KriEditor.tsx +++ b/src/components/kri-editor/KriEditor.tsx @@ -3,6 +3,7 @@ import type { KriEditorProps, KriEditorMode } from "./types"; import { KriEditorHtmlOnly } from "./KriEditorHtmlOnly"; import { KriEditorWysiwyg } from "./KriEditorWysiwyg"; +import {KriEditorSms} from "./KriEditorSms"; function resolveMode(mode?: KriEditorMode, editorMode?: KriEditorMode): KriEditorMode { return mode ?? editorMode ?? "wysiwyg"; @@ -15,5 +16,9 @@ export function KriEditor(props: KriEditorProps) { return ; } + if(mode === "sms") { + return + } + return ; } diff --git a/src/components/kri-editor/KriEditorHtmlOnly.tsx b/src/components/kri-editor/KriEditorHtmlOnly.tsx index 64f77cc..21f4785 100644 --- a/src/components/kri-editor/KriEditorHtmlOnly.tsx +++ b/src/components/kri-editor/KriEditorHtmlOnly.tsx @@ -1,13 +1,15 @@ "use client"; import * as React from "react"; -import { cn } from "@/lib/utils"; +import { toast } from "sonner"; + import type { KriEditorProps } from "./types"; +import { cn } from "./utils/cn"; +import { compressImage } from "./utils/imageCompression"; import { useDebouncedChange } from "./hooks/useDebouncedChange"; import { sanitizeHtmlForPreview } from "./utils/sanitizeHtml"; import {useCallback} from "react"; -import {toast} from "sonner"; -import {compressImage} from "@/components/kri-editor/utils/imageCompression"; +import { useTextareaInsert } from "./hooks/useTextareaInsert"; type HtmlOnlyProps = Omit & { mode: "html"; @@ -38,19 +40,11 @@ export function KriEditorHtmlOnly(props: HtmlOnlyProps) { const textareaRef = React.useRef(null); const fileRef = React.useRef(null); const latestHtmlRef = React.useRef(html); - latestHtmlRef.current = html; const canImageUpload = Boolean(onImageUpload); const { scheduleChange, flushChange } = useDebouncedChange(onChange, debounceMs); - - // 외부 value 변경 시 동기화 - React.useEffect(() => { - if ((value || "") !== latestHtmlRef.current) { - setHtml(value || ""); - latestHtmlRef.current = value || ""; - } - }, [value]); + const ti = useTextareaInsert(textareaRef); const handleTextChange = (e: React.ChangeEvent) => { const next = e.target.value; @@ -156,6 +150,46 @@ export function KriEditorHtmlOnly(props: HtmlOnlyProps) { [html], ); + // 외부 value 변경 시 동기화 + React.useEffect(() => { + if ((value || "") !== latestHtmlRef.current) { + setHtml(value || ""); + latestHtmlRef.current = value || ""; + } + }, [value]); + + React.useEffect(() => { + if (textareaRef.current && props.onEditorReady) { + props.onEditorReady(textareaRef.current); + } + },[props.onEditorReady]) + + React.useEffect(() => { + if (!props.onCommandReady) return; + + props.onCommandReady({ + focus: () => textareaRef.current?.focus(), + insertText: (placeholder: string) => { + const current = latestHtmlRef.current ?? ""; + + ti.insertText( + placeholder, + current, + (next) => { + setHtml(next); + latestHtmlRef.current = next; + }, + (next) => { + // html 모드도 debounce 쓰면 flush 권장 + flushChange(next); + }, + ); + }, + }); + + return () => props.onCommandReady?.(null); + }, [props.onCommandReady, ti, flushChange]); + return (
{label && ( @@ -186,6 +220,7 @@ export function KriEditorHtmlOnly(props: HtmlOnlyProps) { onBlur={handleBlur} placeholder={placeholder} rows={rows} + {...ti.bindSelectionHandlers} /> & { + mode: "sms"; +}; + +export function KriEditorSms(props: SmsEditorProps) { + const { + label, + value, + onChange, + placeholder = "문자 내용을 입력하세요...", + className, + debounceMs = 350, + smsTitle = "", + changeSmsType, + onEditorReady, + } = props; + + const [text, setText] = React.useState(value || ""); + const [byteSize, setByteSize] = React.useState(0); + const textareaRef = React.useRef(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) => { + 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 ( +
+ {label && ( +
+ {label} +
+ )} + + {/* 휴대전화 디자인 */} +
+ {/* 상단 노치 */} +
+ + {/* 스크린 영역 */} +
+ {/* 헤더 - 바이트 및 타입 표시 */} +
+ + {byteSize}/{maxBytes}byte + + + {messageType} + +
+ + {/* 메시지 본문 (textarea) */} +
+ {/* LMS 제목 */} + {smsTitle && ( +
+
+ {smsTitle} +
+
+ )} + + {/* Textarea - 에디터 */} +