260115_01_ENH : SMS 에디터 수정 및 이미지 리로드 관련 버그 수정
This commit is contained in:
@@ -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 <KriEditorHtmlOnly {...props} mode={mode} editorMode={mode} />;
|
||||
}
|
||||
|
||||
if(mode === "sms") {
|
||||
return <KriEditorSms {...props} mode={mode} editorMode={mode} />
|
||||
}
|
||||
|
||||
return <KriEditorWysiwyg {...props} mode={mode} editorMode={mode} />;
|
||||
}
|
||||
|
||||
@@ -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<KriEditorProps, "mode"> & {
|
||||
mode: "html";
|
||||
@@ -38,19 +40,11 @@ export function KriEditorHtmlOnly(props: HtmlOnlyProps) {
|
||||
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null);
|
||||
const fileRef = React.useRef<HTMLInputElement | null>(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<HTMLTextAreaElement>) => {
|
||||
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 (
|
||||
<div className={cn("rounded-md border bg-white", className)}>
|
||||
{label && (
|
||||
@@ -186,6 +220,7 @@ export function KriEditorHtmlOnly(props: HtmlOnlyProps) {
|
||||
onBlur={handleBlur}
|
||||
placeholder={placeholder}
|
||||
rows={rows}
|
||||
{...ti.bindSelectionHandlers}
|
||||
/>
|
||||
|
||||
<input
|
||||
|
||||
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>
|
||||
);
|
||||
};
|
||||
@@ -18,15 +18,16 @@ import TableHeader from "@tiptap/extension-table-header";
|
||||
import TableCell from "@tiptap/extension-table-cell";
|
||||
|
||||
import { toast } from "sonner";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
import type { KriEditorProps, KriEditorMode, ToolbarState } from "./types";
|
||||
import { FontSize } from "./extensions/font-size";
|
||||
import { Div } from "./extensions/div";
|
||||
import { cn } from "./utils/cn";
|
||||
import { compressImage } from "./utils/imageCompression";
|
||||
import { sanitizeHtml } from "./utils/sanitizeHtml";
|
||||
import { Toolbar } from "./toolbar/Toolbar";
|
||||
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
||||
import { useTextareaInsert } from "./hooks/useTextareaInsert";
|
||||
|
||||
type ViewMode = "wysiwyg" | "html";
|
||||
|
||||
@@ -65,6 +66,7 @@ function getToolbarState(editor: Editor): ToolbarState {
|
||||
|
||||
export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
const {
|
||||
|
||||
label,
|
||||
value,
|
||||
onChange,
|
||||
@@ -91,7 +93,6 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
} = props;
|
||||
|
||||
const mode = resolveMode(modeProp, editorMode);
|
||||
const isLmsMode = mode === "lms";
|
||||
const showHtmlToggle = mode === "both";
|
||||
|
||||
const LINK_HTML_ATTRS = {
|
||||
@@ -102,9 +103,13 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
const { scheduleChange, flushChange, optimisticHtmlRef } = useDebouncedChange(onChange, debounceMs);
|
||||
|
||||
const fileRef = useRef<HTMLInputElement | null>(null);
|
||||
const htmlTextareaRef = useRef<HTMLTextAreaElement | null>(null);
|
||||
const htmlTi = useTextareaInsert(htmlTextareaRef);
|
||||
|
||||
const [view, setView] = useState<ViewMode>("wysiwyg");
|
||||
const [wysiwygKey, setWysiwygKey] = useState<number>(0);
|
||||
const viewRef = useRef<ViewMode>("wysiwyg");
|
||||
|
||||
useEffect(() => {
|
||||
viewRef.current = view;
|
||||
}, [view]);
|
||||
@@ -133,26 +138,12 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
immediatelyRender: false,
|
||||
|
||||
extensions: [
|
||||
isLmsMode
|
||||
? StarterKit.configure({
|
||||
heading: false,
|
||||
bulletList: false,
|
||||
orderedList: false,
|
||||
listItem: false,
|
||||
blockquote: false,
|
||||
codeBlock: false,
|
||||
code: false,
|
||||
strike: false,
|
||||
bold: false,
|
||||
italic: false,
|
||||
horizontalRule: false,
|
||||
})
|
||||
: StarterKit.configure({
|
||||
heading: false,
|
||||
blockquote: false,
|
||||
codeBlock: false,
|
||||
code: false,
|
||||
}),
|
||||
StarterKit.configure({
|
||||
heading: false,
|
||||
blockquote: false,
|
||||
codeBlock: false,
|
||||
code: false,
|
||||
}),
|
||||
|
||||
Underline,
|
||||
TextStyle,
|
||||
@@ -183,6 +174,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
allowBase64,
|
||||
HTMLAttributes: {
|
||||
class: "max-w-full h-auto",
|
||||
"data-kri-image": "true",
|
||||
},
|
||||
resize: {
|
||||
enabled: true,
|
||||
@@ -259,52 +251,11 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
},[wysiwygKey]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
const sync = () => {
|
||||
selectionRef.current = { from: editor.state.selection.from, to: editor.state.selection.to };
|
||||
setTb(getToolbarState(editor));
|
||||
};
|
||||
|
||||
sync();
|
||||
editor.on("selectionUpdate", sync);
|
||||
editor.on("transaction", sync);
|
||||
|
||||
return () => {
|
||||
editor.off("selectionUpdate", sync);
|
||||
editor.off("transaction", sync);
|
||||
};
|
||||
}, [editor]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
if (viewRef.current === "html") return;
|
||||
|
||||
const current = editor.getHTML();
|
||||
if (value != null && value !== current && value !== optimisticHtmlRef.current) {
|
||||
editor.commands.setContent(value, {
|
||||
emitUpdate: false,
|
||||
parseOptions: { preserveWhitespace: "full" },
|
||||
});
|
||||
}
|
||||
}, [value, editor, optimisticHtmlRef]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
const onBlur = () => {
|
||||
if (viewRef.current === "html") flushChange(htmlDraftRef.current);
|
||||
else flushChange(editor.getHTML());
|
||||
};
|
||||
|
||||
editor.on("blur", onBlur);
|
||||
return () => {
|
||||
editor.off("blur", onBlur);
|
||||
};
|
||||
}, [editor, flushChange]);
|
||||
console.log(wysiwygKey)
|
||||
}, [wysiwygKey]);
|
||||
|
||||
const restoreSelection = useCallback(() => {
|
||||
if (!editor) return;
|
||||
@@ -504,6 +455,118 @@ const normalizeLinkUrl = (raw: string): string => {
|
||||
setView("wysiwyg");
|
||||
}, [editor, flushChange, sanitizeHtmlOpt]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
const sync = () => {
|
||||
selectionRef.current = { from: editor.state.selection.from, to: editor.state.selection.to };
|
||||
setTb(getToolbarState(editor));
|
||||
};
|
||||
|
||||
sync();
|
||||
editor.on("selectionUpdate", sync);
|
||||
editor.on("transaction", sync);
|
||||
|
||||
return () => {
|
||||
editor.off("selectionUpdate", sync);
|
||||
editor.off("transaction", sync);
|
||||
};
|
||||
}, [editor]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.onEditorReady || !editor) return;
|
||||
|
||||
if (viewRef.current === "html") {
|
||||
props.onEditorReady(htmlTextareaRef.current);
|
||||
} else {
|
||||
props.onEditorReady(editor);
|
||||
}
|
||||
}, [editor, view, props.onEditorReady]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
if (viewRef.current === "html") return;
|
||||
if (value == null) return;
|
||||
|
||||
const current = editor.getHTML();
|
||||
|
||||
if (value !== current && value !== optimisticHtmlRef.current) {
|
||||
// 이미지가 포함된 HTML 업데이트면, 아예 editor를 새로 생성
|
||||
editor.commands.setContent(value, {
|
||||
emitUpdate: false,
|
||||
parseOptions: {preserveWhitespace: "full"},
|
||||
});
|
||||
}
|
||||
}, [value, editor, view]);
|
||||
|
||||
useEffect(() => {
|
||||
if (value.includes("<img")) {
|
||||
setWysiwygKey((k) => k + 1);
|
||||
return;
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
const onBlur = () => {
|
||||
if (viewRef.current === "html") flushChange(htmlDraftRef.current);
|
||||
else flushChange(editor.getHTML());
|
||||
};
|
||||
|
||||
editor.on("blur", onBlur);
|
||||
return () => {
|
||||
editor.off("blur", onBlur);
|
||||
};
|
||||
}, [editor, flushChange]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.onCommandReady) return;
|
||||
|
||||
// editor가 아직 없으면 null 전달
|
||||
if (!editor) {
|
||||
props.onCommandReady(null);
|
||||
return;
|
||||
}
|
||||
|
||||
props.onCommandReady({
|
||||
focus: () => {
|
||||
if (viewRef.current === "html") htmlTextareaRef.current?.focus();
|
||||
else editor.chain().focus().run();
|
||||
},
|
||||
|
||||
insertText: (placeholder: string) => {
|
||||
// 1) HTML textarea 모드
|
||||
if (viewRef.current === "html") {
|
||||
const current = htmlDraftRef.current ?? "";
|
||||
|
||||
htmlTi.insertText(
|
||||
placeholder,
|
||||
current,
|
||||
(next) => {
|
||||
htmlDraftRef.current = next;
|
||||
// uncontrolled textarea이므로 DOM value를 직접 갱신
|
||||
if (htmlTextareaRef.current) htmlTextareaRef.current.value = next;
|
||||
},
|
||||
(next) => {
|
||||
// 삽입은 즉시 커밋(저장 버튼 클릭 직후 반영 누락 방지)
|
||||
flushChange(next);
|
||||
}
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 2) WYSIWYG(tiptap) 모드
|
||||
restoreSelection();
|
||||
editor.chain().focus().insertContent(placeholder).run();
|
||||
flushChange(editor.getHTML());
|
||||
},
|
||||
});
|
||||
|
||||
return () => props.onCommandReady?.(null);
|
||||
}, [editor, props.onCommandReady, restoreSelection, flushChange, htmlTi]);
|
||||
|
||||
const previewHtml = useMemo(() => {
|
||||
const raw = optimisticHtmlRef.current || "";
|
||||
return sanitizeHtmlOpt ? sanitizeHtml(raw) : raw;
|
||||
@@ -532,7 +595,6 @@ const normalizeLinkUrl = (raw: string): string => {
|
||||
showHtmlToggle={showHtmlToggle}
|
||||
isHtmlView={view === "html"}
|
||||
onToggleHtmlView={onToggleHtmlView}
|
||||
isLmsMode={isLmsMode}
|
||||
onSetFontSize={setFontSize}
|
||||
onSetTextColor={setTextColor}
|
||||
onUnsetTextColor={unsetTextColor}
|
||||
@@ -553,6 +615,7 @@ const normalizeLinkUrl = (raw: string): string => {
|
||||
) : (
|
||||
<div className="p-2">
|
||||
<textarea
|
||||
ref={htmlTextareaRef}
|
||||
className="h-[220px] w-full resize-y rounded-md border bg-background p-3 font-mono text-sm outline-none"
|
||||
defaultValue={htmlDraftRef.current}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
@@ -562,6 +625,7 @@ const normalizeLinkUrl = (raw: string): string => {
|
||||
}}
|
||||
onBlur={() => flushChange(htmlDraftRef.current)}
|
||||
placeholder={placeholder}
|
||||
{...htmlTi.bindSelectionHandlers}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
61
src/components/kri-editor/hooks/useTextareaInsert.ts
Normal file
61
src/components/kri-editor/hooks/useTextareaInsert.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import * as React from "react";
|
||||
|
||||
type Selection = { start: number; end: number };
|
||||
|
||||
export function useTextareaInsert(textareaRef: React.RefObject<HTMLTextAreaElement | null>) {
|
||||
const selectionRef = React.useRef<Selection>({start: 0, end: 0});
|
||||
|
||||
const saveSelection = React.useCallback(() => {
|
||||
const el = textareaRef.current;
|
||||
if (!el) return;
|
||||
selectionRef.current = {
|
||||
start: el.selectionStart ?? 0,
|
||||
end: el.selectionEnd ?? 0,
|
||||
};
|
||||
}, [textareaRef]);
|
||||
|
||||
const bindSelectionHandlers = React.useMemo(
|
||||
() => ({
|
||||
onSelect: saveSelection,
|
||||
onKeyUp: saveSelection,
|
||||
onMouseUp: saveSelection,
|
||||
onClick: saveSelection,
|
||||
onFocus: saveSelection,
|
||||
}),
|
||||
[saveSelection],
|
||||
);
|
||||
|
||||
/**
|
||||
* 실제 삽입/치환 로직 (컴포넌트에서 currentText를 제공해야 함)
|
||||
* - apply(nextText): 로컬 state 반영 + latestRef 반영
|
||||
* - commit(nextText): 부모 onChange 즉시 반영(가능하면 flushChange 사용 권장)
|
||||
*/
|
||||
const insertText = React.useCallback(
|
||||
(placeholder: string, currentText: string, apply: (next: string) => void, commit?: (next: string) => void) => {
|
||||
const {start, end} = selectionRef.current;
|
||||
|
||||
const next = currentText.slice(0, start) + placeholder + currentText.slice(end);
|
||||
const nextPos = start + placeholder.length;
|
||||
|
||||
apply(next);
|
||||
commit?.(next);
|
||||
|
||||
// 렌더 반영 후 커서 복원
|
||||
requestAnimationFrame(() => {
|
||||
const el = textareaRef.current;
|
||||
if (!el) return;
|
||||
el.focus();
|
||||
el.setSelectionRange(nextPos, nextPos);
|
||||
selectionRef.current = {start: nextPos, end: nextPos};
|
||||
});
|
||||
},
|
||||
[textareaRef],
|
||||
);
|
||||
|
||||
return {
|
||||
selectionRef,
|
||||
saveSelection,
|
||||
bindSelectionHandlers,
|
||||
insertText,
|
||||
};
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
export { KriEditor } from "./KriEditor";
|
||||
export type { KriEditorProps, KriEditorMode, UploadResult, ToolbarState } from "./types";
|
||||
export type { KriEditorProps, KriEditorMode, UploadResult, ToolbarState, KriEditorCommand } from "./types";
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn } from "../utils/cn";
|
||||
import {
|
||||
AlignCenter,
|
||||
AlignLeft,
|
||||
@@ -43,8 +43,6 @@ type Props = {
|
||||
isHtmlView: boolean;
|
||||
onToggleHtmlView: () => void;
|
||||
|
||||
isLmsMode: boolean;
|
||||
|
||||
onSetFontSize: (px: string) => void;
|
||||
|
||||
onSetTextColor: (hex: string) => void;
|
||||
@@ -97,7 +95,6 @@ export function Toolbar({
|
||||
showHtmlToggle,
|
||||
isHtmlView,
|
||||
onToggleHtmlView,
|
||||
isLmsMode,
|
||||
onSetFontSize,
|
||||
onSetTextColor,
|
||||
onUnsetTextColor,
|
||||
@@ -109,16 +106,6 @@ export function Toolbar({
|
||||
onAddColumn,
|
||||
onDeleteTable,
|
||||
}: Props) {
|
||||
if (isLmsMode) {
|
||||
return (
|
||||
<div className={cn("flex items-center gap-1 whitespace-nowrap px-2 py-1", className)}>
|
||||
<ToolbarButton active={state.isLink} title={state.isLink ? "링크 해제" : "링크 삽입"} onClick={onToggleLink}>
|
||||
<Link2 className="h-4 w-4" />
|
||||
</ToolbarButton>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn("flex items-center gap-1 whitespace-nowrap px-2 py-1", className)}>
|
||||
<select
|
||||
|
||||
@@ -1,8 +1,19 @@
|
||||
import {Editor} from "@tiptap/react";
|
||||
|
||||
export type UploadResult = { url: string };
|
||||
|
||||
export type KriEditorMode = "wysiwyg" | "both" | "html" | "lms";
|
||||
export type KriEditorMode = "wysiwyg" | "both" | "html" | "sms";
|
||||
|
||||
export type KriEditorCommand = {
|
||||
focus: () => void;
|
||||
insertText: (text: string) => void; // placeholder/merge-field 삽입
|
||||
};
|
||||
|
||||
export type KriEditorProps = {
|
||||
onEditorReady?: (editor: Editor | HTMLTextAreaElement | null) => void;
|
||||
|
||||
onCommandReady?: (cmd: KriEditorCommand | null) => void;
|
||||
|
||||
label?: string;
|
||||
|
||||
value: string;
|
||||
@@ -19,7 +30,7 @@ export type KriEditorProps = {
|
||||
* - "wysiwyg": WYSIWYG 전용
|
||||
* - "html": HTML 전용
|
||||
* - "both": 툴바에서 WYSIWYG / HTML 전환 가능
|
||||
* - "lms": LMS 문자 전용 (링크만)
|
||||
* - "sms": SMS 문자 전용 (링크만)
|
||||
*
|
||||
* props.mode 가 없으면 editorMode 를 참고하고,
|
||||
* 둘 다 없으면 "wysiwyg" 으로 동작.
|
||||
@@ -51,6 +62,10 @@ export type KriEditorProps = {
|
||||
|
||||
// onChange 디바운스 시간 (ms)
|
||||
debounceMs?: number;
|
||||
|
||||
// SMS 모드 전용 props
|
||||
smsTitle?: string; // LMS 제목용
|
||||
changeSmsType?: (type: "SMS" | "LMS") => void;
|
||||
};
|
||||
|
||||
export type ToolbarState = {
|
||||
|
||||
3
src/components/kri-editor/utils/cn.ts
Normal file
3
src/components/kri-editor/utils/cn.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export function cn(...inputs: Array<string | undefined | null | false>) {
|
||||
return inputs.filter(Boolean).join(" ");
|
||||
}
|
||||
9
src/components/kri-editor/utils/editorTypeGuards.ts
Normal file
9
src/components/kri-editor/utils/editorTypeGuards.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { Editor } from '@tiptap/react';
|
||||
|
||||
export function isTipTapEditor(editor: any): editor is Editor {
|
||||
return editor && typeof editor.chain === 'function';
|
||||
}
|
||||
|
||||
export function isTextAreaElement(element: any): element is HTMLTextAreaElement {
|
||||
return element instanceof HTMLTextAreaElement;
|
||||
}
|
||||
Reference in New Issue
Block a user