251212_01_ENH : kri-editor 1차 완성, 프로젝트 정보 저장 api연동 완료
This commit is contained in:
164
src/components/kri-editor/KriEditorHtmlOnly.tsx
Normal file
164
src/components/kri-editor/KriEditorHtmlOnly.tsx
Normal file
@@ -0,0 +1,164 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { KriEditorProps } from "./types";
|
||||
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
||||
import { sanitizeHtmlForPreview } from "./utils/sanitizeHtml";
|
||||
|
||||
type HtmlOnlyProps = Omit<KriEditorProps, "mode"> & {
|
||||
mode: "html";
|
||||
};
|
||||
|
||||
export function KriEditorHtmlOnly(props: HtmlOnlyProps) {
|
||||
const {
|
||||
label,
|
||||
value,
|
||||
onChange,
|
||||
placeholder = "HTML을 입력하세요...",
|
||||
className,
|
||||
showPreview,
|
||||
previewClassName,
|
||||
rows = 14,
|
||||
onImageUpload,
|
||||
onImageUploadError,
|
||||
debounceMs = 350,
|
||||
} = props;
|
||||
|
||||
const [html, setHtml] = React.useState<string>(value || "");
|
||||
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null);
|
||||
const fileInputRef = React.useRef<HTMLInputElement | null>(null);
|
||||
const latestHtmlRef = React.useRef(html);
|
||||
latestHtmlRef.current = html;
|
||||
|
||||
const flushChange = useDebouncedChange(onChange, debounceMs);
|
||||
|
||||
// 외부 value 변경 시 동기화
|
||||
React.useEffect(() => {
|
||||
if ((value || "") !== latestHtmlRef.current) {
|
||||
setHtml(value || "");
|
||||
latestHtmlRef.current = value || "";
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
const handleTextChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
const next = e.target.value;
|
||||
setHtml(next);
|
||||
latestHtmlRef.current = next;
|
||||
flushChange(next);
|
||||
};
|
||||
|
||||
const handleBlur = () => {
|
||||
flushChange(latestHtmlRef.current);
|
||||
};
|
||||
|
||||
const handleClickImageButton = () => {
|
||||
if (!onImageUpload) return;
|
||||
fileInputRef.current?.click();
|
||||
};
|
||||
|
||||
const handleFileChange = async (
|
||||
e: React.ChangeEvent<HTMLInputElement>,
|
||||
) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file || !onImageUpload) return;
|
||||
|
||||
try {
|
||||
const url = await onImageUpload(file);
|
||||
if (!url) return;
|
||||
|
||||
setHtml((prev) => {
|
||||
const current = prev ?? "";
|
||||
const textarea = textareaRef.current;
|
||||
const imgTag = `<img src="${url}" alt="" />`;
|
||||
|
||||
if (!textarea) {
|
||||
const next = current + imgTag;
|
||||
latestHtmlRef.current = next;
|
||||
flushChange(next);
|
||||
return next;
|
||||
}
|
||||
|
||||
const { selectionStart, selectionEnd } = textarea;
|
||||
const before = current.slice(0, selectionStart);
|
||||
const after = current.slice(selectionEnd);
|
||||
const next = before + imgTag + after;
|
||||
|
||||
latestHtmlRef.current = next;
|
||||
flushChange(next);
|
||||
|
||||
// 커서 위치를 이미지 태그 뒤로 이동
|
||||
window.requestAnimationFrame(() => {
|
||||
const pos = before.length + imgTag.length;
|
||||
textarea.focus();
|
||||
textarea.setSelectionRange(pos, pos);
|
||||
});
|
||||
|
||||
return next;
|
||||
});
|
||||
} catch (error) {
|
||||
onImageUploadError?.(error);
|
||||
} finally {
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = "";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const sanitizedHtml = React.useMemo(
|
||||
() => sanitizeHtmlForPreview(html || ""),
|
||||
[html],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={cn("rounded-md border bg-white", className)}>
|
||||
{label && (
|
||||
<div className="border-b px-3 py-2 text-sm font-semibold">
|
||||
{label}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex flex-col gap-2 px-3 py-2">
|
||||
<div className="flex items-center justify-between text-xs text-gray-500">
|
||||
<span>HTML 모드</span>
|
||||
{onImageUpload && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClickImageButton}
|
||||
className="rounded border px-2 py-1 text-xs hover:bg-gray-50"
|
||||
>
|
||||
이미지 업로드
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
className="min-h-[200px] w-full resize-y border bg-white p-2 text-sm font-mono outline-none"
|
||||
value={html}
|
||||
onChange={handleTextChange}
|
||||
onBlur={handleBlur}
|
||||
placeholder={placeholder}
|
||||
rows={rows}
|
||||
/>
|
||||
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
className="hidden"
|
||||
onChange={handleFileChange}
|
||||
/>
|
||||
</div>
|
||||
{showPreview ?? (
|
||||
<div className={cn("border-t p-3", previewClassName)}>
|
||||
<div className="mb-2 text-sm font-semibold">미리보기</div>
|
||||
<div className="rounded-md border bg-gray-50 p-3 text-sm leading-relaxed">
|
||||
{/* eslint-disable-next-line react/no-danger */}
|
||||
<div dangerouslySetInnerHTML={{ __html: sanitizedHtml }} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user