'use client' import * as React from 'react' import { toast } from 'sonner' import { cn } from '@/lib/utils' import type { KriEditorProps } from './types' import { resolveUploadUrl } from './types' import { compressImage } from './utils/imageCompression' import { useDebouncedChange } from './hooks/useDebouncedChange' import { sanitizeHtmlForPreview } from './utils/sanitizeHtml' import { useTextareaInsert } from './hooks/useTextareaInsert' type HtmlOnlyProps = Omit & { mode: 'html' } export function KriEditorHtmlOnly({ label, value, onChange, placeholder = 'HTML을 입력하세요...', className, showPreview, previewClassName, rows = 14, debounceMs = 350, onImageUpload, onImageUploadError, maxImageBytes = 5 * 1024 * 1024, imageMaxWidth = 1600, imageMaxHeight = 1600, imageQuality = 0.82, imageMimeType = 'image/webp', onEditorReady, onCommandReady, }: HtmlOnlyProps) { const [html, setHtml] = React.useState(value || '') const textareaRef = React.useRef(null) const fileRef = React.useRef(null) const latestHtmlRef = React.useRef(html) const { scheduleChange, flushChange } = useDebouncedChange(onChange, debounceMs) const textareaInsert = useTextareaInsert(textareaRef) const commitHtml = React.useCallback((next: string, immediate = false) => { setHtml(next) latestHtmlRef.current = next if (immediate) flushChange(next) else scheduleChange(next) }, [flushChange, scheduleChange]) React.useEffect(() => { if ((value || '') !== latestHtmlRef.current) { setHtml(value || '') latestHtmlRef.current = value || '' } }, [value]) React.useEffect(() => { onEditorReady?.(textareaRef.current) }, [onEditorReady]) React.useEffect(() => { if (!onCommandReady) return onCommandReady({ focus: () => textareaRef.current?.focus(), insertText: (text) => { textareaInsert.insertText( text, latestHtmlRef.current, (next) => commitHtml(next), (next) => commitHtml(next, true), ) }, }) return () => onCommandReady(null) }, [commitHtml, onCommandReady, textareaInsert]) const handleFile = async (event: React.ChangeEvent) => { try { const file = event.target.files?.[0] event.target.value = '' if (!file || !onImageUpload) return if (!file.type.startsWith('image/')) { toast.error('이미지 파일만 업로드할 수 있습니다.') return } const compressed = await compressImage(file, { maxBytes: maxImageBytes, maxWidth: imageMaxWidth, maxHeight: imageMaxHeight, mimeType: imageMimeType, quality: imageQuality, }) if (compressed.size > maxImageBytes) { toast.error('이미지 용량이 제한을 초과합니다.') return } const url = resolveUploadUrl(await onImageUpload(compressed)) if (!url) { toast.error('이미지 업로드 응답이 올바르지 않습니다.') return } const current = latestHtmlRef.current const textarea = textareaRef.current const imageTag = `` const start = textarea?.selectionStart ?? current.length const end = textarea?.selectionEnd ?? current.length const next = `${current.slice(0, start)}${imageTag}${current.slice(end)}` commitHtml(next) requestAnimationFrame(() => { const position = start + imageTag.length textarea?.focus() textarea?.setSelectionRange(position, position) }) } catch (error) { if (onImageUploadError) onImageUploadError(error) else toast.error('이미지 업로드에 실패했습니다.') } } const previewHtml = React.useMemo(() => sanitizeHtmlForPreview(html), [html]) return (
{label ?
{label}
: null}
HTML 모드 {onImageUpload ? ( ) : null}